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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6a75c134 was 6a75c134, checked in by Aurelio Colosimo <aurelio@…>, 9 years ago

tab arguments completion: defined and handled hints_enum callback in cmd_info_t

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