blob: a17b60b3aa92cecc9b4bbcaf78c83a84af8d7a8f [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
2 * (C) Copyright 2000
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk501090a2006-07-21 11:33:45 +02005 * Add to readline cmdline-editing by
6 * (C) Copyright 2005
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8 *
wdenkc6097192002-11-03 00:24:07 +00009 * 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
wdenka6c7ad22002-12-03 21:28:10 +000028/* #define DEBUG */
29
wdenkc6097192002-11-03 00:24:07 +000030#include <common.h>
31#include <watchdog.h>
32#include <command.h>
Wolfgang Denkd9631ec2005-09-30 15:18:23 +020033#ifdef CONFIG_MODEM_SUPPORT
34#include <malloc.h> /* for free() prototype */
35#endif
wdenkc6097192002-11-03 00:24:07 +000036
37#ifdef CFG_HUSH_PARSER
38#include <hush.h>
39#endif
40
wdenkbdccc4f2003-08-05 17:43:17 +000041#include <post.h>
42
James Yang597f6c22008-05-05 10:22:53 -050043#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
Wolfgang Denkd87080b2006-03-31 18:32:53 +020044DECLARE_GLOBAL_DATA_PTR;
45#endif
46
Heiko Schocherfad63402007-07-13 09:54:17 +020047/*
48 * Board-specific Platform code can reimplement show_boot_progress () if needed
49 */
50void inline __show_boot_progress (int val) {}
51void inline show_boot_progress (int val) __attribute__((weak, alias("__show_boot_progress")));
52
wdenk8bde7f72003-06-27 21:31:46 +000053#if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
54extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
55#endif
56
57extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
58
59
wdenkc6097192002-11-03 00:24:07 +000060#define MAX_DELAY_STOP_STR 32
61
wdenkc6097192002-11-03 00:24:07 +000062#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
63static int abortboot(int);
64#endif
65
66#undef DEBUG_PARSER
67
68char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
69
Stefan Roese3ca91222006-07-27 16:11:19 +020070static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
wdenkc6097192002-11-03 00:24:07 +000071static char erase_seq[] = "\b \b"; /* erase sequence */
72static char tab_seq[] = " "; /* used to expand TABs */
73
74#ifdef CONFIG_BOOT_RETRY_TIME
75static uint64_t endtime = 0; /* must be set, default is instant timeout */
76static int retry_time = -1; /* -1 so can call readline before main_loop */
77#endif
78
79#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
80
81#ifndef CONFIG_BOOT_RETRY_MIN
82#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
83#endif
84
85#ifdef CONFIG_MODEM_SUPPORT
86int do_mdm_init = 0;
87extern void mdm_init(void); /* defined in board.c */
88#endif
89
90/***************************************************************************
91 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
92 * returns: 0 - no key string, allow autoboot
93 * 1 - got key string, abort
94 */
95#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
96# if defined(CONFIG_AUTOBOOT_KEYED)
97static __inline__ int abortboot(int bootdelay)
98{
99 int abort = 0;
100 uint64_t etime = endtick(bootdelay);
Wolfgang Denk19973b62006-10-28 00:38:39 +0200101 struct {
wdenkc6097192002-11-03 00:24:07 +0000102 char* str;
103 u_int len;
104 int retry;
105 }
Wolfgang Denk19973b62006-10-28 00:38:39 +0200106 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000107 { str: getenv ("bootdelaykey"), retry: 1 },
108 { str: getenv ("bootdelaykey2"), retry: 1 },
109 { str: getenv ("bootstopkey"), retry: 0 },
110 { str: getenv ("bootstopkey2"), retry: 0 },
111 };
112
113 char presskey [MAX_DELAY_STOP_STR];
114 u_int presskey_len = 0;
115 u_int presskey_max = 0;
116 u_int i;
117
118# ifdef CONFIG_AUTOBOOT_PROMPT
Ladislav Michlada4d402007-04-25 16:01:26 +0200119 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000120# endif
121
122# ifdef CONFIG_AUTOBOOT_DELAY_STR
123 if (delaykey[0].str == NULL)
124 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
125# endif
126# ifdef CONFIG_AUTOBOOT_DELAY_STR2
127 if (delaykey[1].str == NULL)
128 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
129# endif
130# ifdef CONFIG_AUTOBOOT_STOP_STR
131 if (delaykey[2].str == NULL)
132 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
133# endif
134# ifdef CONFIG_AUTOBOOT_STOP_STR2
135 if (delaykey[3].str == NULL)
136 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
137# endif
138
139 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
140 delaykey[i].len = delaykey[i].str == NULL ?
141 0 : strlen (delaykey[i].str);
142 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
143 MAX_DELAY_STOP_STR : delaykey[i].len;
144
145 presskey_max = presskey_max > delaykey[i].len ?
146 presskey_max : delaykey[i].len;
147
148# if DEBUG_BOOTKEYS
149 printf("%s key:<%s>\n",
150 delaykey[i].retry ? "delay" : "stop",
151 delaykey[i].str ? delaykey[i].str : "NULL");
152# endif
153 }
154
155 /* In order to keep up with incoming data, check timeout only
156 * when catch up.
157 */
158 while (!abort && get_ticks() <= etime) {
159 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
160 if (delaykey[i].len > 0 &&
161 presskey_len >= delaykey[i].len &&
162 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk8bde7f72003-06-27 21:31:46 +0000163 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000164 delaykey[i].len) == 0) {
165# if DEBUG_BOOTKEYS
166 printf("got %skey\n",
167 delaykey[i].retry ? "delay" : "stop");
168# endif
169
170# ifdef CONFIG_BOOT_RETRY_TIME
171 /* don't retry auto boot */
172 if (! delaykey[i].retry)
173 retry_time = -1;
174# endif
175 abort = 1;
176 }
177 }
178
179 if (tstc()) {
180 if (presskey_len < presskey_max) {
181 presskey [presskey_len ++] = getc();
182 }
183 else {
184 for (i = 0; i < presskey_max - 1; i ++)
185 presskey [i] = presskey [i + 1];
186
187 presskey [i] = getc();
188 }
189 }
190 }
191# if DEBUG_BOOTKEYS
192 if (!abort)
Ladislav Michlada4d402007-04-25 16:01:26 +0200193 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000194# endif
195
dzu8cb81432003-10-24 13:14:45 +0000196#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200197 if (abort)
198 gd->flags &= ~GD_FLG_SILENT;
dzu8cb81432003-10-24 13:14:45 +0000199#endif
200
wdenkc6097192002-11-03 00:24:07 +0000201 return abort;
202}
203
204# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
205
wdenkc7de8292002-11-19 11:04:11 +0000206#ifdef CONFIG_MENUKEY
207static int menukey = 0;
208#endif
209
wdenkc6097192002-11-03 00:24:07 +0000210static __inline__ int abortboot(int bootdelay)
211{
212 int abort = 0;
213
wdenkc7de8292002-11-19 11:04:11 +0000214#ifdef CONFIG_MENUPROMPT
215 printf(CONFIG_MENUPROMPT, bootdelay);
216#else
wdenkc6097192002-11-03 00:24:07 +0000217 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenkc7de8292002-11-19 11:04:11 +0000218#endif
wdenkc6097192002-11-03 00:24:07 +0000219
220#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk8bde7f72003-06-27 21:31:46 +0000221 /*
222 * Check if key already pressed
223 * Don't check if bootdelay < 0
224 */
wdenkc6097192002-11-03 00:24:07 +0000225 if (bootdelay >= 0) {
226 if (tstc()) { /* we got a key press */
227 (void) getc(); /* consume input */
wdenk4b9206e2004-03-23 22:14:11 +0000228 puts ("\b\b\b 0");
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200229 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000230 }
wdenk8bde7f72003-06-27 21:31:46 +0000231 }
wdenkc6097192002-11-03 00:24:07 +0000232#endif
233
wdenkf72da342003-10-10 10:05:42 +0000234 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000235 int i;
236
237 --bootdelay;
238 /* delay 100 * 10ms */
239 for (i=0; !abort && i<100; ++i) {
240 if (tstc()) { /* we got a key press */
241 abort = 1; /* don't auto boot */
242 bootdelay = 0; /* no more delay */
wdenkc7de8292002-11-19 11:04:11 +0000243# ifdef CONFIG_MENUKEY
244 menukey = getc();
245# else
wdenkc6097192002-11-03 00:24:07 +0000246 (void) getc(); /* consume input */
wdenkc7de8292002-11-19 11:04:11 +0000247# endif
wdenkc6097192002-11-03 00:24:07 +0000248 break;
249 }
Ladislav Michlada4d402007-04-25 16:01:26 +0200250 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000251 }
252
Ladislav Michlada4d402007-04-25 16:01:26 +0200253 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000254 }
255
Ladislav Michlada4d402007-04-25 16:01:26 +0200256 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000257
wdenkf72da342003-10-10 10:05:42 +0000258#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl4ec5bd52007-04-25 16:01:26 +0200259 if (abort)
260 gd->flags &= ~GD_FLG_SILENT;
wdenkf72da342003-10-10 10:05:42 +0000261#endif
262
wdenkc6097192002-11-03 00:24:07 +0000263 return abort;
264}
265# endif /* CONFIG_AUTOBOOT_KEYED */
266#endif /* CONFIG_BOOTDELAY >= 0 */
267
268/****************************************************************************/
269
270void main_loop (void)
271{
272#ifndef CFG_HUSH_PARSER
273 static char lastcommand[CFG_CBSIZE] = { 0, };
274 int len;
275 int rc = 1;
276 int flag;
277#endif
278
279#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
280 char *s;
281 int bootdelay;
282#endif
283#ifdef CONFIG_PREBOOT
284 char *p;
285#endif
wdenkbdccc4f2003-08-05 17:43:17 +0000286#ifdef CONFIG_BOOTCOUNT_LIMIT
287 unsigned long bootcount = 0;
288 unsigned long bootlimit = 0;
289 char *bcs;
290 char bcs_set[16];
291#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000292
293#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
294 ulong bmp = 0; /* default bitmap */
295 extern int trab_vfd (ulong bitmap);
296
297#ifdef CONFIG_MODEM_SUPPORT
298 if (do_mdm_init)
299 bmp = 1; /* alternate bitmap */
300#endif
301 trab_vfd (bmp);
302#endif /* CONFIG_VFD && VFD_TEST_LOGO */
303
wdenkbdccc4f2003-08-05 17:43:17 +0000304#ifdef CONFIG_BOOTCOUNT_LIMIT
305 bootcount = bootcount_load();
306 bootcount++;
307 bootcount_store (bootcount);
308 sprintf (bcs_set, "%lu", bootcount);
309 setenv ("bootcount", bcs_set);
310 bcs = getenv ("bootlimit");
311 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
312#endif /* CONFIG_BOOTCOUNT_LIMIT */
313
wdenkc6097192002-11-03 00:24:07 +0000314#ifdef CONFIG_MODEM_SUPPORT
315 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
316 if (do_mdm_init) {
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200317 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000318 setenv ("preboot", str); /* set or delete definition */
319 if (str != NULL)
320 free (str);
321 mdm_init(); /* wait for modem connection */
322 }
323#endif /* CONFIG_MODEM_SUPPORT */
324
stroese05875972003-04-04 15:44:49 +0000325#ifdef CONFIG_VERSION_VARIABLE
326 {
327 extern char version_string[];
stroese05875972003-04-04 15:44:49 +0000328
stroese155cb012003-07-11 08:00:33 +0000329 setenv ("ver", version_string); /* set version variable */
stroese05875972003-04-04 15:44:49 +0000330 }
331#endif /* CONFIG_VERSION_VARIABLE */
332
wdenkc6097192002-11-03 00:24:07 +0000333#ifdef CFG_HUSH_PARSER
334 u_boot_hush_start ();
335#endif
336
wdenk04a85b32004-04-15 18:22:41 +0000337#ifdef CONFIG_AUTO_COMPLETE
338 install_auto_complete();
339#endif
340
wdenkc6097192002-11-03 00:24:07 +0000341#ifdef CONFIG_PREBOOT
342 if ((p = getenv ("preboot")) != NULL) {
343# ifdef CONFIG_AUTOBOOT_KEYED
344 int prev = disable_ctrlc(1); /* disable Control C checking */
345# endif
346
347# ifndef CFG_HUSH_PARSER
348 run_command (p, 0);
349# else
350 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
351 FLAG_EXIT_FROM_LOOP);
352# endif
353
354# ifdef CONFIG_AUTOBOOT_KEYED
355 disable_ctrlc(prev); /* restore Control C checking */
356# endif
357 }
358#endif /* CONFIG_PREBOOT */
359
360#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
361 s = getenv ("bootdelay");
362 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
363
wdenka6c7ad22002-12-03 21:28:10 +0000364 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000365
366# ifdef CONFIG_BOOT_RETRY_TIME
wdenk6dd652f2003-06-19 23:40:20 +0000367 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000368# endif /* CONFIG_BOOT_RETRY_TIME */
369
Yuri Tikhonovb428f6a2008-02-04 14:11:03 +0100370#ifdef CONFIG_POST
371 if (gd->flags & GD_FLG_POSTFAIL) {
372 s = getenv("failbootcmd");
373 }
374 else
375#endif /* CONFIG_POST */
wdenkbdccc4f2003-08-05 17:43:17 +0000376#ifdef CONFIG_BOOTCOUNT_LIMIT
377 if (bootlimit && (bootcount > bootlimit)) {
378 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
379 (unsigned)bootlimit);
380 s = getenv ("altbootcmd");
381 }
382 else
383#endif /* CONFIG_BOOTCOUNT_LIMIT */
384 s = getenv ("bootcmd");
wdenka6c7ad22002-12-03 21:28:10 +0000385
386 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
387
wdenkc6097192002-11-03 00:24:07 +0000388 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
389# ifdef CONFIG_AUTOBOOT_KEYED
390 int prev = disable_ctrlc(1); /* disable Control C checking */
391# endif
392
393# ifndef CFG_HUSH_PARSER
394 run_command (s, 0);
395# else
396 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
397 FLAG_EXIT_FROM_LOOP);
398# endif
399
400# ifdef CONFIG_AUTOBOOT_KEYED
401 disable_ctrlc(prev); /* restore Control C checking */
402# endif
403 }
wdenkc7de8292002-11-19 11:04:11 +0000404
405# ifdef CONFIG_MENUKEY
wdenka6c7ad22002-12-03 21:28:10 +0000406 if (menukey == CONFIG_MENUKEY) {
wdenkc7de8292002-11-19 11:04:11 +0000407 s = getenv("menucmd");
wdenka6c7ad22002-12-03 21:28:10 +0000408 if (s) {
wdenkc7de8292002-11-19 11:04:11 +0000409# ifndef CFG_HUSH_PARSER
wdenk6225c5d2005-01-09 23:33:49 +0000410 run_command (s, 0);
wdenkc7de8292002-11-19 11:04:11 +0000411# else
412 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
413 FLAG_EXIT_FROM_LOOP);
414# endif
415 }
416 }
417#endif /* CONFIG_MENUKEY */
wdenkc6097192002-11-03 00:24:07 +0000418#endif /* CONFIG_BOOTDELAY */
419
wdenkc7de8292002-11-19 11:04:11 +0000420#ifdef CONFIG_AMIGAONEG3SE
421 {
422 extern void video_banner(void);
423 video_banner();
424 }
425#endif
426
wdenkc6097192002-11-03 00:24:07 +0000427 /*
428 * Main Loop for Monitor Command Processing
429 */
430#ifdef CFG_HUSH_PARSER
431 parse_file_outer();
432 /* This point is never reached */
433 for (;;);
434#else
435 for (;;) {
436#ifdef CONFIG_BOOT_RETRY_TIME
437 if (rc >= 0) {
438 /* Saw enough of a valid command to
439 * restart the timeout.
440 */
441 reset_cmd_timeout();
442 }
443#endif
444 len = readline (CFG_PROMPT);
445
446 flag = 0; /* assume no special flags for now */
447 if (len > 0)
448 strcpy (lastcommand, console_buffer);
449 else if (len == 0)
450 flag |= CMD_FLAG_REPEAT;
451#ifdef CONFIG_BOOT_RETRY_TIME
452 else if (len == -2) {
453 /* -2 means timed out, retry autoboot
454 */
wdenk4b9206e2004-03-23 22:14:11 +0000455 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000456# ifdef CONFIG_RESET_TO_RETRY
457 /* Reinit board to run initialization code again */
458 do_reset (NULL, 0, 0, NULL);
459# else
460 return; /* retry autoboot */
461# endif
462 }
463#endif
464
465 if (len == -1)
wdenk4b9206e2004-03-23 22:14:11 +0000466 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000467 else
468 rc = run_command (lastcommand, flag);
469
470 if (rc <= 0) {
471 /* invalid command or not repeatable, forget it */
472 lastcommand[0] = 0;
473 }
474 }
475#endif /*CFG_HUSH_PARSER*/
476}
477
wdenk6dd652f2003-06-19 23:40:20 +0000478#ifdef CONFIG_BOOT_RETRY_TIME
479/***************************************************************************
Wolfgang Denk19973b62006-10-28 00:38:39 +0200480 * initialize command line timeout
wdenk6dd652f2003-06-19 23:40:20 +0000481 */
482void init_cmd_timeout(void)
483{
484 char *s = getenv ("bootretry");
485
486 if (s != NULL)
wdenkb028f712003-12-07 21:39:28 +0000487 retry_time = (int)simple_strtol(s, NULL, 10);
wdenk6dd652f2003-06-19 23:40:20 +0000488 else
489 retry_time = CONFIG_BOOT_RETRY_TIME;
490
491 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
492 retry_time = CONFIG_BOOT_RETRY_MIN;
493}
494
wdenkc6097192002-11-03 00:24:07 +0000495/***************************************************************************
496 * reset command line timeout to retry_time seconds
497 */
wdenkc6097192002-11-03 00:24:07 +0000498void reset_cmd_timeout(void)
499{
500 endtime = endtick(retry_time);
501}
502#endif
503
Wolfgang Denk501090a2006-07-21 11:33:45 +0200504#ifdef CONFIG_CMDLINE_EDITING
505
506/*
507 * cmdline-editing related codes from vivi.
508 * Author: Janghoon Lyu <nandy@mizi.com>
509 */
510
Wolfgang Denk501090a2006-07-21 11:33:45 +0200511#define putnstr(str,n) do { \
512 printf ("%.*s", n, str); \
513 } while (0)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200514
515#define CTL_CH(c) ((c) - 'a' + 1)
516
517#define MAX_CMDBUF_SIZE 256
518
519#define CTL_BACKSPACE ('\b')
520#define DEL ((char)255)
521#define DEL7 ((char)127)
522#define CREAD_HIST_CHAR ('!')
523
524#define getcmd_putch(ch) putc(ch)
525#define getcmd_getch() getc()
526#define getcmd_cbeep() getcmd_putch('\a')
527
528#define HIST_MAX 20
529#define HIST_SIZE MAX_CMDBUF_SIZE
530
531static int hist_max = 0;
532static int hist_add_idx = 0;
533static int hist_cur = -1;
534unsigned hist_num = 0;
535
536char* hist_list[HIST_MAX];
537char hist_lines[HIST_MAX][HIST_SIZE];
538
539#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
540
541static void hist_init(void)
542{
543 int i;
544
545 hist_max = 0;
546 hist_add_idx = 0;
547 hist_cur = -1;
548 hist_num = 0;
549
550 for (i = 0; i < HIST_MAX; i++) {
551 hist_list[i] = hist_lines[i];
552 hist_list[i][0] = '\0';
553 }
554}
555
556static void cread_add_to_hist(char *line)
557{
558 strcpy(hist_list[hist_add_idx], line);
559
560 if (++hist_add_idx >= HIST_MAX)
561 hist_add_idx = 0;
562
563 if (hist_add_idx > hist_max)
564 hist_max = hist_add_idx;
565
566 hist_num++;
567}
568
569static char* hist_prev(void)
570{
571 char *ret;
572 int old_cur;
573
574 if (hist_cur < 0)
575 return NULL;
576
577 old_cur = hist_cur;
578 if (--hist_cur < 0)
579 hist_cur = hist_max;
580
581 if (hist_cur == hist_add_idx) {
582 hist_cur = old_cur;
583 ret = NULL;
584 } else
585 ret = hist_list[hist_cur];
586
587 return (ret);
588}
589
590static char* hist_next(void)
591{
592 char *ret;
593
594 if (hist_cur < 0)
595 return NULL;
596
597 if (hist_cur == hist_add_idx)
598 return NULL;
599
600 if (++hist_cur > hist_max)
601 hist_cur = 0;
602
603 if (hist_cur == hist_add_idx) {
604 ret = "";
605 } else
606 ret = hist_list[hist_cur];
607
608 return (ret);
609}
610
Stefan Roese3ca91222006-07-27 16:11:19 +0200611#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200612static void cread_print_hist_list(void)
613{
614 int i;
615 unsigned long n;
616
617 n = hist_num - hist_max;
618
619 i = hist_add_idx + 1;
620 while (1) {
621 if (i > hist_max)
622 i = 0;
623 if (i == hist_add_idx)
624 break;
625 printf("%s\n", hist_list[i]);
626 n++;
627 i++;
628 }
629}
Stefan Roese3ca91222006-07-27 16:11:19 +0200630#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk501090a2006-07-21 11:33:45 +0200631
632#define BEGINNING_OF_LINE() { \
633 while (num) { \
634 getcmd_putch(CTL_BACKSPACE); \
635 num--; \
636 } \
637}
638
639#define ERASE_TO_EOL() { \
640 if (num < eol_num) { \
641 int tmp; \
642 for (tmp = num; tmp < eol_num; tmp++) \
643 getcmd_putch(' '); \
644 while (tmp-- > num) \
645 getcmd_putch(CTL_BACKSPACE); \
646 eol_num = num; \
647 } \
648}
649
650#define REFRESH_TO_EOL() { \
651 if (num < eol_num) { \
652 wlen = eol_num - num; \
653 putnstr(buf + num, wlen); \
654 num = eol_num; \
655 } \
656}
657
658static void cread_add_char(char ichar, int insert, unsigned long *num,
659 unsigned long *eol_num, char *buf, unsigned long len)
660{
661 unsigned long wlen;
662
663 /* room ??? */
664 if (insert || *num == *eol_num) {
665 if (*eol_num > len - 1) {
666 getcmd_cbeep();
667 return;
668 }
669 (*eol_num)++;
670 }
671
672 if (insert) {
673 wlen = *eol_num - *num;
674 if (wlen > 1) {
675 memmove(&buf[*num+1], &buf[*num], wlen-1);
676 }
677
678 buf[*num] = ichar;
679 putnstr(buf + *num, wlen);
680 (*num)++;
681 while (--wlen) {
682 getcmd_putch(CTL_BACKSPACE);
683 }
684 } else {
685 /* echo the character */
686 wlen = 1;
687 buf[*num] = ichar;
688 putnstr(buf + *num, wlen);
689 (*num)++;
690 }
691}
692
693static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
694 unsigned long *eol_num, char *buf, unsigned long len)
695{
696 while (strsize--) {
697 cread_add_char(*str, insert, num, eol_num, buf, len);
698 str++;
699 }
700}
701
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100702static int cread_line(const char *const prompt, char *buf, unsigned int *len)
Wolfgang Denk501090a2006-07-21 11:33:45 +0200703{
704 unsigned long num = 0;
705 unsigned long eol_num = 0;
706 unsigned long rlen;
707 unsigned long wlen;
708 char ichar;
709 int insert = 1;
710 int esc_len = 0;
711 int rc = 0;
712 char esc_save[8];
713
714 while (1) {
715 rlen = 1;
Andreas Engel00ac50e2008-01-09 17:10:56 +0100716#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 */
720 }
721#endif
722
Wolfgang Denk501090a2006-07-21 11:33:45 +0200723 ichar = getcmd_getch();
724
725 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200726 putc('\n');
Wolfgang Denk501090a2006-07-21 11:33:45 +0200727 break;
728 }
729
730 /*
731 * handle standard linux xterm esc sequences for arrow key, etc.
732 */
733 if (esc_len != 0) {
734 if (esc_len == 1) {
735 if (ichar == '[') {
736 esc_save[esc_len] = ichar;
737 esc_len = 2;
738 } else {
739 cread_add_str(esc_save, esc_len, insert,
740 &num, &eol_num, buf, *len);
741 esc_len = 0;
742 }
743 continue;
744 }
745
746 switch (ichar) {
747
748 case 'D': /* <- key */
749 ichar = CTL_CH('b');
750 esc_len = 0;
751 break;
752 case 'C': /* -> key */
753 ichar = CTL_CH('f');
754 esc_len = 0;
755 break; /* pass off to ^F handler */
756 case 'H': /* Home key */
757 ichar = CTL_CH('a');
758 esc_len = 0;
759 break; /* pass off to ^A handler */
760 case 'A': /* up arrow */
761 ichar = CTL_CH('p');
762 esc_len = 0;
763 break; /* pass off to ^P handler */
764 case 'B': /* down arrow */
765 ichar = CTL_CH('n');
766 esc_len = 0;
767 break; /* pass off to ^N handler */
768 default:
769 esc_save[esc_len++] = ichar;
770 cread_add_str(esc_save, esc_len, insert,
771 &num, &eol_num, buf, *len);
772 esc_len = 0;
773 continue;
774 }
775 }
776
777 switch (ichar) {
778 case 0x1b:
779 if (esc_len == 0) {
780 esc_save[esc_len] = ichar;
781 esc_len = 1;
782 } else {
Wolfgang Denkdd9f06f2006-07-21 11:34:34 +0200783 puts("impossible condition #876\n");
Wolfgang Denk501090a2006-07-21 11:33:45 +0200784 esc_len = 0;
785 }
786 break;
787
788 case CTL_CH('a'):
789 BEGINNING_OF_LINE();
790 break;
791 case CTL_CH('c'): /* ^C - break */
792 *buf = '\0'; /* discard input */
793 return (-1);
794 case CTL_CH('f'):
795 if (num < eol_num) {
796 getcmd_putch(buf[num]);
797 num++;
798 }
799 break;
800 case CTL_CH('b'):
801 if (num) {
802 getcmd_putch(CTL_BACKSPACE);
803 num--;
804 }
805 break;
806 case CTL_CH('d'):
807 if (num < eol_num) {
808 wlen = eol_num - num - 1;
809 if (wlen) {
810 memmove(&buf[num], &buf[num+1], wlen);
811 putnstr(buf + num, wlen);
812 }
813
814 getcmd_putch(' ');
815 do {
816 getcmd_putch(CTL_BACKSPACE);
817 } while (wlen--);
818 eol_num--;
819 }
820 break;
821 case CTL_CH('k'):
822 ERASE_TO_EOL();
823 break;
824 case CTL_CH('e'):
825 REFRESH_TO_EOL();
826 break;
827 case CTL_CH('o'):
828 insert = !insert;
829 break;
830 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100831 case CTL_CH('u'):
Wolfgang Denk501090a2006-07-21 11:33:45 +0200832 BEGINNING_OF_LINE();
833 ERASE_TO_EOL();
834 break;
835 case DEL:
836 case DEL7:
837 case 8:
838 if (num) {
839 wlen = eol_num - num;
840 num--;
841 memmove(&buf[num], &buf[num+1], wlen);
842 getcmd_putch(CTL_BACKSPACE);
843 putnstr(buf + num, wlen);
844 getcmd_putch(' ');
845 do {
846 getcmd_putch(CTL_BACKSPACE);
847 } while (wlen--);
848 eol_num--;
849 }
850 break;
851 case CTL_CH('p'):
852 case CTL_CH('n'):
853 {
854 char * hline;
855
856 esc_len = 0;
857
858 if (ichar == CTL_CH('p'))
859 hline = hist_prev();
860 else
861 hline = hist_next();
862
863 if (!hline) {
864 getcmd_cbeep();
865 continue;
866 }
867
868 /* nuke the current line */
869 /* first, go home */
870 BEGINNING_OF_LINE();
871
872 /* erase to end of line */
873 ERASE_TO_EOL();
874
875 /* copy new line into place and display */
876 strcpy(buf, hline);
877 eol_num = strlen(buf);
878 REFRESH_TO_EOL();
879 continue;
880 }
Jean-Christophe PLAGNIOL-VILLARD23d0baf2007-12-22 15:52:58 +0100881#ifdef CONFIG_AUTO_COMPLETE
882 case '\t': {
883 int num2, col;
884
885 /* do not autocomplete when in the middle */
886 if (num < eol_num) {
887 getcmd_cbeep();
888 break;
889 }
890
891 buf[num] = '\0';
892 col = strlen(prompt) + eol_num;
893 num2 = num;
894 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
895 col = num2 - num;
896 num += col;
897 eol_num += col;
898 }
899 break;
900 }
901#endif
Wolfgang Denk501090a2006-07-21 11:33:45 +0200902 default:
903 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
904 break;
905 }
906 }
907 *len = eol_num;
908 buf[eol_num] = '\0'; /* lose the newline */
909
910 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
911 cread_add_to_hist(buf);
912 hist_cur = hist_add_idx;
913
914 return (rc);
915}
916
917#endif /* CONFIG_CMDLINE_EDITING */
918
wdenkc6097192002-11-03 00:24:07 +0000919/****************************************************************************/
920
921/*
922 * Prompt for input and read a line.
923 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
924 * time out when time goes past endtime (timebase time in ticks).
925 * Return: number of read characters
926 * -1 if break
927 * -2 if timed out
928 */
929int readline (const char *const prompt)
930{
James Yang6636b622008-01-09 11:17:49 -0600931 return readline_into_buffer(prompt, console_buffer);
932}
933
934
935int readline_into_buffer (const char *const prompt, char * buffer)
936{
937 char *p = buffer;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200938#ifdef CONFIG_CMDLINE_EDITING
Wolfgang Denk501090a2006-07-21 11:33:45 +0200939 unsigned int len=MAX_CMDBUF_SIZE;
Stefan Roesed8f961b2006-08-07 15:08:44 +0200940 int rc;
Wolfgang Denk501090a2006-07-21 11:33:45 +0200941 static int initted = 0;
942
943 if (!initted) {
944 hist_init();
945 initted = 1;
946 }
947
Wolfgang Denk501090a2006-07-21 11:33:45 +0200948
James Yang597f6c22008-05-05 10:22:53 -0500949 /*
950 * History uses a global array which is not
951 * writable until after relocation to RAM.
952 * Revert to non-history version if still
953 * running from flash.
954 */
955 if (gd->flags & GD_FLG_RELOC) {
956 if (!initted) {
957 hist_init();
958 initted = 1;
959 }
960
961 puts (prompt);
962
963 rc = cread_line(prompt, p, &len);
964 return rc < 0 ? rc : len;
965
966 } else {
967#endif /* CONFIG_CMDLINE_EDITING */
Kumar Gala0ec59522008-01-10 02:22:05 -0600968 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000969 int n = 0; /* buffer index */
970 int plen = 0; /* prompt length */
971 int col; /* output column cnt */
972 char c;
973
974 /* print prompt */
975 if (prompt) {
976 plen = strlen (prompt);
977 puts (prompt);
978 }
979 col = plen;
980
981 for (;;) {
982#ifdef CONFIG_BOOT_RETRY_TIME
983 while (!tstc()) { /* while no incoming data */
984 if (retry_time >= 0 && get_ticks() > endtime)
985 return (-2); /* timed out */
986 }
987#endif
988 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
989
990#ifdef CONFIG_SHOW_ACTIVITY
991 while (!tstc()) {
992 extern void show_activity(int arg);
993 show_activity(0);
994 }
995#endif
996 c = getc();
997
998 /*
999 * Special character handling
1000 */
1001 switch (c) {
1002 case '\r': /* Enter */
1003 case '\n':
1004 *p = '\0';
1005 puts ("\r\n");
James Yang6636b622008-01-09 11:17:49 -06001006 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001007
wdenk27aa8182004-03-23 22:37:33 +00001008 case '\0': /* nul */
1009 continue;
1010
wdenkc6097192002-11-03 00:24:07 +00001011 case 0x03: /* ^C - break */
James Yang6636b622008-01-09 11:17:49 -06001012 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001013 return (-1);
1014
1015 case 0x15: /* ^U - erase line */
1016 while (col > plen) {
1017 puts (erase_seq);
1018 --col;
1019 }
James Yang6636b622008-01-09 11:17:49 -06001020 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001021 n = 0;
1022 continue;
1023
Wolfgang Denk1636d1c2007-06-22 23:59:00 +02001024 case 0x17: /* ^W - erase word */
James Yang6636b622008-01-09 11:17:49 -06001025 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001026 while ((n > 0) && (*p != ' ')) {
James Yang6636b622008-01-09 11:17:49 -06001027 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001028 }
1029 continue;
1030
1031 case 0x08: /* ^H - backspace */
1032 case 0x7F: /* DEL - backspace */
James Yang6636b622008-01-09 11:17:49 -06001033 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001034 continue;
1035
1036 default:
1037 /*
1038 * Must be a normal character then
1039 */
1040 if (n < CFG_CBSIZE-2) {
1041 if (c == '\t') { /* expand TABs */
wdenk04a85b32004-04-15 18:22:41 +00001042#ifdef CONFIG_AUTO_COMPLETE
1043 /* if auto completion triggered just continue */
1044 *p = '\0';
1045 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang6636b622008-01-09 11:17:49 -06001046 p = p_buf + n; /* reset */
wdenk04a85b32004-04-15 18:22:41 +00001047 continue;
1048 }
1049#endif
wdenkc6097192002-11-03 00:24:07 +00001050 puts (tab_seq+(col&07));
1051 col += 8 - (col&07);
1052 } else {
1053 ++col; /* echo input */
1054 putc (c);
1055 }
1056 *p++ = c;
1057 ++n;
1058 } else { /* Buffer full */
1059 putc ('\a');
1060 }
1061 }
1062 }
James Yang597f6c22008-05-05 10:22:53 -05001063#ifdef CONFIG_CMDLINE_EDITING
1064 }
1065#endif
wdenkc6097192002-11-03 00:24:07 +00001066}
1067
1068/****************************************************************************/
1069
1070static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1071{
1072 char *s;
1073
1074 if (*np == 0) {
1075 return (p);
1076 }
1077
1078 if (*(--p) == '\t') { /* will retype the whole line */
1079 while (*colp > plen) {
1080 puts (erase_seq);
1081 (*colp)--;
1082 }
1083 for (s=buffer; s<p; ++s) {
1084 if (*s == '\t') {
1085 puts (tab_seq+((*colp) & 07));
1086 *colp += 8 - ((*colp) & 07);
1087 } else {
1088 ++(*colp);
1089 putc (*s);
1090 }
1091 }
1092 } else {
1093 puts (erase_seq);
1094 (*colp)--;
1095 }
1096 (*np)--;
1097 return (p);
1098}
1099
1100/****************************************************************************/
1101
1102int parse_line (char *line, char *argv[])
1103{
1104 int nargs = 0;
1105
1106#ifdef DEBUG_PARSER
1107 printf ("parse_line: \"%s\"\n", line);
1108#endif
1109 while (nargs < CFG_MAXARGS) {
1110
1111 /* skip any white space */
1112 while ((*line == ' ') || (*line == '\t')) {
1113 ++line;
1114 }
1115
1116 if (*line == '\0') { /* end of line, no more args */
1117 argv[nargs] = NULL;
1118#ifdef DEBUG_PARSER
1119 printf ("parse_line: nargs=%d\n", nargs);
1120#endif
1121 return (nargs);
1122 }
1123
1124 argv[nargs++] = line; /* begin of argument string */
1125
1126 /* find end of string */
1127 while (*line && (*line != ' ') && (*line != '\t')) {
1128 ++line;
1129 }
1130
1131 if (*line == '\0') { /* end of line, no more args */
1132 argv[nargs] = NULL;
1133#ifdef DEBUG_PARSER
1134 printf ("parse_line: nargs=%d\n", nargs);
1135#endif
1136 return (nargs);
1137 }
1138
1139 *line++ = '\0'; /* terminate current arg */
1140 }
1141
1142 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1143
1144#ifdef DEBUG_PARSER
1145 printf ("parse_line: nargs=%d\n", nargs);
1146#endif
1147 return (nargs);
1148}
1149
1150/****************************************************************************/
1151
1152static void process_macros (const char *input, char *output)
1153{
1154 char c, prev;
1155 const char *varname_start = NULL;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001156 int inputcnt = strlen (input);
wdenkc6097192002-11-03 00:24:07 +00001157 int outputcnt = CFG_CBSIZE;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001158 int state = 0; /* 0 = waiting for '$' */
1159
1160 /* 1 = waiting for '(' or '{' */
1161 /* 2 = waiting for ')' or '}' */
1162 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001163#ifdef DEBUG_PARSER
1164 char *output_start = output;
1165
Wolfgang Denk19973b62006-10-28 00:38:39 +02001166 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1167 input);
wdenkc6097192002-11-03 00:24:07 +00001168#endif
1169
Wolfgang Denk19973b62006-10-28 00:38:39 +02001170 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001171
1172 while (inputcnt && outputcnt) {
wdenk8bde7f72003-06-27 21:31:46 +00001173 c = *input++;
Wolfgang Denk19973b62006-10-28 00:38:39 +02001174 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001175
Wolfgang Denk19973b62006-10-28 00:38:39 +02001176 if (state != 3) {
1177 /* remove one level of escape characters */
1178 if ((c == '\\') && (prev != '\\')) {
1179 if (inputcnt-- == 0)
1180 break;
1181 prev = c;
1182 c = *input++;
1183 }
wdenka25f8622003-01-02 23:57:29 +00001184 }
wdenkc6097192002-11-03 00:24:07 +00001185
Wolfgang Denk19973b62006-10-28 00:38:39 +02001186 switch (state) {
1187 case 0: /* Waiting for (unescaped) $ */
1188 if ((c == '\'') && (prev != '\\')) {
1189 state = 3;
1190 break;
1191 }
1192 if ((c == '$') && (prev != '\\')) {
1193 state++;
1194 } else {
wdenkc6097192002-11-03 00:24:07 +00001195 *(output++) = c;
1196 outputcnt--;
1197 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001198 break;
1199 case 1: /* Waiting for ( */
1200 if (c == '(' || c == '{') {
1201 state++;
1202 varname_start = input;
1203 } else {
1204 state = 0;
1205 *(output++) = '$';
1206 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001207
Wolfgang Denk19973b62006-10-28 00:38:39 +02001208 if (outputcnt) {
1209 *(output++) = c;
wdenkc6097192002-11-03 00:24:07 +00001210 outputcnt--;
1211 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001212 }
1213 break;
1214 case 2: /* Waiting for ) */
1215 if (c == ')' || c == '}') {
1216 int i;
1217 char envname[CFG_CBSIZE], *envval;
1218 int envcnt = input - varname_start - 1; /* Varname # of chars */
1219
1220 /* Get the varname */
1221 for (i = 0; i < envcnt; i++) {
1222 envname[i] = varname_start[i];
1223 }
1224 envname[i] = 0;
1225
1226 /* Get its value */
1227 envval = getenv (envname);
1228
1229 /* Copy into the line if it exists */
1230 if (envval != NULL)
1231 while ((*envval) && outputcnt) {
1232 *(output++) = *(envval++);
1233 outputcnt--;
1234 }
1235 /* Look for another '$' */
1236 state = 0;
1237 }
1238 break;
1239 case 3: /* Waiting for ' */
1240 if ((c == '\'') && (prev != '\\')) {
1241 state = 0;
1242 } else {
1243 *(output++) = c;
1244 outputcnt--;
1245 }
1246 break;
wdenkc6097192002-11-03 00:24:07 +00001247 }
Wolfgang Denk19973b62006-10-28 00:38:39 +02001248 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001249 }
1250
1251 if (outputcnt)
1252 *output = 0;
Bartlomiej Sieka9160b962007-05-27 17:04:18 +02001253 else
1254 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001255
1256#ifdef DEBUG_PARSER
1257 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk19973b62006-10-28 00:38:39 +02001258 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001259#endif
1260}
1261
1262/****************************************************************************
1263 * returns:
1264 * 1 - command executed, repeatable
1265 * 0 - command executed but not repeatable, interrupted commands are
1266 * always considered not repeatable
1267 * -1 - not executed (unrecognized, bootd recursion or too many args)
1268 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1269 * considered unrecognized)
1270 *
1271 * WARNING:
1272 *
1273 * We must create a temporary copy of the command since the command we get
1274 * may be the result from getenv(), which returns a pointer directly to
1275 * the environment data, which may change magicly when the command we run
1276 * creates or modifies environment variables (like "bootp" does).
1277 */
1278
1279int run_command (const char *cmd, int flag)
1280{
1281 cmd_tbl_t *cmdtp;
1282 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1283 char *token; /* start of token in cmdbuf */
1284 char *sep; /* end of token (separator) in cmdbuf */
1285 char finaltoken[CFG_CBSIZE];
1286 char *str = cmdbuf;
1287 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
wdenkf07771c2003-05-28 08:06:31 +00001288 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001289 int repeatable = 1;
wdenkf07771c2003-05-28 08:06:31 +00001290 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001291
1292#ifdef DEBUG_PARSER
1293 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1294 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1295 puts ("\"\n");
1296#endif
1297
1298 clear_ctrlc(); /* forget any previous Control C */
1299
1300 if (!cmd || !*cmd) {
1301 return -1; /* empty command */
1302 }
1303
1304 if (strlen(cmd) >= CFG_CBSIZE) {
1305 puts ("## Command too long!\n");
1306 return -1;
1307 }
1308
1309 strcpy (cmdbuf, cmd);
1310
1311 /* Process separators and check for invalid
1312 * repeatable commands
1313 */
1314
1315#ifdef DEBUG_PARSER
1316 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1317#endif
1318 while (*str) {
1319
1320 /*
1321 * Find separator, or string end
1322 * Allow simple escape of ';' by writing "\;"
1323 */
wdenka25f8622003-01-02 23:57:29 +00001324 for (inquotes = 0, sep = str; *sep; sep++) {
1325 if ((*sep=='\'') &&
1326 (*(sep-1) != '\\'))
1327 inquotes=!inquotes;
1328
1329 if (!inquotes &&
1330 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001331 ( sep != str) && /* past string start */
1332 (*(sep-1) != '\\')) /* and NOT escaped */
1333 break;
1334 }
1335
1336 /*
1337 * Limit the token to data between separators
1338 */
1339 token = str;
1340 if (*sep) {
1341 str = sep + 1; /* start of command for next pass */
1342 *sep = '\0';
1343 }
1344 else
1345 str = sep; /* no more commands for next pass */
1346#ifdef DEBUG_PARSER
1347 printf ("token: \"%s\"\n", token);
1348#endif
1349
1350 /* find macros in this token and replace them */
1351 process_macros (token, finaltoken);
1352
1353 /* Extract arguments */
Wolfgang Denk1264b402006-03-12 02:20:55 +01001354 if ((argc = parse_line (finaltoken, argv)) == 0) {
1355 rc = -1; /* no command at all */
1356 continue;
1357 }
wdenkc6097192002-11-03 00:24:07 +00001358
1359 /* Look up command in command table */
1360 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1361 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf07771c2003-05-28 08:06:31 +00001362 rc = -1; /* give up after bad command */
1363 continue;
wdenkc6097192002-11-03 00:24:07 +00001364 }
1365
1366 /* found - check max args */
1367 if (argc > cmdtp->maxargs) {
1368 printf ("Usage:\n%s\n", cmdtp->usage);
wdenkf07771c2003-05-28 08:06:31 +00001369 rc = -1;
1370 continue;
wdenkc6097192002-11-03 00:24:07 +00001371 }
1372
Jon Loeligerc3517f92007-07-08 18:10:08 -05001373#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001374 /* avoid "bootd" recursion */
1375 if (cmdtp->cmd == do_bootd) {
1376#ifdef DEBUG_PARSER
1377 printf ("[%s]\n", finaltoken);
1378#endif
1379 if (flag & CMD_FLAG_BOOTD) {
wdenk4b9206e2004-03-23 22:14:11 +00001380 puts ("'bootd' recursion detected\n");
wdenkf07771c2003-05-28 08:06:31 +00001381 rc = -1;
1382 continue;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001383 } else {
wdenkc6097192002-11-03 00:24:07 +00001384 flag |= CMD_FLAG_BOOTD;
Wolfgang Denk1264b402006-03-12 02:20:55 +01001385 }
wdenkc6097192002-11-03 00:24:07 +00001386 }
Jon Loeliger90253172007-07-10 11:02:44 -05001387#endif
wdenkc6097192002-11-03 00:24:07 +00001388
1389 /* OK - call function to do the command */
1390 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf07771c2003-05-28 08:06:31 +00001391 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001392 }
1393
1394 repeatable &= cmdtp->repeatable;
1395
1396 /* Did the user stop this? */
1397 if (had_ctrlc ())
Detlev Zundel5afb2022007-05-23 18:47:48 +02001398 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001399 }
1400
wdenkf07771c2003-05-28 08:06:31 +00001401 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001402}
1403
1404/****************************************************************************/
1405
Jon Loeligerc3517f92007-07-08 18:10:08 -05001406#if defined(CONFIG_CMD_RUN)
wdenkc6097192002-11-03 00:24:07 +00001407int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1408{
1409 int i;
wdenkc6097192002-11-03 00:24:07 +00001410
1411 if (argc < 2) {
1412 printf ("Usage:\n%s\n", cmdtp->usage);
1413 return 1;
1414 }
1415
1416 for (i=1; i<argc; ++i) {
wdenk3e386912003-04-05 00:53:31 +00001417 char *arg;
1418
1419 if ((arg = getenv (argv[i])) == NULL) {
1420 printf ("## Error: \"%s\" not defined\n", argv[i]);
1421 return 1;
1422 }
wdenkc6097192002-11-03 00:24:07 +00001423#ifndef CFG_HUSH_PARSER
wdenk3e386912003-04-05 00:53:31 +00001424 if (run_command (arg, flag) == -1)
1425 return 1;
wdenkc6097192002-11-03 00:24:07 +00001426#else
wdenk3e386912003-04-05 00:53:31 +00001427 if (parse_string_outer(arg,
wdenk7aa78612003-05-03 15:50:43 +00001428 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenk3e386912003-04-05 00:53:31 +00001429 return 1;
wdenkc6097192002-11-03 00:24:07 +00001430#endif
1431 }
wdenk3e386912003-04-05 00:53:31 +00001432 return 0;
wdenkc6097192002-11-03 00:24:07 +00001433}
Jon Loeliger90253172007-07-10 11:02:44 -05001434#endif