source: mainline/kernel/generic/src/console/kconsole.c@ e2a0d76

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2a0d76 was a1ecb88, checked in by Martin Decky <martin@…>, 12 years ago

remove duplicate include

  • Property mode set to 100644
File size: 17.7 KB
RevLine 
[2677758]1/*
[df4ed85]2 * Copyright (c) 2005 Jakub Jermar
[2677758]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
[06e1e95]29/** @addtogroup genericconsole
[b45c443]30 * @{
31 */
32
[cf26ba9]33/**
[d6c8ff6]34 * @file kconsole.c
35 * @brief Kernel console.
[cf26ba9]36 *
37 * This file contains kernel thread managing the kernel console.
[d6c8ff6]38 *
[cf26ba9]39 */
40
[f4338d2]41#include <console/kconsole.h>
[2677758]42#include <console/console.h>
43#include <console/chardev.h>
[442d0ae]44#include <console/cmd.h>
[f0d7bd9]45#include <console/prompt.h>
[2677758]46#include <print.h>
[ff3b3197]47#include <panic.h>
[d99c1d2]48#include <typedefs.h>
[5c9a08b]49#include <adt/list.h>
[ff3b3197]50#include <arch.h>
51#include <macros.h>
[f4338d2]52#include <debug.h>
[442d0ae]53#include <func.h>
[19f857a]54#include <str.h>
[3ad953c]55#include <sysinfo/sysinfo.h>
56#include <ddi/device.h>
[e2b762ec]57#include <symtab.h>
[e16e0d59]58#include <errno.h>
[eec616b]59#include <putchar.h>
[19f857a]60#include <str.h>
[e2b762ec]61
[ff3b3197]62/** Simple kernel console.
63 *
64 * The console is realized by kernel thread kconsole.
[f4338d2]65 * It doesn't understand any useful command on its own,
66 * but makes it possible for other kernel subsystems to
[ff3b3197]67 * register their own commands.
68 */
[d6c8ff6]69
[ff3b3197]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
[d6c8ff6]85SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
[55b77d9]86LIST_INITIALIZE(cmd_list); /**< Command list. */
[d6c8ff6]87
88static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
[98000fb]89static size_t history_pos = 0;
[ff3b3197]90
[3ad953c]91/** Initialize kconsole data structures
92 *
93 * This is the most basic initialization, almost no
94 * other kernel subsystem is ready yet.
95 *
96 */
[ff3b3197]97void kconsole_init(void)
98{
[3ad953c]99 unsigned int i;
[d6c8ff6]100
[442d0ae]101 cmd_init();
[cf5ddf6]102 for (i = 0; i < KCONSOLE_HISTORY; i++)
[d6c8ff6]103 history[i][0] = 0;
[ff3b3197]104}
105
106/** Register kconsole command.
107 *
108 * @param cmd Structure describing the command.
109 *
[d6c8ff6]110 * @return False on failure, true on success.
111 *
[ff3b3197]112 */
[d6c8ff6]113bool cmd_register(cmd_info_t *cmd)
[ff3b3197]114{
115 spinlock_lock(&cmd_lock);
116
117 /*
118 * Make sure the command is not already listed.
119 */
[55b77d9]120 list_foreach(cmd_list, cur) {
[d6c8ff6]121 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
[ff3b3197]122
123 if (hlp == cmd) {
124 /* The command is already there. */
125 spinlock_unlock(&cmd_lock);
[d6c8ff6]126 return false;
[ff3b3197]127 }
[d6c8ff6]128
[ff3b3197]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 }
[d6c8ff6]137
138 if (str_cmp(hlp->name, cmd->name) == 0) {
[ff3b3197]139 /* The command is already there. */
140 spinlock_unlock(&hlp->lock);
141 spinlock_unlock(&cmd->lock);
142 spinlock_unlock(&cmd_lock);
[d6c8ff6]143 return false;
[ff3b3197]144 }
145
146 spinlock_unlock(&hlp->lock);
147 spinlock_unlock(&cmd->lock);
148 }
149
150 /*
151 * Now the command can be added.
152 */
[55b77d9]153 list_append(&cmd->link, &cmd_list);
[ff3b3197]154
155 spinlock_unlock(&cmd_lock);
[d6c8ff6]156 return true;
[ff3b3197]157}
[2677758]158
[07bd114e]159/** Print count times a character */
[7a0359b]160NO_TRACE static void print_cc(wchar_t ch, size_t count)
[0c8e692]161{
[98000fb]162 size_t i;
[cf5ddf6]163 for (i = 0; i < count; i++)
[0c8e692]164 putchar(ch);
165}
166
[2cf87e50]167/** Try to find a command beginning with prefix */
[7a0359b]168NO_TRACE static const char *cmdtab_search_one(const char *name,
169 link_t **startpos)
[0c8e692]170{
[98000fb]171 size_t namelen = str_length(name);
[d6c8ff6]172
[0c8e692]173 spinlock_lock(&cmd_lock);
[d6c8ff6]174
175 if (*startpos == NULL)
[55b77d9]176 *startpos = cmd_list.head.next;
[d6c8ff6]177
[55b77d9]178 for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
[d6c8ff6]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)
[0c8e692]183 continue;
[d6c8ff6]184
185 if (str_lcmp(curname, name, namelen) == 0) {
186 spinlock_unlock(&cmd_lock);
187 return (curname + str_lsize(curname, namelen));
[0c8e692]188 }
189 }
[d6c8ff6]190
191 spinlock_unlock(&cmd_lock);
[0c8e692]192 return NULL;
193}
194
[d6c8ff6]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
[0c8e692]201 *
202 */
[e435537]203NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t *indev)
[0c8e692]204{
[d6c8ff6]205 const char *name = input;
206
[98000fb]207 size_t found = 0;
[e435537]208
209 /*
210 * Maximum Match Length: Length of longest matching common
211 * substring in case more than one match is found.
212 */
[1e01a35]213 size_t max_match_len = size;
214 size_t max_match_len_tmp = size;
215 size_t input_len = str_length(input);
[d6c8ff6]216 link_t *pos = NULL;
217 const char *hint;
[a7199c2]218 char *output = malloc(MAX_CMDLINE, 0);
[1e01a35]219 size_t hints_to_show = MAX_TAB_HINTS - 1;
220 size_t total_hints_shown = 0;
[f0d7bd9]221 bool continue_showing_hints = true;
[d6c8ff6]222
223 output[0] = 0;
224
225 while ((hint = cmdtab_search_one(name, &pos))) {
226 if ((found == 0) || (str_length(output) > str_length(hint)))
[f4b1535]227 str_cpy(output, MAX_CMDLINE, hint);
[d6c8ff6]228
229 pos = pos->next;
[0c8e692]230 found++;
231 }
[d6c8ff6]232
[e435537]233 /*
234 * If the number of possible completions is more than MAX_TAB_HINTS,
235 * ask the user whether to display them or not.
236 */
[1e01a35]237 if (found > MAX_TAB_HINTS) {
[aca4a04]238 printf("\n");
[e435537]239 continue_showing_hints =
240 console_prompt_display_all_hints(indev, found);
[1e01a35]241 }
242
[d6c8ff6]243 if ((found > 1) && (str_length(output) != 0)) {
[0c8e692]244 printf("\n");
[d6c8ff6]245 pos = NULL;
[137691a]246 while (cmdtab_search_one(name, &pos)) {
[d6c8ff6]247 cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
[e435537]248
[f0d7bd9]249 if (continue_showing_hints) {
[1e01a35]250 printf("%s (%s)\n", hlp->name, hlp->description);
251 --hints_to_show;
252 ++total_hints_shown;
[e435537]253
254 if ((hints_to_show == 0) && (total_hints_shown != found)) {
255 /* Ask user to continue */
256 continue_showing_hints =
257 console_prompt_more_hints(indev, &hints_to_show);
[1e01a35]258 }
259 }
[e435537]260
[d6c8ff6]261 pos = pos->next;
[e435537]262
263 for (max_match_len_tmp = 0;
264 (output[max_match_len_tmp] ==
265 hlp->name[input_len + max_match_len_tmp]) &&
266 (max_match_len_tmp < max_match_len); ++max_match_len_tmp);
267
[1e01a35]268 max_match_len = max_match_len_tmp;
[0c8e692]269 }
[e435537]270
271 /* Keep only the characters common in all completions */
[1e01a35]272 output[max_match_len] = 0;
[0c8e692]273 }
[d6c8ff6]274
275 if (found > 0)
[f4b1535]276 str_cpy(input, size, output);
[d6c8ff6]277
[a7199c2]278 free(output);
[0c8e692]279 return found;
280}
281
[7a0359b]282NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
[0c8e692]283{
284 printf("%s> ", prompt);
[d6c8ff6]285
[98000fb]286 size_t position = 0;
[d6c8ff6]287 wchar_t *current = history[history_pos];
288 current[0] = 0;
[a7199c2]289 char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
[d6c8ff6]290
291 while (true) {
[44b7783]292 wchar_t ch = indev_pop_character(indev);
[d6c8ff6]293
294 if (ch == '\n') {
295 /* Enter */
296 putchar(ch);
[0c8e692]297 break;
[5d67baa]298 }
[d6c8ff6]299
300 if (ch == '\b') {
301 /* Backspace */
[0c8e692]302 if (position == 0)
303 continue;
[d6c8ff6]304
305 if (wstr_remove(current, position - 1)) {
306 position--;
[c8bf88d]307 putchar('\b');
308 printf("%ls ", current + position);
309 print_cc('\b', wstr_length(current) - position + 1);
[d6c8ff6]310 continue;
311 }
[0c8e692]312 }
[d6c8ff6]313
314 if (ch == '\t') {
315 /* Tab completion */
316
[0c8e692]317 /* Move to the end of the word */
[d6c8ff6]318 for (; (current[position] != 0) && (!isspace(current[position]));
[cf5ddf6]319 position++)
[0c8e692]320 putchar(current[position]);
[d6c8ff6]321
322 if (position == 0)
323 continue;
324
[e435537]325 /*
326 * Find the beginning of the word
327 * and copy it to tmp
328 */
[98000fb]329 size_t beg;
[d6c8ff6]330 for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
331 beg--);
332
333 if (isspace(current[beg]))
334 beg++;
335
[0f06dbc]336 wstr_to_str(tmp, position - beg + 1, current + beg);
[d6c8ff6]337
338 int found;
339 if (beg == 0) {
340 /* Command completion */
[1e01a35]341 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
[d6c8ff6]342 } else {
343 /* Symbol completion */
[1e01a35]344 found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
[0c8e692]345 }
[d6c8ff6]346
347 if (found == 0)
[0c8e692]348 continue;
[1e01a35]349
[e435537]350 /*
351 * We have hints, possibly many. In case of more than one hint,
352 * tmp will contain the common prefix.
353 */
[1e01a35]354 size_t off = 0;
355 size_t i = 0;
356 while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
357 if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
358 break;
[e435537]359
[1e01a35]360 i++;
361 }
[d6c8ff6]362
[37c312a]363 if (found > 1) {
364 /* No unique hint, list was printed */
365 printf("%s> ", prompt);
366 printf("%ls", current);
[1e01a35]367 position += str_length(tmp);
[37c312a]368 print_cc('\b', wstr_length(current) - position);
369 continue;
370 }
371
372 /* We have a hint */
373
374 printf("%ls", current + position);
375 position += str_length(tmp);
376 print_cc('\b', wstr_length(current) - position);
377
378 if (position == wstr_length(current)) {
379 /* Insert a space after the last completed argument */
380 if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
381 printf("%ls", current + position);
382 position++;
[0c8e692]383 }
384 }
385 continue;
386 }
[d6c8ff6]387
[c8bf88d]388 if (ch == U_LEFT_ARROW) {
389 /* Left */
390 if (position > 0) {
391 putchar('\b');
392 position--;
393 }
394 continue;
395 }
396
397 if (ch == U_RIGHT_ARROW) {
398 /* Right */
399 if (position < wstr_length(current)) {
400 putchar(current[position]);
401 position++;
402 }
403 continue;
404 }
405
406 if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
407 /* Up, down */
408 print_cc('\b', position);
409 print_cc(' ', wstr_length(current));
410 print_cc('\b', wstr_length(current));
[d6c8ff6]411
[c8bf88d]412 if (ch == U_UP_ARROW) {
413 /* Up */
414 if (history_pos == 0)
415 history_pos = KCONSOLE_HISTORY - 1;
416 else
417 history_pos--;
418 } else {
419 /* Down */
420 history_pos++;
421 history_pos = history_pos % KCONSOLE_HISTORY;
422 }
423 current = history[history_pos];
424 printf("%ls", current);
425 position = wstr_length(current);
426 continue;
427 }
428
429 if (ch == U_HOME_ARROW) {
430 /* Home */
431 print_cc('\b', position);
432 position = 0;
433 continue;
434 }
435
436 if (ch == U_END_ARROW) {
437 /* End */
438 printf("%ls", current + position);
439 position = wstr_length(current);
440 continue;
441 }
442
443 if (ch == U_DELETE) {
444 /* Delete */
445 if (position == wstr_length(current))
[0c8e692]446 continue;
[d6c8ff6]447
[c8bf88d]448 if (wstr_remove(current, position)) {
449 printf("%ls ", current + position);
450 print_cc('\b', wstr_length(current) - position + 1);
[0c8e692]451 }
452 continue;
453 }
[d6c8ff6]454
455 if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
456 printf("%ls", current + position);
457 position++;
458 print_cc('\b', wstr_length(current) - position);
459 }
[ba276f7]460 }
[a02e383]461
[d6c8ff6]462 if (wstr_length(current) > 0) {
463 history_pos++;
464 history_pos = history_pos % KCONSOLE_HISTORY;
[2677758]465 }
466
[a7199c2]467 free(tmp);
[d6c8ff6]468 return current;
[2677758]469}
[ff3b3197]470
[d6c8ff6]471bool kconsole_check_poll(void)
[76fca31]472{
[d6c8ff6]473 return check_poll(stdin);
[76fca31]474}
475
[7a0359b]476NO_TRACE static bool parse_int_arg(const char *text, size_t len,
[96b02eb9]477 sysarg_t *result)
[91c78c9]478{
479 bool isaddr = false;
[e5fcf00]480 bool isptr = false;
[91c78c9]481
482 /* If we get a name, try to find it in symbol table */
[d0da921]483 if (text[0] == '&') {
484 isaddr = true;
[cf5ddf6]485 text++;
486 len--;
[d0da921]487 } else if (text[0] == '*') {
488 isptr = true;
[cf5ddf6]489 text++;
490 len--;
[d0da921]491 }
[d6c8ff6]492
493 if ((text[0] < '0') || (text[0] > '9')) {
494 char symname[MAX_SYMBOL_NAME];
[f4b1535]495 str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
[d6c8ff6]496
497 uintptr_t symaddr;
498 int rc = symtab_addr_lookup(symname, &symaddr);
[e16e0d59]499 switch (rc) {
500 case ENOENT:
[cf5ddf6]501 printf("Symbol %s not found.\n", symname);
[d6c8ff6]502 return false;
[e16e0d59]503 case EOVERFLOW:
[cf5ddf6]504 printf("Duplicate symbol %s.\n", symname);
[91c78c9]505 symtab_print_search(symname);
[d6c8ff6]506 return false;
507 case ENOTSUP:
[e16e0d59]508 printf("No symbol information available.\n");
[d6c8ff6]509 return false;
[4ce914d4]510 case EOK:
511 if (isaddr)
[96b02eb9]512 *result = (sysarg_t) symaddr;
[4ce914d4]513 else if (isptr)
[96b02eb9]514 *result = **((sysarg_t **) symaddr);
[4ce914d4]515 else
[96b02eb9]516 *result = *((sysarg_t *) symaddr);
[4ce914d4]517 break;
518 default:
519 printf("Unknown error.\n");
520 return false;
[91c78c9]521 }
[d6c8ff6]522 } else {
523 /* It's a number - convert it */
[4ce914d4]524 uint64_t value;
[9b11a971]525 char *end;
526 int rc = str_uint64_t(text, &end, 0, false, &value);
527 if (end != text + len)
528 rc = EINVAL;
[4ce914d4]529 switch (rc) {
530 case EINVAL:
[a7d8739]531 printf("Invalid number '%s'.\n", text);
[4ce914d4]532 return false;
533 case EOVERFLOW:
[a7d8739]534 printf("Integer overflow in '%s'.\n", text);
[4ce914d4]535 return false;
536 case EOK:
[96b02eb9]537 *result = (sysarg_t) value;
[4ce914d4]538 if (isptr)
[96b02eb9]539 *result = *((sysarg_t *) *result);
[4ce914d4]540 break;
541 default:
[a7d8739]542 printf("Unknown error parsing '%s'.\n", text);
[4ce914d4]543 return false;
544 }
[c102a5c8]545 }
[d6c8ff6]546
547 return true;
548}
[d0da921]549
[d6c8ff6]550/** Parse argument.
551 *
552 * Find start and end positions of command line argument.
553 *
554 * @param cmdline Command line as read from the input device.
555 * @param size Size (in bytes) of the string.
556 * @param start On entry, 'start' contains pointer to the offset
557 * of the first unprocessed character of cmdline.
558 * On successful exit, it marks beginning of the next argument.
559 * @param end Undefined on entry. On exit, 'end' is the offset of the first
560 * character behind the next argument.
561 *
562 * @return False on failure, true on success.
563 *
564 */
[7a0359b]565NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
566 size_t *start, size_t *end)
[d6c8ff6]567{
568 ASSERT(start != NULL);
569 ASSERT(end != NULL);
570
571 bool found_start = false;
572 size_t offset = *start;
573 size_t prev = *start;
574 wchar_t ch;
575
576 while ((ch = str_decode(cmdline, &offset, size)) != 0) {
577 if (!found_start) {
578 if (!isspace(ch)) {
579 *start = prev;
580 found_start = true;
581 }
582 } else {
583 if (isspace(ch))
584 break;
585 }
586
587 prev = offset;
588 }
[a7b1071]589 *end = prev;
[d6c8ff6]590
591 return found_start;
[91c78c9]592}
593
[ff3b3197]594/** Parse command line.
595 *
[1e01a35]596 * @param cmdline Command line as read from input device.
[d6c8ff6]597 * @param size Size (in bytes) of the string.
[ff3b3197]598 *
599 * @return Structure describing the command.
[d6c8ff6]600 *
[ff3b3197]601 */
[7a0359b]602NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
[ff3b3197]603{
[d6c8ff6]604 size_t start = 0;
605 size_t end = 0;
606 if (!parse_argument(cmdline, size, &start, &end)) {
[ff3b3197]607 /* Command line did not contain alphanumeric word. */
608 return NULL;
609 }
610 spinlock_lock(&cmd_lock);
611
[d6c8ff6]612 cmd_info_t *cmd = NULL;
613
[55b77d9]614 list_foreach(cmd_list, cur) {
[d6c8ff6]615 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
[ff3b3197]616 spinlock_lock(&hlp->lock);
617
[d6c8ff6]618 if (str_lcmp(hlp->name, cmdline + start,
619 max(str_length(hlp->name),
[563d6077]620 str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
[ff3b3197]621 cmd = hlp;
622 break;
623 }
624
625 spinlock_unlock(&hlp->lock);
626 }
627
[d6c8ff6]628 spinlock_unlock(&cmd_lock);
[ff3b3197]629
630 if (!cmd) {
631 /* Unknown command. */
[f4338d2]632 printf("Unknown command.\n");
[ff3b3197]633 return NULL;
634 }
[d6c8ff6]635
[ff3b3197]636 /* cmd == hlp is locked */
637
638 /*
639 * The command line must be further analyzed and
640 * the parameters therefrom must be matched and
641 * converted to those specified in the cmd info
642 * structure.
643 */
[d6c8ff6]644
645 bool error = false;
[98000fb]646 size_t i;
[f4338d2]647 for (i = 0; i < cmd->argc; i++) {
[c0f13d2]648 char *buf;
649
[d6c8ff6]650 start = end;
651 if (!parse_argument(cmdline, size, &start, &end)) {
[c0f13d2]652 if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
653 buf = (char *) cmd->argv[i].buffer;
654 str_cpy(buf, cmd->argv[i].len, "");
655 continue;
656 }
657
[f4338d2]658 printf("Too few arguments.\n");
659 spinlock_unlock(&cmd->lock);
660 return NULL;
661 }
662
663 switch (cmd->argv[i].type) {
[6e716a59]664 case ARG_TYPE_STRING:
[c0f13d2]665 case ARG_TYPE_STRING_OPTIONAL:
[093752c]666 buf = (char *) cmd->argv[i].buffer;
[f4b1535]667 str_ncpy(buf, cmd->argv[i].len, cmdline + start,
[87d71bf]668 end - start);
[f4338d2]669 break;
[d6c8ff6]670 case ARG_TYPE_INT:
671 if (!parse_int_arg(cmdline + start, end - start,
[cf5ddf6]672 &cmd->argv[i].intval))
[d6c8ff6]673 error = true;
[6e716a59]674 break;
[91c78c9]675 case ARG_TYPE_VAR:
[d6c8ff6]676 if ((start < end - 1) && (cmdline[start] == '"')) {
677 if (cmdline[end - 1] == '"') {
678 buf = (char *) cmd->argv[i].buffer;
[f4b1535]679 str_ncpy(buf, cmd->argv[i].len,
680 cmdline + start + 1,
681 (end - start) - 1);
[96b02eb9]682 cmd->argv[i].intval = (sysarg_t) buf;
[d6c8ff6]683 cmd->argv[i].vartype = ARG_TYPE_STRING;
684 } else {
[b62d5614]685 printf("Wrong syntax.\n");
[d6c8ff6]686 error = true;
687 }
688 } else if (parse_int_arg(cmdline + start,
689 end - start, &cmd->argv[i].intval)) {
[91c78c9]690 cmd->argv[i].vartype = ARG_TYPE_INT;
[cf5ddf6]691 } else {
[91c78c9]692 printf("Unrecognized variable argument.\n");
[d6c8ff6]693 error = true;
[6e716a59]694 }
[91c78c9]695 break;
[6e716a59]696 case ARG_TYPE_INVALID:
697 default:
[d6c8ff6]698 printf("Invalid argument type\n");
699 error = true;
[f4338d2]700 break;
701 }
702 }
703
[80bff342]704 if (error) {
705 spinlock_unlock(&cmd->lock);
706 return NULL;
707 }
708
[d6c8ff6]709 start = end;
710 if (parse_argument(cmdline, size, &start, &end)) {
[f4338d2]711 printf("Too many arguments.\n");
712 spinlock_unlock(&cmd->lock);
713 return NULL;
714 }
[ff3b3197]715
716 spinlock_unlock(&cmd->lock);
717 return cmd;
718}
719
[d6c8ff6]720/** Kernel console prompt.
[f4338d2]721 *
[d6c8ff6]722 * @param prompt Kernel console prompt (e.g kconsole/panic).
723 * @param msg Message to display in the beginning.
724 * @param kcon Wait for keypress to show the prompt
725 * and never exit.
[f4338d2]726 *
727 */
[a000878c]728void kconsole(const char *prompt, const char *msg, bool kcon)
[f4338d2]729{
[d6c8ff6]730 if (!stdin) {
731 LOG("No stdin for kernel console");
732 return;
733 }
[f4338d2]734
[d6c8ff6]735 if (msg)
736 printf("%s", msg);
[f4338d2]737
[d6c8ff6]738 if (kcon)
[44b7783]739 indev_pop_character(stdin);
[d6c8ff6]740 else
741 printf("Type \"exit\" to leave the console.\n");
742
[a7199c2]743 char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
[d6c8ff6]744 while (true) {
745 wchar_t *tmp = clever_readline((char *) prompt, stdin);
[98000fb]746 size_t len = wstr_length(tmp);
[d6c8ff6]747 if (!len)
748 continue;
749
[0f06dbc]750 wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
[d6c8ff6]751
752 if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
753 break;
754
755 cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
756 if (!cmd_info)
757 continue;
758
759 (void) cmd_info->func(cmd_info->argv);
[f4338d2]760 }
[a7199c2]761 free(cmdline);
[d6c8ff6]762}
[f4338d2]763
[d6c8ff6]764/** Kernel console managing thread.
765 *
766 */
767void kconsole_thread(void *data)
768{
769 kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
[f4338d2]770}
[b45c443]771
[06e1e95]772/** @}
[b45c443]773 */
Note: See TracBrowser for help on using the repository browser.