cli_readline.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2000
  4. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  5. *
  6. * Add to readline cmdline-editing by
  7. * (C) Copyright 2005
  8. * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
  9. */
  10. #include <common.h>
  11. #include <bootretry.h>
  12. #include <cli.h>
  13. #include <command.h>
  14. #include <time.h>
  15. #include <watchdog.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. static const char erase_seq[] = "\b \b"; /* erase sequence */
  18. static const char tab_seq[] = " "; /* used to expand TABs */
  19. char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
  20. static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
  21. {
  22. char *s;
  23. if (*np == 0)
  24. return p;
  25. if (*(--p) == '\t') { /* will retype the whole line */
  26. while (*colp > plen) {
  27. puts(erase_seq);
  28. (*colp)--;
  29. }
  30. for (s = buffer; s < p; ++s) {
  31. if (*s == '\t') {
  32. puts(tab_seq + ((*colp) & 07));
  33. *colp += 8 - ((*colp) & 07);
  34. } else {
  35. ++(*colp);
  36. putc(*s);
  37. }
  38. }
  39. } else {
  40. puts(erase_seq);
  41. (*colp)--;
  42. }
  43. (*np)--;
  44. return p;
  45. }
  46. #ifdef CONFIG_CMDLINE_EDITING
  47. /*
  48. * cmdline-editing related codes from vivi.
  49. * Author: Janghoon Lyu <nandy@mizi.com>
  50. */
  51. #define putnstr(str, n) printf("%.*s", (int)n, str)
  52. #define CTL_CH(c) ((c) - 'a' + 1)
  53. #define CTL_BACKSPACE ('\b')
  54. #define DEL ((char)255)
  55. #define DEL7 ((char)127)
  56. #define CREAD_HIST_CHAR ('!')
  57. #define getcmd_putch(ch) putc(ch)
  58. #define getcmd_getch() getc()
  59. #define getcmd_cbeep() getcmd_putch('\a')
  60. #define HIST_MAX 20
  61. #define HIST_SIZE CONFIG_SYS_CBSIZE
  62. static int hist_max;
  63. static int hist_add_idx;
  64. static int hist_cur = -1;
  65. static unsigned hist_num;
  66. static char *hist_list[HIST_MAX];
  67. static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
  68. #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
  69. static void hist_init(void)
  70. {
  71. int i;
  72. hist_max = 0;
  73. hist_add_idx = 0;
  74. hist_cur = -1;
  75. hist_num = 0;
  76. for (i = 0; i < HIST_MAX; i++) {
  77. hist_list[i] = hist_lines[i];
  78. hist_list[i][0] = '\0';
  79. }
  80. }
  81. static void cread_add_to_hist(char *line)
  82. {
  83. strcpy(hist_list[hist_add_idx], line);
  84. if (++hist_add_idx >= HIST_MAX)
  85. hist_add_idx = 0;
  86. if (hist_add_idx > hist_max)
  87. hist_max = hist_add_idx;
  88. hist_num++;
  89. }
  90. static char *hist_prev(void)
  91. {
  92. char *ret;
  93. int old_cur;
  94. if (hist_cur < 0)
  95. return NULL;
  96. old_cur = hist_cur;
  97. if (--hist_cur < 0)
  98. hist_cur = hist_max;
  99. if (hist_cur == hist_add_idx) {
  100. hist_cur = old_cur;
  101. ret = NULL;
  102. } else {
  103. ret = hist_list[hist_cur];
  104. }
  105. return ret;
  106. }
  107. static char *hist_next(void)
  108. {
  109. char *ret;
  110. if (hist_cur < 0)
  111. return NULL;
  112. if (hist_cur == hist_add_idx)
  113. return NULL;
  114. if (++hist_cur > hist_max)
  115. hist_cur = 0;
  116. if (hist_cur == hist_add_idx)
  117. ret = "";
  118. else
  119. ret = hist_list[hist_cur];
  120. return ret;
  121. }
  122. #ifndef CONFIG_CMDLINE_EDITING
  123. static void cread_print_hist_list(void)
  124. {
  125. int i;
  126. unsigned long n;
  127. n = hist_num - hist_max;
  128. i = hist_add_idx + 1;
  129. while (1) {
  130. if (i > hist_max)
  131. i = 0;
  132. if (i == hist_add_idx)
  133. break;
  134. printf("%s\n", hist_list[i]);
  135. n++;
  136. i++;
  137. }
  138. }
  139. #endif /* CONFIG_CMDLINE_EDITING */
  140. #define BEGINNING_OF_LINE() { \
  141. while (num) { \
  142. getcmd_putch(CTL_BACKSPACE); \
  143. num--; \
  144. } \
  145. }
  146. #define ERASE_TO_EOL() { \
  147. if (num < eol_num) { \
  148. printf("%*s", (int)(eol_num - num), ""); \
  149. do { \
  150. getcmd_putch(CTL_BACKSPACE); \
  151. } while (--eol_num > num); \
  152. } \
  153. }
  154. #define REFRESH_TO_EOL() { \
  155. if (num < eol_num) { \
  156. wlen = eol_num - num; \
  157. putnstr(buf + num, wlen); \
  158. num = eol_num; \
  159. } \
  160. }
  161. static void cread_add_char(char ichar, int insert, unsigned long *num,
  162. unsigned long *eol_num, char *buf, unsigned long len)
  163. {
  164. unsigned long wlen;
  165. /* room ??? */
  166. if (insert || *num == *eol_num) {
  167. if (*eol_num > len - 1) {
  168. getcmd_cbeep();
  169. return;
  170. }
  171. (*eol_num)++;
  172. }
  173. if (insert) {
  174. wlen = *eol_num - *num;
  175. if (wlen > 1)
  176. memmove(&buf[*num+1], &buf[*num], wlen-1);
  177. buf[*num] = ichar;
  178. putnstr(buf + *num, wlen);
  179. (*num)++;
  180. while (--wlen)
  181. getcmd_putch(CTL_BACKSPACE);
  182. } else {
  183. /* echo the character */
  184. wlen = 1;
  185. buf[*num] = ichar;
  186. putnstr(buf + *num, wlen);
  187. (*num)++;
  188. }
  189. }
  190. static void cread_add_str(char *str, int strsize, int insert,
  191. unsigned long *num, unsigned long *eol_num,
  192. char *buf, unsigned long len)
  193. {
  194. while (strsize--) {
  195. cread_add_char(*str, insert, num, eol_num, buf, len);
  196. str++;
  197. }
  198. }
  199. static int cread_line(const char *const prompt, char *buf, unsigned int *len,
  200. int timeout)
  201. {
  202. unsigned long num = 0;
  203. unsigned long eol_num = 0;
  204. unsigned long wlen;
  205. char ichar;
  206. int insert = 1;
  207. int esc_len = 0;
  208. char esc_save[8];
  209. int init_len = strlen(buf);
  210. int first = 1;
  211. if (init_len)
  212. cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
  213. while (1) {
  214. if (bootretry_tstc_timeout())
  215. return -2; /* timed out */
  216. if (first && timeout) {
  217. uint64_t etime = endtick(timeout);
  218. while (!tstc()) { /* while no incoming data */
  219. if (get_ticks() >= etime)
  220. return -2; /* timed out */
  221. WATCHDOG_RESET();
  222. }
  223. first = 0;
  224. }
  225. ichar = getcmd_getch();
  226. /* ichar=0x0 when error occurs in U-Boot getc */
  227. if (!ichar)
  228. continue;
  229. if ((ichar == '\n') || (ichar == '\r')) {
  230. putc('\n');
  231. break;
  232. }
  233. /*
  234. * handle standard linux xterm esc sequences for arrow key, etc.
  235. */
  236. if (esc_len != 0) {
  237. enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
  238. if (esc_len == 1) {
  239. if (ichar == '[' || ichar == 'O')
  240. act = ESC_SAVE;
  241. } else if (esc_len == 2) {
  242. switch (ichar) {
  243. case 'D': /* <- key */
  244. ichar = CTL_CH('b');
  245. act = ESC_CONVERTED;
  246. break; /* pass off to ^B handler */
  247. case 'C': /* -> key */
  248. ichar = CTL_CH('f');
  249. act = ESC_CONVERTED;
  250. break; /* pass off to ^F handler */
  251. case 'H': /* Home key */
  252. ichar = CTL_CH('a');
  253. act = ESC_CONVERTED;
  254. break; /* pass off to ^A handler */
  255. case 'F': /* End key */
  256. ichar = CTL_CH('e');
  257. act = ESC_CONVERTED;
  258. break; /* pass off to ^E handler */
  259. case 'A': /* up arrow */
  260. ichar = CTL_CH('p');
  261. act = ESC_CONVERTED;
  262. break; /* pass off to ^P handler */
  263. case 'B': /* down arrow */
  264. ichar = CTL_CH('n');
  265. act = ESC_CONVERTED;
  266. break; /* pass off to ^N handler */
  267. case '1':
  268. case '3':
  269. case '4':
  270. case '7':
  271. case '8':
  272. if (esc_save[1] == '[') {
  273. /* see if next character is ~ */
  274. act = ESC_SAVE;
  275. }
  276. break;
  277. }
  278. } else if (esc_len == 3) {
  279. if (ichar == '~') {
  280. switch (esc_save[2]) {
  281. case '3': /* Delete key */
  282. ichar = CTL_CH('d');
  283. act = ESC_CONVERTED;
  284. break; /* pass to ^D handler */
  285. case '1': /* Home key */
  286. case '7':
  287. ichar = CTL_CH('a');
  288. act = ESC_CONVERTED;
  289. break; /* pass to ^A handler */
  290. case '4': /* End key */
  291. case '8':
  292. ichar = CTL_CH('e');
  293. act = ESC_CONVERTED;
  294. break; /* pass to ^E handler */
  295. }
  296. }
  297. }
  298. switch (act) {
  299. case ESC_SAVE:
  300. esc_save[esc_len++] = ichar;
  301. continue;
  302. case ESC_REJECT:
  303. esc_save[esc_len++] = ichar;
  304. cread_add_str(esc_save, esc_len, insert,
  305. &num, &eol_num, buf, *len);
  306. esc_len = 0;
  307. continue;
  308. case ESC_CONVERTED:
  309. esc_len = 0;
  310. break;
  311. }
  312. }
  313. switch (ichar) {
  314. case 0x1b:
  315. if (esc_len == 0) {
  316. esc_save[esc_len] = ichar;
  317. esc_len = 1;
  318. } else {
  319. puts("impossible condition #876\n");
  320. esc_len = 0;
  321. }
  322. break;
  323. case CTL_CH('a'):
  324. BEGINNING_OF_LINE();
  325. break;
  326. case CTL_CH('c'): /* ^C - break */
  327. *buf = '\0'; /* discard input */
  328. return -1;
  329. case CTL_CH('f'):
  330. if (num < eol_num) {
  331. getcmd_putch(buf[num]);
  332. num++;
  333. }
  334. break;
  335. case CTL_CH('b'):
  336. if (num) {
  337. getcmd_putch(CTL_BACKSPACE);
  338. num--;
  339. }
  340. break;
  341. case CTL_CH('d'):
  342. if (num < eol_num) {
  343. wlen = eol_num - num - 1;
  344. if (wlen) {
  345. memmove(&buf[num], &buf[num+1], wlen);
  346. putnstr(buf + num, wlen);
  347. }
  348. getcmd_putch(' ');
  349. do {
  350. getcmd_putch(CTL_BACKSPACE);
  351. } while (wlen--);
  352. eol_num--;
  353. }
  354. break;
  355. case CTL_CH('k'):
  356. ERASE_TO_EOL();
  357. break;
  358. case CTL_CH('e'):
  359. REFRESH_TO_EOL();
  360. break;
  361. case CTL_CH('o'):
  362. insert = !insert;
  363. break;
  364. case CTL_CH('x'):
  365. case CTL_CH('u'):
  366. BEGINNING_OF_LINE();
  367. ERASE_TO_EOL();
  368. break;
  369. case DEL:
  370. case DEL7:
  371. case 8:
  372. if (num) {
  373. wlen = eol_num - num;
  374. num--;
  375. memmove(&buf[num], &buf[num+1], wlen);
  376. getcmd_putch(CTL_BACKSPACE);
  377. putnstr(buf + num, wlen);
  378. getcmd_putch(' ');
  379. do {
  380. getcmd_putch(CTL_BACKSPACE);
  381. } while (wlen--);
  382. eol_num--;
  383. }
  384. break;
  385. case CTL_CH('p'):
  386. case CTL_CH('n'):
  387. {
  388. char *hline;
  389. esc_len = 0;
  390. if (ichar == CTL_CH('p'))
  391. hline = hist_prev();
  392. else
  393. hline = hist_next();
  394. if (!hline) {
  395. getcmd_cbeep();
  396. continue;
  397. }
  398. /* nuke the current line */
  399. /* first, go home */
  400. BEGINNING_OF_LINE();
  401. /* erase to end of line */
  402. ERASE_TO_EOL();
  403. /* copy new line into place and display */
  404. strcpy(buf, hline);
  405. eol_num = strlen(buf);
  406. REFRESH_TO_EOL();
  407. continue;
  408. }
  409. #ifdef CONFIG_AUTO_COMPLETE
  410. case '\t': {
  411. int num2, col;
  412. /* do not autocomplete when in the middle */
  413. if (num < eol_num) {
  414. getcmd_cbeep();
  415. break;
  416. }
  417. buf[num] = '\0';
  418. col = strlen(prompt) + eol_num;
  419. num2 = num;
  420. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  421. col = num2 - num;
  422. num += col;
  423. eol_num += col;
  424. }
  425. break;
  426. }
  427. #endif
  428. default:
  429. cread_add_char(ichar, insert, &num, &eol_num, buf,
  430. *len);
  431. break;
  432. }
  433. }
  434. *len = eol_num;
  435. buf[eol_num] = '\0'; /* lose the newline */
  436. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  437. cread_add_to_hist(buf);
  438. hist_cur = hist_add_idx;
  439. return 0;
  440. }
  441. #endif /* CONFIG_CMDLINE_EDITING */
  442. /****************************************************************************/
  443. int cli_readline(const char *const prompt)
  444. {
  445. /*
  446. * If console_buffer isn't 0-length the user will be prompted to modify
  447. * it instead of entering it from scratch as desired.
  448. */
  449. console_buffer[0] = '\0';
  450. return cli_readline_into_buffer(prompt, console_buffer, 0);
  451. }
  452. int cli_readline_into_buffer(const char *const prompt, char *buffer,
  453. int timeout)
  454. {
  455. char *p = buffer;
  456. #ifdef CONFIG_CMDLINE_EDITING
  457. unsigned int len = CONFIG_SYS_CBSIZE;
  458. int rc;
  459. static int initted;
  460. /*
  461. * History uses a global array which is not
  462. * writable until after relocation to RAM.
  463. * Revert to non-history version if still
  464. * running from flash.
  465. */
  466. if (gd->flags & GD_FLG_RELOC) {
  467. if (!initted) {
  468. hist_init();
  469. initted = 1;
  470. }
  471. if (prompt)
  472. puts(prompt);
  473. rc = cread_line(prompt, p, &len, timeout);
  474. return rc < 0 ? rc : len;
  475. } else {
  476. #endif /* CONFIG_CMDLINE_EDITING */
  477. char *p_buf = p;
  478. int n = 0; /* buffer index */
  479. int plen = 0; /* prompt length */
  480. int col; /* output column cnt */
  481. char c;
  482. /* print prompt */
  483. if (prompt) {
  484. plen = strlen(prompt);
  485. puts(prompt);
  486. }
  487. col = plen;
  488. for (;;) {
  489. if (bootretry_tstc_timeout())
  490. return -2; /* timed out */
  491. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  492. c = getc();
  493. /*
  494. * Special character handling
  495. */
  496. switch (c) {
  497. case '\r': /* Enter */
  498. case '\n':
  499. *p = '\0';
  500. puts("\r\n");
  501. return p - p_buf;
  502. case '\0': /* nul */
  503. continue;
  504. case 0x03: /* ^C - break */
  505. p_buf[0] = '\0'; /* discard input */
  506. return -1;
  507. case 0x15: /* ^U - erase line */
  508. while (col > plen) {
  509. puts(erase_seq);
  510. --col;
  511. }
  512. p = p_buf;
  513. n = 0;
  514. continue;
  515. case 0x17: /* ^W - erase word */
  516. p = delete_char(p_buf, p, &col, &n, plen);
  517. while ((n > 0) && (*p != ' '))
  518. p = delete_char(p_buf, p, &col, &n, plen);
  519. continue;
  520. case 0x08: /* ^H - backspace */
  521. case 0x7F: /* DEL - backspace */
  522. p = delete_char(p_buf, p, &col, &n, plen);
  523. continue;
  524. default:
  525. /*
  526. * Must be a normal character then
  527. */
  528. if (n < CONFIG_SYS_CBSIZE-2) {
  529. if (c == '\t') { /* expand TABs */
  530. #ifdef CONFIG_AUTO_COMPLETE
  531. /*
  532. * if auto completion triggered just
  533. * continue
  534. */
  535. *p = '\0';
  536. if (cmd_auto_complete(prompt,
  537. console_buffer,
  538. &n, &col)) {
  539. p = p_buf + n; /* reset */
  540. continue;
  541. }
  542. #endif
  543. puts(tab_seq + (col & 07));
  544. col += 8 - (col & 07);
  545. } else {
  546. char __maybe_unused buf[2];
  547. /*
  548. * Echo input using puts() to force an
  549. * LCD flush if we are using an LCD
  550. */
  551. ++col;
  552. buf[0] = c;
  553. buf[1] = '\0';
  554. puts(buf);
  555. }
  556. *p++ = c;
  557. ++n;
  558. } else { /* Buffer full */
  559. putc('\a');
  560. }
  561. }
  562. }
  563. #ifdef CONFIG_CMDLINE_EDITING
  564. }
  565. #endif
  566. }