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 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST)
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 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
63 static int abortboot(int);
68 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
70 #ifndef CONFIG_CMDLINE_EDITING
71 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
72 static char erase_seq[] = "\b \b"; /* erase sequence */
73 static char tab_seq[] = " "; /* used to expand TABs */
74 #endif /* CONFIG_CMDLINE_EDITING */
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
95 * 1 - got key string, abort
97 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
98 # if defined(CONFIG_AUTOBOOT_KEYED)
99 static __inline__ int abortboot(int bootdelay)
102 uint64_t etime = endtick(bootdelay);
109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
120 # ifdef CONFIG_AUTOBOOT_PROMPT
121 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
124 # ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
128 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
132 # ifdef CONFIG_AUTOBOOT_STOP_STR
133 if (delaykey[2].str == NULL)
134 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
136 # ifdef CONFIG_AUTOBOOT_STOP_STR2
137 if (delaykey[3].str == NULL)
138 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
141 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
142 delaykey[i].len = delaykey[i].str == NULL ?
143 0 : strlen (delaykey[i].str);
144 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
145 MAX_DELAY_STOP_STR : delaykey[i].len;
147 presskey_max = presskey_max > delaykey[i].len ?
148 presskey_max : delaykey[i].len;
151 printf("%s key:<%s>\n",
152 delaykey[i].retry ? "delay" : "stop",
153 delaykey[i].str ? delaykey[i].str : "NULL");
157 /* In order to keep up with incoming data, check timeout only
160 while (!abort && get_ticks() <= etime) {
161 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
162 if (delaykey[i].len > 0 &&
163 presskey_len >= delaykey[i].len &&
164 memcmp (presskey + presskey_len - delaykey[i].len,
166 delaykey[i].len) == 0) {
168 printf("got %skey\n",
169 delaykey[i].retry ? "delay" : "stop");
172 # ifdef CONFIG_BOOT_RETRY_TIME
173 /* don't retry auto boot */
174 if (! delaykey[i].retry)
182 if (presskey_len < presskey_max) {
183 presskey [presskey_len ++] = getc();
186 for (i = 0; i < presskey_max - 1; i ++)
187 presskey [i] = presskey [i + 1];
189 presskey [i] = getc();
195 puts("key timeout\n");
198 #ifdef CONFIG_SILENT_CONSOLE
200 gd->flags &= ~GD_FLG_SILENT;
206 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
208 #ifdef CONFIG_MENUKEY
209 static int menukey = 0;
212 static __inline__ int abortboot(int bootdelay)
216 #ifdef CONFIG_MENUPROMPT
217 printf(CONFIG_MENUPROMPT, bootdelay);
219 printf("Hit any key to stop autoboot: %2d ", bootdelay);
222 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
224 * Check if key already pressed
225 * Don't check if bootdelay < 0
227 if (bootdelay >= 0) {
228 if (tstc()) { /* we got a key press */
229 (void) getc(); /* consume input */
231 abort = 1; /* don't auto boot */
236 while ((bootdelay > 0) && (!abort)) {
240 /* delay 100 * 10ms */
241 for (i=0; !abort && i<100; ++i) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
245 # ifdef CONFIG_MENUKEY
248 (void) getc(); /* consume input */
255 printf("\b\b\b%2d ", bootdelay);
260 #ifdef CONFIG_SILENT_CONSOLE
262 gd->flags &= ~GD_FLG_SILENT;
267 # endif /* CONFIG_AUTOBOOT_KEYED */
268 #endif /* CONFIG_BOOTDELAY >= 0 */
270 /****************************************************************************/
272 void main_loop (void)
274 #ifndef CFG_HUSH_PARSER
275 static char lastcommand[CFG_CBSIZE] = { 0, };
281 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
285 #ifdef CONFIG_PREBOOT
288 #ifdef CONFIG_BOOTCOUNT_LIMIT
289 unsigned long bootcount = 0;
290 unsigned long bootlimit = 0;
293 #endif /* CONFIG_BOOTCOUNT_LIMIT */
295 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
296 ulong bmp = 0; /* default bitmap */
297 extern int trab_vfd (ulong bitmap);
299 #ifdef CONFIG_MODEM_SUPPORT
301 bmp = 1; /* alternate bitmap */
304 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
306 #ifdef CONFIG_BOOTCOUNT_LIMIT
307 bootcount = bootcount_load();
309 bootcount_store (bootcount);
310 sprintf (bcs_set, "%lu", bootcount);
311 setenv ("bootcount", bcs_set);
312 bcs = getenv ("bootlimit");
313 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
314 #endif /* CONFIG_BOOTCOUNT_LIMIT */
316 #ifdef CONFIG_MODEM_SUPPORT
317 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
319 char *str = strdup(getenv("mdm_cmd"));
320 setenv ("preboot", str); /* set or delete definition */
323 mdm_init(); /* wait for modem connection */
325 #endif /* CONFIG_MODEM_SUPPORT */
327 #ifdef CONFIG_VERSION_VARIABLE
329 extern char version_string[];
331 setenv ("ver", version_string); /* set version variable */
333 #endif /* CONFIG_VERSION_VARIABLE */
335 #ifdef CFG_HUSH_PARSER
336 u_boot_hush_start ();
339 #ifdef CONFIG_AUTO_COMPLETE
340 install_auto_complete();
343 #ifdef CONFIG_PREBOOT
344 if ((p = getenv ("preboot")) != NULL) {
345 # ifdef CONFIG_AUTOBOOT_KEYED
346 int prev = disable_ctrlc(1); /* disable Control C checking */
349 # ifndef CFG_HUSH_PARSER
352 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
353 FLAG_EXIT_FROM_LOOP);
356 # ifdef CONFIG_AUTOBOOT_KEYED
357 disable_ctrlc(prev); /* restore Control C checking */
360 #endif /* CONFIG_PREBOOT */
362 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
363 s = getenv ("bootdelay");
364 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
366 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
368 # ifdef CONFIG_BOOT_RETRY_TIME
370 # endif /* CONFIG_BOOT_RETRY_TIME */
373 if (gd->flags & GD_FLG_POSTFAIL) {
374 s = getenv("failbootcmd");
377 #endif /* CONFIG_POST */
378 #ifdef CONFIG_BOOTCOUNT_LIMIT
379 if (bootlimit && (bootcount > bootlimit)) {
380 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
381 (unsigned)bootlimit);
382 s = getenv ("altbootcmd");
385 #endif /* CONFIG_BOOTCOUNT_LIMIT */
386 s = getenv ("bootcmd");
388 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
390 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
391 # ifdef CONFIG_AUTOBOOT_KEYED
392 int prev = disable_ctrlc(1); /* disable Control C checking */
395 # ifndef CFG_HUSH_PARSER
398 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
399 FLAG_EXIT_FROM_LOOP);
402 # ifdef CONFIG_AUTOBOOT_KEYED
403 disable_ctrlc(prev); /* restore Control C checking */
407 # ifdef CONFIG_MENUKEY
408 if (menukey == CONFIG_MENUKEY) {
409 s = getenv("menucmd");
411 # ifndef CFG_HUSH_PARSER
414 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
415 FLAG_EXIT_FROM_LOOP);
419 #endif /* CONFIG_MENUKEY */
420 #endif /* CONFIG_BOOTDELAY */
422 #ifdef CONFIG_AMIGAONEG3SE
424 extern void video_banner(void);
430 * Main Loop for Monitor Command Processing
432 #ifdef CFG_HUSH_PARSER
434 /* This point is never reached */
438 #ifdef CONFIG_BOOT_RETRY_TIME
440 /* Saw enough of a valid command to
441 * restart the timeout.
446 len = readline (CFG_PROMPT);
448 flag = 0; /* assume no special flags for now */
450 strcpy (lastcommand, console_buffer);
452 flag |= CMD_FLAG_REPEAT;
453 #ifdef CONFIG_BOOT_RETRY_TIME
454 else if (len == -2) {
455 /* -2 means timed out, retry autoboot
457 puts ("\nTimed out waiting for command\n");
458 # ifdef CONFIG_RESET_TO_RETRY
459 /* Reinit board to run initialization code again */
460 do_reset (NULL, 0, 0, NULL);
462 return; /* retry autoboot */
468 puts ("<INTERRUPT>\n");
470 rc = run_command (lastcommand, flag);
473 /* invalid command or not repeatable, forget it */
477 #endif /*CFG_HUSH_PARSER*/
480 #ifdef CONFIG_BOOT_RETRY_TIME
481 /***************************************************************************
482 * initialize command line timeout
484 void init_cmd_timeout(void)
486 char *s = getenv ("bootretry");
489 retry_time = (int)simple_strtol(s, NULL, 10);
491 retry_time = CONFIG_BOOT_RETRY_TIME;
493 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
494 retry_time = CONFIG_BOOT_RETRY_MIN;
497 /***************************************************************************
498 * reset command line timeout to retry_time seconds
500 void reset_cmd_timeout(void)
502 endtime = endtick(retry_time);
506 #ifdef CONFIG_CMDLINE_EDITING
509 * cmdline-editing related codes from vivi.
510 * Author: Janghoon Lyu <nandy@mizi.com>
513 #define putnstr(str,n) do { \
514 printf ("%.*s", n, str); \
517 #define CTL_CH(c) ((c) - 'a' + 1)
519 #define MAX_CMDBUF_SIZE 256
521 #define CTL_BACKSPACE ('\b')
522 #define DEL ((char)255)
523 #define DEL7 ((char)127)
524 #define CREAD_HIST_CHAR ('!')
526 #define getcmd_putch(ch) putc(ch)
527 #define getcmd_getch() getc()
528 #define getcmd_cbeep() getcmd_putch('\a')
531 #define HIST_SIZE MAX_CMDBUF_SIZE
533 static int hist_max = 0;
534 static int hist_add_idx = 0;
535 static int hist_cur = -1;
536 unsigned hist_num = 0;
538 char* hist_list[HIST_MAX];
539 char hist_lines[HIST_MAX][HIST_SIZE];
541 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
543 static void hist_init(void)
552 for (i = 0; i < HIST_MAX; i++) {
553 hist_list[i] = hist_lines[i];
554 hist_list[i][0] = '\0';
558 static void cread_add_to_hist(char *line)
560 strcpy(hist_list[hist_add_idx], line);
562 if (++hist_add_idx >= HIST_MAX)
565 if (hist_add_idx > hist_max)
566 hist_max = hist_add_idx;
571 static char* hist_prev(void)
583 if (hist_cur == hist_add_idx) {
587 ret = hist_list[hist_cur];
592 static char* hist_next(void)
599 if (hist_cur == hist_add_idx)
602 if (++hist_cur > hist_max)
605 if (hist_cur == hist_add_idx) {
608 ret = hist_list[hist_cur];
613 #ifndef CONFIG_CMDLINE_EDITING
614 static void cread_print_hist_list(void)
619 n = hist_num - hist_max;
621 i = hist_add_idx + 1;
625 if (i == hist_add_idx)
627 printf("%s\n", hist_list[i]);
632 #endif /* CONFIG_CMDLINE_EDITING */
634 #define BEGINNING_OF_LINE() { \
636 getcmd_putch(CTL_BACKSPACE); \
641 #define ERASE_TO_EOL() { \
642 if (num < eol_num) { \
644 for (tmp = num; tmp < eol_num; tmp++) \
646 while (tmp-- > num) \
647 getcmd_putch(CTL_BACKSPACE); \
652 #define REFRESH_TO_EOL() { \
653 if (num < eol_num) { \
654 wlen = eol_num - num; \
655 putnstr(buf + num, wlen); \
660 static void cread_add_char(char ichar, int insert, unsigned long *num,
661 unsigned long *eol_num, char *buf, unsigned long len)
666 if (insert || *num == *eol_num) {
667 if (*eol_num > len - 1) {
675 wlen = *eol_num - *num;
677 memmove(&buf[*num+1], &buf[*num], wlen-1);
681 putnstr(buf + *num, wlen);
684 getcmd_putch(CTL_BACKSPACE);
687 /* echo the character */
690 putnstr(buf + *num, wlen);
695 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
696 unsigned long *eol_num, char *buf, unsigned long len)
699 cread_add_char(*str, insert, num, eol_num, buf, len);
704 static int cread_line(const char *const prompt, char *buf, unsigned int *len)
706 unsigned long num = 0;
707 unsigned long eol_num = 0;
718 #ifdef CONFIG_BOOT_RETRY_TIME
719 while (!tstc()) { /* while no incoming data */
720 if (retry_time >= 0 && get_ticks() > endtime)
721 return (-2); /* timed out */
725 ichar = getcmd_getch();
727 if ((ichar == '\n') || (ichar == '\r')) {
733 * handle standard linux xterm esc sequences for arrow key, etc.
738 esc_save[esc_len] = ichar;
741 cread_add_str(esc_save, esc_len, insert,
742 &num, &eol_num, buf, *len);
750 case 'D': /* <- key */
754 case 'C': /* -> key */
757 break; /* pass off to ^F handler */
758 case 'H': /* Home key */
761 break; /* pass off to ^A handler */
762 case 'A': /* up arrow */
765 break; /* pass off to ^P handler */
766 case 'B': /* down arrow */
769 break; /* pass off to ^N handler */
771 esc_save[esc_len++] = ichar;
772 cread_add_str(esc_save, esc_len, insert,
773 &num, &eol_num, buf, *len);
782 esc_save[esc_len] = ichar;
785 puts("impossible condition #876\n");
793 case CTL_CH('c'): /* ^C - break */
794 *buf = '\0'; /* discard input */
798 getcmd_putch(buf[num]);
804 getcmd_putch(CTL_BACKSPACE);
810 wlen = eol_num - num - 1;
812 memmove(&buf[num], &buf[num+1], wlen);
813 putnstr(buf + num, wlen);
818 getcmd_putch(CTL_BACKSPACE);
841 wlen = eol_num - num;
843 memmove(&buf[num], &buf[num+1], wlen);
844 getcmd_putch(CTL_BACKSPACE);
845 putnstr(buf + num, wlen);
848 getcmd_putch(CTL_BACKSPACE);
860 if (ichar == CTL_CH('p'))
870 /* nuke the current line */
874 /* erase to end of line */
877 /* copy new line into place and display */
879 eol_num = strlen(buf);
883 #ifdef CONFIG_AUTO_COMPLETE
887 /* do not autocomplete when in the middle */
894 col = strlen(prompt) + eol_num;
896 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
905 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
910 buf[eol_num] = '\0'; /* lose the newline */
912 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
913 cread_add_to_hist(buf);
914 hist_cur = hist_add_idx;
919 #endif /* CONFIG_CMDLINE_EDITING */
921 /****************************************************************************/
924 * Prompt for input and read a line.
925 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
926 * time out when time goes past endtime (timebase time in ticks).
927 * Return: number of read characters
931 int readline (const char *const prompt)
933 return readline_into_buffer(prompt, console_buffer);
937 int readline_into_buffer (const char *const prompt, char * buffer)
940 #ifdef CONFIG_CMDLINE_EDITING
941 unsigned int len=MAX_CMDBUF_SIZE;
943 static int initted = 0;
952 rc = cread_line(prompt, p, &len);
953 return rc < 0 ? rc : len;
956 int n = 0; /* buffer index */
957 int plen = 0; /* prompt length */
958 int col; /* output column cnt */
963 plen = strlen (prompt);
969 #ifdef CONFIG_BOOT_RETRY_TIME
970 while (!tstc()) { /* while no incoming data */
971 if (retry_time >= 0 && get_ticks() > endtime)
972 return (-2); /* timed out */
975 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
977 #ifdef CONFIG_SHOW_ACTIVITY
979 extern void show_activity(int arg);
986 * Special character handling
989 case '\r': /* Enter */
998 case 0x03: /* ^C - break */
999 p_buf[0] = '\0'; /* discard input */
1002 case 0x15: /* ^U - erase line */
1003 while (col > plen) {
1011 case 0x17: /* ^W - erase word */
1012 p=delete_char(p_buf, p, &col, &n, plen);
1013 while ((n > 0) && (*p != ' ')) {
1014 p=delete_char(p_buf, p, &col, &n, plen);
1018 case 0x08: /* ^H - backspace */
1019 case 0x7F: /* DEL - backspace */
1020 p=delete_char(p_buf, p, &col, &n, plen);
1025 * Must be a normal character then
1027 if (n < CFG_CBSIZE-2) {
1028 if (c == '\t') { /* expand TABs */
1029 #ifdef CONFIG_AUTO_COMPLETE
1030 /* if auto completion triggered just continue */
1032 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1033 p = p_buf + n; /* reset */
1037 puts (tab_seq+(col&07));
1038 col += 8 - (col&07);
1040 ++col; /* echo input */
1045 } else { /* Buffer full */
1050 #endif /* CONFIG_CMDLINE_EDITING */
1053 /****************************************************************************/
1055 #ifndef CONFIG_CMDLINE_EDITING
1056 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1064 if (*(--p) == '\t') { /* will retype the whole line */
1065 while (*colp > plen) {
1069 for (s=buffer; s<p; ++s) {
1071 puts (tab_seq+((*colp) & 07));
1072 *colp += 8 - ((*colp) & 07);
1085 #endif /* CONFIG_CMDLINE_EDITING */
1087 /****************************************************************************/
1089 int parse_line (char *line, char *argv[])
1094 printf ("parse_line: \"%s\"\n", line);
1096 while (nargs < CFG_MAXARGS) {
1098 /* skip any white space */
1099 while ((*line == ' ') || (*line == '\t')) {
1103 if (*line == '\0') { /* end of line, no more args */
1106 printf ("parse_line: nargs=%d\n", nargs);
1111 argv[nargs++] = line; /* begin of argument string */
1113 /* find end of string */
1114 while (*line && (*line != ' ') && (*line != '\t')) {
1118 if (*line == '\0') { /* end of line, no more args */
1121 printf ("parse_line: nargs=%d\n", nargs);
1126 *line++ = '\0'; /* terminate current arg */
1129 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1132 printf ("parse_line: nargs=%d\n", nargs);
1137 /****************************************************************************/
1139 static void process_macros (const char *input, char *output)
1142 const char *varname_start = NULL;
1143 int inputcnt = strlen (input);
1144 int outputcnt = CFG_CBSIZE;
1145 int state = 0; /* 0 = waiting for '$' */
1147 /* 1 = waiting for '(' or '{' */
1148 /* 2 = waiting for ')' or '}' */
1149 /* 3 = waiting for ''' */
1151 char *output_start = output;
1153 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1157 prev = '\0'; /* previous character */
1159 while (inputcnt && outputcnt) {
1164 /* remove one level of escape characters */
1165 if ((c == '\\') && (prev != '\\')) {
1166 if (inputcnt-- == 0)
1174 case 0: /* Waiting for (unescaped) $ */
1175 if ((c == '\'') && (prev != '\\')) {
1179 if ((c == '$') && (prev != '\\')) {
1186 case 1: /* Waiting for ( */
1187 if (c == '(' || c == '{') {
1189 varname_start = input;
1201 case 2: /* Waiting for ) */
1202 if (c == ')' || c == '}') {
1204 char envname[CFG_CBSIZE], *envval;
1205 int envcnt = input - varname_start - 1; /* Varname # of chars */
1207 /* Get the varname */
1208 for (i = 0; i < envcnt; i++) {
1209 envname[i] = varname_start[i];
1214 envval = getenv (envname);
1216 /* Copy into the line if it exists */
1218 while ((*envval) && outputcnt) {
1219 *(output++) = *(envval++);
1222 /* Look for another '$' */
1226 case 3: /* Waiting for ' */
1227 if ((c == '\'') && (prev != '\\')) {
1244 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1245 strlen (output_start), output_start);
1249 /****************************************************************************
1251 * 1 - command executed, repeatable
1252 * 0 - command executed but not repeatable, interrupted commands are
1253 * always considered not repeatable
1254 * -1 - not executed (unrecognized, bootd recursion or too many args)
1255 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1256 * considered unrecognized)
1260 * We must create a temporary copy of the command since the command we get
1261 * may be the result from getenv(), which returns a pointer directly to
1262 * the environment data, which may change magicly when the command we run
1263 * creates or modifies environment variables (like "bootp" does).
1266 int run_command (const char *cmd, int flag)
1269 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1270 char *token; /* start of token in cmdbuf */
1271 char *sep; /* end of token (separator) in cmdbuf */
1272 char finaltoken[CFG_CBSIZE];
1274 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1280 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1281 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1285 clear_ctrlc(); /* forget any previous Control C */
1287 if (!cmd || !*cmd) {
1288 return -1; /* empty command */
1291 if (strlen(cmd) >= CFG_CBSIZE) {
1292 puts ("## Command too long!\n");
1296 strcpy (cmdbuf, cmd);
1298 /* Process separators and check for invalid
1299 * repeatable commands
1303 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1308 * Find separator, or string end
1309 * Allow simple escape of ';' by writing "\;"
1311 for (inquotes = 0, sep = str; *sep; sep++) {
1317 (*sep == ';') && /* separator */
1318 ( sep != str) && /* past string start */
1319 (*(sep-1) != '\\')) /* and NOT escaped */
1324 * Limit the token to data between separators
1328 str = sep + 1; /* start of command for next pass */
1332 str = sep; /* no more commands for next pass */
1334 printf ("token: \"%s\"\n", token);
1337 /* find macros in this token and replace them */
1338 process_macros (token, finaltoken);
1340 /* Extract arguments */
1341 if ((argc = parse_line (finaltoken, argv)) == 0) {
1342 rc = -1; /* no command at all */
1346 /* Look up command in command table */
1347 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1348 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1349 rc = -1; /* give up after bad command */
1353 /* found - check max args */
1354 if (argc > cmdtp->maxargs) {
1355 printf ("Usage:\n%s\n", cmdtp->usage);
1360 #if defined(CONFIG_CMD_BOOTD)
1361 /* avoid "bootd" recursion */
1362 if (cmdtp->cmd == do_bootd) {
1364 printf ("[%s]\n", finaltoken);
1366 if (flag & CMD_FLAG_BOOTD) {
1367 puts ("'bootd' recursion detected\n");
1371 flag |= CMD_FLAG_BOOTD;
1376 /* OK - call function to do the command */
1377 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1381 repeatable &= cmdtp->repeatable;
1383 /* Did the user stop this? */
1385 return -1; /* if stopped then not repeatable */
1388 return rc ? rc : repeatable;
1391 /****************************************************************************/
1393 #if defined(CONFIG_CMD_RUN)
1394 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1399 printf ("Usage:\n%s\n", cmdtp->usage);
1403 for (i=1; i<argc; ++i) {
1406 if ((arg = getenv (argv[i])) == NULL) {
1407 printf ("## Error: \"%s\" not defined\n", argv[i]);
1410 #ifndef CFG_HUSH_PARSER
1411 if (run_command (arg, flag) == -1)
1414 if (parse_string_outer(arg,
1415 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)