main.c 20 KB

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