blob: e86ee73faf7afd841b331947fedff9ffaf340f7a [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
65#define CTL_CH(c) ((c) - 'a' + 1)
66#define CTL_BACKSPACE ('\b')
67#define DEL ((char)255)
68#define DEL7 ((char)127)
69#define CREAD_HIST_CHAR ('!')
70
71#define getcmd_putch(ch) putc(ch)
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +020072#define getcmd_getch() getchar()
Simon Glass6493ccc2014-04-10 20:01:26 -060073#define getcmd_cbeep() getcmd_putch('\a')
74
75#define HIST_MAX 20
76#define HIST_SIZE CONFIG_SYS_CBSIZE
77
78static int hist_max;
79static int hist_add_idx;
80static int hist_cur = -1;
81static unsigned hist_num;
82
83static char *hist_list[HIST_MAX];
84static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
85
86#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
87
88static void hist_init(void)
89{
90 int i;
91
92 hist_max = 0;
93 hist_add_idx = 0;
94 hist_cur = -1;
95 hist_num = 0;
96
97 for (i = 0; i < HIST_MAX; i++) {
98 hist_list[i] = hist_lines[i];
99 hist_list[i][0] = '\0';
100 }
101}
102
103static void cread_add_to_hist(char *line)
104{
105 strcpy(hist_list[hist_add_idx], line);
106
107 if (++hist_add_idx >= HIST_MAX)
108 hist_add_idx = 0;
109
110 if (hist_add_idx > hist_max)
111 hist_max = hist_add_idx;
112
113 hist_num++;
114}
115
116static char *hist_prev(void)
117{
118 char *ret;
119 int old_cur;
120
121 if (hist_cur < 0)
122 return NULL;
123
124 old_cur = hist_cur;
125 if (--hist_cur < 0)
126 hist_cur = hist_max;
127
128 if (hist_cur == hist_add_idx) {
129 hist_cur = old_cur;
130 ret = NULL;
131 } else {
132 ret = hist_list[hist_cur];
133 }
134
135 return ret;
136}
137
138static char *hist_next(void)
139{
140 char *ret;
141
142 if (hist_cur < 0)
143 return NULL;
144
145 if (hist_cur == hist_add_idx)
146 return NULL;
147
148 if (++hist_cur > hist_max)
149 hist_cur = 0;
150
151 if (hist_cur == hist_add_idx)
152 ret = "";
153 else
154 ret = hist_list[hist_cur];
155
156 return ret;
157}
158
159#ifndef CONFIG_CMDLINE_EDITING
160static void cread_print_hist_list(void)
161{
162 int i;
163 unsigned long n;
164
165 n = hist_num - hist_max;
166
167 i = hist_add_idx + 1;
168 while (1) {
169 if (i > hist_max)
170 i = 0;
171 if (i == hist_add_idx)
172 break;
173 printf("%s\n", hist_list[i]);
174 n++;
175 i++;
176 }
177}
178#endif /* CONFIG_CMDLINE_EDITING */
179
180#define BEGINNING_OF_LINE() { \
181 while (num) { \
182 getcmd_putch(CTL_BACKSPACE); \
183 num--; \
184 } \
185}
186
187#define ERASE_TO_EOL() { \
188 if (num < eol_num) { \
189 printf("%*s", (int)(eol_num - num), ""); \
190 do { \
191 getcmd_putch(CTL_BACKSPACE); \
192 } while (--eol_num > num); \
193 } \
194}
195
196#define REFRESH_TO_EOL() { \
197 if (num < eol_num) { \
198 wlen = eol_num - num; \
199 putnstr(buf + num, wlen); \
200 num = eol_num; \
201 } \
202}
203
204static void cread_add_char(char ichar, int insert, unsigned long *num,
205 unsigned long *eol_num, char *buf, unsigned long len)
206{
207 unsigned long wlen;
208
209 /* room ??? */
210 if (insert || *num == *eol_num) {
211 if (*eol_num > len - 1) {
212 getcmd_cbeep();
213 return;
214 }
215 (*eol_num)++;
216 }
217
218 if (insert) {
219 wlen = *eol_num - *num;
220 if (wlen > 1)
221 memmove(&buf[*num+1], &buf[*num], wlen-1);
222
223 buf[*num] = ichar;
224 putnstr(buf + *num, wlen);
225 (*num)++;
226 while (--wlen)
227 getcmd_putch(CTL_BACKSPACE);
228 } else {
229 /* echo the character */
230 wlen = 1;
231 buf[*num] = ichar;
232 putnstr(buf + *num, wlen);
233 (*num)++;
234 }
235}
236
237static void cread_add_str(char *str, int strsize, int insert,
238 unsigned long *num, unsigned long *eol_num,
239 char *buf, unsigned long len)
240{
241 while (strsize--) {
242 cread_add_char(*str, insert, num, eol_num, buf, len);
243 str++;
244 }
245}
246
247static int cread_line(const char *const prompt, char *buf, unsigned int *len,
248 int timeout)
249{
250 unsigned long num = 0;
251 unsigned long eol_num = 0;
252 unsigned long wlen;
253 char ichar;
254 int insert = 1;
255 int esc_len = 0;
256 char esc_save[8];
257 int init_len = strlen(buf);
258 int first = 1;
259
260 if (init_len)
261 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
262
263 while (1) {
Simon Glass0098e172014-04-10 20:01:30 -0600264 if (bootretry_tstc_timeout())
265 return -2; /* timed out */
Simon Glass6493ccc2014-04-10 20:01:26 -0600266 if (first && timeout) {
267 uint64_t etime = endtick(timeout);
268
269 while (!tstc()) { /* while no incoming data */
270 if (get_ticks() >= etime)
271 return -2; /* timed out */
272 WATCHDOG_RESET();
273 }
274 first = 0;
275 }
276
277 ichar = getcmd_getch();
278
Patrick Delaunay555e3782018-08-03 13:38:45 +0200279 /* ichar=0x0 when error occurs in U-Boot getc */
280 if (!ichar)
281 continue;
282
Simon Glass6493ccc2014-04-10 20:01:26 -0600283 if ((ichar == '\n') || (ichar == '\r')) {
284 putc('\n');
285 break;
286 }
287
288 /*
289 * handle standard linux xterm esc sequences for arrow key, etc.
290 */
291 if (esc_len != 0) {
James Byrnefc18e9b2016-08-16 18:16:28 +0100292 enum { ESC_REJECT, ESC_SAVE, ESC_CONVERTED } act = ESC_REJECT;
293
Simon Glass6493ccc2014-04-10 20:01:26 -0600294 if (esc_len == 1) {
James Byrnefc18e9b2016-08-16 18:16:28 +0100295 if (ichar == '[' || ichar == 'O')
296 act = ESC_SAVE;
297 } else if (esc_len == 2) {
298 switch (ichar) {
299 case 'D': /* <- key */
300 ichar = CTL_CH('b');
301 act = ESC_CONVERTED;
302 break; /* pass off to ^B handler */
303 case 'C': /* -> key */
304 ichar = CTL_CH('f');
305 act = ESC_CONVERTED;
306 break; /* pass off to ^F handler */
307 case 'H': /* Home key */
308 ichar = CTL_CH('a');
309 act = ESC_CONVERTED;
310 break; /* pass off to ^A handler */
311 case 'F': /* End key */
312 ichar = CTL_CH('e');
313 act = ESC_CONVERTED;
314 break; /* pass off to ^E handler */
315 case 'A': /* up arrow */
316 ichar = CTL_CH('p');
317 act = ESC_CONVERTED;
318 break; /* pass off to ^P handler */
319 case 'B': /* down arrow */
320 ichar = CTL_CH('n');
321 act = ESC_CONVERTED;
322 break; /* pass off to ^N handler */
323 case '1':
Heinrich Schuchardt00fa8252022-02-07 19:14:02 +0100324 case '2':
James Byrnefc18e9b2016-08-16 18:16:28 +0100325 case '3':
326 case '4':
327 case '7':
328 case '8':
329 if (esc_save[1] == '[') {
330 /* see if next character is ~ */
331 act = ESC_SAVE;
332 }
333 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600334 }
James Byrnefc18e9b2016-08-16 18:16:28 +0100335 } else if (esc_len == 3) {
Heinrich Schuchardt00fa8252022-02-07 19:14:02 +0100336 switch (ichar) {
337 case '~':
James Byrnefc18e9b2016-08-16 18:16:28 +0100338 switch (esc_save[2]) {
339 case '3': /* Delete key */
340 ichar = CTL_CH('d');
341 act = ESC_CONVERTED;
342 break; /* pass to ^D handler */
343 case '1': /* Home key */
344 case '7':
345 ichar = CTL_CH('a');
346 act = ESC_CONVERTED;
347 break; /* pass to ^A handler */
348 case '4': /* End key */
349 case '8':
350 ichar = CTL_CH('e');
351 act = ESC_CONVERTED;
352 break; /* pass to ^E handler */
353 }
Heinrich Schuchardt00fa8252022-02-07 19:14:02 +0100354 break;
355 case '0':
356 if (esc_save[2] == '2')
357 act = ESC_SAVE;
358 break;
359 }
360 } else if (esc_len == 4) {
361 switch (ichar) {
362 case '0':
363 case '1':
364 act = ESC_SAVE;
365 break; /* bracketed paste */
366 }
367 } else if (esc_len == 5) {
368 if (ichar == '~') { /* bracketed paste */
369 ichar = 0;
370 act = ESC_CONVERTED;
James Byrnefc18e9b2016-08-16 18:16:28 +0100371 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600372 }
James Byrnefc18e9b2016-08-16 18:16:28 +0100373 switch (act) {
374 case ESC_SAVE:
375 esc_save[esc_len++] = ichar;
376 continue;
377 case ESC_REJECT:
Simon Glass6493ccc2014-04-10 20:01:26 -0600378 esc_save[esc_len++] = ichar;
379 cread_add_str(esc_save, esc_len, insert,
380 &num, &eol_num, buf, *len);
381 esc_len = 0;
382 continue;
James Byrnefc18e9b2016-08-16 18:16:28 +0100383 case ESC_CONVERTED:
384 esc_len = 0;
385 break;
Simon Glass6493ccc2014-04-10 20:01:26 -0600386 }
387 }
388
389 switch (ichar) {
390 case 0x1b:
391 if (esc_len == 0) {
392 esc_save[esc_len] = ichar;
393 esc_len = 1;
394 } else {
395 puts("impossible condition #876\n");
396 esc_len = 0;
397 }
398 break;
399
400 case CTL_CH('a'):
401 BEGINNING_OF_LINE();
402 break;
403 case CTL_CH('c'): /* ^C - break */
404 *buf = '\0'; /* discard input */
405 return -1;
406 case CTL_CH('f'):
407 if (num < eol_num) {
408 getcmd_putch(buf[num]);
409 num++;
410 }
411 break;
412 case CTL_CH('b'):
413 if (num) {
414 getcmd_putch(CTL_BACKSPACE);
415 num--;
416 }
417 break;
418 case CTL_CH('d'):
419 if (num < eol_num) {
420 wlen = eol_num - num - 1;
421 if (wlen) {
422 memmove(&buf[num], &buf[num+1], wlen);
423 putnstr(buf + num, wlen);
424 }
425
426 getcmd_putch(' ');
427 do {
428 getcmd_putch(CTL_BACKSPACE);
429 } while (wlen--);
430 eol_num--;
431 }
432 break;
433 case CTL_CH('k'):
434 ERASE_TO_EOL();
435 break;
436 case CTL_CH('e'):
437 REFRESH_TO_EOL();
438 break;
439 case CTL_CH('o'):
440 insert = !insert;
441 break;
442 case CTL_CH('x'):
443 case CTL_CH('u'):
444 BEGINNING_OF_LINE();
445 ERASE_TO_EOL();
446 break;
447 case DEL:
448 case DEL7:
449 case 8:
450 if (num) {
451 wlen = eol_num - num;
452 num--;
453 memmove(&buf[num], &buf[num+1], wlen);
454 getcmd_putch(CTL_BACKSPACE);
455 putnstr(buf + num, wlen);
456 getcmd_putch(' ');
457 do {
458 getcmd_putch(CTL_BACKSPACE);
459 } while (wlen--);
460 eol_num--;
461 }
462 break;
463 case CTL_CH('p'):
464 case CTL_CH('n'):
465 {
466 char *hline;
467
468 esc_len = 0;
469
470 if (ichar == CTL_CH('p'))
471 hline = hist_prev();
472 else
473 hline = hist_next();
474
475 if (!hline) {
476 getcmd_cbeep();
477 continue;
478 }
479
480 /* nuke the current line */
481 /* first, go home */
482 BEGINNING_OF_LINE();
483
484 /* erase to end of line */
485 ERASE_TO_EOL();
486
487 /* copy new line into place and display */
488 strcpy(buf, hline);
489 eol_num = strlen(buf);
490 REFRESH_TO_EOL();
491 continue;
492 }
493#ifdef CONFIG_AUTO_COMPLETE
494 case '\t': {
495 int num2, col;
496
497 /* do not autocomplete when in the middle */
498 if (num < eol_num) {
499 getcmd_cbeep();
500 break;
501 }
502
503 buf[num] = '\0';
504 col = strlen(prompt) + eol_num;
505 num2 = num;
506 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
507 col = num2 - num;
508 num += col;
509 eol_num += col;
510 }
511 break;
512 }
513#endif
514 default:
Steve Bennettd2e64d22020-11-22 14:58:45 +1000515 if (ichar >= ' ' && ichar <= '~') {
516 cread_add_char(ichar, insert, &num, &eol_num,
517 buf, *len);
518 }
Simon Glass6493ccc2014-04-10 20:01:26 -0600519 break;
520 }
521 }
522 *len = eol_num;
523 buf[eol_num] = '\0'; /* lose the newline */
524
525 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
526 cread_add_to_hist(buf);
527 hist_cur = hist_add_idx;
528
529 return 0;
530}
531
532#endif /* CONFIG_CMDLINE_EDITING */
533
534/****************************************************************************/
535
Simon Glasse1bf8242014-04-10 20:01:27 -0600536int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600537{
538 /*
539 * If console_buffer isn't 0-length the user will be prompted to modify
540 * it instead of entering it from scratch as desired.
541 */
542 console_buffer[0] = '\0';
543
Simon Glasse1bf8242014-04-10 20:01:27 -0600544 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600545}
546
547
Simon Glasse1bf8242014-04-10 20:01:27 -0600548int cli_readline_into_buffer(const char *const prompt, char *buffer,
549 int timeout)
Simon Glass6493ccc2014-04-10 20:01:26 -0600550{
551 char *p = buffer;
552#ifdef CONFIG_CMDLINE_EDITING
553 unsigned int len = CONFIG_SYS_CBSIZE;
554 int rc;
555 static int initted;
556
557 /*
558 * History uses a global array which is not
559 * writable until after relocation to RAM.
560 * Revert to non-history version if still
561 * running from flash.
562 */
563 if (gd->flags & GD_FLG_RELOC) {
564 if (!initted) {
565 hist_init();
566 initted = 1;
567 }
568
569 if (prompt)
570 puts(prompt);
571
572 rc = cread_line(prompt, p, &len, timeout);
573 return rc < 0 ? rc : len;
574
575 } else {
576#endif /* CONFIG_CMDLINE_EDITING */
577 char *p_buf = p;
578 int n = 0; /* buffer index */
579 int plen = 0; /* prompt length */
580 int col; /* output column cnt */
581 char c;
582
583 /* print prompt */
584 if (prompt) {
585 plen = strlen(prompt);
586 puts(prompt);
587 }
588 col = plen;
589
590 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600591 if (bootretry_tstc_timeout())
592 return -2; /* timed out */
Simon Glass6493ccc2014-04-10 20:01:26 -0600593 WATCHDOG_RESET(); /* Trigger watchdog, if needed */
594
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200595 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600596
597 /*
598 * Special character handling
599 */
600 switch (c) {
601 case '\r': /* Enter */
602 case '\n':
603 *p = '\0';
604 puts("\r\n");
605 return p - p_buf;
606
607 case '\0': /* nul */
608 continue;
609
610 case 0x03: /* ^C - break */
611 p_buf[0] = '\0'; /* discard input */
612 return -1;
613
614 case 0x15: /* ^U - erase line */
615 while (col > plen) {
616 puts(erase_seq);
617 --col;
618 }
619 p = p_buf;
620 n = 0;
621 continue;
622
623 case 0x17: /* ^W - erase word */
624 p = delete_char(p_buf, p, &col, &n, plen);
625 while ((n > 0) && (*p != ' '))
626 p = delete_char(p_buf, p, &col, &n, plen);
627 continue;
628
629 case 0x08: /* ^H - backspace */
630 case 0x7F: /* DEL - backspace */
631 p = delete_char(p_buf, p, &col, &n, plen);
632 continue;
633
634 default:
635 /*
636 * Must be a normal character then
637 */
638 if (n < CONFIG_SYS_CBSIZE-2) {
639 if (c == '\t') { /* expand TABs */
640#ifdef CONFIG_AUTO_COMPLETE
641 /*
642 * if auto completion triggered just
643 * continue
644 */
645 *p = '\0';
646 if (cmd_auto_complete(prompt,
647 console_buffer,
648 &n, &col)) {
649 p = p_buf + n; /* reset */
650 continue;
651 }
652#endif
653 puts(tab_seq + (col & 07));
654 col += 8 - (col & 07);
655 } else {
Heiko Schocher80402f32015-06-29 09:10:46 +0200656 char __maybe_unused buf[2];
Simon Glass6493ccc2014-04-10 20:01:26 -0600657
658 /*
659 * Echo input using puts() to force an
660 * LCD flush if we are using an LCD
661 */
662 ++col;
663 buf[0] = c;
664 buf[1] = '\0';
665 puts(buf);
666 }
667 *p++ = c;
668 ++n;
669 } else { /* Buffer full */
670 putc('\a');
671 }
672 }
673 }
674#ifdef CONFIG_CMDLINE_EDITING
675 }
676#endif
677}