main.c 29 KB

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