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