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,
34 #ifdef CONFIG_MODEM_SUPPORT
35 #include <malloc.h> /* for free() prototype */
38 #ifdef CONFIG_SYS_HUSH_PARSER
44 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
45 DECLARE_GLOBAL_DATA_PTR
;
49 * Board-specific Platform code can reimplement show_boot_progress () if needed
51 void inline __show_boot_progress (int val
) {}
52 void show_boot_progress (int val
) __attribute__((weak
, alias("__show_boot_progress")));
54 #if defined(CONFIG_UPDATE_TFTP)
55 int update_tftp (ulong addr
);
56 #endif /* CONFIG_UPDATE_TFTP */
58 #define MAX_DELAY_STOP_STR 32
62 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
64 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
);
65 static const char erase_seq
[] = "\b \b"; /* erase sequence */
66 static const char tab_seq
[] = " "; /* used to expand TABs */
68 #ifdef CONFIG_BOOT_RETRY_TIME
69 static uint64_t endtime
= 0; /* must be set, default is instant timeout */
70 static int retry_time
= -1; /* -1 so can call readline before main_loop */
73 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
75 #ifndef CONFIG_BOOT_RETRY_MIN
76 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
79 #ifdef CONFIG_MODEM_SUPPORT
81 extern void mdm_init(void); /* defined in board.c */
84 /***************************************************************************
85 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
86 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
88 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
89 # if defined(CONFIG_AUTOBOOT_KEYED)
93 int abortboot(int bootdelay
)
96 uint64_t etime
= endtick(bootdelay
);
103 { str
: getenv ("bootdelaykey"), retry
: 1 },
104 { str
: getenv ("bootdelaykey2"), retry
: 1 },
105 { str
: getenv ("bootstopkey"), retry
: 0 },
106 { str
: getenv ("bootstopkey2"), retry
: 0 },
109 char presskey
[MAX_DELAY_STOP_STR
];
110 u_int presskey_len
= 0;
111 u_int presskey_max
= 0;
114 # ifdef CONFIG_AUTOBOOT_PROMPT
115 printf(CONFIG_AUTOBOOT_PROMPT
);
118 # ifdef CONFIG_AUTOBOOT_DELAY_STR
119 if (delaykey
[0].str
== NULL
)
120 delaykey
[0].str
= CONFIG_AUTOBOOT_DELAY_STR
;
122 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
123 if (delaykey
[1].str
== NULL
)
124 delaykey
[1].str
= CONFIG_AUTOBOOT_DELAY_STR2
;
126 # ifdef CONFIG_AUTOBOOT_STOP_STR
127 if (delaykey
[2].str
== NULL
)
128 delaykey
[2].str
= CONFIG_AUTOBOOT_STOP_STR
;
130 # ifdef CONFIG_AUTOBOOT_STOP_STR2
131 if (delaykey
[3].str
== NULL
)
132 delaykey
[3].str
= CONFIG_AUTOBOOT_STOP_STR2
;
135 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
136 delaykey
[i
].len
= delaykey
[i
].str
== NULL
?
137 0 : strlen (delaykey
[i
].str
);
138 delaykey
[i
].len
= delaykey
[i
].len
> MAX_DELAY_STOP_STR
?
139 MAX_DELAY_STOP_STR
: delaykey
[i
].len
;
141 presskey_max
= presskey_max
> delaykey
[i
].len
?
142 presskey_max
: delaykey
[i
].len
;
145 printf("%s key:<%s>\n",
146 delaykey
[i
].retry
? "delay" : "stop",
147 delaykey
[i
].str
? delaykey
[i
].str
: "NULL");
151 /* In order to keep up with incoming data, check timeout only
156 if (presskey_len
< presskey_max
) {
157 presskey
[presskey_len
++] = getc();
160 for (i
= 0; i
< presskey_max
- 1; i
++)
161 presskey
[i
] = presskey
[i
+ 1];
163 presskey
[i
] = getc();
167 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
168 if (delaykey
[i
].len
> 0 &&
169 presskey_len
>= delaykey
[i
].len
&&
170 memcmp (presskey
+ presskey_len
- delaykey
[i
].len
,
172 delaykey
[i
].len
) == 0) {
174 printf("got %skey\n",
175 delaykey
[i
].retry
? "delay" : "stop");
178 # ifdef CONFIG_BOOT_RETRY_TIME
179 /* don't retry auto boot */
180 if (! delaykey
[i
].retry
)
186 } while (!abort
&& get_ticks() <= etime
);
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;
210 int abortboot(int bootdelay
)
214 #ifdef CONFIG_MENUPROMPT
215 printf(CONFIG_MENUPROMPT
);
217 printf("Hit any key to stop autoboot: %2d ", bootdelay
);
220 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
222 * Check if key already pressed
223 * Don't check if bootdelay < 0
225 if (bootdelay
>= 0) {
226 if (tstc()) { /* we got a key press */
227 (void) getc(); /* consume input */
229 abort
= 1; /* don't auto boot */
234 while ((bootdelay
> 0) && (!abort
)) {
238 /* delay 100 * 10ms */
239 for (i
=0; !abort
&& i
<100; ++i
) {
240 if (tstc()) { /* we got a key press */
241 abort
= 1; /* don't auto boot */
242 bootdelay
= 0; /* no more delay */
243 # ifdef CONFIG_MENUKEY
246 (void) getc(); /* consume input */
253 printf("\b\b\b%2d ", bootdelay
);
258 #ifdef CONFIG_SILENT_CONSOLE
260 gd
->flags
&= ~GD_FLG_SILENT
;
265 # endif /* CONFIG_AUTOBOOT_KEYED */
266 #endif /* CONFIG_BOOTDELAY >= 0 */
269 * Return 0 on success, or != 0 on error.
272 int run_command2(const char *cmd
, int flag
)
274 #ifndef CONFIG_SYS_HUSH_PARSER
276 * run_command can return 0 or 1 for success, so clean up its result.
278 if (run_command(cmd
, flag
) == -1)
283 return parse_string_outer(cmd
,
284 FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP
);
288 /****************************************************************************/
290 void main_loop (void)
292 #ifndef CONFIG_SYS_HUSH_PARSER
293 static char lastcommand
[CONFIG_SYS_CBSIZE
] = { 0, };
299 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
303 #ifdef CONFIG_PREBOOT
306 #ifdef CONFIG_BOOTCOUNT_LIMIT
307 unsigned long bootcount
= 0;
308 unsigned long bootlimit
= 0;
311 #endif /* CONFIG_BOOTCOUNT_LIMIT */
313 #ifdef CONFIG_BOOTCOUNT_LIMIT
314 bootcount
= bootcount_load();
316 bootcount_store (bootcount
);
317 sprintf (bcs_set
, "%lu", bootcount
);
318 setenv ("bootcount", bcs_set
);
319 bcs
= getenv ("bootlimit");
320 bootlimit
= bcs
? simple_strtoul (bcs
, NULL
, 10) : 0;
321 #endif /* CONFIG_BOOTCOUNT_LIMIT */
323 #ifdef CONFIG_MODEM_SUPPORT
324 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init
);
326 char *str
= strdup(getenv("mdm_cmd"));
327 setenv ("preboot", str
); /* set or delete definition */
330 mdm_init(); /* wait for modem connection */
332 #endif /* CONFIG_MODEM_SUPPORT */
334 #ifdef CONFIG_VERSION_VARIABLE
336 setenv ("ver", version_string
); /* set version variable */
338 #endif /* CONFIG_VERSION_VARIABLE */
340 #ifdef CONFIG_SYS_HUSH_PARSER
341 u_boot_hush_start ();
344 #if defined(CONFIG_HUSH_INIT_VAR)
348 #ifdef CONFIG_PREBOOT
349 if ((p
= getenv ("preboot")) != NULL
) {
350 # ifdef CONFIG_AUTOBOOT_KEYED
351 int prev
= disable_ctrlc(1); /* disable Control C checking */
356 # ifdef CONFIG_AUTOBOOT_KEYED
357 disable_ctrlc(prev
); /* restore Control C checking */
360 #endif /* CONFIG_PREBOOT */
362 #if defined(CONFIG_UPDATE_TFTP)
364 #endif /* CONFIG_UPDATE_TFTP */
366 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
367 s
= getenv ("bootdelay");
368 bootdelay
= s
? (int)simple_strtol(s
, NULL
, 10) : CONFIG_BOOTDELAY
;
370 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay
);
372 # ifdef CONFIG_BOOT_RETRY_TIME
374 # endif /* CONFIG_BOOT_RETRY_TIME */
377 if (gd
->flags
& GD_FLG_POSTFAIL
) {
378 s
= getenv("failbootcmd");
381 #endif /* CONFIG_POST */
382 #ifdef CONFIG_BOOTCOUNT_LIMIT
383 if (bootlimit
&& (bootcount
> bootlimit
)) {
384 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
385 (unsigned)bootlimit
);
386 s
= getenv ("altbootcmd");
389 #endif /* CONFIG_BOOTCOUNT_LIMIT */
390 s
= getenv ("bootcmd");
392 debug ("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
394 if (bootdelay
>= 0 && s
&& !abortboot (bootdelay
)) {
395 # ifdef CONFIG_AUTOBOOT_KEYED
396 int prev
= disable_ctrlc(1); /* disable Control C checking */
401 # ifdef CONFIG_AUTOBOOT_KEYED
402 disable_ctrlc(prev
); /* restore Control C checking */
406 # ifdef CONFIG_MENUKEY
407 if (menukey
== CONFIG_MENUKEY
) {
408 s
= getenv("menucmd");
412 #endif /* CONFIG_MENUKEY */
413 #endif /* CONFIG_BOOTDELAY */
416 * Main Loop for Monitor Command Processing
418 #ifdef CONFIG_SYS_HUSH_PARSER
420 /* This point is never reached */
424 #ifdef CONFIG_BOOT_RETRY_TIME
426 /* Saw enough of a valid command to
427 * restart the timeout.
432 len
= readline (CONFIG_SYS_PROMPT
);
434 flag
= 0; /* assume no special flags for now */
436 strcpy (lastcommand
, console_buffer
);
438 flag
|= CMD_FLAG_REPEAT
;
439 #ifdef CONFIG_BOOT_RETRY_TIME
440 else if (len
== -2) {
441 /* -2 means timed out, retry autoboot
443 puts ("\nTimed out waiting for command\n");
444 # ifdef CONFIG_RESET_TO_RETRY
445 /* Reinit board to run initialization code again */
446 do_reset (NULL
, 0, 0, NULL
);
448 return; /* retry autoboot */
454 puts ("<INTERRUPT>\n");
456 rc
= run_command (lastcommand
, flag
);
459 /* invalid command or not repeatable, forget it */
463 #endif /*CONFIG_SYS_HUSH_PARSER*/
466 #ifdef CONFIG_BOOT_RETRY_TIME
467 /***************************************************************************
468 * initialize command line timeout
470 void init_cmd_timeout(void)
472 char *s
= getenv ("bootretry");
475 retry_time
= (int)simple_strtol(s
, NULL
, 10);
477 retry_time
= CONFIG_BOOT_RETRY_TIME
;
479 if (retry_time
>= 0 && retry_time
< CONFIG_BOOT_RETRY_MIN
)
480 retry_time
= CONFIG_BOOT_RETRY_MIN
;
483 /***************************************************************************
484 * reset command line timeout to retry_time seconds
486 void reset_cmd_timeout(void)
488 endtime
= endtick(retry_time
);
492 #ifdef CONFIG_CMDLINE_EDITING
495 * cmdline-editing related codes from vivi.
496 * Author: Janghoon Lyu <nandy@mizi.com>
499 #define putnstr(str,n) do { \
500 printf ("%.*s", (int)n, str); \
503 #define CTL_CH(c) ((c) - 'a' + 1)
504 #define CTL_BACKSPACE ('\b')
505 #define DEL ((char)255)
506 #define DEL7 ((char)127)
507 #define CREAD_HIST_CHAR ('!')
509 #define getcmd_putch(ch) putc(ch)
510 #define getcmd_getch() getc()
511 #define getcmd_cbeep() getcmd_putch('\a')
514 #define HIST_SIZE CONFIG_SYS_CBSIZE
516 static int hist_max
= 0;
517 static int hist_add_idx
= 0;
518 static int hist_cur
= -1;
519 unsigned hist_num
= 0;
521 char* hist_list
[HIST_MAX
];
522 char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
524 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
526 static void hist_init(void)
535 for (i
= 0; i
< HIST_MAX
; i
++) {
536 hist_list
[i
] = hist_lines
[i
];
537 hist_list
[i
][0] = '\0';
541 static void cread_add_to_hist(char *line
)
543 strcpy(hist_list
[hist_add_idx
], line
);
545 if (++hist_add_idx
>= HIST_MAX
)
548 if (hist_add_idx
> hist_max
)
549 hist_max
= hist_add_idx
;
554 static char* hist_prev(void)
566 if (hist_cur
== hist_add_idx
) {
570 ret
= hist_list
[hist_cur
];
575 static char* hist_next(void)
582 if (hist_cur
== hist_add_idx
)
585 if (++hist_cur
> hist_max
)
588 if (hist_cur
== hist_add_idx
) {
591 ret
= hist_list
[hist_cur
];
596 #ifndef CONFIG_CMDLINE_EDITING
597 static void cread_print_hist_list(void)
602 n
= hist_num
- hist_max
;
604 i
= hist_add_idx
+ 1;
608 if (i
== hist_add_idx
)
610 printf("%s\n", hist_list
[i
]);
615 #endif /* CONFIG_CMDLINE_EDITING */
617 #define BEGINNING_OF_LINE() { \
619 getcmd_putch(CTL_BACKSPACE); \
624 #define ERASE_TO_EOL() { \
625 if (num < eol_num) { \
626 printf("%*s", (int)(eol_num - num), ""); \
628 getcmd_putch(CTL_BACKSPACE); \
629 } while (--eol_num > num); \
633 #define REFRESH_TO_EOL() { \
634 if (num < eol_num) { \
635 wlen = eol_num - num; \
636 putnstr(buf + num, wlen); \
641 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
642 unsigned long *eol_num
, char *buf
, unsigned long len
)
647 if (insert
|| *num
== *eol_num
) {
648 if (*eol_num
> len
- 1) {
656 wlen
= *eol_num
- *num
;
658 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
662 putnstr(buf
+ *num
, wlen
);
665 getcmd_putch(CTL_BACKSPACE
);
668 /* echo the character */
671 putnstr(buf
+ *num
, wlen
);
676 static void cread_add_str(char *str
, int strsize
, int insert
, unsigned long *num
,
677 unsigned long *eol_num
, char *buf
, unsigned long len
)
680 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
685 static int cread_line(const char *const prompt
, char *buf
, unsigned int *len
)
687 unsigned long num
= 0;
688 unsigned long eol_num
= 0;
694 int init_len
= strlen(buf
);
697 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
700 #ifdef CONFIG_BOOT_RETRY_TIME
701 while (!tstc()) { /* while no incoming data */
702 if (retry_time
>= 0 && get_ticks() > endtime
)
703 return (-2); /* timed out */
708 ichar
= getcmd_getch();
710 if ((ichar
== '\n') || (ichar
== '\r')) {
716 * handle standard linux xterm esc sequences for arrow key, etc.
721 esc_save
[esc_len
] = ichar
;
724 cread_add_str(esc_save
, esc_len
, insert
,
725 &num
, &eol_num
, buf
, *len
);
733 case 'D': /* <- key */
737 case 'C': /* -> key */
740 break; /* pass off to ^F handler */
741 case 'H': /* Home key */
744 break; /* pass off to ^A handler */
745 case 'A': /* up arrow */
748 break; /* pass off to ^P handler */
749 case 'B': /* down arrow */
752 break; /* pass off to ^N handler */
754 esc_save
[esc_len
++] = ichar
;
755 cread_add_str(esc_save
, esc_len
, insert
,
756 &num
, &eol_num
, buf
, *len
);
765 esc_save
[esc_len
] = ichar
;
768 puts("impossible condition #876\n");
776 case CTL_CH('c'): /* ^C - break */
777 *buf
= '\0'; /* discard input */
781 getcmd_putch(buf
[num
]);
787 getcmd_putch(CTL_BACKSPACE
);
793 wlen
= eol_num
- num
- 1;
795 memmove(&buf
[num
], &buf
[num
+1], wlen
);
796 putnstr(buf
+ num
, wlen
);
801 getcmd_putch(CTL_BACKSPACE
);
824 wlen
= eol_num
- num
;
826 memmove(&buf
[num
], &buf
[num
+1], wlen
);
827 getcmd_putch(CTL_BACKSPACE
);
828 putnstr(buf
+ num
, wlen
);
831 getcmd_putch(CTL_BACKSPACE
);
843 if (ichar
== CTL_CH('p'))
853 /* nuke the current line */
857 /* erase to end of line */
860 /* copy new line into place and display */
862 eol_num
= strlen(buf
);
866 #ifdef CONFIG_AUTO_COMPLETE
870 /* do not autocomplete when in the middle */
877 col
= strlen(prompt
) + eol_num
;
879 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
888 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, *len
);
893 buf
[eol_num
] = '\0'; /* lose the newline */
895 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
896 cread_add_to_hist(buf
);
897 hist_cur
= hist_add_idx
;
902 #endif /* CONFIG_CMDLINE_EDITING */
904 /****************************************************************************/
907 * Prompt for input and read a line.
908 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
909 * time out when time goes past endtime (timebase time in ticks).
910 * Return: number of read characters
914 int readline (const char *const prompt
)
917 * If console_buffer isn't 0-length the user will be prompted to modify
918 * it instead of entering it from scratch as desired.
920 console_buffer
[0] = '\0';
922 return readline_into_buffer(prompt
, console_buffer
);
926 int readline_into_buffer (const char *const prompt
, char * buffer
)
929 #ifdef CONFIG_CMDLINE_EDITING
930 unsigned int len
= CONFIG_SYS_CBSIZE
;
932 static int initted
= 0;
935 * History uses a global array which is not
936 * writable until after relocation to RAM.
937 * Revert to non-history version if still
938 * running from flash.
940 if (gd
->flags
& GD_FLG_RELOC
) {
949 rc
= cread_line(prompt
, p
, &len
);
950 return rc
< 0 ? rc
: len
;
953 #endif /* CONFIG_CMDLINE_EDITING */
955 int n
= 0; /* buffer index */
956 int plen
= 0; /* prompt length */
957 int col
; /* output column cnt */
962 plen
= strlen (prompt
);
968 #ifdef CONFIG_BOOT_RETRY_TIME
969 while (!tstc()) { /* while no incoming data */
970 if (retry_time
>= 0 && get_ticks() > endtime
)
971 return (-2); /* timed out */
975 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
977 #ifdef CONFIG_SHOW_ACTIVITY
979 extern void show_activity(int arg
);
987 * Special character handling
990 case '\r': /* Enter */
999 case 0x03: /* ^C - break */
1000 p_buf
[0] = '\0'; /* discard input */
1003 case 0x15: /* ^U - erase line */
1004 while (col
> plen
) {
1012 case 0x17: /* ^W - erase word */
1013 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1014 while ((n
> 0) && (*p
!= ' ')) {
1015 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1019 case 0x08: /* ^H - backspace */
1020 case 0x7F: /* DEL - backspace */
1021 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1026 * Must be a normal character then
1028 if (n
< CONFIG_SYS_CBSIZE
-2) {
1029 if (c
== '\t') { /* expand TABs */
1030 #ifdef CONFIG_AUTO_COMPLETE
1031 /* if auto completion triggered just continue */
1033 if (cmd_auto_complete(prompt
, console_buffer
, &n
, &col
)) {
1034 p
= p_buf
+ n
; /* reset */
1038 puts (tab_seq
+(col
&07));
1039 col
+= 8 - (col
&07);
1041 ++col
; /* echo input */
1046 } else { /* Buffer full */
1051 #ifdef CONFIG_CMDLINE_EDITING
1056 /****************************************************************************/
1058 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
1066 if (*(--p
) == '\t') { /* will retype the whole line */
1067 while (*colp
> plen
) {
1071 for (s
=buffer
; s
<p
; ++s
) {
1073 puts (tab_seq
+((*colp
) & 07));
1074 *colp
+= 8 - ((*colp
) & 07);
1088 /****************************************************************************/
1090 int parse_line (char *line
, char *argv
[])
1095 printf ("parse_line: \"%s\"\n", line
);
1097 while (nargs
< CONFIG_SYS_MAXARGS
) {
1099 /* skip any white space */
1100 while ((*line
== ' ') || (*line
== '\t'))
1103 if (*line
== '\0') { /* end of line, no more args */
1106 printf ("parse_line: nargs=%d\n", nargs
);
1111 argv
[nargs
++] = line
; /* begin of argument string */
1113 /* find end of string */
1114 while (*line
&& (*line
!= ' ') && (*line
!= '\t'))
1117 if (*line
== '\0') { /* end of line, no more args */
1120 printf ("parse_line: nargs=%d\n", nargs
);
1125 *line
++ = '\0'; /* terminate current arg */
1128 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS
);
1131 printf ("parse_line: nargs=%d\n", nargs
);
1136 /****************************************************************************/
1138 static void process_macros (const char *input
, char *output
)
1141 const char *varname_start
= NULL
;
1142 int inputcnt
= strlen (input
);
1143 int outputcnt
= CONFIG_SYS_CBSIZE
;
1144 int state
= 0; /* 0 = waiting for '$' */
1146 /* 1 = waiting for '(' or '{' */
1147 /* 2 = waiting for ')' or '}' */
1148 /* 3 = waiting for ''' */
1150 char *output_start
= output
;
1152 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input
),
1156 prev
= '\0'; /* previous character */
1158 while (inputcnt
&& outputcnt
) {
1163 /* remove one level of escape characters */
1164 if ((c
== '\\') && (prev
!= '\\')) {
1165 if (inputcnt
-- == 0)
1173 case 0: /* Waiting for (unescaped) $ */
1174 if ((c
== '\'') && (prev
!= '\\')) {
1178 if ((c
== '$') && (prev
!= '\\')) {
1185 case 1: /* Waiting for ( */
1186 if (c
== '(' || c
== '{') {
1188 varname_start
= input
;
1200 case 2: /* Waiting for ) */
1201 if (c
== ')' || c
== '}') {
1203 char envname
[CONFIG_SYS_CBSIZE
], *envval
;
1204 int envcnt
= input
- varname_start
- 1; /* Varname # of chars */
1206 /* Get the varname */
1207 for (i
= 0; i
< envcnt
; i
++) {
1208 envname
[i
] = varname_start
[i
];
1213 envval
= getenv (envname
);
1215 /* Copy into the line if it exists */
1217 while ((*envval
) && outputcnt
) {
1218 *(output
++) = *(envval
++);
1221 /* Look for another '$' */
1225 case 3: /* Waiting for ' */
1226 if ((c
== '\'') && (prev
!= '\\')) {
1243 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1244 strlen (output_start
), output_start
);
1248 /****************************************************************************
1250 * 1 - command executed, repeatable
1251 * 0 - command executed but not repeatable, interrupted commands are
1252 * always considered not repeatable
1253 * -1 - not executed (unrecognized, bootd recursion or too many args)
1254 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1255 * considered unrecognized)
1259 * We must create a temporary copy of the command since the command we get
1260 * may be the result from getenv(), which returns a pointer directly to
1261 * the environment data, which may change magicly when the command we run
1262 * creates or modifies environment variables (like "bootp" does).
1265 int run_command (const char *cmd
, int flag
)
1268 char cmdbuf
[CONFIG_SYS_CBSIZE
]; /* working copy of cmd */
1269 char *token
; /* start of token in cmdbuf */
1270 char *sep
; /* end of token (separator) in cmdbuf */
1271 char finaltoken
[CONFIG_SYS_CBSIZE
];
1273 char *argv
[CONFIG_SYS_MAXARGS
+ 1]; /* NULL terminated */
1279 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd
);
1280 puts (cmd
? cmd
: "NULL"); /* use puts - string may be loooong */
1284 clear_ctrlc(); /* forget any previous Control C */
1286 if (!cmd
|| !*cmd
) {
1287 return -1; /* empty command */
1290 if (strlen(cmd
) >= CONFIG_SYS_CBSIZE
) {
1291 puts ("## Command too long!\n");
1295 strcpy (cmdbuf
, cmd
);
1297 /* Process separators and check for invalid
1298 * repeatable commands
1302 printf ("[PROCESS_SEPARATORS] %s\n", cmd
);
1307 * Find separator, or string end
1308 * Allow simple escape of ';' by writing "\;"
1310 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
1316 (*sep
== ';') && /* separator */
1317 ( sep
!= str
) && /* past string start */
1318 (*(sep
-1) != '\\')) /* and NOT escaped */
1323 * Limit the token to data between separators
1327 str
= sep
+ 1; /* start of command for next pass */
1331 str
= sep
; /* no more commands for next pass */
1333 printf ("token: \"%s\"\n", token
);
1336 /* find macros in this token and replace them */
1337 process_macros (token
, finaltoken
);
1339 /* Extract arguments */
1340 if ((argc
= parse_line (finaltoken
, argv
)) == 0) {
1341 rc
= -1; /* no command at all */
1345 /* Look up command in command table */
1346 if ((cmdtp
= find_cmd(argv
[0])) == NULL
) {
1347 printf ("Unknown command '%s' - try 'help'\n", argv
[0]);
1348 rc
= -1; /* give up after bad command */
1352 /* found - check max args */
1353 if (argc
> cmdtp
->maxargs
) {
1359 #if defined(CONFIG_CMD_BOOTD)
1360 /* avoid "bootd" recursion */
1361 if (cmdtp
->cmd
== do_bootd
) {
1363 printf ("[%s]\n", finaltoken
);
1365 if (flag
& CMD_FLAG_BOOTD
) {
1366 puts ("'bootd' recursion detected\n");
1370 flag
|= CMD_FLAG_BOOTD
;
1375 /* OK - call function to do the command */
1376 if ((cmdtp
->cmd
) (cmdtp
, flag
, argc
, argv
) != 0) {
1380 repeatable
&= cmdtp
->repeatable
;
1382 /* Did the user stop this? */
1384 return -1; /* if stopped then not repeatable */
1387 return rc
? rc
: repeatable
;
1390 /****************************************************************************/
1392 #if defined(CONFIG_CMD_RUN)
1393 int do_run (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char * const argv
[])
1398 return cmd_usage(cmdtp
);
1400 for (i
=1; i
<argc
; ++i
) {
1403 if ((arg
= getenv (argv
[i
])) == NULL
) {
1404 printf ("## Error: \"%s\" not defined\n", argv
[i
]);
1408 if (run_command2(arg
, flag
) != 0)