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);
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_SILENT_CONSOLE
116 if (gd->flags & GD_FLG_SILENT) {
117 /* Restore serial console */
118 console_assign (stdout, "serial");
119 console_assign (stderr, "serial");
123 # ifdef CONFIG_AUTOBOOT_PROMPT
124 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
127 # ifdef CONFIG_AUTOBOOT_DELAY_STR
128 if (delaykey[0].str == NULL)
129 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
131 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
132 if (delaykey[1].str == NULL)
133 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
135 # ifdef CONFIG_AUTOBOOT_STOP_STR
136 if (delaykey[2].str == NULL)
137 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
139 # ifdef CONFIG_AUTOBOOT_STOP_STR2
140 if (delaykey[3].str == NULL)
141 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
144 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
145 delaykey[i].len = delaykey[i].str == NULL ?
146 0 : strlen (delaykey[i].str);
147 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
148 MAX_DELAY_STOP_STR : delaykey[i].len;
150 presskey_max = presskey_max > delaykey[i].len ?
151 presskey_max : delaykey[i].len;
154 printf("%s key:<%s>\n",
155 delaykey[i].retry ? "delay" : "stop",
156 delaykey[i].str ? delaykey[i].str : "NULL");
160 /* In order to keep up with incoming data, check timeout only
163 while (!abort && get_ticks() <= etime) {
164 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
165 if (delaykey[i].len > 0 &&
166 presskey_len >= delaykey[i].len &&
167 memcmp (presskey + presskey_len - delaykey[i].len,
169 delaykey[i].len) == 0) {
171 printf("got %skey\n",
172 delaykey[i].retry ? "delay" : "stop");
175 # ifdef CONFIG_BOOT_RETRY_TIME
176 /* don't retry auto boot */
177 if (! delaykey[i].retry)
185 if (presskey_len < presskey_max) {
186 presskey [presskey_len ++] = getc();
189 for (i = 0; i < presskey_max - 1; i ++)
190 presskey [i] = presskey [i + 1];
192 presskey [i] = getc();
198 puts ("key timeout\n");
201 #ifdef CONFIG_SILENT_CONSOLE
203 /* permanently enable normal console output */
204 gd->flags &= ~(GD_FLG_SILENT);
205 } else if (gd->flags & GD_FLG_SILENT) {
206 /* Restore silent console */
207 console_assign (stdout, "nulldev");
208 console_assign (stderr, "nulldev");
215 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
217 #ifdef CONFIG_MENUKEY
218 static int menukey = 0;
221 static __inline__ int abortboot(int bootdelay)
225 #ifdef CONFIG_SILENT_CONSOLE
226 if (gd->flags & GD_FLG_SILENT) {
227 /* Restore serial console */
228 console_assign (stdout, "serial");
229 console_assign (stderr, "serial");
233 #ifdef CONFIG_MENUPROMPT
234 printf(CONFIG_MENUPROMPT, bootdelay);
236 printf("Hit any key to stop autoboot: %2d ", bootdelay);
239 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
241 * Check if key already pressed
242 * Don't check if bootdelay < 0
244 if (bootdelay >= 0) {
245 if (tstc()) { /* we got a key press */
246 (void) getc(); /* consume input */
248 abort = 1; /* don't auto boot */
253 while ((bootdelay > 0) && (!abort)) {
257 /* delay 100 * 10ms */
258 for (i=0; !abort && i<100; ++i) {
259 if (tstc()) { /* we got a key press */
260 abort = 1; /* don't auto boot */
261 bootdelay = 0; /* no more delay */
262 # ifdef CONFIG_MENUKEY
265 (void) getc(); /* consume input */
272 printf ("\b\b\b%2d ", bootdelay);
277 #ifdef CONFIG_SILENT_CONSOLE
279 /* permanently enable normal console output */
280 gd->flags &= ~(GD_FLG_SILENT);
281 } else if (gd->flags & GD_FLG_SILENT) {
282 /* Restore silent console */
283 console_assign (stdout, "nulldev");
284 console_assign (stderr, "nulldev");
290 # endif /* CONFIG_AUTOBOOT_KEYED */
291 #endif /* CONFIG_BOOTDELAY >= 0 */
293 /****************************************************************************/
295 void main_loop (void)
297 #ifndef CFG_HUSH_PARSER
298 static char lastcommand[CFG_CBSIZE] = { 0, };
304 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
308 #ifdef CONFIG_PREBOOT
311 #ifdef CONFIG_BOOTCOUNT_LIMIT
312 unsigned long bootcount = 0;
313 unsigned long bootlimit = 0;
316 #endif /* CONFIG_BOOTCOUNT_LIMIT */
318 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
319 ulong bmp = 0; /* default bitmap */
320 extern int trab_vfd (ulong bitmap);
322 #ifdef CONFIG_MODEM_SUPPORT
324 bmp = 1; /* alternate bitmap */
327 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
329 #ifdef CONFIG_BOOTCOUNT_LIMIT
330 bootcount = bootcount_load();
332 bootcount_store (bootcount);
333 sprintf (bcs_set, "%lu", bootcount);
334 setenv ("bootcount", bcs_set);
335 bcs = getenv ("bootlimit");
336 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
337 #endif /* CONFIG_BOOTCOUNT_LIMIT */
339 #ifdef CONFIG_MODEM_SUPPORT
340 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
342 char *str = strdup(getenv("mdm_cmd"));
343 setenv ("preboot", str); /* set or delete definition */
346 mdm_init(); /* wait for modem connection */
348 #endif /* CONFIG_MODEM_SUPPORT */
350 #ifdef CONFIG_VERSION_VARIABLE
352 extern char version_string[];
354 setenv ("ver", version_string); /* set version variable */
356 #endif /* CONFIG_VERSION_VARIABLE */
358 #ifdef CFG_HUSH_PARSER
359 u_boot_hush_start ();
362 #ifdef CONFIG_AUTO_COMPLETE
363 install_auto_complete();
366 #ifdef CONFIG_PREBOOT
367 if ((p = getenv ("preboot")) != NULL) {
368 # ifdef CONFIG_AUTOBOOT_KEYED
369 int prev = disable_ctrlc(1); /* disable Control C checking */
372 # ifndef CFG_HUSH_PARSER
375 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
376 FLAG_EXIT_FROM_LOOP);
379 # ifdef CONFIG_AUTOBOOT_KEYED
380 disable_ctrlc(prev); /* restore Control C checking */
383 #endif /* CONFIG_PREBOOT */
385 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
386 s = getenv ("bootdelay");
387 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
389 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
391 # ifdef CONFIG_BOOT_RETRY_TIME
393 # endif /* CONFIG_BOOT_RETRY_TIME */
395 #ifdef CONFIG_BOOTCOUNT_LIMIT
396 if (bootlimit && (bootcount > bootlimit)) {
397 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
398 (unsigned)bootlimit);
399 s = getenv ("altbootcmd");
402 #endif /* CONFIG_BOOTCOUNT_LIMIT */
403 s = getenv ("bootcmd");
405 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
407 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
408 # ifdef CONFIG_AUTOBOOT_KEYED
409 int prev = disable_ctrlc(1); /* disable Control C checking */
412 # ifndef CFG_HUSH_PARSER
415 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
416 FLAG_EXIT_FROM_LOOP);
419 # ifdef CONFIG_AUTOBOOT_KEYED
420 disable_ctrlc(prev); /* restore Control C checking */
424 # ifdef CONFIG_MENUKEY
425 if (menukey == CONFIG_MENUKEY) {
426 s = getenv("menucmd");
428 # ifndef CFG_HUSH_PARSER
431 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
432 FLAG_EXIT_FROM_LOOP);
436 #endif /* CONFIG_MENUKEY */
437 #endif /* CONFIG_BOOTDELAY */
439 #ifdef CONFIG_AMIGAONEG3SE
441 extern void video_banner(void);
447 * Main Loop for Monitor Command Processing
449 #ifdef CFG_HUSH_PARSER
451 /* This point is never reached */
455 #ifdef CONFIG_BOOT_RETRY_TIME
457 /* Saw enough of a valid command to
458 * restart the timeout.
463 len = readline (CFG_PROMPT);
465 flag = 0; /* assume no special flags for now */
467 strcpy (lastcommand, console_buffer);
469 flag |= CMD_FLAG_REPEAT;
470 #ifdef CONFIG_BOOT_RETRY_TIME
471 else if (len == -2) {
472 /* -2 means timed out, retry autoboot
474 puts ("\nTimed out waiting for command\n");
475 # ifdef CONFIG_RESET_TO_RETRY
476 /* Reinit board to run initialization code again */
477 do_reset (NULL, 0, 0, NULL);
479 return; /* retry autoboot */
485 puts ("<INTERRUPT>\n");
487 rc = run_command (lastcommand, flag);
490 /* invalid command or not repeatable, forget it */
494 #endif /*CFG_HUSH_PARSER*/
497 #ifdef CONFIG_BOOT_RETRY_TIME
498 /***************************************************************************
499 * initialize command line timeout
501 void init_cmd_timeout(void)
503 char *s = getenv ("bootretry");
506 retry_time = (int)simple_strtol(s, NULL, 10);
508 retry_time = CONFIG_BOOT_RETRY_TIME;
510 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
511 retry_time = CONFIG_BOOT_RETRY_MIN;
514 /***************************************************************************
515 * reset command line timeout to retry_time seconds
517 void reset_cmd_timeout(void)
519 endtime = endtick(retry_time);
523 #ifdef CONFIG_CMDLINE_EDITING
526 * cmdline-editing related codes from vivi.
527 * Author: Janghoon Lyu <nandy@mizi.com>
530 #define putnstr(str,n) do { \
531 printf ("%.*s", n, str); \
534 #define CTL_CH(c) ((c) - 'a' + 1)
536 #define MAX_CMDBUF_SIZE 256
538 #define CTL_BACKSPACE ('\b')
539 #define DEL ((char)255)
540 #define DEL7 ((char)127)
541 #define CREAD_HIST_CHAR ('!')
543 #define getcmd_putch(ch) putc(ch)
544 #define getcmd_getch() getc()
545 #define getcmd_cbeep() getcmd_putch('\a')
548 #define HIST_SIZE MAX_CMDBUF_SIZE
550 static int hist_max = 0;
551 static int hist_add_idx = 0;
552 static int hist_cur = -1;
553 unsigned hist_num = 0;
555 char* hist_list[HIST_MAX];
556 char hist_lines[HIST_MAX][HIST_SIZE];
558 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
560 static void hist_init(void)
569 for (i = 0; i < HIST_MAX; i++) {
570 hist_list[i] = hist_lines[i];
571 hist_list[i][0] = '\0';
575 static void cread_add_to_hist(char *line)
577 strcpy(hist_list[hist_add_idx], line);
579 if (++hist_add_idx >= HIST_MAX)
582 if (hist_add_idx > hist_max)
583 hist_max = hist_add_idx;
588 static char* hist_prev(void)
600 if (hist_cur == hist_add_idx) {
604 ret = hist_list[hist_cur];
609 static char* hist_next(void)
616 if (hist_cur == hist_add_idx)
619 if (++hist_cur > hist_max)
622 if (hist_cur == hist_add_idx) {
625 ret = hist_list[hist_cur];
630 #ifndef CONFIG_CMDLINE_EDITING
631 static void cread_print_hist_list(void)
636 n = hist_num - hist_max;
638 i = hist_add_idx + 1;
642 if (i == hist_add_idx)
644 printf("%s\n", hist_list[i]);
649 #endif /* CONFIG_CMDLINE_EDITING */
651 #define BEGINNING_OF_LINE() { \
653 getcmd_putch(CTL_BACKSPACE); \
658 #define ERASE_TO_EOL() { \
659 if (num < eol_num) { \
661 for (tmp = num; tmp < eol_num; tmp++) \
663 while (tmp-- > num) \
664 getcmd_putch(CTL_BACKSPACE); \
669 #define REFRESH_TO_EOL() { \
670 if (num < eol_num) { \
671 wlen = eol_num - num; \
672 putnstr(buf + num, wlen); \
677 static void cread_add_char(char ichar, int insert, unsigned long *num,
678 unsigned long *eol_num, char *buf, unsigned long len)
683 if (insert || *num == *eol_num) {
684 if (*eol_num > len - 1) {
692 wlen = *eol_num - *num;
694 memmove(&buf[*num+1], &buf[*num], wlen-1);
698 putnstr(buf + *num, wlen);
701 getcmd_putch(CTL_BACKSPACE);
704 /* echo the character */
707 putnstr(buf + *num, wlen);
712 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
713 unsigned long *eol_num, char *buf, unsigned long len)
716 cread_add_char(*str, insert, num, eol_num, buf, len);
721 static int cread_line(char *buf, unsigned int *len)
723 unsigned long num = 0;
724 unsigned long eol_num = 0;
735 ichar = getcmd_getch();
737 if ((ichar == '\n') || (ichar == '\r')) {
743 * handle standard linux xterm esc sequences for arrow key, etc.
748 esc_save[esc_len] = ichar;
751 cread_add_str(esc_save, esc_len, insert,
752 &num, &eol_num, buf, *len);
760 case 'D': /* <- key */
764 case 'C': /* -> key */
767 break; /* pass off to ^F handler */
768 case 'H': /* Home key */
771 break; /* pass off to ^A handler */
772 case 'A': /* up arrow */
775 break; /* pass off to ^P handler */
776 case 'B': /* down arrow */
779 break; /* pass off to ^N handler */
781 esc_save[esc_len++] = ichar;
782 cread_add_str(esc_save, esc_len, insert,
783 &num, &eol_num, buf, *len);
792 esc_save[esc_len] = ichar;
795 puts("impossible condition #876\n");
803 case CTL_CH('c'): /* ^C - break */
804 *buf = '\0'; /* discard input */
808 getcmd_putch(buf[num]);
814 getcmd_putch(CTL_BACKSPACE);
820 wlen = eol_num - num - 1;
822 memmove(&buf[num], &buf[num+1], wlen);
823 putnstr(buf + num, wlen);
828 getcmd_putch(CTL_BACKSPACE);
850 wlen = eol_num - num;
852 memmove(&buf[num], &buf[num+1], wlen);
853 getcmd_putch(CTL_BACKSPACE);
854 putnstr(buf + num, wlen);
857 getcmd_putch(CTL_BACKSPACE);
869 if (ichar == CTL_CH('p'))
879 /* nuke the current line */
883 /* erase to end of line */
886 /* copy new line into place and display */
888 eol_num = strlen(buf);
893 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
898 buf[eol_num] = '\0'; /* lose the newline */
900 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
901 cread_add_to_hist(buf);
902 hist_cur = hist_add_idx;
907 #endif /* CONFIG_CMDLINE_EDITING */
909 /****************************************************************************/
912 * Prompt for input and read a line.
913 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
914 * time out when time goes past endtime (timebase time in ticks).
915 * Return: number of read characters
919 int readline (const char *const prompt)
921 #ifdef CONFIG_CMDLINE_EDITING
922 char *p = console_buffer;
923 unsigned int len=MAX_CMDBUF_SIZE;
925 static int initted = 0;
934 rc = cread_line(p, &len);
935 return rc < 0 ? rc : len;
937 char *p = console_buffer;
938 int n = 0; /* buffer index */
939 int plen = 0; /* prompt length */
940 int col; /* output column cnt */
945 plen = strlen (prompt);
951 #ifdef CONFIG_BOOT_RETRY_TIME
952 while (!tstc()) { /* while no incoming data */
953 if (retry_time >= 0 && get_ticks() > endtime)
954 return (-2); /* timed out */
957 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
959 #ifdef CONFIG_SHOW_ACTIVITY
961 extern void show_activity(int arg);
968 * Special character handling
971 case '\r': /* Enter */
975 return (p - console_buffer);
980 case 0x03: /* ^C - break */
981 console_buffer[0] = '\0'; /* discard input */
984 case 0x15: /* ^U - erase line */
993 case 0x17: /* ^W - erase word */
994 p=delete_char(console_buffer, p, &col, &n, plen);
995 while ((n > 0) && (*p != ' ')) {
996 p=delete_char(console_buffer, p, &col, &n, plen);
1000 case 0x08: /* ^H - backspace */
1001 case 0x7F: /* DEL - backspace */
1002 p=delete_char(console_buffer, p, &col, &n, plen);
1007 * Must be a normal character then
1009 if (n < CFG_CBSIZE-2) {
1010 if (c == '\t') { /* expand TABs */
1011 #ifdef CONFIG_AUTO_COMPLETE
1012 /* if auto completion triggered just continue */
1014 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1015 p = console_buffer + n; /* reset */
1019 puts (tab_seq+(col&07));
1020 col += 8 - (col&07);
1022 ++col; /* echo input */
1027 } else { /* Buffer full */
1032 #endif /* CONFIG_CMDLINE_EDITING */
1035 /****************************************************************************/
1037 #ifndef CONFIG_CMDLINE_EDITING
1038 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1046 if (*(--p) == '\t') { /* will retype the whole line */
1047 while (*colp > plen) {
1051 for (s=buffer; s<p; ++s) {
1053 puts (tab_seq+((*colp) & 07));
1054 *colp += 8 - ((*colp) & 07);
1067 #endif /* CONFIG_CMDLINE_EDITING */
1069 /****************************************************************************/
1071 int parse_line (char *line, char *argv[])
1076 printf ("parse_line: \"%s\"\n", line);
1078 while (nargs < CFG_MAXARGS) {
1080 /* skip any white space */
1081 while ((*line == ' ') || (*line == '\t')) {
1085 if (*line == '\0') { /* end of line, no more args */
1088 printf ("parse_line: nargs=%d\n", nargs);
1093 argv[nargs++] = line; /* begin of argument string */
1095 /* find end of string */
1096 while (*line && (*line != ' ') && (*line != '\t')) {
1100 if (*line == '\0') { /* end of line, no more args */
1103 printf ("parse_line: nargs=%d\n", nargs);
1108 *line++ = '\0'; /* terminate current arg */
1111 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1114 printf ("parse_line: nargs=%d\n", nargs);
1119 /****************************************************************************/
1121 static void process_macros (const char *input, char *output)
1124 const char *varname_start = NULL;
1125 int inputcnt = strlen (input);
1126 int outputcnt = CFG_CBSIZE;
1127 int state = 0; /* 0 = waiting for '$' */
1129 /* 1 = waiting for '(' or '{' */
1130 /* 2 = waiting for ')' or '}' */
1131 /* 3 = waiting for ''' */
1133 char *output_start = output;
1135 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1139 prev = '\0'; /* previous character */
1141 while (inputcnt && outputcnt) {
1146 /* remove one level of escape characters */
1147 if ((c == '\\') && (prev != '\\')) {
1148 if (inputcnt-- == 0)
1156 case 0: /* Waiting for (unescaped) $ */
1157 if ((c == '\'') && (prev != '\\')) {
1161 if ((c == '$') && (prev != '\\')) {
1168 case 1: /* Waiting for ( */
1169 if (c == '(' || c == '{') {
1171 varname_start = input;
1183 case 2: /* Waiting for ) */
1184 if (c == ')' || c == '}') {
1186 char envname[CFG_CBSIZE], *envval;
1187 int envcnt = input - varname_start - 1; /* Varname # of chars */
1189 /* Get the varname */
1190 for (i = 0; i < envcnt; i++) {
1191 envname[i] = varname_start[i];
1196 envval = getenv (envname);
1198 /* Copy into the line if it exists */
1200 while ((*envval) && outputcnt) {
1201 *(output++) = *(envval++);
1204 /* Look for another '$' */
1208 case 3: /* Waiting for ' */
1209 if ((c == '\'') && (prev != '\\')) {
1224 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1225 strlen (output_start), output_start);
1229 /****************************************************************************
1231 * 1 - command executed, repeatable
1232 * 0 - command executed but not repeatable, interrupted commands are
1233 * always considered not repeatable
1234 * -1 - not executed (unrecognized, bootd recursion or too many args)
1235 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1236 * considered unrecognized)
1240 * We must create a temporary copy of the command since the command we get
1241 * may be the result from getenv(), which returns a pointer directly to
1242 * the environment data, which may change magicly when the command we run
1243 * creates or modifies environment variables (like "bootp" does).
1246 int run_command (const char *cmd, int flag)
1249 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1250 char *token; /* start of token in cmdbuf */
1251 char *sep; /* end of token (separator) in cmdbuf */
1252 char finaltoken[CFG_CBSIZE];
1254 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1260 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1261 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1265 clear_ctrlc(); /* forget any previous Control C */
1267 if (!cmd || !*cmd) {
1268 return -1; /* empty command */
1271 if (strlen(cmd) >= CFG_CBSIZE) {
1272 puts ("## Command too long!\n");
1276 strcpy (cmdbuf, cmd);
1278 /* Process separators and check for invalid
1279 * repeatable commands
1283 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1288 * Find separator, or string end
1289 * Allow simple escape of ';' by writing "\;"
1291 for (inquotes = 0, sep = str; *sep; sep++) {
1297 (*sep == ';') && /* separator */
1298 ( sep != str) && /* past string start */
1299 (*(sep-1) != '\\')) /* and NOT escaped */
1304 * Limit the token to data between separators
1308 str = sep + 1; /* start of command for next pass */
1312 str = sep; /* no more commands for next pass */
1314 printf ("token: \"%s\"\n", token);
1317 /* find macros in this token and replace them */
1318 process_macros (token, finaltoken);
1320 /* Extract arguments */
1321 if ((argc = parse_line (finaltoken, argv)) == 0) {
1322 rc = -1; /* no command at all */
1326 /* Look up command in command table */
1327 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1328 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1329 rc = -1; /* give up after bad command */
1333 /* found - check max args */
1334 if (argc > cmdtp->maxargs) {
1335 printf ("Usage:\n%s\n", cmdtp->usage);
1340 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1341 /* avoid "bootd" recursion */
1342 if (cmdtp->cmd == do_bootd) {
1344 printf ("[%s]\n", finaltoken);
1346 if (flag & CMD_FLAG_BOOTD) {
1347 puts ("'bootd' recursion detected\n");
1351 flag |= CMD_FLAG_BOOTD;
1354 #endif /* CFG_CMD_BOOTD */
1356 /* OK - call function to do the command */
1357 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1361 repeatable &= cmdtp->repeatable;
1363 /* Did the user stop this? */
1365 return 0; /* if stopped then not repeatable */
1368 return rc ? rc : repeatable;
1371 /****************************************************************************/
1373 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
1374 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1379 printf ("Usage:\n%s\n", cmdtp->usage);
1383 for (i=1; i<argc; ++i) {
1386 if ((arg = getenv (argv[i])) == NULL) {
1387 printf ("## Error: \"%s\" not defined\n", argv[i]);
1390 #ifndef CFG_HUSH_PARSER
1391 if (run_command (arg, flag) == -1)
1394 if (parse_string_outer(arg,
1395 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
1401 #endif /* CFG_CMD_RUN */