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 #include <asm/global_data.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 static const char erase_seq[] = "\b \b"; /* erase sequence */
22 static const char tab_seq[] = " "; /* used to expand TABs */
24 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
26 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
33 if (*(--p) == '\t') { /* will retype the whole line */
34 while (*colp > plen) {
38 for (s = buffer; s < p; ++s) {
40 puts(tab_seq + ((*colp) & 07));
41 *colp += 8 - ((*colp) & 07);
56 #ifdef CONFIG_CMDLINE_EDITING
59 * cmdline-editing related codes from vivi.
60 * Author: Janghoon Lyu <nandy@mizi.com>
63 #define putnstr(str, n) printf("%.*s", (int)n, str)
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')
74 #ifdef CONFIG_SPL_BUILD
79 #define HIST_SIZE CONFIG_SYS_CBSIZE
83 static int hist_add_idx;
84 static int hist_cur = -1;
85 static unsigned hist_num;
87 static char *hist_list[HIST_MAX];
88 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
90 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
92 static void hist_init(void)
101 for (i = 0; i < HIST_MAX; i++) {
102 hist_list[i] = hist_lines[i];
103 hist_list[i][0] = '\0';
107 static void cread_add_to_hist(char *line)
109 strcpy(hist_list[hist_add_idx], line);
111 if (++hist_add_idx >= HIST_MAX)
114 if (hist_add_idx > hist_max)
115 hist_max = hist_add_idx;
120 static char *hist_prev(void)
132 if (hist_cur == hist_add_idx) {
136 ret = hist_list[hist_cur];
142 static char *hist_next(void)
149 if (hist_cur == hist_add_idx)
152 if (++hist_cur > hist_max)
155 if (hist_cur == hist_add_idx)
158 ret = hist_list[hist_cur];
163 #ifndef CONFIG_CMDLINE_EDITING
164 static void cread_print_hist_list(void)
169 n = hist_num - hist_max;
171 i = hist_add_idx + 1;
175 if (i == hist_add_idx)
177 printf("%s\n", hist_list[i]);
182 #endif /* CONFIG_CMDLINE_EDITING */
184 #define BEGINNING_OF_LINE() { \
186 getcmd_putch(CTL_BACKSPACE); \
191 #define ERASE_TO_EOL() { \
192 if (num < eol_num) { \
193 printf("%*s", (int)(eol_num - num), ""); \
195 getcmd_putch(CTL_BACKSPACE); \
196 } while (--eol_num > num); \
200 #define REFRESH_TO_EOL() { \
201 if (num < eol_num) { \
202 wlen = eol_num - num; \
203 putnstr(buf + num, wlen); \
208 static void cread_add_char(char ichar, int insert, unsigned long *num,
209 unsigned long *eol_num, char *buf, unsigned long len)
214 if (insert || *num == *eol_num) {
215 if (*eol_num > len - 1) {
223 wlen = *eol_num - *num;
225 memmove(&buf[*num+1], &buf[*num], wlen-1);
228 putnstr(buf + *num, wlen);
231 getcmd_putch(CTL_BACKSPACE);
233 /* echo the character */
236 putnstr(buf + *num, wlen);
241 static void cread_add_str(char *str, int strsize, int insert,
242 unsigned long *num, unsigned long *eol_num,
243 char *buf, unsigned long len)
246 cread_add_char(*str, insert, num, eol_num, buf, len);
251 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
254 struct cli_ch_state s_cch, *cch = &s_cch;
255 unsigned long num = 0;
256 unsigned long eol_num = 0;
260 int init_len = strlen(buf);
266 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
269 /* Check for saved characters */
270 ichar = cli_ch_process(cch, 0);
273 if (bootretry_tstc_timeout())
274 return -2; /* timed out */
275 if (first && timeout) {
276 u64 etime = endtick(timeout);
278 while (!tstc()) { /* while no incoming data */
279 if (get_ticks() >= etime)
280 return -2; /* timed out */
286 ichar = getcmd_getch();
287 ichar = cli_ch_process(cch, ichar);
290 /* ichar=0x0 when error occurs in U-Boot getc */
303 case CTL_CH('c'): /* ^C - break */
304 *buf = '\0'; /* discard input */
308 getcmd_putch(buf[num]);
314 getcmd_putch(CTL_BACKSPACE);
320 wlen = eol_num - num - 1;
322 memmove(&buf[num], &buf[num+1], wlen);
323 putnstr(buf + num, wlen);
328 getcmd_putch(CTL_BACKSPACE);
351 wlen = eol_num - num;
353 memmove(&buf[num], &buf[num+1], wlen);
354 getcmd_putch(CTL_BACKSPACE);
355 putnstr(buf + num, wlen);
358 getcmd_putch(CTL_BACKSPACE);
368 if (ichar == CTL_CH('p'))
378 /* nuke the current line */
382 /* erase to end of line */
385 /* copy new line into place and display */
387 eol_num = strlen(buf);
391 #ifdef CONFIG_AUTO_COMPLETE
395 /* do not autocomplete when in the middle */
402 col = strlen(prompt) + eol_num;
404 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
413 cread_add_char(ichar, insert, &num, &eol_num, buf,
419 buf[eol_num] = '\0'; /* lose the newline */
421 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
422 cread_add_to_hist(buf);
423 hist_cur = hist_add_idx;
428 #endif /* CONFIG_CMDLINE_EDITING */
430 /****************************************************************************/
432 int cli_readline(const char *const prompt)
435 * If console_buffer isn't 0-length the user will be prompted to modify
436 * it instead of entering it from scratch as desired.
438 console_buffer[0] = '\0';
440 return cli_readline_into_buffer(prompt, console_buffer, 0);
444 int cli_readline_into_buffer(const char *const prompt, char *buffer,
448 #ifdef CONFIG_CMDLINE_EDITING
449 unsigned int len = CONFIG_SYS_CBSIZE;
454 * History uses a global array which is not
455 * writable until after relocation to RAM.
456 * Revert to non-history version if still
457 * running from flash.
459 if (gd->flags & GD_FLG_RELOC) {
468 rc = cread_line(prompt, p, &len, timeout);
469 return rc < 0 ? rc : len;
472 #endif /* CONFIG_CMDLINE_EDITING */
474 int n = 0; /* buffer index */
475 int plen = 0; /* prompt length */
476 int col; /* output column cnt */
481 plen = strlen(prompt);
487 if (bootretry_tstc_timeout())
488 return -2; /* timed out */
489 schedule(); /* Trigger watchdog, if needed */
494 * Special character handling
497 case '\r': /* Enter */
506 case 0x03: /* ^C - break */
507 p_buf[0] = '\0'; /* discard input */
510 case 0x15: /* ^U - erase line */
519 case 0x17: /* ^W - erase word */
520 p = delete_char(p_buf, p, &col, &n, plen);
521 while ((n > 0) && (*p != ' '))
522 p = delete_char(p_buf, p, &col, &n, plen);
525 case 0x08: /* ^H - backspace */
526 case 0x7F: /* DEL - backspace */
527 p = delete_char(p_buf, p, &col, &n, plen);
532 * Must be a normal character then
534 if (n < CONFIG_SYS_CBSIZE-2) {
535 if (c == '\t') { /* expand TABs */
536 #ifdef CONFIG_AUTO_COMPLETE
538 * if auto completion triggered just
542 if (cmd_auto_complete(prompt,
545 p = p_buf + n; /* reset */
549 puts(tab_seq + (col & 07));
550 col += 8 - (col & 07);
552 char __maybe_unused buf[2];
555 * Echo input using puts() to force an
556 * LCD flush if we are using an LCD
565 } else { /* Buffer full */
570 #ifdef CONFIG_CMDLINE_EDITING