Rev 4348 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
510 | jermar | 1 | /* |
2071 | jermar | 2 | * Copyright (c) 2005 Jakub Jermar |
510 | 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 | |||
1888 | jermar | 29 | /** @addtogroup genericconsole |
1702 | cejka | 30 | * @{ |
31 | */ |
||
32 | |||
1264 | jermar | 33 | /** |
4348 | svoboda | 34 | * @file kconsole.c |
35 | * @brief Kernel console. |
||
1264 | jermar | 36 | * |
37 | * This file contains kernel thread managing the kernel console. |
||
4348 | svoboda | 38 | * |
1264 | jermar | 39 | */ |
40 | |||
518 | jermar | 41 | #include <console/kconsole.h> |
510 | jermar | 42 | #include <console/console.h> |
43 | #include <console/chardev.h> |
||
596 | jermar | 44 | #include <console/cmd.h> |
510 | jermar | 45 | #include <print.h> |
517 | jermar | 46 | #include <panic.h> |
510 | jermar | 47 | #include <arch/types.h> |
788 | jermar | 48 | #include <adt/list.h> |
517 | jermar | 49 | #include <arch.h> |
50 | #include <macros.h> |
||
518 | jermar | 51 | #include <debug.h> |
596 | jermar | 52 | #include <func.h> |
4345 | svoboda | 53 | #include <string.h> |
609 | palkovsky | 54 | #include <macros.h> |
4338 | svoboda | 55 | #include <sysinfo/sysinfo.h> |
56 | #include <ddi/device.h> |
||
4346 | svoboda | 57 | #include <symtab.h> |
58 | #include <errno.h> |
||
4347 | svoboda | 59 | #include <putchar.h> |
4348 | svoboda | 60 | #include <string.h> |
510 | jermar | 61 | |
517 | jermar | 62 | /** Simple kernel console. |
63 | * |
||
64 | * The console is realized by kernel thread kconsole. |
||
518 | jermar | 65 | * It doesn't understand any useful command on its own, |
66 | * but makes it possible for other kernel subsystems to |
||
517 | jermar | 67 | * register their own commands. |
68 | */ |
||
4348 | svoboda | 69 | |
517 | jermar | 70 | /** Locking. |
71 | * |
||
72 | * There is a list of cmd_info_t structures. This list |
||
73 | * is protected by cmd_lock spinlock. Note that specially |
||
74 | * the link elements of cmd_info_t are protected by |
||
75 | * this lock. |
||
76 | * |
||
77 | * Each cmd_info_t also has its own lock, which protects |
||
78 | * all elements thereof except the link element. |
||
79 | * |
||
80 | * cmd_lock must be acquired before any cmd_info lock. |
||
81 | * When locking two cmd info structures, structure with |
||
82 | * lower address must be locked first. |
||
83 | */ |
||
84 | |||
4348 | svoboda | 85 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
86 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
||
517 | jermar | 87 | |
4348 | svoboda | 88 | static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
4691 | svoboda | 89 | static size_t history_pos = 0; |
4348 | svoboda | 90 | |
4338 | svoboda | 91 | /** Initialize kconsole data structures |
92 | * |
||
93 | * This is the most basic initialization, almost no |
||
94 | * other kernel subsystem is ready yet. |
||
95 | * |
||
96 | */ |
||
517 | jermar | 97 | void kconsole_init(void) |
98 | { |
||
4338 | svoboda | 99 | unsigned int i; |
4348 | svoboda | 100 | |
596 | jermar | 101 | cmd_init(); |
2108 | jermar | 102 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
4348 | svoboda | 103 | history[i][0] = 0; |
517 | jermar | 104 | } |
105 | |||
106 | /** Register kconsole command. |
||
107 | * |
||
108 | * @param cmd Structure describing the command. |
||
109 | * |
||
4348 | svoboda | 110 | * @return False on failure, true on success. |
111 | * |
||
517 | jermar | 112 | */ |
4348 | svoboda | 113 | bool cmd_register(cmd_info_t *cmd) |
517 | jermar | 114 | { |
115 | link_t *cur; |
||
116 | |||
117 | spinlock_lock(&cmd_lock); |
||
118 | |||
119 | /* |
||
120 | * Make sure the command is not already listed. |
||
121 | */ |
||
122 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
||
4348 | svoboda | 123 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
517 | jermar | 124 | |
125 | if (hlp == cmd) { |
||
126 | /* The command is already there. */ |
||
127 | spinlock_unlock(&cmd_lock); |
||
4348 | svoboda | 128 | return false; |
517 | jermar | 129 | } |
4348 | svoboda | 130 | |
517 | jermar | 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 | } |
||
4348 | svoboda | 139 | |
140 | if (str_cmp(hlp->name, cmd->name) == 0) { |
||
517 | jermar | 141 | /* The command is already there. */ |
142 | spinlock_unlock(&hlp->lock); |
||
143 | spinlock_unlock(&cmd->lock); |
||
144 | spinlock_unlock(&cmd_lock); |
||
4348 | svoboda | 145 | return false; |
517 | jermar | 146 | } |
147 | |||
148 | spinlock_unlock(&hlp->lock); |
||
149 | spinlock_unlock(&cmd->lock); |
||
150 | } |
||
151 | |||
152 | /* |
||
153 | * Now the command can be added. |
||
154 | */ |
||
155 | list_append(&cmd->link, &cmd_head); |
||
156 | |||
157 | spinlock_unlock(&cmd_lock); |
||
4348 | svoboda | 158 | return true; |
517 | jermar | 159 | } |
160 | |||
635 | palkovsky | 161 | /** Print count times a character */ |
4691 | svoboda | 162 | static void print_cc(wchar_t ch, size_t count) |
601 | palkovsky | 163 | { |
4691 | svoboda | 164 | size_t i; |
2108 | jermar | 165 | for (i = 0; i < count; i++) |
601 | palkovsky | 166 | putchar(ch); |
167 | } |
||
168 | |||
4348 | svoboda | 169 | /** Try to find a command beginning with prefix */ |
170 | static const char *cmdtab_search_one(const char *name, link_t **startpos) |
||
601 | palkovsky | 171 | { |
4691 | svoboda | 172 | size_t namelen = str_length(name); |
601 | palkovsky | 173 | |
174 | spinlock_lock(&cmd_lock); |
||
4348 | svoboda | 175 | |
176 | if (*startpos == NULL) |
||
601 | palkovsky | 177 | *startpos = cmd_head.next; |
4348 | svoboda | 178 | |
2108 | jermar | 179 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
4348 | svoboda | 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) |
||
601 | palkovsky | 184 | continue; |
4348 | svoboda | 185 | |
186 | if (str_lcmp(curname, name, namelen) == 0) { |
||
187 | spinlock_unlock(&cmd_lock); |
||
188 | return (curname + str_lsize(curname, namelen)); |
||
601 | palkovsky | 189 | } |
190 | } |
||
4348 | svoboda | 191 | |
192 | spinlock_unlock(&cmd_lock); |
||
601 | palkovsky | 193 | return NULL; |
194 | } |
||
195 | |||
4348 | svoboda | 196 | /** Command completion of the commands |
601 | palkovsky | 197 | * |
4348 | svoboda | 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 | * |
||
601 | palkovsky | 203 | */ |
4348 | svoboda | 204 | static int cmdtab_compl(char *input, size_t size) |
601 | palkovsky | 205 | { |
4348 | svoboda | 206 | const char *name = input; |
207 | |||
4691 | svoboda | 208 | size_t found = 0; |
4348 | svoboda | 209 | link_t *pos = NULL; |
210 | const char *hint; |
||
211 | char output[MAX_CMDLINE]; |
||
212 | |||
213 | output[0] = 0; |
||
214 | |||
215 | while ((hint = cmdtab_search_one(name, &pos))) { |
||
216 | if ((found == 0) || (str_length(output) > str_length(hint))) |
||
217 | str_cpy(output, MAX_CMDLINE, hint); |
||
218 | |||
219 | pos = pos->next; |
||
601 | palkovsky | 220 | found++; |
221 | } |
||
4348 | svoboda | 222 | |
223 | if ((found > 1) && (str_length(output) != 0)) { |
||
601 | palkovsky | 224 | printf("\n"); |
4348 | svoboda | 225 | pos = NULL; |
226 | while ((hint = cmdtab_search_one(name, &pos))) { |
||
227 | cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link); |
||
228 | printf("%s (%s)\n", hlp->name, hlp->description); |
||
229 | pos = pos->next; |
||
601 | palkovsky | 230 | } |
231 | } |
||
4348 | svoboda | 232 | |
233 | if (found > 0) |
||
234 | str_cpy(input, size, output); |
||
235 | |||
601 | palkovsky | 236 | return found; |
237 | } |
||
238 | |||
4348 | svoboda | 239 | static wchar_t *clever_readline(const char *prompt, indev_t *indev) |
601 | palkovsky | 240 | { |
241 | printf("%s> ", prompt); |
||
4348 | svoboda | 242 | |
4691 | svoboda | 243 | size_t position = 0; |
4348 | svoboda | 244 | wchar_t *current = history[history_pos]; |
245 | current[0] = 0; |
||
246 | |||
247 | while (true) { |
||
248 | wchar_t ch = indev_pop_character(indev); |
||
249 | |||
250 | if (ch == '\n') { |
||
251 | /* Enter */ |
||
252 | putchar(ch); |
||
601 | palkovsky | 253 | break; |
3197 | svoboda | 254 | } |
4348 | svoboda | 255 | |
256 | if (ch == '\b') { |
||
257 | /* Backspace */ |
||
601 | palkovsky | 258 | if (position == 0) |
259 | continue; |
||
4348 | svoboda | 260 | |
261 | if (wstr_remove(current, position - 1)) { |
||
262 | position--; |
||
263 | putchar('\b'); |
||
264 | printf("%ls ", current + position); |
||
265 | print_cc('\b', wstr_length(current) - position + 1); |
||
266 | continue; |
||
267 | } |
||
601 | palkovsky | 268 | } |
4348 | svoboda | 269 | |
270 | if (ch == '\t') { |
||
271 | /* Tab completion */ |
||
272 | |||
601 | palkovsky | 273 | /* Move to the end of the word */ |
4348 | svoboda | 274 | for (; (current[position] != 0) && (!isspace(current[position])); |
2108 | jermar | 275 | position++) |
601 | palkovsky | 276 | putchar(current[position]); |
4348 | svoboda | 277 | |
278 | if (position == 0) |
||
279 | continue; |
||
280 | |||
281 | /* Find the beginning of the word |
||
282 | and copy it to tmp */ |
||
4691 | svoboda | 283 | size_t beg; |
4348 | svoboda | 284 | for (beg = position - 1; (beg > 0) && (!isspace(current[beg])); |
285 | beg--); |
||
286 | |||
287 | if (isspace(current[beg])) |
||
288 | beg++; |
||
289 | |||
290 | char tmp[STR_BOUNDS(MAX_CMDLINE)]; |
||
291 | wstr_nstr(tmp, current + beg, position - beg + 1); |
||
292 | |||
293 | int found; |
||
294 | if (beg == 0) { |
||
295 | /* Command completion */ |
||
296 | found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
||
297 | } else { |
||
298 | /* Symbol completion */ |
||
299 | found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE)); |
||
601 | palkovsky | 300 | } |
4348 | svoboda | 301 | |
302 | if (found == 0) |
||
601 | palkovsky | 303 | continue; |
4348 | svoboda | 304 | |
305 | if (found > 1) { |
||
306 | /* No unique hint, list was printed */ |
||
307 | printf("%s> ", prompt); |
||
308 | printf("%ls", current); |
||
309 | print_cc('\b', wstr_length(current) - position); |
||
310 | continue; |
||
311 | } |
||
312 | |||
313 | /* We have a hint */ |
||
314 | |||
315 | size_t off = 0; |
||
4691 | svoboda | 316 | size_t i = 0; |
4348 | svoboda | 317 | while ((ch = str_decode(tmp, &off, STR_NO_LIMIT)) != 0) { |
318 | if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE)) |
||
319 | break; |
||
320 | i++; |
||
321 | } |
||
322 | |||
323 | printf("%ls", current + position); |
||
324 | position += str_length(tmp); |
||
325 | print_cc('\b', wstr_length(current) - position); |
||
326 | |||
327 | if (position == wstr_length(current)) { |
||
328 | /* Insert a space after the last completed argument */ |
||
329 | if (wstr_linsert(current, ' ', position, MAX_CMDLINE)) { |
||
330 | printf("%ls", current + position); |
||
601 | palkovsky | 331 | position++; |
332 | } |
||
333 | } |
||
334 | continue; |
||
335 | } |
||
4348 | svoboda | 336 | |
337 | if (ch == U_LEFT_ARROW) { |
||
338 | /* Left */ |
||
339 | if (position > 0) { |
||
340 | putchar('\b'); |
||
341 | position--; |
||
601 | palkovsky | 342 | } |
343 | continue; |
||
344 | } |
||
4348 | svoboda | 345 | |
346 | if (ch == U_RIGHT_ARROW) { |
||
347 | /* Right */ |
||
348 | if (position < wstr_length(current)) { |
||
349 | putchar(current[position]); |
||
350 | position++; |
||
351 | } |
||
601 | palkovsky | 352 | continue; |
4348 | svoboda | 353 | } |
354 | |||
355 | if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) { |
||
356 | /* Up, down */ |
||
357 | print_cc('\b', position); |
||
358 | print_cc(' ', wstr_length(current)); |
||
359 | print_cc('\b', wstr_length(current)); |
||
360 | |||
361 | if (ch == U_UP_ARROW) { |
||
362 | /* Up */ |
||
363 | if (history_pos == 0) |
||
364 | history_pos = KCONSOLE_HISTORY - 1; |
||
365 | else |
||
366 | history_pos--; |
||
367 | } else { |
||
368 | /* Down */ |
||
369 | history_pos++; |
||
370 | history_pos = history_pos % KCONSOLE_HISTORY; |
||
371 | } |
||
372 | current = history[history_pos]; |
||
373 | printf("%ls", current); |
||
374 | position = wstr_length(current); |
||
518 | jermar | 375 | continue; |
4348 | svoboda | 376 | } |
4337 | svoboda | 377 | |
4348 | svoboda | 378 | if (ch == U_HOME_ARROW) { |
379 | /* Home */ |
||
380 | print_cc('\b', position); |
||
381 | position = 0; |
||
382 | continue; |
||
383 | } |
||
4346 | svoboda | 384 | |
4348 | svoboda | 385 | if (ch == U_END_ARROW) { |
386 | /* End */ |
||
387 | printf("%ls", current + position); |
||
388 | position = wstr_length(current); |
||
517 | jermar | 389 | continue; |
4348 | svoboda | 390 | } |
4337 | svoboda | 391 | |
4348 | svoboda | 392 | if (ch == U_DELETE) { |
393 | /* Delete */ |
||
394 | if (position == wstr_length(current)) |
||
395 | continue; |
||
396 | |||
397 | if (wstr_remove(current, position)) { |
||
398 | printf("%ls ", current + position); |
||
399 | print_cc('\b', wstr_length(current) - position + 1); |
||
400 | } |
||
401 | continue; |
||
402 | } |
||
403 | |||
404 | if (wstr_linsert(current, ch, position, MAX_CMDLINE)) { |
||
405 | printf("%ls", current + position); |
||
406 | position++; |
||
407 | print_cc('\b', wstr_length(current) - position); |
||
408 | } |
||
510 | jermar | 409 | } |
4348 | svoboda | 410 | |
411 | if (wstr_length(current) > 0) { |
||
412 | history_pos++; |
||
413 | history_pos = history_pos % KCONSOLE_HISTORY; |
||
414 | } |
||
415 | |||
416 | return current; |
||
510 | jermar | 417 | } |
517 | jermar | 418 | |
4348 | svoboda | 419 | bool kconsole_check_poll(void) |
4337 | svoboda | 420 | { |
4348 | svoboda | 421 | return check_poll(stdin); |
4337 | svoboda | 422 | } |
423 | |||
4348 | svoboda | 424 | static bool parse_int_arg(const char *text, size_t len, unative_t *result) |
585 | palkovsky | 425 | { |
426 | bool isaddr = false; |
||
589 | palkovsky | 427 | bool isptr = false; |
585 | palkovsky | 428 | |
429 | /* If we get a name, try to find it in symbol table */ |
||
931 | palkovsky | 430 | if (text[0] == '&') { |
431 | isaddr = true; |
||
2108 | jermar | 432 | text++; |
433 | len--; |
||
931 | palkovsky | 434 | } else if (text[0] == '*') { |
435 | isptr = true; |
||
2108 | jermar | 436 | text++; |
437 | len--; |
||
931 | palkovsky | 438 | } |
4348 | svoboda | 439 | |
440 | if ((text[0] < '0') || (text[0] > '9')) { |
||
441 | char symname[MAX_SYMBOL_NAME]; |
||
442 | str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1); |
||
443 | |||
444 | uintptr_t symaddr; |
||
445 | int rc = symtab_addr_lookup(symname, &symaddr); |
||
4346 | svoboda | 446 | switch (rc) { |
447 | case ENOENT: |
||
2108 | jermar | 448 | printf("Symbol %s not found.\n", symname); |
4348 | svoboda | 449 | return false; |
4346 | svoboda | 450 | case EOVERFLOW: |
2108 | jermar | 451 | printf("Duplicate symbol %s.\n", symname); |
585 | palkovsky | 452 | symtab_print_search(symname); |
4348 | svoboda | 453 | return false; |
454 | case ENOTSUP: |
||
4346 | svoboda | 455 | printf("No symbol information available.\n"); |
4348 | svoboda | 456 | return false; |
585 | palkovsky | 457 | } |
4348 | svoboda | 458 | |
932 | palkovsky | 459 | if (isaddr) |
4348 | svoboda | 460 | *result = (unative_t) symaddr; |
932 | palkovsky | 461 | else if (isptr) |
4348 | svoboda | 462 | *result = **((unative_t **) symaddr); |
932 | palkovsky | 463 | else |
4348 | svoboda | 464 | *result = *((unative_t *) symaddr); |
465 | } else { |
||
466 | /* It's a number - convert it */ |
||
585 | palkovsky | 467 | *result = atoi(text); |
932 | palkovsky | 468 | if (isptr) |
4348 | svoboda | 469 | *result = *((unative_t *) *result); |
932 | palkovsky | 470 | } |
4348 | svoboda | 471 | |
472 | return true; |
||
473 | } |
||
931 | palkovsky | 474 | |
4348 | svoboda | 475 | /** Parse argument. |
476 | * |
||
477 | * Find start and end positions of command line argument. |
||
478 | * |
||
479 | * @param cmdline Command line as read from the input device. |
||
480 | * @param size Size (in bytes) of the string. |
||
481 | * @param start On entry, 'start' contains pointer to the offset |
||
482 | * of the first unprocessed character of cmdline. |
||
483 | * On successful exit, it marks beginning of the next argument. |
||
484 | * @param end Undefined on entry. On exit, 'end' is the offset of the first |
||
485 | * character behind the next argument. |
||
486 | * |
||
487 | * @return False on failure, true on success. |
||
488 | * |
||
489 | */ |
||
490 | static bool parse_argument(const char *cmdline, size_t size, size_t *start, size_t *end) |
||
491 | { |
||
492 | ASSERT(start != NULL); |
||
493 | ASSERT(end != NULL); |
||
494 | |||
495 | bool found_start = false; |
||
496 | size_t offset = *start; |
||
497 | size_t prev = *start; |
||
498 | wchar_t ch; |
||
499 | |||
500 | while ((ch = str_decode(cmdline, &offset, size)) != 0) { |
||
501 | if (!found_start) { |
||
502 | if (!isspace(ch)) { |
||
503 | *start = prev; |
||
504 | found_start = true; |
||
505 | } |
||
506 | } else { |
||
507 | if (isspace(ch)) |
||
508 | break; |
||
509 | } |
||
510 | |||
511 | prev = offset; |
||
512 | } |
||
513 | *end = prev; |
||
514 | |||
515 | return found_start; |
||
585 | palkovsky | 516 | } |
517 | |||
517 | jermar | 518 | /** Parse command line. |
519 | * |
||
4348 | svoboda | 520 | * @param cmdline Command line as read from input device. |
521 | * @param size Size (in bytes) of the string. |
||
517 | jermar | 522 | * |
523 | * @return Structure describing the command. |
||
4348 | svoboda | 524 | * |
517 | jermar | 525 | */ |
4348 | svoboda | 526 | static cmd_info_t *parse_cmdline(const char *cmdline, size_t size) |
517 | jermar | 527 | { |
4348 | svoboda | 528 | size_t start = 0; |
529 | size_t end = 0; |
||
530 | if (!parse_argument(cmdline, size, &start, &end)) { |
||
517 | jermar | 531 | /* Command line did not contain alphanumeric word. */ |
532 | return NULL; |
||
533 | } |
||
534 | spinlock_lock(&cmd_lock); |
||
535 | |||
4348 | svoboda | 536 | cmd_info_t *cmd = NULL; |
537 | link_t *cur; |
||
538 | |||
517 | jermar | 539 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
4348 | svoboda | 540 | cmd_info_t *hlp = list_get_instance(cur, cmd_info_t, link); |
517 | jermar | 541 | spinlock_lock(&hlp->lock); |
542 | |||
4348 | svoboda | 543 | if (str_lcmp(hlp->name, cmdline + start, |
544 | max(str_length(hlp->name), |
||
4691 | svoboda | 545 | str_nlength(cmdline + start, (size_t) (end - start) - 1))) == 0) { |
517 | jermar | 546 | cmd = hlp; |
547 | break; |
||
548 | } |
||
549 | |||
550 | spinlock_unlock(&hlp->lock); |
||
551 | } |
||
552 | |||
4348 | svoboda | 553 | spinlock_unlock(&cmd_lock); |
517 | jermar | 554 | |
555 | if (!cmd) { |
||
556 | /* Unknown command. */ |
||
518 | jermar | 557 | printf("Unknown command.\n"); |
517 | jermar | 558 | return NULL; |
559 | } |
||
4348 | svoboda | 560 | |
517 | jermar | 561 | /* cmd == hlp is locked */ |
562 | |||
563 | /* |
||
564 | * The command line must be further analyzed and |
||
565 | * the parameters therefrom must be matched and |
||
566 | * converted to those specified in the cmd info |
||
567 | * structure. |
||
568 | */ |
||
4348 | svoboda | 569 | |
570 | bool error = false; |
||
4691 | svoboda | 571 | size_t i; |
518 | jermar | 572 | for (i = 0; i < cmd->argc; i++) { |
4348 | svoboda | 573 | start = end; |
574 | if (!parse_argument(cmdline, size, &start, &end)) { |
||
518 | jermar | 575 | printf("Too few arguments.\n"); |
576 | spinlock_unlock(&cmd->lock); |
||
577 | return NULL; |
||
578 | } |
||
579 | |||
4348 | svoboda | 580 | char *buf; |
518 | jermar | 581 | switch (cmd->argv[i].type) { |
582 | palkovsky | 582 | case ARG_TYPE_STRING: |
2113 | decky | 583 | buf = (char *) cmd->argv[i].buffer; |
4348 | svoboda | 584 | str_ncpy(buf, cmd->argv[i].len, cmdline + start, |
585 | end - start); |
||
518 | jermar | 586 | break; |
4348 | svoboda | 587 | case ARG_TYPE_INT: |
588 | if (!parse_int_arg(cmdline + start, end - start, |
||
2108 | jermar | 589 | &cmd->argv[i].intval)) |
4348 | svoboda | 590 | error = true; |
518 | jermar | 591 | break; |
585 | palkovsky | 592 | case ARG_TYPE_VAR: |
4348 | svoboda | 593 | if ((start < end - 1) && (cmdline[start] == '"')) { |
594 | if (cmdline[end - 1] == '"') { |
||
595 | buf = (char *) cmd->argv[i].buffer; |
||
596 | str_ncpy(buf, cmd->argv[i].len, |
||
597 | cmdline + start + 1, |
||
598 | (end - start) - 1); |
||
599 | cmd->argv[i].intval = (unative_t) buf; |
||
600 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
||
601 | } else { |
||
602 | printf("Wrong synxtax.\n"); |
||
603 | error = true; |
||
604 | } |
||
605 | } else if (parse_int_arg(cmdline + start, |
||
606 | end - start, &cmd->argv[i].intval)) { |
||
585 | palkovsky | 607 | cmd->argv[i].vartype = ARG_TYPE_INT; |
2108 | jermar | 608 | } else { |
585 | palkovsky | 609 | printf("Unrecognized variable argument.\n"); |
4348 | svoboda | 610 | error = true; |
582 | palkovsky | 611 | } |
585 | palkovsky | 612 | break; |
582 | palkovsky | 613 | case ARG_TYPE_INVALID: |
614 | default: |
||
4348 | svoboda | 615 | printf("Invalid argument type\n"); |
616 | error = true; |
||
582 | palkovsky | 617 | break; |
518 | jermar | 618 | } |
619 | } |
||
517 | jermar | 620 | |
668 | bondari | 621 | if (error) { |
622 | spinlock_unlock(&cmd->lock); |
||
623 | return NULL; |
||
624 | } |
||
625 | |||
4348 | svoboda | 626 | start = end; |
627 | if (parse_argument(cmdline, size, &start, &end)) { |
||
518 | jermar | 628 | printf("Too many arguments.\n"); |
629 | spinlock_unlock(&cmd->lock); |
||
630 | return NULL; |
||
631 | } |
||
632 | |||
517 | jermar | 633 | spinlock_unlock(&cmd->lock); |
634 | return cmd; |
||
635 | } |
||
636 | |||
4348 | svoboda | 637 | /** Kernel console prompt. |
518 | jermar | 638 | * |
4348 | svoboda | 639 | * @param prompt Kernel console prompt (e.g kconsole/panic). |
640 | * @param msg Message to display in the beginning. |
||
641 | * @param kcon Wait for keypress to show the prompt |
||
642 | * and never exit. |
||
518 | jermar | 643 | * |
644 | */ |
||
4348 | svoboda | 645 | void kconsole(char *prompt, char *msg, bool kcon) |
518 | jermar | 646 | { |
4348 | svoboda | 647 | if (!stdin) { |
648 | LOG("No stdin for kernel console"); |
||
649 | return; |
||
650 | } |
||
518 | jermar | 651 | |
4348 | svoboda | 652 | if (msg) |
653 | printf("%s", msg); |
||
518 | jermar | 654 | |
4348 | svoboda | 655 | if (kcon) |
656 | indev_pop_character(stdin); |
||
657 | else |
||
658 | printf("Type \"exit\" to leave the console.\n"); |
||
659 | |||
660 | while (true) { |
||
661 | wchar_t *tmp = clever_readline((char *) prompt, stdin); |
||
4691 | svoboda | 662 | size_t len = wstr_length(tmp); |
4348 | svoboda | 663 | if (!len) |
664 | continue; |
||
665 | |||
666 | char cmdline[STR_BOUNDS(MAX_CMDLINE)]; |
||
667 | wstr_nstr(cmdline, tmp, STR_BOUNDS(MAX_CMDLINE)); |
||
668 | |||
669 | if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0)) |
||
670 | break; |
||
671 | |||
672 | cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE)); |
||
673 | if (!cmd_info) |
||
674 | continue; |
||
675 | |||
676 | (void) cmd_info->func(cmd_info->argv); |
||
518 | jermar | 677 | } |
4348 | svoboda | 678 | } |
518 | jermar | 679 | |
4348 | svoboda | 680 | /** Kernel console managing thread. |
681 | * |
||
682 | */ |
||
683 | void kconsole_thread(void *data) |
||
684 | { |
||
685 | kconsole("kconsole", "Kernel console ready (press any key to activate)\n", true); |
||
518 | jermar | 686 | } |
1702 | cejka | 687 | |
1888 | jermar | 688 | /** @} |
1702 | cejka | 689 | */ |