main.c 31 KB

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