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",
45 #if (CONFIG_COMMANDS & CFG_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, CFG_MAXARGS, 1, do_echo,
74 "echo - echo args to console\n",
76 " - echo args to console; \\c suppresses newline\n"
79 #endif /* CFG_CMD_ECHO */
81 #ifdef CFG_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;
200 printf(": returns %d\n", expr);
207 test, CFG_MAXARGS, 1, do_test,
208 "test - minimal test like /bin/sh\n",
210 " - test functionality\n"
214 do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
220 r = simple_strtoul(argv[1], NULL, 10);
227 "exit - exit script\n",
228 " - exit functionality\n"
235 * Use puts() instead of printf() to avoid printf buffer overflow
236 * for long help messages
238 int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
243 if (argc == 1) { /*show list of commands */
245 int cmd_items = &__u_boot_cmd_end -
246 &__u_boot_cmd_start; /* pointer arith! */
247 cmd_tbl_t *cmd_array[cmd_items];
250 /* Make array of commands from .uboot_cmd section */
251 cmdtp = &__u_boot_cmd_start;
252 for (i = 0; i < cmd_items; i++) {
253 cmd_array[i] = cmdtp++;
256 /* Sort command list (trivial bubble sort) */
257 for (i = cmd_items - 1; i > 0; --i) {
259 for (j = 0; j < i; ++j) {
260 if (strcmp (cmd_array[j]->name,
261 cmd_array[j + 1]->name) > 0) {
264 cmd_array[j] = cmd_array[j + 1];
265 cmd_array[j + 1] = tmp;
273 /* print short help (usage) */
274 for (i = 0; i < cmd_items; i++) {
275 const char *usage = cmd_array[i]->usage;
277 /* allow user abort */
287 * command help (long version)
289 for (i = 1; i < argc; ++i) {
290 if ((cmdtp = find_cmd (argv[i])) != NULL) {
292 /* found - print (long) help info */
298 puts ("- No help available.\n");
302 #else /* no long help available */
305 #endif /* CFG_LONGHELP */
307 printf ("Unknown command '%s' - try 'help'"
308 " without arguments for list of all"
309 " known commands\n\n", argv[i]
319 help, CFG_MAXARGS, 1, do_help,
320 "help - print online help\n",
322 " - show help information (for 'command')\n"
323 "'help' prints online help for the monitor commands.\n\n"
324 "Without arguments, it prints a short usage message for all commands.\n\n"
325 "To get detailed help information for specific commands you can type\n"
326 "'help' with one or more command names as arguments.\n"
329 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
331 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
332 "?", CFG_MAXARGS, 1, do_help,
333 "? - alias for 'help'\n",
337 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
338 "?", CFG_MAXARGS, 1, do_help,
339 "? - alias for 'help'\n"
341 #endif /* CFG_LONGHELP */
343 /***************************************************************************
344 * find command table entry for a command
346 cmd_tbl_t *find_cmd (const char *cmd)
349 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
355 * Some commands allow length modifiers (like "cp.b");
356 * compare command name only until first dot.
358 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
360 for (cmdtp = &__u_boot_cmd_start;
361 cmdtp != &__u_boot_cmd_end;
363 if (strncmp (cmd, cmdtp->name, len) == 0) {
364 if (len == strlen (cmdtp->name))
365 return cmdtp; /* full match */
367 cmdtp_temp = cmdtp; /* abbreviated command ? */
371 if (n_found == 1) { /* exactly one match */
375 return NULL; /* not found or ambiguous command */
378 #ifdef CONFIG_AUTO_COMPLETE
380 int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
382 static char tmp_buf[512];
385 space = last_char == '\0' || last_char == ' ' || last_char == '\t';
387 if (space && argc == 1)
388 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
390 if (!space && argc == 2)
391 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
396 static void install_auto_complete_handler(const char *cmd,
397 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
401 cmdtp = find_cmd(cmd);
405 cmdtp->complete = complete;
408 void install_auto_complete(void)
410 install_auto_complete_handler("printenv", var_complete);
411 install_auto_complete_handler("setenv", var_complete);
412 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
413 install_auto_complete_handler("run", var_complete);
417 /*************************************************************************************/
419 static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
434 /* output full list of commands */
435 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
436 if (n_found >= maxv - 2) {
437 cmdv[n_found++] = "...";
440 cmdv[n_found++] = cmdtp->name;
442 cmdv[n_found] = NULL;
446 /* more than one arg or one but the start of the next */
447 if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
448 cmdtp = find_cmd(argv[0]);
449 if (cmdtp == NULL || cmdtp->complete == NULL) {
453 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
458 * Some commands allow length modifiers (like "cp.b");
459 * compare command name only until first dot.
461 p = strchr(cmd, '.');
467 /* return the partial matches */
468 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
470 clen = strlen(cmdtp->name);
474 if (memcmp(cmd, cmdtp->name, len) != 0)
478 if (n_found >= maxv - 2) {
479 cmdv[n_found++] = "...";
483 cmdv[n_found++] = cmdtp->name;
486 cmdv[n_found] = NULL;
490 static int make_argv(char *s, int argvsz, char *argv[])
494 /* split into argv */
495 while (argc < argvsz - 1) {
497 /* skip any white space */
498 while ((*s == ' ') || (*s == '\t'))
501 if (*s == '\0') /* end of s, no more args */
504 argv[argc++] = s; /* begin of argument string */
506 /* find end of string */
507 while (*s && (*s != ' ') && (*s != '\t'))
510 if (*s == '\0') /* end of s, no more args */
513 *s++ = '\0'; /* terminate current arg */
520 static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
522 int ll = leader != NULL ? strlen(leader) : 0;
523 int sl = sep != NULL ? strlen(sep) : 0;
531 i = linemax; /* force leader and newline */
532 while (*argv != NULL) {
533 len = strlen(*argv) + sl;
534 if (i + len >= linemax) {
547 static int find_common_prefix(char *argv[])
550 char *anchor, *s, *t;
557 len = strlen(anchor);
558 while ((t = *argv++) != NULL) {
560 for (i = 0; i < len; i++, t++, s++) {
569 static char tmp_buf[CFG_CBSIZE]; /* copy of console I/O buffer */
571 int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
573 int n = *np, col = *colp;
574 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
578 int i, j, k, len, seplen, argc;
582 if (strcmp(prompt, CFG_PROMPT) != 0)
583 return 0; /* not in normal console */
587 last_char = buf[cnt - 1];
591 /* copy to secondary buffer which will be affected */
592 strcpy(tmp_buf, buf);
594 /* separate into argv */
595 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
597 /* do the completion and return the possible completions */
598 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
600 /* no match; bell and out */
602 if (argc > 1) /* allow tab for non command */
612 if (i == 1) { /* one match; perfect */
613 k = strlen(argv[argc - 1]);
618 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
619 k = strlen(argv[argc - 1]);
629 /* make sure it fits */
630 if (n + k >= CFG_CBSIZE - 2) {
636 for (i = 0; i < len; i++)
639 for (i = 0; i < seplen; i++)
650 print_argv(NULL, " ", " ", 78, cmdv);