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