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 if ((ichar == '\n') || (ichar == '\r')) {
282 * handle standard linux xterm esc sequences for arrow key, etc.
285 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
288 if (ichar == '[' || ichar == 'O')
290 } else if (esc_len == 2) {
292 case 'D': /* <- key */
295 break; /* pass off to ^B handler */
296 case 'C': /* -> key */
299 break; /* pass off to ^F handler */
300 case 'H': /* Home key */
303 break; /* pass off to ^A handler */
304 case 'F': /* End key */
307 break; /* pass off to ^E handler */
308 case 'A': /* up arrow */
311 break; /* pass off to ^P handler */
312 case 'B': /* down arrow */
315 break; /* pass off to ^N handler */
321 if (esc_save[1] == '[') {
322 /* see if next character is ~ */
327 } else if (esc_len == 3) {
329 switch (esc_save[2]) {
330 case '3': /* Delete key */
333 break; /* pass to ^D handler */
334 case '1': /* Home key */
338 break; /* pass to ^A handler */
339 case '4': /* End key */
343 break; /* pass to ^E handler */
350 esc_save[esc_len++] = ichar;
353 esc_save[esc_len++] = ichar;
354 cread_add_str(esc_save, esc_len, insert,
355 &num, &eol_num, buf, *len);
367 esc_save[esc_len] = ichar;
370 puts("impossible condition #876\n");
378 case CTL_CH('c'): /* ^C - break */
379 *buf = '\0'; /* discard input */
383 getcmd_putch(buf[num]);
389 getcmd_putch(CTL_BACKSPACE);
395 wlen = eol_num - num - 1;
397 memmove(&buf[num], &buf[num+1], wlen);
398 putnstr(buf + num, wlen);
403 getcmd_putch(CTL_BACKSPACE);
426 wlen = eol_num - num;
428 memmove(&buf[num], &buf[num+1], wlen);
429 getcmd_putch(CTL_BACKSPACE);
430 putnstr(buf + num, wlen);
433 getcmd_putch(CTL_BACKSPACE);
445 if (ichar == CTL_CH('p'))
455 /* nuke the current line */
459 /* erase to end of line */
462 /* copy new line into place and display */
464 eol_num = strlen(buf);
468 #ifdef CONFIG_AUTO_COMPLETE
472 /* do not autocomplete when in the middle */
479 col = strlen(prompt) + eol_num;
481 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
490 cread_add_char(ichar, insert, &num, &eol_num, buf,
496 buf[eol_num] = '\0'; /* lose the newline */
498 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
499 cread_add_to_hist(buf);
500 hist_cur = hist_add_idx;
505 #endif /* CONFIG_CMDLINE_EDITING */
507 /****************************************************************************/
509 int cli_readline(const char *const prompt)
512 * If console_buffer isn't 0-length the user will be prompted to modify
513 * it instead of entering it from scratch as desired.
515 console_buffer[0] = '\0';
517 return cli_readline_into_buffer(prompt, console_buffer, 0);
521 int cli_readline_into_buffer(const char *const prompt, char *buffer,
525 #ifdef CONFIG_CMDLINE_EDITING
526 unsigned int len = CONFIG_SYS_CBSIZE;
531 * History uses a global array which is not
532 * writable until after relocation to RAM.
533 * Revert to non-history version if still
534 * running from flash.
536 if (gd->flags & GD_FLG_RELOC) {
545 rc = cread_line(prompt, p, &len, timeout);
546 return rc < 0 ? rc : len;
549 #endif /* CONFIG_CMDLINE_EDITING */
551 int n = 0; /* buffer index */
552 int plen = 0; /* prompt length */
553 int col; /* output column cnt */
558 plen = strlen(prompt);
564 if (bootretry_tstc_timeout())
565 return -2; /* timed out */
566 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
568 #ifdef CONFIG_SHOW_ACTIVITY
577 * Special character handling
580 case '\r': /* Enter */
589 case 0x03: /* ^C - break */
590 p_buf[0] = '\0'; /* discard input */
593 case 0x15: /* ^U - erase line */
602 case 0x17: /* ^W - erase word */
603 p = delete_char(p_buf, p, &col, &n, plen);
604 while ((n > 0) && (*p != ' '))
605 p = delete_char(p_buf, p, &col, &n, plen);
608 case 0x08: /* ^H - backspace */
609 case 0x7F: /* DEL - backspace */
610 p = delete_char(p_buf, p, &col, &n, plen);
615 * Must be a normal character then
617 if (n < CONFIG_SYS_CBSIZE-2) {
618 if (c == '\t') { /* expand TABs */
619 #ifdef CONFIG_AUTO_COMPLETE
621 * if auto completion triggered just
625 if (cmd_auto_complete(prompt,
628 p = p_buf + n; /* reset */
632 puts(tab_seq + (col & 07));
633 col += 8 - (col & 07);
635 char __maybe_unused buf[2];
638 * Echo input using puts() to force an
639 * LCD flush if we are using an LCD
648 } else { /* Buffer full */
653 #ifdef CONFIG_CMDLINE_EDITING