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

Last change on this file since 40043e8 was 2cc569a3, checked in by Matthieu Riolo <matthieu.riolo@…>, 6 years ago

Removing inclusion of halt.h

Several files used to include halt.h even though
this was not necessary. The inclusion has therefore
been removed

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