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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5d67baa was 5d67baa, checked in by Jakub Jermar <jakub@…>, 17 years ago

cstyle fixes

  • Property mode set to 100644
File size: 15.1 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/**
34 * @file kconsole.c
35 * @brief Kernel console.
36 *
37 * This file contains kernel thread managing the kernel console.
38 */
39
[f4338d2]40#include <console/kconsole.h>
[2677758]41#include <console/console.h>
42#include <console/chardev.h>
[442d0ae]43#include <console/cmd.h>
[2677758]44#include <print.h>
[ff3b3197]45#include <panic.h>
[2677758]46#include <arch/types.h>
[5c9a08b]47#include <adt/list.h>
[ff3b3197]48#include <arch.h>
49#include <macros.h>
[f4338d2]50#include <debug.h>
[442d0ae]51#include <func.h>
[6e716a59]52#include <symtab.h>
[e07fe0c]53#include <macros.h>
[ff3b3197]54
55/** Simple kernel console.
56 *
57 * The console is realized by kernel thread kconsole.
[f4338d2]58 * It doesn't understand any useful command on its own,
59 * but makes it possible for other kernel subsystems to
[ff3b3197]60 * register their own commands.
61 */
62
63/** Locking.
64 *
65 * There is a list of cmd_info_t structures. This list
66 * is protected by cmd_lock spinlock. Note that specially
67 * the link elements of cmd_info_t are protected by
68 * this lock.
69 *
70 * Each cmd_info_t also has its own lock, which protects
71 * all elements thereof except the link element.
72 *
73 * cmd_lock must be acquired before any cmd_info lock.
74 * When locking two cmd info structures, structure with
75 * lower address must be locked first.
76 */
77
[dc747e3]78SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
[7dd2561]79LIST_INITIALIZE(cmd_head); /**< Command list. */
[ff3b3197]80
81static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
[cf5ddf6]82static bool parse_argument(char *cmdline, size_t len, index_t *start,
83 index_t *end);
[0c8e692]84static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
[ff3b3197]85
86/** Initialize kconsole data structures. */
87void kconsole_init(void)
88{
[0c8e692]89 int i;
90
[442d0ae]91 cmd_init();
[cf5ddf6]92 for (i = 0; i < KCONSOLE_HISTORY; i++)
[0c8e692]93 history[i][0] = '\0';
[ff3b3197]94}
95
96
97/** Register kconsole command.
98 *
99 * @param cmd Structure describing the command.
100 *
101 * @return 0 on failure, 1 on success.
102 */
103int cmd_register(cmd_info_t *cmd)
104{
105 link_t *cur;
106
107 spinlock_lock(&cmd_lock);
108
109 /*
110 * Make sure the command is not already listed.
111 */
112 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
113 cmd_info_t *hlp;
114
115 hlp = list_get_instance(cur, cmd_info_t, link);
116
117 if (hlp == cmd) {
118 /* The command is already there. */
119 spinlock_unlock(&cmd_lock);
120 return 0;
121 }
122
123 /* Avoid deadlock. */
124 if (hlp < cmd) {
125 spinlock_lock(&hlp->lock);
126 spinlock_lock(&cmd->lock);
127 } else {
128 spinlock_lock(&cmd->lock);
129 spinlock_lock(&hlp->lock);
130 }
[cf5ddf6]131 if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name),
132 strlen(hlp->name))) == 0)) {
[ff3b3197]133 /* The command is already there. */
134 spinlock_unlock(&hlp->lock);
135 spinlock_unlock(&cmd->lock);
136 spinlock_unlock(&cmd_lock);
137 return 0;
138 }
139
140 spinlock_unlock(&hlp->lock);
141 spinlock_unlock(&cmd->lock);
142 }
143
144 /*
145 * Now the command can be added.
146 */
147 list_append(&cmd->link, &cmd_head);
148
149 spinlock_unlock(&cmd_lock);
150 return 1;
151}
[2677758]152
[07bd114e]153/** Print count times a character */
[0c8e692]154static void rdln_print_c(char ch, int count)
155{
156 int i;
[cf5ddf6]157 for (i = 0; i < count; i++)
[0c8e692]158 putchar(ch);
159}
160
[07bd114e]161/** Insert character to string */
[0c8e692]162static void insert_char(char *str, char ch, int pos)
163{
164 int i;
165
[cf5ddf6]166 for (i = strlen(str); i > pos; i--)
167 str[i] = str[i - 1];
[0c8e692]168 str[pos] = ch;
169}
170
[2cf87e50]171/** Try to find a command beginning with prefix */
[5d67baa]172static const char *cmdtab_search_one(const char *name,link_t **startpos)
[0c8e692]173{
[093752c]174 size_t namelen = strlen(name);
[0c8e692]175 const char *curname;
176
177 spinlock_lock(&cmd_lock);
178
179 if (!*startpos)
180 *startpos = cmd_head.next;
181
[cf5ddf6]182 for (; *startpos != &cmd_head; *startpos = (*startpos)->next) {
[0c8e692]183 cmd_info_t *hlp;
184 hlp = list_get_instance(*startpos, cmd_info_t, link);
185
186 curname = hlp->name;
187 if (strlen(curname) < namelen)
188 continue;
189 if (strncmp(curname, name, namelen) == 0) {
190 spinlock_unlock(&cmd_lock);
191 return curname+namelen;
192 }
193 }
194 spinlock_unlock(&cmd_lock);
195 return NULL;
196}
197
198
199/** Command completion of the commands
200 *
201 * @param name - string to match, changed to hint on exit
202 * @return number of found matches
203 */
204static int cmdtab_compl(char *name)
205{
[5d67baa]206 static char output[MAX_SYMBOL_NAME + 1];
[0c8e692]207 link_t *startpos = NULL;
208 const char *foundtxt;
209 int found = 0;
210 int i;
211
212 output[0] = '\0';
213 while ((foundtxt = cmdtab_search_one(name, &startpos))) {
214 startpos = startpos->next;
215 if (!found)
[5d67baa]216 strncpy(output, foundtxt, strlen(foundtxt) + 1);
[0c8e692]217 else {
[cf5ddf6]218 for (i = 0; output[i] && foundtxt[i] &&
219 output[i] == foundtxt[i]; i++)
[0c8e692]220 ;
221 output[i] = '\0';
222 }
223 found++;
224 }
225 if (!found)
226 return 0;
227
[3550c393]228 if (found > 1 && !strlen(output)) {
[0c8e692]229 printf("\n");
230 startpos = NULL;
231 while ((foundtxt = cmdtab_search_one(name, &startpos))) {
232 cmd_info_t *hlp;
233 hlp = list_get_instance(startpos, cmd_info_t, link);
234 printf("%s - %s\n", hlp->name, hlp->description);
235 startpos = startpos->next;
236 }
237 }
238 strncpy(name, output, MAX_SYMBOL_NAME);
239 return found;
240
241}
242
[5d67baa]243//char *clever_readline(const char *prompt, chardev_t *input);
244static char *clever_readline(const char *prompt, chardev_t *input)
[0c8e692]245{
246 static int histposition = 0;
247
[5d67baa]248 static char tmp[MAX_CMDLINE + 1];
[0c8e692]249 int curlen = 0, position = 0;
250 char *current = history[histposition];
251 int i;
[af9a7c5]252 char mod; /* Command Modifier */
[0c8e692]253 char c;
254
255 printf("%s> ", prompt);
256 while (1) {
257 c = _getc(input);
258 if (c == '\n') {
259 putchar(c);
260 break;
[5d67baa]261 }
262 if (c == '\b') { /* Backspace */
[0c8e692]263 if (position == 0)
264 continue;
[cf5ddf6]265 for (i = position; i < curlen; i++)
266 current[i - 1] = current[i];
[0c8e692]267 curlen--;
268 position--;
269 putchar('\b');
[cf5ddf6]270 for (i = position; i < curlen; i++)
[0c8e692]271 putchar(current[i]);
272 putchar(' ');
[cf5ddf6]273 rdln_print_c('\b', curlen - position + 1);
[0c8e692]274 continue;
275 }
[93b84b3]276 if (c == '\t') { /* Tabulator */
[0c8e692]277 int found;
278
279 /* Move to the end of the word */
[cf5ddf6]280 for (; position < curlen && current[position] != ' ';
281 position++)
[0c8e692]282 putchar(current[position]);
283 /* Copy to tmp last word */
[cf5ddf6]284 for (i = position - 1; i >= 0 && current[i] != ' '; i--)
[0c8e692]285 ;
286 /* If word begins with * or &, skip it */
287 if (tmp[0] == '*' || tmp[0] == '&')
[cf5ddf6]288 for (i = 1; tmp[i]; i++)
289 tmp[i - 1] = tmp[i];
[0c8e692]290 i++; /* I is at the start of the word */
[cf5ddf6]291 strncpy(tmp, current + i, position - i + 1);
[0c8e692]292
[cf5ddf6]293 if (i == 0) { /* Command completion */
[0c8e692]294 found = cmdtab_compl(tmp);
295 } else { /* Symtab completion */
296 found = symtab_compl(tmp);
297 }
298
299 if (found == 0)
300 continue;
[cf5ddf6]301 for (i = 0; tmp[i] && curlen < MAX_CMDLINE;
302 i++, curlen++)
303 insert_char(current, tmp[i], i + position);
[3550c393]304
[cf5ddf6]305 if (strlen(tmp) || found == 1) { /* If we have a hint */
306 for (i = position; i < curlen; i++)
[0c8e692]307 putchar(current[i]);
308 position += strlen(tmp);
309 /* Add space to end */
[cf5ddf6]310 if (found == 1 && position == curlen &&
[3550c393]311 curlen < MAX_CMDLINE) {
[0c8e692]312 current[position] = ' ';
313 curlen++;
314 position++;
315 putchar(' ');
316 }
[3550c393]317 } else { /* No hint, table was printed */
[0c8e692]318 printf("%s> ", prompt);
[cf5ddf6]319 for (i = 0; i < curlen; i++)
[0c8e692]320 putchar(current[i]);
321 position += strlen(tmp);
322 }
[cf5ddf6]323 rdln_print_c('\b', curlen - position);
[0c8e692]324 continue;
325 }
[93b84b3]326 if (c == 0x1b) { /* Special command */
[af9a7c5]327 mod = _getc(input);
[0c8e692]328 c = _getc(input);
[af9a7c5]329
330 if (mod != 0x5b && mod != 0x4f)
[0c8e692]331 continue;
[af9a7c5]332
333 if (c == 0x33 && _getc(input) == 0x7e) {
[93b84b3]334 /* Delete */
[af9a7c5]335 if (position == curlen)
336 continue;
[cf5ddf6]337 for (i = position + 1; i < curlen; i++) {
[af9a7c5]338 putchar(current[i]);
[cf5ddf6]339 current[i - 1] = current[i];
[af9a7c5]340 }
341 putchar(' ');
[cf5ddf6]342 rdln_print_c('\b', curlen - position);
[af9a7c5]343 curlen--;
[cf5ddf6]344 } else if (c == 0x48) { /* Home */
345 rdln_print_c('\b', position);
[af9a7c5]346 position = 0;
[cf5ddf6]347 } else if (c == 0x46) { /* End */
348 for (i = position; i < curlen; i++)
[af9a7c5]349 putchar(current[i]);
350 position = curlen;
[cf5ddf6]351 } else if (c == 0x44) { /* Left */
[0c8e692]352 if (position > 0) {
353 putchar('\b');
354 position--;
355 }
356 continue;
[cf5ddf6]357 } else if (c == 0x43) { /* Right */
[0c8e692]358 if (position < curlen) {
359 putchar(current[position]);
360 position++;
361 }
362 continue;
[cf5ddf6]363 } else if (c == 0x41 || c == 0x42) {
364 /* Up, down */
365 rdln_print_c('\b', position);
366 rdln_print_c(' ', curlen);
367 rdln_print_c('\b', curlen);
[93b84b3]368 if (c == 0x41) /* Up */
[0c8e692]369 histposition--;
370 else
371 histposition++;
[cf5ddf6]372 if (histposition < 0) {
373 histposition = KCONSOLE_HISTORY - 1;
374 } else {
375 histposition =
376 histposition % KCONSOLE_HISTORY;
377 }
[0c8e692]378 current = history[histposition];
379 printf("%s", current);
380 curlen = strlen(current);
381 position = curlen;
382 continue;
383 }
384 continue;
385 }
386 if (curlen >= MAX_CMDLINE)
387 continue;
388
389 insert_char(current, c, position);
390
391 curlen++;
[cf5ddf6]392 for (i = position; i < curlen; i++)
[0c8e692]393 putchar(current[i]);
394 position++;
[cf5ddf6]395 rdln_print_c('\b',curlen - position);
[0c8e692]396 }
[ba276f7]397 if (curlen) {
398 histposition++;
399 histposition = histposition % KCONSOLE_HISTORY;
400 }
[0c8e692]401 current[curlen] = '\0';
402 return current;
403}
404
[2677758]405/** Kernel console managing thread.
406 *
[abbc16e]407 * @param prompt Kernel console prompt (e.g kconsole/panic).
[2677758]408 */
[e07fe0c]409void kconsole(void *prompt)
[2677758]410{
[ff3b3197]411 cmd_info_t *cmd_info;
412 count_t len;
[0c8e692]413 char *cmdline;
[2677758]414
415 if (!stdin) {
[3ee8a075]416 printf("%s: no stdin\n", __func__);
[2677758]417 return;
418 }
419
420 while (true) {
[093752c]421 cmdline = clever_readline((char *) prompt, stdin);
[0c8e692]422 len = strlen(cmdline);
423 if (!len)
[f4338d2]424 continue;
[ff3b3197]425 cmd_info = parse_cmdline(cmdline, len);
[f4338d2]426 if (!cmd_info)
[ff3b3197]427 continue;
[cf5ddf6]428 if (strncmp(cmd_info->name, "exit",
429 min(strlen(cmd_info->name), 5)) == 0)
[e07fe0c]430 break;
[ff3b3197]431 (void) cmd_info->func(cmd_info->argv);
[2677758]432 }
433}
[ff3b3197]434
[7f1c620]435static int parse_int_arg(char *text, size_t len, unative_t *result)
[91c78c9]436{
[1b43a04]437 static char symname[MAX_SYMBOL_NAME];
[7f1c620]438 uintptr_t symaddr;
[91c78c9]439 bool isaddr = false;
[e5fcf00]440 bool isptr = false;
[91c78c9]441
442 /* If we get a name, try to find it in symbol table */
[d0da921]443 if (text[0] == '&') {
444 isaddr = true;
[cf5ddf6]445 text++;
446 len--;
[d0da921]447 } else if (text[0] == '*') {
448 isptr = true;
[cf5ddf6]449 text++;
450 len--;
[d0da921]451 }
[5bb8e45]452 if (text[0] < '0' || text[0] > '9') {
[cf5ddf6]453 strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME));
[91c78c9]454 symaddr = get_symbol_addr(symname);
455 if (!symaddr) {
[cf5ddf6]456 printf("Symbol %s not found.\n", symname);
[91c78c9]457 return -1;
458 }
[7f1c620]459 if (symaddr == (uintptr_t) -1) {
[cf5ddf6]460 printf("Duplicate symbol %s.\n", symname);
[91c78c9]461 symtab_print_search(symname);
462 return -1;
463 }
[c102a5c8]464 if (isaddr)
[7f1c620]465 *result = (unative_t)symaddr;
[c102a5c8]466 else if (isptr)
[7f1c620]467 *result = **((unative_t **)symaddr);
[c102a5c8]468 else
[7f1c620]469 *result = *((unative_t *)symaddr);
[c102a5c8]470 } else { /* It's a number - convert it */
[91c78c9]471 *result = atoi(text);
[c102a5c8]472 if (isptr)
[7f1c620]473 *result = *((unative_t *)*result);
[c102a5c8]474 }
[d0da921]475
[91c78c9]476 return 0;
477}
478
[ff3b3197]479/** Parse command line.
480 *
481 * @param cmdline Command line as read from input device.
482 * @param len Command line length.
483 *
484 * @return Structure describing the command.
485 */
486cmd_info_t *parse_cmdline(char *cmdline, size_t len)
487{
488 index_t start = 0, end = 0;
489 cmd_info_t *cmd = NULL;
490 link_t *cur;
[093752c]491 count_t i;
[80bff342]492 int error = 0;
[ff3b3197]493
[f4338d2]494 if (!parse_argument(cmdline, len, &start, &end)) {
[ff3b3197]495 /* Command line did not contain alphanumeric word. */
496 return NULL;
497 }
498
499 spinlock_lock(&cmd_lock);
500
501 for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
502 cmd_info_t *hlp;
503
504 hlp = list_get_instance(cur, cmd_info_t, link);
505 spinlock_lock(&hlp->lock);
506
[07bd114e]507 if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name),
[cf5ddf6]508 end - start + 1)) == 0) {
[ff3b3197]509 cmd = hlp;
510 break;
511 }
512
513 spinlock_unlock(&hlp->lock);
514 }
515
516 spinlock_unlock(&cmd_lock);
517
518 if (!cmd) {
519 /* Unknown command. */
[f4338d2]520 printf("Unknown command.\n");
[ff3b3197]521 return NULL;
522 }
523
524 /* cmd == hlp is locked */
525
526 /*
527 * The command line must be further analyzed and
528 * the parameters therefrom must be matched and
529 * converted to those specified in the cmd info
530 * structure.
531 */
[f4338d2]532
533 for (i = 0; i < cmd->argc; i++) {
534 char *buf;
535 start = end + 1;
536 if (!parse_argument(cmdline, len, &start, &end)) {
537 printf("Too few arguments.\n");
538 spinlock_unlock(&cmd->lock);
539 return NULL;
540 }
541
[80bff342]542 error = 0;
[f4338d2]543 switch (cmd->argv[i].type) {
[6e716a59]544 case ARG_TYPE_STRING:
[093752c]545 buf = (char *) cmd->argv[i].buffer;
546 strncpy(buf, (const char *) &cmdline[start],
[cf5ddf6]547 min((end - start) + 2, cmd->argv[i].len));
[5d67baa]548 buf[min((end - start) + 1, cmd->argv[i].len - 1)] =
549 '\0';
[f4338d2]550 break;
[91c78c9]551 case ARG_TYPE_INT:
[cf5ddf6]552 if (parse_int_arg(cmdline + start, end - start + 1,
553 &cmd->argv[i].intval))
[80bff342]554 error = 1;
[6e716a59]555 break;
[91c78c9]556 case ARG_TYPE_VAR:
[cf5ddf6]557 if (start != end && cmdline[start] == '"' &&
558 cmdline[end] == '"') {
[093752c]559 buf = (char *) cmd->argv[i].buffer;
[cf5ddf6]560 strncpy(buf, (const char *) &cmdline[start + 1],
561 min((end-start), cmd->argv[i].len));
562 buf[min((end - start), cmd->argv[i].len - 1)] =
563 '\0';
[7f1c620]564 cmd->argv[i].intval = (unative_t) buf;
[91c78c9]565 cmd->argv[i].vartype = ARG_TYPE_STRING;
[5d67baa]566 } else if (!parse_int_arg(cmdline + start,
567 end - start + 1, &cmd->argv[i].intval)) {
[91c78c9]568 cmd->argv[i].vartype = ARG_TYPE_INT;
[cf5ddf6]569 } else {
[91c78c9]570 printf("Unrecognized variable argument.\n");
[80bff342]571 error = 1;
[6e716a59]572 }
[91c78c9]573 break;
[6e716a59]574 case ARG_TYPE_INVALID:
575 default:
576 printf("invalid argument type\n");
[80bff342]577 error = 1;
[f4338d2]578 break;
579 }
580 }
581
[80bff342]582 if (error) {
583 spinlock_unlock(&cmd->lock);
584 return NULL;
585 }
586
[f4338d2]587 start = end + 1;
588 if (parse_argument(cmdline, len, &start, &end)) {
589 printf("Too many arguments.\n");
590 spinlock_unlock(&cmd->lock);
591 return NULL;
592 }
[ff3b3197]593
594 spinlock_unlock(&cmd->lock);
595 return cmd;
596}
597
[f4338d2]598/** Parse argument.
599 *
600 * Find start and end positions of command line argument.
601 *
602 * @param cmdline Command line as read from the input device.
603 * @param len Number of characters in cmdline.
604 * @param start On entry, 'start' contains pointer to the index
605 * of first unprocessed character of cmdline.
606 * On successful exit, it marks beginning of the next argument.
607 * @param end Undefined on entry. On exit, 'end' points to the last character
608 * of the next argument.
609 *
610 * @return false on failure, true on success.
611 */
612bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
613{
[093752c]614 index_t i;
[f4338d2]615 bool found_start = false;
616
617 ASSERT(start != NULL);
618 ASSERT(end != NULL);
619
620 for (i = *start; i < len; i++) {
621 if (!found_start) {
[db25906]622 if (isspace(cmdline[i]))
[f4338d2]623 (*start)++;
624 else
625 found_start = true;
626 } else {
[db25906]627 if (isspace(cmdline[i]))
[f4338d2]628 break;
629 }
630 }
631 *end = i - 1;
632
633 return found_start;
634}
[b45c443]635
[06e1e95]636/** @}
[b45c443]637 */
Note: See TracBrowser for help on using the repository browser.