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>
18 DECLARE_GLOBAL_DATA_PTR;
20 static const char erase_seq[] = "\b \b"; /* erase sequence */
21 static const char tab_seq[] = " "; /* used to expand TABs */
23 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
25 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
32 if (*(--p) == '\t') { /* will retype the whole line */
33 while (*colp > plen) {
37 for (s = buffer; s < p; ++s) {
39 puts(tab_seq + ((*colp) & 07));
40 *colp += 8 - ((*colp) & 07);
55 #ifdef CONFIG_CMDLINE_EDITING
58 * cmdline-editing related codes from vivi.
59 * Author: Janghoon Lyu <nandy@mizi.com>
62 #define putnstr(str, n) printf("%.*s", (int)n, str)
64 #define CTL_CH(c) ((c) - 'a' + 1)
65 #define CTL_BACKSPACE ('\b')
66 #define DEL ((char)255)
67 #define DEL7 ((char)127)
68 #define CREAD_HIST_CHAR ('!')
70 #define getcmd_putch(ch) putc(ch)
71 #define getcmd_getch() getchar()
72 #define getcmd_cbeep() getcmd_putch('\a')
75 #define HIST_SIZE CONFIG_SYS_CBSIZE
78 static int hist_add_idx;
79 static int hist_cur = -1;
80 static unsigned hist_num;
82 static char *hist_list[HIST_MAX];
83 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
85 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
87 static void hist_init(void)
96 for (i = 0; i < HIST_MAX; i++) {
97 hist_list[i] = hist_lines[i];
98 hist_list[i][0] = '\0';
102 static void cread_add_to_hist(char *line)
104 strcpy(hist_list[hist_add_idx], line);
106 if (++hist_add_idx >= HIST_MAX)
109 if (hist_add_idx > hist_max)
110 hist_max = hist_add_idx;
115 static char *hist_prev(void)
127 if (hist_cur == hist_add_idx) {
131 ret = hist_list[hist_cur];
137 static char *hist_next(void)
144 if (hist_cur == hist_add_idx)
147 if (++hist_cur > hist_max)
150 if (hist_cur == hist_add_idx)
153 ret = hist_list[hist_cur];
158 #ifndef CONFIG_CMDLINE_EDITING
159 static void cread_print_hist_list(void)
164 n = hist_num - hist_max;
166 i = hist_add_idx + 1;
170 if (i == hist_add_idx)
172 printf("%s\n", hist_list[i]);
177 #endif /* CONFIG_CMDLINE_EDITING */
179 #define BEGINNING_OF_LINE() { \
181 getcmd_putch(CTL_BACKSPACE); \
186 #define ERASE_TO_EOL() { \
187 if (num < eol_num) { \
188 printf("%*s", (int)(eol_num - num), ""); \
190 getcmd_putch(CTL_BACKSPACE); \
191 } while (--eol_num > num); \
195 #define REFRESH_TO_EOL() { \
196 if (num < eol_num) { \
197 wlen = eol_num - num; \
198 putnstr(buf + num, wlen); \
203 static void cread_add_char(char ichar, int insert, unsigned long *num,
204 unsigned long *eol_num, char *buf, unsigned long len)
209 if (insert || *num == *eol_num) {
210 if (*eol_num > len - 1) {
218 wlen = *eol_num - *num;
220 memmove(&buf[*num+1], &buf[*num], wlen-1);
223 putnstr(buf + *num, wlen);
226 getcmd_putch(CTL_BACKSPACE);
228 /* echo the character */
231 putnstr(buf + *num, wlen);
236 static void cread_add_str(char *str, int strsize, int insert,
237 unsigned long *num, unsigned long *eol_num,
238 char *buf, unsigned long len)
241 cread_add_char(*str, insert, num, eol_num, buf, len);
246 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
249 unsigned long num = 0;
250 unsigned long eol_num = 0;
256 int init_len = strlen(buf);
260 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
263 if (bootretry_tstc_timeout())
264 return -2; /* timed out */
265 if (first && timeout) {
266 uint64_t etime = endtick(timeout);
268 while (!tstc()) { /* while no incoming data */
269 if (get_ticks() >= etime)
270 return -2; /* timed out */
276 ichar = getcmd_getch();
278 /* ichar=0x0 when error occurs in U-Boot getc */
282 if ((ichar == '\n') || (ichar == '\r')) {
288 * handle standard linux xterm esc sequences for arrow key, etc.
291 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
294 if (ichar == '[' || ichar == 'O')
296 } else if (esc_len == 2) {
298 case 'D': /* <- key */
301 break; /* pass off to ^B handler */
302 case 'C': /* -> key */
305 break; /* pass off to ^F handler */
306 case 'H': /* Home key */
309 break; /* pass off to ^A handler */
310 case 'F': /* End key */
313 break; /* pass off to ^E handler */
314 case 'A': /* up arrow */
317 break; /* pass off to ^P handler */
318 case 'B': /* down arrow */
321 break; /* pass off to ^N handler */
327 if (esc_save[1] == '[') {
328 /* see if next character is ~ */
333 } else if (esc_len == 3) {
335 switch (esc_save[2]) {
336 case '3': /* Delete key */
339 break; /* pass to ^D handler */
340 case '1': /* Home key */
344 break; /* pass to ^A handler */
345 case '4': /* End key */
349 break; /* pass to ^E handler */
356 esc_save[esc_len++] = ichar;
359 esc_save[esc_len++] = ichar;
360 cread_add_str(esc_save, esc_len, insert,
361 &num, &eol_num, buf, *len);
373 esc_save[esc_len] = ichar;
376 puts("impossible condition #876\n");
384 case CTL_CH('c'): /* ^C - break */
385 *buf = '\0'; /* discard input */
389 getcmd_putch(buf[num]);
395 getcmd_putch(CTL_BACKSPACE);
401 wlen = eol_num - num - 1;
403 memmove(&buf[num], &buf[num+1], wlen);
404 putnstr(buf + num, wlen);
409 getcmd_putch(CTL_BACKSPACE);
432 wlen = eol_num - num;
434 memmove(&buf[num], &buf[num+1], wlen);
435 getcmd_putch(CTL_BACKSPACE);
436 putnstr(buf + num, wlen);
439 getcmd_putch(CTL_BACKSPACE);
451 if (ichar == CTL_CH('p'))
461 /* nuke the current line */
465 /* erase to end of line */
468 /* copy new line into place and display */
470 eol_num = strlen(buf);
474 #ifdef CONFIG_AUTO_COMPLETE
478 /* do not autocomplete when in the middle */
485 col = strlen(prompt) + eol_num;
487 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
496 cread_add_char(ichar, insert, &num, &eol_num, buf,
502 buf[eol_num] = '\0'; /* lose the newline */
504 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
505 cread_add_to_hist(buf);
506 hist_cur = hist_add_idx;
511 #endif /* CONFIG_CMDLINE_EDITING */
513 /****************************************************************************/
515 int cli_readline(const char *const prompt)
518 * If console_buffer isn't 0-length the user will be prompted to modify
519 * it instead of entering it from scratch as desired.
521 console_buffer[0] = '\0';
523 return cli_readline_into_buffer(prompt, console_buffer, 0);
527 int cli_readline_into_buffer(const char *const prompt, char *buffer,
531 #ifdef CONFIG_CMDLINE_EDITING
532 unsigned int len = CONFIG_SYS_CBSIZE;
537 * History uses a global array which is not
538 * writable until after relocation to RAM.
539 * Revert to non-history version if still
540 * running from flash.
542 if (gd->flags & GD_FLG_RELOC) {
551 rc = cread_line(prompt, p, &len, timeout);
552 return rc < 0 ? rc : len;
555 #endif /* CONFIG_CMDLINE_EDITING */
557 int n = 0; /* buffer index */
558 int plen = 0; /* prompt length */
559 int col; /* output column cnt */
564 plen = strlen(prompt);
570 if (bootretry_tstc_timeout())
571 return -2; /* timed out */
572 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
577 * Special character handling
580 case '\r': /* Enter */
589 case 0x03: /* ^C - break */
590 p_buf[0] = '\0'; /* discard input */
593 case 0x15: /* ^U - erase line */
602 case 0x17: /* ^W - erase word */
603 p = delete_char(p_buf, p, &col, &n, plen);
604 while ((n > 0) && (*p != ' '))
605 p = delete_char(p_buf, p, &col, &n, plen);
608 case 0x08: /* ^H - backspace */
609 case 0x7F: /* DEL - backspace */
610 p = delete_char(p_buf, p, &col, &n, plen);
615 * Must be a normal character then
617 if (n < CONFIG_SYS_CBSIZE-2) {
618 if (c == '\t') { /* expand TABs */
619 #ifdef CONFIG_AUTO_COMPLETE
621 * if auto completion triggered just
625 if (cmd_auto_complete(prompt,
628 p = p_buf + n; /* reset */
632 puts(tab_seq + (col & 07));
633 col += 8 - (col & 07);
635 char __maybe_unused buf[2];
638 * Echo input using puts() to force an
639 * LCD flush if we are using an LCD
648 } else { /* Buffer full */
653 #ifdef CONFIG_CMDLINE_EDITING