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_AUTOBOOT_PROMPT
116 printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
119 # ifdef CONFIG_AUTOBOOT_DELAY_STR
120 if (delaykey[0].str == NULL)
121 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
123 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
124 if (delaykey[1].str == NULL)
125 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
127 # ifdef CONFIG_AUTOBOOT_STOP_STR
128 if (delaykey[2].str == NULL)
129 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
131 # ifdef CONFIG_AUTOBOOT_STOP_STR2
132 if (delaykey[3].str == NULL)
133 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
136 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
137 delaykey[i].len = delaykey[i].str == NULL ?
138 0 : strlen (delaykey[i].str);
139 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
140 MAX_DELAY_STOP_STR : delaykey[i].len;
142 presskey_max = presskey_max > delaykey[i].len ?
143 presskey_max : delaykey[i].len;
146 printf("%s key:<%s>\n",
147 delaykey[i].retry ? "delay" : "stop",
148 delaykey[i].str ? delaykey[i].str : "NULL");
152 /* In order to keep up with incoming data, check timeout only
155 while (!abort && get_ticks() <= etime) {
156 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
157 if (delaykey[i].len > 0 &&
158 presskey_len >= delaykey[i].len &&
159 memcmp (presskey + presskey_len - delaykey[i].len,
161 delaykey[i].len) == 0) {
163 printf("got %skey\n",
164 delaykey[i].retry ? "delay" : "stop");
167 # ifdef CONFIG_BOOT_RETRY_TIME
168 /* don't retry auto boot */
169 if (! delaykey[i].retry)
177 if (presskey_len < presskey_max) {
178 presskey [presskey_len ++] = getc();
181 for (i = 0; i < presskey_max - 1; i ++)
182 presskey [i] = presskey [i + 1];
184 presskey [i] = getc();
190 puts ("key timeout\n");
193 #ifdef CONFIG_SILENT_CONSOLE
195 gd->flags &= ~GD_FLG_SILENT;
201 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
203 #ifdef CONFIG_MENUKEY
204 static int menukey = 0;
207 static __inline__ int abortboot(int bootdelay)
211 #ifdef CONFIG_MENUPROMPT
212 printf(CONFIG_MENUPROMPT, bootdelay);
214 printf("Hit any key to stop autoboot: %2d ", bootdelay);
217 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
219 * Check if key already pressed
220 * Don't check if bootdelay < 0
222 if (bootdelay >= 0) {
223 if (tstc()) { /* we got a key press */
224 (void) getc(); /* consume input */
226 abort = 1; /* don't auto boot */
231 while ((bootdelay > 0) && (!abort)) {
235 /* delay 100 * 10ms */
236 for (i=0; !abort && i<100; ++i) {
237 if (tstc()) { /* we got a key press */
238 abort = 1; /* don't auto boot */
239 bootdelay = 0; /* no more delay */
240 # ifdef CONFIG_MENUKEY
243 (void) getc(); /* consume input */
250 printf ("\b\b\b%2d ", bootdelay);
255 #ifdef CONFIG_SILENT_CONSOLE
257 gd->flags &= ~GD_FLG_SILENT;
262 # endif /* CONFIG_AUTOBOOT_KEYED */
263 #endif /* CONFIG_BOOTDELAY >= 0 */
265 /****************************************************************************/
267 void main_loop (void)
269 #ifndef CFG_HUSH_PARSER
270 static char lastcommand[CFG_CBSIZE] = { 0, };
276 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
280 #ifdef CONFIG_PREBOOT
283 #ifdef CONFIG_BOOTCOUNT_LIMIT
284 unsigned long bootcount = 0;
285 unsigned long bootlimit = 0;
288 #endif /* CONFIG_BOOTCOUNT_LIMIT */
290 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
291 ulong bmp = 0; /* default bitmap */
292 extern int trab_vfd (ulong bitmap);
294 #ifdef CONFIG_MODEM_SUPPORT
296 bmp = 1; /* alternate bitmap */
299 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
301 #ifdef CONFIG_BOOTCOUNT_LIMIT
302 bootcount = bootcount_load();
304 bootcount_store (bootcount);
305 sprintf (bcs_set, "%lu", bootcount);
306 setenv ("bootcount", bcs_set);
307 bcs = getenv ("bootlimit");
308 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
309 #endif /* CONFIG_BOOTCOUNT_LIMIT */
311 #ifdef CONFIG_MODEM_SUPPORT
312 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
314 char *str = strdup(getenv("mdm_cmd"));
315 setenv ("preboot", str); /* set or delete definition */
318 mdm_init(); /* wait for modem connection */
320 #endif /* CONFIG_MODEM_SUPPORT */
322 #ifdef CONFIG_VERSION_VARIABLE
324 extern char version_string[];
326 setenv ("ver", version_string); /* set version variable */
328 #endif /* CONFIG_VERSION_VARIABLE */
330 #ifdef CFG_HUSH_PARSER
331 u_boot_hush_start ();
334 #ifdef CONFIG_AUTO_COMPLETE
335 install_auto_complete();
338 #ifdef CONFIG_PREBOOT
339 if ((p = getenv ("preboot")) != NULL) {
340 # ifdef CONFIG_AUTOBOOT_KEYED
341 int prev = disable_ctrlc(1); /* disable Control C checking */
344 # ifndef CFG_HUSH_PARSER
347 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
348 FLAG_EXIT_FROM_LOOP);
351 # ifdef CONFIG_AUTOBOOT_KEYED
352 disable_ctrlc(prev); /* restore Control C checking */
355 #endif /* CONFIG_PREBOOT */
357 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
358 s = getenv ("bootdelay");
359 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
361 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
363 # ifdef CONFIG_BOOT_RETRY_TIME
365 # endif /* CONFIG_BOOT_RETRY_TIME */
367 #ifdef CONFIG_BOOTCOUNT_LIMIT
368 if (bootlimit && (bootcount > bootlimit)) {
369 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
370 (unsigned)bootlimit);
371 s = getenv ("altbootcmd");
374 #endif /* CONFIG_BOOTCOUNT_LIMIT */
375 s = getenv ("bootcmd");
377 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
379 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
380 # ifdef CONFIG_AUTOBOOT_KEYED
381 int prev = disable_ctrlc(1); /* disable Control C checking */
384 # ifndef CFG_HUSH_PARSER
387 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
388 FLAG_EXIT_FROM_LOOP);
391 # ifdef CONFIG_AUTOBOOT_KEYED
392 disable_ctrlc(prev); /* restore Control C checking */
396 # ifdef CONFIG_MENUKEY
397 if (menukey == CONFIG_MENUKEY) {
398 s = getenv("menucmd");
400 # ifndef CFG_HUSH_PARSER
403 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
404 FLAG_EXIT_FROM_LOOP);
408 #endif /* CONFIG_MENUKEY */
409 #endif /* CONFIG_BOOTDELAY */
411 #ifdef CONFIG_AMIGAONEG3SE
413 extern void video_banner(void);
419 * Main Loop for Monitor Command Processing
421 #ifdef CFG_HUSH_PARSER
423 /* This point is never reached */
427 #ifdef CONFIG_BOOT_RETRY_TIME
429 /* Saw enough of a valid command to
430 * restart the timeout.
435 len = readline (CFG_PROMPT);
437 flag = 0; /* assume no special flags for now */
439 strcpy (lastcommand, console_buffer);
441 flag |= CMD_FLAG_REPEAT;
442 #ifdef CONFIG_BOOT_RETRY_TIME
443 else if (len == -2) {
444 /* -2 means timed out, retry autoboot
446 puts ("\nTimed out waiting for command\n");
447 # ifdef CONFIG_RESET_TO_RETRY
448 /* Reinit board to run initialization code again */
449 do_reset (NULL, 0, 0, NULL);
451 return; /* retry autoboot */
457 puts ("<INTERRUPT>\n");
459 rc = run_command (lastcommand, flag);
462 /* invalid command or not repeatable, forget it */
466 #endif /*CFG_HUSH_PARSER*/
469 #ifdef CONFIG_BOOT_RETRY_TIME
470 /***************************************************************************
471 * initialize command line timeout
473 void init_cmd_timeout(void)
475 char *s = getenv ("bootretry");
478 retry_time = (int)simple_strtol(s, NULL, 10);
480 retry_time = CONFIG_BOOT_RETRY_TIME;
482 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
483 retry_time = CONFIG_BOOT_RETRY_MIN;
486 /***************************************************************************
487 * reset command line timeout to retry_time seconds
489 void reset_cmd_timeout(void)
491 endtime = endtick(retry_time);
495 #ifdef CONFIG_CMDLINE_EDITING
498 * cmdline-editing related codes from vivi.
499 * Author: Janghoon Lyu <nandy@mizi.com>
502 #define putnstr(str,n) do { \
503 printf ("%.*s", n, str); \
506 #define CTL_CH(c) ((c) - 'a' + 1)
508 #define MAX_CMDBUF_SIZE 256
510 #define CTL_BACKSPACE ('\b')
511 #define DEL ((char)255)
512 #define DEL7 ((char)127)
513 #define CREAD_HIST_CHAR ('!')
515 #define getcmd_putch(ch) putc(ch)
516 #define getcmd_getch() getc()
517 #define getcmd_cbeep() getcmd_putch('\a')
520 #define HIST_SIZE MAX_CMDBUF_SIZE
522 static int hist_max = 0;
523 static int hist_add_idx = 0;
524 static int hist_cur = -1;
525 unsigned hist_num = 0;
527 char* hist_list[HIST_MAX];
528 char hist_lines[HIST_MAX][HIST_SIZE];
530 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
532 static void hist_init(void)
541 for (i = 0; i < HIST_MAX; i++) {
542 hist_list[i] = hist_lines[i];
543 hist_list[i][0] = '\0';
547 static void cread_add_to_hist(char *line)
549 strcpy(hist_list[hist_add_idx], line);
551 if (++hist_add_idx >= HIST_MAX)
554 if (hist_add_idx > hist_max)
555 hist_max = hist_add_idx;
560 static char* hist_prev(void)
572 if (hist_cur == hist_add_idx) {
576 ret = hist_list[hist_cur];
581 static char* hist_next(void)
588 if (hist_cur == hist_add_idx)
591 if (++hist_cur > hist_max)
594 if (hist_cur == hist_add_idx) {
597 ret = hist_list[hist_cur];
602 #ifndef CONFIG_CMDLINE_EDITING
603 static void cread_print_hist_list(void)
608 n = hist_num - hist_max;
610 i = hist_add_idx + 1;
614 if (i == hist_add_idx)
616 printf("%s\n", hist_list[i]);
621 #endif /* CONFIG_CMDLINE_EDITING */
623 #define BEGINNING_OF_LINE() { \
625 getcmd_putch(CTL_BACKSPACE); \
630 #define ERASE_TO_EOL() { \
631 if (num < eol_num) { \
633 for (tmp = num; tmp < eol_num; tmp++) \
635 while (tmp-- > num) \
636 getcmd_putch(CTL_BACKSPACE); \
641 #define REFRESH_TO_EOL() { \
642 if (num < eol_num) { \
643 wlen = eol_num - num; \
644 putnstr(buf + num, wlen); \
649 static void cread_add_char(char ichar, int insert, unsigned long *num,
650 unsigned long *eol_num, char *buf, unsigned long len)
655 if (insert || *num == *eol_num) {
656 if (*eol_num > len - 1) {
664 wlen = *eol_num - *num;
666 memmove(&buf[*num+1], &buf[*num], wlen-1);
670 putnstr(buf + *num, wlen);
673 getcmd_putch(CTL_BACKSPACE);
676 /* echo the character */
679 putnstr(buf + *num, wlen);
684 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
685 unsigned long *eol_num, char *buf, unsigned long len)
688 cread_add_char(*str, insert, num, eol_num, buf, len);
693 static int cread_line(char *buf, unsigned int *len)
695 unsigned long num = 0;
696 unsigned long eol_num = 0;
707 ichar = getcmd_getch();
709 if ((ichar == '\n') || (ichar == '\r')) {
715 * handle standard linux xterm esc sequences for arrow key, etc.
720 esc_save[esc_len] = ichar;
723 cread_add_str(esc_save, esc_len, insert,
724 &num, &eol_num, buf, *len);
732 case 'D': /* <- key */
736 case 'C': /* -> key */
739 break; /* pass off to ^F handler */
740 case 'H': /* Home key */
743 break; /* pass off to ^A handler */
744 case 'A': /* up arrow */
747 break; /* pass off to ^P handler */
748 case 'B': /* down arrow */
751 break; /* pass off to ^N handler */
753 esc_save[esc_len++] = ichar;
754 cread_add_str(esc_save, esc_len, insert,
755 &num, &eol_num, buf, *len);
764 esc_save[esc_len] = ichar;
767 puts("impossible condition #876\n");
775 case CTL_CH('c'): /* ^C - break */
776 *buf = '\0'; /* discard input */
780 getcmd_putch(buf[num]);
786 getcmd_putch(CTL_BACKSPACE);
792 wlen = eol_num - num - 1;
794 memmove(&buf[num], &buf[num+1], wlen);
795 putnstr(buf + num, wlen);
800 getcmd_putch(CTL_BACKSPACE);
822 wlen = eol_num - num;
824 memmove(&buf[num], &buf[num+1], wlen);
825 getcmd_putch(CTL_BACKSPACE);
826 putnstr(buf + num, wlen);
829 getcmd_putch(CTL_BACKSPACE);
841 if (ichar == CTL_CH('p'))
851 /* nuke the current line */
855 /* erase to end of line */
858 /* copy new line into place and display */
860 eol_num = strlen(buf);
865 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
870 buf[eol_num] = '\0'; /* lose the newline */
872 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
873 cread_add_to_hist(buf);
874 hist_cur = hist_add_idx;
879 #endif /* CONFIG_CMDLINE_EDITING */
881 /****************************************************************************/
884 * Prompt for input and read a line.
885 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
886 * time out when time goes past endtime (timebase time in ticks).
887 * Return: number of read characters
891 int readline (const char *const prompt)
893 #ifdef CONFIG_CMDLINE_EDITING
894 char *p = console_buffer;
895 unsigned int len=MAX_CMDBUF_SIZE;
897 static int initted = 0;
906 rc = cread_line(p, &len);
907 return rc < 0 ? rc : len;
909 char *p = console_buffer;
910 int n = 0; /* buffer index */
911 int plen = 0; /* prompt length */
912 int col; /* output column cnt */
917 plen = strlen (prompt);
923 #ifdef CONFIG_BOOT_RETRY_TIME
924 while (!tstc()) { /* while no incoming data */
925 if (retry_time >= 0 && get_ticks() > endtime)
926 return (-2); /* timed out */
929 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
931 #ifdef CONFIG_SHOW_ACTIVITY
933 extern void show_activity(int arg);
940 * Special character handling
943 case '\r': /* Enter */
947 return (p - console_buffer);
952 case 0x03: /* ^C - break */
953 console_buffer[0] = '\0'; /* discard input */
956 case 0x15: /* ^U - erase line */
965 case 0x17: /* ^W - erase word */
966 p=delete_char(console_buffer, p, &col, &n, plen);
967 while ((n > 0) && (*p != ' ')) {
968 p=delete_char(console_buffer, p, &col, &n, plen);
972 case 0x08: /* ^H - backspace */
973 case 0x7F: /* DEL - backspace */
974 p=delete_char(console_buffer, p, &col, &n, plen);
979 * Must be a normal character then
981 if (n < CFG_CBSIZE-2) {
982 if (c == '\t') { /* expand TABs */
983 #ifdef CONFIG_AUTO_COMPLETE
984 /* if auto completion triggered just continue */
986 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
987 p = console_buffer + n; /* reset */
991 puts (tab_seq+(col&07));
994 ++col; /* echo input */
999 } else { /* Buffer full */
1004 #endif /* CONFIG_CMDLINE_EDITING */
1007 /****************************************************************************/
1009 #ifndef CONFIG_CMDLINE_EDITING
1010 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1018 if (*(--p) == '\t') { /* will retype the whole line */
1019 while (*colp > plen) {
1023 for (s=buffer; s<p; ++s) {
1025 puts (tab_seq+((*colp) & 07));
1026 *colp += 8 - ((*colp) & 07);
1039 #endif /* CONFIG_CMDLINE_EDITING */
1041 /****************************************************************************/
1043 int parse_line (char *line, char *argv[])
1048 printf ("parse_line: \"%s\"\n", line);
1050 while (nargs < CFG_MAXARGS) {
1052 /* skip any white space */
1053 while ((*line == ' ') || (*line == '\t')) {
1057 if (*line == '\0') { /* end of line, no more args */
1060 printf ("parse_line: nargs=%d\n", nargs);
1065 argv[nargs++] = line; /* begin of argument string */
1067 /* find end of string */
1068 while (*line && (*line != ' ') && (*line != '\t')) {
1072 if (*line == '\0') { /* end of line, no more args */
1075 printf ("parse_line: nargs=%d\n", nargs);
1080 *line++ = '\0'; /* terminate current arg */
1083 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1086 printf ("parse_line: nargs=%d\n", nargs);
1091 /****************************************************************************/
1093 static void process_macros (const char *input, char *output)
1096 const char *varname_start = NULL;
1097 int inputcnt = strlen (input);
1098 int outputcnt = CFG_CBSIZE;
1099 int state = 0; /* 0 = waiting for '$' */
1101 /* 1 = waiting for '(' or '{' */
1102 /* 2 = waiting for ')' or '}' */
1103 /* 3 = waiting for ''' */
1105 char *output_start = output;
1107 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1111 prev = '\0'; /* previous character */
1113 while (inputcnt && outputcnt) {
1118 /* remove one level of escape characters */
1119 if ((c == '\\') && (prev != '\\')) {
1120 if (inputcnt-- == 0)
1128 case 0: /* Waiting for (unescaped) $ */
1129 if ((c == '\'') && (prev != '\\')) {
1133 if ((c == '$') && (prev != '\\')) {
1140 case 1: /* Waiting for ( */
1141 if (c == '(' || c == '{') {
1143 varname_start = input;
1155 case 2: /* Waiting for ) */
1156 if (c == ')' || c == '}') {
1158 char envname[CFG_CBSIZE], *envval;
1159 int envcnt = input - varname_start - 1; /* Varname # of chars */
1161 /* Get the varname */
1162 for (i = 0; i < envcnt; i++) {
1163 envname[i] = varname_start[i];
1168 envval = getenv (envname);
1170 /* Copy into the line if it exists */
1172 while ((*envval) && outputcnt) {
1173 *(output++) = *(envval++);
1176 /* Look for another '$' */
1180 case 3: /* Waiting for ' */
1181 if ((c == '\'') && (prev != '\\')) {
1196 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1197 strlen (output_start), output_start);
1201 /****************************************************************************
1203 * 1 - command executed, repeatable
1204 * 0 - command executed but not repeatable, interrupted commands are
1205 * always considered not repeatable
1206 * -1 - not executed (unrecognized, bootd recursion or too many args)
1207 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1208 * considered unrecognized)
1212 * We must create a temporary copy of the command since the command we get
1213 * may be the result from getenv(), which returns a pointer directly to
1214 * the environment data, which may change magicly when the command we run
1215 * creates or modifies environment variables (like "bootp" does).
1218 int run_command (const char *cmd, int flag)
1221 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1222 char *token; /* start of token in cmdbuf */
1223 char *sep; /* end of token (separator) in cmdbuf */
1224 char finaltoken[CFG_CBSIZE];
1226 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
1232 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1233 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1237 clear_ctrlc(); /* forget any previous Control C */
1239 if (!cmd || !*cmd) {
1240 return -1; /* empty command */
1243 if (strlen(cmd) >= CFG_CBSIZE) {
1244 puts ("## Command too long!\n");
1248 strcpy (cmdbuf, cmd);
1250 /* Process separators and check for invalid
1251 * repeatable commands
1255 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1260 * Find separator, or string end
1261 * Allow simple escape of ';' by writing "\;"
1263 for (inquotes = 0, sep = str; *sep; sep++) {
1269 (*sep == ';') && /* separator */
1270 ( sep != str) && /* past string start */
1271 (*(sep-1) != '\\')) /* and NOT escaped */
1276 * Limit the token to data between separators
1280 str = sep + 1; /* start of command for next pass */
1284 str = sep; /* no more commands for next pass */
1286 printf ("token: \"%s\"\n", token);
1289 /* find macros in this token and replace them */
1290 process_macros (token, finaltoken);
1292 /* Extract arguments */
1293 if ((argc = parse_line (finaltoken, argv)) == 0) {
1294 rc = -1; /* no command at all */
1298 /* Look up command in command table */
1299 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1300 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
1301 rc = -1; /* give up after bad command */
1305 /* found - check max args */
1306 if (argc > cmdtp->maxargs) {
1307 printf ("Usage:\n%s\n", cmdtp->usage);
1312 #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1313 /* avoid "bootd" recursion */
1314 if (cmdtp->cmd == do_bootd) {
1316 printf ("[%s]\n", finaltoken);
1318 if (flag & CMD_FLAG_BOOTD) {
1319 puts ("'bootd' recursion detected\n");
1323 flag |= CMD_FLAG_BOOTD;
1326 #endif /* CFG_CMD_BOOTD */
1328 /* OK - call function to do the command */
1329 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
1333 repeatable &= cmdtp->repeatable;
1335 /* Did the user stop this? */
1337 return 0; /* if stopped then not repeatable */
1340 return rc ? rc : repeatable;
1343 /****************************************************************************/
1345 #if (CONFIG_COMMANDS & CFG_CMD_RUN)
1346 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1351 printf ("Usage:\n%s\n", cmdtp->usage);
1355 for (i=1; i<argc; ++i) {
1358 if ((arg = getenv (argv[i])) == NULL) {
1359 printf ("## Error: \"%s\" not defined\n", argv[i]);
1362 #ifndef CFG_HUSH_PARSER
1363 if (run_command (arg, flag) == -1)
1366 if (parse_string_outer(arg,
1367 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
1373 #endif /* CFG_CMD_RUN */