3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Add to readline cmdline-editing by
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9 * See file CREDITS for list of people who contributed to this
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.
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.
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,
33 #ifdef CONFIG_MODEM_SUPPORT
34 #include <malloc.h> /* for free() prototype */
37 #ifdef CFG_HUSH_PARSER
43 #ifdef CONFIG_SILENT_CONSOLE
44 DECLARE_GLOBAL_DATA_PTR;
48 * Board-specific Platform code can reimplement show_boot_progress () if needed
50 void inline __show_boot_progress (int val) {}
51 void inline show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
53 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
54 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
57 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
60 #define MAX_DELAY_STOP_STR 32
62 static int parse_line (char *, char *[]);
63 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
64 static int abortboot(int);
69 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
71 #ifndef CONFIG_CMDLINE_EDITING
72 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
73 static char erase_seq[] = "\b \b"; /* erase sequence */
74 static char tab_seq[] = " "; /* used to expand TABs */
75 #endif /* CONFIG_CMDLINE_EDITING */
77 #ifdef CONFIG_BOOT_RETRY_TIME
78 static uint64_t endtime = 0; /* must be set, default is instant timeout */
79 static int retry_time = -1; /* -1 so can call readline before main_loop */
82 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
84 #ifndef CONFIG_BOOT_RETRY_MIN
85 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
88 #ifdef CONFIG_MODEM_SUPPORT
90 extern void mdm_init(void); /* defined in board.c */
93 /***************************************************************************
94 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
95 * returns: 0 - no key string, allow autoboot
96 * 1 - got key string, abort
98 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
99 # if defined(CONFIG_AUTOBOOT_KEYED)
100 static __inline__ int abortboot(int bootdelay)
103 uint64_t etime = endtick(bootdelay);
110 { str: getenv ("bootdelaykey"), retry: 1 },
111 { str: getenv ("bootdelaykey2"), retry: 1 },
112 { str: getenv ("bootstopkey"), retry: 0 },
113 { str: getenv ("bootstopkey2"), retry: 0 },
116 char presskey [MAX_DELAY_STOP_STR];
117 u_int presskey_len = 0;
118 u_int presskey_max = 0;
121 # ifdef CONFIG_AUTOBOOT_PROMPT
122 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
125 # ifdef CONFIG_AUTOBOOT_DELAY_STR
126 if (delaykey[0].str == NULL)
127 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
129 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
130 if (delaykey[1].str == NULL)
131 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
133 # ifdef CONFIG_AUTOBOOT_STOP_STR
134 if (delaykey[2].str == NULL)
135 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
137 # ifdef CONFIG_AUTOBOOT_STOP_STR2
138 if (delaykey[3].str == NULL)
139 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
142 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
143 delaykey[i].len = delaykey[i].str == NULL ?
144 0 : strlen (delaykey[i].str);
145 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
146 MAX_DELAY_STOP_STR : delaykey[i].len;
148 presskey_max = presskey_max > delaykey[i].len ?
149 presskey_max : delaykey[i].len;
152 printf("%s key:<%s>\n",
153 delaykey[i].retry ? "delay" : "stop",
154 delaykey[i].str ? delaykey[i].str : "NULL");
158 /* In order to keep up with incoming data, check timeout only
161 while (!abort && get_ticks() <= etime) {
162 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
163 if (delaykey[i].len > 0 &&
164 presskey_len >= delaykey[i].len &&
165 memcmp (presskey + presskey_len - delaykey[i].len,
167 delaykey[i].len) == 0) {
169 printf("got %skey\n",
170 delaykey[i].retry ? "delay" : "stop");
173 # ifdef CONFIG_BOOT_RETRY_TIME
174 /* don't retry auto boot */
175 if (! delaykey[i].retry)
183 if (presskey_len < presskey_max) {
184 presskey [presskey_len ++] = getc();
187 for (i = 0; i < presskey_max - 1; i ++)
188 presskey [i] = presskey [i + 1];
190 presskey [i] = getc();
196 puts("key timeout\n");
199 #ifdef CONFIG_SILENT_CONSOLE
201 gd->flags &= ~GD_FLG_SILENT;
207 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
209 #ifdef CONFIG_MENUKEY
210 static int menukey = 0;
213 static __inline__ int abortboot(int bootdelay)
217 #ifdef CONFIG_MENUPROMPT
218 printf(CONFIG_MENUPROMPT, bootdelay);
220 printf("Hit any key to stop autoboot: %2d ", bootdelay);
223 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
228 if (bootdelay >= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
232 abort = 1; /* don't auto boot */
237 while ((bootdelay > 0) && (!abort)) {
241 /* delay 100 * 10ms */
242 for (i=0; !abort && i<100; ++i) {
243 if (tstc()) { /* we got a key press */
244 abort = 1; /* don't auto boot */
245 bootdelay = 0; /* no more delay */
246 # ifdef CONFIG_MENUKEY
249 (void) getc(); /* consume input */
256 printf("\b\b\b%2d ", bootdelay);
261 #ifdef CONFIG_SILENT_CONSOLE
263 gd->flags &= ~GD_FLG_SILENT;
268 # endif /* CONFIG_AUTOBOOT_KEYED */
269 #endif /* CONFIG_BOOTDELAY >= 0 */
271 /****************************************************************************/
273 void main_loop (void)
275 #ifndef CFG_HUSH_PARSER
276 static char lastcommand[CFG_CBSIZE] = { 0, };
282 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
286 #ifdef CONFIG_PREBOOT
289 #ifdef CONFIG_BOOTCOUNT_LIMIT
290 unsigned long bootcount = 0;
291 unsigned long bootlimit = 0;
294 #endif /* CONFIG_BOOTCOUNT_LIMIT */
296 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
297 ulong bmp = 0; /* default bitmap */
298 extern int trab_vfd (ulong bitmap);
300 #ifdef CONFIG_MODEM_SUPPORT
302 bmp = 1; /* alternate bitmap */
305 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
307 #ifdef CONFIG_BOOTCOUNT_LIMIT
308 bootcount = bootcount_load();
310 bootcount_store (bootcount);
311 sprintf (bcs_set, "%lu", bootcount);
312 setenv ("bootcount", bcs_set);
313 bcs = getenv ("bootlimit");
314 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
315 #endif /* CONFIG_BOOTCOUNT_LIMIT */
317 #ifdef CONFIG_MODEM_SUPPORT
318 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
320 char *str = strdup(getenv("mdm_cmd"));
321 setenv ("preboot", str); /* set or delete definition */
324 mdm_init(); /* wait for modem connection */
326 #endif /* CONFIG_MODEM_SUPPORT */
328 #ifdef CONFIG_VERSION_VARIABLE
330 extern char version_string[];
332 setenv ("ver", version_string); /* set version variable */
334 #endif /* CONFIG_VERSION_VARIABLE */
336 #ifdef CFG_HUSH_PARSER
337 u_boot_hush_start ();
340 #ifdef CONFIG_AUTO_COMPLETE
341 install_auto_complete();
344 #ifdef CONFIG_PREBOOT
345 if ((p = getenv ("preboot")) != NULL) {
346 # ifdef CONFIG_AUTOBOOT_KEYED
347 int prev = disable_ctrlc(1); /* disable Control C checking */
350 # ifndef CFG_HUSH_PARSER
353 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
354 FLAG_EXIT_FROM_LOOP);
357 # ifdef CONFIG_AUTOBOOT_KEYED
358 disable_ctrlc(prev); /* restore Control C checking */
361 #endif /* CONFIG_PREBOOT */
363 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
364 s = getenv ("bootdelay");
365 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
367 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
369 # ifdef CONFIG_BOOT_RETRY_TIME
371 # endif /* CONFIG_BOOT_RETRY_TIME */
373 #ifdef CONFIG_BOOTCOUNT_LIMIT
374 if (bootlimit && (bootcount > bootlimit)) {
375 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
376 (unsigned)bootlimit);
377 s = getenv ("altbootcmd");
380 #endif /* CONFIG_BOOTCOUNT_LIMIT */
381 s = getenv ("bootcmd");
383 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
385 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
386 # ifdef CONFIG_AUTOBOOT_KEYED
387 int prev = disable_ctrlc(1); /* disable Control C checking */
390 # ifndef CFG_HUSH_PARSER
393 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
394 FLAG_EXIT_FROM_LOOP);
397 # ifdef CONFIG_AUTOBOOT_KEYED
398 disable_ctrlc(prev); /* restore Control C checking */
402 # ifdef CONFIG_MENUKEY
403 if (menukey == CONFIG_MENUKEY) {
404 s = getenv("menucmd");
406 # ifndef CFG_HUSH_PARSER
409 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
410 FLAG_EXIT_FROM_LOOP);
414 #endif /* CONFIG_MENUKEY */
415 #endif /* CONFIG_BOOTDELAY */
417 #ifdef CONFIG_AMIGAONEG3SE
419 extern void video_banner(void);
425 * Main Loop for Monitor Command Processing
427 #ifdef CFG_HUSH_PARSER
429 /* This point is never reached */
433 #ifdef CONFIG_BOOT_RETRY_TIME
435 /* Saw enough of a valid command to
436 * restart the timeout.
441 len = readline (CFG_PROMPT);
443 flag = 0; /* assume no special flags for now */
445 strcpy (lastcommand, console_buffer);
447 flag |= CMD_FLAG_REPEAT;
448 #ifdef CONFIG_BOOT_RETRY_TIME
449 else if (len == -2) {
450 /* -2 means timed out, retry autoboot
452 puts ("\nTimed out waiting for command\n");
453 # ifdef CONFIG_RESET_TO_RETRY
454 /* Reinit board to run initialization code again */
455 do_reset (NULL, 0, 0, NULL);
457 return; /* retry autoboot */
463 puts ("<INTERRUPT>\n");
465 rc = run_command (lastcommand, flag);
468 /* invalid command or not repeatable, forget it */
472 #endif /*CFG_HUSH_PARSER*/
475 #ifdef CONFIG_BOOT_RETRY_TIME
476 /***************************************************************************
477 * initialize command line timeout
479 void init_cmd_timeout(void)
481 char *s = getenv ("bootretry");
484 retry_time = (int)simple_strtol(s, NULL, 10);
486 retry_time = CONFIG_BOOT_RETRY_TIME;
488 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
489 retry_time = CONFIG_BOOT_RETRY_MIN;
492 /***************************************************************************
493 * reset command line timeout to retry_time seconds
495 void reset_cmd_timeout(void)
497 endtime = endtick(retry_time);
501 #ifdef CONFIG_CMDLINE_EDITING
504 * cmdline-editing related codes from vivi.
505 * Author: Janghoon Lyu <nandy@mizi.com>
508 #define putnstr(str,n) do { \
509 printf ("%.*s", n, str); \
512 #define CTL_CH(c) ((c) - 'a' + 1)
514 #define MAX_CMDBUF_SIZE 256
516 #define CTL_BACKSPACE ('\b')
517 #define DEL ((char)255)
518 #define DEL7 ((char)127)
519 #define CREAD_HIST_CHAR ('!')
521 #define getcmd_putch(ch) putc(ch)
522 #define getcmd_getch() getc()
523 #define getcmd_cbeep() getcmd_putch('\a')
526 #define HIST_SIZE MAX_CMDBUF_SIZE
528 static int hist_max = 0;
529 static int hist_add_idx = 0;
530 static int hist_cur = -1;
531 unsigned hist_num = 0;
533 char* hist_list[HIST_MAX];
534 char hist_lines[HIST_MAX][HIST_SIZE];
536 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
538 static void hist_init(void)
547 for (i = 0; i < HIST_MAX; i++) {
548 hist_list[i] = hist_lines[i];
549 hist_list[i][0] = '\0';
553 static void cread_add_to_hist(char *line)
555 strcpy(hist_list[hist_add_idx], line);
557 if (++hist_add_idx >= HIST_MAX)
560 if (hist_add_idx > hist_max)
561 hist_max = hist_add_idx;
566 static char* hist_prev(void)
578 if (hist_cur == hist_add_idx) {
582 ret = hist_list[hist_cur];
587 static char* hist_next(void)
594 if (hist_cur == hist_add_idx)
597 if (++hist_cur > hist_max)
600 if (hist_cur == hist_add_idx) {
603 ret = hist_list[hist_cur];
608 #ifndef CONFIG_CMDLINE_EDITING
609 static void cread_print_hist_list(void)
614 n = hist_num - hist_max;
616 i = hist_add_idx + 1;
620 if (i == hist_add_idx)
622 printf("%s\n", hist_list[i]);
627 #endif /* CONFIG_CMDLINE_EDITING */
629 #define BEGINNING_OF_LINE() { \
631 getcmd_putch(CTL_BACKSPACE); \
636 #define ERASE_TO_EOL() { \
637 if (num < eol_num) { \
639 for (tmp = num; tmp < eol_num; tmp++) \
641 while (tmp-- > num) \
642 getcmd_putch(CTL_BACKSPACE); \
647 #define REFRESH_TO_EOL() { \
648 if (num < eol_num) { \
649 wlen = eol_num - num; \
650 putnstr(buf + num, wlen); \
655 static void cread_add_char(char ichar, int insert, unsigned long *num,
656 unsigned long *eol_num, char *buf, unsigned long len)
661 if (insert || *num == *eol_num) {
662 if (*eol_num > len - 1) {
670 wlen = *eol_num - *num;
672 memmove(&buf[*num+1], &buf[*num], wlen-1);
676 putnstr(buf + *num, wlen);
679 getcmd_putch(CTL_BACKSPACE);
682 /* echo the character */
685 putnstr(buf + *num, wlen);
690 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
691 unsigned long *eol_num, char *buf, unsigned long len)
694 cread_add_char(*str, insert, num, eol_num, buf, len);
699 static int cread_line(char *buf, unsigned int *len)
701 unsigned long num = 0;
702 unsigned long eol_num = 0;
713 ichar = getcmd_getch();
715 if ((ichar == '\n') || (ichar == '\r')) {
721 * handle standard linux xterm esc sequences for arrow key, etc.
726 esc_save[esc_len] = ichar;
729 cread_add_str(esc_save, esc_len, insert,
730 &num, &eol_num, buf, *len);
738 case 'D': /* <- key */
742 case 'C': /* -> key */
745 break; /* pass off to ^F handler */
746 case 'H': /* Home key */
749 break; /* pass off to ^A handler */
750 case 'A': /* up arrow */
753 break; /* pass off to ^P handler */
754 case 'B': /* down arrow */
757 break; /* pass off to ^N handler */
759 esc_save[esc_len++] = ichar;
760 cread_add_str(esc_save, esc_len, insert,
761 &num, &eol_num, buf, *len);
770 esc_save[esc_len] = ichar;
773 puts("impossible condition #876\n");
781 case CTL_CH('c'): /* ^C - break */
782 *buf = '\0'; /* discard input */
786 getcmd_putch(buf[num]);
792 getcmd_putch(CTL_BACKSPACE);
798 wlen = eol_num - num - 1;
800 memmove(&buf[num], &buf[num+1], wlen);
801 putnstr(buf + num, wlen);
806 getcmd_putch(CTL_BACKSPACE);
828 wlen = eol_num - num;
830 memmove(&buf[num], &buf[num+1], wlen);
831 getcmd_putch(CTL_BACKSPACE);
832 putnstr(buf + num, wlen);
835 getcmd_putch(CTL_BACKSPACE);
847 if (ichar == CTL_CH('p'))
857 /* nuke the current line */
861 /* erase to end of line */
864 /* copy new line into place and display */
866 eol_num = strlen(buf);
871 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
876 buf[eol_num] = '\0'; /* lose the newline */
878 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
879 cread_add_to_hist(buf);
880 hist_cur = hist_add_idx;
885 #endif /* CONFIG_CMDLINE_EDITING */
887 /****************************************************************************/
890 * Prompt for input and read a line.
891 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
892 * time out when time goes past endtime (timebase time in ticks).
893 * Return: number of read characters
897 int readline (const char *const prompt)
899 #ifdef CONFIG_CMDLINE_EDITING
900 char *p = console_buffer;
901 unsigned int len=MAX_CMDBUF_SIZE;
903 static int initted = 0;
912 rc = cread_line(p, &len);
913 return rc < 0 ? rc : len;
915 char *p = console_buffer;
916 int n = 0; /* buffer index */
917 int plen = 0; /* prompt length */
918 int col; /* output column cnt */
923 plen = strlen (prompt);
929 #ifdef CONFIG_BOOT_RETRY_TIME
930 while (!tstc()) { /* while no incoming data */
931 if (retry_time >= 0 && get_ticks() > endtime)
932 return (-2); /* timed out */
935 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
937 #ifdef CONFIG_SHOW_ACTIVITY
939 extern void show_activity(int arg);
946 * Special character handling
949 case '\r': /* Enter */
953 return (p - console_buffer);
958 case 0x03: /* ^C - break */
959 console_buffer[0] = '\0'; /* discard input */
962 case 0x15: /* ^U - erase line */
971 case 0x17: /* ^W - erase word */
972 p=delete_char(console_buffer, p, &col, &n, plen);
973 while ((n > 0) && (*p != ' ')) {
974 p=delete_char(console_buffer, p, &col, &n, plen);
978 case 0x08: /* ^H - backspace */
979 case 0x7F: /* DEL - backspace */
980 p=delete_char(console_buffer, p, &col, &n, plen);
985 * Must be a normal character then
987 if (n < CFG_CBSIZE-2) {
988 if (c == '\t') { /* expand TABs */
989 #ifdef CONFIG_AUTO_COMPLETE
990 /* if auto completion triggered just continue */
992 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
993 p = console_buffer + n; /* reset */
997 puts (tab_seq+(col&07));
1000 ++col; /* echo input */
1005 } else { /* Buffer full */
1010 #endif /* CONFIG_CMDLINE_EDITING */
1013 /****************************************************************************/
1015 #ifndef CONFIG_CMDLINE_EDITING
1016 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1024 if (*(--p) == '\t') { /* will retype the whole line */
1025 while (*colp > plen) {
1029 for (s=buffer; s<p; ++s) {
1031 puts (tab_seq+((*colp) & 07));
1032 *colp += 8 - ((*colp) & 07);
1045 #endif /* CONFIG_CMDLINE_EDITING */
1047 /****************************************************************************/
1049 int parse_line (char *line, char *argv[])
1054 printf ("parse_line: \"%s\"\n", line);
1056 while (nargs < CFG_MAXARGS) {
1058 /* skip any white space */
1059 while ((*line == ' ') || (*line == '\t')) {
1063 if (*line == '\0') { /* end of line, no more args */
1066 printf ("parse_line: nargs=%d\n", nargs);
1071 argv[nargs++] = line; /* begin of argument string */
1073 /* find end of string */
1074 while (*line && (*line != ' ') && (*line != '\t')) {
1078 if (*line == '\0') { /* end of line, no more args */
1081 printf ("parse_line: nargs=%d\n", nargs);
1086 *line++ = '\0'; /* terminate current arg */
1089 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1092 printf ("parse_line: nargs=%d\n", nargs);
1097 /****************************************************************************/
1099 static void process_macros (const char *input, char *output)
1102 const char *varname_start = NULL;
1103 int inputcnt = strlen (input);
1104 int outputcnt = CFG_CBSIZE;
1105 int state = 0; /* 0 = waiting for '$' */
1107 /* 1 = waiting for '(' or '{' */
1108 /* 2 = waiting for ')' or '}' */
1109 /* 3 = waiting for ''' */
1111 char *output_start = output;
1113 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1117 prev = '\0'; /* previous character */
1119 while (inputcnt && outputcnt) {
1124 /* remove one level of escape characters */
1125 if ((c == '\\') && (prev != '\\')) {
1126 if (inputcnt-- == 0)
1134 case 0: /* Waiting for (unescaped) $ */
1135 if ((c == '\'') && (prev != '\\')) {
1139 if ((c == '$') && (prev != '\\')) {
1146 case 1: /* Waiting for ( */
1147 if (c == '(' || c == '{') {
1149 varname_start = input;
1161 case 2: /* Waiting for ) */
1162 if (c == ')' || c == '}') {
1164 char envname[CFG_CBSIZE], *envval;
1165 int envcnt = input - varname_start - 1; /* Varname # of chars */
1167 /* Get the varname */
1168 for (i = 0; i < envcnt; i++) {
1169 envname[i] = varname_start[i];
1174 envval = getenv (envname);
1176 /* Copy into the line if it exists */
1178 while ((*envval) && outputcnt) {
1179 *(output++) = *(envval++);
1182 /* Look for another '$' */
1186 case 3: /* Waiting for ' */
1187 if ((c == '\'') && (prev != '\\')) {
1204 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1205 strlen (output_start), output_start);
1209 /****************************************************************************
1211 * 1 - command executed, repeatable
1212 * 0 - command executed but not repeatable, interrupted commands are
1213 * always considered not repeatable
1214 * -1 - not executed (unrecognized, bootd recursion or too many args)
1215 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1216 * considered unrecognized)
1220 * We must create a temporary copy of the command since the command we get
1221 * may be the result from getenv(), which returns a pointer directly to
1222 * the environment data, which may change magicly when the command we run
1223 * creates or modifies environment variables (like "bootp" does).
1226 int run_command (const char *cmd, int flag)
1229 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1230 char *token; /* start of token in cmdbuf */
1231 char *sep; /* end of token (separator) in cmdbuf */
1232 char finaltoken[CFG_CBSIZE];
1234 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1240 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1241 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1245 clear_ctrlc(); /* forget any previous Control C */
1247 if (!cmd || !*cmd) {
1248 return -1; /* empty command */
1251 if (strlen(cmd) >= CFG_CBSIZE) {
1252 puts ("## Command too long!\n");
1256 strcpy (cmdbuf, cmd);
1258 /* Process separators and check for invalid
1259 * repeatable commands
1263 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1268 * Find separator, or string end
1269 * Allow simple escape of ';' by writing "\;"
1271 for (inquotes = 0, sep = str; *sep; sep++) {
1277 (*sep == ';') && /* separator */
1278 ( sep != str) && /* past string start */
1279 (*(sep-1) != '\\')) /* and NOT escaped */
1284 * Limit the token to data between separators
1288 str = sep + 1; /* start of command for next pass */
1292 str = sep; /* no more commands for next pass */
1294 printf ("token: \"%s\"\n", token);
1297 /* find macros in this token and replace them */
1298 process_macros (token, finaltoken);
1300 /* Extract arguments */
1301 if ((argc = parse_line (finaltoken, argv)) == 0) {
1302 rc = -1; /* no command at all */
1306 /* Look up command in command table */
1307 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1308 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1309 rc = -1; /* give up after bad command */
1313 /* found - check max args */
1314 if (argc > cmdtp->maxargs) {
1315 printf ("Usage:\n%s\n", cmdtp->usage);
1320 #if defined(CONFIG_CMD_BOOTD)
1321 /* avoid "bootd" recursion */
1322 if (cmdtp->cmd == do_bootd) {
1324 printf ("[%s]\n", finaltoken);
1326 if (flag & CMD_FLAG_BOOTD) {
1327 puts ("'bootd' recursion detected\n");
1331 flag |= CMD_FLAG_BOOTD;
1336 /* OK - call function to do the command */
1337 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1341 repeatable &= cmdtp->repeatable;
1343 /* Did the user stop this? */
1345 return -1; /* if stopped then not repeatable */
1348 return rc ? rc : repeatable;
1351 /****************************************************************************/
1353 #if defined(CONFIG_CMD_RUN)
1354 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1359 printf ("Usage:\n%s\n", cmdtp->usage);
1363 for (i=1; i<argc; ++i) {
1366 if ((arg = getenv (argv[i])) == NULL) {
1367 printf ("## Error: \"%s\" not defined\n", argv[i]);
1370 #ifndef CFG_HUSH_PARSER
1371 if (run_command (arg, flag) == -1)
1374 if (parse_string_outer(arg,
1375 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)