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 <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. if ((ichar == '\n') || (ichar == '\r')) {
  225. putc('\n');
  226. break;
  227. }
  228. /*
  229. * handle standard linux xterm esc sequences for arrow key, etc.
  230. */
  231. if (esc_len != 0) {
  232. enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
  233. if (esc_len == 1) {
  234. if (ichar == '[' || ichar == 'O')
  235. act = ESC_SAVE;
  236. } else if (esc_len == 2) {
  237. switch (ichar) {
  238. case 'D': /* <- key */
  239. ichar = CTL_CH('b');
  240. act = ESC_CONVERTED;
  241. break; /* pass off to ^B handler */
  242. case 'C': /* -> key */
  243. ichar = CTL_CH('f');
  244. act = ESC_CONVERTED;
  245. break; /* pass off to ^F handler */
  246. case 'H': /* Home key */
  247. ichar = CTL_CH('a');
  248. act = ESC_CONVERTED;
  249. break; /* pass off to ^A handler */
  250. case 'F': /* End key */
  251. ichar = CTL_CH('e');
  252. act = ESC_CONVERTED;
  253. break; /* pass off to ^E handler */
  254. case 'A': /* up arrow */
  255. ichar = CTL_CH('p');
  256. act = ESC_CONVERTED;
  257. break; /* pass off to ^P handler */
  258. case 'B': /* down arrow */
  259. ichar = CTL_CH('n');
  260. act = ESC_CONVERTED;
  261. break; /* pass off to ^N handler */
  262. case '1':
  263. case '3':
  264. case '4':
  265. case '7':
  266. case '8':
  267. if (esc_save[1] == '[') {
  268. /* see if next character is ~ */
  269. act = ESC_SAVE;
  270. }
  271. break;
  272. }
  273. } else if (esc_len == 3) {
  274. if (ichar == '~') {
  275. switch (esc_save[2]) {
  276. case '3': /* Delete key */
  277. ichar = CTL_CH('d');
  278. act = ESC_CONVERTED;
  279. break; /* pass to ^D handler */
  280. case '1': /* Home key */
  281. case '7':
  282. ichar = CTL_CH('a');
  283. act = ESC_CONVERTED;
  284. break; /* pass to ^A handler */
  285. case '4': /* End key */
  286. case '8':
  287. ichar = CTL_CH('e');
  288. act = ESC_CONVERTED;
  289. break; /* pass to ^E handler */
  290. }
  291. }
  292. }
  293. switch (act) {
  294. case ESC_SAVE:
  295. esc_save[esc_len++] = ichar;
  296. continue;
  297. case ESC_REJECT:
  298. esc_save[esc_len++] = ichar;
  299. cread_add_str(esc_save, esc_len, insert,
  300. &num, &eol_num, buf, *len);
  301. esc_len = 0;
  302. continue;
  303. case ESC_CONVERTED:
  304. esc_len = 0;
  305. break;
  306. }
  307. }
  308. switch (ichar) {
  309. case 0x1b:
  310. if (esc_len == 0) {
  311. esc_save[esc_len] = ichar;
  312. esc_len = 1;
  313. } else {
  314. puts("impossible condition #876\n");
  315. esc_len = 0;
  316. }
  317. break;
  318. case CTL_CH('a'):
  319. BEGINNING_OF_LINE();
  320. break;
  321. case CTL_CH('c'): /* ^C - break */
  322. *buf = '\0'; /* discard input */
  323. return -1;
  324. case CTL_CH('f'):
  325. if (num < eol_num) {
  326. getcmd_putch(buf[num]);
  327. num++;
  328. }
  329. break;
  330. case CTL_CH('b'):
  331. if (num) {
  332. getcmd_putch(CTL_BACKSPACE);
  333. num--;
  334. }
  335. break;
  336. case CTL_CH('d'):
  337. if (num < eol_num) {
  338. wlen = eol_num - num - 1;
  339. if (wlen) {
  340. memmove(&buf[num], &buf[num+1], wlen);
  341. putnstr(buf + num, wlen);
  342. }
  343. getcmd_putch(' ');
  344. do {
  345. getcmd_putch(CTL_BACKSPACE);
  346. } while (wlen--);
  347. eol_num--;
  348. }
  349. break;
  350. case CTL_CH('k'):
  351. ERASE_TO_EOL();
  352. break;
  353. case CTL_CH('e'):
  354. REFRESH_TO_EOL();
  355. break;
  356. case CTL_CH('o'):
  357. insert = !insert;
  358. break;
  359. case CTL_CH('x'):
  360. case CTL_CH('u'):
  361. BEGINNING_OF_LINE();
  362. ERASE_TO_EOL();
  363. break;
  364. case DEL:
  365. case DEL7:
  366. case 8:
  367. if (num) {
  368. wlen = eol_num - num;
  369. num--;
  370. memmove(&buf[num], &buf[num+1], wlen);
  371. getcmd_putch(CTL_BACKSPACE);
  372. putnstr(buf + num, wlen);
  373. getcmd_putch(' ');
  374. do {
  375. getcmd_putch(CTL_BACKSPACE);
  376. } while (wlen--);
  377. eol_num--;
  378. }
  379. break;
  380. case CTL_CH('p'):
  381. case CTL_CH('n'):
  382. {
  383. char *hline;
  384. esc_len = 0;
  385. if (ichar == CTL_CH('p'))
  386. hline = hist_prev();
  387. else
  388. hline = hist_next();
  389. if (!hline) {
  390. getcmd_cbeep();
  391. continue;
  392. }
  393. /* nuke the current line */
  394. /* first, go home */
  395. BEGINNING_OF_LINE();
  396. /* erase to end of line */
  397. ERASE_TO_EOL();
  398. /* copy new line into place and display */
  399. strcpy(buf, hline);
  400. eol_num = strlen(buf);
  401. REFRESH_TO_EOL();
  402. continue;
  403. }
  404. #ifdef CONFIG_AUTO_COMPLETE
  405. case '\t': {
  406. int num2, col;
  407. /* do not autocomplete when in the middle */
  408. if (num < eol_num) {
  409. getcmd_cbeep();
  410. break;
  411. }
  412. buf[num] = '\0';
  413. col = strlen(prompt) + eol_num;
  414. num2 = num;
  415. if (cmd_auto_complete(prompt, buf, &num2, &col)) {
  416. col = num2 - num;
  417. num += col;
  418. eol_num += col;
  419. }
  420. break;
  421. }
  422. #endif
  423. default:
  424. cread_add_char(ichar, insert, &num, &eol_num, buf,
  425. *len);
  426. break;
  427. }
  428. }
  429. *len = eol_num;
  430. buf[eol_num] = '\0'; /* lose the newline */
  431. if (buf[0] && buf[0] != CREAD_HIST_CHAR)
  432. cread_add_to_hist(buf);
  433. hist_cur = hist_add_idx;
  434. return 0;
  435. }
  436. #endif /* CONFIG_CMDLINE_EDITING */
  437. /****************************************************************************/
  438. int cli_readline(const char *const prompt)
  439. {
  440. /*
  441. * If console_buffer isn't 0-length the user will be prompted to modify
  442. * it instead of entering it from scratch as desired.
  443. */
  444. console_buffer[0] = '\0';
  445. return cli_readline_into_buffer(prompt, console_buffer, 0);
  446. }
  447. int cli_readline_into_buffer(const char *const prompt, char *buffer,
  448. int timeout)
  449. {
  450. char *p = buffer;
  451. #ifdef CONFIG_CMDLINE_EDITING
  452. unsigned int len = CONFIG_SYS_CBSIZE;
  453. int rc;
  454. static int initted;
  455. /*
  456. * History uses a global array which is not
  457. * writable until after relocation to RAM.
  458. * Revert to non-history version if still
  459. * running from flash.
  460. */
  461. if (gd->flags & GD_FLG_RELOC) {
  462. if (!initted) {
  463. hist_init();
  464. initted = 1;
  465. }
  466. if (prompt)
  467. puts(prompt);
  468. rc = cread_line(prompt, p, &len, timeout);
  469. return rc < 0 ? rc : len;
  470. } else {
  471. #endif /* CONFIG_CMDLINE_EDITING */
  472. char *p_buf = p;
  473. int n = 0; /* buffer index */
  474. int plen = 0; /* prompt length */
  475. int col; /* output column cnt */
  476. char c;
  477. /* print prompt */
  478. if (prompt) {
  479. plen = strlen(prompt);
  480. puts(prompt);
  481. }
  482. col = plen;
  483. for (;;) {
  484. if (bootretry_tstc_timeout())
  485. return -2; /* timed out */
  486. WATCHDOG_RESET(); /* Trigger watchdog, if needed */
  487. #ifdef CONFIG_SHOW_ACTIVITY
  488. while (!tstc()) {
  489. show_activity(0);
  490. WATCHDOG_RESET();
  491. }
  492. #endif
  493. c = getc();
  494. /*
  495. * Special character handling
  496. */
  497. switch (c) {
  498. case '\r': /* Enter */
  499. case '\n':
  500. *p = '\0';
  501. puts("\r\n");
  502. return p - p_buf;
  503. case '\0': /* nul */
  504. continue;
  505. case 0x03: /* ^C - break */
  506. p_buf[0] = '\0'; /* discard input */
  507. return -1;
  508. case 0x15: /* ^U - erase line */
  509. while (col > plen) {
  510. puts(erase_seq);
  511. --col;
  512. }
  513. p = p_buf;
  514. n = 0;
  515. continue;
  516. case 0x17: /* ^W - erase word */
  517. p = delete_char(p_buf, p, &col, &n, plen);
  518. while ((n > 0) && (*p != ' '))
  519. p = delete_char(p_buf, p, &col, &n, plen);
  520. continue;
  521. case 0x08: /* ^H - backspace */
  522. case 0x7F: /* DEL - backspace */
  523. p = delete_char(p_buf, p, &col, &n, plen);
  524. continue;
  525. default:
  526. /*
  527. * Must be a normal character then
  528. */
  529. if (n < CONFIG_SYS_CBSIZE-2) {
  530. if (c == '\t') { /* expand TABs */
  531. #ifdef CONFIG_AUTO_COMPLETE
  532. /*
  533. * if auto completion triggered just
  534. * continue
  535. */
  536. *p = '\0';
  537. if (cmd_auto_complete(prompt,
  538. console_buffer,
  539. &n, &col)) {
  540. p = p_buf + n; /* reset */
  541. continue;
  542. }
  543. #endif
  544. puts(tab_seq + (col & 07));
  545. col += 8 - (col & 07);
  546. } else {
  547. char __maybe_unused buf[2];
  548. /*
  549. * Echo input using puts() to force an
  550. * LCD flush if we are using an LCD
  551. */
  552. ++col;
  553. buf[0] = c;
  554. buf[1] = '\0';
  555. puts(buf);
  556. }
  557. *p++ = c;
  558. ++n;
  559. } else { /* Buffer full */
  560. putc('\a');
  561. }
  562. }
  563. }
  564. #ifdef CONFIG_CMDLINE_EDITING
  565. }
  566. #endif
  567. }