]>
Commit | Line | Data |
---|---|---|
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 | putchar(' '); | |
181 | ||
182 | if (!cmdtp->help) { | |
183 | my_puts_P(PSTR(" - No additional help available.\n")); | |
184 | return 1; | |
185 | } | |
186 | ||
187 | my_puts_P(cmdtp->help); | |
188 | my_puts_P(PSTR("\n")); | |
189 | #endif /* CONFIG_SYS_LONGHELP */ | |
190 | return 1; | |
191 | } | |
192 | ||
193 | #ifdef CONFIG_AUTO_COMPLETE | |
194 | ||
195 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) | |
196 | { | |
197 | static char tmp_buf[512]; | |
198 | int space; | |
199 | ||
200 | space = last_char == '\0' || isblank(last_char); | |
201 | ||
202 | if (space && argc == 1) | |
203 | return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
204 | ||
205 | if (!space && argc == 2) | |
206 | return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf); | |
207 | ||
208 | return 0; | |
209 | } | |
210 | ||
211 | /*************************************************************************************/ | |
212 | ||
213 | static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) | |
214 | { | |
215 | cmd_tbl_t *cmdtp = cmd_tbl; | |
216 | // const int count = ARRAY_SIZE(cmd_tbl); | |
217 | // const cmd_tbl_t *cmdend = cmdtp + count; | |
218 | // const char *p; | |
219 | int len, clen; | |
220 | int n_found = 0; | |
221 | const char *cmd; | |
222 | ||
223 | /* sanity? */ | |
224 | if (maxv < 2) | |
225 | return -2; | |
226 | ||
227 | cmdv[0] = NULL; | |
228 | ||
229 | if (argc == 0) { | |
230 | /* output full list of commands */ | |
231 | for (; cmdtp->name[0] != '\0'; cmdtp++) { | |
232 | if (n_found >= maxv - 2) { | |
233 | cmdv[n_found++] = "..."; | |
234 | break; | |
235 | } | |
236 | cmdv[n_found++] = cmdtp->name; | |
237 | } | |
238 | cmdv[n_found] = NULL; | |
239 | return n_found; | |
240 | } | |
241 | ||
242 | /* more than one arg or one but the start of the next */ | |
243 | if (argc > 1 || (last_char == '\0' || isblank(last_char))) { | |
244 | cmdtp = find_cmd(argv[0]); | |
245 | if (cmdtp == NULL || cmdtp->complete == NULL) { | |
246 | cmdv[0] = NULL; | |
247 | return 0; | |
248 | } | |
249 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); | |
250 | } | |
251 | ||
252 | cmd = argv[0]; | |
253 | ||
254 | len = strlen(cmd); | |
255 | ||
256 | /* return the partial matches */ | |
257 | for (; cmdtp->name[0] != '\0'; cmdtp++) { | |
258 | ||
259 | clen = strlen(cmdtp->name); | |
260 | if (clen < len) | |
261 | continue; | |
262 | ||
263 | if (memcmp(cmd, cmdtp->name, len) != 0) | |
264 | continue; | |
265 | ||
266 | /* too many! */ | |
267 | if (n_found >= maxv - 2) { | |
268 | cmdv[n_found++] = "..."; | |
269 | break; | |
270 | } | |
271 | ||
272 | cmdv[n_found++] = cmdtp->name; | |
273 | } | |
274 | ||
275 | cmdv[n_found] = NULL; | |
276 | return n_found; | |
277 | } | |
278 | ||
279 | static int make_argv(char *s, int argvsz, char *argv[]) | |
280 | { | |
281 | int argc = 0; | |
282 | ||
283 | /* split into argv */ | |
284 | while (argc < argvsz - 1) { | |
285 | ||
286 | /* skip any white space */ | |
287 | while (isblank(*s)) | |
288 | ++s; | |
289 | ||
290 | if (*s == '\0') /* end of s, no more args */ | |
291 | break; | |
292 | ||
293 | argv[argc++] = s; /* begin of argument string */ | |
294 | ||
295 | /* find end of string */ | |
296 | while (*s && !isblank(*s)) | |
297 | ++s; | |
298 | ||
299 | if (*s == '\0') /* end of s, no more args */ | |
300 | break; | |
301 | ||
302 | *s++ = '\0'; /* terminate current arg */ | |
303 | } | |
304 | argv[argc] = NULL; | |
305 | ||
306 | return argc; | |
307 | } | |
308 | ||
309 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) | |
310 | { | |
311 | int ll = leader != NULL ? strlen(leader) : 0; | |
312 | int sl = sep != NULL ? strlen(sep) : 0; | |
313 | int len, i; | |
314 | ||
315 | if (banner) { | |
316 | my_puts_P(PSTR("\n")); | |
317 | my_puts(banner); | |
318 | } | |
319 | ||
320 | i = linemax; /* force leader and newline */ | |
321 | while (*argv != NULL) { | |
322 | len = strlen(*argv) + sl; | |
323 | if (i + len >= linemax) { | |
324 | my_puts_P(PSTR("\n")); | |
325 | if (leader) | |
326 | my_puts(leader); | |
327 | i = ll - sl; | |
328 | } else if (sep) | |
329 | my_puts(sep); | |
330 | my_puts(*argv++); | |
331 | i += len; | |
332 | } | |
333 | my_puts_P(PSTR("\n")); | |
334 | } | |
335 | ||
336 | static int find_common_prefix(char * const argv[]) | |
337 | { | |
338 | int i, len; | |
339 | char *anchor, *s, *t; | |
340 | ||
341 | if (*argv == NULL) | |
342 | return 0; | |
343 | ||
344 | /* begin with max */ | |
345 | anchor = *argv++; | |
346 | len = strlen(anchor); | |
347 | while ((t = *argv++) != NULL) { | |
348 | s = anchor; | |
349 | for (i = 0; i < len; i++, t++, s++) { | |
350 | if (*t != *s) | |
351 | break; | |
352 | } | |
353 | len = s - anchor; | |
354 | } | |
355 | return len; | |
356 | } | |
357 | ||
358 | static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */ | |
359 | ||
360 | int cmd_auto_complete(const FLASH char *const prompt, char *buf, int *np, int *colp) | |
361 | { | |
362 | int n = *np, col = *colp; | |
363 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ | |
364 | char *cmdv[20]; | |
365 | char *s, *t; | |
366 | const char *sep; | |
367 | int i, j, k, len, seplen, argc; | |
368 | int cnt; | |
369 | char last_char; | |
370 | ||
371 | if (strcmp_PP(prompt, CONFIG_SYS_PROMPT) != 0) | |
372 | return 0; /* not in normal console */ | |
373 | ||
374 | cnt = strlen(buf); | |
375 | if (cnt >= 1) | |
376 | last_char = buf[cnt - 1]; | |
377 | else | |
378 | last_char = '\0'; | |
379 | ||
380 | /* copy to secondary buffer which will be affected */ | |
381 | strcpy(tmp_buf, buf); | |
382 | ||
383 | /* separate into argv */ | |
384 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); | |
385 | ||
386 | /* do the completion and return the possible completions */ | |
387 | i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv); | |
388 | ||
389 | /* no match; bell and out */ | |
390 | if (i == 0) { | |
391 | if (argc > 1) /* allow tab for non command */ | |
392 | return 0; | |
393 | putchar('\a'); | |
394 | return 1; | |
395 | } | |
396 | ||
397 | s = NULL; | |
398 | len = 0; | |
399 | sep = NULL; | |
400 | seplen = 0; | |
401 | if (i == 1) { /* one match; perfect */ | |
402 | k = strlen(argv[argc - 1]); | |
403 | s = cmdv[0] + k; | |
404 | len = strlen(s); | |
405 | sep = " "; | |
406 | seplen = 1; | |
407 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ | |
408 | k = strlen(argv[argc - 1]); | |
409 | j -= k; | |
410 | if (j > 0) { | |
411 | s = cmdv[0] + k; | |
412 | len = j; | |
413 | } | |
414 | } | |
415 | ||
416 | if (s != NULL) { | |
417 | k = len + seplen; | |
418 | /* make sure it fits */ | |
419 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { | |
420 | putchar('\a'); | |
421 | return 1; | |
422 | } | |
423 | ||
424 | t = buf + cnt; | |
425 | for (i = 0; i < len; i++) | |
426 | *t++ = *s++; | |
427 | if (sep != NULL) | |
428 | for (i = 0; i < seplen; i++) | |
429 | *t++ = sep[i]; | |
430 | *t = '\0'; | |
431 | n += k; | |
432 | col += k; | |
433 | my_puts(t - k); | |
434 | if (sep == NULL) | |
435 | putchar('\a'); | |
436 | *np = n; | |
437 | *colp = col; | |
438 | } else { | |
439 | print_argv(NULL, " ", " ", 78, cmdv); | |
440 | ||
441 | my_puts_P(prompt); | |
442 | my_puts(buf); | |
443 | } | |
444 | return 1; | |
445 | } | |
446 | ||
447 | #endif /* CONFIG_AUTO_COMPLETE */ | |
448 | ||
449 | ||
450 | ||
451 | /** | |
452 | * Call a command function. This should be the only route in U-Boot to call | |
453 | * a command, so that we can track whether we are waiting for input or | |
454 | * executing a command. | |
455 | * | |
456 | * @param cmdtp Pointer to the command to execute | |
457 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) | |
458 | * @param argc Number of arguments (arg 0 must be the command text) | |
459 | * @param argv Arguments | |
460 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) | |
461 | */ | |
462 | static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) | |
463 | { | |
464 | int result; | |
465 | ||
466 | result = (cmdtp->cmd)(cmdtp, flag, argc, argv); | |
467 | if (result) | |
468 | debug("Command failed, result=%d\n", result); | |
469 | return result; | |
470 | } | |
471 | ||
472 | enum command_ret_t cmd_process(int flag, int argc, char * const argv[], | |
473 | uint_fast8_t *repeatable) | |
474 | { | |
475 | enum command_ret_t rc = CMD_RET_SUCCESS; | |
476 | cmd_tbl_t *cmdtp; | |
477 | ||
478 | /* Look up command in command table */ | |
479 | cmdtp = find_cmd(argv[0]); | |
480 | if (cmdtp == NULL) { | |
481 | printf_P(PSTR("Unknown command '%s' - try 'help'\n"), argv[0]); | |
482 | return 1; | |
483 | } | |
484 | if (!cmdtp->cmd) { | |
485 | debug("### Command '%s' found, but ->cmd == NULL \n", argv[0]); | |
486 | return 1; | |
487 | } | |
488 | ||
489 | /* found - check max args */ | |
490 | if (argc > cmdtp->maxargs) | |
491 | rc = CMD_RET_USAGE; | |
492 | ||
493 | #if defined(CONFIG_CMD_BOOTD) | |
494 | /* avoid "bootd" recursion */ | |
495 | else if (cmdtp->cmd == do_bootd) { | |
496 | if (flag & CMD_FLAG_BOOTD) { | |
497 | my_puts_P(PSTR("'bootd' recursion detected\n")); | |
498 | rc = CMD_RET_FAILURE; | |
499 | } else { | |
500 | flag |= CMD_FLAG_BOOTD; | |
501 | } | |
502 | } | |
503 | #endif | |
504 | ||
505 | /* If OK so far, then do the command */ | |
506 | if (!rc) { | |
507 | rc = cmd_call(cmdtp, flag, argc, argv); | |
508 | *repeatable &= cmdtp->repeatable; | |
509 | } | |
510 | if (rc == CMD_RET_USAGE) | |
511 | rc = cmd_usage(cmdtp); | |
512 | return rc; | |
513 | } | |
514 | ||
515 | int cmd_process_error(cmd_tbl_t *cmdtp, int err) | |
516 | { | |
517 | char buf[strlen_P(cmdtp->name) + 1]; | |
518 | strcpy_P(buf, cmdtp->name); | |
519 | ||
520 | if (err) { | |
521 | printf_P(PSTR("Command '%s' failed: Error %d\n"), buf, err); | |
522 | return 1; | |
523 | } | |
524 | ||
525 | return 0; | |
526 | } |