blob: 03624ca18afe84d431ce4a62ad68c7ab6e06bd2a [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass6493ccc2014-04-10 20:01:26 -06002/*
3 * (C) Copyright 2000
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 *
6 * Add to readline cmdline-editing by
7 * (C) Copyright 2005
8 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
Simon Glass6493ccc2014-04-10 20:01:26 -06009 */
10
11#include <common.h>
Simon Glass0098e172014-04-10 20:01:30 -060012#include <bootretry.h>
Simon Glass6493ccc2014-04-10 20:01:26 -060013#include <cli.h>
Simon Glass09140112020-05-10 11:40:03 -060014#include <command.h>
Simon Glass10453152019-11-14 12:57:30 -070015#include <time.h>
Simon Glass6493ccc2014-04-10 20:01:26 -060016#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060017#include <asm/global_data.h>
Simon Glass6493ccc2014-04-10 20:01:26 -060018
19DECLARE_GLOBAL_DATA_PTR;
20
21static const char erase_seq[] = "\b \b"; /* erase sequence */
22static const char tab_seq[] = " "; /* used to expand TABs */
23
Simon Glass6493ccc2014-04-10 20:01:26 -060024char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
25
Simon Glass6493ccc2014-04-10 20:01:26 -060026static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
27{
28 char *s;
29
30 if (*np == 0)
31 return p;
32
33 if (*(--p) == '\t') { /* will retype the whole line */
34 while (*colp > plen) {
35 puts(erase_seq);
36 (*colp)--;
37 }
38 for (s = buffer; s < p; ++s) {
39 if (*s == '\t') {
40 puts(tab_seq + ((*colp) & 07));
41 *colp += 8 - ((*colp) & 07);
42 } else {
43 ++(*colp);
44 putc(*s);
45 }
46 }
47 } else {
48 puts(erase_seq);
49 (*colp)--;
50 }
51 (*np)--;
52
53 return p;
54}
55
56#ifdef CONFIG_CMDLINE_EDITING
57
58/*
59 * cmdline-editing related codes from vivi.
60 * Author: Janghoon Lyu <nandy@mizi.com>
61 */
62
63#define putnstr(str, n) printf("%.*s", (int)n, str)
64
Simon Glass6493ccc2014-04-10 20:01:26 -060065#define CTL_BACKSPACE ('\b')
66#define DEL ((char)255)
67#define DEL7 ((char)127)
68#define CREAD_HIST_CHAR ('!')
69
70#define getcmd_putch(ch) putc(ch)
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020071#define getcmd_getch() getchar()
Simon Glass6493ccc2014-04-10 20:01:26 -060072#define getcmd_cbeep() getcmd_putch('\a')
73
Sean Anderson069f0d72022-08-30 16:40:37 -040074#ifdef CONFIG_SPL_BUILD
75#define HIST_MAX 3
76#define HIST_SIZE 32
77#else
Simon Glass6493ccc2014-04-10 20:01:26 -060078#define HIST_MAX 20
79#define HIST_SIZE CONFIG_SYS_CBSIZE
Sean Anderson069f0d72022-08-30 16:40:37 -040080#endif
Simon Glass6493ccc2014-04-10 20:01:26 -060081
82static int hist_max;
83static int hist_add_idx;
84static int hist_cur = -1;
85static unsigned hist_num;
86
87static char *hist_list[HIST_MAX];
88static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
89
90#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
91
Simon Glassdcc18ce2023-10-01 19:13:09 -060092static void getcmd_putchars(int count, int ch)
93{
94 int i;
95
96 for (i = 0; i < count; i++)
97 getcmd_putch(ch);
98}
99
Simon Glass6493ccc2014-04-10 20:01:26 -0600100static void hist_init(void)
101{
102 int i;
103
104 hist_max = 0;
105 hist_add_idx = 0;
106 hist_cur = -1;
107 hist_num = 0;
108
109 for (i = 0; i < HIST_MAX; i++) {
110 hist_list[i] = hist_lines[i];
111 hist_list[i][0] = '\0';
112 }
113}
114
115static void cread_add_to_hist(char *line)
116{
117 strcpy(hist_list[hist_add_idx], line);
118
119 if (++hist_add_idx >= HIST_MAX)
120 hist_add_idx = 0;
121
122 if (hist_add_idx > hist_max)
123 hist_max = hist_add_idx;
124
125 hist_num++;
126}
127
128static char *hist_prev(void)
129{
130 char *ret;
131 int old_cur;
132
133 if (hist_cur < 0)
134 return NULL;
135
136 old_cur = hist_cur;
137 if (--hist_cur < 0)
138 hist_cur = hist_max;
139
140 if (hist_cur == hist_add_idx) {
141 hist_cur = old_cur;
142 ret = NULL;
143 } else {
144 ret = hist_list[hist_cur];
145 }
146
147 return ret;
148}
149
150static char *hist_next(void)
151{
152 char *ret;
153
154 if (hist_cur < 0)
155 return NULL;
156
157 if (hist_cur == hist_add_idx)
158 return NULL;
159
160 if (++hist_cur > hist_max)
161 hist_cur = 0;
162
163 if (hist_cur == hist_add_idx)
164 ret = "";
165 else
166 ret = hist_list[hist_cur];
167
168 return ret;
169}
170
Simon Glass33eb0b92023-10-01 19:13:06 -0600171void cread_print_hist_list(void)
Simon Glass6493ccc2014-04-10 20:01:26 -0600172{
173 int i;
Simon Glassfedd3722023-10-01 19:13:10 -0600174 uint n;
Simon Glass6493ccc2014-04-10 20:01:26 -0600175
176 n = hist_num - hist_max;
177
178 i = hist_add_idx + 1;
179 while (1) {
180 if (i > hist_max)
181 i = 0;
182 if (i == hist_add_idx)
183 break;
184 printf("%s\n", hist_list[i]);
185 n++;
186 i++;
187 }
188}
Simon Glass6493ccc2014-04-10 20:01:26 -0600189
190#define BEGINNING_OF_LINE() { \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600191 while (cls->num) { \
Simon Glass6493ccc2014-04-10 20:01:26 -0600192 getcmd_putch(CTL_BACKSPACE); \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600193 cls->num--; \
Simon Glass6493ccc2014-04-10 20:01:26 -0600194 } \
195}
196
197#define ERASE_TO_EOL() { \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600198 if (cls->num < cls->eol_num) { \
199 printf("%*s", (int)(cls->eol_num - cls->num), ""); \
Simon Glass6493ccc2014-04-10 20:01:26 -0600200 do { \
201 getcmd_putch(CTL_BACKSPACE); \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600202 } while (--cls->eol_num > cls->num); \
Simon Glass6493ccc2014-04-10 20:01:26 -0600203 } \
204}
205
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600206#define REFRESH_TO_EOL() { \
207 if (cls->num < cls->eol_num) { \
208 uint wlen = cls->eol_num - cls->num; \
209 putnstr(buf + cls->num, wlen); \
210 cls->num = cls->eol_num; \
211 } \
Simon Glass6493ccc2014-04-10 20:01:26 -0600212}
213
Simon Glassfedd3722023-10-01 19:13:10 -0600214static void cread_add_char(char ichar, int insert, uint *num,
215 uint *eol_num, char *buf, uint len)
Simon Glass6493ccc2014-04-10 20:01:26 -0600216{
Simon Glassfedd3722023-10-01 19:13:10 -0600217 uint wlen;
Simon Glass6493ccc2014-04-10 20:01:26 -0600218
219 /* room ??? */
220 if (insert || *num == *eol_num) {
221 if (*eol_num > len - 1) {
222 getcmd_cbeep();
223 return;
224 }
225 (*eol_num)++;
226 }
227
228 if (insert) {
229 wlen = *eol_num - *num;
230 if (wlen > 1)
231 memmove(&buf[*num+1], &buf[*num], wlen-1);
232
233 buf[*num] = ichar;
234 putnstr(buf + *num, wlen);
235 (*num)++;
236 while (--wlen)
237 getcmd_putch(CTL_BACKSPACE);
238 } else {
239 /* echo the character */
240 wlen = 1;
241 buf[*num] = ichar;
242 putnstr(buf + *num, wlen);
243 (*num)++;
244 }
245}
246
247static void cread_add_str(char *str, int strsize, int insert,
Simon Glassfedd3722023-10-01 19:13:10 -0600248 uint *num, uint *eol_num, char *buf, uint len)
Simon Glass6493ccc2014-04-10 20:01:26 -0600249{
250 while (strsize--) {
251 cread_add_char(*str, insert, num, eol_num, buf, len);
252 str++;
253 }
254}
255
256static int cread_line(const char *const prompt, char *buf, unsigned int *len,
257 int timeout)
258{
Simon Glassb08e9d42023-01-06 08:52:20 -0600259 struct cli_ch_state s_cch, *cch = &s_cch;
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600260 struct cli_line_state s_cls, *cls = &s_cls;
Simon Glass6493ccc2014-04-10 20:01:26 -0600261 char ichar;
Simon Glass6493ccc2014-04-10 20:01:26 -0600262 int init_len = strlen(buf);
263 int first = 1;
264
Simon Glassb08e9d42023-01-06 08:52:20 -0600265 cli_ch_init(cch);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600266 memset(cls, '\0', sizeof(struct cli_line_state));
267 cls->insert = true;
Simon Glassb08e9d42023-01-06 08:52:20 -0600268
Simon Glass6493ccc2014-04-10 20:01:26 -0600269 if (init_len)
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600270 cread_add_str(buf, init_len, 1, &cls->num, &cls->eol_num, buf,
271 *len);
Simon Glass6493ccc2014-04-10 20:01:26 -0600272
273 while (1) {
Simon Glassb08e9d42023-01-06 08:52:20 -0600274 /* Check for saved characters */
275 ichar = cli_ch_process(cch, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600276
Simon Glassb08e9d42023-01-06 08:52:20 -0600277 if (!ichar) {
278 if (bootretry_tstc_timeout())
279 return -2; /* timed out */
280 if (first && timeout) {
281 u64 etime = endtick(timeout);
282
283 while (!tstc()) { /* while no incoming data */
284 if (get_ticks() >= etime)
285 return -2; /* timed out */
286 schedule();
287 }
288 first = 0;
Simon Glass6493ccc2014-04-10 20:01:26 -0600289 }
Simon Glassb08e9d42023-01-06 08:52:20 -0600290
291 ichar = getcmd_getch();
Simon Glassbe0169f2023-03-28 08:34:14 +1300292 ichar = cli_ch_process(cch, ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600293 }
294
Patrick Delaunay555e3782018-08-03 13:38:45 +0200295 /* ichar=0x0 when error occurs in U-Boot getc */
296 if (!ichar)
297 continue;
298
Simon Glassb08e9d42023-01-06 08:52:20 -0600299 if (ichar == '\n') {
Simon Glass6493ccc2014-04-10 20:01:26 -0600300 putc('\n');
301 break;
302 }
303
Simon Glass6493ccc2014-04-10 20:01:26 -0600304 switch (ichar) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600305 case CTL_CH('a'):
306 BEGINNING_OF_LINE();
307 break;
308 case CTL_CH('c'): /* ^C - break */
309 *buf = '\0'; /* discard input */
310 return -1;
311 case CTL_CH('f'):
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600312 if (cls->num < cls->eol_num) {
313 getcmd_putch(buf[cls->num]);
314 cls->num++;
Simon Glass6493ccc2014-04-10 20:01:26 -0600315 }
316 break;
317 case CTL_CH('b'):
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600318 if (cls->num) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600319 getcmd_putch(CTL_BACKSPACE);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600320 cls->num--;
Simon Glass6493ccc2014-04-10 20:01:26 -0600321 }
322 break;
323 case CTL_CH('d'):
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600324 if (cls->num < cls->eol_num) {
325 uint wlen;
326
327 wlen = cls->eol_num - cls->num - 1;
Simon Glass6493ccc2014-04-10 20:01:26 -0600328 if (wlen) {
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600329 memmove(&buf[cls->num],
330 &buf[cls->num + 1], wlen);
331 putnstr(buf + cls->num, wlen);
Simon Glass6493ccc2014-04-10 20:01:26 -0600332 }
333
334 getcmd_putch(' ');
335 do {
336 getcmd_putch(CTL_BACKSPACE);
337 } while (wlen--);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600338 cls->eol_num--;
Simon Glass6493ccc2014-04-10 20:01:26 -0600339 }
340 break;
341 case CTL_CH('k'):
342 ERASE_TO_EOL();
343 break;
344 case CTL_CH('e'):
345 REFRESH_TO_EOL();
346 break;
347 case CTL_CH('o'):
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600348 cls->insert = !cls->insert;
Simon Glass6493ccc2014-04-10 20:01:26 -0600349 break;
Simon Glassdcc18ce2023-10-01 19:13:09 -0600350 case CTL_CH('w'):
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600351 if (cls->num) {
352 uint base, wlen;
Simon Glassdcc18ce2023-10-01 19:13:09 -0600353
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600354 for (base = cls->num - 1;
Simon Glassdcc18ce2023-10-01 19:13:09 -0600355 base >= 0 && buf[base] == ' ';)
356 base--;
357 for (; base > 0 && buf[base - 1] != ' ';)
358 base--;
359
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600360 /* now delete chars from base to cls->num */
361 wlen = cls->num - base;
362 cls->eol_num -= wlen;
363 memmove(&buf[base], &buf[cls->num],
364 cls->eol_num - base + 1);
365 cls->num = base;
Simon Glassdcc18ce2023-10-01 19:13:09 -0600366 getcmd_putchars(wlen, CTL_BACKSPACE);
367 puts(buf + base);
368 getcmd_putchars(wlen, ' ');
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600369 getcmd_putchars(wlen + cls->eol_num - cls->num,
Simon Glassdcc18ce2023-10-01 19:13:09 -0600370 CTL_BACKSPACE);
371 }
372 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600373 case CTL_CH('x'):
374 case CTL_CH('u'):
375 BEGINNING_OF_LINE();
376 ERASE_TO_EOL();
377 break;
378 case DEL:
379 case DEL7:
380 case 8:
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600381 if (cls->num) {
382 uint wlen;
383
384 wlen = cls->eol_num - cls->num;
385 cls->num--;
386 memmove(&buf[cls->num], &buf[cls->num + 1],
387 wlen);
Simon Glass6493ccc2014-04-10 20:01:26 -0600388 getcmd_putch(CTL_BACKSPACE);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600389 putnstr(buf + cls->num, wlen);
Simon Glass6493ccc2014-04-10 20:01:26 -0600390 getcmd_putch(' ');
391 do {
392 getcmd_putch(CTL_BACKSPACE);
393 } while (wlen--);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600394 cls->eol_num--;
Simon Glass6493ccc2014-04-10 20:01:26 -0600395 }
396 break;
397 case CTL_CH('p'):
398 case CTL_CH('n'):
399 {
400 char *hline;
401
Simon Glass6493ccc2014-04-10 20:01:26 -0600402 if (ichar == CTL_CH('p'))
403 hline = hist_prev();
404 else
405 hline = hist_next();
406
407 if (!hline) {
408 getcmd_cbeep();
409 continue;
410 }
411
412 /* nuke the current line */
413 /* first, go home */
414 BEGINNING_OF_LINE();
415
416 /* erase to end of line */
417 ERASE_TO_EOL();
418
419 /* copy new line into place and display */
420 strcpy(buf, hline);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600421 cls->eol_num = strlen(buf);
Simon Glass6493ccc2014-04-10 20:01:26 -0600422 REFRESH_TO_EOL();
423 continue;
424 }
Simon Glass63213912023-10-01 19:13:08 -0600425 case '\t':
426 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
427 int num2, col;
Simon Glass6493ccc2014-04-10 20:01:26 -0600428
Simon Glass63213912023-10-01 19:13:08 -0600429 /* do not autocomplete when in the middle */
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600430 if (cls->num < cls->eol_num) {
Simon Glass63213912023-10-01 19:13:08 -0600431 getcmd_cbeep();
432 break;
433 }
434
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600435 buf[cls->num] = '\0';
436 col = strlen(prompt) + cls->eol_num;
437 num2 = cls->num;
Simon Glass63213912023-10-01 19:13:08 -0600438 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600439 col = num2 - cls->num;
440 cls->num += col;
441 cls->eol_num += col;
Simon Glass63213912023-10-01 19:13:08 -0600442 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600443 break;
444 }
Simon Glass63213912023-10-01 19:13:08 -0600445 fallthrough;
Simon Glass6493ccc2014-04-10 20:01:26 -0600446 default:
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600447 cread_add_char(ichar, cls->insert, &cls->num,
448 &cls->eol_num, buf, *len);
Simon Glass6493ccc2014-04-10 20:01:26 -0600449 break;
450 }
451 }
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600452 *len = cls->eol_num;
453 buf[cls->eol_num] = '\0'; /* lose the newline */
Simon Glass6493ccc2014-04-10 20:01:26 -0600454
455 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
456 cread_add_to_hist(buf);
457 hist_cur = hist_add_idx;
458
459 return 0;
460}
461
Simon Glass039f8cc2023-10-01 19:13:07 -0600462#else /* !CONFIG_CMDLINE_EDITING */
463
464static inline void hist_init(void)
465{
466}
467
468static int cread_line(const char *const prompt, char *buf, unsigned int *len,
469 int timeout)
470{
471 return 0;
472}
473
Simon Glass6493ccc2014-04-10 20:01:26 -0600474#endif /* CONFIG_CMDLINE_EDITING */
475
476/****************************************************************************/
477
Simon Glasse1bf8242014-04-10 20:01:27 -0600478int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600479{
480 /*
481 * If console_buffer isn't 0-length the user will be prompted to modify
482 * it instead of entering it from scratch as desired.
483 */
484 console_buffer[0] = '\0';
485
Simon Glasse1bf8242014-04-10 20:01:27 -0600486 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600487}
488
Simon Glass0f97e942023-10-01 19:13:05 -0600489/**
490 * cread_line_simple() - Simple (small) command-line reader
491 *
492 * This supports only basic editing, with no cursor movement
493 *
494 * @prompt: Prompt to display
495 * @p: Text buffer to edit
496 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
497 */
498static int cread_line_simple(const char *const prompt, char *p)
Simon Glass6493ccc2014-04-10 20:01:26 -0600499{
Simon Glass6493ccc2014-04-10 20:01:26 -0600500 char *p_buf = p;
Simon Glass0f97e942023-10-01 19:13:05 -0600501 int n = 0; /* buffer index */
502 int plen = 0; /* prompt length */
503 int col; /* output column cnt */
504 char c;
Simon Glass6493ccc2014-04-10 20:01:26 -0600505
506 /* print prompt */
507 if (prompt) {
508 plen = strlen(prompt);
509 puts(prompt);
510 }
511 col = plen;
512
513 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600514 if (bootretry_tstc_timeout())
515 return -2; /* timed out */
Stefan Roese29caf932022-09-02 14:10:46 +0200516 schedule(); /* Trigger watchdog, if needed */
Simon Glass6493ccc2014-04-10 20:01:26 -0600517
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200518 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600519
520 /*
521 * Special character handling
522 */
523 switch (c) {
524 case '\r': /* Enter */
525 case '\n':
526 *p = '\0';
527 puts("\r\n");
528 return p - p_buf;
529
530 case '\0': /* nul */
531 continue;
532
533 case 0x03: /* ^C - break */
534 p_buf[0] = '\0'; /* discard input */
535 return -1;
536
537 case 0x15: /* ^U - erase line */
538 while (col > plen) {
539 puts(erase_seq);
540 --col;
541 }
542 p = p_buf;
543 n = 0;
544 continue;
545
546 case 0x17: /* ^W - erase word */
547 p = delete_char(p_buf, p, &col, &n, plen);
548 while ((n > 0) && (*p != ' '))
549 p = delete_char(p_buf, p, &col, &n, plen);
550 continue;
551
552 case 0x08: /* ^H - backspace */
553 case 0x7F: /* DEL - backspace */
554 p = delete_char(p_buf, p, &col, &n, plen);
555 continue;
556
557 default:
Simon Glass63213912023-10-01 19:13:08 -0600558 /* Must be a normal character then */
559 if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */
560 putc('\a');
561 break;
562 }
563 if (c == '\t') { /* expand TABs */
564 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600565 /*
Simon Glass63213912023-10-01 19:13:08 -0600566 * if auto-completion triggered just
Simon Glass6493ccc2014-04-10 20:01:26 -0600567 * continue
568 */
569 *p = '\0';
570 if (cmd_auto_complete(prompt,
571 console_buffer,
572 &n, &col)) {
573 p = p_buf + n; /* reset */
574 continue;
575 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600576 }
Simon Glass63213912023-10-01 19:13:08 -0600577 puts(tab_seq + (col & 07));
578 col += 8 - (col & 07);
579 } else {
580 char __maybe_unused buf[2];
581
582 /*
583 * Echo input using puts() to force an LCD
584 * flush if we are using an LCD
585 */
586 ++col;
587 buf[0] = c;
588 buf[1] = '\0';
589 puts(buf);
Simon Glass6493ccc2014-04-10 20:01:26 -0600590 }
Simon Glass63213912023-10-01 19:13:08 -0600591 *p++ = c;
592 ++n;
593 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600594 }
595 }
Simon Glass0f97e942023-10-01 19:13:05 -0600596}
597
598int cli_readline_into_buffer(const char *const prompt, char *buffer,
599 int timeout)
600{
601 char *p = buffer;
Simon Glass039f8cc2023-10-01 19:13:07 -0600602 uint len = CONFIG_SYS_CBSIZE;
Simon Glass0f97e942023-10-01 19:13:05 -0600603 int rc;
604 static int initted;
605
606 /*
607 * History uses a global array which is not
608 * writable until after relocation to RAM.
609 * Revert to non-history version if still
610 * running from flash.
611 */
Simon Glass039f8cc2023-10-01 19:13:07 -0600612 if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
Simon Glass0f97e942023-10-01 19:13:05 -0600613 if (!initted) {
614 hist_init();
615 initted = 1;
616 }
617
618 if (prompt)
619 puts(prompt);
620
621 rc = cread_line(prompt, p, &len, timeout);
622 return rc < 0 ? rc : len;
623
624 } else {
Simon Glass0f97e942023-10-01 19:13:05 -0600625 return cread_line_simple(prompt, p);
Simon Glass6493ccc2014-04-10 20:01:26 -0600626 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600627}