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 genericconsole
|
---|
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 <console/kconsole.h>
|
---|
42 | #include <console/console.h>
|
---|
43 | #include <console/chardev.h>
|
---|
44 | #include <console/cmd.h>
|
---|
45 | #include <console/prompt.h>
|
---|
46 | #include <print.h>
|
---|
47 | #include <panic.h>
|
---|
48 | #include <typedefs.h>
|
---|
49 | #include <adt/list.h>
|
---|
50 | #include <arch.h>
|
---|
51 | #include <macros.h>
|
---|
52 | #include <debug.h>
|
---|
53 | #include <func.h>
|
---|
54 | #include <str.h>
|
---|
55 | #include <macros.h>
|
---|
56 | #include <sysinfo/sysinfo.h>
|
---|
57 | #include <ddi/device.h>
|
---|
58 | #include <symtab.h>
|
---|
59 | #include <errno.h>
|
---|
60 | #include <putchar.h>
|
---|
61 | #include <str.h>
|
---|
62 |
|
---|
63 | /** Simple kernel console.
|
---|
64 | *
|
---|
65 | * The console is realized by kernel thread kconsole.
|
---|
66 | * It doesn't understand any useful command on its own,
|
---|
67 | * but makes it possible for other kernel subsystems to
|
---|
68 | * register their own commands.
|
---|
69 | */
|
---|
70 |
|
---|
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 |
|
---|
86 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */
|
---|
87 | LIST_INITIALIZE(cmd_list); /**< Command list. */
|
---|
88 |
|
---|
89 | static wchar_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, cur) {
|
---|
122 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
|
---|
123 |
|
---|
124 | if (hlp == cmd) {
|
---|
125 | /* The command is already there. */
|
---|
126 | spinlock_unlock(&cmd_lock);
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
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 | }
|
---|
138 |
|
---|
139 | if (str_cmp(hlp->name, cmd->name) == 0) {
|
---|
140 | /* The command is already there. */
|
---|
141 | spinlock_unlock(&hlp->lock);
|
---|
142 | spinlock_unlock(&cmd->lock);
|
---|
143 | spinlock_unlock(&cmd_lock);
|
---|
144 | return false;
|
---|
145 | }
|
---|
146 |
|
---|
147 | spinlock_unlock(&hlp->lock);
|
---|
148 | spinlock_unlock(&cmd->lock);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Now the command can be added.
|
---|
153 | */
|
---|
154 | list_append(&cmd->link, &cmd_list);
|
---|
155 |
|
---|
156 | spinlock_unlock(&cmd_lock);
|
---|
157 | return true;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /** Print count times a character */
|
---|
161 | NO_TRACE static void print_cc(wchar_t ch, size_t count)
|
---|
162 | {
|
---|
163 | size_t i;
|
---|
164 | for (i = 0; i < count; i++)
|
---|
165 | putchar(ch);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /** Try to find a command beginning with prefix */
|
---|
169 | NO_TRACE static const char *cmdtab_search_one(const char *name,
|
---|
170 | link_t **startpos)
|
---|
171 | {
|
---|
172 | size_t namelen = str_length(name);
|
---|
173 |
|
---|
174 | spinlock_lock(&cmd_lock);
|
---|
175 |
|
---|
176 | if (*startpos == NULL)
|
---|
177 | *startpos = cmd_list.head.next;
|
---|
178 |
|
---|
179 | for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
|
---|
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)
|
---|
184 | continue;
|
---|
185 |
|
---|
186 | if (str_lcmp(curname, name, namelen) == 0) {
|
---|
187 | spinlock_unlock(&cmd_lock);
|
---|
188 | return (curname + str_lsize(curname, namelen));
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | spinlock_unlock(&cmd_lock);
|
---|
193 | return NULL;
|
---|
194 | }
|
---|
195 |
|
---|
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
|
---|
202 | *
|
---|
203 | */
|
---|
204 | NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t * indev)
|
---|
205 | {
|
---|
206 | const char *name = input;
|
---|
207 |
|
---|
208 | size_t found = 0;
|
---|
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);
|
---|
214 | link_t *pos = NULL;
|
---|
215 | const char *hint;
|
---|
216 | char *output = malloc(MAX_CMDLINE, 0);
|
---|
217 | size_t hints_to_show = MAX_TAB_HINTS - 1;
|
---|
218 | size_t total_hints_shown = 0;
|
---|
219 | bool continue_showing_hints = true;
|
---|
220 |
|
---|
221 | output[0] = 0;
|
---|
222 |
|
---|
223 | while ((hint = cmdtab_search_one(name, &pos))) {
|
---|
224 | if ((found == 0) || (str_length(output) > str_length(hint)))
|
---|
225 | str_cpy(output, MAX_CMDLINE, hint);
|
---|
226 |
|
---|
227 | pos = pos->next;
|
---|
228 | found++;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /* If possible completions are more than MAX_TAB_HINTS, ask user whether to display them or not. */
|
---|
232 | if (found > MAX_TAB_HINTS) {
|
---|
233 | printf("\n");
|
---|
234 | continue_showing_hints = console_prompt_display_all_hints(indev, found);
|
---|
235 | }
|
---|
236 |
|
---|
237 | if ((found > 1) && (str_length(output) != 0)) {
|
---|
238 | printf("\n");
|
---|
239 | pos = NULL;
|
---|
240 | while (cmdtab_search_one(name, &pos)) {
|
---|
241 | cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
|
---|
242 |
|
---|
243 | if (continue_showing_hints) {
|
---|
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 */
|
---|
249 | continue_showing_hints = console_prompt_more_hints(indev, &hints_to_show);
|
---|
250 | }
|
---|
251 | }
|
---|
252 |
|
---|
253 | pos = pos->next;
|
---|
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;
|
---|
257 | }
|
---|
258 | /* keep only the characters common in all completions */
|
---|
259 | output[max_match_len] = 0;
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (found > 0)
|
---|
263 | str_cpy(input, size, output);
|
---|
264 |
|
---|
265 | free(output);
|
---|
266 | return found;
|
---|
267 | }
|
---|
268 |
|
---|
269 | NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev)
|
---|
270 | {
|
---|
271 | printf("%s> ", prompt);
|
---|
272 |
|
---|
273 | size_t position = 0;
|
---|
274 | wchar_t *current = history[history_pos];
|
---|
275 | current[0] = 0;
|
---|
276 | char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
---|
277 |
|
---|
278 | while (true) {
|
---|
279 | wchar_t ch = indev_pop_character(indev);
|
---|
280 |
|
---|
281 | if (ch == '\n') {
|
---|
282 | /* Enter */
|
---|
283 | putchar(ch);
|
---|
284 | break;
|
---|
285 | }
|
---|
286 |
|
---|
287 | if (ch == '\b') {
|
---|
288 | /* Backspace */
|
---|
289 | if (position == 0)
|
---|
290 | continue;
|
---|
291 |
|
---|
292 | if (wstr_remove(current, position - 1)) {
|
---|
293 | position--;
|
---|
294 | putchar('\b');
|
---|
295 | printf("%ls ", current + position);
|
---|
296 | print_cc('\b', wstr_length(current) - position + 1);
|
---|
297 | continue;
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (ch == '\t') {
|
---|
302 | /* Tab completion */
|
---|
303 |
|
---|
304 | /* Move to the end of the word */
|
---|
305 | for (; (current[position] != 0) && (!isspace(current[position]));
|
---|
306 | position++)
|
---|
307 | putchar(current[position]);
|
---|
308 |
|
---|
309 | if (position == 0)
|
---|
310 | continue;
|
---|
311 |
|
---|
312 | /* Find the beginning of the word
|
---|
313 | and copy it to tmp */
|
---|
314 | size_t beg;
|
---|
315 | for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
|
---|
316 | beg--);
|
---|
317 |
|
---|
318 | if (isspace(current[beg]))
|
---|
319 | beg++;
|
---|
320 |
|
---|
321 | wstr_to_str(tmp, position - beg + 1, current + beg);
|
---|
322 |
|
---|
323 | int found;
|
---|
324 | if (beg == 0) {
|
---|
325 | /* Command completion */
|
---|
326 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
|
---|
327 | } else {
|
---|
328 | /* Symbol completion */
|
---|
329 | found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (found == 0)
|
---|
333 | continue;
|
---|
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 | }
|
---|
344 |
|
---|
345 | if (found > 1) {
|
---|
346 | /* No unique hint, list was printed */
|
---|
347 | printf("%s> ", prompt);
|
---|
348 | printf("%ls", current);
|
---|
349 | position += str_length(tmp);
|
---|
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++;
|
---|
365 | }
|
---|
366 | }
|
---|
367 | continue;
|
---|
368 | }
|
---|
369 |
|
---|
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));
|
---|
393 |
|
---|
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))
|
---|
428 | continue;
|
---|
429 |
|
---|
430 | if (wstr_remove(current, position)) {
|
---|
431 | printf("%ls ", current + position);
|
---|
432 | print_cc('\b', wstr_length(current) - position + 1);
|
---|
433 | }
|
---|
434 | continue;
|
---|
435 | }
|
---|
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 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | if (wstr_length(current) > 0) {
|
---|
445 | history_pos++;
|
---|
446 | history_pos = history_pos % KCONSOLE_HISTORY;
|
---|
447 | }
|
---|
448 |
|
---|
449 | free(tmp);
|
---|
450 | return current;
|
---|
451 | }
|
---|
452 |
|
---|
453 | bool kconsole_check_poll(void)
|
---|
454 | {
|
---|
455 | return check_poll(stdin);
|
---|
456 | }
|
---|
457 |
|
---|
458 | NO_TRACE static bool parse_int_arg(const char *text, size_t len,
|
---|
459 | sysarg_t *result)
|
---|
460 | {
|
---|
461 | bool isaddr = false;
|
---|
462 | bool isptr = false;
|
---|
463 |
|
---|
464 | /* If we get a name, try to find it in symbol table */
|
---|
465 | if (text[0] == '&') {
|
---|
466 | isaddr = true;
|
---|
467 | text++;
|
---|
468 | len--;
|
---|
469 | } else if (text[0] == '*') {
|
---|
470 | isptr = true;
|
---|
471 | text++;
|
---|
472 | len--;
|
---|
473 | }
|
---|
474 |
|
---|
475 | if ((text[0] < '0') || (text[0] > '9')) {
|
---|
476 | char symname[MAX_SYMBOL_NAME];
|
---|
477 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
|
---|
478 |
|
---|
479 | uintptr_t symaddr;
|
---|
480 | int rc = symtab_addr_lookup(symname, &symaddr);
|
---|
481 | switch (rc) {
|
---|
482 | case ENOENT:
|
---|
483 | printf("Symbol %s not found.\n", symname);
|
---|
484 | return false;
|
---|
485 | case EOVERFLOW:
|
---|
486 | printf("Duplicate symbol %s.\n", symname);
|
---|
487 | symtab_print_search(symname);
|
---|
488 | return false;
|
---|
489 | case ENOTSUP:
|
---|
490 | printf("No symbol information available.\n");
|
---|
491 | return false;
|
---|
492 | case EOK:
|
---|
493 | if (isaddr)
|
---|
494 | *result = (sysarg_t) symaddr;
|
---|
495 | else if (isptr)
|
---|
496 | *result = **((sysarg_t **) symaddr);
|
---|
497 | else
|
---|
498 | *result = *((sysarg_t *) symaddr);
|
---|
499 | break;
|
---|
500 | default:
|
---|
501 | printf("Unknown error.\n");
|
---|
502 | return false;
|
---|
503 | }
|
---|
504 | } else {
|
---|
505 | /* It's a number - convert it */
|
---|
506 | uint64_t value;
|
---|
507 | int rc = str_uint64_t(text, NULL, 0, true, &value);
|
---|
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:
|
---|
516 | *result = (sysarg_t) value;
|
---|
517 | if (isptr)
|
---|
518 | *result = *((sysarg_t *) *result);
|
---|
519 | break;
|
---|
520 | default:
|
---|
521 | printf("Unknown error.\n");
|
---|
522 | return false;
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | return true;
|
---|
527 | }
|
---|
528 |
|
---|
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 | */
|
---|
544 | NO_TRACE static bool parse_argument(const char *cmdline, size_t size,
|
---|
545 | size_t *start, size_t *end)
|
---|
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 | }
|
---|
568 | *end = prev;
|
---|
569 |
|
---|
570 | return found_start;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /** Parse command line.
|
---|
574 | *
|
---|
575 | * @param cmdline Command line as read from input device.
|
---|
576 | * @param size Size (in bytes) of the string.
|
---|
577 | *
|
---|
578 | * @return Structure describing the command.
|
---|
579 | *
|
---|
580 | */
|
---|
581 | NO_TRACE static cmd_info_t *parse_cmdline(const char *cmdline, size_t size)
|
---|
582 | {
|
---|
583 | size_t start = 0;
|
---|
584 | size_t end = 0;
|
---|
585 | if (!parse_argument(cmdline, size, &start, &end)) {
|
---|
586 | /* Command line did not contain alphanumeric word. */
|
---|
587 | return NULL;
|
---|
588 | }
|
---|
589 | spinlock_lock(&cmd_lock);
|
---|
590 |
|
---|
591 | cmd_info_t *cmd = NULL;
|
---|
592 |
|
---|
593 | list_foreach(cmd_list, cur) {
|
---|
594 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link);
|
---|
595 | spinlock_lock(&hlp->lock);
|
---|
596 |
|
---|
597 | if (str_lcmp(hlp->name, cmdline + start,
|
---|
598 | max(str_length(hlp->name),
|
---|
599 | str_nlength(cmdline + start, (size_t) (end - start)))) == 0) {
|
---|
600 | cmd = hlp;
|
---|
601 | break;
|
---|
602 | }
|
---|
603 |
|
---|
604 | spinlock_unlock(&hlp->lock);
|
---|
605 | }
|
---|
606 |
|
---|
607 | spinlock_unlock(&cmd_lock);
|
---|
608 |
|
---|
609 | if (!cmd) {
|
---|
610 | /* Unknown command. */
|
---|
611 | printf("Unknown command.\n");
|
---|
612 | return NULL;
|
---|
613 | }
|
---|
614 |
|
---|
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 | */
|
---|
623 |
|
---|
624 | bool error = false;
|
---|
625 | size_t i;
|
---|
626 | for (i = 0; i < cmd->argc; i++) {
|
---|
627 | char *buf;
|
---|
628 |
|
---|
629 | start = end;
|
---|
630 | if (!parse_argument(cmdline, size, &start, &end)) {
|
---|
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 |
|
---|
637 | printf("Too few arguments.\n");
|
---|
638 | spinlock_unlock(&cmd->lock);
|
---|
639 | return NULL;
|
---|
640 | }
|
---|
641 |
|
---|
642 | switch (cmd->argv[i].type) {
|
---|
643 | case ARG_TYPE_STRING:
|
---|
644 | case ARG_TYPE_STRING_OPTIONAL:
|
---|
645 | buf = (char *) cmd->argv[i].buffer;
|
---|
646 | str_ncpy(buf, cmd->argv[i].len, cmdline + start,
|
---|
647 | end - start);
|
---|
648 | break;
|
---|
649 | case ARG_TYPE_INT:
|
---|
650 | if (!parse_int_arg(cmdline + start, end - start,
|
---|
651 | &cmd->argv[i].intval))
|
---|
652 | error = true;
|
---|
653 | break;
|
---|
654 | case ARG_TYPE_VAR:
|
---|
655 | if ((start < end - 1) && (cmdline[start] == '"')) {
|
---|
656 | if (cmdline[end - 1] == '"') {
|
---|
657 | buf = (char *) cmd->argv[i].buffer;
|
---|
658 | str_ncpy(buf, cmd->argv[i].len,
|
---|
659 | cmdline + start + 1,
|
---|
660 | (end - start) - 1);
|
---|
661 | cmd->argv[i].intval = (sysarg_t) buf;
|
---|
662 | cmd->argv[i].vartype = ARG_TYPE_STRING;
|
---|
663 | } else {
|
---|
664 | printf("Wrong syntax.\n");
|
---|
665 | error = true;
|
---|
666 | }
|
---|
667 | } else if (parse_int_arg(cmdline + start,
|
---|
668 | end - start, &cmd->argv[i].intval)) {
|
---|
669 | cmd->argv[i].vartype = ARG_TYPE_INT;
|
---|
670 | } else {
|
---|
671 | printf("Unrecognized variable argument.\n");
|
---|
672 | error = true;
|
---|
673 | }
|
---|
674 | break;
|
---|
675 | case ARG_TYPE_INVALID:
|
---|
676 | default:
|
---|
677 | printf("Invalid argument type\n");
|
---|
678 | error = true;
|
---|
679 | break;
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | if (error) {
|
---|
684 | spinlock_unlock(&cmd->lock);
|
---|
685 | return NULL;
|
---|
686 | }
|
---|
687 |
|
---|
688 | start = end;
|
---|
689 | if (parse_argument(cmdline, size, &start, &end)) {
|
---|
690 | printf("Too many arguments.\n");
|
---|
691 | spinlock_unlock(&cmd->lock);
|
---|
692 | return NULL;
|
---|
693 | }
|
---|
694 |
|
---|
695 | spinlock_unlock(&cmd->lock);
|
---|
696 | return cmd;
|
---|
697 | }
|
---|
698 |
|
---|
699 | /** Kernel console prompt.
|
---|
700 | *
|
---|
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.
|
---|
705 | *
|
---|
706 | */
|
---|
707 | void kconsole(const char *prompt, const char *msg, bool kcon)
|
---|
708 | {
|
---|
709 | if (!stdin) {
|
---|
710 | LOG("No stdin for kernel console");
|
---|
711 | return;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if (msg)
|
---|
715 | printf("%s", msg);
|
---|
716 |
|
---|
717 | if (kcon)
|
---|
718 | indev_pop_character(stdin);
|
---|
719 | else
|
---|
720 | printf("Type \"exit\" to leave the console.\n");
|
---|
721 |
|
---|
722 | char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
|
---|
723 | while (true) {
|
---|
724 | wchar_t *tmp = clever_readline((char *) prompt, stdin);
|
---|
725 | size_t len = wstr_length(tmp);
|
---|
726 | if (!len)
|
---|
727 | continue;
|
---|
728 |
|
---|
729 | wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
|
---|
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);
|
---|
739 | }
|
---|
740 | free(cmdline);
|
---|
741 | }
|
---|
742 |
|
---|
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);
|
---|
749 | }
|
---|
750 |
|
---|
751 | /** @}
|
---|
752 | */
|
---|