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