blob: 0c66b7a1d9361e994fd0b7cff97c180d47f600ec [file] [log] [blame]
wdenk5dfa25f2002-10-19 19:42:10 +00001/*
Detlev Zundel2dce5512009-03-25 17:27:52 +01002 * (C) Copyright 2000-2009
wdenk5dfa25f2002-10-19 19:42:10 +00003 * 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>
wdenk5dfa25f2002-10-19 19:42:10 +000030
wdenk5dfa25f2002-10-19 19:42:10 +000031/*
32 * Use puts() instead of printf() to avoid printf buffer overflow
33 * for long help messages
34 */
Detlev Zundel2dce5512009-03-25 17:27:52 +010035
36int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
37 flag, int argc, char *argv[])
wdenk5dfa25f2002-10-19 19:42:10 +000038{
39 int i;
40 int rcode = 0;
41
wdenk8bde7f72003-06-27 21:31:46 +000042 if (argc == 1) { /*show list of commands */
wdenk9d2b18a2003-06-28 23:11:04 +000043 cmd_tbl_t *cmd_array[cmd_items];
44 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +000045
wdenk9d2b18a2003-06-28 23:11:04 +000046 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +010047 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +000048 for (i = 0; i < cmd_items; i++) {
49 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +000050 }
wdenk8bde7f72003-06-27 21:31:46 +000051
wdenk9d2b18a2003-06-28 23:11:04 +000052 /* Sort command list (trivial bubble sort) */
53 for (i = cmd_items - 1; i > 0; --i) {
54 swaps = 0;
55 for (j = 0; j < i; ++j) {
56 if (strcmp (cmd_array[j]->name,
57 cmd_array[j + 1]->name) > 0) {
58 cmd_tbl_t *tmp;
59 tmp = cmd_array[j];
60 cmd_array[j] = cmd_array[j + 1];
61 cmd_array[j + 1] = tmp;
62 ++swaps;
63 }
wdenk8bde7f72003-06-27 21:31:46 +000064 }
wdenk9d2b18a2003-06-28 23:11:04 +000065 if (!swaps)
66 break;
wdenk8bde7f72003-06-27 21:31:46 +000067 }
68
69 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +000070 for (i = 0; i < cmd_items; i++) {
71 const char *usage = cmd_array[i]->usage;
72
wdenk5dfa25f2002-10-19 19:42:10 +000073 /* allow user abort */
wdenk8bde7f72003-06-27 21:31:46 +000074 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +000075 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +000076 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +000077 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -060078 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
79 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +000080 }
wdenk5dfa25f2002-10-19 19:42:10 +000081 return 0;
82 }
wdenk5dfa25f2002-10-19 19:42:10 +000083 /*
84 * command help (long version)
85 */
wdenk8bde7f72003-06-27 21:31:46 +000086 for (i = 1; i < argc; ++i) {
Detlev Zundel2dce5512009-03-25 17:27:52 +010087 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +020088 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +000089 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000090 printf ("Unknown command '%s' - try 'help'"
91 " without arguments for list of all"
wdenk8bde7f72003-06-27 21:31:46 +000092 " known commands\n\n", argv[i]
93 );
wdenk5dfa25f2002-10-19 19:42:10 +000094 rcode = 1;
95 }
96 }
97 return rcode;
98}
99
100/***************************************************************************
101 * find command table entry for a command
102 */
Kumar Galab799cb42008-09-23 10:05:02 -0500103cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +0000104{
105 cmd_tbl_t *cmdtp;
Kumar Galab799cb42008-09-23 10:05:02 -0500106 cmd_tbl_t *cmdtp_temp = table; /*Init value */
wdenk9d2b18a2003-06-28 23:11:04 +0000107 const char *p;
108 int len;
109 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000110
wdenk9d2b18a2003-06-28 23:11:04 +0000111 /*
112 * Some commands allow length modifiers (like "cp.b");
113 * compare command name only until first dot.
114 */
115 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
116
Kumar Galab799cb42008-09-23 10:05:02 -0500117 for (cmdtp = table;
118 cmdtp != table + table_len;
wdenk9d2b18a2003-06-28 23:11:04 +0000119 cmdtp++) {
120 if (strncmp (cmd, cmdtp->name, len) == 0) {
121 if (len == strlen (cmdtp->name))
122 return cmdtp; /* full match */
123
124 cmdtp_temp = cmdtp; /* abbreviated command ? */
125 n_found++;
126 }
wdenk5dfa25f2002-10-19 19:42:10 +0000127 }
wdenk9d2b18a2003-06-28 23:11:04 +0000128 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000129 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000130 }
wdenk8bde7f72003-06-27 21:31:46 +0000131
wdenk9d2b18a2003-06-28 23:11:04 +0000132 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000133}
wdenk04a85b32004-04-15 18:22:41 +0000134
Kumar Galab799cb42008-09-23 10:05:02 -0500135cmd_tbl_t *find_cmd (const char *cmd)
136{
137 int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
138 return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
139}
140
Wolfgang Denk94796d82009-05-24 19:17:29 +0200141int cmd_usage(cmd_tbl_t *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600142{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200143 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
144
145#ifdef CONFIG_SYS_LONGHELP
146 printf("Usage:\n%s ", cmdtp->name);
147
148 if (!cmdtp->help) {
149 puts ("- No additional help available.\n");
150 return 1;
151 }
152
153 puts (cmdtp->help);
154 putc ('\n');
155#endif /* CONFIG_SYS_LONGHELP */
156 return 0;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600157}
158
wdenk04a85b32004-04-15 18:22:41 +0000159#ifdef CONFIG_AUTO_COMPLETE
160
161int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
162{
163 static char tmp_buf[512];
164 int space;
165
166 space = last_char == '\0' || last_char == ' ' || last_char == '\t';
167
168 if (space && argc == 1)
169 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
170
171 if (!space && argc == 2)
172 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
173
174 return 0;
175}
176
177static void install_auto_complete_handler(const char *cmd,
178 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
179{
180 cmd_tbl_t *cmdtp;
181
182 cmdtp = find_cmd(cmd);
183 if (cmdtp == NULL)
184 return;
185
186 cmdtp->complete = complete;
187}
188
189void install_auto_complete(void)
190{
191 install_auto_complete_handler("printenv", var_complete);
192 install_auto_complete_handler("setenv", var_complete);
Jon Loeligerc3517f92007-07-08 18:10:08 -0500193#if defined(CONFIG_CMD_RUN)
wdenk04a85b32004-04-15 18:22:41 +0000194 install_auto_complete_handler("run", var_complete);
195#endif
196}
197
198/*************************************************************************************/
199
200static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
201{
202 cmd_tbl_t *cmdtp;
203 const char *p;
204 int len, clen;
205 int n_found = 0;
206 const char *cmd;
207
208 /* sanity? */
209 if (maxv < 2)
210 return -2;
211
212 cmdv[0] = NULL;
213
214 if (argc == 0) {
215 /* output full list of commands */
216 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
217 if (n_found >= maxv - 2) {
218 cmdv[n_found++] = "...";
219 break;
220 }
221 cmdv[n_found++] = cmdtp->name;
222 }
223 cmdv[n_found] = NULL;
224 return n_found;
225 }
226
227 /* more than one arg or one but the start of the next */
228 if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
229 cmdtp = find_cmd(argv[0]);
230 if (cmdtp == NULL || cmdtp->complete == NULL) {
231 cmdv[0] = NULL;
232 return 0;
233 }
234 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
235 }
236
237 cmd = argv[0];
238 /*
239 * Some commands allow length modifiers (like "cp.b");
240 * compare command name only until first dot.
241 */
242 p = strchr(cmd, '.');
243 if (p == NULL)
244 len = strlen(cmd);
245 else
246 len = p - cmd;
247
248 /* return the partial matches */
249 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
250
251 clen = strlen(cmdtp->name);
252 if (clen < len)
253 continue;
254
255 if (memcmp(cmd, cmdtp->name, len) != 0)
256 continue;
257
258 /* too many! */
259 if (n_found >= maxv - 2) {
260 cmdv[n_found++] = "...";
261 break;
262 }
263
264 cmdv[n_found++] = cmdtp->name;
265 }
266
267 cmdv[n_found] = NULL;
268 return n_found;
269}
270
271static int make_argv(char *s, int argvsz, char *argv[])
272{
273 int argc = 0;
274
275 /* split into argv */
276 while (argc < argvsz - 1) {
277
278 /* skip any white space */
279 while ((*s == ' ') || (*s == '\t'))
280 ++s;
281
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200282 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000283 break;
284
285 argv[argc++] = s; /* begin of argument string */
286
287 /* find end of string */
288 while (*s && (*s != ' ') && (*s != '\t'))
289 ++s;
290
291 if (*s == '\0') /* end of s, no more args */
292 break;
293
294 *s++ = '\0'; /* terminate current arg */
295 }
296 argv[argc] = NULL;
297
298 return argc;
299}
300
301static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
302{
303 int ll = leader != NULL ? strlen(leader) : 0;
304 int sl = sep != NULL ? strlen(sep) : 0;
305 int len, i;
306
307 if (banner) {
308 puts("\n");
309 puts(banner);
310 }
311
312 i = linemax; /* force leader and newline */
313 while (*argv != NULL) {
314 len = strlen(*argv) + sl;
315 if (i + len >= linemax) {
316 puts("\n");
317 if (leader)
318 puts(leader);
319 i = ll - sl;
320 } else if (sep)
321 puts(sep);
322 puts(*argv++);
323 i += len;
324 }
325 printf("\n");
326}
327
328static int find_common_prefix(char *argv[])
329{
330 int i, len;
331 char *anchor, *s, *t;
332
333 if (*argv == NULL)
334 return 0;
335
336 /* begin with max */
337 anchor = *argv++;
338 len = strlen(anchor);
339 while ((t = *argv++) != NULL) {
340 s = anchor;
341 for (i = 0; i < len; i++, t++, s++) {
342 if (*t != *s)
343 break;
344 }
345 len = s - anchor;
346 }
347 return len;
348}
349
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200350static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000351
352int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
353{
354 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200355 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000356 char *cmdv[20];
357 char *s, *t;
358 const char *sep;
359 int i, j, k, len, seplen, argc;
360 int cnt;
361 char last_char;
362
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200363 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000364 return 0; /* not in normal console */
365
366 cnt = strlen(buf);
367 if (cnt >= 1)
368 last_char = buf[cnt - 1];
369 else
370 last_char = '\0';
371
372 /* copy to secondary buffer which will be affected */
373 strcpy(tmp_buf, buf);
374
375 /* separate into argv */
376 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
377
378 /* do the completion and return the possible completions */
379 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
380
381 /* no match; bell and out */
382 if (i == 0) {
383 if (argc > 1) /* allow tab for non command */
384 return 0;
385 putc('\a');
386 return 1;
387 }
388
389 s = NULL;
390 len = 0;
391 sep = NULL;
392 seplen = 0;
393 if (i == 1) { /* one match; perfect */
394 k = strlen(argv[argc - 1]);
395 s = cmdv[0] + k;
396 len = strlen(s);
397 sep = " ";
398 seplen = 1;
399 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
400 k = strlen(argv[argc - 1]);
401 j -= k;
402 if (j > 0) {
403 s = cmdv[0] + k;
404 len = j;
405 }
406 }
407
408 if (s != NULL) {
409 k = len + seplen;
410 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200411 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000412 putc('\a');
413 return 1;
414 }
415
416 t = buf + cnt;
417 for (i = 0; i < len; i++)
418 *t++ = *s++;
419 if (sep != NULL)
420 for (i = 0; i < seplen; i++)
421 *t++ = sep[i];
422 *t = '\0';
423 n += k;
424 col += k;
425 puts(t - k);
426 if (sep == NULL)
427 putc('\a');
428 *np = n;
429 *colp = col;
430 } else {
431 print_argv(NULL, " ", " ", 78, cmdv);
432
433 puts(prompt);
434 puts(buf);
435 }
436 return 1;
437}
438
439#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200440
441#ifdef CMD_DATA_SIZE
442int cmd_get_data_size(char* arg, int default_size)
443{
444 /* Check for a size specification .b, .w or .l.
445 */
446 int len = strlen(arg);
447 if (len > 2 && arg[len-2] == '.') {
448 switch(arg[len-1]) {
449 case 'b':
450 return 1;
451 case 'w':
452 return 2;
453 case 'l':
454 return 4;
455 case 's':
456 return -2;
457 default:
458 return -1;
459 }
460 }
461 return default_size;
462}
463#endif