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[]);
59 #if defined(CONFIG_UPDATE_TFTP)
60 void update_tftp (void);
61 #endif /* CONFIG_UPDATE_TFTP */
63 #define MAX_DELAY_STOP_STR 32
65 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
66 static int abortboot(int);
71 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
73 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
74 static char erase_seq[] = "\b \b"; /* erase sequence */
75 static char tab_seq[] = " "; /* used to expand TABs */
77 #ifdef CONFIG_BOOT_RETRY_TIME
78 static uint64_t endtime = 0; /* must be set, default is instant timeout */
79 static int retry_time = -1; /* -1 so can call readline before main_loop */
82 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
84 #ifndef CONFIG_BOOT_RETRY_MIN
85 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
88 #ifdef CONFIG_MODEM_SUPPORT
90 extern void mdm_init(void); /* defined in board.c */
93 /***************************************************************************
94 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
95 * returns: 0 - no key string, allow autoboot
96 * 1 - got key string, abort
98 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
99 # if defined(CONFIG_AUTOBOOT_KEYED)
100 static __inline__ int abortboot(int bootdelay)
103 uint64_t etime = endtick(bootdelay);
110 { str: getenv ("bootdelaykey"), retry: 1 },
111 { str: getenv ("bootdelaykey2"), retry: 1 },
112 { str: getenv ("bootstopkey"), retry: 0 },
113 { str: getenv ("bootstopkey2"), retry: 0 },
116 char presskey [MAX_DELAY_STOP_STR];
117 u_int presskey_len = 0;
118 u_int presskey_max = 0;
121 # ifdef CONFIG_AUTOBOOT_PROMPT
122 printf(CONFIG_AUTOBOOT_PROMPT);
125 # ifdef CONFIG_AUTOBOOT_DELAY_STR
126 if (delaykey[0].str == NULL)
127 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
129 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
130 if (delaykey[1].str == NULL)
131 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
133 # ifdef CONFIG_AUTOBOOT_STOP_STR
134 if (delaykey[2].str == NULL)
135 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
137 # ifdef CONFIG_AUTOBOOT_STOP_STR2
138 if (delaykey[3].str == NULL)
139 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
142 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
143 delaykey[i].len = delaykey[i].str == NULL ?
144 0 : strlen (delaykey[i].str);
145 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
146 MAX_DELAY_STOP_STR : delaykey[i].len;
148 presskey_max = presskey_max > delaykey[i].len ?
149 presskey_max : delaykey[i].len;
152 printf("%s key:<%s>\n",
153 delaykey[i].retry ? "delay" : "stop",
154 delaykey[i].str ? delaykey[i].str : "NULL");
158 /* In order to keep up with incoming data, check timeout only
161 while (!abort && get_ticks() <= etime) {
162 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
163 if (delaykey[i].len > 0 &&
164 presskey_len >= delaykey[i].len &&
165 memcmp (presskey + presskey_len - delaykey[i].len,
167 delaykey[i].len) == 0) {
169 printf("got %skey\n",
170 delaykey[i].retry ? "delay" : "stop");
173 # ifdef CONFIG_BOOT_RETRY_TIME
174 /* don't retry auto boot */
175 if (! delaykey[i].retry)
183 if (presskey_len < presskey_max) {
184 presskey [presskey_len ++] = getc();
187 for (i = 0; i < presskey_max - 1; i ++)
188 presskey [i] = presskey [i + 1];
190 presskey [i] = getc();
196 puts("key timeout\n");
199 #ifdef CONFIG_SILENT_CONSOLE
201 gd->flags &= ~GD_FLG_SILENT;
207 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
209 #ifdef CONFIG_MENUKEY
210 static int menukey = 0;
213 static __inline__ int abortboot(int bootdelay)
217 #ifdef CONFIG_MENUPROMPT
218 printf(CONFIG_MENUPROMPT);
220 printf("Hit any key to stop autoboot: %2d ", bootdelay);
223 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
228 if (bootdelay >= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
232 abort = 1; /* don't auto boot */
237 while ((bootdelay > 0) && (!abort)) {
241 /* delay 100 * 10ms */
242 for (i=0; !abort && i<100; ++i) {
243 if (tstc()) { /* we got a key press */
244 abort = 1; /* don't auto boot */
245 bootdelay = 0; /* no more delay */
246 # ifdef CONFIG_MENUKEY
249 (void) getc(); /* consume input */
256 printf("\b\b\b%2d ", bootdelay);
261 #ifdef CONFIG_SILENT_CONSOLE
263 gd->flags &= ~GD_FLG_SILENT;
268 # endif /* CONFIG_AUTOBOOT_KEYED */
269 #endif /* CONFIG_BOOTDELAY >= 0 */
271 /****************************************************************************/
273 void main_loop (void)
275 #ifndef CFG_HUSH_PARSER
276 static char lastcommand[CFG_CBSIZE] = { 0, };
282 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
286 #ifdef CONFIG_PREBOOT
289 #ifdef CONFIG_BOOTCOUNT_LIMIT
290 unsigned long bootcount = 0;
291 unsigned long bootlimit = 0;
294 #endif /* CONFIG_BOOTCOUNT_LIMIT */
296 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
297 ulong bmp = 0; /* default bitmap */
298 extern int trab_vfd (ulong bitmap);
300 #ifdef CONFIG_MODEM_SUPPORT
302 bmp = 1; /* alternate bitmap */
305 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
307 #if defined(CONFIG_UPDATE_TFTP)
309 #endif /* CONFIG_UPDATE_TFTP */
311 #ifdef CONFIG_BOOTCOUNT_LIMIT
312 bootcount = bootcount_load();
314 bootcount_store (bootcount);
315 sprintf (bcs_set, "%lu", bootcount);
316 setenv ("bootcount", bcs_set);
317 bcs = getenv ("bootlimit");
318 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
319 #endif /* CONFIG_BOOTCOUNT_LIMIT */
321 #ifdef CONFIG_MODEM_SUPPORT
322 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
324 char *str = strdup(getenv("mdm_cmd"));
325 setenv ("preboot", str); /* set or delete definition */
328 mdm_init(); /* wait for modem connection */
330 #endif /* CONFIG_MODEM_SUPPORT */
332 #ifdef CONFIG_VERSION_VARIABLE
334 extern char version_string[];
336 setenv ("ver", version_string); /* set version variable */
338 #endif /* CONFIG_VERSION_VARIABLE */
340 #ifdef CFG_HUSH_PARSER
341 u_boot_hush_start ();
344 #ifdef CONFIG_AUTO_COMPLETE
345 install_auto_complete();
348 #ifdef CONFIG_PREBOOT
349 if ((p = getenv ("preboot")) != NULL) {
350 # ifdef CONFIG_AUTOBOOT_KEYED
351 int prev = disable_ctrlc(1); /* disable Control C checking */
354 # ifndef CFG_HUSH_PARSER
357 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
358 FLAG_EXIT_FROM_LOOP);
361 # ifdef CONFIG_AUTOBOOT_KEYED
362 disable_ctrlc(prev); /* restore Control C checking */
365 #endif /* CONFIG_PREBOOT */
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 */
400 # ifndef CFG_HUSH_PARSER
403 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
404 FLAG_EXIT_FROM_LOOP);
407 # ifdef CONFIG_AUTOBOOT_KEYED
408 disable_ctrlc(prev); /* restore Control C checking */
412 # ifdef CONFIG_MENUKEY
413 if (menukey == CONFIG_MENUKEY) {
414 s = getenv("menucmd");
416 # ifndef CFG_HUSH_PARSER
419 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
420 FLAG_EXIT_FROM_LOOP);
424 #endif /* CONFIG_MENUKEY */
425 #endif /* CONFIG_BOOTDELAY */
427 #ifdef CONFIG_AMIGAONEG3SE
429 extern void video_banner(void);
435 * Main Loop for Monitor Command Processing
437 #ifdef CFG_HUSH_PARSER
439 /* This point is never reached */
443 #ifdef CONFIG_BOOT_RETRY_TIME
445 /* Saw enough of a valid command to
446 * restart the timeout.
451 len = readline (CFG_PROMPT);
453 flag = 0; /* assume no special flags for now */
455 strcpy (lastcommand, console_buffer);
457 flag |= CMD_FLAG_REPEAT;
458 #ifdef CONFIG_BOOT_RETRY_TIME
459 else if (len == -2) {
460 /* -2 means timed out, retry autoboot
462 puts ("\nTimed out waiting for command\n");
463 # ifdef CONFIG_RESET_TO_RETRY
464 /* Reinit board to run initialization code again */
465 do_reset (NULL, 0, 0, NULL);
467 return; /* retry autoboot */
473 puts ("<INTERRUPT>\n");
475 rc = run_command (lastcommand, flag);
478 /* invalid command or not repeatable, forget it */
482 #endif /*CFG_HUSH_PARSER*/
485 #ifdef CONFIG_BOOT_RETRY_TIME
486 /***************************************************************************
487 * initialize command line timeout
489 void init_cmd_timeout(void)
491 char *s = getenv ("bootretry");
494 retry_time = (int)simple_strtol(s, NULL, 10);
496 retry_time = CONFIG_BOOT_RETRY_TIME;
498 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
499 retry_time = CONFIG_BOOT_RETRY_MIN;
502 /***************************************************************************
503 * reset command line timeout to retry_time seconds
505 void reset_cmd_timeout(void)
507 endtime = endtick(retry_time);
511 #ifdef CONFIG_CMDLINE_EDITING
514 * cmdline-editing related codes from vivi.
515 * Author: Janghoon Lyu <nandy@mizi.com>
518 #define putnstr(str,n) do { \
519 printf ("%.*s", (int)n, str); \
522 #define CTL_CH(c) ((c) - 'a' + 1)
524 #define MAX_CMDBUF_SIZE 256
526 #define CTL_BACKSPACE ('\b')
527 #define DEL ((char)255)
528 #define DEL7 ((char)127)
529 #define CREAD_HIST_CHAR ('!')
531 #define getcmd_putch(ch) putc(ch)
532 #define getcmd_getch() getc()
533 #define getcmd_cbeep() getcmd_putch('\a')
536 #define HIST_SIZE MAX_CMDBUF_SIZE
538 static int hist_max = 0;
539 static int hist_add_idx = 0;
540 static int hist_cur = -1;
541 unsigned hist_num = 0;
543 char* hist_list[HIST_MAX];
544 char hist_lines[HIST_MAX][HIST_SIZE];
546 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
548 static void hist_init(void)
557 for (i = 0; i < HIST_MAX; i++) {
558 hist_list[i] = hist_lines[i];
559 hist_list[i][0] = '\0';
563 static void cread_add_to_hist(char *line)
565 strcpy(hist_list[hist_add_idx], line);
567 if (++hist_add_idx >= HIST_MAX)
570 if (hist_add_idx > hist_max)
571 hist_max = hist_add_idx;
576 static char* hist_prev(void)
588 if (hist_cur == hist_add_idx) {
592 ret = hist_list[hist_cur];
597 static char* hist_next(void)
604 if (hist_cur == hist_add_idx)
607 if (++hist_cur > hist_max)
610 if (hist_cur == hist_add_idx) {
613 ret = hist_list[hist_cur];
618 #ifndef CONFIG_CMDLINE_EDITING
619 static void cread_print_hist_list(void)
624 n = hist_num - hist_max;
626 i = hist_add_idx + 1;
630 if (i == hist_add_idx)
632 printf("%s\n", hist_list[i]);
637 #endif /* CONFIG_CMDLINE_EDITING */
639 #define BEGINNING_OF_LINE() { \
641 getcmd_putch(CTL_BACKSPACE); \
646 #define ERASE_TO_EOL() { \
647 if (num < eol_num) { \
649 for (tmp = num; tmp < eol_num; tmp++) \
651 while (tmp-- > num) \
652 getcmd_putch(CTL_BACKSPACE); \
657 #define REFRESH_TO_EOL() { \
658 if (num < eol_num) { \
659 wlen = eol_num - num; \
660 putnstr(buf + num, wlen); \
665 static void cread_add_char(char ichar, int insert, unsigned long *num,
666 unsigned long *eol_num, char *buf, unsigned long len)
671 if (insert || *num == *eol_num) {
672 if (*eol_num > len - 1) {
680 wlen = *eol_num - *num;
682 memmove(&buf[*num+1], &buf[*num], wlen-1);
686 putnstr(buf + *num, wlen);
689 getcmd_putch(CTL_BACKSPACE);
692 /* echo the character */
695 putnstr(buf + *num, wlen);
700 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
701 unsigned long *eol_num, char *buf, unsigned long len)
704 cread_add_char(*str, insert, num, eol_num, buf, len);
709 static int cread_line(const char *const prompt, char *buf, unsigned int *len)
711 unsigned long num = 0;
712 unsigned long eol_num = 0;
723 #ifdef CONFIG_BOOT_RETRY_TIME
724 while (!tstc()) { /* while no incoming data */
725 if (retry_time >= 0 && get_ticks() > endtime)
726 return (-2); /* timed out */
730 ichar = getcmd_getch();
732 if ((ichar == '\n') || (ichar == '\r')) {
738 * handle standard linux xterm esc sequences for arrow key, etc.
743 esc_save[esc_len] = ichar;
746 cread_add_str(esc_save, esc_len, insert,
747 &num, &eol_num, buf, *len);
755 case 'D': /* <- key */
759 case 'C': /* -> key */
762 break; /* pass off to ^F handler */
763 case 'H': /* Home key */
766 break; /* pass off to ^A handler */
767 case 'A': /* up arrow */
770 break; /* pass off to ^P handler */
771 case 'B': /* down arrow */
774 break; /* pass off to ^N handler */
776 esc_save[esc_len++] = ichar;
777 cread_add_str(esc_save, esc_len, insert,
778 &num, &eol_num, buf, *len);
787 esc_save[esc_len] = ichar;
790 puts("impossible condition #876\n");
798 case CTL_CH('c'): /* ^C - break */
799 *buf = '\0'; /* discard input */
803 getcmd_putch(buf[num]);
809 getcmd_putch(CTL_BACKSPACE);
815 wlen = eol_num - num - 1;
817 memmove(&buf[num], &buf[num+1], wlen);
818 putnstr(buf + num, wlen);
823 getcmd_putch(CTL_BACKSPACE);
846 wlen = eol_num - num;
848 memmove(&buf[num], &buf[num+1], wlen);
849 getcmd_putch(CTL_BACKSPACE);
850 putnstr(buf + num, wlen);
853 getcmd_putch(CTL_BACKSPACE);
865 if (ichar == CTL_CH('p'))
875 /* nuke the current line */
879 /* erase to end of line */
882 /* copy new line into place and display */
884 eol_num = strlen(buf);
888 #ifdef CONFIG_AUTO_COMPLETE
892 /* do not autocomplete when in the middle */
899 col = strlen(prompt) + eol_num;
901 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
910 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
915 buf[eol_num] = '\0'; /* lose the newline */
917 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
918 cread_add_to_hist(buf);
919 hist_cur = hist_add_idx;
924 #endif /* CONFIG_CMDLINE_EDITING */
926 /****************************************************************************/
929 * Prompt for input and read a line.
930 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
931 * time out when time goes past endtime (timebase time in ticks).
932 * Return: number of read characters
936 int readline (const char *const prompt)
938 return readline_into_buffer(prompt, console_buffer);
942 int readline_into_buffer (const char *const prompt, char * buffer)
945 #ifdef CONFIG_CMDLINE_EDITING
946 unsigned int len=MAX_CMDBUF_SIZE;
948 static int initted = 0;
951 * History uses a global array which is not
952 * writable until after relocation to RAM.
953 * Revert to non-history version if still
954 * running from flash.
956 if (gd->flags & GD_FLG_RELOC) {
964 rc = cread_line(prompt, p, &len);
965 return rc < 0 ? rc : len;
968 #endif /* CONFIG_CMDLINE_EDITING */
970 int n = 0; /* buffer index */
971 int plen = 0; /* prompt length */
972 int col; /* output column cnt */
977 plen = strlen (prompt);
983 #ifdef CONFIG_BOOT_RETRY_TIME
984 while (!tstc()) { /* while no incoming data */
985 if (retry_time >= 0 && get_ticks() > endtime)
986 return (-2); /* timed out */
989 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
991 #ifdef CONFIG_SHOW_ACTIVITY
993 extern void show_activity(int arg);
1000 * Special character handling
1003 case '\r': /* Enter */
1009 case '\0': /* nul */
1012 case 0x03: /* ^C - break */
1013 p_buf[0] = '\0'; /* discard input */
1016 case 0x15: /* ^U - erase line */
1017 while (col > plen) {
1025 case 0x17: /* ^W - erase word */
1026 p=delete_char(p_buf, p, &col, &n, plen);
1027 while ((n > 0) && (*p != ' ')) {
1028 p=delete_char(p_buf, p, &col, &n, plen);
1032 case 0x08: /* ^H - backspace */
1033 case 0x7F: /* DEL - backspace */
1034 p=delete_char(p_buf, p, &col, &n, plen);
1039 * Must be a normal character then
1041 if (n < CFG_CBSIZE-2) {
1042 if (c == '\t') { /* expand TABs */
1043 #ifdef CONFIG_AUTO_COMPLETE
1044 /* if auto completion triggered just continue */
1046 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1047 p = p_buf + n; /* reset */
1051 puts (tab_seq+(col&07));
1052 col += 8 - (col&07);
1054 ++col; /* echo input */
1059 } else { /* Buffer full */
1064 #ifdef CONFIG_CMDLINE_EDITING
1069 /****************************************************************************/
1071 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1079 if (*(--p) == '\t') { /* will retype the whole line */
1080 while (*colp > plen) {
1084 for (s=buffer; s<p; ++s) {
1086 puts (tab_seq+((*colp) & 07));
1087 *colp += 8 - ((*colp) & 07);
1101 /****************************************************************************/
1103 int parse_line (char *line, char *argv[])
1108 printf ("parse_line: \"%s\"\n", line);
1110 while (nargs < CFG_MAXARGS) {
1112 /* skip any white space */
1113 while ((*line == ' ') || (*line == '\t')) {
1117 if (*line == '\0') { /* end of line, no more args */
1120 printf ("parse_line: nargs=%d\n", nargs);
1125 argv[nargs++] = line; /* begin of argument string */
1127 /* find end of string */
1128 while (*line && (*line != ' ') && (*line != '\t')) {
1132 if (*line == '\0') { /* end of line, no more args */
1135 printf ("parse_line: nargs=%d\n", nargs);
1140 *line++ = '\0'; /* terminate current arg */
1143 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1146 printf ("parse_line: nargs=%d\n", nargs);
1151 /****************************************************************************/
1153 static void process_macros (const char *input, char *output)
1156 const char *varname_start = NULL;
1157 int inputcnt = strlen (input);
1158 int outputcnt = CFG_CBSIZE;
1159 int state = 0; /* 0 = waiting for '$' */
1161 /* 1 = waiting for '(' or '{' */
1162 /* 2 = waiting for ')' or '}' */
1163 /* 3 = waiting for ''' */
1165 char *output_start = output;
1167 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1171 prev = '\0'; /* previous character */
1173 while (inputcnt && outputcnt) {
1178 /* remove one level of escape characters */
1179 if ((c == '\\') && (prev != '\\')) {
1180 if (inputcnt-- == 0)
1188 case 0: /* Waiting for (unescaped) $ */
1189 if ((c == '\'') && (prev != '\\')) {
1193 if ((c == '$') && (prev != '\\')) {
1200 case 1: /* Waiting for ( */
1201 if (c == '(' || c == '{') {
1203 varname_start = input;
1215 case 2: /* Waiting for ) */
1216 if (c == ')' || c == '}') {
1218 char envname[CFG_CBSIZE], *envval;
1219 int envcnt = input - varname_start - 1; /* Varname # of chars */
1221 /* Get the varname */
1222 for (i = 0; i < envcnt; i++) {
1223 envname[i] = varname_start[i];
1228 envval = getenv (envname);
1230 /* Copy into the line if it exists */
1232 while ((*envval) && outputcnt) {
1233 *(output++) = *(envval++);
1236 /* Look for another '$' */
1240 case 3: /* Waiting for ' */
1241 if ((c == '\'') && (prev != '\\')) {
1258 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1259 strlen (output_start), output_start);
1263 /****************************************************************************
1265 * 1 - command executed, repeatable
1266 * 0 - command executed but not repeatable, interrupted commands are
1267 * always considered not repeatable
1268 * -1 - not executed (unrecognized, bootd recursion or too many args)
1269 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1270 * considered unrecognized)
1274 * We must create a temporary copy of the command since the command we get
1275 * may be the result from getenv(), which returns a pointer directly to
1276 * the environment data, which may change magicly when the command we run
1277 * creates or modifies environment variables (like "bootp" does).
1280 int run_command (const char *cmd, int flag)
1283 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1284 char *token; /* start of token in cmdbuf */
1285 char *sep; /* end of token (separator) in cmdbuf */
1286 char finaltoken[CFG_CBSIZE];
1288 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1294 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1295 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1299 clear_ctrlc(); /* forget any previous Control C */
1301 if (!cmd || !*cmd) {
1302 return -1; /* empty command */
1305 if (strlen(cmd) >= CFG_CBSIZE) {
1306 puts ("## Command too long!\n");
1310 strcpy (cmdbuf, cmd);
1312 /* Process separators and check for invalid
1313 * repeatable commands
1317 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1322 * Find separator, or string end
1323 * Allow simple escape of ';' by writing "\;"
1325 for (inquotes = 0, sep = str; *sep; sep++) {
1331 (*sep == ';') && /* separator */
1332 ( sep != str) && /* past string start */
1333 (*(sep-1) != '\\')) /* and NOT escaped */
1338 * Limit the token to data between separators
1342 str = sep + 1; /* start of command for next pass */
1346 str = sep; /* no more commands for next pass */
1348 printf ("token: \"%s\"\n", token);
1351 /* find macros in this token and replace them */
1352 process_macros (token, finaltoken);
1354 /* Extract arguments */
1355 if ((argc = parse_line (finaltoken, argv)) == 0) {
1356 rc = -1; /* no command at all */
1360 /* Look up command in command table */
1361 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1362 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1363 rc = -1; /* give up after bad command */
1367 /* found - check max args */
1368 if (argc > cmdtp->maxargs) {
1369 printf ("Usage:\n%s\n", cmdtp->usage);
1374 #if defined(CONFIG_CMD_BOOTD)
1375 /* avoid "bootd" recursion */
1376 if (cmdtp->cmd == do_bootd) {
1378 printf ("[%s]\n", finaltoken);
1380 if (flag & CMD_FLAG_BOOTD) {
1381 puts ("'bootd' recursion detected\n");
1385 flag |= CMD_FLAG_BOOTD;
1390 /* OK - call function to do the command */
1391 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1395 repeatable &= cmdtp->repeatable;
1397 /* Did the user stop this? */
1399 return -1; /* if stopped then not repeatable */
1402 return rc ? rc : repeatable;
1405 /****************************************************************************/
1407 #if defined(CONFIG_CMD_RUN)
1408 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1413 printf ("Usage:\n%s\n", cmdtp->usage);
1417 for (i=1; i<argc; ++i) {
1420 if ((arg = getenv (argv[i])) == NULL) {
1421 printf ("## Error: \"%s\" not defined\n", argv[i]);
1424 #ifndef CFG_HUSH_PARSER
1425 if (run_command (arg, flag) == -1)
1428 if (parse_string_outer(arg,
1429 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)