main.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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 CTL_BACKSPACE ('\b')
  442. #define DEL ((char)255)
  443. #define DEL7 ((char)127)
  444. #define CREAD_HIST_CHAR ('!')
  445. #define getcmd_putch(ch) putc(ch)
  446. #define getcmd_getch() getc()
  447. #define getcmd_cbeep() getcmd_putch('\a')
  448. #define HIST_MAX 20
  449. #define HIST_SIZE CONFIG_SYS_CBSIZE
  450. static int hist_max = 0;
  451. static int hist_add_idx = 0;
  452. static int hist_cur = -1;
  453. unsigned hist_num = 0;
  454. char* hist_list[HIST_MAX];
  455. char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
  456. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  457. static void hist_init(void)
  458. {
  459. int i;
  460. hist_max = 0;
  461. hist_add_idx = 0;
  462. hist_cur = -1;
  463. hist_num = 0;
  464. for (i = 0; i < HIST_MAX; i++) {
  465. hist_list[i] = hist_lines[i];
  466. hist_list[i][0] = '\0';
  467. }
  468. }
  469. static void cread_add_to_hist(char *line)
  470. {
  471. strcpy(hist_list[hist_add_idx], line);
  472. if (++hist_add_idx >= HIST_MAX)
  473. hist_add_idx = 0;
  474. if (hist_add_idx > hist_max)
  475. hist_max = hist_add_idx;
  476. hist_num++;
  477. }
  478. static char* hist_prev(void)
  479. {
  480. char *ret;
  481. int old_cur;
  482. if (hist_cur < 0)
  483. return NULL;
  484. old_cur = hist_cur;
  485. if (--hist_cur < 0)
  486. hist_cur = hist_max;
  487. if (hist_cur == hist_add_idx) {
  488. hist_cur = old_cur;
  489. ret = NULL;
  490. } else
  491. ret = hist_list[hist_cur];
  492. return (ret);
  493. }
  494. static char* hist_next(void)
  495. {
  496. char *ret;
  497. if (hist_cur < 0)
  498. return NULL;
  499. if (hist_cur == hist_add_idx)
  500. return NULL;
  501. if (++hist_cur > hist_max)
  502. hist_cur = 0;
  503. if (hist_cur == hist_add_idx) {
  504. ret = "";
  505. } else
  506. ret = hist_list[hist_cur];
  507. return (ret);
  508. }
  509. #ifndef CONFIG_CMDLINE_EDITING
  510. static void cread_print_hist_list(void)
  511. {
  512. int i;
  513. unsigned long n;
  514. n = hist_num - hist_max;
  515. i = hist_add_idx + 1;
  516. while (1) {
  517. if (i > hist_max)
  518. i = 0;
  519. if (i == hist_add_idx)
  520. break;
  521. printf("%s\n", hist_list[i]);
  522. n++;
  523. i++;
  524. }
  525. }
  526. #endif /* CONFIG_CMDLINE_EDITING */
  527. #define BEGINNING_OF_LINE() { \
  528. while (num) { \
  529. getcmd_putch(CTL_BACKSPACE); \
  530. num--; \
  531. } \
  532. }
  533. #define ERASE_TO_EOL() { \
  534. if (num < eol_num) { \
  535. printf("%*s", (int)(eol_num - num), ""); \
  536. do { \
  537. getcmd_putch(CTL_BACKSPACE); \
  538. } while (--eol_num > num); \
  539. } \
  540. }
  541. #define REFRESH_TO_EOL() { \
  542. if (num < eol_num) { \
  543. wlen = eol_num - num; \
  544. putnstr(buf + num, wlen); \
  545. num = eol_num; \
  546. } \
  547. }
  548. static void cread_add_char(char ichar, int insert, unsigned long *num,
  549. unsigned long *eol_num, char *buf, unsigned long len)
  550. {
  551. unsigned long wlen;
  552. /* room ??? */
  553. if (insert || *num == *eol_num) {
  554. if (*eol_num > len - 1) {
  555. getcmd_cbeep();
  556. return;
  557. }
  558. (*eol_num)++;
  559. }
  560. if (insert) {
  561. wlen = *eol_num - *num;
  562. if (wlen > 1) {
  563. memmove(&buf[*num+1], &buf[*num], wlen-1);
  564. }
  565. buf[*num] = ichar;
  566. putnstr(buf + *num, wlen);
  567. (*num)++;
  568. while (--wlen) {
  569. getcmd_putch(CTL_BACKSPACE);
  570. }
  571. } else {
  572. /* echo the character */
  573. wlen = 1;
  574. buf[*num] = ichar;
  575. putnstr(buf + *num, wlen);
  576. (*num)++;
  577. }
  578. }
  579. static void cread_add_str(char *str, int strsize, int insert, unsigned long *num,
  580. unsigned long *eol_num, char *buf, unsigned long len)
  581. {
  582. while (strsize--) {
  583. cread_add_char(*str, insert, num, eol_num, buf, len);
  584. str++;
  585. }
  586. }
  587. static int cread_line(const char *const prompt, char *buf, unsigned int *len)
  588. {
  589. unsigned long num = 0;
  590. unsigned long eol_num = 0;
  591. unsigned long wlen;
  592. char ichar;
  593. int insert = 1;
  594. int esc_len = 0;
  595. char esc_save[8];
  596. int init_len = strlen(buf);
  597. if (init_len)
  598. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  599. while (1) {
  600. #ifdef CONFIG_BOOT_RETRY_TIME
  601. while (!tstc()) { /* while no incoming data */
  602. if (retry_time >= 0 && get_ticks() > endtime)
  603. return (-2); /* timed out */
  604. WATCHDOG_RESET();
  605. }
  606. #endif
  607. ichar = getcmd_getch();
  608. if ((ichar == '\n') || (ichar == '\r')) {
  609. putc('\n');
  610. break;
  611. }
  612. /*
  613. * handle standard linux xterm esc sequences for arrow key, etc.
  614. */
  615. if (esc_len != 0) {
  616. if (esc_len == 1) {
  617. if (ichar == '[') {
  618. esc_save[esc_len] = ichar;
  619. esc_len = 2;
  620. } else {
  621. cread_add_str(esc_save, esc_len, insert,
  622. &num, &eol_num, buf, *len);
  623. esc_len = 0;
  624. }
  625. continue;
  626. }
  627. switch (ichar) {
  628. case 'D': /* <- key */
  629. ichar = CTL_CH('b');
  630. esc_len = 0;
  631. break;
  632. case 'C': /* -> key */
  633. ichar = CTL_CH('f');
  634. esc_len = 0;
  635. break; /* pass off to ^F handler */
  636. case 'H': /* Home key */
  637. ichar = CTL_CH('a');
  638. esc_len = 0;
  639. break; /* pass off to ^A handler */
  640. case 'A': /* up arrow */
  641. ichar = CTL_CH('p');
  642. esc_len = 0;
  643. break; /* pass off to ^P handler */
  644. case 'B': /* down arrow */
  645. ichar = CTL_CH('n');
  646. esc_len = 0;
  647. break; /* pass off to ^N handler */
  648. default:
  649. esc_save[esc_len++] = ichar;
  650. cread_add_str(esc_save, esc_len, insert,
  651. &num, &eol_num, buf, *len);
  652. esc_len = 0;
  653. continue;
  654. }
  655. }
  656. switch (ichar) {
  657. case 0x1b:
  658. if (esc_len == 0) {
  659. esc_save[esc_len] = ichar;
  660. esc_len = 1;
  661. } else {
  662. puts("impossible condition #876\n");
  663. esc_len = 0;
  664. }
  665. break;
  666. case CTL_CH('a'):
  667. BEGINNING_OF_LINE();
  668. break;
  669. case CTL_CH('c'): /* ^C - break */
  670. *buf = '\0'; /* discard input */
  671. return (-1);
  672. case CTL_CH('f'):
  673. if (num < eol_num) {
  674. getcmd_putch(buf[num]);
  675. num++;
  676. }
  677. break;
  678. case CTL_CH('b'):
  679. if (num) {
  680. getcmd_putch(CTL_BACKSPACE);
  681. num--;
  682. }
  683. break;
  684. case CTL_CH('d'):
  685. if (num < eol_num) {
  686. wlen = eol_num - num - 1;
  687. if (wlen) {
  688. memmove(&buf[num], &buf[num+1], wlen);
  689. putnstr(buf + num, wlen);
  690. }
  691. getcmd_putch(' ');
  692. do {
  693. getcmd_putch(CTL_BACKSPACE);
  694. } while (wlen--);
  695. eol_num--;
  696. }
  697. break;
  698. case CTL_CH('k'):
  699. ERASE_TO_EOL();
  700. break;
  701. case CTL_CH('e'):
  702. REFRESH_TO_EOL();
  703. break;
  704. case CTL_CH('o'):
  705. insert = !insert;
  706. break;
  707. case CTL_CH('x'):
  708. case CTL_CH('u'):
  709. BEGINNING_OF_LINE();
  710. ERASE_TO_EOL();
  711. break;
  712. case DEL:
  713. case DEL7:
  714. case 8:
  715. if (num) {
  716. wlen = eol_num - num;
  717. num--;
  718. memmove(&buf[num], &buf[num+1], wlen);
  719. getcmd_putch(CTL_BACKSPACE);
  720. putnstr(buf + num, wlen);
  721. getcmd_putch(' ');
  722. do {
  723. getcmd_putch(CTL_BACKSPACE);
  724. } while (wlen--);
  725. eol_num--;
  726. }
  727. break;
  728. case CTL_CH('p'):
  729. case CTL_CH('n'):
  730. {
  731. char * hline;
  732. esc_len = 0;
  733. if (ichar == CTL_CH('p'))
  734. hline = hist_prev();
  735. else
  736. hline = hist_next();
  737. if (!hline) {
  738. getcmd_cbeep();
  739. continue;
  740. }
  741. /* nuke the current line */
  742. /* first, go home */
  743. BEGINNING_OF_LINE();
  744. /* erase to end of line */
  745. ERASE_TO_EOL();
  746. /* copy new line into place and display */
  747. strcpy(buf, hline);
  748. eol_num = strlen(buf);
  749. REFRESH_TO_EOL();
  750. continue;
  751. }
  752. #ifdef CONFIG_AUTO_COMPLETE
  753. case '\t': {
  754. int num2, col;
  755. /* do not autocomplete when in the middle */
  756. if (num < eol_num) {
  757. getcmd_cbeep();
  758. break;
  759. }
  760. buf[num] = '\0';
  761. col = strlen(prompt) + eol_num;
  762. num2 = num;
  763. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  764. col = num2 - num;
  765. num += col;
  766. eol_num += col;
  767. }
  768. break;
  769. }
  770. #endif
  771. default:
  772. cread_add_char(ichar, insert, &num, &eol_num, buf, *len);
  773. break;
  774. }
  775. }
  776. *len = eol_num;
  777. buf[eol_num] = '\0'; /* lose the newline */
  778. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  779. cread_add_to_hist(buf);
  780. hist_cur = hist_add_idx;
  781. return 0;
  782. }
  783. #endif /* CONFIG_CMDLINE_EDITING */
  784. /****************************************************************************/
  785. /*
  786. * Prompt for input and read a line.
  787. * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0,
  788. * time out when time goes past endtime (timebase time in ticks).
  789. * Return: number of read characters
  790. * -1 if break
  791. * -2 if timed out
  792. */
  793. int readline (const char *const prompt)
  794. {
  795. /*
  796. * If console_buffer isn't 0-length the user will be prompted to modify
  797. * it instead of entering it from scratch as desired.
  798. */
  799. console_buffer[0] = '\0';
  800. return readline_into_buffer(prompt, console_buffer);
  801. }
  802. int readline_into_buffer (const char *const prompt, char * buffer)
  803. {
  804. char *p = buffer;
  805. #ifdef CONFIG_CMDLINE_EDITING
  806. unsigned int len = CONFIG_SYS_CBSIZE;
  807. int rc;
  808. static int initted = 0;
  809. /*
  810. * History uses a global array which is not
  811. * writable until after relocation to RAM.
  812. * Revert to non-history version if still
  813. * running from flash.
  814. */
  815. if (gd->flags & GD_FLG_RELOC) {
  816. if (!initted) {
  817. hist_init();
  818. initted = 1;
  819. }
  820. if (prompt)
  821. puts (prompt);
  822. rc = cread_line(prompt, p, &len);
  823. return rc < 0 ? rc : len;
  824. } else {
  825. #endif /* CONFIG_CMDLINE_EDITING */
  826. char * p_buf = p;
  827. int n = 0; /* buffer index */
  828. int plen = 0; /* prompt length */
  829. int col; /* output column cnt */
  830. char c;
  831. /* print prompt */
  832. if (prompt) {
  833. plen = strlen (prompt);
  834. puts (prompt);
  835. }
  836. col = plen;
  837. for (;;) {
  838. #ifdef CONFIG_BOOT_RETRY_TIME
  839. while (!tstc()) { /* while no incoming data */
  840. if (retry_time >= 0 && get_ticks() > endtime)
  841. return (-2); /* timed out */
  842. WATCHDOG_RESET();
  843. }
  844. #endif
  845. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  846. #ifdef CONFIG_SHOW_ACTIVITY
  847. while (!tstc()) {
  848. extern void show_activity(int arg);
  849. show_activity(0);
  850. WATCHDOG_RESET();
  851. }
  852. #endif
  853. c = getc();
  854. /*
  855. * Special character handling
  856. */
  857. switch (c) {
  858. case '\r': /* Enter */
  859. case '\n':
  860. *p = '\0';
  861. puts ("\r\n");
  862. return (p - p_buf);
  863. case '\0': /* nul */
  864. continue;
  865. case 0x03: /* ^C - break */
  866. p_buf[0] = '\0'; /* discard input */
  867. return (-1);
  868. case 0x15: /* ^U - erase line */
  869. while (col > plen) {
  870. puts (erase_seq);
  871. --col;
  872. }
  873. p = p_buf;
  874. n = 0;
  875. continue;
  876. case 0x17: /* ^W - erase word */
  877. p=delete_char(p_buf, p, &col, &n, plen);
  878. while ((n > 0) && (*p != ' ')) {
  879. p=delete_char(p_buf, p, &col, &n, plen);
  880. }
  881. continue;
  882. case 0x08: /* ^H - backspace */
  883. case 0x7F: /* DEL - backspace */
  884. p=delete_char(p_buf, p, &col, &n, plen);
  885. continue;
  886. default:
  887. /*
  888. * Must be a normal character then
  889. */
  890. if (n < CONFIG_SYS_CBSIZE-2) {
  891. if (c == '\t') { /* expand TABs */
  892. #ifdef CONFIG_AUTO_COMPLETE
  893. /* if auto completion triggered just continue */
  894. *p = '\0';
  895. if (cmd_auto_complete(prompt, console_buffer, &n, &col)) {
  896. p = p_buf + n; /* reset */
  897. continue;
  898. }
  899. #endif
  900. puts (tab_seq+(col&07));
  901. col += 8 - (col&07);
  902. } else {
  903. ++col; /* echo input */
  904. putc (c);
  905. }
  906. *p++ = c;
  907. ++n;
  908. } else { /* Buffer full */
  909. putc ('\a');
  910. }
  911. }
  912. }
  913. #ifdef CONFIG_CMDLINE_EDITING
  914. }
  915. #endif
  916. }
  917. /****************************************************************************/
  918. static char * delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  919. {
  920. char *s;
  921. if (*np == 0) {
  922. return (p);
  923. }
  924. if (*(--p) == '\t') { /* will retype the whole line */
  925. while (*colp > plen) {
  926. puts (erase_seq);
  927. (*colp)--;
  928. }
  929. for (s=buffer; s<p; ++s) {
  930. if (*s == '\t') {
  931. puts (tab_seq+((*colp) & 07));
  932. *colp += 8 - ((*colp) & 07);
  933. } else {
  934. ++(*colp);
  935. putc (*s);
  936. }
  937. }
  938. } else {
  939. puts (erase_seq);
  940. (*colp)--;
  941. }
  942. (*np)--;
  943. return (p);
  944. }
  945. /****************************************************************************/
  946. int parse_line (char *line, char *argv[])
  947. {
  948. int nargs = 0;
  949. #ifdef DEBUG_PARSER
  950. printf ("parse_line: \"%s\"\n", line);
  951. #endif
  952. while (nargs < CONFIG_SYS_MAXARGS) {
  953. /* skip any white space */
  954. while ((*line == ' ') || (*line == '\t')) {
  955. ++line;
  956. }
  957. if (*line == '\0') { /* end of line, no more args */
  958. argv[nargs] = NULL;
  959. #ifdef DEBUG_PARSER
  960. printf ("parse_line: nargs=%d\n", nargs);
  961. #endif
  962. return (nargs);
  963. }
  964. argv[nargs++] = line; /* begin of argument string */
  965. /* find end of string */
  966. while (*line && (*line != ' ') && (*line != '\t')) {
  967. ++line;
  968. }
  969. if (*line == '\0') { /* end of line, no more args */
  970. argv[nargs] = NULL;
  971. #ifdef DEBUG_PARSER
  972. printf ("parse_line: nargs=%d\n", nargs);
  973. #endif
  974. return (nargs);
  975. }
  976. *line++ = '\0'; /* terminate current arg */
  977. }
  978. printf ("** Too many args (max. %d) **\n", CONFIG_SYS_MAXARGS);
  979. #ifdef DEBUG_PARSER
  980. printf ("parse_line: nargs=%d\n", nargs);
  981. #endif
  982. return (nargs);
  983. }
  984. /****************************************************************************/
  985. static void process_macros (const char *input, char *output)
  986. {
  987. char c, prev;
  988. const char *varname_start = NULL;
  989. int inputcnt = strlen (input);
  990. int outputcnt = CONFIG_SYS_CBSIZE;
  991. int state = 0; /* 0 = waiting for '$' */
  992. /* 1 = waiting for '(' or '{' */
  993. /* 2 = waiting for ')' or '}' */
  994. /* 3 = waiting for ''' */
  995. #ifdef DEBUG_PARSER
  996. char *output_start = output;
  997. printf ("[PROCESS_MACROS] INPUT len %d: \"%s\"\n", strlen (input),
  998. input);
  999. #endif
  1000. prev = '\0'; /* previous character */
  1001. while (inputcnt && outputcnt) {
  1002. c = *input++;
  1003. inputcnt--;
  1004. if (state != 3) {
  1005. /* remove one level of escape characters */
  1006. if ((c == '\\') && (prev != '\\')) {
  1007. if (inputcnt-- == 0)
  1008. break;
  1009. prev = c;
  1010. c = *input++;
  1011. }
  1012. }
  1013. switch (state) {
  1014. case 0: /* Waiting for (unescaped) $ */
  1015. if ((c == '\'') && (prev != '\\')) {
  1016. state = 3;
  1017. break;
  1018. }
  1019. if ((c == '$') && (prev != '\\')) {
  1020. state++;
  1021. } else {
  1022. *(output++) = c;
  1023. outputcnt--;
  1024. }
  1025. break;
  1026. case 1: /* Waiting for ( */
  1027. if (c == '(' || c == '{') {
  1028. state++;
  1029. varname_start = input;
  1030. } else {
  1031. state = 0;
  1032. *(output++) = '$';
  1033. outputcnt--;
  1034. if (outputcnt) {
  1035. *(output++) = c;
  1036. outputcnt--;
  1037. }
  1038. }
  1039. break;
  1040. case 2: /* Waiting for ) */
  1041. if (c == ')' || c == '}') {
  1042. int i;
  1043. char envname[CONFIG_SYS_CBSIZE], *envval;
  1044. int envcnt = input - varname_start - 1; /* Varname # of chars */
  1045. /* Get the varname */
  1046. for (i = 0; i < envcnt; i++) {
  1047. envname[i] = varname_start[i];
  1048. }
  1049. envname[i] = 0;
  1050. /* Get its value */
  1051. envval = getenv (envname);
  1052. /* Copy into the line if it exists */
  1053. if (envval != NULL)
  1054. while ((*envval) && outputcnt) {
  1055. *(output++) = *(envval++);
  1056. outputcnt--;
  1057. }
  1058. /* Look for another '$' */
  1059. state = 0;
  1060. }
  1061. break;
  1062. case 3: /* Waiting for ' */
  1063. if ((c == '\'') && (prev != '\\')) {
  1064. state = 0;
  1065. } else {
  1066. *(output++) = c;
  1067. outputcnt--;
  1068. }
  1069. break;
  1070. }
  1071. prev = c;
  1072. }
  1073. if (outputcnt)
  1074. *output = 0;
  1075. else
  1076. *(output - 1) = 0;
  1077. #ifdef DEBUG_PARSER
  1078. printf ("[PROCESS_MACROS] OUTPUT len %d: \"%s\"\n",
  1079. strlen (output_start), output_start);
  1080. #endif
  1081. }
  1082. /****************************************************************************
  1083. * returns:
  1084. * 1 - command executed, repeatable
  1085. * 0 - command executed but not repeatable, interrupted commands are
  1086. * always considered not repeatable
  1087. * -1 - not executed (unrecognized, bootd recursion or too many args)
  1088. * (If cmd is NULL or "" or longer than CONFIG_SYS_CBSIZE-1 it is
  1089. * considered unrecognized)
  1090. *
  1091. * WARNING:
  1092. *
  1093. * We must create a temporary copy of the command since the command we get
  1094. * may be the result from getenv(), which returns a pointer directly to
  1095. * the environment data, which may change magicly when the command we run
  1096. * creates or modifies environment variables (like "bootp" does).
  1097. */
  1098. int run_command (const char *cmd, int flag)
  1099. {
  1100. cmd_tbl_t *cmdtp;
  1101. char cmdbuf[CONFIG_SYS_CBSIZE]; /* working copy of cmd */
  1102. char *token; /* start of token in cmdbuf */
  1103. char *sep; /* end of token (separator) in cmdbuf */
  1104. char finaltoken[CONFIG_SYS_CBSIZE];
  1105. char *str = cmdbuf;
  1106. char *argv[CONFIG_SYS_MAXARGS + 1]; /* NULL terminated */
  1107. int argc, inquotes;
  1108. int repeatable = 1;
  1109. int rc = 0;
  1110. #ifdef DEBUG_PARSER
  1111. printf ("[RUN_COMMAND] cmd[%p]=\"", cmd);
  1112. puts (cmd ? cmd : "NULL"); /* use puts - string may be loooong */
  1113. puts ("\"\n");
  1114. #endif
  1115. clear_ctrlc(); /* forget any previous Control C */
  1116. if (!cmd || !*cmd) {
  1117. return -1; /* empty command */
  1118. }
  1119. if (strlen(cmd) >= CONFIG_SYS_CBSIZE) {
  1120. puts ("## Command too long!\n");
  1121. return -1;
  1122. }
  1123. strcpy (cmdbuf, cmd);
  1124. /* Process separators and check for invalid
  1125. * repeatable commands
  1126. */
  1127. #ifdef DEBUG_PARSER
  1128. printf ("[PROCESS_SEPARATORS] %s\n", cmd);
  1129. #endif
  1130. while (*str) {
  1131. /*
  1132. * Find separator, or string end
  1133. * Allow simple escape of ';' by writing "\;"
  1134. */
  1135. for (inquotes = 0, sep = str; *sep; sep++) {
  1136. if ((*sep=='\'') &&
  1137. (*(sep-1) != '\\'))
  1138. inquotes=!inquotes;
  1139. if (!inquotes &&
  1140. (*sep == ';') && /* separator */
  1141. ( sep != str) && /* past string start */
  1142. (*(sep-1) != '\\')) /* and NOT escaped */
  1143. break;
  1144. }
  1145. /*
  1146. * Limit the token to data between separators
  1147. */
  1148. token = str;
  1149. if (*sep) {
  1150. str = sep + 1; /* start of command for next pass */
  1151. *sep = '\0';
  1152. }
  1153. else
  1154. str = sep; /* no more commands for next pass */
  1155. #ifdef DEBUG_PARSER
  1156. printf ("token: \"%s\"\n", token);
  1157. #endif
  1158. /* find macros in this token and replace them */
  1159. process_macros (token, finaltoken);
  1160. /* Extract arguments */
  1161. if ((argc = parse_line (finaltoken, argv)) == 0) {
  1162. rc = -1; /* no command at all */
  1163. continue;
  1164. }
  1165. /* Look up command in command table */
  1166. if ((cmdtp = find_cmd(argv[0])) == NULL) {
  1167. printf ("Unknown command '%s' - try 'help'\n", argv[0]);
  1168. rc = -1; /* give up after bad command */
  1169. continue;
  1170. }
  1171. /* found - check max args */
  1172. if (argc > cmdtp->maxargs) {
  1173. cmd_usage(cmdtp);
  1174. rc = -1;
  1175. continue;
  1176. }
  1177. #if defined(CONFIG_CMD_BOOTD)
  1178. /* avoid "bootd" recursion */
  1179. if (cmdtp->cmd == do_bootd) {
  1180. #ifdef DEBUG_PARSER
  1181. printf ("[%s]\n", finaltoken);
  1182. #endif
  1183. if (flag & CMD_FLAG_BOOTD) {
  1184. puts ("'bootd' recursion detected\n");
  1185. rc = -1;
  1186. continue;
  1187. } else {
  1188. flag |= CMD_FLAG_BOOTD;
  1189. }
  1190. }
  1191. #endif
  1192. /* OK - call function to do the command */
  1193. if ((cmdtp->cmd) (cmdtp, flag, argc, argv) != 0) {
  1194. rc = -1;
  1195. }
  1196. repeatable &= cmdtp->repeatable;
  1197. /* Did the user stop this? */
  1198. if (had_ctrlc ())
  1199. return -1; /* if stopped then not repeatable */
  1200. }
  1201. return rc ? rc : repeatable;
  1202. }
  1203. /****************************************************************************/
  1204. #if defined(CONFIG_CMD_RUN)
  1205. int do_run (cmd_tbl_t * cmdtp, int flag, int argc, char * const argv[])
  1206. {
  1207. int i;
  1208. if (argc < 2)
  1209. return cmd_usage(cmdtp);
  1210. for (i=1; i<argc; ++i) {
  1211. char *arg;
  1212. if ((arg = getenv (argv[i])) == NULL) {
  1213. printf ("## Error: \"%s\" not defined\n", argv[i]);
  1214. return 1;
  1215. }
  1216. #ifndef CONFIG_SYS_HUSH_PARSER
  1217. if (run_command (arg, flag) == -1)
  1218. return 1;
  1219. #else
  1220. if (parse_string_outer(arg,
  1221. FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
  1222. return 1;
  1223. #endif
  1224. }
  1225. return 0;
  1226. }
  1227. #endif