main.c 31 KB

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