main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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 <malloc.h>
  28. #ifdef CFG_HUSH_PARSER
  29. #include <hush.h>
  30. #endif
  31. #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
  32. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
  33. #endif
  34. extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  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. #ifdef CONFIG_MENUKEY
  158. static int menukey = 0;
  159. #endif
  160. static __inline__ int abortboot(int bootdelay)
  161. {
  162. int abort = 0;
  163. #ifdef CONFIG_MENUPROMPT
  164. printf(CONFIG_MENUPROMPT, bootdelay);
  165. #else
  166. printf("Hit any key to stop autoboot: %2d ", bootdelay);
  167. #endif
  168. #if defined CONFIG_ZERO_BOOTDELAY_CHECK
  169. /*
  170. * Check if key already pressed
  171. * Don't check if bootdelay < 0
  172. */
  173. if (bootdelay >= 0) {
  174. if (tstc()) { /* we got a key press */
  175. (void) getc(); /* consume input */
  176. printf ("\b\b\b 0\n");
  177. return 1; /* don't auto boot */
  178. }
  179. }
  180. #endif
  181. while (bootdelay > 0) {
  182. int i;
  183. --bootdelay;
  184. /* delay 100 * 10ms */
  185. for (i=0; !abort && i<100; ++i) {
  186. if (tstc()) { /* we got a key press */
  187. abort = 1; /* don't auto boot */
  188. bootdelay = 0; /* no more delay */
  189. # ifdef CONFIG_MENUKEY
  190. menukey = getc();
  191. # else
  192. (void) getc(); /* consume input */
  193. # endif
  194. break;
  195. }
  196. udelay (10000);
  197. }
  198. printf ("\b\b\b%2d ", bootdelay);
  199. }
  200. putc ('\n');
  201. return abort;
  202. }
  203. # endif /* CONFIG_AUTOBOOT_KEYED */
  204. #endif /* CONFIG_BOOTDELAY >= 0 */
  205. /****************************************************************************/
  206. void main_loop (void)
  207. {
  208. #ifndef CFG_HUSH_PARSER
  209. static char lastcommand[CFG_CBSIZE] = { 0, };
  210. int len;
  211. int rc = 1;
  212. int flag;
  213. #endif
  214. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  215. char *s;
  216. int bootdelay;
  217. #endif
  218. #ifdef CONFIG_PREBOOT
  219. char *p;
  220. #endif
  221. #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
  222. ulong bmp = 0; /* default bitmap */
  223. extern int trab_vfd (ulong bitmap);
  224. #ifdef CONFIG_MODEM_SUPPORT
  225. if (do_mdm_init)
  226. bmp = 1; /* alternate bitmap */
  227. #endif
  228. trab_vfd (bmp);
  229. #endif /* CONFIG_VFD && VFD_TEST_LOGO */
  230. #ifdef CONFIG_MODEM_SUPPORT
  231. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  232. if (do_mdm_init) {
  233. uchar *str = strdup(getenv("mdm_cmd"));
  234. setenv ("preboot", str); /* set or delete definition */
  235. if (str != NULL)
  236. free (str);
  237. mdm_init(); /* wait for modem connection */
  238. }
  239. #endif /* CONFIG_MODEM_SUPPORT */
  240. #ifdef CONFIG_VERSION_VARIABLE
  241. {
  242. extern char version_string[];
  243. char *str = getenv("ver");
  244. if (!str)
  245. setenv ("ver", version_string); /* set version variable */
  246. }
  247. #endif /* CONFIG_VERSION_VARIABLE */
  248. #ifdef CFG_HUSH_PARSER
  249. u_boot_hush_start ();
  250. #endif
  251. #ifdef CONFIG_PREBOOT
  252. if ((p = getenv ("preboot")) != NULL) {
  253. # ifdef CONFIG_AUTOBOOT_KEYED
  254. int prev = disable_ctrlc(1); /* disable Control C checking */
  255. # endif
  256. # ifndef CFG_HUSH_PARSER
  257. run_command (p, 0);
  258. # else
  259. parse_string_outer(p, FLAG_PARSE_SEMICOLON |
  260. FLAG_EXIT_FROM_LOOP);
  261. # endif
  262. # ifdef CONFIG_AUTOBOOT_KEYED
  263. disable_ctrlc(prev); /* restore Control C checking */
  264. # endif
  265. }
  266. #endif /* CONFIG_PREBOOT */
  267. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  268. s = getenv ("bootdelay");
  269. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  270. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  271. # ifdef CONFIG_BOOT_RETRY_TIME
  272. init_cmd_timeout ();
  273. # endif /* CONFIG_BOOT_RETRY_TIME */
  274. s = getenv ("bootcmd");
  275. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  276. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  277. # ifdef CONFIG_AUTOBOOT_KEYED
  278. int prev = disable_ctrlc(1); /* disable Control C checking */
  279. # endif
  280. # ifndef CFG_HUSH_PARSER
  281. run_command (s, 0);
  282. # else
  283. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  284. FLAG_EXIT_FROM_LOOP);
  285. # endif
  286. # ifdef CONFIG_AUTOBOOT_KEYED
  287. disable_ctrlc(prev); /* restore Control C checking */
  288. # endif
  289. }
  290. # ifdef CONFIG_MENUKEY
  291. if (menukey == CONFIG_MENUKEY) {
  292. s = getenv("menucmd");
  293. if (s) {
  294. # ifndef CFG_HUSH_PARSER
  295. run_command (s, bd, 0);
  296. # else
  297. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  298. FLAG_EXIT_FROM_LOOP);
  299. # endif
  300. }
  301. }
  302. #endif /* CONFIG_MENUKEY */
  303. #endif /* CONFIG_BOOTDELAY */
  304. #ifdef CONFIG_AMIGAONEG3SE
  305. {
  306. extern void video_banner(void);
  307. video_banner();
  308. }
  309. #endif
  310. /*
  311. * Main Loop for Monitor Command Processing
  312. */
  313. #ifdef CFG_HUSH_PARSER
  314. parse_file_outer();
  315. /* This point is never reached */
  316. for (;;);
  317. #else
  318. for (;;) {
  319. #ifdef CONFIG_BOOT_RETRY_TIME
  320. if (rc >= 0) {
  321. /* Saw enough of a valid command to
  322. * restart the timeout.
  323. */
  324. reset_cmd_timeout();
  325. }
  326. #endif
  327. len = readline (CFG_PROMPT);
  328. flag = 0; /* assume no special flags for now */
  329. if (len > 0)
  330. strcpy (lastcommand, console_buffer);
  331. else if (len == 0)
  332. flag |= CMD_FLAG_REPEAT;
  333. #ifdef CONFIG_BOOT_RETRY_TIME
  334. else if (len == -2) {
  335. /* -2 means timed out, retry autoboot
  336. */
  337. printf("\nTimed out waiting for command\n");
  338. # ifdef CONFIG_RESET_TO_RETRY
  339. /* Reinit board to run initialization code again */
  340. do_reset (NULL, 0, 0, NULL);
  341. # else
  342. return; /* retry autoboot */
  343. # endif
  344. }
  345. #endif
  346. if (len == -1)
  347. printf ("<INTERRUPT>\n");
  348. else
  349. rc = run_command (lastcommand, flag);
  350. if (rc <= 0) {
  351. /* invalid command or not repeatable, forget it */
  352. lastcommand[0] = 0;
  353. }
  354. }
  355. #endif /*CFG_HUSH_PARSER*/
  356. }
  357. #ifdef CONFIG_BOOT_RETRY_TIME
  358. /***************************************************************************
  359. * initialise command line timeout
  360. */
  361. void init_cmd_timeout(void)
  362. {
  363. char *s = getenv ("bootretry");
  364. if (s != NULL)
  365. retry_time = (int)simple_strtoul(s, NULL, 10);
  366. else
  367. retry_time = CONFIG_BOOT_RETRY_TIME;
  368. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  369. retry_time = CONFIG_BOOT_RETRY_MIN;
  370. }
  371. /***************************************************************************
  372. * reset command line timeout to retry_time seconds
  373. */
  374. void reset_cmd_timeout(void)
  375. {
  376. endtime = endtick(retry_time);
  377. }
  378. #endif
  379. /****************************************************************************/
  380. /*
  381. * Prompt for input and read a line.
  382. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  383. * time out when time goes past endtime (timebase time in ticks).
  384. * Return: number of read characters
  385. * -1 if break
  386. * -2 if timed out
  387. */
  388. int readline (const char *const prompt)
  389. {
  390. char *p = console_buffer;
  391. int n = 0; /* buffer index */
  392. int plen = 0; /* prompt length */
  393. int col; /* output column cnt */
  394. char c;
  395. /* print prompt */
  396. if (prompt) {
  397. plen = strlen (prompt);
  398. puts (prompt);
  399. }
  400. col = plen;
  401. for (;;) {
  402. #ifdef CONFIG_BOOT_RETRY_TIME
  403. while (!tstc()) { /* while no incoming data */
  404. if (retry_time >= 0 && get_ticks() > endtime)
  405. return (-2); /* timed out */
  406. }
  407. #endif
  408. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  409. #ifdef CONFIG_SHOW_ACTIVITY
  410. while (!tstc()) {
  411. extern void show_activity(int arg);
  412. show_activity(0);
  413. }
  414. #endif
  415. c = getc();
  416. /*
  417. * Special character handling
  418. */
  419. switch (c) {
  420. case '\r': /* Enter */
  421. case '\n':
  422. *p = '\0';
  423. puts ("\r\n");
  424. return (p - console_buffer);
  425. case 0x03: /* ^C - break */
  426. console_buffer[0] = '\0'; /* discard input */
  427. return (-1);
  428. case 0x15: /* ^U - erase line */
  429. while (col > plen) {
  430. puts (erase_seq);
  431. --col;
  432. }
  433. p = console_buffer;
  434. n = 0;
  435. continue;
  436. case 0x17: /* ^W - erase word */
  437. p=delete_char(console_buffer, p, &col, &n, plen);
  438. while ((n > 0) && (*p != ' ')) {
  439. p=delete_char(console_buffer, p, &col, &n, plen);
  440. }
  441. continue;
  442. case 0x08: /* ^H - backspace */
  443. case 0x7F: /* DEL - backspace */
  444. p=delete_char(console_buffer, p, &col, &n, plen);
  445. continue;
  446. default:
  447. /*
  448. * Must be a normal character then
  449. */
  450. if (n < CFG_CBSIZE-2) {
  451. if (c == '\t') { /* expand TABs */
  452. puts (tab_seq+(col&07));
  453. col += 8 - (col&07);
  454. } else {
  455. ++col; /* echo input */
  456. putc (c);
  457. }
  458. *p++ = c;
  459. ++n;
  460. } else { /* Buffer full */
  461. putc ('\a');
  462. }
  463. }
  464. }
  465. }
  466. /****************************************************************************/
  467. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  468. {
  469. char *s;
  470. if (*np == 0) {
  471. return (p);
  472. }
  473. if (*(--p) == '\t') { /* will retype the whole line */
  474. while (*colp > plen) {
  475. puts (erase_seq);
  476. (*colp)--;
  477. }
  478. for (s=buffer; s<p; ++s) {
  479. if (*s == '\t') {
  480. puts (tab_seq+((*colp) & 07));
  481. *colp += 8 - ((*colp) & 07);
  482. } else {
  483. ++(*colp);
  484. putc (*s);
  485. }
  486. }
  487. } else {
  488. puts (erase_seq);
  489. (*colp)--;
  490. }
  491. (*np)--;
  492. return (p);
  493. }
  494. /****************************************************************************/
  495. int parse_line (char *line, char *argv[])
  496. {
  497. int nargs = 0;
  498. #ifdef DEBUG_PARSER
  499. printf ("parse_line: \"%s\"\n", line);
  500. #endif
  501. while (nargs < CFG_MAXARGS) {
  502. /* skip any white space */
  503. while ((*line == ' ') || (*line == '\t')) {
  504. ++line;
  505. }
  506. if (*line == '\0') { /* end of line, no more args */
  507. argv[nargs] = NULL;
  508. #ifdef DEBUG_PARSER
  509. printf ("parse_line: nargs=%d\n", nargs);
  510. #endif
  511. return (nargs);
  512. }
  513. argv[nargs++] = line; /* begin of argument string */
  514. /* find end of string */
  515. while (*line && (*line != ' ') && (*line != '\t')) {
  516. ++line;
  517. }
  518. if (*line == '\0') { /* end of line, no more args */
  519. argv[nargs] = NULL;
  520. #ifdef DEBUG_PARSER
  521. printf ("parse_line: nargs=%d\n", nargs);
  522. #endif
  523. return (nargs);
  524. }
  525. *line++ = '\0'; /* terminate current arg */
  526. }
  527. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  528. #ifdef DEBUG_PARSER
  529. printf ("parse_line: nargs=%d\n", nargs);
  530. #endif
  531. return (nargs);
  532. }
  533. /****************************************************************************/
  534. static void process_macros (const char *input, char *output)
  535. {
  536. char c, prev;
  537. const char *varname_start = NULL;
  538. int inputcnt = strlen (input);
  539. int outputcnt = CFG_CBSIZE;
  540. int state = 0; /* 0 = waiting for '$' */
  541. /* 1 = waiting for '(' */
  542. /* 2 = waiting for ')' */
  543. /* 3 = waiting for ''' */
  544. #ifdef DEBUG_PARSER
  545. char *output_start = output;
  546. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  547. #endif
  548. prev = '\0'; /* previous character */
  549. while (inputcnt && outputcnt) {
  550. c = *input++;
  551. inputcnt--;
  552. if (state!=3) {
  553. /* remove one level of escape characters */
  554. if ((c == '\\') && (prev != '\\')) {
  555. if (inputcnt-- == 0)
  556. break;
  557. prev = c;
  558. c = *input++;
  559. }
  560. }
  561. switch (state) {
  562. case 0: /* Waiting for (unescaped) $ */
  563. if ((c == '\'') && (prev != '\\')) {
  564. state = 3;
  565. break;
  566. }
  567. if ((c == '$') && (prev != '\\')) {
  568. state++;
  569. } else {
  570. *(output++) = c;
  571. outputcnt--;
  572. }
  573. break;
  574. case 1: /* Waiting for ( */
  575. if (c == '(') {
  576. state++;
  577. varname_start = input;
  578. } else {
  579. state = 0;
  580. *(output++) = '$';
  581. outputcnt--;
  582. if (outputcnt) {
  583. *(output++) = c;
  584. outputcnt--;
  585. }
  586. }
  587. break;
  588. case 2: /* Waiting for ) */
  589. if (c == ')') {
  590. int i;
  591. char envname[CFG_CBSIZE], *envval;
  592. int envcnt = input-varname_start-1; /* Varname # of chars */
  593. /* Get the varname */
  594. for (i = 0; i < envcnt; i++) {
  595. envname[i] = varname_start[i];
  596. }
  597. envname[i] = 0;
  598. /* Get its value */
  599. envval = getenv (envname);
  600. /* Copy into the line if it exists */
  601. if (envval != NULL)
  602. while ((*envval) && outputcnt) {
  603. *(output++) = *(envval++);
  604. outputcnt--;
  605. }
  606. /* Look for another '$' */
  607. state = 0;
  608. }
  609. break;
  610. case 3: /* Waiting for ' */
  611. if ((c == '\'') && (prev != '\\')) {
  612. state = 0;
  613. } else {
  614. *(output++) = c;
  615. outputcnt--;
  616. }
  617. break;
  618. }
  619. prev = c;
  620. }
  621. if (outputcnt)
  622. *output = 0;
  623. #ifdef DEBUG_PARSER
  624. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  625. strlen(output_start), output_start);
  626. #endif
  627. }
  628. /****************************************************************************
  629. * returns:
  630. * 1 - command executed, repeatable
  631. * 0 - command executed but not repeatable, interrupted commands are
  632. * always considered not repeatable
  633. * -1 - not executed (unrecognized, bootd recursion or too many args)
  634. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  635. * considered unrecognized)
  636. *
  637. * WARNING:
  638. *
  639. * We must create a temporary copy of the command since the command we get
  640. * may be the result from getenv(), which returns a pointer directly to
  641. * the environment data, which may change magicly when the command we run
  642. * creates or modifies environment variables (like "bootp" does).
  643. */
  644. int run_command (const char *cmd, int flag)
  645. {
  646. cmd_tbl_t *cmdtp;
  647. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  648. char *token; /* start of token in cmdbuf */
  649. char *sep; /* end of token (separator) in cmdbuf */
  650. char finaltoken[CFG_CBSIZE];
  651. char *str = cmdbuf;
  652. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  653. int argc, inquotes;
  654. int repeatable = 1;
  655. int rc = 0;
  656. #ifdef DEBUG_PARSER
  657. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  658. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  659. puts ("\"\n");
  660. #endif
  661. clear_ctrlc(); /* forget any previous Control C */
  662. if (!cmd || !*cmd) {
  663. return -1; /* empty command */
  664. }
  665. if (strlen(cmd) >= CFG_CBSIZE) {
  666. puts ("## Command too long!\n");
  667. return -1;
  668. }
  669. strcpy (cmdbuf, cmd);
  670. /* Process separators and check for invalid
  671. * repeatable commands
  672. */
  673. #ifdef DEBUG_PARSER
  674. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  675. #endif
  676. while (*str) {
  677. /*
  678. * Find separator, or string end
  679. * Allow simple escape of ';' by writing "\;"
  680. */
  681. for (inquotes = 0, sep = str; *sep; sep++) {
  682. if ((*sep=='\'') &&
  683. (*(sep-1) != '\\'))
  684. inquotes=!inquotes;
  685. if (!inquotes &&
  686. (*sep == ';') && /* separator */
  687. ( sep != str) && /* past string start */
  688. (*(sep-1) != '\\')) /* and NOT escaped */
  689. break;
  690. }
  691. /*
  692. * Limit the token to data between separators
  693. */
  694. token = str;
  695. if (*sep) {
  696. str = sep + 1; /* start of command for next pass */
  697. *sep = '\0';
  698. }
  699. else
  700. str = sep; /* no more commands for next pass */
  701. #ifdef DEBUG_PARSER
  702. printf ("token: \"%s\"\n", token);
  703. #endif
  704. /* find macros in this token and replace them */
  705. process_macros (token, finaltoken);
  706. /* Extract arguments */
  707. argc = parse_line (finaltoken, argv);
  708. /* Look up command in command table */
  709. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  710. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  711. rc = -1; /* give up after bad command */
  712. continue;
  713. }
  714. /* found - check max args */
  715. if (argc > cmdtp->maxargs) {
  716. printf ("Usage:\n%s\n", cmdtp->usage);
  717. rc = -1;
  718. continue;
  719. }
  720. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  721. /* avoid "bootd" recursion */
  722. if (cmdtp->cmd == do_bootd) {
  723. #ifdef DEBUG_PARSER
  724. printf ("[%s]\n", finaltoken);
  725. #endif
  726. if (flag & CMD_FLAG_BOOTD) {
  727. printf ("'bootd' recursion detected\n");
  728. rc = -1;
  729. continue;
  730. }
  731. else
  732. flag |= CMD_FLAG_BOOTD;
  733. }
  734. #endif /* CFG_CMD_BOOTD */
  735. /* OK - call function to do the command */
  736. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  737. rc = -1;
  738. }
  739. repeatable &= cmdtp->repeatable;
  740. /* Did the user stop this? */
  741. if (had_ctrlc ())
  742. return 0; /* if stopped then not repeatable */
  743. }
  744. return rc ? rc : repeatable;
  745. }
  746. /****************************************************************************/
  747. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  748. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  749. {
  750. int i;
  751. if (argc < 2) {
  752. printf ("Usage:\n%s\n", cmdtp->usage);
  753. return 1;
  754. }
  755. for (i=1; i<argc; ++i) {
  756. char *arg;
  757. if ((arg = getenv (argv[i])) == NULL) {
  758. printf ("## Error: \"%s\" not defined\n", argv[i]);
  759. return 1;
  760. }
  761. #ifndef CFG_HUSH_PARSER
  762. if (run_command (arg, flag) == -1)
  763. return 1;
  764. #else
  765. if (parse_string_outer(arg,
  766. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  767. return 1;
  768. #endif
  769. }
  770. return 0;
  771. }
  772. #endif /* CFG_CMD_RUN */