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 #ifdef CONFIG_SILENT_CONSOLE
44 DECLARE_GLOBAL_DATA_PTR;
47 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
48 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
51 extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
54 #define MAX_DELAY_STOP_STR 32
56 static int parse_line (char *, char *[]);
57 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
58 static int abortboot(int);
63 char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
65 #ifndef CONFIG_CMDLINE_EDITING
66 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
67 static char erase_seq[] = "\b \b"; /* erase sequence */
68 static char tab_seq[] = " "; /* used to expand TABs */
69 #endif /* CONFIG_CMDLINE_EDITING */
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);
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 #ifdef CONFIG_SILENT_CONSOLE
118 if (gd->flags & GD_FLG_SILENT) {
119 /* Restore serial console */
120 console_assign (stdout, "serial");
121 console_assign (stderr, "serial");
125 # ifdef CONFIG_AUTOBOOT_PROMPT
126 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
129 # ifdef CONFIG_AUTOBOOT_DELAY_STR
130 if (delaykey[0].str == NULL)
131 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
133 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
134 if (delaykey[1].str == NULL)
135 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
137 # ifdef CONFIG_AUTOBOOT_STOP_STR
138 if (delaykey[2].str == NULL)
139 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
141 # ifdef CONFIG_AUTOBOOT_STOP_STR2
142 if (delaykey[3].str == NULL)
143 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
146 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
147 delaykey[i].len = delaykey[i].str == NULL ?
148 0 : strlen (delaykey[i].str);
149 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
150 MAX_DELAY_STOP_STR : delaykey[i].len;
152 presskey_max = presskey_max > delaykey[i].len ?
153 presskey_max : delaykey[i].len;
156 printf("%s key:<%s>\n",
157 delaykey[i].retry ? "delay" : "stop",
158 delaykey[i].str ? delaykey[i].str : "NULL");
162 /* In order to keep up with incoming data, check timeout only
165 while (!abort && get_ticks() <= etime) {
166 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
167 if (delaykey[i].len > 0 &&
168 presskey_len >= delaykey[i].len &&
169 memcmp (presskey + presskey_len - delaykey[i].len,
171 delaykey[i].len) == 0) {
173 printf("got %skey\n",
174 delaykey[i].retry ? "delay" : "stop");
177 # ifdef CONFIG_BOOT_RETRY_TIME
178 /* don't retry auto boot */
179 if (! delaykey[i].retry)
187 if (presskey_len < presskey_max) {
188 presskey [presskey_len ++] = getc();
191 for (i = 0; i < presskey_max - 1; i ++)
192 presskey [i] = presskey [i + 1];
194 presskey [i] = getc();
200 puts ("key timeout\n");
203 #ifdef CONFIG_SILENT_CONSOLE
205 /* permanently enable normal console output */
206 gd->flags &= ~(GD_FLG_SILENT);
207 } else if (gd->flags & GD_FLG_SILENT) {
208 /* Restore silent console */
209 console_assign (stdout, "nulldev");
210 console_assign (stderr, "nulldev");
217 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
219 #ifdef CONFIG_MENUKEY
220 static int menukey = 0;
223 static __inline__ int abortboot(int bootdelay)
227 #ifdef CONFIG_SILENT_CONSOLE
228 if (gd->flags & GD_FLG_SILENT) {
229 /* Restore serial console */
230 console_assign (stdout, "serial");
231 console_assign (stderr, "serial");
235 #ifdef CONFIG_MENUPROMPT
236 printf(CONFIG_MENUPROMPT, bootdelay);
238 printf("Hit any key to stop autoboot: %2d ", bootdelay);
241 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
243 * Check if key already pressed
244 * Don't check if bootdelay < 0
246 if (bootdelay >= 0) {
247 if (tstc()) { /* we got a key press */
248 (void) getc(); /* consume input */
250 abort = 1; /* don't auto boot */
255 while ((bootdelay > 0) && (!abort)) {
259 /* delay 100 * 10ms */
260 for (i=0; !abort && i<100; ++i) {
261 if (tstc()) { /* we got a key press */
262 abort = 1; /* don't auto boot */
263 bootdelay = 0; /* no more delay */
264 # ifdef CONFIG_MENUKEY
267 (void) getc(); /* consume input */
274 printf ("\b\b\b%2d ", bootdelay);
279 #ifdef CONFIG_SILENT_CONSOLE
281 /* permanently enable normal console output */
282 gd->flags &= ~(GD_FLG_SILENT);
283 } else if (gd->flags & GD_FLG_SILENT) {
284 /* Restore silent console */
285 console_assign (stdout, "nulldev");
286 console_assign (stderr, "nulldev");
292 # endif /* CONFIG_AUTOBOOT_KEYED */
293 #endif /* CONFIG_BOOTDELAY >= 0 */
295 /****************************************************************************/
297 void main_loop (void)
299 #ifndef CFG_HUSH_PARSER
300 static char lastcommand[CFG_CBSIZE] = { 0, };
306 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
310 #ifdef CONFIG_PREBOOT
313 #ifdef CONFIG_BOOTCOUNT_LIMIT
314 unsigned long bootcount = 0;
315 unsigned long bootlimit = 0;
318 #endif /* CONFIG_BOOTCOUNT_LIMIT */
320 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
321 ulong bmp = 0; /* default bitmap */
322 extern int trab_vfd (ulong bitmap);
324 #ifdef CONFIG_MODEM_SUPPORT
326 bmp = 1; /* alternate bitmap */
329 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
331 #ifdef CONFIG_BOOTCOUNT_LIMIT
332 bootcount = bootcount_load();
334 bootcount_store (bootcount);
335 sprintf (bcs_set, "%lu", bootcount);
336 setenv ("bootcount", bcs_set);
337 bcs = getenv ("bootlimit");
338 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
339 #endif /* CONFIG_BOOTCOUNT_LIMIT */
341 #ifdef CONFIG_MODEM_SUPPORT
342 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
344 char *str = strdup(getenv("mdm_cmd"));
345 setenv ("preboot", str); /* set or delete definition */
348 mdm_init(); /* wait for modem connection */
350 #endif /* CONFIG_MODEM_SUPPORT */
352 #ifdef CONFIG_VERSION_VARIABLE
354 extern char version_string[];
356 setenv ("ver", version_string); /* set version variable */
358 #endif /* CONFIG_VERSION_VARIABLE */
360 #ifdef CFG_HUSH_PARSER
361 u_boot_hush_start ();
364 #ifdef CONFIG_AUTO_COMPLETE
365 install_auto_complete();
368 #ifdef CONFIG_PREBOOT
369 if ((p = getenv ("preboot")) != NULL) {
370 # ifdef CONFIG_AUTOBOOT_KEYED
371 int prev = disable_ctrlc(1); /* disable Control C checking */
374 # ifndef CFG_HUSH_PARSER
377 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
378 FLAG_EXIT_FROM_LOOP);
381 # ifdef CONFIG_AUTOBOOT_KEYED
382 disable_ctrlc(prev); /* restore Control C checking */
385 #endif /* CONFIG_PREBOOT */
387 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
388 s = getenv ("bootdelay");
389 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
391 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
393 # ifdef CONFIG_BOOT_RETRY_TIME
395 # endif /* CONFIG_BOOT_RETRY_TIME */
397 #ifdef CONFIG_BOOTCOUNT_LIMIT
398 if (bootlimit && (bootcount > bootlimit)) {
399 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
400 (unsigned)bootlimit);
401 s = getenv ("altbootcmd");
404 #endif /* CONFIG_BOOTCOUNT_LIMIT */
405 s = getenv ("bootcmd");
407 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
409 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
410 # ifdef CONFIG_AUTOBOOT_KEYED
411 int prev = disable_ctrlc(1); /* disable Control C checking */
414 # ifndef CFG_HUSH_PARSER
417 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
418 FLAG_EXIT_FROM_LOOP);
421 # ifdef CONFIG_AUTOBOOT_KEYED
422 disable_ctrlc(prev); /* restore Control C checking */
426 # ifdef CONFIG_MENUKEY
427 if (menukey == CONFIG_MENUKEY) {
428 s = getenv("menucmd");
430 # ifndef CFG_HUSH_PARSER
433 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
434 FLAG_EXIT_FROM_LOOP);
438 #endif /* CONFIG_MENUKEY */
439 #endif /* CONFIG_BOOTDELAY */
441 #ifdef CONFIG_AMIGAONEG3SE
443 extern void video_banner(void);
449 * Main Loop for Monitor Command Processing
451 #ifdef CFG_HUSH_PARSER
453 /* This point is never reached */
457 #ifdef CONFIG_BOOT_RETRY_TIME
459 /* Saw enough of a valid command to
460 * restart the timeout.
465 len = readline (CFG_PROMPT);
467 flag = 0; /* assume no special flags for now */
469 strcpy (lastcommand, console_buffer);
471 flag |= CMD_FLAG_REPEAT;
472 #ifdef CONFIG_BOOT_RETRY_TIME
473 else if (len == -2) {
474 /* -2 means timed out, retry autoboot
476 puts ("\nTimed out waiting for command\n");
477 # ifdef CONFIG_RESET_TO_RETRY
478 /* Reinit board to run initialization code again */
479 do_reset (NULL, 0, 0, NULL);
481 return; /* retry autoboot */
487 puts ("<INTERRUPT>\n");
489 rc = run_command (lastcommand, flag);
492 /* invalid command or not repeatable, forget it */
496 #endif /*CFG_HUSH_PARSER*/
499 #ifdef CONFIG_BOOT_RETRY_TIME
500 /***************************************************************************
501 * initialise command line timeout
503 void init_cmd_timeout(void)
505 char *s = getenv ("bootretry");
508 retry_time = (int)simple_strtol(s, NULL, 10);
510 retry_time = CONFIG_BOOT_RETRY_TIME;
512 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
513 retry_time = CONFIG_BOOT_RETRY_MIN;
516 /***************************************************************************
517 * reset command line timeout to retry_time seconds
519 void reset_cmd_timeout(void)
521 endtime = endtick(retry_time);
525 #ifdef CONFIG_CMDLINE_EDITING
528 * cmdline-editing related codes from vivi.
529 * Author: Janghoon Lyu <nandy@mizi.com>
532 #if 1 /* avoid redundand code -- wd */
533 #define putnstr(str,n) do { \
534 printf ("%.*s", n, str); \
537 void putnstr(const char *str, size_t n)
542 while (n && *str != '\0') {
550 #define CTL_CH(c) ((c) - 'a' + 1)
552 #define MAX_CMDBUF_SIZE 256
554 #define CTL_BACKSPACE ('\b')
555 #define DEL ((char)255)
556 #define DEL7 ((char)127)
557 #define CREAD_HIST_CHAR ('!')
559 #define getcmd_putch(ch) putc(ch)
560 #define getcmd_getch() getc()
561 #define getcmd_cbeep() getcmd_putch('\a')
564 #define HIST_SIZE MAX_CMDBUF_SIZE
566 static int hist_max = 0;
567 static int hist_add_idx = 0;
568 static int hist_cur = -1;
569 unsigned hist_num = 0;
571 char* hist_list[HIST_MAX];
572 char hist_lines[HIST_MAX][HIST_SIZE];
574 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
576 static void hist_init(void)
585 for (i = 0; i < HIST_MAX; i++) {
586 hist_list[i] = hist_lines[i];
587 hist_list[i][0] = '\0';
591 static void cread_add_to_hist(char *line)
593 strcpy(hist_list[hist_add_idx], line);
595 if (++hist_add_idx >= HIST_MAX)
598 if (hist_add_idx > hist_max)
599 hist_max = hist_add_idx;
604 static char* hist_prev(void)
616 if (hist_cur == hist_add_idx) {
620 ret = hist_list[hist_cur];
625 static char* hist_next(void)
632 if (hist_cur == hist_add_idx)
635 if (++hist_cur > hist_max)
638 if (hist_cur == hist_add_idx) {
641 ret = hist_list[hist_cur];
646 #ifndef CONFIG_CMDLINE_EDITING
647 static void cread_print_hist_list(void)
652 n = hist_num - hist_max;
654 i = hist_add_idx + 1;
658 if (i == hist_add_idx)
660 printf("%s\n", hist_list[i]);
665 #endif /* CONFIG_CMDLINE_EDITING */
667 #define BEGINNING_OF_LINE() { \
669 getcmd_putch(CTL_BACKSPACE); \
674 #define ERASE_TO_EOL() { \
675 if (num < eol_num) { \
677 for (tmp = num; tmp < eol_num; tmp++) \
679 while (tmp-- > num) \
680 getcmd_putch(CTL_BACKSPACE); \
685 #define REFRESH_TO_EOL() { \
686 if (num < eol_num) { \
687 wlen = eol_num - num; \
688 putnstr(buf + num, wlen); \
693 static void cread_add_char(char ichar, int insert, unsigned long *num,
694 unsigned long *eol_num, char *buf, unsigned long len)
699 if (insert || *num == *eol_num) {
700 if (*eol_num > len - 1) {
708 wlen = *eol_num - *num;
710 memmove(&buf[*num+1], &buf[*num], wlen-1);
714 putnstr(buf + *num, wlen);
717 getcmd_putch(CTL_BACKSPACE);
720 /* echo the character */
723 putnstr(buf + *num, wlen);
728 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
729 unsigned long *eol_num, char *buf, unsigned long len)
732 cread_add_char(*str, insert, num, eol_num, buf, len);
737 static int cread_line(char *buf, unsigned int *len)
739 unsigned long num = 0;
740 unsigned long eol_num = 0;
751 ichar = getcmd_getch();
753 if ((ichar == '\n') || (ichar == '\r')) {
759 * handle standard linux xterm esc sequences for arrow key, etc.
764 esc_save[esc_len] = ichar;
767 cread_add_str(esc_save, esc_len, insert,
768 &num, &eol_num, buf, *len);
776 case 'D': /* <- key */
780 case 'C': /* -> key */
783 break; /* pass off to ^F handler */
784 case 'H': /* Home key */
787 break; /* pass off to ^A handler */
788 case 'A': /* up arrow */
791 break; /* pass off to ^P handler */
792 case 'B': /* down arrow */
795 break; /* pass off to ^N handler */
797 esc_save[esc_len++] = ichar;
798 cread_add_str(esc_save, esc_len, insert,
799 &num, &eol_num, buf, *len);
808 esc_save[esc_len] = ichar;
811 puts("impossible condition #876\n");
819 case CTL_CH('c'): /* ^C - break */
820 *buf = '\0'; /* discard input */
824 getcmd_putch(buf[num]);
830 getcmd_putch(CTL_BACKSPACE);
836 wlen = eol_num - num - 1;
838 memmove(&buf[num], &buf[num+1], wlen);
839 putnstr(buf + num, wlen);
844 getcmd_putch(CTL_BACKSPACE);
866 wlen = eol_num - num;
868 memmove(&buf[num], &buf[num+1], wlen);
869 getcmd_putch(CTL_BACKSPACE);
870 putnstr(buf + num, wlen);
873 getcmd_putch(CTL_BACKSPACE);
885 if (ichar == CTL_CH('p'))
895 /* nuke the current line */
899 /* erase to end of line */
902 /* copy new line into place and display */
904 eol_num = strlen(buf);
909 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
914 buf[eol_num] = '\0'; /* lose the newline */
916 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
917 cread_add_to_hist(buf);
918 hist_cur = hist_add_idx;
923 #endif /* CONFIG_CMDLINE_EDITING */
925 /****************************************************************************/
928 * Prompt for input and read a line.
929 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
930 * time out when time goes past endtime (timebase time in ticks).
931 * Return: number of read characters
935 int readline (const char *const prompt)
937 #ifdef CONFIG_CMDLINE_EDITING
938 char *p = console_buffer;
939 unsigned int len=MAX_CMDBUF_SIZE;
941 static int initted = 0;
950 rc = cread_line(p, &len);
951 return rc < 0 ? rc : len;
953 char *p = console_buffer;
954 int n = 0; /* buffer index */
955 int plen = 0; /* prompt length */
956 int col; /* output column cnt */
961 plen = strlen (prompt);
967 #ifdef CONFIG_BOOT_RETRY_TIME
968 while (!tstc()) { /* while no incoming data */
969 if (retry_time >= 0 && get_ticks() > endtime)
970 return (-2); /* timed out */
973 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
975 #ifdef CONFIG_SHOW_ACTIVITY
977 extern void show_activity(int arg);
984 * Special character handling
987 case '\r': /* Enter */
991 return (p - console_buffer);
996 case 0x03: /* ^C - break */
997 console_buffer[0] = '\0'; /* discard input */
1000 case 0x15: /* ^U - erase line */
1001 while (col > plen) {
1009 case 0x17: /* ^W - erase word */
1010 p=delete_char(console_buffer, p, &col, &n, plen);
1011 while ((n > 0) && (*p != ' ')) {
1012 p=delete_char(console_buffer, p, &col, &n, plen);
1016 case 0x08: /* ^H - backspace */
1017 case 0x7F: /* DEL - backspace */
1018 p=delete_char(console_buffer, p, &col, &n, plen);
1023 * Must be a normal character then
1025 if (n < CFG_CBSIZE-2) {
1026 if (c == '\t') { /* expand TABs */
1027 #ifdef CONFIG_AUTO_COMPLETE
1028 /* if auto completion triggered just continue */
1030 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1031 p = console_buffer + n; /* reset */
1035 puts (tab_seq+(col&07));
1036 col += 8 - (col&07);
1038 ++col; /* echo input */
1043 } else { /* Buffer full */
1048 #endif /* CONFIG_CMDLINE_EDITING */
1051 /****************************************************************************/
1053 #ifndef CONFIG_CMDLINE_EDITING
1054 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1062 if (*(--p) == '\t') { /* will retype the whole line */
1063 while (*colp > plen) {
1067 for (s=buffer; s<p; ++s) {
1069 puts (tab_seq+((*colp) & 07));
1070 *colp += 8 - ((*colp) & 07);
1083 #endif /* CONFIG_CMDLINE_EDITING */
1085 /****************************************************************************/
1087 int parse_line (char *line, char *argv[])
1092 printf ("parse_line: \"%s\"\n", line);
1094 while (nargs < CFG_MAXARGS) {
1096 /* skip any white space */
1097 while ((*line == ' ') || (*line == '\t')) {
1101 if (*line == '\0') { /* end of line, no more args */
1104 printf ("parse_line: nargs=%d\n", nargs);
1109 argv[nargs++] = line; /* begin of argument string */
1111 /* find end of string */
1112 while (*line && (*line != ' ') && (*line != '\t')) {
1116 if (*line == '\0') { /* end of line, no more args */
1119 printf ("parse_line: nargs=%d\n", nargs);
1124 *line++ = '\0'; /* terminate current arg */
1127 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1130 printf ("parse_line: nargs=%d\n", nargs);
1135 /****************************************************************************/
1137 static void process_macros (const char *input, char *output)
1140 const char *varname_start = NULL;
1141 int inputcnt = strlen (input);
1142 int outputcnt = CFG_CBSIZE;
1143 int state = 0; /* 0 = waiting for '$' */
1144 /* 1 = waiting for '(' or '{' */
1145 /* 2 = waiting for ')' or '}' */
1146 /* 3 = waiting for ''' */
1148 char *output_start = output;
1150 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
1153 prev = '\0'; /* previous character */
1155 while (inputcnt && outputcnt) {
1160 /* remove one level of escape characters */
1161 if ((c == '\\') && (prev != '\\')) {
1162 if (inputcnt-- == 0)
1170 case 0: /* Waiting for (unescaped) $ */
1171 if ((c == '\'') && (prev != '\\')) {
1175 if ((c == '$') && (prev != '\\')) {
1182 case 1: /* Waiting for ( */
1183 if (c == '(' || c == '{') {
1185 varname_start = input;
1197 case 2: /* Waiting for ) */
1198 if (c == ')' || c == '}') {
1200 char envname[CFG_CBSIZE], *envval;
1201 int envcnt = input-varname_start-1; /* Varname # of chars */
1203 /* Get the varname */
1204 for (i = 0; i < envcnt; i++) {
1205 envname[i] = varname_start[i];
1210 envval = getenv (envname);
1212 /* Copy into the line if it exists */
1214 while ((*envval) && outputcnt) {
1215 *(output++) = *(envval++);
1218 /* Look for another '$' */
1222 case 3: /* Waiting for ' */
1223 if ((c == '\'') && (prev != '\\')) {
1238 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1239 strlen(output_start), output_start);
1243 /****************************************************************************
1245 * 1 - command executed, repeatable
1246 * 0 - command executed but not repeatable, interrupted commands are
1247 * always considered not repeatable
1248 * -1 - not executed (unrecognized, bootd recursion or too many args)
1249 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1250 * considered unrecognized)
1254 * We must create a temporary copy of the command since the command we get
1255 * may be the result from getenv(), which returns a pointer directly to
1256 * the environment data, which may change magicly when the command we run
1257 * creates or modifies environment variables (like "bootp" does).
1260 int run_command (const char *cmd, int flag)
1263 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1264 char *token; /* start of token in cmdbuf */
1265 char *sep; /* end of token (separator) in cmdbuf */
1266 char finaltoken[CFG_CBSIZE];
1268 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1274 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1275 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1279 clear_ctrlc(); /* forget any previous Control C */
1281 if (!cmd || !*cmd) {
1282 return -1; /* empty command */
1285 if (strlen(cmd) >= CFG_CBSIZE) {
1286 puts ("## Command too long!\n");
1290 strcpy (cmdbuf, cmd);
1292 /* Process separators and check for invalid
1293 * repeatable commands
1297 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1302 * Find separator, or string end
1303 * Allow simple escape of ';' by writing "\;"
1305 for (inquotes = 0, sep = str; *sep; sep++) {
1311 (*sep == ';') && /* separator */
1312 ( sep != str) && /* past string start */
1313 (*(sep-1) != '\\')) /* and NOT escaped */
1318 * Limit the token to data between separators
1322 str = sep + 1; /* start of command for next pass */
1326 str = sep; /* no more commands for next pass */
1328 printf ("token: \"%s\"\n", token);
1331 /* find macros in this token and replace them */
1332 process_macros (token, finaltoken);
1334 /* Extract arguments */
1335 if ((argc = parse_line (finaltoken, argv)) == 0) {
1336 rc = -1; /* no command at all */
1340 /* Look up command in command table */
1341 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1342 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1343 rc = -1; /* give up after bad command */
1347 /* found - check max args */
1348 if (argc > cmdtp->maxargs) {
1349 printf ("Usage:\n%s\n", cmdtp->usage);
1354 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1355 /* avoid "bootd" recursion */
1356 if (cmdtp->cmd == do_bootd) {
1358 printf ("[%s]\n", finaltoken);
1360 if (flag & CMD_FLAG_BOOTD) {
1361 puts ("'bootd' recursion detected\n");
1365 flag |= CMD_FLAG_BOOTD;
1368 #endif /* CFG_CMD_BOOTD */
1370 /* OK - call function to do the command */
1371 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1375 repeatable &= cmdtp->repeatable;
1377 /* Did the user stop this? */
1379 return 0; /* if stopped then not repeatable */
1382 return rc ? rc : repeatable;
1385 /****************************************************************************/
1387 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
1388 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1393 printf ("Usage:\n%s\n", cmdtp->usage);
1397 for (i=1; i<argc; ++i) {
1400 if ((arg = getenv (argv[i])) == NULL) {
1401 printf ("## Error: \"%s\" not defined\n", argv[i]);
1404 #ifndef CFG_HUSH_PARSER
1405 if (run_command (arg, flag) == -1)
1408 if (parse_string_outer(arg,
1409 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
1415 #endif /* CFG_CMD_RUN */