main.c 30 KB

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