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

Last change on this file since acb14ce was fdfb24e, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 years ago

Deduplicate string related functions

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