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