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 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 "print monitor version",
45 #if defined(CONFIG_CMD_ECHO)
48 do_echo (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
52 for (i
= 1; i
< argc
; i
++) {
57 while ((c
= *p
++) != '\0') {
58 if (c
== '\\' && *p
== 'c') {
73 echo
, CONFIG_SYS_MAXARGS
, 1, do_echo
,
74 "echo args to console",
76 " - echo args to console; \\c suppresses newline\n"
81 #ifdef CONFIG_SYS_HUSH_PARSER
84 do_test (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
87 int left
, adv
, expr
, last_expr
, neg
, last_cmp
;
98 printf(" %s", argv
[left
++]);
103 left
= argc
- 1; ap
= argv
+ 1;
104 if (left
> 0 && strcmp(ap
[0], "!") == 0) {
116 if (strcmp(ap
[0], "-o") == 0 || strcmp(ap
[0], "-a") == 0)
118 else if (strcmp(ap
[0], "-z") == 0 || strcmp(ap
[0], "-n") == 0)
129 if (strcmp(ap
[0], "-o") == 0) {
132 } else if (strcmp(ap
[0], "-a") == 0) {
142 if (strcmp(ap
[0], "-z") == 0)
143 expr
= strlen(ap
[1]) == 0 ? 1 : 0;
144 else if (strcmp(ap
[0], "-n") == 0)
145 expr
= strlen(ap
[1]) == 0 ? 0 : 1;
152 expr
= last_expr
|| expr
;
153 else if (last_cmp
== 1)
154 expr
= last_expr
&& expr
;
159 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], ">") == 0)
164 expr
= strcmp(ap
[0], ap
[2]) > 0;
165 else if (strcmp(ap
[1], "<") == 0)
166 expr
= strcmp(ap
[0], ap
[2]) < 0;
167 else if (strcmp(ap
[1], "-eq") == 0)
168 expr
= simple_strtol(ap
[0], NULL
, 10) == simple_strtol(ap
[2], NULL
, 10);
169 else if (strcmp(ap
[1], "-ne") == 0)
170 expr
= simple_strtol(ap
[0], NULL
, 10) != simple_strtol(ap
[2], NULL
, 10);
171 else if (strcmp(ap
[1], "-lt") == 0)
172 expr
= simple_strtol(ap
[0], NULL
, 10) < simple_strtol(ap
[2], NULL
, 10);
173 else if (strcmp(ap
[1], "-le") == 0)
174 expr
= simple_strtol(ap
[0], NULL
, 10) <= simple_strtol(ap
[2], NULL
, 10);
175 else if (strcmp(ap
[1], "-gt") == 0)
176 expr
= simple_strtol(ap
[0], NULL
, 10) > simple_strtol(ap
[2], NULL
, 10);
177 else if (strcmp(ap
[1], "-ge") == 0)
178 expr
= simple_strtol(ap
[0], NULL
, 10) >= simple_strtol(ap
[2], NULL
, 10);
185 expr
= last_expr
|| expr
;
186 else if (last_cmp
== 1)
187 expr
= last_expr
&& expr
;
191 ap
+= adv
; left
-= adv
;
199 debug (": returns %d\n", expr
);
205 test
, CONFIG_SYS_MAXARGS
, 1, do_test
,
206 "minimal test like /bin/sh",
208 " - test functionality\n"
212 do_exit (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[])
218 r
= simple_strtoul(argv
[1], NULL
, 10);
226 " - exit functionality\n"
233 * Use puts() instead of printf() to avoid printf buffer overflow
234 * for long help messages
237 int _do_help (cmd_tbl_t
*cmd_start
, int cmd_items
, cmd_tbl_t
* cmdtp
, int
238 flag
, int argc
, char *argv
[])
243 if (argc
== 1) { /*show list of commands */
244 cmd_tbl_t
*cmd_array
[cmd_items
];
247 /* Make array of commands from .uboot_cmd section */
249 for (i
= 0; i
< cmd_items
; i
++) {
250 cmd_array
[i
] = cmdtp
++;
253 /* Sort command list (trivial bubble sort) */
254 for (i
= cmd_items
- 1; i
> 0; --i
) {
256 for (j
= 0; j
< i
; ++j
) {
257 if (strcmp (cmd_array
[j
]->name
,
258 cmd_array
[j
+ 1]->name
) > 0) {
261 cmd_array
[j
] = cmd_array
[j
+ 1];
262 cmd_array
[j
+ 1] = tmp
;
270 /* print short help (usage) */
271 for (i
= 0; i
< cmd_items
; i
++) {
272 const char *usage
= cmd_array
[i
]->usage
;
274 /* allow user abort */
279 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH
,
280 cmd_array
[i
]->name
, usage
);
285 * command help (long version)
287 for (i
= 1; i
< argc
; ++i
) {
288 if ((cmdtp
= find_cmd_tbl (argv
[i
], cmd_start
, cmd_items
)) != NULL
) {
289 #ifdef CONFIG_SYS_LONGHELP
290 /* found - print (long) help info */
296 puts ("- No help available.\n");
300 #else /* no long help available */
302 printf ("%s - %s\n", cmdtp
->name
, cmdtp
->usage
);
303 #endif /* CONFIG_SYS_LONGHELP */
305 printf ("Unknown command '%s' - try 'help'"
306 " without arguments for list of all"
307 " known commands\n\n", argv
[i
]
315 int do_help (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
317 return _do_help(&__u_boot_cmd_start
,
318 &__u_boot_cmd_end
- &__u_boot_cmd_start
,
319 cmdtp
, flag
, argc
, argv
);
324 help
, CONFIG_SYS_MAXARGS
, 1, do_help
,
327 " - show help information (for 'command')\n"
328 "'help' prints online help for the monitor commands.\n\n"
329 "Without arguments, it prints a short usage message for all commands.\n\n"
330 "To get detailed help information for specific commands you can type\n"
331 "'help' with one or more command names as arguments.\n"
334 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
335 #ifdef CONFIG_SYS_LONGHELP
336 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
337 "?", CONFIG_SYS_MAXARGS
, 1, do_help
,
342 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section
= {
343 "?", CONFIG_SYS_MAXARGS
, 1, do_help
,
346 #endif /* CONFIG_SYS_LONGHELP */
348 /***************************************************************************
349 * find command table entry for a command
351 cmd_tbl_t
*find_cmd_tbl (const char *cmd
, cmd_tbl_t
*table
, int table_len
)
354 cmd_tbl_t
*cmdtp_temp
= table
; /*Init value */
360 * Some commands allow length modifiers (like "cp.b");
361 * compare command name only until first dot.
363 len
= ((p
= strchr(cmd
, '.')) == NULL
) ? strlen (cmd
) : (p
- cmd
);
366 cmdtp
!= table
+ table_len
;
368 if (strncmp (cmd
, cmdtp
->name
, len
) == 0) {
369 if (len
== strlen (cmdtp
->name
))
370 return cmdtp
; /* full match */
372 cmdtp_temp
= cmdtp
; /* abbreviated command ? */
376 if (n_found
== 1) { /* exactly one match */
380 return NULL
; /* not found or ambiguous command */
383 cmd_tbl_t
*find_cmd (const char *cmd
)
385 int len
= &__u_boot_cmd_end
- &__u_boot_cmd_start
;
386 return find_cmd_tbl(cmd
, &__u_boot_cmd_start
, len
);
389 void cmd_usage(cmd_tbl_t
*cmdtp
)
391 printf("Usage:\n%s - %s\n\n", cmdtp
->name
, cmdtp
->usage
);
394 #ifdef CONFIG_AUTO_COMPLETE
396 int var_complete(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
398 static char tmp_buf
[512];
401 space
= last_char
== '\0' || last_char
== ' ' || last_char
== '\t';
403 if (space
&& argc
== 1)
404 return env_complete("", maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
406 if (!space
&& argc
== 2)
407 return env_complete(argv
[1], maxv
, cmdv
, sizeof(tmp_buf
), tmp_buf
);
412 static void install_auto_complete_handler(const char *cmd
,
413 int (*complete
)(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[]))
417 cmdtp
= find_cmd(cmd
);
421 cmdtp
->complete
= complete
;
424 void install_auto_complete(void)
426 install_auto_complete_handler("printenv", var_complete
);
427 install_auto_complete_handler("setenv", var_complete
);
428 #if defined(CONFIG_CMD_RUN)
429 install_auto_complete_handler("run", var_complete
);
433 /*************************************************************************************/
435 static int complete_cmdv(int argc
, char *argv
[], char last_char
, int maxv
, char *cmdv
[])
450 /* output full list of commands */
451 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
452 if (n_found
>= maxv
- 2) {
453 cmdv
[n_found
++] = "...";
456 cmdv
[n_found
++] = cmdtp
->name
;
458 cmdv
[n_found
] = NULL
;
462 /* more than one arg or one but the start of the next */
463 if (argc
> 1 || (last_char
== '\0' || last_char
== ' ' || last_char
== '\t')) {
464 cmdtp
= find_cmd(argv
[0]);
465 if (cmdtp
== NULL
|| cmdtp
->complete
== NULL
) {
469 return (*cmdtp
->complete
)(argc
, argv
, last_char
, maxv
, cmdv
);
474 * Some commands allow length modifiers (like "cp.b");
475 * compare command name only until first dot.
477 p
= strchr(cmd
, '.');
483 /* return the partial matches */
484 for (cmdtp
= &__u_boot_cmd_start
; cmdtp
!= &__u_boot_cmd_end
; cmdtp
++) {
486 clen
= strlen(cmdtp
->name
);
490 if (memcmp(cmd
, cmdtp
->name
, len
) != 0)
494 if (n_found
>= maxv
- 2) {
495 cmdv
[n_found
++] = "...";
499 cmdv
[n_found
++] = cmdtp
->name
;
502 cmdv
[n_found
] = NULL
;
506 static int make_argv(char *s
, int argvsz
, char *argv
[])
510 /* split into argv */
511 while (argc
< argvsz
- 1) {
513 /* skip any white space */
514 while ((*s
== ' ') || (*s
== '\t'))
517 if (*s
== '\0') /* end of s, no more args */
520 argv
[argc
++] = s
; /* begin of argument string */
522 /* find end of string */
523 while (*s
&& (*s
!= ' ') && (*s
!= '\t'))
526 if (*s
== '\0') /* end of s, no more args */
529 *s
++ = '\0'; /* terminate current arg */
536 static void print_argv(const char *banner
, const char *leader
, const char *sep
, int linemax
, char *argv
[])
538 int ll
= leader
!= NULL
? strlen(leader
) : 0;
539 int sl
= sep
!= NULL
? strlen(sep
) : 0;
547 i
= linemax
; /* force leader and newline */
548 while (*argv
!= NULL
) {
549 len
= strlen(*argv
) + sl
;
550 if (i
+ len
>= linemax
) {
563 static int find_common_prefix(char *argv
[])
566 char *anchor
, *s
, *t
;
573 len
= strlen(anchor
);
574 while ((t
= *argv
++) != NULL
) {
576 for (i
= 0; i
< len
; i
++, t
++, s
++) {
585 static char tmp_buf
[CONFIG_SYS_CBSIZE
]; /* copy of console I/O buffer */
587 int cmd_auto_complete(const char *const prompt
, char *buf
, int *np
, int *colp
)
589 int n
= *np
, col
= *colp
;
590 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
594 int i
, j
, k
, len
, seplen
, argc
;
598 if (strcmp(prompt
, CONFIG_SYS_PROMPT
) != 0)
599 return 0; /* not in normal console */
603 last_char
= buf
[cnt
- 1];
607 /* copy to secondary buffer which will be affected */
608 strcpy(tmp_buf
, buf
);
610 /* separate into argv */
611 argc
= make_argv(tmp_buf
, sizeof(argv
)/sizeof(argv
[0]), argv
);
613 /* do the completion and return the possible completions */
614 i
= complete_cmdv(argc
, argv
, last_char
, sizeof(cmdv
)/sizeof(cmdv
[0]), cmdv
);
616 /* no match; bell and out */
618 if (argc
> 1) /* allow tab for non command */
628 if (i
== 1) { /* one match; perfect */
629 k
= strlen(argv
[argc
- 1]);
634 } else if (i
> 1 && (j
= find_common_prefix(cmdv
)) != 0) { /* more */
635 k
= strlen(argv
[argc
- 1]);
645 /* make sure it fits */
646 if (n
+ k
>= CONFIG_SYS_CBSIZE
- 2) {
652 for (i
= 0; i
< len
; i
++)
655 for (i
= 0; i
< seplen
; i
++)
666 print_argv(NULL
, " ", " ", 78, cmdv
);
677 int cmd_get_data_size(char* arg
, int default_size
)
679 /* Check for a size specification .b, .w or .l.
681 int len
= strlen(arg
);
682 if (len
> 2 && arg
[len
-2] == '.') {