main.c 21 KB

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