blob: b57f8dfc81f5d40e8686208d8b2613c7dc95fb25 [file] [log] [blame]
wdenk5dfa25f2002-10-19 19:42:10 +00001/*
Detlev Zundel5e6c06b2009-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
wdenkf287a242003-07-01 21:06:45 +000039U_BOOT_CMD(
40 version, 1, 1, do_version,
Peter Tyserdfb72b82009-01-27 18:03:12 -060041 "print monitor version",
Wolfgang Denkc54781c2009-05-24 17:06:54 +020042 ""
wdenkf12e3962003-06-29 21:03:46 +000043);
44
Jon Loeliger052fc842007-07-08 18:10:08 -050045#if defined(CONFIG_CMD_ECHO)
Wolfgang Denk582f3dd2006-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(' ');
wdenk7a428cc2003-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++;
wdenk7a428cc2003-06-15 22:40:42 +000061 } else {
wdenk5dfa25f2002-10-19 19:42:10 +000062 putc(c);
wdenk7a428cc2003-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
wdenkf287a242003-07-01 21:06:45 +000072U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020073 echo, CONFIG_SYS_MAXARGS, 1, do_echo,
Peter Tyserdfb72b82009-01-27 18:03:12 -060074 "echo args to console",
Wolfgang Denka1be4762008-05-20 16:00:29 +020075 "[args..]\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +020076 " - echo args to console; \\c suppresses newline"
wdenkf12e3962003-06-29 21:03:46 +000077);
78
Jon Loeligerd704d912007-07-10 11:02:44 -050079#endif
Wolfgang Denk582f3dd2006-03-12 16:51:59 +010080
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081#ifdef CONFIG_SYS_HUSH_PARSER
wdenk6203e402004-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:");
wdenk05939202004-04-18 17:39:38 +000096 left = 1;
wdenk6203e402004-04-18 10:13:26 +000097 while (argv[left])
98 printf(" %s", argv[left++]);
99 }
100#endif
wdenk05939202004-04-18 17:39:38 +0000101
wdenk6203e402004-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)
wdenk6203e402004-04-18 10:13:26 +0000143 expr = strlen(ap[1]) == 0 ? 1 : 0;
wdenk2a831612005-04-06 00:04:16 +0000144 else if (strcmp(ap[0], "-n") == 0)
145 expr = strlen(ap[1]) == 0 ? 0 : 1;
wdenk6203e402004-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 Denka1be4762008-05-20 16:00:29 +0200199 debug (": returns %d\n", expr);
wdenk6203e402004-04-18 10:13:26 +0000200
201 return expr;
202}
203
204U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200205 test, CONFIG_SYS_MAXARGS, 1, do_test,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600206 "minimal test like /bin/sh",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200207 "[args..]"
wdenk6203e402004-04-18 10:13:26 +0000208);
209
210int
211do_exit (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
212{
213 int r;
214
215 r = 0;
216 if (argc > 1)
217 r = simple_strtoul(argv[1], NULL, 10);
218
219 return -r - 2;
220}
221
222U_BOOT_CMD(
223 exit, 2, 1, do_exit,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600224 "exit script",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200225 ""
wdenk6203e402004-04-18 10:13:26 +0000226);
227
228
229#endif
230
wdenk5dfa25f2002-10-19 19:42:10 +0000231/*
232 * Use puts() instead of printf() to avoid printf buffer overflow
233 * for long help messages
234 */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +0100235
236int _do_help (cmd_tbl_t *cmd_start, int cmd_items, cmd_tbl_t * cmdtp, int
237 flag, int argc, char *argv[])
wdenk5dfa25f2002-10-19 19:42:10 +0000238{
239 int i;
240 int rcode = 0;
241
wdenk57b2d802003-06-27 21:31:46 +0000242 if (argc == 1) { /*show list of commands */
wdenk3086a972003-06-28 23:11:04 +0000243 cmd_tbl_t *cmd_array[cmd_items];
244 int i, j, swaps;
wdenk57b2d802003-06-27 21:31:46 +0000245
wdenk3086a972003-06-28 23:11:04 +0000246 /* Make array of commands from .uboot_cmd section */
Detlev Zundel5e6c06b2009-03-25 17:27:52 +0100247 cmdtp = cmd_start;
wdenk3086a972003-06-28 23:11:04 +0000248 for (i = 0; i < cmd_items; i++) {
249 cmd_array[i] = cmdtp++;
wdenk57b2d802003-06-27 21:31:46 +0000250 }
wdenk57b2d802003-06-27 21:31:46 +0000251
wdenk3086a972003-06-28 23:11:04 +0000252 /* Sort command list (trivial bubble sort) */
253 for (i = cmd_items - 1; i > 0; --i) {
254 swaps = 0;
255 for (j = 0; j < i; ++j) {
256 if (strcmp (cmd_array[j]->name,
257 cmd_array[j + 1]->name) > 0) {
258 cmd_tbl_t *tmp;
259 tmp = cmd_array[j];
260 cmd_array[j] = cmd_array[j + 1];
261 cmd_array[j + 1] = tmp;
262 ++swaps;
263 }
wdenk57b2d802003-06-27 21:31:46 +0000264 }
wdenk3086a972003-06-28 23:11:04 +0000265 if (!swaps)
266 break;
wdenk57b2d802003-06-27 21:31:46 +0000267 }
268
269 /* print short help (usage) */
wdenk3086a972003-06-28 23:11:04 +0000270 for (i = 0; i < cmd_items; i++) {
271 const char *usage = cmd_array[i]->usage;
272
wdenk5dfa25f2002-10-19 19:42:10 +0000273 /* allow user abort */
wdenk57b2d802003-06-27 21:31:46 +0000274 if (ctrlc ())
wdenk5dfa25f2002-10-19 19:42:10 +0000275 return 1;
wdenk3086a972003-06-28 23:11:04 +0000276 if (usage == NULL)
wdenk5dfa25f2002-10-19 19:42:10 +0000277 continue;
Peter Tyserdfb72b82009-01-27 18:03:12 -0600278 printf("%-*s- %s\n", CONFIG_SYS_HELP_CMD_WIDTH,
279 cmd_array[i]->name, usage);
wdenk5dfa25f2002-10-19 19:42:10 +0000280 }
wdenk5dfa25f2002-10-19 19:42:10 +0000281 return 0;
282 }
wdenk5dfa25f2002-10-19 19:42:10 +0000283 /*
284 * command help (long version)
285 */
wdenk57b2d802003-06-27 21:31:46 +0000286 for (i = 1; i < argc; ++i) {
Detlev Zundel5e6c06b2009-03-25 17:27:52 +0100287 if ((cmdtp = find_cmd_tbl (argv[i], cmd_start, cmd_items )) != NULL) {
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200288 rcode |= cmd_usage(cmdtp);
wdenk7a428cc2003-06-15 22:40:42 +0000289 } else {
wdenk5dfa25f2002-10-19 19:42:10 +0000290 printf ("Unknown command '%s' - try 'help'"
291 " without arguments for list of all"
wdenk57b2d802003-06-27 21:31:46 +0000292 " known commands\n\n", argv[i]
293 );
wdenk5dfa25f2002-10-19 19:42:10 +0000294 rcode = 1;
295 }
296 }
297 return rcode;
298}
299
Detlev Zundel5e6c06b2009-03-25 17:27:52 +0100300int do_help (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
301{
302 return _do_help(&__u_boot_cmd_start,
303 &__u_boot_cmd_end - &__u_boot_cmd_start,
304 cmdtp, flag, argc, argv);
305}
306
wdenk57b2d802003-06-27 21:31:46 +0000307
wdenkf287a242003-07-01 21:06:45 +0000308U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200309 help, CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600310 "print online help",
Wolfgang Denka1be4762008-05-20 16:00:29 +0200311 "[command ...]\n"
312 " - show help information (for 'command')\n"
313 "'help' prints online help for the monitor commands.\n\n"
314 "Without arguments, it prints a short usage message for all commands.\n\n"
315 "To get detailed help information for specific commands you can type\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200316 "'help' with one or more command names as arguments."
wdenk57b2d802003-06-27 21:31:46 +0000317);
318
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200319/* This does not use the U_BOOT_CMD macro as ? can't be used in symbol names */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200320#ifdef CONFIG_SYS_LONGHELP
wdenkf287a242003-07-01 21:06:45 +0000321cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200322 "?", CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600323 "alias for 'help'",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200324 ""
wdenkf287a242003-07-01 21:06:45 +0000325};
326#else
327cmd_tbl_t __u_boot_cmd_question_mark Struct_Section = {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200328 "?", CONFIG_SYS_MAXARGS, 1, do_help,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600329 "alias for 'help'"
wdenkf287a242003-07-01 21:06:45 +0000330};
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200331#endif /* CONFIG_SYS_LONGHELP */
wdenk57b2d802003-06-27 21:31:46 +0000332
wdenk5dfa25f2002-10-19 19:42:10 +0000333/***************************************************************************
334 * find command table entry for a command
335 */
Kumar Galaac2dad62008-09-23 10:05:02 -0500336cmd_tbl_t *find_cmd_tbl (const char *cmd, cmd_tbl_t *table, int table_len)
wdenk5dfa25f2002-10-19 19:42:10 +0000337{
338 cmd_tbl_t *cmdtp;
Kumar Galaac2dad62008-09-23 10:05:02 -0500339 cmd_tbl_t *cmdtp_temp = table; /*Init value */
wdenk3086a972003-06-28 23:11:04 +0000340 const char *p;
341 int len;
342 int n_found = 0;
wdenk57b2d802003-06-27 21:31:46 +0000343
wdenk3086a972003-06-28 23:11:04 +0000344 /*
345 * Some commands allow length modifiers (like "cp.b");
346 * compare command name only until first dot.
347 */
348 len = ((p = strchr(cmd, '.')) == NULL) ? strlen (cmd) : (p - cmd);
349
Kumar Galaac2dad62008-09-23 10:05:02 -0500350 for (cmdtp = table;
351 cmdtp != table + table_len;
wdenk3086a972003-06-28 23:11:04 +0000352 cmdtp++) {
353 if (strncmp (cmd, cmdtp->name, len) == 0) {
354 if (len == strlen (cmdtp->name))
355 return cmdtp; /* full match */
356
357 cmdtp_temp = cmdtp; /* abbreviated command ? */
358 n_found++;
359 }
wdenk5dfa25f2002-10-19 19:42:10 +0000360 }
wdenk3086a972003-06-28 23:11:04 +0000361 if (n_found == 1) { /* exactly one match */
wdenk57b2d802003-06-27 21:31:46 +0000362 return cmdtp_temp;
wdenk3086a972003-06-28 23:11:04 +0000363 }
wdenk5dfa25f2002-10-19 19:42:10 +0000364
wdenk3086a972003-06-28 23:11:04 +0000365 return NULL; /* not found or ambiguous command */
wdenk57b2d802003-06-27 21:31:46 +0000366}
wdenk3902d702004-04-15 18:22:41 +0000367
Kumar Galaac2dad62008-09-23 10:05:02 -0500368cmd_tbl_t *find_cmd (const char *cmd)
369{
370 int len = &__u_boot_cmd_end - &__u_boot_cmd_start;
371 return find_cmd_tbl(cmd, &__u_boot_cmd_start, len);
372}
373
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200374int cmd_usage(cmd_tbl_t *cmdtp)
Peter Tyserddb3af92009-01-27 18:03:10 -0600375{
Wolfgang Denk9dd775b2009-05-24 19:17:29 +0200376 printf("%s - %s\n\n", cmdtp->name, cmdtp->usage);
377
378#ifdef CONFIG_SYS_LONGHELP
379 printf("Usage:\n%s ", cmdtp->name);
380
381 if (!cmdtp->help) {
382 puts ("- No additional help available.\n");
383 return 1;
384 }
385
386 puts (cmdtp->help);
387 putc ('\n');
388#endif /* CONFIG_SYS_LONGHELP */
389 return 0;
Peter Tyserddb3af92009-01-27 18:03:10 -0600390}
391
wdenk3902d702004-04-15 18:22:41 +0000392#ifdef CONFIG_AUTO_COMPLETE
393
394int var_complete(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
395{
396 static char tmp_buf[512];
397 int space;
398
399 space = last_char == '\0' || last_char == ' ' || last_char == '\t';
400
401 if (space && argc == 1)
402 return env_complete("", maxv, cmdv, sizeof(tmp_buf), tmp_buf);
403
404 if (!space && argc == 2)
405 return env_complete(argv[1], maxv, cmdv, sizeof(tmp_buf), tmp_buf);
406
407 return 0;
408}
409
410static void install_auto_complete_handler(const char *cmd,
411 int (*complete)(int argc, char *argv[], char last_char, int maxv, char *cmdv[]))
412{
413 cmd_tbl_t *cmdtp;
414
415 cmdtp = find_cmd(cmd);
416 if (cmdtp == NULL)
417 return;
418
419 cmdtp->complete = complete;
420}
421
422void install_auto_complete(void)
423{
424 install_auto_complete_handler("printenv", var_complete);
425 install_auto_complete_handler("setenv", var_complete);
Jon Loeliger052fc842007-07-08 18:10:08 -0500426#if defined(CONFIG_CMD_RUN)
wdenk3902d702004-04-15 18:22:41 +0000427 install_auto_complete_handler("run", var_complete);
428#endif
429}
430
431/*************************************************************************************/
432
433static int complete_cmdv(int argc, char *argv[], char last_char, int maxv, char *cmdv[])
434{
435 cmd_tbl_t *cmdtp;
436 const char *p;
437 int len, clen;
438 int n_found = 0;
439 const char *cmd;
440
441 /* sanity? */
442 if (maxv < 2)
443 return -2;
444
445 cmdv[0] = NULL;
446
447 if (argc == 0) {
448 /* output full list of commands */
449 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
450 if (n_found >= maxv - 2) {
451 cmdv[n_found++] = "...";
452 break;
453 }
454 cmdv[n_found++] = cmdtp->name;
455 }
456 cmdv[n_found] = NULL;
457 return n_found;
458 }
459
460 /* more than one arg or one but the start of the next */
461 if (argc > 1 || (last_char == '\0' || last_char == ' ' || last_char == '\t')) {
462 cmdtp = find_cmd(argv[0]);
463 if (cmdtp == NULL || cmdtp->complete == NULL) {
464 cmdv[0] = NULL;
465 return 0;
466 }
467 return (*cmdtp->complete)(argc, argv, last_char, maxv, cmdv);
468 }
469
470 cmd = argv[0];
471 /*
472 * Some commands allow length modifiers (like "cp.b");
473 * compare command name only until first dot.
474 */
475 p = strchr(cmd, '.');
476 if (p == NULL)
477 len = strlen(cmd);
478 else
479 len = p - cmd;
480
481 /* return the partial matches */
482 for (cmdtp = &__u_boot_cmd_start; cmdtp != &__u_boot_cmd_end; cmdtp++) {
483
484 clen = strlen(cmdtp->name);
485 if (clen < len)
486 continue;
487
488 if (memcmp(cmd, cmdtp->name, len) != 0)
489 continue;
490
491 /* too many! */
492 if (n_found >= maxv - 2) {
493 cmdv[n_found++] = "...";
494 break;
495 }
496
497 cmdv[n_found++] = cmdtp->name;
498 }
499
500 cmdv[n_found] = NULL;
501 return n_found;
502}
503
504static int make_argv(char *s, int argvsz, char *argv[])
505{
506 int argc = 0;
507
508 /* split into argv */
509 while (argc < argvsz - 1) {
510
511 /* skip any white space */
512 while ((*s == ' ') || (*s == '\t'))
513 ++s;
514
Wolfgang Denka1be4762008-05-20 16:00:29 +0200515 if (*s == '\0') /* end of s, no more args */
wdenk3902d702004-04-15 18:22:41 +0000516 break;
517
518 argv[argc++] = s; /* begin of argument string */
519
520 /* find end of string */
521 while (*s && (*s != ' ') && (*s != '\t'))
522 ++s;
523
524 if (*s == '\0') /* end of s, no more args */
525 break;
526
527 *s++ = '\0'; /* terminate current arg */
528 }
529 argv[argc] = NULL;
530
531 return argc;
532}
533
534static void print_argv(const char *banner, const char *leader, const char *sep, int linemax, char *argv[])
535{
536 int ll = leader != NULL ? strlen(leader) : 0;
537 int sl = sep != NULL ? strlen(sep) : 0;
538 int len, i;
539
540 if (banner) {
541 puts("\n");
542 puts(banner);
543 }
544
545 i = linemax; /* force leader and newline */
546 while (*argv != NULL) {
547 len = strlen(*argv) + sl;
548 if (i + len >= linemax) {
549 puts("\n");
550 if (leader)
551 puts(leader);
552 i = ll - sl;
553 } else if (sep)
554 puts(sep);
555 puts(*argv++);
556 i += len;
557 }
558 printf("\n");
559}
560
561static int find_common_prefix(char *argv[])
562{
563 int i, len;
564 char *anchor, *s, *t;
565
566 if (*argv == NULL)
567 return 0;
568
569 /* begin with max */
570 anchor = *argv++;
571 len = strlen(anchor);
572 while ((t = *argv++) != NULL) {
573 s = anchor;
574 for (i = 0; i < len; i++, t++, s++) {
575 if (*t != *s)
576 break;
577 }
578 len = s - anchor;
579 }
580 return len;
581}
582
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200583static char tmp_buf[CONFIG_SYS_CBSIZE]; /* copy of console I/O buffer */
wdenk3902d702004-04-15 18:22:41 +0000584
585int cmd_auto_complete(const char *const prompt, char *buf, int *np, int *colp)
586{
587 int n = *np, col = *colp;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200588 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
wdenk3902d702004-04-15 18:22:41 +0000589 char *cmdv[20];
590 char *s, *t;
591 const char *sep;
592 int i, j, k, len, seplen, argc;
593 int cnt;
594 char last_char;
595
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200596 if (strcmp(prompt, CONFIG_SYS_PROMPT) != 0)
wdenk3902d702004-04-15 18:22:41 +0000597 return 0; /* not in normal console */
598
599 cnt = strlen(buf);
600 if (cnt >= 1)
601 last_char = buf[cnt - 1];
602 else
603 last_char = '\0';
604
605 /* copy to secondary buffer which will be affected */
606 strcpy(tmp_buf, buf);
607
608 /* separate into argv */
609 argc = make_argv(tmp_buf, sizeof(argv)/sizeof(argv[0]), argv);
610
611 /* do the completion and return the possible completions */
612 i = complete_cmdv(argc, argv, last_char, sizeof(cmdv)/sizeof(cmdv[0]), cmdv);
613
614 /* no match; bell and out */
615 if (i == 0) {
616 if (argc > 1) /* allow tab for non command */
617 return 0;
618 putc('\a');
619 return 1;
620 }
621
622 s = NULL;
623 len = 0;
624 sep = NULL;
625 seplen = 0;
626 if (i == 1) { /* one match; perfect */
627 k = strlen(argv[argc - 1]);
628 s = cmdv[0] + k;
629 len = strlen(s);
630 sep = " ";
631 seplen = 1;
632 } else if (i > 1 && (j = find_common_prefix(cmdv)) != 0) { /* more */
633 k = strlen(argv[argc - 1]);
634 j -= k;
635 if (j > 0) {
636 s = cmdv[0] + k;
637 len = j;
638 }
639 }
640
641 if (s != NULL) {
642 k = len + seplen;
643 /* make sure it fits */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200644 if (n + k >= CONFIG_SYS_CBSIZE - 2) {
wdenk3902d702004-04-15 18:22:41 +0000645 putc('\a');
646 return 1;
647 }
648
649 t = buf + cnt;
650 for (i = 0; i < len; i++)
651 *t++ = *s++;
652 if (sep != NULL)
653 for (i = 0; i < seplen; i++)
654 *t++ = sep[i];
655 *t = '\0';
656 n += k;
657 col += k;
658 puts(t - k);
659 if (sep == NULL)
660 putc('\a');
661 *np = n;
662 *colp = col;
663 } else {
664 print_argv(NULL, " ", " ", 78, cmdv);
665
666 puts(prompt);
667 puts(buf);
668 }
669 return 1;
670}
671
672#endif
Jean-Christophe PLAGNIOL-VILLARD82290f22008-09-10 22:48:05 +0200673
674#ifdef CMD_DATA_SIZE
675int cmd_get_data_size(char* arg, int default_size)
676{
677 /* Check for a size specification .b, .w or .l.
678 */
679 int len = strlen(arg);
680 if (len > 2 && arg[len-2] == '.') {
681 switch(arg[len-1]) {
682 case 'b':
683 return 1;
684 case 'w':
685 return 2;
686 case 'l':
687 return 4;
688 case 's':
689 return -2;
690 default:
691 return -1;
692 }
693 }
694 return default_size;
695}
696#endif