add SDHC support in mmc driver
[u-boot-openmoko/mini2440.git] / common / command.c
blobaf2f8cbf7b8c163e74dc8530d89c50f67dc82665
1 /*
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
6 * project.
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,
21 * MA 02111-1307 USA
25 * Command Processor Table
28 #include <common.h>
29 #include <command.h>
31 int
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);
36 return 0;
39 U_BOOT_CMD(
40 version, 1, 1, do_version,
41 "version - print monitor version\n",
42 NULL
45 #if defined(CONFIG_CMD_ECHO)
47 int
48 do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
50 int i, putnl = 1;
52 for (i = 1; i < argc; i++) {
53 char *p = argv[i], c;
55 if (i > 1)
56 putc(' ');
57 while ((c = *p++) != '\0') {
58 if (c == '\\' && *p == 'c') {
59 putnl = 0;
60 p++;
61 } else {
62 putc(c);
67 if (putnl)
68 putc('\n');
69 return 0;
72 U_BOOT_CMD(
73 echo, CFG_MAXARGS, 1, do_echo,
74 "echo - echo args to console\n",
75 "[args..]\n"
76 " - echo args to console; \\c suppresses newline\n"
79 #endif
81 #ifdef CFG_HUSH_PARSER
83 int
84 do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
86 char **ap;
87 int left, adv, expr, last_expr, neg, last_cmp;
89 /* args? */
90 if (argc < 3)
91 return 1;
93 #if 0
95 printf("test:");
96 left = 1;
97 while (argv[left])
98 printf(" %s", argv[left++]);
100 #endif
102 last_expr = 0;
103 left = argc - 1; ap = argv + 1;
104 if (left > 0 && strcmp(ap[0], "!") == 0) {
105 neg = 1;
106 ap++;
107 left--;
108 } else
109 neg = 0;
111 expr = -1;
112 last_cmp = -1;
113 last_expr = -1;
114 while (left > 0) {
116 if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
117 adv = 1;
118 else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
119 adv = 2;
120 else
121 adv = 3;
123 if (left < adv) {
124 expr = 1;
125 break;
128 if (adv == 1) {
129 if (strcmp(ap[0], "-o") == 0) {
130 last_expr = expr;
131 last_cmp = 0;
132 } else if (strcmp(ap[0], "-a") == 0) {
133 last_expr = expr;
134 last_cmp = 1;
135 } else {
136 expr = 1;
137 break;
141 if (adv == 2) {
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;
146 else {
147 expr = 1;
148 break;
151 if (last_cmp == 0)
152 expr = last_expr || expr;
153 else if (last_cmp == 1)
154 expr = last_expr && expr;
155 last_cmp = -1;
158 if (adv == 3) {
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);
179 else {
180 expr = 1;
181 break;
184 if (last_cmp == 0)
185 expr = last_expr || expr;
186 else if (last_cmp == 1)
187 expr = last_expr && expr;
188 last_cmp = -1;
191 ap += adv; left -= adv;
194 if (neg)
195 expr = !expr;
197 expr = !expr;
199 #if 0
200 printf(": returns %d\n", expr);
201 #endif
203 return expr;
206 U_BOOT_CMD(
207 test, CFG_MAXARGS, 1, do_test,
208 "test - minimal test like /bin/sh\n",
209 "[args..]\n"
210 " - test functionality\n"
214 do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
216 int r;
218 r = 0;
219 if (argc > 1)
220 r = simple_strtoul(argv[1], NULL, 10);
222 return -r - 2;
225 U_BOOT_CMD(
226 exit, 2, 1, do_exit,
227 "exit - exit script\n",
228 " - exit functionality\n"
232 #endif
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[])
240 int i;
241 int rcode = 0;
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];
248 int i, j, swaps;
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) {
258 swaps = 0;
259 for (j = 0; j < i; ++j) {
260 if (strcmp (cmd_array[j]->name,
261 cmd_array[j + 1]->name) > 0) {
262 cmd_tbl_t *tmp;
263 tmp = cmd_array[j];
264 cmd_array[j] = cmd_array[j + 1];
265 cmd_array[j + 1] = tmp;
266 ++swaps;
269 if (!swaps)
270 break;
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 */
278 if (ctrlc ())
279 return 1;
280 if (usage == NULL)
281 continue;
282 puts (usage);
284 return 0;
287 * command help (long version)
289 for (i = 1; i < argc; ++i) {
290 if ((cmdtp = find_cmd (argv[i])) != NULL) {
291 #ifdef CFG_LONGHELP
292 /* found - print (long) help info */
293 puts (cmdtp->name);
294 putc (' ');
295 if (cmdtp->help) {
296 puts (cmdtp->help);
297 } else {
298 puts ("- No help available.\n");
299 rcode = 1;
301 putc ('\n');
302 #else /* no long help available */
303 if (cmdtp->usage)
304 puts (cmdtp->usage);
305 #endif /* CFG_LONGHELP */
306 } else {
307 printf ("Unknown command '%s' - try 'help'"
308 " without arguments for list of all"
309 " known commands\n\n", argv[i]
311 rcode = 1;
314 return rcode;
318 U_BOOT_CMD(
319 help, CFG_MAXARGS, 1, do_help,
320 "help - print online help\n",
321 "[command ...]\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 */
330 #ifdef CFG_LONGHELP
331 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
332 "?", CFG_MAXARGS, 1, do_help,
333 "? - alias for 'help'\n",
334 NULL
336 #else
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)
348 cmd_tbl_t *cmdtp;
349 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
350 const char *p;
351 int len;
352 int n_found = 0;
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;
362 cmdtp++) {
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 ? */
368 n_found++;
371 if (n_found == 1) { /* exactly one match */
372 return cmdtp_temp;
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];
383 int space;
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);
393 return 0;
396 static void install_auto_complete_handler(const char *cmd,
397 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
399 cmd_tbl_t *cmdtp;
401 cmdtp = find_cmd(cmd);
402 if (cmdtp == NULL)
403 return;
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 defined(CONFIG_CMD_RUN)
413 install_auto_complete_handler("run", var_complete);
414 #endif
417 /*************************************************************************************/
419 static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
421 cmd_tbl_t *cmdtp;
422 const char *p;
423 int len, clen;
424 int n_found = 0;
425 const char *cmd;
427 /* sanity? */
428 if (maxv < 2)
429 return -2;
431 cmdv[0] = NULL;
433 if (argc == 0) {
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++] = "...";
438 break;
440 cmdv[n_found++] = cmdtp->name;
442 cmdv[n_found] = NULL;
443 return n_found;
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) {
450 cmdv[0] = NULL;
451 return 0;
453 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
456 cmd = argv[0];
458 * Some commands allow length modifiers (like "cp.b");
459 * compare command name only until first dot.
461 p = strchr(cmd, '.');
462 if (p == NULL)
463 len = strlen(cmd);
464 else
465 len = p - cmd;
467 /* return the partial matches */
468 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
470 clen = strlen(cmdtp->name);
471 if (clen < len)
472 continue;
474 if (memcmp(cmd, cmdtp->name, len) != 0)
475 continue;
477 /* too many! */
478 if (n_found >= maxv - 2) {
479 cmdv[n_found++] = "...";
480 break;
483 cmdv[n_found++] = cmdtp->name;
486 cmdv[n_found] = NULL;
487 return n_found;
490 static int make_argv(char *s, int argvsz, char *argv[])
492 int argc = 0;
494 /* split into argv */
495 while (argc < argvsz - 1) {
497 /* skip any white space */
498 while ((*s == ' ') || (*s == '\t'))
499 ++s;
501 if (*s == '\0') /* end of s, no more args */
502 break;
504 argv[argc++] = s; /* begin of argument string */
506 /* find end of string */
507 while (*s && (*s != ' ') && (*s != '\t'))
508 ++s;
510 if (*s == '\0') /* end of s, no more args */
511 break;
513 *s++ = '\0'; /* terminate current arg */
515 argv[argc] = NULL;
517 return argc;
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;
524 int len, i;
526 if (banner) {
527 puts("\n");
528 puts(banner);
531 i = linemax; /* force leader and newline */
532 while (*argv != NULL) {
533 len = strlen(*argv) + sl;
534 if (i + len >= linemax) {
535 puts("\n");
536 if (leader)
537 puts(leader);
538 i = ll - sl;
539 } else if (sep)
540 puts(sep);
541 puts(*argv++);
542 i += len;
544 printf("\n");
547 static int find_common_prefix(char *argv[])
549 int i, len;
550 char *anchor, *s, *t;
552 if (*argv == NULL)
553 return 0;
555 /* begin with max */
556 anchor = *argv++;
557 len = strlen(anchor);
558 while ((t = *argv++) != NULL) {
559 s = anchor;
560 for (i = 0; i < len; i++, t++, s++) {
561 if (*t != *s)
562 break;
564 len = s - anchor;
566 return len;
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 */
575 char *cmdv[20];
576 char *s, *t;
577 const char *sep;
578 int i, j, k, len, seplen, argc;
579 int cnt;
580 char last_char;
582 if (strcmp(prompt, CFG_PROMPT) != 0)
583 return 0; /* not in normal console */
585 cnt = strlen(buf);
586 if (cnt >= 1)
587 last_char = buf[cnt - 1];
588 else
589 last_char = '\0';
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 */
601 if (i == 0) {
602 if (argc > 1) /* allow tab for non command */
603 return 0;
604 putc('\a');
605 return 1;
608 s = NULL;
609 len = 0;
610 sep = NULL;
611 seplen = 0;
612 if (i == 1) { /* one match; perfect */
613 k = strlen(argv[argc - 1]);
614 s = cmdv[0] + k;
615 len = strlen(s);
616 sep = " ";
617 seplen = 1;
618 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
619 k = strlen(argv[argc - 1]);
620 j -= k;
621 if (j > 0) {
622 s = cmdv[0] + k;
623 len = j;
627 if (s != NULL) {
628 k = len + seplen;
629 /* make sure it fits */
630 if (n + k >= CFG_CBSIZE - 2) {
631 putc('\a');
632 return 1;
635 t = buf + cnt;
636 for (i = 0; i < len; i++)
637 *t++ = *s++;
638 if (sep != NULL)
639 for (i = 0; i < seplen; i++)
640 *t++ = sep[i];
641 *t = '\0';
642 n += k;
643 col += k;
644 puts(t - k);
645 if (sep == NULL)
646 putc('\a');
647 *np = n;
648 *colp = col;
649 } else {
650 print_argv(NULL, " ", " ", 78, cmdv);
652 puts(prompt);
653 puts(buf);
655 return 1;
658 #endif