cli_readline.c 13 KB

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