blob: 9a7c268719da46b30f2e4c2c2262e0bb8751a6f8 [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
163#ifndef CONFIG_CMDLINE_EDITING
164static void cread_print_hist_list(void)
165{
166 int i;
167 unsigned long n;
168
169 n = hist_num - hist_max;
170
171 i = hist_add_idx + 1;
172 while (1) {
173 if (i > hist_max)
174 i = 0;
175 if (i == hist_add_idx)
176 break;
177 printf("%s\n", hist_list[i]);
178 n++;
179 i++;
180 }
181}
182#endif /* CONFIG_CMDLINE_EDITING */
183
184#define BEGINNING_OF_LINE() { \
185 while (num) { \
186 getcmd_putch(CTL_BACKSPACE); \
187 num--; \
188 } \
189}
190
191#define ERASE_TO_EOL() { \
192 if (num < eol_num) { \
193 printf("%*s", (int)(eol_num - num), ""); \
194 do { \
195 getcmd_putch(CTL_BACKSPACE); \
196 } while (--eol_num > num); \
197 } \
198}
199
200#define REFRESH_TO_EOL() { \
201 if (num < eol_num) { \
202 wlen = eol_num - num; \
203 putnstr(buf + num, wlen); \
204 num = eol_num; \
205 } \
206}
207
208static void cread_add_char(char ichar, int insert, unsigned long *num,
209 unsigned long *eol_num, char *buf, unsigned long len)
210{
211 unsigned long wlen;
212
213 /* room ??? */
214 if (insert || *num == *eol_num) {
215 if (*eol_num > len - 1) {
216 getcmd_cbeep();
217 return;
218 }
219 (*eol_num)++;
220 }
221
222 if (insert) {
223 wlen = *eol_num - *num;
224 if (wlen > 1)
225 memmove(&buf[*num+1], &buf[*num], wlen-1);
226
227 buf[*num] = ichar;
228 putnstr(buf + *num, wlen);
229 (*num)++;
230 while (--wlen)
231 getcmd_putch(CTL_BACKSPACE);
232 } else {
233 /* echo the character */
234 wlen = 1;
235 buf[*num] = ichar;
236 putnstr(buf + *num, wlen);
237 (*num)++;
238 }
239}
240
241static void cread_add_str(char *str, int strsize, int insert,
242 unsigned long *num, unsigned long *eol_num,
243 char *buf, unsigned long len)
244{
245 while (strsize--) {
246 cread_add_char(*str, insert, num, eol_num, buf, len);
247 str++;
248 }
249}
250
251static int cread_line(const char *const prompt, char *buf, unsigned int *len,
252 int timeout)
253{
Simon Glassb08e9d42023-01-06 08:52:20 -0600254 struct cli_ch_state s_cch, *cch = &s_cch;
Simon Glass6493ccc2014-04-10 20:01:26 -0600255 unsigned long num = 0;
256 unsigned long eol_num = 0;
257 unsigned long wlen;
258 char ichar;
259 int insert = 1;
Simon Glass6493ccc2014-04-10 20:01:26 -0600260 int init_len = strlen(buf);
261 int first = 1;
262
Simon Glassb08e9d42023-01-06 08:52:20 -0600263 cli_ch_init(cch);
264
Simon Glass6493ccc2014-04-10 20:01:26 -0600265 if (init_len)
266 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
267
268 while (1) {
Simon Glassb08e9d42023-01-06 08:52:20 -0600269 /* Check for saved characters */
270 ichar = cli_ch_process(cch, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600271
Simon Glassb08e9d42023-01-06 08:52:20 -0600272 if (!ichar) {
273 if (bootretry_tstc_timeout())
274 return -2; /* timed out */
275 if (first && timeout) {
276 u64 etime = endtick(timeout);
277
278 while (!tstc()) { /* while no incoming data */
279 if (get_ticks() >= etime)
280 return -2; /* timed out */
281 schedule();
282 }
283 first = 0;
Simon Glass6493ccc2014-04-10 20:01:26 -0600284 }
Simon Glassb08e9d42023-01-06 08:52:20 -0600285
286 ichar = getcmd_getch();
Simon Glassbe0169f2023-03-28 08:34:14 +1300287 ichar = cli_ch_process(cch, ichar);
Simon Glass6493ccc2014-04-10 20:01:26 -0600288 }
289
Patrick Delaunay555e3782018-08-03 13:38:45 +0200290 /* ichar=0x0 when error occurs in U-Boot getc */
291 if (!ichar)
292 continue;
293
Simon Glassb08e9d42023-01-06 08:52:20 -0600294 if (ichar == '\n') {
Simon Glass6493ccc2014-04-10 20:01:26 -0600295 putc('\n');
296 break;
297 }
298
Simon Glass6493ccc2014-04-10 20:01:26 -0600299 switch (ichar) {
Simon Glass6493ccc2014-04-10 20:01:26 -0600300 case CTL_CH('a'):
301 BEGINNING_OF_LINE();
302 break;
303 case CTL_CH('c'): /* ^C - break */
304 *buf = '\0'; /* discard input */
305 return -1;
306 case CTL_CH('f'):
307 if (num < eol_num) {
308 getcmd_putch(buf[num]);
309 num++;
310 }
311 break;
312 case CTL_CH('b'):
313 if (num) {
314 getcmd_putch(CTL_BACKSPACE);
315 num--;
316 }
317 break;
318 case CTL_CH('d'):
319 if (num < eol_num) {
320 wlen = eol_num - num - 1;
321 if (wlen) {
322 memmove(&buf[num], &buf[num+1], wlen);
323 putnstr(buf + num, wlen);
324 }
325
326 getcmd_putch(' ');
327 do {
328 getcmd_putch(CTL_BACKSPACE);
329 } while (wlen--);
330 eol_num--;
331 }
332 break;
333 case CTL_CH('k'):
334 ERASE_TO_EOL();
335 break;
336 case CTL_CH('e'):
337 REFRESH_TO_EOL();
338 break;
339 case CTL_CH('o'):
340 insert = !insert;
341 break;
342 case CTL_CH('x'):
343 case CTL_CH('u'):
344 BEGINNING_OF_LINE();
345 ERASE_TO_EOL();
346 break;
347 case DEL:
348 case DEL7:
349 case 8:
350 if (num) {
351 wlen = eol_num - num;
352 num--;
353 memmove(&buf[num], &buf[num+1], wlen);
354 getcmd_putch(CTL_BACKSPACE);
355 putnstr(buf + num, wlen);
356 getcmd_putch(' ');
357 do {
358 getcmd_putch(CTL_BACKSPACE);
359 } while (wlen--);
360 eol_num--;
361 }
362 break;
363 case CTL_CH('p'):
364 case CTL_CH('n'):
365 {
366 char *hline;
367
Simon Glass6493ccc2014-04-10 20:01:26 -0600368 if (ichar == CTL_CH('p'))
369 hline = hist_prev();
370 else
371 hline = hist_next();
372
373 if (!hline) {
374 getcmd_cbeep();
375 continue;
376 }
377
378 /* nuke the current line */
379 /* first, go home */
380 BEGINNING_OF_LINE();
381
382 /* erase to end of line */
383 ERASE_TO_EOL();
384
385 /* copy new line into place and display */
386 strcpy(buf, hline);
387 eol_num = strlen(buf);
388 REFRESH_TO_EOL();
389 continue;
390 }
391#ifdef CONFIG_AUTO_COMPLETE
392 case '\t': {
393 int num2, col;
394
395 /* do not autocomplete when in the middle */
396 if (num < eol_num) {
397 getcmd_cbeep();
398 break;
399 }
400
401 buf[num] = '\0';
402 col = strlen(prompt) + eol_num;
403 num2 = num;
404 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
405 col = num2 - num;
406 num += col;
407 eol_num += col;
408 }
409 break;
410 }
411#endif
412 default:
Pali Rohár60d78202022-11-02 00:40:46 +0100413 cread_add_char(ichar, insert, &num, &eol_num, buf,
414 *len);
Simon Glass6493ccc2014-04-10 20:01:26 -0600415 break;
416 }
417 }
418 *len = eol_num;
419 buf[eol_num] = '\0'; /* lose the newline */
420
421 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
422 cread_add_to_hist(buf);
423 hist_cur = hist_add_idx;
424
425 return 0;
426}
427
428#endif /* CONFIG_CMDLINE_EDITING */
429
430/****************************************************************************/
431
Simon Glasse1bf8242014-04-10 20:01:27 -0600432int cli_readline(const char *const prompt)
Simon Glass6493ccc2014-04-10 20:01:26 -0600433{
434 /*
435 * If console_buffer isn't 0-length the user will be prompted to modify
436 * it instead of entering it from scratch as desired.
437 */
438 console_buffer[0] = '\0';
439
Simon Glasse1bf8242014-04-10 20:01:27 -0600440 return cli_readline_into_buffer(prompt, console_buffer, 0);
Simon Glass6493ccc2014-04-10 20:01:26 -0600441}
442
Simon Glass0f97e942023-10-01 19:13:05 -0600443/**
444 * cread_line_simple() - Simple (small) command-line reader
445 *
446 * This supports only basic editing, with no cursor movement
447 *
448 * @prompt: Prompt to display
449 * @p: Text buffer to edit
450 * Return: length of text buffer, or -1 if input was cannncelled (Ctrl-C)
451 */
452static int cread_line_simple(const char *const prompt, char *p)
Simon Glass6493ccc2014-04-10 20:01:26 -0600453{
Simon Glass6493ccc2014-04-10 20:01:26 -0600454 char *p_buf = p;
Simon Glass0f97e942023-10-01 19:13:05 -0600455 int n = 0; /* buffer index */
456 int plen = 0; /* prompt length */
457 int col; /* output column cnt */
458 char c;
Simon Glass6493ccc2014-04-10 20:01:26 -0600459
460 /* print prompt */
461 if (prompt) {
462 plen = strlen(prompt);
463 puts(prompt);
464 }
465 col = plen;
466
467 for (;;) {
Simon Glass0098e172014-04-10 20:01:30 -0600468 if (bootretry_tstc_timeout())
469 return -2; /* timed out */
Stefan Roese29caf932022-09-02 14:10:46 +0200470 schedule(); /* Trigger watchdog, if needed */
Simon Glass6493ccc2014-04-10 20:01:26 -0600471
Heinrich Schuchardtc670aee2020-10-07 18:11:48 +0200472 c = getchar();
Simon Glass6493ccc2014-04-10 20:01:26 -0600473
474 /*
475 * Special character handling
476 */
477 switch (c) {
478 case '\r': /* Enter */
479 case '\n':
480 *p = '\0';
481 puts("\r\n");
482 return p - p_buf;
483
484 case '\0': /* nul */
485 continue;
486
487 case 0x03: /* ^C - break */
488 p_buf[0] = '\0'; /* discard input */
489 return -1;
490
491 case 0x15: /* ^U - erase line */
492 while (col > plen) {
493 puts(erase_seq);
494 --col;
495 }
496 p = p_buf;
497 n = 0;
498 continue;
499
500 case 0x17: /* ^W - erase word */
501 p = delete_char(p_buf, p, &col, &n, plen);
502 while ((n > 0) && (*p != ' '))
503 p = delete_char(p_buf, p, &col, &n, plen);
504 continue;
505
506 case 0x08: /* ^H - backspace */
507 case 0x7F: /* DEL - backspace */
508 p = delete_char(p_buf, p, &col, &n, plen);
509 continue;
510
511 default:
512 /*
513 * Must be a normal character then
514 */
515 if (n < CONFIG_SYS_CBSIZE-2) {
516 if (c == '\t') { /* expand TABs */
517#ifdef CONFIG_AUTO_COMPLETE
518 /*
519 * if auto completion triggered just
520 * continue
521 */
522 *p = '\0';
523 if (cmd_auto_complete(prompt,
524 console_buffer,
525 &n, &col)) {
526 p = p_buf + n; /* reset */
527 continue;
528 }
529#endif
530 puts(tab_seq + (col & 07));
531 col += 8 - (col & 07);
532 } else {
Heiko Schocher80402f32015-06-29 09:10:46 +0200533 char __maybe_unused buf[2];
Simon Glass6493ccc2014-04-10 20:01:26 -0600534
535 /*
536 * Echo input using puts() to force an
537 * LCD flush if we are using an LCD
538 */
539 ++col;
540 buf[0] = c;
541 buf[1] = '\0';
542 puts(buf);
543 }
544 *p++ = c;
545 ++n;
546 } else { /* Buffer full */
547 putc('\a');
548 }
549 }
550 }
Simon Glass0f97e942023-10-01 19:13:05 -0600551}
552
553int cli_readline_into_buffer(const char *const prompt, char *buffer,
554 int timeout)
555{
556 char *p = buffer;
557#ifdef CONFIG_CMDLINE_EDITING
558 unsigned int len = CONFIG_SYS_CBSIZE;
559 int rc;
560 static int initted;
561
562 /*
563 * History uses a global array which is not
564 * writable until after relocation to RAM.
565 * Revert to non-history version if still
566 * running from flash.
567 */
568 if (gd->flags & GD_FLG_RELOC) {
569 if (!initted) {
570 hist_init();
571 initted = 1;
572 }
573
574 if (prompt)
575 puts(prompt);
576
577 rc = cread_line(prompt, p, &len, timeout);
578 return rc < 0 ? rc : len;
579
580 } else {
581#endif /* CONFIG_CMDLINE_EDITING */
582 return cread_line_simple(prompt, p);
Simon Glass6493ccc2014-04-10 20:01:26 -0600583#ifdef CONFIG_CMDLINE_EDITING
584 }
585#endif
586}