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

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

Merged mainline,1723.

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