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,
34 #ifdef CONFIG_MODEM_SUPPORT
35 #include <malloc.h> /* for free() prototype */
38 #ifdef CONFIG_SYS_HUSH_PARSER
43 #include <linux/ctype.h>
45 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
46 DECLARE_GLOBAL_DATA_PTR;
50 * Board-specific Platform code can reimplement show_boot_progress () if needed
52 void inline __show_boot_progress (int val) {}
53 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
55 #if defined(CONFIG_UPDATE_TFTP)
56 int update_tftp (ulong addr);
57 #endif /* CONFIG_UPDATE_TFTP */
59 #define MAX_DELAY_STOP_STR 32
63 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
65 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
66 static const char erase_seq[] = "\b \b"; /* erase sequence */
67 static const char tab_seq[] = " "; /* used to expand TABs */
69 #ifdef CONFIG_BOOT_RETRY_TIME
70 static uint64_t endtime = 0; /* must be set, default is instant timeout */
71 static int retry_time = -1; /* -1 so can call readline before main_loop */
74 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
76 #ifndef CONFIG_BOOT_RETRY_MIN
77 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
80 #ifdef CONFIG_MODEM_SUPPORT
82 extern void mdm_init(void); /* defined in board.c */
85 /***************************************************************************
86 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
87 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
89 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
90 # if defined(CONFIG_AUTOBOOT_KEYED)
94 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;
211 int abortboot(int bootdelay)
215 #ifdef CONFIG_MENUPROMPT
216 printf(CONFIG_MENUPROMPT);
218 printf("Hit any key to stop autoboot: %2d ", bootdelay);
221 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
223 * Check if key already pressed
224 * Don't check if bootdelay < 0
226 if (bootdelay >= 0) {
227 if (tstc()) { /* we got a key press */
228 (void) getc(); /* consume input */
230 abort = 1; /* don't auto boot */
235 while ((bootdelay > 0) && (!abort)) {
239 /* delay 100 * 10ms */
240 for (i=0; !abort && i<100; ++i) {
241 if (tstc()) { /* we got a key press */
242 abort = 1; /* don't auto boot */
243 bootdelay = 0; /* no more delay */
244 # ifdef CONFIG_MENUKEY
247 (void) getc(); /* consume input */
254 printf("\b\b\b%2d ", bootdelay);
259 #ifdef CONFIG_SILENT_CONSOLE
261 gd->flags &= ~GD_FLG_SILENT;
266 # endif /* CONFIG_AUTOBOOT_KEYED */
267 #endif /* CONFIG_BOOTDELAY >= 0 */
270 * Return 0 on success, or != 0 on error.
273 int run_command2(const char *cmd, int flag)
275 #ifndef CONFIG_SYS_HUSH_PARSER
277 * run_command can return 0 or 1 for success, so clean up its result.
279 if (run_command(cmd, flag) == -1)
284 return parse_string_outer(cmd,
285 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
289 /****************************************************************************/
291 void main_loop (void)
293 #ifndef CONFIG_SYS_HUSH_PARSER
294 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
300 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
304 #ifdef CONFIG_PREBOOT
307 #ifdef CONFIG_BOOTCOUNT_LIMIT
308 unsigned long bootcount = 0;
309 unsigned long bootlimit = 0;
312 #endif /* CONFIG_BOOTCOUNT_LIMIT */
314 #ifdef CONFIG_BOOTCOUNT_LIMIT
315 bootcount = bootcount_load();
317 bootcount_store (bootcount);
318 sprintf (bcs_set, "%lu", bootcount);
319 setenv ("bootcount", bcs_set);
320 bcs = getenv ("bootlimit");
321 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
322 #endif /* CONFIG_BOOTCOUNT_LIMIT */
324 #ifdef CONFIG_MODEM_SUPPORT
325 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
327 char *str = strdup(getenv("mdm_cmd"));
328 setenv ("preboot", str); /* set or delete definition */
331 mdm_init(); /* wait for modem connection */
333 #endif /* CONFIG_MODEM_SUPPORT */
335 #ifdef CONFIG_VERSION_VARIABLE
337 setenv ("ver", version_string); /* set version variable */
339 #endif /* CONFIG_VERSION_VARIABLE */
341 #ifdef CONFIG_SYS_HUSH_PARSER
342 u_boot_hush_start ();
345 #if defined(CONFIG_HUSH_INIT_VAR)
349 #ifdef CONFIG_PREBOOT
350 if ((p = getenv ("preboot")) != NULL) {
351 # ifdef CONFIG_AUTOBOOT_KEYED
352 int prev = disable_ctrlc(1); /* disable Control C checking */
357 # ifdef CONFIG_AUTOBOOT_KEYED
358 disable_ctrlc(prev); /* restore Control C checking */
361 #endif /* CONFIG_PREBOOT */
363 #if defined(CONFIG_UPDATE_TFTP)
365 #endif /* CONFIG_UPDATE_TFTP */
367 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
368 s = getenv ("bootdelay");
369 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
371 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
373 # ifdef CONFIG_BOOT_RETRY_TIME
375 # endif /* CONFIG_BOOT_RETRY_TIME */
378 if (gd->flags & GD_FLG_POSTFAIL) {
379 s = getenv("failbootcmd");
382 #endif /* CONFIG_POST */
383 #ifdef CONFIG_BOOTCOUNT_LIMIT
384 if (bootlimit && (bootcount > bootlimit)) {
385 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
386 (unsigned)bootlimit);
387 s = getenv ("altbootcmd");
390 #endif /* CONFIG_BOOTCOUNT_LIMIT */
391 s = getenv ("bootcmd");
393 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
395 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
396 # ifdef CONFIG_AUTOBOOT_KEYED
397 int prev = disable_ctrlc(1); /* disable Control C checking */
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");
413 #endif /* CONFIG_MENUKEY */
414 #endif /* CONFIG_BOOTDELAY */
417 * Main Loop for Monitor Command Processing
419 #ifdef CONFIG_SYS_HUSH_PARSER
421 /* This point is never reached */
425 #ifdef CONFIG_BOOT_RETRY_TIME
427 /* Saw enough of a valid command to
428 * restart the timeout.
433 len = readline (CONFIG_SYS_PROMPT);
435 flag = 0; /* assume no special flags for now */
437 strcpy (lastcommand, console_buffer);
439 flag |= CMD_FLAG_REPEAT;
440 #ifdef CONFIG_BOOT_RETRY_TIME
441 else if (len == -2) {
442 /* -2 means timed out, retry autoboot
444 puts ("\nTimed out waiting for command\n");
445 # ifdef CONFIG_RESET_TO_RETRY
446 /* Reinit board to run initialization code again */
447 do_reset (NULL, 0, 0, NULL);
449 return; /* retry autoboot */
455 puts ("<INTERRUPT>\n");
457 rc = run_command (lastcommand, flag);
460 /* invalid command or not repeatable, forget it */
464 #endif /*CONFIG_SYS_HUSH_PARSER*/
467 #ifdef CONFIG_BOOT_RETRY_TIME
468 /***************************************************************************
469 * initialize command line timeout
471 void init_cmd_timeout(void)
473 char *s = getenv ("bootretry");
476 retry_time = (int)simple_strtol(s, NULL, 10);
478 retry_time = CONFIG_BOOT_RETRY_TIME;
480 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
481 retry_time = CONFIG_BOOT_RETRY_MIN;
484 /***************************************************************************
485 * reset command line timeout to retry_time seconds
487 void reset_cmd_timeout(void)
489 endtime = endtick(retry_time);
493 #ifdef CONFIG_CMDLINE_EDITING
496 * cmdline-editing related codes from vivi.
497 * Author: Janghoon Lyu <nandy@mizi.com>
500 #define putnstr(str,n) do { \
501 printf ("%.*s", (int)n, str); \
504 #define CTL_CH(c) ((c) - 'a' + 1)
505 #define CTL_BACKSPACE ('\b')
506 #define DEL ((char)255)
507 #define DEL7 ((char)127)
508 #define CREAD_HIST_CHAR ('!')
510 #define getcmd_putch(ch) putc(ch)
511 #define getcmd_getch() getc()
512 #define getcmd_cbeep() getcmd_putch('\a')
515 #define HIST_SIZE CONFIG_SYS_CBSIZE
517 static int hist_max = 0;
518 static int hist_add_idx = 0;
519 static int hist_cur = -1;
520 unsigned hist_num = 0;
522 char* hist_list[HIST_MAX];
523 char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
525 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
527 static void hist_init(void)
536 for (i = 0; i < HIST_MAX; i++) {
537 hist_list[i] = hist_lines[i];
538 hist_list[i][0] = '\0';
542 static void cread_add_to_hist(char *line)
544 strcpy(hist_list[hist_add_idx], line);
546 if (++hist_add_idx >= HIST_MAX)
549 if (hist_add_idx > hist_max)
550 hist_max = hist_add_idx;
555 static char* hist_prev(void)
567 if (hist_cur == hist_add_idx) {
571 ret = hist_list[hist_cur];
576 static char* hist_next(void)
583 if (hist_cur == hist_add_idx)
586 if (++hist_cur > hist_max)
589 if (hist_cur == hist_add_idx) {
592 ret = hist_list[hist_cur];
597 #ifndef CONFIG_CMDLINE_EDITING
598 static void cread_print_hist_list(void)
603 n = hist_num - hist_max;
605 i = hist_add_idx + 1;
609 if (i == hist_add_idx)
611 printf("%s\n", hist_list[i]);
616 #endif /* CONFIG_CMDLINE_EDITING */
618 #define BEGINNING_OF_LINE() { \
620 getcmd_putch(CTL_BACKSPACE); \
625 #define ERASE_TO_EOL() { \
626 if (num < eol_num) { \
627 printf("%*s", (int)(eol_num - num), ""); \
629 getcmd_putch(CTL_BACKSPACE); \
630 } while (--eol_num > num); \
634 #define REFRESH_TO_EOL() { \
635 if (num < eol_num) { \
636 wlen = eol_num - num; \
637 putnstr(buf + num, wlen); \
642 static void cread_add_char(char ichar, int insert, unsigned long *num,
643 unsigned long *eol_num, char *buf, unsigned long len)
648 if (insert || *num == *eol_num) {
649 if (*eol_num > len - 1) {
657 wlen = *eol_num - *num;
659 memmove(&buf[*num+1], &buf[*num], wlen-1);
663 putnstr(buf + *num, wlen);
666 getcmd_putch(CTL_BACKSPACE);
669 /* echo the character */
672 putnstr(buf + *num, wlen);
677 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
678 unsigned long *eol_num, char *buf, unsigned long len)
681 cread_add_char(*str, insert, num, eol_num, buf, len);
686 static int cread_line(const char *const prompt, char *buf, unsigned int *len)
688 unsigned long num = 0;
689 unsigned long eol_num = 0;
695 int init_len = strlen(buf);
698 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
701 #ifdef CONFIG_BOOT_RETRY_TIME
702 while (!tstc()) { /* while no incoming data */
703 if (retry_time >= 0 && get_ticks() > endtime)
704 return (-2); /* timed out */
709 ichar = getcmd_getch();
711 if ((ichar == '\n') || (ichar == '\r')) {
717 * handle standard linux xterm esc sequences for arrow key, etc.
722 esc_save[esc_len] = ichar;
725 cread_add_str(esc_save, esc_len, insert,
726 &num, &eol_num, buf, *len);
734 case 'D': /* <- key */
738 case 'C': /* -> key */
741 break; /* pass off to ^F handler */
742 case 'H': /* Home key */
745 break; /* pass off to ^A handler */
746 case 'A': /* up arrow */
749 break; /* pass off to ^P handler */
750 case 'B': /* down arrow */
753 break; /* pass off to ^N handler */
755 esc_save[esc_len++] = ichar;
756 cread_add_str(esc_save, esc_len, insert,
757 &num, &eol_num, buf, *len);
766 esc_save[esc_len] = ichar;
769 puts("impossible condition #876\n");
777 case CTL_CH('c'): /* ^C - break */
778 *buf = '\0'; /* discard input */
782 getcmd_putch(buf[num]);
788 getcmd_putch(CTL_BACKSPACE);
794 wlen = eol_num - num - 1;
796 memmove(&buf[num], &buf[num+1], wlen);
797 putnstr(buf + num, wlen);
802 getcmd_putch(CTL_BACKSPACE);
825 wlen = eol_num - num;
827 memmove(&buf[num], &buf[num+1], wlen);
828 getcmd_putch(CTL_BACKSPACE);
829 putnstr(buf + num, wlen);
832 getcmd_putch(CTL_BACKSPACE);
844 if (ichar == CTL_CH('p'))
854 /* nuke the current line */
858 /* erase to end of line */
861 /* copy new line into place and display */
863 eol_num = strlen(buf);
867 #ifdef CONFIG_AUTO_COMPLETE
871 /* do not autocomplete when in the middle */
878 col = strlen(prompt) + eol_num;
880 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
889 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
894 buf[eol_num] = '\0'; /* lose the newline */
896 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
897 cread_add_to_hist(buf);
898 hist_cur = hist_add_idx;
903 #endif /* CONFIG_CMDLINE_EDITING */
905 /****************************************************************************/
908 * Prompt for input and read a line.
909 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
910 * time out when time goes past endtime (timebase time in ticks).
911 * Return: number of read characters
915 int readline (const char *const prompt)
918 * If console_buffer isn't 0-length the user will be prompted to modify
919 * it instead of entering it from scratch as desired.
921 console_buffer[0] = '\0';
923 return readline_into_buffer(prompt, console_buffer);
927 int readline_into_buffer (const char *const prompt, char * buffer)
930 #ifdef CONFIG_CMDLINE_EDITING
931 unsigned int len = CONFIG_SYS_CBSIZE;
933 static int initted = 0;
936 * History uses a global array which is not
937 * writable until after relocation to RAM.
938 * Revert to non-history version if still
939 * running from flash.
941 if (gd->flags & GD_FLG_RELOC) {
950 rc = cread_line(prompt, p, &len);
951 return rc < 0 ? rc : len;
954 #endif /* CONFIG_CMDLINE_EDITING */
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 */
976 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
978 #ifdef CONFIG_SHOW_ACTIVITY
980 extern void show_activity(int arg);
988 * Special character handling
991 case '\r': /* Enter */
1000 case 0x03: /* ^C - break */
1001 p_buf[0] = '\0'; /* discard input */
1004 case 0x15: /* ^U - erase line */
1005 while (col > plen) {
1013 case 0x17: /* ^W - erase word */
1014 p=delete_char(p_buf, p, &col, &n, plen);
1015 while ((n > 0) && (*p != ' ')) {
1016 p=delete_char(p_buf, p, &col, &n, plen);
1020 case 0x08: /* ^H - backspace */
1021 case 0x7F: /* DEL - backspace */
1022 p=delete_char(p_buf, p, &col, &n, plen);
1027 * Must be a normal character then
1029 if (n < CONFIG_SYS_CBSIZE-2) {
1030 if (c == '\t') { /* expand TABs */
1031 #ifdef CONFIG_AUTO_COMPLETE
1032 /* if auto completion triggered just continue */
1034 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1035 p = p_buf + n; /* reset */
1039 puts (tab_seq+(col&07));
1040 col += 8 - (col&07);
1042 ++col; /* echo input */
1047 } else { /* Buffer full */
1052 #ifdef CONFIG_CMDLINE_EDITING
1057 /****************************************************************************/
1059 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1067 if (*(--p) == '\t') { /* will retype the whole line */
1068 while (*colp > plen) {
1072 for (s=buffer; s<p; ++s) {
1074 puts (tab_seq+((*colp) & 07));
1075 *colp += 8 - ((*colp) & 07);
1089 /****************************************************************************/
1091 int parse_line (char *line, char *argv[])
1096 printf ("parse_line: \"%s\"\n", line);
1098 while (nargs < CONFIG_SYS_MAXARGS) {
1100 /* skip any white space */
1101 while (isblank(*line))
1104 if (*line == '\0') { /* end of line, no more args */
1107 printf ("parse_line: nargs=%d\n", nargs);
1112 argv[nargs++] = line; /* begin of argument string */
1114 /* find end of string */
1115 while (*line && !isblank(*line))
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", CONFIG_SYS_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 = CONFIG_SYS_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[CONFIG_SYS_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 CONFIG_SYS_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[CONFIG_SYS_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[CONFIG_SYS_CBSIZE];
1274 char *argv[CONFIG_SYS_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) >= CONFIG_SYS_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) {
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 * const argv[])
1399 return cmd_usage(cmdtp);
1401 for (i=1; i<argc; ++i) {
1404 if ((arg = getenv (argv[i])) == NULL) {
1405 printf ("## Error: \"%s\" not defined\n", argv[i]);
1409 if (run_command2(arg, flag) != 0)