Rev 3343 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 510 | jermar | 1 | /* |
| 2071 | jermar | 2 | * Copyright (c) 2005 Jakub Jermar |
| 510 | 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 | |||
| 1888 | jermar | 29 | /** @addtogroup genericconsole |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | |||
| 1264 | jermar | 33 | /** |
| 34 | * @file kconsole.c |
||
| 35 | * @brief Kernel console. |
||
| 36 | * |
||
| 37 | * This file contains kernel thread managing the kernel console. |
||
| 38 | */ |
||
| 39 | |||
| 518 | jermar | 40 | #include <console/kconsole.h> |
| 510 | jermar | 41 | #include <console/console.h> |
| 42 | #include <console/chardev.h> |
||
| 596 | jermar | 43 | #include <console/cmd.h> |
| 510 | jermar | 44 | #include <print.h> |
| 517 | jermar | 45 | #include <panic.h> |
| 510 | jermar | 46 | #include <arch/types.h> |
| 788 | jermar | 47 | #include <adt/list.h> |
| 517 | jermar | 48 | #include <arch.h> |
| 49 | #include <macros.h> |
||
| 518 | jermar | 50 | #include <debug.h> |
| 596 | jermar | 51 | #include <func.h> |
| 582 | palkovsky | 52 | #include <symtab.h> |
| 609 | palkovsky | 53 | #include <macros.h> |
| 510 | jermar | 54 | |
| 517 | jermar | 55 | /** Simple kernel console. |
| 56 | * |
||
| 57 | * The console is realized by kernel thread kconsole. |
||
| 518 | jermar | 58 | * It doesn't understand any useful command on its own, |
| 59 | * but makes it possible for other kernel subsystems to |
||
| 517 | jermar | 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 | |||
| 623 | jermar | 78 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
| 624 | jermar | 79 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
| 517 | jermar | 80 | |
| 81 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
||
| 2108 | jermar | 82 | static bool parse_argument(char *cmdline, size_t len, index_t *start, |
| 83 | index_t *end); |
||
| 601 | palkovsky | 84 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
| 517 | jermar | 85 | |
| 86 | /** Initialize kconsole data structures. */ |
||
| 87 | void kconsole_init(void) |
||
| 88 | { |
||
| 601 | palkovsky | 89 | int i; |
| 90 | |||
| 596 | jermar | 91 | cmd_init(); |
| 2108 | jermar | 92 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
| 601 | palkovsky | 93 | history[i][0] = '\0'; |
| 517 | jermar | 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 | } |
||
| 2108 | jermar | 131 | if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name), |
| 132 | strlen(hlp->name))) == 0)) { |
||
| 517 | jermar | 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 | } |
||
| 152 | |||
| 635 | palkovsky | 153 | /** Print count times a character */ |
| 601 | palkovsky | 154 | static void rdln_print_c(char ch, int count) |
| 155 | { |
||
| 156 | int i; |
||
| 2108 | jermar | 157 | for (i = 0; i < count; i++) |
| 601 | palkovsky | 158 | putchar(ch); |
| 159 | } |
||
| 160 | |||
| 635 | palkovsky | 161 | /** Insert character to string */ |
| 601 | palkovsky | 162 | static void insert_char(char *str, char ch, int pos) |
| 163 | { |
||
| 164 | int i; |
||
| 165 | |||
| 2108 | jermar | 166 | for (i = strlen(str); i > pos; i--) |
| 167 | str[i] = str[i - 1]; |
||
| 601 | palkovsky | 168 | str[pos] = ch; |
| 169 | } |
||
| 170 | |||
| 640 | jermar | 171 | /** Try to find a command beginning with prefix */ |
| 3193 | jermar | 172 | static const char *cmdtab_search_one(const char *name,link_t **startpos) |
| 601 | palkovsky | 173 | { |
| 2113 | decky | 174 | size_t namelen = strlen(name); |
| 601 | palkovsky | 175 | const char *curname; |
| 176 | |||
| 177 | spinlock_lock(&cmd_lock); |
||
| 178 | |||
| 179 | if (!*startpos) |
||
| 180 | *startpos = cmd_head.next; |
||
| 181 | |||
| 2108 | jermar | 182 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
| 601 | palkovsky | 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 | { |
||
| 3193 | jermar | 206 | static char output[MAX_SYMBOL_NAME + 1]; |
| 601 | palkovsky | 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) |
||
| 3193 | jermar | 216 | strncpy(output, foundtxt, strlen(foundtxt) + 1); |
| 601 | palkovsky | 217 | else { |
| 2108 | jermar | 218 | for (i = 0; output[i] && foundtxt[i] && |
| 219 | output[i] == foundtxt[i]; i++) |
||
| 601 | palkovsky | 220 | ; |
| 221 | output[i] = '\0'; |
||
| 222 | } |
||
| 223 | found++; |
||
| 224 | } |
||
| 225 | if (!found) |
||
| 226 | return 0; |
||
| 227 | |||
| 602 | palkovsky | 228 | if (found > 1 && !strlen(output)) { |
| 601 | palkovsky | 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 | |||
| 3193 | jermar | 243 | static char *clever_readline(const char *prompt, chardev_t *input) |
| 601 | palkovsky | 244 | { |
| 245 | static int histposition = 0; |
||
| 246 | |||
| 3193 | jermar | 247 | static char tmp[MAX_CMDLINE + 1]; |
| 601 | palkovsky | 248 | int curlen = 0, position = 0; |
| 249 | char *current = history[histposition]; |
||
| 250 | int i; |
||
| 606 | palkovsky | 251 | char mod; /* Command Modifier */ |
| 601 | palkovsky | 252 | char c; |
| 253 | |||
| 254 | printf("%s> ", prompt); |
||
| 255 | while (1) { |
||
| 256 | c = _getc(input); |
||
| 257 | if (c == '\n') { |
||
| 258 | putchar(c); |
||
| 259 | break; |
||
| 3193 | jermar | 260 | } |
| 261 | if (c == '\b') { /* Backspace */ |
||
| 601 | palkovsky | 262 | if (position == 0) |
| 263 | continue; |
||
| 2108 | jermar | 264 | for (i = position; i < curlen; i++) |
| 265 | current[i - 1] = current[i]; |
||
| 601 | palkovsky | 266 | curlen--; |
| 267 | position--; |
||
| 268 | putchar('\b'); |
||
| 2108 | jermar | 269 | for (i = position; i < curlen; i++) |
| 601 | palkovsky | 270 | putchar(current[i]); |
| 271 | putchar(' '); |
||
| 2108 | jermar | 272 | rdln_print_c('\b', curlen - position + 1); |
| 601 | palkovsky | 273 | continue; |
| 274 | } |
||
| 607 | palkovsky | 275 | if (c == '\t') { /* Tabulator */ |
| 601 | palkovsky | 276 | int found; |
| 277 | |||
| 278 | /* Move to the end of the word */ |
||
| 2108 | jermar | 279 | for (; position < curlen && current[position] != ' '; |
| 280 | position++) |
||
| 601 | palkovsky | 281 | putchar(current[position]); |
| 282 | /* Copy to tmp last word */ |
||
| 2108 | jermar | 283 | for (i = position - 1; i >= 0 && current[i] != ' '; i--) |
| 601 | palkovsky | 284 | ; |
| 285 | /* If word begins with * or &, skip it */ |
||
| 286 | if (tmp[0] == '*' || tmp[0] == '&') |
||
| 2108 | jermar | 287 | for (i = 1; tmp[i]; i++) |
| 288 | tmp[i - 1] = tmp[i]; |
||
| 601 | palkovsky | 289 | i++; /* I is at the start of the word */ |
| 2108 | jermar | 290 | strncpy(tmp, current + i, position - i + 1); |
| 601 | palkovsky | 291 | |
| 2108 | jermar | 292 | if (i == 0) { /* Command completion */ |
| 601 | palkovsky | 293 | found = cmdtab_compl(tmp); |
| 294 | } else { /* Symtab completion */ |
||
| 295 | found = symtab_compl(tmp); |
||
| 296 | } |
||
| 297 | |||
| 298 | if (found == 0) |
||
| 299 | continue; |
||
| 2108 | jermar | 300 | for (i = 0; tmp[i] && curlen < MAX_CMDLINE; |
| 301 | i++, curlen++) |
||
| 302 | insert_char(current, tmp[i], i + position); |
||
| 602 | palkovsky | 303 | |
| 2108 | jermar | 304 | if (strlen(tmp) || found == 1) { /* If we have a hint */ |
| 305 | for (i = position; i < curlen; i++) |
||
| 601 | palkovsky | 306 | putchar(current[i]); |
| 307 | position += strlen(tmp); |
||
| 308 | /* Add space to end */ |
||
| 2108 | jermar | 309 | if (found == 1 && position == curlen && |
| 602 | palkovsky | 310 | curlen < MAX_CMDLINE) { |
| 601 | palkovsky | 311 | current[position] = ' '; |
| 312 | curlen++; |
||
| 313 | position++; |
||
| 314 | putchar(' '); |
||
| 315 | } |
||
| 602 | palkovsky | 316 | } else { /* No hint, table was printed */ |
| 601 | palkovsky | 317 | printf("%s> ", prompt); |
| 2108 | jermar | 318 | for (i = 0; i < curlen; i++) |
| 601 | palkovsky | 319 | putchar(current[i]); |
| 320 | position += strlen(tmp); |
||
| 321 | } |
||
| 2108 | jermar | 322 | rdln_print_c('\b', curlen - position); |
| 601 | palkovsky | 323 | continue; |
| 324 | } |
||
| 607 | palkovsky | 325 | if (c == 0x1b) { /* Special command */ |
| 606 | palkovsky | 326 | mod = _getc(input); |
| 601 | palkovsky | 327 | c = _getc(input); |
| 606 | palkovsky | 328 | |
| 329 | if (mod != 0x5b && mod != 0x4f) |
||
| 601 | palkovsky | 330 | continue; |
| 606 | palkovsky | 331 | |
| 332 | if (c == 0x33 && _getc(input) == 0x7e) { |
||
| 607 | palkovsky | 333 | /* Delete */ |
| 606 | palkovsky | 334 | if (position == curlen) |
| 335 | continue; |
||
| 2108 | jermar | 336 | for (i = position + 1; i < curlen; i++) { |
| 606 | palkovsky | 337 | putchar(current[i]); |
| 2108 | jermar | 338 | current[i - 1] = current[i]; |
| 606 | palkovsky | 339 | } |
| 340 | putchar(' '); |
||
| 2108 | jermar | 341 | rdln_print_c('\b', curlen - position); |
| 606 | palkovsky | 342 | curlen--; |
| 2108 | jermar | 343 | } else if (c == 0x48) { /* Home */ |
| 344 | rdln_print_c('\b', position); |
||
| 606 | palkovsky | 345 | position = 0; |
| 2108 | jermar | 346 | } else if (c == 0x46) { /* End */ |
| 347 | for (i = position; i < curlen; i++) |
||
| 606 | palkovsky | 348 | putchar(current[i]); |
| 349 | position = curlen; |
||
| 2108 | jermar | 350 | } else if (c == 0x44) { /* Left */ |
| 601 | palkovsky | 351 | if (position > 0) { |
| 352 | putchar('\b'); |
||
| 353 | position--; |
||
| 354 | } |
||
| 355 | continue; |
||
| 2108 | jermar | 356 | } else if (c == 0x43) { /* Right */ |
| 601 | palkovsky | 357 | if (position < curlen) { |
| 358 | putchar(current[position]); |
||
| 359 | position++; |
||
| 360 | } |
||
| 361 | continue; |
||
| 2108 | jermar | 362 | } else if (c == 0x41 || c == 0x42) { |
| 363 | /* Up, down */ |
||
| 364 | rdln_print_c('\b', position); |
||
| 365 | rdln_print_c(' ', curlen); |
||
| 366 | rdln_print_c('\b', curlen); |
||
| 607 | palkovsky | 367 | if (c == 0x41) /* Up */ |
| 601 | palkovsky | 368 | histposition--; |
| 369 | else |
||
| 370 | histposition++; |
||
| 2108 | jermar | 371 | if (histposition < 0) { |
| 372 | histposition = KCONSOLE_HISTORY - 1; |
||
| 373 | } else { |
||
| 374 | histposition = |
||
| 375 | histposition % KCONSOLE_HISTORY; |
||
| 376 | } |
||
| 601 | palkovsky | 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++; |
||
| 2108 | jermar | 391 | for (i = position; i < curlen; i++) |
| 601 | palkovsky | 392 | putchar(current[i]); |
| 393 | position++; |
||
| 2108 | jermar | 394 | rdln_print_c('\b',curlen - position); |
| 601 | palkovsky | 395 | } |
| 603 | palkovsky | 396 | if (curlen) { |
| 397 | histposition++; |
||
| 398 | histposition = histposition % KCONSOLE_HISTORY; |
||
| 399 | } |
||
| 601 | palkovsky | 400 | current[curlen] = '\0'; |
| 401 | return current; |
||
| 402 | } |
||
| 403 | |||
| 3742 | rimsky | 404 | /** Kernel console prompt. |
| 510 | jermar | 405 | * |
| 1708 | jermar | 406 | * @param prompt Kernel console prompt (e.g kconsole/panic). |
| 3742 | rimsky | 407 | * @param msg Message to display in the beginning. |
| 408 | * @param kcon Wait for keypress to show the prompt |
||
| 409 | * and never exit. |
||
| 410 | * |
||
| 510 | jermar | 411 | */ |
| 3742 | rimsky | 412 | void kconsole(char *prompt, char *msg, bool kcon) |
| 510 | jermar | 413 | { |
| 517 | jermar | 414 | cmd_info_t *cmd_info; |
| 415 | count_t len; |
||
| 601 | palkovsky | 416 | char *cmdline; |
| 510 | jermar | 417 | |
| 418 | if (!stdin) { |
||
| 3742 | rimsky | 419 | LOG("No stdin for kernel console"); |
| 510 | jermar | 420 | return; |
| 421 | } |
||
| 422 | |||
| 3742 | rimsky | 423 | if (msg) |
| 424 | printf("%s", msg); |
||
| 425 | |||
| 426 | if (kcon) |
||
| 427 | _getc(stdin); |
||
| 428 | |||
| 510 | jermar | 429 | while (true) { |
| 2113 | decky | 430 | cmdline = clever_readline((char *) prompt, stdin); |
| 601 | palkovsky | 431 | len = strlen(cmdline); |
| 432 | if (!len) |
||
| 518 | jermar | 433 | continue; |
| 3742 | rimsky | 434 | |
| 517 | jermar | 435 | cmd_info = parse_cmdline(cmdline, len); |
| 518 | jermar | 436 | if (!cmd_info) |
| 517 | jermar | 437 | continue; |
| 3742 | rimsky | 438 | |
| 439 | if ((!kcon) |
||
| 440 | && (strncmp(cmd_info->name, "exit", min(strlen(cmd_info->name), 5)) == 0)) |
||
| 609 | palkovsky | 441 | break; |
| 3742 | rimsky | 442 | |
| 517 | jermar | 443 | (void) cmd_info->func(cmd_info->argv); |
| 510 | jermar | 444 | } |
| 445 | } |
||
| 517 | jermar | 446 | |
| 3742 | rimsky | 447 | /** Kernel console managing thread. |
| 448 | * |
||
| 449 | */ |
||
| 450 | void kconsole_thread(void *data) |
||
| 451 | { |
||
| 452 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
||
| 453 | } |
||
| 454 | |||
| 1780 | jermar | 455 | static int parse_int_arg(char *text, size_t len, unative_t *result) |
| 585 | palkovsky | 456 | { |
| 1972 | jermar | 457 | static char symname[MAX_SYMBOL_NAME]; |
| 1780 | jermar | 458 | uintptr_t symaddr; |
| 585 | palkovsky | 459 | bool isaddr = false; |
| 589 | palkovsky | 460 | bool isptr = false; |
| 585 | palkovsky | 461 | |
| 462 | /* If we get a name, try to find it in symbol table */ |
||
| 931 | palkovsky | 463 | if (text[0] == '&') { |
| 464 | isaddr = true; |
||
| 2108 | jermar | 465 | text++; |
| 466 | len--; |
||
| 931 | palkovsky | 467 | } else if (text[0] == '*') { |
| 468 | isptr = true; |
||
| 2108 | jermar | 469 | text++; |
| 470 | len--; |
||
| 931 | palkovsky | 471 | } |
| 614 | palkovsky | 472 | if (text[0] < '0' || text[0] > '9') { |
| 2108 | jermar | 473 | strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME)); |
| 585 | palkovsky | 474 | symaddr = get_symbol_addr(symname); |
| 475 | if (!symaddr) { |
||
| 2108 | jermar | 476 | printf("Symbol %s not found.\n", symname); |
| 585 | palkovsky | 477 | return -1; |
| 478 | } |
||
| 1780 | jermar | 479 | if (symaddr == (uintptr_t) -1) { |
| 2108 | jermar | 480 | printf("Duplicate symbol %s.\n", symname); |
| 585 | palkovsky | 481 | symtab_print_search(symname); |
| 482 | return -1; |
||
| 483 | } |
||
| 932 | palkovsky | 484 | if (isaddr) |
| 1780 | jermar | 485 | *result = (unative_t)symaddr; |
| 932 | palkovsky | 486 | else if (isptr) |
| 1780 | jermar | 487 | *result = **((unative_t **)symaddr); |
| 932 | palkovsky | 488 | else |
| 1780 | jermar | 489 | *result = *((unative_t *)symaddr); |
| 932 | palkovsky | 490 | } else { /* It's a number - convert it */ |
| 585 | palkovsky | 491 | *result = atoi(text); |
| 932 | palkovsky | 492 | if (isptr) |
| 1780 | jermar | 493 | *result = *((unative_t *)*result); |
| 932 | palkovsky | 494 | } |
| 931 | palkovsky | 495 | |
| 585 | palkovsky | 496 | return 0; |
| 497 | } |
||
| 498 | |||
| 517 | jermar | 499 | /** Parse command line. |
| 500 | * |
||
| 501 | * @param cmdline Command line as read from input device. |
||
| 502 | * @param len Command line length. |
||
| 503 | * |
||
| 504 | * @return Structure describing the command. |
||
| 505 | */ |
||
| 506 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
||
| 507 | { |
||
| 508 | index_t start = 0, end = 0; |
||
| 509 | cmd_info_t *cmd = NULL; |
||
| 510 | link_t *cur; |
||
| 2113 | decky | 511 | count_t i; |
| 668 | bondari | 512 | int error = 0; |
| 517 | jermar | 513 | |
| 518 | jermar | 514 | if (!parse_argument(cmdline, len, &start, &end)) { |
| 517 | jermar | 515 | /* Command line did not contain alphanumeric word. */ |
| 516 | return NULL; |
||
| 517 | } |
||
| 518 | |||
| 519 | spinlock_lock(&cmd_lock); |
||
| 520 | |||
| 521 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
| 522 | cmd_info_t *hlp; |
||
| 523 | |||
| 524 | hlp = list_get_instance(cur, cmd_info_t, link); |
||
| 525 | spinlock_lock(&hlp->lock); |
||
| 526 | |||
| 635 | palkovsky | 527 | if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
| 2108 | jermar | 528 | end - start + 1)) == 0) { |
| 517 | jermar | 529 | cmd = hlp; |
| 530 | break; |
||
| 531 | } |
||
| 532 | |||
| 533 | spinlock_unlock(&hlp->lock); |
||
| 534 | } |
||
| 535 | |||
| 536 | spinlock_unlock(&cmd_lock); |
||
| 537 | |||
| 538 | if (!cmd) { |
||
| 539 | /* Unknown command. */ |
||
| 518 | jermar | 540 | printf("Unknown command.\n"); |
| 517 | jermar | 541 | return NULL; |
| 542 | } |
||
| 543 | |||
| 544 | /* cmd == hlp is locked */ |
||
| 545 | |||
| 546 | /* |
||
| 547 | * The command line must be further analyzed and |
||
| 548 | * the parameters therefrom must be matched and |
||
| 549 | * converted to those specified in the cmd info |
||
| 550 | * structure. |
||
| 551 | */ |
||
| 518 | jermar | 552 | |
| 553 | for (i = 0; i < cmd->argc; i++) { |
||
| 554 | char *buf; |
||
| 555 | start = end + 1; |
||
| 556 | if (!parse_argument(cmdline, len, &start, &end)) { |
||
| 557 | printf("Too few arguments.\n"); |
||
| 558 | spinlock_unlock(&cmd->lock); |
||
| 559 | return NULL; |
||
| 560 | } |
||
| 561 | |||
| 668 | bondari | 562 | error = 0; |
| 518 | jermar | 563 | switch (cmd->argv[i].type) { |
| 582 | palkovsky | 564 | case ARG_TYPE_STRING: |
| 2113 | decky | 565 | buf = (char *) cmd->argv[i].buffer; |
| 566 | strncpy(buf, (const char *) &cmdline[start], |
||
| 2108 | jermar | 567 | min((end - start) + 2, cmd->argv[i].len)); |
| 3193 | jermar | 568 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = |
| 569 | '\0'; |
||
| 518 | jermar | 570 | break; |
| 585 | palkovsky | 571 | case ARG_TYPE_INT: |
| 2108 | jermar | 572 | if (parse_int_arg(cmdline + start, end - start + 1, |
| 573 | &cmd->argv[i].intval)) |
||
| 668 | bondari | 574 | error = 1; |
| 518 | jermar | 575 | break; |
| 585 | palkovsky | 576 | case ARG_TYPE_VAR: |
| 2108 | jermar | 577 | if (start != end && cmdline[start] == '"' && |
| 578 | cmdline[end] == '"') { |
||
| 2113 | decky | 579 | buf = (char *) cmd->argv[i].buffer; |
| 2108 | jermar | 580 | strncpy(buf, (const char *) &cmdline[start + 1], |
| 581 | min((end-start), cmd->argv[i].len)); |
||
| 582 | buf[min((end - start), cmd->argv[i].len - 1)] = |
||
| 583 | '\0'; |
||
| 1780 | jermar | 584 | cmd->argv[i].intval = (unative_t) buf; |
| 585 | palkovsky | 585 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
| 3193 | jermar | 586 | } else if (!parse_int_arg(cmdline + start, |
| 587 | end - start + 1, &cmd->argv[i].intval)) { |
||
| 585 | palkovsky | 588 | cmd->argv[i].vartype = ARG_TYPE_INT; |
| 2108 | jermar | 589 | } else { |
| 585 | palkovsky | 590 | printf("Unrecognized variable argument.\n"); |
| 668 | bondari | 591 | error = 1; |
| 582 | palkovsky | 592 | } |
| 585 | palkovsky | 593 | break; |
| 582 | palkovsky | 594 | case ARG_TYPE_INVALID: |
| 595 | default: |
||
| 596 | printf("invalid argument type\n"); |
||
| 668 | bondari | 597 | error = 1; |
| 582 | palkovsky | 598 | break; |
| 518 | jermar | 599 | } |
| 600 | } |
||
| 517 | jermar | 601 | |
| 668 | bondari | 602 | if (error) { |
| 603 | spinlock_unlock(&cmd->lock); |
||
| 604 | return NULL; |
||
| 605 | } |
||
| 606 | |||
| 518 | jermar | 607 | start = end + 1; |
| 608 | if (parse_argument(cmdline, len, &start, &end)) { |
||
| 609 | printf("Too many arguments.\n"); |
||
| 610 | spinlock_unlock(&cmd->lock); |
||
| 611 | return NULL; |
||
| 612 | } |
||
| 613 | |||
| 517 | jermar | 614 | spinlock_unlock(&cmd->lock); |
| 615 | return cmd; |
||
| 616 | } |
||
| 617 | |||
| 518 | jermar | 618 | /** Parse argument. |
| 619 | * |
||
| 620 | * Find start and end positions of command line argument. |
||
| 621 | * |
||
| 622 | * @param cmdline Command line as read from the input device. |
||
| 623 | * @param len Number of characters in cmdline. |
||
| 624 | * @param start On entry, 'start' contains pointer to the index |
||
| 625 | * of first unprocessed character of cmdline. |
||
| 626 | * On successful exit, it marks beginning of the next argument. |
||
| 627 | * @param end Undefined on entry. On exit, 'end' points to the last character |
||
| 628 | * of the next argument. |
||
| 629 | * |
||
| 630 | * @return false on failure, true on success. |
||
| 631 | */ |
||
| 632 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
||
| 633 | { |
||
| 2113 | decky | 634 | index_t i; |
| 518 | jermar | 635 | bool found_start = false; |
| 636 | |||
| 637 | ASSERT(start != NULL); |
||
| 638 | ASSERT(end != NULL); |
||
| 639 | |||
| 640 | for (i = *start; i < len; i++) { |
||
| 641 | if (!found_start) { |
||
| 2572 | jermar | 642 | if (isspace(cmdline[i])) |
| 518 | jermar | 643 | (*start)++; |
| 644 | else |
||
| 645 | found_start = true; |
||
| 646 | } else { |
||
| 2572 | jermar | 647 | if (isspace(cmdline[i])) |
| 518 | jermar | 648 | break; |
| 649 | } |
||
| 650 | } |
||
| 651 | *end = i - 1; |
||
| 652 | |||
| 653 | return found_start; |
||
| 654 | } |
||
| 1702 | cejka | 655 | |
| 1888 | jermar | 656 | /** @} |
| 1702 | cejka | 657 | */ |