cli_readline.c 12 KB

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