2 * (C) Copyright 2000-2009
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 (cmd_items > CONFIG_SYS_MAXARGS) {
44 printf("%s: cmd_items %d exceeds hardcoded limit %d."
45 " Recompile with higher CONFIG_SYS_MAXARGS?\n",
46 __func__, cmd_items, CONFIG_SYS_MAXARGS);
50 if (argc == 1) { /*show list of commands */
51 cmd_tbl_t *cmd_array[CONFIG_SYS_MAXARGS];
54 /* Make array of commands from .uboot_cmd section */
56 for (i = 0; i < cmd_items; i++) {
57 cmd_array[i] = cmdtp++;
60 /* Sort command list (trivial bubble sort) */
61 for (i = cmd_items - 1; i > 0; --i) {
63 for (j = 0; j < i; ++j) {
64 if (strcmp (cmd_array[j]->name,
65 cmd_array[j + 1]->name) > 0) {
68 cmd_array[j] = cmd_array[j + 1];
69 cmd_array[j + 1] = tmp;
77 /* print short help (usage) */
78 for (i = 0; i < cmd_items; i++) {
79 const char *usage = cmd_array[i]->usage;
81 /* allow user abort */
86 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
87 cmd_array[i]->name, usage);
92 * command help (long version)
94 for (i = 1; i < argc; ++i) {
95 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
96 rcode |= cmd_usage(cmdtp);
98 printf ("Unknown command '%s' - try 'help'"
99 " without arguments for list of all"
100 " known commands\n\n", argv[i]
108 /***************************************************************************
109 * find command table entry for a command
111 cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
114 cmd_tbl_t *cmdtp_temp = table; /*Init value */
122 * Some commands allow length modifiers (like "cp.b");
123 * compare command name only until first dot.
125 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
128 cmdtp != table + table_len;
130 if (strncmp (cmd, cmdtp->name, len) == 0) {
131 if (len == strlen (cmdtp->name))
132 return cmdtp; /* full match */
134 cmdtp_temp = cmdtp; /* abbreviated command ? */
138 if (n_found == 1) { /* exactly one match */
142 return NULL; /* not found or ambiguous command */
145 cmd_tbl_t *find_cmd (const char *cmd)
147 cmd_tbl_t *start = ll_entry_start(cmd_tbl_t, cmd);
148 const int len = ll_entry_count(cmd_tbl_t, cmd);
149 return find_cmd_tbl(cmd, start, len);
152 int cmd_usage(const cmd_tbl_t *cmdtp)
154 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
156 #ifdef CONFIG_SYS_LONGHELP
157 printf("Usage:\n%s ", cmdtp->name);
160 puts ("- No additional help available.\n");
166 #endif /* CONFIG_SYS_LONGHELP */
170 #ifdef CONFIG_AUTO_COMPLETE
172 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
174 static char tmp_buf[512];
177 space = last_char == '\0' || isblank(last_char);
179 if (space && argc == 1)
180 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
182 if (!space && argc == 2)
183 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
188 /*************************************************************************************/
190 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
192 cmd_tbl_t *cmdtp = ll_entry_start(cmd_tbl_t, cmd);
193 const int count = ll_entry_count(cmd_tbl_t, cmd);
194 const cmd_tbl_t *cmdend = cmdtp + count;
207 /* output full list of commands */
208 for (; cmdtp != cmdend; cmdtp++) {
209 if (n_found >= maxv - 2) {
210 cmdv[n_found] = "...";
213 cmdv[n_found] = cmdtp->name;
215 cmdv[n_found] = NULL;
219 /* more than one arg or one but the start of the next */
220 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
221 cmdtp = find_cmd(argv[0]);
222 if (cmdtp == NULL || cmdtp->complete == NULL) {
226 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
231 * Some commands allow length modifiers (like "cp.b");
232 * compare command name only until first dot.
234 p = strchr(cmd, '.');
240 /* return the partial matches */
241 for (; cmdtp != cmdend; cmdtp++) {
243 clen = strlen(cmdtp->name);
247 if (memcmp(cmd, cmdtp->name, len) != 0)
251 if (n_found >= maxv - 2) {
252 cmdv[n_found++] = "...";
256 cmdv[n_found++] = cmdtp->name;
259 cmdv[n_found] = NULL;
263 static int make_argv(char *s, int argvsz, char *argv[])
267 /* split into argv */
268 while (argc < argvsz - 1) {
270 /* skip any white space */
274 if (*s == '\0') /* end of s, no more args */
277 argv[argc++] = s; /* begin of argument string */
279 /* find end of string */
280 while (*s && !isblank(*s))
283 if (*s == '\0') /* end of s, no more args */
286 *s++ = '\0'; /* terminate current arg */
293 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
295 int ll = leader != NULL ? strlen(leader) : 0;
296 int sl = sep != NULL ? strlen(sep) : 0;
304 i = linemax; /* force leader and newline */
305 while (*argv != NULL) {
306 len = strlen(*argv) + sl;
307 if (i + len >= linemax) {
320 static int find_common_prefix(char * const argv[])
323 char *anchor, *s, *t;
330 len = strlen(anchor);
331 while ((t = *argv++) != NULL) {
333 for (i = 0; i < len; i++, t++, s++) {
342 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
344 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
346 int n = *np, col = *colp;
347 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
351 int i, j, k, len, seplen, argc;
355 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
356 return 0; /* not in normal console */
360 last_char = buf[cnt - 1];
364 /* copy to secondary buffer which will be affected */
365 strcpy(tmp_buf, buf);
367 /* separate into argv */
368 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
370 /* do the completion and return the possible completions */
371 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
373 /* no match; bell and out */
375 if (argc > 1) /* allow tab for non command */
385 if (i == 1) { /* one match; perfect */
386 k = strlen(argv[argc - 1]);
391 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
392 k = strlen(argv[argc - 1]);
402 /* make sure it fits */
403 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
409 for (i = 0; i < len; i++)
412 for (i = 0; i < seplen; i++)
423 print_argv(NULL, " ", " ", 78, cmdv);
434 int cmd_get_data_size(char* arg, int default_size)
436 /* Check for a size specification .b, .w or .l.
438 int len = strlen(arg);
439 if (len > 2 && arg[len-2] == '.') {
457 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
458 DECLARE_GLOBAL_DATA_PTR;
460 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
464 if (gd->reloc_off == 0)
467 for (i = 0; i < size; i++) {
470 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
472 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
473 cmdtp->name, (ulong) (cmdtp->cmd), addr);
476 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
477 addr = (ulong)(cmdtp->name) + gd->reloc_off;
478 cmdtp->name = (char *)addr;
480 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
481 cmdtp->usage = (char *)addr;
483 #ifdef CONFIG_SYS_LONGHELP
485 addr = (ulong)(cmdtp->help) + gd->reloc_off;
486 cmdtp->help = (char *)addr;
489 #ifdef CONFIG_AUTO_COMPLETE
490 if (cmdtp->complete) {
491 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
493 (int (*)(int, char * const [], char, int, char * []))addr;
502 * Call a command function. This should be the only route in U-Boot to call
503 * a command, so that we can track whether we are waiting for input or
504 * executing a command.
506 * @param cmdtp Pointer to the command to execute
507 * @param flag Some flags normally 0 (see CMD_FLAG_.. above)
508 * @param argc Number of arguments (arg 0 must be the command text)
509 * @param argv Arguments
510 * @return 0 if command succeeded, else non-zero (CMD_RET_...)
512 static int cmd_call(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
516 result = (cmdtp->cmd)(cmdtp, flag, argc, argv);
518 debug("Command failed, result=%d", result);
522 enum command_ret_t cmd_process(int flag, int argc, char * const argv[],
525 enum command_ret_t rc = CMD_RET_SUCCESS;
528 /* Look up command in command table */
529 cmdtp = find_cmd(argv[0]);
531 printf("Unknown command '%s' - try 'help'\n", argv[0]);
535 /* found - check max args */
536 if (argc > cmdtp->maxargs)
539 #if defined(CONFIG_CMD_BOOTD)
540 /* avoid "bootd" recursion */
541 else if (cmdtp->cmd == do_bootd) {
542 if (flag & CMD_FLAG_BOOTD) {
543 puts("'bootd' recursion detected\n");
544 rc = CMD_RET_FAILURE;
546 flag |= CMD_FLAG_BOOTD;
551 /* If OK so far, then do the command */
553 rc = cmd_call(cmdtp, flag, argc, argv);
554 *repeatable &= cmdtp->repeatable;
556 if (rc == CMD_RET_USAGE)
557 rc = cmd_usage(cmdtp);