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