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_CH(c) ((c) - 'a' + 1)
66 #define CTL_BACKSPACE ('\b')
67 #define DEL ((char)255)
68 #define DEL7 ((char)127)
69 #define CREAD_HIST_CHAR ('!')
71 #define getcmd_putch(ch) putc(ch)
72 #define getcmd_getch() getchar()
73 #define getcmd_cbeep() getcmd_putch('\a')
76 #define HIST_SIZE CONFIG_SYS_CBSIZE
79 static int hist_add_idx;
80 static int hist_cur = -1;
81 static unsigned hist_num;
83 static char *hist_list[HIST_MAX];
84 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
86 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
88 static void hist_init(void)
97 for (i = 0; i < HIST_MAX; i++) {
98 hist_list[i] = hist_lines[i];
99 hist_list[i][0] = '\0';
103 static void cread_add_to_hist(char *line)
105 strcpy(hist_list[hist_add_idx], line);
107 if (++hist_add_idx >= HIST_MAX)
110 if (hist_add_idx > hist_max)
111 hist_max = hist_add_idx;
116 static char *hist_prev(void)
128 if (hist_cur == hist_add_idx) {
132 ret = hist_list[hist_cur];
138 static char *hist_next(void)
145 if (hist_cur == hist_add_idx)
148 if (++hist_cur > hist_max)
151 if (hist_cur == hist_add_idx)
154 ret = hist_list[hist_cur];
159 #ifndef CONFIG_CMDLINE_EDITING
160 static void cread_print_hist_list(void)
165 n = hist_num - hist_max;
167 i = hist_add_idx + 1;
171 if (i == hist_add_idx)
173 printf("%s\n", hist_list[i]);
178 #endif /* CONFIG_CMDLINE_EDITING */
180 #define BEGINNING_OF_LINE() { \
182 getcmd_putch(CTL_BACKSPACE); \
187 #define ERASE_TO_EOL() { \
188 if (num < eol_num) { \
189 printf("%*s", (int)(eol_num - num), ""); \
191 getcmd_putch(CTL_BACKSPACE); \
192 } while (--eol_num > num); \
196 #define REFRESH_TO_EOL() { \
197 if (num < eol_num) { \
198 wlen = eol_num - num; \
199 putnstr(buf + num, wlen); \
204 static void cread_add_char(char ichar, int insert, unsigned long *num,
205 unsigned long *eol_num, char *buf, unsigned long len)
210 if (insert || *num == *eol_num) {
211 if (*eol_num > len - 1) {
219 wlen = *eol_num - *num;
221 memmove(&buf[*num+1], &buf[*num], wlen-1);
224 putnstr(buf + *num, wlen);
227 getcmd_putch(CTL_BACKSPACE);
229 /* echo the character */
232 putnstr(buf + *num, wlen);
237 static void cread_add_str(char *str, int strsize, int insert,
238 unsigned long *num, unsigned long *eol_num,
239 char *buf, unsigned long len)
242 cread_add_char(*str, insert, num, eol_num, buf, len);
247 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
250 unsigned long num = 0;
251 unsigned long eol_num = 0;
257 int init_len = strlen(buf);
261 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
264 if (bootretry_tstc_timeout())
265 return -2; /* timed out */
266 if (first && timeout) {
267 uint64_t etime = endtick(timeout);
269 while (!tstc()) { /* while no incoming data */
270 if (get_ticks() >= etime)
271 return -2; /* timed out */
277 ichar = getcmd_getch();
279 /* ichar=0x0 when error occurs in U-Boot getc */
283 if ((ichar == '\n') || (ichar == '\r')) {
289 * handle standard linux xterm esc sequences for arrow key, etc.
292 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
295 if (ichar == '[' || ichar == 'O')
297 } else if (esc_len == 2) {
299 case 'D': /* <- key */
302 break; /* pass off to ^B handler */
303 case 'C': /* -> key */
306 break; /* pass off to ^F handler */
307 case 'H': /* Home key */
310 break; /* pass off to ^A handler */
311 case 'F': /* End key */
314 break; /* pass off to ^E handler */
315 case 'A': /* up arrow */
318 break; /* pass off to ^P handler */
319 case 'B': /* down arrow */
322 break; /* pass off to ^N handler */
328 if (esc_save[1] == '[') {
329 /* see if next character is ~ */
334 } else if (esc_len == 3) {
336 switch (esc_save[2]) {
337 case '3': /* Delete key */
340 break; /* pass to ^D handler */
341 case '1': /* Home key */
345 break; /* pass to ^A handler */
346 case '4': /* End key */
350 break; /* pass to ^E handler */
357 esc_save[esc_len++] = ichar;
360 esc_save[esc_len++] = ichar;
361 cread_add_str(esc_save, esc_len, insert,
362 &num, &eol_num, buf, *len);
374 esc_save[esc_len] = ichar;
377 puts("impossible condition #876\n");
385 case CTL_CH('c'): /* ^C - break */
386 *buf = '\0'; /* discard input */
390 getcmd_putch(buf[num]);
396 getcmd_putch(CTL_BACKSPACE);
402 wlen = eol_num - num - 1;
404 memmove(&buf[num], &buf[num+1], wlen);
405 putnstr(buf + num, wlen);
410 getcmd_putch(CTL_BACKSPACE);
433 wlen = eol_num - num;
435 memmove(&buf[num], &buf[num+1], wlen);
436 getcmd_putch(CTL_BACKSPACE);
437 putnstr(buf + num, wlen);
440 getcmd_putch(CTL_BACKSPACE);
452 if (ichar == CTL_CH('p'))
462 /* nuke the current line */
466 /* erase to end of line */
469 /* copy new line into place and display */
471 eol_num = strlen(buf);
475 #ifdef CONFIG_AUTO_COMPLETE
479 /* do not autocomplete when in the middle */
486 col = strlen(prompt) + eol_num;
488 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
497 if (ichar >= ' ' && ichar <= '~') {
498 cread_add_char(ichar, insert, &num, &eol_num,
505 buf[eol_num] = '\0'; /* lose the newline */
507 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
508 cread_add_to_hist(buf);
509 hist_cur = hist_add_idx;
514 #endif /* CONFIG_CMDLINE_EDITING */
516 /****************************************************************************/
518 int cli_readline(const char *const prompt)
521 * If console_buffer isn't 0-length the user will be prompted to modify
522 * it instead of entering it from scratch as desired.
524 console_buffer[0] = '\0';
526 return cli_readline_into_buffer(prompt, console_buffer, 0);
530 int cli_readline_into_buffer(const char *const prompt, char *buffer,
534 #ifdef CONFIG_CMDLINE_EDITING
535 unsigned int len = CONFIG_SYS_CBSIZE;
540 * History uses a global array which is not
541 * writable until after relocation to RAM.
542 * Revert to non-history version if still
543 * running from flash.
545 if (gd->flags & GD_FLG_RELOC) {
554 rc = cread_line(prompt, p, &len, timeout);
555 return rc < 0 ? rc : len;
558 #endif /* CONFIG_CMDLINE_EDITING */
560 int n = 0; /* buffer index */
561 int plen = 0; /* prompt length */
562 int col; /* output column cnt */
567 plen = strlen(prompt);
573 if (bootretry_tstc_timeout())
574 return -2; /* timed out */
575 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
580 * Special character handling
583 case '\r': /* Enter */
592 case 0x03: /* ^C - break */
593 p_buf[0] = '\0'; /* discard input */
596 case 0x15: /* ^U - erase line */
605 case 0x17: /* ^W - erase word */
606 p = delete_char(p_buf, p, &col, &n, plen);
607 while ((n > 0) && (*p != ' '))
608 p = delete_char(p_buf, p, &col, &n, plen);
611 case 0x08: /* ^H - backspace */
612 case 0x7F: /* DEL - backspace */
613 p = delete_char(p_buf, p, &col, &n, plen);
618 * Must be a normal character then
620 if (n < CONFIG_SYS_CBSIZE-2) {
621 if (c == '\t') { /* expand TABs */
622 #ifdef CONFIG_AUTO_COMPLETE
624 * if auto completion triggered just
628 if (cmd_auto_complete(prompt,
631 p = p_buf + n; /* reset */
635 puts(tab_seq + (col & 07));
636 col += 8 - (col & 07);
638 char __maybe_unused buf[2];
641 * Echo input using puts() to force an
642 * LCD flush if we are using an LCD
651 } else { /* Buffer full */
656 #ifdef CONFIG_CMDLINE_EDITING