main.c 21 KB

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