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,
36 #ifdef CONFIG_MODEM_SUPPORT
37 #include <malloc.h> /* for free() prototype */
40 #ifdef CONFIG_SYS_HUSH_PARSER
44 #ifdef CONFIG_OF_CONTROL
49 #include <linux/ctype.h>
52 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
53 DECLARE_GLOBAL_DATA_PTR;
57 * Board-specific Platform code can reimplement show_boot_progress () if needed
59 void inline __show_boot_progress (int val) {}
60 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
62 #if defined(CONFIG_UPDATE_TFTP)
63 int update_tftp (ulong addr);
64 #endif /* CONFIG_UPDATE_TFTP */
66 #define MAX_DELAY_STOP_STR 32
70 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
72 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
73 static const char erase_seq[] = "\b \b"; /* erase sequence */
74 static const char tab_seq[] = " "; /* used to expand TABs */
76 #ifdef CONFIG_BOOT_RETRY_TIME
77 static uint64_t endtime = 0; /* must be set, default is instant timeout */
78 static int retry_time = -1; /* -1 so can call readline before main_loop */
81 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
83 #ifndef CONFIG_BOOT_RETRY_MIN
84 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
87 #ifdef CONFIG_MODEM_SUPPORT
89 extern void mdm_init(void); /* defined in board.c */
92 /***************************************************************************
93 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
94 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
96 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
97 # if defined(CONFIG_AUTOBOOT_KEYED)
101 int abortboot(int bootdelay)
104 uint64_t etime = endtick(bootdelay);
111 { str: getenv ("bootdelaykey"), retry: 1 },
112 { str: getenv ("bootdelaykey2"), retry: 1 },
113 { str: getenv ("bootstopkey"), retry: 0 },
114 { str: getenv ("bootstopkey2"), retry: 0 },
117 char presskey [MAX_DELAY_STOP_STR];
118 u_int presskey_len = 0;
119 u_int presskey_max = 0;
122 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
127 # ifdef CONFIG_AUTOBOOT_PROMPT
128 printf(CONFIG_AUTOBOOT_PROMPT);
131 # ifdef CONFIG_AUTOBOOT_DELAY_STR
132 if (delaykey[0].str == NULL)
133 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
135 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
136 if (delaykey[1].str == NULL)
137 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
139 # ifdef CONFIG_AUTOBOOT_STOP_STR
140 if (delaykey[2].str == NULL)
141 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
143 # ifdef CONFIG_AUTOBOOT_STOP_STR2
144 if (delaykey[3].str == NULL)
145 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
148 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
149 delaykey[i].len = delaykey[i].str == NULL ?
150 0 : strlen (delaykey[i].str);
151 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
152 MAX_DELAY_STOP_STR : delaykey[i].len;
154 presskey_max = presskey_max > delaykey[i].len ?
155 presskey_max : delaykey[i].len;
158 printf("%s key:<%s>\n",
159 delaykey[i].retry ? "delay" : "stop",
160 delaykey[i].str ? delaykey[i].str : "NULL");
164 /* In order to keep up with incoming data, check timeout only
169 if (presskey_len < presskey_max) {
170 presskey [presskey_len ++] = getc();
173 for (i = 0; i < presskey_max - 1; i ++)
174 presskey [i] = presskey [i + 1];
176 presskey [i] = getc();
180 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
181 if (delaykey[i].len > 0 &&
182 presskey_len >= delaykey[i].len &&
183 memcmp (presskey + presskey_len - delaykey[i].len,
185 delaykey[i].len) == 0) {
187 printf("got %skey\n",
188 delaykey[i].retry ? "delay" : "stop");
191 # ifdef CONFIG_BOOT_RETRY_TIME
192 /* don't retry auto boot */
193 if (! delaykey[i].retry)
199 } while (!abort && get_ticks() <= etime);
203 puts("key timeout\n");
206 #ifdef CONFIG_SILENT_CONSOLE
208 gd->flags &= ~GD_FLG_SILENT;
214 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
216 #ifdef CONFIG_MENUKEY
217 static int menukey = 0;
223 int abortboot(int bootdelay)
227 #ifdef CONFIG_MENUPROMPT
228 printf(CONFIG_MENUPROMPT);
231 printf("Hit any key to stop autoboot: %2d ", bootdelay);
234 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
236 * Check if key already pressed
237 * Don't check if bootdelay < 0
239 if (bootdelay >= 0) {
240 if (tstc()) { /* we got a key press */
241 (void) getc(); /* consume input */
243 abort = 1; /* don't auto boot */
248 while ((bootdelay > 0) && (!abort)) {
252 /* delay 100 * 10ms */
253 for (i=0; !abort && i<100; ++i) {
254 if (tstc()) { /* we got a key press */
255 abort = 1; /* don't auto boot */
256 bootdelay = 0; /* no more delay */
257 # ifdef CONFIG_MENUKEY
260 (void) getc(); /* consume input */
267 printf("\b\b\b%2d ", bootdelay);
272 #ifdef CONFIG_SILENT_CONSOLE
274 gd->flags &= ~GD_FLG_SILENT;
279 # endif /* CONFIG_AUTOBOOT_KEYED */
280 #endif /* CONFIG_BOOTDELAY >= 0 */
282 /****************************************************************************/
284 void main_loop (void)
286 #ifndef CONFIG_SYS_HUSH_PARSER
287 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
292 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
293 defined(CONFIG_OF_CONTROL)
296 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
300 #ifdef CONFIG_PREBOOT
303 #ifdef CONFIG_BOOTCOUNT_LIMIT
304 unsigned long bootcount = 0;
305 unsigned long bootlimit = 0;
308 #endif /* CONFIG_BOOTCOUNT_LIMIT */
310 #ifdef CONFIG_BOOTCOUNT_LIMIT
311 bootcount = bootcount_load();
313 bootcount_store (bootcount);
314 sprintf (bcs_set, "%lu", bootcount);
315 setenv ("bootcount", bcs_set);
316 bcs = getenv ("bootlimit");
317 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
318 #endif /* CONFIG_BOOTCOUNT_LIMIT */
320 #ifdef CONFIG_MODEM_SUPPORT
321 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
323 char *str = strdup(getenv("mdm_cmd"));
324 setenv ("preboot", str); /* set or delete definition */
327 mdm_init(); /* wait for modem connection */
329 #endif /* CONFIG_MODEM_SUPPORT */
331 #ifdef CONFIG_VERSION_VARIABLE
333 setenv ("ver", version_string); /* set version variable */
335 #endif /* CONFIG_VERSION_VARIABLE */
337 #ifdef CONFIG_SYS_HUSH_PARSER
338 u_boot_hush_start ();
341 #if defined(CONFIG_HUSH_INIT_VAR)
345 #ifdef CONFIG_PREBOOT
346 if ((p = getenv ("preboot")) != NULL) {
347 # ifdef CONFIG_AUTOBOOT_KEYED
348 int prev = disable_ctrlc(1); /* disable Control C checking */
351 run_command_list(p, -1, 0);
353 # ifdef CONFIG_AUTOBOOT_KEYED
354 disable_ctrlc(prev); /* restore Control C checking */
357 #endif /* CONFIG_PREBOOT */
359 #if defined(CONFIG_UPDATE_TFTP)
361 #endif /* CONFIG_UPDATE_TFTP */
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 #if defined(CONFIG_MENU_SHOW)
370 bootdelay = menu_show(bootdelay);
372 # ifdef CONFIG_BOOT_RETRY_TIME
374 # endif /* CONFIG_BOOT_RETRY_TIME */
377 if (gd->flags & GD_FLG_POSTFAIL) {
378 s = getenv("failbootcmd");
381 #endif /* CONFIG_POST */
382 #ifdef CONFIG_BOOTCOUNT_LIMIT
383 if (bootlimit && (bootcount > bootlimit)) {
384 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
385 (unsigned)bootlimit);
386 s = getenv ("altbootcmd");
389 #endif /* CONFIG_BOOTCOUNT_LIMIT */
390 s = getenv ("bootcmd");
391 #ifdef CONFIG_OF_CONTROL
392 /* Allow the fdt to override the boot command */
393 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
396 #endif /* CONFIG_OF_CONTROL */
398 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
400 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
401 # ifdef CONFIG_AUTOBOOT_KEYED
402 int prev = disable_ctrlc(1); /* disable Control C checking */
405 run_command_list(s, -1, 0);
407 # ifdef CONFIG_AUTOBOOT_KEYED
408 disable_ctrlc(prev); /* restore Control C checking */
412 # ifdef CONFIG_MENUKEY
413 if (menukey == CONFIG_MENUKEY) {
414 s = getenv("menucmd");
416 run_command_list(s, -1, 0);
418 #endif /* CONFIG_MENUKEY */
419 #endif /* CONFIG_BOOTDELAY */
422 * Main Loop for Monitor Command Processing
424 #ifdef CONFIG_SYS_HUSH_PARSER
426 /* This point is never reached */
430 #ifdef CONFIG_BOOT_RETRY_TIME
432 /* Saw enough of a valid command to
433 * restart the timeout.
438 len = readline (CONFIG_SYS_PROMPT);
440 flag = 0; /* assume no special flags for now */
442 strcpy (lastcommand, console_buffer);
444 flag |= CMD_FLAG_REPEAT;
445 #ifdef CONFIG_BOOT_RETRY_TIME
446 else if (len == -2) {
447 /* -2 means timed out, retry autoboot
449 puts ("\nTimed out waiting for command\n");
450 # ifdef CONFIG_RESET_TO_RETRY
451 /* Reinit board to run initialization code again */
452 do_reset (NULL, 0, 0, NULL);
454 return; /* retry autoboot */
460 puts ("<INTERRUPT>\n");
462 rc = run_command(lastcommand, flag);
465 /* invalid command or not repeatable, forget it */
469 #endif /*CONFIG_SYS_HUSH_PARSER*/
472 #ifdef CONFIG_BOOT_RETRY_TIME
473 /***************************************************************************
474 * initialize command line timeout
476 void init_cmd_timeout(void)
478 char *s = getenv ("bootretry");
481 retry_time = (int)simple_strtol(s, NULL, 10);
483 retry_time = CONFIG_BOOT_RETRY_TIME;
485 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
486 retry_time = CONFIG_BOOT_RETRY_MIN;
489 /***************************************************************************
490 * reset command line timeout to retry_time seconds
492 void reset_cmd_timeout(void)
494 endtime = endtick(retry_time);
498 #ifdef CONFIG_CMDLINE_EDITING
501 * cmdline-editing related codes from vivi.
502 * Author: Janghoon Lyu <nandy@mizi.com>
505 #define putnstr(str,n) do { \
506 printf ("%.*s", (int)n, str); \
509 #define CTL_CH(c) ((c) - 'a' + 1)
510 #define CTL_BACKSPACE ('\b')
511 #define DEL ((char)255)
512 #define DEL7 ((char)127)
513 #define CREAD_HIST_CHAR ('!')
515 #define getcmd_putch(ch) putc(ch)
516 #define getcmd_getch() getc()
517 #define getcmd_cbeep() getcmd_putch('\a')
520 #define HIST_SIZE CONFIG_SYS_CBSIZE
523 static int hist_add_idx;
524 static int hist_cur = -1;
525 static unsigned hist_num;
527 static char *hist_list[HIST_MAX];
528 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
530 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
532 static void hist_init(void)
541 for (i = 0; i < HIST_MAX; i++) {
542 hist_list[i] = hist_lines[i];
543 hist_list[i][0] = '\0';
547 static void cread_add_to_hist(char *line)
549 strcpy(hist_list[hist_add_idx], line);
551 if (++hist_add_idx >= HIST_MAX)
554 if (hist_add_idx > hist_max)
555 hist_max = hist_add_idx;
560 static char* hist_prev(void)
572 if (hist_cur == hist_add_idx) {
576 ret = hist_list[hist_cur];
581 static char* hist_next(void)
588 if (hist_cur == hist_add_idx)
591 if (++hist_cur > hist_max)
594 if (hist_cur == hist_add_idx) {
597 ret = hist_list[hist_cur];
602 #ifndef CONFIG_CMDLINE_EDITING
603 static void cread_print_hist_list(void)
608 n = hist_num - hist_max;
610 i = hist_add_idx + 1;
614 if (i == hist_add_idx)
616 printf("%s\n", hist_list[i]);
621 #endif /* CONFIG_CMDLINE_EDITING */
623 #define BEGINNING_OF_LINE() { \
625 getcmd_putch(CTL_BACKSPACE); \
630 #define ERASE_TO_EOL() { \
631 if (num < eol_num) { \
632 printf("%*s", (int)(eol_num - num), ""); \
634 getcmd_putch(CTL_BACKSPACE); \
635 } while (--eol_num > num); \
639 #define REFRESH_TO_EOL() { \
640 if (num < eol_num) { \
641 wlen = eol_num - num; \
642 putnstr(buf + num, wlen); \
647 static void cread_add_char(char ichar, int insert, unsigned long *num,
648 unsigned long *eol_num, char *buf, unsigned long len)
653 if (insert || *num == *eol_num) {
654 if (*eol_num > len - 1) {
662 wlen = *eol_num - *num;
664 memmove(&buf[*num+1], &buf[*num], wlen-1);
668 putnstr(buf + *num, wlen);
671 getcmd_putch(CTL_BACKSPACE);
674 /* echo the character */
677 putnstr(buf + *num, wlen);
682 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
683 unsigned long *eol_num, char *buf, unsigned long len)
686 cread_add_char(*str, insert, num, eol_num, buf, len);
691 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
694 unsigned long num = 0;
695 unsigned long eol_num = 0;
701 int init_len = strlen(buf);
705 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
708 #ifdef CONFIG_BOOT_RETRY_TIME
709 while (!tstc()) { /* while no incoming data */
710 if (retry_time >= 0 && get_ticks() > endtime)
711 return (-2); /* timed out */
715 if (first && timeout) {
716 uint64_t etime = endtick(timeout);
718 while (!tstc()) { /* while no incoming data */
719 if (get_ticks() >= etime)
720 return -2; /* timed out */
726 ichar = getcmd_getch();
728 if ((ichar == '\n') || (ichar == '\r')) {
734 * handle standard linux xterm esc sequences for arrow key, etc.
739 esc_save[esc_len] = ichar;
742 cread_add_str(esc_save, esc_len, insert,
743 &num, &eol_num, buf, *len);
751 case 'D': /* <- key */
755 case 'C': /* -> key */
758 break; /* pass off to ^F handler */
759 case 'H': /* Home key */
762 break; /* pass off to ^A handler */
763 case 'A': /* up arrow */
766 break; /* pass off to ^P handler */
767 case 'B': /* down arrow */
770 break; /* pass off to ^N handler */
772 esc_save[esc_len++] = ichar;
773 cread_add_str(esc_save, esc_len, insert,
774 &num, &eol_num, buf, *len);
783 esc_save[esc_len] = ichar;
786 puts("impossible condition #876\n");
794 case CTL_CH('c'): /* ^C - break */
795 *buf = '\0'; /* discard input */
799 getcmd_putch(buf[num]);
805 getcmd_putch(CTL_BACKSPACE);
811 wlen = eol_num - num - 1;
813 memmove(&buf[num], &buf[num+1], wlen);
814 putnstr(buf + num, wlen);
819 getcmd_putch(CTL_BACKSPACE);
842 wlen = eol_num - num;
844 memmove(&buf[num], &buf[num+1], wlen);
845 getcmd_putch(CTL_BACKSPACE);
846 putnstr(buf + num, wlen);
849 getcmd_putch(CTL_BACKSPACE);
861 if (ichar == CTL_CH('p'))
871 /* nuke the current line */
875 /* erase to end of line */
878 /* copy new line into place and display */
880 eol_num = strlen(buf);
884 #ifdef CONFIG_AUTO_COMPLETE
888 /* do not autocomplete when in the middle */
895 col = strlen(prompt) + eol_num;
897 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
906 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
911 buf[eol_num] = '\0'; /* lose the newline */
913 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
914 cread_add_to_hist(buf);
915 hist_cur = hist_add_idx;
920 #endif /* CONFIG_CMDLINE_EDITING */
922 /****************************************************************************/
925 * Prompt for input and read a line.
926 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
927 * time out when time goes past endtime (timebase time in ticks).
928 * Return: number of read characters
932 int readline (const char *const prompt)
935 * If console_buffer isn't 0-length the user will be prompted to modify
936 * it instead of entering it from scratch as desired.
938 console_buffer[0] = '\0';
940 return readline_into_buffer(prompt, console_buffer, 0);
944 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
947 #ifdef CONFIG_CMDLINE_EDITING
948 unsigned int len = CONFIG_SYS_CBSIZE;
950 static int initted = 0;
953 * History uses a global array which is not
954 * writable until after relocation to RAM.
955 * Revert to non-history version if still
956 * running from flash.
958 if (gd->flags & GD_FLG_RELOC) {
967 rc = cread_line(prompt, p, &len, timeout);
968 return rc < 0 ? rc : len;
971 #endif /* CONFIG_CMDLINE_EDITING */
973 int n = 0; /* buffer index */
974 int plen = 0; /* prompt length */
975 int col; /* output column cnt */
980 plen = strlen (prompt);
986 #ifdef CONFIG_BOOT_RETRY_TIME
987 while (!tstc()) { /* while no incoming data */
988 if (retry_time >= 0 && get_ticks() > endtime)
989 return (-2); /* timed out */
993 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
995 #ifdef CONFIG_SHOW_ACTIVITY
1004 * Special character handling
1007 case '\r': /* Enter */
1013 case '\0': /* nul */
1016 case 0x03: /* ^C - break */
1017 p_buf[0] = '\0'; /* discard input */
1020 case 0x15: /* ^U - erase line */
1021 while (col > plen) {
1029 case 0x17: /* ^W - erase word */
1030 p=delete_char(p_buf, p, &col, &n, plen);
1031 while ((n > 0) && (*p != ' ')) {
1032 p=delete_char(p_buf, p, &col, &n, plen);
1036 case 0x08: /* ^H - backspace */
1037 case 0x7F: /* DEL - backspace */
1038 p=delete_char(p_buf, p, &col, &n, plen);
1043 * Must be a normal character then
1045 if (n < CONFIG_SYS_CBSIZE-2) {
1046 if (c == '\t') { /* expand TABs */
1047 #ifdef CONFIG_AUTO_COMPLETE
1048 /* if auto completion triggered just continue */
1050 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1051 p = p_buf + n; /* reset */
1055 puts (tab_seq+(col&07));
1056 col += 8 - (col&07);
1058 ++col; /* echo input */
1063 } else { /* Buffer full */
1068 #ifdef CONFIG_CMDLINE_EDITING
1073 /****************************************************************************/
1075 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1083 if (*(--p) == '\t') { /* will retype the whole line */
1084 while (*colp > plen) {
1088 for (s=buffer; s<p; ++s) {
1090 puts (tab_seq+((*colp) & 07));
1091 *colp += 8 - ((*colp) & 07);
1105 /****************************************************************************/
1107 int parse_line (char *line, char *argv[])
1112 printf ("parse_line: \"%s\"\n", line);
1114 while (nargs < CONFIG_SYS_MAXARGS) {
1116 /* skip any white space */
1117 while (isblank(*line))
1120 if (*line == '\0') { /* end of line, no more args */
1123 printf ("parse_line: nargs=%d\n", nargs);
1128 argv[nargs++] = line; /* begin of argument string */
1130 /* find end of string */
1131 while (*line && !isblank(*line))
1134 if (*line == '\0') { /* end of line, no more args */
1137 printf ("parse_line: nargs=%d\n", nargs);
1142 *line++ = '\0'; /* terminate current arg */
1145 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1148 printf ("parse_line: nargs=%d\n", nargs);
1153 /****************************************************************************/
1155 #ifndef CONFIG_SYS_HUSH_PARSER
1156 static void process_macros (const char *input, char *output)
1159 const char *varname_start = NULL;
1160 int inputcnt = strlen (input);
1161 int outputcnt = CONFIG_SYS_CBSIZE;
1162 int state = 0; /* 0 = waiting for '$' */
1164 /* 1 = waiting for '(' or '{' */
1165 /* 2 = waiting for ')' or '}' */
1166 /* 3 = waiting for ''' */
1168 char *output_start = output;
1170 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1174 prev = '\0'; /* previous character */
1176 while (inputcnt && outputcnt) {
1181 /* remove one level of escape characters */
1182 if ((c == '\\') && (prev != '\\')) {
1183 if (inputcnt-- == 0)
1191 case 0: /* Waiting for (unescaped) $ */
1192 if ((c == '\'') && (prev != '\\')) {
1196 if ((c == '$') && (prev != '\\')) {
1203 case 1: /* Waiting for ( */
1204 if (c == '(' || c == '{') {
1206 varname_start = input;
1218 case 2: /* Waiting for ) */
1219 if (c == ')' || c == '}') {
1221 char envname[CONFIG_SYS_CBSIZE], *envval;
1222 int envcnt = input - varname_start - 1; /* Varname # of chars */
1224 /* Get the varname */
1225 for (i = 0; i < envcnt; i++) {
1226 envname[i] = varname_start[i];
1231 envval = getenv (envname);
1233 /* Copy into the line if it exists */
1235 while ((*envval) && outputcnt) {
1236 *(output++) = *(envval++);
1239 /* Look for another '$' */
1243 case 3: /* Waiting for ' */
1244 if ((c == '\'') && (prev != '\\')) {
1261 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1262 strlen (output_start), output_start);
1266 /****************************************************************************
1268 * 1 - command executed, repeatable
1269 * 0 - command executed but not repeatable, interrupted commands are
1270 * always considered not repeatable
1271 * -1 - not executed (unrecognized, bootd recursion or too many args)
1272 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1273 * considered unrecognized)
1277 * We must create a temporary copy of the command since the command we get
1278 * may be the result from getenv(), which returns a pointer directly to
1279 * the environment data, which may change magicly when the command we run
1280 * creates or modifies environment variables (like "bootp" does).
1282 static int builtin_run_command(const char *cmd, int flag)
1284 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1285 char *token; /* start of token in cmdbuf */
1286 char *sep; /* end of token (separator) in cmdbuf */
1287 char finaltoken[CONFIG_SYS_CBSIZE];
1289 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1295 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1296 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1300 clear_ctrlc(); /* forget any previous Control C */
1302 if (!cmd || !*cmd) {
1303 return -1; /* empty command */
1306 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1307 puts ("## Command too long!\n");
1311 strcpy (cmdbuf, cmd);
1313 /* Process separators and check for invalid
1314 * repeatable commands
1318 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1323 * Find separator, or string end
1324 * Allow simple escape of ';' by writing "\;"
1326 for (inquotes = 0, sep = str; *sep; sep++) {
1332 (*sep == ';') && /* separator */
1333 ( sep != str) && /* past string start */
1334 (*(sep-1) != '\\')) /* and NOT escaped */
1339 * Limit the token to data between separators
1343 str = sep + 1; /* start of command for next pass */
1347 str = sep; /* no more commands for next pass */
1349 printf ("token: \"%s\"\n", token);
1352 /* find macros in this token and replace them */
1353 process_macros (token, finaltoken);
1355 /* Extract arguments */
1356 if ((argc = parse_line (finaltoken, argv)) == 0) {
1357 rc = -1; /* no command at all */
1361 if (cmd_process(flag, argc, argv, &repeatable))
1364 /* Did the user stop this? */
1366 return -1; /* if stopped then not repeatable */
1369 return rc ? rc : repeatable;
1374 * Run a command using the selected parser.
1376 * @param cmd Command to run
1377 * @param flag Execution flags (CMD_FLAG_...)
1378 * @return 0 on success, or != 0 on error.
1380 int run_command(const char *cmd, int flag)
1382 #ifndef CONFIG_SYS_HUSH_PARSER
1384 * builtin_run_command can return 0 or 1 for success, so clean up
1387 if (builtin_run_command(cmd, flag) == -1)
1392 return parse_string_outer(cmd,
1393 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1397 #ifndef CONFIG_SYS_HUSH_PARSER
1399 * Execute a list of command separated by ; or \n using the built-in parser.
1401 * This function cannot take a const char * for the command, since if it
1402 * finds newlines in the string, it replaces them with \0.
1404 * @param cmd String containing list of commands
1405 * @param flag Execution flags (CMD_FLAG_...)
1406 * @return 0 on success, or != 0 on error.
1408 static int builtin_run_command_list(char *cmd, int flag)
1414 * Break into individual lines, and execute each line; terminate on
1419 if (*next == '\n') {
1421 /* run only non-empty commands */
1423 debug("** exec: \"%s\"\n", line);
1424 if (builtin_run_command(line, 0) < 0) {
1433 if (rcode == 0 && *line)
1434 rcode = (builtin_run_command(line, 0) >= 0);
1440 int run_command_list(const char *cmd, int len, int flag)
1443 char *buff = (char *)cmd; /* cast away const */
1448 #ifdef CONFIG_SYS_HUSH_PARSER
1449 /* hush will never change our string */
1452 /* the built-in parser will change our string if it sees \n */
1453 need_buff = strchr(cmd, '\n') != NULL;
1457 buff = malloc(len + 1);
1460 memcpy(buff, cmd, len);
1463 #ifdef CONFIG_SYS_HUSH_PARSER
1464 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1467 * This function will overwrite any \n it sees with a \0, which
1468 * is why it can't work with a const char *. Here we are making
1469 * using of internal knowledge of this function, to avoid always
1470 * doing a malloc() which is actually required only in a case that
1473 rcode = builtin_run_command_list(buff, flag);
1481 /****************************************************************************/
1483 #if defined(CONFIG_CMD_RUN)
1484 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1489 return CMD_RET_USAGE;
1491 for (i=1; i<argc; ++i) {
1494 if ((arg = getenv (argv[i])) == NULL) {
1495 printf ("## Error: \"%s\" not defined\n", argv[i]);
1499 if (run_command(arg, flag) != 0)