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