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