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>
16 DECLARE_GLOBAL_DATA_PTR
;
18 static const char erase_seq
[] = "\b \b"; /* erase sequence */
19 static const char tab_seq
[] = " "; /* used to expand TABs */
21 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
23 static char *delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
30 if (*(--p
) == '\t') { /* will retype the whole line */
31 while (*colp
> plen
) {
35 for (s
= buffer
; s
< p
; ++s
) {
37 puts(tab_seq
+ ((*colp
) & 07));
38 *colp
+= 8 - ((*colp
) & 07);
53 #ifdef CONFIG_CMDLINE_EDITING
56 * cmdline-editing related codes from vivi.
57 * Author: Janghoon Lyu <nandy@mizi.com>
60 #define putnstr(str, n) printf("%.*s", (int)n, str)
62 #define CTL_CH(c) ((c) - 'a' + 1)
63 #define CTL_BACKSPACE ('\b')
64 #define DEL ((char)255)
65 #define DEL7 ((char)127)
66 #define CREAD_HIST_CHAR ('!')
68 #define getcmd_putch(ch) putc(ch)
69 #define getcmd_getch() getc()
70 #define getcmd_cbeep() getcmd_putch('\a')
73 #define HIST_SIZE CONFIG_SYS_CBSIZE
76 static int hist_add_idx
;
77 static int hist_cur
= -1;
78 static unsigned hist_num
;
80 static char *hist_list
[HIST_MAX
];
81 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
83 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
85 static void hist_init(void)
94 for (i
= 0; i
< HIST_MAX
; i
++) {
95 hist_list
[i
] = hist_lines
[i
];
96 hist_list
[i
][0] = '\0';
100 static void cread_add_to_hist(char *line
)
102 strcpy(hist_list
[hist_add_idx
], line
);
104 if (++hist_add_idx
>= HIST_MAX
)
107 if (hist_add_idx
> hist_max
)
108 hist_max
= hist_add_idx
;
113 static char *hist_prev(void)
125 if (hist_cur
== hist_add_idx
) {
129 ret
= hist_list
[hist_cur
];
135 static char *hist_next(void)
142 if (hist_cur
== hist_add_idx
)
145 if (++hist_cur
> hist_max
)
148 if (hist_cur
== hist_add_idx
)
151 ret
= hist_list
[hist_cur
];
156 #ifndef CONFIG_CMDLINE_EDITING
157 static void cread_print_hist_list(void)
162 n
= hist_num
- hist_max
;
164 i
= hist_add_idx
+ 1;
168 if (i
== hist_add_idx
)
170 printf("%s\n", hist_list
[i
]);
175 #endif /* CONFIG_CMDLINE_EDITING */
177 #define BEGINNING_OF_LINE() { \
179 getcmd_putch(CTL_BACKSPACE); \
184 #define ERASE_TO_EOL() { \
185 if (num < eol_num) { \
186 printf("%*s", (int)(eol_num - num), ""); \
188 getcmd_putch(CTL_BACKSPACE); \
189 } while (--eol_num > num); \
193 #define REFRESH_TO_EOL() { \
194 if (num < eol_num) { \
195 wlen = eol_num - num; \
196 putnstr(buf + num, wlen); \
201 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
202 unsigned long *eol_num
, char *buf
, unsigned long len
)
207 if (insert
|| *num
== *eol_num
) {
208 if (*eol_num
> len
- 1) {
216 wlen
= *eol_num
- *num
;
218 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
221 putnstr(buf
+ *num
, wlen
);
224 getcmd_putch(CTL_BACKSPACE
);
226 /* echo the character */
229 putnstr(buf
+ *num
, wlen
);
234 static void cread_add_str(char *str
, int strsize
, int insert
,
235 unsigned long *num
, unsigned long *eol_num
,
236 char *buf
, unsigned long len
)
239 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
244 static int cread_line(const char *const prompt
, char *buf
, unsigned int *len
,
247 unsigned long num
= 0;
248 unsigned long eol_num
= 0;
254 int init_len
= strlen(buf
);
258 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
261 if (bootretry_tstc_timeout())
262 return -2; /* timed out */
263 if (first
&& timeout
) {
264 uint64_t etime
= endtick(timeout
);
266 while (!tstc()) { /* while no incoming data */
267 if (get_ticks() >= etime
)
268 return -2; /* timed out */
274 ichar
= getcmd_getch();
276 /* ichar=0x0 when error occurs in U-Boot getc */
280 if ((ichar
== '\n') || (ichar
== '\r')) {
286 * handle standard linux xterm esc sequences for arrow key, etc.
289 enum { ESC_REJECT
, ESC_SAVE
, ESC_CONVERTED
} act
= ESC_REJECT
;
292 if (ichar
== '[' || ichar
== 'O')
294 } else if (esc_len
== 2) {
296 case 'D': /* <- key */
299 break; /* pass off to ^B handler */
300 case 'C': /* -> key */
303 break; /* pass off to ^F handler */
304 case 'H': /* Home key */
307 break; /* pass off to ^A handler */
308 case 'F': /* End key */
311 break; /* pass off to ^E 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 */
325 if (esc_save
[1] == '[') {
326 /* see if next character is ~ */
331 } else if (esc_len
== 3) {
333 switch (esc_save
[2]) {
334 case '3': /* Delete key */
337 break; /* pass to ^D handler */
338 case '1': /* Home key */
342 break; /* pass to ^A handler */
343 case '4': /* End key */
347 break; /* pass to ^E handler */
354 esc_save
[esc_len
++] = ichar
;
357 esc_save
[esc_len
++] = ichar
;
358 cread_add_str(esc_save
, esc_len
, insert
,
359 &num
, &eol_num
, buf
, *len
);
371 esc_save
[esc_len
] = ichar
;
374 puts("impossible condition #876\n");
382 case CTL_CH('c'): /* ^C - break */
383 *buf
= '\0'; /* discard input */
387 getcmd_putch(buf
[num
]);
393 getcmd_putch(CTL_BACKSPACE
);
399 wlen
= eol_num
- num
- 1;
401 memmove(&buf
[num
], &buf
[num
+1], wlen
);
402 putnstr(buf
+ num
, wlen
);
407 getcmd_putch(CTL_BACKSPACE
);
430 wlen
= eol_num
- num
;
432 memmove(&buf
[num
], &buf
[num
+1], wlen
);
433 getcmd_putch(CTL_BACKSPACE
);
434 putnstr(buf
+ num
, wlen
);
437 getcmd_putch(CTL_BACKSPACE
);
449 if (ichar
== CTL_CH('p'))
459 /* nuke the current line */
463 /* erase to end of line */
466 /* copy new line into place and display */
468 eol_num
= strlen(buf
);
472 #ifdef CONFIG_AUTO_COMPLETE
476 /* do not autocomplete when in the middle */
483 col
= strlen(prompt
) + eol_num
;
485 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
494 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
500 buf
[eol_num
] = '\0'; /* lose the newline */
502 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
503 cread_add_to_hist(buf
);
504 hist_cur
= hist_add_idx
;
509 #endif /* CONFIG_CMDLINE_EDITING */
511 /****************************************************************************/
513 int cli_readline(const char *const prompt
)
516 * If console_buffer isn't 0-length the user will be prompted to modify
517 * it instead of entering it from scratch as desired.
519 console_buffer
[0] = '\0';
521 return cli_readline_into_buffer(prompt
, console_buffer
, 0);
525 int cli_readline_into_buffer(const char *const prompt
, char *buffer
,
529 #ifdef CONFIG_CMDLINE_EDITING
530 unsigned int len
= CONFIG_SYS_CBSIZE
;
535 * History uses a global array which is not
536 * writable until after relocation to RAM.
537 * Revert to non-history version if still
538 * running from flash.
540 if (gd
->flags
& GD_FLG_RELOC
) {
549 rc
= cread_line(prompt
, p
, &len
, timeout
);
550 return rc
< 0 ? rc
: len
;
553 #endif /* CONFIG_CMDLINE_EDITING */
555 int n
= 0; /* buffer index */
556 int plen
= 0; /* prompt length */
557 int col
; /* output column cnt */
562 plen
= strlen(prompt
);
568 if (bootretry_tstc_timeout())
569 return -2; /* timed out */
570 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
572 #ifdef CONFIG_SHOW_ACTIVITY
581 * Special character handling
584 case '\r': /* Enter */
593 case 0x03: /* ^C - break */
594 p_buf
[0] = '\0'; /* discard input */
597 case 0x15: /* ^U - erase line */
606 case 0x17: /* ^W - erase word */
607 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
608 while ((n
> 0) && (*p
!= ' '))
609 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
612 case 0x08: /* ^H - backspace */
613 case 0x7F: /* DEL - backspace */
614 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
619 * Must be a normal character then
621 if (n
< CONFIG_SYS_CBSIZE
-2) {
622 if (c
== '\t') { /* expand TABs */
623 #ifdef CONFIG_AUTO_COMPLETE
625 * if auto completion triggered just
629 if (cmd_auto_complete(prompt
,
632 p
= p_buf
+ n
; /* reset */
636 puts(tab_seq
+ (col
& 07));
637 col
+= 8 - (col
& 07);
639 char __maybe_unused buf
[2];
642 * Echo input using puts() to force an
643 * LCD flush if we are using an LCD
652 } else { /* Buffer full */
657 #ifdef CONFIG_CMDLINE_EDITING