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

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

kconsole tab completion: permit tab at first char too

This patch makes it possible, for the user, to tab the first char,
so that a list of available commands is shown: a quick alternative
to typing "help".

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