cli_readline.c 12 KB

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