main.c 29 KB

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