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 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 /* ichar=0x0 when error occurs in U-Boot getc */
281 if ((ichar == '\n') || (ichar == '\r')) {
287 * handle standard linux xterm esc sequences for arrow key, etc.
290 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
293 if (ichar == '[' || ichar == 'O')
295 } else if (esc_len == 2) {
297 case 'D': /* <- key */
300 break; /* pass off to ^B handler */
301 case 'C': /* -> key */
304 break; /* pass off to ^F handler */
305 case 'H': /* Home key */
308 break; /* pass off to ^A handler */
309 case 'F': /* End key */
312 break; /* pass off to ^E handler */
313 case 'A': /* up arrow */
316 break; /* pass off to ^P handler */
317 case 'B': /* down arrow */
320 break; /* pass off to ^N handler */
326 if (esc_save[1] == '[') {
327 /* see if next character is ~ */
332 } else if (esc_len == 3) {
334 switch (esc_save[2]) {
335 case '3': /* Delete key */
338 break; /* pass to ^D handler */
339 case '1': /* Home key */
343 break; /* pass to ^A handler */
344 case '4': /* End key */
348 break; /* pass to ^E handler */
355 esc_save[esc_len++] = ichar;
358 esc_save[esc_len++] = ichar;
359 cread_add_str(esc_save, esc_len, insert,
360 &num, &eol_num, buf, *len);
372 esc_save[esc_len] = ichar;
375 puts("impossible condition #876\n");
383 case CTL_CH('c'): /* ^C - break */
384 *buf = '\0'; /* discard input */
388 getcmd_putch(buf[num]);
394 getcmd_putch(CTL_BACKSPACE);
400 wlen = eol_num - num - 1;
402 memmove(&buf[num], &buf[num+1], wlen);
403 putnstr(buf + num, wlen);
408 getcmd_putch(CTL_BACKSPACE);
431 wlen = eol_num - num;
433 memmove(&buf[num], &buf[num+1], wlen);
434 getcmd_putch(CTL_BACKSPACE);
435 putnstr(buf + num, wlen);
438 getcmd_putch(CTL_BACKSPACE);
450 if (ichar == CTL_CH('p'))
460 /* nuke the current line */
464 /* erase to end of line */
467 /* copy new line into place and display */
469 eol_num = strlen(buf);
473 #ifdef CONFIG_AUTO_COMPLETE
477 /* do not autocomplete when in the middle */
484 col = strlen(prompt) + eol_num;
486 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
495 cread_add_char(ichar, insert, &num, &eol_num, buf,
501 buf[eol_num] = '\0'; /* lose the newline */
503 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
504 cread_add_to_hist(buf);
505 hist_cur = hist_add_idx;
510 #endif /* CONFIG_CMDLINE_EDITING */
512 /****************************************************************************/
514 int cli_readline(const char *const prompt)
517 * If console_buffer isn't 0-length the user will be prompted to modify
518 * it instead of entering it from scratch as desired.
520 console_buffer[0] = '\0';
522 return cli_readline_into_buffer(prompt, console_buffer, 0);
526 int cli_readline_into_buffer(const char *const prompt, char *buffer,
530 #ifdef CONFIG_CMDLINE_EDITING
531 unsigned int len = CONFIG_SYS_CBSIZE;
536 * History uses a global array which is not
537 * writable until after relocation to RAM.
538 * Revert to non-history version if still
539 * running from flash.
541 if (gd->flags & GD_FLG_RELOC) {
550 rc = cread_line(prompt, p, &len, timeout);
551 return rc < 0 ? rc : len;
554 #endif /* CONFIG_CMDLINE_EDITING */
556 int n = 0; /* buffer index */
557 int plen = 0; /* prompt length */
558 int col; /* output column cnt */
563 plen = strlen(prompt);
569 if (bootretry_tstc_timeout())
570 return -2; /* timed out */
571 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
576 * Special character handling
579 case '\r': /* Enter */
588 case 0x03: /* ^C - break */
589 p_buf[0] = '\0'; /* discard input */
592 case 0x15: /* ^U - erase line */
601 case 0x17: /* ^W - erase word */
602 p = delete_char(p_buf, p, &col, &n, plen);
603 while ((n > 0) && (*p != ' '))
604 p = delete_char(p_buf, p, &col, &n, plen);
607 case 0x08: /* ^H - backspace */
608 case 0x7F: /* DEL - backspace */
609 p = delete_char(p_buf, p, &col, &n, plen);
614 * Must be a normal character then
616 if (n < CONFIG_SYS_CBSIZE-2) {
617 if (c == '\t') { /* expand TABs */
618 #ifdef CONFIG_AUTO_COMPLETE
620 * if auto completion triggered just
624 if (cmd_auto_complete(prompt,
627 p = p_buf + n; /* reset */
631 puts(tab_seq + (col & 07));
632 col += 8 - (col & 07);
634 char __maybe_unused buf[2];
637 * Echo input using puts() to force an
638 * LCD flush if we are using an LCD
647 } else { /* Buffer full */
652 #ifdef CONFIG_CMDLINE_EDITING