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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e2ea4ab1 was c0f13d2, checked in by Martin Decky <martin@…>, 15 years ago

introduce more compact and more readable command output to kconsole (suitable even for 80-column screens)

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