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 CONFIG_SYS_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 show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
53 #if defined(CONFIG_UPDATE_TFTP)
54 void update_tftp (void);
55 #endif /* CONFIG_UPDATE_TFTP */
57 #define MAX_DELAY_STOP_STR 32
59 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
60 static int abortboot(int);
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
90 * 1 - got key string, abort
92 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
93 # if defined(CONFIG_AUTOBOOT_KEYED)
94 static __inline__ int abortboot(int bootdelay)
97 uint64_t etime = endtick(bootdelay);
104 { str: getenv ("bootdelaykey"), retry: 1 },
105 { str: getenv ("bootdelaykey2"), retry: 1 },
106 { str: getenv ("bootstopkey"), retry: 0 },
107 { str: getenv ("bootstopkey2"), retry: 0 },
110 char presskey [MAX_DELAY_STOP_STR];
111 u_int presskey_len = 0;
112 u_int presskey_max = 0;
115 # ifdef CONFIG_AUTOBOOT_PROMPT
116 printf(CONFIG_AUTOBOOT_PROMPT);
119 # ifdef CONFIG_AUTOBOOT_DELAY_STR
120 if (delaykey[0].str == NULL)
121 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
123 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
124 if (delaykey[1].str == NULL)
125 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
127 # ifdef CONFIG_AUTOBOOT_STOP_STR
128 if (delaykey[2].str == NULL)
129 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
131 # ifdef CONFIG_AUTOBOOT_STOP_STR2
132 if (delaykey[3].str == NULL)
133 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
136 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
137 delaykey[i].len = delaykey[i].str == NULL ?
138 0 : strlen (delaykey[i].str);
139 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
140 MAX_DELAY_STOP_STR : delaykey[i].len;
142 presskey_max = presskey_max > delaykey[i].len ?
143 presskey_max : delaykey[i].len;
146 printf("%s key:<%s>\n",
147 delaykey[i].retry ? "delay" : "stop",
148 delaykey[i].str ? delaykey[i].str : "NULL");
152 /* In order to keep up with incoming data, check timeout only
157 if (presskey_len < presskey_max) {
158 presskey [presskey_len ++] = getc();
161 for (i = 0; i < presskey_max - 1; i ++)
162 presskey [i] = presskey [i + 1];
164 presskey [i] = getc();
168 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
169 if (delaykey[i].len > 0 &&
170 presskey_len >= delaykey[i].len &&
171 memcmp (presskey + presskey_len - delaykey[i].len,
173 delaykey[i].len) == 0) {
175 printf("got %skey\n",
176 delaykey[i].retry ? "delay" : "stop");
179 # ifdef CONFIG_BOOT_RETRY_TIME
180 /* don't retry auto boot */
181 if (! delaykey[i].retry)
187 } while (!abort && get_ticks() <= etime);
191 puts("key timeout\n");
194 #ifdef CONFIG_SILENT_CONSOLE
196 gd->flags &= ~GD_FLG_SILENT;
202 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
204 #ifdef CONFIG_MENUKEY
205 static int menukey = 0;
208 static __inline__ int abortboot(int bootdelay)
212 #ifdef CONFIG_MENUPROMPT
213 printf(CONFIG_MENUPROMPT);
215 printf("Hit any key to stop autoboot: %2d ", bootdelay);
218 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
220 * Check if key already pressed
221 * Don't check if bootdelay < 0
223 if (bootdelay >= 0) {
224 if (tstc()) { /* we got a key press */
225 (void) getc(); /* consume input */
227 abort = 1; /* don't auto boot */
232 while ((bootdelay > 0) && (!abort)) {
236 /* delay 100 * 10ms */
237 for (i=0; !abort && i<100; ++i) {
238 if (tstc()) { /* we got a key press */
239 abort = 1; /* don't auto boot */
240 bootdelay = 0; /* no more delay */
241 # ifdef CONFIG_MENUKEY
244 (void) getc(); /* consume input */
251 printf("\b\b\b%2d ", bootdelay);
256 #ifdef CONFIG_SILENT_CONSOLE
258 gd->flags &= ~GD_FLG_SILENT;
263 # endif /* CONFIG_AUTOBOOT_KEYED */
264 #endif /* CONFIG_BOOTDELAY >= 0 */
266 /****************************************************************************/
268 void main_loop (void)
270 #ifndef CONFIG_SYS_HUSH_PARSER
271 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
277 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
281 #ifdef CONFIG_PREBOOT
284 #ifdef CONFIG_BOOTCOUNT_LIMIT
285 unsigned long bootcount = 0;
286 unsigned long bootlimit = 0;
289 #endif /* CONFIG_BOOTCOUNT_LIMIT */
291 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
292 ulong bmp = 0; /* default bitmap */
293 extern int trab_vfd (ulong bitmap);
295 #ifdef CONFIG_MODEM_SUPPORT
297 bmp = 1; /* alternate bitmap */
300 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
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 extern char version_string[];
327 setenv ("ver", version_string); /* set version variable */
329 #endif /* CONFIG_VERSION_VARIABLE */
331 #ifdef CONFIG_SYS_HUSH_PARSER
332 u_boot_hush_start ();
335 #if defined(CONFIG_HUSH_INIT_VAR)
339 #ifdef CONFIG_PREBOOT
340 if ((p = getenv ("preboot")) != NULL) {
341 # ifdef CONFIG_AUTOBOOT_KEYED
342 int prev = disable_ctrlc(1); /* disable Control C checking */
345 # ifndef CONFIG_SYS_HUSH_PARSER
348 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
349 FLAG_EXIT_FROM_LOOP);
352 # ifdef CONFIG_AUTOBOOT_KEYED
353 disable_ctrlc(prev); /* restore Control C checking */
356 #endif /* CONFIG_PREBOOT */
358 #if defined(CONFIG_UPDATE_TFTP)
360 #endif /* CONFIG_UPDATE_TFTP */
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 CONFIG_SYS_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 CONFIG_SYS_HUSH_PARSER
414 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
415 FLAG_EXIT_FROM_LOOP);
419 #endif /* CONFIG_MENUKEY */
420 #endif /* CONFIG_BOOTDELAY */
423 * Main Loop for Monitor Command Processing
425 #ifdef CONFIG_SYS_HUSH_PARSER
427 /* This point is never reached */
431 #ifdef CONFIG_BOOT_RETRY_TIME
433 /* Saw enough of a valid command to
434 * restart the timeout.
439 len = readline (CONFIG_SYS_PROMPT);
441 flag = 0; /* assume no special flags for now */
443 strcpy (lastcommand, console_buffer);
445 flag |= CMD_FLAG_REPEAT;
446 #ifdef CONFIG_BOOT_RETRY_TIME
447 else if (len == -2) {
448 /* -2 means timed out, retry autoboot
450 puts ("\nTimed out waiting for command\n");
451 # ifdef CONFIG_RESET_TO_RETRY
452 /* Reinit board to run initialization code again */
453 do_reset (NULL, 0, 0, NULL);
455 return; /* retry autoboot */
461 puts ("<INTERRUPT>\n");
463 rc = run_command (lastcommand, flag);
466 /* invalid command or not repeatable, forget it */
470 #endif /*CONFIG_SYS_HUSH_PARSER*/
473 #ifdef CONFIG_BOOT_RETRY_TIME
474 /***************************************************************************
475 * initialize command line timeout
477 void init_cmd_timeout(void)
479 char *s = getenv ("bootretry");
482 retry_time = (int)simple_strtol(s, NULL, 10);
484 retry_time = CONFIG_BOOT_RETRY_TIME;
486 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
487 retry_time = CONFIG_BOOT_RETRY_MIN;
490 /***************************************************************************
491 * reset command line timeout to retry_time seconds
493 void reset_cmd_timeout(void)
495 endtime = endtick(retry_time);
499 #ifdef CONFIG_CMDLINE_EDITING
502 * cmdline-editing related codes from vivi.
503 * Author: Janghoon Lyu <nandy@mizi.com>
506 #define putnstr(str,n) do { \
507 printf ("%.*s", (int)n, str); \
510 #define CTL_CH(c) ((c) - 'a' + 1)
511 #define CTL_BACKSPACE ('\b')
512 #define DEL ((char)255)
513 #define DEL7 ((char)127)
514 #define CREAD_HIST_CHAR ('!')
516 #define getcmd_putch(ch) putc(ch)
517 #define getcmd_getch() getc()
518 #define getcmd_cbeep() getcmd_putch('\a')
521 #define HIST_SIZE CONFIG_SYS_CBSIZE
523 static int hist_max = 0;
524 static int hist_add_idx = 0;
525 static int hist_cur = -1;
526 unsigned hist_num = 0;
528 char* hist_list[HIST_MAX];
529 char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
531 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
533 static void hist_init(void)
542 for (i = 0; i < HIST_MAX; i++) {
543 hist_list[i] = hist_lines[i];
544 hist_list[i][0] = '\0';
548 static void cread_add_to_hist(char *line)
550 strcpy(hist_list[hist_add_idx], line);
552 if (++hist_add_idx >= HIST_MAX)
555 if (hist_add_idx > hist_max)
556 hist_max = hist_add_idx;
561 static char* hist_prev(void)
573 if (hist_cur == hist_add_idx) {
577 ret = hist_list[hist_cur];
582 static char* hist_next(void)
589 if (hist_cur == hist_add_idx)
592 if (++hist_cur > hist_max)
595 if (hist_cur == hist_add_idx) {
598 ret = hist_list[hist_cur];
603 #ifndef CONFIG_CMDLINE_EDITING
604 static void cread_print_hist_list(void)
609 n = hist_num - hist_max;
611 i = hist_add_idx + 1;
615 if (i == hist_add_idx)
617 printf("%s\n", hist_list[i]);
622 #endif /* CONFIG_CMDLINE_EDITING */
624 #define BEGINNING_OF_LINE() { \
626 getcmd_putch(CTL_BACKSPACE); \
631 #define ERASE_TO_EOL() { \
632 if (num < eol_num) { \
633 printf("%*s", (int)(eol_num - num), ""); \
635 getcmd_putch(CTL_BACKSPACE); \
636 } while (--eol_num > num); \
640 #define REFRESH_TO_EOL() { \
641 if (num < eol_num) { \
642 wlen = eol_num - num; \
643 putnstr(buf + num, wlen); \
648 static void cread_add_char(char ichar, int insert, unsigned long *num,
649 unsigned long *eol_num, char *buf, unsigned long len)
654 if (insert || *num == *eol_num) {
655 if (*eol_num > len - 1) {
663 wlen = *eol_num - *num;
665 memmove(&buf[*num+1], &buf[*num], wlen-1);
669 putnstr(buf + *num, wlen);
672 getcmd_putch(CTL_BACKSPACE);
675 /* echo the character */
678 putnstr(buf + *num, wlen);
683 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
684 unsigned long *eol_num, char *buf, unsigned long len)
687 cread_add_char(*str, insert, num, eol_num, buf, len);
692 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);
704 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
707 #ifdef CONFIG_BOOT_RETRY_TIME
708 while (!tstc()) { /* while no incoming data */
709 if (retry_time >= 0 && get_ticks() > endtime)
710 return (-2); /* timed out */
715 ichar = getcmd_getch();
717 if ((ichar == '\n') || (ichar == '\r')) {
723 * handle standard linux xterm esc sequences for arrow key, etc.
728 esc_save[esc_len] = ichar;
731 cread_add_str(esc_save, esc_len, insert,
732 &num, &eol_num, buf, *len);
740 case 'D': /* <- key */
744 case 'C': /* -> key */
747 break; /* pass off to ^F handler */
748 case 'H': /* Home key */
751 break; /* pass off to ^A handler */
752 case 'A': /* up arrow */
755 break; /* pass off to ^P handler */
756 case 'B': /* down arrow */
759 break; /* pass off to ^N handler */
761 esc_save[esc_len++] = ichar;
762 cread_add_str(esc_save, esc_len, insert,
763 &num, &eol_num, buf, *len);
772 esc_save[esc_len] = ichar;
775 puts("impossible condition #876\n");
783 case CTL_CH('c'): /* ^C - break */
784 *buf = '\0'; /* discard input */
788 getcmd_putch(buf[num]);
794 getcmd_putch(CTL_BACKSPACE);
800 wlen = eol_num - num - 1;
802 memmove(&buf[num], &buf[num+1], wlen);
803 putnstr(buf + num, wlen);
808 getcmd_putch(CTL_BACKSPACE);
831 wlen = eol_num - num;
833 memmove(&buf[num], &buf[num+1], wlen);
834 getcmd_putch(CTL_BACKSPACE);
835 putnstr(buf + num, wlen);
838 getcmd_putch(CTL_BACKSPACE);
850 if (ichar == CTL_CH('p'))
860 /* nuke the current line */
864 /* erase to end of line */
867 /* copy new line into place and display */
869 eol_num = strlen(buf);
873 #ifdef CONFIG_AUTO_COMPLETE
877 /* do not autocomplete when in the middle */
884 col = strlen(prompt) + eol_num;
886 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
895 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
900 buf[eol_num] = '\0'; /* lose the newline */
902 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
903 cread_add_to_hist(buf);
904 hist_cur = hist_add_idx;
909 #endif /* CONFIG_CMDLINE_EDITING */
911 /****************************************************************************/
914 * Prompt for input and read a line.
915 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
916 * time out when time goes past endtime (timebase time in ticks).
917 * Return: number of read characters
921 int readline (const char *const prompt)
924 * If console_buffer isn't 0-length the user will be prompted to modify
925 * it instead of entering it from scratch as desired.
927 console_buffer[0] = '\0';
929 return readline_into_buffer(prompt, console_buffer);
933 int readline_into_buffer (const char *const prompt, char * buffer)
936 #ifdef CONFIG_CMDLINE_EDITING
937 unsigned int len = CONFIG_SYS_CBSIZE;
939 static int initted = 0;
942 * History uses a global array which is not
943 * writable until after relocation to RAM.
944 * Revert to non-history version if still
945 * running from flash.
947 if (gd->flags & GD_FLG_RELOC) {
956 rc = cread_line(prompt, p, &len);
957 return rc < 0 ? rc : len;
960 #endif /* CONFIG_CMDLINE_EDITING */
962 int n = 0; /* buffer index */
963 int plen = 0; /* prompt length */
964 int col; /* output column cnt */
969 plen = strlen (prompt);
975 #ifdef CONFIG_BOOT_RETRY_TIME
976 while (!tstc()) { /* while no incoming data */
977 if (retry_time >= 0 && get_ticks() > endtime)
978 return (-2); /* timed out */
982 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
984 #ifdef CONFIG_SHOW_ACTIVITY
986 extern void show_activity(int arg);
994 * Special character handling
997 case '\r': /* Enter */
1003 case '\0': /* nul */
1006 case 0x03: /* ^C - break */
1007 p_buf[0] = '\0'; /* discard input */
1010 case 0x15: /* ^U - erase line */
1011 while (col > plen) {
1019 case 0x17: /* ^W - erase word */
1020 p=delete_char(p_buf, p, &col, &n, plen);
1021 while ((n > 0) && (*p != ' ')) {
1022 p=delete_char(p_buf, p, &col, &n, plen);
1026 case 0x08: /* ^H - backspace */
1027 case 0x7F: /* DEL - backspace */
1028 p=delete_char(p_buf, p, &col, &n, plen);
1033 * Must be a normal character then
1035 if (n < CONFIG_SYS_CBSIZE-2) {
1036 if (c == '\t') { /* expand TABs */
1037 #ifdef CONFIG_AUTO_COMPLETE
1038 /* if auto completion triggered just continue */
1040 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1041 p = p_buf + n; /* reset */
1045 puts (tab_seq+(col&07));
1046 col += 8 - (col&07);
1048 ++col; /* echo input */
1053 } else { /* Buffer full */
1058 #ifdef CONFIG_CMDLINE_EDITING
1063 /****************************************************************************/
1065 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1073 if (*(--p) == '\t') { /* will retype the whole line */
1074 while (*colp > plen) {
1078 for (s=buffer; s<p; ++s) {
1080 puts (tab_seq+((*colp) & 07));
1081 *colp += 8 - ((*colp) & 07);
1095 /****************************************************************************/
1097 int parse_line (char *line, char *argv[])
1102 printf ("parse_line: \"%s\"\n", line);
1104 while (nargs < CONFIG_SYS_MAXARGS) {
1106 /* skip any white space */
1107 while ((*line == ' ') || (*line == '\t')) {
1111 if (*line == '\0') { /* end of line, no more args */
1114 printf ("parse_line: nargs=%d\n", nargs);
1119 argv[nargs++] = line; /* begin of argument string */
1121 /* find end of string */
1122 while (*line && (*line != ' ') && (*line != '\t')) {
1126 if (*line == '\0') { /* end of line, no more args */
1129 printf ("parse_line: nargs=%d\n", nargs);
1134 *line++ = '\0'; /* terminate current arg */
1137 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1140 printf ("parse_line: nargs=%d\n", nargs);
1145 /****************************************************************************/
1147 static void process_macros (const char *input, char *output)
1150 const char *varname_start = NULL;
1151 int inputcnt = strlen (input);
1152 int outputcnt = CONFIG_SYS_CBSIZE;
1153 int state = 0; /* 0 = waiting for '$' */
1155 /* 1 = waiting for '(' or '{' */
1156 /* 2 = waiting for ')' or '}' */
1157 /* 3 = waiting for ''' */
1159 char *output_start = output;
1161 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1165 prev = '\0'; /* previous character */
1167 while (inputcnt && outputcnt) {
1172 /* remove one level of escape characters */
1173 if ((c == '\\') && (prev != '\\')) {
1174 if (inputcnt-- == 0)
1182 case 0: /* Waiting for (unescaped) $ */
1183 if ((c == '\'') && (prev != '\\')) {
1187 if ((c == '$') && (prev != '\\')) {
1194 case 1: /* Waiting for ( */
1195 if (c == '(' || c == '{') {
1197 varname_start = input;
1209 case 2: /* Waiting for ) */
1210 if (c == ')' || c == '}') {
1212 char envname[CONFIG_SYS_CBSIZE], *envval;
1213 int envcnt = input - varname_start - 1; /* Varname # of chars */
1215 /* Get the varname */
1216 for (i = 0; i < envcnt; i++) {
1217 envname[i] = varname_start[i];
1222 envval = getenv (envname);
1224 /* Copy into the line if it exists */
1226 while ((*envval) && outputcnt) {
1227 *(output++) = *(envval++);
1230 /* Look for another '$' */
1234 case 3: /* Waiting for ' */
1235 if ((c == '\'') && (prev != '\\')) {
1252 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1253 strlen (output_start), output_start);
1257 /****************************************************************************
1259 * 1 - command executed, repeatable
1260 * 0 - command executed but not repeatable, interrupted commands are
1261 * always considered not repeatable
1262 * -1 - not executed (unrecognized, bootd recursion or too many args)
1263 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1264 * considered unrecognized)
1268 * We must create a temporary copy of the command since the command we get
1269 * may be the result from getenv(), which returns a pointer directly to
1270 * the environment data, which may change magicly when the command we run
1271 * creates or modifies environment variables (like "bootp" does).
1274 int run_command (const char *cmd, int flag)
1277 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1278 char *token; /* start of token in cmdbuf */
1279 char *sep; /* end of token (separator) in cmdbuf */
1280 char finaltoken[CONFIG_SYS_CBSIZE];
1282 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1288 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1289 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1293 clear_ctrlc(); /* forget any previous Control C */
1295 if (!cmd || !*cmd) {
1296 return -1; /* empty command */
1299 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1300 puts ("## Command too long!\n");
1304 strcpy (cmdbuf, cmd);
1306 /* Process separators and check for invalid
1307 * repeatable commands
1311 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1316 * Find separator, or string end
1317 * Allow simple escape of ';' by writing "\;"
1319 for (inquotes = 0, sep = str; *sep; sep++) {
1325 (*sep == ';') && /* separator */
1326 ( sep != str) && /* past string start */
1327 (*(sep-1) != '\\')) /* and NOT escaped */
1332 * Limit the token to data between separators
1336 str = sep + 1; /* start of command for next pass */
1340 str = sep; /* no more commands for next pass */
1342 printf ("token: \"%s\"\n", token);
1345 /* find macros in this token and replace them */
1346 process_macros (token, finaltoken);
1348 /* Extract arguments */
1349 if ((argc = parse_line (finaltoken, argv)) == 0) {
1350 rc = -1; /* no command at all */
1354 /* Look up command in command table */
1355 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1356 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1357 rc = -1; /* give up after bad command */
1361 /* found - check max args */
1362 if (argc > cmdtp->maxargs) {
1368 #if defined(CONFIG_CMD_BOOTD)
1369 /* avoid "bootd" recursion */
1370 if (cmdtp->cmd == do_bootd) {
1372 printf ("[%s]\n", finaltoken);
1374 if (flag & CMD_FLAG_BOOTD) {
1375 puts ("'bootd' recursion detected\n");
1379 flag |= CMD_FLAG_BOOTD;
1384 /* OK - call function to do the command */
1385 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1389 repeatable &= cmdtp->repeatable;
1391 /* Did the user stop this? */
1393 return -1; /* if stopped then not repeatable */
1396 return rc ? rc : repeatable;
1399 /****************************************************************************/
1401 #if defined(CONFIG_CMD_RUN)
1402 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1407 return cmd_usage(cmdtp);
1409 for (i=1; i<argc; ++i) {
1412 if ((arg = getenv (argv[i])) == NULL) {
1413 printf ("## Error: \"%s\" not defined\n", argv[i]);
1416 #ifndef CONFIG_SYS_HUSH_PARSER
1417 if (run_command (arg, flag) == -1)
1420 if (parse_string_outer(arg,
1421 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)