main.c 32 KB

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