main.c 32 KB

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