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,
34 #ifdef CONFIG_MODEM_SUPPORT
35 #include <malloc.h> /* for free() prototype */
38 #ifdef CONFIG_SYS_HUSH_PARSER
44 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
45 DECLARE_GLOBAL_DATA_PTR;
49 * Board-specific Platform code can reimplement show_boot_progress () if needed
51 void inline __show_boot_progress (int val) {}
52 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
54 #if defined(CONFIG_UPDATE_TFTP)
55 int update_tftp (ulong addr);
56 #endif /* CONFIG_UPDATE_TFTP */
58 #define MAX_DELAY_STOP_STR 32
62 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
64 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
65 static const char erase_seq[] = "\b \b"; /* erase sequence */
66 static const char tab_seq[] = " "; /* used to expand TABs */
68 #ifdef CONFIG_BOOT_RETRY_TIME
69 static uint64_t endtime = 0; /* must be set, default is instant timeout */
70 static int retry_time = -1; /* -1 so can call readline before main_loop */
73 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
75 #ifndef CONFIG_BOOT_RETRY_MIN
76 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
79 #ifdef CONFIG_MODEM_SUPPORT
81 extern void mdm_init(void); /* defined in board.c */
84 /***************************************************************************
85 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
86 * returns: 0 - no key string, allow autoboot
87 * 1 - got key string, abort
89 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
90 # if defined(CONFIG_AUTOBOOT_KEYED)
91 static inline int abortboot(int bootdelay)
94 uint64_t etime = endtick(bootdelay);
101 { str: getenv ("bootdelaykey"), retry: 1 },
102 { str: getenv ("bootdelaykey2"), retry: 1 },
103 { str: getenv ("bootstopkey"), retry: 0 },
104 { str: getenv ("bootstopkey2"), retry: 0 },
107 char presskey [MAX_DELAY_STOP_STR];
108 u_int presskey_len = 0;
109 u_int presskey_max = 0;
112 # ifdef CONFIG_AUTOBOOT_PROMPT
113 printf(CONFIG_AUTOBOOT_PROMPT);
116 # ifdef CONFIG_AUTOBOOT_DELAY_STR
117 if (delaykey[0].str == NULL)
118 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
120 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
121 if (delaykey[1].str == NULL)
122 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
124 # ifdef CONFIG_AUTOBOOT_STOP_STR
125 if (delaykey[2].str == NULL)
126 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
128 # ifdef CONFIG_AUTOBOOT_STOP_STR2
129 if (delaykey[3].str == NULL)
130 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
133 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
134 delaykey[i].len = delaykey[i].str == NULL ?
135 0 : strlen (delaykey[i].str);
136 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
137 MAX_DELAY_STOP_STR : delaykey[i].len;
139 presskey_max = presskey_max > delaykey[i].len ?
140 presskey_max : delaykey[i].len;
143 printf("%s key:<%s>\n",
144 delaykey[i].retry ? "delay" : "stop",
145 delaykey[i].str ? delaykey[i].str : "NULL");
149 /* In order to keep up with incoming data, check timeout only
154 if (presskey_len < presskey_max) {
155 presskey [presskey_len ++] = getc();
158 for (i = 0; i < presskey_max - 1; i ++)
159 presskey [i] = presskey [i + 1];
161 presskey [i] = getc();
165 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
166 if (delaykey[i].len > 0 &&
167 presskey_len >= delaykey[i].len &&
168 memcmp (presskey + presskey_len - delaykey[i].len,
170 delaykey[i].len) == 0) {
172 printf("got %skey\n",
173 delaykey[i].retry ? "delay" : "stop");
176 # ifdef CONFIG_BOOT_RETRY_TIME
177 /* don't retry auto boot */
178 if (! delaykey[i].retry)
184 } while (!abort && get_ticks() <= etime);
188 puts("key timeout\n");
191 #ifdef CONFIG_SILENT_CONSOLE
193 gd->flags &= ~GD_FLG_SILENT;
199 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
201 #ifdef CONFIG_MENUKEY
202 static int menukey = 0;
205 static inline int abortboot(int bootdelay)
209 #ifdef CONFIG_MENUPROMPT
210 printf(CONFIG_MENUPROMPT);
212 printf("Hit any key to stop autoboot: %2d ", bootdelay);
215 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
217 * Check if key already pressed
218 * Don't check if bootdelay < 0
220 if (bootdelay >= 0) {
221 if (tstc()) { /* we got a key press */
222 (void) getc(); /* consume input */
224 abort = 1; /* don't auto boot */
229 while ((bootdelay > 0) && (!abort)) {
233 /* delay 100 * 10ms */
234 for (i=0; !abort && i<100; ++i) {
235 if (tstc()) { /* we got a key press */
236 abort = 1; /* don't auto boot */
237 bootdelay = 0; /* no more delay */
238 # ifdef CONFIG_MENUKEY
241 (void) getc(); /* consume input */
248 printf("\b\b\b%2d ", bootdelay);
253 #ifdef CONFIG_SILENT_CONSOLE
255 gd->flags &= ~GD_FLG_SILENT;
260 # endif /* CONFIG_AUTOBOOT_KEYED */
261 #endif /* CONFIG_BOOTDELAY >= 0 */
263 /****************************************************************************/
265 void main_loop (void)
267 #ifndef CONFIG_SYS_HUSH_PARSER
268 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
274 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
278 #ifdef CONFIG_PREBOOT
281 #ifdef CONFIG_BOOTCOUNT_LIMIT
282 unsigned long bootcount = 0;
283 unsigned long bootlimit = 0;
286 #endif /* CONFIG_BOOTCOUNT_LIMIT */
288 #ifdef CONFIG_BOOTCOUNT_LIMIT
289 bootcount = bootcount_load();
291 bootcount_store (bootcount);
292 sprintf (bcs_set, "%lu", bootcount);
293 setenv ("bootcount", bcs_set);
294 bcs = getenv ("bootlimit");
295 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
296 #endif /* CONFIG_BOOTCOUNT_LIMIT */
298 #ifdef CONFIG_MODEM_SUPPORT
299 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
301 char *str = strdup(getenv("mdm_cmd"));
302 setenv ("preboot", str); /* set or delete definition */
305 mdm_init(); /* wait for modem connection */
307 #endif /* CONFIG_MODEM_SUPPORT */
309 #ifdef CONFIG_VERSION_VARIABLE
311 setenv ("ver", version_string); /* set version variable */
313 #endif /* CONFIG_VERSION_VARIABLE */
315 #ifdef CONFIG_SYS_HUSH_PARSER
316 u_boot_hush_start ();
319 #if defined(CONFIG_HUSH_INIT_VAR)
323 #ifdef CONFIG_PREBOOT
324 if ((p = getenv ("preboot")) != NULL) {
325 # ifdef CONFIG_AUTOBOOT_KEYED
326 int prev = disable_ctrlc(1); /* disable Control C checking */
329 # ifndef CONFIG_SYS_HUSH_PARSER
332 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
333 FLAG_EXIT_FROM_LOOP);
336 # ifdef CONFIG_AUTOBOOT_KEYED
337 disable_ctrlc(prev); /* restore Control C checking */
340 #endif /* CONFIG_PREBOOT */
342 #if defined(CONFIG_UPDATE_TFTP)
344 #endif /* CONFIG_UPDATE_TFTP */
346 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
347 s = getenv ("bootdelay");
348 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
350 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
352 # ifdef CONFIG_BOOT_RETRY_TIME
354 # endif /* CONFIG_BOOT_RETRY_TIME */
357 if (gd->flags & GD_FLG_POSTFAIL) {
358 s = getenv("failbootcmd");
361 #endif /* CONFIG_POST */
362 #ifdef CONFIG_BOOTCOUNT_LIMIT
363 if (bootlimit && (bootcount > bootlimit)) {
364 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
365 (unsigned)bootlimit);
366 s = getenv ("altbootcmd");
369 #endif /* CONFIG_BOOTCOUNT_LIMIT */
370 s = getenv ("bootcmd");
372 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
374 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
375 # ifdef CONFIG_AUTOBOOT_KEYED
376 int prev = disable_ctrlc(1); /* disable Control C checking */
379 # ifndef CONFIG_SYS_HUSH_PARSER
382 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
383 FLAG_EXIT_FROM_LOOP);
386 # ifdef CONFIG_AUTOBOOT_KEYED
387 disable_ctrlc(prev); /* restore Control C checking */
391 # ifdef CONFIG_MENUKEY
392 if (menukey == CONFIG_MENUKEY) {
393 s = getenv("menucmd");
395 # ifndef CONFIG_SYS_HUSH_PARSER
398 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
399 FLAG_EXIT_FROM_LOOP);
403 #endif /* CONFIG_MENUKEY */
404 #endif /* CONFIG_BOOTDELAY */
407 * Main Loop for Monitor Command Processing
409 #ifdef CONFIG_SYS_HUSH_PARSER
411 /* This point is never reached */
415 #ifdef CONFIG_BOOT_RETRY_TIME
417 /* Saw enough of a valid command to
418 * restart the timeout.
423 len = readline (CONFIG_SYS_PROMPT);
425 flag = 0; /* assume no special flags for now */
427 strcpy (lastcommand, console_buffer);
429 flag |= CMD_FLAG_REPEAT;
430 #ifdef CONFIG_BOOT_RETRY_TIME
431 else if (len == -2) {
432 /* -2 means timed out, retry autoboot
434 puts ("\nTimed out waiting for command\n");
435 # ifdef CONFIG_RESET_TO_RETRY
436 /* Reinit board to run initialization code again */
437 do_reset (NULL, 0, 0, NULL);
439 return; /* retry autoboot */
445 puts ("<INTERRUPT>\n");
447 rc = run_command (lastcommand, flag);
450 /* invalid command or not repeatable, forget it */
454 #endif /*CONFIG_SYS_HUSH_PARSER*/
457 #ifdef CONFIG_BOOT_RETRY_TIME
458 /***************************************************************************
459 * initialize command line timeout
461 void init_cmd_timeout(void)
463 char *s = getenv ("bootretry");
466 retry_time = (int)simple_strtol(s, NULL, 10);
468 retry_time = CONFIG_BOOT_RETRY_TIME;
470 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
471 retry_time = CONFIG_BOOT_RETRY_MIN;
474 /***************************************************************************
475 * reset command line timeout to retry_time seconds
477 void reset_cmd_timeout(void)
479 endtime = endtick(retry_time);
483 #ifdef CONFIG_CMDLINE_EDITING
486 * cmdline-editing related codes from vivi.
487 * Author: Janghoon Lyu <nandy@mizi.com>
490 #define putnstr(str,n) do { \
491 printf ("%.*s", (int)n, str); \
494 #define CTL_CH(c) ((c) - 'a' + 1)
495 #define CTL_BACKSPACE ('\b')
496 #define DEL ((char)255)
497 #define DEL7 ((char)127)
498 #define CREAD_HIST_CHAR ('!')
500 #define getcmd_putch(ch) putc(ch)
501 #define getcmd_getch() getc()
502 #define getcmd_cbeep() getcmd_putch('\a')
505 #define HIST_SIZE CONFIG_SYS_CBSIZE
507 static int hist_max = 0;
508 static int hist_add_idx = 0;
509 static int hist_cur = -1;
510 unsigned hist_num = 0;
512 char* hist_list[HIST_MAX];
513 char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
515 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
517 static void hist_init(void)
526 for (i = 0; i < HIST_MAX; i++) {
527 hist_list[i] = hist_lines[i];
528 hist_list[i][0] = '\0';
532 static void cread_add_to_hist(char *line)
534 strcpy(hist_list[hist_add_idx], line);
536 if (++hist_add_idx >= HIST_MAX)
539 if (hist_add_idx > hist_max)
540 hist_max = hist_add_idx;
545 static char* hist_prev(void)
557 if (hist_cur == hist_add_idx) {
561 ret = hist_list[hist_cur];
566 static char* hist_next(void)
573 if (hist_cur == hist_add_idx)
576 if (++hist_cur > hist_max)
579 if (hist_cur == hist_add_idx) {
582 ret = hist_list[hist_cur];
587 #ifndef CONFIG_CMDLINE_EDITING
588 static void cread_print_hist_list(void)
593 n = hist_num - hist_max;
595 i = hist_add_idx + 1;
599 if (i == hist_add_idx)
601 printf("%s\n", hist_list[i]);
606 #endif /* CONFIG_CMDLINE_EDITING */
608 #define BEGINNING_OF_LINE() { \
610 getcmd_putch(CTL_BACKSPACE); \
615 #define ERASE_TO_EOL() { \
616 if (num < eol_num) { \
617 printf("%*s", (int)(eol_num - num), ""); \
619 getcmd_putch(CTL_BACKSPACE); \
620 } while (--eol_num > num); \
624 #define REFRESH_TO_EOL() { \
625 if (num < eol_num) { \
626 wlen = eol_num - num; \
627 putnstr(buf + num, wlen); \
632 static void cread_add_char(char ichar, int insert, unsigned long *num,
633 unsigned long *eol_num, char *buf, unsigned long len)
638 if (insert || *num == *eol_num) {
639 if (*eol_num > len - 1) {
647 wlen = *eol_num - *num;
649 memmove(&buf[*num+1], &buf[*num], wlen-1);
653 putnstr(buf + *num, wlen);
656 getcmd_putch(CTL_BACKSPACE);
659 /* echo the character */
662 putnstr(buf + *num, wlen);
667 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
668 unsigned long *eol_num, char *buf, unsigned long len)
671 cread_add_char(*str, insert, num, eol_num, buf, len);
676 static int cread_line(const char *const prompt, char *buf, unsigned int *len)
678 unsigned long num = 0;
679 unsigned long eol_num = 0;
685 int init_len = strlen(buf);
688 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
691 #ifdef CONFIG_BOOT_RETRY_TIME
692 while (!tstc()) { /* while no incoming data */
693 if (retry_time >= 0 && get_ticks() > endtime)
694 return (-2); /* timed out */
699 ichar = getcmd_getch();
701 if ((ichar == '\n') || (ichar == '\r')) {
707 * handle standard linux xterm esc sequences for arrow key, etc.
712 esc_save[esc_len] = ichar;
715 cread_add_str(esc_save, esc_len, insert,
716 &num, &eol_num, buf, *len);
724 case 'D': /* <- key */
728 case 'C': /* -> key */
731 break; /* pass off to ^F handler */
732 case 'H': /* Home key */
735 break; /* pass off to ^A handler */
736 case 'A': /* up arrow */
739 break; /* pass off to ^P handler */
740 case 'B': /* down arrow */
743 break; /* pass off to ^N handler */
745 esc_save[esc_len++] = ichar;
746 cread_add_str(esc_save, esc_len, insert,
747 &num, &eol_num, buf, *len);
756 esc_save[esc_len] = ichar;
759 puts("impossible condition #876\n");
767 case CTL_CH('c'): /* ^C - break */
768 *buf = '\0'; /* discard input */
772 getcmd_putch(buf[num]);
778 getcmd_putch(CTL_BACKSPACE);
784 wlen = eol_num - num - 1;
786 memmove(&buf[num], &buf[num+1], wlen);
787 putnstr(buf + num, wlen);
792 getcmd_putch(CTL_BACKSPACE);
815 wlen = eol_num - num;
817 memmove(&buf[num], &buf[num+1], wlen);
818 getcmd_putch(CTL_BACKSPACE);
819 putnstr(buf + num, wlen);
822 getcmd_putch(CTL_BACKSPACE);
834 if (ichar == CTL_CH('p'))
844 /* nuke the current line */
848 /* erase to end of line */
851 /* copy new line into place and display */
853 eol_num = strlen(buf);
857 #ifdef CONFIG_AUTO_COMPLETE
861 /* do not autocomplete when in the middle */
868 col = strlen(prompt) + eol_num;
870 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
879 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
884 buf[eol_num] = '\0'; /* lose the newline */
886 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
887 cread_add_to_hist(buf);
888 hist_cur = hist_add_idx;
893 #endif /* CONFIG_CMDLINE_EDITING */
895 /****************************************************************************/
898 * Prompt for input and read a line.
899 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
900 * time out when time goes past endtime (timebase time in ticks).
901 * Return: number of read characters
905 int readline (const char *const prompt)
908 * If console_buffer isn't 0-length the user will be prompted to modify
909 * it instead of entering it from scratch as desired.
911 console_buffer[0] = '\0';
913 return readline_into_buffer(prompt, console_buffer);
917 int readline_into_buffer (const char *const prompt, char * buffer)
920 #ifdef CONFIG_CMDLINE_EDITING
921 unsigned int len = CONFIG_SYS_CBSIZE;
923 static int initted = 0;
926 * History uses a global array which is not
927 * writable until after relocation to RAM.
928 * Revert to non-history version if still
929 * running from flash.
931 if (gd->flags & GD_FLG_RELOC) {
940 rc = cread_line(prompt, p, &len);
941 return rc < 0 ? rc : len;
944 #endif /* CONFIG_CMDLINE_EDITING */
946 int n = 0; /* buffer index */
947 int plen = 0; /* prompt length */
948 int col; /* output column cnt */
953 plen = strlen (prompt);
959 #ifdef CONFIG_BOOT_RETRY_TIME
960 while (!tstc()) { /* while no incoming data */
961 if (retry_time >= 0 && get_ticks() > endtime)
962 return (-2); /* timed out */
966 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
968 #ifdef CONFIG_SHOW_ACTIVITY
970 extern void show_activity(int arg);
978 * Special character handling
981 case '\r': /* Enter */
990 case 0x03: /* ^C - break */
991 p_buf[0] = '\0'; /* discard input */
994 case 0x15: /* ^U - erase line */
1003 case 0x17: /* ^W - erase word */
1004 p=delete_char(p_buf, p, &col, &n, plen);
1005 while ((n > 0) && (*p != ' ')) {
1006 p=delete_char(p_buf, p, &col, &n, plen);
1010 case 0x08: /* ^H - backspace */
1011 case 0x7F: /* DEL - backspace */
1012 p=delete_char(p_buf, p, &col, &n, plen);
1017 * Must be a normal character then
1019 if (n < CONFIG_SYS_CBSIZE-2) {
1020 if (c == '\t') { /* expand TABs */
1021 #ifdef CONFIG_AUTO_COMPLETE
1022 /* if auto completion triggered just continue */
1024 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1025 p = p_buf + n; /* reset */
1029 puts (tab_seq+(col&07));
1030 col += 8 - (col&07);
1032 ++col; /* echo input */
1037 } else { /* Buffer full */
1042 #ifdef CONFIG_CMDLINE_EDITING
1047 /****************************************************************************/
1049 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1057 if (*(--p) == '\t') { /* will retype the whole line */
1058 while (*colp > plen) {
1062 for (s=buffer; s<p; ++s) {
1064 puts (tab_seq+((*colp) & 07));
1065 *colp += 8 - ((*colp) & 07);
1079 /****************************************************************************/
1081 int parse_line (char *line, char *argv[])
1086 printf ("parse_line: \"%s\"\n", line);
1088 while (nargs < CONFIG_SYS_MAXARGS) {
1090 /* skip any white space */
1091 while ((*line == ' ') || (*line == '\t')) {
1095 if (*line == '\0') { /* end of line, no more args */
1098 printf ("parse_line: nargs=%d\n", nargs);
1103 argv[nargs++] = line; /* begin of argument string */
1105 /* find end of string */
1106 while (*line && (*line != ' ') && (*line != '\t')) {
1110 if (*line == '\0') { /* end of line, no more args */
1113 printf ("parse_line: nargs=%d\n", nargs);
1118 *line++ = '\0'; /* terminate current arg */
1121 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1124 printf ("parse_line: nargs=%d\n", nargs);
1129 /****************************************************************************/
1131 static void process_macros (const char *input, char *output)
1134 const char *varname_start = NULL;
1135 int inputcnt = strlen (input);
1136 int outputcnt = CONFIG_SYS_CBSIZE;
1137 int state = 0; /* 0 = waiting for '$' */
1139 /* 1 = waiting for '(' or '{' */
1140 /* 2 = waiting for ')' or '}' */
1141 /* 3 = waiting for ''' */
1143 char *output_start = output;
1145 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1149 prev = '\0'; /* previous character */
1151 while (inputcnt && outputcnt) {
1156 /* remove one level of escape characters */
1157 if ((c == '\\') && (prev != '\\')) {
1158 if (inputcnt-- == 0)
1166 case 0: /* Waiting for (unescaped) $ */
1167 if ((c == '\'') && (prev != '\\')) {
1171 if ((c == '$') && (prev != '\\')) {
1178 case 1: /* Waiting for ( */
1179 if (c == '(' || c == '{') {
1181 varname_start = input;
1193 case 2: /* Waiting for ) */
1194 if (c == ')' || c == '}') {
1196 char envname[CONFIG_SYS_CBSIZE], *envval;
1197 int envcnt = input - varname_start - 1; /* Varname # of chars */
1199 /* Get the varname */
1200 for (i = 0; i < envcnt; i++) {
1201 envname[i] = varname_start[i];
1206 envval = getenv (envname);
1208 /* Copy into the line if it exists */
1210 while ((*envval) && outputcnt) {
1211 *(output++) = *(envval++);
1214 /* Look for another '$' */
1218 case 3: /* Waiting for ' */
1219 if ((c == '\'') && (prev != '\\')) {
1236 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1237 strlen (output_start), output_start);
1241 /****************************************************************************
1243 * 1 - command executed, repeatable
1244 * 0 - command executed but not repeatable, interrupted commands are
1245 * always considered not repeatable
1246 * -1 - not executed (unrecognized, bootd recursion or too many args)
1247 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1248 * considered unrecognized)
1252 * We must create a temporary copy of the command since the command we get
1253 * may be the result from getenv(), which returns a pointer directly to
1254 * the environment data, which may change magicly when the command we run
1255 * creates or modifies environment variables (like "bootp" does).
1258 int run_command (const char *cmd, int flag)
1261 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1262 char *token; /* start of token in cmdbuf */
1263 char *sep; /* end of token (separator) in cmdbuf */
1264 char finaltoken[CONFIG_SYS_CBSIZE];
1266 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1272 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1273 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1277 clear_ctrlc(); /* forget any previous Control C */
1279 if (!cmd || !*cmd) {
1280 return -1; /* empty command */
1283 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1284 puts ("## Command too long!\n");
1288 strcpy (cmdbuf, cmd);
1290 /* Process separators and check for invalid
1291 * repeatable commands
1295 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1300 * Find separator, or string end
1301 * Allow simple escape of ';' by writing "\;"
1303 for (inquotes = 0, sep = str; *sep; sep++) {
1309 (*sep == ';') && /* separator */
1310 ( sep != str) && /* past string start */
1311 (*(sep-1) != '\\')) /* and NOT escaped */
1316 * Limit the token to data between separators
1320 str = sep + 1; /* start of command for next pass */
1324 str = sep; /* no more commands for next pass */
1326 printf ("token: \"%s\"\n", token);
1329 /* find macros in this token and replace them */
1330 process_macros (token, finaltoken);
1332 /* Extract arguments */
1333 if ((argc = parse_line (finaltoken, argv)) == 0) {
1334 rc = -1; /* no command at all */
1338 /* Look up command in command table */
1339 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1340 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1341 rc = -1; /* give up after bad command */
1345 /* found - check max args */
1346 if (argc > cmdtp->maxargs) {
1352 #if defined(CONFIG_CMD_BOOTD)
1353 /* avoid "bootd" recursion */
1354 if (cmdtp->cmd == do_bootd) {
1356 printf ("[%s]\n", finaltoken);
1358 if (flag & CMD_FLAG_BOOTD) {
1359 puts ("'bootd' recursion detected\n");
1363 flag |= CMD_FLAG_BOOTD;
1368 /* OK - call function to do the command */
1369 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1373 repeatable &= cmdtp->repeatable;
1375 /* Did the user stop this? */
1377 return -1; /* if stopped then not repeatable */
1380 return rc ? rc : repeatable;
1383 /****************************************************************************/
1385 #if defined(CONFIG_CMD_RUN)
1386 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1391 return cmd_usage(cmdtp);
1393 for (i=1; i<argc; ++i) {
1396 if ((arg = getenv (argv[i])) == NULL) {
1397 printf ("## Error: \"%s\" not defined\n", argv[i]);
1400 #ifndef CONFIG_SYS_HUSH_PARSER
1401 if (run_command (arg, flag) == -1)
1404 if (parse_string_outer(arg,
1405 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)