2 * (C) Copyright 2000-2009
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24 #if defined(__GNUC__) && defined(__OPTIMIZE__) && (__GNUC__ >= 4 && __GNUC_MINOR__ >= 4)
25 #pragma GCC optimize ("O2")
29 * Command Processor Table
34 #include <linux/ctype.h>
37 * Use puts() instead of printf() to avoid printf buffer overflow
38 * for long help messages
41 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
42 flag, int argc, char * const argv[])
47 if (argc == 1) { /*show list of commands */
48 cmd_tbl_t *cmd_array[cmd_items];
51 /* Make array of commands from .uboot_cmd section */
53 for (i = 0; i < cmd_items; i++) {
54 cmd_array[i] = cmdtp++;
57 /* Sort command list (trivial bubble sort) */
58 for (i = cmd_items - 1; i > 0; --i) {
60 for (j = 0; j < i; ++j) {
61 if (strcmp (cmd_array[j]->name,
62 cmd_array[j + 1]->name) > 0) {
65 cmd_array[j] = cmd_array[j + 1];
66 cmd_array[j + 1] = tmp;
74 /* print short help (usage) */
75 for (i = 0; i < cmd_items; i++) {
76 const char *usage = cmd_array[i]->usage;
78 /* allow user abort */
83 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
84 cmd_array[i]->name, usage);
89 * command help (long version)
91 for (i = 1; i < argc; ++i) {
92 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
93 rcode |= cmd_usage(cmdtp);
95 printf ("Unknown command '%s' - try 'help'"
96 " without arguments for list of all"
97 " known commands\n\n", argv[i]
105 /***************************************************************************
106 * find command table entry for a command
108 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
111 cmd_tbl_t *cmdtp_temp = table; /*Init value */
119 * Some commands allow length modifiers (like "cp.b");
120 * compare command name only until first dot.
122 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
125 cmdtp != table + table_len;
127 if (strncmp (cmd, cmdtp->name, len) == 0) {
128 if (len == strlen (cmdtp->name))
129 return cmdtp; /* full match */
131 cmdtp_temp = cmdtp; /* abbreviated command ? */
135 if (n_found == 1) { /* exactly one match */
139 return NULL; /* not found or ambiguous command */
142 cmd_tbl_t *find_cmd (const char *cmd)
144 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
145 const int len = ll_entry_count(cmd_tbl_t, cmd);
146 return find_cmd_tbl(cmd, start, len);
149 int cmd_usage(const cmd_tbl_t *cmdtp)
151 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
153 #ifdef CONFIG_SYS_LONGHELP
154 printf("Usage:\n%s ", cmdtp->name);
157 puts ("- No additional help available.\n");
163 #endif /* CONFIG_SYS_LONGHELP */
167 #ifdef CONFIG_AUTO_COMPLETE
169 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
171 static char tmp_buf[512];
174 space = last_char == '\0' || isblank(last_char);
176 if (space && argc == 1)
177 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
179 if (!space && argc == 2)
180 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
185 /*************************************************************************************/
187 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
189 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
190 const int count = ll_entry_count(cmd_tbl_t, cmd);
191 const cmd_tbl_t *cmdend = cmdtp + count;
204 /* output full list of commands */
205 for (; cmdtp != cmdend; cmdtp++) {
206 if (n_found >= maxv - 2) {
207 cmdv[n_found] = "...";
210 cmdv[n_found] = cmdtp->name;
212 cmdv[n_found] = NULL;
216 /* more than one arg or one but the start of the next */
217 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
218 cmdtp = find_cmd(argv[0]);
219 if (cmdtp == NULL || cmdtp->complete == NULL) {
223 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
228 * Some commands allow length modifiers (like "cp.b");
229 * compare command name only until first dot.
231 p = strchr(cmd, '.');
237 /* return the partial matches */
238 for (; cmdtp != cmdend; cmdtp++) {
240 clen = strlen(cmdtp->name);
244 if (memcmp(cmd, cmdtp->name, len) != 0)
248 if (n_found >= maxv - 2) {
249 cmdv[n_found++] = "...";
253 cmdv[n_found++] = cmdtp->name;
256 cmdv[n_found] = NULL;
260 static int make_argv(char *s, int argvsz, char *argv[])
264 /* split into argv */
265 while (argc < argvsz - 1) {
267 /* skip any white space */
271 if (*s == '\0') /* end of s, no more args */
274 argv[argc++] = s; /* begin of argument string */
276 /* find end of string */
277 while (*s && !isblank(*s))
280 if (*s == '\0') /* end of s, no more args */
283 *s++ = '\0'; /* terminate current arg */
290 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
292 int ll = leader != NULL ? strlen(leader) : 0;
293 int sl = sep != NULL ? strlen(sep) : 0;
301 i = linemax; /* force leader and newline */
302 while (*argv != NULL) {
303 len = strlen(*argv) + sl;
304 if (i + len >= linemax) {
317 static int find_common_prefix(char * const argv[])
320 char *anchor, *s, *t;
327 len = strlen(anchor);
328 while ((t = *argv++) != NULL) {
330 for (i = 0; i < len; i++, t++, s++) {
339 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
341 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
343 int n = *np, col = *colp;
344 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
348 int i, j, k, len, seplen, argc;
352 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
353 return 0; /* not in normal console */
357 last_char = buf[cnt - 1];
361 /* copy to secondary buffer which will be affected */
362 strcpy(tmp_buf, buf);
364 /* separate into argv */
365 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
367 /* do the completion and return the possible completions */
368 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
370 /* no match; bell and out */
372 if (argc > 1) /* allow tab for non command */
382 if (i == 1) { /* one match; perfect */
383 k = strlen(argv[argc - 1]);
388 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
389 k = strlen(argv[argc - 1]);
399 /* make sure it fits */
400 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
406 for (i = 0; i < len; i++)
409 for (i = 0; i < seplen; i++)
420 print_argv(NULL, " ", " ", 78, cmdv);
431 int cmd_get_data_size(char* arg, int default_size)
433 /* Check for a size specification .b, .w or .l.
435 int len = strlen(arg);
436 if (len > 2 && arg[len-2] == '.') {
454 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
455 DECLARE_GLOBAL_DATA_PTR;
457 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
461 if (gd->reloc_off == 0)
464 for (i = 0; i < size; i++) {
467 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
469 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
470 cmdtp->name, (ulong) (cmdtp->cmd), addr);
473 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
474 addr = (ulong)(cmdtp->name) + gd->reloc_off;
475 cmdtp->name = (char *)addr;
477 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
478 cmdtp->usage = (char *)addr;
480 #ifdef CONFIG_SYS_LONGHELP
482 addr = (ulong)(cmdtp->help) + gd->reloc_off;
483 cmdtp->help = (char *)addr;
486 #ifdef CONFIG_AUTO_COMPLETE
487 if (cmdtp->complete) {
488 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
490 (int (*)(int, char * const [], char, int, char * []))addr;
499 * Call a command function. This should be the only route in U-Boot to call
500 * a command, so that we can track whether we are waiting for input or
501 * executing a command.
503 * @param cmdtp Pointer to the command to execute
504 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
505 * @param argc Number of arguments (arg 0 must be the command text)
506 * @param argv Arguments
507 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
509 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
513 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
515 debug("Command failed, result=%d", result);
519 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
522 enum command_ret_t rc = CMD_RET_SUCCESS;
525 /* Look up command in command table */
526 cmdtp = find_cmd(argv[0]);
528 printf("Unknown command '%s' - try 'help'\n", argv[0]);
532 /* found - check max args */
533 if (argc > cmdtp->maxargs)
536 #if defined(CONFIG_CMD_BOOTD)
537 /* avoid "bootd" recursion */
538 else if (cmdtp->cmd == do_bootd) {
539 if (flag & CMD_FLAG_BOOTD) {
540 puts("'bootd' recursion detected\n");
541 rc = CMD_RET_FAILURE;
543 flag |= CMD_FLAG_BOOTD;
548 /* If OK so far, then do the command */
550 rc = cmd_call(cmdtp, flag, argc, argv);
551 *repeatable &= cmdtp->repeatable;
553 if (rc == CMD_RET_USAGE)
554 rc = cmd_usage(cmdtp);