cli_readline.c 13 KB

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