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