| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup genericconsole
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /**
|
|---|
| 34 | * @file kconsole.c
|
|---|
| 35 | * @brief Kernel console.
|
|---|
| 36 | *
|
|---|
| 37 | * This file contains kernel thread managing the kernel console.
|
|---|
| 38 | *
|
|---|
| 39 | */
|
|---|
| 40 |
|
|---|
| 41 | #include <console/kconsole.h>
|
|---|
| 42 | #include <console/console.h>
|
|---|
| 43 | #include <console/chardev.h>
|
|---|
| 44 | #include <console/cmd.h>
|
|---|
| 45 | #include <print.h>
|
|---|
| 46 | #include <panic.h>
|
|---|
| 47 | #include <typedefs.h>
|
|---|
| 48 | #include <adt/list.h>
|
|---|
| 49 | #include <arch.h>
|
|---|
| 50 | #include <macros.h>
|
|---|
| 51 | #include <debug.h>
|
|---|
| 52 | #include <func.h>
|
|---|
| 53 | #include <str.h>
|
|---|
| 54 | #include <macros.h>
|
|---|
| 55 | #include <sysinfo/sysinfo.h>
|
|---|
| 56 | #include <ddi/device.h>
|
|---|
| 57 | #include <symtab.h>
|
|---|
| 58 | #include <errno.h>
|
|---|
| 59 | #include <putchar.h>
|
|---|
| 60 | #include <str.h>
|
|---|
| 61 |
|
|---|
| 62 | /** Simple kernel console.
|
|---|
| 63 | *
|
|---|
| 64 | * The console is realized by kernel thread kconsole.
|
|---|
| 65 | * It doesn't understand any useful command on its own,
|
|---|
| 66 | * but makes it possible for other kernel subsystems to
|
|---|
| 67 | * register their own commands.
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | /** Locking.
|
|---|
| 71 | *
|
|---|
| 72 | * There is a list of cmd_info_t structures. This list
|
|---|
| 73 | * is protected by cmd_lock spinlock. Note that specially
|
|---|
| 74 | * the link elements of cmd_info_t are protected by
|
|---|
| 75 | * this lock.
|
|---|
| 76 | *
|
|---|
| 77 | * Each cmd_info_t also has its own lock, which protects
|
|---|
| 78 | * all elements thereof except the link element.
|
|---|
| 79 | *
|
|---|
| 80 | * cmd_lock must be acquired before any cmd_info lock.
|
|---|
| 81 | * When locking two cmd info structures, structure with
|
|---|
| 82 | * lower address must be locked first.
|
|---|
| 83 | */
|
|---|
| 84 |
|
|---|
| 85 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
|
|---|
| 86 | LIST_INITIALIZE(cmd_list); /**< Command list. */
|
|---|
| 87 |
|
|---|
| 88 | static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
|
|---|
| 89 | static size_t history_pos = 0;
|
|---|
| 90 |
|
|---|
| 91 | /** Initialize kconsole data structures
|
|---|
| 92 | *
|
|---|
| 93 | * This is the most basic initialization, almost no
|
|---|
| 94 | * other kernel subsystem is ready yet.
|
|---|
| 95 | *
|
|---|
| 96 | */
|
|---|
| 97 | void kconsole_init(void)
|
|---|
| 98 | {
|
|---|
| 99 | unsigned int i;
|
|---|
| 100 |
|
|---|
| 101 | cmd_init();
|
|---|
| 102 | for (i = 0; i < KCONSOLE_HISTORY; i++)
|
|---|
| 103 | history[i][0] = 0;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Register kconsole command.
|
|---|
| 107 | *
|
|---|
| 108 | * @param cmd Structure describing the command.
|
|---|
| 109 | *
|
|---|
| 110 | * @return False on failure, true on success.
|
|---|
| 111 | *
|
|---|
| 112 | */
|
|---|
| 113 | bool cmd_register(cmd_info_t *cmd)
|
|---|
| 114 | {
|
|---|
| 115 | spinlock_lock(&cmd_lock);
|
|---|
| 116 |
|
|---|
| 117 | /*
|
|---|
| 118 | * Make sure the command is not already listed.
|
|---|
| 119 | */
|
|---|
| 120 | list_foreach(cmd_list, cur) {
|
|---|
| 121 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
|
|---|
| 122 |
|
|---|
| 123 | if (hlp == cmd) {
|
|---|
| 124 | /* The command is already there. */
|
|---|
| 125 | spinlock_unlock(&cmd_lock);
|
|---|
| 126 | return false;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | /* Avoid deadlock. */
|
|---|
| 130 | if (hlp < cmd) {
|
|---|
| 131 | spinlock_lock(&hlp->lock);
|
|---|
| 132 | spinlock_lock(&cmd->lock);
|
|---|
| 133 | } else {
|
|---|
| 134 | spinlock_lock(&cmd->lock);
|
|---|
| 135 | spinlock_lock(&hlp->lock);
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | if (str_cmp(hlp->name, cmd->name) == 0) {
|
|---|
| 139 | /* The command is already there. */
|
|---|
| 140 | spinlock_unlock(&hlp->lock);
|
|---|
| 141 | spinlock_unlock(&cmd->lock);
|
|---|
| 142 | spinlock_unlock(&cmd_lock);
|
|---|
| 143 | return false;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | spinlock_unlock(&hlp->lock);
|
|---|
| 147 | spinlock_unlock(&cmd->lock);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /*
|
|---|
| 151 | * Now the command can be added.
|
|---|
| 152 | */
|
|---|
| 153 | list_append(&cmd->link, &cmd_list);
|
|---|
| 154 |
|
|---|
| 155 | spinlock_unlock(&cmd_lock);
|
|---|
| 156 | return true;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /** Print count times a character */
|
|---|
| 160 | NO_TRACE static void print_cc(wchar_t ch, size_t count)
|
|---|
| 161 | {
|
|---|
| 162 | size_t i;
|
|---|
| 163 | for (i = 0; i < count; i++)
|
|---|
| 164 | putchar(ch);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | /** Try to find a command beginning with prefix */
|
|---|
| 168 | NO_TRACE static const char *cmdtab_search_one(const char *name,
|
|---|
| 169 | link_t **startpos)
|
|---|
| 170 | {
|
|---|
| 171 | size_t namelen = str_length(name);
|
|---|
| 172 |
|
|---|
| 173 | spinlock_lock(&cmd_lock);
|
|---|
| 174 |
|
|---|
| 175 | if (*startpos == NULL)
|
|---|
| 176 | *startpos = cmd_list.head.next;
|
|---|
| 177 |
|
|---|
| 178 | for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
|
|---|
| 179 | cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
|
|---|
| 180 |
|
|---|
| 181 | const char *curname = hlp->name;
|
|---|
| 182 | if (str_length(curname) < namelen)
|
|---|
| 183 | continue;
|
|---|
| 184 |
|
|---|
| 185 | if (str_lcmp(curname, name, namelen) == 0) {
|
|---|
| 186 | spinlock_unlock(&cmd_lock);
|
|---|
| 187 | return (curname + str_lsize(curname, namelen));
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | spinlock_unlock(&cmd_lock);
|
|---|
| 192 | return NULL;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /** Command completion of the commands
|
|---|
| 196 | *
|
|---|
| 197 | * @param name String to match, changed to hint on exit
|
|---|
| 198 | * @param size Input buffer size
|
|---|
| 199 | *
|
|---|
| 200 | * @return Number of found matches
|
|---|
| 201 | *
|
|---|
| 202 | */
|
|---|
| 203 | NO_TRACE static int cmdtab_compl(char *input, size_t size)
|
|---|
| 204 | {
|
|---|
| 205 | const char *name = input;
|
|---|
| 206 |
|
|---|
| 207 | size_t found = 0;
|
|---|
| 208 | link_t *pos = NULL;
|
|---|
| 209 | const char *hint;
|
|---|
| 210 | char *output = malloc(MAX_CMDLINE, 0);
|
|---|
| 211 |
|
|---|
| 212 | output[0] = 0;
|
|---|
| 213 |
|
|---|
| 214 | while ((hint = cmdtab_search_one(name, &pos))) {
|
|---|
| 215 | if ((found == 0) || (str_length(output) > str_length(hint)))
|
|---|
| 216 | str_cpy(output, MAX_CMDLINE, hint);
|
|---|
| 217 |
|
|---|
| 218 | pos = pos->next;
|
|---|
| 219 | found++;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | if ((found > 1) && (str_length(output) != 0)) {
|
|---|
| 223 | printf("\n");
|
|---|
| 224 | pos = NULL;
|
|---|
| 225 | while (cmdtab_search_one(name, &pos)) {
|
|---|
| 226 | cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
|
|---|
| 227 | printf("%s (%s)\n", hlp->name, hlp->description);
|
|---|
| 228 | pos = pos->next;
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | if (found > 0)
|
|---|
| 233 | str_cpy(input, size, output);
|
|---|
| 234 |
|
|---|
| 235 | free(output);
|
|---|
| 236 | return found;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
|
|---|
| 240 | {
|
|---|
| 241 | printf("%s> ", prompt);
|
|---|
| 242 |
|
|---|
| 243 | size_t position = 0;
|
|---|
| 244 | wchar_t *current = history[history_pos];
|
|---|
| 245 | current[0] = 0;
|
|---|
| 246 | char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
|---|
| 247 |
|
|---|
| 248 | while (true) {
|
|---|
| 249 | wchar_t ch = indev_pop_character(indev);
|
|---|
| 250 |
|
|---|
| 251 | if (ch == '\n') {
|
|---|
| 252 | /* Enter */
|
|---|
| 253 | putchar(ch);
|
|---|
| 254 | break;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (ch == '\b') {
|
|---|
| 258 | /* Backspace */
|
|---|
| 259 | if (position == 0)
|
|---|
| 260 | continue;
|
|---|
| 261 |
|
|---|
| 262 | if (wstr_remove(current, position - 1)) {
|
|---|
| 263 | position--;
|
|---|
| 264 | putchar('\b');
|
|---|
| 265 | printf("%ls ", current + position);
|
|---|
| 266 | print_cc('\b', wstr_length(current) - position + 1);
|
|---|
| 267 | continue;
|
|---|
| 268 | }
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | if (ch == '\t') {
|
|---|
| 272 | /* Tab completion */
|
|---|
| 273 |
|
|---|
| 274 | /* Move to the end of the word */
|
|---|
| 275 | for (; (current[position] != 0) && (!isspace(current[position]));
|
|---|
| 276 | position++)
|
|---|
| 277 | putchar(current[position]);
|
|---|
| 278 |
|
|---|
| 279 | if (position == 0)
|
|---|
| 280 | continue;
|
|---|
| 281 |
|
|---|
| 282 | /* Find the beginning of the word
|
|---|
| 283 | and copy it to tmp */
|
|---|
| 284 | size_t beg;
|
|---|
| 285 | for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
|
|---|
| 286 | beg--);
|
|---|
| 287 |
|
|---|
| 288 | if (isspace(current[beg]))
|
|---|
| 289 | beg++;
|
|---|
| 290 |
|
|---|
| 291 | wstr_to_str(tmp, position - beg + 1, current + beg);
|
|---|
| 292 |
|
|---|
| 293 | int found;
|
|---|
| 294 | if (beg == 0) {
|
|---|
| 295 | /* Command completion */
|
|---|
| 296 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
|
|---|
| 297 | } else {
|
|---|
| 298 | /* Symbol completion */
|
|---|
| 299 | found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | if (found == 0)
|
|---|
| 303 | continue;
|
|---|
| 304 |
|
|---|
| 305 | if (found > 1) {
|
|---|
| 306 | /* No unique hint, list was printed */
|
|---|
| 307 | printf("%s> ", prompt);
|
|---|
| 308 | printf("%ls", current);
|
|---|
| 309 | print_cc('\b', wstr_length(current) - position);
|
|---|
| 310 | continue;
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /* We have a hint */
|
|---|
| 314 |
|
|---|
| 315 | size_t off = 0;
|
|---|
| 316 | size_t i = 0;
|
|---|
| 317 | while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
|
|---|
| 318 | if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
|
|---|
| 319 | break;
|
|---|
| 320 | i++;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | printf("%ls", current + position);
|
|---|
| 324 | position += str_length(tmp);
|
|---|
| 325 | print_cc('\b', wstr_length(current) - position);
|
|---|
| 326 |
|
|---|
| 327 | if (position == wstr_length(current)) {
|
|---|
| 328 | /* Insert a space after the last completed argument */
|
|---|
| 329 | if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
|
|---|
| 330 | printf("%ls", current + position);
|
|---|
| 331 | position++;
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 | continue;
|
|---|
| 335 | }
|
|---|
| 336 |
|
|---|
| 337 | if (ch == U_LEFT_ARROW) {
|
|---|
| 338 | /* Left */
|
|---|
| 339 | if (position > 0) {
|
|---|
| 340 | putchar('\b');
|
|---|
| 341 | position--;
|
|---|
| 342 | }
|
|---|
| 343 | continue;
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | if (ch == U_RIGHT_ARROW) {
|
|---|
| 347 | /* Right */
|
|---|
| 348 | if (position < wstr_length(current)) {
|
|---|
| 349 | putchar(current[position]);
|
|---|
| 350 | position++;
|
|---|
| 351 | }
|
|---|
| 352 | continue;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
|
|---|
| 356 | /* Up, down */
|
|---|
| 357 | print_cc('\b', position);
|
|---|
| 358 | print_cc(' ', wstr_length(current));
|
|---|
| 359 | print_cc('\b', wstr_length(current));
|
|---|
| 360 |
|
|---|
| 361 | if (ch == U_UP_ARROW) {
|
|---|
| 362 | /* Up */
|
|---|
| 363 | if (history_pos == 0)
|
|---|
| 364 | history_pos = KCONSOLE_HISTORY - 1;
|
|---|
| 365 | else
|
|---|
| 366 | history_pos--;
|
|---|
| 367 | } else {
|
|---|
| 368 | /* Down */
|
|---|
| 369 | history_pos++;
|
|---|
| 370 | history_pos = history_pos % KCONSOLE_HISTORY;
|
|---|
| 371 | }
|
|---|
| 372 | current = history[history_pos];
|
|---|
| 373 | printf("%ls", current);
|
|---|
| 374 | position = wstr_length(current);
|
|---|
| 375 | continue;
|
|---|
| 376 | }
|
|---|
| 377 |
|
|---|
| 378 | if (ch == U_HOME_ARROW) {
|
|---|
| 379 | /* Home */
|
|---|
| 380 | print_cc('\b', position);
|
|---|
| 381 | position = 0;
|
|---|
| 382 | continue;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | if (ch == U_END_ARROW) {
|
|---|
| 386 | /* End */
|
|---|
| 387 | printf("%ls", current + position);
|
|---|
| 388 | position = wstr_length(current);
|
|---|
| 389 | continue;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | if (ch == U_DELETE) {
|
|---|
| 393 | /* Delete */
|
|---|
| 394 | if (position == wstr_length(current))
|
|---|
| 395 | continue;
|
|---|
| 396 |
|
|---|
| 397 | if (wstr_remove(current, position)) {
|
|---|
| 398 | printf("%ls ", current + position);
|
|---|
| 399 | print_cc('\b', wstr_length(current) - position + 1);
|
|---|
| 400 | }
|
|---|
| 401 | continue;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
|
|---|
| 405 | printf("%ls", current + position);
|
|---|
| 406 | position++;
|
|---|
| 407 | print_cc('\b', wstr_length(current) - position);
|
|---|
| 408 | }
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | if (wstr_length(current) > 0) {
|
|---|
| 412 | history_pos++;
|
|---|
| 413 | history_pos = history_pos % KCONSOLE_HISTORY;
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | free(tmp);
|
|---|
| 417 | return current;
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | bool kconsole_check_poll(void)
|
|---|
| 421 | {
|
|---|
| 422 | return check_poll(stdin);
|
|---|
| 423 | }
|
|---|
| 424 |
|
|---|
| 425 | NO_TRACE static bool parse_int_arg(const char *text, size_t len,
|
|---|
| 426 | sysarg_t *result)
|
|---|
| 427 | {
|
|---|
| 428 | bool isaddr = false;
|
|---|
| 429 | bool isptr = false;
|
|---|
| 430 |
|
|---|
| 431 | /* If we get a name, try to find it in symbol table */
|
|---|
| 432 | if (text[0] == '&') {
|
|---|
| 433 | isaddr = true;
|
|---|
| 434 | text++;
|
|---|
| 435 | len--;
|
|---|
| 436 | } else if (text[0] == '*') {
|
|---|
| 437 | isptr = true;
|
|---|
| 438 | text++;
|
|---|
| 439 | len--;
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | if ((text[0] < '0') || (text[0] > '9')) {
|
|---|
| 443 | char symname[MAX_SYMBOL_NAME];
|
|---|
| 444 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
|
|---|
| 445 |
|
|---|
| 446 | uintptr_t symaddr;
|
|---|
| 447 | int rc = symtab_addr_lookup(symname, &symaddr);
|
|---|
| 448 | switch (rc) {
|
|---|
| 449 | case ENOENT:
|
|---|
| 450 | printf("Symbol %s not found.\n", symname);
|
|---|
| 451 | return false;
|
|---|
| 452 | case EOVERFLOW:
|
|---|
| 453 | printf("Duplicate symbol %s.\n", symname);
|
|---|
| 454 | symtab_print_search(symname);
|
|---|
| 455 | return false;
|
|---|
| 456 | case ENOTSUP:
|
|---|
| 457 | printf("No symbol information available.\n");
|
|---|
| 458 | return false;
|
|---|
| 459 | case EOK:
|
|---|
| 460 | if (isaddr)
|
|---|
| 461 | *result = (sysarg_t) symaddr;
|
|---|
| 462 | else if (isptr)
|
|---|
| 463 | *result = **((sysarg_t **) symaddr);
|
|---|
| 464 | else
|
|---|
| 465 | *result = *((sysarg_t *) symaddr);
|
|---|
| 466 | break;
|
|---|
| 467 | default:
|
|---|
| 468 | printf("Unknown error.\n");
|
|---|
| 469 | return false;
|
|---|
| 470 | }
|
|---|
| 471 | } else {
|
|---|
| 472 | /* It's a number - convert it */
|
|---|
| 473 | uint64_t value;
|
|---|
| 474 | int rc = str_uint64(text, NULL, 0, true, &value);
|
|---|
| 475 | switch (rc) {
|
|---|
| 476 | case EINVAL:
|
|---|
| 477 | printf("Invalid number.\n");
|
|---|
| 478 | return false;
|
|---|
| 479 | case EOVERFLOW:
|
|---|
| 480 | printf("Integer overflow.\n");
|
|---|
| 481 | return false;
|
|---|
| 482 | case EOK:
|
|---|
| 483 | *result = (sysarg_t) value;
|
|---|
| 484 | if (isptr)
|
|---|
| 485 | *result = *((sysarg_t *) *result);
|
|---|
| 486 | break;
|
|---|
| 487 | default:
|
|---|
| 488 | printf("Unknown error.\n");
|
|---|
| 489 | return false;
|
|---|
| 490 | }
|
|---|
| 491 | }
|
|---|
| 492 |
|
|---|
| 493 | return true;
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | /** Parse argument.
|
|---|
| 497 | *
|
|---|
| 498 | * Find start and end positions of command line argument.
|
|---|
| 499 | *
|
|---|
| 500 | * @param cmdline Command line as read from the input device.
|
|---|
| 501 | * @param size Size (in bytes) of the string.
|
|---|
| 502 | * @param start On entry, 'start' contains pointer to the offset
|
|---|
| 503 | * of the first unprocessed character of cmdline.
|
|---|
| 504 | * On successful exit, it marks beginning of the next argument.
|
|---|
| 505 | * @param end Undefined on entry. On exit, 'end' is the offset of the first
|
|---|
| 506 | * character behind the next argument.
|
|---|
| 507 | *
|
|---|
| 508 | * @return False on failure, true on success.
|
|---|
| 509 | *
|
|---|
| 510 | */
|
|---|
| 511 | NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
|
|---|
| 512 | size_t *start, size_t *end)
|
|---|
| 513 | {
|
|---|
| 514 | ASSERT(start != NULL);
|
|---|
| 515 | ASSERT(end != NULL);
|
|---|
| 516 |
|
|---|
| 517 | bool found_start = false;
|
|---|
| 518 | size_t offset = *start;
|
|---|
| 519 | size_t prev = *start;
|
|---|
| 520 | wchar_t ch;
|
|---|
| 521 |
|
|---|
| 522 | while ((ch = str_decode(cmdline, &offset, size)) != 0) {
|
|---|
| 523 | if (!found_start) {
|
|---|
| 524 | if (!isspace(ch)) {
|
|---|
| 525 | *start = prev;
|
|---|
| 526 | found_start = true;
|
|---|
| 527 | }
|
|---|
| 528 | } else {
|
|---|
| 529 | if (isspace(ch))
|
|---|
| 530 | break;
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 | prev = offset;
|
|---|
| 534 | }
|
|---|
| 535 | *end = prev;
|
|---|
| 536 |
|
|---|
| 537 | return found_start;
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | /** Parse command line.
|
|---|
| 541 | *
|
|---|
| 542 | * @param cmdline Command line as read from input device.
|
|---|
| 543 | * @param size Size (in bytes) of the string.
|
|---|
| 544 | *
|
|---|
| 545 | * @return Structure describing the command.
|
|---|
| 546 | *
|
|---|
| 547 | */
|
|---|
| 548 | NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
|
|---|
| 549 | {
|
|---|
| 550 | size_t start = 0;
|
|---|
| 551 | size_t end = 0;
|
|---|
| 552 | if (!parse_argument(cmdline, size, &start, &end)) {
|
|---|
| 553 | /* Command line did not contain alphanumeric word. */
|
|---|
| 554 | return NULL;
|
|---|
| 555 | }
|
|---|
| 556 | spinlock_lock(&cmd_lock);
|
|---|
| 557 |
|
|---|
| 558 | cmd_info_t *cmd = NULL;
|
|---|
| 559 |
|
|---|
| 560 | list_foreach(cmd_list, cur) {
|
|---|
| 561 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
|
|---|
| 562 | spinlock_lock(&hlp->lock);
|
|---|
| 563 |
|
|---|
| 564 | if (str_lcmp(hlp->name, cmdline + start,
|
|---|
| 565 | max(str_length(hlp->name),
|
|---|
| 566 | str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
|
|---|
| 567 | cmd = hlp;
|
|---|
| 568 | break;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | spinlock_unlock(&hlp->lock);
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | spinlock_unlock(&cmd_lock);
|
|---|
| 575 |
|
|---|
| 576 | if (!cmd) {
|
|---|
| 577 | /* Unknown command. */
|
|---|
| 578 | printf("Unknown command.\n");
|
|---|
| 579 | return NULL;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | /* cmd == hlp is locked */
|
|---|
| 583 |
|
|---|
| 584 | /*
|
|---|
| 585 | * The command line must be further analyzed and
|
|---|
| 586 | * the parameters therefrom must be matched and
|
|---|
| 587 | * converted to those specified in the cmd info
|
|---|
| 588 | * structure.
|
|---|
| 589 | */
|
|---|
| 590 |
|
|---|
| 591 | bool error = false;
|
|---|
| 592 | size_t i;
|
|---|
| 593 | for (i = 0; i < cmd->argc; i++) {
|
|---|
| 594 | char *buf;
|
|---|
| 595 |
|
|---|
| 596 | start = end;
|
|---|
| 597 | if (!parse_argument(cmdline, size, &start, &end)) {
|
|---|
| 598 | if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
|
|---|
| 599 | buf = (char *) cmd->argv[i].buffer;
|
|---|
| 600 | str_cpy(buf, cmd->argv[i].len, "");
|
|---|
| 601 | continue;
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | printf("Too few arguments.\n");
|
|---|
| 605 | spinlock_unlock(&cmd->lock);
|
|---|
| 606 | return NULL;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | switch (cmd->argv[i].type) {
|
|---|
| 610 | case ARG_TYPE_STRING:
|
|---|
| 611 | case ARG_TYPE_STRING_OPTIONAL:
|
|---|
| 612 | buf = (char *) cmd->argv[i].buffer;
|
|---|
| 613 | str_ncpy(buf, cmd->argv[i].len, cmdline + start,
|
|---|
| 614 | end - start);
|
|---|
| 615 | break;
|
|---|
| 616 | case ARG_TYPE_INT:
|
|---|
| 617 | if (!parse_int_arg(cmdline + start, end - start,
|
|---|
| 618 | &cmd->argv[i].intval))
|
|---|
| 619 | error = true;
|
|---|
| 620 | break;
|
|---|
| 621 | case ARG_TYPE_VAR:
|
|---|
| 622 | if ((start < end - 1) && (cmdline[start] == '"')) {
|
|---|
| 623 | if (cmdline[end - 1] == '"') {
|
|---|
| 624 | buf = (char *) cmd->argv[i].buffer;
|
|---|
| 625 | str_ncpy(buf, cmd->argv[i].len,
|
|---|
| 626 | cmdline + start + 1,
|
|---|
| 627 | (end - start) - 1);
|
|---|
| 628 | cmd->argv[i].intval = (sysarg_t) buf;
|
|---|
| 629 | cmd->argv[i].vartype = ARG_TYPE_STRING;
|
|---|
| 630 | } else {
|
|---|
| 631 | printf("Wrong syntax.\n");
|
|---|
| 632 | error = true;
|
|---|
| 633 | }
|
|---|
| 634 | } else if (parse_int_arg(cmdline + start,
|
|---|
| 635 | end - start, &cmd->argv[i].intval)) {
|
|---|
| 636 | cmd->argv[i].vartype = ARG_TYPE_INT;
|
|---|
| 637 | } else {
|
|---|
| 638 | printf("Unrecognized variable argument.\n");
|
|---|
| 639 | error = true;
|
|---|
| 640 | }
|
|---|
| 641 | break;
|
|---|
| 642 | case ARG_TYPE_INVALID:
|
|---|
| 643 | default:
|
|---|
| 644 | printf("Invalid argument type\n");
|
|---|
| 645 | error = true;
|
|---|
| 646 | break;
|
|---|
| 647 | }
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | if (error) {
|
|---|
| 651 | spinlock_unlock(&cmd->lock);
|
|---|
| 652 | return NULL;
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | start = end;
|
|---|
| 656 | if (parse_argument(cmdline, size, &start, &end)) {
|
|---|
| 657 | printf("Too many arguments.\n");
|
|---|
| 658 | spinlock_unlock(&cmd->lock);
|
|---|
| 659 | return NULL;
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | spinlock_unlock(&cmd->lock);
|
|---|
| 663 | return cmd;
|
|---|
| 664 | }
|
|---|
| 665 |
|
|---|
| 666 | /** Kernel console prompt.
|
|---|
| 667 | *
|
|---|
| 668 | * @param prompt Kernel console prompt (e.g kconsole/panic).
|
|---|
| 669 | * @param msg Message to display in the beginning.
|
|---|
| 670 | * @param kcon Wait for keypress to show the prompt
|
|---|
| 671 | * and never exit.
|
|---|
| 672 | *
|
|---|
| 673 | */
|
|---|
| 674 | void kconsole(const char *prompt, const char *msg, bool kcon)
|
|---|
| 675 | {
|
|---|
| 676 | if (!stdin) {
|
|---|
| 677 | LOG("No stdin for kernel console");
|
|---|
| 678 | return;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | if (msg)
|
|---|
| 682 | printf("%s", msg);
|
|---|
| 683 |
|
|---|
| 684 | if (kcon)
|
|---|
| 685 | indev_pop_character(stdin);
|
|---|
| 686 | else
|
|---|
| 687 | printf("Type \"exit\" to leave the console.\n");
|
|---|
| 688 |
|
|---|
| 689 | char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
|---|
| 690 | while (true) {
|
|---|
| 691 | wchar_t *tmp = clever_readline((char *) prompt, stdin);
|
|---|
| 692 | size_t len = wstr_length(tmp);
|
|---|
| 693 | if (!len)
|
|---|
| 694 | continue;
|
|---|
| 695 |
|
|---|
| 696 | wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
|
|---|
| 697 |
|
|---|
| 698 | if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
|
|---|
| 699 | break;
|
|---|
| 700 |
|
|---|
| 701 | cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
|
|---|
| 702 | if (!cmd_info)
|
|---|
| 703 | continue;
|
|---|
| 704 |
|
|---|
| 705 | (void) cmd_info->func(cmd_info->argv);
|
|---|
| 706 | }
|
|---|
| 707 | free(cmdline);
|
|---|
| 708 | }
|
|---|
| 709 |
|
|---|
| 710 | /** Kernel console managing thread.
|
|---|
| 711 | *
|
|---|
| 712 | */
|
|---|
| 713 | void kconsole_thread(void *data)
|
|---|
| 714 | {
|
|---|
| 715 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | /** @}
|
|---|
| 719 | */
|
|---|