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