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) || defined(CONFIG_CMDLINE_EDITING)
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 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
71 static char erase_seq[] = "\b \b"; /* erase sequence */
72 static char tab_seq[] = " "; /* used to expand TABs */
74 #ifdef CONFIG_BOOT_RETRY_TIME
75 static uint64_t endtime = 0; /* must be set, default is instant timeout */
76 static int retry_time = -1; /* -1 so can call readline before main_loop */
79 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
81 #ifndef CONFIG_BOOT_RETRY_MIN
82 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85 #ifdef CONFIG_MODEM_SUPPORT
87 extern void mdm_init(void); /* defined in board.c */
90 /***************************************************************************
91 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
92 * returns: 0 - no key string, allow autoboot
93 * 1 - got key string, abort
95 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
96 # if defined(CONFIG_AUTOBOOT_KEYED)
97 static __inline__ int abortboot(int bootdelay)
100 uint64_t etime = endtick(bootdelay);
107 { str: getenv ("bootdelaykey"), retry: 1 },
108 { str: getenv ("bootdelaykey2"), retry: 1 },
109 { str: getenv ("bootstopkey"), retry: 0 },
110 { str: getenv ("bootstopkey2"), retry: 0 },
113 char presskey [MAX_DELAY_STOP_STR];
114 u_int presskey_len = 0;
115 u_int presskey_max = 0;
118 # ifdef CONFIG_AUTOBOOT_PROMPT
119 printf(CONFIG_AUTOBOOT_PROMPT);
122 # ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey[0].str == NULL)
124 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
126 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
127 if (delaykey[1].str == NULL)
128 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
130 # ifdef CONFIG_AUTOBOOT_STOP_STR
131 if (delaykey[2].str == NULL)
132 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
134 # ifdef CONFIG_AUTOBOOT_STOP_STR2
135 if (delaykey[3].str == NULL)
136 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
139 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
140 delaykey[i].len = delaykey[i].str == NULL ?
141 0 : strlen (delaykey[i].str);
142 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
143 MAX_DELAY_STOP_STR : delaykey[i].len;
145 presskey_max = presskey_max > delaykey[i].len ?
146 presskey_max : delaykey[i].len;
149 printf("%s key:<%s>\n",
150 delaykey[i].retry ? "delay" : "stop",
151 delaykey[i].str ? delaykey[i].str : "NULL");
155 /* In order to keep up with incoming data, check timeout only
158 while (!abort && get_ticks() <= etime) {
159 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
160 if (delaykey[i].len > 0 &&
161 presskey_len >= delaykey[i].len &&
162 memcmp (presskey + presskey_len - delaykey[i].len,
164 delaykey[i].len) == 0) {
166 printf("got %skey\n",
167 delaykey[i].retry ? "delay" : "stop");
170 # ifdef CONFIG_BOOT_RETRY_TIME
171 /* don't retry auto boot */
172 if (! delaykey[i].retry)
180 if (presskey_len < presskey_max) {
181 presskey [presskey_len ++] = getc();
184 for (i = 0; i < presskey_max - 1; i ++)
185 presskey [i] = presskey [i + 1];
187 presskey [i] = getc();
193 puts("key timeout\n");
196 #ifdef CONFIG_SILENT_CONSOLE
198 gd->flags &= ~GD_FLG_SILENT;
204 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
206 #ifdef CONFIG_MENUKEY
207 static int menukey = 0;
210 static __inline__ int abortboot(int bootdelay)
214 #ifdef CONFIG_MENUPROMPT
215 printf(CONFIG_MENUPROMPT);
217 printf("Hit any key to stop autoboot: %2d ", bootdelay);
220 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
222 * Check if key already pressed
223 * Don't check if bootdelay < 0
225 if (bootdelay >= 0) {
226 if (tstc()) { /* we got a key press */
227 (void) getc(); /* consume input */
229 abort = 1; /* don't auto boot */
234 while ((bootdelay > 0) && (!abort)) {
238 /* delay 100 * 10ms */
239 for (i=0; !abort && i<100; ++i) {
240 if (tstc()) { /* we got a key press */
241 abort = 1; /* don't auto boot */
242 bootdelay = 0; /* no more delay */
243 # ifdef CONFIG_MENUKEY
246 (void) getc(); /* consume input */
253 printf("\b\b\b%2d ", bootdelay);
258 #ifdef CONFIG_SILENT_CONSOLE
260 gd->flags &= ~GD_FLG_SILENT;
265 # endif /* CONFIG_AUTOBOOT_KEYED */
266 #endif /* CONFIG_BOOTDELAY >= 0 */
268 /****************************************************************************/
270 void main_loop (void)
272 #ifndef CFG_HUSH_PARSER
273 static char lastcommand[CFG_CBSIZE] = { 0, };
279 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
283 #ifdef CONFIG_PREBOOT
286 #ifdef CONFIG_BOOTCOUNT_LIMIT
287 unsigned long bootcount = 0;
288 unsigned long bootlimit = 0;
291 #endif /* CONFIG_BOOTCOUNT_LIMIT */
293 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
294 ulong bmp = 0; /* default bitmap */
295 extern int trab_vfd (ulong bitmap);
297 #ifdef CONFIG_MODEM_SUPPORT
299 bmp = 1; /* alternate bitmap */
302 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
304 #ifdef CONFIG_BOOTCOUNT_LIMIT
305 bootcount = bootcount_load();
307 bootcount_store (bootcount);
308 sprintf (bcs_set, "%lu", bootcount);
309 setenv ("bootcount", bcs_set);
310 bcs = getenv ("bootlimit");
311 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
312 #endif /* CONFIG_BOOTCOUNT_LIMIT */
314 #ifdef CONFIG_MODEM_SUPPORT
315 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
317 char *str = strdup(getenv("mdm_cmd"));
318 setenv ("preboot", str); /* set or delete definition */
321 mdm_init(); /* wait for modem connection */
323 #endif /* CONFIG_MODEM_SUPPORT */
325 #ifdef CONFIG_VERSION_VARIABLE
327 extern char version_string[];
329 setenv ("ver", version_string); /* set version variable */
331 #endif /* CONFIG_VERSION_VARIABLE */
333 #ifdef CFG_HUSH_PARSER
334 u_boot_hush_start ();
337 #ifdef CONFIG_AUTO_COMPLETE
338 install_auto_complete();
341 #ifdef CONFIG_PREBOOT
342 if ((p = getenv ("preboot")) != NULL) {
343 # ifdef CONFIG_AUTOBOOT_KEYED
344 int prev = disable_ctrlc(1); /* disable Control C checking */
347 # ifndef CFG_HUSH_PARSER
350 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
351 FLAG_EXIT_FROM_LOOP);
354 # ifdef CONFIG_AUTOBOOT_KEYED
355 disable_ctrlc(prev); /* restore Control C checking */
358 #endif /* CONFIG_PREBOOT */
360 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
361 s = getenv ("bootdelay");
362 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
364 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
366 # ifdef CONFIG_BOOT_RETRY_TIME
368 # endif /* CONFIG_BOOT_RETRY_TIME */
371 if (gd->flags & GD_FLG_POSTFAIL) {
372 s = getenv("failbootcmd");
375 #endif /* CONFIG_POST */
376 #ifdef CONFIG_BOOTCOUNT_LIMIT
377 if (bootlimit && (bootcount > bootlimit)) {
378 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
379 (unsigned)bootlimit);
380 s = getenv ("altbootcmd");
383 #endif /* CONFIG_BOOTCOUNT_LIMIT */
384 s = getenv ("bootcmd");
386 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
388 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
389 # ifdef CONFIG_AUTOBOOT_KEYED
390 int prev = disable_ctrlc(1); /* disable Control C checking */
393 # ifndef CFG_HUSH_PARSER
396 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
397 FLAG_EXIT_FROM_LOOP);
400 # ifdef CONFIG_AUTOBOOT_KEYED
401 disable_ctrlc(prev); /* restore Control C checking */
405 # ifdef CONFIG_MENUKEY
406 if (menukey == CONFIG_MENUKEY) {
407 s = getenv("menucmd");
409 # ifndef CFG_HUSH_PARSER
412 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
413 FLAG_EXIT_FROM_LOOP);
417 #endif /* CONFIG_MENUKEY */
418 #endif /* CONFIG_BOOTDELAY */
420 #ifdef CONFIG_AMIGAONEG3SE
422 extern void video_banner(void);
428 * Main Loop for Monitor Command Processing
430 #ifdef CFG_HUSH_PARSER
432 /* This point is never reached */
436 #ifdef CONFIG_BOOT_RETRY_TIME
438 /* Saw enough of a valid command to
439 * restart the timeout.
444 len = readline (CFG_PROMPT);
446 flag = 0; /* assume no special flags for now */
448 strcpy (lastcommand, console_buffer);
450 flag |= CMD_FLAG_REPEAT;
451 #ifdef CONFIG_BOOT_RETRY_TIME
452 else if (len == -2) {
453 /* -2 means timed out, retry autoboot
455 puts ("\nTimed out waiting for command\n");
456 # ifdef CONFIG_RESET_TO_RETRY
457 /* Reinit board to run initialization code again */
458 do_reset (NULL, 0, 0, NULL);
460 return; /* retry autoboot */
466 puts ("<INTERRUPT>\n");
468 rc = run_command (lastcommand, flag);
471 /* invalid command or not repeatable, forget it */
475 #endif /*CFG_HUSH_PARSER*/
478 #ifdef CONFIG_BOOT_RETRY_TIME
479 /***************************************************************************
480 * initialize command line timeout
482 void init_cmd_timeout(void)
484 char *s = getenv ("bootretry");
487 retry_time = (int)simple_strtol(s, NULL, 10);
489 retry_time = CONFIG_BOOT_RETRY_TIME;
491 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
492 retry_time = CONFIG_BOOT_RETRY_MIN;
495 /***************************************************************************
496 * reset command line timeout to retry_time seconds
498 void reset_cmd_timeout(void)
500 endtime = endtick(retry_time);
504 #ifdef CONFIG_CMDLINE_EDITING
507 * cmdline-editing related codes from vivi.
508 * Author: Janghoon Lyu <nandy@mizi.com>
511 #define putnstr(str,n) do { \
512 printf ("%.*s", (int)n, str); \
515 #define CTL_CH(c) ((c) - 'a' + 1)
517 #define MAX_CMDBUF_SIZE 256
519 #define CTL_BACKSPACE ('\b')
520 #define DEL ((char)255)
521 #define DEL7 ((char)127)
522 #define CREAD_HIST_CHAR ('!')
524 #define getcmd_putch(ch) putc(ch)
525 #define getcmd_getch() getc()
526 #define getcmd_cbeep() getcmd_putch('\a')
529 #define HIST_SIZE MAX_CMDBUF_SIZE
531 static int hist_max = 0;
532 static int hist_add_idx = 0;
533 static int hist_cur = -1;
534 unsigned hist_num = 0;
536 char* hist_list[HIST_MAX];
537 char hist_lines[HIST_MAX][HIST_SIZE];
539 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
541 static void hist_init(void)
550 for (i = 0; i < HIST_MAX; i++) {
551 hist_list[i] = hist_lines[i];
552 hist_list[i][0] = '\0';
556 static void cread_add_to_hist(char *line)
558 strcpy(hist_list[hist_add_idx], line);
560 if (++hist_add_idx >= HIST_MAX)
563 if (hist_add_idx > hist_max)
564 hist_max = hist_add_idx;
569 static char* hist_prev(void)
581 if (hist_cur == hist_add_idx) {
585 ret = hist_list[hist_cur];
590 static char* hist_next(void)
597 if (hist_cur == hist_add_idx)
600 if (++hist_cur > hist_max)
603 if (hist_cur == hist_add_idx) {
606 ret = hist_list[hist_cur];
611 #ifndef CONFIG_CMDLINE_EDITING
612 static void cread_print_hist_list(void)
617 n = hist_num - hist_max;
619 i = hist_add_idx + 1;
623 if (i == hist_add_idx)
625 printf("%s\n", hist_list[i]);
630 #endif /* CONFIG_CMDLINE_EDITING */
632 #define BEGINNING_OF_LINE() { \
634 getcmd_putch(CTL_BACKSPACE); \
639 #define ERASE_TO_EOL() { \
640 if (num < eol_num) { \
642 for (tmp = num; tmp < eol_num; tmp++) \
644 while (tmp-- > num) \
645 getcmd_putch(CTL_BACKSPACE); \
650 #define REFRESH_TO_EOL() { \
651 if (num < eol_num) { \
652 wlen = eol_num - num; \
653 putnstr(buf + num, wlen); \
658 static void cread_add_char(char ichar, int insert, unsigned long *num,
659 unsigned long *eol_num, char *buf, unsigned long len)
664 if (insert || *num == *eol_num) {
665 if (*eol_num > len - 1) {
673 wlen = *eol_num - *num;
675 memmove(&buf[*num+1], &buf[*num], wlen-1);
679 putnstr(buf + *num, wlen);
682 getcmd_putch(CTL_BACKSPACE);
685 /* echo the character */
688 putnstr(buf + *num, wlen);
693 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
694 unsigned long *eol_num, char *buf, unsigned long len)
697 cread_add_char(*str, insert, num, eol_num, buf, len);
702 static int cread_line(const char *const prompt, char *buf, unsigned int *len)
704 unsigned long num = 0;
705 unsigned long eol_num = 0;
716 #ifdef CONFIG_BOOT_RETRY_TIME
717 while (!tstc()) { /* while no incoming data */
718 if (retry_time >= 0 && get_ticks() > endtime)
719 return (-2); /* timed out */
723 ichar = getcmd_getch();
725 if ((ichar == '\n') || (ichar == '\r')) {
731 * handle standard linux xterm esc sequences for arrow key, etc.
736 esc_save[esc_len] = ichar;
739 cread_add_str(esc_save, esc_len, insert,
740 &num, &eol_num, buf, *len);
748 case 'D': /* <- key */
752 case 'C': /* -> key */
755 break; /* pass off to ^F handler */
756 case 'H': /* Home key */
759 break; /* pass off to ^A handler */
760 case 'A': /* up arrow */
763 break; /* pass off to ^P handler */
764 case 'B': /* down arrow */
767 break; /* pass off to ^N handler */
769 esc_save[esc_len++] = ichar;
770 cread_add_str(esc_save, esc_len, insert,
771 &num, &eol_num, buf, *len);
780 esc_save[esc_len] = ichar;
783 puts("impossible condition #876\n");
791 case CTL_CH('c'): /* ^C - break */
792 *buf = '\0'; /* discard input */
796 getcmd_putch(buf[num]);
802 getcmd_putch(CTL_BACKSPACE);
808 wlen = eol_num - num - 1;
810 memmove(&buf[num], &buf[num+1], wlen);
811 putnstr(buf + num, wlen);
816 getcmd_putch(CTL_BACKSPACE);
839 wlen = eol_num - num;
841 memmove(&buf[num], &buf[num+1], wlen);
842 getcmd_putch(CTL_BACKSPACE);
843 putnstr(buf + num, wlen);
846 getcmd_putch(CTL_BACKSPACE);
858 if (ichar == CTL_CH('p'))
868 /* nuke the current line */
872 /* erase to end of line */
875 /* copy new line into place and display */
877 eol_num = strlen(buf);
881 #ifdef CONFIG_AUTO_COMPLETE
885 /* do not autocomplete when in the middle */
892 col = strlen(prompt) + eol_num;
894 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
903 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
908 buf[eol_num] = '\0'; /* lose the newline */
910 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
911 cread_add_to_hist(buf);
912 hist_cur = hist_add_idx;
917 #endif /* CONFIG_CMDLINE_EDITING */
919 /****************************************************************************/
922 * Prompt for input and read a line.
923 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
924 * time out when time goes past endtime (timebase time in ticks).
925 * Return: number of read characters
929 int readline (const char *const prompt)
931 return readline_into_buffer(prompt, console_buffer);
935 int readline_into_buffer (const char *const prompt, char * buffer)
938 #ifdef CONFIG_CMDLINE_EDITING
939 unsigned int len=MAX_CMDBUF_SIZE;
941 static int initted = 0;
944 * History uses a global array which is not
945 * writable until after relocation to RAM.
946 * Revert to non-history version if still
947 * running from flash.
949 if (gd->flags & GD_FLG_RELOC) {
957 rc = cread_line(prompt, p, &len);
958 return rc < 0 ? rc : len;
961 #endif /* CONFIG_CMDLINE_EDITING */
963 int n = 0; /* buffer index */
964 int plen = 0; /* prompt length */
965 int col; /* output column cnt */
970 plen = strlen (prompt);
976 #ifdef CONFIG_BOOT_RETRY_TIME
977 while (!tstc()) { /* while no incoming data */
978 if (retry_time >= 0 && get_ticks() > endtime)
979 return (-2); /* timed out */
982 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
984 #ifdef CONFIG_SHOW_ACTIVITY
986 extern void show_activity(int arg);
993 * Special character handling
996 case '\r': /* Enter */
1002 case '\0': /* nul */
1005 case 0x03: /* ^C - break */
1006 p_buf[0] = '\0'; /* discard input */
1009 case 0x15: /* ^U - erase line */
1010 while (col > plen) {
1018 case 0x17: /* ^W - erase word */
1019 p=delete_char(p_buf, p, &col, &n, plen);
1020 while ((n > 0) && (*p != ' ')) {
1021 p=delete_char(p_buf, p, &col, &n, plen);
1025 case 0x08: /* ^H - backspace */
1026 case 0x7F: /* DEL - backspace */
1027 p=delete_char(p_buf, p, &col, &n, plen);
1032 * Must be a normal character then
1034 if (n < CFG_CBSIZE-2) {
1035 if (c == '\t') { /* expand TABs */
1036 #ifdef CONFIG_AUTO_COMPLETE
1037 /* if auto completion triggered just continue */
1039 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1040 p = p_buf + n; /* reset */
1044 puts (tab_seq+(col&07));
1045 col += 8 - (col&07);
1047 ++col; /* echo input */
1052 } else { /* Buffer full */
1057 #ifdef CONFIG_CMDLINE_EDITING
1062 /****************************************************************************/
1064 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1072 if (*(--p) == '\t') { /* will retype the whole line */
1073 while (*colp > plen) {
1077 for (s=buffer; s<p; ++s) {
1079 puts (tab_seq+((*colp) & 07));
1080 *colp += 8 - ((*colp) & 07);
1094 /****************************************************************************/
1096 int parse_line (char *line, char *argv[])
1101 printf ("parse_line: \"%s\"\n", line);
1103 while (nargs < CFG_MAXARGS) {
1105 /* skip any white space */
1106 while ((*line == ' ') || (*line == '\t')) {
1110 if (*line == '\0') { /* end of line, no more args */
1113 printf ("parse_line: nargs=%d\n", nargs);
1118 argv[nargs++] = line; /* begin of argument string */
1120 /* find end of string */
1121 while (*line && (*line != ' ') && (*line != '\t')) {
1125 if (*line == '\0') { /* end of line, no more args */
1128 printf ("parse_line: nargs=%d\n", nargs);
1133 *line++ = '\0'; /* terminate current arg */
1136 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1139 printf ("parse_line: nargs=%d\n", nargs);
1144 /****************************************************************************/
1146 static void process_macros (const char *input, char *output)
1149 const char *varname_start = NULL;
1150 int inputcnt = strlen (input);
1151 int outputcnt = CFG_CBSIZE;
1152 int state = 0; /* 0 = waiting for '$' */
1154 /* 1 = waiting for '(' or '{' */
1155 /* 2 = waiting for ')' or '}' */
1156 /* 3 = waiting for ''' */
1158 char *output_start = output;
1160 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1164 prev = '\0'; /* previous character */
1166 while (inputcnt && outputcnt) {
1171 /* remove one level of escape characters */
1172 if ((c == '\\') && (prev != '\\')) {
1173 if (inputcnt-- == 0)
1181 case 0: /* Waiting for (unescaped) $ */
1182 if ((c == '\'') && (prev != '\\')) {
1186 if ((c == '$') && (prev != '\\')) {
1193 case 1: /* Waiting for ( */
1194 if (c == '(' || c == '{') {
1196 varname_start = input;
1208 case 2: /* Waiting for ) */
1209 if (c == ')' || c == '}') {
1211 char envname[CFG_CBSIZE], *envval;
1212 int envcnt = input - varname_start - 1; /* Varname # of chars */
1214 /* Get the varname */
1215 for (i = 0; i < envcnt; i++) {
1216 envname[i] = varname_start[i];
1221 envval = getenv (envname);
1223 /* Copy into the line if it exists */
1225 while ((*envval) && outputcnt) {
1226 *(output++) = *(envval++);
1229 /* Look for another '$' */
1233 case 3: /* Waiting for ' */
1234 if ((c == '\'') && (prev != '\\')) {
1251 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1252 strlen (output_start), output_start);
1256 /****************************************************************************
1258 * 1 - command executed, repeatable
1259 * 0 - command executed but not repeatable, interrupted commands are
1260 * always considered not repeatable
1261 * -1 - not executed (unrecognized, bootd recursion or too many args)
1262 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1263 * considered unrecognized)
1267 * We must create a temporary copy of the command since the command we get
1268 * may be the result from getenv(), which returns a pointer directly to
1269 * the environment data, which may change magicly when the command we run
1270 * creates or modifies environment variables (like "bootp" does).
1273 int run_command (const char *cmd, int flag)
1276 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1277 char *token; /* start of token in cmdbuf */
1278 char *sep; /* end of token (separator) in cmdbuf */
1279 char finaltoken[CFG_CBSIZE];
1281 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1287 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1288 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1292 clear_ctrlc(); /* forget any previous Control C */
1294 if (!cmd || !*cmd) {
1295 return -1; /* empty command */
1298 if (strlen(cmd) >= CFG_CBSIZE) {
1299 puts ("## Command too long!\n");
1303 strcpy (cmdbuf, cmd);
1305 /* Process separators and check for invalid
1306 * repeatable commands
1310 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1315 * Find separator, or string end
1316 * Allow simple escape of ';' by writing "\;"
1318 for (inquotes = 0, sep = str; *sep; sep++) {
1324 (*sep == ';') && /* separator */
1325 ( sep != str) && /* past string start */
1326 (*(sep-1) != '\\')) /* and NOT escaped */
1331 * Limit the token to data between separators
1335 str = sep + 1; /* start of command for next pass */
1339 str = sep; /* no more commands for next pass */
1341 printf ("token: \"%s\"\n", token);
1344 /* find macros in this token and replace them */
1345 process_macros (token, finaltoken);
1347 /* Extract arguments */
1348 if ((argc = parse_line (finaltoken, argv)) == 0) {
1349 rc = -1; /* no command at all */
1353 /* Look up command in command table */
1354 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1355 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1356 rc = -1; /* give up after bad command */
1360 /* found - check max args */
1361 if (argc > cmdtp->maxargs) {
1362 printf ("Usage:\n%s\n", cmdtp->usage);
1367 #if defined(CONFIG_CMD_BOOTD)
1368 /* avoid "bootd" recursion */
1369 if (cmdtp->cmd == do_bootd) {
1371 printf ("[%s]\n", finaltoken);
1373 if (flag & CMD_FLAG_BOOTD) {
1374 puts ("'bootd' recursion detected\n");
1378 flag |= CMD_FLAG_BOOTD;
1383 /* OK - call function to do the command */
1384 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1388 repeatable &= cmdtp->repeatable;
1390 /* Did the user stop this? */
1392 return -1; /* if stopped then not repeatable */
1395 return rc ? rc : repeatable;
1398 /****************************************************************************/
1400 #if defined(CONFIG_CMD_RUN)
1401 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1406 printf ("Usage:\n%s\n", cmdtp->usage);
1410 for (i=1; i<argc; ++i) {
1413 if ((arg = getenv (argv[i])) == NULL) {
1414 printf ("## Error: \"%s\" not defined\n", argv[i]);
1417 #ifndef CFG_HUSH_PARSER
1418 if (run_command (arg, flag) == -1)
1421 if (parse_string_outer(arg,
1422 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)