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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f0d7bd9 was f0d7bd9, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

Create function for displaying —more— prompt

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