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