Merge branch 'support_for_ar9341'
[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  * Use puts() instead of printf() to avoid printf buffer overflow
72  * for long help messages
73  */
74 int do_help(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){
75         int i;
76         int rcode = 0;
77
78         if(argc == 1){ /*show list of commands */
79
80                 int cmd_items = &__u_boot_cmd_end - &__u_boot_cmd_start; /* pointer arith! */
81                 cmd_tbl_t *cmd_array[cmd_items];
82                 int i, j, swaps, max_len = 0;
83
84                 /* Make array of commands from .uboot_cmd section */
85                 cmdtp = &__u_boot_cmd_start;
86
87                 for(i = 0; i < cmd_items; i++){
88                         cmd_array[i] = cmdtp++;
89                 }
90
91                 /* Sort command list (trivial bubble sort) */
92                 for(i = cmd_items - 1; i > 0; --i){
93                         swaps = 0;
94                         for(j = 0; j < i; ++j){
95
96                                 const char *name = cmd_array[j]->name;
97
98                                 if(strlen(name) >= max_len){
99                                         max_len = strlen(name);
100                                 }
101
102                                 if(strcmp(cmd_array[j]->name, cmd_array[j + 1]->name) > 0){
103                                         cmd_tbl_t *tmp;
104                                         tmp = cmd_array[j];
105                                         cmd_array[j] = cmd_array[j + 1];
106                                         cmd_array[j + 1] = tmp;
107                                         ++swaps;
108                                 }
109                         }
110
111                         if(!swaps){
112                                 break;
113                         }
114                 }
115
116                 max_len++;
117
118                 /* print short help (usage) */
119                 for(i = 0; i < cmd_items; i++){
120                         const char *usage = cmd_array[i]->usage;
121                         const char *name = cmd_array[i]->name;
122
123                         /* allow user abort */
124                         if(ctrlc()){
125                                 return(1);
126                         }
127
128                         if(usage == NULL){
129                                 continue;
130                         }
131                         /* print aligned command name and usage */
132                         printf("%-*s - ", max_len, name);
133                         puts(usage);
134                 }
135
136                 printf("\n");
137
138                 return(0);
139         }
140         /*
141          * command help (long version)
142          */
143         for(i = 1; i < argc; ++i){
144                 if((cmdtp = find_cmd(argv[i])) != NULL){
145 #ifdef  CFG_LONGHELP
146                         /* found - print (long) help info */
147                         puts(cmdtp->name);
148                         putc(' ');
149
150                         if(cmdtp->help){
151                                 puts(cmdtp->help);
152                         } else {
153                                 puts("- there is no help for this command\n");
154                                 rcode = 1;
155                         }
156
157                         putc('\n');
158 #else   /* no long help available */
159                         if(cmdtp->usage){
160                                 puts(cmdtp->usage);
161                         }
162 #endif  /* CFG_LONGHELP */
163                 } else {
164                         printf("Unknown command '%s' - try 'help' without arguments\n\n", argv[i]);
165                         rcode = 1;
166                 }
167         }
168
169         return(rcode);
170 }
171
172 U_BOOT_CMD(help, CFG_MAXARGS, 1, do_help, "print embedded help\n",
173                         "[command ...]\n"
174                         "\t- show help information (for 'command')\n"
175                         "\twithout arguments, it prints a short usage message for available commands.\n");
176
177 /* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
178 #ifdef CFG_LONGHELP
179 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {"?", CFG_MAXARGS, 1, do_help, "alias for 'help'\n", NULL};
180 #else
181 cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {"?", CFG_MAXARGS, 1, do_help, "alias for 'help'\n"};
182 #endif /* CFG_LONGHELP */
183
184 /***************************************************************************
185  * find command table entry for a command
186  */
187 cmd_tbl_t *find_cmd(const char *cmd){
188         cmd_tbl_t *cmdtp;
189         cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
190         const char *p;
191         int len;
192         int n_found = 0;
193
194         /*
195          * Some commands allow length modifiers (like "cp.b");
196          * compare command name only until first dot.
197          */
198         len = ((p = strchr(cmd, '.')) == NULL) ? strlen(cmd) : (p - cmd);
199
200         for(cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++){
201                 if(strncmp(cmd, cmdtp->name, len) == 0){
202                         if(len == strlen(cmdtp->name)){
203                                 return(cmdtp); /* full match */
204                         }
205
206                         cmdtp_temp = cmdtp; /* abbreviated command ? */
207                         n_found++;
208                 }
209         }
210         if(n_found == 1){ /* exactly one match */
211                 return(cmdtp_temp);
212         }
213
214         return(NULL); /* not found or ambiguous command */
215 }