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"
77 #ifdef CFG_HUSH_PARSER
80 do_test (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
83 int left
, adv
, expr
, last_expr
, neg
, last_cmp
;
94 printf(" %s", argv
[left
++]);
99 left
= argc
- 1; ap
= argv
+ 1;
100 if (left
> 0 && strcmp(ap
[0], "!") == 0) {
112 if (strcmp(ap
[0], "-o") == 0 || strcmp(ap
[0], "-a") == 0)
114 else if (strcmp(ap
[0], "-z") == 0 || strcmp(ap
[0], "-n") == 0)
125 if (strcmp(ap
[0], "-o") == 0) {
128 } else if (strcmp(ap
[0], "-a") == 0) {
138 if (strcmp(ap
[0], "-z") == 0)
139 expr
= strlen(ap
[1]) == 0 ? 1 : 0;
140 else if (strcmp(ap
[0], "-n") == 0)
141 expr
= strlen(ap
[1]) == 0 ? 0 : 1;
148 expr
= last_expr
|| expr
;
149 else if (last_cmp
== 1)
150 expr
= last_expr
&& expr
;
155 if (strcmp(ap
[1], "=") == 0)
156 expr
= strcmp(ap
[0], ap
[2]) == 0;
157 else if (strcmp(ap
[1], "!=") == 0)
158 expr
= strcmp(ap
[0], ap
[2]) != 0;
159 else if (strcmp(ap
[1], ">") == 0)
160 expr
= strcmp(ap
[0], ap
[2]) > 0;
161 else if (strcmp(ap
[1], "<") == 0)
162 expr
= strcmp(ap
[0], ap
[2]) < 0;
163 else if (strcmp(ap
[1], "-eq") == 0)
164 expr
= simple_strtol(ap
[0], NULL
, 10) == simple_strtol(ap
[2], NULL
, 10);
165 else if (strcmp(ap
[1], "-ne") == 0)
166 expr
= simple_strtol(ap
[0], NULL
, 10) != simple_strtol(ap
[2], NULL
, 10);
167 else if (strcmp(ap
[1], "-lt") == 0)
168 expr
= simple_strtol(ap
[0], NULL
, 10) < simple_strtol(ap
[2], NULL
, 10);
169 else if (strcmp(ap
[1], "-le") == 0)
170 expr
= simple_strtol(ap
[0], NULL
, 10) <= simple_strtol(ap
[2], NULL
, 10);
171 else if (strcmp(ap
[1], "-gt") == 0)
172 expr
= simple_strtol(ap
[0], NULL
, 10) > simple_strtol(ap
[2], NULL
, 10);
173 else if (strcmp(ap
[1], "-ge") == 0)
174 expr
= simple_strtol(ap
[0], NULL
, 10) >= simple_strtol(ap
[2], NULL
, 10);
181 expr
= last_expr
|| expr
;
182 else if (last_cmp
== 1)
183 expr
= last_expr
&& expr
;
187 ap
+= adv
; left
-= adv
;
196 printf(": returns %d\n", expr
);
203 test
, CFG_MAXARGS
, 1, do_test
,
204 "test - minimal test like /bin/sh\n",
206 " - test functionality\n"
210 do_exit (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
216 r
= simple_strtoul(argv
[1], NULL
, 10);
223 "exit - exit script\n",
224 " - exit functionality\n"
231 * Use puts() instead of printf() to avoid printf buffer overflow
232 * for long help messages
234 int do_help (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
239 if (argc
== 1) { /*show list of commands */
241 int cmd_items
= &__u_boot_cmd_end
-
242 &__u_boot_cmd_start
; /* pointer arith! */
243 cmd_tbl_t
*cmd_array
[cmd_items
];
246 /* Make array of commands from .uboot_cmd section */
247 cmdtp
= &__u_boot_cmd_start
;
248 for (i
= 0; i
< cmd_items
; i
++) {
249 cmd_array
[i
] = cmdtp
++;
252 /* Sort command list (trivial bubble sort) */
253 for (i
= cmd_items
- 1; i
> 0; --i
) {
255 for (j
= 0; j
< i
; ++j
) {
256 if (strcmp (cmd_array
[j
]->name
,
257 cmd_array
[j
+ 1]->name
) > 0) {
260 cmd_array
[j
] = cmd_array
[j
+ 1];
261 cmd_array
[j
+ 1] = tmp
;
269 /* print short help (usage) */
270 for (i
= 0; i
< cmd_items
; i
++) {
271 const char *usage
= cmd_array
[i
]->usage
;
273 /* allow user abort */
283 * command help (long version)
285 for (i
= 1; i
< argc
; ++i
) {
286 if ((cmdtp
= find_cmd (argv
[i
])) != NULL
) {
288 /* found - print (long) help info */
294 puts ("- No help available.\n");
298 #else /* no long help available */
301 #endif /* CFG_LONGHELP */
303 printf ("Unknown command '%s' - try 'help'"
304 " without arguments for list of all"
305 " known commands\n\n", argv
[i
]
315 help
, CFG_MAXARGS
, 1, do_help
,
316 "help - print online help\n",
318 " - show help information (for 'command')\n"
319 "'help' prints online help for the monitor commands.\n\n"
320 "Without arguments, it prints a short usage message for all commands.\n\n"
321 "To get detailed help information for specific commands you can type\n"
322 "'help' with one or more command names as arguments.\n"
325 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
327 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
328 "?", CFG_MAXARGS
, 1, do_help
,
329 "? - alias for 'help'\n",
333 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
334 "?", CFG_MAXARGS
, 1, do_help
,
335 "? - alias for 'help'\n"
337 #endif /* CFG_LONGHELP */
339 /***************************************************************************
340 * find command table entry for a command
342 cmd_tbl_t
*find_cmd (const char *cmd
)
345 cmd_tbl_t
*cmdtp_temp
= &__u_boot_cmd_start
; /*Init value */
351 * Some commands allow length modifiers (like "cp.b");
352 * compare command name only until first dot.
354 len
= ((p
= strchr(cmd
, '.')) == NULL
) ? strlen (cmd
) : (p
- cmd
);
356 for (cmdtp
= &__u_boot_cmd_start
;
357 cmdtp
!= &__u_boot_cmd_end
;
359 if (strncmp (cmd
, cmdtp
->name
, len
) == 0) {
360 if (len
== strlen (cmdtp
->name
))
361 return cmdtp
; /* full match */
363 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
367 if (n_found
== 1) { /* exactly one match */
371 return NULL
; /* not found or ambiguous command */
374 #ifdef CONFIG_AUTO_COMPLETE
376 int var_complete(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
378 static char tmp_buf
[512];
381 space
= last_char
== '\0' || last_char
== ' ' || last_char
== '\t';
383 if (space
&& argc
== 1)
384 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
386 if (!space
&& argc
== 2)
387 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
392 static void install_auto_complete_handler(const char *cmd
,
393 int (*complete
)(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[]))
397 cmdtp
= find_cmd(cmd
);
401 cmdtp
->complete
= complete
;
404 void install_auto_complete(void)
406 install_auto_complete_handler("printenv", var_complete
);
407 install_auto_complete_handler("setenv", var_complete
);
408 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
409 install_auto_complete_handler("run", var_complete
);
413 /*************************************************************************************/
415 static int complete_cmdv(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
430 /* output full list of commands */
431 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
432 if (n_found
>= maxv
- 2) {
433 cmdv
[n_found
++] = "...";
436 cmdv
[n_found
++] = cmdtp
->name
;
438 cmdv
[n_found
] = NULL
;
442 /* more than one arg or one but the start of the next */
443 if (argc
> 1 || (last_char
== '\0' || last_char
== ' ' || last_char
== '\t')) {
444 cmdtp
= find_cmd(argv
[0]);
445 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
449 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
454 * Some commands allow length modifiers (like "cp.b");
455 * compare command name only until first dot.
457 p
= strchr(cmd
, '.');
463 /* return the partial matches */
464 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
466 clen
= strlen(cmdtp
->name
);
470 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
474 if (n_found
>= maxv
- 2) {
475 cmdv
[n_found
++] = "...";
479 cmdv
[n_found
++] = cmdtp
->name
;
482 cmdv
[n_found
] = NULL
;
486 static int make_argv(char *s
, int argvsz
, char *argv
[])
490 /* split into argv */
491 while (argc
< argvsz
- 1) {
493 /* skip any white space */
494 while ((*s
== ' ') || (*s
== '\t'))
497 if (*s
== '\0') /* end of s, no more args */
500 argv
[argc
++] = s
; /* begin of argument string */
502 /* find end of string */
503 while (*s
&& (*s
!= ' ') && (*s
!= '\t'))
506 if (*s
== '\0') /* end of s, no more args */
509 *s
++ = '\0'; /* terminate current arg */
516 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char *argv
[])
518 int ll
= leader
!= NULL
? strlen(leader
) : 0;
519 int sl
= sep
!= NULL
? strlen(sep
) : 0;
527 i
= linemax
; /* force leader and newline */
528 while (*argv
!= NULL
) {
529 len
= strlen(*argv
) + sl
;
530 if (i
+ len
>= linemax
) {
543 static int find_common_prefix(char *argv
[])
546 char *anchor
, *s
, *t
;
553 len
= strlen(anchor
);
554 while ((t
= *argv
++) != NULL
) {
556 for (i
= 0; i
< len
; i
++, t
++, s
++) {
565 static char tmp_buf
[CFG_CBSIZE
]; /* copy of console I/O buffer */
567 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
569 int n
= *np
, col
= *colp
;
570 char *argv
[CFG_MAXARGS
+ 1]; /* NULL terminated */
574 int i
, j
, k
, len
, seplen
, argc
;
578 if (strcmp(prompt
, CFG_PROMPT
) != 0)
579 return 0; /* not in normal console */
583 last_char
= buf
[cnt
- 1];
587 /* copy to secondary buffer which will be affected */
588 strcpy(tmp_buf
, buf
);
590 /* separate into argv */
591 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
593 /* do the completion and return the possible completions */
594 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
596 /* no match; bell and out */
598 if (argc
> 1) /* allow tab for non command */
608 if (i
== 1) { /* one match; perfect */
609 k
= strlen(argv
[argc
- 1]);
614 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
615 k
= strlen(argv
[argc
- 1]);
625 /* make sure it fits */
626 if (n
+ k
>= CFG_CBSIZE
- 2) {
632 for (i
= 0; i
< len
; i
++)
635 for (i
= 0; i
< seplen
; i
++)
646 print_argv(NULL
, " ", " ", 78, cmdv
);