main.c 22 KB

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