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