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