main.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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 int parse_line (char *, char *[]);
  47. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  48. static int abortboot(int);
  49. #endif
  50. #undef DEBUG_PARSER
  51. char console_buffer[CFG_CBSIZE]; /* console I/O buffer */
  52. #ifndef CONFIG_CMDLINE_EDITING
  53. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen);
  54. static char erase_seq[] = "\b \b"; /* erase sequence */
  55. static char tab_seq[] = " "; /* used to expand TABs */
  56. #endif /* CONFIG_CMDLINE_EDITING */
  57. #ifdef CONFIG_BOOT_RETRY_TIME
  58. static uint64_t endtime = 0; /* must be set, default is instant timeout */
  59. static int retry_time = -1; /* -1 so can call readline before main_loop */
  60. #endif
  61. #define endtick(seconds) (get_ticks() + (uint64_t)(seconds) * get_tbclk())
  62. #ifndef CONFIG_BOOT_RETRY_MIN
  63. #define CONFIG_BOOT_RETRY_MIN CONFIG_BOOT_RETRY_TIME
  64. #endif
  65. #ifdef CONFIG_MODEM_SUPPORT
  66. int do_mdm_init = 0;
  67. extern void mdm_init(void); /* defined in board.c */
  68. #endif
  69. /***************************************************************************
  70. * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  71. * returns: 0 - no key string, allow autoboot
  72. * 1 - got key string, abort
  73. */
  74. #if defined(CONFIG_BOOTDELAY) && (CONFIG_BOOTDELAY >= 0)
  75. # if defined(CONFIG_AUTOBOOT_KEYED)
  76. static __inline__ int abortboot(int bootdelay)
  77. {
  78. int abort = 0;
  79. uint64_t etime = endtick(bootdelay);
  80. struct {
  81. char* str;
  82. u_int len;
  83. int retry;
  84. }
  85. delaykey [] = {
  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. * initialize 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. #define putnstr(str,n) do { \
  453. printf ("%.*s", n, str); \
  454. } while (0)
  455. #define CTL_CH(c) ((c) - 'a' + 1)
  456. #define MAX_CMDBUF_SIZE 256
  457. #define CTL_BACKSPACE ('\b')
  458. #define DEL ((char)255)
  459. #define DEL7 ((char)127)
  460. #define CREAD_HIST_CHAR ('!')
  461. #define getcmd_putch(ch) putc(ch)
  462. #define getcmd_getch() getc()
  463. #define getcmd_cbeep() getcmd_putch('\a')
  464. #define HIST_MAX 20
  465. #define HIST_SIZE MAX_CMDBUF_SIZE
  466. static int hist_max = 0;
  467. static int hist_add_idx = 0;
  468. static int hist_cur = -1;
  469. unsigned hist_num = 0;
  470. char* hist_list[HIST_MAX];
  471. char hist_lines[HIST_MAX][HIST_SIZE];
  472. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  473. static void hist_init(void)
  474. {
  475. int i;
  476. hist_max = 0;
  477. hist_add_idx = 0;
  478. hist_cur = -1;
  479. hist_num = 0;
  480. for (i = 0; i < HIST_MAX; i++) {
  481. hist_list[i] = hist_lines[i];
  482. hist_list[i][0] = '\0';
  483. }
  484. }
  485. static void cread_add_to_hist(char *line)
  486. {
  487. strcpy(hist_list[hist_add_idx], line);
  488. if (++hist_add_idx >= HIST_MAX)
  489. hist_add_idx = 0;
  490. if (hist_add_idx > hist_max)
  491. hist_max = hist_add_idx;
  492. hist_num++;
  493. }
  494. static char* hist_prev(void)
  495. {
  496. char *ret;
  497. int old_cur;
  498. if (hist_cur < 0)
  499. return NULL;
  500. old_cur = hist_cur;
  501. if (--hist_cur < 0)
  502. hist_cur = hist_max;
  503. if (hist_cur == hist_add_idx) {
  504. hist_cur = old_cur;
  505. ret = NULL;
  506. } else
  507. ret = hist_list[hist_cur];
  508. return (ret);
  509. }
  510. static char* hist_next(void)
  511. {
  512. char *ret;
  513. if (hist_cur < 0)
  514. return NULL;
  515. if (hist_cur == hist_add_idx)
  516. return NULL;
  517. if (++hist_cur > hist_max)
  518. hist_cur = 0;
  519. if (hist_cur == hist_add_idx) {
  520. ret = "";
  521. } else
  522. ret = hist_list[hist_cur];
  523. return (ret);
  524. }
  525. #ifndef CONFIG_CMDLINE_EDITING
  526. static void cread_print_hist_list(void)
  527. {
  528. int i;
  529. unsigned long n;
  530. n = hist_num - hist_max;
  531. i = hist_add_idx + 1;
  532. while (1) {
  533. if (i > hist_max)
  534. i = 0;
  535. if (i == hist_add_idx)
  536. break;
  537. printf("%s\n", hist_list[i]);
  538. n++;
  539. i++;
  540. }
  541. }
  542. #endif /* CONFIG_CMDLINE_EDITING */
  543. #define BEGINNING_OF_LINE() { \
  544. while (num) { \
  545. getcmd_putch(CTL_BACKSPACE); \
  546. num--; \
  547. } \
  548. }
  549. #define ERASE_TO_EOL() { \
  550. if (num < eol_num) { \
  551. int tmp; \
  552. for (tmp = num; tmp < eol_num; tmp++) \
  553. getcmd_putch(' '); \
  554. while (tmp-- > num) \
  555. getcmd_putch(CTL_BACKSPACE); \
  556. eol_num = num; \
  557. } \
  558. }
  559. #define REFRESH_TO_EOL() { \
  560. if (num < eol_num) { \
  561. wlen = eol_num - num; \
  562. putnstr(buf + num, wlen); \
  563. num = eol_num; \
  564. } \
  565. }
  566. static void cread_add_char(char ichar, int insert, unsigned long *num,
  567. unsigned long *eol_num, char *buf, unsigned long len)
  568. {
  569. unsigned long wlen;
  570. /* room ??? */
  571. if (insert || *num == *eol_num) {
  572. if (*eol_num > len - 1) {
  573. getcmd_cbeep();
  574. return;
  575. }
  576. (*eol_num)++;
  577. }
  578. if (insert) {
  579. wlen = *eol_num - *num;
  580. if (wlen > 1) {
  581. memmove(&buf[*num+1], &buf[*num], wlen-1);
  582. }
  583. buf[*num] = ichar;
  584. putnstr(buf + *num, wlen);
  585. (*num)++;
  586. while (--wlen) {
  587. getcmd_putch(CTL_BACKSPACE);
  588. }
  589. } else {
  590. /* echo the character */
  591. wlen = 1;
  592. buf[*num] = ichar;
  593. putnstr(buf + *num, wlen);
  594. (*num)++;
  595. }
  596. }
  597. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  598. unsigned long *eol_num, char *buf, unsigned long len)
  599. {
  600. while (strsize--) {
  601. cread_add_char(*str, insert, num, eol_num, buf, len);
  602. str++;
  603. }
  604. }
  605. static int cread_line(char *buf, unsigned int *len)
  606. {
  607. unsigned long num = 0;
  608. unsigned long eol_num = 0;
  609. unsigned long rlen;
  610. unsigned long wlen;
  611. char ichar;
  612. int insert = 1;
  613. int esc_len = 0;
  614. int rc = 0;
  615. char esc_save[8];
  616. while (1) {
  617. rlen = 1;
  618. ichar = getcmd_getch();
  619. if ((ichar == '\n') || (ichar == '\r')) {
  620. putc('\n');
  621. break;
  622. }
  623. /*
  624. * handle standard linux xterm esc sequences for arrow key, etc.
  625. */
  626. if (esc_len != 0) {
  627. if (esc_len == 1) {
  628. if (ichar == '[') {
  629. esc_save[esc_len] = ichar;
  630. esc_len = 2;
  631. } else {
  632. cread_add_str(esc_save, esc_len, insert,
  633. &num, &eol_num, buf, *len);
  634. esc_len = 0;
  635. }
  636. continue;
  637. }
  638. switch (ichar) {
  639. case 'D': /* <- key */
  640. ichar = CTL_CH('b');
  641. esc_len = 0;
  642. break;
  643. case 'C': /* -> key */
  644. ichar = CTL_CH('f');
  645. esc_len = 0;
  646. break; /* pass off to ^F handler */
  647. case 'H': /* Home key */
  648. ichar = CTL_CH('a');
  649. esc_len = 0;
  650. break; /* pass off to ^A handler */
  651. case 'A': /* up arrow */
  652. ichar = CTL_CH('p');
  653. esc_len = 0;
  654. break; /* pass off to ^P handler */
  655. case 'B': /* down arrow */
  656. ichar = CTL_CH('n');
  657. esc_len = 0;
  658. break; /* pass off to ^N handler */
  659. default:
  660. esc_save[esc_len++] = ichar;
  661. cread_add_str(esc_save, esc_len, insert,
  662. &num, &eol_num, buf, *len);
  663. esc_len = 0;
  664. continue;
  665. }
  666. }
  667. switch (ichar) {
  668. case 0x1b:
  669. if (esc_len == 0) {
  670. esc_save[esc_len] = ichar;
  671. esc_len = 1;
  672. } else {
  673. puts("impossible condition #876\n");
  674. esc_len = 0;
  675. }
  676. break;
  677. case CTL_CH('a'):
  678. BEGINNING_OF_LINE();
  679. break;
  680. case CTL_CH('c'): /* ^C - break */
  681. *buf = '\0'; /* discard input */
  682. return (-1);
  683. case CTL_CH('f'):
  684. if (num < eol_num) {
  685. getcmd_putch(buf[num]);
  686. num++;
  687. }
  688. break;
  689. case CTL_CH('b'):
  690. if (num) {
  691. getcmd_putch(CTL_BACKSPACE);
  692. num--;
  693. }
  694. break;
  695. case CTL_CH('d'):
  696. if (num < eol_num) {
  697. wlen = eol_num - num - 1;
  698. if (wlen) {
  699. memmove(&buf[num], &buf[num+1], wlen);
  700. putnstr(buf + num, wlen);
  701. }
  702. getcmd_putch(' ');
  703. do {
  704. getcmd_putch(CTL_BACKSPACE);
  705. } while (wlen--);
  706. eol_num--;
  707. }
  708. break;
  709. case CTL_CH('k'):
  710. ERASE_TO_EOL();
  711. break;
  712. case CTL_CH('e'):
  713. REFRESH_TO_EOL();
  714. break;
  715. case CTL_CH('o'):
  716. insert = !insert;
  717. break;
  718. case CTL_CH('x'):
  719. BEGINNING_OF_LINE();
  720. ERASE_TO_EOL();
  721. break;
  722. case DEL:
  723. case DEL7:
  724. case 8:
  725. if (num) {
  726. wlen = eol_num - num;
  727. num--;
  728. memmove(&buf[num], &buf[num+1], wlen);
  729. getcmd_putch(CTL_BACKSPACE);
  730. putnstr(buf + num, wlen);
  731. getcmd_putch(' ');
  732. do {
  733. getcmd_putch(CTL_BACKSPACE);
  734. } while (wlen--);
  735. eol_num--;
  736. }
  737. break;
  738. case CTL_CH('p'):
  739. case CTL_CH('n'):
  740. {
  741. char * hline;
  742. esc_len = 0;
  743. if (ichar == CTL_CH('p'))
  744. hline = hist_prev();
  745. else
  746. hline = hist_next();
  747. if (!hline) {
  748. getcmd_cbeep();
  749. continue;
  750. }
  751. /* nuke the current line */
  752. /* first, go home */
  753. BEGINNING_OF_LINE();
  754. /* erase to end of line */
  755. ERASE_TO_EOL();
  756. /* copy new line into place and display */
  757. strcpy(buf, hline);
  758. eol_num = strlen(buf);
  759. REFRESH_TO_EOL();
  760. continue;
  761. }
  762. default:
  763. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  764. break;
  765. }
  766. }
  767. *len = eol_num;
  768. buf[eol_num] = '\0'; /* lose the newline */
  769. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  770. cread_add_to_hist(buf);
  771. hist_cur = hist_add_idx;
  772. return (rc);
  773. }
  774. #endif /* CONFIG_CMDLINE_EDITING */
  775. /****************************************************************************/
  776. /*
  777. * Prompt for input and read a line.
  778. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  779. * time out when time goes past endtime (timebase time in ticks).
  780. * Return: number of read characters
  781. * -1 if break
  782. * -2 if timed out
  783. */
  784. int readline (const char *const prompt)
  785. {
  786. #ifdef CONFIG_CMDLINE_EDITING
  787. char *p = console_buffer;
  788. unsigned int len=MAX_CMDBUF_SIZE;
  789. int rc;
  790. static int initted = 0;
  791. if (!initted) {
  792. hist_init();
  793. initted = 1;
  794. }
  795. puts (prompt);
  796. rc = cread_line(p, &len);
  797. return rc < 0 ? rc : len;
  798. #else
  799. char *p = console_buffer;
  800. int n = 0; /* buffer index */
  801. int plen = 0; /* prompt length */
  802. int col; /* output column cnt */
  803. char c;
  804. /* print prompt */
  805. if (prompt) {
  806. plen = strlen (prompt);
  807. puts (prompt);
  808. }
  809. col = plen;
  810. for (;;) {
  811. #ifdef CONFIG_BOOT_RETRY_TIME
  812. while (!tstc()) { /* while no incoming data */
  813. if (retry_time >= 0 && get_ticks() > endtime)
  814. return (-2); /* timed out */
  815. }
  816. #endif
  817. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  818. #ifdef CONFIG_SHOW_ACTIVITY
  819. while (!tstc()) {
  820. extern void show_activity(int arg);
  821. show_activity(0);
  822. }
  823. #endif
  824. c = getc();
  825. /*
  826. * Special character handling
  827. */
  828. switch (c) {
  829. case '\r': /* Enter */
  830. case '\n':
  831. *p = '\0';
  832. puts ("\r\n");
  833. return (p - console_buffer);
  834. case '\0': /* nul */
  835. continue;
  836. case 0x03: /* ^C - break */
  837. console_buffer[0] = '\0'; /* discard input */
  838. return (-1);
  839. case 0x15: /* ^U - erase line */
  840. while (col > plen) {
  841. puts (erase_seq);
  842. --col;
  843. }
  844. p = console_buffer;
  845. n = 0;
  846. continue;
  847. case 0x17: /* ^W - erase word */
  848. p=delete_char(console_buffer, p, &col, &n, plen);
  849. while ((n > 0) && (*p != ' ')) {
  850. p=delete_char(console_buffer, p, &col, &n, plen);
  851. }
  852. continue;
  853. case 0x08: /* ^H - backspace */
  854. case 0x7F: /* DEL - backspace */
  855. p=delete_char(console_buffer, p, &col, &n, plen);
  856. continue;
  857. default:
  858. /*
  859. * Must be a normal character then
  860. */
  861. if (n < CFG_CBSIZE-2) {
  862. if (c == '\t') { /* expand TABs */
  863. #ifdef CONFIG_AUTO_COMPLETE
  864. /* if auto completion triggered just continue */
  865. *p = '\0';
  866. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  867. p = console_buffer + n; /* reset */
  868. continue;
  869. }
  870. #endif
  871. puts (tab_seq+(col&07));
  872. col += 8 - (col&07);
  873. } else {
  874. ++col; /* echo input */
  875. putc (c);
  876. }
  877. *p++ = c;
  878. ++n;
  879. } else { /* Buffer full */
  880. putc ('\a');
  881. }
  882. }
  883. }
  884. #endif /* CONFIG_CMDLINE_EDITING */
  885. }
  886. /****************************************************************************/
  887. #ifndef CONFIG_CMDLINE_EDITING
  888. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  889. {
  890. char *s;
  891. if (*np == 0) {
  892. return (p);
  893. }
  894. if (*(--p) == '\t') { /* will retype the whole line */
  895. while (*colp > plen) {
  896. puts (erase_seq);
  897. (*colp)--;
  898. }
  899. for (s=buffer; s<p; ++s) {
  900. if (*s == '\t') {
  901. puts (tab_seq+((*colp) & 07));
  902. *colp += 8 - ((*colp) & 07);
  903. } else {
  904. ++(*colp);
  905. putc (*s);
  906. }
  907. }
  908. } else {
  909. puts (erase_seq);
  910. (*colp)--;
  911. }
  912. (*np)--;
  913. return (p);
  914. }
  915. #endif /* CONFIG_CMDLINE_EDITING */
  916. /****************************************************************************/
  917. int parse_line (char *line, char *argv[])
  918. {
  919. int nargs = 0;
  920. #ifdef DEBUG_PARSER
  921. printf ("parse_line: \"%s\"\n", line);
  922. #endif
  923. while (nargs < CFG_MAXARGS) {
  924. /* skip any white space */
  925. while ((*line == ' ') || (*line == '\t')) {
  926. ++line;
  927. }
  928. if (*line == '\0') { /* end of line, no more args */
  929. argv[nargs] = NULL;
  930. #ifdef DEBUG_PARSER
  931. printf ("parse_line: nargs=%d\n", nargs);
  932. #endif
  933. return (nargs);
  934. }
  935. argv[nargs++] = line; /* begin of argument string */
  936. /* find end of string */
  937. while (*line && (*line != ' ') && (*line != '\t')) {
  938. ++line;
  939. }
  940. if (*line == '\0') { /* end of line, no more args */
  941. argv[nargs] = NULL;
  942. #ifdef DEBUG_PARSER
  943. printf ("parse_line: nargs=%d\n", nargs);
  944. #endif
  945. return (nargs);
  946. }
  947. *line++ = '\0'; /* terminate current arg */
  948. }
  949. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  950. #ifdef DEBUG_PARSER
  951. printf ("parse_line: nargs=%d\n", nargs);
  952. #endif
  953. return (nargs);
  954. }
  955. /****************************************************************************/
  956. static void process_macros (const char *input, char *output)
  957. {
  958. char c, prev;
  959. const char *varname_start = NULL;
  960. int inputcnt = strlen (input);
  961. int outputcnt = CFG_CBSIZE;
  962. int state = 0; /* 0 = waiting for '$' */
  963. /* 1 = waiting for '(' or '{' */
  964. /* 2 = waiting for ')' or '}' */
  965. /* 3 = waiting for ''' */
  966. #ifdef DEBUG_PARSER
  967. char *output_start = output;
  968. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  969. input);
  970. #endif
  971. prev = '\0'; /* previous character */
  972. while (inputcnt && outputcnt) {
  973. c = *input++;
  974. inputcnt--;
  975. if (state != 3) {
  976. /* remove one level of escape characters */
  977. if ((c == '\\') && (prev != '\\')) {
  978. if (inputcnt-- == 0)
  979. break;
  980. prev = c;
  981. c = *input++;
  982. }
  983. }
  984. switch (state) {
  985. case 0: /* Waiting for (unescaped) $ */
  986. if ((c == '\'') && (prev != '\\')) {
  987. state = 3;
  988. break;
  989. }
  990. if ((c == '$') && (prev != '\\')) {
  991. state++;
  992. } else {
  993. *(output++) = c;
  994. outputcnt--;
  995. }
  996. break;
  997. case 1: /* Waiting for ( */
  998. if (c == '(' || c == '{') {
  999. state++;
  1000. varname_start = input;
  1001. } else {
  1002. state = 0;
  1003. *(output++) = '$';
  1004. outputcnt--;
  1005. if (outputcnt) {
  1006. *(output++) = c;
  1007. outputcnt--;
  1008. }
  1009. }
  1010. break;
  1011. case 2: /* Waiting for ) */
  1012. if (c == ')' || c == '}') {
  1013. int i;
  1014. char envname[CFG_CBSIZE], *envval;
  1015. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1016. /* Get the varname */
  1017. for (i = 0; i < envcnt; i++) {
  1018. envname[i] = varname_start[i];
  1019. }
  1020. envname[i] = 0;
  1021. /* Get its value */
  1022. envval = getenv (envname);
  1023. /* Copy into the line if it exists */
  1024. if (envval != NULL)
  1025. while ((*envval) && outputcnt) {
  1026. *(output++) = *(envval++);
  1027. outputcnt--;
  1028. }
  1029. /* Look for another '$' */
  1030. state = 0;
  1031. }
  1032. break;
  1033. case 3: /* Waiting for ' */
  1034. if ((c == '\'') && (prev != '\\')) {
  1035. state = 0;
  1036. } else {
  1037. *(output++) = c;
  1038. outputcnt--;
  1039. }
  1040. break;
  1041. }
  1042. prev = c;
  1043. }
  1044. if (outputcnt)
  1045. *output = 0;
  1046. #ifdef DEBUG_PARSER
  1047. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1048. strlen (output_start), output_start);
  1049. #endif
  1050. }
  1051. /****************************************************************************
  1052. * returns:
  1053. * 1 - command executed, repeatable
  1054. * 0 - command executed but not repeatable, interrupted commands are
  1055. * always considered not repeatable
  1056. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1057. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  1058. * considered unrecognized)
  1059. *
  1060. * WARNING:
  1061. *
  1062. * We must create a temporary copy of the command since the command we get
  1063. * may be the result from getenv(), which returns a pointer directly to
  1064. * the environment data, which may change magicly when the command we run
  1065. * creates or modifies environment variables (like "bootp" does).
  1066. */
  1067. int run_command (const char *cmd, int flag)
  1068. {
  1069. cmd_tbl_t *cmdtp;
  1070. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  1071. char *token; /* start of token in cmdbuf */
  1072. char *sep; /* end of token (separator) in cmdbuf */
  1073. char finaltoken[CFG_CBSIZE];
  1074. char *str = cmdbuf;
  1075. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  1076. int argc, inquotes;
  1077. int repeatable = 1;
  1078. int rc = 0;
  1079. #ifdef DEBUG_PARSER
  1080. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1081. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1082. puts ("\"\n");
  1083. #endif
  1084. clear_ctrlc(); /* forget any previous Control C */
  1085. if (!cmd || !*cmd) {
  1086. return -1; /* empty command */
  1087. }
  1088. if (strlen(cmd) >= CFG_CBSIZE) {
  1089. puts ("## Command too long!\n");
  1090. return -1;
  1091. }
  1092. strcpy (cmdbuf, cmd);
  1093. /* Process separators and check for invalid
  1094. * repeatable commands
  1095. */
  1096. #ifdef DEBUG_PARSER
  1097. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1098. #endif
  1099. while (*str) {
  1100. /*
  1101. * Find separator, or string end
  1102. * Allow simple escape of ';' by writing "\;"
  1103. */
  1104. for (inquotes = 0, sep = str; *sep; sep++) {
  1105. if ((*sep=='\'') &&
  1106. (*(sep-1) != '\\'))
  1107. inquotes=!inquotes;
  1108. if (!inquotes &&
  1109. (*sep == ';') && /* separator */
  1110. ( sep != str) && /* past string start */
  1111. (*(sep-1) != '\\')) /* and NOT escaped */
  1112. break;
  1113. }
  1114. /*
  1115. * Limit the token to data between separators
  1116. */
  1117. token = str;
  1118. if (*sep) {
  1119. str = sep + 1; /* start of command for next pass */
  1120. *sep = '\0';
  1121. }
  1122. else
  1123. str = sep; /* no more commands for next pass */
  1124. #ifdef DEBUG_PARSER
  1125. printf ("token: \"%s\"\n", token);
  1126. #endif
  1127. /* find macros in this token and replace them */
  1128. process_macros (token, finaltoken);
  1129. /* Extract arguments */
  1130. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1131. rc = -1; /* no command at all */
  1132. continue;
  1133. }
  1134. /* Look up command in command table */
  1135. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1136. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1137. rc = -1; /* give up after bad command */
  1138. continue;
  1139. }
  1140. /* found - check max args */
  1141. if (argc > cmdtp->maxargs) {
  1142. printf ("Usage:\n%s\n", cmdtp->usage);
  1143. rc = -1;
  1144. continue;
  1145. }
  1146. #if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
  1147. /* avoid "bootd" recursion */
  1148. if (cmdtp->cmd == do_bootd) {
  1149. #ifdef DEBUG_PARSER
  1150. printf ("[%s]\n", finaltoken);
  1151. #endif
  1152. if (flag & CMD_FLAG_BOOTD) {
  1153. puts ("'bootd' recursion detected\n");
  1154. rc = -1;
  1155. continue;
  1156. } else {
  1157. flag |= CMD_FLAG_BOOTD;
  1158. }
  1159. }
  1160. #endif /* CFG_CMD_BOOTD */
  1161. /* OK - call function to do the command */
  1162. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1163. rc = -1;
  1164. }
  1165. repeatable &= cmdtp->repeatable;
  1166. /* Did the user stop this? */
  1167. if (had_ctrlc ())
  1168. return 0; /* if stopped then not repeatable */
  1169. }
  1170. return rc ? rc : repeatable;
  1171. }
  1172. /****************************************************************************/
  1173. #if (CONFIG_COMMANDS & CFG_CMD_RUN)
  1174. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1175. {
  1176. int i;
  1177. if (argc < 2) {
  1178. printf ("Usage:\n%s\n", cmdtp->usage);
  1179. return 1;
  1180. }
  1181. for (i=1; i<argc; ++i) {
  1182. char *arg;
  1183. if ((arg = getenv (argv[i])) == NULL) {
  1184. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1185. return 1;
  1186. }
  1187. #ifndef CFG_HUSH_PARSER
  1188. if (run_command (arg, flag) == -1)
  1189. return 1;
  1190. #else
  1191. if (parse_string_outer(arg,
  1192. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  1193. return 1;
  1194. #endif
  1195. }
  1196. return 0;
  1197. }
  1198. #endif /* CFG_CMD_RUN */