blob: ad2a386c86586a62fc40bdcfaec530dd7576d107 [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk501090a2006-07-21 11:33:45 +02005 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
wdenkc6097192002-11-03 00:24:07 +00009 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
25 * MA 02111-1307 USA
26 */
27
wdenka6c7ad22002-12-03 21:28:10 +000028/* #define DEBUG */
29
wdenkc6097192002-11-03 00:24:07 +000030#include <common.h>
31#include <watchdog.h>
32#include <command.h>
Wolfgang Denkd9631ec2005-09-30 15:18:23 +020033#ifdef CONFIG_MODEM_SUPPORT
34#include <malloc.h> /* for free() prototype */
35#endif
wdenkc6097192002-11-03 00:24:07 +000036
37#ifdef CFG_HUSH_PARSER
38#include <hush.h>
39#endif
40
wdenkbdccc4f2003-08-05 17:43:17 +000041#include <post.h>
42
Wolfgang Denkd87080b2006-03-31 18:32:53 +020043#ifdef CONFIG_SILENT_CONSOLE
44DECLARE_GLOBAL_DATA_PTR;
45#endif
46
Heiko Schocherfad63402007-07-13 09:54:17 +020047/*
48 * Board-specific Platform code can reimplement show_boot_progress () if needed
49 */
50void inline __show_boot_progress (int val) {}
51void inline show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
52
wdenk8bde7f72003-06-27 21:31:46 +000053#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
54extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
55#endif
56
57extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58
59
wdenkc6097192002-11-03 00:24:07 +000060#define MAX_DELAY_STOP_STR 32
61
wdenkc6097192002-11-03 00:24:07 +000062#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
63static int abortboot(int);
64#endif
65
66#undef DEBUG_PARSER
67
68char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
69
Stefan Roese3ca91222006-07-27 16:11:19 +020070#ifndef CONFIG_CMDLINE_EDITING
71static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
wdenkc6097192002-11-03 00:24:07 +000072static char erase_seq[] = "\b \b"; /* erase sequence */
73static char tab_seq[] = " "; /* used to expand TABs */
Stefan Roese3ca91222006-07-27 16:11:19 +020074#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +000075
76#ifdef CONFIG_BOOT_RETRY_TIME
77static uint64_t endtime = 0; /* must be set, default is instant timeout */
78static int retry_time = -1; /* -1 so can call readline before main_loop */
79#endif
80
81#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
82
83#ifndef CONFIG_BOOT_RETRY_MIN
84#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85#endif
86
87#ifdef CONFIG_MODEM_SUPPORT
88int do_mdm_init = 0;
89extern void mdm_init(void); /* defined in board.c */
90#endif
91
92/***************************************************************************
93 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
94 * returns: 0 - no key string, allow autoboot
95 * 1 - got key string, abort
96 */
97#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
98# if defined(CONFIG_AUTOBOOT_KEYED)
99static __inline__ int abortboot(int bootdelay)
100{
101 int abort = 0;
102 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +0200103 struct {
wdenkc6097192002-11-03 00:24:07 +0000104 char* str;
105 u_int len;
106 int retry;
107 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200108 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
113 };
114
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
118 u_int i;
119
120# ifdef CONFIG_AUTOBOOT_PROMPT
Ladislav Michlada4d402007-04-25 16:01:26 +0200121 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000122# endif
123
124# ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127# endif
128# ifdef CONFIG_AUTOBOOT_DELAY_STR2
129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR
133 if (delaykey[2].str == NULL)
134 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
135# endif
136# ifdef CONFIG_AUTOBOOT_STOP_STR2
137 if (delaykey[3].str == NULL)
138 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
139# endif
140
141 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
142 delaykey[i].len = delaykey[i].str == NULL ?
143 0 : strlen (delaykey[i].str);
144 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
145 MAX_DELAY_STOP_STR : delaykey[i].len;
146
147 presskey_max = presskey_max > delaykey[i].len ?
148 presskey_max : delaykey[i].len;
149
150# if DEBUG_BOOTKEYS
151 printf("%s key:<%s>\n",
152 delaykey[i].retry ? "delay" : "stop",
153 delaykey[i].str ? delaykey[i].str : "NULL");
154# endif
155 }
156
157 /* In order to keep up with incoming data, check timeout only
158 * when catch up.
159 */
160 while (!abort && get_ticks() <= etime) {
161 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
162 if (delaykey[i].len > 0 &&
163 presskey_len >= delaykey[i].len &&
164 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000165 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000166 delaykey[i].len) == 0) {
167# if DEBUG_BOOTKEYS
168 printf("got %skey\n",
169 delaykey[i].retry ? "delay" : "stop");
170# endif
171
172# ifdef CONFIG_BOOT_RETRY_TIME
173 /* don't retry auto boot */
174 if (! delaykey[i].retry)
175 retry_time = -1;
176# endif
177 abort = 1;
178 }
179 }
180
181 if (tstc()) {
182 if (presskey_len < presskey_max) {
183 presskey [presskey_len ++] = getc();
184 }
185 else {
186 for (i = 0; i < presskey_max - 1; i ++)
187 presskey [i] = presskey [i + 1];
188
189 presskey [i] = getc();
190 }
191 }
192 }
193# if DEBUG_BOOTKEYS
194 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200195 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000196# endif
197
dzu8cb81432003-10-24 13:14:45 +0000198#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200199 if (abort)
200 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000201#endif
202
wdenkc6097192002-11-03 00:24:07 +0000203 return abort;
204}
205
206# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
207
wdenkc7de8292002-11-19 11:04:11 +0000208#ifdef CONFIG_MENUKEY
209static int menukey = 0;
210#endif
211
wdenkc6097192002-11-03 00:24:07 +0000212static __inline__ int abortboot(int bootdelay)
213{
214 int abort = 0;
215
wdenkc7de8292002-11-19 11:04:11 +0000216#ifdef CONFIG_MENUPROMPT
217 printf(CONFIG_MENUPROMPT, bootdelay);
218#else
wdenkc6097192002-11-03 00:24:07 +0000219 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000220#endif
wdenkc6097192002-11-03 00:24:07 +0000221
222#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000223 /*
224 * Check if key already pressed
225 * Don't check if bootdelay < 0
226 */
wdenkc6097192002-11-03 00:24:07 +0000227 if (bootdelay >= 0) {
228 if (tstc()) { /* we got a key press */
229 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000230 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200231 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000232 }
wdenk8bde7f72003-06-27 21:31:46 +0000233 }
wdenkc6097192002-11-03 00:24:07 +0000234#endif
235
wdenkf72da342003-10-10 10:05:42 +0000236 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000237 int i;
238
239 --bootdelay;
240 /* delay 100 * 10ms */
241 for (i=0; !abort && i<100; ++i) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
wdenkc6097192002-11-03 00:24:07 +0000248 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000249# endif
wdenkc6097192002-11-03 00:24:07 +0000250 break;
251 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200252 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000253 }
254
Ladislav Michlada4d402007-04-25 16:01:26 +0200255 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000256 }
257
Ladislav Michlada4d402007-04-25 16:01:26 +0200258 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000259
wdenkf72da342003-10-10 10:05:42 +0000260#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000263#endif
264
wdenkc6097192002-11-03 00:24:07 +0000265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
268#endif /* CONFIG_BOOTDELAY >= 0 */
269
270/****************************************************************************/
271
272void main_loop (void)
273{
274#ifndef CFG_HUSH_PARSER
275 static char lastcommand[CFG_CBSIZE] = { 0, };
276 int len;
277 int rc = 1;
278 int flag;
279#endif
280
281#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
282 char *s;
283 int bootdelay;
284#endif
285#ifdef CONFIG_PREBOOT
286 char *p;
287#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000288#ifdef CONFIG_BOOTCOUNT_LIMIT
289 unsigned long bootcount = 0;
290 unsigned long bootlimit = 0;
291 char *bcs;
292 char bcs_set[16];
293#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000294
295#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
296 ulong bmp = 0; /* default bitmap */
297 extern int trab_vfd (ulong bitmap);
298
299#ifdef CONFIG_MODEM_SUPPORT
300 if (do_mdm_init)
301 bmp = 1; /* alternate bitmap */
302#endif
303 trab_vfd (bmp);
304#endif /* CONFIG_VFD && VFD_TEST_LOGO */
305
wdenkbdccc4f2003-08-05 17:43:17 +0000306#ifdef CONFIG_BOOTCOUNT_LIMIT
307 bootcount = bootcount_load();
308 bootcount++;
309 bootcount_store (bootcount);
310 sprintf (bcs_set, "%lu", bootcount);
311 setenv ("bootcount", bcs_set);
312 bcs = getenv ("bootlimit");
313 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
314#endif /* CONFIG_BOOTCOUNT_LIMIT */
315
wdenkc6097192002-11-03 00:24:07 +0000316#ifdef CONFIG_MODEM_SUPPORT
317 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
318 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200319 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000320 setenv ("preboot", str); /* set or delete definition */
321 if (str != NULL)
322 free (str);
323 mdm_init(); /* wait for modem connection */
324 }
325#endif /* CONFIG_MODEM_SUPPORT */
326
stroese05875972003-04-04 15:44:49 +0000327#ifdef CONFIG_VERSION_VARIABLE
328 {
329 extern char version_string[];
stroese05875972003-04-04 15:44:49 +0000330
stroese155cb012003-07-11 08:00:33 +0000331 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000332 }
333#endif /* CONFIG_VERSION_VARIABLE */
334
wdenkc6097192002-11-03 00:24:07 +0000335#ifdef CFG_HUSH_PARSER
336 u_boot_hush_start ();
337#endif
338
wdenk04a85b32004-04-15 18:22:41 +0000339#ifdef CONFIG_AUTO_COMPLETE
340 install_auto_complete();
341#endif
342
wdenkc6097192002-11-03 00:24:07 +0000343#ifdef CONFIG_PREBOOT
344 if ((p = getenv ("preboot")) != NULL) {
345# ifdef CONFIG_AUTOBOOT_KEYED
346 int prev = disable_ctrlc(1); /* disable Control C checking */
347# endif
348
349# ifndef CFG_HUSH_PARSER
350 run_command (p, 0);
351# else
352 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
353 FLAG_EXIT_FROM_LOOP);
354# endif
355
356# ifdef CONFIG_AUTOBOOT_KEYED
357 disable_ctrlc(prev); /* restore Control C checking */
358# endif
359 }
360#endif /* CONFIG_PREBOOT */
361
362#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
363 s = getenv ("bootdelay");
364 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
365
wdenka6c7ad22002-12-03 21:28:10 +0000366 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000367
368# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000369 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000370# endif /* CONFIG_BOOT_RETRY_TIME */
371
wdenkbdccc4f2003-08-05 17:43:17 +0000372#ifdef CONFIG_BOOTCOUNT_LIMIT
373 if (bootlimit && (bootcount > bootlimit)) {
374 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
375 (unsigned)bootlimit);
376 s = getenv ("altbootcmd");
377 }
378 else
379#endif /* CONFIG_BOOTCOUNT_LIMIT */
380 s = getenv ("bootcmd");
wdenka6c7ad22002-12-03 21:28:10 +0000381
382 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
383
wdenkc6097192002-11-03 00:24:07 +0000384 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
385# ifdef CONFIG_AUTOBOOT_KEYED
386 int prev = disable_ctrlc(1); /* disable Control C checking */
387# endif
388
389# ifndef CFG_HUSH_PARSER
390 run_command (s, 0);
391# else
392 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
393 FLAG_EXIT_FROM_LOOP);
394# endif
395
396# ifdef CONFIG_AUTOBOOT_KEYED
397 disable_ctrlc(prev); /* restore Control C checking */
398# endif
399 }
wdenkc7de8292002-11-19 11:04:11 +0000400
401# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000402 if (menukey == CONFIG_MENUKEY) {
wdenkc7de8292002-11-19 11:04:11 +0000403 s = getenv("menucmd");
wdenka6c7ad22002-12-03 21:28:10 +0000404 if (s) {
wdenkc7de8292002-11-19 11:04:11 +0000405# ifndef CFG_HUSH_PARSER
wdenk6225c5d2005-01-09 23:33:49 +0000406 run_command (s, 0);
wdenkc7de8292002-11-19 11:04:11 +0000407# else
408 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
409 FLAG_EXIT_FROM_LOOP);
410# endif
411 }
412 }
413#endif /* CONFIG_MENUKEY */
wdenkc6097192002-11-03 00:24:07 +0000414#endif /* CONFIG_BOOTDELAY */
415
wdenkc7de8292002-11-19 11:04:11 +0000416#ifdef CONFIG_AMIGAONEG3SE
417 {
418 extern void video_banner(void);
419 video_banner();
420 }
421#endif
422
wdenkc6097192002-11-03 00:24:07 +0000423 /*
424 * Main Loop for Monitor Command Processing
425 */
426#ifdef CFG_HUSH_PARSER
427 parse_file_outer();
428 /* This point is never reached */
429 for (;;);
430#else
431 for (;;) {
432#ifdef CONFIG_BOOT_RETRY_TIME
433 if (rc >= 0) {
434 /* Saw enough of a valid command to
435 * restart the timeout.
436 */
437 reset_cmd_timeout();
438 }
439#endif
440 len = readline (CFG_PROMPT);
441
442 flag = 0; /* assume no special flags for now */
443 if (len > 0)
444 strcpy (lastcommand, console_buffer);
445 else if (len == 0)
446 flag |= CMD_FLAG_REPEAT;
447#ifdef CONFIG_BOOT_RETRY_TIME
448 else if (len == -2) {
449 /* -2 means timed out, retry autoboot
450 */
wdenk4b9206e2004-03-23 22:14:11 +0000451 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000452# ifdef CONFIG_RESET_TO_RETRY
453 /* Reinit board to run initialization code again */
454 do_reset (NULL, 0, 0, NULL);
455# else
456 return; /* retry autoboot */
457# endif
458 }
459#endif
460
461 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000462 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000463 else
464 rc = run_command (lastcommand, flag);
465
466 if (rc <= 0) {
467 /* invalid command or not repeatable, forget it */
468 lastcommand[0] = 0;
469 }
470 }
471#endif /*CFG_HUSH_PARSER*/
472}
473
wdenk6dd652f2003-06-19 23:40:20 +0000474#ifdef CONFIG_BOOT_RETRY_TIME
475/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200476 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000477 */
478void init_cmd_timeout(void)
479{
480 char *s = getenv ("bootretry");
481
482 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000483 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000484 else
485 retry_time = CONFIG_BOOT_RETRY_TIME;
486
487 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
488 retry_time = CONFIG_BOOT_RETRY_MIN;
489}
490
wdenkc6097192002-11-03 00:24:07 +0000491/***************************************************************************
492 * reset command line timeout to retry_time seconds
493 */
wdenkc6097192002-11-03 00:24:07 +0000494void reset_cmd_timeout(void)
495{
496 endtime = endtick(retry_time);
497}
498#endif
499
Wolfgang Denk501090a2006-07-21 11:33:45 +0200500#ifdef CONFIG_CMDLINE_EDITING
501
502/*
503 * cmdline-editing related codes from vivi.
504 * Author: Janghoon Lyu <nandy@mizi.com>
505 */
506
Wolfgang Denk501090a2006-07-21 11:33:45 +0200507#define putnstr(str,n) do { \
508 printf ("%.*s", n, str); \
509 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200510
511#define CTL_CH(c) ((c) - 'a' + 1)
512
513#define MAX_CMDBUF_SIZE 256
514
515#define CTL_BACKSPACE ('\b')
516#define DEL ((char)255)
517#define DEL7 ((char)127)
518#define CREAD_HIST_CHAR ('!')
519
520#define getcmd_putch(ch) putc(ch)
521#define getcmd_getch() getc()
522#define getcmd_cbeep() getcmd_putch('\a')
523
524#define HIST_MAX 20
525#define HIST_SIZE MAX_CMDBUF_SIZE
526
527static int hist_max = 0;
528static int hist_add_idx = 0;
529static int hist_cur = -1;
530unsigned hist_num = 0;
531
532char* hist_list[HIST_MAX];
533char hist_lines[HIST_MAX][HIST_SIZE];
534
535#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
536
537static void hist_init(void)
538{
539 int i;
540
541 hist_max = 0;
542 hist_add_idx = 0;
543 hist_cur = -1;
544 hist_num = 0;
545
546 for (i = 0; i < HIST_MAX; i++) {
547 hist_list[i] = hist_lines[i];
548 hist_list[i][0] = '\0';
549 }
550}
551
552static void cread_add_to_hist(char *line)
553{
554 strcpy(hist_list[hist_add_idx], line);
555
556 if (++hist_add_idx >= HIST_MAX)
557 hist_add_idx = 0;
558
559 if (hist_add_idx > hist_max)
560 hist_max = hist_add_idx;
561
562 hist_num++;
563}
564
565static char* hist_prev(void)
566{
567 char *ret;
568 int old_cur;
569
570 if (hist_cur < 0)
571 return NULL;
572
573 old_cur = hist_cur;
574 if (--hist_cur < 0)
575 hist_cur = hist_max;
576
577 if (hist_cur == hist_add_idx) {
578 hist_cur = old_cur;
579 ret = NULL;
580 } else
581 ret = hist_list[hist_cur];
582
583 return (ret);
584}
585
586static char* hist_next(void)
587{
588 char *ret;
589
590 if (hist_cur < 0)
591 return NULL;
592
593 if (hist_cur == hist_add_idx)
594 return NULL;
595
596 if (++hist_cur > hist_max)
597 hist_cur = 0;
598
599 if (hist_cur == hist_add_idx) {
600 ret = "";
601 } else
602 ret = hist_list[hist_cur];
603
604 return (ret);
605}
606
Stefan Roese3ca91222006-07-27 16:11:19 +0200607#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200608static void cread_print_hist_list(void)
609{
610 int i;
611 unsigned long n;
612
613 n = hist_num - hist_max;
614
615 i = hist_add_idx + 1;
616 while (1) {
617 if (i > hist_max)
618 i = 0;
619 if (i == hist_add_idx)
620 break;
621 printf("%s\n", hist_list[i]);
622 n++;
623 i++;
624 }
625}
Stefan Roese3ca91222006-07-27 16:11:19 +0200626#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200627
628#define BEGINNING_OF_LINE() { \
629 while (num) { \
630 getcmd_putch(CTL_BACKSPACE); \
631 num--; \
632 } \
633}
634
635#define ERASE_TO_EOL() { \
636 if (num < eol_num) { \
637 int tmp; \
638 for (tmp = num; tmp < eol_num; tmp++) \
639 getcmd_putch(' '); \
640 while (tmp-- > num) \
641 getcmd_putch(CTL_BACKSPACE); \
642 eol_num = num; \
643 } \
644}
645
646#define REFRESH_TO_EOL() { \
647 if (num < eol_num) { \
648 wlen = eol_num - num; \
649 putnstr(buf + num, wlen); \
650 num = eol_num; \
651 } \
652}
653
654static void cread_add_char(char ichar, int insert, unsigned long *num,
655 unsigned long *eol_num, char *buf, unsigned long len)
656{
657 unsigned long wlen;
658
659 /* room ??? */
660 if (insert || *num == *eol_num) {
661 if (*eol_num > len - 1) {
662 getcmd_cbeep();
663 return;
664 }
665 (*eol_num)++;
666 }
667
668 if (insert) {
669 wlen = *eol_num - *num;
670 if (wlen > 1) {
671 memmove(&buf[*num+1], &buf[*num], wlen-1);
672 }
673
674 buf[*num] = ichar;
675 putnstr(buf + *num, wlen);
676 (*num)++;
677 while (--wlen) {
678 getcmd_putch(CTL_BACKSPACE);
679 }
680 } else {
681 /* echo the character */
682 wlen = 1;
683 buf[*num] = ichar;
684 putnstr(buf + *num, wlen);
685 (*num)++;
686 }
687}
688
689static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
690 unsigned long *eol_num, char *buf, unsigned long len)
691{
692 while (strsize--) {
693 cread_add_char(*str, insert, num, eol_num, buf, len);
694 str++;
695 }
696}
697
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100698static int cread_line(const char *const prompt, char *buf, unsigned int *len)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200699{
700 unsigned long num = 0;
701 unsigned long eol_num = 0;
702 unsigned long rlen;
703 unsigned long wlen;
704 char ichar;
705 int insert = 1;
706 int esc_len = 0;
707 int rc = 0;
708 char esc_save[8];
709
710 while (1) {
711 rlen = 1;
712 ichar = getcmd_getch();
713
714 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200715 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200716 break;
717 }
718
719 /*
720 * handle standard linux xterm esc sequences for arrow key, etc.
721 */
722 if (esc_len != 0) {
723 if (esc_len == 1) {
724 if (ichar == '[') {
725 esc_save[esc_len] = ichar;
726 esc_len = 2;
727 } else {
728 cread_add_str(esc_save, esc_len, insert,
729 &num, &eol_num, buf, *len);
730 esc_len = 0;
731 }
732 continue;
733 }
734
735 switch (ichar) {
736
737 case 'D': /* <- key */
738 ichar = CTL_CH('b');
739 esc_len = 0;
740 break;
741 case 'C': /* -> key */
742 ichar = CTL_CH('f');
743 esc_len = 0;
744 break; /* pass off to ^F handler */
745 case 'H': /* Home key */
746 ichar = CTL_CH('a');
747 esc_len = 0;
748 break; /* pass off to ^A handler */
749 case 'A': /* up arrow */
750 ichar = CTL_CH('p');
751 esc_len = 0;
752 break; /* pass off to ^P handler */
753 case 'B': /* down arrow */
754 ichar = CTL_CH('n');
755 esc_len = 0;
756 break; /* pass off to ^N handler */
757 default:
758 esc_save[esc_len++] = ichar;
759 cread_add_str(esc_save, esc_len, insert,
760 &num, &eol_num, buf, *len);
761 esc_len = 0;
762 continue;
763 }
764 }
765
766 switch (ichar) {
767 case 0x1b:
768 if (esc_len == 0) {
769 esc_save[esc_len] = ichar;
770 esc_len = 1;
771 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200772 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200773 esc_len = 0;
774 }
775 break;
776
777 case CTL_CH('a'):
778 BEGINNING_OF_LINE();
779 break;
780 case CTL_CH('c'): /* ^C - break */
781 *buf = '\0'; /* discard input */
782 return (-1);
783 case CTL_CH('f'):
784 if (num < eol_num) {
785 getcmd_putch(buf[num]);
786 num++;
787 }
788 break;
789 case CTL_CH('b'):
790 if (num) {
791 getcmd_putch(CTL_BACKSPACE);
792 num--;
793 }
794 break;
795 case CTL_CH('d'):
796 if (num < eol_num) {
797 wlen = eol_num - num - 1;
798 if (wlen) {
799 memmove(&buf[num], &buf[num+1], wlen);
800 putnstr(buf + num, wlen);
801 }
802
803 getcmd_putch(' ');
804 do {
805 getcmd_putch(CTL_BACKSPACE);
806 } while (wlen--);
807 eol_num--;
808 }
809 break;
810 case CTL_CH('k'):
811 ERASE_TO_EOL();
812 break;
813 case CTL_CH('e'):
814 REFRESH_TO_EOL();
815 break;
816 case CTL_CH('o'):
817 insert = !insert;
818 break;
819 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100820 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200821 BEGINNING_OF_LINE();
822 ERASE_TO_EOL();
823 break;
824 case DEL:
825 case DEL7:
826 case 8:
827 if (num) {
828 wlen = eol_num - num;
829 num--;
830 memmove(&buf[num], &buf[num+1], wlen);
831 getcmd_putch(CTL_BACKSPACE);
832 putnstr(buf + num, wlen);
833 getcmd_putch(' ');
834 do {
835 getcmd_putch(CTL_BACKSPACE);
836 } while (wlen--);
837 eol_num--;
838 }
839 break;
840 case CTL_CH('p'):
841 case CTL_CH('n'):
842 {
843 char * hline;
844
845 esc_len = 0;
846
847 if (ichar == CTL_CH('p'))
848 hline = hist_prev();
849 else
850 hline = hist_next();
851
852 if (!hline) {
853 getcmd_cbeep();
854 continue;
855 }
856
857 /* nuke the current line */
858 /* first, go home */
859 BEGINNING_OF_LINE();
860
861 /* erase to end of line */
862 ERASE_TO_EOL();
863
864 /* copy new line into place and display */
865 strcpy(buf, hline);
866 eol_num = strlen(buf);
867 REFRESH_TO_EOL();
868 continue;
869 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100870#ifdef CONFIG_AUTO_COMPLETE
871 case '\t': {
872 int num2, col;
873
874 /* do not autocomplete when in the middle */
875 if (num < eol_num) {
876 getcmd_cbeep();
877 break;
878 }
879
880 buf[num] = '\0';
881 col = strlen(prompt) + eol_num;
882 num2 = num;
883 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
884 col = num2 - num;
885 num += col;
886 eol_num += col;
887 }
888 break;
889 }
890#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200891 default:
892 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
893 break;
894 }
895 }
896 *len = eol_num;
897 buf[eol_num] = '\0'; /* lose the newline */
898
899 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
900 cread_add_to_hist(buf);
901 hist_cur = hist_add_idx;
902
903 return (rc);
904}
905
906#endif /* CONFIG_CMDLINE_EDITING */
907
wdenkc6097192002-11-03 00:24:07 +0000908/****************************************************************************/
909
910/*
911 * Prompt for input and read a line.
912 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
913 * time out when time goes past endtime (timebase time in ticks).
914 * Return: number of read characters
915 * -1 if break
916 * -2 if timed out
917 */
918int readline (const char *const prompt)
919{
James Yang6636b622008-01-09 11:17:49 -0600920 return readline_into_buffer(prompt, console_buffer);
921}
922
923
924int readline_into_buffer (const char *const prompt, char * buffer)
925{
926 char *p = buffer;
927 char * p_buf = p;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200928#ifdef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200929 unsigned int len=MAX_CMDBUF_SIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200930 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200931 static int initted = 0;
932
933 if (!initted) {
934 hist_init();
935 initted = 1;
936 }
937
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200938 puts (prompt);
Wolfgang Denk501090a2006-07-21 11:33:45 +0200939
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100940 rc = cread_line(prompt, p, &len);
Stefan Roesed8f961b2006-08-07 15:08:44 +0200941 return rc < 0 ? rc : len;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200942#else
wdenkc6097192002-11-03 00:24:07 +0000943 int n = 0; /* buffer index */
944 int plen = 0; /* prompt length */
945 int col; /* output column cnt */
946 char c;
947
948 /* print prompt */
949 if (prompt) {
950 plen = strlen (prompt);
951 puts (prompt);
952 }
953 col = plen;
954
955 for (;;) {
956#ifdef CONFIG_BOOT_RETRY_TIME
957 while (!tstc()) { /* while no incoming data */
958 if (retry_time >= 0 && get_ticks() > endtime)
959 return (-2); /* timed out */
960 }
961#endif
962 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
963
964#ifdef CONFIG_SHOW_ACTIVITY
965 while (!tstc()) {
966 extern void show_activity(int arg);
967 show_activity(0);
968 }
969#endif
970 c = getc();
971
972 /*
973 * Special character handling
974 */
975 switch (c) {
976 case '\r': /* Enter */
977 case '\n':
978 *p = '\0';
979 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -0600980 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +0000981
wdenk27aa8182004-03-23 22:37:33 +0000982 case '\0': /* nul */
983 continue;
984
wdenkc6097192002-11-03 00:24:07 +0000985 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -0600986 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +0000987 return (-1);
988
989 case 0x15: /* ^U - erase line */
990 while (col > plen) {
991 puts (erase_seq);
992 --col;
993 }
James Yang6636b622008-01-09 11:17:49 -0600994 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +0000995 n = 0;
996 continue;
997
Wolfgang Denk1636d1c2007-06-22 23:59:00 +0200998 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -0600999 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001000 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001001 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001002 }
1003 continue;
1004
1005 case 0x08: /* ^H - backspace */
1006 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001007 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001008 continue;
1009
1010 default:
1011 /*
1012 * Must be a normal character then
1013 */
1014 if (n < CFG_CBSIZE-2) {
1015 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001016#ifdef CONFIG_AUTO_COMPLETE
1017 /* if auto completion triggered just continue */
1018 *p = '\0';
1019 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001020 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001021 continue;
1022 }
1023#endif
wdenkc6097192002-11-03 00:24:07 +00001024 puts (tab_seq+(col&07));
1025 col += 8 - (col&07);
1026 } else {
1027 ++col; /* echo input */
1028 putc (c);
1029 }
1030 *p++ = c;
1031 ++n;
1032 } else { /* Buffer full */
1033 putc ('\a');
1034 }
1035 }
1036 }
Wolfgang Denk501090a2006-07-21 11:33:45 +02001037#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001038}
1039
1040/****************************************************************************/
1041
Stefan Roese3ca91222006-07-27 16:11:19 +02001042#ifndef CONFIG_CMDLINE_EDITING
wdenkc6097192002-11-03 00:24:07 +00001043static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1044{
1045 char *s;
1046
1047 if (*np == 0) {
1048 return (p);
1049 }
1050
1051 if (*(--p) == '\t') { /* will retype the whole line */
1052 while (*colp > plen) {
1053 puts (erase_seq);
1054 (*colp)--;
1055 }
1056 for (s=buffer; s<p; ++s) {
1057 if (*s == '\t') {
1058 puts (tab_seq+((*colp) & 07));
1059 *colp += 8 - ((*colp) & 07);
1060 } else {
1061 ++(*colp);
1062 putc (*s);
1063 }
1064 }
1065 } else {
1066 puts (erase_seq);
1067 (*colp)--;
1068 }
1069 (*np)--;
1070 return (p);
1071}
Stefan Roese3ca91222006-07-27 16:11:19 +02001072#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001073
1074/****************************************************************************/
1075
1076int parse_line (char *line, char *argv[])
1077{
1078 int nargs = 0;
1079
1080#ifdef DEBUG_PARSER
1081 printf ("parse_line: \"%s\"\n", line);
1082#endif
1083 while (nargs < CFG_MAXARGS) {
1084
1085 /* skip any white space */
1086 while ((*line == ' ') || (*line == '\t')) {
1087 ++line;
1088 }
1089
1090 if (*line == '\0') { /* end of line, no more args */
1091 argv[nargs] = NULL;
1092#ifdef DEBUG_PARSER
1093 printf ("parse_line: nargs=%d\n", nargs);
1094#endif
1095 return (nargs);
1096 }
1097
1098 argv[nargs++] = line; /* begin of argument string */
1099
1100 /* find end of string */
1101 while (*line && (*line != ' ') && (*line != '\t')) {
1102 ++line;
1103 }
1104
1105 if (*line == '\0') { /* end of line, no more args */
1106 argv[nargs] = NULL;
1107#ifdef DEBUG_PARSER
1108 printf ("parse_line: nargs=%d\n", nargs);
1109#endif
1110 return (nargs);
1111 }
1112
1113 *line++ = '\0'; /* terminate current arg */
1114 }
1115
1116 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1117
1118#ifdef DEBUG_PARSER
1119 printf ("parse_line: nargs=%d\n", nargs);
1120#endif
1121 return (nargs);
1122}
1123
1124/****************************************************************************/
1125
1126static void process_macros (const char *input, char *output)
1127{
1128 char c, prev;
1129 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001130 int inputcnt = strlen (input);
wdenkc6097192002-11-03 00:24:07 +00001131 int outputcnt = CFG_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001132 int state = 0; /* 0 = waiting for '$' */
1133
1134 /* 1 = waiting for '(' or '{' */
1135 /* 2 = waiting for ')' or '}' */
1136 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001137#ifdef DEBUG_PARSER
1138 char *output_start = output;
1139
Wolfgang Denk19973b62006-10-28 00:38:39 +02001140 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1141 input);
wdenkc6097192002-11-03 00:24:07 +00001142#endif
1143
Wolfgang Denk19973b62006-10-28 00:38:39 +02001144 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001145
1146 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001147 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001148 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001149
Wolfgang Denk19973b62006-10-28 00:38:39 +02001150 if (state != 3) {
1151 /* remove one level of escape characters */
1152 if ((c == '\\') && (prev != '\\')) {
1153 if (inputcnt-- == 0)
1154 break;
1155 prev = c;
1156 c = *input++;
1157 }
wdenka25f8622003-01-02 23:57:29 +00001158 }
wdenkc6097192002-11-03 00:24:07 +00001159
Wolfgang Denk19973b62006-10-28 00:38:39 +02001160 switch (state) {
1161 case 0: /* Waiting for (unescaped) $ */
1162 if ((c == '\'') && (prev != '\\')) {
1163 state = 3;
1164 break;
1165 }
1166 if ((c == '$') && (prev != '\\')) {
1167 state++;
1168 } else {
wdenkc6097192002-11-03 00:24:07 +00001169 *(output++) = c;
1170 outputcnt--;
1171 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001172 break;
1173 case 1: /* Waiting for ( */
1174 if (c == '(' || c == '{') {
1175 state++;
1176 varname_start = input;
1177 } else {
1178 state = 0;
1179 *(output++) = '$';
1180 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001181
Wolfgang Denk19973b62006-10-28 00:38:39 +02001182 if (outputcnt) {
1183 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001184 outputcnt--;
1185 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001186 }
1187 break;
1188 case 2: /* Waiting for ) */
1189 if (c == ')' || c == '}') {
1190 int i;
1191 char envname[CFG_CBSIZE], *envval;
1192 int envcnt = input - varname_start - 1; /* Varname # of chars */
1193
1194 /* Get the varname */
1195 for (i = 0; i < envcnt; i++) {
1196 envname[i] = varname_start[i];
1197 }
1198 envname[i] = 0;
1199
1200 /* Get its value */
1201 envval = getenv (envname);
1202
1203 /* Copy into the line if it exists */
1204 if (envval != NULL)
1205 while ((*envval) && outputcnt) {
1206 *(output++) = *(envval++);
1207 outputcnt--;
1208 }
1209 /* Look for another '$' */
1210 state = 0;
1211 }
1212 break;
1213 case 3: /* Waiting for ' */
1214 if ((c == '\'') && (prev != '\\')) {
1215 state = 0;
1216 } else {
1217 *(output++) = c;
1218 outputcnt--;
1219 }
1220 break;
wdenkc6097192002-11-03 00:24:07 +00001221 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001222 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001223 }
1224
1225 if (outputcnt)
1226 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001227 else
1228 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001229
1230#ifdef DEBUG_PARSER
1231 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001232 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001233#endif
1234}
1235
1236/****************************************************************************
1237 * returns:
1238 * 1 - command executed, repeatable
1239 * 0 - command executed but not repeatable, interrupted commands are
1240 * always considered not repeatable
1241 * -1 - not executed (unrecognized, bootd recursion or too many args)
1242 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1243 * considered unrecognized)
1244 *
1245 * WARNING:
1246 *
1247 * We must create a temporary copy of the command since the command we get
1248 * may be the result from getenv(), which returns a pointer directly to
1249 * the environment data, which may change magicly when the command we run
1250 * creates or modifies environment variables (like "bootp" does).
1251 */
1252
1253int run_command (const char *cmd, int flag)
1254{
1255 cmd_tbl_t *cmdtp;
1256 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1257 char *token; /* start of token in cmdbuf */
1258 char *sep; /* end of token (separator) in cmdbuf */
1259 char finaltoken[CFG_CBSIZE];
1260 char *str = cmdbuf;
1261 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001262 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001263 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001264 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001265
1266#ifdef DEBUG_PARSER
1267 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1268 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1269 puts ("\"\n");
1270#endif
1271
1272 clear_ctrlc(); /* forget any previous Control C */
1273
1274 if (!cmd || !*cmd) {
1275 return -1; /* empty command */
1276 }
1277
1278 if (strlen(cmd) >= CFG_CBSIZE) {
1279 puts ("## Command too long!\n");
1280 return -1;
1281 }
1282
1283 strcpy (cmdbuf, cmd);
1284
1285 /* Process separators and check for invalid
1286 * repeatable commands
1287 */
1288
1289#ifdef DEBUG_PARSER
1290 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1291#endif
1292 while (*str) {
1293
1294 /*
1295 * Find separator, or string end
1296 * Allow simple escape of ';' by writing "\;"
1297 */
wdenka25f8622003-01-02 23:57:29 +00001298 for (inquotes = 0, sep = str; *sep; sep++) {
1299 if ((*sep=='\'') &&
1300 (*(sep-1) != '\\'))
1301 inquotes=!inquotes;
1302
1303 if (!inquotes &&
1304 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001305 ( sep != str) && /* past string start */
1306 (*(sep-1) != '\\')) /* and NOT escaped */
1307 break;
1308 }
1309
1310 /*
1311 * Limit the token to data between separators
1312 */
1313 token = str;
1314 if (*sep) {
1315 str = sep + 1; /* start of command for next pass */
1316 *sep = '\0';
1317 }
1318 else
1319 str = sep; /* no more commands for next pass */
1320#ifdef DEBUG_PARSER
1321 printf ("token: \"%s\"\n", token);
1322#endif
1323
1324 /* find macros in this token and replace them */
1325 process_macros (token, finaltoken);
1326
1327 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001328 if ((argc = parse_line (finaltoken, argv)) == 0) {
1329 rc = -1; /* no command at all */
1330 continue;
1331 }
wdenkc6097192002-11-03 00:24:07 +00001332
1333 /* Look up command in command table */
1334 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1335 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf07771c2003-05-28 08:06:31 +00001336 rc = -1; /* give up after bad command */
1337 continue;
wdenkc6097192002-11-03 00:24:07 +00001338 }
1339
1340 /* found - check max args */
1341 if (argc > cmdtp->maxargs) {
1342 printf ("Usage:\n%s\n", cmdtp->usage);
wdenkf07771c2003-05-28 08:06:31 +00001343 rc = -1;
1344 continue;
wdenkc6097192002-11-03 00:24:07 +00001345 }
1346
Jon Loeligerc3517f92007-07-08 18:10:08 -05001347#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001348 /* avoid "bootd" recursion */
1349 if (cmdtp->cmd == do_bootd) {
1350#ifdef DEBUG_PARSER
1351 printf ("[%s]\n", finaltoken);
1352#endif
1353 if (flag & CMD_FLAG_BOOTD) {
wdenk4b9206e2004-03-23 22:14:11 +00001354 puts ("'bootd' recursion detected\n");
wdenkf07771c2003-05-28 08:06:31 +00001355 rc = -1;
1356 continue;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001357 } else {
wdenkc6097192002-11-03 00:24:07 +00001358 flag |= CMD_FLAG_BOOTD;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001359 }
wdenkc6097192002-11-03 00:24:07 +00001360 }
Jon Loeliger90253172007-07-10 11:02:44 -05001361#endif
wdenkc6097192002-11-03 00:24:07 +00001362
1363 /* OK - call function to do the command */
1364 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf07771c2003-05-28 08:06:31 +00001365 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001366 }
1367
1368 repeatable &= cmdtp->repeatable;
1369
1370 /* Did the user stop this? */
1371 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001372 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001373 }
1374
wdenkf07771c2003-05-28 08:06:31 +00001375 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001376}
1377
1378/****************************************************************************/
1379
Jon Loeligerc3517f92007-07-08 18:10:08 -05001380#if defined(CONFIG_CMD_RUN)
wdenkc6097192002-11-03 00:24:07 +00001381int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1382{
1383 int i;
wdenkc6097192002-11-03 00:24:07 +00001384
1385 if (argc < 2) {
1386 printf ("Usage:\n%s\n", cmdtp->usage);
1387 return 1;
1388 }
1389
1390 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001391 char *arg;
1392
1393 if ((arg = getenv (argv[i])) == NULL) {
1394 printf ("## Error: \"%s\" not defined\n", argv[i]);
1395 return 1;
1396 }
wdenkc6097192002-11-03 00:24:07 +00001397#ifndef CFG_HUSH_PARSER
wdenk3e386912003-04-05 00:53:31 +00001398 if (run_command (arg, flag) == -1)
1399 return 1;
wdenkc6097192002-11-03 00:24:07 +00001400#else
wdenk3e386912003-04-05 00:53:31 +00001401 if (parse_string_outer(arg,
wdenk7aa78612003-05-03 15:50:43 +00001402 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenk3e386912003-04-05 00:53:31 +00001403 return 1;
wdenkc6097192002-11-03 00:24:07 +00001404#endif
1405 }
wdenk3e386912003-04-05 00:53:31 +00001406 return 0;
wdenkc6097192002-11-03 00:24:07 +00001407}
Jon Loeliger90253172007-07-10 11:02:44 -05001408#endif