blob: 16f3afe3947ef608b00f78665d8974700399b2b1 [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
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
wdenk0d498392003-07-01 21:06:45 +000039U_BOOT_CMD(
40 version, 1, 1, do_version,
Peter Tyser2fb26042009-01-27 18:03:12 -060041 "print monitor version",
wdenkb0fce992003-06-29 21:03:46 +000042 NULL
43);
44
Jon Loeligerc3517f92007-07-08 18:10:08 -050045#if defined(CONFIG_CMD_ECHO)
Wolfgang Denk953c5b62006-03-12 16:51:59 +010046
wdenk5dfa25f2002-10-19 19:42:10 +000047int
48do_echo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
49{
50 int i, putnl = 1;
51
52 for (i = 1; i < argc; i++) {
53 char *p = argv[i], c;
54
55 if (i > 1)
56 putc(' ');
wdenk71f95112003-06-15 22:40:42 +000057 while ((c = *p++) != '\0') {
wdenk5dfa25f2002-10-19 19:42:10 +000058 if (c == '\\' && *p == 'c') {
59 putnl = 0;
60 p++;
wdenk71f95112003-06-15 22:40:42 +000061 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000062 putc(c);
wdenk71f95112003-06-15 22:40:42 +000063 }
64 }
wdenk5dfa25f2002-10-19 19:42:10 +000065 }
66
67 if (putnl)
68 putc('\n');
69 return 0;
70}
71
wdenk0d498392003-07-01 21:06:45 +000072U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073 echo, CONFIG_SYS_MAXARGS, 1, do_echo,
Peter Tyser2fb26042009-01-27 18:03:12 -060074 "echo args to console",
Wolfgang Denk53677ef2008-05-20 16:00:29 +020075 "[args..]\n"
wdenkb0fce992003-06-29 21:03:46 +000076 " - echo args to console; \\c suppresses newline\n"
77);
78
Jon Loeliger90253172007-07-10 11:02:44 -050079#endif
Wolfgang Denk953c5b62006-03-12 16:51:59 +010080
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020081#ifdef CONFIG_SYS_HUSH_PARSER
wdenkc26e4542004-04-18 10:13:26 +000082
83int
84do_test (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
85{
86 char **ap;
87 int left, adv, expr, last_expr, neg, last_cmp;
88
89 /* args? */
90 if (argc < 3)
91 return 1;
92
93#if 0
94 {
95 printf("test:");
wdenk6e592382004-04-18 17:39:38 +000096 left = 1;
wdenkc26e4542004-04-18 10:13:26 +000097 while (argv[left])
98 printf(" %s", argv[left++]);
99 }
100#endif
wdenk6e592382004-04-18 17:39:38 +0000101
wdenkc26e4542004-04-18 10:13:26 +0000102 last_expr = 0;
103 left = argc - 1; ap = argv + 1;
104 if (left > 0 && strcmp(ap[0], "!") == 0) {
105 neg = 1;
106 ap++;
107 left--;
108 } else
109 neg = 0;
110
111 expr = -1;
112 last_cmp = -1;
113 last_expr = -1;
114 while (left > 0) {
115
116 if (strcmp(ap[0], "-o") == 0 || strcmp(ap[0], "-a") == 0)
117 adv = 1;
118 else if (strcmp(ap[0], "-z") == 0 || strcmp(ap[0], "-n") == 0)
119 adv = 2;
120 else
121 adv = 3;
122
123 if (left < adv) {
124 expr = 1;
125 break;
126 }
127
128 if (adv == 1) {
129 if (strcmp(ap[0], "-o") == 0) {
130 last_expr = expr;
131 last_cmp = 0;
132 } else if (strcmp(ap[0], "-a") == 0) {
133 last_expr = expr;
134 last_cmp = 1;
135 } else {
136 expr = 1;
137 break;
138 }
139 }
140
141 if (adv == 2) {
142 if (strcmp(ap[0], "-z") == 0)
wdenkc26e4542004-04-18 10:13:26 +0000143 expr = strlen(ap[1]) == 0 ? 1 : 0;
wdenk20787e22005-04-06 00:04:16 +0000144 else if (strcmp(ap[0], "-n") == 0)
145 expr = strlen(ap[1]) == 0 ? 0 : 1;
wdenkc26e4542004-04-18 10:13:26 +0000146 else {
147 expr = 1;
148 break;
149 }
150
151 if (last_cmp == 0)
152 expr = last_expr || expr;
153 else if (last_cmp == 1)
154 expr = last_expr && expr;
155 last_cmp = -1;
156 }
157
158 if (adv == 3) {
159 if (strcmp(ap[1], "=") == 0)
160 expr = strcmp(ap[0], ap[2]) == 0;
161 else if (strcmp(ap[1], "!=") == 0)
162 expr = strcmp(ap[0], ap[2]) != 0;
163 else if (strcmp(ap[1], ">") == 0)
164 expr = strcmp(ap[0], ap[2]) > 0;
165 else if (strcmp(ap[1], "<") == 0)
166 expr = strcmp(ap[0], ap[2]) < 0;
167 else if (strcmp(ap[1], "-eq") == 0)
168 expr = simple_strtol(ap[0], NULL, 10) == simple_strtol(ap[2], NULL, 10);
169 else if (strcmp(ap[1], "-ne") == 0)
170 expr = simple_strtol(ap[0], NULL, 10) != simple_strtol(ap[2], NULL, 10);
171 else if (strcmp(ap[1], "-lt") == 0)
172 expr = simple_strtol(ap[0], NULL, 10) < simple_strtol(ap[2], NULL, 10);
173 else if (strcmp(ap[1], "-le") == 0)
174 expr = simple_strtol(ap[0], NULL, 10) <= simple_strtol(ap[2], NULL, 10);
175 else if (strcmp(ap[1], "-gt") == 0)
176 expr = simple_strtol(ap[0], NULL, 10) > simple_strtol(ap[2], NULL, 10);
177 else if (strcmp(ap[1], "-ge") == 0)
178 expr = simple_strtol(ap[0], NULL, 10) >= simple_strtol(ap[2], NULL, 10);
179 else {
180 expr = 1;
181 break;
182 }
183
184 if (last_cmp == 0)
185 expr = last_expr || expr;
186 else if (last_cmp == 1)
187 expr = last_expr && expr;
188 last_cmp = -1;
189 }
190
191 ap += adv; left -= adv;
192 }
193
194 if (neg)
195 expr = !expr;
196
197 expr = !expr;
198
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200199 debug (": returns %d\n", expr);
wdenkc26e4542004-04-18 10:13:26 +0000200
201 return expr;
202}
203
204U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200205 test, CONFIG_SYS_MAXARGS, 1, do_test,
Peter Tyser2fb26042009-01-27 18:03:12 -0600206 "minimal test like /bin/sh",
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200207 "[args..]\n"
wdenkc26e4542004-04-18 10:13:26 +0000208 " - test functionality\n"
209);
210
211int
212do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
213{
214 int r;
215
216 r = 0;
217 if (argc > 1)
218 r = simple_strtoul(argv[1], NULL, 10);
219
220 return -r - 2;
221}
222
223U_BOOT_CMD(
224 exit, 2, 1, do_exit,
Peter Tyser2fb26042009-01-27 18:03:12 -0600225 "exit script",
wdenkc26e4542004-04-18 10:13:26 +0000226 " - exit functionality\n"
227);
228
229
230#endif
231
wdenk5dfa25f2002-10-19 19:42:10 +0000232/*
233 * Use puts() instead of printf() to avoid printf buffer overflow
234 * for long help messages
235 */
Detlev Zundel2dce5512009-03-25 17:27:52 +0100236
237int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
238 flag, int argc, char *argv[])
wdenk5dfa25f2002-10-19 19:42:10 +0000239{
240 int i;
241 int rcode = 0;
242
wdenk8bde7f72003-06-27 21:31:46 +0000243 if (argc == 1) { /*show list of commands */
wdenk9d2b18a2003-06-28 23:11:04 +0000244 cmd_tbl_t *cmd_array[cmd_items];
245 int i, j, swaps;
wdenk8bde7f72003-06-27 21:31:46 +0000246
wdenk9d2b18a2003-06-28 23:11:04 +0000247 /* Make array of commands from .uboot_cmd section */
Detlev Zundel2dce5512009-03-25 17:27:52 +0100248 cmdtp = cmd_start;
wdenk9d2b18a2003-06-28 23:11:04 +0000249 for (i = 0; i < cmd_items; i++) {
250 cmd_array[i] = cmdtp++;
wdenk8bde7f72003-06-27 21:31:46 +0000251 }
wdenk8bde7f72003-06-27 21:31:46 +0000252
wdenk9d2b18a2003-06-28 23:11:04 +0000253 /* Sort command list (trivial bubble sort) */
254 for (i = cmd_items - 1; i > 0; --i) {
255 swaps = 0;
256 for (j = 0; j < i; ++j) {
257 if (strcmp (cmd_array[j]->name,
258 cmd_array[j + 1]->name) > 0) {
259 cmd_tbl_t *tmp;
260 tmp = cmd_array[j];
261 cmd_array[j] = cmd_array[j + 1];
262 cmd_array[j + 1] = tmp;
263 ++swaps;
264 }
wdenk8bde7f72003-06-27 21:31:46 +0000265 }
wdenk9d2b18a2003-06-28 23:11:04 +0000266 if (!swaps)
267 break;
wdenk8bde7f72003-06-27 21:31:46 +0000268 }
269
270 /* print short help (usage) */
wdenk9d2b18a2003-06-28 23:11:04 +0000271 for (i = 0; i < cmd_items; i++) {
272 const char *usage = cmd_array[i]->usage;
273
wdenk5dfa25f2002-10-19 19:42:10 +0000274 /* allow user abort */
wdenk8bde7f72003-06-27 21:31:46 +0000275 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +0000276 return 1;
wdenk9d2b18a2003-06-28 23:11:04 +0000277 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +0000278 continue;
Peter Tyser2fb26042009-01-27 18:03:12 -0600279 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
280 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +0000281 }
wdenk5dfa25f2002-10-19 19:42:10 +0000282 return 0;
283 }
wdenk5dfa25f2002-10-19 19:42:10 +0000284 /*
285 * command help (long version)
286 */
wdenk8bde7f72003-06-27 21:31:46 +0000287 for (i = 1; i < argc; ++i) {
Detlev Zundel2dce5512009-03-25 17:27:52 +0100288 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
Wolfgang Denk94796d82009-05-24 19:17:29 +0200289 rcode |= cmd_usage(cmdtp);
wdenk71f95112003-06-15 22:40:42 +0000290 } else {
wdenk5dfa25f2002-10-19 19:42:10 +0000291 printf ("Unknown command '%s' - try 'help'"
292 " without arguments for list of all"
wdenk8bde7f72003-06-27 21:31:46 +0000293 " known commands\n\n", argv[i]
294 );
wdenk5dfa25f2002-10-19 19:42:10 +0000295 rcode = 1;
296 }
297 }
298 return rcode;
299}
300
Detlev Zundel2dce5512009-03-25 17:27:52 +0100301int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
302{
303 return _do_help(&__u_boot_cmd_start,
304 &__u_boot_cmd_end - &__u_boot_cmd_start,
305 cmdtp, flag, argc, argv);
306}
307
wdenk8bde7f72003-06-27 21:31:46 +0000308
wdenk0d498392003-07-01 21:06:45 +0000309U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200310 help, CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyser2fb26042009-01-27 18:03:12 -0600311 "print online help",
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200312 "[command ...]\n"
313 " - show help information (for 'command')\n"
314 "'help' prints online help for the monitor commands.\n\n"
315 "Without arguments, it prints a short usage message for all commands.\n\n"
316 "To get detailed help information for specific commands you can type\n"
wdenk8bde7f72003-06-27 21:31:46 +0000317 "'help' with one or more command names as arguments.\n"
318);
319
wdenk0d498392003-07-01 21:06:45 +0000320/* This do not ust the U_BOOT_CMD macro as ? can't be used in symbol names */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200321#ifdef CONFIG_SYS_LONGHELP
wdenk0d498392003-07-01 21:06:45 +0000322cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200323 "?", CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyser2fb26042009-01-27 18:03:12 -0600324 "alias for 'help'",
wdenk8bde7f72003-06-27 21:31:46 +0000325 NULL
wdenk0d498392003-07-01 21:06:45 +0000326};
327#else
328cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200329 "?", CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyser2fb26042009-01-27 18:03:12 -0600330 "alias for 'help'"
wdenk0d498392003-07-01 21:06:45 +0000331};
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200332#endif /* CONFIG_SYS_LONGHELP */
wdenk8bde7f72003-06-27 21:31:46 +0000333
wdenk5dfa25f2002-10-19 19:42:10 +0000334/***************************************************************************
335 * find command table entry for a command
336 */
Kumar Galab799cb42008-09-23 10:05:02 -0500337cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +0000338{
339 cmd_tbl_t *cmdtp;
Kumar Galab799cb42008-09-23 10:05:02 -0500340 cmd_tbl_t *cmdtp_temp = table; /*Init value */
wdenk9d2b18a2003-06-28 23:11:04 +0000341 const char *p;
342 int len;
343 int n_found = 0;
wdenk8bde7f72003-06-27 21:31:46 +0000344
wdenk9d2b18a2003-06-28 23:11:04 +0000345 /*
346 * Some commands allow length modifiers (like "cp.b");
347 * compare command name only until first dot.
348 */
349 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
350
Kumar Galab799cb42008-09-23 10:05:02 -0500351 for (cmdtp = table;
352 cmdtp != table + table_len;
wdenk9d2b18a2003-06-28 23:11:04 +0000353 cmdtp++) {
354 if (strncmp (cmd, cmdtp->name, len) == 0) {
355 if (len == strlen (cmdtp->name))
356 return cmdtp; /* full match */
357
358 cmdtp_temp = cmdtp; /* abbreviated command ? */
359 n_found++;
360 }
wdenk5dfa25f2002-10-19 19:42:10 +0000361 }
wdenk9d2b18a2003-06-28 23:11:04 +0000362 if (n_found == 1) { /* exactly one match */
wdenk8bde7f72003-06-27 21:31:46 +0000363 return cmdtp_temp;
wdenk9d2b18a2003-06-28 23:11:04 +0000364 }
wdenk8bde7f72003-06-27 21:31:46 +0000365
wdenk9d2b18a2003-06-28 23:11:04 +0000366 return NULL; /* not found or ambiguous command */
wdenk5dfa25f2002-10-19 19:42:10 +0000367}
wdenk04a85b32004-04-15 18:22:41 +0000368
Kumar Galab799cb42008-09-23 10:05:02 -0500369cmd_tbl_t *find_cmd (const char *cmd)
370{
371 int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
372 return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
373}
374
Wolfgang Denk94796d82009-05-24 19:17:29 +0200375int cmd_usage(cmd_tbl_t *cmdtp)
Peter Tyser62c3ae72009-01-27 18:03:10 -0600376{
Wolfgang Denk94796d82009-05-24 19:17:29 +0200377 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
378
379#ifdef CONFIG_SYS_LONGHELP
380 printf("Usage:\n%s ", cmdtp->name);
381
382 if (!cmdtp->help) {
383 puts ("- No additional help available.\n");
384 return 1;
385 }
386
387 puts (cmdtp->help);
388 putc ('\n');
389#endif /* CONFIG_SYS_LONGHELP */
390 return 0;
Peter Tyser62c3ae72009-01-27 18:03:10 -0600391}
392
wdenk04a85b32004-04-15 18:22:41 +0000393#ifdef CONFIG_AUTO_COMPLETE
394
395int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
396{
397 static char tmp_buf[512];
398 int space;
399
400 space = last_char == '\0' || last_char == ' ' || last_char == '\t';
401
402 if (space && argc == 1)
403 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
404
405 if (!space && argc == 2)
406 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
407
408 return 0;
409}
410
411static void install_auto_complete_handler(const char *cmd,
412 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
413{
414 cmd_tbl_t *cmdtp;
415
416 cmdtp = find_cmd(cmd);
417 if (cmdtp == NULL)
418 return;
419
420 cmdtp->complete = complete;
421}
422
423void install_auto_complete(void)
424{
425 install_auto_complete_handler("printenv", var_complete);
426 install_auto_complete_handler("setenv", var_complete);
Jon Loeligerc3517f92007-07-08 18:10:08 -0500427#if defined(CONFIG_CMD_RUN)
wdenk04a85b32004-04-15 18:22:41 +0000428 install_auto_complete_handler("run", var_complete);
429#endif
430}
431
432/*************************************************************************************/
433
434static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
435{
436 cmd_tbl_t *cmdtp;
437 const char *p;
438 int len, clen;
439 int n_found = 0;
440 const char *cmd;
441
442 /* sanity? */
443 if (maxv < 2)
444 return -2;
445
446 cmdv[0] = NULL;
447
448 if (argc == 0) {
449 /* output full list of commands */
450 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
451 if (n_found >= maxv - 2) {
452 cmdv[n_found++] = "...";
453 break;
454 }
455 cmdv[n_found++] = cmdtp->name;
456 }
457 cmdv[n_found] = NULL;
458 return n_found;
459 }
460
461 /* more than one arg or one but the start of the next */
462 if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
463 cmdtp = find_cmd(argv[0]);
464 if (cmdtp == NULL || cmdtp->complete == NULL) {
465 cmdv[0] = NULL;
466 return 0;
467 }
468 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
469 }
470
471 cmd = argv[0];
472 /*
473 * Some commands allow length modifiers (like "cp.b");
474 * compare command name only until first dot.
475 */
476 p = strchr(cmd, '.');
477 if (p == NULL)
478 len = strlen(cmd);
479 else
480 len = p - cmd;
481
482 /* return the partial matches */
483 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
484
485 clen = strlen(cmdtp->name);
486 if (clen < len)
487 continue;
488
489 if (memcmp(cmd, cmdtp->name, len) != 0)
490 continue;
491
492 /* too many! */
493 if (n_found >= maxv - 2) {
494 cmdv[n_found++] = "...";
495 break;
496 }
497
498 cmdv[n_found++] = cmdtp->name;
499 }
500
501 cmdv[n_found] = NULL;
502 return n_found;
503}
504
505static int make_argv(char *s, int argvsz, char *argv[])
506{
507 int argc = 0;
508
509 /* split into argv */
510 while (argc < argvsz - 1) {
511
512 /* skip any white space */
513 while ((*s == ' ') || (*s == '\t'))
514 ++s;
515
Wolfgang Denk53677ef2008-05-20 16:00:29 +0200516 if (*s == '\0') /* end of s, no more args */
wdenk04a85b32004-04-15 18:22:41 +0000517 break;
518
519 argv[argc++] = s; /* begin of argument string */
520
521 /* find end of string */
522 while (*s && (*s != ' ') && (*s != '\t'))
523 ++s;
524
525 if (*s == '\0') /* end of s, no more args */
526 break;
527
528 *s++ = '\0'; /* terminate current arg */
529 }
530 argv[argc] = NULL;
531
532 return argc;
533}
534
535static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
536{
537 int ll = leader != NULL ? strlen(leader) : 0;
538 int sl = sep != NULL ? strlen(sep) : 0;
539 int len, i;
540
541 if (banner) {
542 puts("\n");
543 puts(banner);
544 }
545
546 i = linemax; /* force leader and newline */
547 while (*argv != NULL) {
548 len = strlen(*argv) + sl;
549 if (i + len >= linemax) {
550 puts("\n");
551 if (leader)
552 puts(leader);
553 i = ll - sl;
554 } else if (sep)
555 puts(sep);
556 puts(*argv++);
557 i += len;
558 }
559 printf("\n");
560}
561
562static int find_common_prefix(char *argv[])
563{
564 int i, len;
565 char *anchor, *s, *t;
566
567 if (*argv == NULL)
568 return 0;
569
570 /* begin with max */
571 anchor = *argv++;
572 len = strlen(anchor);
573 while ((t = *argv++) != NULL) {
574 s = anchor;
575 for (i = 0; i < len; i++, t++, s++) {
576 if (*t != *s)
577 break;
578 }
579 len = s - anchor;
580 }
581 return len;
582}
583
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200584static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
wdenk04a85b32004-04-15 18:22:41 +0000585
586int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
587{
588 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200589 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk04a85b32004-04-15 18:22:41 +0000590 char *cmdv[20];
591 char *s, *t;
592 const char *sep;
593 int i, j, k, len, seplen, argc;
594 int cnt;
595 char last_char;
596
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200597 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk04a85b32004-04-15 18:22:41 +0000598 return 0; /* not in normal console */
599
600 cnt = strlen(buf);
601 if (cnt >= 1)
602 last_char = buf[cnt - 1];
603 else
604 last_char = '\0';
605
606 /* copy to secondary buffer which will be affected */
607 strcpy(tmp_buf, buf);
608
609 /* separate into argv */
610 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
611
612 /* do the completion and return the possible completions */
613 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
614
615 /* no match; bell and out */
616 if (i == 0) {
617 if (argc > 1) /* allow tab for non command */
618 return 0;
619 putc('\a');
620 return 1;
621 }
622
623 s = NULL;
624 len = 0;
625 sep = NULL;
626 seplen = 0;
627 if (i == 1) { /* one match; perfect */
628 k = strlen(argv[argc - 1]);
629 s = cmdv[0] + k;
630 len = strlen(s);
631 sep = " ";
632 seplen = 1;
633 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
634 k = strlen(argv[argc - 1]);
635 j -= k;
636 if (j > 0) {
637 s = cmdv[0] + k;
638 len = j;
639 }
640 }
641
642 if (s != NULL) {
643 k = len + seplen;
644 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200645 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk04a85b32004-04-15 18:22:41 +0000646 putc('\a');
647 return 1;
648 }
649
650 t = buf + cnt;
651 for (i = 0; i < len; i++)
652 *t++ = *s++;
653 if (sep != NULL)
654 for (i = 0; i < seplen; i++)
655 *t++ = sep[i];
656 *t = '\0';
657 n += k;
658 col += k;
659 puts(t - k);
660 if (sep == NULL)
661 putc('\a');
662 *np = n;
663 *colp = col;
664 } else {
665 print_argv(NULL, " ", " ", 78, cmdv);
666
667 puts(prompt);
668 puts(buf);
669 }
670 return 1;
671}
672
673#endif
Jean-Christophe PLAGNIOL-VILLARD8a40fb12008-09-10 22:48:05 +0200674
675#ifdef CMD_DATA_SIZE
676int cmd_get_data_size(char* arg, int default_size)
677{
678 /* Check for a size specification .b, .w or .l.
679 */
680 int len = strlen(arg);
681 if (len > 2 && arg[len-2] == '.') {
682 switch(arg[len-1]) {
683 case 'b':
684 return 1;
685 case 'w':
686 return 2;
687 case 'l':
688 return 4;
689 case 's':
690 return -2;
691 default:
692 return -1;
693 }
694 }
695 return default_size;
696}
697#endif