]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/command.c
* Patches by Pantelis Antoniou, 30 Mar 2004:
[people/ms/u-boot.git] / common / command.c
CommitLineData
5dfa25f2 1/*
8bde7f77 2 * (C) Copyright 2000-2003
5dfa25f2
WD
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>
5dfa25f2
WD
30
31int
32do_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
0d498393
WD
39U_BOOT_CMD(
40 version, 1, 1, do_version,
b0fce99b
WD
41 "version - print monitor version\n",
42 NULL
43);
44
5dfa25f2
WD
45int
46do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47{
48 int i, putnl = 1;
49
50 for (i = 1; i < argc; i++) {
51 char *p = argv[i], c;
52
53 if (i > 1)
54 putc(' ');
71f95118 55 while ((c = *p++) != '\0') {
5dfa25f2
WD
56 if (c == '\\' && *p == 'c') {
57 putnl = 0;
58 p++;
71f95118 59 } else {
5dfa25f2 60 putc(c);
71f95118
WD
61 }
62 }
5dfa25f2
WD
63 }
64
65 if (putnl)
66 putc('\n');
67 return 0;
68}
69
0d498393
WD
70U_BOOT_CMD(
71 echo, CFG_MAXARGS, 1, do_echo,
b0fce99b
WD
72 "echo - echo args to console\n",
73 "[args..]\n"
74 " - echo args to console; \\c suppresses newline\n"
75);
76
5dfa25f2
WD
77/*
78 * Use puts() instead of printf() to avoid printf buffer overflow
79 * for long help messages
80 */
8bde7f77 81int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
5dfa25f2
WD
82{
83 int i;
84 int rcode = 0;
85
8bde7f77
WD
86 if (argc == 1) { /*show list of commands */
87
9d2b18a0
WD
88 int cmd_items = &__u_boot_cmd_end -
89 &__u_boot_cmd_start; /* pointer arith! */
90 cmd_tbl_t *cmd_array[cmd_items];
91 int i, j, swaps;
92
93 /* Make array of commands from .uboot_cmd section */
94 cmdtp = &__u_boot_cmd_start;
95 for (i = 0; i < cmd_items; i++) {
96 cmd_array[i] = cmdtp++;
8bde7f77 97 }
8bde7f77 98
9d2b18a0
WD
99 /* Sort command list (trivial bubble sort) */
100 for (i = cmd_items - 1; i > 0; --i) {
101 swaps = 0;
102 for (j = 0; j < i; ++j) {
103 if (strcmp (cmd_array[j]->name,
104 cmd_array[j + 1]->name) > 0) {
105 cmd_tbl_t *tmp;
106 tmp = cmd_array[j];
107 cmd_array[j] = cmd_array[j + 1];
108 cmd_array[j + 1] = tmp;
109 ++swaps;
110 }
8bde7f77 111 }
9d2b18a0
WD
112 if (!swaps)
113 break;
8bde7f77 114 }
5dfa25f2 115
8bde7f77 116 /* print short help (usage) */
9d2b18a0
WD
117 for (i = 0; i < cmd_items; i++) {
118 const char *usage = cmd_array[i]->usage;
119
5dfa25f2 120 /* allow user abort */
8bde7f77 121 if (ctrlc ())
5dfa25f2 122 return 1;
9d2b18a0 123 if (usage == NULL)
5dfa25f2 124 continue;
9d2b18a0 125 puts (usage);
5dfa25f2 126 }
5dfa25f2
WD
127 return 0;
128 }
5dfa25f2
WD
129 /*
130 * command help (long version)
131 */
8bde7f77
WD
132 for (i = 1; i < argc; ++i) {
133 if ((cmdtp = find_cmd (argv[i])) != NULL) {
5dfa25f2
WD
134#ifdef CFG_LONGHELP
135 /* found - print (long) help info */
136 puts (cmdtp->name);
137 putc (' ');
138 if (cmdtp->help) {
139 puts (cmdtp->help);
140 } else {
141 puts ("- No help available.\n");
142 rcode = 1;
143 }
144 putc ('\n');
145#else /* no long help available */
146 if (cmdtp->usage)
147 puts (cmdtp->usage);
148#endif /* CFG_LONGHELP */
71f95118 149 } else {
5dfa25f2
WD
150 printf ("Unknown command '%s' - try 'help'"
151 " without arguments for list of all"
8bde7f77
WD
152 " known commands\n\n", argv[i]
153 );
5dfa25f2
WD
154 rcode = 1;
155 }
156 }
157 return rcode;
158}
159
8bde7f77 160
0d498393
WD
161U_BOOT_CMD(
162 help, CFG_MAXARGS, 1, do_help,
8bde7f77
WD
163 "help - print online help\n",
164 "[command ...]\n"
165 " - show help information (for 'command')\n"
166 "'help' prints online help for the monitor commands.\n\n"
167 "Without arguments, it prints a short usage message for all commands.\n\n"
168 "To get detailed help information for specific commands you can type\n"
169 "'help' with one or more command names as arguments.\n"
170);
171
0d498393
WD
172/* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
173#ifdef CFG_LONGHELP
174cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
8bde7f77
WD
175 "?", CFG_MAXARGS, 1, do_help,
176 "? - alias for 'help'\n",
177 NULL
0d498393
WD
178};
179#else
180cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
181 "?", CFG_MAXARGS, 1, do_help,
182 "? - alias for 'help'\n"
183};
184#endif /* CFG_LONGHELP */
8bde7f77 185
5dfa25f2
WD
186/***************************************************************************
187 * find command table entry for a command
188 */
8bde7f77 189cmd_tbl_t *find_cmd (const char *cmd)
5dfa25f2
WD
190{
191 cmd_tbl_t *cmdtp;
8bde7f77 192 cmd_tbl_t *cmdtp_temp = &__u_boot_cmd_start; /*Init value */
9d2b18a0
WD
193 const char *p;
194 int len;
195 int n_found = 0;
196
197 /*
198 * Some commands allow length modifiers (like "cp.b");
199 * compare command name only until first dot.
200 */
201 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
202
203 for (cmdtp = &__u_boot_cmd_start;
204 cmdtp != &__u_boot_cmd_end;
205 cmdtp++) {
206 if (strncmp (cmd, cmdtp->name, len) == 0) {
207 if (len == strlen (cmdtp->name))
208 return cmdtp; /* full match */
209
210 cmdtp_temp = cmdtp; /* abbreviated command ? */
211 n_found++;
212 }
5dfa25f2 213 }
9d2b18a0 214 if (n_found == 1) { /* exactly one match */
8bde7f77 215 return cmdtp_temp;
9d2b18a0 216 }
5dfa25f2 217
9d2b18a0 218 return NULL; /* not found or ambiguous command */
8bde7f77 219}
04a85b3b
WD
220
221#ifdef CONFIG_AUTO_COMPLETE
222
223int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
224{
225 static char tmp_buf[512];
226 int space;
227
228 space = last_char == '\0' || last_char == ' ' || last_char == '\t';
229
230 if (space && argc == 1)
231 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
232
233 if (!space && argc == 2)
234 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
235
236 return 0;
237}
238
239static void install_auto_complete_handler(const char *cmd,
240 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
241{
242 cmd_tbl_t *cmdtp;
243
244 cmdtp = find_cmd(cmd);
245 if (cmdtp == NULL)
246 return;
247
248 cmdtp->complete = complete;
249}
250
251void install_auto_complete(void)
252{
253 install_auto_complete_handler("printenv", var_complete);
254 install_auto_complete_handler("setenv", var_complete);
255#if (CONFIG_COMMANDS & CFG_CMD_RUN)
256 install_auto_complete_handler("run", var_complete);
257#endif
258}
259
260/*************************************************************************************/
261
262static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
263{
264 cmd_tbl_t *cmdtp;
265 const char *p;
266 int len, clen;
267 int n_found = 0;
268 const char *cmd;
269
270 /* sanity? */
271 if (maxv < 2)
272 return -2;
273
274 cmdv[0] = NULL;
275
276 if (argc == 0) {
277 /* output full list of commands */
278 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
279 if (n_found >= maxv - 2) {
280 cmdv[n_found++] = "...";
281 break;
282 }
283 cmdv[n_found++] = cmdtp->name;
284 }
285 cmdv[n_found] = NULL;
286 return n_found;
287 }
288
289 /* more than one arg or one but the start of the next */
290 if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
291 cmdtp = find_cmd(argv[0]);
292 if (cmdtp == NULL || cmdtp->complete == NULL) {
293 cmdv[0] = NULL;
294 return 0;
295 }
296 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
297 }
298
299 cmd = argv[0];
300 /*
301 * Some commands allow length modifiers (like "cp.b");
302 * compare command name only until first dot.
303 */
304 p = strchr(cmd, '.');
305 if (p == NULL)
306 len = strlen(cmd);
307 else
308 len = p - cmd;
309
310 /* return the partial matches */
311 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
312
313 clen = strlen(cmdtp->name);
314 if (clen < len)
315 continue;
316
317 if (memcmp(cmd, cmdtp->name, len) != 0)
318 continue;
319
320 /* too many! */
321 if (n_found >= maxv - 2) {
322 cmdv[n_found++] = "...";
323 break;
324 }
325
326 cmdv[n_found++] = cmdtp->name;
327 }
328
329 cmdv[n_found] = NULL;
330 return n_found;
331}
332
333static int make_argv(char *s, int argvsz, char *argv[])
334{
335 int argc = 0;
336
337 /* split into argv */
338 while (argc < argvsz - 1) {
339
340 /* skip any white space */
341 while ((*s == ' ') || (*s == '\t'))
342 ++s;
343
344 if (*s == '\0') /* end of s, no more args */
345 break;
346
347 argv[argc++] = s; /* begin of argument string */
348
349 /* find end of string */
350 while (*s && (*s != ' ') && (*s != '\t'))
351 ++s;
352
353 if (*s == '\0') /* end of s, no more args */
354 break;
355
356 *s++ = '\0'; /* terminate current arg */
357 }
358 argv[argc] = NULL;
359
360 return argc;
361}
362
363static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
364{
365 int ll = leader != NULL ? strlen(leader) : 0;
366 int sl = sep != NULL ? strlen(sep) : 0;
367 int len, i;
368
369 if (banner) {
370 puts("\n");
371 puts(banner);
372 }
373
374 i = linemax; /* force leader and newline */
375 while (*argv != NULL) {
376 len = strlen(*argv) + sl;
377 if (i + len >= linemax) {
378 puts("\n");
379 if (leader)
380 puts(leader);
381 i = ll - sl;
382 } else if (sep)
383 puts(sep);
384 puts(*argv++);
385 i += len;
386 }
387 printf("\n");
388}
389
390static int find_common_prefix(char *argv[])
391{
392 int i, len;
393 char *anchor, *s, *t;
394
395 if (*argv == NULL)
396 return 0;
397
398 /* begin with max */
399 anchor = *argv++;
400 len = strlen(anchor);
401 while ((t = *argv++) != NULL) {
402 s = anchor;
403 for (i = 0; i < len; i++, t++, s++) {
404 if (*t != *s)
405 break;
406 }
407 len = s - anchor;
408 }
409 return len;
410}
411
412static char tmp_buf[CFG_CBSIZE]; /* copy of console I/O buffer */
413
414int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
415{
416 int n = *np, col = *colp;
417 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
418 char *cmdv[20];
419 char *s, *t;
420 const char *sep;
421 int i, j, k, len, seplen, argc;
422 int cnt;
423 char last_char;
424
425 if (strcmp(prompt, CFG_PROMPT) != 0)
426 return 0; /* not in normal console */
427
428 cnt = strlen(buf);
429 if (cnt >= 1)
430 last_char = buf[cnt - 1];
431 else
432 last_char = '\0';
433
434 /* copy to secondary buffer which will be affected */
435 strcpy(tmp_buf, buf);
436
437 /* separate into argv */
438 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
439
440 /* do the completion and return the possible completions */
441 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
442
443 /* no match; bell and out */
444 if (i == 0) {
445 if (argc > 1) /* allow tab for non command */
446 return 0;
447 putc('\a');
448 return 1;
449 }
450
451 s = NULL;
452 len = 0;
453 sep = NULL;
454 seplen = 0;
455 if (i == 1) { /* one match; perfect */
456 k = strlen(argv[argc - 1]);
457 s = cmdv[0] + k;
458 len = strlen(s);
459 sep = " ";
460 seplen = 1;
461 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
462 k = strlen(argv[argc - 1]);
463 j -= k;
464 if (j > 0) {
465 s = cmdv[0] + k;
466 len = j;
467 }
468 }
469
470 if (s != NULL) {
471 k = len + seplen;
472 /* make sure it fits */
473 if (n + k >= CFG_CBSIZE - 2) {
474 putc('\a');
475 return 1;
476 }
477
478 t = buf + cnt;
479 for (i = 0; i < len; i++)
480 *t++ = *s++;
481 if (sep != NULL)
482 for (i = 0; i < seplen; i++)
483 *t++ = sep[i];
484 *t = '\0';
485 n += k;
486 col += k;
487 puts(t - k);
488 if (sep == NULL)
489 putc('\a');
490 *np = n;
491 *colp = col;
492 } else {
493 print_argv(NULL, " ", " ", 78, cmdv);
494
495 puts(prompt);
496 puts(buf);
497 }
498 return 1;
499}
500
501#endif