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,
25 * Command Processor Table
30 #include <linux/ctype.h>
33 * Use puts() instead of printf() to avoid printf buffer overflow
34 * for long help messages
37 int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
38 flag, int argc, char * const argv[])
43 if (argc == 1) { /*show list of commands */
44 cmd_tbl_t *cmd_array[cmd_items];
47 /* Make array of commands from .uboot_cmd section */
49 for (i = 0; i < cmd_items; i++) {
50 cmd_array[i] = cmdtp++;
53 /* Sort command list (trivial bubble sort) */
54 for (i = cmd_items - 1; i > 0; --i) {
56 for (j = 0; j < i; ++j) {
57 if (strcmp (cmd_array[j]->name,
58 cmd_array[j + 1]->name) > 0) {
61 cmd_array[j] = cmd_array[j + 1];
62 cmd_array[j + 1] = tmp;
70 /* print short help (usage) */
71 for (i = 0; i < cmd_items; i++) {
72 const char *usage = cmd_array[i]->usage;
74 /* allow user abort */
79 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
80 cmd_array[i]->name, usage);
85 * command help (long version)
87 for (i = 1; i < argc; ++i) {
88 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
89 rcode |= cmd_usage(cmdtp);
91 printf ("Unknown command '%s' - try 'help'"
92 " without arguments for list of all"
93 " known commands\n\n", argv[i]
101 /***************************************************************************
102 * find command table entry for a command
104 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
107 cmd_tbl_t *cmdtp_temp = table; /*Init value */
115 * Some commands allow length modifiers (like "cp.b");
116 * compare command name only until first dot.
118 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
121 cmdtp != table + table_len;
123 if (strncmp (cmd, cmdtp->name, len) == 0) {
124 if (len == strlen (cmdtp->name))
125 return cmdtp; /* full match */
127 cmdtp_temp = cmdtp; /* abbreviated command ? */
131 if (n_found == 1) { /* exactly one match */
135 return NULL; /* not found or ambiguous command */
138 cmd_tbl_t *find_cmd (const char *cmd)
140 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
141 const int len = ll_entry_count(cmd_tbl_t, cmd);
142 return find_cmd_tbl(cmd, start, len);
145 int cmd_usage(const cmd_tbl_t *cmdtp)
147 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
149 #ifdef CONFIG_SYS_LONGHELP
150 printf("Usage:\n%s ", cmdtp->name);
153 puts ("- No additional help available.\n");
159 #endif /* CONFIG_SYS_LONGHELP */
163 #ifdef CONFIG_AUTO_COMPLETE
165 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
167 static char tmp_buf[512];
170 space = last_char == '\0' || isblank(last_char);
172 if (space && argc == 1)
173 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
175 if (!space && argc == 2)
176 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
181 /*************************************************************************************/
183 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
185 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
186 const int count = ll_entry_count(cmd_tbl_t, cmd);
187 const cmd_tbl_t *cmdend = cmdtp + count;
200 /* output full list of commands */
201 for (; cmdtp != cmdend; cmdtp++) {
202 if (n_found >= maxv - 2) {
203 cmdv[n_found] = "...";
206 cmdv[n_found] = cmdtp->name;
208 cmdv[n_found] = NULL;
212 /* more than one arg or one but the start of the next */
213 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
214 cmdtp = find_cmd(argv[0]);
215 if (cmdtp == NULL || cmdtp->complete == NULL) {
219 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
224 * Some commands allow length modifiers (like "cp.b");
225 * compare command name only until first dot.
227 p = strchr(cmd, '.');
233 /* return the partial matches */
234 for (; cmdtp != cmdend; cmdtp++) {
236 clen = strlen(cmdtp->name);
240 if (memcmp(cmd, cmdtp->name, len) != 0)
244 if (n_found >= maxv - 2) {
245 cmdv[n_found++] = "...";
249 cmdv[n_found++] = cmdtp->name;
252 cmdv[n_found] = NULL;
256 static int make_argv(char *s, int argvsz, char *argv[])
260 /* split into argv */
261 while (argc < argvsz - 1) {
263 /* skip any white space */
267 if (*s == '\0') /* end of s, no more args */
270 argv[argc++] = s; /* begin of argument string */
272 /* find end of string */
273 while (*s && !isblank(*s))
276 if (*s == '\0') /* end of s, no more args */
279 *s++ = '\0'; /* terminate current arg */
286 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
288 int ll = leader != NULL ? strlen(leader) : 0;
289 int sl = sep != NULL ? strlen(sep) : 0;
297 i = linemax; /* force leader and newline */
298 while (*argv != NULL) {
299 len = strlen(*argv) + sl;
300 if (i + len >= linemax) {
313 static int find_common_prefix(char * const argv[])
316 char *anchor, *s, *t;
323 len = strlen(anchor);
324 while ((t = *argv++) != NULL) {
326 for (i = 0; i < len; i++, t++, s++) {
335 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
337 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
339 int n = *np, col = *colp;
340 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
344 int i, j, k, len, seplen, argc;
348 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
349 return 0; /* not in normal console */
353 last_char = buf[cnt - 1];
357 /* copy to secondary buffer which will be affected */
358 strcpy(tmp_buf, buf);
360 /* separate into argv */
361 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
363 /* do the completion and return the possible completions */
364 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
366 /* no match; bell and out */
368 if (argc > 1) /* allow tab for non command */
378 if (i == 1) { /* one match; perfect */
379 k = strlen(argv[argc - 1]);
384 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
385 k = strlen(argv[argc - 1]);
395 /* make sure it fits */
396 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
402 for (i = 0; i < len; i++)
405 for (i = 0; i < seplen; i++)
416 print_argv(NULL, " ", " ", 78, cmdv);
427 int cmd_get_data_size(char* arg, int default_size)
429 /* Check for a size specification .b, .w or .l.
431 int len = strlen(arg);
432 if (len > 2 && arg[len-2] == '.') {
450 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
451 DECLARE_GLOBAL_DATA_PTR;
453 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
457 if (gd->reloc_off == 0)
460 for (i = 0; i < size; i++) {
463 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
465 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
466 cmdtp->name, (ulong) (cmdtp->cmd), addr);
469 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
470 addr = (ulong)(cmdtp->name) + gd->reloc_off;
471 cmdtp->name = (char *)addr;
473 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
474 cmdtp->usage = (char *)addr;
476 #ifdef CONFIG_SYS_LONGHELP
478 addr = (ulong)(cmdtp->help) + gd->reloc_off;
479 cmdtp->help = (char *)addr;
482 #ifdef CONFIG_AUTO_COMPLETE
483 if (cmdtp->complete) {
484 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
486 (int (*)(int, char * const [], char, int, char * []))addr;
495 * Call a command function. This should be the only route in U-Boot to call
496 * a command, so that we can track whether we are waiting for input or
497 * executing a command.
499 * @param cmdtp Pointer to the command to execute
500 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
501 * @param argc Number of arguments (arg 0 must be the command text)
502 * @param argv Arguments
503 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
505 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
509 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
511 debug("Command failed, result=%d", result);
515 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
516 int *repeatable, ulong *ticks)
518 enum command_ret_t rc = CMD_RET_SUCCESS;
521 /* Look up command in command table */
522 cmdtp = find_cmd(argv[0]);
524 printf("Unknown command '%s' - try 'help'\n", argv[0]);
528 /* found - check max args */
529 if (argc > cmdtp->maxargs)
532 #if defined(CONFIG_CMD_BOOTD)
533 /* avoid "bootd" recursion */
534 else if (cmdtp->cmd == do_bootd) {
535 if (flag & CMD_FLAG_BOOTD) {
536 puts("'bootd' recursion detected\n");
537 rc = CMD_RET_FAILURE;
539 flag |= CMD_FLAG_BOOTD;
544 /* If OK so far, then do the command */
547 *ticks = get_timer(0);
548 rc = cmd_call(cmdtp, flag, argc, argv);
550 *ticks = get_timer(*ticks);
551 *repeatable &= cmdtp->repeatable;
553 if (rc == CMD_RET_USAGE)
554 rc = cmd_usage(cmdtp);