blob: 2507be2295269a64b9eefe50ba4572c5e4111337 [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>
Marek Vasut400cb2a2023-12-02 21:52:30 +010015#include <hang.h>
16#include <malloc.h>
Simon Glass10453152019-11-14 12:57:30 -070017#include <time.h>
Simon Glass6493ccc2014-04-10 20:01:26 -060018#include <watchdog.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass6493ccc2014-04-10 20:01:26 -060020
21DECLARE_GLOBAL_DATA_PTR;
22
23static const char erase_seq[] = "\b \b"; /* erase sequence */
24static const char tab_seq[] = " "; /* used to expand TABs */
25
Simon Glass6493ccc2014-04-10 20:01:26 -060026char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
27
Simon Glass6493ccc2014-04-10 20:01:26 -060028static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
29{
30 char *s;
31
32 if (*np == 0)
33 return p;
34
35 if (*(--p) == '\t') { /* will retype the whole line */
36 while (*colp > plen) {
37 puts(erase_seq);
38 (*colp)--;
39 }
40 for (s = buffer; s < p; ++s) {
41 if (*s == '\t') {
42 puts(tab_seq + ((*colp) & 07));
43 *colp += 8 - ((*colp) & 07);
44 } else {
45 ++(*colp);
46 putc(*s);
47 }
48 }
49 } else {
50 puts(erase_seq);
51 (*colp)--;
52 }
53 (*np)--;
54
55 return p;
56}
57
58#ifdef CONFIG_CMDLINE_EDITING
59
60/*
61 * cmdline-editing related codes from vivi.
62 * Author: Janghoon Lyu <nandy@mizi.com>
63 */
64
65#define putnstr(str, n) printf("%.*s", (int)n, str)
66
Simon Glass6493ccc2014-04-10 20:01:26 -060067#define CTL_BACKSPACE ('\b')
68#define DEL ((char)255)
69#define DEL7 ((char)127)
70#define CREAD_HIST_CHAR ('!')
71
72#define getcmd_putch(ch) putc(ch)
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020073#define getcmd_getch() getchar()
Simon Glass6493ccc2014-04-10 20:01:26 -060074#define getcmd_cbeep() getcmd_putch('\a')
75
Sean Anderson069f0d72022-08-30 16:40:37 -040076#ifdef CONFIG_SPL_BUILD
77#define HIST_MAX 3
78#define HIST_SIZE 32
79#else
Simon Glass6493ccc2014-04-10 20:01:26 -060080#define HIST_MAX 20
81#define HIST_SIZE CONFIG_SYS_CBSIZE
Sean Anderson069f0d72022-08-30 16:40:37 -040082#endif
Simon Glass6493ccc2014-04-10 20:01:26 -060083
84static int hist_max;
85static int hist_add_idx;
86static int hist_cur = -1;
87static unsigned hist_num;
88
89static char *hist_list[HIST_MAX];
Simon Glass6493ccc2014-04-10 20:01:26 -060090
91#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
92
Simon Glassdcc18ce2023-10-01 19:13:09 -060093static void getcmd_putchars(int count, int ch)
94{
95 int i;
96
97 for (i = 0; i < count; i++)
98 getcmd_putch(ch);
99}
100
Marek Vasut400cb2a2023-12-02 21:52:30 +0100101static int hist_init(void)
Simon Glass6493ccc2014-04-10 20:01:26 -0600102{
Marek Vasut400cb2a2023-12-02 21:52:30 +0100103 unsigned char *hist;
Simon Glass6493ccc2014-04-10 20:01:26 -0600104 int i;
105
106 hist_max = 0;
107 hist_add_idx = 0;
108 hist_cur = -1;
109 hist_num = 0;
110
Marek Vasut400cb2a2023-12-02 21:52:30 +0100111 hist = calloc(HIST_MAX, HIST_SIZE + 1);
112 if (!hist)
113 return -ENOMEM;
114
115 for (i = 0; i < HIST_MAX; i++)
116 hist_list[i] = hist + (i * (HIST_SIZE + 1));
117
118 return 0;
Simon Glass6493ccc2014-04-10 20:01:26 -0600119}
120
121static void cread_add_to_hist(char *line)
122{
123 strcpy(hist_list[hist_add_idx], line);
124
125 if (++hist_add_idx >= HIST_MAX)
126 hist_add_idx = 0;
127
128 if (hist_add_idx > hist_max)
129 hist_max = hist_add_idx;
130
131 hist_num++;
132}
133
134static char *hist_prev(void)
135{
136 char *ret;
137 int old_cur;
138
139 if (hist_cur < 0)
140 return NULL;
141
142 old_cur = hist_cur;
143 if (--hist_cur < 0)
144 hist_cur = hist_max;
145
146 if (hist_cur == hist_add_idx) {
147 hist_cur = old_cur;
148 ret = NULL;
149 } else {
150 ret = hist_list[hist_cur];
151 }
152
153 return ret;
154}
155
156static char *hist_next(void)
157{
158 char *ret;
159
160 if (hist_cur < 0)
161 return NULL;
162
163 if (hist_cur == hist_add_idx)
164 return NULL;
165
166 if (++hist_cur > hist_max)
167 hist_cur = 0;
168
169 if (hist_cur == hist_add_idx)
170 ret = "";
171 else
172 ret = hist_list[hist_cur];
173
174 return ret;
175}
176
Simon Glass33eb0b92023-10-01 19:13:06 -0600177void cread_print_hist_list(void)
Simon Glass6493ccc2014-04-10 20:01:26 -0600178{
179 int i;
Simon Glassfedd3722023-10-01 19:13:10 -0600180 uint n;
Simon Glass6493ccc2014-04-10 20:01:26 -0600181
182 n = hist_num - hist_max;
183
184 i = hist_add_idx + 1;
185 while (1) {
186 if (i > hist_max)
187 i = 0;
188 if (i == hist_add_idx)
189 break;
190 printf("%s\n", hist_list[i]);
191 n++;
192 i++;
193 }
194}
Simon Glass6493ccc2014-04-10 20:01:26 -0600195
196#define BEGINNING_OF_LINE() { \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600197 while (cls->num) { \
Simon Glass6493ccc2014-04-10 20:01:26 -0600198 getcmd_putch(CTL_BACKSPACE); \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600199 cls->num--; \
Simon Glass6493ccc2014-04-10 20:01:26 -0600200 } \
201}
202
203#define ERASE_TO_EOL() { \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600204 if (cls->num < cls->eol_num) { \
205 printf("%*s", (int)(cls->eol_num - cls->num), ""); \
Simon Glass6493ccc2014-04-10 20:01:26 -0600206 do { \
207 getcmd_putch(CTL_BACKSPACE); \
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600208 } while (--cls->eol_num > cls->num); \
Simon Glass6493ccc2014-04-10 20:01:26 -0600209 } \
210}
211
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600212#define REFRESH_TO_EOL() { \
213 if (cls->num < cls->eol_num) { \
214 uint wlen = cls->eol_num - cls->num; \
215 putnstr(buf + cls->num, wlen); \
216 cls->num = cls->eol_num; \
217 } \
Simon Glass6493ccc2014-04-10 20:01:26 -0600218}
219
Simon Glassfedd3722023-10-01 19:13:10 -0600220static void cread_add_char(char ichar, int insert, uint *num,
221 uint *eol_num, char *buf, uint len)
Simon Glass6493ccc2014-04-10 20:01:26 -0600222{
Simon Glassfedd3722023-10-01 19:13:10 -0600223 uint wlen;
Simon Glass6493ccc2014-04-10 20:01:26 -0600224
225 /* room ??? */
226 if (insert || *num == *eol_num) {
227 if (*eol_num > len - 1) {
228 getcmd_cbeep();
229 return;
230 }
231 (*eol_num)++;
232 }
233
234 if (insert) {
235 wlen = *eol_num - *num;
236 if (wlen > 1)
237 memmove(&buf[*num+1], &buf[*num], wlen-1);
238
239 buf[*num] = ichar;
240 putnstr(buf + *num, wlen);
241 (*num)++;
242 while (--wlen)
243 getcmd_putch(CTL_BACKSPACE);
244 } else {
245 /* echo the character */
246 wlen = 1;
247 buf[*num] = ichar;
248 putnstr(buf + *num, wlen);
249 (*num)++;
250 }
251}
252
253static void cread_add_str(char *str, int strsize, int insert,
Simon Glassfedd3722023-10-01 19:13:10 -0600254 uint *num, uint *eol_num, char *buf, uint len)
Simon Glass6493ccc2014-04-10 20:01:26 -0600255{
256 while (strsize--) {
257 cread_add_char(*str, insert, num, eol_num, buf, len);
258 str++;
259 }
260}
261
Simon Glasse5509ce2023-10-01 19:13:13 -0600262int cread_line_process_ch(struct cli_line_state *cls, char ichar)
Simon Glass6493ccc2014-04-10 20:01:26 -0600263{
Simon Glasse5509ce2023-10-01 19:13:13 -0600264 char *buf = cls->buf;
Simon Glass6493ccc2014-04-10 20:01:26 -0600265
Simon Glass8d997aa2023-10-01 19:13:12 -0600266 /* ichar=0x0 when error occurs in U-Boot getc */
267 if (!ichar)
Simon Glasse5509ce2023-10-01 19:13:13 -0600268 return -EAGAIN;
Patrick Delaunay555e3782018-08-03 13:38:45 +0200269
Simon Glass8d997aa2023-10-01 19:13:12 -0600270 if (ichar == '\n') {
271 putc('\n');
Simon Glass657e14d2023-10-01 19:13:14 -0600272 buf[cls->eol_num] = '\0'; /* terminate the string */
Simon Glasse5509ce2023-10-01 19:13:13 -0600273 return 0;
Simon Glass8d997aa2023-10-01 19:13:12 -0600274 }
275
276 switch (ichar) {
277 case CTL_CH('a'):
278 BEGINNING_OF_LINE();
279 break;
280 case CTL_CH('c'): /* ^C - break */
281 *buf = '\0'; /* discard input */
Simon Glasse5509ce2023-10-01 19:13:13 -0600282 return -EINTR;
Simon Glass8d997aa2023-10-01 19:13:12 -0600283 case CTL_CH('f'):
284 if (cls->num < cls->eol_num) {
285 getcmd_putch(buf[cls->num]);
286 cls->num++;
Simon Glass6493ccc2014-04-10 20:01:26 -0600287 }
Simon Glass8d997aa2023-10-01 19:13:12 -0600288 break;
289 case CTL_CH('b'):
290 if (cls->num) {
291 getcmd_putch(CTL_BACKSPACE);
292 cls->num--;
293 }
294 break;
295 case CTL_CH('d'):
296 if (cls->num < cls->eol_num) {
297 uint wlen;
Simon Glass6493ccc2014-04-10 20:01:26 -0600298
Simon Glass8d997aa2023-10-01 19:13:12 -0600299 wlen = cls->eol_num - cls->num - 1;
300 if (wlen) {
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600301 memmove(&buf[cls->num], &buf[cls->num + 1],
302 wlen);
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600303 putnstr(buf + cls->num, wlen);
Simon Glass6493ccc2014-04-10 20:01:26 -0600304 }
305
Simon Glass8d997aa2023-10-01 19:13:12 -0600306 getcmd_putch(' ');
307 do {
308 getcmd_putch(CTL_BACKSPACE);
309 } while (wlen--);
310 cls->eol_num--;
311 }
312 break;
313 case CTL_CH('k'):
314 ERASE_TO_EOL();
315 break;
316 case CTL_CH('e'):
317 REFRESH_TO_EOL();
318 break;
319 case CTL_CH('o'):
320 cls->insert = !cls->insert;
321 break;
322 case CTL_CH('w'):
323 if (cls->num) {
324 uint base, wlen;
Simon Glass6493ccc2014-04-10 20:01:26 -0600325
Simon Glass8d997aa2023-10-01 19:13:12 -0600326 for (base = cls->num - 1;
327 base >= 0 && buf[base] == ' ';)
328 base--;
329 for (; base > 0 && buf[base - 1] != ' ';)
330 base--;
Simon Glass6493ccc2014-04-10 20:01:26 -0600331
Simon Glass8d997aa2023-10-01 19:13:12 -0600332 /* now delete chars from base to cls->num */
333 wlen = cls->num - base;
334 cls->eol_num -= wlen;
335 memmove(&buf[base], &buf[cls->num],
336 cls->eol_num - base + 1);
337 cls->num = base;
338 getcmd_putchars(wlen, CTL_BACKSPACE);
339 puts(buf + base);
340 getcmd_putchars(wlen, ' ');
341 getcmd_putchars(wlen + cls->eol_num - cls->num,
342 CTL_BACKSPACE);
343 }
344 break;
345 case CTL_CH('x'):
346 case CTL_CH('u'):
347 BEGINNING_OF_LINE();
348 ERASE_TO_EOL();
349 break;
350 case DEL:
351 case DEL7:
352 case 8:
353 if (cls->num) {
354 uint wlen;
355
356 wlen = cls->eol_num - cls->num;
357 cls->num--;
358 memmove(&buf[cls->num], &buf[cls->num + 1], wlen);
359 getcmd_putch(CTL_BACKSPACE);
360 putnstr(buf + cls->num, wlen);
361 getcmd_putch(' ');
362 do {
363 getcmd_putch(CTL_BACKSPACE);
364 } while (wlen--);
365 cls->eol_num--;
366 }
367 break;
368 case CTL_CH('p'):
369 case CTL_CH('n'):
Simon Glass8fc041f2023-10-01 19:13:15 -0600370 if (cls->history) {
371 char *hline;
Simon Glass8d997aa2023-10-01 19:13:12 -0600372
Simon Glass8fc041f2023-10-01 19:13:15 -0600373 if (ichar == CTL_CH('p'))
374 hline = hist_prev();
375 else
376 hline = hist_next();
Simon Glass8d997aa2023-10-01 19:13:12 -0600377
Simon Glass8fc041f2023-10-01 19:13:15 -0600378 if (!hline) {
379 getcmd_cbeep();
380 break;
381 }
382
383 /* nuke the current line */
384 /* first, go home */
385 BEGINNING_OF_LINE();
386
387 /* erase to end of line */
388 ERASE_TO_EOL();
389
390 /* copy new line into place and display */
391 strcpy(buf, hline);
392 cls->eol_num = strlen(buf);
393 REFRESH_TO_EOL();
Simon Glasse5509ce2023-10-01 19:13:13 -0600394 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600395 }
Simon Glasse5509ce2023-10-01 19:13:13 -0600396 break;
Simon Glass8d997aa2023-10-01 19:13:12 -0600397 case '\t':
Simon Glass3b487bf2023-10-01 19:13:16 -0600398 if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
Simon Glass8d997aa2023-10-01 19:13:12 -0600399 int num2, col;
400
401 /* do not autocomplete when in the middle */
402 if (cls->num < cls->eol_num) {
403 getcmd_cbeep();
Simon Glass6493ccc2014-04-10 20:01:26 -0600404 break;
405 }
Simon Glass8d997aa2023-10-01 19:13:12 -0600406
407 buf[cls->num] = '\0';
Simon Glasse5509ce2023-10-01 19:13:13 -0600408 col = strlen(cls->prompt) + cls->eol_num;
Simon Glass8d997aa2023-10-01 19:13:12 -0600409 num2 = cls->num;
Simon Glasse5509ce2023-10-01 19:13:13 -0600410 if (cmd_auto_complete(cls->prompt, buf, &num2, &col)) {
Simon Glass8d997aa2023-10-01 19:13:12 -0600411 col = num2 - cls->num;
412 cls->num += col;
413 cls->eol_num += col;
414 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600415 break;
416 }
Simon Glass8d997aa2023-10-01 19:13:12 -0600417 fallthrough;
418 default:
419 cread_add_char(ichar, cls->insert, &cls->num, &cls->eol_num,
Simon Glasse5509ce2023-10-01 19:13:13 -0600420 buf, cls->len);
Simon Glass8d997aa2023-10-01 19:13:12 -0600421 break;
422 }
Simon Glasse5509ce2023-10-01 19:13:13 -0600423
Simon Glass657e14d2023-10-01 19:13:14 -0600424 /*
425 * keep the string terminated...if we added a char at the end then we
426 * want a \0 after it
427 */
428 buf[cls->eol_num] = '\0';
429
Simon Glasse5509ce2023-10-01 19:13:13 -0600430 return -EAGAIN;
431}
432
Simon Glass39ee3212023-10-01 19:13:17 -0600433void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size)
434{
435 int init_len = strlen(buf);
436
437 memset(cls, '\0', sizeof(struct cli_line_state));
438 cls->insert = true;
439 cls->buf = buf;
440 cls->len = buf_size;
441
442 if (init_len)
443 cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf,
444 buf_size);
445}
446
Simon Glasse5509ce2023-10-01 19:13:13 -0600447static int cread_line(const char *const prompt, char *buf, unsigned int *len,
448 int timeout)
449{
450 struct cli_ch_state s_cch, *cch = &s_cch;
451 struct cli_line_state s_cls, *cls = &s_cls;
452 char ichar;
Simon Glasse5509ce2023-10-01 19:13:13 -0600453 int first = 1;
454
455 cli_ch_init(cch);
Simon Glass39ee3212023-10-01 19:13:17 -0600456 cli_cread_init(cls, buf, *len);
Simon Glasse5509ce2023-10-01 19:13:13 -0600457 cls->prompt = prompt;
Simon Glass8fc041f2023-10-01 19:13:15 -0600458 cls->history = true;
Simon Glass3b487bf2023-10-01 19:13:16 -0600459 cls->cmd_complete = true;
Simon Glasse5509ce2023-10-01 19:13:13 -0600460
Simon Glasse5509ce2023-10-01 19:13:13 -0600461 while (1) {
462 int ret;
463
464 /* Check for saved characters */
465 ichar = cli_ch_process(cch, 0);
466
467 if (!ichar) {
468 if (bootretry_tstc_timeout())
469 return -2; /* timed out */
470 if (first && timeout) {
471 u64 etime = endtick(timeout);
472
473 while (!tstc()) { /* while no incoming data */
474 if (get_ticks() >= etime)
475 return -2; /* timed out */
476 schedule();
477 }
478 first = 0;
479 }
480
481 ichar = getcmd_getch();
482 ichar = cli_ch_process(cch, ichar);
483 }
484
485 ret = cread_line_process_ch(cls, ichar);
486 if (ret == -EINTR)
487 return -1;
488 else if (!ret)
489 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600490 }
Simon Glassbe5c2ed2023-10-01 19:13:11 -0600491 *len = cls->eol_num;
Simon Glass6493ccc2014-04-10 20:01:26 -0600492
493 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
494 cread_add_to_hist(buf);
495 hist_cur = hist_add_idx;
496
497 return 0;
498}
499
Simon Glass039f8cc2023-10-01 19:13:07 -0600500#else /* !CONFIG_CMDLINE_EDITING */
501
Marek Vasut400cb2a2023-12-02 21:52:30 +0100502static inline int hist_init(void)
Simon Glass039f8cc2023-10-01 19:13:07 -0600503{
Marek Vasut400cb2a2023-12-02 21:52:30 +0100504 return 0;
Simon Glass039f8cc2023-10-01 19:13:07 -0600505}
506
507static int cread_line(const char *const prompt, char *buf, unsigned int *len,
508 int timeout)
509{
510 return 0;
511}
512
Simon Glass6493ccc2014-04-10 20:01:26 -0600513#endif /* CONFIG_CMDLINE_EDITING */
514
515/****************************************************************************/
516
Simon Glasse1bf8242014-04-10 20:01:27 -0600517int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600518{
519 /*
520 * If console_buffer isn't 0-length the user will be prompted to modify
521 * it instead of entering it from scratch as desired.
522 */
523 console_buffer[0] = '\0';
524
Simon Glasse1bf8242014-04-10 20:01:27 -0600525 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600526}
527
Simon Glass0f97e942023-10-01 19:13:05 -0600528/**
529 * cread_line_simple() - Simple (small) command-line reader
530 *
531 * This supports only basic editing, with no cursor movement
532 *
533 * @prompt: Prompt to display
534 * @p: Text buffer to edit
535 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
536 */
537static int cread_line_simple(const char *const prompt, char *p)
Simon Glass6493ccc2014-04-10 20:01:26 -0600538{
Simon Glass6493ccc2014-04-10 20:01:26 -0600539 char *p_buf = p;
Simon Glass0f97e942023-10-01 19:13:05 -0600540 int n = 0; /* buffer index */
541 int plen = 0; /* prompt length */
542 int col; /* output column cnt */
Tom Rini21a2c122024-01-09 17:57:16 -0500543 int c;
Simon Glass6493ccc2014-04-10 20:01:26 -0600544
545 /* print prompt */
546 if (prompt) {
547 plen = strlen(prompt);
548 puts(prompt);
549 }
550 col = plen;
551
552 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600553 if (bootretry_tstc_timeout())
554 return -2; /* timed out */
Stefan Roese29caf932022-09-02 14:10:46 +0200555 schedule(); /* Trigger watchdog, if needed */
Simon Glass6493ccc2014-04-10 20:01:26 -0600556
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200557 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600558
559 /*
560 * Special character handling
561 */
562 switch (c) {
563 case '\r': /* Enter */
564 case '\n':
565 *p = '\0';
566 puts("\r\n");
567 return p - p_buf;
568
569 case '\0': /* nul */
570 continue;
571
572 case 0x03: /* ^C - break */
573 p_buf[0] = '\0'; /* discard input */
574 return -1;
575
576 case 0x15: /* ^U - erase line */
577 while (col > plen) {
578 puts(erase_seq);
579 --col;
580 }
581 p = p_buf;
582 n = 0;
583 continue;
584
585 case 0x17: /* ^W - erase word */
586 p = delete_char(p_buf, p, &col, &n, plen);
587 while ((n > 0) && (*p != ' '))
588 p = delete_char(p_buf, p, &col, &n, plen);
589 continue;
590
591 case 0x08: /* ^H - backspace */
592 case 0x7F: /* DEL - backspace */
593 p = delete_char(p_buf, p, &col, &n, plen);
594 continue;
595
596 default:
Simon Glass63213912023-10-01 19:13:08 -0600597 /* Must be a normal character then */
598 if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */
599 putc('\a');
600 break;
601 }
602 if (c == '\t') { /* expand TABs */
603 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600604 /*
Simon Glass63213912023-10-01 19:13:08 -0600605 * if auto-completion triggered just
Simon Glass6493ccc2014-04-10 20:01:26 -0600606 * continue
607 */
608 *p = '\0';
609 if (cmd_auto_complete(prompt,
610 console_buffer,
611 &n, &col)) {
612 p = p_buf + n; /* reset */
613 continue;
614 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600615 }
Simon Glass63213912023-10-01 19:13:08 -0600616 puts(tab_seq + (col & 07));
617 col += 8 - (col & 07);
618 } else {
619 char __maybe_unused buf[2];
620
621 /*
622 * Echo input using puts() to force an LCD
623 * flush if we are using an LCD
624 */
625 ++col;
626 buf[0] = c;
627 buf[1] = '\0';
628 puts(buf);
Simon Glass6493ccc2014-04-10 20:01:26 -0600629 }
Simon Glass63213912023-10-01 19:13:08 -0600630 *p++ = c;
631 ++n;
632 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600633 }
634 }
Simon Glass0f97e942023-10-01 19:13:05 -0600635}
636
637int cli_readline_into_buffer(const char *const prompt, char *buffer,
638 int timeout)
639{
640 char *p = buffer;
Simon Glass039f8cc2023-10-01 19:13:07 -0600641 uint len = CONFIG_SYS_CBSIZE;
Simon Glass0f97e942023-10-01 19:13:05 -0600642 int rc;
643 static int initted;
644
645 /*
646 * History uses a global array which is not
647 * writable until after relocation to RAM.
648 * Revert to non-history version if still
649 * running from flash.
650 */
Simon Glass039f8cc2023-10-01 19:13:07 -0600651 if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
Simon Glass0f97e942023-10-01 19:13:05 -0600652 if (!initted) {
Marek Vasut400cb2a2023-12-02 21:52:30 +0100653 rc = hist_init();
654 if (rc == 0)
655 initted = 1;
Simon Glass0f97e942023-10-01 19:13:05 -0600656 }
657
658 if (prompt)
659 puts(prompt);
660
661 rc = cread_line(prompt, p, &len, timeout);
662 return rc < 0 ? rc : len;
663
664 } else {
Simon Glass0f97e942023-10-01 19:13:05 -0600665 return cread_line_simple(prompt, p);
Simon Glass6493ccc2014-04-10 20:01:26 -0600666 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600667}