main.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. * (C) Copyright 2000
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * Add to readline cmdline-editing by
  6. * (C) Copyright 2005
  7. * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. /* #define DEBUG */
  28. #include <common.h>
  29. #include <watchdog.h>
  30. #include <command.h>
  31. #ifdef CONFIG_MODEM_SUPPORT
  32. #include <malloc.h> /* for free() prototype */
  33. #endif
  34. #ifdef CFG_HUSH_PARSER
  35. #include <hush.h>
  36. #endif
  37. #include <post.h>
  38. #ifdef CONFIG_SILENT_CONSOLE
  39. DECLARE_GLOBAL_DATA_PTR;
  40. #endif
  41. #if defined(CONFIG_BOOT_RETRY_TIME) && defined(CONFIG_RESET_TO_RETRY)
  42. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /* for do_reset() prototype */
  43. #endif
  44. extern int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  45. #define MAX_DELAY_STOP_STR 32
  46. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  47. static int parse_line (char *, char *[]);
  48. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  49. static int abortboot(int);
  50. #endif
  51. #undef DEBUG_PARSER
  52. char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
  53. static char erase_seq[] = "\b \b"; /* erase sequence */
  54. static char tab_seq[] = " "; /* used to expand TABs */
  55. #ifdef CONFIG_BOOT_RETRY_TIME
  56. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  57. static int retry_time = -1; /* -1 so can call readline before main_loop */
  58. #endif
  59. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  60. #ifndef CONFIG_BOOT_RETRY_MIN
  61. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  62. #endif
  63. #ifdef CONFIG_MODEM_SUPPORT
  64. int do_mdm_init = 0;
  65. extern void mdm_init(void); /* defined in board.c */
  66. #endif
  67. /***************************************************************************
  68. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  69. * returns: 0 - no key string, allow autoboot
  70. * 1 - got key string, abort
  71. */
  72. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  73. # if defined(CONFIG_AUTOBOOT_KEYED)
  74. static __inline__ int abortboot(int bootdelay)
  75. {
  76. int abort = 0;
  77. uint64_t etime = endtick(bootdelay);
  78. struct
  79. {
  80. char* str;
  81. u_int len;
  82. int retry;
  83. }
  84. delaykey [] =
  85. {
  86. { str: getenv ("bootdelaykey"), retry: 1 },
  87. { str: getenv ("bootdelaykey2"), retry: 1 },
  88. { str: getenv ("bootstopkey"), retry: 0 },
  89. { str: getenv ("bootstopkey2"), retry: 0 },
  90. };
  91. char presskey [MAX_DELAY_STOP_STR];
  92. u_int presskey_len = 0;
  93. u_int presskey_max = 0;
  94. u_int i;
  95. #ifdef CONFIG_SILENT_CONSOLE
  96. if (gd->flags & GD_FLG_SILENT) {
  97. /* Restore serial console */
  98. console_assign (stdout, "serial");
  99. console_assign (stderr, "serial");
  100. }
  101. #endif
  102. # ifdef CONFIG_AUTOBOOT_PROMPT
  103. printf (CONFIG_AUTOBOOT_PROMPT, bootdelay);
  104. # endif
  105. # ifdef CONFIG_AUTOBOOT_DELAY_STR
  106. if (delaykey[0].str == NULL)
  107. delaykey[0].str = CONFIG_AUTOBOOT_DELAY_STR;
  108. # endif
  109. # ifdef CONFIG_AUTOBOOT_DELAY_STR2
  110. if (delaykey[1].str == NULL)
  111. delaykey[1].str = CONFIG_AUTOBOOT_DELAY_STR2;
  112. # endif
  113. # ifdef CONFIG_AUTOBOOT_STOP_STR
  114. if (delaykey[2].str == NULL)
  115. delaykey[2].str = CONFIG_AUTOBOOT_STOP_STR;
  116. # endif
  117. # ifdef CONFIG_AUTOBOOT_STOP_STR2
  118. if (delaykey[3].str == NULL)
  119. delaykey[3].str = CONFIG_AUTOBOOT_STOP_STR2;
  120. # endif
  121. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  122. delaykey[i].len = delaykey[i].str == NULL ?
  123. 0 : strlen (delaykey[i].str);
  124. delaykey[i].len = delaykey[i].len > MAX_DELAY_STOP_STR ?
  125. MAX_DELAY_STOP_STR : delaykey[i].len;
  126. presskey_max = presskey_max > delaykey[i].len ?
  127. presskey_max : delaykey[i].len;
  128. # if DEBUG_BOOTKEYS
  129. printf("%s key:<%s>\n",
  130. delaykey[i].retry ? "delay" : "stop",
  131. delaykey[i].str ? delaykey[i].str : "NULL");
  132. # endif
  133. }
  134. /* In order to keep up with incoming data, check timeout only
  135. * when catch up.
  136. */
  137. while (!abort && get_ticks() <= etime) {
  138. for (i = 0; i < sizeof(delaykey) / sizeof(delaykey[0]); i ++) {
  139. if (delaykey[i].len > 0 &&
  140. presskey_len >= delaykey[i].len &&
  141. memcmp (presskey + presskey_len - delaykey[i].len,
  142. delaykey[i].str,
  143. delaykey[i].len) == 0) {
  144. # if DEBUG_BOOTKEYS
  145. printf("got %skey\n",
  146. delaykey[i].retry ? "delay" : "stop");
  147. # endif
  148. # ifdef CONFIG_BOOT_RETRY_TIME
  149. /* don't retry auto boot */
  150. if (! delaykey[i].retry)
  151. retry_time = -1;
  152. # endif
  153. abort = 1;
  154. }
  155. }
  156. if (tstc()) {
  157. if (presskey_len < presskey_max) {
  158. presskey [presskey_len ++] = getc();
  159. }
  160. else {
  161. for (i = 0; i < presskey_max - 1; i ++)
  162. presskey [i] = presskey [i + 1];
  163. presskey [i] = getc();
  164. }
  165. }
  166. }
  167. # if DEBUG_BOOTKEYS
  168. if (!abort)
  169. puts ("key timeout\n");
  170. # endif
  171. #ifdef CONFIG_SILENT_CONSOLE
  172. if (abort) {
  173. /* permanently enable normal console output */
  174. gd->flags &= ~(GD_FLG_SILENT);
  175. } else if (gd->flags & GD_FLG_SILENT) {
  176. /* Restore silent console */
  177. console_assign (stdout, "nulldev");
  178. console_assign (stderr, "nulldev");
  179. }
  180. #endif
  181. return abort;
  182. }
  183. # else /* !defined(CONFIG_AUTOBOOT_KEYED) */
  184. #ifdef CONFIG_MENUKEY
  185. static int menukey = 0;
  186. #endif
  187. static __inline__ int abortboot(int bootdelay)
  188. {
  189. int abort = 0;
  190. #ifdef CONFIG_SILENT_CONSOLE
  191. if (gd->flags & GD_FLG_SILENT) {
  192. /* Restore serial console */
  193. console_assign (stdout, "serial");
  194. console_assign (stderr, "serial");
  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. if (abort) {
  237. /* permanently enable normal console output */
  238. gd->flags &= ~(GD_FLG_SILENT);
  239. } else if (gd->flags & GD_FLG_SILENT) {
  240. /* Restore silent console */
  241. console_assign (stdout, "nulldev");
  242. console_assign (stderr, "nulldev");
  243. }
  244. #endif
  245. return abort;
  246. }
  247. # endif /* CONFIG_AUTOBOOT_KEYED */
  248. #endif /* CONFIG_BOOTDELAY >= 0 */
  249. /****************************************************************************/
  250. void main_loop (void)
  251. {
  252. #ifndef CFG_HUSH_PARSER
  253. static char lastcommand[CFG_CBSIZE] = { 0, };
  254. int len;
  255. int rc = 1;
  256. int flag;
  257. #endif
  258. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  259. char *s;
  260. int bootdelay;
  261. #endif
  262. #ifdef CONFIG_PREBOOT
  263. char *p;
  264. #endif
  265. #ifdef CONFIG_BOOTCOUNT_LIMIT
  266. unsigned long bootcount = 0;
  267. unsigned long bootlimit = 0;
  268. char *bcs;
  269. char bcs_set[16];
  270. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  271. #if defined(CONFIG_VFD) && defined(VFD_TEST_LOGO)
  272. ulong bmp = 0; /* default bitmap */
  273. extern int trab_vfd (ulong bitmap);
  274. #ifdef CONFIG_MODEM_SUPPORT
  275. if (do_mdm_init)
  276. bmp = 1; /* alternate bitmap */
  277. #endif
  278. trab_vfd (bmp);
  279. #endif /* CONFIG_VFD && VFD_TEST_LOGO */
  280. #ifdef CONFIG_BOOTCOUNT_LIMIT
  281. bootcount = bootcount_load();
  282. bootcount++;
  283. bootcount_store (bootcount);
  284. sprintf (bcs_set, "%lu", bootcount);
  285. setenv ("bootcount", bcs_set);
  286. bcs = getenv ("bootlimit");
  287. bootlimit = bcs ? simple_strtoul (bcs, NULL, 10) : 0;
  288. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  289. #ifdef CONFIG_MODEM_SUPPORT
  290. debug ("DEBUG: main_loop: do_mdm_init=%d\n", do_mdm_init);
  291. if (do_mdm_init) {
  292. char *str = strdup(getenv("mdm_cmd"));
  293. setenv ("preboot", str); /* set or delete definition */
  294. if (str != NULL)
  295. free (str);
  296. mdm_init(); /* wait for modem connection */
  297. }
  298. #endif /* CONFIG_MODEM_SUPPORT */
  299. #ifdef CONFIG_VERSION_VARIABLE
  300. {
  301. extern char version_string[];
  302. setenv ("ver", version_string); /* set version variable */
  303. }
  304. #endif /* CONFIG_VERSION_VARIABLE */
  305. #ifdef CFG_HUSH_PARSER
  306. u_boot_hush_start ();
  307. #endif
  308. #ifdef CONFIG_AUTO_COMPLETE
  309. install_auto_complete();
  310. #endif
  311. #ifdef CONFIG_PREBOOT
  312. if ((p = getenv ("preboot")) != NULL) {
  313. # ifdef CONFIG_AUTOBOOT_KEYED
  314. int prev = disable_ctrlc(1); /* disable Control C checking */
  315. # endif
  316. # ifndef CFG_HUSH_PARSER
  317. run_command (p, 0);
  318. # else
  319. parse_string_outer(p, FLAG_PARSE_SEMICOLON |
  320. FLAG_EXIT_FROM_LOOP);
  321. # endif
  322. # ifdef CONFIG_AUTOBOOT_KEYED
  323. disable_ctrlc(prev); /* restore Control C checking */
  324. # endif
  325. }
  326. #endif /* CONFIG_PREBOOT */
  327. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  328. s = getenv ("bootdelay");
  329. bootdelay = s ? (int)simple_strtol(s, NULL, 10) : CONFIG_BOOTDELAY;
  330. debug ("### main_loop entered: bootdelay=%d\n\n", bootdelay);
  331. # ifdef CONFIG_BOOT_RETRY_TIME
  332. init_cmd_timeout ();
  333. # endif /* CONFIG_BOOT_RETRY_TIME */
  334. #ifdef CONFIG_BOOTCOUNT_LIMIT
  335. if (bootlimit && (bootcount > bootlimit)) {
  336. printf ("Warning: Bootlimit (%u) exceeded. Using altbootcmd.\n",
  337. (unsigned)bootlimit);
  338. s = getenv ("altbootcmd");
  339. }
  340. else
  341. #endif /* CONFIG_BOOTCOUNT_LIMIT */
  342. s = getenv ("bootcmd");
  343. debug ("### main_loop: bootcmd=\"%s\"\n", s ? s : "<UNDEFINED>");
  344. if (bootdelay >= 0 && s && !abortboot (bootdelay)) {
  345. # ifdef CONFIG_AUTOBOOT_KEYED
  346. int prev = disable_ctrlc(1); /* disable Control C checking */
  347. # endif
  348. # ifndef CFG_HUSH_PARSER
  349. run_command (s, 0);
  350. # else
  351. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  352. FLAG_EXIT_FROM_LOOP);
  353. # endif
  354. # ifdef CONFIG_AUTOBOOT_KEYED
  355. disable_ctrlc(prev); /* restore Control C checking */
  356. # endif
  357. }
  358. # ifdef CONFIG_MENUKEY
  359. if (menukey == CONFIG_MENUKEY) {
  360. s = getenv("menucmd");
  361. if (s) {
  362. # ifndef CFG_HUSH_PARSER
  363. run_command (s, 0);
  364. # else
  365. parse_string_outer(s, FLAG_PARSE_SEMICOLON |
  366. FLAG_EXIT_FROM_LOOP);
  367. # endif
  368. }
  369. }
  370. #endif /* CONFIG_MENUKEY */
  371. #endif /* CONFIG_BOOTDELAY */
  372. #ifdef CONFIG_AMIGAONEG3SE
  373. {
  374. extern void video_banner(void);
  375. video_banner();
  376. }
  377. #endif
  378. /*
  379. * Main Loop for Monitor Command Processing
  380. */
  381. #ifdef CFG_HUSH_PARSER
  382. parse_file_outer();
  383. /* This point is never reached */
  384. for (;;);
  385. #else
  386. for (;;) {
  387. #ifdef CONFIG_BOOT_RETRY_TIME
  388. if (rc >= 0) {
  389. /* Saw enough of a valid command to
  390. * restart the timeout.
  391. */
  392. reset_cmd_timeout();
  393. }
  394. #endif
  395. len = readline (CFG_PROMPT);
  396. flag = 0; /* assume no special flags for now */
  397. if (len > 0)
  398. strcpy (lastcommand, console_buffer);
  399. else if (len == 0)
  400. flag |= CMD_FLAG_REPEAT;
  401. #ifdef CONFIG_BOOT_RETRY_TIME
  402. else if (len == -2) {
  403. /* -2 means timed out, retry autoboot
  404. */
  405. puts ("\nTimed out waiting for command\n");
  406. # ifdef CONFIG_RESET_TO_RETRY
  407. /* Reinit board to run initialization code again */
  408. do_reset (NULL, 0, 0, NULL);
  409. # else
  410. return; /* retry autoboot */
  411. # endif
  412. }
  413. #endif
  414. if (len == -1)
  415. puts ("<INTERRUPT>\n");
  416. else
  417. rc = run_command (lastcommand, flag);
  418. if (rc <= 0) {
  419. /* invalid command or not repeatable, forget it */
  420. lastcommand[0] = 0;
  421. }
  422. }
  423. #endif /*CFG_HUSH_PARSER*/
  424. }
  425. #ifdef CONFIG_BOOT_RETRY_TIME
  426. /***************************************************************************
  427. * initialise command line timeout
  428. */
  429. void init_cmd_timeout(void)
  430. {
  431. char *s = getenv ("bootretry");
  432. if (s != NULL)
  433. retry_time = (int)simple_strtol(s, NULL, 10);
  434. else
  435. retry_time = CONFIG_BOOT_RETRY_TIME;
  436. if (retry_time >= 0 && retry_time < CONFIG_BOOT_RETRY_MIN)
  437. retry_time = CONFIG_BOOT_RETRY_MIN;
  438. }
  439. /***************************************************************************
  440. * reset command line timeout to retry_time seconds
  441. */
  442. void reset_cmd_timeout(void)
  443. {
  444. endtime = endtick(retry_time);
  445. }
  446. #endif
  447. #ifdef CONFIG_CMDLINE_EDITING
  448. /*
  449. * cmdline-editing related codes from vivi.
  450. * Author: Janghoon Lyu <nandy@mizi.com>
  451. */
  452. #if 1 /* avoid redundand code -- wd */
  453. #define putnstr(str,n) do { \
  454. printf ("%.*s", n, str); \
  455. } while (0)
  456. #else
  457. void putnstr(const char *str, size_t n)
  458. {
  459. if (str == NULL)
  460. return;
  461. while (n && *str != '\0') {
  462. putc(*str);
  463. str++;
  464. n--;
  465. }
  466. }
  467. #endif
  468. #define CTL_CH(c) ((c) - 'a' + 1)
  469. #define MAX_CMDBUF_SIZE 256
  470. #define CTL_BACKSPACE ('\b')
  471. #define DEL ((char)255)
  472. #define DEL7 ((char)127)
  473. #define CREAD_HIST_CHAR ('!')
  474. #define getcmd_putch(ch) putc(ch)
  475. #define getcmd_getch() getc()
  476. #define getcmd_cbeep() getcmd_putch('\a')
  477. #define HIST_MAX 20
  478. #define HIST_SIZE MAX_CMDBUF_SIZE
  479. static int hist_max = 0;
  480. static int hist_add_idx = 0;
  481. static int hist_cur = -1;
  482. unsigned hist_num = 0;
  483. char* hist_list[HIST_MAX];
  484. char hist_lines[HIST_MAX][HIST_SIZE];
  485. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  486. static void hist_init(void)
  487. {
  488. int i;
  489. hist_max = 0;
  490. hist_add_idx = 0;
  491. hist_cur = -1;
  492. hist_num = 0;
  493. for (i = 0; i < HIST_MAX; i++) {
  494. hist_list[i] = hist_lines[i];
  495. hist_list[i][0] = '\0';
  496. }
  497. }
  498. static void cread_add_to_hist(char *line)
  499. {
  500. strcpy(hist_list[hist_add_idx], line);
  501. if (++hist_add_idx >= HIST_MAX)
  502. hist_add_idx = 0;
  503. if (hist_add_idx > hist_max)
  504. hist_max = hist_add_idx;
  505. hist_num++;
  506. }
  507. static char* hist_prev(void)
  508. {
  509. char *ret;
  510. int old_cur;
  511. if (hist_cur < 0)
  512. return NULL;
  513. old_cur = hist_cur;
  514. if (--hist_cur < 0)
  515. hist_cur = hist_max;
  516. if (hist_cur == hist_add_idx) {
  517. hist_cur = old_cur;
  518. ret = NULL;
  519. } else
  520. ret = hist_list[hist_cur];
  521. return (ret);
  522. }
  523. static char* hist_next(void)
  524. {
  525. char *ret;
  526. if (hist_cur < 0)
  527. return NULL;
  528. if (hist_cur == hist_add_idx)
  529. return NULL;
  530. if (++hist_cur > hist_max)
  531. hist_cur = 0;
  532. if (hist_cur == hist_add_idx) {
  533. ret = "";
  534. } else
  535. ret = hist_list[hist_cur];
  536. return (ret);
  537. }
  538. static void cread_print_hist_list(void)
  539. {
  540. int i;
  541. unsigned long n;
  542. n = hist_num - hist_max;
  543. i = hist_add_idx + 1;
  544. while (1) {
  545. if (i > hist_max)
  546. i = 0;
  547. if (i == hist_add_idx)
  548. break;
  549. printf("%s\n", hist_list[i]);
  550. n++;
  551. i++;
  552. }
  553. }
  554. #define BEGINNING_OF_LINE() { \
  555. while (num) { \
  556. getcmd_putch(CTL_BACKSPACE); \
  557. num--; \
  558. } \
  559. }
  560. #define ERASE_TO_EOL() { \
  561. if (num < eol_num) { \
  562. int tmp; \
  563. for (tmp = num; tmp < eol_num; tmp++) \
  564. getcmd_putch(' '); \
  565. while (tmp-- > num) \
  566. getcmd_putch(CTL_BACKSPACE); \
  567. eol_num = num; \
  568. } \
  569. }
  570. #define REFRESH_TO_EOL() { \
  571. if (num < eol_num) { \
  572. wlen = eol_num - num; \
  573. putnstr(buf + num, wlen); \
  574. num = eol_num; \
  575. } \
  576. }
  577. static void cread_add_char(char ichar, int insert, unsigned long *num,
  578. unsigned long *eol_num, char *buf, unsigned long len)
  579. {
  580. unsigned long wlen;
  581. /* room ??? */
  582. if (insert || *num == *eol_num) {
  583. if (*eol_num > len - 1) {
  584. getcmd_cbeep();
  585. return;
  586. }
  587. (*eol_num)++;
  588. }
  589. if (insert) {
  590. wlen = *eol_num - *num;
  591. if (wlen > 1) {
  592. memmove(&buf[*num+1], &buf[*num], wlen-1);
  593. }
  594. buf[*num] = ichar;
  595. putnstr(buf + *num, wlen);
  596. (*num)++;
  597. while (--wlen) {
  598. getcmd_putch(CTL_BACKSPACE);
  599. }
  600. } else {
  601. /* echo the character */
  602. wlen = 1;
  603. buf[*num] = ichar;
  604. putnstr(buf + *num, wlen);
  605. (*num)++;
  606. }
  607. }
  608. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  609. unsigned long *eol_num, char *buf, unsigned long len)
  610. {
  611. while (strsize--) {
  612. cread_add_char(*str, insert, num, eol_num, buf, len);
  613. str++;
  614. }
  615. }
  616. static int cread_line(char *buf, unsigned int *len)
  617. {
  618. unsigned long num = 0;
  619. unsigned long eol_num = 0;
  620. unsigned long rlen;
  621. unsigned long wlen;
  622. char ichar;
  623. int insert = 1;
  624. int esc_len = 0;
  625. int rc = 0;
  626. char esc_save[8];
  627. while (1) {
  628. rlen = 1;
  629. ichar = getcmd_getch();
  630. if ((ichar == '\n') || (ichar == '\r')) {
  631. putc('\n');
  632. break;
  633. }
  634. /*
  635. * handle standard linux xterm esc sequences for arrow key, etc.
  636. */
  637. if (esc_len != 0) {
  638. if (esc_len == 1) {
  639. if (ichar == '[') {
  640. esc_save[esc_len] = ichar;
  641. esc_len = 2;
  642. } else {
  643. cread_add_str(esc_save, esc_len, insert,
  644. &num, &eol_num, buf, *len);
  645. esc_len = 0;
  646. }
  647. continue;
  648. }
  649. switch (ichar) {
  650. case 'D': /* <- key */
  651. ichar = CTL_CH('b');
  652. esc_len = 0;
  653. break;
  654. case 'C': /* -> key */
  655. ichar = CTL_CH('f');
  656. esc_len = 0;
  657. break; /* pass off to ^F handler */
  658. case 'H': /* Home key */
  659. ichar = CTL_CH('a');
  660. esc_len = 0;
  661. break; /* pass off to ^A handler */
  662. case 'A': /* up arrow */
  663. ichar = CTL_CH('p');
  664. esc_len = 0;
  665. break; /* pass off to ^P handler */
  666. case 'B': /* down arrow */
  667. ichar = CTL_CH('n');
  668. esc_len = 0;
  669. break; /* pass off to ^N handler */
  670. default:
  671. esc_save[esc_len++] = ichar;
  672. cread_add_str(esc_save, esc_len, insert,
  673. &num, &eol_num, buf, *len);
  674. esc_len = 0;
  675. continue;
  676. }
  677. }
  678. switch (ichar) {
  679. case 0x1b:
  680. if (esc_len == 0) {
  681. esc_save[esc_len] = ichar;
  682. esc_len = 1;
  683. } else {
  684. puts("impossible condition #876\n");
  685. esc_len = 0;
  686. }
  687. break;
  688. case CTL_CH('a'):
  689. BEGINNING_OF_LINE();
  690. break;
  691. case CTL_CH('c'): /* ^C - break */
  692. *buf = '\0'; /* discard input */
  693. return (-1);
  694. case CTL_CH('f'):
  695. if (num < eol_num) {
  696. getcmd_putch(buf[num]);
  697. num++;
  698. }
  699. break;
  700. case CTL_CH('b'):
  701. if (num) {
  702. getcmd_putch(CTL_BACKSPACE);
  703. num--;
  704. }
  705. break;
  706. case CTL_CH('d'):
  707. if (num < eol_num) {
  708. wlen = eol_num - num - 1;
  709. if (wlen) {
  710. memmove(&buf[num], &buf[num+1], wlen);
  711. putnstr(buf + num, wlen);
  712. }
  713. getcmd_putch(' ');
  714. do {
  715. getcmd_putch(CTL_BACKSPACE);
  716. } while (wlen--);
  717. eol_num--;
  718. }
  719. break;
  720. case CTL_CH('k'):
  721. ERASE_TO_EOL();
  722. break;
  723. case CTL_CH('e'):
  724. REFRESH_TO_EOL();
  725. break;
  726. case CTL_CH('o'):
  727. insert = !insert;
  728. break;
  729. case CTL_CH('x'):
  730. BEGINNING_OF_LINE();
  731. ERASE_TO_EOL();
  732. break;
  733. case DEL:
  734. case DEL7:
  735. case 8:
  736. if (num) {
  737. wlen = eol_num - num;
  738. num--;
  739. memmove(&buf[num], &buf[num+1], wlen);
  740. getcmd_putch(CTL_BACKSPACE);
  741. putnstr(buf + num, wlen);
  742. getcmd_putch(' ');
  743. do {
  744. getcmd_putch(CTL_BACKSPACE);
  745. } while (wlen--);
  746. eol_num--;
  747. }
  748. break;
  749. case CTL_CH('p'):
  750. case CTL_CH('n'):
  751. {
  752. char * hline;
  753. esc_len = 0;
  754. if (ichar == CTL_CH('p'))
  755. hline = hist_prev();
  756. else
  757. hline = hist_next();
  758. if (!hline) {
  759. getcmd_cbeep();
  760. continue;
  761. }
  762. /* nuke the current line */
  763. /* first, go home */
  764. BEGINNING_OF_LINE();
  765. /* erase to end of line */
  766. ERASE_TO_EOL();
  767. /* copy new line into place and display */
  768. strcpy(buf, hline);
  769. eol_num = strlen(buf);
  770. REFRESH_TO_EOL();
  771. continue;
  772. }
  773. default:
  774. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  775. break;
  776. }
  777. }
  778. *len = eol_num;
  779. buf[eol_num] = '\0'; /* lose the newline */
  780. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  781. cread_add_to_hist(buf);
  782. hist_cur = hist_add_idx;
  783. return (rc);
  784. }
  785. #endif /* CONFIG_CMDLINE_EDITING */
  786. /****************************************************************************/
  787. /*
  788. * Prompt for input and read a line.
  789. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  790. * time out when time goes past endtime (timebase time in ticks).
  791. * Return: number of read characters
  792. * -1 if break
  793. * -2 if timed out
  794. */
  795. int readline (const char *const prompt)
  796. {
  797. #ifdef CONFIG_CMDLINE_EDITING
  798. char *p = console_buffer;
  799. unsigned int len=MAX_CMDBUF_SIZE;
  800. static int initted = 0;
  801. if (!initted) {
  802. hist_init();
  803. initted = 1;
  804. }
  805. puts (prompt);
  806. return cread_line(p, &len);
  807. #else
  808. char *p = console_buffer;
  809. int n = 0; /* buffer index */
  810. int plen = 0; /* prompt length */
  811. int col; /* output column cnt */
  812. char c;
  813. /* print prompt */
  814. if (prompt) {
  815. plen = strlen (prompt);
  816. puts (prompt);
  817. }
  818. col = plen;
  819. for (;;) {
  820. #ifdef CONFIG_BOOT_RETRY_TIME
  821. while (!tstc()) { /* while no incoming data */
  822. if (retry_time >= 0 && get_ticks() > endtime)
  823. return (-2); /* timed out */
  824. }
  825. #endif
  826. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  827. #ifdef CONFIG_SHOW_ACTIVITY
  828. while (!tstc()) {
  829. extern void show_activity(int arg);
  830. show_activity(0);
  831. }
  832. #endif
  833. c = getc();
  834. /*
  835. * Special character handling
  836. */
  837. switch (c) {
  838. case '\r': /* Enter */
  839. case '\n':
  840. *p = '\0';
  841. puts ("\r\n");
  842. return (p - console_buffer);
  843. case '\0': /* nul */
  844. continue;
  845. case 0x03: /* ^C - break */
  846. console_buffer[0] = '\0'; /* discard input */
  847. return (-1);
  848. case 0x15: /* ^U - erase line */
  849. while (col > plen) {
  850. puts (erase_seq);
  851. --col;
  852. }
  853. p = console_buffer;
  854. n = 0;
  855. continue;
  856. case 0x17: /* ^W - erase word */
  857. p=delete_char(console_buffer, p, &col, &n, plen);
  858. while ((n > 0) && (*p != ' ')) {
  859. p=delete_char(console_buffer, p, &col, &n, plen);
  860. }
  861. continue;
  862. case 0x08: /* ^H - backspace */
  863. case 0x7F: /* DEL - backspace */
  864. p=delete_char(console_buffer, p, &col, &n, plen);
  865. continue;
  866. default:
  867. /*
  868. * Must be a normal character then
  869. */
  870. if (n < CFG_CBSIZE-2) {
  871. if (c == '\t') { /* expand TABs */
  872. #ifdef CONFIG_AUTO_COMPLETE
  873. /* if auto completion triggered just continue */
  874. *p = '\0';
  875. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  876. p = console_buffer + n; /* reset */
  877. continue;
  878. }
  879. #endif
  880. puts (tab_seq+(col&07));
  881. col += 8 - (col&07);
  882. } else {
  883. ++col; /* echo input */
  884. putc (c);
  885. }
  886. *p++ = c;
  887. ++n;
  888. } else { /* Buffer full */
  889. putc ('\a');
  890. }
  891. }
  892. }
  893. #endif /* CONFIG_CMDLINE_EDITING */
  894. }
  895. /****************************************************************************/
  896. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  897. {
  898. char *s;
  899. if (*np == 0) {
  900. return (p);
  901. }
  902. if (*(--p) == '\t') { /* will retype the whole line */
  903. while (*colp > plen) {
  904. puts (erase_seq);
  905. (*colp)--;
  906. }
  907. for (s=buffer; s<p; ++s) {
  908. if (*s == '\t') {
  909. puts (tab_seq+((*colp) & 07));
  910. *colp += 8 - ((*colp) & 07);
  911. } else {
  912. ++(*colp);
  913. putc (*s);
  914. }
  915. }
  916. } else {
  917. puts (erase_seq);
  918. (*colp)--;
  919. }
  920. (*np)--;
  921. return (p);
  922. }
  923. /****************************************************************************/
  924. int parse_line (char *line, char *argv[])
  925. {
  926. int nargs = 0;
  927. #ifdef DEBUG_PARSER
  928. printf ("parse_line: \"%s\"\n", line);
  929. #endif
  930. while (nargs < CFG_MAXARGS) {
  931. /* skip any white space */
  932. while ((*line == ' ') || (*line == '\t')) {
  933. ++line;
  934. }
  935. if (*line == '\0') { /* end of line, no more args */
  936. argv[nargs] = NULL;
  937. #ifdef DEBUG_PARSER
  938. printf ("parse_line: nargs=%d\n", nargs);
  939. #endif
  940. return (nargs);
  941. }
  942. argv[nargs++] = line; /* begin of argument string */
  943. /* find end of string */
  944. while (*line && (*line != ' ') && (*line != '\t')) {
  945. ++line;
  946. }
  947. if (*line == '\0') { /* end of line, no more args */
  948. argv[nargs] = NULL;
  949. #ifdef DEBUG_PARSER
  950. printf ("parse_line: nargs=%d\n", nargs);
  951. #endif
  952. return (nargs);
  953. }
  954. *line++ = '\0'; /* terminate current arg */
  955. }
  956. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  957. #ifdef DEBUG_PARSER
  958. printf ("parse_line: nargs=%d\n", nargs);
  959. #endif
  960. return (nargs);
  961. }
  962. /****************************************************************************/
  963. static void process_macros (const char *input, char *output)
  964. {
  965. char c, prev;
  966. const char *varname_start = NULL;
  967. int inputcnt = strlen (input);
  968. int outputcnt = CFG_CBSIZE;
  969. int state = 0; /* 0 = waiting for '$' */
  970. /* 1 = waiting for '(' or '{' */
  971. /* 2 = waiting for ')' or '}' */
  972. /* 3 = waiting for ''' */
  973. #ifdef DEBUG_PARSER
  974. char *output_start = output;
  975. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen(input), input);
  976. #endif
  977. prev = '\0'; /* previous character */
  978. while (inputcnt && outputcnt) {
  979. c = *input++;
  980. inputcnt--;
  981. if (state!=3) {
  982. /* remove one level of escape characters */
  983. if ((c == '\\') && (prev != '\\')) {
  984. if (inputcnt-- == 0)
  985. break;
  986. prev = c;
  987. c = *input++;
  988. }
  989. }
  990. switch (state) {
  991. case 0: /* Waiting for (unescaped) $ */
  992. if ((c == '\'') && (prev != '\\')) {
  993. state = 3;
  994. break;
  995. }
  996. if ((c == '$') && (prev != '\\')) {
  997. state++;
  998. } else {
  999. *(output++) = c;
  1000. outputcnt--;
  1001. }
  1002. break;
  1003. case 1: /* Waiting for ( */
  1004. if (c == '(' || c == '{') {
  1005. state++;
  1006. varname_start = input;
  1007. } else {
  1008. state = 0;
  1009. *(output++) = '$';
  1010. outputcnt--;
  1011. if (outputcnt) {
  1012. *(output++) = c;
  1013. outputcnt--;
  1014. }
  1015. }
  1016. break;
  1017. case 2: /* Waiting for ) */
  1018. if (c == ')' || c == '}') {
  1019. int i;
  1020. char envname[CFG_CBSIZE], *envval;
  1021. int envcnt = input-varname_start-1; /* Varname # of chars */
  1022. /* Get the varname */
  1023. for (i = 0; i < envcnt; i++) {
  1024. envname[i] = varname_start[i];
  1025. }
  1026. envname[i] = 0;
  1027. /* Get its value */
  1028. envval = getenv (envname);
  1029. /* Copy into the line if it exists */
  1030. if (envval != NULL)
  1031. while ((*envval) && outputcnt) {
  1032. *(output++) = *(envval++);
  1033. outputcnt--;
  1034. }
  1035. /* Look for another '$' */
  1036. state = 0;
  1037. }
  1038. break;
  1039. case 3: /* Waiting for ' */
  1040. if ((c == '\'') && (prev != '\\')) {
  1041. state = 0;
  1042. } else {
  1043. *(output++) = c;
  1044. outputcnt--;
  1045. }
  1046. break;
  1047. }
  1048. prev = c;
  1049. }
  1050. if (outputcnt)
  1051. *output = 0;
  1052. #ifdef DEBUG_PARSER
  1053. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1054. strlen(output_start), output_start);
  1055. #endif
  1056. }
  1057. /****************************************************************************
  1058. * returns:
  1059. * 1 - command executed, repeatable
  1060. * 0 - command executed but not repeatable, interrupted commands are
  1061. * always considered not repeatable
  1062. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1063. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  1064. * considered unrecognized)
  1065. *
  1066. * WARNING:
  1067. *
  1068. * We must create a temporary copy of the command since the command we get
  1069. * may be the result from getenv(), which returns a pointer directly to
  1070. * the environment data, which may change magicly when the command we run
  1071. * creates or modifies environment variables (like "bootp" does).
  1072. */
  1073. int run_command (const char *cmd, int flag)
  1074. {
  1075. cmd_tbl_t *cmdtp;
  1076. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  1077. char *token; /* start of token in cmdbuf */
  1078. char *sep; /* end of token (separator) in cmdbuf */
  1079. char finaltoken[CFG_CBSIZE];
  1080. char *str = cmdbuf;
  1081. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  1082. int argc, inquotes;
  1083. int repeatable = 1;
  1084. int rc = 0;
  1085. #ifdef DEBUG_PARSER
  1086. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1087. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1088. puts ("\"\n");
  1089. #endif
  1090. clear_ctrlc(); /* forget any previous Control C */
  1091. if (!cmd || !*cmd) {
  1092. return -1; /* empty command */
  1093. }
  1094. if (strlen(cmd) >= CFG_CBSIZE) {
  1095. puts ("## Command too long!\n");
  1096. return -1;
  1097. }
  1098. strcpy (cmdbuf, cmd);
  1099. /* Process separators and check for invalid
  1100. * repeatable commands
  1101. */
  1102. #ifdef DEBUG_PARSER
  1103. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1104. #endif
  1105. while (*str) {
  1106. /*
  1107. * Find separator, or string end
  1108. * Allow simple escape of ';' by writing "\;"
  1109. */
  1110. for (inquotes = 0, sep = str; *sep; sep++) {
  1111. if ((*sep=='\'') &&
  1112. (*(sep-1) != '\\'))
  1113. inquotes=!inquotes;
  1114. if (!inquotes &&
  1115. (*sep == ';') && /* separator */
  1116. ( sep != str) && /* past string start */
  1117. (*(sep-1) != '\\')) /* and NOT escaped */
  1118. break;
  1119. }
  1120. /*
  1121. * Limit the token to data between separators
  1122. */
  1123. token = str;
  1124. if (*sep) {
  1125. str = sep + 1; /* start of command for next pass */
  1126. *sep = '\0';
  1127. }
  1128. else
  1129. str = sep; /* no more commands for next pass */
  1130. #ifdef DEBUG_PARSER
  1131. printf ("token: \"%s\"\n", token);
  1132. #endif
  1133. /* find macros in this token and replace them */
  1134. process_macros (token, finaltoken);
  1135. /* Extract arguments */
  1136. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1137. rc = -1; /* no command at all */
  1138. continue;
  1139. }
  1140. /* Look up command in command table */
  1141. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1142. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1143. rc = -1; /* give up after bad command */
  1144. continue;
  1145. }
  1146. /* found - check max args */
  1147. if (argc > cmdtp->maxargs) {
  1148. printf ("Usage:\n%s\n", cmdtp->usage);
  1149. rc = -1;
  1150. continue;
  1151. }
  1152. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  1153. /* avoid "bootd" recursion */
  1154. if (cmdtp->cmd == do_bootd) {
  1155. #ifdef DEBUG_PARSER
  1156. printf ("[%s]\n", finaltoken);
  1157. #endif
  1158. if (flag & CMD_FLAG_BOOTD) {
  1159. puts ("'bootd' recursion detected\n");
  1160. rc = -1;
  1161. continue;
  1162. } else {
  1163. flag |= CMD_FLAG_BOOTD;
  1164. }
  1165. }
  1166. #endif /* CFG_CMD_BOOTD */
  1167. /* OK - call function to do the command */
  1168. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1169. rc = -1;
  1170. }
  1171. repeatable &= cmdtp->repeatable;
  1172. /* Did the user stop this? */
  1173. if (had_ctrlc ())
  1174. return 0; /* if stopped then not repeatable */
  1175. }
  1176. return rc ? rc : repeatable;
  1177. }
  1178. /****************************************************************************/
  1179. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  1180. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1181. {
  1182. int i;
  1183. if (argc < 2) {
  1184. printf ("Usage:\n%s\n", cmdtp->usage);
  1185. return 1;
  1186. }
  1187. for (i=1; i<argc; ++i) {
  1188. char *arg;
  1189. if ((arg = getenv (argv[i])) == NULL) {
  1190. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1191. return 1;
  1192. }
  1193. #ifndef CFG_HUSH_PARSER
  1194. if (run_command (arg, flag) == -1)
  1195. return 1;
  1196. #else
  1197. if (parse_string_outer(arg,
  1198. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  1199. return 1;
  1200. #endif
  1201. }
  1202. return 0;
  1203. }
  1204. #endif /* CFG_CMD_RUN */