main.c 31 KB

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