main.c 22 KB

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