main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. /* #define DEBUG */
  24. #include <common.h>
  25. #include <watchdog.h>
  26. #include <command.h>
  27. #include <cmd_nvedit.h>
  28. #include <cmd_bootm.h>
  29. #include <malloc.h>
  30. #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
  31. #include <cmd_boot.h> /* for do_reset() prototype */
  32. #endif
  33. #ifdef CFG_HUSH_PARSER
  34. #include <hush.h>
  35. #endif
  36. #define MAX_DELAY_STOP_STR 32
  37. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  38. static int parse_line (char *, char *[]);
  39. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  40. static int abortboot(int);
  41. #endif
  42. #undef DEBUG_PARSER
  43. char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
  44. static char erase_seq[] = "\b \b"; /* erase sequence */
  45. static char tab_seq[] = " "; /* used to expand TABs */
  46. #ifdef CONFIG_BOOT_RETRY_TIME
  47. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  48. static int retry_time = -1; /* -1 so can call readline before main_loop */
  49. #endif
  50. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  51. #ifndef CONFIG_BOOT_RETRY_MIN
  52. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  53. #endif
  54. #ifdef CONFIG_MODEM_SUPPORT
  55. int do_mdm_init = 0;
  56. extern void mdm_init(void); /* defined in board.c */
  57. #endif
  58. /***************************************************************************
  59. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  60. * returns: 0 - no key string, allow autoboot
  61. * 1 - got key string, abort
  62. */
  63. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  64. # if defined(CONFIG_AUTOBOOT_KEYED)
  65. static __inline__ int abortboot(int bootdelay)
  66. {
  67. int abort = 0;
  68. uint64_t etime = endtick(bootdelay);
  69. struct
  70. {
  71. char* str;
  72. u_int len;
  73. int retry;
  74. }
  75. delaykey [] =
  76. {
  77. { str: getenv ("bootdelaykey"), retry: 1 },
  78. { str: getenv ("bootdelaykey2"), retry: 1 },
  79. { str: getenv ("bootstopkey"), retry: 0 },
  80. { str: getenv ("bootstopkey2"), retry: 0 },
  81. };
  82. char presskey [MAX_DELAY_STOP_STR];
  83. u_int presskey_len = 0;
  84. u_int presskey_max = 0;
  85. u_int i;
  86. # ifdef CONFIG_AUTOBOOT_PROMPT
  87. printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
  88. # endif
  89. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  90. if (delaykey[0].str == NULL)
  91. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  92. # endif
  93. # ifdef CONFIG_AUTOBOOT_DELAY_STR2
  94. if (delaykey[1].str == NULL)
  95. delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
  96. # endif
  97. # ifdef CONFIG_AUTOBOOT_STOP_STR
  98. if (delaykey[2].str == NULL)
  99. delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
  100. # endif
  101. # ifdef CONFIG_AUTOBOOT_STOP_STR2
  102. if (delaykey[3].str == NULL)
  103. delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
  104. # endif
  105. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  106. delaykey[i].len = delaykey[i].str == NULL ?
  107. 0 : strlen (delaykey[i].str);
  108. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  109. MAX_DELAY_STOP_STR : delaykey[i].len;
  110. presskey_max = presskey_max > delaykey[i].len ?
  111. presskey_max : delaykey[i].len;
  112. # if DEBUG_BOOTKEYS
  113. printf("%s key:<%s>\n",
  114. delaykey[i].retry ? "delay" : "stop",
  115. delaykey[i].str ? delaykey[i].str : "NULL");
  116. # endif
  117. }
  118. /* In order to keep up with incoming data, check timeout only
  119. * when catch up.
  120. */
  121. while (!abort && get_ticks() <= etime) {
  122. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  123. if (delaykey[i].len > 0 &&
  124. presskey_len >= delaykey[i].len &&
  125. memcmp (presskey + presskey_len - delaykey[i].len,
  126. delaykey[i].str,
  127. delaykey[i].len) == 0) {
  128. # if DEBUG_BOOTKEYS
  129. printf("got %skey\n",
  130. delaykey[i].retry ? "delay" : "stop");
  131. # endif
  132. # ifdef CONFIG_BOOT_RETRY_TIME
  133. /* don't retry auto boot */
  134. if (! delaykey[i].retry)
  135. retry_time = -1;
  136. # endif
  137. abort = 1;
  138. }
  139. }
  140. if (tstc()) {
  141. if (presskey_len < presskey_max) {
  142. presskey [presskey_len ++] = getc();
  143. }
  144. else {
  145. for (i = 0; i < presskey_max - 1; i ++)
  146. presskey [i] = presskey [i + 1];
  147. presskey [i] = getc();
  148. }
  149. }
  150. }
  151. # if DEBUG_BOOTKEYS
  152. if (!abort)
  153. printf("key timeout\n");
  154. # endif
  155. return abort;
  156. }
  157. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  158. #ifdef CONFIG_MENUKEY
  159. static int menukey = 0;
  160. #endif
  161. static __inline__ int abortboot(int bootdelay)
  162. {
  163. int abort = 0;
  164. #ifdef CONFIG_MENUPROMPT
  165. printf(CONFIG_MENUPROMPT, bootdelay);
  166. #else
  167. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  168. #endif
  169. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  170. /*
  171. * Check if key already pressed
  172. * Don't check if bootdelay < 0
  173. */
  174. if (bootdelay >= 0) {
  175. if (tstc()) { /* we got a key press */
  176. (void) getc(); /* consume input */
  177. printf ("\b\b\b 0\n");
  178. return 1; /* don't auto boot */
  179. }
  180. }
  181. #endif
  182. while (bootdelay > 0) {
  183. int i;
  184. --bootdelay;
  185. /* delay 100 * 10ms */
  186. for (i=0; !abort && i<100; ++i) {
  187. if (tstc()) { /* we got a key press */
  188. abort = 1; /* don't auto boot */
  189. bootdelay = 0; /* no more delay */
  190. # ifdef CONFIG_MENUKEY
  191. menukey = getc();
  192. # else
  193. (void) getc(); /* consume input */
  194. # endif
  195. break;
  196. }
  197. udelay (10000);
  198. }
  199. printf ("\b\b\b%2d ", bootdelay);
  200. }
  201. putc ('\n');
  202. return abort;
  203. }
  204. # endif /* CONFIG_AUTOBOOT_KEYED */
  205. #endif /* CONFIG_BOOTDELAY >= 0 */
  206. /****************************************************************************/
  207. void main_loop (void)
  208. {
  209. #ifndef CFG_HUSH_PARSER
  210. static char lastcommand[CFG_CBSIZE] = { 0, };
  211. int len;
  212. int rc = 1;
  213. int flag;
  214. #endif
  215. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  216. char *s;
  217. int bootdelay;
  218. #endif
  219. #ifdef CONFIG_PREBOOT
  220. char *p;
  221. #endif
  222. #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
  223. ulong bmp = 0; /* default bitmap */
  224. extern int trab_vfd (ulong bitmap);
  225. #ifdef CONFIG_MODEM_SUPPORT
  226. if (do_mdm_init)
  227. bmp = 1; /* alternate bitmap */
  228. #endif
  229. trab_vfd (bmp);
  230. #endif /* CONFIG_VFD && VFD_TEST_LOGO */
  231. #ifdef CONFIG_MODEM_SUPPORT
  232. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  233. if (do_mdm_init) {
  234. uchar *str = strdup(getenv("mdm_cmd"));
  235. setenv ("preboot", str); /* set or delete definition */
  236. if (str != NULL)
  237. free (str);
  238. mdm_init(); /* wait for modem connection */
  239. }
  240. #endif /* CONFIG_MODEM_SUPPORT */
  241. #ifdef CFG_HUSH_PARSER
  242. u_boot_hush_start ();
  243. #endif
  244. #ifdef CONFIG_PREBOOT
  245. if ((p = getenv ("preboot")) != NULL) {
  246. # ifdef CONFIG_AUTOBOOT_KEYED
  247. int prev = disable_ctrlc(1); /* disable Control C checking */
  248. # endif
  249. # ifndef CFG_HUSH_PARSER
  250. run_command (p, 0);
  251. # else
  252. parse_string_outer(p, FLAG_PARSE_SEMICOLON |
  253. FLAG_EXIT_FROM_LOOP);
  254. # endif
  255. # ifdef CONFIG_AUTOBOOT_KEYED
  256. disable_ctrlc(prev); /* restore Control C checking */
  257. # endif
  258. }
  259. #endif /* CONFIG_PREBOOT */
  260. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  261. s = getenv ("bootdelay");
  262. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  263. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  264. # ifdef CONFIG_BOOT_RETRY_TIME
  265. s = getenv ("bootretry");
  266. if (s != NULL)
  267. retry_time = (int)simple_strtoul(s, NULL, 10);
  268. else
  269. retry_time = CONFIG_BOOT_RETRY_TIME;
  270. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  271. retry_time = CONFIG_BOOT_RETRY_MIN;
  272. # endif /* CONFIG_BOOT_RETRY_TIME */
  273. s = getenv ("bootcmd");
  274. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  275. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  276. # ifdef CONFIG_AUTOBOOT_KEYED
  277. int prev = disable_ctrlc(1); /* disable Control C checking */
  278. # endif
  279. # ifndef CFG_HUSH_PARSER
  280. run_command (s, 0);
  281. # else
  282. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  283. FLAG_EXIT_FROM_LOOP);
  284. # endif
  285. # ifdef CONFIG_AUTOBOOT_KEYED
  286. disable_ctrlc(prev); /* restore Control C checking */
  287. # endif
  288. }
  289. # ifdef CONFIG_MENUKEY
  290. if (menukey == CONFIG_MENUKEY) {
  291. s = getenv("menucmd");
  292. if (s) {
  293. # ifndef CFG_HUSH_PARSER
  294. run_command (s, bd, 0);
  295. # else
  296. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  297. FLAG_EXIT_FROM_LOOP);
  298. # endif
  299. }
  300. }
  301. #endif /* CONFIG_MENUKEY */
  302. #endif /* CONFIG_BOOTDELAY */
  303. #ifdef CONFIG_AMIGAONEG3SE
  304. {
  305. extern void video_banner(void);
  306. video_banner();
  307. }
  308. #endif
  309. /*
  310. * Main Loop for Monitor Command Processing
  311. */
  312. #ifdef CFG_HUSH_PARSER
  313. parse_file_outer();
  314. /* This point is never reached */
  315. for (;;);
  316. #else
  317. for (;;) {
  318. #ifdef CONFIG_BOOT_RETRY_TIME
  319. if (rc >= 0) {
  320. /* Saw enough of a valid command to
  321. * restart the timeout.
  322. */
  323. reset_cmd_timeout();
  324. }
  325. #endif
  326. len = readline (CFG_PROMPT);
  327. flag = 0; /* assume no special flags for now */
  328. if (len > 0)
  329. strcpy (lastcommand, console_buffer);
  330. else if (len == 0)
  331. flag |= CMD_FLAG_REPEAT;
  332. #ifdef CONFIG_BOOT_RETRY_TIME
  333. else if (len == -2) {
  334. /* -2 means timed out, retry autoboot
  335. */
  336. printf("\nTimed out waiting for command\n");
  337. # ifdef CONFIG_RESET_TO_RETRY
  338. /* Reinit board to run initialization code again */
  339. do_reset (NULL, 0, 0, NULL);
  340. # else
  341. return; /* retry autoboot */
  342. # endif
  343. }
  344. #endif
  345. if (len == -1)
  346. printf ("<INTERRUPT>\n");
  347. else
  348. rc = run_command (lastcommand, flag);
  349. if (rc <= 0) {
  350. /* invalid command or not repeatable, forget it */
  351. lastcommand[0] = 0;
  352. }
  353. }
  354. #endif /*CFG_HUSH_PARSER*/
  355. }
  356. /***************************************************************************
  357. * reset command line timeout to retry_time seconds
  358. */
  359. #ifdef CONFIG_BOOT_RETRY_TIME
  360. void reset_cmd_timeout(void)
  361. {
  362. endtime = endtick(retry_time);
  363. }
  364. #endif
  365. /****************************************************************************/
  366. /*
  367. * Prompt for input and read a line.
  368. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  369. * time out when time goes past endtime (timebase time in ticks).
  370. * Return: number of read characters
  371. * -1 if break
  372. * -2 if timed out
  373. */
  374. int readline (const char *const prompt)
  375. {
  376. char *p = console_buffer;
  377. int n = 0; /* buffer index */
  378. int plen = 0; /* prompt length */
  379. int col; /* output column cnt */
  380. char c;
  381. /* print prompt */
  382. if (prompt) {
  383. plen = strlen (prompt);
  384. puts (prompt);
  385. }
  386. col = plen;
  387. for (;;) {
  388. #ifdef CONFIG_BOOT_RETRY_TIME
  389. while (!tstc()) { /* while no incoming data */
  390. if (retry_time >= 0 && get_ticks() > endtime)
  391. return (-2); /* timed out */
  392. }
  393. #endif
  394. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  395. #ifdef CONFIG_SHOW_ACTIVITY
  396. while (!tstc()) {
  397. extern void show_activity(int arg);
  398. show_activity(0);
  399. }
  400. #endif
  401. c = getc();
  402. /*
  403. * Special character handling
  404. */
  405. switch (c) {
  406. case '\r': /* Enter */
  407. case '\n':
  408. *p = '\0';
  409. puts ("\r\n");
  410. return (p - console_buffer);
  411. case 0x03: /* ^C - break */
  412. console_buffer[0] = '\0'; /* discard input */
  413. return (-1);
  414. case 0x15: /* ^U - erase line */
  415. while (col > plen) {
  416. puts (erase_seq);
  417. --col;
  418. }
  419. p = console_buffer;
  420. n = 0;
  421. continue;
  422. case 0x17: /* ^W - erase word */
  423. p=delete_char(console_buffer, p, &col, &n, plen);
  424. while ((n > 0) && (*p != ' ')) {
  425. p=delete_char(console_buffer, p, &col, &n, plen);
  426. }
  427. continue;
  428. case 0x08: /* ^H - backspace */
  429. case 0x7F: /* DEL - backspace */
  430. p=delete_char(console_buffer, p, &col, &n, plen);
  431. continue;
  432. default:
  433. /*
  434. * Must be a normal character then
  435. */
  436. if (n < CFG_CBSIZE-2) {
  437. if (c == '\t') { /* expand TABs */
  438. puts (tab_seq+(col&07));
  439. col += 8 - (col&07);
  440. } else {
  441. ++col; /* echo input */
  442. putc (c);
  443. }
  444. *p++ = c;
  445. ++n;
  446. } else { /* Buffer full */
  447. putc ('\a');
  448. }
  449. }
  450. }
  451. }
  452. /****************************************************************************/
  453. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  454. {
  455. char *s;
  456. if (*np == 0) {
  457. return (p);
  458. }
  459. if (*(--p) == '\t') { /* will retype the whole line */
  460. while (*colp > plen) {
  461. puts (erase_seq);
  462. (*colp)--;
  463. }
  464. for (s=buffer; s<p; ++s) {
  465. if (*s == '\t') {
  466. puts (tab_seq+((*colp) & 07));
  467. *colp += 8 - ((*colp) & 07);
  468. } else {
  469. ++(*colp);
  470. putc (*s);
  471. }
  472. }
  473. } else {
  474. puts (erase_seq);
  475. (*colp)--;
  476. }
  477. (*np)--;
  478. return (p);
  479. }
  480. /****************************************************************************/
  481. int parse_line (char *line, char *argv[])
  482. {
  483. int nargs = 0;
  484. #ifdef DEBUG_PARSER
  485. printf ("parse_line: \"%s\"\n", line);
  486. #endif
  487. while (nargs < CFG_MAXARGS) {
  488. /* skip any white space */
  489. while ((*line == ' ') || (*line == '\t')) {
  490. ++line;
  491. }
  492. if (*line == '\0') { /* end of line, no more args */
  493. argv[nargs] = NULL;
  494. #ifdef DEBUG_PARSER
  495. printf ("parse_line: nargs=%d\n", nargs);
  496. #endif
  497. return (nargs);
  498. }
  499. argv[nargs++] = line; /* begin of argument string */
  500. /* find end of string */
  501. while (*line && (*line != ' ') && (*line != '\t')) {
  502. ++line;
  503. }
  504. if (*line == '\0') { /* end of line, no more args */
  505. argv[nargs] = NULL;
  506. #ifdef DEBUG_PARSER
  507. printf ("parse_line: nargs=%d\n", nargs);
  508. #endif
  509. return (nargs);
  510. }
  511. *line++ = '\0'; /* terminate current arg */
  512. }
  513. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  514. #ifdef DEBUG_PARSER
  515. printf ("parse_line: nargs=%d\n", nargs);
  516. #endif
  517. return (nargs);
  518. }
  519. /****************************************************************************/
  520. static void process_macros (const char *input, char *output)
  521. {
  522. char c, prev;
  523. const char *varname_start = NULL;
  524. int inputcnt = strlen (input);
  525. int outputcnt = CFG_CBSIZE;
  526. int state = 0; /* 0 = waiting for '$' */
  527. /* 1 = waiting for '(' */
  528. /* 2 = waiting for ')' */
  529. #ifdef DEBUG_PARSER
  530. char *output_start = output;
  531. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  532. #endif
  533. prev = '\0'; /* previous character */
  534. while (inputcnt && outputcnt) {
  535. c = *input++;
  536. inputcnt--;
  537. /* remove one level of escape characters */
  538. if ((c == '\\') && (prev != '\\')) {
  539. if (inputcnt-- == 0)
  540. break;
  541. prev = c;
  542. c = *input++;
  543. }
  544. switch (state) {
  545. case 0: /* Waiting for (unescaped) $ */
  546. if ((c == '$') && (prev != '\\')) {
  547. state++;
  548. } else {
  549. *(output++) = c;
  550. outputcnt--;
  551. }
  552. break;
  553. case 1: /* Waiting for ( */
  554. if (c == '(') {
  555. state++;
  556. varname_start = input;
  557. } else {
  558. state = 0;
  559. *(output++) = '$';
  560. outputcnt--;
  561. if (outputcnt) {
  562. *(output++) = c;
  563. outputcnt--;
  564. }
  565. }
  566. break;
  567. case 2: /* Waiting for ) */
  568. if (c == ')') {
  569. int i;
  570. char envname[CFG_CBSIZE], *envval;
  571. int envcnt = input-varname_start-1; /* Varname # of chars */
  572. /* Get the varname */
  573. for (i = 0; i < envcnt; i++) {
  574. envname[i] = varname_start[i];
  575. }
  576. envname[i] = 0;
  577. /* Get its value */
  578. envval = getenv (envname);
  579. /* Copy into the line if it exists */
  580. if (envval != NULL)
  581. while ((*envval) && outputcnt) {
  582. *(output++) = *(envval++);
  583. outputcnt--;
  584. }
  585. /* Look for another '$' */
  586. state = 0;
  587. }
  588. break;
  589. }
  590. prev = c;
  591. }
  592. if (outputcnt)
  593. *output = 0;
  594. #ifdef DEBUG_PARSER
  595. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  596. strlen(output_start), output_start);
  597. #endif
  598. }
  599. /****************************************************************************
  600. * returns:
  601. * 1 - command executed, repeatable
  602. * 0 - command executed but not repeatable, interrupted commands are
  603. * always considered not repeatable
  604. * -1 - not executed (unrecognized, bootd recursion or too many args)
  605. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  606. * considered unrecognized)
  607. *
  608. * WARNING:
  609. *
  610. * We must create a temporary copy of the command since the command we get
  611. * may be the result from getenv(), which returns a pointer directly to
  612. * the environment data, which may change magicly when the command we run
  613. * creates or modifies environment variables (like "bootp" does).
  614. */
  615. int run_command (const char *cmd, int flag)
  616. {
  617. cmd_tbl_t *cmdtp;
  618. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  619. char *token; /* start of token in cmdbuf */
  620. char *sep; /* end of token (separator) in cmdbuf */
  621. char finaltoken[CFG_CBSIZE];
  622. char *str = cmdbuf;
  623. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  624. int argc;
  625. int repeatable = 1;
  626. #ifdef DEBUG_PARSER
  627. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  628. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  629. puts ("\"\n");
  630. #endif
  631. clear_ctrlc(); /* forget any previous Control C */
  632. if (!cmd || !*cmd) {
  633. return -1; /* empty command */
  634. }
  635. if (strlen(cmd) >= CFG_CBSIZE) {
  636. puts ("## Command too long!\n");
  637. return -1;
  638. }
  639. strcpy (cmdbuf, cmd);
  640. /* Process separators and check for invalid
  641. * repeatable commands
  642. */
  643. #ifdef DEBUG_PARSER
  644. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  645. #endif
  646. while (*str) {
  647. /*
  648. * Find separator, or string end
  649. * Allow simple escape of ';' by writing "\;"
  650. */
  651. for (sep = str; *sep; sep++) {
  652. if ((*sep == ';') && /* separator */
  653. ( sep != str) && /* past string start */
  654. (*(sep-1) != '\\')) /* and NOT escaped */
  655. break;
  656. }
  657. /*
  658. * Limit the token to data between separators
  659. */
  660. token = str;
  661. if (*sep) {
  662. str = sep + 1; /* start of command for next pass */
  663. *sep = '\0';
  664. }
  665. else
  666. str = sep; /* no more commands for next pass */
  667. #ifdef DEBUG_PARSER
  668. printf ("token: \"%s\"\n", token);
  669. #endif
  670. /* find macros in this token and replace them */
  671. process_macros (token, finaltoken);
  672. /* Extract arguments */
  673. argc = parse_line (finaltoken, argv);
  674. /* Look up command in command table */
  675. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  676. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  677. return -1; /* give up after bad command */
  678. }
  679. /* found - check max args */
  680. if (argc > cmdtp->maxargs) {
  681. printf ("Usage:\n%s\n", cmdtp->usage);
  682. return -1;
  683. }
  684. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  685. /* avoid "bootd" recursion */
  686. if (cmdtp->cmd == do_bootd) {
  687. #ifdef DEBUG_PARSER
  688. printf ("[%s]\n", finaltoken);
  689. #endif
  690. if (flag & CMD_FLAG_BOOTD) {
  691. printf ("'bootd' recursion detected\n");
  692. return -1;
  693. }
  694. else
  695. flag |= CMD_FLAG_BOOTD;
  696. }
  697. #endif /* CFG_CMD_BOOTD */
  698. /* OK - call function to do the command */
  699. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  700. return (-1);
  701. }
  702. repeatable &= cmdtp->repeatable;
  703. /* Did the user stop this? */
  704. if (had_ctrlc ())
  705. return 0; /* if stopped then not repeatable */
  706. }
  707. return repeatable;
  708. }
  709. /****************************************************************************/
  710. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  711. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  712. {
  713. int i;
  714. int rcode = 1;
  715. if (argc < 2) {
  716. printf ("Usage:\n%s\n", cmdtp->usage);
  717. return 1;
  718. }
  719. for (i=1; i<argc; ++i) {
  720. #ifndef CFG_HUSH_PARSER
  721. if (run_command (getenv (argv[i]), flag) != -1) ++rcode;
  722. #else
  723. if (parse_string_outer(getenv (argv[i]),
  724. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) == 0) ++rcode;
  725. #endif
  726. }
  727. return ((rcode == i) ? 0 : 1);
  728. }
  729. #endif