]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/command.c
Rewrite command lookup and help command (fix problems with bubble
[karo-tx-uboot.git] / common / command.c
1 /*
2  * (C) Copyright 2000-2003
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
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.
12  *
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.
17  *
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
22  */
23
24 /*
25  *  Command Processor Table
26  */
27
28 #include <common.h>
29 #include <command.h>
30
31 int
32 do_version (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
33 {
34         extern char version_string[];
35         printf ("\n%s\n", version_string);
36         return 0;
37 }
38
39 int
40 do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
41 {
42         int i, putnl = 1;
43
44         for (i = 1; i < argc; i++) {
45                 char *p = argv[i], c;
46
47                 if (i > 1)
48                         putc(' ');
49                 while ((c = *p++) != '\0') {
50                         if (c == '\\' && *p == 'c') {
51                                 putnl = 0;
52                                 p++;
53                         } else {
54                                 putc(c);
55                         }
56                 }
57         }
58
59         if (putnl)
60                 putc('\n');
61         return 0;
62 }
63
64 /*
65  * Use puts() instead of printf() to avoid printf buffer overflow
66  * for long help messages
67  */
68 int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
69 {
70         int i;
71         int rcode = 0;
72
73         if (argc == 1) {        /*show list of commands */
74
75                 int cmd_items = &__u_boot_cmd_end -
76                                 &__u_boot_cmd_start;    /* pointer arith! */
77                 cmd_tbl_t *cmd_array[cmd_items];
78                 int i, j, swaps;
79
80                 /* Make array of commands from .uboot_cmd section */
81                 cmdtp = &__u_boot_cmd_start;
82                 for (i = 0; i < cmd_items; i++) {
83                         cmd_array[i] = cmdtp++;
84                 }
85
86                 /* Sort command list (trivial bubble sort) */
87                 for (i = cmd_items - 1; i > 0; --i) {
88                         swaps = 0;
89                         for (j = 0; j < i; ++j) {
90                                 if (strcmp (cmd_array[j]->name,
91                                             cmd_array[j + 1]->name) > 0) {
92                                         cmd_tbl_t *tmp;
93                                         tmp = cmd_array[j];
94                                         cmd_array[j] = cmd_array[j + 1];
95                                         cmd_array[j + 1] = tmp;
96                                         ++swaps;
97                                 }
98                         }
99                         if (!swaps)
100                                 break;
101                 }
102
103                 /* print short help (usage) */
104                 for (i = 0; i < cmd_items; i++) {
105                         const char *usage = cmd_array[i]->usage;
106
107                         /* allow user abort */
108                         if (ctrlc ())
109                                 return 1;
110                         if (usage == NULL)
111                                 continue;
112                         puts (usage);
113                 }
114                 return 0;
115         }
116         /*
117          * command help (long version)
118          */
119         for (i = 1; i < argc; ++i) {
120                 if ((cmdtp = find_cmd (argv[i])) != NULL) {
121 #ifdef  CFG_LONGHELP
122                         /* found - print (long) help info */
123                         puts (cmdtp->name);
124                         putc (' ');
125                         if (cmdtp->help) {
126                                 puts (cmdtp->help);
127                         } else {
128                                 puts ("- No help available.\n");
129                                 rcode = 1;
130                         }
131                         putc ('\n');
132 #else   /* no long help available */
133                         if (cmdtp->usage)
134                                 puts (cmdtp->usage);
135 #endif  /* CFG_LONGHELP */
136                 } else {
137                         printf ("Unknown command '%s' - try 'help'"
138                                 " without arguments for list of all"
139                                 " known commands\n\n", argv[i]
140                                         );
141                         rcode = 1;
142                 }
143         }
144         return rcode;
145 }
146
147
148 cmd_tbl_t U_BOOT_CMD(HELP) = MK_CMD_ENTRY(
149         "help", CFG_MAXARGS,    1,      do_help,
150         "help    - print online help\n",
151         "[command ...]\n"
152         "    - show help information (for 'command')\n"
153         "'help' prints online help for the monitor commands.\n\n"
154         "Without arguments, it prints a short usage message for all commands.\n\n"
155         "To get detailed help information for specific commands you can type\n"
156   "'help' with one or more command names as arguments.\n"
157 );
158
159 cmd_tbl_t U_BOOT_CMD(QUES) = MK_CMD_ENTRY(
160         "?",    CFG_MAXARGS,    1,      do_help,
161         "?       - alias for 'help'\n",
162         NULL
163 );
164
165 cmd_tbl_t U_BOOT_CMD(VERS) = MK_CMD_ENTRY(
166         "version",      1,              1,      do_version,
167         "version - print monitor version\n",
168         NULL
169 );
170
171 cmd_tbl_t U_BOOT_CMD(ECHO) = MK_CMD_ENTRY(
172         "echo", CFG_MAXARGS,    1,      do_echo,
173         "echo    - echo args to console\n",
174         "[args..]\n"
175         "    - echo args to console; \\c suppresses newline\n"
176 );
177
178 /***************************************************************************
179  * find command table entry for a command
180  */
181 cmd_tbl_t *find_cmd (const char *cmd)
182 {
183         cmd_tbl_t *cmdtp;
184         cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start;    /*Init value */
185         const char *p;
186         int len;
187         int n_found = 0;
188
189         /*
190          * Some commands allow length modifiers (like "cp.b");
191          * compare command name only until first dot.
192          */
193         len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
194
195         for (cmdtp = &__u_boot_cmd_start;
196              cmdtp != &__u_boot_cmd_end;
197              cmdtp++) {
198                 if (strncmp (cmd, cmdtp->name, len) == 0) {
199                         if (len == strlen (cmdtp->name))
200                                 return cmdtp;   /* full match */
201
202                         cmdtp_temp = cmdtp;     /* abbreviated command ? */
203                         n_found++;
204                 }
205         }
206         if (n_found == 1) {                     /* exactly one match */
207                 return cmdtp_temp;
208         }
209
210         return NULL;    /* not found or ambiguous command */
211 }