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