Initial revision
[oweals/u-boot.git] / common / main.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <watchdog.h>
26 #include <command.h>
27 #include <cmd_nvedit.h>
28 #include <cmd_bootm.h>
29 #include <malloc.h>
30 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
31 #include <cmd_boot.h>           /* for do_reset() prototype */
32 #endif
33
34 #ifdef CFG_HUSH_PARSER
35 #include <hush.h>
36 #endif
37
38 #define MAX_DELAY_STOP_STR 32
39
40 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
41 static int parse_line (char *, char *[]);
42 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
43 static int abortboot(int);
44 #endif
45
46 #undef DEBUG_PARSER
47
48 char        console_buffer[CFG_CBSIZE];         /* console I/O buffer   */
49
50 static char erase_seq[] = "\b \b";              /* erase sequence       */
51 static char   tab_seq[] = "        ";           /* used to expand TABs  */
52
53 #ifdef CONFIG_BOOT_RETRY_TIME
54 static uint64_t endtime = 0;  /* must be set, default is instant timeout */
55 static int      retry_time = -1; /* -1 so can call readline before main_loop */
56 #endif
57
58 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
59
60 #ifndef CONFIG_BOOT_RETRY_MIN
61 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
62 #endif
63
64 #ifdef CONFIG_MODEM_SUPPORT
65 int do_mdm_init = 0;
66 extern void mdm_init(void); /* defined in board.c */
67 #endif
68
69 /***************************************************************************
70  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
71  * returns: 0 -  no key string, allow autoboot
72  *          1 - got key string, abort
73  */
74 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
75 # if defined(CONFIG_AUTOBOOT_KEYED)
76 static __inline__ int abortboot(int bootdelay)
77 {
78         int abort = 0;
79         uint64_t etime = endtick(bootdelay);
80         struct
81         {
82                 char* str;
83                 u_int len;
84                 int retry;
85         }
86         delaykey [] =
87         {
88                 { str: getenv ("bootdelaykey"),  retry: 1 },
89                 { str: getenv ("bootdelaykey2"), retry: 1 },
90                 { str: getenv ("bootstopkey"),   retry: 0 },
91                 { str: getenv ("bootstopkey2"),  retry: 0 },
92         };
93
94         char presskey [MAX_DELAY_STOP_STR];
95         u_int presskey_len = 0;
96         u_int presskey_max = 0;
97         u_int i;
98
99 #  ifdef CONFIG_AUTOBOOT_PROMPT
100         printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
101 #  endif
102
103 #  ifdef CONFIG_AUTOBOOT_DELAY_STR
104         if (delaykey[0].str == NULL)
105                 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
106 #  endif
107 #  ifdef CONFIG_AUTOBOOT_DELAY_STR2
108         if (delaykey[1].str == NULL)
109                 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
110 #  endif
111 #  ifdef CONFIG_AUTOBOOT_STOP_STR
112         if (delaykey[2].str == NULL)
113                 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
114 #  endif
115 #  ifdef CONFIG_AUTOBOOT_STOP_STR2
116         if (delaykey[3].str == NULL)
117                 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
118 #  endif
119
120         for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
121                 delaykey[i].len = delaykey[i].str == NULL ?
122                                     0 : strlen (delaykey[i].str);
123                 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
124                                     MAX_DELAY_STOP_STR : delaykey[i].len;
125
126                 presskey_max = presskey_max > delaykey[i].len ?
127                                     presskey_max : delaykey[i].len;
128
129 #  if DEBUG_BOOTKEYS
130                 printf("%s key:<%s>\n",
131                        delaykey[i].retry ? "delay" : "stop",
132                        delaykey[i].str ? delaykey[i].str : "NULL");
133 #  endif
134         }
135
136         /* In order to keep up with incoming data, check timeout only
137          * when catch up.
138          */
139         while (!abort && get_ticks() <= etime) {
140                 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
141                         if (delaykey[i].len > 0 &&
142                             presskey_len >= delaykey[i].len &&
143                             memcmp (presskey + presskey_len - delaykey[i].len,
144                                     delaykey[i].str,
145                                     delaykey[i].len) == 0) {
146 #  if DEBUG_BOOTKEYS
147                                 printf("got %skey\n",
148                                        delaykey[i].retry ? "delay" : "stop");
149 #  endif
150
151 #  ifdef CONFIG_BOOT_RETRY_TIME
152                                 /* don't retry auto boot */
153                                 if (! delaykey[i].retry)
154                                         retry_time = -1;
155 #  endif
156                                 abort = 1;
157                         }
158                 }
159
160                 if (tstc()) {
161                         if (presskey_len < presskey_max) {
162                                 presskey [presskey_len ++] = getc();
163                         }
164                         else {
165                                 for (i = 0; i < presskey_max - 1; i ++)
166                                         presskey [i] = presskey [i + 1];
167
168                                 presskey [i] = getc();
169                         }
170                 }
171         }
172 #  if DEBUG_BOOTKEYS
173         if (!abort)
174                 printf("key timeout\n");
175 #  endif
176
177         return abort;
178 }
179
180 # else  /* !defined(CONFIG_AUTOBOOT_KEYED) */
181
182 static __inline__ int abortboot(int bootdelay)
183 {
184         int abort = 0;
185
186         printf("Hit any key to stop autoboot: %2d ", bootdelay);
187
188 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
189         /*
190          * Check if key already pressed
191          * Don't check if bootdelay < 0
192          */
193         if (bootdelay >= 0) {
194                 if (tstc()) {   /* we got a key press   */
195                         (void) getc();  /* consume input        */
196                         printf ("\b\b\b 0\n");
197                         return 1;       /* don't auto boot      */
198                 }
199         }
200 #endif
201
202         while (bootdelay > 0) {
203                 int i;
204
205                 --bootdelay;
206                 /* delay 100 * 10ms */
207                 for (i=0; !abort && i<100; ++i) {
208                         if (tstc()) {   /* we got a key press   */
209                                 abort  = 1;     /* don't auto boot      */
210                                 bootdelay = 0;  /* no more delay        */
211                                 (void) getc();  /* consume input        */
212                                 break;
213                         }
214                         udelay (10000);
215                 }
216
217                 printf ("\b\b\b%2d ", bootdelay);
218         }
219
220         putc ('\n');
221
222         return abort;
223 }
224 # endif /* CONFIG_AUTOBOOT_KEYED */
225 #endif  /* CONFIG_BOOTDELAY >= 0  */
226
227 /****************************************************************************/
228
229 void main_loop (void)
230 {
231 #ifndef CFG_HUSH_PARSER
232         static char lastcommand[CFG_CBSIZE] = { 0, };
233         int len;
234         int rc = 1;
235         int flag;
236 #endif
237
238 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
239         char *s;
240         int bootdelay;
241 #endif
242 #ifdef CONFIG_PREBOOT
243         char *p;
244 #endif
245
246 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
247         ulong bmp = 0;          /* default bitmap */
248         extern int trab_vfd (ulong bitmap);
249
250 #ifdef CONFIG_MODEM_SUPPORT
251         if (do_mdm_init)
252                 bmp = 1;        /* alternate bitmap */
253 #endif
254         trab_vfd (bmp);
255 #endif  /* CONFIG_VFD && VFD_TEST_LOGO */
256
257 #ifdef CONFIG_MODEM_SUPPORT
258         debug ("DEBUG: main_loop:   do_mdm_init=%d\n", do_mdm_init);
259         if (do_mdm_init) {
260                 uchar *str = strdup(getenv("mdm_cmd"));
261                 setenv ("preboot", str);  /* set or delete definition */
262                 if (str != NULL)
263                         free (str);
264                 mdm_init(); /* wait for modem connection */
265         }
266 #endif  /* CONFIG_MODEM_SUPPORT */
267
268 #ifdef CFG_HUSH_PARSER
269         u_boot_hush_start ();
270 #endif
271
272 #ifdef CONFIG_PREBOOT
273         if ((p = getenv ("preboot")) != NULL) {
274 # ifdef CONFIG_AUTOBOOT_KEYED
275                 int prev = disable_ctrlc(1);    /* disable Control C checking */
276 # endif
277
278 # ifndef CFG_HUSH_PARSER
279                 run_command (p, 0);
280 # else
281                 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
282                                     FLAG_EXIT_FROM_LOOP);
283 # endif
284
285 # ifdef CONFIG_AUTOBOOT_KEYED
286                 disable_ctrlc(prev);    /* restore Control C checking */
287 # endif
288         }
289 #endif /* CONFIG_PREBOOT */
290
291 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
292         s = getenv ("bootdelay");
293         bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
294
295 #if 0
296         printf ("### main_loop entered:\n\n");
297 #endif
298
299 # ifdef CONFIG_BOOT_RETRY_TIME
300         s = getenv ("bootretry");
301         if (s != NULL)
302                 retry_time = (int)simple_strtoul(s, NULL, 10);
303         else
304                 retry_time =  CONFIG_BOOT_RETRY_TIME;
305         if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
306                 retry_time = CONFIG_BOOT_RETRY_MIN;
307 # endif /* CONFIG_BOOT_RETRY_TIME */
308
309         s = getenv ("bootcmd");
310         if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
311 # ifdef CONFIG_AUTOBOOT_KEYED
312                 int prev = disable_ctrlc(1);    /* disable Control C checking */
313 # endif
314
315 # ifndef CFG_HUSH_PARSER
316                 run_command (s, 0);
317 # else
318                 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
319                                     FLAG_EXIT_FROM_LOOP);
320 # endif
321
322 # ifdef CONFIG_AUTOBOOT_KEYED
323                 disable_ctrlc(prev);    /* restore Control C checking */
324 # endif
325         }
326 #endif  /* CONFIG_BOOTDELAY */
327
328         /*
329          * Main Loop for Monitor Command Processing
330          */
331 #ifdef CFG_HUSH_PARSER
332         parse_file_outer();
333         /* This point is never reached */
334         for (;;);
335 #else
336         for (;;) {
337 #ifdef CONFIG_BOOT_RETRY_TIME
338                 if (rc >= 0) {
339                         /* Saw enough of a valid command to
340                          * restart the timeout.
341                          */
342                         reset_cmd_timeout();
343                 }
344 #endif
345                 len = readline (CFG_PROMPT);
346
347                 flag = 0;       /* assume no special flags for now */
348                 if (len > 0)
349                         strcpy (lastcommand, console_buffer);
350                 else if (len == 0)
351                         flag |= CMD_FLAG_REPEAT;
352 #ifdef CONFIG_BOOT_RETRY_TIME
353                 else if (len == -2) {
354                         /* -2 means timed out, retry autoboot
355                          */
356                         printf("\nTimed out waiting for command\n");
357 # ifdef CONFIG_RESET_TO_RETRY
358                         /* Reinit board to run initialization code again */
359                         do_reset (NULL, 0, 0, NULL);
360 # else
361                         return;         /* retry autoboot */
362 # endif
363                 }
364 #endif
365
366                 if (len == -1)
367                         printf ("<INTERRUPT>\n");
368                 else
369                         rc = run_command (lastcommand, flag);
370
371                 if (rc <= 0) {
372                         /* invalid command or not repeatable, forget it */
373                         lastcommand[0] = 0;
374                 }
375         }
376 #endif /*CFG_HUSH_PARSER*/
377 }
378
379 /***************************************************************************
380  * reset command line timeout to retry_time seconds
381  */
382 #ifdef CONFIG_BOOT_RETRY_TIME
383 void reset_cmd_timeout(void)
384 {
385         endtime = endtick(retry_time);
386 }
387 #endif
388
389 /****************************************************************************/
390
391 /*
392  * Prompt for input and read a line.
393  * If  CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
394  * time out when time goes past endtime (timebase time in ticks).
395  * Return:      number of read characters
396  *              -1 if break
397  *              -2 if timed out
398  */
399 int readline (const char *const prompt)
400 {
401         char   *p = console_buffer;
402         int     n = 0;                          /* buffer index         */
403         int     plen = 0;                       /* prompt length        */
404         int     col;                            /* output column cnt    */
405         char    c;
406
407         /* print prompt */
408         if (prompt) {
409                 plen = strlen (prompt);
410                 puts (prompt);
411         }
412         col = plen;
413
414         for (;;) {
415 #ifdef CONFIG_BOOT_RETRY_TIME
416                 while (!tstc()) {       /* while no incoming data */
417                         if (retry_time >= 0 && get_ticks() > endtime)
418                                 return (-2);    /* timed out */
419                 }
420 #endif
421                 WATCHDOG_RESET();               /* Trigger watchdog, if needed */
422
423 #ifdef CONFIG_SHOW_ACTIVITY
424                 while (!tstc()) {
425                         extern void show_activity(int arg);
426                         show_activity(0);
427                 }
428 #endif
429                 c = getc();
430
431                 /*
432                  * Special character handling
433                  */
434                 switch (c) {
435                 case '\r':                              /* Enter                */
436                 case '\n':
437                         *p = '\0';
438                         puts ("\r\n");
439                         return (p - console_buffer);
440
441                 case 0x03:                              /* ^C - break           */
442                         console_buffer[0] = '\0';       /* discard input */
443                         return (-1);
444
445                 case 0x15:                              /* ^U - erase line      */
446                         while (col > plen) {
447                                 puts (erase_seq);
448                                 --col;
449                         }
450                         p = console_buffer;
451                         n = 0;
452                         continue;
453
454                 case 0x17:                              /* ^W - erase word      */
455                         p=delete_char(console_buffer, p, &col, &n, plen);
456                         while ((n > 0) && (*p != ' ')) {
457                                 p=delete_char(console_buffer, p, &col, &n, plen);
458                         }
459                         continue;
460
461                 case 0x08:                              /* ^H  - backspace      */
462                 case 0x7F:                              /* DEL - backspace      */
463                         p=delete_char(console_buffer, p, &col, &n, plen);
464                         continue;
465
466                 default:
467                         /*
468                          * Must be a normal character then
469                          */
470                         if (n < CFG_CBSIZE-2) {
471                                 if (c == '\t') {        /* expand TABs          */
472                                         puts (tab_seq+(col&07));
473                                         col += 8 - (col&07);
474                                 } else {
475                                         ++col;          /* echo input           */
476                                         putc (c);
477                                 }
478                                 *p++ = c;
479                                 ++n;
480                         } else {                        /* Buffer full          */
481                                 putc ('\a');
482                         }
483                 }
484         }
485 }
486
487 /****************************************************************************/
488
489 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
490 {
491         char *s;
492
493         if (*np == 0) {
494                 return (p);
495         }
496
497         if (*(--p) == '\t') {                   /* will retype the whole line   */
498                 while (*colp > plen) {
499                         puts (erase_seq);
500                         (*colp)--;
501                 }
502                 for (s=buffer; s<p; ++s) {
503                         if (*s == '\t') {
504                                 puts (tab_seq+((*colp) & 07));
505                                 *colp += 8 - ((*colp) & 07);
506                         } else {
507                                 ++(*colp);
508                                 putc (*s);
509                         }
510                 }
511         } else {
512                 puts (erase_seq);
513                 (*colp)--;
514         }
515         (*np)--;
516         return (p);
517 }
518
519 /****************************************************************************/
520
521 int parse_line (char *line, char *argv[])
522 {
523         int nargs = 0;
524
525 #ifdef DEBUG_PARSER
526         printf ("parse_line: \"%s\"\n", line);
527 #endif
528         while (nargs < CFG_MAXARGS) {
529
530                 /* skip any white space */
531                 while ((*line == ' ') || (*line == '\t')) {
532                         ++line;
533                 }
534
535                 if (*line == '\0') {    /* end of line, no more args    */
536                         argv[nargs] = NULL;
537 #ifdef DEBUG_PARSER
538                 printf ("parse_line: nargs=%d\n", nargs);
539 #endif
540                         return (nargs);
541                 }
542
543                 argv[nargs++] = line;   /* begin of argument string     */
544
545                 /* find end of string */
546                 while (*line && (*line != ' ') && (*line != '\t')) {
547                         ++line;
548                 }
549
550                 if (*line == '\0') {    /* end of line, no more args    */
551                         argv[nargs] = NULL;
552 #ifdef DEBUG_PARSER
553                 printf ("parse_line: nargs=%d\n", nargs);
554 #endif
555                         return (nargs);
556                 }
557
558                 *line++ = '\0';         /* terminate current arg         */
559         }
560
561         printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
562
563 #ifdef DEBUG_PARSER
564         printf ("parse_line: nargs=%d\n", nargs);
565 #endif
566         return (nargs);
567 }
568
569 /****************************************************************************/
570
571 static void process_macros (const char *input, char *output)
572 {
573         char c, prev;
574         const char *varname_start = NULL;
575         int inputcnt  = strlen (input);
576         int outputcnt = CFG_CBSIZE;
577         int state = 0;  /* 0 = waiting for '$'  */
578                         /* 1 = waiting for '('  */
579                         /* 2 = waiting for ')'  */
580
581 #ifdef DEBUG_PARSER
582         char *output_start = output;
583
584         printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
585 #endif
586
587         prev = '\0';                    /* previous character   */
588
589         while (inputcnt && outputcnt) {
590             c = *input++;
591             inputcnt--;
592
593             /* remove one level of escape characters */
594             if ((c == '\\') && (prev != '\\')) {
595                 if (inputcnt-- == 0)
596                         break;
597                 prev = c;
598                 c = *input++;
599             }
600
601             switch (state) {
602             case 0:                     /* Waiting for (unescaped) $    */
603                 if ((c == '$') && (prev != '\\')) {
604                         state++;
605                 } else {
606                         *(output++) = c;
607                         outputcnt--;
608                 }
609                 break;
610             case 1:                     /* Waiting for (        */
611                 if (c == '(') {
612                         state++;
613                         varname_start = input;
614                 } else {
615                         state = 0;
616                         *(output++) = '$';
617                         outputcnt--;
618
619                         if (outputcnt) {
620                                 *(output++) = c;
621                                 outputcnt--;
622                         }
623                 }
624                 break;
625             case 2:                     /* Waiting for )        */
626                 if (c == ')') {
627                         int i;
628                         char envname[CFG_CBSIZE], *envval;
629                         int envcnt = input-varname_start-1; /* Varname # of chars */
630
631                         /* Get the varname */
632                         for (i = 0; i < envcnt; i++) {
633                                 envname[i] = varname_start[i];
634                         }
635                         envname[i] = 0;
636
637                         /* Get its value */
638                         envval = getenv (envname);
639
640                         /* Copy into the line if it exists */
641                         if (envval != NULL)
642                                 while ((*envval) && outputcnt) {
643                                         *(output++) = *(envval++);
644                                         outputcnt--;
645                                 }
646                         /* Look for another '$' */
647                         state = 0;
648                 }
649                 break;
650             }
651
652             prev = c;
653         }
654
655         if (outputcnt)
656                 *output = 0;
657
658 #ifdef DEBUG_PARSER
659         printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
660                 strlen(output_start), output_start);
661 #endif
662 }
663
664 /****************************************************************************
665  * returns:
666  *      1  - command executed, repeatable
667  *      0  - command executed but not repeatable, interrupted commands are
668  *           always considered not repeatable
669  *      -1 - not executed (unrecognized, bootd recursion or too many args)
670  *           (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
671  *           considered unrecognized)
672  *
673  * WARNING:
674  *
675  * We must create a temporary copy of the command since the command we get
676  * may be the result from getenv(), which returns a pointer directly to
677  * the environment data, which may change magicly when the command we run
678  * creates or modifies environment variables (like "bootp" does).
679  */
680
681 int run_command (const char *cmd, int flag)
682 {
683         cmd_tbl_t *cmdtp;
684         char cmdbuf[CFG_CBSIZE];        /* working copy of cmd          */
685         char *token;                    /* start of token in cmdbuf     */
686         char *sep;                      /* end of token (separator) in cmdbuf */
687         char finaltoken[CFG_CBSIZE];
688         char *str = cmdbuf;
689         char *argv[CFG_MAXARGS + 1];    /* NULL terminated      */
690         int argc;
691         int repeatable = 1;
692
693 #ifdef DEBUG_PARSER
694         printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
695         puts (cmd ? cmd : "NULL");      /* use puts - string may be loooong */
696         puts ("\"\n");
697 #endif
698
699         clear_ctrlc();          /* forget any previous Control C */
700
701         if (!cmd || !*cmd) {
702                 return -1;      /* empty command */
703         }
704
705         if (strlen(cmd) >= CFG_CBSIZE) {
706                 puts ("## Command too long!\n");
707                 return -1;
708         }
709
710         strcpy (cmdbuf, cmd);
711
712         /* Process separators and check for invalid
713          * repeatable commands
714          */
715
716 #ifdef DEBUG_PARSER
717         printf ("[PROCESS_SEPARATORS] %s\n", cmd);
718 #endif
719         while (*str) {
720
721                 /*
722                  * Find separator, or string end
723                  * Allow simple escape of ';' by writing "\;"
724                  */
725                 for (sep = str; *sep; sep++) {
726                         if ((*sep == ';') &&    /* separator            */
727                             ( sep != str) &&    /* past string start    */
728                             (*(sep-1) != '\\')) /* and NOT escaped      */
729                                 break;
730                 }
731
732                 /*
733                  * Limit the token to data between separators
734                  */
735                 token = str;
736                 if (*sep) {
737                         str = sep + 1;  /* start of command for next pass */
738                         *sep = '\0';
739                 }
740                 else
741                         str = sep;      /* no more commands for next pass */
742 #ifdef DEBUG_PARSER
743                 printf ("token: \"%s\"\n", token);
744 #endif
745
746                 /* find macros in this token and replace them */
747                 process_macros (token, finaltoken);
748
749                 /* Extract arguments */
750                 argc = parse_line (finaltoken, argv);
751
752                 /* Look up command in command table */
753                 if ((cmdtp = find_cmd(argv[0])) == NULL) {
754                         printf ("Unknown command '%s' - try 'help'\n", argv[0]);
755                         return -1;      /* give up after bad command */
756                 }
757
758                 /* found - check max args */
759                 if (argc > cmdtp->maxargs) {
760                         printf ("Usage:\n%s\n", cmdtp->usage);
761                         return -1;
762                 }
763
764 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
765                 /* avoid "bootd" recursion */
766                 if (cmdtp->cmd == do_bootd) {
767 #ifdef DEBUG_PARSER
768                         printf ("[%s]\n", finaltoken);
769 #endif
770                         if (flag & CMD_FLAG_BOOTD) {
771                                 printf ("'bootd' recursion detected\n");
772                                 return -1;
773                         }
774                         else
775                                 flag |= CMD_FLAG_BOOTD;
776                 }
777 #endif  /* CFG_CMD_BOOTD */
778
779                 /* OK - call function to do the command */
780                 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
781                         return (-1);
782                 }
783
784                 repeatable &= cmdtp->repeatable;
785
786                 /* Did the user stop this? */
787                 if (had_ctrlc ())
788                         return 0;       /* if stopped then not repeatable */
789         }
790
791         return repeatable;
792 }
793
794 /****************************************************************************/
795
796 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
797 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
798 {
799         int i;
800         int rcode = 1;
801
802         if (argc < 2) {
803                 printf ("Usage:\n%s\n", cmdtp->usage);
804                 return 1;
805         }
806
807         for (i=1; i<argc; ++i) {
808 #ifndef CFG_HUSH_PARSER
809             if (run_command (getenv (argv[i]), flag) != -1) ++rcode;
810 #else
811             if (parse_string_outer(getenv (argv[i]),
812                     FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) == 0) ++rcode;
813 #endif
814         }
815         return ((rcode == i) ? 0 : 1);
816 }
817 #endif