main.c 20 KB

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