[2677758] | 1 | /*
|
---|
| 2 | * Copyright (C) 2005 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[f4338d2] | 29 | #include <console/kconsole.h>
|
---|
[2677758] | 30 | #include <console/console.h>
|
---|
| 31 | #include <console/chardev.h>
|
---|
[442d0ae] | 32 | #include <console/cmd.h>
|
---|
[2677758] | 33 | #include <print.h>
|
---|
[ff3b3197] | 34 | #include <panic.h>
|
---|
[2677758] | 35 | #include <typedefs.h>
|
---|
| 36 | #include <arch/types.h>
|
---|
[ff3b3197] | 37 | #include <list.h>
|
---|
| 38 | #include <arch.h>
|
---|
| 39 | #include <macros.h>
|
---|
[f4338d2] | 40 | #include <debug.h>
|
---|
[442d0ae] | 41 | #include <func.h>
|
---|
[6e716a59] | 42 | #include <symtab.h>
|
---|
[ff3b3197] | 43 |
|
---|
| 44 | /** Simple kernel console.
|
---|
| 45 | *
|
---|
| 46 | * The console is realized by kernel thread kconsole.
|
---|
[f4338d2] | 47 | * It doesn't understand any useful command on its own,
|
---|
| 48 | * but makes it possible for other kernel subsystems to
|
---|
[ff3b3197] | 49 | * register their own commands.
|
---|
| 50 | */
|
---|
| 51 |
|
---|
| 52 | /** Locking.
|
---|
| 53 | *
|
---|
| 54 | * There is a list of cmd_info_t structures. This list
|
---|
| 55 | * is protected by cmd_lock spinlock. Note that specially
|
---|
| 56 | * the link elements of cmd_info_t are protected by
|
---|
| 57 | * this lock.
|
---|
| 58 | *
|
---|
| 59 | * Each cmd_info_t also has its own lock, which protects
|
---|
| 60 | * all elements thereof except the link element.
|
---|
| 61 | *
|
---|
| 62 | * cmd_lock must be acquired before any cmd_info lock.
|
---|
| 63 | * When locking two cmd info structures, structure with
|
---|
| 64 | * lower address must be locked first.
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | spinlock_t cmd_lock; /**< Lock protecting command list. */
|
---|
| 68 | link_t cmd_head; /**< Command list. */
|
---|
| 69 |
|
---|
| 70 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len);
|
---|
[f4338d2] | 71 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end);
|
---|
[0c8e692] | 72 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {};
|
---|
[ff3b3197] | 73 |
|
---|
| 74 | /** Initialize kconsole data structures. */
|
---|
| 75 | void kconsole_init(void)
|
---|
| 76 | {
|
---|
[0c8e692] | 77 | int i;
|
---|
| 78 |
|
---|
[2d93f1f9] | 79 | spinlock_initialize(&cmd_lock, "kconsole_cmd");
|
---|
[ff3b3197] | 80 | list_initialize(&cmd_head);
|
---|
[2312685] | 81 |
|
---|
[442d0ae] | 82 | cmd_init();
|
---|
[0c8e692] | 83 | for (i=0; i<KCONSOLE_HISTORY; i++)
|
---|
| 84 | history[i][0] = '\0';
|
---|
[ff3b3197] | 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | /** Register kconsole command.
|
---|
| 89 | *
|
---|
| 90 | * @param cmd Structure describing the command.
|
---|
| 91 | *
|
---|
| 92 | * @return 0 on failure, 1 on success.
|
---|
| 93 | */
|
---|
| 94 | int cmd_register(cmd_info_t *cmd)
|
---|
| 95 | {
|
---|
| 96 | ipl_t ipl;
|
---|
| 97 | link_t *cur;
|
---|
| 98 |
|
---|
| 99 | spinlock_lock(&cmd_lock);
|
---|
| 100 |
|
---|
| 101 | /*
|
---|
| 102 | * Make sure the command is not already listed.
|
---|
| 103 | */
|
---|
| 104 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
|
---|
| 105 | cmd_info_t *hlp;
|
---|
| 106 |
|
---|
| 107 | hlp = list_get_instance(cur, cmd_info_t, link);
|
---|
| 108 |
|
---|
| 109 | if (hlp == cmd) {
|
---|
| 110 | /* The command is already there. */
|
---|
| 111 | spinlock_unlock(&cmd_lock);
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /* Avoid deadlock. */
|
---|
| 116 | if (hlp < cmd) {
|
---|
| 117 | spinlock_lock(&hlp->lock);
|
---|
| 118 | spinlock_lock(&cmd->lock);
|
---|
| 119 | } else {
|
---|
| 120 | spinlock_lock(&cmd->lock);
|
---|
| 121 | spinlock_lock(&hlp->lock);
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[f4338d2] | 124 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) {
|
---|
[ff3b3197] | 125 | /* The command is already there. */
|
---|
| 126 | spinlock_unlock(&hlp->lock);
|
---|
| 127 | spinlock_unlock(&cmd->lock);
|
---|
| 128 | spinlock_unlock(&cmd_lock);
|
---|
| 129 | return 0;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | spinlock_unlock(&hlp->lock);
|
---|
| 133 | spinlock_unlock(&cmd->lock);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | /*
|
---|
| 137 | * Now the command can be added.
|
---|
| 138 | */
|
---|
| 139 | list_append(&cmd->link, &cmd_head);
|
---|
| 140 |
|
---|
| 141 | spinlock_unlock(&cmd_lock);
|
---|
| 142 | return 1;
|
---|
| 143 | }
|
---|
[2677758] | 144 |
|
---|
[0c8e692] | 145 | static void rdln_print_c(char ch, int count)
|
---|
| 146 | {
|
---|
| 147 | int i;
|
---|
| 148 | for (i=0;i<count;i++)
|
---|
| 149 | putchar(ch);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | static void insert_char(char *str, char ch, int pos)
|
---|
| 153 | {
|
---|
| 154 | int i;
|
---|
| 155 |
|
---|
| 156 | for (i=strlen(str);i > pos; i--)
|
---|
| 157 | str[i] = str[i-1];
|
---|
| 158 | str[pos] = ch;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | static const char * cmdtab_search_one(const char *name,link_t **startpos)
|
---|
| 162 | {
|
---|
| 163 | int namelen = strlen(name);
|
---|
| 164 | const char *curname;
|
---|
| 165 | char *foundsym = NULL;
|
---|
| 166 | int foundpos = 0;
|
---|
| 167 |
|
---|
| 168 | spinlock_lock(&cmd_lock);
|
---|
| 169 |
|
---|
| 170 | if (!*startpos)
|
---|
| 171 | *startpos = cmd_head.next;
|
---|
| 172 |
|
---|
| 173 | for (;*startpos != &cmd_head;*startpos = (*startpos)->next) {
|
---|
| 174 | cmd_info_t *hlp;
|
---|
| 175 | hlp = list_get_instance(*startpos, cmd_info_t, link);
|
---|
| 176 |
|
---|
| 177 | curname = hlp->name;
|
---|
| 178 | if (strlen(curname) < namelen)
|
---|
| 179 | continue;
|
---|
| 180 | if (strncmp(curname, name, namelen) == 0) {
|
---|
| 181 | spinlock_unlock(&cmd_lock);
|
---|
| 182 | return curname+namelen;
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 | spinlock_unlock(&cmd_lock);
|
---|
| 186 | return NULL;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | /** Command completion of the commands
|
---|
| 191 | *
|
---|
| 192 | * @param name - string to match, changed to hint on exit
|
---|
| 193 | * @return number of found matches
|
---|
| 194 | */
|
---|
| 195 | static int cmdtab_compl(char *name)
|
---|
| 196 | {
|
---|
| 197 | char output[MAX_SYMBOL_NAME+1];
|
---|
| 198 | link_t *startpos = NULL;
|
---|
| 199 | const char *foundtxt;
|
---|
| 200 | int found = 0;
|
---|
| 201 | int i;
|
---|
| 202 |
|
---|
| 203 | output[0] = '\0';
|
---|
| 204 | while ((foundtxt = cmdtab_search_one(name, &startpos))) {
|
---|
| 205 | startpos = startpos->next;
|
---|
| 206 | if (!found)
|
---|
| 207 | strncpy(output, foundtxt, strlen(foundtxt)+1);
|
---|
| 208 | else {
|
---|
| 209 | for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++)
|
---|
| 210 | ;
|
---|
| 211 | output[i] = '\0';
|
---|
| 212 | }
|
---|
| 213 | found++;
|
---|
| 214 | }
|
---|
| 215 | if (!found)
|
---|
| 216 | return 0;
|
---|
| 217 |
|
---|
[3550c393] | 218 | if (found > 1 && !strlen(output)) {
|
---|
[0c8e692] | 219 | printf("\n");
|
---|
| 220 | startpos = NULL;
|
---|
| 221 | while ((foundtxt = cmdtab_search_one(name, &startpos))) {
|
---|
| 222 | cmd_info_t *hlp;
|
---|
| 223 | hlp = list_get_instance(startpos, cmd_info_t, link);
|
---|
| 224 | printf("%s - %s\n", hlp->name, hlp->description);
|
---|
| 225 | startpos = startpos->next;
|
---|
| 226 | }
|
---|
| 227 | }
|
---|
| 228 | strncpy(name, output, MAX_SYMBOL_NAME);
|
---|
| 229 | return found;
|
---|
| 230 |
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | static char * clever_readline(const char *prompt, chardev_t *input)
|
---|
| 234 | {
|
---|
| 235 | static int histposition = 0;
|
---|
| 236 |
|
---|
| 237 | char tmp[MAX_CMDLINE+1];
|
---|
| 238 | int curlen = 0, position = 0;
|
---|
| 239 | char *current = history[histposition];
|
---|
| 240 | int i;
|
---|
[af9a7c5] | 241 | char mod; /* Command Modifier */
|
---|
[0c8e692] | 242 | char c;
|
---|
| 243 |
|
---|
| 244 | printf("%s> ", prompt);
|
---|
| 245 | while (1) {
|
---|
| 246 | c = _getc(input);
|
---|
| 247 | if (c == '\n') {
|
---|
| 248 | putchar(c);
|
---|
| 249 | break;
|
---|
[93b84b3] | 250 | } if (c == '\b') { /* Backspace */
|
---|
[0c8e692] | 251 | if (position == 0)
|
---|
| 252 | continue;
|
---|
| 253 | for (i=position; i<curlen;i++)
|
---|
| 254 | current[i-1] = current[i];
|
---|
| 255 | curlen--;
|
---|
| 256 | position--;
|
---|
| 257 | putchar('\b');
|
---|
| 258 | for (i=position;i<curlen;i++)
|
---|
| 259 | putchar(current[i]);
|
---|
| 260 | putchar(' ');
|
---|
| 261 | rdln_print_c('\b',curlen-position+1);
|
---|
| 262 | continue;
|
---|
| 263 | }
|
---|
[93b84b3] | 264 | if (c == '\t') { /* Tabulator */
|
---|
[0c8e692] | 265 | int found;
|
---|
| 266 |
|
---|
| 267 | /* Move to the end of the word */
|
---|
| 268 | for (;position<curlen && current[position]!=' ';position++)
|
---|
| 269 | putchar(current[position]);
|
---|
| 270 | /* Copy to tmp last word */
|
---|
| 271 | for (i=position-1;i >= 0 && current[i]!=' ' ;i--)
|
---|
| 272 | ;
|
---|
| 273 | /* If word begins with * or &, skip it */
|
---|
| 274 | if (tmp[0] == '*' || tmp[0] == '&')
|
---|
| 275 | for (i=1;tmp[i];i++)
|
---|
| 276 | tmp[i-1] = tmp[i];
|
---|
| 277 | i++; /* I is at the start of the word */
|
---|
| 278 | strncpy(tmp, current+i, position-i+1);
|
---|
| 279 |
|
---|
| 280 | if (i==0) { /* Command completion */
|
---|
| 281 | found = cmdtab_compl(tmp);
|
---|
| 282 | } else { /* Symtab completion */
|
---|
| 283 | found = symtab_compl(tmp);
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | if (found == 0)
|
---|
| 287 | continue;
|
---|
| 288 | for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++)
|
---|
| 289 | insert_char(current, tmp[i], i+position);
|
---|
[3550c393] | 290 |
|
---|
| 291 | if (strlen(tmp) || found==1) { /* If we have a hint */
|
---|
| 292 | for (i=position;i<curlen;i++)
|
---|
[0c8e692] | 293 | putchar(current[i]);
|
---|
| 294 | position += strlen(tmp);
|
---|
| 295 | /* Add space to end */
|
---|
[3550c393] | 296 | if (found == 1 && position == curlen && \
|
---|
| 297 | curlen < MAX_CMDLINE) {
|
---|
[0c8e692] | 298 | current[position] = ' ';
|
---|
| 299 | curlen++;
|
---|
| 300 | position++;
|
---|
| 301 | putchar(' ');
|
---|
| 302 | }
|
---|
[3550c393] | 303 | } else { /* No hint, table was printed */
|
---|
[0c8e692] | 304 | printf("%s> ", prompt);
|
---|
| 305 | for (i=0; i<curlen;i++)
|
---|
| 306 | putchar(current[i]);
|
---|
| 307 | position += strlen(tmp);
|
---|
| 308 | }
|
---|
| 309 | rdln_print_c('\b', curlen-position);
|
---|
| 310 | continue;
|
---|
| 311 | }
|
---|
[93b84b3] | 312 | if (c == 0x1b) { /* Special command */
|
---|
[af9a7c5] | 313 | mod = _getc(input);
|
---|
[0c8e692] | 314 | c = _getc(input);
|
---|
[af9a7c5] | 315 |
|
---|
| 316 | if (mod != 0x5b && mod != 0x4f)
|
---|
[0c8e692] | 317 | continue;
|
---|
[af9a7c5] | 318 |
|
---|
| 319 | if (c == 0x33 && _getc(input) == 0x7e) {
|
---|
[93b84b3] | 320 | /* Delete */
|
---|
[af9a7c5] | 321 | if (position == curlen)
|
---|
| 322 | continue;
|
---|
| 323 | for (i=position+1; i<curlen;i++) {
|
---|
| 324 | putchar(current[i]);
|
---|
| 325 | current[i-1] = current[i];
|
---|
| 326 | }
|
---|
| 327 | putchar(' ');
|
---|
| 328 | rdln_print_c('\b',curlen-position);
|
---|
| 329 | curlen--;
|
---|
| 330 | }
|
---|
| 331 | else if (c == 0x48) { /* Home */
|
---|
| 332 | rdln_print_c('\b',position);
|
---|
| 333 | position = 0;
|
---|
| 334 | }
|
---|
[93b84b3] | 335 | else if (c == 0x46) { /* End */
|
---|
[af9a7c5] | 336 | for (i=position;i<curlen;i++)
|
---|
| 337 | putchar(current[i]);
|
---|
| 338 | position = curlen;
|
---|
| 339 | }
|
---|
| 340 | else if (c == 0x44) { /* Left */
|
---|
[0c8e692] | 341 | if (position > 0) {
|
---|
| 342 | putchar('\b');
|
---|
| 343 | position--;
|
---|
| 344 | }
|
---|
| 345 | continue;
|
---|
| 346 | }
|
---|
[af9a7c5] | 347 | else if (c == 0x43) { /* Right */
|
---|
[0c8e692] | 348 | if (position < curlen) {
|
---|
| 349 | putchar(current[position]);
|
---|
| 350 | position++;
|
---|
| 351 | }
|
---|
| 352 | continue;
|
---|
| 353 | }
|
---|
[af9a7c5] | 354 | else if (c == 0x41 || c == 0x42) {
|
---|
| 355 | /* Up,down */
|
---|
[0c8e692] | 356 | rdln_print_c('\b',position);
|
---|
| 357 | rdln_print_c(' ',curlen);
|
---|
| 358 | rdln_print_c('\b',curlen);
|
---|
[93b84b3] | 359 | if (c == 0x41) /* Up */
|
---|
[0c8e692] | 360 | histposition--;
|
---|
| 361 | else
|
---|
| 362 | histposition++;
|
---|
| 363 | if (histposition < 0)
|
---|
| 364 | histposition = KCONSOLE_HISTORY -1 ;
|
---|
| 365 | else
|
---|
| 366 | histposition = histposition % KCONSOLE_HISTORY;
|
---|
| 367 | current = history[histposition];
|
---|
| 368 | printf("%s", current);
|
---|
| 369 | curlen = strlen(current);
|
---|
| 370 | position = curlen;
|
---|
| 371 | continue;
|
---|
| 372 | }
|
---|
| 373 | continue;
|
---|
| 374 | }
|
---|
| 375 | if (curlen >= MAX_CMDLINE)
|
---|
| 376 | continue;
|
---|
| 377 |
|
---|
| 378 | insert_char(current, c, position);
|
---|
| 379 |
|
---|
| 380 | curlen++;
|
---|
| 381 | for (i=position;i<curlen;i++)
|
---|
| 382 | putchar(current[i]);
|
---|
| 383 | position++;
|
---|
| 384 | rdln_print_c('\b',curlen-position);
|
---|
| 385 | }
|
---|
[ba276f7] | 386 | if (curlen) {
|
---|
| 387 | histposition++;
|
---|
| 388 | histposition = histposition % KCONSOLE_HISTORY;
|
---|
| 389 | }
|
---|
[0c8e692] | 390 | current[curlen] = '\0';
|
---|
| 391 | return current;
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[2677758] | 394 | /** Kernel console managing thread.
|
---|
| 395 | *
|
---|
| 396 | * @param arg Not used.
|
---|
| 397 | */
|
---|
| 398 | void kconsole(void *arg)
|
---|
| 399 | {
|
---|
[ff3b3197] | 400 | cmd_info_t *cmd_info;
|
---|
| 401 | count_t len;
|
---|
[0c8e692] | 402 | char *cmdline;
|
---|
[2677758] | 403 |
|
---|
| 404 | if (!stdin) {
|
---|
| 405 | printf("%s: no stdin\n", __FUNCTION__);
|
---|
| 406 | return;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | while (true) {
|
---|
[0c8e692] | 410 | cmdline = clever_readline(__FUNCTION__, stdin);
|
---|
| 411 | len = strlen(cmdline);
|
---|
| 412 | if (!len)
|
---|
[f4338d2] | 413 | continue;
|
---|
[ff3b3197] | 414 | cmd_info = parse_cmdline(cmdline, len);
|
---|
[f4338d2] | 415 | if (!cmd_info)
|
---|
[ff3b3197] | 416 | continue;
|
---|
| 417 | (void) cmd_info->func(cmd_info->argv);
|
---|
[2677758] | 418 | }
|
---|
| 419 | }
|
---|
[ff3b3197] | 420 |
|
---|
[91c78c9] | 421 | static int parse_int_arg(char *text, size_t len, __native *result)
|
---|
| 422 | {
|
---|
| 423 | char symname[MAX_SYMBOL_NAME];
|
---|
| 424 | __address symaddr;
|
---|
| 425 | bool isaddr = false;
|
---|
[e5fcf00] | 426 | bool isptr = false;
|
---|
[91c78c9] | 427 |
|
---|
| 428 | /* If we get a name, try to find it in symbol table */
|
---|
| 429 | if (text[0] < '0' | text[0] > '9') {
|
---|
| 430 | if (text[0] == '&') {
|
---|
| 431 | isaddr = true;
|
---|
| 432 | text++;len--;
|
---|
[e5fcf00] | 433 | } else if (text[0] == '*') {
|
---|
| 434 | isptr = true;
|
---|
| 435 | text++;len--;
|
---|
[91c78c9] | 436 | }
|
---|
| 437 | strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME));
|
---|
| 438 | symaddr = get_symbol_addr(symname);
|
---|
| 439 | if (!symaddr) {
|
---|
| 440 | printf("Symbol %s not found.\n",symname);
|
---|
| 441 | return -1;
|
---|
| 442 | }
|
---|
| 443 | if (symaddr == (__address) -1) {
|
---|
| 444 | printf("Duplicate symbol %s.\n",symname);
|
---|
| 445 | symtab_print_search(symname);
|
---|
| 446 | return -1;
|
---|
| 447 | }
|
---|
| 448 | if (isaddr)
|
---|
| 449 | *result = (__native)symaddr;
|
---|
[e5fcf00] | 450 | else if (isptr)
|
---|
| 451 | *result = **((__native **)symaddr);
|
---|
[91c78c9] | 452 | else
|
---|
| 453 | *result = *((__native *)symaddr);
|
---|
| 454 | } else /* It's a number - convert it */
|
---|
| 455 | *result = atoi(text);
|
---|
| 456 | return 0;
|
---|
| 457 | }
|
---|
| 458 |
|
---|
[ff3b3197] | 459 | /** Parse command line.
|
---|
| 460 | *
|
---|
| 461 | * @param cmdline Command line as read from input device.
|
---|
| 462 | * @param len Command line length.
|
---|
| 463 | *
|
---|
| 464 | * @return Structure describing the command.
|
---|
| 465 | */
|
---|
| 466 | cmd_info_t *parse_cmdline(char *cmdline, size_t len)
|
---|
| 467 | {
|
---|
| 468 | index_t start = 0, end = 0;
|
---|
| 469 | cmd_info_t *cmd = NULL;
|
---|
| 470 | link_t *cur;
|
---|
| 471 | ipl_t ipl;
|
---|
| 472 | int i;
|
---|
| 473 |
|
---|
[f4338d2] | 474 | if (!parse_argument(cmdline, len, &start, &end)) {
|
---|
[ff3b3197] | 475 | /* Command line did not contain alphanumeric word. */
|
---|
| 476 | return NULL;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | spinlock_lock(&cmd_lock);
|
---|
| 480 |
|
---|
| 481 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) {
|
---|
| 482 | cmd_info_t *hlp;
|
---|
| 483 |
|
---|
| 484 | hlp = list_get_instance(cur, cmd_info_t, link);
|
---|
| 485 | spinlock_lock(&hlp->lock);
|
---|
| 486 |
|
---|
[f4338d2] | 487 | if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) {
|
---|
[ff3b3197] | 488 | cmd = hlp;
|
---|
| 489 | break;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 | spinlock_unlock(&hlp->lock);
|
---|
| 493 | }
|
---|
| 494 |
|
---|
| 495 | spinlock_unlock(&cmd_lock);
|
---|
| 496 |
|
---|
| 497 | if (!cmd) {
|
---|
| 498 | /* Unknown command. */
|
---|
[f4338d2] | 499 | printf("Unknown command.\n");
|
---|
[ff3b3197] | 500 | return NULL;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /* cmd == hlp is locked */
|
---|
| 504 |
|
---|
| 505 | /*
|
---|
| 506 | * The command line must be further analyzed and
|
---|
| 507 | * the parameters therefrom must be matched and
|
---|
| 508 | * converted to those specified in the cmd info
|
---|
| 509 | * structure.
|
---|
| 510 | */
|
---|
[f4338d2] | 511 |
|
---|
| 512 | for (i = 0; i < cmd->argc; i++) {
|
---|
| 513 | char *buf;
|
---|
| 514 | start = end + 1;
|
---|
| 515 | if (!parse_argument(cmdline, len, &start, &end)) {
|
---|
| 516 | printf("Too few arguments.\n");
|
---|
| 517 | spinlock_unlock(&cmd->lock);
|
---|
| 518 | return NULL;
|
---|
| 519 | }
|
---|
| 520 |
|
---|
| 521 | switch (cmd->argv[i].type) {
|
---|
[6e716a59] | 522 | case ARG_TYPE_STRING:
|
---|
[f4338d2] | 523 | buf = cmd->argv[i].buffer;
|
---|
[91c78c9] | 524 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len));
|
---|
[f4338d2] | 525 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0';
|
---|
| 526 | break;
|
---|
[91c78c9] | 527 | case ARG_TYPE_INT:
|
---|
| 528 | if (parse_int_arg(cmdline+start, end-start+1,
|
---|
| 529 | &cmd->argv[i].intval))
|
---|
| 530 | return NULL;
|
---|
[6e716a59] | 531 | break;
|
---|
[91c78c9] | 532 | case ARG_TYPE_VAR:
|
---|
| 533 | if (start != end && cmdline[start] == '"' && cmdline[end] == '"') {
|
---|
| 534 | buf = cmd->argv[i].buffer;
|
---|
| 535 | strncpy(buf, (const char *) &cmdline[start+1],
|
---|
| 536 | min((end-start), cmd->argv[i].len));
|
---|
| 537 | buf[min((end - start), cmd->argv[i].len - 1)] = '\0';
|
---|
| 538 | cmd->argv[i].intval = (__native) buf;
|
---|
| 539 | cmd->argv[i].vartype = ARG_TYPE_STRING;
|
---|
| 540 | } else if (!parse_int_arg(cmdline+start, end-start+1,
|
---|
| 541 | &cmd->argv[i].intval))
|
---|
| 542 | cmd->argv[i].vartype = ARG_TYPE_INT;
|
---|
| 543 | else {
|
---|
| 544 | printf("Unrecognized variable argument.\n");
|
---|
| 545 | return NULL;
|
---|
[6e716a59] | 546 | }
|
---|
[91c78c9] | 547 | break;
|
---|
[6e716a59] | 548 | case ARG_TYPE_INVALID:
|
---|
| 549 | default:
|
---|
| 550 | printf("invalid argument type\n");
|
---|
| 551 | return NULL;
|
---|
[f4338d2] | 552 | break;
|
---|
| 553 | }
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | start = end + 1;
|
---|
| 557 | if (parse_argument(cmdline, len, &start, &end)) {
|
---|
| 558 | printf("Too many arguments.\n");
|
---|
| 559 | spinlock_unlock(&cmd->lock);
|
---|
| 560 | return NULL;
|
---|
| 561 | }
|
---|
[ff3b3197] | 562 |
|
---|
| 563 | spinlock_unlock(&cmd->lock);
|
---|
| 564 | return cmd;
|
---|
| 565 | }
|
---|
| 566 |
|
---|
[f4338d2] | 567 | /** Parse argument.
|
---|
| 568 | *
|
---|
| 569 | * Find start and end positions of command line argument.
|
---|
| 570 | *
|
---|
| 571 | * @param cmdline Command line as read from the input device.
|
---|
| 572 | * @param len Number of characters in cmdline.
|
---|
| 573 | * @param start On entry, 'start' contains pointer to the index
|
---|
| 574 | * of first unprocessed character of cmdline.
|
---|
| 575 | * On successful exit, it marks beginning of the next argument.
|
---|
| 576 | * @param end Undefined on entry. On exit, 'end' points to the last character
|
---|
| 577 | * of the next argument.
|
---|
| 578 | *
|
---|
| 579 | * @return false on failure, true on success.
|
---|
| 580 | */
|
---|
| 581 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end)
|
---|
| 582 | {
|
---|
| 583 | int i;
|
---|
| 584 | bool found_start = false;
|
---|
| 585 |
|
---|
| 586 | ASSERT(start != NULL);
|
---|
| 587 | ASSERT(end != NULL);
|
---|
| 588 |
|
---|
| 589 | for (i = *start; i < len; i++) {
|
---|
| 590 | if (!found_start) {
|
---|
| 591 | if (is_white(cmdline[i]))
|
---|
| 592 | (*start)++;
|
---|
| 593 | else
|
---|
| 594 | found_start = true;
|
---|
| 595 | } else {
|
---|
| 596 | if (is_white(cmdline[i]))
|
---|
| 597 | break;
|
---|
| 598 | }
|
---|
| 599 | }
|
---|
| 600 | *end = i - 1;
|
---|
| 601 |
|
---|
| 602 | return found_start;
|
---|
| 603 | }
|
---|