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>
11 #include <bootretry.h>
18 #include <linux/errno.h>
19 #include <asm/global_data.h>
21 DECLARE_GLOBAL_DATA_PTR;
23 static const char erase_seq[] = "\b \b"; /* erase sequence */
24 static const char tab_seq[] = " "; /* used to expand TABs */
26 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
28 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
35 if (*(--p) == '\t') { /* will retype the whole line */
36 while (*colp > plen) {
40 for (s = buffer; s < p; ++s) {
42 puts(tab_seq + ((*colp) & 07));
43 *colp += 8 - ((*colp) & 07);
58 #ifdef CONFIG_CMDLINE_EDITING
61 * cmdline-editing related codes from vivi.
62 * Author: Janghoon Lyu <nandy@mizi.com>
65 #define putnstr(str, n) printf("%.*s", (int)n, str)
67 #define CTL_BACKSPACE ('\b')
68 #define DEL ((char)255)
69 #define DEL7 ((char)127)
70 #define CREAD_HIST_CHAR ('!')
72 #define getcmd_putch(ch) putc(ch)
73 #define getcmd_getch() getchar()
74 #define getcmd_cbeep() getcmd_putch('\a')
76 #ifdef CONFIG_SPL_BUILD
81 #define HIST_SIZE CONFIG_SYS_CBSIZE
85 static int hist_add_idx;
86 static int hist_cur = -1;
87 static unsigned hist_num;
89 #ifndef CONFIG_CMD_HISTORY_USE_CALLOC
90 static char hist_data[HIST_MAX][HIST_SIZE + 1];
92 static char *hist_list[HIST_MAX];
94 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
96 static void getcmd_putchars(int count, int ch)
100 for (i = 0; i < count; i++)
104 static int hist_init(void)
108 #ifndef CONFIG_CMD_HISTORY_USE_CALLOC
109 for (i = 0; i < HIST_MAX; i++) {
110 hist_list[i] = hist_data[i];
111 hist_list[i][0] = '\0';
114 unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1);
116 panic("%s: calloc: out of memory!\n", __func__);
118 for (i = 0; i < HIST_MAX; i++)
119 hist_list[i] = hist + (i * (HIST_SIZE + 1));
130 static void cread_add_to_hist(char *line)
132 strcpy(hist_list[hist_add_idx], line);
134 if (++hist_add_idx >= HIST_MAX)
137 if (hist_add_idx > hist_max)
138 hist_max = hist_add_idx;
143 static char *hist_prev(void)
155 if (hist_cur == hist_add_idx) {
159 ret = hist_list[hist_cur];
165 static char *hist_next(void)
172 if (hist_cur == hist_add_idx)
175 if (++hist_cur > hist_max)
178 if (hist_cur == hist_add_idx)
181 ret = hist_list[hist_cur];
186 void cread_print_hist_list(void)
191 n = hist_num - hist_max;
193 i = hist_add_idx + 1;
197 if (i == hist_add_idx)
199 printf("%s\n", hist_list[i]);
205 #define BEGINNING_OF_LINE() { \
207 getcmd_putch(CTL_BACKSPACE); \
212 #define ERASE_TO_EOL() { \
213 if (cls->num < cls->eol_num) { \
214 printf("%*s", (int)(cls->eol_num - cls->num), ""); \
216 getcmd_putch(CTL_BACKSPACE); \
217 } while (--cls->eol_num > cls->num); \
221 #define REFRESH_TO_EOL() { \
222 if (cls->num < cls->eol_num) { \
223 uint wlen = cls->eol_num - cls->num; \
224 putnstr(buf + cls->num, wlen); \
225 cls->num = cls->eol_num; \
229 static void cread_add_char(char ichar, int insert, uint *num,
230 uint *eol_num, char *buf, uint len)
235 if (insert || *num == *eol_num) {
236 if (*eol_num > len - 1) {
244 wlen = *eol_num - *num;
246 memmove(&buf[*num+1], &buf[*num], wlen-1);
249 putnstr(buf + *num, wlen);
252 getcmd_putch(CTL_BACKSPACE);
254 /* echo the character */
257 putnstr(buf + *num, wlen);
262 static void cread_add_str(char *str, int strsize, int insert,
263 uint *num, uint *eol_num, char *buf, uint len)
266 cread_add_char(*str, insert, num, eol_num, buf, len);
271 int cread_line_process_ch(struct cli_line_state *cls, char ichar)
273 char *buf = cls->buf;
275 /* ichar=0x0 when error occurs in U-Boot getc */
281 buf[cls->eol_num] = '\0'; /* terminate the string */
289 case CTL_CH('c'): /* ^C - break */
290 *buf = '\0'; /* discard input */
293 if (cls->num < cls->eol_num) {
294 getcmd_putch(buf[cls->num]);
300 getcmd_putch(CTL_BACKSPACE);
305 if (cls->num < cls->eol_num) {
308 wlen = cls->eol_num - cls->num - 1;
310 memmove(&buf[cls->num], &buf[cls->num + 1],
312 putnstr(buf + cls->num, wlen);
317 getcmd_putch(CTL_BACKSPACE);
329 cls->insert = !cls->insert;
335 for (base = cls->num - 1;
336 base >= 0 && buf[base] == ' ';)
338 for (; base > 0 && buf[base - 1] != ' ';)
341 /* now delete chars from base to cls->num */
342 wlen = cls->num - base;
343 cls->eol_num -= wlen;
344 memmove(&buf[base], &buf[cls->num],
345 cls->eol_num - base + 1);
347 getcmd_putchars(wlen, CTL_BACKSPACE);
349 getcmd_putchars(wlen, ' ');
350 getcmd_putchars(wlen + cls->eol_num - cls->num,
365 wlen = cls->eol_num - cls->num;
367 memmove(&buf[cls->num], &buf[cls->num + 1], wlen);
368 getcmd_putch(CTL_BACKSPACE);
369 putnstr(buf + cls->num, wlen);
372 getcmd_putch(CTL_BACKSPACE);
382 if (ichar == CTL_CH('p'))
392 /* nuke the current line */
396 /* erase to end of line */
399 /* copy new line into place and display */
401 cls->eol_num = strlen(buf);
407 if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
410 /* do not autocomplete when in the middle */
411 if (cls->num < cls->eol_num) {
416 buf[cls->num] = '\0';
417 col = strlen(cls->prompt) + cls->eol_num;
419 if (cmd_auto_complete(cls->prompt, buf, &num2, &col)) {
420 col = num2 - cls->num;
428 cread_add_char(ichar, cls->insert, &cls->num, &cls->eol_num,
434 * keep the string terminated...if we added a char at the end then we
437 buf[cls->eol_num] = '\0';
442 void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size)
444 int init_len = strlen(buf);
446 memset(cls, '\0', sizeof(struct cli_line_state));
452 cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf,
456 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
459 struct cli_ch_state s_cch, *cch = &s_cch;
460 struct cli_line_state s_cls, *cls = &s_cls;
465 cli_cread_init(cls, buf, *len);
466 cls->prompt = prompt;
468 cls->cmd_complete = true;
473 /* Check for saved characters */
474 ichar = cli_ch_process(cch, 0);
477 if (bootretry_tstc_timeout())
478 return -2; /* timed out */
479 if (first && timeout) {
480 u64 etime = endtick(timeout);
482 while (!tstc()) { /* while no incoming data */
483 if (get_ticks() >= etime)
484 return -2; /* timed out */
490 ichar = getcmd_getch();
491 ichar = cli_ch_process(cch, ichar);
494 ret = cread_line_process_ch(cls, ichar);
502 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
503 cread_add_to_hist(buf);
504 hist_cur = hist_add_idx;
509 #else /* !CONFIG_CMDLINE_EDITING */
511 static inline int hist_init(void)
516 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
522 #endif /* CONFIG_CMDLINE_EDITING */
524 /****************************************************************************/
526 int cli_readline(const char *const prompt)
529 * If console_buffer isn't 0-length the user will be prompted to modify
530 * it instead of entering it from scratch as desired.
532 console_buffer[0] = '\0';
534 return cli_readline_into_buffer(prompt, console_buffer, 0);
538 * cread_line_simple() - Simple (small) command-line reader
540 * This supports only basic editing, with no cursor movement
542 * @prompt: Prompt to display
543 * @p: Text buffer to edit
544 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
546 static int cread_line_simple(const char *const prompt, char *p)
549 int n = 0; /* buffer index */
550 int plen = 0; /* prompt length */
551 int col; /* output column cnt */
556 plen = strlen(prompt);
562 if (bootretry_tstc_timeout())
563 return -2; /* timed out */
564 schedule(); /* Trigger watchdog, if needed */
569 * Special character handling
572 case '\r': /* Enter */
581 case 0x03: /* ^C - break */
582 p_buf[0] = '\0'; /* discard input */
585 case 0x15: /* ^U - erase line */
594 case 0x17: /* ^W - erase word */
595 p = delete_char(p_buf, p, &col, &n, plen);
596 while ((n > 0) && (*p != ' '))
597 p = delete_char(p_buf, p, &col, &n, plen);
600 case 0x08: /* ^H - backspace */
601 case 0x7F: /* DEL - backspace */
602 p = delete_char(p_buf, p, &col, &n, plen);
606 /* Must be a normal character then */
607 if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */
611 if (c == '\t') { /* expand TABs */
612 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
614 * if auto-completion triggered just
618 if (cmd_auto_complete(prompt,
621 p = p_buf + n; /* reset */
625 puts(tab_seq + (col & 07));
626 col += 8 - (col & 07);
628 char __maybe_unused buf[2];
631 * Echo input using puts() to force an LCD
632 * flush if we are using an LCD
646 int cli_readline_into_buffer(const char *const prompt, char *buffer,
650 uint len = CONFIG_SYS_CBSIZE;
655 * Say N to CMD_HISTORY_USE_CALLOC will skip runtime
656 * allocation for the history buffer and directly
657 * use an uninitialized static array as the buffer.
658 * Doing this might have better performance and not
659 * increase the binary file's size, as it only marks
660 * the size. However, the array is only writable after
661 * relocation to RAM. If u-boot is running from ROM
662 * all the time, consider say Y to CMD_HISTORY_USE_CALLOC
663 * or disable CMD_HISTORY.
665 if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
675 rc = cread_line(prompt, p, &len, timeout);
676 return rc < 0 ? rc : len;
679 return cread_line_simple(prompt, p);