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