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 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST)
44 DECLARE_GLOBAL_DATA_PTR
;
48 * Board-specific Platform code can reimplement show_boot_progress () if needed
50 #ifndef CONFIG_MINI2440
51 void inline __show_boot_progress (int val
) {}
52 void inline show_boot_progress (int val
) __attribute__((weak
, alias("__show_boot_progress")));
54 void inline show_boot_progress (int val
) {}
57 #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
58 extern int do_reset (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[]); /* for do_reset() prototype */
61 extern int do_bootd (cmd_tbl_t
*cmdtp
, int flag
, int argc
, char *argv
[]);
64 #define MAX_DELAY_STOP_STR 32
66 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
67 static int abortboot(int);
72 char console_buffer
[CFG_CBSIZE
]; /* console I/O buffer */
75 #ifndef CONFIG_CMDLINE_EDITING
76 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
);
77 static char erase_seq
[] = "\b \b"; /* erase sequence */
78 static char tab_seq
[] = " "; /* used to expand TABs */
79 #endif /* CONFIG_CMDLINE_EDITING */
81 #ifdef CONFIG_BOOT_RETRY_TIME
82 static uint64_t endtime
= 0; /* must be set, default is instant timeout */
83 static int retry_time
= -1; /* -1 so can call readline before main_loop */
86 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
88 #ifndef CONFIG_BOOT_RETRY_MIN
89 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
92 #ifdef CONFIG_MODEM_SUPPORT
94 extern void mdm_init(void); /* defined in board.c */
97 #ifdef CFG_PREBOOT_OVERRIDE
98 extern char *preboot_override
;
102 /***************************************************************************
103 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
104 * returns: 0 - no key string, allow autoboot
105 * 1 - got key string, abort
107 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
108 # if defined(CONFIG_AUTOBOOT_KEYED)
109 static __inline__
int abortboot(int bootdelay
)
112 uint64_t etime
= endtick(bootdelay
);
119 { str
: getenv ("bootdelaykey"), retry
: 1 },
120 { str
: getenv ("bootdelaykey2"), retry
: 1 },
121 { str
: getenv ("bootstopkey"), retry
: 0 },
122 { str
: getenv ("bootstopkey2"), retry
: 0 },
125 char presskey
[MAX_DELAY_STOP_STR
];
126 u_int presskey_len
= 0;
127 u_int presskey_max
= 0;
130 # ifdef CONFIG_AUTOBOOT_PROMPT
131 printf(CONFIG_AUTOBOOT_PROMPT
, bootdelay
);
134 # ifdef CONFIG_AUTOBOOT_DELAY_STR
135 if (delaykey
[0].str
== NULL
)
136 delaykey
[0].str
= CONFIG_AUTOBOOT_DELAY_STR
;
138 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
139 if (delaykey
[1].str
== NULL
)
140 delaykey
[1].str
= CONFIG_AUTOBOOT_DELAY_STR2
;
142 # ifdef CONFIG_AUTOBOOT_STOP_STR
143 if (delaykey
[2].str
== NULL
)
144 delaykey
[2].str
= CONFIG_AUTOBOOT_STOP_STR
;
146 # ifdef CONFIG_AUTOBOOT_STOP_STR2
147 if (delaykey
[3].str
== NULL
)
148 delaykey
[3].str
= CONFIG_AUTOBOOT_STOP_STR2
;
151 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
152 delaykey
[i
].len
= delaykey
[i
].str
== NULL
?
153 0 : strlen (delaykey
[i
].str
);
154 delaykey
[i
].len
= delaykey
[i
].len
> MAX_DELAY_STOP_STR
?
155 MAX_DELAY_STOP_STR
: delaykey
[i
].len
;
157 presskey_max
= presskey_max
> delaykey
[i
].len
?
158 presskey_max
: delaykey
[i
].len
;
161 printf("%s key:<%s>\n",
162 delaykey
[i
].retry
? "delay" : "stop",
163 delaykey
[i
].str
? delaykey
[i
].str
: "NULL");
167 /* In order to keep up with incoming data, check timeout only
170 while (!abort
&& get_ticks() <= etime
) {
171 for (i
= 0; i
< sizeof(delaykey
) / sizeof(delaykey
[0]); i
++) {
172 if (delaykey
[i
].len
> 0 &&
173 presskey_len
>= delaykey
[i
].len
&&
174 memcmp (presskey
+ presskey_len
- delaykey
[i
].len
,
176 delaykey
[i
].len
) == 0) {
178 printf("got %skey\n",
179 delaykey
[i
].retry
? "delay" : "stop");
182 # ifdef CONFIG_BOOT_RETRY_TIME
183 /* don't retry auto boot */
184 if (! delaykey
[i
].retry
)
192 if (presskey_len
< presskey_max
) {
193 presskey
[presskey_len
++] = getc();
196 for (i
= 0; i
< presskey_max
- 1; i
++)
197 presskey
[i
] = presskey
[i
+ 1];
199 presskey
[i
] = getc();
205 puts("key timeout\n");
208 #ifdef CONFIG_SILENT_CONSOLE
210 gd
->flags
&= ~GD_FLG_SILENT
;
216 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
218 #ifdef CONFIG_MENUKEY
219 static int menukey
= 0;
222 static __inline__
int abortboot(int bootdelay
)
226 #ifdef CONFIG_MENUPROMPT
227 printf(CONFIG_MENUPROMPT
, bootdelay
);
229 printf("Hit any key to stop autoboot: %2d ", bootdelay
);
232 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
234 * Check if key already pressed
235 * Don't check if bootdelay < 0
237 if (bootdelay
>= 0) {
238 if (tstc()) { /* we got a key press */
239 (void) getc(); /* consume input */
241 abort
= 1; /* don't auto boot */
246 while ((bootdelay
> 0) && (!abort
)) {
250 /* delay 100 * 10ms */
251 for (i
=0; !abort
&& i
<100; ++i
) {
252 if (tstc()) { /* we got a key press */
253 abort
= 1; /* don't auto boot */
254 bootdelay
= 0; /* no more delay */
255 # ifdef CONFIG_MENUKEY
258 (void) getc(); /* consume input */
265 printf("\b\b\b%2d ", bootdelay
);
270 #ifdef CONFIG_SILENT_CONSOLE
272 gd
->flags
&= ~GD_FLG_SILENT
;
277 # endif /* CONFIG_AUTOBOOT_KEYED */
278 #endif /* CONFIG_BOOTDELAY >= 0 */
280 /****************************************************************************/
282 void main_loop (void)
284 #ifndef CFG_HUSH_PARSER
285 static char lastcommand
[CFG_CBSIZE
] = { 0, };
291 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
295 #if defined(CONFIG_PREBOOT) || defined(CFG_PREBOOT_OVERRIDE)
298 #ifdef CONFIG_BOOTCOUNT_LIMIT
299 unsigned long bootcount
= 0;
300 unsigned long bootlimit
= 0;
303 #endif /* CONFIG_BOOTCOUNT_LIMIT */
305 #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
306 ulong bmp
= 0; /* default bitmap */
307 extern int trab_vfd (ulong bitmap
);
309 #ifdef CONFIG_MODEM_SUPPORT
311 bmp
= 1; /* alternate bitmap */
314 #endif /* CONFIG_VFD && VFD_TEST_LOGO */
316 #ifdef CONFIG_BOOTCOUNT_LIMIT
317 bootcount
= bootcount_load();
319 bootcount_store (bootcount
);
320 sprintf (bcs_set
, "%lu", bootcount
);
321 setenv ("bootcount", bcs_set
);
322 bcs
= getenv ("bootlimit");
323 bootlimit
= bcs
? simple_strtoul (bcs
, NULL
, 10) : 0;
324 #endif /* CONFIG_BOOTCOUNT_LIMIT */
326 #ifdef CONFIG_MODEM_SUPPORT
327 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init
);
329 char *str
= strdup(getenv("mdm_cmd"));
330 setenv ("preboot", str
); /* set or delete definition */
333 mdm_init(); /* wait for modem connection */
335 #endif /* CONFIG_MODEM_SUPPORT */
337 #ifdef CONFIG_VERSION_VARIABLE
339 extern char version_string
[];
341 setenv ("ver", version_string
); /* set version variable */
343 #endif /* CONFIG_VERSION_VARIABLE */
345 #ifdef CFG_HUSH_PARSER
346 u_boot_hush_start ();
349 #ifdef CONFIG_AUTO_COMPLETE
350 install_auto_complete();
353 #if defined(CONFIG_PREBOOT) || defined(CFG_PREBOOT_OVERRIDE)
354 #ifdef CONFIG_PREBOOT
355 p
= getenv ("preboot");
357 #ifdef CFG_PREBOOT_OVERRIDE
358 if (preboot_override
) {
359 /* for convenience, preboot_override may end in \n, not \0 */
360 p
= strchr(preboot_override
, '\n');
363 /* make sure we can overwrite the load area if we want to */
364 p
= strdup(preboot_override
);
365 /* clean the image in case we want to flash it */
366 preboot_override
= NULL
;
368 #endif /* CFG_PREBOOT_OVERRIDE */
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 || CFG_PREBOOT_OVERRIDE */
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 */
398 if (gd
->flags
& GD_FLG_POSTFAIL
) {
399 s
= getenv("failbootcmd");
402 #endif /* CONFIG_POST */
403 #ifdef CONFIG_BOOTCOUNT_LIMIT
404 if (bootlimit
&& (bootcount
> bootlimit
)) {
405 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
406 (unsigned)bootlimit
);
407 s
= getenv ("altbootcmd");
410 #endif /* CONFIG_BOOTCOUNT_LIMIT */
411 s
= getenv ("bootcmd");
413 debug ("### main_loop: bootcmd=\"%s\"\n", s
? s
: "<UNDEFINED>");
415 if (!nobootdelay
&& bootdelay
>= 0 && s
&& !abortboot (bootdelay
)) {
416 # ifdef CONFIG_AUTOBOOT_KEYED
417 int prev
= disable_ctrlc(1); /* disable Control C checking */
420 # ifndef CFG_HUSH_PARSER
423 parse_string_outer(s
, FLAG_PARSE_SEMICOLON
|
424 FLAG_EXIT_FROM_LOOP
);
427 # ifdef CONFIG_AUTOBOOT_KEYED
428 disable_ctrlc(prev
); /* restore Control C checking */
432 # ifdef CONFIG_MENUKEY
433 if (menukey
== CONFIG_MENUKEY
) {
434 s
= getenv("menucmd");
436 # ifndef CFG_HUSH_PARSER
439 parse_string_outer(s
, FLAG_PARSE_SEMICOLON
|
440 FLAG_EXIT_FROM_LOOP
);
444 #endif /* CONFIG_MENUKEY */
445 #endif /* CONFIG_BOOTDELAY */
447 #ifdef CONFIG_AMIGAONEG3SE
449 extern void video_banner(void);
455 * Main Loop for Monitor Command Processing
457 #ifdef CFG_HUSH_PARSER
459 /* This point is never reached */
463 #ifdef CONFIG_BOOT_RETRY_TIME
465 /* Saw enough of a valid command to
466 * restart the timeout.
471 len
= readline (CFG_PROMPT
);
473 flag
= 0; /* assume no special flags for now */
475 strcpy (lastcommand
, console_buffer
);
477 flag
|= CMD_FLAG_REPEAT
;
478 #ifdef CONFIG_BOOT_RETRY_TIME
479 else if (len
== -2) {
480 /* -2 means timed out, retry autoboot
482 puts ("\nTimed out waiting for command\n");
483 # ifdef CONFIG_RESET_TO_RETRY
484 /* Reinit board to run initialization code again */
485 do_reset (NULL
, 0, 0, NULL
);
487 return; /* retry autoboot */
493 puts ("<INTERRUPT>\n");
495 rc
= run_command (lastcommand
, flag
);
498 /* invalid command or not repeatable, forget it */
502 #endif /*CFG_HUSH_PARSER*/
505 #ifdef CONFIG_BOOT_RETRY_TIME
506 /***************************************************************************
507 * initialize command line timeout
509 void init_cmd_timeout(void)
511 char *s
= getenv ("bootretry");
514 retry_time
= (int)simple_strtol(s
, NULL
, 10);
516 retry_time
= CONFIG_BOOT_RETRY_TIME
;
518 if (retry_time
>= 0 && retry_time
< CONFIG_BOOT_RETRY_MIN
)
519 retry_time
= CONFIG_BOOT_RETRY_MIN
;
522 /***************************************************************************
523 * reset command line timeout to retry_time seconds
525 void reset_cmd_timeout(void)
527 endtime
= endtick(retry_time
);
531 #ifdef CONFIG_CMDLINE_EDITING
534 * cmdline-editing related codes from vivi.
535 * Author: Janghoon Lyu <nandy@mizi.com>
538 #define putnstr(str,n) do { \
539 printf ("%.*s", n, str); \
542 #define CTL_CH(c) ((c) - 'a' + 1)
544 #define MAX_CMDBUF_SIZE 256
546 #define CTL_BACKSPACE ('\b')
547 #define DEL ((char)255)
548 #define DEL7 ((char)127)
549 #define CREAD_HIST_CHAR ('!')
551 #define getcmd_putch(ch) putc(ch)
552 #define getcmd_getch() getc()
553 #define getcmd_cbeep() getcmd_putch('\a')
556 #define HIST_SIZE MAX_CMDBUF_SIZE
558 static int hist_max
= 0;
559 static int hist_add_idx
= 0;
560 static int hist_cur
= -1;
561 unsigned hist_num
= 0;
563 char* hist_list
[HIST_MAX
];
564 char hist_lines
[HIST_MAX
][HIST_SIZE
];
566 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
568 static void hist_init(void)
577 for (i
= 0; i
< HIST_MAX
; i
++) {
578 hist_list
[i
] = hist_lines
[i
];
579 hist_list
[i
][0] = '\0';
583 static void cread_add_to_hist(char *line
)
585 strcpy(hist_list
[hist_add_idx
], line
);
587 if (++hist_add_idx
>= HIST_MAX
)
590 if (hist_add_idx
> hist_max
)
591 hist_max
= hist_add_idx
;
596 static char* hist_prev(void)
608 if (hist_cur
== hist_add_idx
) {
612 ret
= hist_list
[hist_cur
];
617 static char* hist_next(void)
624 if (hist_cur
== hist_add_idx
)
627 if (++hist_cur
> hist_max
)
630 if (hist_cur
== hist_add_idx
) {
633 ret
= hist_list
[hist_cur
];
638 #ifndef CONFIG_CMDLINE_EDITING
639 static void cread_print_hist_list(void)
644 n
= hist_num
- hist_max
;
646 i
= hist_add_idx
+ 1;
650 if (i
== hist_add_idx
)
652 printf("%s\n", hist_list
[i
]);
657 #endif /* CONFIG_CMDLINE_EDITING */
659 #define BEGINNING_OF_LINE() { \
661 getcmd_putch(CTL_BACKSPACE); \
666 #define ERASE_TO_EOL() { \
667 if (num < eol_num) { \
669 for (tmp = num; tmp < eol_num; tmp++) \
671 while (tmp-- > num) \
672 getcmd_putch(CTL_BACKSPACE); \
677 #define REFRESH_TO_EOL() { \
678 if (num < eol_num) { \
679 wlen = eol_num - num; \
680 putnstr(buf + num, wlen); \
685 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
686 unsigned long *eol_num
, char *buf
, unsigned long len
)
691 if (insert
|| *num
== *eol_num
) {
692 if (*eol_num
> len
- 1) {
700 wlen
= *eol_num
- *num
;
702 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
706 putnstr(buf
+ *num
, wlen
);
709 getcmd_putch(CTL_BACKSPACE
);
712 /* echo the character */
715 putnstr(buf
+ *num
, wlen
);
720 static void cread_add_str(char *str
, int strsize
, int insert
, unsigned long *num
,
721 unsigned long *eol_num
, char *buf
, unsigned long len
)
724 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
729 static int cread_line(const char *const prompt
, char *buf
, unsigned int *len
)
731 unsigned long num
= 0;
732 unsigned long eol_num
= 0;
743 #ifdef CONFIG_BOOT_RETRY_TIME
744 while (!tstc()) { /* while no incoming data */
745 if (retry_time
>= 0 && get_ticks() > endtime
)
746 return (-2); /* timed out */
750 ichar
= getcmd_getch();
752 if ((ichar
== '\n') || (ichar
== '\r')) {
758 * handle standard linux xterm esc sequences for arrow key, etc.
763 esc_save
[esc_len
] = ichar
;
766 cread_add_str(esc_save
, esc_len
, insert
,
767 &num
, &eol_num
, buf
, *len
);
775 case 'D': /* <- key */
779 case 'C': /* -> key */
782 break; /* pass off to ^F handler */
783 case 'H': /* Home key */
786 break; /* pass off to ^A handler */
787 case 'A': /* up arrow */
790 break; /* pass off to ^P handler */
791 case 'B': /* down arrow */
794 break; /* pass off to ^N handler */
796 esc_save
[esc_len
++] = ichar
;
797 cread_add_str(esc_save
, esc_len
, insert
,
798 &num
, &eol_num
, buf
, *len
);
807 esc_save
[esc_len
] = ichar
;
810 puts("impossible condition #876\n");
818 case CTL_CH('c'): /* ^C - break */
819 *buf
= '\0'; /* discard input */
823 getcmd_putch(buf
[num
]);
829 getcmd_putch(CTL_BACKSPACE
);
835 wlen
= eol_num
- num
- 1;
837 memmove(&buf
[num
], &buf
[num
+1], wlen
);
838 putnstr(buf
+ num
, wlen
);
843 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
);
908 #ifdef CONFIG_AUTO_COMPLETE
912 /* do not autocomplete when in the middle */
919 col
= strlen(prompt
) + eol_num
;
921 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
930 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, *len
);
935 buf
[eol_num
] = '\0'; /* lose the newline */
937 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
938 cread_add_to_hist(buf
);
939 hist_cur
= hist_add_idx
;
944 #endif /* CONFIG_CMDLINE_EDITING */
946 /****************************************************************************/
949 * Prompt for input and read a line.
950 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
951 * time out when time goes past endtime (timebase time in ticks).
952 * Return: number of read characters
956 int readline (const char *const prompt
)
958 return readline_into_buffer(prompt
, console_buffer
);
962 int readline_into_buffer (const char *const prompt
, char * buffer
)
965 #ifdef CONFIG_CMDLINE_EDITING
966 unsigned int len
=MAX_CMDBUF_SIZE
;
968 static int initted
= 0;
977 rc
= cread_line(prompt
, p
, &len
);
978 return rc
< 0 ? rc
: len
;
981 int n
= 0; /* buffer index */
982 int plen
= 0; /* prompt length */
983 int col
; /* output column cnt */
988 plen
= strlen (prompt
);
994 #ifdef CONFIG_BOOT_RETRY_TIME
995 while (!tstc()) { /* while no incoming data */
996 if (retry_time
>= 0 && get_ticks() > endtime
)
997 return (-2); /* timed out */
1000 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1002 #ifdef CONFIG_SHOW_ACTIVITY
1004 extern void show_activity(int arg
);
1011 * Special character handling
1014 case '\r': /* Enter */
1020 case '\0': /* nul */
1023 case 0x03: /* ^C - break */
1024 p_buf
[0] = '\0'; /* discard input */
1027 case 0x15: /* ^U - erase line */
1028 while (col
> plen
) {
1036 case 0x17: /* ^W - erase word */
1037 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1038 while ((n
> 0) && (*p
!= ' ')) {
1039 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1043 case 0x08: /* ^H - backspace */
1044 case 0x7F: /* DEL - backspace */
1045 p
=delete_char(p_buf
, p
, &col
, &n
, plen
);
1050 * Must be a normal character then
1052 if (n
< CFG_CBSIZE
-2) {
1053 if (c
== '\t') { /* expand TABs */
1054 #ifdef CONFIG_AUTO_COMPLETE
1055 /* if auto completion triggered just continue */
1057 if (cmd_auto_complete(prompt
, console_buffer
, &n
, &col
)) {
1058 p
= p_buf
+ n
; /* reset */
1062 puts (tab_seq
+(col
&07));
1063 col
+= 8 - (col
&07);
1065 ++col
; /* echo input */
1070 } else { /* Buffer full */
1075 #endif /* CONFIG_CMDLINE_EDITING */
1078 /****************************************************************************/
1080 #ifndef CONFIG_CMDLINE_EDITING
1081 static char * delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
1089 if (*(--p
) == '\t') { /* will retype the whole line */
1090 while (*colp
> plen
) {
1094 for (s
=buffer
; s
<p
; ++s
) {
1096 puts (tab_seq
+((*colp
) & 07));
1097 *colp
+= 8 - ((*colp
) & 07);
1110 #endif /* CONFIG_CMDLINE_EDITING */
1112 /****************************************************************************/
1114 int parse_line (char *line
, char *argv
[])
1119 printf ("parse_line: \"%s\"\n", line
);
1121 while (nargs
< CFG_MAXARGS
) {
1123 /* skip any white space */
1124 while ((*line
== ' ') || (*line
== '\t')) {
1128 if (*line
== '\0') { /* end of line, no more args */
1131 printf ("parse_line: nargs=%d\n", nargs
);
1136 argv
[nargs
++] = line
; /* begin of argument string */
1138 /* find end of string */
1139 while (*line
&& (*line
!= ' ') && (*line
!= '\t')) {
1143 if (*line
== '\0') { /* end of line, no more args */
1146 printf ("parse_line: nargs=%d\n", nargs
);
1151 *line
++ = '\0'; /* terminate current arg */
1154 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS
);
1157 printf ("parse_line: nargs=%d\n", nargs
);
1162 /****************************************************************************/
1164 static void process_macros (const char *input
, char *output
)
1167 const char *varname_start
= NULL
;
1168 int inputcnt
= strlen (input
);
1169 int outputcnt
= CFG_CBSIZE
;
1170 int state
= 0; /* 0 = waiting for '$' */
1172 /* 1 = waiting for '(' or '{' */
1173 /* 2 = waiting for ')' or '}' */
1174 /* 3 = waiting for ''' */
1176 char *output_start
= output
;
1178 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input
),
1182 prev
= '\0'; /* previous character */
1184 while (inputcnt
&& outputcnt
) {
1189 /* remove one level of escape characters */
1190 if ((c
== '\\') && (prev
!= '\\')) {
1191 if (inputcnt
-- == 0)
1199 case 0: /* Waiting for (unescaped) $ */
1200 if ((c
== '\'') && (prev
!= '\\')) {
1204 if ((c
== '$') && (prev
!= '\\')) {
1211 case 1: /* Waiting for ( */
1212 if (c
== '(' || c
== '{') {
1214 varname_start
= input
;
1226 case 2: /* Waiting for ) */
1227 if (c
== ')' || c
== '}') {
1229 char envname
[CFG_CBSIZE
], *envval
;
1230 int envcnt
= input
- varname_start
- 1; /* Varname # of chars */
1232 /* Get the varname */
1233 for (i
= 0; i
< envcnt
; i
++) {
1234 envname
[i
] = varname_start
[i
];
1239 envval
= getenv (envname
);
1241 /* Copy into the line if it exists */
1243 while ((*envval
) && outputcnt
) {
1244 *(output
++) = *(envval
++);
1247 /* Look for another '$' */
1251 case 3: /* Waiting for ' */
1252 if ((c
== '\'') && (prev
!= '\\')) {
1269 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1270 strlen (output_start
), output_start
);
1274 /****************************************************************************
1276 * 1 - command executed, repeatable
1277 * 0 - command executed but not repeatable, interrupted commands are
1278 * always considered not repeatable
1279 * -1 - not executed (unrecognized, bootd recursion or too many args)
1280 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1281 * considered unrecognized)
1285 * We must create a temporary copy of the command since the command we get
1286 * may be the result from getenv(), which returns a pointer directly to
1287 * the environment data, which may change magicly when the command we run
1288 * creates or modifies environment variables (like "bootp" does).
1291 int run_command (const char *cmd
, int flag
)
1294 char cmdbuf
[CFG_CBSIZE
]; /* working copy of cmd */
1295 char *token
; /* start of token in cmdbuf */
1296 char *sep
; /* end of token (separator) in cmdbuf */
1297 char finaltoken
[CFG_CBSIZE
];
1299 char *argv
[CFG_MAXARGS
+ 1]; /* NULL terminated */
1305 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd
);
1306 puts (cmd
? cmd
: "NULL"); /* use puts - string may be loooong */
1310 clear_ctrlc(); /* forget any previous Control C */
1312 if (!cmd
|| !*cmd
) {
1313 return -1; /* empty command */
1316 if (strlen(cmd
) >= CFG_CBSIZE
) {
1317 puts ("## Command too long!\n");
1321 strcpy (cmdbuf
, cmd
);
1323 /* Process separators and check for invalid
1324 * repeatable commands
1328 printf ("[PROCESS_SEPARATORS] %s\n", cmd
);
1333 * Find separator, or string end
1334 * Allow simple escape of ';' by writing "\;"
1336 for (inquotes
= 0, sep
= str
; *sep
; sep
++) {
1342 (*sep
== ';') && /* separator */
1343 ( sep
!= str
) && /* past string start */
1344 (*(sep
-1) != '\\')) /* and NOT escaped */
1349 * Limit the token to data between separators
1353 str
= sep
+ 1; /* start of command for next pass */
1357 str
= sep
; /* no more commands for next pass */
1359 printf ("token: \"%s\"\n", token
);
1362 /* find macros in this token and replace them */
1363 process_macros (token
, finaltoken
);
1365 /* Extract arguments */
1366 if ((argc
= parse_line (finaltoken
, argv
)) == 0) {
1367 rc
= -1; /* no command at all */
1371 /* Look up command in command table */
1372 if ((cmdtp
= find_cmd(argv
[0])) == NULL
) {
1373 printf ("Unknown command '%s' - try 'help'\n", argv
[0]);
1374 rc
= -1; /* give up after bad command */
1378 /* found - check max args */
1379 if (argc
> cmdtp
->maxargs
) {
1380 printf ("Usage:\n%s\n", cmdtp
->usage
);
1385 #if defined(CONFIG_CMD_BOOTD)
1386 /* avoid "bootd" recursion */
1387 if (cmdtp
->cmd
== do_bootd
) {
1389 printf ("[%s]\n", finaltoken
);
1391 if (flag
& CMD_FLAG_BOOTD
) {
1392 puts ("'bootd' recursion detected\n");
1396 flag
|= CMD_FLAG_BOOTD
;
1401 /* OK - call function to do the command */
1402 if ((cmdtp
->cmd
) (cmdtp
, flag
, argc
, argv
) != 0) {
1406 repeatable
&= cmdtp
->repeatable
;
1408 /* Did the user stop this? */
1410 return -1; /* if stopped then not repeatable */
1413 return rc
? rc
: repeatable
;
1416 /****************************************************************************/
1418 #if defined(CONFIG_CMD_RUN)
1419 int do_run (cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
1424 printf ("Usage:\n%s\n", cmdtp
->usage
);
1428 for (i
=1; i
<argc
; ++i
) {
1431 if ((arg
= getenv (argv
[i
])) == NULL
) {
1432 printf ("## Error: \"%s\" not defined\n", argv
[i
]);
1435 #ifndef CFG_HUSH_PARSER
1436 if (run_command (arg
, flag
) == -1)
1439 if (parse_string_outer(arg
,
1440 FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP
) != 0)