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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c14762e was 1066041, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

preemption_disable: Turned functions into macros. Moved THREAD, AS, TASK, CPU into thread.h, as.h, task.h, cpu.h to fix the include hell that ensued.

  • Property mode set to 100644
File size: 16.3 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>
[2677758]45#include <print.h>
[ff3b3197]46#include <panic.h>
[d99c1d2]47#include <typedefs.h>
[5c9a08b]48#include <adt/list.h>
[ff3b3197]49#include <arch.h>
50#include <macros.h>
[f4338d2]51#include <debug.h>
[442d0ae]52#include <func.h>
[19f857a]53#include <str.h>
[e07fe0c]54#include <macros.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 */
[55b77d9]121 list_foreach(cmd_list, cur) {
[d6c8ff6]122 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
[ff3b3197]123
124 if (hlp == cmd) {
125 /* The command is already there. */
126 spinlock_unlock(&cmd_lock);
[d6c8ff6]127 return false;
[ff3b3197]128 }
[d6c8ff6]129
[ff3b3197]130 /* Avoid deadlock. */
131 if (hlp < cmd) {
132 spinlock_lock(&hlp->lock);
133 spinlock_lock(&cmd->lock);
134 } else {
135 spinlock_lock(&cmd->lock);
136 spinlock_lock(&hlp->lock);
137 }
[d6c8ff6]138
139 if (str_cmp(hlp->name, cmd->name) == 0) {
[ff3b3197]140 /* The command is already there. */
141 spinlock_unlock(&hlp->lock);
142 spinlock_unlock(&cmd->lock);
143 spinlock_unlock(&cmd_lock);
[d6c8ff6]144 return false;
[ff3b3197]145 }
146
147 spinlock_unlock(&hlp->lock);
148 spinlock_unlock(&cmd->lock);
149 }
150
151 /*
152 * Now the command can be added.
153 */
[55b77d9]154 list_append(&cmd->link, &cmd_list);
[ff3b3197]155
156 spinlock_unlock(&cmd_lock);
[d6c8ff6]157 return true;
[ff3b3197]158}
[2677758]159
[07bd114e]160/** Print count times a character */
[7a0359b]161NO_TRACE static void print_cc(wchar_t ch, size_t count)
[0c8e692]162{
[98000fb]163 size_t i;
[cf5ddf6]164 for (i = 0; i < count; i++)
[0c8e692]165 putchar(ch);
166}
167
[2cf87e50]168/** Try to find a command beginning with prefix */
[7a0359b]169NO_TRACE static const char *cmdtab_search_one(const char *name,
170 link_t **startpos)
[0c8e692]171{
[98000fb]172 size_t namelen = str_length(name);
[d6c8ff6]173
[0c8e692]174 spinlock_lock(&cmd_lock);
[d6c8ff6]175
176 if (*startpos == NULL)
[55b77d9]177 *startpos = cmd_list.head.next;
[d6c8ff6]178
[55b77d9]179 for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
[d6c8ff6]180 cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
181
182 const char *curname = hlp->name;
183 if (str_length(curname) < namelen)
[0c8e692]184 continue;
[d6c8ff6]185
186 if (str_lcmp(curname, name, namelen) == 0) {
187 spinlock_unlock(&cmd_lock);
188 return (curname + str_lsize(curname, namelen));
[0c8e692]189 }
190 }
[d6c8ff6]191
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 */
[7a0359b]204NO_TRACE static int cmdtab_compl(char *input, size_t size)
[0c8e692]205{
[d6c8ff6]206 const char *name = input;
207
[98000fb]208 size_t found = 0;
[d6c8ff6]209 link_t *pos = NULL;
210 const char *hint;
[a7199c2]211 char *output = malloc(MAX_CMDLINE, 0);
[d6c8ff6]212
213 output[0] = 0;
214
215 while ((hint = cmdtab_search_one(name, &pos))) {
216 if ((found == 0) || (str_length(output) > str_length(hint)))
[f4b1535]217 str_cpy(output, MAX_CMDLINE, hint);
[d6c8ff6]218
219 pos = pos->next;
[0c8e692]220 found++;
221 }
[d6c8ff6]222
223 if ((found > 1) && (str_length(output) != 0)) {
[0c8e692]224 printf("\n");
[d6c8ff6]225 pos = NULL;
[137691a]226 while (cmdtab_search_one(name, &pos)) {
[d6c8ff6]227 cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
228 printf("%s (%s)\n", hlp->name, hlp->description);
229 pos = pos->next;
[0c8e692]230 }
231 }
[d6c8ff6]232
233 if (found > 0)
[f4b1535]234 str_cpy(input, size, output);
[d6c8ff6]235
[a7199c2]236 free(output);
[0c8e692]237 return found;
238}
239
[7a0359b]240NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
[0c8e692]241{
242 printf("%s> ", prompt);
[d6c8ff6]243
[98000fb]244 size_t position = 0;
[d6c8ff6]245 wchar_t *current = history[history_pos];
246 current[0] = 0;
[a7199c2]247 char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
[d6c8ff6]248
249 while (true) {
[44b7783]250 wchar_t ch = indev_pop_character(indev);
[d6c8ff6]251
252 if (ch == '\n') {
253 /* Enter */
254 putchar(ch);
[0c8e692]255 break;
[5d67baa]256 }
[d6c8ff6]257
258 if (ch == '\b') {
259 /* Backspace */
[0c8e692]260 if (position == 0)
261 continue;
[d6c8ff6]262
263 if (wstr_remove(current, position - 1)) {
264 position--;
[c8bf88d]265 putchar('\b');
266 printf("%ls ", current + position);
267 print_cc('\b', wstr_length(current) - position + 1);
[d6c8ff6]268 continue;
269 }
[0c8e692]270 }
[d6c8ff6]271
272 if (ch == '\t') {
273 /* Tab completion */
274
[0c8e692]275 /* Move to the end of the word */
[d6c8ff6]276 for (; (current[position] != 0) && (!isspace(current[position]));
[cf5ddf6]277 position++)
[0c8e692]278 putchar(current[position]);
[d6c8ff6]279
280 if (position == 0)
281 continue;
282
283 /* Find the beginning of the word
284 and copy it to tmp */
[98000fb]285 size_t beg;
[d6c8ff6]286 for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
287 beg--);
288
289 if (isspace(current[beg]))
290 beg++;
291
[0f06dbc]292 wstr_to_str(tmp, position - beg + 1, current + beg);
[d6c8ff6]293
294 int found;
295 if (beg == 0) {
296 /* Command completion */
297 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
298 } else {
299 /* Symbol completion */
300 found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
[0c8e692]301 }
[d6c8ff6]302
303 if (found == 0)
[0c8e692]304 continue;
[d6c8ff6]305
[37c312a]306 if (found > 1) {
307 /* No unique hint, list was printed */
308 printf("%s> ", prompt);
309 printf("%ls", current);
310 print_cc('\b', wstr_length(current) - position);
311 continue;
312 }
313
314 /* We have a hint */
315
[d6c8ff6]316 size_t off = 0;
[98000fb]317 size_t i = 0;
[d6c8ff6]318 while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) {
319 if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
320 break;
321 i++;
322 }
323
[37c312a]324 printf("%ls", current + position);
325 position += str_length(tmp);
326 print_cc('\b', wstr_length(current) - position);
327
328 if (position == wstr_length(current)) {
329 /* Insert a space after the last completed argument */
330 if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) {
331 printf("%ls", current + position);
332 position++;
[0c8e692]333 }
334 }
335 continue;
336 }
[d6c8ff6]337
[c8bf88d]338 if (ch == U_LEFT_ARROW) {
339 /* Left */
340 if (position > 0) {
341 putchar('\b');
342 position--;
343 }
344 continue;
345 }
346
347 if (ch == U_RIGHT_ARROW) {
348 /* Right */
349 if (position < wstr_length(current)) {
350 putchar(current[position]);
351 position++;
352 }
353 continue;
354 }
355
356 if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
357 /* Up, down */
358 print_cc('\b', position);
359 print_cc(' ', wstr_length(current));
360 print_cc('\b', wstr_length(current));
[d6c8ff6]361
[c8bf88d]362 if (ch == U_UP_ARROW) {
363 /* Up */
364 if (history_pos == 0)
365 history_pos = KCONSOLE_HISTORY - 1;
366 else
367 history_pos--;
368 } else {
369 /* Down */
370 history_pos++;
371 history_pos = history_pos % KCONSOLE_HISTORY;
372 }
373 current = history[history_pos];
374 printf("%ls", current);
375 position = wstr_length(current);
376 continue;
377 }
378
379 if (ch == U_HOME_ARROW) {
380 /* Home */
381 print_cc('\b', position);
382 position = 0;
383 continue;
384 }
385
386 if (ch == U_END_ARROW) {
387 /* End */
388 printf("%ls", current + position);
389 position = wstr_length(current);
390 continue;
391 }
392
393 if (ch == U_DELETE) {
394 /* Delete */
395 if (position == wstr_length(current))
[0c8e692]396 continue;
[d6c8ff6]397
[c8bf88d]398 if (wstr_remove(current, position)) {
399 printf("%ls ", current + position);
400 print_cc('\b', wstr_length(current) - position + 1);
[0c8e692]401 }
402 continue;
403 }
[d6c8ff6]404
405 if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
406 printf("%ls", current + position);
407 position++;
408 print_cc('\b', wstr_length(current) - position);
409 }
[ba276f7]410 }
[a02e383]411
[d6c8ff6]412 if (wstr_length(current) > 0) {
413 history_pos++;
414 history_pos = history_pos % KCONSOLE_HISTORY;
[2677758]415 }
416
[a7199c2]417 free(tmp);
[d6c8ff6]418 return current;
[2677758]419}
[ff3b3197]420
[d6c8ff6]421bool kconsole_check_poll(void)
[76fca31]422{
[d6c8ff6]423 return check_poll(stdin);
[76fca31]424}
425
[7a0359b]426NO_TRACE static bool parse_int_arg(const char *text, size_t len,
[96b02eb9]427 sysarg_t *result)
[91c78c9]428{
429 bool isaddr = false;
[e5fcf00]430 bool isptr = false;
[91c78c9]431
432 /* If we get a name, try to find it in symbol table */
[d0da921]433 if (text[0] == '&') {
434 isaddr = true;
[cf5ddf6]435 text++;
436 len--;
[d0da921]437 } else if (text[0] == '*') {
438 isptr = true;
[cf5ddf6]439 text++;
440 len--;
[d0da921]441 }
[d6c8ff6]442
443 if ((text[0] < '0') || (text[0] > '9')) {
444 char symname[MAX_SYMBOL_NAME];
[f4b1535]445 str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
[d6c8ff6]446
447 uintptr_t symaddr;
448 int rc = symtab_addr_lookup(symname, &symaddr);
[e16e0d59]449 switch (rc) {
450 case ENOENT:
[cf5ddf6]451 printf("Symbol %s not found.\n", symname);
[d6c8ff6]452 return false;
[e16e0d59]453 case EOVERFLOW:
[cf5ddf6]454 printf("Duplicate symbol %s.\n", symname);
[91c78c9]455 symtab_print_search(symname);
[d6c8ff6]456 return false;
457 case ENOTSUP:
[e16e0d59]458 printf("No symbol information available.\n");
[d6c8ff6]459 return false;
[4ce914d4]460 case EOK:
461 if (isaddr)
[96b02eb9]462 *result = (sysarg_t) symaddr;
[4ce914d4]463 else if (isptr)
[96b02eb9]464 *result = **((sysarg_t **) symaddr);
[4ce914d4]465 else
[96b02eb9]466 *result = *((sysarg_t *) symaddr);
[4ce914d4]467 break;
468 default:
469 printf("Unknown error.\n");
470 return false;
[91c78c9]471 }
[d6c8ff6]472 } else {
473 /* It's a number - convert it */
[4ce914d4]474 uint64_t value;
[059a8e4]475 int rc = str_uint64_t(text, NULL, 0, true, &value);
[4ce914d4]476 switch (rc) {
477 case EINVAL:
478 printf("Invalid number.\n");
479 return false;
480 case EOVERFLOW:
481 printf("Integer overflow.\n");
482 return false;
483 case EOK:
[96b02eb9]484 *result = (sysarg_t) value;
[4ce914d4]485 if (isptr)
[96b02eb9]486 *result = *((sysarg_t *) *result);
[4ce914d4]487 break;
488 default:
489 printf("Unknown error.\n");
490 return false;
491 }
[c102a5c8]492 }
[d6c8ff6]493
494 return true;
495}
[d0da921]496
[d6c8ff6]497/** Parse argument.
498 *
499 * Find start and end positions of command line argument.
500 *
501 * @param cmdline Command line as read from the input device.
502 * @param size Size (in bytes) of the string.
503 * @param start On entry, 'start' contains pointer to the offset
504 * of the first unprocessed character of cmdline.
505 * On successful exit, it marks beginning of the next argument.
506 * @param end Undefined on entry. On exit, 'end' is the offset of the first
507 * character behind the next argument.
508 *
509 * @return False on failure, true on success.
510 *
511 */
[7a0359b]512NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
513 size_t *start, size_t *end)
[d6c8ff6]514{
515 ASSERT(start != NULL);
516 ASSERT(end != NULL);
517
518 bool found_start = false;
519 size_t offset = *start;
520 size_t prev = *start;
521 wchar_t ch;
522
523 while ((ch = str_decode(cmdline, &offset, size)) != 0) {
524 if (!found_start) {
525 if (!isspace(ch)) {
526 *start = prev;
527 found_start = true;
528 }
529 } else {
530 if (isspace(ch))
531 break;
532 }
533
534 prev = offset;
535 }
[a7b1071]536 *end = prev;
[d6c8ff6]537
538 return found_start;
[91c78c9]539}
540
[ff3b3197]541/** Parse command line.
542 *
[d6c8ff6]543 * @param cmdline Command line as read from input device.
544 * @param size Size (in bytes) of the string.
[ff3b3197]545 *
546 * @return Structure describing the command.
[d6c8ff6]547 *
[ff3b3197]548 */
[7a0359b]549NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
[ff3b3197]550{
[d6c8ff6]551 size_t start = 0;
552 size_t end = 0;
553 if (!parse_argument(cmdline, size, &start, &end)) {
[ff3b3197]554 /* Command line did not contain alphanumeric word. */
555 return NULL;
556 }
557 spinlock_lock(&cmd_lock);
558
[d6c8ff6]559 cmd_info_t *cmd = NULL;
560
[55b77d9]561 list_foreach(cmd_list, cur) {
[d6c8ff6]562 cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
[ff3b3197]563 spinlock_lock(&hlp->lock);
564
[d6c8ff6]565 if (str_lcmp(hlp->name, cmdline + start,
566 max(str_length(hlp->name),
[563d6077]567 str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
[ff3b3197]568 cmd = hlp;
569 break;
570 }
571
572 spinlock_unlock(&hlp->lock);
573 }
574
[d6c8ff6]575 spinlock_unlock(&cmd_lock);
[ff3b3197]576
577 if (!cmd) {
578 /* Unknown command. */
[f4338d2]579 printf("Unknown command.\n");
[ff3b3197]580 return NULL;
581 }
[d6c8ff6]582
[ff3b3197]583 /* cmd == hlp is locked */
584
585 /*
586 * The command line must be further analyzed and
587 * the parameters therefrom must be matched and
588 * converted to those specified in the cmd info
589 * structure.
590 */
[d6c8ff6]591
592 bool error = false;
[98000fb]593 size_t i;
[f4338d2]594 for (i = 0; i < cmd->argc; i++) {
[c0f13d2]595 char *buf;
596
[d6c8ff6]597 start = end;
598 if (!parse_argument(cmdline, size, &start, &end)) {
[c0f13d2]599 if (cmd->argv[i].type == ARG_TYPE_STRING_OPTIONAL) {
600 buf = (char *) cmd->argv[i].buffer;
601 str_cpy(buf, cmd->argv[i].len, "");
602 continue;
603 }
604
[f4338d2]605 printf("Too few arguments.\n");
606 spinlock_unlock(&cmd->lock);
607 return NULL;
608 }
609
610 switch (cmd->argv[i].type) {
[6e716a59]611 case ARG_TYPE_STRING:
[c0f13d2]612 case ARG_TYPE_STRING_OPTIONAL:
[093752c]613 buf = (char *) cmd->argv[i].buffer;
[f4b1535]614 str_ncpy(buf, cmd->argv[i].len, cmdline + start,
[87d71bf]615 end - start);
[f4338d2]616 break;
[d6c8ff6]617 case ARG_TYPE_INT:
618 if (!parse_int_arg(cmdline + start, end - start,
[cf5ddf6]619 &cmd->argv[i].intval))
[d6c8ff6]620 error = true;
[6e716a59]621 break;
[91c78c9]622 case ARG_TYPE_VAR:
[d6c8ff6]623 if ((start < end - 1) && (cmdline[start] == '"')) {
624 if (cmdline[end - 1] == '"') {
625 buf = (char *) cmd->argv[i].buffer;
[f4b1535]626 str_ncpy(buf, cmd->argv[i].len,
627 cmdline + start + 1,
628 (end - start) - 1);
[96b02eb9]629 cmd->argv[i].intval = (sysarg_t) buf;
[d6c8ff6]630 cmd->argv[i].vartype = ARG_TYPE_STRING;
631 } else {
[b62d5614]632 printf("Wrong syntax.\n");
[d6c8ff6]633 error = true;
634 }
635 } else if (parse_int_arg(cmdline + start,
636 end - start, &cmd->argv[i].intval)) {
[91c78c9]637 cmd->argv[i].vartype = ARG_TYPE_INT;
[cf5ddf6]638 } else {
[91c78c9]639 printf("Unrecognized variable argument.\n");
[d6c8ff6]640 error = true;
[6e716a59]641 }
[91c78c9]642 break;
[6e716a59]643 case ARG_TYPE_INVALID:
644 default:
[d6c8ff6]645 printf("Invalid argument type\n");
646 error = true;
[f4338d2]647 break;
648 }
649 }
650
[80bff342]651 if (error) {
652 spinlock_unlock(&cmd->lock);
653 return NULL;
654 }
655
[d6c8ff6]656 start = end;
657 if (parse_argument(cmdline, size, &start, &end)) {
[f4338d2]658 printf("Too many arguments.\n");
659 spinlock_unlock(&cmd->lock);
660 return NULL;
661 }
[ff3b3197]662
663 spinlock_unlock(&cmd->lock);
664 return cmd;
665}
666
[d6c8ff6]667/** Kernel console prompt.
[f4338d2]668 *
[d6c8ff6]669 * @param prompt Kernel console prompt (e.g kconsole/panic).
670 * @param msg Message to display in the beginning.
671 * @param kcon Wait for keypress to show the prompt
672 * and never exit.
[f4338d2]673 *
674 */
[a000878c]675void kconsole(const char *prompt, const char *msg, bool kcon)
[f4338d2]676{
[d6c8ff6]677 if (!stdin) {
678 LOG("No stdin for kernel console");
679 return;
680 }
[f4338d2]681
[d6c8ff6]682 if (msg)
683 printf("%s", msg);
[f4338d2]684
[d6c8ff6]685 if (kcon)
[44b7783]686 indev_pop_character(stdin);
[d6c8ff6]687 else
688 printf("Type \"exit\" to leave the console.\n");
689
[a7199c2]690 char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
[d6c8ff6]691 while (true) {
692 wchar_t *tmp = clever_readline((char *) prompt, stdin);
[98000fb]693 size_t len = wstr_length(tmp);
[d6c8ff6]694 if (!len)
695 continue;
696
[0f06dbc]697 wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
[d6c8ff6]698
699 if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
700 break;
701
702 cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
703 if (!cmd_info)
704 continue;
705
706 (void) cmd_info->func(cmd_info->argv);
[f4338d2]707 }
[a7199c2]708 free(cmdline);
[d6c8ff6]709}
[f4338d2]710
[d6c8ff6]711/** Kernel console managing thread.
712 *
713 */
714void kconsole_thread(void *data)
715{
716 kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true);
[f4338d2]717}
[b45c443]718
[06e1e95]719/** @}
[b45c443]720 */
Note: See TracBrowser for help on using the repository browser.