treewide: drop executable file attrib for non-executable files
[oweals/u-boot_mod.git] / u-boot / 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 do_version(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
32         extern char version_string[];
33         printf("%s\n\n", version_string);
34
35         return(0);
36 }
37
38 U_BOOT_CMD(version, 1, 1, do_version, "print U-Boot version\n", NULL);
39
40 #if (CONFIG_COMMANDS & CFG_CMD_ECHO)
41 int do_echo(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
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                 }
50
51                 while((c = *p++) != '\0'){
52                         if(c == '\\' && *p == 'c'){
53                                 putnl = 0;
54                                 p++;
55                         } else {
56                                 putc(c);
57                         }
58                 }
59         }
60
61         if(putnl){
62                 putc('\n');
63         }
64
65         return(0);
66 }
67 U_BOOT_CMD(echo, CFG_MAXARGS, 1, do_echo, "echo args to console\n", "[args..]\n" "\t- echo args to console; \\c suppresses newline\n");
68 #endif  /*  CFG_CMD_ECHO */
69
70
71 #ifdef CFG_HUSH_PARSER
72 int do_test(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
73         char **ap;
74         int left, adv, expr, last_expr, neg, last_cmp;
75
76         /* args? */
77         if (argc < 3){
78                 return(1);
79         }
80
81 #if 0
82         printf("test:");
83         left = 1;
84         while (argv[left]){
85                 printf(" %s", argv[left++]);
86         }
87 #endif
88
89         last_expr = 0;
90         left = argc - 1; ap = argv + 1;
91
92         if(left > 0 && strcmp(ap[0], "!") == 0){
93                 neg = 1;
94                 ap++;
95                 left--;
96         } else {
97                 neg = 0;
98         }
99
100         expr = -1;
101         last_cmp = -1;
102         last_expr = -1;
103
104         while(left > 0){
105
106                 if(strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0){
107                         adv = 1;
108                 } else if(strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0){
109                         adv = 2;
110                 } else {
111                         adv = 3;
112                 }
113
114                 if(left < adv){
115                         expr = 1;
116                         break;
117                 }
118
119                 if(adv == 1){
120                         if (strcmp(ap[0], "-o") == 0){
121                                 last_expr = expr;
122                                 last_cmp = 0;
123                         } else if(strcmp(ap[0], "-a") == 0){
124                                 last_expr = expr;
125                                 last_cmp = 1;
126                         } else {
127                                 expr = 1;
128                                 break;
129                         }
130                 }
131
132                 if(adv == 2){
133                         if (strcmp(ap[0], "-z") == 0){
134                                 expr = strlen(ap[1]) == 0 ? 1 : 0;
135                         } else if(strcmp(ap[0], "-n") == 0){
136                                 expr = strlen(ap[1]) == 0 ? 0 : 1;
137                         } else {
138                                 expr = 1;
139                                 break;
140                         }
141
142                         if(last_cmp == 0){
143                                 expr = last_expr || expr;
144                         } else if(last_cmp == 1){
145                                 expr = last_expr && expr;
146                         }
147                         last_cmp = -1;
148                 }
149
150                 if(adv == 3){
151                         if(strcmp(ap[1], "=") == 0){
152                                 expr = strcmp(ap[0], ap[2]) == 0;
153                         } else if(strcmp(ap[1], "!=") == 0){
154                                 expr = strcmp(ap[0], ap[2]) != 0;
155                         } else 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], "-eq") == 0){
160                                 expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
161                         } else if(strcmp(ap[1], "-ne") == 0){
162                                 expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
163                         } else if(strcmp(ap[1], "-lt") == 0){
164                                 expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
165                         } else if(strcmp(ap[1], "-le") == 0){
166                                 expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
167                         } else if(strcmp(ap[1], "-gt") == 0){
168                                 expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
169                         } else if(strcmp(ap[1], "-ge") == 0){
170                                 expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
171                         } else {
172                                 expr = 1;
173                                 break;
174                         }
175
176                         if(last_cmp == 0){
177                                 expr = last_expr || expr;
178                         } else if(last_cmp == 1){
179                                 expr = last_expr && expr;
180                         }
181                         last_cmp = -1;
182                 }
183
184                 ap += adv; left -= adv;
185         }
186
187         if(neg){
188                 expr = !expr;
189         }
190
191         expr = !expr;
192
193 #if 0
194         printf(": returns %d\n", expr);
195 #endif
196
197         return(expr);
198 }
199
200 U_BOOT_CMD(test, CFG_MAXARGS, 1, do_test, "minimal test like /bin/sh\n", "[args..]\n"
201 "\t- test functionality\n");
202
203 int do_exit(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]){
204         int r = 0;
205
206         if(argc > 1){
207                 r = simple_strtoul(argv[1], NULL, 10);
208         }
209
210         return(-r - 2);
211 }
212
213 U_BOOT_CMD(exit, 2, 1, do_exit, "exit script\n", "\n\t- exit functionality\n");
214 #endif /* CFG_HUSH_PARSER */
215
216 /*
217  * Use puts() instead of printf() to avoid printf buffer overflow
218  * for long help messages
219  */
220 int do_help(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){
221         int i;
222         int rcode = 0;
223
224         if(argc == 1){ /*show list of commands */
225
226                 int cmd_items = &__u_boot_cmd_end - &__u_boot_cmd_start; /* pointer arith! */
227                 cmd_tbl_t *cmd_array[cmd_items];
228                 int i, j, swaps, max_len = 0;
229
230                 /* Make array of commands from .uboot_cmd section */
231                 cmdtp = &__u_boot_cmd_start;
232
233                 for(i = 0; i < cmd_items; i++){
234                         cmd_array[i] = cmdtp++;
235                 }
236
237                 /* Sort command list (trivial bubble sort) */
238                 for(i = cmd_items - 1; i > 0; --i){
239                         swaps = 0;
240                         for(j = 0; j < i; ++j){
241
242                                 const char *name = cmd_array[j]->name;
243
244                                 if(strlen(name) >= max_len){
245                                         max_len = strlen(name);
246                                 }
247
248                                 if(strcmp(cmd_array[j]->name, cmd_array[j + 1]->name) > 0){
249                                         cmd_tbl_t *tmp;
250                                         tmp = cmd_array[j];
251                                         cmd_array[j] = cmd_array[j + 1];
252                                         cmd_array[j + 1] = tmp;
253                                         ++swaps;
254                                 }
255                         }
256
257                         if(!swaps){
258                                 break;
259                         }
260                 }
261
262                 /* print short help (usage) */
263                 for(i = 0; i < cmd_items; i++){
264                         const char *usage = cmd_array[i]->usage;
265                         const char *name = cmd_array[i]->name;
266
267                         /* allow user abort */
268                         if(ctrlc()){
269                                 return(1);
270                         }
271
272                         if(usage == NULL){
273                                 continue;
274                         }
275                         /* print aligned command name and usage */
276                         printf("%-*s - ", max_len, name);
277                         puts(usage);
278                 }
279
280                 printf("\n");
281
282                 return(0);
283         }
284         /*
285          * command help (long version)
286          */
287         for(i = 1; i < argc; ++i){
288                 if((cmdtp = find_cmd(argv[i])) != NULL){
289 #ifdef  CFG_LONGHELP
290                         /* found - print (long) help info */
291                         puts(cmdtp->name);
292                         putc(' ');
293
294                         if(cmdtp->help){
295                                 puts(cmdtp->help);
296                         } else {
297                                 puts("- there is no help for this command\n");
298                                 rcode = 1;
299                         }
300
301                         putc('\n');
302 #else   /* no long help available */
303                         if(cmdtp->usage){
304                                 puts(cmdtp->usage);
305                         }
306 #endif  /* CFG_LONGHELP */
307                 } else {
308                         printf("Unknown command '%s' - try 'help' without arguments\n\n", argv[i]);
309                         rcode = 1;
310                 }
311         }
312
313         return(rcode);
314 }
315
316 U_BOOT_CMD(help, CFG_MAXARGS, 1, do_help, "print embedded help\n",
317                         "[command ...]\n"
318                         "\t- show help information (for 'command')\n"
319                         "\twithout arguments, it prints a short usage message for available commands.\n");
320
321 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
322 #ifdef CFG_LONGHELP
323 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {"?", CFG_MAXARGS, 1, do_help, "alias for 'help'\n", NULL};
324 #else
325 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {"?", CFG_MAXARGS, 1, do_help, "alias for 'help'\n"};
326 #endif /* CFG_LONGHELP */
327
328 /***************************************************************************
329  * find command table entry for a command
330  */
331 cmd_tbl_t *find_cmd(const char *cmd){
332         cmd_tbl_t *cmdtp;
333         cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
334         const char *p;
335         int len;
336         int n_found = 0;
337
338         /*
339          * Some commands allow length modifiers (like "cp.b");
340          * compare command name only until first dot.
341          */
342         len = ((p = strchr(cmd, '.')) == NULL) ? strlen(cmd) : (p - cmd);
343
344         for(cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++){
345                 if(strncmp(cmd, cmdtp->name, len) == 0){
346                         if(len == strlen(cmdtp->name)){
347                                 return(cmdtp); /* full match */
348                         }
349
350                         cmdtp_temp = cmdtp; /* abbreviated command ? */
351                         n_found++;
352                 }
353         }
354         if(n_found == 1){ /* exactly one match */
355                 return(cmdtp_temp);
356         }
357
358         return(NULL); /* not found or ambiguous command */
359 }