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,
36 #ifdef CONFIG_MODEM_SUPPORT
37 #include <malloc.h> /* for free() prototype */
40 #ifdef CONFIG_SYS_HUSH_PARSER
44 #ifdef CONFIG_OF_CONTROL
48 #ifdef CONFIG_OF_LIBFDT
49 #include <fdt_support.h>
50 #endif /* CONFIG_OF_LIBFDT */
53 #include <linux/ctype.h>
56 #if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
57 DECLARE_GLOBAL_DATA_PTR;
61 * Board-specific Platform code can reimplement show_boot_progress () if needed
63 void inline __show_boot_progress (int val) {}
64 void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
66 #if defined(CONFIG_UPDATE_TFTP)
67 int update_tftp (ulong addr);
68 #endif /* CONFIG_UPDATE_TFTP */
70 #define MAX_DELAY_STOP_STR 32
74 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
76 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
77 static const char erase_seq[] = "\b \b"; /* erase sequence */
78 static const char tab_seq[] = " "; /* used to expand TABs */
80 #ifdef CONFIG_BOOT_RETRY_TIME
81 static uint64_t endtime = 0; /* must be set, default is instant timeout */
82 static int retry_time = -1; /* -1 so can call readline before main_loop */
85 #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
87 #ifndef CONFIG_BOOT_RETRY_MIN
88 #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
91 #ifdef CONFIG_MODEM_SUPPORT
93 extern void mdm_init(void); /* defined in board.c */
96 /***************************************************************************
97 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
98 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
100 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
101 # if defined(CONFIG_AUTOBOOT_KEYED)
105 int abortboot(int bootdelay)
108 uint64_t etime = endtick(bootdelay);
115 { str: getenv ("bootdelaykey"), retry: 1 },
116 { str: getenv ("bootdelaykey2"), retry: 1 },
117 { str: getenv ("bootstopkey"), retry: 0 },
118 { str: getenv ("bootstopkey2"), retry: 0 },
121 char presskey [MAX_DELAY_STOP_STR];
122 u_int presskey_len = 0;
123 u_int presskey_max = 0;
126 #ifndef CONFIG_ZERO_BOOTDELAY_CHECK
131 # ifdef CONFIG_AUTOBOOT_PROMPT
132 printf(CONFIG_AUTOBOOT_PROMPT);
135 # ifdef CONFIG_AUTOBOOT_DELAY_STR
136 if (delaykey[0].str == NULL)
137 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
139 # ifdef CONFIG_AUTOBOOT_DELAY_STR2
140 if (delaykey[1].str == NULL)
141 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
143 # ifdef CONFIG_AUTOBOOT_STOP_STR
144 if (delaykey[2].str == NULL)
145 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
147 # ifdef CONFIG_AUTOBOOT_STOP_STR2
148 if (delaykey[3].str == NULL)
149 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
152 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
153 delaykey[i].len = delaykey[i].str == NULL ?
154 0 : strlen (delaykey[i].str);
155 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
156 MAX_DELAY_STOP_STR : delaykey[i].len;
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
162 printf("%s key:<%s>\n",
163 delaykey[i].retry ? "delay" : "stop",
164 delaykey[i].str ? delaykey[i].str : "NULL");
168 /* In order to keep up with incoming data, check timeout only
173 if (presskey_len < presskey_max) {
174 presskey [presskey_len ++] = getc();
177 for (i = 0; i < presskey_max - 1; i ++)
178 presskey [i] = presskey [i + 1];
180 presskey [i] = getc();
184 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
185 if (delaykey[i].len > 0 &&
186 presskey_len >= delaykey[i].len &&
187 memcmp (presskey + presskey_len - delaykey[i].len,
189 delaykey[i].len) == 0) {
191 printf("got %skey\n",
192 delaykey[i].retry ? "delay" : "stop");
195 # ifdef CONFIG_BOOT_RETRY_TIME
196 /* don't retry auto boot */
197 if (! delaykey[i].retry)
203 } while (!abort && get_ticks() <= etime);
207 puts("key timeout\n");
210 #ifdef CONFIG_SILENT_CONSOLE
212 gd->flags &= ~GD_FLG_SILENT;
218 # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
220 #ifdef CONFIG_MENUKEY
221 static int menukey = 0;
227 int abortboot(int bootdelay)
231 #ifdef CONFIG_MENUPROMPT
232 printf(CONFIG_MENUPROMPT);
235 printf("Hit any key to stop autoboot: %2d ", bootdelay);
238 #if defined CONFIG_ZERO_BOOTDELAY_CHECK
240 * Check if key already pressed
241 * Don't check if bootdelay < 0
243 if (bootdelay >= 0) {
244 if (tstc()) { /* we got a key press */
245 (void) getc(); /* consume input */
247 abort = 1; /* don't auto boot */
252 while ((bootdelay > 0) && (!abort)) {
256 /* delay 100 * 10ms */
257 for (i=0; !abort && i<100; ++i) {
258 if (tstc()) { /* we got a key press */
259 abort = 1; /* don't auto boot */
260 bootdelay = 0; /* no more delay */
261 # ifdef CONFIG_MENUKEY
264 (void) getc(); /* consume input */
271 printf("\b\b\b%2d ", bootdelay);
276 #ifdef CONFIG_SILENT_CONSOLE
278 gd->flags &= ~GD_FLG_SILENT;
283 # endif /* CONFIG_AUTOBOOT_KEYED */
284 #endif /* CONFIG_BOOTDELAY >= 0 */
287 * Runs the given boot command securely. Specifically:
288 * - Doesn't run the command with the shell (run_command or parse_string_outer),
289 * since that's a lot of code surface that an attacker might exploit.
290 * Because of this, we don't do any argument parsing--the secure boot command
291 * has to be a full-fledged u-boot command.
292 * - Doesn't check for keypresses before booting, since that could be a
293 * security hole; also disables Ctrl-C.
294 * - Doesn't allow the command to return.
296 * Upon any failures, this function will drop into an infinite loop after
297 * printing the error message to console.
300 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
301 defined(CONFIG_OF_CONTROL)
302 static void secure_boot_cmd(char *cmd)
308 printf("## Error: Secure boot command not specified\n");
312 /* Disable Ctrl-C just in case some command is used that checks it. */
315 /* Find the command directly. */
316 cmdtp = find_cmd(cmd);
318 printf("## Error: \"%s\" not defined\n", cmd);
322 /* Run the command, forcing no flags and faking argc and argv. */
323 rc = (cmdtp->cmd)(cmdtp, 0, 1, &cmd);
325 /* Shouldn't ever return from boot command. */
326 printf("## Error: \"%s\" returned (code %d)\n", cmd, rc);
330 * Not a whole lot to do here. Rebooting won't help much, since we'll
331 * just end up right back here. Just loop.
336 #endif /* CONFIG_OF_CONTROL */
339 /****************************************************************************/
341 void main_loop (void)
343 #ifndef CONFIG_SYS_HUSH_PARSER
344 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
349 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
350 defined(CONFIG_OF_CONTROL)
353 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
357 #ifdef CONFIG_PREBOOT
360 #ifdef CONFIG_BOOTCOUNT_LIMIT
361 unsigned long bootcount = 0;
362 unsigned long bootlimit = 0;
365 #endif /* CONFIG_BOOTCOUNT_LIMIT */
367 #ifdef CONFIG_BOOTCOUNT_LIMIT
368 bootcount = bootcount_load();
370 bootcount_store (bootcount);
371 sprintf (bcs_set, "%lu", bootcount);
372 setenv ("bootcount", bcs_set);
373 bcs = getenv ("bootlimit");
374 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
375 #endif /* CONFIG_BOOTCOUNT_LIMIT */
377 #ifdef CONFIG_MODEM_SUPPORT
378 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
380 char *str = strdup(getenv("mdm_cmd"));
381 setenv ("preboot", str); /* set or delete definition */
384 mdm_init(); /* wait for modem connection */
386 #endif /* CONFIG_MODEM_SUPPORT */
388 #ifdef CONFIG_VERSION_VARIABLE
390 setenv ("ver", version_string); /* set version variable */
392 #endif /* CONFIG_VERSION_VARIABLE */
394 #ifdef CONFIG_SYS_HUSH_PARSER
395 u_boot_hush_start ();
398 #if defined(CONFIG_HUSH_INIT_VAR)
402 #ifdef CONFIG_PREBOOT
403 if ((p = getenv ("preboot")) != NULL) {
404 # ifdef CONFIG_AUTOBOOT_KEYED
405 int prev = disable_ctrlc(1); /* disable Control C checking */
408 run_command_list(p, -1, 0);
410 # ifdef CONFIG_AUTOBOOT_KEYED
411 disable_ctrlc(prev); /* restore Control C checking */
414 #endif /* CONFIG_PREBOOT */
416 #if defined(CONFIG_UPDATE_TFTP)
418 #endif /* CONFIG_UPDATE_TFTP */
420 #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
421 s = getenv ("bootdelay");
422 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
424 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
426 #if defined(CONFIG_MENU_SHOW)
427 bootdelay = menu_show(bootdelay);
429 # ifdef CONFIG_BOOT_RETRY_TIME
431 # endif /* CONFIG_BOOT_RETRY_TIME */
434 if (gd->flags & GD_FLG_POSTFAIL) {
435 s = getenv("failbootcmd");
438 #endif /* CONFIG_POST */
439 #ifdef CONFIG_BOOTCOUNT_LIMIT
440 if (bootlimit && (bootcount > bootlimit)) {
441 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
442 (unsigned)bootlimit);
443 s = getenv ("altbootcmd");
446 #endif /* CONFIG_BOOTCOUNT_LIMIT */
447 s = getenv ("bootcmd");
448 #ifdef CONFIG_OF_CONTROL
449 /* Allow the fdt to override the boot command */
450 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
455 * If the bootsecure option was chosen, use secure_boot_cmd().
456 * Always use 'env' in this case, since bootsecure requres that the
457 * bootcmd was specified in the FDT too.
459 if (fdtdec_get_config_int(gd->fdt_blob, "bootsecure", 0))
460 secure_boot_cmd(env);
462 #endif /* CONFIG_OF_CONTROL */
464 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
466 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
467 # ifdef CONFIG_AUTOBOOT_KEYED
468 int prev = disable_ctrlc(1); /* disable Control C checking */
471 run_command_list(s, -1, 0);
473 # ifdef CONFIG_AUTOBOOT_KEYED
474 disable_ctrlc(prev); /* restore Control C checking */
478 # ifdef CONFIG_MENUKEY
479 if (menukey == CONFIG_MENUKEY) {
480 s = getenv("menucmd");
482 run_command_list(s, -1, 0);
484 #endif /* CONFIG_MENUKEY */
485 #endif /* CONFIG_BOOTDELAY */
487 #if defined CONFIG_OF_CONTROL
488 set_working_fdt_addr((void *)gd->fdt_blob);
489 #endif /* CONFIG_OF_CONTROL */
492 * Main Loop for Monitor Command Processing
494 #ifdef CONFIG_SYS_HUSH_PARSER
496 /* This point is never reached */
500 #ifdef CONFIG_BOOT_RETRY_TIME
502 /* Saw enough of a valid command to
503 * restart the timeout.
508 len = readline (CONFIG_SYS_PROMPT);
510 flag = 0; /* assume no special flags for now */
512 strcpy (lastcommand, console_buffer);
514 flag |= CMD_FLAG_REPEAT;
515 #ifdef CONFIG_BOOT_RETRY_TIME
516 else if (len == -2) {
517 /* -2 means timed out, retry autoboot
519 puts ("\nTimed out waiting for command\n");
520 # ifdef CONFIG_RESET_TO_RETRY
521 /* Reinit board to run initialization code again */
522 do_reset (NULL, 0, 0, NULL);
524 return; /* retry autoboot */
530 puts ("<INTERRUPT>\n");
532 rc = run_command(lastcommand, flag);
535 /* invalid command or not repeatable, forget it */
539 #endif /*CONFIG_SYS_HUSH_PARSER*/
542 #ifdef CONFIG_BOOT_RETRY_TIME
543 /***************************************************************************
544 * initialize command line timeout
546 void init_cmd_timeout(void)
548 char *s = getenv ("bootretry");
551 retry_time = (int)simple_strtol(s, NULL, 10);
553 retry_time = CONFIG_BOOT_RETRY_TIME;
555 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
556 retry_time = CONFIG_BOOT_RETRY_MIN;
559 /***************************************************************************
560 * reset command line timeout to retry_time seconds
562 void reset_cmd_timeout(void)
564 endtime = endtick(retry_time);
568 #ifdef CONFIG_CMDLINE_EDITING
571 * cmdline-editing related codes from vivi.
572 * Author: Janghoon Lyu <nandy@mizi.com>
575 #define putnstr(str,n) do { \
576 printf ("%.*s", (int)n, str); \
579 #define CTL_CH(c) ((c) - 'a' + 1)
580 #define CTL_BACKSPACE ('\b')
581 #define DEL ((char)255)
582 #define DEL7 ((char)127)
583 #define CREAD_HIST_CHAR ('!')
585 #define getcmd_putch(ch) putc(ch)
586 #define getcmd_getch() getc()
587 #define getcmd_cbeep() getcmd_putch('\a')
590 #define HIST_SIZE CONFIG_SYS_CBSIZE
593 static int hist_add_idx;
594 static int hist_cur = -1;
595 static unsigned hist_num;
597 static char *hist_list[HIST_MAX];
598 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
600 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
602 static void hist_init(void)
611 for (i = 0; i < HIST_MAX; i++) {
612 hist_list[i] = hist_lines[i];
613 hist_list[i][0] = '\0';
617 static void cread_add_to_hist(char *line)
619 strcpy(hist_list[hist_add_idx], line);
621 if (++hist_add_idx >= HIST_MAX)
624 if (hist_add_idx > hist_max)
625 hist_max = hist_add_idx;
630 static char* hist_prev(void)
642 if (hist_cur == hist_add_idx) {
646 ret = hist_list[hist_cur];
651 static char* hist_next(void)
658 if (hist_cur == hist_add_idx)
661 if (++hist_cur > hist_max)
664 if (hist_cur == hist_add_idx) {
667 ret = hist_list[hist_cur];
672 #ifndef CONFIG_CMDLINE_EDITING
673 static void cread_print_hist_list(void)
678 n = hist_num - hist_max;
680 i = hist_add_idx + 1;
684 if (i == hist_add_idx)
686 printf("%s\n", hist_list[i]);
691 #endif /* CONFIG_CMDLINE_EDITING */
693 #define BEGINNING_OF_LINE() { \
695 getcmd_putch(CTL_BACKSPACE); \
700 #define ERASE_TO_EOL() { \
701 if (num < eol_num) { \
702 printf("%*s", (int)(eol_num - num), ""); \
704 getcmd_putch(CTL_BACKSPACE); \
705 } while (--eol_num > num); \
709 #define REFRESH_TO_EOL() { \
710 if (num < eol_num) { \
711 wlen = eol_num - num; \
712 putnstr(buf + num, wlen); \
717 static void cread_add_char(char ichar, int insert, unsigned long *num,
718 unsigned long *eol_num, char *buf, unsigned long len)
723 if (insert || *num == *eol_num) {
724 if (*eol_num > len - 1) {
732 wlen = *eol_num - *num;
734 memmove(&buf[*num+1], &buf[*num], wlen-1);
738 putnstr(buf + *num, wlen);
741 getcmd_putch(CTL_BACKSPACE);
744 /* echo the character */
747 putnstr(buf + *num, wlen);
752 static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
753 unsigned long *eol_num, char *buf, unsigned long len)
756 cread_add_char(*str, insert, num, eol_num, buf, len);
761 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
764 unsigned long num = 0;
765 unsigned long eol_num = 0;
771 int init_len = strlen(buf);
775 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
778 #ifdef CONFIG_BOOT_RETRY_TIME
779 while (!tstc()) { /* while no incoming data */
780 if (retry_time >= 0 && get_ticks() > endtime)
781 return (-2); /* timed out */
785 if (first && timeout) {
786 uint64_t etime = endtick(timeout);
788 while (!tstc()) { /* while no incoming data */
789 if (get_ticks() >= etime)
790 return -2; /* timed out */
796 ichar = getcmd_getch();
798 if ((ichar == '\n') || (ichar == '\r')) {
804 * handle standard linux xterm esc sequences for arrow key, etc.
809 esc_save[esc_len] = ichar;
812 cread_add_str(esc_save, esc_len, insert,
813 &num, &eol_num, buf, *len);
821 case 'D': /* <- key */
825 case 'C': /* -> key */
828 break; /* pass off to ^F handler */
829 case 'H': /* Home key */
832 break; /* pass off to ^A handler */
833 case 'A': /* up arrow */
836 break; /* pass off to ^P handler */
837 case 'B': /* down arrow */
840 break; /* pass off to ^N handler */
842 esc_save[esc_len++] = ichar;
843 cread_add_str(esc_save, esc_len, insert,
844 &num, &eol_num, buf, *len);
853 esc_save[esc_len] = ichar;
856 puts("impossible condition #876\n");
864 case CTL_CH('c'): /* ^C - break */
865 *buf = '\0'; /* discard input */
869 getcmd_putch(buf[num]);
875 getcmd_putch(CTL_BACKSPACE);
881 wlen = eol_num - num - 1;
883 memmove(&buf[num], &buf[num+1], wlen);
884 putnstr(buf + num, wlen);
889 getcmd_putch(CTL_BACKSPACE);
912 wlen = eol_num - num;
914 memmove(&buf[num], &buf[num+1], wlen);
915 getcmd_putch(CTL_BACKSPACE);
916 putnstr(buf + num, wlen);
919 getcmd_putch(CTL_BACKSPACE);
931 if (ichar == CTL_CH('p'))
941 /* nuke the current line */
945 /* erase to end of line */
948 /* copy new line into place and display */
950 eol_num = strlen(buf);
954 #ifdef CONFIG_AUTO_COMPLETE
958 /* do not autocomplete when in the middle */
965 col = strlen(prompt) + eol_num;
967 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
976 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
981 buf[eol_num] = '\0'; /* lose the newline */
983 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
984 cread_add_to_hist(buf);
985 hist_cur = hist_add_idx;
990 #endif /* CONFIG_CMDLINE_EDITING */
992 /****************************************************************************/
995 * Prompt for input and read a line.
996 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
997 * time out when time goes past endtime (timebase time in ticks).
998 * Return: number of read characters
1002 int readline (const char *const prompt)
1005 * If console_buffer isn't 0-length the user will be prompted to modify
1006 * it instead of entering it from scratch as desired.
1008 console_buffer[0] = '\0';
1010 return readline_into_buffer(prompt, console_buffer, 0);
1014 int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
1017 #ifdef CONFIG_CMDLINE_EDITING
1018 unsigned int len = CONFIG_SYS_CBSIZE;
1020 static int initted = 0;
1023 * History uses a global array which is not
1024 * writable until after relocation to RAM.
1025 * Revert to non-history version if still
1026 * running from flash.
1028 if (gd->flags & GD_FLG_RELOC) {
1037 rc = cread_line(prompt, p, &len, timeout);
1038 return rc < 0 ? rc : len;
1041 #endif /* CONFIG_CMDLINE_EDITING */
1043 int n = 0; /* buffer index */
1044 int plen = 0; /* prompt length */
1045 int col; /* output column cnt */
1050 plen = strlen (prompt);
1056 #ifdef CONFIG_BOOT_RETRY_TIME
1057 while (!tstc()) { /* while no incoming data */
1058 if (retry_time >= 0 && get_ticks() > endtime)
1059 return (-2); /* timed out */
1063 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1065 #ifdef CONFIG_SHOW_ACTIVITY
1074 * Special character handling
1077 case '\r': /* Enter */
1083 case '\0': /* nul */
1086 case 0x03: /* ^C - break */
1087 p_buf[0] = '\0'; /* discard input */
1090 case 0x15: /* ^U - erase line */
1091 while (col > plen) {
1099 case 0x17: /* ^W - erase word */
1100 p=delete_char(p_buf, p, &col, &n, plen);
1101 while ((n > 0) && (*p != ' ')) {
1102 p=delete_char(p_buf, p, &col, &n, plen);
1106 case 0x08: /* ^H - backspace */
1107 case 0x7F: /* DEL - backspace */
1108 p=delete_char(p_buf, p, &col, &n, plen);
1113 * Must be a normal character then
1115 if (n < CONFIG_SYS_CBSIZE-2) {
1116 if (c == '\t') { /* expand TABs */
1117 #ifdef CONFIG_AUTO_COMPLETE
1118 /* if auto completion triggered just continue */
1120 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
1121 p = p_buf + n; /* reset */
1125 puts (tab_seq+(col&07));
1126 col += 8 - (col&07);
1128 ++col; /* echo input */
1133 } else { /* Buffer full */
1138 #ifdef CONFIG_CMDLINE_EDITING
1143 /****************************************************************************/
1145 static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1153 if (*(--p) == '\t') { /* will retype the whole line */
1154 while (*colp > plen) {
1158 for (s=buffer; s<p; ++s) {
1160 puts (tab_seq+((*colp) & 07));
1161 *colp += 8 - ((*colp) & 07);
1175 /****************************************************************************/
1177 int parse_line (char *line, char *argv[])
1182 printf ("parse_line: \"%s\"\n", line);
1184 while (nargs < CONFIG_SYS_MAXARGS) {
1186 /* skip any white space */
1187 while (isblank(*line))
1190 if (*line == '\0') { /* end of line, no more args */
1193 printf ("parse_line: nargs=%d\n", nargs);
1198 argv[nargs++] = line; /* begin of argument string */
1200 /* find end of string */
1201 while (*line && !isblank(*line))
1204 if (*line == '\0') { /* end of line, no more args */
1207 printf ("parse_line: nargs=%d\n", nargs);
1212 *line++ = '\0'; /* terminate current arg */
1215 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
1218 printf ("parse_line: nargs=%d\n", nargs);
1223 /****************************************************************************/
1225 #ifndef CONFIG_SYS_HUSH_PARSER
1226 static void process_macros (const char *input, char *output)
1229 const char *varname_start = NULL;
1230 int inputcnt = strlen (input);
1231 int outputcnt = CONFIG_SYS_CBSIZE;
1232 int state = 0; /* 0 = waiting for '$' */
1234 /* 1 = waiting for '(' or '{' */
1235 /* 2 = waiting for ')' or '}' */
1236 /* 3 = waiting for ''' */
1238 char *output_start = output;
1240 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1244 prev = '\0'; /* previous character */
1246 while (inputcnt && outputcnt) {
1251 /* remove one level of escape characters */
1252 if ((c == '\\') && (prev != '\\')) {
1253 if (inputcnt-- == 0)
1261 case 0: /* Waiting for (unescaped) $ */
1262 if ((c == '\'') && (prev != '\\')) {
1266 if ((c == '$') && (prev != '\\')) {
1273 case 1: /* Waiting for ( */
1274 if (c == '(' || c == '{') {
1276 varname_start = input;
1288 case 2: /* Waiting for ) */
1289 if (c == ')' || c == '}') {
1291 char envname[CONFIG_SYS_CBSIZE], *envval;
1292 int envcnt = input - varname_start - 1; /* Varname # of chars */
1294 /* Get the varname */
1295 for (i = 0; i < envcnt; i++) {
1296 envname[i] = varname_start[i];
1301 envval = getenv (envname);
1303 /* Copy into the line if it exists */
1305 while ((*envval) && outputcnt) {
1306 *(output++) = *(envval++);
1309 /* Look for another '$' */
1313 case 3: /* Waiting for ' */
1314 if ((c == '\'') && (prev != '\\')) {
1331 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
1332 strlen (output_start), output_start);
1336 /****************************************************************************
1338 * 1 - command executed, repeatable
1339 * 0 - command executed but not repeatable, interrupted commands are
1340 * always considered not repeatable
1341 * -1 - not executed (unrecognized, bootd recursion or too many args)
1342 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
1343 * considered unrecognized)
1347 * We must create a temporary copy of the command since the command we get
1348 * may be the result from getenv(), which returns a pointer directly to
1349 * the environment data, which may change magicly when the command we run
1350 * creates or modifies environment variables (like "bootp" does).
1352 static int builtin_run_command(const char *cmd, int flag)
1354 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
1355 char *token; /* start of token in cmdbuf */
1356 char *sep; /* end of token (separator) in cmdbuf */
1357 char finaltoken[CONFIG_SYS_CBSIZE];
1359 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
1365 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1366 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1370 clear_ctrlc(); /* forget any previous Control C */
1372 if (!cmd || !*cmd) {
1373 return -1; /* empty command */
1376 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
1377 puts ("## Command too long!\n");
1381 strcpy (cmdbuf, cmd);
1383 /* Process separators and check for invalid
1384 * repeatable commands
1388 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1393 * Find separator, or string end
1394 * Allow simple escape of ';' by writing "\;"
1396 for (inquotes = 0, sep = str; *sep; sep++) {
1402 (*sep == ';') && /* separator */
1403 ( sep != str) && /* past string start */
1404 (*(sep-1) != '\\')) /* and NOT escaped */
1409 * Limit the token to data between separators
1413 str = sep + 1; /* start of command for next pass */
1417 str = sep; /* no more commands for next pass */
1419 printf ("token: \"%s\"\n", token);
1422 /* find macros in this token and replace them */
1423 process_macros (token, finaltoken);
1425 /* Extract arguments */
1426 if ((argc = parse_line (finaltoken, argv)) == 0) {
1427 rc = -1; /* no command at all */
1431 if (cmd_process(flag, argc, argv, &repeatable))
1434 /* Did the user stop this? */
1436 return -1; /* if stopped then not repeatable */
1439 return rc ? rc : repeatable;
1444 * Run a command using the selected parser.
1446 * @param cmd Command to run
1447 * @param flag Execution flags (CMD_FLAG_...)
1448 * @return 0 on success, or != 0 on error.
1450 int run_command(const char *cmd, int flag)
1452 #ifndef CONFIG_SYS_HUSH_PARSER
1454 * builtin_run_command can return 0 or 1 for success, so clean up
1457 if (builtin_run_command(cmd, flag) == -1)
1462 return parse_string_outer(cmd,
1463 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1467 #ifndef CONFIG_SYS_HUSH_PARSER
1469 * Execute a list of command separated by ; or \n using the built-in parser.
1471 * This function cannot take a const char * for the command, since if it
1472 * finds newlines in the string, it replaces them with \0.
1474 * @param cmd String containing list of commands
1475 * @param flag Execution flags (CMD_FLAG_...)
1476 * @return 0 on success, or != 0 on error.
1478 static int builtin_run_command_list(char *cmd, int flag)
1484 * Break into individual lines, and execute each line; terminate on
1489 if (*next == '\n') {
1491 /* run only non-empty commands */
1493 debug("** exec: \"%s\"\n", line);
1494 if (builtin_run_command(line, 0) < 0) {
1503 if (rcode == 0 && *line)
1504 rcode = (builtin_run_command(line, 0) >= 0);
1510 int run_command_list(const char *cmd, int len, int flag)
1513 char *buff = (char *)cmd; /* cast away const */
1518 #ifdef CONFIG_SYS_HUSH_PARSER
1519 /* hush will never change our string */
1522 /* the built-in parser will change our string if it sees \n */
1523 need_buff = strchr(cmd, '\n') != NULL;
1527 buff = malloc(len + 1);
1530 memcpy(buff, cmd, len);
1533 #ifdef CONFIG_SYS_HUSH_PARSER
1534 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1537 * This function will overwrite any \n it sees with a \0, which
1538 * is why it can't work with a const char *. Here we are making
1539 * using of internal knowledge of this function, to avoid always
1540 * doing a malloc() which is actually required only in a case that
1543 rcode = builtin_run_command_list(buff, flag);
1551 /****************************************************************************/
1553 #if defined(CONFIG_CMD_RUN)
1554 int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
1559 return CMD_RET_USAGE;
1561 for (i=1; i<argc; ++i) {
1564 if ((arg = getenv (argv[i])) == NULL) {
1565 printf ("## Error: \"%s\" not defined\n", argv[i]);
1569 if (run_command(arg, flag) != 0)