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