blob: 21e7afab61fde2db49a1de071baafec77bc6f6c3 [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 Denk4c9ca782006-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
wdenk9dd2b882002-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 Denkb0723792005-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
wdenk0a658552003-08-05 17:43:17 +000041#include <post.h>
42
Yuri Tikhonovd773efb2008-02-04 14:11:03 +010043#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST)
Wolfgang Denk6405a152006-03-31 18:32:53 +020044DECLARE_GLOBAL_DATA_PTR;
45#endif
46
Heiko Schocher8a8ec532007-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
wdenk57b2d802003-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 Roese59ff5a52006-07-27 16:11:19 +020070#ifndef CONFIG_CMDLINE_EDITING
71static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
wdenkc6097192002-11-03 00:24:07 +000072static char erase_seq[] = "\b \b"; /* erase sequence */
73static char tab_seq[] = " "; /* used to expand TABs */
Stefan Roese59ff5a52006-07-27 16:11:19 +020074#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +000075
76#ifdef CONFIG_BOOT_RETRY_TIME
77static uint64_t endtime = 0; /* must be set, default is instant timeout */
78static int retry_time = -1; /* -1 so can call readline before main_loop */
79#endif
80
81#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
82
83#ifndef CONFIG_BOOT_RETRY_MIN
84#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
85#endif
86
87#ifdef CONFIG_MODEM_SUPPORT
88int do_mdm_init = 0;
89extern void mdm_init(void); /* defined in board.c */
90#endif
91
92/***************************************************************************
93 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
94 * returns: 0 - no key string, allow autoboot
95 * 1 - got key string, abort
96 */
97#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
98# if defined(CONFIG_AUTOBOOT_KEYED)
99static __inline__ int abortboot(int bootdelay)
100{
101 int abort = 0;
102 uint64_t etime = endtick(bootdelay);
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200103 struct {
wdenkc6097192002-11-03 00:24:07 +0000104 char* str;
105 u_int len;
106 int retry;
107 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200108 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000109 { str: getenv ("bootdelaykey"), retry: 1 },
110 { str: getenv ("bootdelaykey2"), retry: 1 },
111 { str: getenv ("bootstopkey"), retry: 0 },
112 { str: getenv ("bootstopkey2"), retry: 0 },
113 };
114
115 char presskey [MAX_DELAY_STOP_STR];
116 u_int presskey_len = 0;
117 u_int presskey_max = 0;
118 u_int i;
119
120# ifdef CONFIG_AUTOBOOT_PROMPT
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200121 printf(CONFIG_AUTOBOOT_PROMPT, bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000122# endif
123
124# ifdef CONFIG_AUTOBOOT_DELAY_STR
125 if (delaykey[0].str == NULL)
126 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
127# endif
128# ifdef CONFIG_AUTOBOOT_DELAY_STR2
129 if (delaykey[1].str == NULL)
130 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
131# endif
132# ifdef CONFIG_AUTOBOOT_STOP_STR
133 if (delaykey[2].str == NULL)
134 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
135# endif
136# ifdef CONFIG_AUTOBOOT_STOP_STR2
137 if (delaykey[3].str == NULL)
138 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
139# endif
140
141 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
142 delaykey[i].len = delaykey[i].str == NULL ?
143 0 : strlen (delaykey[i].str);
144 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
145 MAX_DELAY_STOP_STR : delaykey[i].len;
146
147 presskey_max = presskey_max > delaykey[i].len ?
148 presskey_max : delaykey[i].len;
149
150# if DEBUG_BOOTKEYS
151 printf("%s key:<%s>\n",
152 delaykey[i].retry ? "delay" : "stop",
153 delaykey[i].str ? delaykey[i].str : "NULL");
154# endif
155 }
156
157 /* In order to keep up with incoming data, check timeout only
158 * when catch up.
159 */
160 while (!abort && get_ticks() <= etime) {
161 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
162 if (delaykey[i].len > 0 &&
163 presskey_len >= delaykey[i].len &&
164 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk57b2d802003-06-27 21:31:46 +0000165 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000166 delaykey[i].len) == 0) {
167# if DEBUG_BOOTKEYS
168 printf("got %skey\n",
169 delaykey[i].retry ? "delay" : "stop");
170# endif
171
172# ifdef CONFIG_BOOT_RETRY_TIME
173 /* don't retry auto boot */
174 if (! delaykey[i].retry)
175 retry_time = -1;
176# endif
177 abort = 1;
178 }
179 }
180
181 if (tstc()) {
182 if (presskey_len < presskey_max) {
183 presskey [presskey_len ++] = getc();
184 }
185 else {
186 for (i = 0; i < presskey_max - 1; i ++)
187 presskey [i] = presskey [i + 1];
188
189 presskey [i] = getc();
190 }
191 }
192 }
193# if DEBUG_BOOTKEYS
194 if (!abort)
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200195 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000196# endif
197
dzu6605c842003-10-24 13:14:45 +0000198#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200199 if (abort)
200 gd->flags &= ~GD_FLG_SILENT;
dzu6605c842003-10-24 13:14:45 +0000201#endif
202
wdenkc6097192002-11-03 00:24:07 +0000203 return abort;
204}
205
206# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
207
wdenk452cfd62002-11-19 11:04:11 +0000208#ifdef CONFIG_MENUKEY
209static int menukey = 0;
210#endif
211
wdenkc6097192002-11-03 00:24:07 +0000212static __inline__ int abortboot(int bootdelay)
213{
214 int abort = 0;
215
wdenk452cfd62002-11-19 11:04:11 +0000216#ifdef CONFIG_MENUPROMPT
217 printf(CONFIG_MENUPROMPT, bootdelay);
218#else
wdenkc6097192002-11-03 00:24:07 +0000219 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenk452cfd62002-11-19 11:04:11 +0000220#endif
wdenkc6097192002-11-03 00:24:07 +0000221
222#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk57b2d802003-06-27 21:31:46 +0000223 /*
224 * Check if key already pressed
225 * Don't check if bootdelay < 0
226 */
wdenkc6097192002-11-03 00:24:07 +0000227 if (bootdelay >= 0) {
228 if (tstc()) { /* we got a key press */
229 (void) getc(); /* consume input */
wdenk42c05472004-03-23 22:14:11 +0000230 puts ("\b\b\b 0");
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200231 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000232 }
wdenk57b2d802003-06-27 21:31:46 +0000233 }
wdenkc6097192002-11-03 00:24:07 +0000234#endif
235
wdenk808532a2003-10-10 10:05:42 +0000236 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000237 int i;
238
239 --bootdelay;
240 /* delay 100 * 10ms */
241 for (i=0; !abort && i<100; ++i) {
242 if (tstc()) { /* we got a key press */
243 abort = 1; /* don't auto boot */
244 bootdelay = 0; /* no more delay */
wdenk452cfd62002-11-19 11:04:11 +0000245# ifdef CONFIG_MENUKEY
246 menukey = getc();
247# else
wdenkc6097192002-11-03 00:24:07 +0000248 (void) getc(); /* consume input */
wdenk452cfd62002-11-19 11:04:11 +0000249# endif
wdenkc6097192002-11-03 00:24:07 +0000250 break;
251 }
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200252 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000253 }
254
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200255 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000256 }
257
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200258 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000259
wdenk808532a2003-10-10 10:05:42 +0000260#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200261 if (abort)
262 gd->flags &= ~GD_FLG_SILENT;
wdenk808532a2003-10-10 10:05:42 +0000263#endif
264
wdenkc6097192002-11-03 00:24:07 +0000265 return abort;
266}
267# endif /* CONFIG_AUTOBOOT_KEYED */
268#endif /* CONFIG_BOOTDELAY >= 0 */
269
270/****************************************************************************/
271
272void main_loop (void)
273{
274#ifndef CFG_HUSH_PARSER
275 static char lastcommand[CFG_CBSIZE] = { 0, };
276 int len;
277 int rc = 1;
278 int flag;
279#endif
280
281#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
282 char *s;
283 int bootdelay;
284#endif
285#ifdef CONFIG_PREBOOT
286 char *p;
287#endif
wdenk0a658552003-08-05 17:43:17 +0000288#ifdef CONFIG_BOOTCOUNT_LIMIT
289 unsigned long bootcount = 0;
290 unsigned long bootlimit = 0;
291 char *bcs;
292 char bcs_set[16];
293#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000294
295#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
296 ulong bmp = 0; /* default bitmap */
297 extern int trab_vfd (ulong bitmap);
298
299#ifdef CONFIG_MODEM_SUPPORT
300 if (do_mdm_init)
301 bmp = 1; /* alternate bitmap */
302#endif
303 trab_vfd (bmp);
304#endif /* CONFIG_VFD && VFD_TEST_LOGO */
305
wdenk0a658552003-08-05 17:43:17 +0000306#ifdef CONFIG_BOOTCOUNT_LIMIT
307 bootcount = bootcount_load();
308 bootcount++;
309 bootcount_store (bootcount);
310 sprintf (bcs_set, "%lu", bootcount);
311 setenv ("bootcount", bcs_set);
312 bcs = getenv ("bootlimit");
313 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
314#endif /* CONFIG_BOOTCOUNT_LIMIT */
315
wdenkc6097192002-11-03 00:24:07 +0000316#ifdef CONFIG_MODEM_SUPPORT
317 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
318 if (do_mdm_init) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200319 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000320 setenv ("preboot", str); /* set or delete definition */
321 if (str != NULL)
322 free (str);
323 mdm_init(); /* wait for modem connection */
324 }
325#endif /* CONFIG_MODEM_SUPPORT */
326
stroesef8105cf2003-04-04 15:44:49 +0000327#ifdef CONFIG_VERSION_VARIABLE
328 {
329 extern char version_string[];
stroesef8105cf2003-04-04 15:44:49 +0000330
stroeseb9ab27c2003-07-11 08:00:33 +0000331 setenv ("ver", version_string); /* set version variable */
stroesef8105cf2003-04-04 15:44:49 +0000332 }
333#endif /* CONFIG_VERSION_VARIABLE */
334
wdenkc6097192002-11-03 00:24:07 +0000335#ifdef CFG_HUSH_PARSER
336 u_boot_hush_start ();
337#endif
338
wdenk3902d702004-04-15 18:22:41 +0000339#ifdef CONFIG_AUTO_COMPLETE
340 install_auto_complete();
341#endif
342
wdenkc6097192002-11-03 00:24:07 +0000343#ifdef CONFIG_PREBOOT
344 if ((p = getenv ("preboot")) != NULL) {
345# ifdef CONFIG_AUTOBOOT_KEYED
346 int prev = disable_ctrlc(1); /* disable Control C checking */
347# endif
348
349# ifndef CFG_HUSH_PARSER
350 run_command (p, 0);
351# else
352 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
353 FLAG_EXIT_FROM_LOOP);
354# endif
355
356# ifdef CONFIG_AUTOBOOT_KEYED
357 disable_ctrlc(prev); /* restore Control C checking */
358# endif
359 }
360#endif /* CONFIG_PREBOOT */
361
362#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
363 s = getenv ("bootdelay");
364 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
365
wdenk9dd2b882002-12-03 21:28:10 +0000366 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000367
368# ifdef CONFIG_BOOT_RETRY_TIME
wdenkb00ec162003-06-19 23:40:20 +0000369 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000370# endif /* CONFIG_BOOT_RETRY_TIME */
371
Yuri Tikhonovd773efb2008-02-04 14:11:03 +0100372#ifdef CONFIG_POST
373 if (gd->flags & GD_FLG_POSTFAIL) {
374 s = getenv("failbootcmd");
375 }
376 else
377#endif /* CONFIG_POST */
wdenk0a658552003-08-05 17:43:17 +0000378#ifdef CONFIG_BOOTCOUNT_LIMIT
379 if (bootlimit && (bootcount > bootlimit)) {
380 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
381 (unsigned)bootlimit);
382 s = getenv ("altbootcmd");
383 }
384 else
385#endif /* CONFIG_BOOTCOUNT_LIMIT */
386 s = getenv ("bootcmd");
wdenk9dd2b882002-12-03 21:28:10 +0000387
388 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
389
wdenkc6097192002-11-03 00:24:07 +0000390 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
391# ifdef CONFIG_AUTOBOOT_KEYED
392 int prev = disable_ctrlc(1); /* disable Control C checking */
393# endif
394
395# ifndef CFG_HUSH_PARSER
396 run_command (s, 0);
397# else
398 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
399 FLAG_EXIT_FROM_LOOP);
400# endif
401
402# ifdef CONFIG_AUTOBOOT_KEYED
403 disable_ctrlc(prev); /* restore Control C checking */
404# endif
405 }
wdenk452cfd62002-11-19 11:04:11 +0000406
407# ifdef CONFIG_MENUKEY
wdenk9dd2b882002-12-03 21:28:10 +0000408 if (menukey == CONFIG_MENUKEY) {
wdenk452cfd62002-11-19 11:04:11 +0000409 s = getenv("menucmd");
wdenk9dd2b882002-12-03 21:28:10 +0000410 if (s) {
wdenk452cfd62002-11-19 11:04:11 +0000411# ifndef CFG_HUSH_PARSER
wdenk91a4f362005-01-09 23:33:49 +0000412 run_command (s, 0);
wdenk452cfd62002-11-19 11:04:11 +0000413# else
414 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
415 FLAG_EXIT_FROM_LOOP);
416# endif
417 }
418 }
419#endif /* CONFIG_MENUKEY */
wdenkc6097192002-11-03 00:24:07 +0000420#endif /* CONFIG_BOOTDELAY */
421
wdenk452cfd62002-11-19 11:04:11 +0000422#ifdef CONFIG_AMIGAONEG3SE
423 {
424 extern void video_banner(void);
425 video_banner();
426 }
427#endif
428
wdenkc6097192002-11-03 00:24:07 +0000429 /*
430 * Main Loop for Monitor Command Processing
431 */
432#ifdef CFG_HUSH_PARSER
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
446 len = readline (CFG_PROMPT);
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 */
wdenk42c05472004-03-23 22:14:11 +0000457 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000458# 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)
wdenk42c05472004-03-23 22:14:11 +0000468 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000469 else
470 rc = run_command (lastcommand, flag);
471
472 if (rc <= 0) {
473 /* invalid command or not repeatable, forget it */
474 lastcommand[0] = 0;
475 }
476 }
477#endif /*CFG_HUSH_PARSER*/
478}
479
wdenkb00ec162003-06-19 23:40:20 +0000480#ifdef CONFIG_BOOT_RETRY_TIME
481/***************************************************************************
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200482 * initialize command line timeout
wdenkb00ec162003-06-19 23:40:20 +0000483 */
484void init_cmd_timeout(void)
485{
486 char *s = getenv ("bootretry");
487
488 if (s != NULL)
wdenk444f22b2003-12-07 21:39:28 +0000489 retry_time = (int)simple_strtol(s, NULL, 10);
wdenkb00ec162003-06-19 23:40:20 +0000490 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
wdenkc6097192002-11-03 00:24:07 +0000497/***************************************************************************
498 * reset command line timeout to retry_time seconds
499 */
wdenkc6097192002-11-03 00:24:07 +0000500void reset_cmd_timeout(void)
501{
502 endtime = endtick(retry_time);
503}
504#endif
505
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200506#ifdef CONFIG_CMDLINE_EDITING
507
508/*
509 * cmdline-editing related codes from vivi.
510 * Author: Janghoon Lyu <nandy@mizi.com>
511 */
512
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200513#define putnstr(str,n) do { \
514 printf ("%.*s", n, str); \
515 } while (0)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200516
517#define CTL_CH(c) ((c) - 'a' + 1)
518
519#define MAX_CMDBUF_SIZE 256
520
521#define CTL_BACKSPACE ('\b')
522#define DEL ((char)255)
523#define DEL7 ((char)127)
524#define CREAD_HIST_CHAR ('!')
525
526#define getcmd_putch(ch) putc(ch)
527#define getcmd_getch() getc()
528#define getcmd_cbeep() getcmd_putch('\a')
529
530#define HIST_MAX 20
531#define HIST_SIZE MAX_CMDBUF_SIZE
532
533static int hist_max = 0;
534static int hist_add_idx = 0;
535static int hist_cur = -1;
536unsigned hist_num = 0;
537
538char* hist_list[HIST_MAX];
539char hist_lines[HIST_MAX][HIST_SIZE];
540
541#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
542
543static void hist_init(void)
544{
545 int i;
546
547 hist_max = 0;
548 hist_add_idx = 0;
549 hist_cur = -1;
550 hist_num = 0;
551
552 for (i = 0; i < HIST_MAX; i++) {
553 hist_list[i] = hist_lines[i];
554 hist_list[i][0] = '\0';
555 }
556}
557
558static void cread_add_to_hist(char *line)
559{
560 strcpy(hist_list[hist_add_idx], line);
561
562 if (++hist_add_idx >= HIST_MAX)
563 hist_add_idx = 0;
564
565 if (hist_add_idx > hist_max)
566 hist_max = hist_add_idx;
567
568 hist_num++;
569}
570
571static char* hist_prev(void)
572{
573 char *ret;
574 int old_cur;
575
576 if (hist_cur < 0)
577 return NULL;
578
579 old_cur = hist_cur;
580 if (--hist_cur < 0)
581 hist_cur = hist_max;
582
583 if (hist_cur == hist_add_idx) {
584 hist_cur = old_cur;
585 ret = NULL;
586 } else
587 ret = hist_list[hist_cur];
588
589 return (ret);
590}
591
592static char* hist_next(void)
593{
594 char *ret;
595
596 if (hist_cur < 0)
597 return NULL;
598
599 if (hist_cur == hist_add_idx)
600 return NULL;
601
602 if (++hist_cur > hist_max)
603 hist_cur = 0;
604
605 if (hist_cur == hist_add_idx) {
606 ret = "";
607 } else
608 ret = hist_list[hist_cur];
609
610 return (ret);
611}
612
Stefan Roese59ff5a52006-07-27 16:11:19 +0200613#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200614static void cread_print_hist_list(void)
615{
616 int i;
617 unsigned long n;
618
619 n = hist_num - hist_max;
620
621 i = hist_add_idx + 1;
622 while (1) {
623 if (i > hist_max)
624 i = 0;
625 if (i == hist_add_idx)
626 break;
627 printf("%s\n", hist_list[i]);
628 n++;
629 i++;
630 }
631}
Stefan Roese59ff5a52006-07-27 16:11:19 +0200632#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200633
634#define BEGINNING_OF_LINE() { \
635 while (num) { \
636 getcmd_putch(CTL_BACKSPACE); \
637 num--; \
638 } \
639}
640
641#define ERASE_TO_EOL() { \
642 if (num < eol_num) { \
643 int tmp; \
644 for (tmp = num; tmp < eol_num; tmp++) \
645 getcmd_putch(' '); \
646 while (tmp-- > num) \
647 getcmd_putch(CTL_BACKSPACE); \
648 eol_num = num; \
649 } \
650}
651
652#define REFRESH_TO_EOL() { \
653 if (num < eol_num) { \
654 wlen = eol_num - num; \
655 putnstr(buf + num, wlen); \
656 num = eol_num; \
657 } \
658}
659
660static void cread_add_char(char ichar, int insert, unsigned long *num,
661 unsigned long *eol_num, char *buf, unsigned long len)
662{
663 unsigned long wlen;
664
665 /* room ??? */
666 if (insert || *num == *eol_num) {
667 if (*eol_num > len - 1) {
668 getcmd_cbeep();
669 return;
670 }
671 (*eol_num)++;
672 }
673
674 if (insert) {
675 wlen = *eol_num - *num;
676 if (wlen > 1) {
677 memmove(&buf[*num+1], &buf[*num], wlen-1);
678 }
679
680 buf[*num] = ichar;
681 putnstr(buf + *num, wlen);
682 (*num)++;
683 while (--wlen) {
684 getcmd_putch(CTL_BACKSPACE);
685 }
686 } else {
687 /* echo the character */
688 wlen = 1;
689 buf[*num] = ichar;
690 putnstr(buf + *num, wlen);
691 (*num)++;
692 }
693}
694
695static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
696 unsigned long *eol_num, char *buf, unsigned long len)
697{
698 while (strsize--) {
699 cread_add_char(*str, insert, num, eol_num, buf, len);
700 str++;
701 }
702}
703
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100704static int cread_line(const char *const prompt, char *buf, unsigned int *len)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200705{
706 unsigned long num = 0;
707 unsigned long eol_num = 0;
708 unsigned long rlen;
709 unsigned long wlen;
710 char ichar;
711 int insert = 1;
712 int esc_len = 0;
713 int rc = 0;
714 char esc_save[8];
715
716 while (1) {
717 rlen = 1;
Andreas Engel3a0ba102008-01-09 17:10:56 +0100718#ifdef CONFIG_BOOT_RETRY_TIME
719 while (!tstc()) { /* while no incoming data */
720 if (retry_time >= 0 && get_ticks() > endtime)
721 return (-2); /* timed out */
722 }
723#endif
724
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200725 ichar = getcmd_getch();
726
727 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200728 putc('\n');
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200729 break;
730 }
731
732 /*
733 * handle standard linux xterm esc sequences for arrow key, etc.
734 */
735 if (esc_len != 0) {
736 if (esc_len == 1) {
737 if (ichar == '[') {
738 esc_save[esc_len] = ichar;
739 esc_len = 2;
740 } else {
741 cread_add_str(esc_save, esc_len, insert,
742 &num, &eol_num, buf, *len);
743 esc_len = 0;
744 }
745 continue;
746 }
747
748 switch (ichar) {
749
750 case 'D': /* <- key */
751 ichar = CTL_CH('b');
752 esc_len = 0;
753 break;
754 case 'C': /* -> key */
755 ichar = CTL_CH('f');
756 esc_len = 0;
757 break; /* pass off to ^F handler */
758 case 'H': /* Home key */
759 ichar = CTL_CH('a');
760 esc_len = 0;
761 break; /* pass off to ^A handler */
762 case 'A': /* up arrow */
763 ichar = CTL_CH('p');
764 esc_len = 0;
765 break; /* pass off to ^P handler */
766 case 'B': /* down arrow */
767 ichar = CTL_CH('n');
768 esc_len = 0;
769 break; /* pass off to ^N handler */
770 default:
771 esc_save[esc_len++] = ichar;
772 cread_add_str(esc_save, esc_len, insert,
773 &num, &eol_num, buf, *len);
774 esc_len = 0;
775 continue;
776 }
777 }
778
779 switch (ichar) {
780 case 0x1b:
781 if (esc_len == 0) {
782 esc_save[esc_len] = ichar;
783 esc_len = 1;
784 } else {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200785 puts("impossible condition #876\n");
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200786 esc_len = 0;
787 }
788 break;
789
790 case CTL_CH('a'):
791 BEGINNING_OF_LINE();
792 break;
793 case CTL_CH('c'): /* ^C - break */
794 *buf = '\0'; /* discard input */
795 return (-1);
796 case CTL_CH('f'):
797 if (num < eol_num) {
798 getcmd_putch(buf[num]);
799 num++;
800 }
801 break;
802 case CTL_CH('b'):
803 if (num) {
804 getcmd_putch(CTL_BACKSPACE);
805 num--;
806 }
807 break;
808 case CTL_CH('d'):
809 if (num < eol_num) {
810 wlen = eol_num - num - 1;
811 if (wlen) {
812 memmove(&buf[num], &buf[num+1], wlen);
813 putnstr(buf + num, wlen);
814 }
815
816 getcmd_putch(' ');
817 do {
818 getcmd_putch(CTL_BACKSPACE);
819 } while (wlen--);
820 eol_num--;
821 }
822 break;
823 case CTL_CH('k'):
824 ERASE_TO_EOL();
825 break;
826 case CTL_CH('e'):
827 REFRESH_TO_EOL();
828 break;
829 case CTL_CH('o'):
830 insert = !insert;
831 break;
832 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100833 case CTL_CH('u'):
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200834 BEGINNING_OF_LINE();
835 ERASE_TO_EOL();
836 break;
837 case DEL:
838 case DEL7:
839 case 8:
840 if (num) {
841 wlen = eol_num - num;
842 num--;
843 memmove(&buf[num], &buf[num+1], wlen);
844 getcmd_putch(CTL_BACKSPACE);
845 putnstr(buf + num, wlen);
846 getcmd_putch(' ');
847 do {
848 getcmd_putch(CTL_BACKSPACE);
849 } while (wlen--);
850 eol_num--;
851 }
852 break;
853 case CTL_CH('p'):
854 case CTL_CH('n'):
855 {
856 char * hline;
857
858 esc_len = 0;
859
860 if (ichar == CTL_CH('p'))
861 hline = hist_prev();
862 else
863 hline = hist_next();
864
865 if (!hline) {
866 getcmd_cbeep();
867 continue;
868 }
869
870 /* nuke the current line */
871 /* first, go home */
872 BEGINNING_OF_LINE();
873
874 /* erase to end of line */
875 ERASE_TO_EOL();
876
877 /* copy new line into place and display */
878 strcpy(buf, hline);
879 eol_num = strlen(buf);
880 REFRESH_TO_EOL();
881 continue;
882 }
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100883#ifdef CONFIG_AUTO_COMPLETE
884 case '\t': {
885 int num2, col;
886
887 /* do not autocomplete when in the middle */
888 if (num < eol_num) {
889 getcmd_cbeep();
890 break;
891 }
892
893 buf[num] = '\0';
894 col = strlen(prompt) + eol_num;
895 num2 = num;
896 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
897 col = num2 - num;
898 num += col;
899 eol_num += col;
900 }
901 break;
902 }
903#endif
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200904 default:
905 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
906 break;
907 }
908 }
909 *len = eol_num;
910 buf[eol_num] = '\0'; /* lose the newline */
911
912 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
913 cread_add_to_hist(buf);
914 hist_cur = hist_add_idx;
915
916 return (rc);
917}
918
919#endif /* CONFIG_CMDLINE_EDITING */
920
wdenkc6097192002-11-03 00:24:07 +0000921/****************************************************************************/
922
923/*
924 * Prompt for input and read a line.
925 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
926 * time out when time goes past endtime (timebase time in ticks).
927 * Return: number of read characters
928 * -1 if break
929 * -2 if timed out
930 */
931int readline (const char *const prompt)
932{
James Yang20ffa972008-01-09 11:17:49 -0600933 return readline_into_buffer(prompt, console_buffer);
934}
935
936
937int readline_into_buffer (const char *const prompt, char * buffer)
938{
939 char *p = buffer;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200940#ifdef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200941 unsigned int len=MAX_CMDBUF_SIZE;
Stefan Roesedc5ff702006-08-07 15:08:44 +0200942 int rc;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200943 static int initted = 0;
944
945 if (!initted) {
946 hist_init();
947 initted = 1;
948 }
949
Wolfgang Denk92549de2006-07-21 11:34:34 +0200950 puts (prompt);
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200951
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100952 rc = cread_line(prompt, p, &len);
Stefan Roesedc5ff702006-08-07 15:08:44 +0200953 return rc < 0 ? rc : len;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200954#else
Kumar Galae225b172008-01-10 02:22:05 -0600955 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000956 int n = 0; /* buffer index */
957 int plen = 0; /* prompt length */
958 int col; /* output column cnt */
959 char c;
960
961 /* print prompt */
962 if (prompt) {
963 plen = strlen (prompt);
964 puts (prompt);
965 }
966 col = plen;
967
968 for (;;) {
969#ifdef CONFIG_BOOT_RETRY_TIME
970 while (!tstc()) { /* while no incoming data */
971 if (retry_time >= 0 && get_ticks() > endtime)
972 return (-2); /* timed out */
973 }
974#endif
975 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
976
977#ifdef CONFIG_SHOW_ACTIVITY
978 while (!tstc()) {
979 extern void show_activity(int arg);
980 show_activity(0);
981 }
982#endif
983 c = getc();
984
985 /*
986 * Special character handling
987 */
988 switch (c) {
989 case '\r': /* Enter */
990 case '\n':
991 *p = '\0';
992 puts ("\r\n");
James Yang20ffa972008-01-09 11:17:49 -0600993 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +0000994
wdenk8d4d1f62004-03-23 22:37:33 +0000995 case '\0': /* nul */
996 continue;
997
wdenkc6097192002-11-03 00:24:07 +0000998 case 0x03: /* ^C - break */
James Yang20ffa972008-01-09 11:17:49 -0600999 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001000 return (-1);
1001
1002 case 0x15: /* ^U - erase line */
1003 while (col > plen) {
1004 puts (erase_seq);
1005 --col;
1006 }
James Yang20ffa972008-01-09 11:17:49 -06001007 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001008 n = 0;
1009 continue;
1010
Wolfgang Denk70df7bc2007-06-22 23:59:00 +02001011 case 0x17: /* ^W - erase word */
James Yang20ffa972008-01-09 11:17:49 -06001012 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001013 while ((n > 0) && (*p != ' ')) {
James Yang20ffa972008-01-09 11:17:49 -06001014 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001015 }
1016 continue;
1017
1018 case 0x08: /* ^H - backspace */
1019 case 0x7F: /* DEL - backspace */
James Yang20ffa972008-01-09 11:17:49 -06001020 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001021 continue;
1022
1023 default:
1024 /*
1025 * Must be a normal character then
1026 */
1027 if (n < CFG_CBSIZE-2) {
1028 if (c == '\t') { /* expand TABs */
wdenk3902d702004-04-15 18:22:41 +00001029#ifdef CONFIG_AUTO_COMPLETE
1030 /* if auto completion triggered just continue */
1031 *p = '\0';
1032 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang20ffa972008-01-09 11:17:49 -06001033 p = p_buf + n; /* reset */
wdenk3902d702004-04-15 18:22:41 +00001034 continue;
1035 }
1036#endif
wdenkc6097192002-11-03 00:24:07 +00001037 puts (tab_seq+(col&07));
1038 col += 8 - (col&07);
1039 } else {
1040 ++col; /* echo input */
1041 putc (c);
1042 }
1043 *p++ = c;
1044 ++n;
1045 } else { /* Buffer full */
1046 putc ('\a');
1047 }
1048 }
1049 }
Wolfgang Denk4c9ca782006-07-21 11:33:45 +02001050#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001051}
1052
1053/****************************************************************************/
1054
Stefan Roese59ff5a52006-07-27 16:11:19 +02001055#ifndef CONFIG_CMDLINE_EDITING
wdenkc6097192002-11-03 00:24:07 +00001056static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1057{
1058 char *s;
1059
1060 if (*np == 0) {
1061 return (p);
1062 }
1063
1064 if (*(--p) == '\t') { /* will retype the whole line */
1065 while (*colp > plen) {
1066 puts (erase_seq);
1067 (*colp)--;
1068 }
1069 for (s=buffer; s<p; ++s) {
1070 if (*s == '\t') {
1071 puts (tab_seq+((*colp) & 07));
1072 *colp += 8 - ((*colp) & 07);
1073 } else {
1074 ++(*colp);
1075 putc (*s);
1076 }
1077 }
1078 } else {
1079 puts (erase_seq);
1080 (*colp)--;
1081 }
1082 (*np)--;
1083 return (p);
1084}
Stefan Roese59ff5a52006-07-27 16:11:19 +02001085#endif /* CONFIG_CMDLINE_EDITING */
wdenkc6097192002-11-03 00:24:07 +00001086
1087/****************************************************************************/
1088
1089int parse_line (char *line, char *argv[])
1090{
1091 int nargs = 0;
1092
1093#ifdef DEBUG_PARSER
1094 printf ("parse_line: \"%s\"\n", line);
1095#endif
1096 while (nargs < CFG_MAXARGS) {
1097
1098 /* skip any white space */
1099 while ((*line == ' ') || (*line == '\t')) {
1100 ++line;
1101 }
1102
1103 if (*line == '\0') { /* end of line, no more args */
1104 argv[nargs] = NULL;
1105#ifdef DEBUG_PARSER
1106 printf ("parse_line: nargs=%d\n", nargs);
1107#endif
1108 return (nargs);
1109 }
1110
1111 argv[nargs++] = line; /* begin of argument string */
1112
1113 /* find end of string */
1114 while (*line && (*line != ' ') && (*line != '\t')) {
1115 ++line;
1116 }
1117
1118 if (*line == '\0') { /* end of line, no more args */
1119 argv[nargs] = NULL;
1120#ifdef DEBUG_PARSER
1121 printf ("parse_line: nargs=%d\n", nargs);
1122#endif
1123 return (nargs);
1124 }
1125
1126 *line++ = '\0'; /* terminate current arg */
1127 }
1128
1129 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1130
1131#ifdef DEBUG_PARSER
1132 printf ("parse_line: nargs=%d\n", nargs);
1133#endif
1134 return (nargs);
1135}
1136
1137/****************************************************************************/
1138
1139static void process_macros (const char *input, char *output)
1140{
1141 char c, prev;
1142 const char *varname_start = NULL;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001143 int inputcnt = strlen (input);
wdenkc6097192002-11-03 00:24:07 +00001144 int outputcnt = CFG_CBSIZE;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001145 int state = 0; /* 0 = waiting for '$' */
1146
1147 /* 1 = waiting for '(' or '{' */
1148 /* 2 = waiting for ')' or '}' */
1149 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001150#ifdef DEBUG_PARSER
1151 char *output_start = output;
1152
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001153 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1154 input);
wdenkc6097192002-11-03 00:24:07 +00001155#endif
1156
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001157 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001158
1159 while (inputcnt && outputcnt) {
wdenk57b2d802003-06-27 21:31:46 +00001160 c = *input++;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001161 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001162
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001163 if (state != 3) {
1164 /* remove one level of escape characters */
1165 if ((c == '\\') && (prev != '\\')) {
1166 if (inputcnt-- == 0)
1167 break;
1168 prev = c;
1169 c = *input++;
1170 }
wdenkc6097192002-11-03 00:24:07 +00001171 }
wdenkc6097192002-11-03 00:24:07 +00001172
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001173 switch (state) {
1174 case 0: /* Waiting for (unescaped) $ */
1175 if ((c == '\'') && (prev != '\\')) {
1176 state = 3;
1177 break;
1178 }
1179 if ((c == '$') && (prev != '\\')) {
1180 state++;
1181 } else {
wdenkc6097192002-11-03 00:24:07 +00001182 *(output++) = c;
1183 outputcnt--;
1184 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001185 break;
1186 case 1: /* Waiting for ( */
1187 if (c == '(' || c == '{') {
1188 state++;
1189 varname_start = input;
1190 } else {
1191 state = 0;
1192 *(output++) = '$';
1193 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001194
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001195 if (outputcnt) {
1196 *(output++) = c;
1197 outputcnt--;
1198 }
wdenkc6097192002-11-03 00:24:07 +00001199 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001200 break;
1201 case 2: /* Waiting for ) */
1202 if (c == ')' || c == '}') {
1203 int i;
1204 char envname[CFG_CBSIZE], *envval;
1205 int envcnt = input - varname_start - 1; /* Varname # of chars */
wdenkc6097192002-11-03 00:24:07 +00001206
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001207 /* Get the varname */
1208 for (i = 0; i < envcnt; i++) {
1209 envname[i] = varname_start[i];
wdenkc6097192002-11-03 00:24:07 +00001210 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001211 envname[i] = 0;
1212
1213 /* Get its value */
1214 envval = getenv (envname);
1215
1216 /* Copy into the line if it exists */
1217 if (envval != NULL)
1218 while ((*envval) && outputcnt) {
1219 *(output++) = *(envval++);
1220 outputcnt--;
1221 }
1222 /* Look for another '$' */
1223 state = 0;
1224 }
1225 break;
1226 case 3: /* Waiting for ' */
1227 if ((c == '\'') && (prev != '\\')) {
1228 state = 0;
1229 } else {
1230 *(output++) = c;
1231 outputcnt--;
1232 }
1233 break;
wdenkc6097192002-11-03 00:24:07 +00001234 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001235 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001236 }
1237
1238 if (outputcnt)
1239 *output = 0;
Bartlomiej Siekacadc8e82007-05-27 17:04:18 +02001240 else
1241 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001242
1243#ifdef DEBUG_PARSER
1244 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001245 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001246#endif
1247}
1248
1249/****************************************************************************
1250 * returns:
1251 * 1 - command executed, repeatable
1252 * 0 - command executed but not repeatable, interrupted commands are
1253 * always considered not repeatable
1254 * -1 - not executed (unrecognized, bootd recursion or too many args)
1255 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1256 * considered unrecognized)
1257 *
1258 * WARNING:
1259 *
1260 * We must create a temporary copy of the command since the command we get
1261 * may be the result from getenv(), which returns a pointer directly to
1262 * the environment data, which may change magicly when the command we run
1263 * creates or modifies environment variables (like "bootp" does).
1264 */
1265
1266int run_command (const char *cmd, int flag)
1267{
1268 cmd_tbl_t *cmdtp;
1269 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1270 char *token; /* start of token in cmdbuf */
1271 char *sep; /* end of token (separator) in cmdbuf */
1272 char finaltoken[CFG_CBSIZE];
1273 char *str = cmdbuf;
1274 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
wdenkf4688a22003-05-28 08:06:31 +00001275 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001276 int repeatable = 1;
wdenkf4688a22003-05-28 08:06:31 +00001277 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001278
1279#ifdef DEBUG_PARSER
1280 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1281 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1282 puts ("\"\n");
1283#endif
1284
1285 clear_ctrlc(); /* forget any previous Control C */
1286
1287 if (!cmd || !*cmd) {
1288 return -1; /* empty command */
1289 }
1290
1291 if (strlen(cmd) >= CFG_CBSIZE) {
1292 puts ("## Command too long!\n");
1293 return -1;
1294 }
1295
1296 strcpy (cmdbuf, cmd);
1297
1298 /* Process separators and check for invalid
1299 * repeatable commands
1300 */
1301
1302#ifdef DEBUG_PARSER
1303 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1304#endif
1305 while (*str) {
1306
1307 /*
1308 * Find separator, or string end
1309 * Allow simple escape of ';' by writing "\;"
1310 */
wdenk654868c2003-01-02 23:57:29 +00001311 for (inquotes = 0, sep = str; *sep; sep++) {
1312 if ((*sep=='\'') &&
1313 (*(sep-1) != '\\'))
1314 inquotes=!inquotes;
1315
1316 if (!inquotes &&
1317 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001318 ( sep != str) && /* past string start */
1319 (*(sep-1) != '\\')) /* and NOT escaped */
1320 break;
1321 }
1322
1323 /*
1324 * Limit the token to data between separators
1325 */
1326 token = str;
1327 if (*sep) {
1328 str = sep + 1; /* start of command for next pass */
1329 *sep = '\0';
1330 }
1331 else
1332 str = sep; /* no more commands for next pass */
1333#ifdef DEBUG_PARSER
1334 printf ("token: \"%s\"\n", token);
1335#endif
1336
1337 /* find macros in this token and replace them */
1338 process_macros (token, finaltoken);
1339
1340 /* Extract arguments */
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001341 if ((argc = parse_line (finaltoken, argv)) == 0) {
1342 rc = -1; /* no command at all */
1343 continue;
1344 }
wdenkc6097192002-11-03 00:24:07 +00001345
1346 /* Look up command in command table */
1347 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1348 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf4688a22003-05-28 08:06:31 +00001349 rc = -1; /* give up after bad command */
1350 continue;
wdenkc6097192002-11-03 00:24:07 +00001351 }
1352
1353 /* found - check max args */
1354 if (argc > cmdtp->maxargs) {
1355 printf ("Usage:\n%s\n", cmdtp->usage);
wdenkf4688a22003-05-28 08:06:31 +00001356 rc = -1;
1357 continue;
wdenkc6097192002-11-03 00:24:07 +00001358 }
1359
Jon Loeliger052fc842007-07-08 18:10:08 -05001360#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001361 /* avoid "bootd" recursion */
1362 if (cmdtp->cmd == do_bootd) {
1363#ifdef DEBUG_PARSER
1364 printf ("[%s]\n", finaltoken);
1365#endif
1366 if (flag & CMD_FLAG_BOOTD) {
wdenk42c05472004-03-23 22:14:11 +00001367 puts ("'bootd' recursion detected\n");
wdenkf4688a22003-05-28 08:06:31 +00001368 rc = -1;
1369 continue;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001370 } else {
wdenkc6097192002-11-03 00:24:07 +00001371 flag |= CMD_FLAG_BOOTD;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001372 }
wdenkc6097192002-11-03 00:24:07 +00001373 }
Jon Loeligerd704d912007-07-10 11:02:44 -05001374#endif
wdenkc6097192002-11-03 00:24:07 +00001375
1376 /* OK - call function to do the command */
1377 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf4688a22003-05-28 08:06:31 +00001378 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001379 }
1380
1381 repeatable &= cmdtp->repeatable;
1382
1383 /* Did the user stop this? */
1384 if (had_ctrlc ())
Detlev Zundeleeab5a12007-05-23 18:47:48 +02001385 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001386 }
1387
wdenkf4688a22003-05-28 08:06:31 +00001388 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001389}
1390
1391/****************************************************************************/
1392
Jon Loeliger052fc842007-07-08 18:10:08 -05001393#if defined(CONFIG_CMD_RUN)
wdenkc6097192002-11-03 00:24:07 +00001394int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1395{
1396 int i;
wdenkc6097192002-11-03 00:24:07 +00001397
1398 if (argc < 2) {
1399 printf ("Usage:\n%s\n", cmdtp->usage);
1400 return 1;
1401 }
1402
1403 for (i=1; i<argc; ++i) {
wdenkb02744a2003-04-05 00:53:31 +00001404 char *arg;
1405
1406 if ((arg = getenv (argv[i])) == NULL) {
1407 printf ("## Error: \"%s\" not defined\n", argv[i]);
1408 return 1;
1409 }
wdenkc6097192002-11-03 00:24:07 +00001410#ifndef CFG_HUSH_PARSER
wdenkb02744a2003-04-05 00:53:31 +00001411 if (run_command (arg, flag) == -1)
1412 return 1;
wdenkc6097192002-11-03 00:24:07 +00001413#else
wdenkb02744a2003-04-05 00:53:31 +00001414 if (parse_string_outer(arg,
wdenk541a76d2003-05-03 15:50:43 +00001415 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenkb02744a2003-04-05 00:53:31 +00001416 return 1;
wdenkc6097192002-11-03 00:24:07 +00001417#endif
1418 }
wdenkb02744a2003-04-05 00:53:31 +00001419 return 0;
wdenkc6097192002-11-03 00:24:07 +00001420}
Jon Loeligerd704d912007-07-10 11:02:44 -05001421#endif