]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> | |
3 | * | |
4 | * (C) Copyright 2000-2009 | |
5 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. | |
6 | * | |
7 | * SPDX-License-Identifier: GPL-2.0+ | |
8 | */ | |
9 | ||
10 | /* | |
11 | * Command Processor Table | |
12 | */ | |
13 | ||
14 | #include "common.h" | |
15 | #include <stdlib.h> | |
16 | #include <string.h> | |
17 | #include <ctype.h> | |
18 | ||
19 | #include "config.h" | |
20 | #include "print-utils.h" | |
21 | #include "con-utils.h" | |
22 | #ifdef CONFIG_AUTO_COMPLETE | |
23 | #include "env.h" | |
24 | #endif | |
25 | #include "debug.h" | |
26 | #include "command.h" | |
27 | ||
28 | ||
29 | static void print_usage_line(const FLASH char *name, int width, | |
30 | const FLASH char *usage) | |
31 | { | |
32 | width -= strlen_P(name); | |
33 | if (width < 0) | |
34 | width = 0; | |
35 | my_puts_P(name); | |
36 | print_blanks(width); | |
37 | my_puts_P(PSTR(" - ")); | |
38 | my_puts_P(usage); | |
39 | my_puts_P(PSTR("\n")); | |
40 | } | |
41 | ||
42 | int strcmp_PP(const FLASH char *s1, const FLASH char *s2) | |
43 | { | |
44 | unsigned char c1, c2; | |
45 | ||
46 | while ((c1 = *(const FLASH unsigned char *)s1++) | |
47 | == (c2 = *(const FLASH unsigned char *)s2++)) | |
48 | if (c1 == 0) | |
49 | return 0; | |
50 | ||
51 | return c1 - c2; | |
52 | } | |
53 | ||
54 | int cmpstring_PP(const void *p1, const void *p2) | |
55 | { | |
56 | return strcmp_PP((*(const FLASH cmd_tbl_t **) p1)->name, | |
57 | (*(const FLASH cmd_tbl_t **) p2)->name); | |
58 | } | |
59 | ||
60 | int cmd_tbl_item_count(void) | |
61 | { | |
62 | cmd_tbl_t * p = cmd_tbl; | |
63 | int count = 0; | |
64 | ||
65 | while (p->name != NULL) { | |
66 | p++; count++; | |
67 | } | |
68 | return count; | |
69 | } | |
70 | ||
71 | /* | |
72 | * Use puts() instead of printf() to avoid printf buffer overflow | |
73 | * for long help messages | |
74 | */ | |
75 | ||
76 | command_ret_t _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, | |
77 | int flag, int argc, char * const argv[]) | |
78 | { | |
79 | uint_fast8_t i, max_len = 0; | |
80 | command_ret_t rcode = CMD_RET_SUCCESS; | |
81 | ||
82 | (void) flag; | |
83 | ||
84 | if (argc == 1) { /*show list of commands */ | |
85 | cmd_tbl_t *cmd_array[cmd_items]; | |
86 | int i; | |
87 | ||
88 | /* Make array of commands from .uboot_cmd section */ | |
89 | cmdtp = cmd_start; | |
90 | for (i = 0; i < cmd_items; i++) { | |
91 | cmd_array[i] = cmdtp++; | |
92 | uint_fast8_t l = strlen_P(cmd_array[i]->name); | |
93 | if (l > max_len) | |
94 | max_len = l; | |
95 | } | |
96 | ||
97 | /* Sort command list */ | |
98 | qsort(cmd_array, cmd_items, sizeof (cmd_tbl_t *), cmpstring_PP); | |
99 | ||
100 | /* print short help (usage) */ | |
101 | for (i = 0; i < cmd_items; i++) { | |
102 | const FLASH char *usage = cmd_array[i]->usage; | |
103 | ||
104 | /* allow user abort */ | |
105 | if (ctrlc ()) | |
106 | return CMD_RET_FAILURE; | |
107 | if (usage == NULL) | |
108 | continue; | |
109 | #ifdef GCC_BUG_61443 | |
110 | print_usage_line(cmd_array[i]->name, max_len, usage); | |
111 | #else | |
112 | printf_P(PSTR("%-" stringify(8) /*FIXME*/ "S - %S\n"), | |
113 | cmd_array[i]->name, usage); | |
114 | #endif | |
115 | } | |
116 | return CMD_RET_SUCCESS; | |
117 | } | |
118 | /* | |
119 | * command help (long version) | |
120 | */ | |
121 | for (i = 1; i < argc; ++i) { | |
122 | if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) { | |
123 | cmd_usage(cmdtp); | |
124 | } else { | |
125 | printf_P(PSTR("Unknown command '%s' - try 'help'" | |
126 | " without arguments.\n\n"), argv[i] | |
127 | ); | |
128 | rcode = CMD_RET_FAILURE; | |
129 | } | |
130 | } | |
131 | return rcode; | |
132 | } | |
133 | ||
134 | /*************************************************************************** | |
135 | * find command table entry for a command | |
136 | */ | |
137 | cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len) | |
138 | { | |
139 | cmd_tbl_t *cmdtp; | |
140 | cmd_tbl_t *cmdtp_temp = table; /*Init value */ | |
141 | size_t len; | |
142 | uint_fast8_t n_found = 0; | |
143 | ||
144 | if (!cmd) | |
145 | return NULL; | |
146 | ||
147 | len = strlen(cmd); | |
148 | ||
149 | for (cmdtp = table; | |
150 | cmdtp != table + table_len; | |
151 | cmdtp++) { | |
152 | if (strncmp_P(cmd, cmdtp->name, len) == 0) { | |
153 | if (len == strlen_P(cmdtp->name)) | |
154 | return cmdtp; /* full match */ | |
155 | ||
156 | cmdtp_temp = cmdtp; /* abbreviated command ? */ | |
157 | n_found++; | |
158 | } | |
159 | } | |
160 | if (n_found == 1) { /* exactly one match */ | |
161 | return cmdtp_temp; | |
162 | } | |
163 | ||
164 | return NULL; /* not found or ambiguous command */ | |
165 | } | |
166 | ||
167 | ||
168 | cmd_tbl_t *find_cmd (const char *cmd) | |
169 | { | |
170 | return find_cmd_tbl(cmd, cmd_tbl, cmd_tbl_item_count()); | |
171 | } | |
172 | ||
173 | ||
174 | command_ret_t cmd_usage(const FLASH cmd_tbl_t *cmdtp) | |
175 | { | |
176 | // printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); | |
177 | print_usage_line(cmdtp->name, 0, cmdtp->usage); | |
178 | #if 0 | |
179 | my_puts_P(cmdtp->name); | |
180 | print_blanks(/*FIXME*/ 8 - strlen_P(cmdtp->name)); | |
181 | my_puts_P(PSTR(" - ")); | |
182 | my_puts_P(cmdtp->usage); | |
183 | my_puts_P(PSTR("\n\n")); | |
184 | #endif | |
185 | #ifdef CONFIG_SYS_LONGHELP | |
186 | // printf("Usage:\n%s ", cmdtp->name); | |
187 | my_puts_P(PSTR("Usage:\n")); | |
188 | my_puts_P(cmdtp->name); | |
189 | my_puts_P(PSTR(" ")); | |
190 | ||
191 | if (!cmdtp->help) { | |
192 | my_puts_P(PSTR(" - No additional help available.\n")); | |
193 | return CMD_RET_FAILURE; | |
194 | } | |
195 | ||
196 | my_puts_P(cmdtp->help); | |
197 | my_puts_P(PSTR("\n")); | |
198 | #endif /* CONFIG_SYS_LONGHELP */ | |
199 | return CMD_RET_FAILURE; | |
200 | } | |
201 | ||
202 | #ifdef CONFIG_AUTO_COMPLETE | |
203 | ||
204 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) | |
205 | { | |
206 | static char tmp_buf[CONFIG_SYS_CBSIZE]; | |
207 | int space; | |
208 | ||
209 | space = last_char == '\0' || isblank(last_char); | |
210 | ||
211 | if (space && argc == 1) | |
212 | return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
213 | ||
214 | if (!space && argc == 2) | |
215 | return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
216 | ||
217 | return 0; | |
218 | } | |
219 | ||
220 | /*************************************************************************************/ | |
221 | ||
222 | /* TODO: cmdtp points to FLASH */ | |
223 | ||
224 | static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) | |
225 | { | |
226 | cmd_tbl_t *cmdtp = cmd_tbl; | |
227 | // const int count = ARRAY_SIZE(cmd_tbl); | |
228 | // const cmd_tbl_t *cmdend = cmdtp + count; | |
229 | // const char *p; | |
230 | int len, clen; | |
231 | int n_found = 0; | |
232 | const char *cmd; | |
233 | ||
234 | /* sanity? */ | |
235 | if (maxv < 2) | |
236 | return -2; | |
237 | ||
238 | cmdv[0] = NULL; | |
239 | ||
240 | if (argc == 0) { | |
241 | /* output full list of commands */ | |
242 | for (; cmdtp->name[0] != '\0'; cmdtp++) { | |
243 | if (n_found >= maxv - 2) { | |
244 | cmdv[n_found++] = "..."; | |
245 | break; | |
246 | } | |
247 | cmdv[n_found++] = cmdtp->name; | |
248 | } | |
249 | cmdv[n_found] = NULL; | |
250 | return n_found; | |
251 | } | |
252 | ||
253 | /* more than one arg or one but the start of the next */ | |
254 | if (argc > 1 || (last_char == '\0' || isblank(last_char))) { | |
255 | cmdtp = find_cmd(argv[0]); | |
256 | if (cmdtp == NULL || cmdtp->complete == NULL) { | |
257 | cmdv[0] = NULL; | |
258 | return 0; | |
259 | } | |
260 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); | |
261 | } | |
262 | ||
263 | cmd = argv[0]; | |
264 | ||
265 | len = strlen(cmd); | |
266 | ||
267 | /* return the partial matches */ | |
268 | for (; cmdtp->name[0] != '\0'; cmdtp++) { | |
269 | ||
270 | clen = strlen(cmdtp->name); | |
271 | if (clen < len) | |
272 | continue; | |
273 | ||
274 | if (memcmp(cmd, cmdtp->name, len) != 0) | |
275 | continue; | |
276 | ||
277 | /* too many! */ | |
278 | if (n_found >= maxv - 2) { | |
279 | cmdv[n_found++] = "..."; | |
280 | break; | |
281 | } | |
282 | ||
283 | cmdv[n_found++] = cmdtp->name; | |
284 | } | |
285 | ||
286 | cmdv[n_found] = NULL; | |
287 | return n_found; | |
288 | } | |
289 | ||
290 | static int make_argv(char *s, int argvsz, char *argv[]) | |
291 | { | |
292 | int argc = 0; | |
293 | ||
294 | /* split into argv */ | |
295 | while (argc < argvsz - 1) { | |
296 | ||
297 | /* skip any white space */ | |
298 | while (isblank(*s)) | |
299 | ++s; | |
300 | ||
301 | if (*s == '\0') /* end of s, no more args */ | |
302 | break; | |
303 | ||
304 | argv[argc++] = s; /* begin of argument string */ | |
305 | ||
306 | /* find end of string */ | |
307 | while (*s && !isblank(*s)) | |
308 | ++s; | |
309 | ||
310 | if (*s == '\0') /* end of s, no more args */ | |
311 | break; | |
312 | ||
313 | *s++ = '\0'; /* terminate current arg */ | |
314 | } | |
315 | argv[argc] = NULL; | |
316 | ||
317 | return argc; | |
318 | } | |
319 | ||
320 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) | |
321 | { | |
322 | int ll = leader != NULL ? strlen(leader) : 0; | |
323 | int sl = sep != NULL ? strlen(sep) : 0; | |
324 | int len, i; | |
325 | ||
326 | if (banner) { | |
327 | my_puts_P(PSTR("\n")); | |
328 | my_puts(banner); | |
329 | } | |
330 | ||
331 | i = linemax; /* force leader and newline */ | |
332 | while (*argv != NULL) { | |
333 | len = strlen(*argv) + sl; | |
334 | if (i + len >= linemax) { | |
335 | my_puts_P(PSTR("\n")); | |
336 | if (leader) | |
337 | my_puts(leader); | |
338 | i = ll - sl; | |
339 | } else if (sep) | |
340 | my_puts(sep); | |
341 | my_puts(*argv++); | |
342 | i += len; | |
343 | } | |
344 | my_puts_P(PSTR("\n")); | |
345 | } | |
346 | ||
347 | static int find_common_prefix(char * const argv[]) | |
348 | { | |
349 | int i, len; | |
350 | char *anchor, *s, *t; | |
351 | ||
352 | if (*argv == NULL) | |
353 | return 0; | |
354 | ||
355 | /* begin with max */ | |
356 | anchor = *argv++; | |
357 | len = strlen(anchor); | |
358 | while ((t = *argv++) != NULL) { | |
359 | s = anchor; | |
360 | for (i = 0; i < len; i++, t++, s++) { | |
361 | if (*t != *s) | |
362 | break; | |
363 | } | |
364 | len = s - anchor; | |
365 | } | |
366 | return len; | |
367 | } | |
368 | ||
369 | static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */ | |
370 | ||
371 | ||
372 | int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp) | |
373 | { | |
374 | int n = *np, col = *colp; | |
375 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ | |
376 | char *cmdv[20]; | |
377 | char *s, *t; | |
378 | const char *sep; | |
379 | int i, j, k, len, seplen, argc; | |
380 | int cnt; | |
381 | char last_char; | |
382 | ||
383 | if (strcmp_PP(prompt, CONFIG_SYS_PROMPT) != 0) | |
384 | return 0; /* not in normal console */ | |
385 | ||
386 | cnt = strlen(buf); | |
387 | if (cnt >= 1) | |
388 | last_char = buf[cnt - 1]; | |
389 | else | |
390 | last_char = '\0'; | |
391 | ||
392 | /* copy to secondary buffer which will be affected */ | |
393 | strcpy(tmp_buf, buf); | |
394 | ||
395 | /* separate into argv */ | |
396 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); | |
397 | ||
398 | /* do the completion and return the possible completions */ | |
399 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); | |
400 | ||
401 | /* no match; bell and out */ | |
402 | if (i == 0) { | |
403 | if (argc > 1) /* allow tab for non command */ | |
404 | return 0; | |
405 | putchar('\a'); | |
406 | return 1; | |
407 | } | |
408 | ||
409 | s = NULL; | |
410 | len = 0; | |
411 | sep = NULL; | |
412 | seplen = 0; | |
413 | if (i == 1) { /* one match; perfect */ | |
414 | k = strlen(argv[argc - 1]); | |
415 | s = cmdv[0] + k; | |
416 | len = strlen(s); | |
417 | sep = " "; | |
418 | seplen = 1; | |
419 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ | |
420 | k = strlen(argv[argc - 1]); | |
421 | j -= k; | |
422 | if (j > 0) { | |
423 | s = cmdv[0] + k; | |
424 | len = j; | |
425 | } | |
426 | } | |
427 | ||
428 | if (s != NULL) { | |
429 | k = len + seplen; | |
430 | /* make sure it fits */ | |
431 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { | |
432 | putchar('\a'); | |
433 | return 1; | |
434 | } | |
435 | ||
436 | t = buf + cnt; | |
437 | for (i = 0; i < len; i++) | |
438 | *t++ = *s++; | |
439 | if (sep != NULL) | |
440 | for (i = 0; i < seplen; i++) | |
441 | *t++ = sep[i]; | |
442 | *t = '\0'; | |
443 | n += k; | |
444 | col += k; | |
445 | my_puts(t - k); | |
446 | if (sep == NULL) | |
447 | putchar('\a'); | |
448 | *np = n; | |
449 | *colp = col; | |
450 | } else { | |
451 | print_argv(NULL, " ", " ", 78, cmdv); | |
452 | ||
453 | my_puts_P(prompt); | |
454 | my_puts(buf); | |
455 | } | |
456 | return 1; | |
457 | } | |
458 | ||
459 | #endif /* CONFIG_AUTO_COMPLETE */ | |
460 | ||
461 | ||
462 | ||
463 | /** | |
464 | * Call a command function. This should be the only route in U-Boot to call | |
465 | * a command, so that we can track whether we are waiting for input or | |
466 | * executing a command. | |
467 | * | |
468 | * @param cmdtp Pointer to the command to execute | |
469 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) | |
470 | * @param argc Number of arguments (arg 0 must be the command text) | |
471 | * @param argv Arguments | |
472 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) | |
473 | */ | |
474 | command_ret_t cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
475 | { | |
476 | command_ret_t result; | |
477 | ||
478 | result = (cmdtp->cmd)(cmdtp, flag, argc, argv); | |
479 | if (result != CMD_RET_SUCCESS) | |
480 | debug("Command failed, result=%d\n", result); | |
481 | return result; | |
482 | } | |
483 | ||
484 | command_ret_t cmd_process(int flag, int argc, char * const argv[], | |
485 | uint_fast8_t *repeatable) | |
486 | { | |
487 | command_ret_t rc = CMD_RET_SUCCESS; | |
488 | cmd_tbl_t *cmdtp; | |
489 | ||
490 | /* Look up command in command table */ | |
491 | cmdtp = find_cmd(argv[0]); | |
492 | if (cmdtp == NULL) { | |
493 | printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]); | |
494 | return CMD_RET_FAILURE; | |
495 | } | |
496 | if (!cmdtp->cmd) { | |
497 | debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]); | |
498 | return CMD_RET_FAILURE; | |
499 | } | |
500 | ||
501 | /* found - check max args */ | |
502 | if (argc > cmdtp->maxargs) | |
503 | rc = CMD_RET_USAGE; | |
504 | ||
505 | #if defined(CONFIG_CMD_BOOTD) | |
506 | /* avoid "bootd" recursion */ | |
507 | else if (cmdtp->cmd == do_bootd) { | |
508 | if (flag & CMD_FLAG_BOOTD) { | |
509 | my_puts_P(PSTR("'bootd' recursion detected\n")); | |
510 | rc = CMD_RET_FAILURE; | |
511 | } else { | |
512 | flag |= CMD_FLAG_BOOTD; | |
513 | } | |
514 | } | |
515 | #endif | |
516 | ||
517 | /* If OK so far, then do the command */ | |
518 | if (!rc) { | |
519 | rc = cmd_call(cmdtp, flag, argc, argv); | |
520 | *repeatable &= cmdtp->repeatable; | |
521 | } | |
522 | if (rc == CMD_RET_USAGE) | |
523 | rc = cmd_usage(cmdtp); | |
524 | return rc; | |
525 | } | |
526 | ||
527 | int cmd_process_error(cmd_tbl_t *cmdtp, int err) | |
528 | { | |
529 | char buf[strlen_P(cmdtp->name) + 1]; | |
530 | strcpy_P(buf, cmdtp->name); | |
531 | ||
532 | if (err) { | |
533 | printf_P(PSTR("Command '%s' failed: Error %d\n"), buf, err); | |
534 | return 1; | |
535 | } | |
536 | ||
537 | return 0; | |
538 | } |