main.c 22 KB

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