blob: c06ea07d414aa744ca90ef19ea0cc9002d3dcb5a [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
James Yangb83983f2008-05-05 10:22:53 -050043#if defined(CONFIG_SILENT_CONSOLE) || defined(CONFIG_POST) || defined(CONFIG_CMDLINE_EDITING)
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
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +020059#if defined(CONFIG_UPDATE_TFTP)
60void update_tftp (void);
61#endif /* CONFIG_UPDATE_TFTP */
wdenk57b2d802003-06-27 21:31:46 +000062
wdenkc6097192002-11-03 00:24:07 +000063#define MAX_DELAY_STOP_STR 32
64
wdenkc6097192002-11-03 00:24:07 +000065#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
66static int abortboot(int);
67#endif
68
69#undef DEBUG_PARSER
70
71char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
72
Stefan Roese59ff5a52006-07-27 16:11:19 +020073static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
wdenkc6097192002-11-03 00:24:07 +000074static char erase_seq[] = "\b \b"; /* erase sequence */
75static char tab_seq[] = " "; /* used to expand TABs */
76
77#ifdef CONFIG_BOOT_RETRY_TIME
78static uint64_t endtime = 0; /* must be set, default is instant timeout */
79static int retry_time = -1; /* -1 so can call readline before main_loop */
80#endif
81
82#define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
83
84#ifndef CONFIG_BOOT_RETRY_MIN
85#define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
86#endif
87
88#ifdef CONFIG_MODEM_SUPPORT
89int do_mdm_init = 0;
90extern void mdm_init(void); /* defined in board.c */
91#endif
92
93/***************************************************************************
94 * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
95 * returns: 0 - no key string, allow autoboot
96 * 1 - got key string, abort
97 */
98#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
99# if defined(CONFIG_AUTOBOOT_KEYED)
100static __inline__ int abortboot(int bootdelay)
101{
102 int abort = 0;
103 uint64_t etime = endtick(bootdelay);
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200104 struct {
wdenkc6097192002-11-03 00:24:07 +0000105 char* str;
106 u_int len;
107 int retry;
108 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200109 delaykey [] = {
wdenkc6097192002-11-03 00:24:07 +0000110 { str: getenv ("bootdelaykey"), retry: 1 },
111 { str: getenv ("bootdelaykey2"), retry: 1 },
112 { str: getenv ("bootstopkey"), retry: 0 },
113 { str: getenv ("bootstopkey2"), retry: 0 },
114 };
115
116 char presskey [MAX_DELAY_STOP_STR];
117 u_int presskey_len = 0;
118 u_int presskey_max = 0;
119 u_int i;
120
121# ifdef CONFIG_AUTOBOOT_PROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200122 printf(CONFIG_AUTOBOOT_PROMPT);
wdenkc6097192002-11-03 00:24:07 +0000123# endif
124
125# ifdef CONFIG_AUTOBOOT_DELAY_STR
126 if (delaykey[0].str == NULL)
127 delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
128# endif
129# ifdef CONFIG_AUTOBOOT_DELAY_STR2
130 if (delaykey[1].str == NULL)
131 delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
132# endif
133# ifdef CONFIG_AUTOBOOT_STOP_STR
134 if (delaykey[2].str == NULL)
135 delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
136# endif
137# ifdef CONFIG_AUTOBOOT_STOP_STR2
138 if (delaykey[3].str == NULL)
139 delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
140# endif
141
142 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
143 delaykey[i].len = delaykey[i].str == NULL ?
144 0 : strlen (delaykey[i].str);
145 delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
146 MAX_DELAY_STOP_STR : delaykey[i].len;
147
148 presskey_max = presskey_max > delaykey[i].len ?
149 presskey_max : delaykey[i].len;
150
151# if DEBUG_BOOTKEYS
152 printf("%s key:<%s>\n",
153 delaykey[i].retry ? "delay" : "stop",
154 delaykey[i].str ? delaykey[i].str : "NULL");
155# endif
156 }
157
158 /* In order to keep up with incoming data, check timeout only
159 * when catch up.
160 */
161 while (!abort && get_ticks() <= etime) {
162 for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
163 if (delaykey[i].len > 0 &&
164 presskey_len >= delaykey[i].len &&
165 memcmp (presskey + presskey_len - delaykey[i].len,
wdenk57b2d802003-06-27 21:31:46 +0000166 delaykey[i].str,
wdenkc6097192002-11-03 00:24:07 +0000167 delaykey[i].len) == 0) {
168# if DEBUG_BOOTKEYS
169 printf("got %skey\n",
170 delaykey[i].retry ? "delay" : "stop");
171# endif
172
173# ifdef CONFIG_BOOT_RETRY_TIME
174 /* don't retry auto boot */
175 if (! delaykey[i].retry)
176 retry_time = -1;
177# endif
178 abort = 1;
179 }
180 }
181
182 if (tstc()) {
183 if (presskey_len < presskey_max) {
184 presskey [presskey_len ++] = getc();
185 }
186 else {
187 for (i = 0; i < presskey_max - 1; i ++)
188 presskey [i] = presskey [i + 1];
189
190 presskey [i] = getc();
191 }
192 }
193 }
194# if DEBUG_BOOTKEYS
195 if (!abort)
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200196 puts("key timeout\n");
wdenkc6097192002-11-03 00:24:07 +0000197# endif
198
dzu6605c842003-10-24 13:14:45 +0000199#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200200 if (abort)
201 gd->flags &= ~GD_FLG_SILENT;
dzu6605c842003-10-24 13:14:45 +0000202#endif
203
wdenkc6097192002-11-03 00:24:07 +0000204 return abort;
205}
206
207# else /* !defined(CONFIG_AUTOBOOT_KEYED) */
208
wdenk452cfd62002-11-19 11:04:11 +0000209#ifdef CONFIG_MENUKEY
210static int menukey = 0;
211#endif
212
wdenkc6097192002-11-03 00:24:07 +0000213static __inline__ int abortboot(int bootdelay)
214{
215 int abort = 0;
216
wdenk452cfd62002-11-19 11:04:11 +0000217#ifdef CONFIG_MENUPROMPT
Stefan Roese37628252008-08-06 14:05:38 +0200218 printf(CONFIG_MENUPROMPT);
wdenk452cfd62002-11-19 11:04:11 +0000219#else
wdenkc6097192002-11-03 00:24:07 +0000220 printf("Hit any key to stop autoboot: %2d ", bootdelay);
wdenk452cfd62002-11-19 11:04:11 +0000221#endif
wdenkc6097192002-11-03 00:24:07 +0000222
223#if defined CONFIG_ZERO_BOOTDELAY_CHECK
wdenk57b2d802003-06-27 21:31:46 +0000224 /*
225 * Check if key already pressed
226 * Don't check if bootdelay < 0
227 */
wdenkc6097192002-11-03 00:24:07 +0000228 if (bootdelay >= 0) {
229 if (tstc()) { /* we got a key press */
230 (void) getc(); /* consume input */
wdenk42c05472004-03-23 22:14:11 +0000231 puts ("\b\b\b 0");
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200232 abort = 1; /* don't auto boot */
wdenkc6097192002-11-03 00:24:07 +0000233 }
wdenk57b2d802003-06-27 21:31:46 +0000234 }
wdenkc6097192002-11-03 00:24:07 +0000235#endif
236
wdenk808532a2003-10-10 10:05:42 +0000237 while ((bootdelay > 0) && (!abort)) {
wdenkc6097192002-11-03 00:24:07 +0000238 int i;
239
240 --bootdelay;
241 /* delay 100 * 10ms */
242 for (i=0; !abort && i<100; ++i) {
243 if (tstc()) { /* we got a key press */
244 abort = 1; /* don't auto boot */
245 bootdelay = 0; /* no more delay */
wdenk452cfd62002-11-19 11:04:11 +0000246# ifdef CONFIG_MENUKEY
247 menukey = getc();
248# else
wdenkc6097192002-11-03 00:24:07 +0000249 (void) getc(); /* consume input */
wdenk452cfd62002-11-19 11:04:11 +0000250# endif
wdenkc6097192002-11-03 00:24:07 +0000251 break;
252 }
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200253 udelay(10000);
wdenkc6097192002-11-03 00:24:07 +0000254 }
255
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200256 printf("\b\b\b%2d ", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000257 }
258
Ladislav Michl83fc7e52007-04-25 16:01:26 +0200259 putc('\n');
wdenkc6097192002-11-03 00:24:07 +0000260
wdenk808532a2003-10-10 10:05:42 +0000261#ifdef CONFIG_SILENT_CONSOLE
Ladislav Michl6c6a38a2007-04-25 16:01:26 +0200262 if (abort)
263 gd->flags &= ~GD_FLG_SILENT;
wdenk808532a2003-10-10 10:05:42 +0000264#endif
265
wdenkc6097192002-11-03 00:24:07 +0000266 return abort;
267}
268# endif /* CONFIG_AUTOBOOT_KEYED */
269#endif /* CONFIG_BOOTDELAY >= 0 */
270
271/****************************************************************************/
272
273void main_loop (void)
274{
275#ifndef CFG_HUSH_PARSER
276 static char lastcommand[CFG_CBSIZE] = { 0, };
277 int len;
278 int rc = 1;
279 int flag;
280#endif
281
282#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
283 char *s;
284 int bootdelay;
285#endif
286#ifdef CONFIG_PREBOOT
287 char *p;
288#endif
wdenk0a658552003-08-05 17:43:17 +0000289#ifdef CONFIG_BOOTCOUNT_LIMIT
290 unsigned long bootcount = 0;
291 unsigned long bootlimit = 0;
292 char *bcs;
293 char bcs_set[16];
294#endif /* CONFIG_BOOTCOUNT_LIMIT */
wdenkc6097192002-11-03 00:24:07 +0000295
296#if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
297 ulong bmp = 0; /* default bitmap */
298 extern int trab_vfd (ulong bitmap);
299
300#ifdef CONFIG_MODEM_SUPPORT
301 if (do_mdm_init)
302 bmp = 1; /* alternate bitmap */
303#endif
304 trab_vfd (bmp);
305#endif /* CONFIG_VFD && VFD_TEST_LOGO */
306
Bartlomiej Siekae273e9f2008-10-01 15:26:31 +0200307#if defined(CONFIG_UPDATE_TFTP)
308 update_tftp ();
309#endif /* CONFIG_UPDATE_TFTP */
310
wdenk0a658552003-08-05 17:43:17 +0000311#ifdef CONFIG_BOOTCOUNT_LIMIT
312 bootcount = bootcount_load();
313 bootcount++;
314 bootcount_store (bootcount);
315 sprintf (bcs_set, "%lu", bootcount);
316 setenv ("bootcount", bcs_set);
317 bcs = getenv ("bootlimit");
318 bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
319#endif /* CONFIG_BOOTCOUNT_LIMIT */
320
wdenkc6097192002-11-03 00:24:07 +0000321#ifdef CONFIG_MODEM_SUPPORT
322 debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
323 if (do_mdm_init) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200324 char *str = strdup(getenv("mdm_cmd"));
wdenkc6097192002-11-03 00:24:07 +0000325 setenv ("preboot", str); /* set or delete definition */
326 if (str != NULL)
327 free (str);
328 mdm_init(); /* wait for modem connection */
329 }
330#endif /* CONFIG_MODEM_SUPPORT */
331
stroesef8105cf2003-04-04 15:44:49 +0000332#ifdef CONFIG_VERSION_VARIABLE
333 {
334 extern char version_string[];
stroesef8105cf2003-04-04 15:44:49 +0000335
stroeseb9ab27c2003-07-11 08:00:33 +0000336 setenv ("ver", version_string); /* set version variable */
stroesef8105cf2003-04-04 15:44:49 +0000337 }
338#endif /* CONFIG_VERSION_VARIABLE */
339
wdenkc6097192002-11-03 00:24:07 +0000340#ifdef CFG_HUSH_PARSER
341 u_boot_hush_start ();
342#endif
343
wdenk3902d702004-04-15 18:22:41 +0000344#ifdef CONFIG_AUTO_COMPLETE
345 install_auto_complete();
346#endif
347
wdenkc6097192002-11-03 00:24:07 +0000348#ifdef CONFIG_PREBOOT
349 if ((p = getenv ("preboot")) != NULL) {
350# ifdef CONFIG_AUTOBOOT_KEYED
351 int prev = disable_ctrlc(1); /* disable Control C checking */
352# endif
353
354# ifndef CFG_HUSH_PARSER
355 run_command (p, 0);
356# else
357 parse_string_outer(p, FLAG_PARSE_SEMICOLON |
358 FLAG_EXIT_FROM_LOOP);
359# endif
360
361# ifdef CONFIG_AUTOBOOT_KEYED
362 disable_ctrlc(prev); /* restore Control C checking */
363# endif
364 }
365#endif /* CONFIG_PREBOOT */
366
367#if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
368 s = getenv ("bootdelay");
369 bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
370
wdenk9dd2b882002-12-03 21:28:10 +0000371 debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
wdenkc6097192002-11-03 00:24:07 +0000372
373# ifdef CONFIG_BOOT_RETRY_TIME
wdenkb00ec162003-06-19 23:40:20 +0000374 init_cmd_timeout ();
wdenkc6097192002-11-03 00:24:07 +0000375# endif /* CONFIG_BOOT_RETRY_TIME */
376
Yuri Tikhonovd773efb2008-02-04 14:11:03 +0100377#ifdef CONFIG_POST
378 if (gd->flags & GD_FLG_POSTFAIL) {
379 s = getenv("failbootcmd");
380 }
381 else
382#endif /* CONFIG_POST */
wdenk0a658552003-08-05 17:43:17 +0000383#ifdef CONFIG_BOOTCOUNT_LIMIT
384 if (bootlimit && (bootcount > bootlimit)) {
385 printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
386 (unsigned)bootlimit);
387 s = getenv ("altbootcmd");
388 }
389 else
390#endif /* CONFIG_BOOTCOUNT_LIMIT */
391 s = getenv ("bootcmd");
wdenk9dd2b882002-12-03 21:28:10 +0000392
393 debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
394
wdenkc6097192002-11-03 00:24:07 +0000395 if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
396# ifdef CONFIG_AUTOBOOT_KEYED
397 int prev = disable_ctrlc(1); /* disable Control C checking */
398# endif
399
400# ifndef CFG_HUSH_PARSER
401 run_command (s, 0);
402# else
403 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
404 FLAG_EXIT_FROM_LOOP);
405# endif
406
407# ifdef CONFIG_AUTOBOOT_KEYED
408 disable_ctrlc(prev); /* restore Control C checking */
409# endif
410 }
wdenk452cfd62002-11-19 11:04:11 +0000411
412# ifdef CONFIG_MENUKEY
wdenk9dd2b882002-12-03 21:28:10 +0000413 if (menukey == CONFIG_MENUKEY) {
wdenk452cfd62002-11-19 11:04:11 +0000414 s = getenv("menucmd");
wdenk9dd2b882002-12-03 21:28:10 +0000415 if (s) {
wdenk452cfd62002-11-19 11:04:11 +0000416# ifndef CFG_HUSH_PARSER
wdenk91a4f362005-01-09 23:33:49 +0000417 run_command (s, 0);
wdenk452cfd62002-11-19 11:04:11 +0000418# else
419 parse_string_outer(s, FLAG_PARSE_SEMICOLON |
420 FLAG_EXIT_FROM_LOOP);
421# endif
422 }
423 }
424#endif /* CONFIG_MENUKEY */
wdenkc6097192002-11-03 00:24:07 +0000425#endif /* CONFIG_BOOTDELAY */
426
wdenk452cfd62002-11-19 11:04:11 +0000427#ifdef CONFIG_AMIGAONEG3SE
428 {
429 extern void video_banner(void);
430 video_banner();
431 }
432#endif
433
wdenkc6097192002-11-03 00:24:07 +0000434 /*
435 * Main Loop for Monitor Command Processing
436 */
437#ifdef CFG_HUSH_PARSER
438 parse_file_outer();
439 /* This point is never reached */
440 for (;;);
441#else
442 for (;;) {
443#ifdef CONFIG_BOOT_RETRY_TIME
444 if (rc >= 0) {
445 /* Saw enough of a valid command to
446 * restart the timeout.
447 */
448 reset_cmd_timeout();
449 }
450#endif
451 len = readline (CFG_PROMPT);
452
453 flag = 0; /* assume no special flags for now */
454 if (len > 0)
455 strcpy (lastcommand, console_buffer);
456 else if (len == 0)
457 flag |= CMD_FLAG_REPEAT;
458#ifdef CONFIG_BOOT_RETRY_TIME
459 else if (len == -2) {
460 /* -2 means timed out, retry autoboot
461 */
wdenk42c05472004-03-23 22:14:11 +0000462 puts ("\nTimed out waiting for command\n");
wdenkc6097192002-11-03 00:24:07 +0000463# ifdef CONFIG_RESET_TO_RETRY
464 /* Reinit board to run initialization code again */
465 do_reset (NULL, 0, 0, NULL);
466# else
467 return; /* retry autoboot */
468# endif
469 }
470#endif
471
472 if (len == -1)
wdenk42c05472004-03-23 22:14:11 +0000473 puts ("<INTERRUPT>\n");
wdenkc6097192002-11-03 00:24:07 +0000474 else
475 rc = run_command (lastcommand, flag);
476
477 if (rc <= 0) {
478 /* invalid command or not repeatable, forget it */
479 lastcommand[0] = 0;
480 }
481 }
482#endif /*CFG_HUSH_PARSER*/
483}
484
wdenkb00ec162003-06-19 23:40:20 +0000485#ifdef CONFIG_BOOT_RETRY_TIME
486/***************************************************************************
Wolfgang Denk9c82c682006-10-28 00:38:39 +0200487 * initialize command line timeout
wdenkb00ec162003-06-19 23:40:20 +0000488 */
489void init_cmd_timeout(void)
490{
491 char *s = getenv ("bootretry");
492
493 if (s != NULL)
wdenk444f22b2003-12-07 21:39:28 +0000494 retry_time = (int)simple_strtol(s, NULL, 10);
wdenkb00ec162003-06-19 23:40:20 +0000495 else
496 retry_time = CONFIG_BOOT_RETRY_TIME;
497
498 if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
499 retry_time = CONFIG_BOOT_RETRY_MIN;
500}
501
wdenkc6097192002-11-03 00:24:07 +0000502/***************************************************************************
503 * reset command line timeout to retry_time seconds
504 */
wdenkc6097192002-11-03 00:24:07 +0000505void reset_cmd_timeout(void)
506{
507 endtime = endtick(retry_time);
508}
509#endif
510
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200511#ifdef CONFIG_CMDLINE_EDITING
512
513/*
514 * cmdline-editing related codes from vivi.
515 * Author: Janghoon Lyu <nandy@mizi.com>
516 */
517
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200518#define putnstr(str,n) do { \
Andrew Klossnere4ad4542008-07-07 06:41:14 -0700519 printf ("%.*s", (int)n, str); \
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200520 } while (0)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200521
522#define CTL_CH(c) ((c) - 'a' + 1)
523
524#define MAX_CMDBUF_SIZE 256
525
526#define CTL_BACKSPACE ('\b')
527#define DEL ((char)255)
528#define DEL7 ((char)127)
529#define CREAD_HIST_CHAR ('!')
530
531#define getcmd_putch(ch) putc(ch)
532#define getcmd_getch() getc()
533#define getcmd_cbeep() getcmd_putch('\a')
534
535#define HIST_MAX 20
536#define HIST_SIZE MAX_CMDBUF_SIZE
537
538static int hist_max = 0;
539static int hist_add_idx = 0;
540static int hist_cur = -1;
541unsigned hist_num = 0;
542
543char* hist_list[HIST_MAX];
544char hist_lines[HIST_MAX][HIST_SIZE];
545
546#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
547
548static void hist_init(void)
549{
550 int i;
551
552 hist_max = 0;
553 hist_add_idx = 0;
554 hist_cur = -1;
555 hist_num = 0;
556
557 for (i = 0; i < HIST_MAX; i++) {
558 hist_list[i] = hist_lines[i];
559 hist_list[i][0] = '\0';
560 }
561}
562
563static void cread_add_to_hist(char *line)
564{
565 strcpy(hist_list[hist_add_idx], line);
566
567 if (++hist_add_idx >= HIST_MAX)
568 hist_add_idx = 0;
569
570 if (hist_add_idx > hist_max)
571 hist_max = hist_add_idx;
572
573 hist_num++;
574}
575
576static char* hist_prev(void)
577{
578 char *ret;
579 int old_cur;
580
581 if (hist_cur < 0)
582 return NULL;
583
584 old_cur = hist_cur;
585 if (--hist_cur < 0)
586 hist_cur = hist_max;
587
588 if (hist_cur == hist_add_idx) {
589 hist_cur = old_cur;
590 ret = NULL;
591 } else
592 ret = hist_list[hist_cur];
593
594 return (ret);
595}
596
597static char* hist_next(void)
598{
599 char *ret;
600
601 if (hist_cur < 0)
602 return NULL;
603
604 if (hist_cur == hist_add_idx)
605 return NULL;
606
607 if (++hist_cur > hist_max)
608 hist_cur = 0;
609
610 if (hist_cur == hist_add_idx) {
611 ret = "";
612 } else
613 ret = hist_list[hist_cur];
614
615 return (ret);
616}
617
Stefan Roese59ff5a52006-07-27 16:11:19 +0200618#ifndef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200619static void cread_print_hist_list(void)
620{
621 int i;
622 unsigned long n;
623
624 n = hist_num - hist_max;
625
626 i = hist_add_idx + 1;
627 while (1) {
628 if (i > hist_max)
629 i = 0;
630 if (i == hist_add_idx)
631 break;
632 printf("%s\n", hist_list[i]);
633 n++;
634 i++;
635 }
636}
Stefan Roese59ff5a52006-07-27 16:11:19 +0200637#endif /* CONFIG_CMDLINE_EDITING */
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200638
639#define BEGINNING_OF_LINE() { \
640 while (num) { \
641 getcmd_putch(CTL_BACKSPACE); \
642 num--; \
643 } \
644}
645
646#define ERASE_TO_EOL() { \
647 if (num < eol_num) { \
648 int tmp; \
649 for (tmp = num; tmp < eol_num; tmp++) \
650 getcmd_putch(' '); \
651 while (tmp-- > num) \
652 getcmd_putch(CTL_BACKSPACE); \
653 eol_num = num; \
654 } \
655}
656
657#define REFRESH_TO_EOL() { \
658 if (num < eol_num) { \
659 wlen = eol_num - num; \
660 putnstr(buf + num, wlen); \
661 num = eol_num; \
662 } \
663}
664
665static void cread_add_char(char ichar, int insert, unsigned long *num,
666 unsigned long *eol_num, char *buf, unsigned long len)
667{
668 unsigned long wlen;
669
670 /* room ??? */
671 if (insert || *num == *eol_num) {
672 if (*eol_num > len - 1) {
673 getcmd_cbeep();
674 return;
675 }
676 (*eol_num)++;
677 }
678
679 if (insert) {
680 wlen = *eol_num - *num;
681 if (wlen > 1) {
682 memmove(&buf[*num+1], &buf[*num], wlen-1);
683 }
684
685 buf[*num] = ichar;
686 putnstr(buf + *num, wlen);
687 (*num)++;
688 while (--wlen) {
689 getcmd_putch(CTL_BACKSPACE);
690 }
691 } else {
692 /* echo the character */
693 wlen = 1;
694 buf[*num] = ichar;
695 putnstr(buf + *num, wlen);
696 (*num)++;
697 }
698}
699
700static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
701 unsigned long *eol_num, char *buf, unsigned long len)
702{
703 while (strsize--) {
704 cread_add_char(*str, insert, num, eol_num, buf, len);
705 str++;
706 }
707}
708
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100709static int cread_line(const char *const prompt, char *buf, unsigned int *len)
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200710{
711 unsigned long num = 0;
712 unsigned long eol_num = 0;
713 unsigned long rlen;
714 unsigned long wlen;
715 char ichar;
716 int insert = 1;
717 int esc_len = 0;
718 int rc = 0;
719 char esc_save[8];
720
721 while (1) {
722 rlen = 1;
Andreas Engel3a0ba102008-01-09 17:10:56 +0100723#ifdef CONFIG_BOOT_RETRY_TIME
724 while (!tstc()) { /* while no incoming data */
725 if (retry_time >= 0 && get_ticks() > endtime)
726 return (-2); /* timed out */
727 }
728#endif
729
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200730 ichar = getcmd_getch();
731
732 if ((ichar == '\n') || (ichar == '\r')) {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200733 putc('\n');
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200734 break;
735 }
736
737 /*
738 * handle standard linux xterm esc sequences for arrow key, etc.
739 */
740 if (esc_len != 0) {
741 if (esc_len == 1) {
742 if (ichar == '[') {
743 esc_save[esc_len] = ichar;
744 esc_len = 2;
745 } else {
746 cread_add_str(esc_save, esc_len, insert,
747 &num, &eol_num, buf, *len);
748 esc_len = 0;
749 }
750 continue;
751 }
752
753 switch (ichar) {
754
755 case 'D': /* <- key */
756 ichar = CTL_CH('b');
757 esc_len = 0;
758 break;
759 case 'C': /* -> key */
760 ichar = CTL_CH('f');
761 esc_len = 0;
762 break; /* pass off to ^F handler */
763 case 'H': /* Home key */
764 ichar = CTL_CH('a');
765 esc_len = 0;
766 break; /* pass off to ^A handler */
767 case 'A': /* up arrow */
768 ichar = CTL_CH('p');
769 esc_len = 0;
770 break; /* pass off to ^P handler */
771 case 'B': /* down arrow */
772 ichar = CTL_CH('n');
773 esc_len = 0;
774 break; /* pass off to ^N handler */
775 default:
776 esc_save[esc_len++] = ichar;
777 cread_add_str(esc_save, esc_len, insert,
778 &num, &eol_num, buf, *len);
779 esc_len = 0;
780 continue;
781 }
782 }
783
784 switch (ichar) {
785 case 0x1b:
786 if (esc_len == 0) {
787 esc_save[esc_len] = ichar;
788 esc_len = 1;
789 } else {
Wolfgang Denk92549de2006-07-21 11:34:34 +0200790 puts("impossible condition #876\n");
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200791 esc_len = 0;
792 }
793 break;
794
795 case CTL_CH('a'):
796 BEGINNING_OF_LINE();
797 break;
798 case CTL_CH('c'): /* ^C - break */
799 *buf = '\0'; /* discard input */
800 return (-1);
801 case CTL_CH('f'):
802 if (num < eol_num) {
803 getcmd_putch(buf[num]);
804 num++;
805 }
806 break;
807 case CTL_CH('b'):
808 if (num) {
809 getcmd_putch(CTL_BACKSPACE);
810 num--;
811 }
812 break;
813 case CTL_CH('d'):
814 if (num < eol_num) {
815 wlen = eol_num - num - 1;
816 if (wlen) {
817 memmove(&buf[num], &buf[num+1], wlen);
818 putnstr(buf + num, wlen);
819 }
820
821 getcmd_putch(' ');
822 do {
823 getcmd_putch(CTL_BACKSPACE);
824 } while (wlen--);
825 eol_num--;
826 }
827 break;
828 case CTL_CH('k'):
829 ERASE_TO_EOL();
830 break;
831 case CTL_CH('e'):
832 REFRESH_TO_EOL();
833 break;
834 case CTL_CH('o'):
835 insert = !insert;
836 break;
837 case CTL_CH('x'):
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100838 case CTL_CH('u'):
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200839 BEGINNING_OF_LINE();
840 ERASE_TO_EOL();
841 break;
842 case DEL:
843 case DEL7:
844 case 8:
845 if (num) {
846 wlen = eol_num - num;
847 num--;
848 memmove(&buf[num], &buf[num+1], wlen);
849 getcmd_putch(CTL_BACKSPACE);
850 putnstr(buf + num, wlen);
851 getcmd_putch(' ');
852 do {
853 getcmd_putch(CTL_BACKSPACE);
854 } while (wlen--);
855 eol_num--;
856 }
857 break;
858 case CTL_CH('p'):
859 case CTL_CH('n'):
860 {
861 char * hline;
862
863 esc_len = 0;
864
865 if (ichar == CTL_CH('p'))
866 hline = hist_prev();
867 else
868 hline = hist_next();
869
870 if (!hline) {
871 getcmd_cbeep();
872 continue;
873 }
874
875 /* nuke the current line */
876 /* first, go home */
877 BEGINNING_OF_LINE();
878
879 /* erase to end of line */
880 ERASE_TO_EOL();
881
882 /* copy new line into place and display */
883 strcpy(buf, hline);
884 eol_num = strlen(buf);
885 REFRESH_TO_EOL();
886 continue;
887 }
Jean-Christophe PLAGNIOL-VILLARD6366f002007-12-22 15:52:58 +0100888#ifdef CONFIG_AUTO_COMPLETE
889 case '\t': {
890 int num2, col;
891
892 /* do not autocomplete when in the middle */
893 if (num < eol_num) {
894 getcmd_cbeep();
895 break;
896 }
897
898 buf[num] = '\0';
899 col = strlen(prompt) + eol_num;
900 num2 = num;
901 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
902 col = num2 - num;
903 num += col;
904 eol_num += col;
905 }
906 break;
907 }
908#endif
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200909 default:
910 cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
911 break;
912 }
913 }
914 *len = eol_num;
915 buf[eol_num] = '\0'; /* lose the newline */
916
917 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
918 cread_add_to_hist(buf);
919 hist_cur = hist_add_idx;
920
921 return (rc);
922}
923
924#endif /* CONFIG_CMDLINE_EDITING */
925
wdenkc6097192002-11-03 00:24:07 +0000926/****************************************************************************/
927
928/*
929 * Prompt for input and read a line.
930 * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
931 * time out when time goes past endtime (timebase time in ticks).
932 * Return: number of read characters
933 * -1 if break
934 * -2 if timed out
935 */
936int readline (const char *const prompt)
937{
James Yang20ffa972008-01-09 11:17:49 -0600938 return readline_into_buffer(prompt, console_buffer);
939}
940
941
942int readline_into_buffer (const char *const prompt, char * buffer)
943{
944 char *p = buffer;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200945#ifdef CONFIG_CMDLINE_EDITING
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200946 unsigned int len=MAX_CMDBUF_SIZE;
Stefan Roesedc5ff702006-08-07 15:08:44 +0200947 int rc;
Wolfgang Denk4c9ca782006-07-21 11:33:45 +0200948 static int initted = 0;
949
James Yangb83983f2008-05-05 10:22:53 -0500950 /*
951 * History uses a global array which is not
952 * writable until after relocation to RAM.
953 * Revert to non-history version if still
954 * running from flash.
955 */
956 if (gd->flags & GD_FLG_RELOC) {
957 if (!initted) {
958 hist_init();
959 initted = 1;
960 }
961
962 puts (prompt);
963
964 rc = cread_line(prompt, p, &len);
965 return rc < 0 ? rc : len;
966
967 } else {
968#endif /* CONFIG_CMDLINE_EDITING */
Kumar Galae225b172008-01-10 02:22:05 -0600969 char * p_buf = p;
wdenkc6097192002-11-03 00:24:07 +0000970 int n = 0; /* buffer index */
971 int plen = 0; /* prompt length */
972 int col; /* output column cnt */
973 char c;
974
975 /* print prompt */
976 if (prompt) {
977 plen = strlen (prompt);
978 puts (prompt);
979 }
980 col = plen;
981
982 for (;;) {
983#ifdef CONFIG_BOOT_RETRY_TIME
984 while (!tstc()) { /* while no incoming data */
985 if (retry_time >= 0 && get_ticks() > endtime)
986 return (-2); /* timed out */
987 }
988#endif
989 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
990
991#ifdef CONFIG_SHOW_ACTIVITY
992 while (!tstc()) {
993 extern void show_activity(int arg);
994 show_activity(0);
995 }
996#endif
997 c = getc();
998
999 /*
1000 * Special character handling
1001 */
1002 switch (c) {
1003 case '\r': /* Enter */
1004 case '\n':
1005 *p = '\0';
1006 puts ("\r\n");
James Yang20ffa972008-01-09 11:17:49 -06001007 return (p - p_buf);
wdenkc6097192002-11-03 00:24:07 +00001008
wdenk8d4d1f62004-03-23 22:37:33 +00001009 case '\0': /* nul */
1010 continue;
1011
wdenkc6097192002-11-03 00:24:07 +00001012 case 0x03: /* ^C - break */
James Yang20ffa972008-01-09 11:17:49 -06001013 p_buf[0] = '\0'; /* discard input */
wdenkc6097192002-11-03 00:24:07 +00001014 return (-1);
1015
1016 case 0x15: /* ^U - erase line */
1017 while (col > plen) {
1018 puts (erase_seq);
1019 --col;
1020 }
James Yang20ffa972008-01-09 11:17:49 -06001021 p = p_buf;
wdenkc6097192002-11-03 00:24:07 +00001022 n = 0;
1023 continue;
1024
Wolfgang Denk70df7bc2007-06-22 23:59:00 +02001025 case 0x17: /* ^W - erase word */
James Yang20ffa972008-01-09 11:17:49 -06001026 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001027 while ((n > 0) && (*p != ' ')) {
James Yang20ffa972008-01-09 11:17:49 -06001028 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001029 }
1030 continue;
1031
1032 case 0x08: /* ^H - backspace */
1033 case 0x7F: /* DEL - backspace */
James Yang20ffa972008-01-09 11:17:49 -06001034 p=delete_char(p_buf, p, &col, &n, plen);
wdenkc6097192002-11-03 00:24:07 +00001035 continue;
1036
1037 default:
1038 /*
1039 * Must be a normal character then
1040 */
1041 if (n < CFG_CBSIZE-2) {
1042 if (c == '\t') { /* expand TABs */
wdenk3902d702004-04-15 18:22:41 +00001043#ifdef CONFIG_AUTO_COMPLETE
1044 /* if auto completion triggered just continue */
1045 *p = '\0';
1046 if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
James Yang20ffa972008-01-09 11:17:49 -06001047 p = p_buf + n; /* reset */
wdenk3902d702004-04-15 18:22:41 +00001048 continue;
1049 }
1050#endif
wdenkc6097192002-11-03 00:24:07 +00001051 puts (tab_seq+(col&07));
1052 col += 8 - (col&07);
1053 } else {
1054 ++col; /* echo input */
1055 putc (c);
1056 }
1057 *p++ = c;
1058 ++n;
1059 } else { /* Buffer full */
1060 putc ('\a');
1061 }
1062 }
1063 }
James Yangb83983f2008-05-05 10:22:53 -05001064#ifdef CONFIG_CMDLINE_EDITING
1065 }
1066#endif
wdenkc6097192002-11-03 00:24:07 +00001067}
1068
1069/****************************************************************************/
1070
1071static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
1072{
1073 char *s;
1074
1075 if (*np == 0) {
1076 return (p);
1077 }
1078
1079 if (*(--p) == '\t') { /* will retype the whole line */
1080 while (*colp > plen) {
1081 puts (erase_seq);
1082 (*colp)--;
1083 }
1084 for (s=buffer; s<p; ++s) {
1085 if (*s == '\t') {
1086 puts (tab_seq+((*colp) & 07));
1087 *colp += 8 - ((*colp) & 07);
1088 } else {
1089 ++(*colp);
1090 putc (*s);
1091 }
1092 }
1093 } else {
1094 puts (erase_seq);
1095 (*colp)--;
1096 }
1097 (*np)--;
1098 return (p);
1099}
1100
1101/****************************************************************************/
1102
1103int parse_line (char *line, char *argv[])
1104{
1105 int nargs = 0;
1106
1107#ifdef DEBUG_PARSER
1108 printf ("parse_line: \"%s\"\n", line);
1109#endif
1110 while (nargs < CFG_MAXARGS) {
1111
1112 /* skip any white space */
1113 while ((*line == ' ') || (*line == '\t')) {
1114 ++line;
1115 }
1116
1117 if (*line == '\0') { /* end of line, no more args */
1118 argv[nargs] = NULL;
1119#ifdef DEBUG_PARSER
1120 printf ("parse_line: nargs=%d\n", nargs);
1121#endif
1122 return (nargs);
1123 }
1124
1125 argv[nargs++] = line; /* begin of argument string */
1126
1127 /* find end of string */
1128 while (*line && (*line != ' ') && (*line != '\t')) {
1129 ++line;
1130 }
1131
1132 if (*line == '\0') { /* end of line, no more args */
1133 argv[nargs] = NULL;
1134#ifdef DEBUG_PARSER
1135 printf ("parse_line: nargs=%d\n", nargs);
1136#endif
1137 return (nargs);
1138 }
1139
1140 *line++ = '\0'; /* terminate current arg */
1141 }
1142
1143 printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
1144
1145#ifdef DEBUG_PARSER
1146 printf ("parse_line: nargs=%d\n", nargs);
1147#endif
1148 return (nargs);
1149}
1150
1151/****************************************************************************/
1152
1153static void process_macros (const char *input, char *output)
1154{
1155 char c, prev;
1156 const char *varname_start = NULL;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001157 int inputcnt = strlen (input);
wdenkc6097192002-11-03 00:24:07 +00001158 int outputcnt = CFG_CBSIZE;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001159 int state = 0; /* 0 = waiting for '$' */
1160
1161 /* 1 = waiting for '(' or '{' */
1162 /* 2 = waiting for ')' or '}' */
1163 /* 3 = waiting for ''' */
wdenkc6097192002-11-03 00:24:07 +00001164#ifdef DEBUG_PARSER
1165 char *output_start = output;
1166
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001167 printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
1168 input);
wdenkc6097192002-11-03 00:24:07 +00001169#endif
1170
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001171 prev = '\0'; /* previous character */
wdenkc6097192002-11-03 00:24:07 +00001172
1173 while (inputcnt && outputcnt) {
wdenk57b2d802003-06-27 21:31:46 +00001174 c = *input++;
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001175 inputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001176
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001177 if (state != 3) {
1178 /* remove one level of escape characters */
1179 if ((c == '\\') && (prev != '\\')) {
1180 if (inputcnt-- == 0)
1181 break;
1182 prev = c;
1183 c = *input++;
1184 }
wdenkc6097192002-11-03 00:24:07 +00001185 }
wdenkc6097192002-11-03 00:24:07 +00001186
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001187 switch (state) {
1188 case 0: /* Waiting for (unescaped) $ */
1189 if ((c == '\'') && (prev != '\\')) {
1190 state = 3;
1191 break;
1192 }
1193 if ((c == '$') && (prev != '\\')) {
1194 state++;
1195 } else {
wdenkc6097192002-11-03 00:24:07 +00001196 *(output++) = c;
1197 outputcnt--;
1198 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001199 break;
1200 case 1: /* Waiting for ( */
1201 if (c == '(' || c == '{') {
1202 state++;
1203 varname_start = input;
1204 } else {
1205 state = 0;
1206 *(output++) = '$';
1207 outputcnt--;
wdenkc6097192002-11-03 00:24:07 +00001208
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001209 if (outputcnt) {
1210 *(output++) = c;
1211 outputcnt--;
1212 }
wdenkc6097192002-11-03 00:24:07 +00001213 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001214 break;
1215 case 2: /* Waiting for ) */
1216 if (c == ')' || c == '}') {
1217 int i;
1218 char envname[CFG_CBSIZE], *envval;
1219 int envcnt = input - varname_start - 1; /* Varname # of chars */
wdenkc6097192002-11-03 00:24:07 +00001220
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001221 /* Get the varname */
1222 for (i = 0; i < envcnt; i++) {
1223 envname[i] = varname_start[i];
wdenkc6097192002-11-03 00:24:07 +00001224 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001225 envname[i] = 0;
1226
1227 /* Get its value */
1228 envval = getenv (envname);
1229
1230 /* Copy into the line if it exists */
1231 if (envval != NULL)
1232 while ((*envval) && outputcnt) {
1233 *(output++) = *(envval++);
1234 outputcnt--;
1235 }
1236 /* Look for another '$' */
1237 state = 0;
1238 }
1239 break;
1240 case 3: /* Waiting for ' */
1241 if ((c == '\'') && (prev != '\\')) {
1242 state = 0;
1243 } else {
1244 *(output++) = c;
1245 outputcnt--;
1246 }
1247 break;
wdenkc6097192002-11-03 00:24:07 +00001248 }
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001249 prev = c;
wdenkc6097192002-11-03 00:24:07 +00001250 }
1251
1252 if (outputcnt)
1253 *output = 0;
Bartlomiej Siekacadc8e82007-05-27 17:04:18 +02001254 else
1255 *(output - 1) = 0;
wdenkc6097192002-11-03 00:24:07 +00001256
1257#ifdef DEBUG_PARSER
1258 printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
Wolfgang Denk9c82c682006-10-28 00:38:39 +02001259 strlen (output_start), output_start);
wdenkc6097192002-11-03 00:24:07 +00001260#endif
1261}
1262
1263/****************************************************************************
1264 * returns:
1265 * 1 - command executed, repeatable
1266 * 0 - command executed but not repeatable, interrupted commands are
1267 * always considered not repeatable
1268 * -1 - not executed (unrecognized, bootd recursion or too many args)
1269 * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
1270 * considered unrecognized)
1271 *
1272 * WARNING:
1273 *
1274 * We must create a temporary copy of the command since the command we get
1275 * may be the result from getenv(), which returns a pointer directly to
1276 * the environment data, which may change magicly when the command we run
1277 * creates or modifies environment variables (like "bootp" does).
1278 */
1279
1280int run_command (const char *cmd, int flag)
1281{
1282 cmd_tbl_t *cmdtp;
1283 char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
1284 char *token; /* start of token in cmdbuf */
1285 char *sep; /* end of token (separator) in cmdbuf */
1286 char finaltoken[CFG_CBSIZE];
1287 char *str = cmdbuf;
1288 char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
wdenkf4688a22003-05-28 08:06:31 +00001289 int argc, inquotes;
wdenkc6097192002-11-03 00:24:07 +00001290 int repeatable = 1;
wdenkf4688a22003-05-28 08:06:31 +00001291 int rc = 0;
wdenkc6097192002-11-03 00:24:07 +00001292
1293#ifdef DEBUG_PARSER
1294 printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
1295 puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
1296 puts ("\"\n");
1297#endif
1298
1299 clear_ctrlc(); /* forget any previous Control C */
1300
1301 if (!cmd || !*cmd) {
1302 return -1; /* empty command */
1303 }
1304
1305 if (strlen(cmd) >= CFG_CBSIZE) {
1306 puts ("## Command too long!\n");
1307 return -1;
1308 }
1309
1310 strcpy (cmdbuf, cmd);
1311
1312 /* Process separators and check for invalid
1313 * repeatable commands
1314 */
1315
1316#ifdef DEBUG_PARSER
1317 printf ("[PROCESS_SEPARATORS] %s\n", cmd);
1318#endif
1319 while (*str) {
1320
1321 /*
1322 * Find separator, or string end
1323 * Allow simple escape of ';' by writing "\;"
1324 */
wdenk654868c2003-01-02 23:57:29 +00001325 for (inquotes = 0, sep = str; *sep; sep++) {
1326 if ((*sep=='\'') &&
1327 (*(sep-1) != '\\'))
1328 inquotes=!inquotes;
1329
1330 if (!inquotes &&
1331 (*sep == ';') && /* separator */
wdenkc6097192002-11-03 00:24:07 +00001332 ( sep != str) && /* past string start */
1333 (*(sep-1) != '\\')) /* and NOT escaped */
1334 break;
1335 }
1336
1337 /*
1338 * Limit the token to data between separators
1339 */
1340 token = str;
1341 if (*sep) {
1342 str = sep + 1; /* start of command for next pass */
1343 *sep = '\0';
1344 }
1345 else
1346 str = sep; /* no more commands for next pass */
1347#ifdef DEBUG_PARSER
1348 printf ("token: \"%s\"\n", token);
1349#endif
1350
1351 /* find macros in this token and replace them */
1352 process_macros (token, finaltoken);
1353
1354 /* Extract arguments */
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001355 if ((argc = parse_line (finaltoken, argv)) == 0) {
1356 rc = -1; /* no command at all */
1357 continue;
1358 }
wdenkc6097192002-11-03 00:24:07 +00001359
1360 /* Look up command in command table */
1361 if ((cmdtp = find_cmd(argv[0])) == NULL) {
1362 printf ("Unknown command '%s' - try 'help'\n", argv[0]);
wdenkf4688a22003-05-28 08:06:31 +00001363 rc = -1; /* give up after bad command */
1364 continue;
wdenkc6097192002-11-03 00:24:07 +00001365 }
1366
1367 /* found - check max args */
1368 if (argc > cmdtp->maxargs) {
1369 printf ("Usage:\n%s\n", cmdtp->usage);
wdenkf4688a22003-05-28 08:06:31 +00001370 rc = -1;
1371 continue;
wdenkc6097192002-11-03 00:24:07 +00001372 }
1373
Jon Loeliger052fc842007-07-08 18:10:08 -05001374#if defined(CONFIG_CMD_BOOTD)
wdenkc6097192002-11-03 00:24:07 +00001375 /* avoid "bootd" recursion */
1376 if (cmdtp->cmd == do_bootd) {
1377#ifdef DEBUG_PARSER
1378 printf ("[%s]\n", finaltoken);
1379#endif
1380 if (flag & CMD_FLAG_BOOTD) {
wdenk42c05472004-03-23 22:14:11 +00001381 puts ("'bootd' recursion detected\n");
wdenkf4688a22003-05-28 08:06:31 +00001382 rc = -1;
1383 continue;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001384 } else {
wdenkc6097192002-11-03 00:24:07 +00001385 flag |= CMD_FLAG_BOOTD;
Wolfgang Denkd44e7bb2006-03-12 02:20:55 +01001386 }
wdenkc6097192002-11-03 00:24:07 +00001387 }
Jon Loeligerd704d912007-07-10 11:02:44 -05001388#endif
wdenkc6097192002-11-03 00:24:07 +00001389
1390 /* OK - call function to do the command */
1391 if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
wdenkf4688a22003-05-28 08:06:31 +00001392 rc = -1;
wdenkc6097192002-11-03 00:24:07 +00001393 }
1394
1395 repeatable &= cmdtp->repeatable;
1396
1397 /* Did the user stop this? */
1398 if (had_ctrlc ())
Detlev Zundeleeab5a12007-05-23 18:47:48 +02001399 return -1; /* if stopped then not repeatable */
wdenkc6097192002-11-03 00:24:07 +00001400 }
1401
wdenkf4688a22003-05-28 08:06:31 +00001402 return rc ? rc : repeatable;
wdenkc6097192002-11-03 00:24:07 +00001403}
1404
1405/****************************************************************************/
1406
Jon Loeliger052fc842007-07-08 18:10:08 -05001407#if defined(CONFIG_CMD_RUN)
wdenkc6097192002-11-03 00:24:07 +00001408int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
1409{
1410 int i;
wdenkc6097192002-11-03 00:24:07 +00001411
1412 if (argc < 2) {
1413 printf ("Usage:\n%s\n", cmdtp->usage);
1414 return 1;
1415 }
1416
1417 for (i=1; i<argc; ++i) {
wdenkb02744a2003-04-05 00:53:31 +00001418 char *arg;
1419
1420 if ((arg = getenv (argv[i])) == NULL) {
1421 printf ("## Error: \"%s\" not defined\n", argv[i]);
1422 return 1;
1423 }
wdenkc6097192002-11-03 00:24:07 +00001424#ifndef CFG_HUSH_PARSER
wdenkb02744a2003-04-05 00:53:31 +00001425 if (run_command (arg, flag) == -1)
1426 return 1;
wdenkc6097192002-11-03 00:24:07 +00001427#else
wdenkb02744a2003-04-05 00:53:31 +00001428 if (parse_string_outer(arg,
wdenk541a76d2003-05-03 15:50:43 +00001429 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
wdenkb02744a2003-04-05 00:53:31 +00001430 return 1;
wdenkc6097192002-11-03 00:24:07 +00001431#endif
1432 }
wdenkb02744a2003-04-05 00:53:31 +00001433 return 0;
wdenkc6097192002-11-03 00:24:07 +00001434}
Jon Loeligerd704d912007-07-10 11:02:44 -05001435#endif