main.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  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(const char *const prompt, 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. case CTL_CH('u'):
  699. BEGINNING_OF_LINE();
  700. ERASE_TO_EOL();
  701. break;
  702. case DEL:
  703. case DEL7:
  704. case 8:
  705. if (num) {
  706. wlen = eol_num - num;
  707. num--;
  708. memmove(&buf[num], &buf[num+1], wlen);
  709. getcmd_putch(CTL_BACKSPACE);
  710. putnstr(buf + num, wlen);
  711. getcmd_putch(' ');
  712. do {
  713. getcmd_putch(CTL_BACKSPACE);
  714. } while (wlen--);
  715. eol_num--;
  716. }
  717. break;
  718. case CTL_CH('p'):
  719. case CTL_CH('n'):
  720. {
  721. char * hline;
  722. esc_len = 0;
  723. if (ichar == CTL_CH('p'))
  724. hline = hist_prev();
  725. else
  726. hline = hist_next();
  727. if (!hline) {
  728. getcmd_cbeep();
  729. continue;
  730. }
  731. /* nuke the current line */
  732. /* first, go home */
  733. BEGINNING_OF_LINE();
  734. /* erase to end of line */
  735. ERASE_TO_EOL();
  736. /* copy new line into place and display */
  737. strcpy(buf, hline);
  738. eol_num = strlen(buf);
  739. REFRESH_TO_EOL();
  740. continue;
  741. }
  742. #ifdef CONFIG_AUTO_COMPLETE
  743. case '\t': {
  744. int num2, col;
  745. /* do not autocomplete when in the middle */
  746. if (num < eol_num) {
  747. getcmd_cbeep();
  748. break;
  749. }
  750. buf[num] = '\0';
  751. col = strlen(prompt) + eol_num;
  752. num2 = num;
  753. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  754. col = num2 - num;
  755. num += col;
  756. eol_num += col;
  757. }
  758. break;
  759. }
  760. #endif
  761. default:
  762. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  763. break;
  764. }
  765. }
  766. *len = eol_num;
  767. buf[eol_num] = '\0'; /* lose the newline */
  768. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  769. cread_add_to_hist(buf);
  770. hist_cur = hist_add_idx;
  771. return (rc);
  772. }
  773. #endif /* CONFIG_CMDLINE_EDITING */
  774. /****************************************************************************/
  775. /*
  776. * Prompt for input and read a line.
  777. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  778. * time out when time goes past endtime (timebase time in ticks).
  779. * Return: number of read characters
  780. * -1 if break
  781. * -2 if timed out
  782. */
  783. int readline (const char *const prompt)
  784. {
  785. #ifdef CONFIG_CMDLINE_EDITING
  786. char *p = console_buffer;
  787. unsigned int len=MAX_CMDBUF_SIZE;
  788. int rc;
  789. static int initted = 0;
  790. if (!initted) {
  791. hist_init();
  792. initted = 1;
  793. }
  794. puts (prompt);
  795. rc = cread_line(prompt, p, &len);
  796. return rc < 0 ? rc : len;
  797. #else
  798. char *p = console_buffer;
  799. int n = 0; /* buffer index */
  800. int plen = 0; /* prompt length */
  801. int col; /* output column cnt */
  802. char c;
  803. /* print prompt */
  804. if (prompt) {
  805. plen = strlen (prompt);
  806. puts (prompt);
  807. }
  808. col = plen;
  809. for (;;) {
  810. #ifdef CONFIG_BOOT_RETRY_TIME
  811. while (!tstc()) { /* while no incoming data */
  812. if (retry_time >= 0 && get_ticks() > endtime)
  813. return (-2); /* timed out */
  814. }
  815. #endif
  816. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  817. #ifdef CONFIG_SHOW_ACTIVITY
  818. while (!tstc()) {
  819. extern void show_activity(int arg);
  820. show_activity(0);
  821. }
  822. #endif
  823. c = getc();
  824. /*
  825. * Special character handling
  826. */
  827. switch (c) {
  828. case '\r': /* Enter */
  829. case '\n':
  830. *p = '\0';
  831. puts ("\r\n");
  832. return (p - console_buffer);
  833. case '\0': /* nul */
  834. continue;
  835. case 0x03: /* ^C - break */
  836. console_buffer[0] = '\0'; /* discard input */
  837. return (-1);
  838. case 0x15: /* ^U - erase line */
  839. while (col > plen) {
  840. puts (erase_seq);
  841. --col;
  842. }
  843. p = console_buffer;
  844. n = 0;
  845. continue;
  846. case 0x17: /* ^W - erase word */
  847. p=delete_char(console_buffer, p, &col, &n, plen);
  848. while ((n > 0) && (*p != ' ')) {
  849. p=delete_char(console_buffer, p, &col, &n, plen);
  850. }
  851. continue;
  852. case 0x08: /* ^H - backspace */
  853. case 0x7F: /* DEL - backspace */
  854. p=delete_char(console_buffer, p, &col, &n, plen);
  855. continue;
  856. default:
  857. /*
  858. * Must be a normal character then
  859. */
  860. if (n < CFG_CBSIZE-2) {
  861. if (c == '\t') { /* expand TABs */
  862. #ifdef CONFIG_AUTO_COMPLETE
  863. /* if auto completion triggered just continue */
  864. *p = '\0';
  865. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  866. p = console_buffer + n; /* reset */
  867. continue;
  868. }
  869. #endif
  870. puts (tab_seq+(col&07));
  871. col += 8 - (col&07);
  872. } else {
  873. ++col; /* echo input */
  874. putc (c);
  875. }
  876. *p++ = c;
  877. ++n;
  878. } else { /* Buffer full */
  879. putc ('\a');
  880. }
  881. }
  882. }
  883. #endif /* CONFIG_CMDLINE_EDITING */
  884. }
  885. /****************************************************************************/
  886. #ifndef CONFIG_CMDLINE_EDITING
  887. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  888. {
  889. char *s;
  890. if (*np == 0) {
  891. return (p);
  892. }
  893. if (*(--p) == '\t') { /* will retype the whole line */
  894. while (*colp > plen) {
  895. puts (erase_seq);
  896. (*colp)--;
  897. }
  898. for (s=buffer; s<p; ++s) {
  899. if (*s == '\t') {
  900. puts (tab_seq+((*colp) & 07));
  901. *colp += 8 - ((*colp) & 07);
  902. } else {
  903. ++(*colp);
  904. putc (*s);
  905. }
  906. }
  907. } else {
  908. puts (erase_seq);
  909. (*colp)--;
  910. }
  911. (*np)--;
  912. return (p);
  913. }
  914. #endif /* CONFIG_CMDLINE_EDITING */
  915. /****************************************************************************/
  916. int parse_line (char *line, char *argv[])
  917. {
  918. int nargs = 0;
  919. #ifdef DEBUG_PARSER
  920. printf ("parse_line: \"%s\"\n", line);
  921. #endif
  922. while (nargs < CFG_MAXARGS) {
  923. /* skip any white space */
  924. while ((*line == ' ') || (*line == '\t')) {
  925. ++line;
  926. }
  927. if (*line == '\0') { /* end of line, no more args */
  928. argv[nargs] = NULL;
  929. #ifdef DEBUG_PARSER
  930. printf ("parse_line: nargs=%d\n", nargs);
  931. #endif
  932. return (nargs);
  933. }
  934. argv[nargs++] = line; /* begin of argument string */
  935. /* find end of string */
  936. while (*line && (*line != ' ') && (*line != '\t')) {
  937. ++line;
  938. }
  939. if (*line == '\0') { /* end of line, no more args */
  940. argv[nargs] = NULL;
  941. #ifdef DEBUG_PARSER
  942. printf ("parse_line: nargs=%d\n", nargs);
  943. #endif
  944. return (nargs);
  945. }
  946. *line++ = '\0'; /* terminate current arg */
  947. }
  948. printf ("** Too many args (max. %d) **\n", CFG_MAXARGS);
  949. #ifdef DEBUG_PARSER
  950. printf ("parse_line: nargs=%d\n", nargs);
  951. #endif
  952. return (nargs);
  953. }
  954. /****************************************************************************/
  955. static void process_macros (const char *input, char *output)
  956. {
  957. char c, prev;
  958. const char *varname_start = NULL;
  959. int inputcnt = strlen (input);
  960. int outputcnt = CFG_CBSIZE;
  961. int state = 0; /* 0 = waiting for '$' */
  962. /* 1 = waiting for '(' or '{' */
  963. /* 2 = waiting for ')' or '}' */
  964. /* 3 = waiting for ''' */
  965. #ifdef DEBUG_PARSER
  966. char *output_start = output;
  967. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  968. input);
  969. #endif
  970. prev = '\0'; /* previous character */
  971. while (inputcnt && outputcnt) {
  972. c = *input++;
  973. inputcnt--;
  974. if (state != 3) {
  975. /* remove one level of escape characters */
  976. if ((c == '\\') && (prev != '\\')) {
  977. if (inputcnt-- == 0)
  978. break;
  979. prev = c;
  980. c = *input++;
  981. }
  982. }
  983. switch (state) {
  984. case 0: /* Waiting for (unescaped) $ */
  985. if ((c == '\'') && (prev != '\\')) {
  986. state = 3;
  987. break;
  988. }
  989. if ((c == '$') && (prev != '\\')) {
  990. state++;
  991. } else {
  992. *(output++) = c;
  993. outputcnt--;
  994. }
  995. break;
  996. case 1: /* Waiting for ( */
  997. if (c == '(' || c == '{') {
  998. state++;
  999. varname_start = input;
  1000. } else {
  1001. state = 0;
  1002. *(output++) = '$';
  1003. outputcnt--;
  1004. if (outputcnt) {
  1005. *(output++) = c;
  1006. outputcnt--;
  1007. }
  1008. }
  1009. break;
  1010. case 2: /* Waiting for ) */
  1011. if (c == ')' || c == '}') {
  1012. int i;
  1013. char envname[CFG_CBSIZE], *envval;
  1014. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1015. /* Get the varname */
  1016. for (i = 0; i < envcnt; i++) {
  1017. envname[i] = varname_start[i];
  1018. }
  1019. envname[i] = 0;
  1020. /* Get its value */
  1021. envval = getenv (envname);
  1022. /* Copy into the line if it exists */
  1023. if (envval != NULL)
  1024. while ((*envval) && outputcnt) {
  1025. *(output++) = *(envval++);
  1026. outputcnt--;
  1027. }
  1028. /* Look for another '$' */
  1029. state = 0;
  1030. }
  1031. break;
  1032. case 3: /* Waiting for ' */
  1033. if ((c == '\'') && (prev != '\\')) {
  1034. state = 0;
  1035. } else {
  1036. *(output++) = c;
  1037. outputcnt--;
  1038. }
  1039. break;
  1040. }
  1041. prev = c;
  1042. }
  1043. if (outputcnt)
  1044. *output = 0;
  1045. else
  1046. *(output - 1) = 0;
  1047. #ifdef DEBUG_PARSER
  1048. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1049. strlen (output_start), output_start);
  1050. #endif
  1051. }
  1052. /****************************************************************************
  1053. * returns:
  1054. * 1 - command executed, repeatable
  1055. * 0 - command executed but not repeatable, interrupted commands are
  1056. * always considered not repeatable
  1057. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1058. * (If cmd is NULL or "" or longer than CFG_CBSIZE-1 it is
  1059. * considered unrecognized)
  1060. *
  1061. * WARNING:
  1062. *
  1063. * We must create a temporary copy of the command since the command we get
  1064. * may be the result from getenv(), which returns a pointer directly to
  1065. * the environment data, which may change magicly when the command we run
  1066. * creates or modifies environment variables (like "bootp" does).
  1067. */
  1068. int run_command (const char *cmd, int flag)
  1069. {
  1070. cmd_tbl_t *cmdtp;
  1071. char cmdbuf[CFG_CBSIZE]; /* working copy of cmd */
  1072. char *token; /* start of token in cmdbuf */
  1073. char *sep; /* end of token (separator) in cmdbuf */
  1074. char finaltoken[CFG_CBSIZE];
  1075. char *str = cmdbuf;
  1076. char *argv[CFG_MAXARGS + 1]; /* NULL terminated */
  1077. int argc, inquotes;
  1078. int repeatable = 1;
  1079. int rc = 0;
  1080. #ifdef DEBUG_PARSER
  1081. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1082. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1083. puts ("\"\n");
  1084. #endif
  1085. clear_ctrlc(); /* forget any previous Control C */
  1086. if (!cmd || !*cmd) {
  1087. return -1; /* empty command */
  1088. }
  1089. if (strlen(cmd) >= CFG_CBSIZE) {
  1090. puts ("## Command too long!\n");
  1091. return -1;
  1092. }
  1093. strcpy (cmdbuf, cmd);
  1094. /* Process separators and check for invalid
  1095. * repeatable commands
  1096. */
  1097. #ifdef DEBUG_PARSER
  1098. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1099. #endif
  1100. while (*str) {
  1101. /*
  1102. * Find separator, or string end
  1103. * Allow simple escape of ';' by writing "\;"
  1104. */
  1105. for (inquotes = 0, sep = str; *sep; sep++) {
  1106. if ((*sep=='\'') &&
  1107. (*(sep-1) != '\\'))
  1108. inquotes=!inquotes;
  1109. if (!inquotes &&
  1110. (*sep == ';') && /* separator */
  1111. ( sep != str) && /* past string start */
  1112. (*(sep-1) != '\\')) /* and NOT escaped */
  1113. break;
  1114. }
  1115. /*
  1116. * Limit the token to data between separators
  1117. */
  1118. token = str;
  1119. if (*sep) {
  1120. str = sep + 1; /* start of command for next pass */
  1121. *sep = '\0';
  1122. }
  1123. else
  1124. str = sep; /* no more commands for next pass */
  1125. #ifdef DEBUG_PARSER
  1126. printf ("token: \"%s\"\n", token);
  1127. #endif
  1128. /* find macros in this token and replace them */
  1129. process_macros (token, finaltoken);
  1130. /* Extract arguments */
  1131. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1132. rc = -1; /* no command at all */
  1133. continue;
  1134. }
  1135. /* Look up command in command table */
  1136. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1137. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1138. rc = -1; /* give up after bad command */
  1139. continue;
  1140. }
  1141. /* found - check max args */
  1142. if (argc > cmdtp->maxargs) {
  1143. printf ("Usage:\n%s\n", cmdtp->usage);
  1144. rc = -1;
  1145. continue;
  1146. }
  1147. #if defined(CONFIG_CMD_BOOTD)
  1148. /* avoid "bootd" recursion */
  1149. if (cmdtp->cmd == do_bootd) {
  1150. #ifdef DEBUG_PARSER
  1151. printf ("[%s]\n", finaltoken);
  1152. #endif
  1153. if (flag & CMD_FLAG_BOOTD) {
  1154. puts ("'bootd' recursion detected\n");
  1155. rc = -1;
  1156. continue;
  1157. } else {
  1158. flag |= CMD_FLAG_BOOTD;
  1159. }
  1160. }
  1161. #endif
  1162. /* OK - call function to do the command */
  1163. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1164. rc = -1;
  1165. }
  1166. repeatable &= cmdtp->repeatable;
  1167. /* Did the user stop this? */
  1168. if (had_ctrlc ())
  1169. return -1; /* if stopped then not repeatable */
  1170. }
  1171. return rc ? rc : repeatable;
  1172. }
  1173. /****************************************************************************/
  1174. #if defined(CONFIG_CMD_RUN)
  1175. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
  1176. {
  1177. int i;
  1178. if (argc < 2) {
  1179. printf ("Usage:\n%s\n", cmdtp->usage);
  1180. return 1;
  1181. }
  1182. for (i=1; i<argc; ++i) {
  1183. char *arg;
  1184. if ((arg = getenv (argv[i])) == NULL) {
  1185. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1186. return 1;
  1187. }
  1188. #ifndef CFG_HUSH_PARSER
  1189. if (run_command (arg, flag) == -1)
  1190. return 1;
  1191. #else
  1192. if (parse_string_outer(arg,
  1193. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  1194. return 1;
  1195. #endif
  1196. }
  1197. return 0;
  1198. }
  1199. #endif