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