]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
5dfa25f2 | 2 | /* |
2dce551e | 3 | * (C) Copyright 2000-2009 |
5dfa25f2 | 4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
5dfa25f2 WD |
5 | */ |
6 | ||
7 | /* | |
8 | * Command Processor Table | |
9 | */ | |
10 | ||
11 | #include <common.h> | |
12 | #include <command.h> | |
24b852a7 | 13 | #include <console.h> |
af95f206 | 14 | #include <env.h> |
4d91a6ec | 15 | #include <linux/ctype.h> |
5dfa25f2 | 16 | |
5dfa25f2 WD |
17 | /* |
18 | * Use puts() instead of printf() to avoid printf buffer overflow | |
19 | * for long help messages | |
20 | */ | |
2dce551e | 21 | |
616e2162 MY |
22 | int _do_help(cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t *cmdtp, int flag, |
23 | int argc, char * const argv[]) | |
5dfa25f2 WD |
24 | { |
25 | int i; | |
26 | int rcode = 0; | |
27 | ||
616e2162 | 28 | if (argc == 1) { /* show list of commands */ |
22b6fcb5 | 29 | cmd_tbl_t *cmd_array[cmd_items]; |
9d2b18a0 WD |
30 | int i, j, swaps; |
31 | ||
32 | /* Make array of commands from .uboot_cmd section */ | |
2dce551e | 33 | cmdtp = cmd_start; |
9d2b18a0 WD |
34 | for (i = 0; i < cmd_items; i++) { |
35 | cmd_array[i] = cmdtp++; | |
8bde7f77 | 36 | } |
8bde7f77 | 37 | |
9d2b18a0 WD |
38 | /* Sort command list (trivial bubble sort) */ |
39 | for (i = cmd_items - 1; i > 0; --i) { | |
40 | swaps = 0; | |
41 | for (j = 0; j < i; ++j) { | |
616e2162 MY |
42 | if (strcmp(cmd_array[j]->name, |
43 | cmd_array[j + 1]->name) > 0) { | |
9d2b18a0 WD |
44 | cmd_tbl_t *tmp; |
45 | tmp = cmd_array[j]; | |
46 | cmd_array[j] = cmd_array[j + 1]; | |
47 | cmd_array[j + 1] = tmp; | |
48 | ++swaps; | |
49 | } | |
8bde7f77 | 50 | } |
9d2b18a0 WD |
51 | if (!swaps) |
52 | break; | |
8bde7f77 | 53 | } |
5dfa25f2 | 54 | |
8bde7f77 | 55 | /* print short help (usage) */ |
9d2b18a0 WD |
56 | for (i = 0; i < cmd_items; i++) { |
57 | const char *usage = cmd_array[i]->usage; | |
58 | ||
5dfa25f2 | 59 | /* allow user abort */ |
616e2162 | 60 | if (ctrlc()) |
5dfa25f2 | 61 | return 1; |
9d2b18a0 | 62 | if (usage == NULL) |
5dfa25f2 | 63 | continue; |
2fb2604d PT |
64 | printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH, |
65 | cmd_array[i]->name, usage); | |
5dfa25f2 | 66 | } |
5dfa25f2 WD |
67 | return 0; |
68 | } | |
5dfa25f2 WD |
69 | /* |
70 | * command help (long version) | |
71 | */ | |
8bde7f77 | 72 | for (i = 1; i < argc; ++i) { |
616e2162 MY |
73 | cmdtp = find_cmd_tbl(argv[i], cmd_start, cmd_items); |
74 | if (cmdtp != NULL) { | |
94796d85 | 75 | rcode |= cmd_usage(cmdtp); |
71f95118 | 76 | } else { |
616e2162 MY |
77 | printf("Unknown command '%s' - try 'help' without arguments for list of all known commands\n\n", |
78 | argv[i]); | |
5dfa25f2 WD |
79 | rcode = 1; |
80 | } | |
81 | } | |
82 | return rcode; | |
83 | } | |
84 | ||
616e2162 MY |
85 | /* find command table entry for a command */ |
86 | cmd_tbl_t *find_cmd_tbl(const char *cmd, cmd_tbl_t *table, int table_len) | |
5dfa25f2 | 87 | { |
f8bb6964 | 88 | #ifdef CONFIG_CMDLINE |
5dfa25f2 | 89 | cmd_tbl_t *cmdtp; |
616e2162 | 90 | cmd_tbl_t *cmdtp_temp = table; /* Init value */ |
9d2b18a0 WD |
91 | const char *p; |
92 | int len; | |
93 | int n_found = 0; | |
94 | ||
7013c061 TW |
95 | if (!cmd) |
96 | return NULL; | |
9d2b18a0 WD |
97 | /* |
98 | * Some commands allow length modifiers (like "cp.b"); | |
99 | * compare command name only until first dot. | |
100 | */ | |
101 | len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd); | |
102 | ||
616e2162 MY |
103 | for (cmdtp = table; cmdtp != table + table_len; cmdtp++) { |
104 | if (strncmp(cmd, cmdtp->name, len) == 0) { | |
105 | if (len == strlen(cmdtp->name)) | |
9d2b18a0 WD |
106 | return cmdtp; /* full match */ |
107 | ||
108 | cmdtp_temp = cmdtp; /* abbreviated command ? */ | |
109 | n_found++; | |
110 | } | |
5dfa25f2 | 111 | } |
9d2b18a0 | 112 | if (n_found == 1) { /* exactly one match */ |
8bde7f77 | 113 | return cmdtp_temp; |
9d2b18a0 | 114 | } |
f8bb6964 | 115 | #endif /* CONFIG_CMDLINE */ |
5dfa25f2 | 116 | |
9d2b18a0 | 117 | return NULL; /* not found or ambiguous command */ |
8bde7f77 | 118 | } |
04a85b3b | 119 | |
616e2162 | 120 | cmd_tbl_t *find_cmd(const char *cmd) |
b799cb4c | 121 | { |
6c7c946c MV |
122 | cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd); |
123 | const int len = ll_entry_count(cmd_tbl_t, cmd); | |
124 | return find_cmd_tbl(cmd, start, len); | |
b799cb4c KG |
125 | } |
126 | ||
e84ffddb | 127 | int cmd_usage(const cmd_tbl_t *cmdtp) |
62c3ae7c | 128 | { |
94796d85 WD |
129 | printf("%s - %s\n\n", cmdtp->name, cmdtp->usage); |
130 | ||
131 | #ifdef CONFIG_SYS_LONGHELP | |
132 | printf("Usage:\n%s ", cmdtp->name); | |
133 | ||
134 | if (!cmdtp->help) { | |
135 | puts ("- No additional help available.\n"); | |
136 | return 1; | |
137 | } | |
138 | ||
616e2162 MY |
139 | puts(cmdtp->help); |
140 | putc('\n'); | |
94796d85 | 141 | #endif /* CONFIG_SYS_LONGHELP */ |
47e26b1b | 142 | return 1; |
62c3ae7c PT |
143 | } |
144 | ||
04a85b3b | 145 | #ifdef CONFIG_AUTO_COMPLETE |
03dcf17d | 146 | static char env_complete_buf[512]; |
04a85b3b | 147 | |
54841ab5 | 148 | int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[]) |
04a85b3b | 149 | { |
04a85b3b WD |
150 | int space; |
151 | ||
4d91a6ec | 152 | space = last_char == '\0' || isblank(last_char); |
04a85b3b WD |
153 | |
154 | if (space && argc == 1) | |
03dcf17d BB |
155 | return env_complete("", maxv, cmdv, sizeof(env_complete_buf), |
156 | env_complete_buf, false); | |
04a85b3b WD |
157 | |
158 | if (!space && argc == 2) | |
03dcf17d BB |
159 | return env_complete(argv[1], maxv, cmdv, |
160 | sizeof(env_complete_buf), | |
161 | env_complete_buf, false); | |
560d424b | 162 | |
04a85b3b WD |
163 | return 0; |
164 | } | |
165 | ||
03dcf17d BB |
166 | static int dollar_complete(int argc, char * const argv[], char last_char, |
167 | int maxv, char *cmdv[]) | |
168 | { | |
169 | /* Make sure the last argument starts with a $. */ | |
170 | if (argc < 1 || argv[argc - 1][0] != '$' || | |
171 | last_char == '\0' || isblank(last_char)) | |
172 | return 0; | |
173 | ||
174 | return env_complete(argv[argc - 1], maxv, cmdv, sizeof(env_complete_buf), | |
175 | env_complete_buf, true); | |
176 | } | |
177 | ||
04a85b3b WD |
178 | /*************************************************************************************/ |
179 | ||
6fb61445 BB |
180 | int complete_subcmdv(cmd_tbl_t *cmdtp, int count, int argc, |
181 | char * const argv[], char last_char, | |
182 | int maxv, char *cmdv[]) | |
04a85b3b | 183 | { |
f8bb6964 | 184 | #ifdef CONFIG_CMDLINE |
6c7c946c | 185 | const cmd_tbl_t *cmdend = cmdtp + count; |
04a85b3b WD |
186 | const char *p; |
187 | int len, clen; | |
188 | int n_found = 0; | |
189 | const char *cmd; | |
190 | ||
191 | /* sanity? */ | |
192 | if (maxv < 2) | |
193 | return -2; | |
194 | ||
195 | cmdv[0] = NULL; | |
196 | ||
197 | if (argc == 0) { | |
198 | /* output full list of commands */ | |
6c7c946c | 199 | for (; cmdtp != cmdend; cmdtp++) { |
04a85b3b | 200 | if (n_found >= maxv - 2) { |
9b438946 | 201 | cmdv[n_found++] = "..."; |
04a85b3b WD |
202 | break; |
203 | } | |
9b438946 | 204 | cmdv[n_found++] = cmdtp->name; |
04a85b3b WD |
205 | } |
206 | cmdv[n_found] = NULL; | |
207 | return n_found; | |
208 | } | |
209 | ||
210 | /* more than one arg or one but the start of the next */ | |
616e2162 | 211 | if (argc > 1 || last_char == '\0' || isblank(last_char)) { |
6fb61445 | 212 | cmdtp = find_cmd_tbl(argv[0], cmdtp, count); |
04a85b3b WD |
213 | if (cmdtp == NULL || cmdtp->complete == NULL) { |
214 | cmdv[0] = NULL; | |
215 | return 0; | |
216 | } | |
217 | return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv); | |
218 | } | |
219 | ||
220 | cmd = argv[0]; | |
221 | /* | |
222 | * Some commands allow length modifiers (like "cp.b"); | |
223 | * compare command name only until first dot. | |
224 | */ | |
225 | p = strchr(cmd, '.'); | |
226 | if (p == NULL) | |
227 | len = strlen(cmd); | |
228 | else | |
229 | len = p - cmd; | |
230 | ||
231 | /* return the partial matches */ | |
6c7c946c | 232 | for (; cmdtp != cmdend; cmdtp++) { |
04a85b3b WD |
233 | |
234 | clen = strlen(cmdtp->name); | |
235 | if (clen < len) | |
236 | continue; | |
237 | ||
238 | if (memcmp(cmd, cmdtp->name, len) != 0) | |
239 | continue; | |
240 | ||
241 | /* too many! */ | |
242 | if (n_found >= maxv - 2) { | |
243 | cmdv[n_found++] = "..."; | |
244 | break; | |
245 | } | |
246 | ||
247 | cmdv[n_found++] = cmdtp->name; | |
248 | } | |
249 | ||
250 | cmdv[n_found] = NULL; | |
251 | return n_found; | |
f8bb6964 SG |
252 | #else |
253 | return 0; | |
254 | #endif | |
04a85b3b WD |
255 | } |
256 | ||
6fb61445 BB |
257 | static int complete_cmdv(int argc, char * const argv[], char last_char, |
258 | int maxv, char *cmdv[]) | |
259 | { | |
260 | #ifdef CONFIG_CMDLINE | |
261 | return complete_subcmdv(ll_entry_start(cmd_tbl_t, cmd), | |
262 | ll_entry_count(cmd_tbl_t, cmd), argc, argv, | |
263 | last_char, maxv, cmdv); | |
264 | #else | |
265 | return 0; | |
266 | #endif | |
267 | } | |
268 | ||
04a85b3b WD |
269 | static int make_argv(char *s, int argvsz, char *argv[]) |
270 | { | |
271 | int argc = 0; | |
272 | ||
273 | /* split into argv */ | |
274 | while (argc < argvsz - 1) { | |
275 | ||
276 | /* skip any white space */ | |
4d91a6ec | 277 | while (isblank(*s)) |
04a85b3b WD |
278 | ++s; |
279 | ||
53677ef1 | 280 | if (*s == '\0') /* end of s, no more args */ |
04a85b3b WD |
281 | break; |
282 | ||
283 | argv[argc++] = s; /* begin of argument string */ | |
284 | ||
285 | /* find end of string */ | |
4d91a6ec | 286 | while (*s && !isblank(*s)) |
04a85b3b WD |
287 | ++s; |
288 | ||
289 | if (*s == '\0') /* end of s, no more args */ | |
290 | break; | |
291 | ||
292 | *s++ = '\0'; /* terminate current arg */ | |
293 | } | |
294 | argv[argc] = NULL; | |
295 | ||
296 | return argc; | |
297 | } | |
298 | ||
54841ab5 | 299 | static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[]) |
04a85b3b WD |
300 | { |
301 | int ll = leader != NULL ? strlen(leader) : 0; | |
302 | int sl = sep != NULL ? strlen(sep) : 0; | |
303 | int len, i; | |
304 | ||
305 | if (banner) { | |
306 | puts("\n"); | |
307 | puts(banner); | |
308 | } | |
309 | ||
310 | i = linemax; /* force leader and newline */ | |
311 | while (*argv != NULL) { | |
312 | len = strlen(*argv) + sl; | |
313 | if (i + len >= linemax) { | |
314 | puts("\n"); | |
315 | if (leader) | |
316 | puts(leader); | |
317 | i = ll - sl; | |
318 | } else if (sep) | |
319 | puts(sep); | |
320 | puts(*argv++); | |
321 | i += len; | |
322 | } | |
323 | printf("\n"); | |
324 | } | |
325 | ||
54841ab5 | 326 | static int find_common_prefix(char * const argv[]) |
04a85b3b WD |
327 | { |
328 | int i, len; | |
329 | char *anchor, *s, *t; | |
330 | ||
331 | if (*argv == NULL) | |
332 | return 0; | |
333 | ||
334 | /* begin with max */ | |
335 | anchor = *argv++; | |
336 | len = strlen(anchor); | |
337 | while ((t = *argv++) != NULL) { | |
338 | s = anchor; | |
339 | for (i = 0; i < len; i++, t++, s++) { | |
340 | if (*t != *s) | |
341 | break; | |
342 | } | |
343 | len = s - anchor; | |
344 | } | |
345 | return len; | |
346 | } | |
347 | ||
2614a208 | 348 | static char tmp_buf[CONFIG_SYS_CBSIZE + 1]; /* copy of console I/O buffer */ |
04a85b3b WD |
349 | |
350 | int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp) | |
351 | { | |
352 | int n = *np, col = *colp; | |
6d0f6bcf | 353 | char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */ |
04a85b3b WD |
354 | char *cmdv[20]; |
355 | char *s, *t; | |
356 | const char *sep; | |
357 | int i, j, k, len, seplen, argc; | |
358 | int cnt; | |
359 | char last_char; | |
4225f830 MV |
360 | #ifdef CONFIG_CMDLINE_PS_SUPPORT |
361 | const char *ps_prompt = env_get("PS1"); | |
362 | #else | |
363 | const char *ps_prompt = CONFIG_SYS_PROMPT; | |
364 | #endif | |
04a85b3b | 365 | |
4225f830 | 366 | if (strcmp(prompt, ps_prompt) != 0) |
04a85b3b WD |
367 | return 0; /* not in normal console */ |
368 | ||
369 | cnt = strlen(buf); | |
370 | if (cnt >= 1) | |
371 | last_char = buf[cnt - 1]; | |
372 | else | |
373 | last_char = '\0'; | |
374 | ||
375 | /* copy to secondary buffer which will be affected */ | |
376 | strcpy(tmp_buf, buf); | |
377 | ||
378 | /* separate into argv */ | |
379 | argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv); | |
380 | ||
03dcf17d BB |
381 | /* first try a $ completion */ |
382 | i = dollar_complete(argc, argv, last_char, | |
383 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); | |
384 | if (!i) { | |
385 | /* do the completion and return the possible completions */ | |
386 | i = complete_cmdv(argc, argv, last_char, | |
387 | sizeof(cmdv) / sizeof(cmdv[0]), cmdv); | |
388 | } | |
04a85b3b WD |
389 | |
390 | /* no match; bell and out */ | |
391 | if (i == 0) { | |
392 | if (argc > 1) /* allow tab for non command */ | |
393 | return 0; | |
394 | putc('\a'); | |
395 | return 1; | |
396 | } | |
397 | ||
398 | s = NULL; | |
399 | len = 0; | |
400 | sep = NULL; | |
401 | seplen = 0; | |
402 | if (i == 1) { /* one match; perfect */ | |
cbe07ebe BB |
403 | if (last_char != '\0' && !isblank(last_char)) |
404 | k = strlen(argv[argc - 1]); | |
405 | else | |
406 | k = 0; | |
407 | ||
04a85b3b WD |
408 | s = cmdv[0] + k; |
409 | len = strlen(s); | |
410 | sep = " "; | |
411 | seplen = 1; | |
616e2162 | 412 | } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */ |
cbe07ebe BB |
413 | if (last_char != '\0' && !isblank(last_char)) |
414 | k = strlen(argv[argc - 1]); | |
415 | else | |
416 | k = 0; | |
417 | ||
04a85b3b WD |
418 | j -= k; |
419 | if (j > 0) { | |
420 | s = cmdv[0] + k; | |
421 | len = j; | |
422 | } | |
423 | } | |
424 | ||
425 | if (s != NULL) { | |
426 | k = len + seplen; | |
427 | /* make sure it fits */ | |
6d0f6bcf | 428 | if (n + k >= CONFIG_SYS_CBSIZE - 2) { |
04a85b3b WD |
429 | putc('\a'); |
430 | return 1; | |
431 | } | |
432 | ||
433 | t = buf + cnt; | |
434 | for (i = 0; i < len; i++) | |
435 | *t++ = *s++; | |
436 | if (sep != NULL) | |
437 | for (i = 0; i < seplen; i++) | |
438 | *t++ = sep[i]; | |
439 | *t = '\0'; | |
440 | n += k; | |
441 | col += k; | |
442 | puts(t - k); | |
443 | if (sep == NULL) | |
444 | putc('\a'); | |
445 | *np = n; | |
446 | *colp = col; | |
447 | } else { | |
448 | print_argv(NULL, " ", " ", 78, cmdv); | |
449 | ||
450 | puts(prompt); | |
451 | puts(buf); | |
452 | } | |
453 | return 1; | |
454 | } | |
455 | ||
456 | #endif | |
8a40fb14 JCPV |
457 | |
458 | #ifdef CMD_DATA_SIZE | |
459 | int cmd_get_data_size(char* arg, int default_size) | |
460 | { | |
461 | /* Check for a size specification .b, .w or .l. | |
462 | */ | |
463 | int len = strlen(arg); | |
464 | if (len > 2 && arg[len-2] == '.') { | |
616e2162 | 465 | switch (arg[len-1]) { |
8a40fb14 JCPV |
466 | case 'b': |
467 | return 1; | |
468 | case 'w': | |
469 | return 2; | |
470 | case 'l': | |
471 | return 4; | |
4d979bfd | 472 | #ifdef MEM_SUPPORT_64BIT_DATA |
4d1fd7f1 YS |
473 | case 'q': |
474 | return 8; | |
475 | #endif | |
8a40fb14 JCPV |
476 | case 's': |
477 | return -2; | |
478 | default: | |
479 | return -1; | |
480 | } | |
481 | } | |
482 | return default_size; | |
483 | } | |
484 | #endif | |
620f1f6a | 485 | |
2e5167cc | 486 | #if defined(CONFIG_NEEDS_MANUAL_RELOC) |
620f1f6a HS |
487 | DECLARE_GLOBAL_DATA_PTR; |
488 | ||
489 | void fixup_cmdtable(cmd_tbl_t *cmdtp, int size) | |
490 | { | |
491 | int i; | |
492 | ||
493 | if (gd->reloc_off == 0) | |
494 | return; | |
495 | ||
496 | for (i = 0; i < size; i++) { | |
497 | ulong addr; | |
498 | ||
6f94daf9 MS |
499 | addr = (ulong)(cmdtp->cmd_rep) + gd->reloc_off; |
500 | cmdtp->cmd_rep = | |
501 | (int (*)(struct cmd_tbl_s *, int, int, | |
502 | char * const [], int *))addr; | |
503 | ||
616e2162 | 504 | addr = (ulong)(cmdtp->cmd) + gd->reloc_off; |
2e88bb28 | 505 | #ifdef DEBUG_COMMANDS |
620f1f6a | 506 | printf("Command \"%s\": 0x%08lx => 0x%08lx\n", |
616e2162 | 507 | cmdtp->name, (ulong)(cmdtp->cmd), addr); |
620f1f6a HS |
508 | #endif |
509 | cmdtp->cmd = | |
510 | (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr; | |
511 | addr = (ulong)(cmdtp->name) + gd->reloc_off; | |
512 | cmdtp->name = (char *)addr; | |
513 | if (cmdtp->usage) { | |
514 | addr = (ulong)(cmdtp->usage) + gd->reloc_off; | |
515 | cmdtp->usage = (char *)addr; | |
516 | } | |
517 | #ifdef CONFIG_SYS_LONGHELP | |
518 | if (cmdtp->help) { | |
519 | addr = (ulong)(cmdtp->help) + gd->reloc_off; | |
520 | cmdtp->help = (char *)addr; | |
521 | } | |
fa28bd2e DS |
522 | #endif |
523 | #ifdef CONFIG_AUTO_COMPLETE | |
524 | if (cmdtp->complete) { | |
525 | addr = (ulong)(cmdtp->complete) + gd->reloc_off; | |
3668d8fa DS |
526 | cmdtp->complete = |
527 | (int (*)(int, char * const [], char, int, char * []))addr; | |
fa28bd2e | 528 | } |
620f1f6a HS |
529 | #endif |
530 | cmdtp++; | |
531 | } | |
532 | } | |
533 | #endif | |
bdf8e34b | 534 | |
80a48dd4 BB |
535 | int cmd_always_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, |
536 | char * const argv[], int *repeatable) | |
537 | { | |
538 | *repeatable = 1; | |
539 | ||
540 | return cmdtp->cmd(cmdtp, flag, argc, argv); | |
541 | } | |
542 | ||
543 | int cmd_never_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, | |
544 | char * const argv[], int *repeatable) | |
545 | { | |
546 | *repeatable = 0; | |
547 | ||
548 | return cmdtp->cmd(cmdtp, flag, argc, argv); | |
549 | } | |
550 | ||
551 | int cmd_discard_repeatable(cmd_tbl_t *cmdtp, int flag, int argc, | |
552 | char * const argv[]) | |
553 | { | |
554 | int repeatable; | |
555 | ||
556 | return cmdtp->cmd_rep(cmdtp, flag, argc, argv, &repeatable); | |
557 | } | |
558 | ||
bdf8e34b SG |
559 | /** |
560 | * Call a command function. This should be the only route in U-Boot to call | |
561 | * a command, so that we can track whether we are waiting for input or | |
562 | * executing a command. | |
563 | * | |
564 | * @param cmdtp Pointer to the command to execute | |
565 | * @param flag Some flags normally 0 (see CMD_FLAG_.. above) | |
566 | * @param argc Number of arguments (arg 0 must be the command text) | |
567 | * @param argv Arguments | |
80a48dd4 | 568 | * @param repeatable Can the command be repeated |
bdf8e34b SG |
569 | * @return 0 if command succeeded, else non-zero (CMD_RET_...) |
570 | */ | |
80a48dd4 BB |
571 | static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[], |
572 | int *repeatable) | |
bdf8e34b SG |
573 | { |
574 | int result; | |
575 | ||
80a48dd4 | 576 | result = cmdtp->cmd_rep(cmdtp, flag, argc, argv, repeatable); |
bdf8e34b | 577 | if (result) |
58b6ad68 | 578 | debug("Command failed, result=%d\n", result); |
bdf8e34b SG |
579 | return result; |
580 | } | |
9d12d5d4 SG |
581 | |
582 | enum command_ret_t cmd_process(int flag, int argc, char * const argv[], | |
34765e88 | 583 | int *repeatable, ulong *ticks) |
9d12d5d4 SG |
584 | { |
585 | enum command_ret_t rc = CMD_RET_SUCCESS; | |
586 | cmd_tbl_t *cmdtp; | |
587 | ||
7ae31fcc CM |
588 | #if defined(CONFIG_SYS_XTRACE) |
589 | char *xtrace; | |
590 | ||
591 | xtrace = env_get("xtrace"); | |
592 | if (xtrace) { | |
593 | puts("+"); | |
594 | for (int i = 0; i < argc; i++) { | |
595 | puts(" "); | |
596 | puts(argv[i]); | |
597 | } | |
598 | puts("\n"); | |
599 | } | |
600 | #endif | |
601 | ||
9d12d5d4 SG |
602 | /* Look up command in command table */ |
603 | cmdtp = find_cmd(argv[0]); | |
604 | if (cmdtp == NULL) { | |
605 | printf("Unknown command '%s' - try 'help'\n", argv[0]); | |
606 | return 1; | |
607 | } | |
608 | ||
609 | /* found - check max args */ | |
610 | if (argc > cmdtp->maxargs) | |
611 | rc = CMD_RET_USAGE; | |
612 | ||
613 | #if defined(CONFIG_CMD_BOOTD) | |
614 | /* avoid "bootd" recursion */ | |
615 | else if (cmdtp->cmd == do_bootd) { | |
616 | if (flag & CMD_FLAG_BOOTD) { | |
617 | puts("'bootd' recursion detected\n"); | |
618 | rc = CMD_RET_FAILURE; | |
619 | } else { | |
620 | flag |= CMD_FLAG_BOOTD; | |
621 | } | |
622 | } | |
623 | #endif | |
624 | ||
625 | /* If OK so far, then do the command */ | |
626 | if (!rc) { | |
80a48dd4 BB |
627 | int newrep; |
628 | ||
34765e88 RG |
629 | if (ticks) |
630 | *ticks = get_timer(0); | |
80a48dd4 | 631 | rc = cmd_call(cmdtp, flag, argc, argv, &newrep); |
34765e88 RG |
632 | if (ticks) |
633 | *ticks = get_timer(*ticks); | |
80a48dd4 | 634 | *repeatable &= newrep; |
9d12d5d4 SG |
635 | } |
636 | if (rc == CMD_RET_USAGE) | |
637 | rc = cmd_usage(cmdtp); | |
638 | return rc; | |
639 | } | |
16ff9902 SG |
640 | |
641 | int cmd_process_error(cmd_tbl_t *cmdtp, int err) | |
642 | { | |
27eb7bce MS |
643 | if (err == CMD_RET_USAGE) |
644 | return CMD_RET_USAGE; | |
645 | ||
16ff9902 SG |
646 | if (err) { |
647 | printf("Command '%s' failed: Error %d\n", cmdtp->name, err); | |
37233240 | 648 | return CMD_RET_FAILURE; |
16ff9902 SG |
649 | } |
650 | ||
37233240 | 651 | return CMD_RET_SUCCESS; |
16ff9902 | 652 | } |