blob: cc4b50f615ab070c806773cfa57661f1dd889bbe [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 Denk4c9ca782006-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
wdenk9dd2b882002-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 Denkb0723792005-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
wdenk0a658552003-08-05 17:43:17 +000041#include <post.h>
42
Wolfgang Denk6405a152006-03-31 18:32:53 +020043#ifdef CONFIG_SILENT_CONSOLE
44DECLARE_GLOBAL_DATA_PTR;
45#endif
46
wdenk57b2d802003-06-27 21:31:46 +000047#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
48extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
49#endif
50
51extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
52
53
wdenkc6097192002-11-03 00:24:07 +000054#define MAX_DELAY_STOP_STR 32
55
wdenkc6097192002-11-03 00:24:07 +000056static int parse_line (char *, char *[]);
57#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
58static int abortboot(int);
59#endif
60
61#undef DEBUG_PARSER
62
63char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
64
Stefan Roese59ff5a52006-07-27 16:11:19 +020065#ifndef CONFIG_CMDLINE_EDITING
66static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
wdenkc6097192002-11-03 00:24:07 +000067static char erase_seq[] = "\b \b"; /* erase sequence */
68static char tab_seq[] = " "; /* used to expand TABs */
Stefan Roese59ff5a52006-07-27 16:11:19 +020069#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +000070
71#ifdef CONFIG_BOOT_RETRY_TIME
72static uint64_t endtime = 0; /* must be set, default is instant timeout */
73static int retry_time = -1; /* -1 so can call readline before main_loop */
74#endif
75
76#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
77
78#ifndef CONFIG_BOOT_RETRY_MIN
79#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
80#endif
81
82#ifdef CONFIG_MODEM_SUPPORT
83int do_mdm_init = 0;
84extern void mdm_init(void); /* defined in board.c */
85#endif
86
87/***************************************************************************
88 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
89 * returns: 0 - no key string, allow autoboot
90 * 1 - got key string, abort
91 */
92#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
93# if defined(CONFIG_AUTOBOOT_KEYED)
94static __inline__ int abortboot(int bootdelay)
95{
96 int abort = 0;
97 uint64_t etime = endtick(bootdelay);
Wolfgang Denk9c82c682006-10-28 00:38:39 +020098 struct {
wdenkc6097192002-11-03 00:24:07 +000099 char* str;
100 u_int len;
101 int retry;
102 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200103 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000104 { str: getenv ("bootdelaykey"), retry: 1 },
105 { str: getenv ("bootdelaykey2"), retry: 1 },
106 { str: getenv ("bootstopkey"), retry: 0 },
107 { str: getenv ("bootstopkey2"), retry: 0 },
108 };
109
110 char presskey [MAX_DELAY_STOP_STR];
111 u_int presskey_len = 0;
112 u_int presskey_max = 0;
113 u_int i;
114
dzu6605c842003-10-24 13:14:45 +0000115#ifdef CONFIG_SILENT_CONSOLE
Wolfgang Denk6405a152006-03-31 18:32:53 +0200116 if (gd->flags & GD_FLG_SILENT) {
117 /* Restore serial console */
118 console_assign (stdout, "serial");
119 console_assign (stderr, "serial");
dzu6605c842003-10-24 13:14:45 +0000120 }
121#endif
122
wdenkc6097192002-11-03 00:24:07 +0000123# ifdef CONFIG_AUTOBOOT_PROMPT
124 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
125# endif
126
127# ifdef CONFIG_AUTOBOOT_DELAY_STR
128 if (delaykey[0].str == NULL)
129 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
130# endif
131# ifdef CONFIG_AUTOBOOT_DELAY_STR2
132 if (delaykey[1].str == NULL)
133 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
134# endif
135# ifdef CONFIG_AUTOBOOT_STOP_STR
136 if (delaykey[2].str == NULL)
137 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
138# endif
139# ifdef CONFIG_AUTOBOOT_STOP_STR2
140 if (delaykey[3].str == NULL)
141 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
142# endif
143
144 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
145 delaykey[i].len = delaykey[i].str == NULL ?
146 0 : strlen (delaykey[i].str);
147 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
148 MAX_DELAY_STOP_STR : delaykey[i].len;
149
150 presskey_max = presskey_max > delaykey[i].len ?
151 presskey_max : delaykey[i].len;
152
153# if DEBUG_BOOTKEYS
154 printf("%s key:<%s>\n",
155 delaykey[i].retry ? "delay" : "stop",
156 delaykey[i].str ? delaykey[i].str : "NULL");
157# endif
158 }
159
160 /* In order to keep up with incoming data, check timeout only
161 * when catch up.
162 */
163 while (!abort && get_ticks() <= etime) {
164 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
165 if (delaykey[i].len > 0 &&
166 presskey_len >= delaykey[i].len &&
167 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk57b2d802003-06-27 21:31:46 +0000168 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000169 delaykey[i].len) == 0) {
170# if DEBUG_BOOTKEYS
171 printf("got %skey\n",
172 delaykey[i].retry ? "delay" : "stop");
173# endif
174
175# ifdef CONFIG_BOOT_RETRY_TIME
176 /* don't retry auto boot */
177 if (! delaykey[i].retry)
178 retry_time = -1;
179# endif
180 abort = 1;
181 }
182 }
183
184 if (tstc()) {
185 if (presskey_len < presskey_max) {
186 presskey [presskey_len ++] = getc();
187 }
188 else {
189 for (i = 0; i < presskey_max - 1; i ++)
190 presskey [i] = presskey [i + 1];
191
192 presskey [i] = getc();
193 }
194 }
195 }
196# if DEBUG_BOOTKEYS
197 if (!abort)
wdenk42c05472004-03-23 22:14:11 +0000198 puts ("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000199# endif
200
dzu6605c842003-10-24 13:14:45 +0000201#ifdef CONFIG_SILENT_CONSOLE
Wolfgang Denk6405a152006-03-31 18:32:53 +0200202 if (abort) {
203 /* permanently enable normal console output */
204 gd->flags &= ~(GD_FLG_SILENT);
205 } else if (gd->flags & GD_FLG_SILENT) {
206 /* Restore silent console */
207 console_assign (stdout, "nulldev");
208 console_assign (stderr, "nulldev");
dzu6605c842003-10-24 13:14:45 +0000209 }
210#endif
211
wdenkc6097192002-11-03 00:24:07 +0000212 return abort;
213}
214
215# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
216
wdenk452cfd62002-11-19 11:04:11 +0000217#ifdef CONFIG_MENUKEY
218static int menukey = 0;
219#endif
220
wdenkc6097192002-11-03 00:24:07 +0000221static __inline__ int abortboot(int bootdelay)
222{
223 int abort = 0;
224
wdenk808532a2003-10-10 10:05:42 +0000225#ifdef CONFIG_SILENT_CONSOLE
Wolfgang Denk6405a152006-03-31 18:32:53 +0200226 if (gd->flags & GD_FLG_SILENT) {
227 /* Restore serial console */
228 console_assign (stdout, "serial");
229 console_assign (stderr, "serial");
wdenk808532a2003-10-10 10:05:42 +0000230 }
231#endif
232
wdenk452cfd62002-11-19 11:04:11 +0000233#ifdef CONFIG_MENUPROMPT
234 printf(CONFIG_MENUPROMPT, bootdelay);
235#else
wdenkc6097192002-11-03 00:24:07 +0000236 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenk452cfd62002-11-19 11:04:11 +0000237#endif
wdenkc6097192002-11-03 00:24:07 +0000238
239#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk57b2d802003-06-27 21:31:46 +0000240 /*
241 * Check if key already pressed
242 * Don't check if bootdelay < 0
243 */
wdenkc6097192002-11-03 00:24:07 +0000244 if (bootdelay >= 0) {
245 if (tstc()) { /* we got a key press */
246 (void) getc(); /* consume input */
wdenk42c05472004-03-23 22:14:11 +0000247 puts ("\b\b\b 0");
wdenk808532a2003-10-10 10:05:42 +0000248 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000249 }
wdenk57b2d802003-06-27 21:31:46 +0000250 }
wdenkc6097192002-11-03 00:24:07 +0000251#endif
252
wdenk808532a2003-10-10 10:05:42 +0000253 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000254 int i;
255
256 --bootdelay;
257 /* delay 100 * 10ms */
258 for (i=0; !abort && i<100; ++i) {
259 if (tstc()) { /* we got a key press */
260 abort = 1; /* don't auto boot */
261 bootdelay = 0; /* no more delay */
wdenk452cfd62002-11-19 11:04:11 +0000262# ifdef CONFIG_MENUKEY
263 menukey = getc();
264# else
wdenkc6097192002-11-03 00:24:07 +0000265 (void) getc(); /* consume input */
wdenk452cfd62002-11-19 11:04:11 +0000266# endif
wdenkc6097192002-11-03 00:24:07 +0000267 break;
268 }
269 udelay (10000);
270 }
271
272 printf ("\b\b\b%2d ", bootdelay);
273 }
274
275 putc ('\n');
276
wdenk808532a2003-10-10 10:05:42 +0000277#ifdef CONFIG_SILENT_CONSOLE
Wolfgang Denk6405a152006-03-31 18:32:53 +0200278 if (abort) {
279 /* permanently enable normal console output */
280 gd->flags &= ~(GD_FLG_SILENT);
281 } else if (gd->flags & GD_FLG_SILENT) {
282 /* Restore silent console */
283 console_assign (stdout, "nulldev");
284 console_assign (stderr, "nulldev");
wdenk808532a2003-10-10 10:05:42 +0000285 }
286#endif
287
wdenkc6097192002-11-03 00:24:07 +0000288 return abort;
289}
290# endif /* CONFIG_AUTOBOOT_KEYED */
291#endif /* CONFIG_BOOTDELAY >= 0 */
292
293/****************************************************************************/
294
295void main_loop (void)
296{
297#ifndef CFG_HUSH_PARSER
298 static char lastcommand[CFG_CBSIZE] = { 0, };
299 int len;
300 int rc = 1;
301 int flag;
302#endif
303
304#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
305 char *s;
306 int bootdelay;
307#endif
308#ifdef CONFIG_PREBOOT
309 char *p;
310#endif
wdenk0a658552003-08-05 17:43:17 +0000311#ifdef CONFIG_BOOTCOUNT_LIMIT
312 unsigned long bootcount = 0;
313 unsigned long bootlimit = 0;
314 char *bcs;
315 char bcs_set[16];
316#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000317
318#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
319 ulong bmp = 0; /* default bitmap */
320 extern int trab_vfd (ulong bitmap);
321
322#ifdef CONFIG_MODEM_SUPPORT
323 if (do_mdm_init)
324 bmp = 1; /* alternate bitmap */
325#endif
326 trab_vfd (bmp);
327#endif /* CONFIG_VFD && VFD_TEST_LOGO */
328
wdenk0a658552003-08-05 17:43:17 +0000329#ifdef CONFIG_BOOTCOUNT_LIMIT
330 bootcount = bootcount_load();
331 bootcount++;
332 bootcount_store (bootcount);
333 sprintf (bcs_set, "%lu", bootcount);
334 setenv ("bootcount", bcs_set);
335 bcs = getenv ("bootlimit");
336 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
337#endif /* CONFIG_BOOTCOUNT_LIMIT */
338
wdenkc6097192002-11-03 00:24:07 +0000339#ifdef CONFIG_MODEM_SUPPORT
340 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
341 if (do_mdm_init) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200342 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000343 setenv ("preboot", str); /* set or delete definition */
344 if (str != NULL)
345 free (str);
346 mdm_init(); /* wait for modem connection */
347 }
348#endif /* CONFIG_MODEM_SUPPORT */
349
stroesef8105cf2003-04-04 15:44:49 +0000350#ifdef CONFIG_VERSION_VARIABLE
351 {
352 extern char version_string[];
stroesef8105cf2003-04-04 15:44:49 +0000353
stroeseb9ab27c2003-07-11 08:00:33 +0000354 setenv ("ver", version_string); /* set version variable */
stroesef8105cf2003-04-04 15:44:49 +0000355 }
356#endif /* CONFIG_VERSION_VARIABLE */
357
wdenkc6097192002-11-03 00:24:07 +0000358#ifdef CFG_HUSH_PARSER
359 u_boot_hush_start ();
360#endif
361
wdenk3902d702004-04-15 18:22:41 +0000362#ifdef CONFIG_AUTO_COMPLETE
363 install_auto_complete();
364#endif
365
wdenkc6097192002-11-03 00:24:07 +0000366#ifdef CONFIG_PREBOOT
367 if ((p = getenv ("preboot")) != NULL) {
368# ifdef CONFIG_AUTOBOOT_KEYED
369 int prev = disable_ctrlc(1); /* disable Control C checking */
370# endif
371
372# ifndef CFG_HUSH_PARSER
373 run_command (p, 0);
374# else
375 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
376 FLAG_EXIT_FROM_LOOP);
377# endif
378
379# ifdef CONFIG_AUTOBOOT_KEYED
380 disable_ctrlc(prev); /* restore Control C checking */
381# endif
382 }
383#endif /* CONFIG_PREBOOT */
384
385#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
386 s = getenv ("bootdelay");
387 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
388
wdenk9dd2b882002-12-03 21:28:10 +0000389 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000390
391# ifdef CONFIG_BOOT_RETRY_TIME
wdenkb00ec162003-06-19 23:40:20 +0000392 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000393# endif /* CONFIG_BOOT_RETRY_TIME */
394
wdenk0a658552003-08-05 17:43:17 +0000395#ifdef CONFIG_BOOTCOUNT_LIMIT
396 if (bootlimit && (bootcount > bootlimit)) {
397 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
398 (unsigned)bootlimit);
399 s = getenv ("altbootcmd");
400 }
401 else
402#endif /* CONFIG_BOOTCOUNT_LIMIT */
403 s = getenv ("bootcmd");
wdenk9dd2b882002-12-03 21:28:10 +0000404
405 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
406
wdenkc6097192002-11-03 00:24:07 +0000407 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
408# ifdef CONFIG_AUTOBOOT_KEYED
409 int prev = disable_ctrlc(1); /* disable Control C checking */
410# endif
411
412# ifndef CFG_HUSH_PARSER
413 run_command (s, 0);
414# else
415 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
416 FLAG_EXIT_FROM_LOOP);
417# endif
418
419# ifdef CONFIG_AUTOBOOT_KEYED
420 disable_ctrlc(prev); /* restore Control C checking */
421# endif
422 }
wdenk452cfd62002-11-19 11:04:11 +0000423
424# ifdef CONFIG_MENUKEY
wdenk9dd2b882002-12-03 21:28:10 +0000425 if (menukey == CONFIG_MENUKEY) {
wdenk452cfd62002-11-19 11:04:11 +0000426 s = getenv("menucmd");
wdenk9dd2b882002-12-03 21:28:10 +0000427 if (s) {
wdenk452cfd62002-11-19 11:04:11 +0000428# ifndef CFG_HUSH_PARSER
wdenk91a4f362005-01-09 23:33:49 +0000429 run_command (s, 0);
wdenk452cfd62002-11-19 11:04:11 +0000430# else
431 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
432 FLAG_EXIT_FROM_LOOP);
433# endif
434 }
435 }
436#endif /* CONFIG_MENUKEY */
wdenkc6097192002-11-03 00:24:07 +0000437#endif /* CONFIG_BOOTDELAY */
438
wdenk452cfd62002-11-19 11:04:11 +0000439#ifdef CONFIG_AMIGAONEG3SE
440 {
441 extern void video_banner(void);
442 video_banner();
443 }
444#endif
445
wdenkc6097192002-11-03 00:24:07 +0000446 /*
447 * Main Loop for Monitor Command Processing
448 */
449#ifdef CFG_HUSH_PARSER
450 parse_file_outer();
451 /* This point is never reached */
452 for (;;);
453#else
454 for (;;) {
455#ifdef CONFIG_BOOT_RETRY_TIME
456 if (rc >= 0) {
457 /* Saw enough of a valid command to
458 * restart the timeout.
459 */
460 reset_cmd_timeout();
461 }
462#endif
463 len = readline (CFG_PROMPT);
464
465 flag = 0; /* assume no special flags for now */
466 if (len > 0)
467 strcpy (lastcommand, console_buffer);
468 else if (len == 0)
469 flag |= CMD_FLAG_REPEAT;
470#ifdef CONFIG_BOOT_RETRY_TIME
471 else if (len == -2) {
472 /* -2 means timed out, retry autoboot
473 */
wdenk42c05472004-03-23 22:14:11 +0000474 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000475# ifdef CONFIG_RESET_TO_RETRY
476 /* Reinit board to run initialization code again */
477 do_reset (NULL, 0, 0, NULL);
478# else
479 return; /* retry autoboot */
480# endif
481 }
482#endif
483
484 if (len == -1)
wdenk42c05472004-03-23 22:14:11 +0000485 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000486 else
487 rc = run_command (lastcommand, flag);
488
489 if (rc <= 0) {
490 /* invalid command or not repeatable, forget it */
491 lastcommand[0] = 0;
492 }
493 }
494#endif /*CFG_HUSH_PARSER*/
495}
496
wdenkb00ec162003-06-19 23:40:20 +0000497#ifdef CONFIG_BOOT_RETRY_TIME
498/***************************************************************************
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200499 * initialize command line timeout
wdenkb00ec162003-06-19 23:40:20 +0000500 */
501void init_cmd_timeout(void)
502{
503 char *s = getenv ("bootretry");
504
505 if (s != NULL)
wdenk444f22b2003-12-07 21:39:28 +0000506 retry_time = (int)simple_strtol(s, NULL, 10);
wdenkb00ec162003-06-19 23:40:20 +0000507 else
508 retry_time = CONFIG_BOOT_RETRY_TIME;
509
510 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
511 retry_time = CONFIG_BOOT_RETRY_MIN;
512}
513
wdenkc6097192002-11-03 00:24:07 +0000514/***************************************************************************
515 * reset command line timeout to retry_time seconds
516 */
wdenkc6097192002-11-03 00:24:07 +0000517void reset_cmd_timeout(void)
518{
519 endtime = endtick(retry_time);
520}
521#endif
522
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200523#ifdef CONFIG_CMDLINE_EDITING
524
525/*
526 * cmdline-editing related codes from vivi.
527 * Author: Janghoon Lyu <nandy@mizi.com>
528 */
529
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200530#define putnstr(str,n) do { \
531 printf ("%.*s", n, str); \
532 } while (0)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200533
534#define CTL_CH(c) ((c) - 'a' + 1)
535
536#define MAX_CMDBUF_SIZE 256
537
538#define CTL_BACKSPACE ('\b')
539#define DEL ((char)255)
540#define DEL7 ((char)127)
541#define CREAD_HIST_CHAR ('!')
542
543#define getcmd_putch(ch) putc(ch)
544#define getcmd_getch() getc()
545#define getcmd_cbeep() getcmd_putch('\a')
546
547#define HIST_MAX 20
548#define HIST_SIZE MAX_CMDBUF_SIZE
549
550static int hist_max = 0;
551static int hist_add_idx = 0;
552static int hist_cur = -1;
553unsigned hist_num = 0;
554
555char* hist_list[HIST_MAX];
556char hist_lines[HIST_MAX][HIST_SIZE];
557
558#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
559
560static void hist_init(void)
561{
562 int i;
563
564 hist_max = 0;
565 hist_add_idx = 0;
566 hist_cur = -1;
567 hist_num = 0;
568
569 for (i = 0; i < HIST_MAX; i++) {
570 hist_list[i] = hist_lines[i];
571 hist_list[i][0] = '\0';
572 }
573}
574
575static void cread_add_to_hist(char *line)
576{
577 strcpy(hist_list[hist_add_idx], line);
578
579 if (++hist_add_idx >= HIST_MAX)
580 hist_add_idx = 0;
581
582 if (hist_add_idx > hist_max)
583 hist_max = hist_add_idx;
584
585 hist_num++;
586}
587
588static char* hist_prev(void)
589{
590 char *ret;
591 int old_cur;
592
593 if (hist_cur < 0)
594 return NULL;
595
596 old_cur = hist_cur;
597 if (--hist_cur < 0)
598 hist_cur = hist_max;
599
600 if (hist_cur == hist_add_idx) {
601 hist_cur = old_cur;
602 ret = NULL;
603 } else
604 ret = hist_list[hist_cur];
605
606 return (ret);
607}
608
609static char* hist_next(void)
610{
611 char *ret;
612
613 if (hist_cur < 0)
614 return NULL;
615
616 if (hist_cur == hist_add_idx)
617 return NULL;
618
619 if (++hist_cur > hist_max)
620 hist_cur = 0;
621
622 if (hist_cur == hist_add_idx) {
623 ret = "";
624 } else
625 ret = hist_list[hist_cur];
626
627 return (ret);
628}
629
Stefan Roese59ff5a52006-07-27 16:11:19 +0200630#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200631static void cread_print_hist_list(void)
632{
633 int i;
634 unsigned long n;
635
636 n = hist_num - hist_max;
637
638 i = hist_add_idx + 1;
639 while (1) {
640 if (i > hist_max)
641 i = 0;
642 if (i == hist_add_idx)
643 break;
644 printf("%s\n", hist_list[i]);
645 n++;
646 i++;
647 }
648}
Stefan Roese59ff5a52006-07-27 16:11:19 +0200649#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200650
651#define BEGINNING_OF_LINE() { \
652 while (num) { \
653 getcmd_putch(CTL_BACKSPACE); \
654 num--; \
655 } \
656}
657
658#define ERASE_TO_EOL() { \
659 if (num < eol_num) { \
660 int tmp; \
661 for (tmp = num; tmp < eol_num; tmp++) \
662 getcmd_putch(' '); \
663 while (tmp-- > num) \
664 getcmd_putch(CTL_BACKSPACE); \
665 eol_num = num; \
666 } \
667}
668
669#define REFRESH_TO_EOL() { \
670 if (num < eol_num) { \
671 wlen = eol_num - num; \
672 putnstr(buf + num, wlen); \
673 num = eol_num; \
674 } \
675}
676
677static void cread_add_char(char ichar, int insert, unsigned long *num,
678 unsigned long *eol_num, char *buf, unsigned long len)
679{
680 unsigned long wlen;
681
682 /* room ??? */
683 if (insert || *num == *eol_num) {
684 if (*eol_num > len - 1) {
685 getcmd_cbeep();
686 return;
687 }
688 (*eol_num)++;
689 }
690
691 if (insert) {
692 wlen = *eol_num - *num;
693 if (wlen > 1) {
694 memmove(&buf[*num+1], &buf[*num], wlen-1);
695 }
696
697 buf[*num] = ichar;
698 putnstr(buf + *num, wlen);
699 (*num)++;
700 while (--wlen) {
701 getcmd_putch(CTL_BACKSPACE);
702 }
703 } else {
704 /* echo the character */
705 wlen = 1;
706 buf[*num] = ichar;
707 putnstr(buf + *num, wlen);
708 (*num)++;
709 }
710}
711
712static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
713 unsigned long *eol_num, char *buf, unsigned long len)
714{
715 while (strsize--) {
716 cread_add_char(*str, insert, num, eol_num, buf, len);
717 str++;
718 }
719}
720
721static int cread_line(char *buf, unsigned int *len)
722{
723 unsigned long num = 0;
724 unsigned long eol_num = 0;
725 unsigned long rlen;
726 unsigned long wlen;
727 char ichar;
728 int insert = 1;
729 int esc_len = 0;
730 int rc = 0;
731 char esc_save[8];
732
733 while (1) {
734 rlen = 1;
735 ichar = getcmd_getch();
736
737 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200738 putc('\n');
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200739 break;
740 }
741
742 /*
743 * handle standard linux xterm esc sequences for arrow key, etc.
744 */
745 if (esc_len != 0) {
746 if (esc_len == 1) {
747 if (ichar == '[') {
748 esc_save[esc_len] = ichar;
749 esc_len = 2;
750 } else {
751 cread_add_str(esc_save, esc_len, insert,
752 &num, &eol_num, buf, *len);
753 esc_len = 0;
754 }
755 continue;
756 }
757
758 switch (ichar) {
759
760 case 'D': /* <- key */
761 ichar = CTL_CH('b');
762 esc_len = 0;
763 break;
764 case 'C': /* -> key */
765 ichar = CTL_CH('f');
766 esc_len = 0;
767 break; /* pass off to ^F handler */
768 case 'H': /* Home key */
769 ichar = CTL_CH('a');
770 esc_len = 0;
771 break; /* pass off to ^A handler */
772 case 'A': /* up arrow */
773 ichar = CTL_CH('p');
774 esc_len = 0;
775 break; /* pass off to ^P handler */
776 case 'B': /* down arrow */
777 ichar = CTL_CH('n');
778 esc_len = 0;
779 break; /* pass off to ^N handler */
780 default:
781 esc_save[esc_len++] = ichar;
782 cread_add_str(esc_save, esc_len, insert,
783 &num, &eol_num, buf, *len);
784 esc_len = 0;
785 continue;
786 }
787 }
788
789 switch (ichar) {
790 case 0x1b:
791 if (esc_len == 0) {
792 esc_save[esc_len] = ichar;
793 esc_len = 1;
794 } else {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200795 puts("impossible condition #876\n");
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200796 esc_len = 0;
797 }
798 break;
799
800 case CTL_CH('a'):
801 BEGINNING_OF_LINE();
802 break;
803 case CTL_CH('c'): /* ^C - break */
804 *buf = '\0'; /* discard input */
805 return (-1);
806 case CTL_CH('f'):
807 if (num < eol_num) {
808 getcmd_putch(buf[num]);
809 num++;
810 }
811 break;
812 case CTL_CH('b'):
813 if (num) {
814 getcmd_putch(CTL_BACKSPACE);
815 num--;
816 }
817 break;
818 case CTL_CH('d'):
819 if (num < eol_num) {
820 wlen = eol_num - num - 1;
821 if (wlen) {
822 memmove(&buf[num], &buf[num+1], wlen);
823 putnstr(buf + num, wlen);
824 }
825
826 getcmd_putch(' ');
827 do {
828 getcmd_putch(CTL_BACKSPACE);
829 } while (wlen--);
830 eol_num--;
831 }
832 break;
833 case CTL_CH('k'):
834 ERASE_TO_EOL();
835 break;
836 case CTL_CH('e'):
837 REFRESH_TO_EOL();
838 break;
839 case CTL_CH('o'):
840 insert = !insert;
841 break;
842 case CTL_CH('x'):
843 BEGINNING_OF_LINE();
844 ERASE_TO_EOL();
845 break;
846 case DEL:
847 case DEL7:
848 case 8:
849 if (num) {
850 wlen = eol_num - num;
851 num--;
852 memmove(&buf[num], &buf[num+1], wlen);
853 getcmd_putch(CTL_BACKSPACE);
854 putnstr(buf + num, wlen);
855 getcmd_putch(' ');
856 do {
857 getcmd_putch(CTL_BACKSPACE);
858 } while (wlen--);
859 eol_num--;
860 }
861 break;
862 case CTL_CH('p'):
863 case CTL_CH('n'):
864 {
865 char * hline;
866
867 esc_len = 0;
868
869 if (ichar == CTL_CH('p'))
870 hline = hist_prev();
871 else
872 hline = hist_next();
873
874 if (!hline) {
875 getcmd_cbeep();
876 continue;
877 }
878
879 /* nuke the current line */
880 /* first, go home */
881 BEGINNING_OF_LINE();
882
883 /* erase to end of line */
884 ERASE_TO_EOL();
885
886 /* copy new line into place and display */
887 strcpy(buf, hline);
888 eol_num = strlen(buf);
889 REFRESH_TO_EOL();
890 continue;
891 }
892 default:
893 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
894 break;
895 }
896 }
897 *len = eol_num;
898 buf[eol_num] = '\0'; /* lose the newline */
899
900 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
901 cread_add_to_hist(buf);
902 hist_cur = hist_add_idx;
903
904 return (rc);
905}
906
907#endif /* CONFIG_CMDLINE_EDITING */
908
wdenkc6097192002-11-03 00:24:07 +0000909/****************************************************************************/
910
911/*
912 * Prompt for input and read a line.
913 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
914 * time out when time goes past endtime (timebase time in ticks).
915 * Return: number of read characters
916 * -1 if break
917 * -2 if timed out
918 */
919int readline (const char *const prompt)
920{
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200921#ifdef CONFIG_CMDLINE_EDITING
922 char *p = console_buffer;
923 unsigned int len=MAX_CMDBUF_SIZE;
Stefan Roesedc5ff702006-08-07 15:08:44 +0200924 int rc;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200925 static int initted = 0;
926
927 if (!initted) {
928 hist_init();
929 initted = 1;
930 }
931
Wolfgang Denk92549de2006-07-21 11:34:34 +0200932 puts (prompt);
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200933
Stefan Roesedc5ff702006-08-07 15:08:44 +0200934 rc = cread_line(p, &len);
935 return rc < 0 ? rc : len;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200936#else
wdenkc6097192002-11-03 00:24:07 +0000937 char *p = console_buffer;
938 int n = 0; /* buffer index */
939 int plen = 0; /* prompt length */
940 int col; /* output column cnt */
941 char c;
942
943 /* print prompt */
944 if (prompt) {
945 plen = strlen (prompt);
946 puts (prompt);
947 }
948 col = plen;
949
950 for (;;) {
951#ifdef CONFIG_BOOT_RETRY_TIME
952 while (!tstc()) { /* while no incoming data */
953 if (retry_time >= 0 && get_ticks() > endtime)
954 return (-2); /* timed out */
955 }
956#endif
957 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
958
959#ifdef CONFIG_SHOW_ACTIVITY
960 while (!tstc()) {
961 extern void show_activity(int arg);
962 show_activity(0);
963 }
964#endif
965 c = getc();
966
967 /*
968 * Special character handling
969 */
970 switch (c) {
971 case '\r': /* Enter */
972 case '\n':
973 *p = '\0';
974 puts ("\r\n");
975 return (p - console_buffer);
976
wdenk8d4d1f62004-03-23 22:37:33 +0000977 case '\0': /* nul */
978 continue;
979
wdenkc6097192002-11-03 00:24:07 +0000980 case 0x03: /* ^C - break */
981 console_buffer[0] = '\0'; /* discard input */
982 return (-1);
983
984 case 0x15: /* ^U - erase line */
985 while (col > plen) {
986 puts (erase_seq);
987 --col;
988 }
989 p = console_buffer;
990 n = 0;
991 continue;
992
993 case 0x17: /* ^W - erase word */
994 p=delete_char(console_buffer, p, &col, &n, plen);
995 while ((n > 0) && (*p != ' ')) {
996 p=delete_char(console_buffer, p, &col, &n, plen);
997 }
998 continue;
999
1000 case 0x08: /* ^H - backspace */
1001 case 0x7F: /* DEL - backspace */
1002 p=delete_char(console_buffer, p, &col, &n, plen);
1003 continue;
1004
1005 default:
1006 /*
1007 * Must be a normal character then
1008 */
1009 if (n < CFG_CBSIZE-2) {
1010 if (c == '\t') { /* expand TABs */
wdenk3902d702004-04-15 18:22:41 +00001011#ifdef CONFIG_AUTO_COMPLETE
1012 /* if auto completion triggered just continue */
1013 *p = '\0';
1014 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1015 p = console_buffer + n; /* reset */
1016 continue;
1017 }
1018#endif
wdenkc6097192002-11-03 00:24:07 +00001019 puts (tab_seq+(col&07));
1020 col += 8 - (col&07);
1021 } else {
1022 ++col; /* echo input */
1023 putc (c);
1024 }
1025 *p++ = c;
1026 ++n;
1027 } else { /* Buffer full */
1028 putc ('\a');
1029 }
1030 }
1031 }
Wolfgang Denk4c9ca782006-07-21 11:33:45 +02001032#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001033}
1034
1035/****************************************************************************/
1036
Stefan Roese59ff5a52006-07-27 16:11:19 +02001037#ifndef CONFIG_CMDLINE_EDITING
wdenkc6097192002-11-03 00:24:07 +00001038static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1039{
1040 char *s;
1041
1042 if (*np == 0) {
1043 return (p);
1044 }
1045
1046 if (*(--p) == '\t') { /* will retype the whole line */
1047 while (*colp > plen) {
1048 puts (erase_seq);
1049 (*colp)--;
1050 }
1051 for (s=buffer; s<p; ++s) {
1052 if (*s == '\t') {
1053 puts (tab_seq+((*colp) & 07));
1054 *colp += 8 - ((*colp) & 07);
1055 } else {
1056 ++(*colp);
1057 putc (*s);
1058 }
1059 }
1060 } else {
1061 puts (erase_seq);
1062 (*colp)--;
1063 }
1064 (*np)--;
1065 return (p);
1066}
Stefan Roese59ff5a52006-07-27 16:11:19 +02001067#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001068
1069/****************************************************************************/
1070
1071int parse_line (char *line, char *argv[])
1072{
1073 int nargs = 0;
1074
1075#ifdef DEBUG_PARSER
1076 printf ("parse_line: \"%s\"\n", line);
1077#endif
1078 while (nargs < CFG_MAXARGS) {
1079
1080 /* skip any white space */
1081 while ((*line == ' ') || (*line == '\t')) {
1082 ++line;
1083 }
1084
1085 if (*line == '\0') { /* end of line, no more args */
1086 argv[nargs] = NULL;
1087#ifdef DEBUG_PARSER
1088 printf ("parse_line: nargs=%d\n", nargs);
1089#endif
1090 return (nargs);
1091 }
1092
1093 argv[nargs++] = line; /* begin of argument string */
1094
1095 /* find end of string */
1096 while (*line && (*line != ' ') && (*line != '\t')) {
1097 ++line;
1098 }
1099
1100 if (*line == '\0') { /* end of line, no more args */
1101 argv[nargs] = NULL;
1102#ifdef DEBUG_PARSER
1103 printf ("parse_line: nargs=%d\n", nargs);
1104#endif
1105 return (nargs);
1106 }
1107
1108 *line++ = '\0'; /* terminate current arg */
1109 }
1110
1111 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1112
1113#ifdef DEBUG_PARSER
1114 printf ("parse_line: nargs=%d\n", nargs);
1115#endif
1116 return (nargs);
1117}
1118
1119/****************************************************************************/
1120
1121static void process_macros (const char *input, char *output)
1122{
1123 char c, prev;
1124 const char *varname_start = NULL;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001125 int inputcnt = strlen (input);
wdenkc6097192002-11-03 00:24:07 +00001126 int outputcnt = CFG_CBSIZE;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001127 int state = 0; /* 0 = waiting for '$' */
1128
1129 /* 1 = waiting for '(' or '{' */
1130 /* 2 = waiting for ')' or '}' */
1131 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001132#ifdef DEBUG_PARSER
1133 char *output_start = output;
1134
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001135 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1136 input);
wdenkc6097192002-11-03 00:24:07 +00001137#endif
1138
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001139 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001140
1141 while (inputcnt && outputcnt) {
wdenk57b2d802003-06-27 21:31:46 +00001142 c = *input++;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001143 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001144
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001145 if (state != 3) {
1146 /* remove one level of escape characters */
1147 if ((c == '\\') && (prev != '\\')) {
1148 if (inputcnt-- == 0)
1149 break;
1150 prev = c;
1151 c = *input++;
1152 }
wdenkc6097192002-11-03 00:24:07 +00001153 }
wdenkc6097192002-11-03 00:24:07 +00001154
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001155 switch (state) {
1156 case 0: /* Waiting for (unescaped) $ */
1157 if ((c == '\'') && (prev != '\\')) {
1158 state = 3;
1159 break;
1160 }
1161 if ((c == '$') && (prev != '\\')) {
1162 state++;
1163 } else {
wdenkc6097192002-11-03 00:24:07 +00001164 *(output++) = c;
1165 outputcnt--;
1166 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001167 break;
1168 case 1: /* Waiting for ( */
1169 if (c == '(' || c == '{') {
1170 state++;
1171 varname_start = input;
1172 } else {
1173 state = 0;
1174 *(output++) = '$';
1175 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001176
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001177 if (outputcnt) {
1178 *(output++) = c;
1179 outputcnt--;
1180 }
wdenkc6097192002-11-03 00:24:07 +00001181 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001182 break;
1183 case 2: /* Waiting for ) */
1184 if (c == ')' || c == '}') {
1185 int i;
1186 char envname[CFG_CBSIZE], *envval;
1187 int envcnt = input - varname_start - 1; /* Varname # of chars */
wdenkc6097192002-11-03 00:24:07 +00001188
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001189 /* Get the varname */
1190 for (i = 0; i < envcnt; i++) {
1191 envname[i] = varname_start[i];
wdenkc6097192002-11-03 00:24:07 +00001192 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001193 envname[i] = 0;
1194
1195 /* Get its value */
1196 envval = getenv (envname);
1197
1198 /* Copy into the line if it exists */
1199 if (envval != NULL)
1200 while ((*envval) && outputcnt) {
1201 *(output++) = *(envval++);
1202 outputcnt--;
1203 }
1204 /* Look for another '$' */
1205 state = 0;
1206 }
1207 break;
1208 case 3: /* Waiting for ' */
1209 if ((c == '\'') && (prev != '\\')) {
1210 state = 0;
1211 } else {
1212 *(output++) = c;
1213 outputcnt--;
1214 }
1215 break;
wdenkc6097192002-11-03 00:24:07 +00001216 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001217 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001218 }
1219
1220 if (outputcnt)
1221 *output = 0;
1222
1223#ifdef DEBUG_PARSER
1224 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001225 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001226#endif
1227}
1228
1229/****************************************************************************
1230 * returns:
1231 * 1 - command executed, repeatable
1232 * 0 - command executed but not repeatable, interrupted commands are
1233 * always considered not repeatable
1234 * -1 - not executed (unrecognized, bootd recursion or too many args)
1235 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1236 * considered unrecognized)
1237 *
1238 * WARNING:
1239 *
1240 * We must create a temporary copy of the command since the command we get
1241 * may be the result from getenv(), which returns a pointer directly to
1242 * the environment data, which may change magicly when the command we run
1243 * creates or modifies environment variables (like "bootp" does).
1244 */
1245
1246int run_command (const char *cmd, int flag)
1247{
1248 cmd_tbl_t *cmdtp;
1249 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1250 char *token; /* start of token in cmdbuf */
1251 char *sep; /* end of token (separator) in cmdbuf */
1252 char finaltoken[CFG_CBSIZE];
1253 char *str = cmdbuf;
1254 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
wdenkf4688a22003-05-28 08:06:31 +00001255 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001256 int repeatable = 1;
wdenkf4688a22003-05-28 08:06:31 +00001257 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001258
1259#ifdef DEBUG_PARSER
1260 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1261 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1262 puts ("\"\n");
1263#endif
1264
1265 clear_ctrlc(); /* forget any previous Control C */
1266
1267 if (!cmd || !*cmd) {
1268 return -1; /* empty command */
1269 }
1270
1271 if (strlen(cmd) >= CFG_CBSIZE) {
1272 puts ("## Command too long!\n");
1273 return -1;
1274 }
1275
1276 strcpy (cmdbuf, cmd);
1277
1278 /* Process separators and check for invalid
1279 * repeatable commands
1280 */
1281
1282#ifdef DEBUG_PARSER
1283 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1284#endif
1285 while (*str) {
1286
1287 /*
1288 * Find separator, or string end
1289 * Allow simple escape of ';' by writing "\;"
1290 */
wdenk654868c2003-01-02 23:57:29 +00001291 for (inquotes = 0, sep = str; *sep; sep++) {
1292 if ((*sep=='\'') &&
1293 (*(sep-1) != '\\'))
1294 inquotes=!inquotes;
1295
1296 if (!inquotes &&
1297 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001298 ( sep != str) && /* past string start */
1299 (*(sep-1) != '\\')) /* and NOT escaped */
1300 break;
1301 }
1302
1303 /*
1304 * Limit the token to data between separators
1305 */
1306 token = str;
1307 if (*sep) {
1308 str = sep + 1; /* start of command for next pass */
1309 *sep = '\0';
1310 }
1311 else
1312 str = sep; /* no more commands for next pass */
1313#ifdef DEBUG_PARSER
1314 printf ("token: \"%s\"\n", token);
1315#endif
1316
1317 /* find macros in this token and replace them */
1318 process_macros (token, finaltoken);
1319
1320 /* Extract arguments */
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001321 if ((argc = parse_line (finaltoken, argv)) == 0) {
1322 rc = -1; /* no command at all */
1323 continue;
1324 }
wdenkc6097192002-11-03 00:24:07 +00001325
1326 /* Look up command in command table */
1327 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1328 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf4688a22003-05-28 08:06:31 +00001329 rc = -1; /* give up after bad command */
1330 continue;
wdenkc6097192002-11-03 00:24:07 +00001331 }
1332
1333 /* found - check max args */
1334 if (argc > cmdtp->maxargs) {
1335 printf ("Usage:\n%s\n", cmdtp->usage);
wdenkf4688a22003-05-28 08:06:31 +00001336 rc = -1;
1337 continue;
wdenkc6097192002-11-03 00:24:07 +00001338 }
1339
1340#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1341 /* avoid "bootd" recursion */
1342 if (cmdtp->cmd == do_bootd) {
1343#ifdef DEBUG_PARSER
1344 printf ("[%s]\n", finaltoken);
1345#endif
1346 if (flag & CMD_FLAG_BOOTD) {
wdenk42c05472004-03-23 22:14:11 +00001347 puts ("'bootd' recursion detected\n");
wdenkf4688a22003-05-28 08:06:31 +00001348 rc = -1;
1349 continue;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001350 } else {
wdenkc6097192002-11-03 00:24:07 +00001351 flag |= CMD_FLAG_BOOTD;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001352 }
wdenkc6097192002-11-03 00:24:07 +00001353 }
1354#endif /* CFG_CMD_BOOTD */
1355
1356 /* OK - call function to do the command */
1357 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf4688a22003-05-28 08:06:31 +00001358 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001359 }
1360
1361 repeatable &= cmdtp->repeatable;
1362
1363 /* Did the user stop this? */
1364 if (had_ctrlc ())
1365 return 0; /* if stopped then not repeatable */
1366 }
1367
wdenkf4688a22003-05-28 08:06:31 +00001368 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001369}
1370
1371/****************************************************************************/
1372
1373#if (CONFIG_COMMANDS & CFG_CMD_RUN)
1374int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1375{
1376 int i;
wdenkc6097192002-11-03 00:24:07 +00001377
1378 if (argc < 2) {
1379 printf ("Usage:\n%s\n", cmdtp->usage);
1380 return 1;
1381 }
1382
1383 for (i=1; i<argc; ++i) {
wdenkb02744a2003-04-05 00:53:31 +00001384 char *arg;
1385
1386 if ((arg = getenv (argv[i])) == NULL) {
1387 printf ("## Error: \"%s\" not defined\n", argv[i]);
1388 return 1;
1389 }
wdenkc6097192002-11-03 00:24:07 +00001390#ifndef CFG_HUSH_PARSER
wdenkb02744a2003-04-05 00:53:31 +00001391 if (run_command (arg, flag) == -1)
1392 return 1;
wdenkc6097192002-11-03 00:24:07 +00001393#else
wdenkb02744a2003-04-05 00:53:31 +00001394 if (parse_string_outer(arg,
wdenk541a76d2003-05-03 15:50:43 +00001395 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenkb02744a2003-04-05 00:53:31 +00001396 return 1;
wdenkc6097192002-11-03 00:24:07 +00001397#endif
1398 }
wdenkb02744a2003-04-05 00:53:31 +00001399 return 0;
wdenkc6097192002-11-03 00:24:07 +00001400}
wdenkb02744a2003-04-05 00:53:31 +00001401#endif /* CFG_CMD_RUN */