]> Git Repo - J-u-boot.git/blame - common/main.c
fdt: Tell the FDT library where the device tree is
[J-u-boot.git] / common / main.c
CommitLineData
c609719b
WD
1/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, [email protected].
4 *
501090aa
WD
5 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <[email protected]>
8 *
c609719b
WD
9 * See file CREDITS for list of people who contributed to this
10 * project.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307 USA
26 */
27
a6c7ad2f
WD
28/* #define DEBUG */
29
c609719b
WD
30#include <common.h>
31#include <watchdog.h>
32#include <command.h>
224b72e6 33#include <fdtdec.h>
d51004a8 34#include <malloc.h>
09c2e90c 35#include <version.h>
d9631ecf
WD
36#ifdef CONFIG_MODEM_SUPPORT
37#include <malloc.h> /* for free() prototype */
38#endif
c609719b 39
6d0f6bcf 40#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
41#include <hush.h>
42#endif
43
224b72e6
CLC
44#ifdef CONFIG_OF_CONTROL
45#include <fdtdec.h>
46#endif
47
3b73459e
GB
48#ifdef CONFIG_OF_LIBFDT
49#include <fdt_support.h>
50#endif /* CONFIG_OF_LIBFDT */
51
bdccc4fe 52#include <post.h>
4d91a6ec 53#include <linux/ctype.h>
317d6c57 54#include <menu.h>
bdccc4fe 55
597f6c26 56#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
d87080b7
WD
57DECLARE_GLOBAL_DATA_PTR;
58#endif
59
fad63407
HS
60/*
61 * Board-specific Platform code can reimplement show_boot_progress () if needed
62 */
63void inline __show_boot_progress (int val) {}
5e2c08c3 64void show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
fad63407 65
4bae9090 66#if defined(CONFIG_UPDATE_TFTP)
8d6b7320 67int update_tftp (ulong addr);
4bae9090 68#endif /* CONFIG_UPDATE_TFTP */
8bde7f77 69
c609719b
WD
70#define MAX_DELAY_STOP_STR 32
71
c609719b
WD
72#undef DEBUG_PARSER
73
6475b9f9 74char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
c609719b 75
3ca9122f 76static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
82359aec
MF
77static const char erase_seq[] = "\b \b"; /* erase sequence */
78static const char tab_seq[] = " "; /* used to expand TABs */
c609719b
WD
79
80#ifdef CONFIG_BOOT_RETRY_TIME
81static uint64_t endtime = 0; /* must be set, default is instant timeout */
82static int retry_time = -1; /* -1 so can call readline before main_loop */
83#endif
84
85#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
86
87#ifndef CONFIG_BOOT_RETRY_MIN
88#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
89#endif
90
91#ifdef CONFIG_MODEM_SUPPORT
92int do_mdm_init = 0;
93extern void mdm_init(void); /* defined in board.c */
94#endif
95
96/***************************************************************************
97 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
c8a2079e 98 * returns: 0 - no key string, allow autoboot 1 - got key string, abort
c609719b
WD
99 */
100#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
101# if defined(CONFIG_AUTOBOOT_KEYED)
b41bc5a8
JH
102#ifndef CONFIG_MENU
103static inline
104#endif
105int abortboot(int bootdelay)
c609719b
WD
106{
107 int abort = 0;
108 uint64_t etime = endtick(bootdelay);
19973b6a 109 struct {
c609719b
WD
110 char* str;
111 u_int len;
112 int retry;
113 }
19973b6a 114 delaykey [] = {
c609719b
WD
115 { str: getenv ("bootdelaykey"), retry: 1 },
116 { str: getenv ("bootdelaykey2"), retry: 1 },
117 { str: getenv ("bootstopkey"), retry: 0 },
118 { str: getenv ("bootstopkey2"), retry: 0 },
119 };
120
121 char presskey [MAX_DELAY_STOP_STR];
122 u_int presskey_len = 0;
123 u_int presskey_max = 0;
124 u_int i;
125
a5aae0a1
DE
126#ifndef CONFIG_ZERO_BOOTDELAY_CHECK
127 if (bootdelay == 0)
128 return 0;
129#endif
130
c609719b 131# ifdef CONFIG_AUTOBOOT_PROMPT
f2302d44 132 printf(CONFIG_AUTOBOOT_PROMPT);
c609719b
WD
133# endif
134
135# ifdef CONFIG_AUTOBOOT_DELAY_STR
136 if (delaykey[0].str == NULL)
137 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
138# endif
139# ifdef CONFIG_AUTOBOOT_DELAY_STR2
140 if (delaykey[1].str == NULL)
141 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
142# endif
143# ifdef CONFIG_AUTOBOOT_STOP_STR
144 if (delaykey[2].str == NULL)
145 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
146# endif
147# ifdef CONFIG_AUTOBOOT_STOP_STR2
148 if (delaykey[3].str == NULL)
149 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
150# endif
151
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;
157
158 presskey_max = presskey_max > delaykey[i].len ?
159 presskey_max : delaykey[i].len;
160
161# if DEBUG_BOOTKEYS
162 printf("%s key:<%s>\n",
163 delaykey[i].retry ? "delay" : "stop",
164 delaykey[i].str ? delaykey[i].str : "NULL");
165# endif
166 }
167
168 /* In order to keep up with incoming data, check timeout only
169 * when catch up.
170 */
c3284b03
PK
171 do {
172 if (tstc()) {
173 if (presskey_len < presskey_max) {
174 presskey [presskey_len ++] = getc();
175 }
176 else {
177 for (i = 0; i < presskey_max - 1; i ++)
178 presskey [i] = presskey [i + 1];
179
180 presskey [i] = getc();
181 }
182 }
183
c609719b
WD
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,
8bde7f77 188 delaykey[i].str,
c609719b
WD
189 delaykey[i].len) == 0) {
190# if DEBUG_BOOTKEYS
191 printf("got %skey\n",
192 delaykey[i].retry ? "delay" : "stop");
193# endif
194
195# ifdef CONFIG_BOOT_RETRY_TIME
196 /* don't retry auto boot */
197 if (! delaykey[i].retry)
198 retry_time = -1;
199# endif
200 abort = 1;
201 }
202 }
c3284b03 203 } while (!abort && get_ticks() <= etime);
c609719b 204
c609719b
WD
205# if DEBUG_BOOTKEYS
206 if (!abort)
ada4d400 207 puts("key timeout\n");
c609719b
WD
208# endif
209
8cb8143e 210#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
211 if (abort)
212 gd->flags &= ~GD_FLG_SILENT;
8cb8143e 213#endif
214
c609719b
WD
215 return abort;
216}
217
218# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
219
c7de829c
WD
220#ifdef CONFIG_MENUKEY
221static int menukey = 0;
222#endif
223
b41bc5a8
JH
224#ifndef CONFIG_MENU
225static inline
226#endif
227int abortboot(int bootdelay)
c609719b
WD
228{
229 int abort = 0;
230
c7de829c 231#ifdef CONFIG_MENUPROMPT
f2302d44 232 printf(CONFIG_MENUPROMPT);
c7de829c 233#else
93d7212f
JH
234 if (bootdelay >= 0)
235 printf("Hit any key to stop autoboot: %2d ", bootdelay);
c7de829c 236#endif
c609719b
WD
237
238#if defined CONFIG_ZERO_BOOTDELAY_CHECK
8bde7f77
WD
239 /*
240 * Check if key already pressed
241 * Don't check if bootdelay < 0
242 */
c609719b
WD
243 if (bootdelay >= 0) {
244 if (tstc()) { /* we got a key press */
245 (void) getc(); /* consume input */
4b9206ed 246 puts ("\b\b\b 0");
4ec5bd55 247 abort = 1; /* don't auto boot */
c609719b 248 }
8bde7f77 249 }
c609719b
WD
250#endif
251
f72da340 252 while ((bootdelay > 0) && (!abort)) {
c609719b
WD
253 int i;
254
255 --bootdelay;
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 */
c7de829c
WD
261# ifdef CONFIG_MENUKEY
262 menukey = getc();
263# else
c609719b 264 (void) getc(); /* consume input */
c7de829c 265# endif
c609719b
WD
266 break;
267 }
ada4d400 268 udelay(10000);
c609719b
WD
269 }
270
ada4d400 271 printf("\b\b\b%2d ", bootdelay);
c609719b
WD
272 }
273
ada4d400 274 putc('\n');
c609719b 275
f72da340 276#ifdef CONFIG_SILENT_CONSOLE
4ec5bd55
LM
277 if (abort)
278 gd->flags &= ~GD_FLG_SILENT;
f72da340
WD
279#endif
280
c609719b
WD
281 return abort;
282}
283# endif /* CONFIG_AUTOBOOT_KEYED */
284#endif /* CONFIG_BOOTDELAY >= 0 */
285
286/****************************************************************************/
287
288void main_loop (void)
289{
6d0f6bcf
JCPV
290#ifndef CONFIG_SYS_HUSH_PARSER
291 static char lastcommand[CONFIG_SYS_CBSIZE] = { 0, };
c609719b
WD
292 int len;
293 int rc = 1;
294 int flag;
295#endif
224b72e6
CLC
296#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0) && \
297 defined(CONFIG_OF_CONTROL)
298 char *env;
299#endif
c609719b
WD
300#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
301 char *s;
302 int bootdelay;
303#endif
304#ifdef CONFIG_PREBOOT
305 char *p;
306#endif
bdccc4fe
WD
307#ifdef CONFIG_BOOTCOUNT_LIMIT
308 unsigned long bootcount = 0;
309 unsigned long bootlimit = 0;
310 char *bcs;
311 char bcs_set[16];
312#endif /* CONFIG_BOOTCOUNT_LIMIT */
c609719b 313
bdccc4fe
WD
314#ifdef CONFIG_BOOTCOUNT_LIMIT
315 bootcount = bootcount_load();
316 bootcount++;
317 bootcount_store (bootcount);
318 sprintf (bcs_set, "%lu", bootcount);
319 setenv ("bootcount", bcs_set);
320 bcs = getenv ("bootlimit");
321 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
322#endif /* CONFIG_BOOTCOUNT_LIMIT */
323
c609719b
WD
324#ifdef CONFIG_MODEM_SUPPORT
325 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
326 if (do_mdm_init) {
77ddac94 327 char *str = strdup(getenv("mdm_cmd"));
c609719b
WD
328 setenv ("preboot", str); /* set or delete definition */
329 if (str != NULL)
330 free (str);
331 mdm_init(); /* wait for modem connection */
332 }
333#endif /* CONFIG_MODEM_SUPPORT */
334
0587597c
SR
335#ifdef CONFIG_VERSION_VARIABLE
336 {
155cb010 337 setenv ("ver", version_string); /* set version variable */
0587597c
SR
338 }
339#endif /* CONFIG_VERSION_VARIABLE */
340
6d0f6bcf 341#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
342 u_boot_hush_start ();
343#endif
344
81473f67
HS
345#if defined(CONFIG_HUSH_INIT_VAR)
346 hush_init_var ();
347#endif
348
c609719b
WD
349#ifdef CONFIG_PREBOOT
350 if ((p = getenv ("preboot")) != NULL) {
351# ifdef CONFIG_AUTOBOOT_KEYED
352 int prev = disable_ctrlc(1); /* disable Control C checking */
353# endif
354
3a8a02be 355 run_command_list(p, -1, 0);
c609719b
WD
356
357# ifdef CONFIG_AUTOBOOT_KEYED
358 disable_ctrlc(prev); /* restore Control C checking */
359# endif
360 }
361#endif /* CONFIG_PREBOOT */
362
143cd21f 363#if defined(CONFIG_UPDATE_TFTP)
8d6b7320 364 update_tftp (0UL);
143cd21f
WD
365#endif /* CONFIG_UPDATE_TFTP */
366
c609719b
WD
367#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
368 s = getenv ("bootdelay");
369 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
370
a6c7ad2f 371 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
c609719b 372
317d6c57
HS
373#if defined(CONFIG_MENU_SHOW)
374 bootdelay = menu_show(bootdelay);
375#endif
c609719b 376# ifdef CONFIG_BOOT_RETRY_TIME
6dd652fa 377 init_cmd_timeout ();
c609719b
WD
378# endif /* CONFIG_BOOT_RETRY_TIME */
379
b428f6a8
YT
380#ifdef CONFIG_POST
381 if (gd->flags & GD_FLG_POSTFAIL) {
382 s = getenv("failbootcmd");
383 }
384 else
385#endif /* CONFIG_POST */
bdccc4fe
WD
386#ifdef CONFIG_BOOTCOUNT_LIMIT
387 if (bootlimit && (bootcount > bootlimit)) {
388 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
389 (unsigned)bootlimit);
390 s = getenv ("altbootcmd");
391 }
392 else
393#endif /* CONFIG_BOOTCOUNT_LIMIT */
394 s = getenv ("bootcmd");
224b72e6
CLC
395#ifdef CONFIG_OF_CONTROL
396 /* Allow the fdt to override the boot command */
397 env = fdtdec_get_config_string(gd->fdt_blob, "bootcmd");
398 if (env)
399 s = env;
400#endif /* CONFIG_OF_CONTROL */
a6c7ad2f
WD
401
402 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
403
93d7212f 404 if (bootdelay != -1 && s && !abortboot(bootdelay)) {
c609719b
WD
405# ifdef CONFIG_AUTOBOOT_KEYED
406 int prev = disable_ctrlc(1); /* disable Control C checking */
407# endif
408
3a8a02be 409 run_command_list(s, -1, 0);
c609719b
WD
410
411# ifdef CONFIG_AUTOBOOT_KEYED
412 disable_ctrlc(prev); /* restore Control C checking */
413# endif
414 }
c7de829c
WD
415
416# ifdef CONFIG_MENUKEY
a6c7ad2f 417 if (menukey == CONFIG_MENUKEY) {
370d1e3e 418 s = getenv("menucmd");
c8a2079e 419 if (s)
3a8a02be 420 run_command_list(s, -1, 0);
c7de829c
WD
421 }
422#endif /* CONFIG_MENUKEY */
953b7e62 423#endif /* CONFIG_BOOTDELAY */
c7de829c 424
3b73459e
GB
425#if defined CONFIG_OF_CONTROL
426 set_working_fdt_addr((void *)gd->fdt_blob);
427#endif /* CONFIG_OF_CONTROL */
428
c609719b
WD
429 /*
430 * Main Loop for Monitor Command Processing
431 */
6d0f6bcf 432#ifdef CONFIG_SYS_HUSH_PARSER
c609719b
WD
433 parse_file_outer();
434 /* This point is never reached */
435 for (;;);
436#else
437 for (;;) {
438#ifdef CONFIG_BOOT_RETRY_TIME
439 if (rc >= 0) {
440 /* Saw enough of a valid command to
441 * restart the timeout.
442 */
443 reset_cmd_timeout();
444 }
445#endif
6d0f6bcf 446 len = readline (CONFIG_SYS_PROMPT);
c609719b
WD
447
448 flag = 0; /* assume no special flags for now */
449 if (len > 0)
450 strcpy (lastcommand, console_buffer);
451 else if (len == 0)
452 flag |= CMD_FLAG_REPEAT;
453#ifdef CONFIG_BOOT_RETRY_TIME
454 else if (len == -2) {
455 /* -2 means timed out, retry autoboot
456 */
4b9206ed 457 puts ("\nTimed out waiting for command\n");
c609719b
WD
458# ifdef CONFIG_RESET_TO_RETRY
459 /* Reinit board to run initialization code again */
460 do_reset (NULL, 0, 0, NULL);
461# else
462 return; /* retry autoboot */
463# endif
464 }
465#endif
466
467 if (len == -1)
4b9206ed 468 puts ("<INTERRUPT>\n");
c609719b 469 else
53071532 470 rc = run_command(lastcommand, flag);
c609719b
WD
471
472 if (rc <= 0) {
473 /* invalid command or not repeatable, forget it */
474 lastcommand[0] = 0;
475 }
476 }
6d0f6bcf 477#endif /*CONFIG_SYS_HUSH_PARSER*/
c609719b
WD
478}
479
6dd652fa
WD
480#ifdef CONFIG_BOOT_RETRY_TIME
481/***************************************************************************
19973b6a 482 * initialize command line timeout
6dd652fa
WD
483 */
484void init_cmd_timeout(void)
485{
486 char *s = getenv ("bootretry");
487
488 if (s != NULL)
b028f715 489 retry_time = (int)simple_strtol(s, NULL, 10);
6dd652fa
WD
490 else
491 retry_time = CONFIG_BOOT_RETRY_TIME;
492
493 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
494 retry_time = CONFIG_BOOT_RETRY_MIN;
495}
496
c609719b
WD
497/***************************************************************************
498 * reset command line timeout to retry_time seconds
499 */
c609719b
WD
500void reset_cmd_timeout(void)
501{
502 endtime = endtick(retry_time);
503}
504#endif
505
501090aa
WD
506#ifdef CONFIG_CMDLINE_EDITING
507
508/*
509 * cmdline-editing related codes from vivi.
510 * Author: Janghoon Lyu <[email protected]>
511 */
512
501090aa 513#define putnstr(str,n) do { \
dc4b0b38 514 printf ("%.*s", (int)n, str); \
501090aa 515 } while (0)
501090aa
WD
516
517#define CTL_CH(c) ((c) - 'a' + 1)
501090aa
WD
518#define CTL_BACKSPACE ('\b')
519#define DEL ((char)255)
520#define DEL7 ((char)127)
521#define CREAD_HIST_CHAR ('!')
522
523#define getcmd_putch(ch) putc(ch)
524#define getcmd_getch() getc()
525#define getcmd_cbeep() getcmd_putch('\a')
526
527#define HIST_MAX 20
8804ae3b 528#define HIST_SIZE CONFIG_SYS_CBSIZE
501090aa 529
199adb60
KP
530static int hist_max;
531static int hist_add_idx;
501090aa 532static int hist_cur = -1;
199adb60 533static unsigned hist_num;
501090aa 534
199adb60
KP
535static char *hist_list[HIST_MAX];
536static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
501090aa
WD
537
538#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
539
540static void hist_init(void)
541{
542 int i;
543
544 hist_max = 0;
545 hist_add_idx = 0;
546 hist_cur = -1;
547 hist_num = 0;
548
549 for (i = 0; i < HIST_MAX; i++) {
550 hist_list[i] = hist_lines[i];
551 hist_list[i][0] = '\0';
552 }
553}
554
555static void cread_add_to_hist(char *line)
556{
557 strcpy(hist_list[hist_add_idx], line);
558
559 if (++hist_add_idx >= HIST_MAX)
560 hist_add_idx = 0;
561
562 if (hist_add_idx > hist_max)
563 hist_max = hist_add_idx;
564
565 hist_num++;
566}
567
568static char* hist_prev(void)
569{
570 char *ret;
571 int old_cur;
572
573 if (hist_cur < 0)
574 return NULL;
575
576 old_cur = hist_cur;
577 if (--hist_cur < 0)
578 hist_cur = hist_max;
579
580 if (hist_cur == hist_add_idx) {
581 hist_cur = old_cur;
582 ret = NULL;
583 } else
584 ret = hist_list[hist_cur];
585
586 return (ret);
587}
588
589static char* hist_next(void)
590{
591 char *ret;
592
593 if (hist_cur < 0)
594 return NULL;
595
596 if (hist_cur == hist_add_idx)
597 return NULL;
598
599 if (++hist_cur > hist_max)
600 hist_cur = 0;
601
602 if (hist_cur == hist_add_idx) {
603 ret = "";
604 } else
605 ret = hist_list[hist_cur];
606
607 return (ret);
608}
609
3ca9122f 610#ifndef CONFIG_CMDLINE_EDITING
501090aa
WD
611static void cread_print_hist_list(void)
612{
613 int i;
614 unsigned long n;
615
616 n = hist_num - hist_max;
617
618 i = hist_add_idx + 1;
619 while (1) {
620 if (i > hist_max)
621 i = 0;
622 if (i == hist_add_idx)
623 break;
624 printf("%s\n", hist_list[i]);
625 n++;
626 i++;
627 }
628}
3ca9122f 629#endif /* CONFIG_CMDLINE_EDITING */
501090aa
WD
630
631#define BEGINNING_OF_LINE() { \
632 while (num) { \
633 getcmd_putch(CTL_BACKSPACE); \
634 num--; \
635 } \
636}
637
638#define ERASE_TO_EOL() { \
639 if (num < eol_num) { \
8faba489
MF
640 printf("%*s", (int)(eol_num - num), ""); \
641 do { \
501090aa 642 getcmd_putch(CTL_BACKSPACE); \
8faba489 643 } while (--eol_num > num); \
501090aa
WD
644 } \
645}
646
647#define REFRESH_TO_EOL() { \
648 if (num < eol_num) { \
649 wlen = eol_num - num; \
650 putnstr(buf + num, wlen); \
651 num = eol_num; \
652 } \
653}
654
655static void cread_add_char(char ichar, int insert, unsigned long *num,
656 unsigned long *eol_num, char *buf, unsigned long len)
657{
658 unsigned long wlen;
659
660 /* room ??? */
661 if (insert || *num == *eol_num) {
662 if (*eol_num > len - 1) {
663 getcmd_cbeep();
664 return;
665 }
666 (*eol_num)++;
667 }
668
669 if (insert) {
670 wlen = *eol_num - *num;
671 if (wlen > 1) {
672 memmove(&buf[*num+1], &buf[*num], wlen-1);
673 }
674
675 buf[*num] = ichar;
676 putnstr(buf + *num, wlen);
677 (*num)++;
678 while (--wlen) {
679 getcmd_putch(CTL_BACKSPACE);
680 }
681 } else {
682 /* echo the character */
683 wlen = 1;
684 buf[*num] = ichar;
685 putnstr(buf + *num, wlen);
686 (*num)++;
687 }
688}
689
690static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
691 unsigned long *eol_num, char *buf, unsigned long len)
692{
693 while (strsize--) {
694 cread_add_char(*str, insert, num, eol_num, buf, len);
695 str++;
696 }
697}
698
9c348311
HS
699static int cread_line(const char *const prompt, char *buf, unsigned int *len,
700 int timeout)
501090aa
WD
701{
702 unsigned long num = 0;
703 unsigned long eol_num = 0;
501090aa
WD
704 unsigned long wlen;
705 char ichar;
706 int insert = 1;
707 int esc_len = 0;
501090aa 708 char esc_save[8];
ecc5500e 709 int init_len = strlen(buf);
9c348311 710 int first = 1;
ecc5500e
PT
711
712 if (init_len)
713 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
501090aa
WD
714
715 while (1) {
00ac50e3
AE
716#ifdef CONFIG_BOOT_RETRY_TIME
717 while (!tstc()) { /* while no incoming data */
718 if (retry_time >= 0 && get_ticks() > endtime)
719 return (-2); /* timed out */
30dc165a 720 WATCHDOG_RESET();
00ac50e3
AE
721 }
722#endif
9c348311
HS
723 if (first && timeout) {
724 uint64_t etime = endtick(timeout);
725
726 while (!tstc()) { /* while no incoming data */
727 if (get_ticks() >= etime)
728 return -2; /* timed out */
729 WATCHDOG_RESET();
730 }
731 first = 0;
732 }
00ac50e3 733
501090aa
WD
734 ichar = getcmd_getch();
735
736 if ((ichar == '\n') || (ichar == '\r')) {
dd9f06f0 737 putc('\n');
501090aa
WD
738 break;
739 }
740
741 /*
742 * handle standard linux xterm esc sequences for arrow key, etc.
743 */
744 if (esc_len != 0) {
745 if (esc_len == 1) {
746 if (ichar == '[') {
747 esc_save[esc_len] = ichar;
748 esc_len = 2;
749 } else {
750 cread_add_str(esc_save, esc_len, insert,
751 &num, &eol_num, buf, *len);
752 esc_len = 0;
753 }
754 continue;
755 }
756
757 switch (ichar) {
758
759 case 'D': /* <- key */
760 ichar = CTL_CH('b');
761 esc_len = 0;
762 break;
763 case 'C': /* -> key */
764 ichar = CTL_CH('f');
765 esc_len = 0;
766 break; /* pass off to ^F handler */
767 case 'H': /* Home key */
768 ichar = CTL_CH('a');
769 esc_len = 0;
770 break; /* pass off to ^A handler */
771 case 'A': /* up arrow */
772 ichar = CTL_CH('p');
773 esc_len = 0;
774 break; /* pass off to ^P handler */
775 case 'B': /* down arrow */
776 ichar = CTL_CH('n');
777 esc_len = 0;
778 break; /* pass off to ^N handler */
779 default:
780 esc_save[esc_len++] = ichar;
781 cread_add_str(esc_save, esc_len, insert,
782 &num, &eol_num, buf, *len);
783 esc_len = 0;
784 continue;
785 }
786 }
787
788 switch (ichar) {
789 case 0x1b:
790 if (esc_len == 0) {
791 esc_save[esc_len] = ichar;
792 esc_len = 1;
793 } else {
dd9f06f0 794 puts("impossible condition #876\n");
501090aa
WD
795 esc_len = 0;
796 }
797 break;
798
799 case CTL_CH('a'):
800 BEGINNING_OF_LINE();
801 break;
802 case CTL_CH('c'): /* ^C - break */
803 *buf = '\0'; /* discard input */
804 return (-1);
805 case CTL_CH('f'):
806 if (num < eol_num) {
807 getcmd_putch(buf[num]);
808 num++;
809 }
810 break;
811 case CTL_CH('b'):
812 if (num) {
813 getcmd_putch(CTL_BACKSPACE);
814 num--;
815 }
816 break;
817 case CTL_CH('d'):
818 if (num < eol_num) {
819 wlen = eol_num - num - 1;
820 if (wlen) {
821 memmove(&buf[num], &buf[num+1], wlen);
822 putnstr(buf + num, wlen);
823 }
824
825 getcmd_putch(' ');
826 do {
827 getcmd_putch(CTL_BACKSPACE);
828 } while (wlen--);
829 eol_num--;
830 }
831 break;
832 case CTL_CH('k'):
833 ERASE_TO_EOL();
834 break;
835 case CTL_CH('e'):
836 REFRESH_TO_EOL();
837 break;
838 case CTL_CH('o'):
839 insert = !insert;
840 break;
841 case CTL_CH('x'):
23d0baf9 842 case CTL_CH('u'):
501090aa
WD
843 BEGINNING_OF_LINE();
844 ERASE_TO_EOL();
845 break;
846 case DEL:
847 case DEL7:
848 case 8:
849 if (num) {
850 wlen = eol_num - num;
851 num--;
852 memmove(&buf[num], &buf[num+1], wlen);
853 getcmd_putch(CTL_BACKSPACE);
854 putnstr(buf + num, wlen);
855 getcmd_putch(' ');
856 do {
857 getcmd_putch(CTL_BACKSPACE);
858 } while (wlen--);
859 eol_num--;
860 }
861 break;
862 case CTL_CH('p'):
863 case CTL_CH('n'):
864 {
865 char * hline;
866
867 esc_len = 0;
868
869 if (ichar == CTL_CH('p'))
870 hline = hist_prev();
871 else
872 hline = hist_next();
873
874 if (!hline) {
875 getcmd_cbeep();
876 continue;
877 }
878
879 /* nuke the current line */
880 /* first, go home */
881 BEGINNING_OF_LINE();
882
883 /* erase to end of line */
884 ERASE_TO_EOL();
885
886 /* copy new line into place and display */
887 strcpy(buf, hline);
888 eol_num = strlen(buf);
889 REFRESH_TO_EOL();
890 continue;
891 }
23d0baf9
JCPV
892#ifdef CONFIG_AUTO_COMPLETE
893 case '\t': {
894 int num2, col;
895
896 /* do not autocomplete when in the middle */
897 if (num < eol_num) {
898 getcmd_cbeep();
899 break;
900 }
901
902 buf[num] = '\0';
903 col = strlen(prompt) + eol_num;
904 num2 = num;
905 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
906 col = num2 - num;
907 num += col;
908 eol_num += col;
909 }
910 break;
911 }
912#endif
501090aa
WD
913 default:
914 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
915 break;
916 }
917 }
918 *len = eol_num;
919 buf[eol_num] = '\0'; /* lose the newline */
920
921 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
922 cread_add_to_hist(buf);
923 hist_cur = hist_add_idx;
924
f9239438 925 return 0;
501090aa
WD
926}
927
928#endif /* CONFIG_CMDLINE_EDITING */
929
c609719b
WD
930/****************************************************************************/
931
932/*
933 * Prompt for input and read a line.
934 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
935 * time out when time goes past endtime (timebase time in ticks).
936 * Return: number of read characters
937 * -1 if break
938 * -2 if timed out
939 */
940int readline (const char *const prompt)
941{
ecc5500e
PT
942 /*
943 * If console_buffer isn't 0-length the user will be prompted to modify
944 * it instead of entering it from scratch as desired.
945 */
946 console_buffer[0] = '\0';
947
9c348311 948 return readline_into_buffer(prompt, console_buffer, 0);
6636b62a
JY
949}
950
951
9c348311 952int readline_into_buffer(const char *const prompt, char *buffer, int timeout)
6636b62a
JY
953{
954 char *p = buffer;
501090aa 955#ifdef CONFIG_CMDLINE_EDITING
8804ae3b 956 unsigned int len = CONFIG_SYS_CBSIZE;
d8f961bb 957 int rc;
501090aa
WD
958 static int initted = 0;
959
597f6c26
JY
960 /*
961 * History uses a global array which is not
962 * writable until after relocation to RAM.
963 * Revert to non-history version if still
964 * running from flash.
965 */
966 if (gd->flags & GD_FLG_RELOC) {
967 if (!initted) {
968 hist_init();
969 initted = 1;
970 }
971
e491a71e
PT
972 if (prompt)
973 puts (prompt);
597f6c26 974
9c348311 975 rc = cread_line(prompt, p, &len, timeout);
597f6c26
JY
976 return rc < 0 ? rc : len;
977
978 } else {
979#endif /* CONFIG_CMDLINE_EDITING */
0ec59524 980 char * p_buf = p;
c609719b
WD
981 int n = 0; /* buffer index */
982 int plen = 0; /* prompt length */
983 int col; /* output column cnt */
984 char c;
985
986 /* print prompt */
987 if (prompt) {
988 plen = strlen (prompt);
989 puts (prompt);
990 }
991 col = plen;
992
993 for (;;) {
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 */
30dc165a 998 WATCHDOG_RESET();
c609719b
WD
999 }
1000#endif
1001 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
1002
1003#ifdef CONFIG_SHOW_ACTIVITY
1004 while (!tstc()) {
c609719b 1005 show_activity(0);
30dc165a 1006 WATCHDOG_RESET();
c609719b
WD
1007 }
1008#endif
1009 c = getc();
1010
1011 /*
1012 * Special character handling
1013 */
1014 switch (c) {
1015 case '\r': /* Enter */
1016 case '\n':
1017 *p = '\0';
1018 puts ("\r\n");
6636b62a 1019 return (p - p_buf);
c609719b 1020
27aa8186
WD
1021 case '\0': /* nul */
1022 continue;
1023
c609719b 1024 case 0x03: /* ^C - break */
6636b62a 1025 p_buf[0] = '\0'; /* discard input */
c609719b
WD
1026 return (-1);
1027
1028 case 0x15: /* ^U - erase line */
1029 while (col > plen) {
1030 puts (erase_seq);
1031 --col;
1032 }
6636b62a 1033 p = p_buf;
c609719b
WD
1034 n = 0;
1035 continue;
1036
1636d1c8 1037 case 0x17: /* ^W - erase word */
6636b62a 1038 p=delete_char(p_buf, p, &col, &n, plen);
c609719b 1039 while ((n > 0) && (*p != ' ')) {
6636b62a 1040 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1041 }
1042 continue;
1043
1044 case 0x08: /* ^H - backspace */
1045 case 0x7F: /* DEL - backspace */
6636b62a 1046 p=delete_char(p_buf, p, &col, &n, plen);
c609719b
WD
1047 continue;
1048
1049 default:
1050 /*
1051 * Must be a normal character then
1052 */
6d0f6bcf 1053 if (n < CONFIG_SYS_CBSIZE-2) {
c609719b 1054 if (c == '\t') { /* expand TABs */
04a85b3b
WD
1055#ifdef CONFIG_AUTO_COMPLETE
1056 /* if auto completion triggered just continue */
1057 *p = '\0';
1058 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
6636b62a 1059 p = p_buf + n; /* reset */
04a85b3b
WD
1060 continue;
1061 }
1062#endif
c609719b
WD
1063 puts (tab_seq+(col&07));
1064 col += 8 - (col&07);
1065 } else {
1066 ++col; /* echo input */
1067 putc (c);
1068 }
1069 *p++ = c;
1070 ++n;
1071 } else { /* Buffer full */
1072 putc ('\a');
1073 }
1074 }
1075 }
597f6c26
JY
1076#ifdef CONFIG_CMDLINE_EDITING
1077 }
1078#endif
c609719b
WD
1079}
1080
1081/****************************************************************************/
1082
1083static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1084{
1085 char *s;
1086
1087 if (*np == 0) {
1088 return (p);
1089 }
1090
1091 if (*(--p) == '\t') { /* will retype the whole line */
1092 while (*colp > plen) {
1093 puts (erase_seq);
1094 (*colp)--;
1095 }
1096 for (s=buffer; s<p; ++s) {
1097 if (*s == '\t') {
1098 puts (tab_seq+((*colp) & 07));
1099 *colp += 8 - ((*colp) & 07);
1100 } else {
1101 ++(*colp);
1102 putc (*s);
1103 }
1104 }
1105 } else {
1106 puts (erase_seq);
1107 (*colp)--;
1108 }
1109 (*np)--;
1110 return (p);
1111}
1112
1113/****************************************************************************/
1114
1115int parse_line (char *line, char *argv[])
1116{
1117 int nargs = 0;
1118
1119#ifdef DEBUG_PARSER
1120 printf ("parse_line: \"%s\"\n", line);
1121#endif
6d0f6bcf 1122 while (nargs < CONFIG_SYS_MAXARGS) {
c609719b
WD
1123
1124 /* skip any white space */
4d91a6ec 1125 while (isblank(*line))
c609719b 1126 ++line;
c609719b
WD
1127
1128 if (*line == '\0') { /* end of line, no more args */
1129 argv[nargs] = NULL;
1130#ifdef DEBUG_PARSER
1131 printf ("parse_line: nargs=%d\n", nargs);
1132#endif
1133 return (nargs);
1134 }
1135
1136 argv[nargs++] = line; /* begin of argument string */
1137
1138 /* find end of string */
4d91a6ec 1139 while (*line && !isblank(*line))
c609719b 1140 ++line;
c609719b
WD
1141
1142 if (*line == '\0') { /* end of line, no more args */
1143 argv[nargs] = NULL;
1144#ifdef DEBUG_PARSER
1145 printf ("parse_line: nargs=%d\n", nargs);
1146#endif
1147 return (nargs);
1148 }
1149
1150 *line++ = '\0'; /* terminate current arg */
1151 }
1152
6d0f6bcf 1153 printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
c609719b
WD
1154
1155#ifdef DEBUG_PARSER
1156 printf ("parse_line: nargs=%d\n", nargs);
1157#endif
1158 return (nargs);
1159}
1160
1161/****************************************************************************/
1162
7fed89e0 1163#ifndef CONFIG_SYS_HUSH_PARSER
c609719b
WD
1164static void process_macros (const char *input, char *output)
1165{
1166 char c, prev;
1167 const char *varname_start = NULL;
19973b6a 1168 int inputcnt = strlen (input);
6d0f6bcf 1169 int outputcnt = CONFIG_SYS_CBSIZE;
19973b6a
WD
1170 int state = 0; /* 0 = waiting for '$' */
1171
1172 /* 1 = waiting for '(' or '{' */
1173 /* 2 = waiting for ')' or '}' */
1174 /* 3 = waiting for ''' */
c609719b
WD
1175#ifdef DEBUG_PARSER
1176 char *output_start = output;
1177
19973b6a
WD
1178 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1179 input);
c609719b
WD
1180#endif
1181
19973b6a 1182 prev = '\0'; /* previous character */
c609719b
WD
1183
1184 while (inputcnt && outputcnt) {
8bde7f77 1185 c = *input++;
19973b6a
WD
1186 inputcnt--;
1187
1188 if (state != 3) {
1189 /* remove one level of escape characters */
1190 if ((c == '\\') && (prev != '\\')) {
1191 if (inputcnt-- == 0)
1192 break;
1193 prev = c;
1194 c = *input++;
1195 }
c609719b 1196 }
19973b6a
WD
1197
1198 switch (state) {
1199 case 0: /* Waiting for (unescaped) $ */
1200 if ((c == '\'') && (prev != '\\')) {
1201 state = 3;
1202 break;
1203 }
1204 if ((c == '$') && (prev != '\\')) {
1205 state++;
1206 } else {
c609719b
WD
1207 *(output++) = c;
1208 outputcnt--;
1209 }
19973b6a
WD
1210 break;
1211 case 1: /* Waiting for ( */
1212 if (c == '(' || c == '{') {
1213 state++;
1214 varname_start = input;
1215 } else {
1216 state = 0;
1217 *(output++) = '$';
1218 outputcnt--;
c609719b 1219
19973b6a
WD
1220 if (outputcnt) {
1221 *(output++) = c;
c609719b
WD
1222 outputcnt--;
1223 }
19973b6a
WD
1224 }
1225 break;
1226 case 2: /* Waiting for ) */
1227 if (c == ')' || c == '}') {
1228 int i;
6d0f6bcf 1229 char envname[CONFIG_SYS_CBSIZE], *envval;
19973b6a
WD
1230 int envcnt = input - varname_start - 1; /* Varname # of chars */
1231
1232 /* Get the varname */
1233 for (i = 0; i < envcnt; i++) {
1234 envname[i] = varname_start[i];
1235 }
1236 envname[i] = 0;
1237
1238 /* Get its value */
1239 envval = getenv (envname);
1240
1241 /* Copy into the line if it exists */
1242 if (envval != NULL)
1243 while ((*envval) && outputcnt) {
1244 *(output++) = *(envval++);
1245 outputcnt--;
1246 }
1247 /* Look for another '$' */
1248 state = 0;
1249 }
1250 break;
1251 case 3: /* Waiting for ' */
1252 if ((c == '\'') && (prev != '\\')) {
1253 state = 0;
1254 } else {
1255 *(output++) = c;
1256 outputcnt--;
1257 }
1258 break;
a25f862b 1259 }
19973b6a 1260 prev = c;
c609719b
WD
1261 }
1262
1263 if (outputcnt)
1264 *output = 0;
9160b96f
BS
1265 else
1266 *(output - 1) = 0;
c609719b
WD
1267
1268#ifdef DEBUG_PARSER
1269 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
19973b6a 1270 strlen (output_start), output_start);
c609719b
WD
1271#endif
1272}
1273
1274/****************************************************************************
1275 * returns:
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)
6d0f6bcf 1280 * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
c609719b
WD
1281 * considered unrecognized)
1282 *
1283 * WARNING:
1284 *
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).
1289 */
53071532 1290static int builtin_run_command(const char *cmd, int flag)
c609719b 1291{
6d0f6bcf 1292 char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
c609719b
WD
1293 char *token; /* start of token in cmdbuf */
1294 char *sep; /* end of token (separator) in cmdbuf */
6d0f6bcf 1295 char finaltoken[CONFIG_SYS_CBSIZE];
c609719b 1296 char *str = cmdbuf;
6d0f6bcf 1297 char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
f07771cc 1298 int argc, inquotes;
c609719b 1299 int repeatable = 1;
f07771cc 1300 int rc = 0;
c609719b
WD
1301
1302#ifdef DEBUG_PARSER
1303 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1304 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1305 puts ("\"\n");
1306#endif
1307
1308 clear_ctrlc(); /* forget any previous Control C */
1309
1310 if (!cmd || !*cmd) {
1311 return -1; /* empty command */
1312 }
1313
6d0f6bcf 1314 if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
c609719b
WD
1315 puts ("## Command too long!\n");
1316 return -1;
1317 }
1318
1319 strcpy (cmdbuf, cmd);
1320
1321 /* Process separators and check for invalid
1322 * repeatable commands
1323 */
1324
1325#ifdef DEBUG_PARSER
1326 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1327#endif
1328 while (*str) {
1329
1330 /*
1331 * Find separator, or string end
1332 * Allow simple escape of ';' by writing "\;"
1333 */
a25f862b
WD
1334 for (inquotes = 0, sep = str; *sep; sep++) {
1335 if ((*sep=='\'') &&
1336 (*(sep-1) != '\\'))
1337 inquotes=!inquotes;
1338
1339 if (!inquotes &&
1340 (*sep == ';') && /* separator */
c609719b
WD
1341 ( sep != str) && /* past string start */
1342 (*(sep-1) != '\\')) /* and NOT escaped */
1343 break;
1344 }
1345
1346 /*
1347 * Limit the token to data between separators
1348 */
1349 token = str;
1350 if (*sep) {
1351 str = sep + 1; /* start of command for next pass */
1352 *sep = '\0';
1353 }
1354 else
1355 str = sep; /* no more commands for next pass */
1356#ifdef DEBUG_PARSER
1357 printf ("token: \"%s\"\n", token);
1358#endif
1359
1360 /* find macros in this token and replace them */
1361 process_macros (token, finaltoken);
1362
1363 /* Extract arguments */
1264b405
WD
1364 if ((argc = parse_line (finaltoken, argv)) == 0) {
1365 rc = -1; /* no command at all */
1366 continue;
1367 }
c609719b 1368
030fca52
TK
1369 if (cmd_process(flag, argc, argv, &repeatable))
1370 rc = -1;
c609719b
WD
1371
1372 /* Did the user stop this? */
1373 if (had_ctrlc ())
5afb2020 1374 return -1; /* if stopped then not repeatable */
c609719b
WD
1375 }
1376
f07771cc 1377 return rc ? rc : repeatable;
c609719b 1378}
7fed89e0 1379#endif
c609719b 1380
53071532
SG
1381/*
1382 * Run a command using the selected parser.
1383 *
1384 * @param cmd Command to run
1385 * @param flag Execution flags (CMD_FLAG_...)
1386 * @return 0 on success, or != 0 on error.
1387 */
1388int run_command(const char *cmd, int flag)
1389{
1390#ifndef CONFIG_SYS_HUSH_PARSER
1391 /*
1392 * builtin_run_command can return 0 or 1 for success, so clean up
1393 * its result.
1394 */
1395 if (builtin_run_command(cmd, flag) == -1)
1396 return 1;
1397
1398 return 0;
1399#else
1400 return parse_string_outer(cmd,
1401 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP);
1402#endif
1403}
1404
d51004a8
SG
1405#ifndef CONFIG_SYS_HUSH_PARSER
1406/**
1407 * Execute a list of command separated by ; or \n using the built-in parser.
1408 *
1409 * This function cannot take a const char * for the command, since if it
1410 * finds newlines in the string, it replaces them with \0.
1411 *
1412 * @param cmd String containing list of commands
1413 * @param flag Execution flags (CMD_FLAG_...)
1414 * @return 0 on success, or != 0 on error.
1415 */
1416static int builtin_run_command_list(char *cmd, int flag)
1417{
1418 char *line, *next;
1419 int rcode = 0;
1420
1421 /*
1422 * Break into individual lines, and execute each line; terminate on
1423 * error.
1424 */
1425 line = next = cmd;
1426 while (*next) {
1427 if (*next == '\n') {
1428 *next = '\0';
1429 /* run only non-empty commands */
1430 if (*line) {
1431 debug("** exec: \"%s\"\n", line);
1432 if (builtin_run_command(line, 0) < 0) {
1433 rcode = 1;
1434 break;
1435 }
1436 }
1437 line = next + 1;
1438 }
1439 ++next;
1440 }
1441 if (rcode == 0 && *line)
1442 rcode = (builtin_run_command(line, 0) >= 0);
1443
1444 return rcode;
1445}
1446#endif
1447
1448int run_command_list(const char *cmd, int len, int flag)
1449{
1450 int need_buff = 1;
1451 char *buff = (char *)cmd; /* cast away const */
1452 int rcode = 0;
1453
1454 if (len == -1) {
1455 len = strlen(cmd);
1456#ifdef CONFIG_SYS_HUSH_PARSER
1457 /* hush will never change our string */
1458 need_buff = 0;
1459#else
1460 /* the built-in parser will change our string if it sees \n */
1461 need_buff = strchr(cmd, '\n') != NULL;
1462#endif
1463 }
1464 if (need_buff) {
1465 buff = malloc(len + 1);
1466 if (!buff)
1467 return 1;
1468 memcpy(buff, cmd, len);
1469 buff[len] = '\0';
1470 }
1471#ifdef CONFIG_SYS_HUSH_PARSER
1472 rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
1473#else
1474 /*
1475 * This function will overwrite any \n it sees with a \0, which
1476 * is why it can't work with a const char *. Here we are making
1477 * using of internal knowledge of this function, to avoid always
1478 * doing a malloc() which is actually required only in a case that
1479 * is pretty rare.
1480 */
1481 rcode = builtin_run_command_list(buff, flag);
1482 if (need_buff)
1483 free(buff);
1484#endif
1485
1486 return rcode;
1487}
1488
c609719b
WD
1489/****************************************************************************/
1490
c3517f91 1491#if defined(CONFIG_CMD_RUN)
54841ab5 1492int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
c609719b
WD
1493{
1494 int i;
c609719b 1495
47e26b1b 1496 if (argc < 2)
4c12eeb8 1497 return CMD_RET_USAGE;
c609719b
WD
1498
1499 for (i=1; i<argc; ++i) {
3e38691e
WD
1500 char *arg;
1501
1502 if ((arg = getenv (argv[i])) == NULL) {
1503 printf ("## Error: \"%s\" not defined\n", argv[i]);
1504 return 1;
1505 }
c8a2079e 1506
009dde19 1507 if (run_command(arg, flag) != 0)
3e38691e 1508 return 1;
c609719b 1509 }
3e38691e 1510 return 0;
c609719b 1511}
90253178 1512#endif
This page took 0.437354 seconds and 4 git commands to generate.