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