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
32 * Use puts() instead of printf() to avoid printf buffer overflow
33 * for long help messages
36 int _do_help (cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
, int
37 flag
, int argc
, char *argv
[])
42 if (argc
== 1) { /*show list of commands */
43 cmd_tbl_t
*cmd_array
[cmd_items
];
46 /* Make array of commands from .uboot_cmd section */
48 for (i
= 0; i
< cmd_items
; i
++) {
49 cmd_array
[i
] = cmdtp
++;
52 /* Sort command list (trivial bubble sort) */
53 for (i
= cmd_items
- 1; i
> 0; --i
) {
55 for (j
= 0; j
< i
; ++j
) {
56 if (strcmp (cmd_array
[j
]->name
,
57 cmd_array
[j
+ 1]->name
) > 0) {
60 cmd_array
[j
] = cmd_array
[j
+ 1];
61 cmd_array
[j
+ 1] = tmp
;
69 /* print short help (usage) */
70 for (i
= 0; i
< cmd_items
; i
++) {
71 const char *usage
= cmd_array
[i
]->usage
;
73 /* allow user abort */
78 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH
,
79 cmd_array
[i
]->name
, usage
);
84 * command help (long version)
86 for (i
= 1; i
< argc
; ++i
) {
87 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
88 rcode
|= cmd_usage(cmdtp
);
90 printf ("Unknown command '%s' - try 'help'"
91 " without arguments for list of all"
92 " known commands\n\n", argv
[i
]
100 /***************************************************************************
101 * find command table entry for a command
103 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
106 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
112 * Some commands allow length modifiers (like "cp.b");
113 * compare command name only until first dot.
115 len
= ((p
= strchr(cmd
, '.')) == NULL
) ? strlen (cmd
) : (p
- cmd
);
118 cmdtp
!= table
+ table_len
;
120 if (strncmp (cmd
, cmdtp
->name
, len
) == 0) {
121 if (len
== strlen (cmdtp
->name
))
122 return cmdtp
; /* full match */
124 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
128 if (n_found
== 1) { /* exactly one match */
132 return NULL
; /* not found or ambiguous command */
135 cmd_tbl_t
*find_cmd (const char *cmd
)
137 int len
= &__u_boot_cmd_end
- &__u_boot_cmd_start
;
138 return find_cmd_tbl(cmd
, &__u_boot_cmd_start
, len
);
141 int cmd_usage(cmd_tbl_t
*cmdtp
)
143 printf("%s - %s\n\n", cmdtp
->name
, cmdtp
->usage
);
145 #ifdef CONFIG_SYS_LONGHELP
146 printf("Usage:\n%s ", cmdtp
->name
);
149 puts ("- No additional help available.\n");
155 #endif /* CONFIG_SYS_LONGHELP */
159 #ifdef CONFIG_AUTO_COMPLETE
161 int var_complete(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
163 static char tmp_buf
[512];
166 space
= last_char
== '\0' || last_char
== ' ' || last_char
== '\t';
168 if (space
&& argc
== 1)
169 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
171 if (!space
&& argc
== 2)
172 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
177 static void install_auto_complete_handler(const char *cmd
,
178 int (*complete
)(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[]))
182 cmdtp
= find_cmd(cmd
);
186 cmdtp
->complete
= complete
;
189 void install_auto_complete(void)
191 install_auto_complete_handler("printenv", var_complete
);
192 install_auto_complete_handler("setenv", var_complete
);
193 #if defined(CONFIG_CMD_RUN)
194 install_auto_complete_handler("run", var_complete
);
198 /*************************************************************************************/
200 static int complete_cmdv(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
215 /* output full list of commands */
216 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
217 if (n_found
>= maxv
- 2) {
218 cmdv
[n_found
++] = "...";
221 cmdv
[n_found
++] = cmdtp
->name
;
223 cmdv
[n_found
] = NULL
;
227 /* more than one arg or one but the start of the next */
228 if (argc
> 1 || (last_char
== '\0' || last_char
== ' ' || last_char
== '\t')) {
229 cmdtp
= find_cmd(argv
[0]);
230 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
234 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
239 * Some commands allow length modifiers (like "cp.b");
240 * compare command name only until first dot.
242 p
= strchr(cmd
, '.');
248 /* return the partial matches */
249 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
251 clen
= strlen(cmdtp
->name
);
255 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
259 if (n_found
>= maxv
- 2) {
260 cmdv
[n_found
++] = "...";
264 cmdv
[n_found
++] = cmdtp
->name
;
267 cmdv
[n_found
] = NULL
;
271 static int make_argv(char *s
, int argvsz
, char *argv
[])
275 /* split into argv */
276 while (argc
< argvsz
- 1) {
278 /* skip any white space */
279 while ((*s
== ' ') || (*s
== '\t'))
282 if (*s
== '\0') /* end of s, no more args */
285 argv
[argc
++] = s
; /* begin of argument string */
287 /* find end of string */
288 while (*s
&& (*s
!= ' ') && (*s
!= '\t'))
291 if (*s
== '\0') /* end of s, no more args */
294 *s
++ = '\0'; /* terminate current arg */
301 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char *argv
[])
303 int ll
= leader
!= NULL
? strlen(leader
) : 0;
304 int sl
= sep
!= NULL
? strlen(sep
) : 0;
312 i
= linemax
; /* force leader and newline */
313 while (*argv
!= NULL
) {
314 len
= strlen(*argv
) + sl
;
315 if (i
+ len
>= linemax
) {
328 static int find_common_prefix(char *argv
[])
331 char *anchor
, *s
, *t
;
338 len
= strlen(anchor
);
339 while ((t
= *argv
++) != NULL
) {
341 for (i
= 0; i
< len
; i
++, t
++, s
++) {
350 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
352 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
354 int n
= *np
, col
= *colp
;
355 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
359 int i
, j
, k
, len
, seplen
, argc
;
363 if (strcmp(prompt
, CONFIG_SYS_PROMPT
) != 0)
364 return 0; /* not in normal console */
368 last_char
= buf
[cnt
- 1];
372 /* copy to secondary buffer which will be affected */
373 strcpy(tmp_buf
, buf
);
375 /* separate into argv */
376 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
378 /* do the completion and return the possible completions */
379 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
381 /* no match; bell and out */
383 if (argc
> 1) /* allow tab for non command */
393 if (i
== 1) { /* one match; perfect */
394 k
= strlen(argv
[argc
- 1]);
399 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
400 k
= strlen(argv
[argc
- 1]);
410 /* make sure it fits */
411 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
417 for (i
= 0; i
< len
; i
++)
420 for (i
= 0; i
< seplen
; i
++)
431 print_argv(NULL
, " ", " ", 78, cmdv
);
442 int cmd_get_data_size(char* arg
, int default_size
)
444 /* Check for a size specification .b, .w or .l.
446 int len
= strlen(arg
);
447 if (len
> 2 && arg
[len
-2] == '.') {