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

Last change on this file was 690ad20, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 2 months ago

Convert kio buffer to bytes (part 1)

  • Property mode set to 100644
File size: 19.2 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
[690ad20]41#include <adt/list.h>
42#include <arch.h>
[63e27ef]43#include <assert.h>
[2677758]44#include <console/chardev.h>
[442d0ae]45#include <console/cmd.h>
[690ad20]46#include <console/console.h>
47#include <console/kconsole.h>
[f0d7bd9]48#include <console/prompt.h>
[f4338d2]49#include <debug.h>
[690ad20]50#include <errno.h>
[b2e121a]51#include <halt.h>
[690ad20]52#include <macros.h>
53#include <panic.h>
54#include <stdio.h>
55#include <stdlib.h>
[19f857a]56#include <str.h>
[e2b762ec]57#include <symtab.h>
[690ad20]58#include <sysinfo/sysinfo.h>
59#include <typedefs.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
[da13982]87#define MAX_SYMBOL_NAME 64
88
[28a5ebd]89static char32_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;
[a35b458]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);
[a35b458]117
[ff3b3197]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 }
[a35b458]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 }
[a35b458]136
[d6c8ff6]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 }
[a35b458]144
[ff3b3197]145 spinlock_unlock(&hlp->lock);
146 spinlock_unlock(&cmd->lock);
147 }
[a35b458]148
[ff3b3197]149 /*
150 * Now the command can be added.
151 */
[55b77d9]152 list_append(&cmd->link, &cmd_list);
[a35b458]153
[ff3b3197]154 spinlock_unlock(&cmd_lock);
[d6c8ff6]155 return true;
[ff3b3197]156}
[2677758]157
[07bd114e]158/** Print count times a character */
[690ad20]159_NO_TRACE static void print_cc(char ch, size_t count)
[0c8e692]160{
[690ad20]161 // FIXME: only lock once
162
163 for (size_t i = 0; i < count; i++)
164 putstr(&ch, 1);
[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 */
[690ad20]349 putstr("\n", 1);
[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--;
[690ad20]360 putstr("\b", 1);
[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
[690ad20]370 size_t i = position;
371 while (current[i] && !isspace(current[i]))
372 i++;
373
374 char32_t stash = current[i];
375 current[i] = 0;
376 printf("%ls", &current[position]);
377 current[i] = stash;
378 position = i;
[a35b458]379
[e435537]380 /*
381 * Find the beginning of the word
382 * and copy it to tmp
383 */
[98000fb]384 size_t beg;
[d9ffc54]385 unsigned narg = 0;
[6b00876]386 if (position == 0) {
387 tmp[0] = '\0';
388 beg = 0;
[d9ffc54]389 } else {
[84239b1]390 beg = position - 1;
391 while ((beg > 0) && (!isspace(current[beg])))
[3bacee1]392 beg--;
[a35b458]393
[6b00876]394 if (isspace(current[beg]))
395 beg++;
[a35b458]396
[6b00876]397 wstr_to_str(tmp, position - beg + 1, current + beg);
398 }
[a35b458]399
[0cfc18d3]400 /* Count which argument number are we tabbing (narg=0 is cmd) */
[d9ffc54]401 bool sp = false;
[0cfc18d3]402 for (; beg > 0; beg--) {
403 if (isspace(current[beg])) {
404 if (!sp) {
405 narg++;
[d9ffc54]406 sp = true;
[0cfc18d3]407 }
[d9ffc54]408 } else
409 sp = false;
[0cfc18d3]410 }
[a35b458]411
[0cfc18d3]412 if (narg && isspace(current[0]))
413 narg--;
[a35b458]414
[d6c8ff6]415 int found;
[0cfc18d3]416 if (narg == 0) {
[d6c8ff6]417 /* Command completion */
[6a75c134]418 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
419 cmdtab_enum);
[d6c8ff6]420 } else {
[6a75c134]421 /* Arguments completion */
422 cmd_info_t *cmd = parse_cmd(current);
[0cfc18d3]423 if (!cmd || !cmd->hints_enum || cmd->argc < narg)
[6a75c134]424 continue;
425 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
426 cmd->hints_enum);
[0c8e692]427 }
[a35b458]428
[d6c8ff6]429 if (found == 0)
[0c8e692]430 continue;
[1e01a35]431
[e435537]432 /*
433 * We have hints, possibly many. In case of more than one hint,
434 * tmp will contain the common prefix.
435 */
[1e01a35]436 size_t off = 0;
[690ad20]437 for (size_t i = 0; (ch = str_decode(tmp, &off, STR_NO_LIMIT)); i++)
[1e01a35]438 if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
439 break;
[a35b458]440
[37c312a]441 if (found > 1) {
442 /* No unique hint, list was printed */
443 printf("%s> ", prompt);
444 printf("%ls", current);
[1e01a35]445 position += str_length(tmp);
[37c312a]446 print_cc('\b', wstr_length(current) - position);
447 continue;
448 }
[a35b458]449
[37c312a]450 /* We have a hint */
[a35b458]451
[37c312a]452 printf("%ls", current + position);
453 position += str_length(tmp);
454 print_cc('\b', wstr_length(current) - position);
[a35b458]455
[37c312a]456 if (position == wstr_length(current)) {
457 /* Insert a space after the last completed argument */
458 if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
459 printf("%ls", current + position);
460 position++;
[0c8e692]461 }
462 }
463 continue;
464 }
[a35b458]465
[c8bf88d]466 if (ch == U_LEFT_ARROW) {
467 /* Left */
468 if (position > 0) {
[690ad20]469 putstr("\b", 1);
[c8bf88d]470 position--;
471 }
472 continue;
473 }
[a35b458]474
[c8bf88d]475 if (ch == U_RIGHT_ARROW) {
476 /* Right */
477 if (position < wstr_length(current)) {
[690ad20]478 printf("%lc", current[position]);
[c8bf88d]479 position++;
480 }
481 continue;
482 }
[a35b458]483
[c8bf88d]484 if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
485 /* Up, down */
486 print_cc('\b', position);
487 print_cc(' ', wstr_length(current));
488 print_cc('\b', wstr_length(current));
[a35b458]489
[c8bf88d]490 if (ch == U_UP_ARROW) {
491 /* Up */
492 if (history_pos == 0)
493 history_pos = KCONSOLE_HISTORY - 1;
494 else
495 history_pos--;
496 } else {
497 /* Down */
498 history_pos++;
499 history_pos = history_pos % KCONSOLE_HISTORY;
500 }
501 current = history[history_pos];
502 printf("%ls", current);
503 position = wstr_length(current);
504 continue;
505 }
[a35b458]506
[c8bf88d]507 if (ch == U_HOME_ARROW) {
508 /* Home */
509 print_cc('\b', position);
510 position = 0;
511 continue;
512 }
[a35b458]513
[c8bf88d]514 if (ch == U_END_ARROW) {
515 /* End */
516 printf("%ls", current + position);
517 position = wstr_length(current);
518 continue;
519 }
[a35b458]520
[c8bf88d]521 if (ch == U_DELETE) {
522 /* Delete */
523 if (position == wstr_length(current))
[0c8e692]524 continue;
[a35b458]525
[c8bf88d]526 if (wstr_remove(current, position)) {
527 printf("%ls ", current + position);
528 print_cc('\b', wstr_length(current) - position + 1);
[0c8e692]529 }
530 continue;
531 }
[a35b458]532
[d6c8ff6]533 if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
534 printf("%ls", current + position);
535 position++;
536 print_cc('\b', wstr_length(current) - position);
537 }
[ba276f7]538 }
[a35b458]539
[d6c8ff6]540 if (wstr_length(current) > 0) {
541 history_pos++;
542 history_pos = history_pos % KCONSOLE_HISTORY;
[2677758]543 }
[a35b458]544
[d6c8ff6]545 return current;
[2677758]546}
[ff3b3197]547
[d6c8ff6]548bool kconsole_check_poll(void)
[76fca31]549{
[d6c8ff6]550 return check_poll(stdin);
[76fca31]551}
552
[8df5f20]553_NO_TRACE static bool parse_int_arg(const char *text, size_t len,
[96b02eb9]554 sysarg_t *result)
[91c78c9]555{
556 bool isaddr = false;
[e5fcf00]557 bool isptr = false;
[a35b458]558
[91c78c9]559 /* If we get a name, try to find it in symbol table */
[d0da921]560 if (text[0] == '&') {
561 isaddr = true;
[cf5ddf6]562 text++;
563 len--;
[d0da921]564 } else if (text[0] == '*') {
565 isptr = true;
[cf5ddf6]566 text++;
567 len--;
[d0da921]568 }
[a35b458]569
[d6c8ff6]570 if ((text[0] < '0') || (text[0] > '9')) {
571 char symname[MAX_SYMBOL_NAME];
[f4b1535]572 str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
[a35b458]573
[d6c8ff6]574 uintptr_t symaddr;
[b7fd2a0]575 errno_t rc = symtab_addr_lookup(symname, &symaddr);
[e16e0d59]576 switch (rc) {
577 case ENOENT:
[cf5ddf6]578 printf("Symbol %s not found.\n", symname);
[d6c8ff6]579 return false;
[e16e0d59]580 case EOVERFLOW:
[cf5ddf6]581 printf("Duplicate symbol %s.\n", symname);
[91c78c9]582 symtab_print_search(symname);
[d6c8ff6]583 return false;
584 case ENOTSUP:
[e16e0d59]585 printf("No symbol information available.\n");
[d6c8ff6]586 return false;
[4ce914d4]587 case EOK:
588 if (isaddr)
[96b02eb9]589 *result = (sysarg_t) symaddr;
[4ce914d4]590 else if (isptr)
[96b02eb9]591 *result = **((sysarg_t **) symaddr);
[4ce914d4]592 else
[96b02eb9]593 *result = *((sysarg_t *) symaddr);
[4ce914d4]594 break;
595 default:
596 printf("Unknown error.\n");
597 return false;
[91c78c9]598 }
[d6c8ff6]599 } else {
600 /* It's a number - convert it */
[4ce914d4]601 uint64_t value;
[fdfb24e]602 const char *end;
[b7fd2a0]603 errno_t rc = str_uint64_t(text, &end, 0, false, &value);
[9b11a971]604 if (end != text + len)
605 rc = EINVAL;
[4ce914d4]606 switch (rc) {
607 case EINVAL:
[a7d8739]608 printf("Invalid number '%s'.\n", text);
[4ce914d4]609 return false;
610 case EOVERFLOW:
[a7d8739]611 printf("Integer overflow in '%s'.\n", text);
[4ce914d4]612 return false;
613 case EOK:
[96b02eb9]614 *result = (sysarg_t) value;
[4ce914d4]615 if (isptr)
[96b02eb9]616 *result = *((sysarg_t *) *result);
[4ce914d4]617 break;
618 default:
[a7d8739]619 printf("Unknown error parsing '%s'.\n", text);
[4ce914d4]620 return false;
621 }
[c102a5c8]622 }
[a35b458]623
[d6c8ff6]624 return true;
625}
[d0da921]626
[d6c8ff6]627/** Parse argument.
628 *
629 * Find start and end positions of command line argument.
630 *
631 * @param cmdline Command line as read from the input device.
632 * @param size Size (in bytes) of the string.
633 * @param start On entry, 'start' contains pointer to the offset
634 * of the first unprocessed character of cmdline.
635 * On successful exit, it marks beginning of the next argument.
636 * @param end Undefined on entry. On exit, 'end' is the offset of the first
637 * character behind the next argument.
638 *
639 * @return False on failure, true on success.
640 *
641 */
[8df5f20]642_NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
[7a0359b]643 size_t *start, size_t *end)
[d6c8ff6]644{
[63e27ef]645 assert(start != NULL);
646 assert(end != NULL);
[a35b458]647
[d6c8ff6]648 bool found_start = false;
649 size_t offset = *start;
650 size_t prev = *start;
[28a5ebd]651 char32_t ch;
[a35b458]652
[d6c8ff6]653 while ((ch = str_decode(cmdline, &offset, size)) != 0) {
654 if (!found_start) {
655 if (!isspace(ch)) {
656 *start = prev;
657 found_start = true;
658 }
659 } else {
660 if (isspace(ch))
661 break;
662 }
[a35b458]663
[d6c8ff6]664 prev = offset;
665 }
[a7b1071]666 *end = prev;
[a35b458]667
[d6c8ff6]668 return found_start;
[91c78c9]669}
670
[ff3b3197]671/** Parse command line.
672 *
[1e01a35]673 * @param cmdline Command line as read from input device.
[d6c8ff6]674 * @param size Size (in bytes) of the string.
[ff3b3197]675 *
676 * @return Structure describing the command.
[d6c8ff6]677 *
[ff3b3197]678 */
[8df5f20]679_NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
[ff3b3197]680{
[d6c8ff6]681 size_t start = 0;
682 size_t end = 0;
683 if (!parse_argument(cmdline, size, &start, &end)) {
[ff3b3197]684 /* Command line did not contain alphanumeric word. */
685 return NULL;
686 }
687 spinlock_lock(&cmd_lock);
[a35b458]688
[d6c8ff6]689 cmd_info_t *cmd = NULL;
[a35b458]690
[feeac0d]691 list_foreach(cmd_list, link, cmd_info_t, hlp) {
[ff3b3197]692 spinlock_lock(&hlp->lock);
[a35b458]693
[d6c8ff6]694 if (str_lcmp(hlp->name, cmdline + start,
695 max(str_length(hlp->name),
[563d6077]696 str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
[ff3b3197]697 cmd = hlp;
698 break;
699 }
[a35b458]700
[ff3b3197]701 spinlock_unlock(&hlp->lock);
702 }
[a35b458]703
[d6c8ff6]704 spinlock_unlock(&cmd_lock);
[a35b458]705
[ff3b3197]706 if (!cmd) {
707 /* Unknown command. */
[f4338d2]708 printf("Unknown command.\n");
[ff3b3197]709 return NULL;
710 }
[a35b458]711
[ff3b3197]712 /* cmd == hlp is locked */
[a35b458]713
[ff3b3197]714 /*
715 * The command line must be further analyzed and
716 * the parameters therefrom must be matched and
717 * converted to those specified in the cmd info
718 * structure.
719 */
[a35b458]720
[d6c8ff6]721 bool error = false;
[98000fb]722 size_t i;
[f4338d2]723 for (i = 0; i < cmd->argc; i++) {
[c0f13d2]724 char *buf;
[a35b458]725
[d6c8ff6]726 start = end;
727 if (!parse_argument(cmdline, size, &start, &end)) {
[c0f13d2]728 if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
729 buf = (char *) cmd->argv[i].buffer;
730 str_cpy(buf, cmd->argv[i].len, "");
731 continue;
732 }
[a35b458]733
[f4338d2]734 printf("Too few arguments.\n");
735 spinlock_unlock(&cmd->lock);
736 return NULL;
737 }
[a35b458]738
[f4338d2]739 switch (cmd->argv[i].type) {
[6e716a59]740 case ARG_TYPE_STRING:
[c0f13d2]741 case ARG_TYPE_STRING_OPTIONAL:
[093752c]742 buf = (char *) cmd->argv[i].buffer;
[f4b1535]743 str_ncpy(buf, cmd->argv[i].len, cmdline + start,
[87d71bf]744 end - start);
[f4338d2]745 break;
[d6c8ff6]746 case ARG_TYPE_INT:
747 if (!parse_int_arg(cmdline + start, end - start,
[cf5ddf6]748 &cmd->argv[i].intval))
[d6c8ff6]749 error = true;
[6e716a59]750 break;
[91c78c9]751 case ARG_TYPE_VAR:
[d6c8ff6]752 if ((start < end - 1) && (cmdline[start] == '"')) {
753 if (cmdline[end - 1] == '"') {
754 buf = (char *) cmd->argv[i].buffer;
[f4b1535]755 str_ncpy(buf, cmd->argv[i].len,
756 cmdline + start + 1,
757 (end - start) - 1);
[96b02eb9]758 cmd->argv[i].intval = (sysarg_t) buf;
[d6c8ff6]759 cmd->argv[i].vartype = ARG_TYPE_STRING;
760 } else {
[b62d5614]761 printf("Wrong syntax.\n");
[d6c8ff6]762 error = true;
763 }
764 } else if (parse_int_arg(cmdline + start,
765 end - start, &cmd->argv[i].intval)) {
[91c78c9]766 cmd->argv[i].vartype = ARG_TYPE_INT;
[cf5ddf6]767 } else {
[91c78c9]768 printf("Unrecognized variable argument.\n");
[d6c8ff6]769 error = true;
[6e716a59]770 }
[91c78c9]771 break;
[6e716a59]772 case ARG_TYPE_INVALID:
773 default:
[d6c8ff6]774 printf("Invalid argument type\n");
775 error = true;
[f4338d2]776 break;
777 }
778 }
[a35b458]779
[80bff342]780 if (error) {
781 spinlock_unlock(&cmd->lock);
782 return NULL;
783 }
[a35b458]784
[d6c8ff6]785 start = end;
786 if (parse_argument(cmdline, size, &start, &end)) {
[f4338d2]787 printf("Too many arguments.\n");
788 spinlock_unlock(&cmd->lock);
789 return NULL;
790 }
[a35b458]791
[ff3b3197]792 spinlock_unlock(&cmd->lock);
793 return cmd;
794}
795
[d6c8ff6]796/** Kernel console prompt.
[f4338d2]797 *
[d6c8ff6]798 * @param prompt Kernel console prompt (e.g kconsole/panic).
799 * @param msg Message to display in the beginning.
800 * @param kcon Wait for keypress to show the prompt
801 * and never exit.
[f4338d2]802 *
803 */
[a000878c]804void kconsole(const char *prompt, const char *msg, bool kcon)
[f4338d2]805{
[d6c8ff6]806 if (!stdin) {
807 LOG("No stdin for kernel console");
808 return;
809 }
[a35b458]810
[d6c8ff6]811 if (msg)
812 printf("%s", msg);
[a35b458]813
[d6c8ff6]814 if (kcon)
[44b7783]815 indev_pop_character(stdin);
[d6c8ff6]816 else
817 printf("Type \"exit\" to leave the console.\n");
[a35b458]818
[4f3aa76]819 char *buffer = malloc(STR_BOUNDS(MAX_CMDLINE));
820 char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE));
821 if (!buffer || !cmdline) {
822 // TODO: fix the function so that it does not need allocations
823 printf("Can't start console, out of memory.\n");
824 free(buffer);
825 free(cmdline);
826 return;
827 }
828
[d6c8ff6]829 while (true) {
[28a5ebd]830 char32_t *tmp = clever_readline((char *) prompt, stdin, buffer);
[98000fb]831 size_t len = wstr_length(tmp);
[d6c8ff6]832 if (!len)
833 continue;
[a35b458]834
[0f06dbc]835 wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
[a35b458]836
[d6c8ff6]837 if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
838 break;
[a35b458]839
[d6c8ff6]840 cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
841 if (!cmd_info)
842 continue;
[a35b458]843
[d6c8ff6]844 (void) cmd_info->func(cmd_info->argv);
[f4338d2]845 }
[4f3aa76]846 free(buffer);
[a7199c2]847 free(cmdline);
[d6c8ff6]848}
[f4338d2]849
[d6c8ff6]850/** Kernel console managing thread.
851 *
852 */
853void kconsole_thread(void *data)
854{
855 kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
[f4338d2]856}
[b45c443]857
[06e1e95]858/** @}
[b45c443]859 */
Note: See TracBrowser for help on using the repository browser.