3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Add to readline cmdline-editing by
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9 * SPDX-License-Identifier: GPL-2.0+
13 #include <bootretry.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 static const char erase_seq[] = "\b \b"; /* erase sequence */
20 static const char tab_seq[] = " "; /* used to expand TABs */
22 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
24 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
31 if (*(--p) == '\t') { /* will retype the whole line */
32 while (*colp > plen) {
36 for (s = buffer; s < p; ++s) {
38 puts(tab_seq + ((*colp) & 07));
39 *colp += 8 - ((*colp) & 07);
54 #ifdef CONFIG_CMDLINE_EDITING
57 * cmdline-editing related codes from vivi.
58 * Author: Janghoon Lyu <nandy@mizi.com>
61 #define putnstr(str, n) printf("%.*s", (int)n, str)
63 #define CTL_CH(c) ((c) - 'a' + 1)
64 #define CTL_BACKSPACE ('\b')
65 #define DEL ((char)255)
66 #define DEL7 ((char)127)
67 #define CREAD_HIST_CHAR ('!')
69 #define getcmd_putch(ch) putc(ch)
70 #define getcmd_getch() getc()
71 #define getcmd_cbeep() getcmd_putch('\a')
74 #define HIST_SIZE CONFIG_SYS_CBSIZE
77 static int hist_add_idx;
78 static int hist_cur = -1;
79 static unsigned hist_num;
81 static char *hist_list[HIST_MAX];
82 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
84 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
86 static void hist_init(void)
95 for (i = 0; i < HIST_MAX; i++) {
96 hist_list[i] = hist_lines[i];
97 hist_list[i][0] = '\0';
101 static void cread_add_to_hist(char *line)
103 strcpy(hist_list[hist_add_idx], line);
105 if (++hist_add_idx >= HIST_MAX)
108 if (hist_add_idx > hist_max)
109 hist_max = hist_add_idx;
114 static char *hist_prev(void)
126 if (hist_cur == hist_add_idx) {
130 ret = hist_list[hist_cur];
136 static char *hist_next(void)
143 if (hist_cur == hist_add_idx)
146 if (++hist_cur > hist_max)
149 if (hist_cur == hist_add_idx)
152 ret = hist_list[hist_cur];
157 #ifndef CONFIG_CMDLINE_EDITING
158 static void cread_print_hist_list(void)
163 n = hist_num - hist_max;
165 i = hist_add_idx + 1;
169 if (i == hist_add_idx)
171 printf("%s\n", hist_list[i]);
176 #endif /* CONFIG_CMDLINE_EDITING */
178 #define BEGINNING_OF_LINE() { \
180 getcmd_putch(CTL_BACKSPACE); \
185 #define ERASE_TO_EOL() { \
186 if (num < eol_num) { \
187 printf("%*s", (int)(eol_num - num), ""); \
189 getcmd_putch(CTL_BACKSPACE); \
190 } while (--eol_num > num); \
194 #define REFRESH_TO_EOL() { \
195 if (num < eol_num) { \
196 wlen = eol_num - num; \
197 putnstr(buf + num, wlen); \
202 static void cread_add_char(char ichar, int insert, unsigned long *num,
203 unsigned long *eol_num, char *buf, unsigned long len)
208 if (insert || *num == *eol_num) {
209 if (*eol_num > len - 1) {
217 wlen = *eol_num - *num;
219 memmove(&buf[*num+1], &buf[*num], wlen-1);
222 putnstr(buf + *num, wlen);
225 getcmd_putch(CTL_BACKSPACE);
227 /* echo the character */
230 putnstr(buf + *num, wlen);
235 static void cread_add_str(char *str, int strsize, int insert,
236 unsigned long *num, unsigned long *eol_num,
237 char *buf, unsigned long len)
240 cread_add_char(*str, insert, num, eol_num, buf, len);
245 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
248 unsigned long num = 0;
249 unsigned long eol_num = 0;
255 int init_len = strlen(buf);
259 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
262 if (bootretry_tstc_timeout())
263 return -2; /* timed out */
264 if (first && timeout) {
265 uint64_t etime = endtick(timeout);
267 while (!tstc()) { /* while no incoming data */
268 if (get_ticks() >= etime)
269 return -2; /* timed out */
275 ichar = getcmd_getch();
277 if ((ichar == '\n') || (ichar == '\r')) {
283 * handle standard linux xterm esc sequences for arrow key, etc.
286 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
289 if (ichar == '[' || ichar == 'O')
291 } else if (esc_len == 2) {
293 case 'D': /* <- key */
296 break; /* pass off to ^B handler */
297 case 'C': /* -> key */
300 break; /* pass off to ^F handler */
301 case 'H': /* Home key */
304 break; /* pass off to ^A handler */
305 case 'F': /* End key */
308 break; /* pass off to ^E handler */
309 case 'A': /* up arrow */
312 break; /* pass off to ^P handler */
313 case 'B': /* down arrow */
316 break; /* pass off to ^N handler */
322 if (esc_save[1] == '[') {
323 /* see if next character is ~ */
328 } else if (esc_len == 3) {
330 switch (esc_save[2]) {
331 case '3': /* Delete key */
334 break; /* pass to ^D handler */
335 case '1': /* Home key */
339 break; /* pass to ^A handler */
340 case '4': /* End key */
344 break; /* pass to ^E handler */
351 esc_save[esc_len++] = ichar;
354 esc_save[esc_len++] = ichar;
355 cread_add_str(esc_save, esc_len, insert,
356 &num, &eol_num, buf, *len);
368 esc_save[esc_len] = ichar;
371 puts("impossible condition #876\n");
379 case CTL_CH('c'): /* ^C - break */
380 *buf = '\0'; /* discard input */
384 getcmd_putch(buf[num]);
390 getcmd_putch(CTL_BACKSPACE);
396 wlen = eol_num - num - 1;
398 memmove(&buf[num], &buf[num+1], wlen);
399 putnstr(buf + num, wlen);
404 getcmd_putch(CTL_BACKSPACE);
427 wlen = eol_num - num;
429 memmove(&buf[num], &buf[num+1], wlen);
430 getcmd_putch(CTL_BACKSPACE);
431 putnstr(buf + num, wlen);
434 getcmd_putch(CTL_BACKSPACE);
446 if (ichar == CTL_CH('p'))
456 /* nuke the current line */
460 /* erase to end of line */
463 /* copy new line into place and display */
465 eol_num = strlen(buf);
469 #ifdef CONFIG_AUTO_COMPLETE
473 /* do not autocomplete when in the middle */
480 col = strlen(prompt) + eol_num;
482 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
491 cread_add_char(ichar, insert, &num, &eol_num, buf,
497 buf[eol_num] = '\0'; /* lose the newline */
499 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
500 cread_add_to_hist(buf);
501 hist_cur = hist_add_idx;
506 #endif /* CONFIG_CMDLINE_EDITING */
508 /****************************************************************************/
510 int cli_readline(const char *const prompt)
513 * If console_buffer isn't 0-length the user will be prompted to modify
514 * it instead of entering it from scratch as desired.
516 console_buffer[0] = '\0';
518 return cli_readline_into_buffer(prompt, console_buffer, 0);
522 int cli_readline_into_buffer(const char *const prompt, char *buffer,
526 #ifdef CONFIG_CMDLINE_EDITING
527 unsigned int len = CONFIG_SYS_CBSIZE;
532 * History uses a global array which is not
533 * writable until after relocation to RAM.
534 * Revert to non-history version if still
535 * running from flash.
537 if (gd->flags & GD_FLG_RELOC) {
546 rc = cread_line(prompt, p, &len, timeout);
547 return rc < 0 ? rc : len;
550 #endif /* CONFIG_CMDLINE_EDITING */
552 int n = 0; /* buffer index */
553 int plen = 0; /* prompt length */
554 int col; /* output column cnt */
559 plen = strlen(prompt);
565 if (bootretry_tstc_timeout())
566 return -2; /* timed out */
567 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
569 #ifdef CONFIG_SHOW_ACTIVITY
578 * Special character handling
581 case '\r': /* Enter */
590 case 0x03: /* ^C - break */
591 p_buf[0] = '\0'; /* discard input */
594 case 0x15: /* ^U - erase line */
603 case 0x17: /* ^W - erase word */
604 p = delete_char(p_buf, p, &col, &n, plen);
605 while ((n > 0) && (*p != ' '))
606 p = delete_char(p_buf, p, &col, &n, plen);
609 case 0x08: /* ^H - backspace */
610 case 0x7F: /* DEL - backspace */
611 p = delete_char(p_buf, p, &col, &n, plen);
616 * Must be a normal character then
618 if (n < CONFIG_SYS_CBSIZE-2) {
619 if (c == '\t') { /* expand TABs */
620 #ifdef CONFIG_AUTO_COMPLETE
622 * if auto completion triggered just
626 if (cmd_auto_complete(prompt,
629 p = p_buf + n; /* reset */
633 puts(tab_seq + (col & 07));
634 col += 8 - (col & 07);
636 char __maybe_unused buf[2];
639 * Echo input using puts() to force an
640 * LCD flush if we are using an LCD
649 } else { /* Buffer full */
654 #ifdef CONFIG_CMDLINE_EDITING