blob: f3ba76a62ec81a19ed760259a5c9b19b9acacbc3 [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
92static void hist_init(void)
93{
94 int i;
95
96 hist_max = 0;
97 hist_add_idx = 0;
98 hist_cur = -1;
99 hist_num = 0;
100
101 for (i = 0; i < HIST_MAX; i++) {
102 hist_list[i] = hist_lines[i];
103 hist_list[i][0] = '\0';
104 }
105}
106
107static void cread_add_to_hist(char *line)
108{
109 strcpy(hist_list[hist_add_idx], line);
110
111 if (++hist_add_idx >= HIST_MAX)
112 hist_add_idx = 0;
113
114 if (hist_add_idx > hist_max)
115 hist_max = hist_add_idx;
116
117 hist_num++;
118}
119
120static char *hist_prev(void)
121{
122 char *ret;
123 int old_cur;
124
125 if (hist_cur < 0)
126 return NULL;
127
128 old_cur = hist_cur;
129 if (--hist_cur < 0)
130 hist_cur = hist_max;
131
132 if (hist_cur == hist_add_idx) {
133 hist_cur = old_cur;
134 ret = NULL;
135 } else {
136 ret = hist_list[hist_cur];
137 }
138
139 return ret;
140}
141
142static char *hist_next(void)
143{
144 char *ret;
145
146 if (hist_cur < 0)
147 return NULL;
148
149 if (hist_cur == hist_add_idx)
150 return NULL;
151
152 if (++hist_cur > hist_max)
153 hist_cur = 0;
154
155 if (hist_cur == hist_add_idx)
156 ret = "";
157 else
158 ret = hist_list[hist_cur];
159
160 return ret;
161}
162
Simon Glass33eb0b92023-10-01 19:13:06 -0600163void cread_print_hist_list(void)
Simon Glass6493ccc2014-04-10 20:01:26 -0600164{
165 int i;
166 unsigned long n;
167
168 n = hist_num - hist_max;
169
170 i = hist_add_idx + 1;
171 while (1) {
172 if (i > hist_max)
173 i = 0;
174 if (i == hist_add_idx)
175 break;
176 printf("%s\n", hist_list[i]);
177 n++;
178 i++;
179 }
180}
Simon Glass6493ccc2014-04-10 20:01:26 -0600181
182#define BEGINNING_OF_LINE() { \
183 while (num) { \
184 getcmd_putch(CTL_BACKSPACE); \
185 num--; \
186 } \
187}
188
189#define ERASE_TO_EOL() { \
190 if (num < eol_num) { \
191 printf("%*s", (int)(eol_num - num), ""); \
192 do { \
193 getcmd_putch(CTL_BACKSPACE); \
194 } while (--eol_num > num); \
195 } \
196}
197
198#define REFRESH_TO_EOL() { \
199 if (num < eol_num) { \
200 wlen = eol_num - num; \
201 putnstr(buf + num, wlen); \
202 num = eol_num; \
203 } \
204}
205
206static void cread_add_char(char ichar, int insert, unsigned long *num,
207 unsigned long *eol_num, char *buf, unsigned long len)
208{
209 unsigned long wlen;
210
211 /* room ??? */
212 if (insert || *num == *eol_num) {
213 if (*eol_num > len - 1) {
214 getcmd_cbeep();
215 return;
216 }
217 (*eol_num)++;
218 }
219
220 if (insert) {
221 wlen = *eol_num - *num;
222 if (wlen > 1)
223 memmove(&buf[*num+1], &buf[*num], wlen-1);
224
225 buf[*num] = ichar;
226 putnstr(buf + *num, wlen);
227 (*num)++;
228 while (--wlen)
229 getcmd_putch(CTL_BACKSPACE);
230 } else {
231 /* echo the character */
232 wlen = 1;
233 buf[*num] = ichar;
234 putnstr(buf + *num, wlen);
235 (*num)++;
236 }
237}
238
239static void cread_add_str(char *str, int strsize, int insert,
240 unsigned long *num, unsigned long *eol_num,
241 char *buf, unsigned long len)
242{
243 while (strsize--) {
244 cread_add_char(*str, insert, num, eol_num, buf, len);
245 str++;
246 }
247}
248
249static int cread_line(const char *const prompt, char *buf, unsigned int *len,
250 int timeout)
251{
Simon Glassb08e9d42023-01-06 08:52:20 -0600252 struct cli_ch_state s_cch, *cch = &s_cch;
Simon Glass6493ccc2014-04-10 20:01:26 -0600253 unsigned long num = 0;
254 unsigned long eol_num = 0;
255 unsigned long wlen;
256 char ichar;
257 int insert = 1;
Simon Glass6493ccc2014-04-10 20:01:26 -0600258 int init_len = strlen(buf);
259 int first = 1;
260
Simon Glassb08e9d42023-01-06 08:52:20 -0600261 cli_ch_init(cch);
262
Simon Glass6493ccc2014-04-10 20:01:26 -0600263 if (init_len)
264 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
265
266 while (1) {
Simon Glassb08e9d42023-01-06 08:52:20 -0600267 /* Check for saved characters */
268 ichar = cli_ch_process(cch, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600269
Simon Glassb08e9d42023-01-06 08:52:20 -0600270 if (!ichar) {
271 if (bootretry_tstc_timeout())
272 return -2; /* timed out */
273 if (first && timeout) {
274 u64 etime = endtick(timeout);
275
276 while (!tstc()) { /* while no incoming data */
277 if (get_ticks() >= etime)
278 return -2; /* timed out */
279 schedule();
280 }
281 first = 0;
Simon Glass6493ccc2014-04-10 20:01:26 -0600282 }
Simon Glassb08e9d42023-01-06 08:52:20 -0600283
284 ichar = getcmd_getch();
Simon Glassbe0169f2023-03-28 08:34:14 +1300285 ichar = cli_ch_process(cch, ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600286 }
287
Patrick Delaunay555e3782018-08-03 13:38:45 +0200288 /* ichar=0x0 when error occurs in U-Boot getc */
289 if (!ichar)
290 continue;
291
Simon Glassb08e9d42023-01-06 08:52:20 -0600292 if (ichar == '\n') {
Simon Glass6493ccc2014-04-10 20:01:26 -0600293 putc('\n');
294 break;
295 }
296
Simon Glass6493ccc2014-04-10 20:01:26 -0600297 switch (ichar) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600298 case CTL_CH('a'):
299 BEGINNING_OF_LINE();
300 break;
301 case CTL_CH('c'): /* ^C - break */
302 *buf = '\0'; /* discard input */
303 return -1;
304 case CTL_CH('f'):
305 if (num < eol_num) {
306 getcmd_putch(buf[num]);
307 num++;
308 }
309 break;
310 case CTL_CH('b'):
311 if (num) {
312 getcmd_putch(CTL_BACKSPACE);
313 num--;
314 }
315 break;
316 case CTL_CH('d'):
317 if (num < eol_num) {
318 wlen = eol_num - num - 1;
319 if (wlen) {
320 memmove(&buf[num], &buf[num+1], wlen);
321 putnstr(buf + num, wlen);
322 }
323
324 getcmd_putch(' ');
325 do {
326 getcmd_putch(CTL_BACKSPACE);
327 } while (wlen--);
328 eol_num--;
329 }
330 break;
331 case CTL_CH('k'):
332 ERASE_TO_EOL();
333 break;
334 case CTL_CH('e'):
335 REFRESH_TO_EOL();
336 break;
337 case CTL_CH('o'):
338 insert = !insert;
339 break;
340 case CTL_CH('x'):
341 case CTL_CH('u'):
342 BEGINNING_OF_LINE();
343 ERASE_TO_EOL();
344 break;
345 case DEL:
346 case DEL7:
347 case 8:
348 if (num) {
349 wlen = eol_num - num;
350 num--;
351 memmove(&buf[num], &buf[num+1], wlen);
352 getcmd_putch(CTL_BACKSPACE);
353 putnstr(buf + num, wlen);
354 getcmd_putch(' ');
355 do {
356 getcmd_putch(CTL_BACKSPACE);
357 } while (wlen--);
358 eol_num--;
359 }
360 break;
361 case CTL_CH('p'):
362 case CTL_CH('n'):
363 {
364 char *hline;
365
Simon Glass6493ccc2014-04-10 20:01:26 -0600366 if (ichar == CTL_CH('p'))
367 hline = hist_prev();
368 else
369 hline = hist_next();
370
371 if (!hline) {
372 getcmd_cbeep();
373 continue;
374 }
375
376 /* nuke the current line */
377 /* first, go home */
378 BEGINNING_OF_LINE();
379
380 /* erase to end of line */
381 ERASE_TO_EOL();
382
383 /* copy new line into place and display */
384 strcpy(buf, hline);
385 eol_num = strlen(buf);
386 REFRESH_TO_EOL();
387 continue;
388 }
389#ifdef CONFIG_AUTO_COMPLETE
390 case '\t': {
391 int num2, col;
392
393 /* do not autocomplete when in the middle */
394 if (num < eol_num) {
395 getcmd_cbeep();
396 break;
397 }
398
399 buf[num] = '\0';
400 col = strlen(prompt) + eol_num;
401 num2 = num;
402 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
403 col = num2 - num;
404 num += col;
405 eol_num += col;
406 }
407 break;
408 }
409#endif
410 default:
Pali Rohár60d78202022-11-02 00:40:46 +0100411 cread_add_char(ichar, insert, &num, &eol_num, buf,
412 *len);
Simon Glass6493ccc2014-04-10 20:01:26 -0600413 break;
414 }
415 }
416 *len = eol_num;
417 buf[eol_num] = '\0'; /* lose the newline */
418
419 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
420 cread_add_to_hist(buf);
421 hist_cur = hist_add_idx;
422
423 return 0;
424}
425
426#endif /* CONFIG_CMDLINE_EDITING */
427
428/****************************************************************************/
429
Simon Glasse1bf8242014-04-10 20:01:27 -0600430int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600431{
432 /*
433 * If console_buffer isn't 0-length the user will be prompted to modify
434 * it instead of entering it from scratch as desired.
435 */
436 console_buffer[0] = '\0';
437
Simon Glasse1bf8242014-04-10 20:01:27 -0600438 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600439}
440
Simon Glass0f97e942023-10-01 19:13:05 -0600441/**
442 * cread_line_simple() - Simple (small) command-line reader
443 *
444 * This supports only basic editing, with no cursor movement
445 *
446 * @prompt: Prompt to display
447 * @p: Text buffer to edit
448 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
449 */
450static int cread_line_simple(const char *const prompt, char *p)
Simon Glass6493ccc2014-04-10 20:01:26 -0600451{
Simon Glass6493ccc2014-04-10 20:01:26 -0600452 char *p_buf = p;
Simon Glass0f97e942023-10-01 19:13:05 -0600453 int n = 0; /* buffer index */
454 int plen = 0; /* prompt length */
455 int col; /* output column cnt */
456 char c;
Simon Glass6493ccc2014-04-10 20:01:26 -0600457
458 /* print prompt */
459 if (prompt) {
460 plen = strlen(prompt);
461 puts(prompt);
462 }
463 col = plen;
464
465 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600466 if (bootretry_tstc_timeout())
467 return -2; /* timed out */
Stefan Roese29caf932022-09-02 14:10:46 +0200468 schedule(); /* Trigger watchdog, if needed */
Simon Glass6493ccc2014-04-10 20:01:26 -0600469
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200470 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600471
472 /*
473 * Special character handling
474 */
475 switch (c) {
476 case '\r': /* Enter */
477 case '\n':
478 *p = '\0';
479 puts("\r\n");
480 return p - p_buf;
481
482 case '\0': /* nul */
483 continue;
484
485 case 0x03: /* ^C - break */
486 p_buf[0] = '\0'; /* discard input */
487 return -1;
488
489 case 0x15: /* ^U - erase line */
490 while (col > plen) {
491 puts(erase_seq);
492 --col;
493 }
494 p = p_buf;
495 n = 0;
496 continue;
497
498 case 0x17: /* ^W - erase word */
499 p = delete_char(p_buf, p, &col, &n, plen);
500 while ((n > 0) && (*p != ' '))
501 p = delete_char(p_buf, p, &col, &n, plen);
502 continue;
503
504 case 0x08: /* ^H - backspace */
505 case 0x7F: /* DEL - backspace */
506 p = delete_char(p_buf, p, &col, &n, plen);
507 continue;
508
509 default:
510 /*
511 * Must be a normal character then
512 */
513 if (n < CONFIG_SYS_CBSIZE-2) {
514 if (c == '\t') { /* expand TABs */
515#ifdef CONFIG_AUTO_COMPLETE
516 /*
517 * if auto completion triggered just
518 * continue
519 */
520 *p = '\0';
521 if (cmd_auto_complete(prompt,
522 console_buffer,
523 &n, &col)) {
524 p = p_buf + n; /* reset */
525 continue;
526 }
527#endif
528 puts(tab_seq + (col & 07));
529 col += 8 - (col & 07);
530 } else {
Heiko Schocher80402f32015-06-29 09:10:46 +0200531 char __maybe_unused buf[2];
Simon Glass6493ccc2014-04-10 20:01:26 -0600532
533 /*
534 * Echo input using puts() to force an
535 * LCD flush if we are using an LCD
536 */
537 ++col;
538 buf[0] = c;
539 buf[1] = '\0';
540 puts(buf);
541 }
542 *p++ = c;
543 ++n;
544 } else { /* Buffer full */
545 putc('\a');
546 }
547 }
548 }
Simon Glass0f97e942023-10-01 19:13:05 -0600549}
550
551int cli_readline_into_buffer(const char *const prompt, char *buffer,
552 int timeout)
553{
554 char *p = buffer;
555#ifdef CONFIG_CMDLINE_EDITING
556 unsigned int len = CONFIG_SYS_CBSIZE;
557 int rc;
558 static int initted;
559
560 /*
561 * History uses a global array which is not
562 * writable until after relocation to RAM.
563 * Revert to non-history version if still
564 * running from flash.
565 */
566 if (gd->flags & GD_FLG_RELOC) {
567 if (!initted) {
568 hist_init();
569 initted = 1;
570 }
571
572 if (prompt)
573 puts(prompt);
574
575 rc = cread_line(prompt, p, &len, timeout);
576 return rc < 0 ? rc : len;
577
578 } else {
579#endif /* CONFIG_CMDLINE_EDITING */
580 return cread_line_simple(prompt, p);
Simon Glass6493ccc2014-04-10 20:01:26 -0600581#ifdef CONFIG_CMDLINE_EDITING
582 }
583#endif
584}