1 // SPDX-License-Identifier: GPL-2.0+
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 * Add to readline cmdline-editing by
8 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
12 #include <bootretry.h>
17 #include <asm/global_data.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static const char erase_seq[] = "\b \b"; /* erase sequence */
22 static const char tab_seq[] = " "; /* used to expand TABs */
24 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
26 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
33 if (*(--p) == '\t') { /* will retype the whole line */
34 while (*colp > plen) {
38 for (s = buffer; s < p; ++s) {
40 puts(tab_seq + ((*colp) & 07));
41 *colp += 8 - ((*colp) & 07);
56 #ifdef CONFIG_CMDLINE_EDITING
59 * cmdline-editing related codes from vivi.
60 * Author: Janghoon Lyu <nandy@mizi.com>
63 #define putnstr(str, n) printf("%.*s", (int)n, str)
65 #define CTL_CH(c) ((c) - 'a' + 1)
66 #define CTL_BACKSPACE ('\b')
67 #define DEL ((char)255)
68 #define DEL7 ((char)127)
69 #define CREAD_HIST_CHAR ('!')
71 #define getcmd_putch(ch) putc(ch)
72 #define getcmd_getch() getchar()
73 #define getcmd_cbeep() getcmd_putch('\a')
75 #ifdef CONFIG_SPL_BUILD
80 #define HIST_SIZE CONFIG_SYS_CBSIZE
84 static int hist_add_idx;
85 static int hist_cur = -1;
86 static unsigned hist_num;
88 static char *hist_list[HIST_MAX];
89 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
91 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
93 static void hist_init(void)
102 for (i = 0; i < HIST_MAX; i++) {
103 hist_list[i] = hist_lines[i];
104 hist_list[i][0] = '\0';
108 static void cread_add_to_hist(char *line)
110 strcpy(hist_list[hist_add_idx], line);
112 if (++hist_add_idx >= HIST_MAX)
115 if (hist_add_idx > hist_max)
116 hist_max = hist_add_idx;
121 static char *hist_prev(void)
133 if (hist_cur == hist_add_idx) {
137 ret = hist_list[hist_cur];
143 static char *hist_next(void)
150 if (hist_cur == hist_add_idx)
153 if (++hist_cur > hist_max)
156 if (hist_cur == hist_add_idx)
159 ret = hist_list[hist_cur];
164 #ifndef CONFIG_CMDLINE_EDITING
165 static void cread_print_hist_list(void)
170 n = hist_num - hist_max;
172 i = hist_add_idx + 1;
176 if (i == hist_add_idx)
178 printf("%s\n", hist_list[i]);
183 #endif /* CONFIG_CMDLINE_EDITING */
185 #define BEGINNING_OF_LINE() { \
187 getcmd_putch(CTL_BACKSPACE); \
192 #define ERASE_TO_EOL() { \
193 if (num < eol_num) { \
194 printf("%*s", (int)(eol_num - num), ""); \
196 getcmd_putch(CTL_BACKSPACE); \
197 } while (--eol_num > num); \
201 #define REFRESH_TO_EOL() { \
202 if (num < eol_num) { \
203 wlen = eol_num - num; \
204 putnstr(buf + num, wlen); \
209 static void cread_add_char(char ichar, int insert, unsigned long *num,
210 unsigned long *eol_num, char *buf, unsigned long len)
215 if (insert || *num == *eol_num) {
216 if (*eol_num > len - 1) {
224 wlen = *eol_num - *num;
226 memmove(&buf[*num+1], &buf[*num], wlen-1);
229 putnstr(buf + *num, wlen);
232 getcmd_putch(CTL_BACKSPACE);
234 /* echo the character */
237 putnstr(buf + *num, wlen);
242 static void cread_add_str(char *str, int strsize, int insert,
243 unsigned long *num, unsigned long *eol_num,
244 char *buf, unsigned long len)
247 cread_add_char(*str, insert, num, eol_num, buf, len);
252 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
255 unsigned long num = 0;
256 unsigned long eol_num = 0;
262 int init_len = strlen(buf);
266 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
269 if (bootretry_tstc_timeout())
270 return -2; /* timed out */
271 if (first && timeout) {
272 uint64_t etime = endtick(timeout);
274 while (!tstc()) { /* while no incoming data */
275 if (get_ticks() >= etime)
276 return -2; /* timed out */
282 ichar = getcmd_getch();
284 /* ichar=0x0 when error occurs in U-Boot getc */
288 if ((ichar == '\n') || (ichar == '\r')) {
294 * handle standard linux xterm esc sequences for arrow key, etc.
297 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
300 if (ichar == '[' || ichar == 'O')
302 } else if (esc_len == 2) {
304 case 'D': /* <- key */
307 break; /* pass off to ^B handler */
308 case 'C': /* -> key */
311 break; /* pass off to ^F handler */
312 case 'H': /* Home key */
315 break; /* pass off to ^A handler */
316 case 'F': /* End key */
319 break; /* pass off to ^E handler */
320 case 'A': /* up arrow */
323 break; /* pass off to ^P handler */
324 case 'B': /* down arrow */
327 break; /* pass off to ^N handler */
334 if (esc_save[1] == '[') {
335 /* see if next character is ~ */
340 } else if (esc_len == 3) {
343 switch (esc_save[2]) {
344 case '3': /* Delete key */
347 break; /* pass to ^D handler */
348 case '1': /* Home key */
352 break; /* pass to ^A handler */
353 case '4': /* End key */
357 break; /* pass to ^E handler */
361 if (esc_save[2] == '2')
365 } else if (esc_len == 4) {
370 break; /* bracketed paste */
372 } else if (esc_len == 5) {
373 if (ichar == '~') { /* bracketed paste */
380 esc_save[esc_len++] = ichar;
383 esc_save[esc_len++] = ichar;
384 cread_add_str(esc_save, esc_len, insert,
385 &num, &eol_num, buf, *len);
397 esc_save[esc_len] = ichar;
400 puts("impossible condition #876\n");
408 case CTL_CH('c'): /* ^C - break */
409 *buf = '\0'; /* discard input */
413 getcmd_putch(buf[num]);
419 getcmd_putch(CTL_BACKSPACE);
425 wlen = eol_num - num - 1;
427 memmove(&buf[num], &buf[num+1], wlen);
428 putnstr(buf + num, wlen);
433 getcmd_putch(CTL_BACKSPACE);
456 wlen = eol_num - num;
458 memmove(&buf[num], &buf[num+1], wlen);
459 getcmd_putch(CTL_BACKSPACE);
460 putnstr(buf + num, wlen);
463 getcmd_putch(CTL_BACKSPACE);
475 if (ichar == CTL_CH('p'))
485 /* nuke the current line */
489 /* erase to end of line */
492 /* copy new line into place and display */
494 eol_num = strlen(buf);
498 #ifdef CONFIG_AUTO_COMPLETE
502 /* do not autocomplete when in the middle */
509 col = strlen(prompt) + eol_num;
511 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
520 if (ichar >= ' ' && ichar <= '~') {
521 cread_add_char(ichar, insert, &num, &eol_num,
528 buf[eol_num] = '\0'; /* lose the newline */
530 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
531 cread_add_to_hist(buf);
532 hist_cur = hist_add_idx;
537 #endif /* CONFIG_CMDLINE_EDITING */
539 /****************************************************************************/
541 int cli_readline(const char *const prompt)
544 * If console_buffer isn't 0-length the user will be prompted to modify
545 * it instead of entering it from scratch as desired.
547 console_buffer[0] = '\0';
549 return cli_readline_into_buffer(prompt, console_buffer, 0);
553 int cli_readline_into_buffer(const char *const prompt, char *buffer,
557 #ifdef CONFIG_CMDLINE_EDITING
558 unsigned int len = CONFIG_SYS_CBSIZE;
563 * History uses a global array which is not
564 * writable until after relocation to RAM.
565 * Revert to non-history version if still
566 * running from flash.
568 if (gd->flags & GD_FLG_RELOC) {
577 rc = cread_line(prompt, p, &len, timeout);
578 return rc < 0 ? rc : len;
581 #endif /* CONFIG_CMDLINE_EDITING */
583 int n = 0; /* buffer index */
584 int plen = 0; /* prompt length */
585 int col; /* output column cnt */
590 plen = strlen(prompt);
596 if (bootretry_tstc_timeout())
597 return -2; /* timed out */
598 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
603 * Special character handling
606 case '\r': /* Enter */
615 case 0x03: /* ^C - break */
616 p_buf[0] = '\0'; /* discard input */
619 case 0x15: /* ^U - erase line */
628 case 0x17: /* ^W - erase word */
629 p = delete_char(p_buf, p, &col, &n, plen);
630 while ((n > 0) && (*p != ' '))
631 p = delete_char(p_buf, p, &col, &n, plen);
634 case 0x08: /* ^H - backspace */
635 case 0x7F: /* DEL - backspace */
636 p = delete_char(p_buf, p, &col, &n, plen);
641 * Must be a normal character then
643 if (n < CONFIG_SYS_CBSIZE-2) {
644 if (c == '\t') { /* expand TABs */
645 #ifdef CONFIG_AUTO_COMPLETE
647 * if auto completion triggered just
651 if (cmd_auto_complete(prompt,
654 p = p_buf + n; /* reset */
658 puts(tab_seq + (col & 07));
659 col += 8 - (col & 07);
661 char __maybe_unused buf[2];
664 * Echo input using puts() to force an
665 * LCD flush if we are using an LCD
674 } else { /* Buffer full */
679 #ifdef CONFIG_CMDLINE_EDITING