main.c 22 KB

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