main.c 30 KB

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