main.c 29 KB

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