main.c 30 KB

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