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,
35 #ifdef CONFIG_MODEM_SUPPORT
36 #include <malloc.h> /* for free() prototype */
39 #ifdef CONFIG_SYS_HUSH_PARSER
44 #include <linux/ctype.h>
47 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
48 DECLARE_GLOBAL_DATA_PTR;
52 * Board-specific Platform code can reimplement show_boot_progress () if needed
54 void inline __show_boot_progress (int val) {}
55 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
57 #if defined(CONFIG_UPDATE_TFTP)
58 int update_tftp (ulong addr);
59 #endif /* CONFIG_UPDATE_TFTP */
61 #define MAX_DELAY_STOP_STR 32
65 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
67 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
68 static const char erase_seq[] = "\b \b"; /* erase sequence */
69 static const char tab_seq[] = " "; /* used to expand TABs */
71 #ifdef CONFIG_BOOT_RETRY_TIME
72 static uint64_t endtime = 0; /* must be set, default is instant timeout */
73 static int retry_time = -1; /* -1 so can call readline before main_loop */
76 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
78 #ifndef CONFIG_BOOT_RETRY_MIN
79 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
82 #ifdef CONFIG_MODEM_SUPPORT
84 extern void mdm_init(void); /* defined in board.c */
87 /***************************************************************************
88 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
89 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
91 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
92 # if defined(CONFIG_AUTOBOOT_KEYED)
96 int abortboot(int bootdelay)
99 uint64_t etime = endtick(bootdelay);
106 { str: getenv ("bootdelaykey"), retry: 1 },
107 { str: getenv ("bootdelaykey2"), retry: 1 },
108 { str: getenv ("bootstopkey"), retry: 0 },
109 { str: getenv ("bootstopkey2"), retry: 0 },
112 char presskey [MAX_DELAY_STOP_STR];
113 u_int presskey_len = 0;
114 u_int presskey_max = 0;
117 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
122 # ifdef CONFIG_AUTOBOOT_PROMPT
123 printf(CONFIG_AUTOBOOT_PROMPT);
126 # ifdef CONFIG_AUTOBOOT_DELAY_STR
127 if (delaykey[0].str == NULL)
128 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
130 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
131 if (delaykey[1].str == NULL)
132 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
134 # ifdef CONFIG_AUTOBOOT_STOP_STR
135 if (delaykey[2].str == NULL)
136 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
138 # ifdef CONFIG_AUTOBOOT_STOP_STR2
139 if (delaykey[3].str == NULL)
140 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
143 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
144 delaykey[i].len = delaykey[i].str == NULL ?
145 0 : strlen (delaykey[i].str);
146 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
147 MAX_DELAY_STOP_STR : delaykey[i].len;
149 presskey_max = presskey_max > delaykey[i].len ?
150 presskey_max : delaykey[i].len;
153 printf("%s key:<%s>\n",
154 delaykey[i].retry ? "delay" : "stop",
155 delaykey[i].str ? delaykey[i].str : "NULL");
159 /* In order to keep up with incoming data, check timeout only
164 if (presskey_len < presskey_max) {
165 presskey [presskey_len ++] = getc();
168 for (i = 0; i < presskey_max - 1; i ++)
169 presskey [i] = presskey [i + 1];
171 presskey [i] = getc();
175 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
176 if (delaykey[i].len > 0 &&
177 presskey_len >= delaykey[i].len &&
178 memcmp (presskey + presskey_len - delaykey[i].len,
180 delaykey[i].len) == 0) {
182 printf("got %skey\n",
183 delaykey[i].retry ? "delay" : "stop");
186 # ifdef CONFIG_BOOT_RETRY_TIME
187 /* don't retry auto boot */
188 if (! delaykey[i].retry)
194 } while (!abort && get_ticks() <= etime);
198 puts("key timeout\n");
201 #ifdef CONFIG_SILENT_CONSOLE
203 gd->flags &= ~GD_FLG_SILENT;
209 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
211 #ifdef CONFIG_MENUKEY
212 static int menukey = 0;
218 int abortboot(int bootdelay)
222 #ifdef CONFIG_MENUPROMPT
223 printf(CONFIG_MENUPROMPT);
226 printf("Hit any key to stop autoboot: %2d ", bootdelay);
229 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
231 * Check if key already pressed
232 * Don't check if bootdelay < 0
234 if (bootdelay >= 0) {
235 if (tstc()) { /* we got a key press */
236 (void) getc(); /* consume input */
238 abort = 1; /* don't auto boot */
243 while ((bootdelay > 0) && (!abort)) {
247 /* delay 100 * 10ms */
248 for (i=0; !abort && i<100; ++i) {
249 if (tstc()) { /* we got a key press */
250 abort = 1; /* don't auto boot */
251 bootdelay = 0; /* no more delay */
252 # ifdef CONFIG_MENUKEY
255 (void) getc(); /* consume input */
262 printf("\b\b\b%2d ", bootdelay);
267 #ifdef CONFIG_SILENT_CONSOLE
269 gd->flags &= ~GD_FLG_SILENT;
274 # endif /* CONFIG_AUTOBOOT_KEYED */
275 #endif /* CONFIG_BOOTDELAY >= 0 */
277 /****************************************************************************/
279 void main_loop (void)
281 #ifndef CONFIG_SYS_HUSH_PARSER
282 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
288 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
292 #ifdef CONFIG_PREBOOT
295 #ifdef CONFIG_BOOTCOUNT_LIMIT
296 unsigned long bootcount = 0;
297 unsigned long bootlimit = 0;
300 #endif /* CONFIG_BOOTCOUNT_LIMIT */
302 #ifdef CONFIG_BOOTCOUNT_LIMIT
303 bootcount = bootcount_load();
305 bootcount_store (bootcount);
306 sprintf (bcs_set, "%lu", bootcount);
307 setenv ("bootcount", bcs_set);
308 bcs = getenv ("bootlimit");
309 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
310 #endif /* CONFIG_BOOTCOUNT_LIMIT */
312 #ifdef CONFIG_MODEM_SUPPORT
313 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
315 char *str = strdup(getenv("mdm_cmd"));
316 setenv ("preboot", str); /* set or delete definition */
319 mdm_init(); /* wait for modem connection */
321 #endif /* CONFIG_MODEM_SUPPORT */
323 #ifdef CONFIG_VERSION_VARIABLE
325 setenv ("ver", version_string); /* set version variable */
327 #endif /* CONFIG_VERSION_VARIABLE */
329 #ifdef CONFIG_SYS_HUSH_PARSER
330 u_boot_hush_start ();
333 #if defined(CONFIG_HUSH_INIT_VAR)
337 #ifdef CONFIG_PREBOOT
338 if ((p = getenv ("preboot")) != NULL) {
339 # ifdef CONFIG_AUTOBOOT_KEYED
340 int prev = disable_ctrlc(1); /* disable Control C checking */
343 run_command_list(p, -1, 0);
345 # ifdef CONFIG_AUTOBOOT_KEYED
346 disable_ctrlc(prev); /* restore Control C checking */
349 #endif /* CONFIG_PREBOOT */
351 #if defined(CONFIG_UPDATE_TFTP)
353 #endif /* CONFIG_UPDATE_TFTP */
355 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
356 s = getenv ("bootdelay");
357 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
359 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
361 #if defined(CONFIG_MENU_SHOW)
362 bootdelay = menu_show(bootdelay);
364 # ifdef CONFIG_BOOT_RETRY_TIME
366 # endif /* CONFIG_BOOT_RETRY_TIME */
369 if (gd->flags & GD_FLG_POSTFAIL) {
370 s = getenv("failbootcmd");
373 #endif /* CONFIG_POST */
374 #ifdef CONFIG_BOOTCOUNT_LIMIT
375 if (bootlimit && (bootcount > bootlimit)) {
376 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
377 (unsigned)bootlimit);
378 s = getenv ("altbootcmd");
381 #endif /* CONFIG_BOOTCOUNT_LIMIT */
382 s = getenv ("bootcmd");
384 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
386 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
387 # ifdef CONFIG_AUTOBOOT_KEYED
388 int prev = disable_ctrlc(1); /* disable Control C checking */
391 run_command_list(s, -1, 0);
393 # ifdef CONFIG_AUTOBOOT_KEYED
394 disable_ctrlc(prev); /* restore Control C checking */
398 # ifdef CONFIG_MENUKEY
399 if (menukey == CONFIG_MENUKEY) {
400 s = getenv("menucmd");
402 run_command_list(s, -1, 0);
404 #endif /* CONFIG_MENUKEY */
405 #endif /* CONFIG_BOOTDELAY */
408 * Main Loop for Monitor Command Processing
410 #ifdef CONFIG_SYS_HUSH_PARSER
412 /* This point is never reached */
416 #ifdef CONFIG_BOOT_RETRY_TIME
418 /* Saw enough of a valid command to
419 * restart the timeout.
424 len = readline (CONFIG_SYS_PROMPT);
426 flag = 0; /* assume no special flags for now */
428 strcpy (lastcommand, console_buffer);
430 flag |= CMD_FLAG_REPEAT;
431 #ifdef CONFIG_BOOT_RETRY_TIME
432 else if (len == -2) {
433 /* -2 means timed out, retry autoboot
435 puts ("\nTimed out waiting for command\n");
436 # ifdef CONFIG_RESET_TO_RETRY
437 /* Reinit board to run initialization code again */
438 do_reset (NULL, 0, 0, NULL);
440 return; /* retry autoboot */
446 puts ("<INTERRUPT>\n");
448 rc = run_command(lastcommand, flag);
451 /* invalid command or not repeatable, forget it */
455 #endif /*CONFIG_SYS_HUSH_PARSER*/
458 #ifdef CONFIG_BOOT_RETRY_TIME
459 /***************************************************************************
460 * initialize command line timeout
462 void init_cmd_timeout(void)
464 char *s = getenv ("bootretry");
467 retry_time = (int)simple_strtol(s, NULL, 10);
469 retry_time = CONFIG_BOOT_RETRY_TIME;
471 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
472 retry_time = CONFIG_BOOT_RETRY_MIN;
475 /***************************************************************************
476 * reset command line timeout to retry_time seconds
478 void reset_cmd_timeout(void)
480 endtime = endtick(retry_time);
484 #ifdef CONFIG_CMDLINE_EDITING
487 * cmdline-editing related codes from vivi.
488 * Author: Janghoon Lyu <nandy@mizi.com>
491 #define putnstr(str,n) do { \
492 printf ("%.*s", (int)n, str); \
495 #define CTL_CH(c) ((c) - 'a' + 1)
496 #define CTL_BACKSPACE ('\b')
497 #define DEL ((char)255)
498 #define DEL7 ((char)127)
499 #define CREAD_HIST_CHAR ('!')
501 #define getcmd_putch(ch) putc(ch)
502 #define getcmd_getch() getc()
503 #define getcmd_cbeep() getcmd_putch('\a')
506 #define HIST_SIZE CONFIG_SYS_CBSIZE
509 static int hist_add_idx;
510 static int hist_cur = -1;
511 static unsigned hist_num;
513 static char *hist_list[HIST_MAX];
514 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
516 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
518 static void hist_init(void)
527 for (i = 0; i < HIST_MAX; i++) {
528 hist_list[i] = hist_lines[i];
529 hist_list[i][0] = '\0';
533 static void cread_add_to_hist(char *line)
535 strcpy(hist_list[hist_add_idx], line);
537 if (++hist_add_idx >= HIST_MAX)
540 if (hist_add_idx > hist_max)
541 hist_max = hist_add_idx;
546 static char* hist_prev(void)
558 if (hist_cur == hist_add_idx) {
562 ret = hist_list[hist_cur];
567 static char* hist_next(void)
574 if (hist_cur == hist_add_idx)
577 if (++hist_cur > hist_max)
580 if (hist_cur == hist_add_idx) {
583 ret = hist_list[hist_cur];
588 #ifndef CONFIG_CMDLINE_EDITING
589 static void cread_print_hist_list(void)
594 n = hist_num - hist_max;
596 i = hist_add_idx + 1;
600 if (i == hist_add_idx)
602 printf("%s\n", hist_list[i]);
607 #endif /* CONFIG_CMDLINE_EDITING */
609 #define BEGINNING_OF_LINE() { \
611 getcmd_putch(CTL_BACKSPACE); \
616 #define ERASE_TO_EOL() { \
617 if (num < eol_num) { \
618 printf("%*s", (int)(eol_num - num), ""); \
620 getcmd_putch(CTL_BACKSPACE); \
621 } while (--eol_num > num); \
625 #define REFRESH_TO_EOL() { \
626 if (num < eol_num) { \
627 wlen = eol_num - num; \
628 putnstr(buf + num, wlen); \
633 static void cread_add_char(char ichar, int insert, unsigned long *num,
634 unsigned long *eol_num, char *buf, unsigned long len)
639 if (insert || *num == *eol_num) {
640 if (*eol_num > len - 1) {
648 wlen = *eol_num - *num;
650 memmove(&buf[*num+1], &buf[*num], wlen-1);
654 putnstr(buf + *num, wlen);
657 getcmd_putch(CTL_BACKSPACE);
660 /* echo the character */
663 putnstr(buf + *num, wlen);
668 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
669 unsigned long *eol_num, char *buf, unsigned long len)
672 cread_add_char(*str, insert, num, eol_num, buf, len);
677 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
680 unsigned long num = 0;
681 unsigned long eol_num = 0;
687 int init_len = strlen(buf);
691 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
694 #ifdef CONFIG_BOOT_RETRY_TIME
695 while (!tstc()) { /* while no incoming data */
696 if (retry_time >= 0 && get_ticks() > endtime)
697 return (-2); /* timed out */
701 if (first && timeout) {
702 uint64_t etime = endtick(timeout);
704 while (!tstc()) { /* while no incoming data */
705 if (get_ticks() >= etime)
706 return -2; /* timed out */
712 ichar = getcmd_getch();
714 if ((ichar == '\n') || (ichar == '\r')) {
720 * handle standard linux xterm esc sequences for arrow key, etc.
725 esc_save[esc_len] = ichar;
728 cread_add_str(esc_save, esc_len, insert,
729 &num, &eol_num, buf, *len);
737 case 'D': /* <- key */
741 case 'C': /* -> key */
744 break; /* pass off to ^F handler */
745 case 'H': /* Home key */
748 break; /* pass off to ^A handler */
749 case 'A': /* up arrow */
752 break; /* pass off to ^P handler */
753 case 'B': /* down arrow */
756 break; /* pass off to ^N handler */
758 esc_save[esc_len++] = ichar;
759 cread_add_str(esc_save, esc_len, insert,
760 &num, &eol_num, buf, *len);
769 esc_save[esc_len] = ichar;
772 puts("impossible condition #876\n");
780 case CTL_CH('c'): /* ^C - break */
781 *buf = '\0'; /* discard input */
785 getcmd_putch(buf[num]);
791 getcmd_putch(CTL_BACKSPACE);
797 wlen = eol_num - num - 1;
799 memmove(&buf[num], &buf[num+1], wlen);
800 putnstr(buf + num, wlen);
805 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);
870 #ifdef CONFIG_AUTO_COMPLETE
874 /* do not autocomplete when in the middle */
881 col = strlen(prompt) + eol_num;
883 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
892 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
897 buf[eol_num] = '\0'; /* lose the newline */
899 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
900 cread_add_to_hist(buf);
901 hist_cur = hist_add_idx;
906 #endif /* CONFIG_CMDLINE_EDITING */
908 /****************************************************************************/
911 * Prompt for input and read a line.
912 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
913 * time out when time goes past endtime (timebase time in ticks).
914 * Return: number of read characters
918 int readline (const char *const prompt)
921 * If console_buffer isn't 0-length the user will be prompted to modify
922 * it instead of entering it from scratch as desired.
924 console_buffer[0] = '\0';
926 return readline_into_buffer(prompt, console_buffer, 0);
930 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
933 #ifdef CONFIG_CMDLINE_EDITING
934 unsigned int len = CONFIG_SYS_CBSIZE;
936 static int initted = 0;
939 * History uses a global array which is not
940 * writable until after relocation to RAM.
941 * Revert to non-history version if still
942 * running from flash.
944 if (gd->flags & GD_FLG_RELOC) {
953 rc = cread_line(prompt, p, &len, timeout);
954 return rc < 0 ? rc : len;
957 #endif /* CONFIG_CMDLINE_EDITING */
959 int n = 0; /* buffer index */
960 int plen = 0; /* prompt length */
961 int col; /* output column cnt */
966 plen = strlen (prompt);
972 #ifdef CONFIG_BOOT_RETRY_TIME
973 while (!tstc()) { /* while no incoming data */
974 if (retry_time >= 0 && get_ticks() > endtime)
975 return (-2); /* timed out */
979 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
981 #ifdef CONFIG_SHOW_ACTIVITY
990 * Special character handling
993 case '\r': /* Enter */
1002 case 0x03: /* ^C - break */
1003 p_buf[0] = '\0'; /* discard input */
1006 case 0x15: /* ^U - erase line */
1007 while (col > plen) {
1015 case 0x17: /* ^W - erase word */
1016 p=delete_char(p_buf, p, &col, &n, plen);
1017 while ((n > 0) && (*p != ' ')) {
1018 p=delete_char(p_buf, p, &col, &n, plen);
1022 case 0x08: /* ^H - backspace */
1023 case 0x7F: /* DEL - backspace */
1024 p=delete_char(p_buf, p, &col, &n, plen);
1029 * Must be a normal character then
1031 if (n < CONFIG_SYS_CBSIZE-2) {
1032 if (c == '\t') { /* expand TABs */
1033 #ifdef CONFIG_AUTO_COMPLETE
1034 /* if auto completion triggered just continue */
1036 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1037 p = p_buf + n; /* reset */
1041 puts (tab_seq+(col&07));
1042 col += 8 - (col&07);
1047 * Echo input using puts() to force am
1048 * LCD flush if we are using an LCD
1057 } else { /* Buffer full */
1062 #ifdef CONFIG_CMDLINE_EDITING
1067 /****************************************************************************/
1069 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1077 if (*(--p) == '\t') { /* will retype the whole line */
1078 while (*colp > plen) {
1082 for (s=buffer; s<p; ++s) {
1084 puts (tab_seq+((*colp) & 07));
1085 *colp += 8 - ((*colp) & 07);
1099 /****************************************************************************/
1101 int parse_line (char *line, char *argv[])
1106 printf ("parse_line: \"%s\"\n", line);
1108 while (nargs < CONFIG_SYS_MAXARGS) {
1110 /* skip any white space */
1111 while (isblank(*line))
1114 if (*line == '\0') { /* end of line, no more args */
1117 printf ("parse_line: nargs=%d\n", nargs);
1122 argv[nargs++] = line; /* begin of argument string */
1124 /* find end of string */
1125 while (*line && !isblank(*line))
1128 if (*line == '\0') { /* end of line, no more args */
1131 printf ("parse_line: nargs=%d\n", nargs);
1136 *line++ = '\0'; /* terminate current arg */
1139 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1142 printf ("parse_line: nargs=%d\n", nargs);
1147 /****************************************************************************/
1149 #ifndef CONFIG_SYS_HUSH_PARSER
1150 static void process_macros (const char *input, char *output)
1153 const char *varname_start = NULL;
1154 int inputcnt = strlen (input);
1155 int outputcnt = CONFIG_SYS_CBSIZE;
1156 int state = 0; /* 0 = waiting for '$' */
1158 /* 1 = waiting for '(' or '{' */
1159 /* 2 = waiting for ')' or '}' */
1160 /* 3 = waiting for ''' */
1162 char *output_start = output;
1164 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1168 prev = '\0'; /* previous character */
1170 while (inputcnt && outputcnt) {
1175 /* remove one level of escape characters */
1176 if ((c == '\\') && (prev != '\\')) {
1177 if (inputcnt-- == 0)
1185 case 0: /* Waiting for (unescaped) $ */
1186 if ((c == '\'') && (prev != '\\')) {
1190 if ((c == '$') && (prev != '\\')) {
1197 case 1: /* Waiting for ( */
1198 if (c == '(' || c == '{') {
1200 varname_start = input;
1212 case 2: /* Waiting for ) */
1213 if (c == ')' || c == '}') {
1215 char envname[CONFIG_SYS_CBSIZE], *envval;
1216 int envcnt = input - varname_start - 1; /* Varname # of chars */
1218 /* Get the varname */
1219 for (i = 0; i < envcnt; i++) {
1220 envname[i] = varname_start[i];
1225 envval = getenv (envname);
1227 /* Copy into the line if it exists */
1229 while ((*envval) && outputcnt) {
1230 *(output++) = *(envval++);
1233 /* Look for another '$' */
1237 case 3: /* Waiting for ' */
1238 if ((c == '\'') && (prev != '\\')) {
1255 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1256 strlen (output_start), output_start);
1260 /****************************************************************************
1262 * 1 - command executed, repeatable
1263 * 0 - command executed but not repeatable, interrupted commands are
1264 * always considered not repeatable
1265 * -1 - not executed (unrecognized, bootd recursion or too many args)
1266 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1267 * considered unrecognized)
1271 * We must create a temporary copy of the command since the command we get
1272 * may be the result from getenv(), which returns a pointer directly to
1273 * the environment data, which may change magicly when the command we run
1274 * creates or modifies environment variables (like "bootp" does).
1276 static int builtin_run_command(const char *cmd, int flag)
1278 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1279 char *token; /* start of token in cmdbuf */
1280 char *sep; /* end of token (separator) in cmdbuf */
1281 char finaltoken[CONFIG_SYS_CBSIZE];
1283 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1289 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1290 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1294 clear_ctrlc(); /* forget any previous Control C */
1296 if (!cmd || !*cmd) {
1297 return -1; /* empty command */
1300 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1301 puts ("## Command too long!\n");
1305 strcpy (cmdbuf, cmd);
1307 /* Process separators and check for invalid
1308 * repeatable commands
1312 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1317 * Find separator, or string end
1318 * Allow simple escape of ';' by writing "\;"
1320 for (inquotes = 0, sep = str; *sep; sep++) {
1326 (*sep == ';') && /* separator */
1327 ( sep != str) && /* past string start */
1328 (*(sep-1) != '\\')) /* and NOT escaped */
1333 * Limit the token to data between separators
1337 str = sep + 1; /* start of command for next pass */
1341 str = sep; /* no more commands for next pass */
1343 printf ("token: \"%s\"\n", token);
1346 /* find macros in this token and replace them */
1347 process_macros (token, finaltoken);
1349 /* Extract arguments */
1350 if ((argc = parse_line (finaltoken, argv)) == 0) {
1351 rc = -1; /* no command at all */
1355 if (cmd_process(flag, argc, argv, &repeatable))
1358 /* Did the user stop this? */
1360 return -1; /* if stopped then not repeatable */
1363 return rc ? rc : repeatable;
1368 * Run a command using the selected parser.
1370 * @param cmd Command to run
1371 * @param flag Execution flags (CMD_FLAG_...)
1372 * @return 0 on success, or != 0 on error.
1374 int run_command(const char *cmd, int flag)
1376 #ifndef CONFIG_SYS_HUSH_PARSER
1378 * builtin_run_command can return 0 or 1 for success, so clean up
1381 if (builtin_run_command(cmd, flag) == -1)
1386 return parse_string_outer(cmd,
1387 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1391 #ifndef CONFIG_SYS_HUSH_PARSER
1393 * Execute a list of command separated by ; or \n using the built-in parser.
1395 * This function cannot take a const char * for the command, since if it
1396 * finds newlines in the string, it replaces them with \0.
1398 * @param cmd String containing list of commands
1399 * @param flag Execution flags (CMD_FLAG_...)
1400 * @return 0 on success, or != 0 on error.
1402 static int builtin_run_command_list(char *cmd, int flag)
1408 * Break into individual lines, and execute each line; terminate on
1413 if (*next == '\n') {
1415 /* run only non-empty commands */
1417 debug("** exec: \"%s\"\n", line);
1418 if (builtin_run_command(line, 0) < 0) {
1427 if (rcode == 0 && *line)
1428 rcode = (builtin_run_command(line, 0) >= 0);
1434 int run_command_list(const char *cmd, int len, int flag)
1437 char *buff = (char *)cmd; /* cast away const */
1442 #ifdef CONFIG_SYS_HUSH_PARSER
1443 /* hush will never change our string */
1446 /* the built-in parser will change our string if it sees \n */
1447 need_buff = strchr(cmd, '\n') != NULL;
1451 buff = malloc(len + 1);
1454 memcpy(buff, cmd, len);
1457 #ifdef CONFIG_SYS_HUSH_PARSER
1458 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1461 * This function will overwrite any \n it sees with a \0, which
1462 * is why it can't work with a const char *. Here we are making
1463 * using of internal knowledge of this function, to avoid always
1464 * doing a malloc() which is actually required only in a case that
1467 rcode = builtin_run_command_list(buff, flag);
1475 /****************************************************************************/
1477 #if defined(CONFIG_CMD_RUN)
1478 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1483 return CMD_RET_USAGE;
1485 for (i=1; i<argc; ++i) {
1488 if ((arg = getenv (argv[i])) == NULL) {
1489 printf ("## Error: \"%s\" not defined\n", argv[i]);
1493 if (run_command(arg, flag) != 0)