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 int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
141 return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
144 int cmd_usage(const cmd_tbl_t *cmdtp)
146 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
148 #ifdef CONFIG_SYS_LONGHELP
149 printf("Usage:\n%s ", cmdtp->name);
152 puts ("- No additional help available.\n");
158 #endif /* CONFIG_SYS_LONGHELP */
162 #ifdef CONFIG_AUTO_COMPLETE
164 int var_complete(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
166 static char tmp_buf[512];
169 space = last_char == '\0' || isblank(last_char);
171 if (space && argc == 1)
172 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
174 if (!space && argc == 2)
175 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
180 /*************************************************************************************/
182 static int complete_cmdv(int argc, char * const argv[], char last_char, int maxv, char *cmdv[])
197 /* output full list of commands */
198 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
199 if (n_found >= maxv - 2) {
200 cmdv[n_found++] = "...";
203 cmdv[n_found++] = cmdtp->name;
205 cmdv[n_found] = NULL;
209 /* more than one arg or one but the start of the next */
210 if (argc > 1 || (last_char == '\0' || isblank(last_char))) {
211 cmdtp = find_cmd(argv[0]);
212 if (cmdtp == NULL || cmdtp->complete == NULL) {
216 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
221 * Some commands allow length modifiers (like "cp.b");
222 * compare command name only until first dot.
224 p = strchr(cmd, '.');
230 /* return the partial matches */
231 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
233 clen = strlen(cmdtp->name);
237 if (memcmp(cmd, cmdtp->name, len) != 0)
241 if (n_found >= maxv - 2) {
242 cmdv[n_found++] = "...";
246 cmdv[n_found++] = cmdtp->name;
249 cmdv[n_found] = NULL;
253 static int make_argv(char *s, int argvsz, char *argv[])
257 /* split into argv */
258 while (argc < argvsz - 1) {
260 /* skip any white space */
264 if (*s == '\0') /* end of s, no more args */
267 argv[argc++] = s; /* begin of argument string */
269 /* find end of string */
270 while (*s && !isblank(*s))
273 if (*s == '\0') /* end of s, no more args */
276 *s++ = '\0'; /* terminate current arg */
283 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char * const argv[])
285 int ll = leader != NULL ? strlen(leader) : 0;
286 int sl = sep != NULL ? strlen(sep) : 0;
294 i = linemax; /* force leader and newline */
295 while (*argv != NULL) {
296 len = strlen(*argv) + sl;
297 if (i + len >= linemax) {
310 static int find_common_prefix(char * const argv[])
313 char *anchor, *s, *t;
320 len = strlen(anchor);
321 while ((t = *argv++) != NULL) {
323 for (i = 0; i < len; i++, t++, s++) {
332 static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
334 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
336 int n = *np, col = *colp;
337 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
341 int i, j, k, len, seplen, argc;
345 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
346 return 0; /* not in normal console */
350 last_char = buf[cnt - 1];
354 /* copy to secondary buffer which will be affected */
355 strcpy(tmp_buf, buf);
357 /* separate into argv */
358 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
360 /* do the completion and return the possible completions */
361 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
363 /* no match; bell and out */
365 if (argc > 1) /* allow tab for non command */
375 if (i == 1) { /* one match; perfect */
376 k = strlen(argv[argc - 1]);
381 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
382 k = strlen(argv[argc - 1]);
392 /* make sure it fits */
393 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
399 for (i = 0; i < len; i++)
402 for (i = 0; i < seplen; i++)
413 print_argv(NULL, " ", " ", 78, cmdv);
424 int cmd_get_data_size(char* arg, int default_size)
426 /* Check for a size specification .b, .w or .l.
428 int len = strlen(arg);
429 if (len > 2 && arg[len-2] == '.') {
447 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
448 DECLARE_GLOBAL_DATA_PTR;
450 void fixup_cmdtable(cmd_tbl_t *cmdtp, int size)
454 if (gd->reloc_off == 0)
457 for (i = 0; i < size; i++) {
460 addr = (ulong) (cmdtp->cmd) + gd->reloc_off;
462 printf("Command \"%s\": 0x%08lx => 0x%08lx\n",
463 cmdtp->name, (ulong) (cmdtp->cmd), addr);
466 (int (*)(struct cmd_tbl_s *, int, int, char * const []))addr;
467 addr = (ulong)(cmdtp->name) + gd->reloc_off;
468 cmdtp->name = (char *)addr;
470 addr = (ulong)(cmdtp->usage) + gd->reloc_off;
471 cmdtp->usage = (char *)addr;
473 #ifdef CONFIG_SYS_LONGHELP
475 addr = (ulong)(cmdtp->help) + gd->reloc_off;
476 cmdtp->help = (char *)addr;
479 #ifdef CONFIG_AUTO_COMPLETE
480 if (cmdtp->complete) {
481 addr = (ulong)(cmdtp->complete) + gd->reloc_off;
483 (int (*)(int, char * const [], char, int, char * []))addr;