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.
288 esc_save[esc_len] = ichar;
291 cread_add_str(esc_save, esc_len,
292 insert, &num, &eol_num,
300 case 'D': /* <- key */
304 case 'C': /* -> key */
307 break; /* pass off to ^F handler */
308 case 'H': /* Home key */
311 break; /* pass off to ^A handler */
312 case 'A': /* up arrow */
315 break; /* pass off to ^P handler */
316 case 'B': /* down arrow */
319 break; /* pass off to ^N handler */
321 esc_save[esc_len++] = ichar;
322 cread_add_str(esc_save, esc_len, insert,
323 &num, &eol_num, buf, *len);
332 esc_save[esc_len] = ichar;
335 puts("impossible condition #876\n");
343 case CTL_CH('c'): /* ^C - break */
344 *buf = '\0'; /* discard input */
348 getcmd_putch(buf[num]);
354 getcmd_putch(CTL_BACKSPACE);
360 wlen = eol_num - num - 1;
362 memmove(&buf[num], &buf[num+1], wlen);
363 putnstr(buf + num, wlen);
368 getcmd_putch(CTL_BACKSPACE);
391 wlen = eol_num - num;
393 memmove(&buf[num], &buf[num+1], wlen);
394 getcmd_putch(CTL_BACKSPACE);
395 putnstr(buf + num, wlen);
398 getcmd_putch(CTL_BACKSPACE);
410 if (ichar == CTL_CH('p'))
420 /* nuke the current line */
424 /* erase to end of line */
427 /* copy new line into place and display */
429 eol_num = strlen(buf);
433 #ifdef CONFIG_AUTO_COMPLETE
437 /* do not autocomplete when in the middle */
444 col = strlen(prompt) + eol_num;
446 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
455 cread_add_char(ichar, insert, &num, &eol_num, buf,
461 buf[eol_num] = '\0'; /* lose the newline */
463 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
464 cread_add_to_hist(buf);
465 hist_cur = hist_add_idx;
470 #endif /* CONFIG_CMDLINE_EDITING */
472 /****************************************************************************/
474 int cli_readline(const char *const prompt)
477 * If console_buffer isn't 0-length the user will be prompted to modify
478 * it instead of entering it from scratch as desired.
480 console_buffer[0] = '\0';
482 return cli_readline_into_buffer(prompt, console_buffer, 0);
486 int cli_readline_into_buffer(const char *const prompt, char *buffer,
490 #ifdef CONFIG_CMDLINE_EDITING
491 unsigned int len = CONFIG_SYS_CBSIZE;
496 * History uses a global array which is not
497 * writable until after relocation to RAM.
498 * Revert to non-history version if still
499 * running from flash.
501 if (gd->flags & GD_FLG_RELOC) {
510 rc = cread_line(prompt, p, &len, timeout);
511 return rc < 0 ? rc : len;
514 #endif /* CONFIG_CMDLINE_EDITING */
516 int n = 0; /* buffer index */
517 int plen = 0; /* prompt length */
518 int col; /* output column cnt */
523 plen = strlen(prompt);
529 if (bootretry_tstc_timeout())
530 return -2; /* timed out */
531 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
533 #ifdef CONFIG_SHOW_ACTIVITY
542 * Special character handling
545 case '\r': /* Enter */
554 case 0x03: /* ^C - break */
555 p_buf[0] = '\0'; /* discard input */
558 case 0x15: /* ^U - erase line */
567 case 0x17: /* ^W - erase word */
568 p = delete_char(p_buf, p, &col, &n, plen);
569 while ((n > 0) && (*p != ' '))
570 p = delete_char(p_buf, p, &col, &n, plen);
573 case 0x08: /* ^H - backspace */
574 case 0x7F: /* DEL - backspace */
575 p = delete_char(p_buf, p, &col, &n, plen);
580 * Must be a normal character then
582 if (n < CONFIG_SYS_CBSIZE-2) {
583 if (c == '\t') { /* expand TABs */
584 #ifdef CONFIG_AUTO_COMPLETE
586 * if auto completion triggered just
590 if (cmd_auto_complete(prompt,
593 p = p_buf + n; /* reset */
597 puts(tab_seq + (col & 07));
598 col += 8 - (col & 07);
603 * Echo input using puts() to force an
604 * LCD flush if we are using an LCD
613 } else { /* Buffer full */
618 #ifdef CONFIG_CMDLINE_EDITING