main.c 19 KB

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