main.c 32 KB

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