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,
35 #ifdef CONFIG_MODEM_SUPPORT
36 #include <malloc.h> /* for free() prototype */
39 #ifdef CONFIG_SYS_HUSH_PARSER
44 #include <linux/ctype.h>
47 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
48 DECLARE_GLOBAL_DATA_PTR;
52 * Board-specific Platform code can reimplement show_boot_progress () if needed
54 void inline __show_boot_progress (int val) {}
55 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
57 #if defined(CONFIG_UPDATE_TFTP)
58 int update_tftp (ulong addr);
59 #endif /* CONFIG_UPDATE_TFTP */
61 #define MAX_DELAY_STOP_STR 32
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 1 - got key string, abort
91 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
92 # if defined(CONFIG_AUTOBOOT_KEYED)
96 int abortboot(int bootdelay)
99 uint64_t etime = endtick(bootdelay);
106 { str: getenv ("bootdelaykey"), retry: 1 },
107 { str: getenv ("bootdelaykey2"), retry: 1 },
108 { str: getenv ("bootstopkey"), retry: 0 },
109 { str: getenv ("bootstopkey2"), retry: 0 },
112 char presskey [MAX_DELAY_STOP_STR];
113 u_int presskey_len = 0;
114 u_int presskey_max = 0;
117 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
122 # ifdef CONFIG_AUTOBOOT_PROMPT
123 printf(CONFIG_AUTOBOOT_PROMPT);
126 # ifdef CONFIG_AUTOBOOT_DELAY_STR
127 if (delaykey[0].str == NULL)
128 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
130 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
131 if (delaykey[1].str == NULL)
132 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
134 # ifdef CONFIG_AUTOBOOT_STOP_STR
135 if (delaykey[2].str == NULL)
136 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
138 # ifdef CONFIG_AUTOBOOT_STOP_STR2
139 if (delaykey[3].str == NULL)
140 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
143 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
144 delaykey[i].len = delaykey[i].str == NULL ?
145 0 : strlen (delaykey[i].str);
146 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
147 MAX_DELAY_STOP_STR : delaykey[i].len;
149 presskey_max = presskey_max > delaykey[i].len ?
150 presskey_max : delaykey[i].len;
153 printf("%s key:<%s>\n",
154 delaykey[i].retry ? "delay" : "stop",
155 delaykey[i].str ? delaykey[i].str : "NULL");
159 /* In order to keep up with incoming data, check timeout only
164 if (presskey_len < presskey_max) {
165 presskey [presskey_len ++] = getc();
168 for (i = 0; i < presskey_max - 1; i ++)
169 presskey [i] = presskey [i + 1];
171 presskey [i] = getc();
175 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
176 if (delaykey[i].len > 0 &&
177 presskey_len >= delaykey[i].len &&
178 memcmp (presskey + presskey_len - delaykey[i].len,
180 delaykey[i].len) == 0) {
182 printf("got %skey\n",
183 delaykey[i].retry ? "delay" : "stop");
186 # ifdef CONFIG_BOOT_RETRY_TIME
187 /* don't retry auto boot */
188 if (! delaykey[i].retry)
194 } while (!abort && get_ticks() <= etime);
198 puts("key timeout\n");
201 #ifdef CONFIG_SILENT_CONSOLE
203 gd->flags &= ~GD_FLG_SILENT;
209 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
211 #ifdef CONFIG_MENUKEY
212 static int menukey = 0;
218 int abortboot(int bootdelay)
222 #ifdef CONFIG_MENUPROMPT
223 printf(CONFIG_MENUPROMPT);
226 printf("Hit any key to stop autoboot: %2d ", bootdelay);
229 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
231 * Check if key already pressed
232 * Don't check if bootdelay < 0
234 if (bootdelay >= 0) {
235 if (tstc()) { /* we got a key press */
236 (void) getc(); /* consume input */
238 abort = 1; /* don't auto boot */
243 while ((bootdelay > 0) && (!abort)) {
247 /* delay 100 * 10ms */
248 for (i=0; !abort && i<100; ++i) {
249 if (tstc()) { /* we got a key press */
250 abort = 1; /* don't auto boot */
251 bootdelay = 0; /* no more delay */
252 # ifdef CONFIG_MENUKEY
255 (void) getc(); /* consume input */
262 printf("\b\b\b%2d ", bootdelay);
267 #ifdef CONFIG_SILENT_CONSOLE
269 gd->flags &= ~GD_FLG_SILENT;
274 # endif /* CONFIG_AUTOBOOT_KEYED */
275 #endif /* CONFIG_BOOTDELAY >= 0 */
277 /****************************************************************************/
279 void main_loop (void)
281 #ifndef CONFIG_SYS_HUSH_PARSER
282 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
288 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
292 #ifdef CONFIG_PREBOOT
295 #ifdef CONFIG_BOOTCOUNT_LIMIT
296 unsigned long bootcount = 0;
297 unsigned long bootlimit = 0;
300 #endif /* CONFIG_BOOTCOUNT_LIMIT */
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 setenv ("ver", version_string); /* set version variable */
327 #endif /* CONFIG_VERSION_VARIABLE */
329 #ifdef CONFIG_SYS_HUSH_PARSER
330 u_boot_hush_start ();
333 #if defined(CONFIG_HUSH_INIT_VAR)
337 #ifdef CONFIG_PREBOOT
338 if ((p = getenv ("preboot")) != NULL) {
339 # ifdef CONFIG_AUTOBOOT_KEYED
340 int prev = disable_ctrlc(1); /* disable Control C checking */
343 run_command_list(p, -1, 0);
345 # ifdef CONFIG_AUTOBOOT_KEYED
346 disable_ctrlc(prev); /* restore Control C checking */
349 #endif /* CONFIG_PREBOOT */
351 #if defined(CONFIG_UPDATE_TFTP)
353 #endif /* CONFIG_UPDATE_TFTP */
355 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
356 s = getenv ("bootdelay");
357 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
359 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
361 #if defined(CONFIG_MENU_SHOW)
362 bootdelay = menu_show(bootdelay);
364 # ifdef CONFIG_BOOT_RETRY_TIME
366 # endif /* CONFIG_BOOT_RETRY_TIME */
369 if (gd->flags & GD_FLG_POSTFAIL) {
370 s = getenv("failbootcmd");
373 #endif /* CONFIG_POST */
374 #ifdef CONFIG_BOOTCOUNT_LIMIT
375 if (bootlimit && (bootcount > bootlimit)) {
376 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
377 (unsigned)bootlimit);
378 s = getenv ("altbootcmd");
381 #endif /* CONFIG_BOOTCOUNT_LIMIT */
382 s = getenv ("bootcmd");
384 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
386 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
387 # ifdef CONFIG_AUTOBOOT_KEYED
388 int prev = disable_ctrlc(1); /* disable Control C checking */
391 run_command_list(s, -1, 0);
393 # ifdef CONFIG_AUTOBOOT_KEYED
394 disable_ctrlc(prev); /* restore Control C checking */
398 # ifdef CONFIG_MENUKEY
399 if (menukey == CONFIG_MENUKEY) {
400 s = getenv("menucmd");
402 run_command_list(s, -1, 0);
404 #endif /* CONFIG_MENUKEY */
405 #endif /* CONFIG_BOOTDELAY */
408 * Main Loop for Monitor Command Processing
410 #ifdef CONFIG_SYS_HUSH_PARSER
412 /* This point is never reached */
416 #ifdef CONFIG_BOOT_RETRY_TIME
418 /* Saw enough of a valid command to
419 * restart the timeout.
424 len = readline (CONFIG_SYS_PROMPT);
426 flag = 0; /* assume no special flags for now */
428 strcpy (lastcommand, console_buffer);
430 flag |= CMD_FLAG_REPEAT;
431 #ifdef CONFIG_BOOT_RETRY_TIME
432 else if (len == -2) {
433 /* -2 means timed out, retry autoboot
435 puts ("\nTimed out waiting for command\n");
436 # ifdef CONFIG_RESET_TO_RETRY
437 /* Reinit board to run initialization code again */
438 do_reset (NULL, 0, 0, NULL);
440 return; /* retry autoboot */
446 puts ("<INTERRUPT>\n");
448 rc = run_command(lastcommand, flag);
451 /* invalid command or not repeatable, forget it */
455 #endif /*CONFIG_SYS_HUSH_PARSER*/
458 #ifdef CONFIG_BOOT_RETRY_TIME
459 /***************************************************************************
460 * initialize command line timeout
462 void init_cmd_timeout(void)
464 char *s = getenv ("bootretry");
467 retry_time = (int)simple_strtol(s, NULL, 10);
469 retry_time = CONFIG_BOOT_RETRY_TIME;
471 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
472 retry_time = CONFIG_BOOT_RETRY_MIN;
475 /***************************************************************************
476 * reset command line timeout to retry_time seconds
478 void reset_cmd_timeout(void)
480 endtime = endtick(retry_time);
484 #ifdef CONFIG_CMDLINE_EDITING
487 * cmdline-editing related codes from vivi.
488 * Author: Janghoon Lyu <nandy@mizi.com>
491 #define putnstr(str,n) do { \
492 printf ("%.*s", (int)n, str); \
495 #define CTL_CH(c) ((c) - 'a' + 1)
496 #define CTL_BACKSPACE ('\b')
497 #define DEL ((char)255)
498 #define DEL7 ((char)127)
499 #define CREAD_HIST_CHAR ('!')
501 #define getcmd_putch(ch) putc(ch)
502 #define getcmd_getch() getc()
503 #define getcmd_cbeep() getcmd_putch('\a')
506 #define HIST_SIZE CONFIG_SYS_CBSIZE
508 static int hist_max = 0;
509 static int hist_add_idx = 0;
510 static int hist_cur = -1;
511 unsigned hist_num = 0;
513 char* hist_list[HIST_MAX];
514 char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
516 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
518 static void hist_init(void)
527 for (i = 0; i < HIST_MAX; i++) {
528 hist_list[i] = hist_lines[i];
529 hist_list[i][0] = '\0';
533 static void cread_add_to_hist(char *line)
535 strcpy(hist_list[hist_add_idx], line);
537 if (++hist_add_idx >= HIST_MAX)
540 if (hist_add_idx > hist_max)
541 hist_max = hist_add_idx;
546 static char* hist_prev(void)
558 if (hist_cur == hist_add_idx) {
562 ret = hist_list[hist_cur];
567 static char* hist_next(void)
574 if (hist_cur == hist_add_idx)
577 if (++hist_cur > hist_max)
580 if (hist_cur == hist_add_idx) {
583 ret = hist_list[hist_cur];
588 #ifndef CONFIG_CMDLINE_EDITING
589 static void cread_print_hist_list(void)
594 n = hist_num - hist_max;
596 i = hist_add_idx + 1;
600 if (i == hist_add_idx)
602 printf("%s\n", hist_list[i]);
607 #endif /* CONFIG_CMDLINE_EDITING */
609 #define BEGINNING_OF_LINE() { \
611 getcmd_putch(CTL_BACKSPACE); \
616 #define ERASE_TO_EOL() { \
617 if (num < eol_num) { \
618 printf("%*s", (int)(eol_num - num), ""); \
620 getcmd_putch(CTL_BACKSPACE); \
621 } while (--eol_num > num); \
625 #define REFRESH_TO_EOL() { \
626 if (num < eol_num) { \
627 wlen = eol_num - num; \
628 putnstr(buf + num, wlen); \
633 static void cread_add_char(char ichar, int insert, unsigned long *num,
634 unsigned long *eol_num, char *buf, unsigned long len)
639 if (insert || *num == *eol_num) {
640 if (*eol_num > len - 1) {
648 wlen = *eol_num - *num;
650 memmove(&buf[*num+1], &buf[*num], wlen-1);
654 putnstr(buf + *num, wlen);
657 getcmd_putch(CTL_BACKSPACE);
660 /* echo the character */
663 putnstr(buf + *num, wlen);
668 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
669 unsigned long *eol_num, char *buf, unsigned long len)
672 cread_add_char(*str, insert, num, eol_num, buf, len);
677 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
680 unsigned long num = 0;
681 unsigned long eol_num = 0;
687 int init_len = strlen(buf);
691 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
694 #ifdef CONFIG_BOOT_RETRY_TIME
695 while (!tstc()) { /* while no incoming data */
696 if (retry_time >= 0 && get_ticks() > endtime)
697 return (-2); /* timed out */
701 if (first && timeout) {
702 uint64_t etime = endtick(timeout);
704 while (!tstc()) { /* while no incoming data */
705 if (get_ticks() >= etime)
706 return -2; /* timed out */
712 ichar = getcmd_getch();
714 if ((ichar == '\n') || (ichar == '\r')) {
720 * handle standard linux xterm esc sequences for arrow key, etc.
725 esc_save[esc_len] = ichar;
728 cread_add_str(esc_save, esc_len, insert,
729 &num, &eol_num, buf, *len);
737 case 'D': /* <- key */
741 case 'C': /* -> key */
744 break; /* pass off to ^F handler */
745 case 'H': /* Home key */
748 break; /* pass off to ^A handler */
749 case 'A': /* up arrow */
752 break; /* pass off to ^P handler */
753 case 'B': /* down arrow */
756 break; /* pass off to ^N handler */
758 esc_save[esc_len++] = ichar;
759 cread_add_str(esc_save, esc_len, insert,
760 &num, &eol_num, buf, *len);
769 esc_save[esc_len] = ichar;
772 puts("impossible condition #876\n");
780 case CTL_CH('c'): /* ^C - break */
781 *buf = '\0'; /* discard input */
785 getcmd_putch(buf[num]);
791 getcmd_putch(CTL_BACKSPACE);
797 wlen = eol_num - num - 1;
799 memmove(&buf[num], &buf[num+1], wlen);
800 putnstr(buf + num, wlen);
805 getcmd_putch(CTL_BACKSPACE);
828 wlen = eol_num - num;
830 memmove(&buf[num], &buf[num+1], wlen);
831 getcmd_putch(CTL_BACKSPACE);
832 putnstr(buf + num, wlen);
835 getcmd_putch(CTL_BACKSPACE);
847 if (ichar == CTL_CH('p'))
857 /* nuke the current line */
861 /* erase to end of line */
864 /* copy new line into place and display */
866 eol_num = strlen(buf);
870 #ifdef CONFIG_AUTO_COMPLETE
874 /* do not autocomplete when in the middle */
881 col = strlen(prompt) + eol_num;
883 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
892 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
897 buf[eol_num] = '\0'; /* lose the newline */
899 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
900 cread_add_to_hist(buf);
901 hist_cur = hist_add_idx;
906 #endif /* CONFIG_CMDLINE_EDITING */
908 /****************************************************************************/
911 * Prompt for input and read a line.
912 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
913 * time out when time goes past endtime (timebase time in ticks).
914 * Return: number of read characters
918 int readline (const char *const prompt)
921 * If console_buffer isn't 0-length the user will be prompted to modify
922 * it instead of entering it from scratch as desired.
924 console_buffer[0] = '\0';
926 return readline_into_buffer(prompt, console_buffer, 0);
930 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
933 #ifdef CONFIG_CMDLINE_EDITING
934 unsigned int len = CONFIG_SYS_CBSIZE;
936 static int initted = 0;
939 * History uses a global array which is not
940 * writable until after relocation to RAM.
941 * Revert to non-history version if still
942 * running from flash.
944 if (gd->flags & GD_FLG_RELOC) {
953 rc = cread_line(prompt, p, &len, timeout);
954 return rc < 0 ? rc : len;
957 #endif /* CONFIG_CMDLINE_EDITING */
959 int n = 0; /* buffer index */
960 int plen = 0; /* prompt length */
961 int col; /* output column cnt */
966 plen = strlen (prompt);
972 #ifdef CONFIG_BOOT_RETRY_TIME
973 while (!tstc()) { /* while no incoming data */
974 if (retry_time >= 0 && get_ticks() > endtime)
975 return (-2); /* timed out */
979 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
981 #ifdef CONFIG_SHOW_ACTIVITY
990 * Special character handling
993 case '\r': /* Enter */
1002 case 0x03: /* ^C - break */
1003 p_buf[0] = '\0'; /* discard input */
1006 case 0x15: /* ^U - erase line */
1007 while (col > plen) {
1015 case 0x17: /* ^W - erase word */
1016 p=delete_char(p_buf, p, &col, &n, plen);
1017 while ((n > 0) && (*p != ' ')) {
1018 p=delete_char(p_buf, p, &col, &n, plen);
1022 case 0x08: /* ^H - backspace */
1023 case 0x7F: /* DEL - backspace */
1024 p=delete_char(p_buf, p, &col, &n, plen);
1029 * Must be a normal character then
1031 if (n < CONFIG_SYS_CBSIZE-2) {
1032 if (c == '\t') { /* expand TABs */
1033 #ifdef CONFIG_AUTO_COMPLETE
1034 /* if auto completion triggered just continue */
1036 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1037 p = p_buf + n; /* reset */
1041 puts (tab_seq+(col&07));
1042 col += 8 - (col&07);
1044 ++col; /* echo input */
1049 } else { /* Buffer full */
1054 #ifdef CONFIG_CMDLINE_EDITING
1059 /****************************************************************************/
1061 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1069 if (*(--p) == '\t') { /* will retype the whole line */
1070 while (*colp > plen) {
1074 for (s=buffer; s<p; ++s) {
1076 puts (tab_seq+((*colp) & 07));
1077 *colp += 8 - ((*colp) & 07);
1091 /****************************************************************************/
1093 int parse_line (char *line, char *argv[])
1098 printf ("parse_line: \"%s\"\n", line);
1100 while (nargs < CONFIG_SYS_MAXARGS) {
1102 /* skip any white space */
1103 while (isblank(*line))
1106 if (*line == '\0') { /* end of line, no more args */
1109 printf ("parse_line: nargs=%d\n", nargs);
1114 argv[nargs++] = line; /* begin of argument string */
1116 /* find end of string */
1117 while (*line && !isblank(*line))
1120 if (*line == '\0') { /* end of line, no more args */
1123 printf ("parse_line: nargs=%d\n", nargs);
1128 *line++ = '\0'; /* terminate current arg */
1131 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1134 printf ("parse_line: nargs=%d\n", nargs);
1139 /****************************************************************************/
1141 #ifndef CONFIG_SYS_HUSH_PARSER
1142 static void process_macros (const char *input, char *output)
1145 const char *varname_start = NULL;
1146 int inputcnt = strlen (input);
1147 int outputcnt = CONFIG_SYS_CBSIZE;
1148 int state = 0; /* 0 = waiting for '$' */
1150 /* 1 = waiting for '(' or '{' */
1151 /* 2 = waiting for ')' or '}' */
1152 /* 3 = waiting for ''' */
1154 char *output_start = output;
1156 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1160 prev = '\0'; /* previous character */
1162 while (inputcnt && outputcnt) {
1167 /* remove one level of escape characters */
1168 if ((c == '\\') && (prev != '\\')) {
1169 if (inputcnt-- == 0)
1177 case 0: /* Waiting for (unescaped) $ */
1178 if ((c == '\'') && (prev != '\\')) {
1182 if ((c == '$') && (prev != '\\')) {
1189 case 1: /* Waiting for ( */
1190 if (c == '(' || c == '{') {
1192 varname_start = input;
1204 case 2: /* Waiting for ) */
1205 if (c == ')' || c == '}') {
1207 char envname[CONFIG_SYS_CBSIZE], *envval;
1208 int envcnt = input - varname_start - 1; /* Varname # of chars */
1210 /* Get the varname */
1211 for (i = 0; i < envcnt; i++) {
1212 envname[i] = varname_start[i];
1217 envval = getenv (envname);
1219 /* Copy into the line if it exists */
1221 while ((*envval) && outputcnt) {
1222 *(output++) = *(envval++);
1225 /* Look for another '$' */
1229 case 3: /* Waiting for ' */
1230 if ((c == '\'') && (prev != '\\')) {
1247 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1248 strlen (output_start), output_start);
1252 /****************************************************************************
1254 * 1 - command executed, repeatable
1255 * 0 - command executed but not repeatable, interrupted commands are
1256 * always considered not repeatable
1257 * -1 - not executed (unrecognized, bootd recursion or too many args)
1258 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1259 * considered unrecognized)
1263 * We must create a temporary copy of the command since the command we get
1264 * may be the result from getenv(), which returns a pointer directly to
1265 * the environment data, which may change magicly when the command we run
1266 * creates or modifies environment variables (like "bootp" does).
1268 static int builtin_run_command(const char *cmd, int flag)
1270 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1271 char *token; /* start of token in cmdbuf */
1272 char *sep; /* end of token (separator) in cmdbuf */
1273 char finaltoken[CONFIG_SYS_CBSIZE];
1275 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1281 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1282 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1286 clear_ctrlc(); /* forget any previous Control C */
1288 if (!cmd || !*cmd) {
1289 return -1; /* empty command */
1292 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1293 puts ("## Command too long!\n");
1297 strcpy (cmdbuf, cmd);
1299 /* Process separators and check for invalid
1300 * repeatable commands
1304 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1309 * Find separator, or string end
1310 * Allow simple escape of ';' by writing "\;"
1312 for (inquotes = 0, sep = str; *sep; sep++) {
1318 (*sep == ';') && /* separator */
1319 ( sep != str) && /* past string start */
1320 (*(sep-1) != '\\')) /* and NOT escaped */
1325 * Limit the token to data between separators
1329 str = sep + 1; /* start of command for next pass */
1333 str = sep; /* no more commands for next pass */
1335 printf ("token: \"%s\"\n", token);
1338 /* find macros in this token and replace them */
1339 process_macros (token, finaltoken);
1341 /* Extract arguments */
1342 if ((argc = parse_line (finaltoken, argv)) == 0) {
1343 rc = -1; /* no command at all */
1347 if (cmd_process(flag, argc, argv, &repeatable))
1350 /* Did the user stop this? */
1352 return -1; /* if stopped then not repeatable */
1355 return rc ? rc : repeatable;
1360 * Run a command using the selected parser.
1362 * @param cmd Command to run
1363 * @param flag Execution flags (CMD_FLAG_...)
1364 * @return 0 on success, or != 0 on error.
1366 int run_command(const char *cmd, int flag)
1368 #ifndef CONFIG_SYS_HUSH_PARSER
1370 * builtin_run_command can return 0 or 1 for success, so clean up
1373 if (builtin_run_command(cmd, flag) == -1)
1378 return parse_string_outer(cmd,
1379 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1383 #ifndef CONFIG_SYS_HUSH_PARSER
1385 * Execute a list of command separated by ; or \n using the built-in parser.
1387 * This function cannot take a const char * for the command, since if it
1388 * finds newlines in the string, it replaces them with \0.
1390 * @param cmd String containing list of commands
1391 * @param flag Execution flags (CMD_FLAG_...)
1392 * @return 0 on success, or != 0 on error.
1394 static int builtin_run_command_list(char *cmd, int flag)
1400 * Break into individual lines, and execute each line; terminate on
1405 if (*next == '\n') {
1407 /* run only non-empty commands */
1409 debug("** exec: \"%s\"\n", line);
1410 if (builtin_run_command(line, 0) < 0) {
1419 if (rcode == 0 && *line)
1420 rcode = (builtin_run_command(line, 0) >= 0);
1426 int run_command_list(const char *cmd, int len, int flag)
1429 char *buff = (char *)cmd; /* cast away const */
1434 #ifdef CONFIG_SYS_HUSH_PARSER
1435 /* hush will never change our string */
1438 /* the built-in parser will change our string if it sees \n */
1439 need_buff = strchr(cmd, '\n') != NULL;
1443 buff = malloc(len + 1);
1446 memcpy(buff, cmd, len);
1449 #ifdef CONFIG_SYS_HUSH_PARSER
1450 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1453 * This function will overwrite any \n it sees with a \0, which
1454 * is why it can't work with a const char *. Here we are making
1455 * using of internal knowledge of this function, to avoid always
1456 * doing a malloc() which is actually required only in a case that
1459 rcode = builtin_run_command_list(buff, flag);
1467 /****************************************************************************/
1469 #if defined(CONFIG_CMD_RUN)
1470 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1475 return CMD_RET_USAGE;
1477 for (i=1; i<argc; ++i) {
1480 if ((arg = getenv (argv[i])) == NULL) {
1481 printf ("## Error: \"%s\" not defined\n", argv[i]);
1485 if (run_command(arg, flag) != 0)