2 * (C) Copyright 2000-2003
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 do_version (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
34 extern char version_string
[];
35 printf ("\n%s\n", version_string
);
40 version
, 1, 1, do_version
,
41 "version - print monitor version\n",
46 do_echo (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
50 for (i
= 1; i
< argc
; i
++) {
55 while ((c
= *p
++) != '\0') {
56 if (c
== '\\' && *p
== 'c') {
71 echo
, CFG_MAXARGS
, 1, do_echo
,
72 "echo - echo args to console\n",
74 " - echo args to console; \\c suppresses newline\n"
78 * Use puts() instead of printf() to avoid printf buffer overflow
79 * for long help messages
81 int do_help (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
86 if (argc
== 1) { /*show list of commands */
88 int cmd_items
= &__u_boot_cmd_end
-
89 &__u_boot_cmd_start
; /* pointer arith! */
90 cmd_tbl_t
*cmd_array
[cmd_items
];
93 /* Make array of commands from .uboot_cmd section */
94 cmdtp
= &__u_boot_cmd_start
;
95 for (i
= 0; i
< cmd_items
; i
++) {
96 cmd_array
[i
] = cmdtp
++;
99 /* Sort command list (trivial bubble sort) */
100 for (i
= cmd_items
- 1; i
> 0; --i
) {
102 for (j
= 0; j
< i
; ++j
) {
103 if (strcmp (cmd_array
[j
]->name
,
104 cmd_array
[j
+ 1]->name
) > 0) {
107 cmd_array
[j
] = cmd_array
[j
+ 1];
108 cmd_array
[j
+ 1] = tmp
;
116 /* print short help (usage) */
117 for (i
= 0; i
< cmd_items
; i
++) {
118 const char *usage
= cmd_array
[i
]->usage
;
120 /* allow user abort */
130 * command help (long version)
132 for (i
= 1; i
< argc
; ++i
) {
133 if ((cmdtp
= find_cmd (argv
[i
])) != NULL
) {
135 /* found - print (long) help info */
141 puts ("- No help available.\n");
145 #else /* no long help available */
148 #endif /* CFG_LONGHELP */
150 printf ("Unknown command '%s' - try 'help'"
151 " without arguments for list of all"
152 " known commands\n\n", argv
[i
]
162 help
, CFG_MAXARGS
, 1, do_help
,
163 "help - print online help\n",
165 " - show help information (for 'command')\n"
166 "'help' prints online help for the monitor commands.\n\n"
167 "Without arguments, it prints a short usage message for all commands.\n\n"
168 "To get detailed help information for specific commands you can type\n"
169 "'help' with one or more command names as arguments.\n"
172 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
174 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
175 "?", CFG_MAXARGS
, 1, do_help
,
176 "? - alias for 'help'\n",
180 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
181 "?", CFG_MAXARGS
, 1, do_help
,
182 "? - alias for 'help'\n"
184 #endif /* CFG_LONGHELP */
186 /***************************************************************************
187 * find command table entry for a command
189 cmd_tbl_t
*find_cmd (const char *cmd
)
192 cmd_tbl_t
*cmdtp_temp
= &__u_boot_cmd_start
; /*Init value */
198 * Some commands allow length modifiers (like "cp.b");
199 * compare command name only until first dot.
201 len
= ((p
= strchr(cmd
, '.')) == NULL
) ? strlen (cmd
) : (p
- cmd
);
203 for (cmdtp
= &__u_boot_cmd_start
;
204 cmdtp
!= &__u_boot_cmd_end
;
206 if (strncmp (cmd
, cmdtp
->name
, len
) == 0) {
207 if (len
== strlen (cmdtp
->name
))
208 return cmdtp
; /* full match */
210 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
214 if (n_found
== 1) { /* exactly one match */
218 return NULL
; /* not found or ambiguous command */
221 #ifdef CONFIG_AUTO_COMPLETE
223 int var_complete(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
225 static char tmp_buf
[512];
228 space
= last_char
== '\0' || last_char
== ' ' || last_char
== '\t';
230 if (space
&& argc
== 1)
231 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
233 if (!space
&& argc
== 2)
234 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
239 static void install_auto_complete_handler(const char *cmd
,
240 int (*complete
)(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[]))
244 cmdtp
= find_cmd(cmd
);
248 cmdtp
->complete
= complete
;
251 void install_auto_complete(void)
253 install_auto_complete_handler("printenv", var_complete
);
254 install_auto_complete_handler("setenv", var_complete
);
255 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
256 install_auto_complete_handler("run", var_complete
);
260 /*************************************************************************************/
262 static int complete_cmdv(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
277 /* output full list of commands */
278 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
279 if (n_found
>= maxv
- 2) {
280 cmdv
[n_found
++] = "...";
283 cmdv
[n_found
++] = cmdtp
->name
;
285 cmdv
[n_found
] = NULL
;
289 /* more than one arg or one but the start of the next */
290 if (argc
> 1 || (last_char
== '\0' || last_char
== ' ' || last_char
== '\t')) {
291 cmdtp
= find_cmd(argv
[0]);
292 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
296 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
301 * Some commands allow length modifiers (like "cp.b");
302 * compare command name only until first dot.
304 p
= strchr(cmd
, '.');
310 /* return the partial matches */
311 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
313 clen
= strlen(cmdtp
->name
);
317 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
321 if (n_found
>= maxv
- 2) {
322 cmdv
[n_found
++] = "...";
326 cmdv
[n_found
++] = cmdtp
->name
;
329 cmdv
[n_found
] = NULL
;
333 static int make_argv(char *s
, int argvsz
, char *argv
[])
337 /* split into argv */
338 while (argc
< argvsz
- 1) {
340 /* skip any white space */
341 while ((*s
== ' ') || (*s
== '\t'))
344 if (*s
== '\0') /* end of s, no more args */
347 argv
[argc
++] = s
; /* begin of argument string */
349 /* find end of string */
350 while (*s
&& (*s
!= ' ') && (*s
!= '\t'))
353 if (*s
== '\0') /* end of s, no more args */
356 *s
++ = '\0'; /* terminate current arg */
363 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char *argv
[])
365 int ll
= leader
!= NULL
? strlen(leader
) : 0;
366 int sl
= sep
!= NULL
? strlen(sep
) : 0;
374 i
= linemax
; /* force leader and newline */
375 while (*argv
!= NULL
) {
376 len
= strlen(*argv
) + sl
;
377 if (i
+ len
>= linemax
) {
390 static int find_common_prefix(char *argv
[])
393 char *anchor
, *s
, *t
;
400 len
= strlen(anchor
);
401 while ((t
= *argv
++) != NULL
) {
403 for (i
= 0; i
< len
; i
++, t
++, s
++) {
412 static char tmp_buf
[CFG_CBSIZE
]; /* copy of console I/O buffer */
414 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
416 int n
= *np
, col
= *colp
;
417 char *argv
[CFG_MAXARGS
+ 1]; /* NULL terminated */
421 int i
, j
, k
, len
, seplen
, argc
;
425 if (strcmp(prompt
, CFG_PROMPT
) != 0)
426 return 0; /* not in normal console */
430 last_char
= buf
[cnt
- 1];
434 /* copy to secondary buffer which will be affected */
435 strcpy(tmp_buf
, buf
);
437 /* separate into argv */
438 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
440 /* do the completion and return the possible completions */
441 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
443 /* no match; bell and out */
445 if (argc
> 1) /* allow tab for non command */
455 if (i
== 1) { /* one match; perfect */
456 k
= strlen(argv
[argc
- 1]);
461 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
462 k
= strlen(argv
[argc
- 1]);
472 /* make sure it fits */
473 if (n
+ k
>= CFG_CBSIZE
- 2) {
479 for (i
= 0; i
< len
; i
++)
482 for (i
= 0; i
< seplen
; i
++)
493 print_argv(NULL
, " ", " ", 78, cmdv
);