blob: 687e239bfa77ec856e200fed5214e198c86f3508 [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;
174 unsigned long n;
175
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() { \
191 while (num) { \
192 getcmd_putch(CTL_BACKSPACE); \
193 num--; \
194 } \
195}
196
197#define ERASE_TO_EOL() { \
198 if (num < eol_num) { \
199 printf("%*s", (int)(eol_num - num), ""); \
200 do { \
201 getcmd_putch(CTL_BACKSPACE); \
202 } while (--eol_num > num); \
203 } \
204}
205
206#define REFRESH_TO_EOL() { \
207 if (num < eol_num) { \
208 wlen = eol_num - num; \
209 putnstr(buf + num, wlen); \
210 num = eol_num; \
211 } \
212}
213
214static void cread_add_char(char ichar, int insert, unsigned long *num,
215 unsigned long *eol_num, char *buf, unsigned long len)
216{
217 unsigned long wlen;
218
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,
248 unsigned long *num, unsigned long *eol_num,
249 char *buf, unsigned long len)
250{
251 while (strsize--) {
252 cread_add_char(*str, insert, num, eol_num, buf, len);
253 str++;
254 }
255}
256
257static int cread_line(const char *const prompt, char *buf, unsigned int *len,
258 int timeout)
259{
Simon Glassb08e9d42023-01-06 08:52:20 -0600260 struct cli_ch_state s_cch, *cch = &s_cch;
Simon Glass6493ccc2014-04-10 20:01:26 -0600261 unsigned long num = 0;
262 unsigned long eol_num = 0;
263 unsigned long wlen;
264 char ichar;
265 int insert = 1;
Simon Glass6493ccc2014-04-10 20:01:26 -0600266 int init_len = strlen(buf);
267 int first = 1;
268
Simon Glassb08e9d42023-01-06 08:52:20 -0600269 cli_ch_init(cch);
270
Simon Glass6493ccc2014-04-10 20:01:26 -0600271 if (init_len)
272 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
273
274 while (1) {
Simon Glassb08e9d42023-01-06 08:52:20 -0600275 /* Check for saved characters */
276 ichar = cli_ch_process(cch, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600277
Simon Glassb08e9d42023-01-06 08:52:20 -0600278 if (!ichar) {
279 if (bootretry_tstc_timeout())
280 return -2; /* timed out */
281 if (first && timeout) {
282 u64 etime = endtick(timeout);
283
284 while (!tstc()) { /* while no incoming data */
285 if (get_ticks() >= etime)
286 return -2; /* timed out */
287 schedule();
288 }
289 first = 0;
Simon Glass6493ccc2014-04-10 20:01:26 -0600290 }
Simon Glassb08e9d42023-01-06 08:52:20 -0600291
292 ichar = getcmd_getch();
Simon Glassbe0169f2023-03-28 08:34:14 +1300293 ichar = cli_ch_process(cch, ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600294 }
295
Patrick Delaunay555e3782018-08-03 13:38:45 +0200296 /* ichar=0x0 when error occurs in U-Boot getc */
297 if (!ichar)
298 continue;
299
Simon Glassb08e9d42023-01-06 08:52:20 -0600300 if (ichar == '\n') {
Simon Glass6493ccc2014-04-10 20:01:26 -0600301 putc('\n');
302 break;
303 }
304
Simon Glass6493ccc2014-04-10 20:01:26 -0600305 switch (ichar) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600306 case CTL_CH('a'):
307 BEGINNING_OF_LINE();
308 break;
309 case CTL_CH('c'): /* ^C - break */
310 *buf = '\0'; /* discard input */
311 return -1;
312 case CTL_CH('f'):
313 if (num < eol_num) {
314 getcmd_putch(buf[num]);
315 num++;
316 }
317 break;
318 case CTL_CH('b'):
319 if (num) {
320 getcmd_putch(CTL_BACKSPACE);
321 num--;
322 }
323 break;
324 case CTL_CH('d'):
325 if (num < eol_num) {
326 wlen = eol_num - num - 1;
327 if (wlen) {
328 memmove(&buf[num], &buf[num+1], wlen);
329 putnstr(buf + num, wlen);
330 }
331
332 getcmd_putch(' ');
333 do {
334 getcmd_putch(CTL_BACKSPACE);
335 } while (wlen--);
336 eol_num--;
337 }
338 break;
339 case CTL_CH('k'):
340 ERASE_TO_EOL();
341 break;
342 case CTL_CH('e'):
343 REFRESH_TO_EOL();
344 break;
345 case CTL_CH('o'):
346 insert = !insert;
347 break;
Simon Glassdcc18ce2023-10-01 19:13:09 -0600348 case CTL_CH('w'):
349 if (num) {
350 uint base;
351
352 for (base = num - 1;
353 base >= 0 && buf[base] == ' ';)
354 base--;
355 for (; base > 0 && buf[base - 1] != ' ';)
356 base--;
357
358 /* now delete chars from base to num */
359 wlen = num - base;
360 eol_num -= wlen;
361 memmove(&buf[base], &buf[num],
362 eol_num - base + 1);
363 num = base;
364 getcmd_putchars(wlen, CTL_BACKSPACE);
365 puts(buf + base);
366 getcmd_putchars(wlen, ' ');
367 getcmd_putchars(wlen + eol_num - num,
368 CTL_BACKSPACE);
369 }
370 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600371 case CTL_CH('x'):
372 case CTL_CH('u'):
373 BEGINNING_OF_LINE();
374 ERASE_TO_EOL();
375 break;
376 case DEL:
377 case DEL7:
378 case 8:
379 if (num) {
380 wlen = eol_num - num;
381 num--;
382 memmove(&buf[num], &buf[num+1], wlen);
383 getcmd_putch(CTL_BACKSPACE);
384 putnstr(buf + num, wlen);
385 getcmd_putch(' ');
386 do {
387 getcmd_putch(CTL_BACKSPACE);
388 } while (wlen--);
389 eol_num--;
390 }
391 break;
392 case CTL_CH('p'):
393 case CTL_CH('n'):
394 {
395 char *hline;
396
Simon Glass6493ccc2014-04-10 20:01:26 -0600397 if (ichar == CTL_CH('p'))
398 hline = hist_prev();
399 else
400 hline = hist_next();
401
402 if (!hline) {
403 getcmd_cbeep();
404 continue;
405 }
406
407 /* nuke the current line */
408 /* first, go home */
409 BEGINNING_OF_LINE();
410
411 /* erase to end of line */
412 ERASE_TO_EOL();
413
414 /* copy new line into place and display */
415 strcpy(buf, hline);
416 eol_num = strlen(buf);
417 REFRESH_TO_EOL();
418 continue;
419 }
Simon Glass63213912023-10-01 19:13:08 -0600420 case '\t':
421 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
422 int num2, col;
Simon Glass6493ccc2014-04-10 20:01:26 -0600423
Simon Glass63213912023-10-01 19:13:08 -0600424 /* do not autocomplete when in the middle */
425 if (num < eol_num) {
426 getcmd_cbeep();
427 break;
428 }
429
430 buf[num] = '\0';
431 col = strlen(prompt) + eol_num;
432 num2 = num;
433 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
434 col = num2 - num;
435 num += col;
436 eol_num += col;
437 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600438 break;
439 }
Simon Glass63213912023-10-01 19:13:08 -0600440 fallthrough;
Simon Glass6493ccc2014-04-10 20:01:26 -0600441 default:
Pali Rohár60d78202022-11-02 00:40:46 +0100442 cread_add_char(ichar, insert, &num, &eol_num, buf,
443 *len);
Simon Glass6493ccc2014-04-10 20:01:26 -0600444 break;
445 }
446 }
447 *len = eol_num;
448 buf[eol_num] = '\0'; /* lose the newline */
449
450 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
451 cread_add_to_hist(buf);
452 hist_cur = hist_add_idx;
453
454 return 0;
455}
456
Simon Glass039f8cc2023-10-01 19:13:07 -0600457#else /* !CONFIG_CMDLINE_EDITING */
458
459static inline void hist_init(void)
460{
461}
462
463static int cread_line(const char *const prompt, char *buf, unsigned int *len,
464 int timeout)
465{
466 return 0;
467}
468
Simon Glass6493ccc2014-04-10 20:01:26 -0600469#endif /* CONFIG_CMDLINE_EDITING */
470
471/****************************************************************************/
472
Simon Glasse1bf8242014-04-10 20:01:27 -0600473int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600474{
475 /*
476 * If console_buffer isn't 0-length the user will be prompted to modify
477 * it instead of entering it from scratch as desired.
478 */
479 console_buffer[0] = '\0';
480
Simon Glasse1bf8242014-04-10 20:01:27 -0600481 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600482}
483
Simon Glass0f97e942023-10-01 19:13:05 -0600484/**
485 * cread_line_simple() - Simple (small) command-line reader
486 *
487 * This supports only basic editing, with no cursor movement
488 *
489 * @prompt: Prompt to display
490 * @p: Text buffer to edit
491 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
492 */
493static int cread_line_simple(const char *const prompt, char *p)
Simon Glass6493ccc2014-04-10 20:01:26 -0600494{
Simon Glass6493ccc2014-04-10 20:01:26 -0600495 char *p_buf = p;
Simon Glass0f97e942023-10-01 19:13:05 -0600496 int n = 0; /* buffer index */
497 int plen = 0; /* prompt length */
498 int col; /* output column cnt */
499 char c;
Simon Glass6493ccc2014-04-10 20:01:26 -0600500
501 /* print prompt */
502 if (prompt) {
503 plen = strlen(prompt);
504 puts(prompt);
505 }
506 col = plen;
507
508 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600509 if (bootretry_tstc_timeout())
510 return -2; /* timed out */
Stefan Roese29caf932022-09-02 14:10:46 +0200511 schedule(); /* Trigger watchdog, if needed */
Simon Glass6493ccc2014-04-10 20:01:26 -0600512
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200513 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600514
515 /*
516 * Special character handling
517 */
518 switch (c) {
519 case '\r': /* Enter */
520 case '\n':
521 *p = '\0';
522 puts("\r\n");
523 return p - p_buf;
524
525 case '\0': /* nul */
526 continue;
527
528 case 0x03: /* ^C - break */
529 p_buf[0] = '\0'; /* discard input */
530 return -1;
531
532 case 0x15: /* ^U - erase line */
533 while (col > plen) {
534 puts(erase_seq);
535 --col;
536 }
537 p = p_buf;
538 n = 0;
539 continue;
540
541 case 0x17: /* ^W - erase word */
542 p = delete_char(p_buf, p, &col, &n, plen);
543 while ((n > 0) && (*p != ' '))
544 p = delete_char(p_buf, p, &col, &n, plen);
545 continue;
546
547 case 0x08: /* ^H - backspace */
548 case 0x7F: /* DEL - backspace */
549 p = delete_char(p_buf, p, &col, &n, plen);
550 continue;
551
552 default:
Simon Glass63213912023-10-01 19:13:08 -0600553 /* Must be a normal character then */
554 if (n >= CONFIG_SYS_CBSIZE - 2) { /* Buffer full */
555 putc('\a');
556 break;
557 }
558 if (c == '\t') { /* expand TABs */
559 if (IS_ENABLED(CONFIG_AUTO_COMPLETE)) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600560 /*
Simon Glass63213912023-10-01 19:13:08 -0600561 * if auto-completion triggered just
Simon Glass6493ccc2014-04-10 20:01:26 -0600562 * continue
563 */
564 *p = '\0';
565 if (cmd_auto_complete(prompt,
566 console_buffer,
567 &n, &col)) {
568 p = p_buf + n; /* reset */
569 continue;
570 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600571 }
Simon Glass63213912023-10-01 19:13:08 -0600572 puts(tab_seq + (col & 07));
573 col += 8 - (col & 07);
574 } else {
575 char __maybe_unused buf[2];
576
577 /*
578 * Echo input using puts() to force an LCD
579 * flush if we are using an LCD
580 */
581 ++col;
582 buf[0] = c;
583 buf[1] = '\0';
584 puts(buf);
Simon Glass6493ccc2014-04-10 20:01:26 -0600585 }
Simon Glass63213912023-10-01 19:13:08 -0600586 *p++ = c;
587 ++n;
588 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600589 }
590 }
Simon Glass0f97e942023-10-01 19:13:05 -0600591}
592
593int cli_readline_into_buffer(const char *const prompt, char *buffer,
594 int timeout)
595{
596 char *p = buffer;
Simon Glass039f8cc2023-10-01 19:13:07 -0600597 uint len = CONFIG_SYS_CBSIZE;
Simon Glass0f97e942023-10-01 19:13:05 -0600598 int rc;
599 static int initted;
600
601 /*
602 * History uses a global array which is not
603 * writable until after relocation to RAM.
604 * Revert to non-history version if still
605 * running from flash.
606 */
Simon Glass039f8cc2023-10-01 19:13:07 -0600607 if (IS_ENABLED(CONFIG_CMDLINE_EDITING) && (gd->flags & GD_FLG_RELOC)) {
Simon Glass0f97e942023-10-01 19:13:05 -0600608 if (!initted) {
609 hist_init();
610 initted = 1;
611 }
612
613 if (prompt)
614 puts(prompt);
615
616 rc = cread_line(prompt, p, &len, timeout);
617 return rc < 0 ? rc : len;
618
619 } else {
Simon Glass0f97e942023-10-01 19:13:05 -0600620 return cread_line_simple(prompt, p);
Simon Glass6493ccc2014-04-10 20:01:26 -0600621 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600622}