blob: cf4339d0e509e95d3b78c9d88ebf013c0ba0defc [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass66b8c8b2014-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 Glass66b8c8b2014-04-10 20:01:26 -06009 */
10
11#include <common.h>
Simon Glass399ed9a2014-04-10 20:01:30 -060012#include <bootretry.h>
Simon Glass66b8c8b2014-04-10 20:01:26 -060013#include <cli.h>
Simon Glassed38aef2020-05-10 11:40:03 -060014#include <command.h>
Marek Vasut6210f6d2023-12-02 21:52:30 +010015#include <hang.h>
16#include <malloc.h>
Simon Glass495a5dc2019-11-14 12:57:30 -070017#include <time.h>
Simon Glass66b8c8b2014-04-10 20:01:26 -060018#include <watchdog.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glass66b8c8b2014-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 Glass66b8c8b2014-04-10 20:01:26 -060026char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
27
Simon Glass66b8c8b2014-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 Glass66b8c8b2014-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 Schuchardtc4954fb2020-10-07 18:11:48 +020073#define getcmd_getch() getchar()
Simon Glass66b8c8b2014-04-10 20:01:26 -060074#define getcmd_cbeep() getcmd_putch('\a')
75
Sean Anderson5d5efec2022-08-30 16:40:37 -040076#ifdef CONFIG_SPL_BUILD
77#define HIST_MAX 3
78#define HIST_SIZE 32
79#else
Simon Glass66b8c8b2014-04-10 20:01:26 -060080#define HIST_MAX 20
81#define HIST_SIZE CONFIG_SYS_CBSIZE
Sean Anderson5d5efec2022-08-30 16:40:37 -040082#endif
Simon Glass66b8c8b2014-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
Hanyuan Zhaoae31efc2024-03-05 15:37:35 +080089#ifndef CONFIG_CMD_HISTORY_USE_CALLOC
90static char hist_data[HIST_MAX][HIST_SIZE + 1];
91#endif
Simon Glass66b8c8b2014-04-10 20:01:26 -060092static char *hist_list[HIST_MAX];
Simon Glass66b8c8b2014-04-10 20:01:26 -060093
94#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
95
Simon Glass415f32d2023-10-01 19:13:09 -060096static void getcmd_putchars(int count, int ch)
97{
98 int i;
99
100 for (i = 0; i < count; i++)
101 getcmd_putch(ch);
102}
103
Marek Vasut6210f6d2023-12-02 21:52:30 +0100104static int hist_init(void)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600105{
106 int i;
107
Hanyuan Zhaoae31efc2024-03-05 15:37:35 +0800108#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';
112 }
113#else
114 unsigned char *hist = calloc(HIST_MAX, HIST_SIZE + 1);
Marek Vasut6210f6d2023-12-02 21:52:30 +0100115 if (!hist)
Hanyuan Zhao13c54e72024-03-05 15:37:33 +0800116 panic("%s: calloc: out of memory!\n", __func__);
Marek Vasut6210f6d2023-12-02 21:52:30 +0100117
118 for (i = 0; i < HIST_MAX; i++)
119 hist_list[i] = hist + (i * (HIST_SIZE + 1));
Hanyuan Zhaoae31efc2024-03-05 15:37:35 +0800120#endif
121
122 hist_max = 0;
123 hist_add_idx = 0;
124 hist_cur = -1;
125 hist_num = 0;
Marek Vasut6210f6d2023-12-02 21:52:30 +0100126
127 return 0;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600128}
129
130static void cread_add_to_hist(char *line)
131{
132 strcpy(hist_list[hist_add_idx], line);
133
134 if (++hist_add_idx >= HIST_MAX)
135 hist_add_idx = 0;
136
137 if (hist_add_idx > hist_max)
138 hist_max = hist_add_idx;
139
140 hist_num++;
141}
142
143static char *hist_prev(void)
144{
145 char *ret;
146 int old_cur;
147
148 if (hist_cur < 0)
149 return NULL;
150
151 old_cur = hist_cur;
152 if (--hist_cur < 0)
153 hist_cur = hist_max;
154
155 if (hist_cur == hist_add_idx) {
156 hist_cur = old_cur;
157 ret = NULL;
158 } else {
159 ret = hist_list[hist_cur];
160 }
161
162 return ret;
163}
164
165static char *hist_next(void)
166{
167 char *ret;
168
169 if (hist_cur < 0)
170 return NULL;
171
172 if (hist_cur == hist_add_idx)
173 return NULL;
174
175 if (++hist_cur > hist_max)
176 hist_cur = 0;
177
178 if (hist_cur == hist_add_idx)
179 ret = "";
180 else
181 ret = hist_list[hist_cur];
182
183 return ret;
184}
185
Simon Glass4c0bf972023-10-01 19:13:06 -0600186void cread_print_hist_list(void)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600187{
188 int i;
Simon Glass9e6201e2023-10-01 19:13:10 -0600189 uint n;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600190
191 n = hist_num - hist_max;
192
193 i = hist_add_idx + 1;
194 while (1) {
195 if (i > hist_max)
196 i = 0;
197 if (i == hist_add_idx)
198 break;
199 printf("%s\n", hist_list[i]);
200 n++;
201 i++;
202 }
203}
Simon Glass66b8c8b2014-04-10 20:01:26 -0600204
205#define BEGINNING_OF_LINE() { \
Simon Glass07627c52023-10-01 19:13:11 -0600206 while (cls->num) { \
Simon Glass66b8c8b2014-04-10 20:01:26 -0600207 getcmd_putch(CTL_BACKSPACE); \
Simon Glass07627c52023-10-01 19:13:11 -0600208 cls->num--; \
Simon Glass66b8c8b2014-04-10 20:01:26 -0600209 } \
210}
211
212#define ERASE_TO_EOL() { \
Simon Glass07627c52023-10-01 19:13:11 -0600213 if (cls->num < cls->eol_num) { \
214 printf("%*s", (int)(cls->eol_num - cls->num), ""); \
Simon Glass66b8c8b2014-04-10 20:01:26 -0600215 do { \
216 getcmd_putch(CTL_BACKSPACE); \
Simon Glass07627c52023-10-01 19:13:11 -0600217 } while (--cls->eol_num > cls->num); \
Simon Glass66b8c8b2014-04-10 20:01:26 -0600218 } \
219}
220
Simon Glass07627c52023-10-01 19:13:11 -0600221#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; \
226 } \
Simon Glass66b8c8b2014-04-10 20:01:26 -0600227}
228
Simon Glass9e6201e2023-10-01 19:13:10 -0600229static void cread_add_char(char ichar, int insert, uint *num,
230 uint *eol_num, char *buf, uint len)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600231{
Simon Glass9e6201e2023-10-01 19:13:10 -0600232 uint wlen;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600233
234 /* room ??? */
235 if (insert || *num == *eol_num) {
236 if (*eol_num > len - 1) {
237 getcmd_cbeep();
238 return;
239 }
240 (*eol_num)++;
241 }
242
243 if (insert) {
244 wlen = *eol_num - *num;
245 if (wlen > 1)
246 memmove(&buf[*num+1], &buf[*num], wlen-1);
247
248 buf[*num] = ichar;
249 putnstr(buf + *num, wlen);
250 (*num)++;
251 while (--wlen)
252 getcmd_putch(CTL_BACKSPACE);
253 } else {
254 /* echo the character */
255 wlen = 1;
256 buf[*num] = ichar;
257 putnstr(buf + *num, wlen);
258 (*num)++;
259 }
260}
261
262static void cread_add_str(char *str, int strsize, int insert,
Simon Glass9e6201e2023-10-01 19:13:10 -0600263 uint *num, uint *eol_num, char *buf, uint len)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600264{
265 while (strsize--) {
266 cread_add_char(*str, insert, num, eol_num, buf, len);
267 str++;
268 }
269}
270
Simon Glass2f13ae52023-10-01 19:13:13 -0600271int cread_line_process_ch(struct cli_line_state *cls, char ichar)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600272{
Simon Glass2f13ae52023-10-01 19:13:13 -0600273 char *buf = cls->buf;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600274
Simon Glassc8d7ac72023-10-01 19:13:12 -0600275 /* ichar=0x0 when error occurs in U-Boot getc */
276 if (!ichar)
Simon Glass2f13ae52023-10-01 19:13:13 -0600277 return -EAGAIN;
Patrick Delaunayf0017d22018-08-03 13:38:45 +0200278
Simon Glassc8d7ac72023-10-01 19:13:12 -0600279 if (ichar == '\n') {
280 putc('\n');
Simon Glass9f071092023-10-01 19:13:14 -0600281 buf[cls->eol_num] = '\0'; /* terminate the string */
Simon Glass2f13ae52023-10-01 19:13:13 -0600282 return 0;
Simon Glassc8d7ac72023-10-01 19:13:12 -0600283 }
Simon Glass66b8c8b2014-04-10 20:01:26 -0600284
Simon Glassc8d7ac72023-10-01 19:13:12 -0600285 switch (ichar) {
286 case CTL_CH('a'):
287 BEGINNING_OF_LINE();
288 break;
289 case CTL_CH('c'): /* ^C - break */
290 *buf = '\0'; /* discard input */
Simon Glass2f13ae52023-10-01 19:13:13 -0600291 return -EINTR;
Simon Glassc8d7ac72023-10-01 19:13:12 -0600292 case CTL_CH('f'):
293 if (cls->num < cls->eol_num) {
294 getcmd_putch(buf[cls->num]);
295 cls->num++;
296 }
297 break;
298 case CTL_CH('b'):
299 if (cls->num) {
300 getcmd_putch(CTL_BACKSPACE);
301 cls->num--;
302 }
303 break;
304 case CTL_CH('d'):
305 if (cls->num < cls->eol_num) {
306 uint wlen;
Simon Glass07627c52023-10-01 19:13:11 -0600307
Simon Glassc8d7ac72023-10-01 19:13:12 -0600308 wlen = cls->eol_num - cls->num - 1;
309 if (wlen) {
Simon Glass07627c52023-10-01 19:13:11 -0600310 memmove(&buf[cls->num], &buf[cls->num + 1],
311 wlen);
Simon Glass07627c52023-10-01 19:13:11 -0600312 putnstr(buf + cls->num, wlen);
Simon Glass66b8c8b2014-04-10 20:01:26 -0600313 }
Simon Glass66b8c8b2014-04-10 20:01:26 -0600314
Simon Glassc8d7ac72023-10-01 19:13:12 -0600315 getcmd_putch(' ');
316 do {
317 getcmd_putch(CTL_BACKSPACE);
318 } while (wlen--);
319 cls->eol_num--;
320 }
321 break;
322 case CTL_CH('k'):
323 ERASE_TO_EOL();
324 break;
325 case CTL_CH('e'):
326 REFRESH_TO_EOL();
327 break;
328 case CTL_CH('o'):
329 cls->insert = !cls->insert;
330 break;
331 case CTL_CH('w'):
332 if (cls->num) {
333 uint base, wlen;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600334
Simon Glassc8d7ac72023-10-01 19:13:12 -0600335 for (base = cls->num - 1;
336 base >= 0 && buf[base] == ' ';)
337 base--;
338 for (; base > 0 && buf[base - 1] != ' ';)
339 base--;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600340
Simon Glassc8d7ac72023-10-01 19:13:12 -0600341 /* 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);
346 cls->num = base;
347 getcmd_putchars(wlen, CTL_BACKSPACE);
348 puts(buf + base);
349 getcmd_putchars(wlen, ' ');
350 getcmd_putchars(wlen + cls->eol_num - cls->num,
351 CTL_BACKSPACE);
352 }
353 break;
354 case CTL_CH('x'):
355 case CTL_CH('u'):
356 BEGINNING_OF_LINE();
357 ERASE_TO_EOL();
358 break;
359 case DEL:
360 case DEL7:
361 case 8:
362 if (cls->num) {
363 uint wlen;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600364
Simon Glassc8d7ac72023-10-01 19:13:12 -0600365 wlen = cls->eol_num - cls->num;
366 cls->num--;
367 memmove(&buf[cls->num], &buf[cls->num + 1], wlen);
368 getcmd_putch(CTL_BACKSPACE);
369 putnstr(buf + cls->num, wlen);
370 getcmd_putch(' ');
371 do {
372 getcmd_putch(CTL_BACKSPACE);
373 } while (wlen--);
374 cls->eol_num--;
375 }
376 break;
377 case CTL_CH('p'):
378 case CTL_CH('n'):
Simon Glass1c67ae42023-10-01 19:13:15 -0600379 if (cls->history) {
380 char *hline;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600381
Simon Glass1c67ae42023-10-01 19:13:15 -0600382 if (ichar == CTL_CH('p'))
383 hline = hist_prev();
384 else
385 hline = hist_next();
Simon Glassc8d7ac72023-10-01 19:13:12 -0600386
Simon Glass1c67ae42023-10-01 19:13:15 -0600387 if (!hline) {
388 getcmd_cbeep();
389 break;
390 }
Simon Glassc8d7ac72023-10-01 19:13:12 -0600391
Simon Glass1c67ae42023-10-01 19:13:15 -0600392 /* nuke the current line */
393 /* first, go home */
394 BEGINNING_OF_LINE();
Simon Glass66b8c8b2014-04-10 20:01:26 -0600395
Simon Glass1c67ae42023-10-01 19:13:15 -0600396 /* erase to end of line */
397 ERASE_TO_EOL();
Simon Glass66b8c8b2014-04-10 20:01:26 -0600398
Simon Glass1c67ae42023-10-01 19:13:15 -0600399 /* copy new line into place and display */
400 strcpy(buf, hline);
401 cls->eol_num = strlen(buf);
402 REFRESH_TO_EOL();
403 break;
404 }
Simon Glass2f13ae52023-10-01 19:13:13 -0600405 break;
Simon Glassc8d7ac72023-10-01 19:13:12 -0600406 case '\t':
Simon Glass3e9bc772023-10-01 19:13:16 -0600407 if (IS_ENABLED(CONFIG_AUTO_COMPLETE) && cls->cmd_complete) {
Simon Glassc8d7ac72023-10-01 19:13:12 -0600408 int num2, col;
409
410 /* do not autocomplete when in the middle */
411 if (cls->num < cls->eol_num) {
412 getcmd_cbeep();
Simon Glassc0734c32023-10-01 19:13:08 -0600413 break;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600414 }
Simon Glassc8d7ac72023-10-01 19:13:12 -0600415
416 buf[cls->num] = '\0';
Simon Glass2f13ae52023-10-01 19:13:13 -0600417 col = strlen(cls->prompt) + cls->eol_num;
Simon Glassc8d7ac72023-10-01 19:13:12 -0600418 num2 = cls->num;
Simon Glass2f13ae52023-10-01 19:13:13 -0600419 if (cmd_auto_complete(cls->prompt, buf, &num2, &col)) {
Simon Glassc8d7ac72023-10-01 19:13:12 -0600420 col = num2 - cls->num;
421 cls->num += col;
422 cls->eol_num += col;
423 }
Simon Glass66b8c8b2014-04-10 20:01:26 -0600424 break;
425 }
Simon Glassc8d7ac72023-10-01 19:13:12 -0600426 fallthrough;
427 default:
428 cread_add_char(ichar, cls->insert, &cls->num, &cls->eol_num,
Simon Glass2f13ae52023-10-01 19:13:13 -0600429 buf, cls->len);
Simon Glassc8d7ac72023-10-01 19:13:12 -0600430 break;
431 }
Simon Glass2f13ae52023-10-01 19:13:13 -0600432
Simon Glass9f071092023-10-01 19:13:14 -0600433 /*
434 * keep the string terminated...if we added a char at the end then we
435 * want a \0 after it
436 */
437 buf[cls->eol_num] = '\0';
438
Simon Glass2f13ae52023-10-01 19:13:13 -0600439 return -EAGAIN;
440}
441
Simon Glassb9491312023-10-01 19:13:17 -0600442void cli_cread_init(struct cli_line_state *cls, char *buf, uint buf_size)
443{
444 int init_len = strlen(buf);
445
446 memset(cls, '\0', sizeof(struct cli_line_state));
447 cls->insert = true;
448 cls->buf = buf;
449 cls->len = buf_size;
450
451 if (init_len)
452 cread_add_str(buf, init_len, 0, &cls->num, &cls->eol_num, buf,
453 buf_size);
454}
455
Simon Glass2f13ae52023-10-01 19:13:13 -0600456static int cread_line(const char *const prompt, char *buf, unsigned int *len,
457 int timeout)
458{
459 struct cli_ch_state s_cch, *cch = &s_cch;
460 struct cli_line_state s_cls, *cls = &s_cls;
461 char ichar;
Simon Glass2f13ae52023-10-01 19:13:13 -0600462 int first = 1;
463
464 cli_ch_init(cch);
Simon Glassb9491312023-10-01 19:13:17 -0600465 cli_cread_init(cls, buf, *len);
Simon Glass2f13ae52023-10-01 19:13:13 -0600466 cls->prompt = prompt;
Simon Glass1c67ae42023-10-01 19:13:15 -0600467 cls->history = true;
Simon Glass3e9bc772023-10-01 19:13:16 -0600468 cls->cmd_complete = true;
Simon Glass2f13ae52023-10-01 19:13:13 -0600469
Simon Glass2f13ae52023-10-01 19:13:13 -0600470 while (1) {
471 int ret;
472
473 /* Check for saved characters */
474 ichar = cli_ch_process(cch, 0);
475
476 if (!ichar) {
477 if (bootretry_tstc_timeout())
478 return -2; /* timed out */
479 if (first && timeout) {
480 u64 etime = endtick(timeout);
481
482 while (!tstc()) { /* while no incoming data */
483 if (get_ticks() >= etime)
484 return -2; /* timed out */
485 schedule();
486 }
487 first = 0;
488 }
489
490 ichar = getcmd_getch();
491 ichar = cli_ch_process(cch, ichar);
492 }
493
494 ret = cread_line_process_ch(cls, ichar);
495 if (ret == -EINTR)
496 return -1;
497 else if (!ret)
498 break;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600499 }
Simon Glass07627c52023-10-01 19:13:11 -0600500 *len = cls->eol_num;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600501
502 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
503 cread_add_to_hist(buf);
504 hist_cur = hist_add_idx;
505
506 return 0;
507}
508
Simon Glass4dc73c42023-10-01 19:13:07 -0600509#else /* !CONFIG_CMDLINE_EDITING */
510
Marek Vasut6210f6d2023-12-02 21:52:30 +0100511static inline int hist_init(void)
Simon Glass4dc73c42023-10-01 19:13:07 -0600512{
Marek Vasut6210f6d2023-12-02 21:52:30 +0100513 return 0;
Simon Glass4dc73c42023-10-01 19:13:07 -0600514}
515
516static int cread_line(const char *const prompt, char *buf, unsigned int *len,
517 int timeout)
518{
519 return 0;
520}
521
Simon Glass66b8c8b2014-04-10 20:01:26 -0600522#endif /* CONFIG_CMDLINE_EDITING */
523
524/****************************************************************************/
525
Simon Glassbe6aafc2014-04-10 20:01:27 -0600526int cli_readline(const char *const prompt)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600527{
528 /*
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.
531 */
532 console_buffer[0] = '\0';
533
Simon Glassbe6aafc2014-04-10 20:01:27 -0600534 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass66b8c8b2014-04-10 20:01:26 -0600535}
536
Simon Glass043d0fd2023-10-01 19:13:05 -0600537/**
538 * cread_line_simple() - Simple (small) command-line reader
539 *
540 * This supports only basic editing, with no cursor movement
541 *
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)
545 */
546static int cread_line_simple(const char *const prompt, char *p)
Simon Glass66b8c8b2014-04-10 20:01:26 -0600547{
Simon Glass66b8c8b2014-04-10 20:01:26 -0600548 char *p_buf = p;
Simon Glass043d0fd2023-10-01 19:13:05 -0600549 int n = 0; /* buffer index */
550 int plen = 0; /* prompt length */
551 int col; /* output column cnt */
Tom Rini479f54a2024-01-09 17:57:16 -0500552 int c;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600553
554 /* print prompt */
555 if (prompt) {
556 plen = strlen(prompt);
557 puts(prompt);
558 }
559 col = plen;
560
561 for (;;) {
Simon Glass399ed9a2014-04-10 20:01:30 -0600562 if (bootretry_tstc_timeout())
563 return -2; /* timed out */
Stefan Roese80877fa2022-09-02 14:10:46 +0200564 schedule(); /* Trigger watchdog, if needed */
Simon Glass66b8c8b2014-04-10 20:01:26 -0600565
Heinrich Schuchardtc4954fb2020-10-07 18:11:48 +0200566 c = getchar();
Simon Glass66b8c8b2014-04-10 20:01:26 -0600567
568 /*
569 * Special character handling
570 */
571 switch (c) {
572 case '\r': /* Enter */
573 case '\n':
574 *p = '\0';
575 puts("\r\n");
576 return p - p_buf;
577
578 case '\0': /* nul */
579 continue;
580
581 case 0x03: /* ^C - break */
582 p_buf[0] = '\0'; /* discard input */
583 return -1;
584
585 case 0x15: /* ^U - erase line */
586 while (col > plen) {
587 puts(erase_seq);
588 --col;
589 }
590 p = p_buf;
591 n = 0;
592 continue;
593
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);
598 continue;
599
600 case 0x08: /* ^H - backspace */
601 case 0x7F: /* DEL - backspace */
602 p = delete_char(p_buf, p, &col, &n, plen);
603 continue;
604
605 default:
Simon Glassc0734c32023-10-01 19:13:08 -0600606 /* Must be a normal character then */
607 if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */
608 putc('\a');
609 break;
610 }
611 if (c == '\t') { /* expand TABs */
612 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
Simon Glass66b8c8b2014-04-10 20:01:26 -0600613 /*
Simon Glassc0734c32023-10-01 19:13:08 -0600614 * if auto-completion triggered just
Simon Glass66b8c8b2014-04-10 20:01:26 -0600615 * continue
616 */
617 *p = '\0';
618 if (cmd_auto_complete(prompt,
619 console_buffer,
620 &n, &col)) {
621 p = p_buf + n; /* reset */
622 continue;
623 }
Simon Glass66b8c8b2014-04-10 20:01:26 -0600624 }
Simon Glassc0734c32023-10-01 19:13:08 -0600625 puts(tab_seq + (col & 07));
626 col += 8 - (col & 07);
627 } else {
628 char __maybe_unused buf[2];
629
630 /*
631 * Echo input using puts() to force an LCD
632 * flush if we are using an LCD
633 */
634 ++col;
635 buf[0] = c;
636 buf[1] = '\0';
637 puts(buf);
Simon Glass66b8c8b2014-04-10 20:01:26 -0600638 }
Simon Glassc0734c32023-10-01 19:13:08 -0600639 *p++ = c;
640 ++n;
641 break;
Simon Glass66b8c8b2014-04-10 20:01:26 -0600642 }
643 }
Simon Glass043d0fd2023-10-01 19:13:05 -0600644}
645
646int cli_readline_into_buffer(const char *const prompt, char *buffer,
647 int timeout)
648{
649 char *p = buffer;
Simon Glass4dc73c42023-10-01 19:13:07 -0600650 uint len = CONFIG_SYS_CBSIZE;
Simon Glass043d0fd2023-10-01 19:13:05 -0600651 int rc;
652 static int initted;
653
654 /*
Hanyuan Zhaoae31efc2024-03-05 15:37:35 +0800655 * 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.
Simon Glass043d0fd2023-10-01 19:13:05 -0600664 */
Simon Glass4dc73c42023-10-01 19:13:07 -0600665 if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
Simon Glass043d0fd2023-10-01 19:13:05 -0600666 if (!initted) {
Marek Vasut6210f6d2023-12-02 21:52:30 +0100667 rc = hist_init();
668 if (rc == 0)
669 initted = 1;
Simon Glass043d0fd2023-10-01 19:13:05 -0600670 }
671
672 if (prompt)
673 puts(prompt);
674
675 rc = cread_line(prompt, p, &len, timeout);
676 return rc < 0 ? rc : len;
677
678 } else {
Simon Glass043d0fd2023-10-01 19:13:05 -0600679 return cread_line_simple(prompt, p);
Simon Glass66b8c8b2014-04-10 20:01:26 -0600680 }
Simon Glass66b8c8b2014-04-10 20:01:26 -0600681}