common: add call to show_activity() in main cmd loop
[karo-tx-uboot.git] / common / cli_readline.c
1 /*
2  * (C) Copyright 2000
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * Add to readline cmdline-editing by
6  * (C) Copyright 2005
7  * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
8  *
9  * SPDX-License-Identifier:     GPL-2.0+
10  */
11
12 #include <common.h>
13 #include <bootretry.h>
14 #include <cli.h>
15 #include <watchdog.h>
16
17 DECLARE_GLOBAL_DATA_PTR;
18
19 static const char erase_seq[] = "\b \b";        /* erase sequence */
20 static const char   tab_seq[] = "        ";     /* used to expand TABs */
21
22 char console_buffer[CONFIG_SYS_CBSIZE + 1];     /* console I/O buffer   */
23
24 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
25 {
26         char *s;
27
28         if (*np == 0)
29                 return p;
30
31         if (*(--p) == '\t') {           /* will retype the whole line */
32                 while (*colp > plen) {
33                         puts(erase_seq);
34                         (*colp)--;
35                 }
36                 for (s = buffer; s < p; ++s) {
37                         if (*s == '\t') {
38                                 puts(tab_seq + ((*colp) & 07));
39                                 *colp += 8 - ((*colp) & 07);
40                         } else {
41                                 ++(*colp);
42                                 putc(*s);
43                         }
44                 }
45         } else {
46                 puts(erase_seq);
47                 (*colp)--;
48         }
49         (*np)--;
50
51         return p;
52 }
53
54 #ifdef CONFIG_CMDLINE_EDITING
55
56 /*
57  * cmdline-editing related codes from vivi.
58  * Author: Janghoon Lyu <nandy@mizi.com>
59  */
60
61 #define putnstr(str, n) printf("%.*s", (int)n, str)
62
63 #define CTL_CH(c)               ((c) - 'a' + 1)
64 #define CTL_BACKSPACE           ('\b')
65 #define DEL                     ((char)255)
66 #define DEL7                    ((char)127)
67 #define CREAD_HIST_CHAR         ('!')
68
69 #define getcmd_putch(ch)        putc(ch)
70 #define getcmd_getch()          getc()
71 #define getcmd_cbeep()          getcmd_putch('\a')
72
73 #define HIST_MAX                20
74 #define HIST_SIZE               CONFIG_SYS_CBSIZE
75
76 static int hist_max;
77 static int hist_add_idx;
78 static int hist_cur = -1;
79 static unsigned hist_num;
80
81 static char *hist_list[HIST_MAX];
82 static char hist_lines[HIST_MAX][HIST_SIZE + 1];        /* Save room for NULL */
83
84 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
85
86 static void hist_init(void)
87 {
88         int i;
89
90         hist_max = 0;
91         hist_add_idx = 0;
92         hist_cur = -1;
93         hist_num = 0;
94
95         for (i = 0; i < HIST_MAX; i++) {
96                 hist_list[i] = hist_lines[i];
97                 hist_list[i][0] = '\0';
98         }
99 }
100
101 static void cread_add_to_hist(char *line)
102 {
103         strcpy(hist_list[hist_add_idx], line);
104
105         if (++hist_add_idx >= HIST_MAX)
106                 hist_add_idx = 0;
107
108         if (hist_add_idx > hist_max)
109                 hist_max = hist_add_idx;
110
111         hist_num++;
112 }
113
114 static char *hist_prev(void)
115 {
116         char *ret;
117         int old_cur;
118
119         if (hist_cur < 0)
120                 return NULL;
121
122         old_cur = hist_cur;
123         if (--hist_cur < 0)
124                 hist_cur = hist_max;
125
126         if (hist_cur == hist_add_idx) {
127                 hist_cur = old_cur;
128                 ret = NULL;
129         } else {
130                 ret = hist_list[hist_cur];
131         }
132
133         return ret;
134 }
135
136 static char *hist_next(void)
137 {
138         char *ret;
139
140         if (hist_cur < 0)
141                 return NULL;
142
143         if (hist_cur == hist_add_idx)
144                 return NULL;
145
146         if (++hist_cur > hist_max)
147                 hist_cur = 0;
148
149         if (hist_cur == hist_add_idx)
150                 ret = "";
151         else
152                 ret = hist_list[hist_cur];
153
154         return ret;
155 }
156
157 #ifndef CONFIG_CMDLINE_EDITING
158 static void cread_print_hist_list(void)
159 {
160         int i;
161         unsigned long n;
162
163         n = hist_num - hist_max;
164
165         i = hist_add_idx + 1;
166         while (1) {
167                 if (i > hist_max)
168                         i = 0;
169                 if (i == hist_add_idx)
170                         break;
171                 printf("%s\n", hist_list[i]);
172                 n++;
173                 i++;
174         }
175 }
176 #endif /* CONFIG_CMDLINE_EDITING */
177
178 #define BEGINNING_OF_LINE() {                   \
179         while (num) {                           \
180                 getcmd_putch(CTL_BACKSPACE);    \
181                 num--;                          \
182         }                                       \
183 }
184
185 #define ERASE_TO_EOL() {                                \
186         if (num < eol_num) {                            \
187                 printf("%*s", (int)(eol_num - num), ""); \
188                 do {                                    \
189                         getcmd_putch(CTL_BACKSPACE);    \
190                 } while (--eol_num > num);              \
191         }                                               \
192 }
193
194 #define REFRESH_TO_EOL() {                      \
195         if (num < eol_num) {                    \
196                 wlen = eol_num - num;           \
197                 putnstr(buf + num, wlen);       \
198                 num = eol_num;                  \
199         }                                       \
200 }
201
202 static void cread_add_char(char ichar, int insert, unsigned long *num,
203                unsigned long *eol_num, char *buf, unsigned long len)
204 {
205         unsigned long wlen;
206
207         /* room ??? */
208         if (insert || *num == *eol_num) {
209                 if (*eol_num > len - 1) {
210                         getcmd_cbeep();
211                         return;
212                 }
213                 (*eol_num)++;
214         }
215
216         if (insert) {
217                 wlen = *eol_num - *num;
218                 if (wlen > 1)
219                         memmove(&buf[*num+1], &buf[*num], wlen-1);
220
221                 buf[*num] = ichar;
222                 putnstr(buf + *num, wlen);
223                 (*num)++;
224                 while (--wlen)
225                         getcmd_putch(CTL_BACKSPACE);
226         } else {
227                 /* echo the character */
228                 wlen = 1;
229                 buf[*num] = ichar;
230                 putnstr(buf + *num, wlen);
231                 (*num)++;
232         }
233 }
234
235 static void cread_add_str(char *str, int strsize, int insert,
236                           unsigned long *num, unsigned long *eol_num,
237                           char *buf, unsigned long len)
238 {
239         while (strsize--) {
240                 cread_add_char(*str, insert, num, eol_num, buf, len);
241                 str++;
242         }
243 }
244
245 static int cread_line(const char *const prompt, char *buf, unsigned int *len,
246                 int timeout)
247 {
248         unsigned long num = 0;
249         unsigned long eol_num = 0;
250         unsigned long wlen;
251         char ichar;
252         int insert = 1;
253         int esc_len = 0;
254         char esc_save[8];
255         int init_len = strlen(buf);
256         int first = 1;
257
258         if (init_len)
259                 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
260
261         while (1) {
262                 if (bootretry_tstc_timeout())
263                         return -2;      /* timed out */
264                 if (first && timeout) {
265                         uint64_t etime = endtick(timeout);
266
267                         while (!tstc()) {       /* while no incoming data */
268                                 if (get_ticks() >= etime)
269                                         return -2;      /* timed out */
270                                 WATCHDOG_RESET();
271                         }
272                         first = 0;
273                 }
274
275 #ifdef CONFIG_SHOW_ACTIVITY
276                 while (!tstc()) {       /* while no incoming data */
277                         show_activity(0);
278                         WATCHDOG_RESET();
279                 }
280 #endif
281                 ichar = getcmd_getch();
282
283                 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) {
292                         if (esc_len == 1) {
293                                 if (ichar == '[') {
294                                         esc_save[esc_len] = ichar;
295                                         esc_len = 2;
296                                 } else {
297                                         cread_add_str(esc_save, esc_len,
298                                                       insert, &num, &eol_num,
299                                                       buf, *len);
300                                         esc_len = 0;
301                                 }
302                                 continue;
303                         }
304
305                         switch (ichar) {
306                         case 'D':       /* <- key */
307                                 ichar = CTL_CH('b');
308                                 esc_len = 0;
309                                 break;
310                         case 'C':       /* -> key */
311                                 ichar = CTL_CH('f');
312                                 esc_len = 0;
313                                 break;  /* pass off to ^F handler */
314                         case 'H':       /* Home key */
315                                 ichar = CTL_CH('a');
316                                 esc_len = 0;
317                                 break;  /* pass off to ^A handler */
318                         case 'A':       /* up arrow */
319                                 ichar = CTL_CH('p');
320                                 esc_len = 0;
321                                 break;  /* pass off to ^P handler */
322                         case 'B':       /* down arrow */
323                                 ichar = CTL_CH('n');
324                                 esc_len = 0;
325                                 break;  /* pass off to ^N handler */
326                         default:
327                                 esc_save[esc_len++] = ichar;
328                                 cread_add_str(esc_save, esc_len, insert,
329                                               &num, &eol_num, buf, *len);
330                                 esc_len = 0;
331                                 continue;
332                         }
333                 }
334
335                 switch (ichar) {
336                 case 0x1b:
337                         if (esc_len == 0) {
338                                 esc_save[esc_len] = ichar;
339                                 esc_len = 1;
340                         } else {
341                                 puts("impossible condition #876\n");
342                                 esc_len = 0;
343                         }
344                         break;
345
346                 case CTL_CH('a'):
347                         BEGINNING_OF_LINE();
348                         break;
349                 case CTL_CH('c'):       /* ^C - break */
350                         *buf = '\0';    /* discard input */
351                         return -1;
352                 case CTL_CH('f'):
353                         if (num < eol_num) {
354                                 getcmd_putch(buf[num]);
355                                 num++;
356                         }
357                         break;
358                 case CTL_CH('b'):
359                         if (num) {
360                                 getcmd_putch(CTL_BACKSPACE);
361                                 num--;
362                         }
363                         break;
364                 case CTL_CH('d'):
365                         if (num < eol_num) {
366                                 wlen = eol_num - num - 1;
367                                 if (wlen) {
368                                         memmove(&buf[num], &buf[num+1], wlen);
369                                         putnstr(buf + num, wlen);
370                                 }
371
372                                 getcmd_putch(' ');
373                                 do {
374                                         getcmd_putch(CTL_BACKSPACE);
375                                 } while (wlen--);
376                                 eol_num--;
377                         }
378                         break;
379                 case CTL_CH('k'):
380                         ERASE_TO_EOL();
381                         break;
382                 case CTL_CH('e'):
383                         REFRESH_TO_EOL();
384                         break;
385                 case CTL_CH('o'):
386                         insert = !insert;
387                         break;
388                 case CTL_CH('x'):
389                 case CTL_CH('u'):
390                         BEGINNING_OF_LINE();
391                         ERASE_TO_EOL();
392                         break;
393                 case DEL:
394                 case DEL7:
395                 case 8:
396                         if (num) {
397                                 wlen = eol_num - num;
398                                 num--;
399                                 memmove(&buf[num], &buf[num+1], wlen);
400                                 getcmd_putch(CTL_BACKSPACE);
401                                 putnstr(buf + num, wlen);
402                                 getcmd_putch(' ');
403                                 do {
404                                         getcmd_putch(CTL_BACKSPACE);
405                                 } while (wlen--);
406                                 eol_num--;
407                         }
408                         break;
409                 case CTL_CH('p'):
410                 case CTL_CH('n'):
411                 {
412                         char *hline;
413
414                         esc_len = 0;
415
416                         if (ichar == CTL_CH('p'))
417                                 hline = hist_prev();
418                         else
419                                 hline = hist_next();
420
421                         if (!hline) {
422                                 getcmd_cbeep();
423                                 continue;
424                         }
425
426                         /* nuke the current line */
427                         /* first, go home */
428                         BEGINNING_OF_LINE();
429
430                         /* erase to end of line */
431                         ERASE_TO_EOL();
432
433                         /* copy new line into place and display */
434                         strcpy(buf, hline);
435                         eol_num = strlen(buf);
436                         REFRESH_TO_EOL();
437                         continue;
438                 }
439 #ifdef CONFIG_AUTO_COMPLETE
440                 case '\t': {
441                         int num2, col;
442
443                         /* do not autocomplete when in the middle */
444                         if (num < eol_num) {
445                                 getcmd_cbeep();
446                                 break;
447                         }
448
449                         buf[num] = '\0';
450                         col = strlen(prompt) + eol_num;
451                         num2 = num;
452                         if (cmd_auto_complete(prompt, buf, &num2, &col)) {
453                                 col = num2 - num;
454                                 num += col;
455                                 eol_num += col;
456                         }
457                         break;
458                 }
459 #endif
460                 default:
461                         cread_add_char(ichar, insert, &num, &eol_num, buf,
462                                        *len);
463                         break;
464                 }
465         }
466         *len = eol_num;
467         buf[eol_num] = '\0';    /* lose the newline */
468
469         if (buf[0] && buf[0] != CREAD_HIST_CHAR)
470                 cread_add_to_hist(buf);
471         hist_cur = hist_add_idx;
472
473         return 0;
474 }
475
476 #endif /* CONFIG_CMDLINE_EDITING */
477
478 /****************************************************************************/
479
480 int cli_readline(const char *const prompt)
481 {
482         /*
483          * If console_buffer isn't 0-length the user will be prompted to modify
484          * it instead of entering it from scratch as desired.
485          */
486         console_buffer[0] = '\0';
487
488         return cli_readline_into_buffer(prompt, console_buffer, 0);
489 }
490
491
492 int cli_readline_into_buffer(const char *const prompt, char *buffer,
493                              int timeout)
494 {
495         char *p = buffer;
496 #ifdef CONFIG_CMDLINE_EDITING
497         unsigned int len = CONFIG_SYS_CBSIZE;
498         int rc;
499         static int initted;
500
501         /*
502          * History uses a global array which is not
503          * writable until after relocation to RAM.
504          * Revert to non-history version if still
505          * running from flash.
506          */
507         if (gd->flags & GD_FLG_RELOC) {
508                 if (!initted) {
509                         hist_init();
510                         initted = 1;
511                 }
512
513                 if (prompt)
514                         puts(prompt);
515
516                 rc = cread_line(prompt, p, &len, timeout);
517                 return rc < 0 ? rc : len;
518
519         } else {
520 #endif  /* CONFIG_CMDLINE_EDITING */
521         char *p_buf = p;
522         int     n = 0;                          /* buffer index         */
523         int     plen = 0;                       /* prompt length        */
524         int     col;                            /* output column cnt    */
525         char    c;
526
527         /* print prompt */
528         if (prompt) {
529                 plen = strlen(prompt);
530                 puts(prompt);
531         }
532         col = plen;
533
534         for (;;) {
535                 if (bootretry_tstc_timeout())
536                         return -2;      /* timed out */
537                 WATCHDOG_RESET();       /* Trigger watchdog, if needed */
538
539 #ifdef CONFIG_SHOW_ACTIVITY
540                 while (!tstc()) {
541                         show_activity(0);
542                         WATCHDOG_RESET();
543                 }
544 #endif
545                 c = getc();
546
547                 /*
548                  * Special character handling
549                  */
550                 switch (c) {
551                 case '\r':                      /* Enter                */
552                 case '\n':
553                         *p = '\0';
554                         puts("\r\n");
555                         return p - p_buf;
556
557                 case '\0':                      /* nul                  */
558                         continue;
559
560                 case 0x03:                      /* ^C - break           */
561                         p_buf[0] = '\0';        /* discard input */
562                         return -1;
563
564                 case 0x15:                      /* ^U - erase line      */
565                         while (col > plen) {
566                                 puts(erase_seq);
567                                 --col;
568                         }
569                         p = p_buf;
570                         n = 0;
571                         continue;
572
573                 case 0x17:                      /* ^W - erase word      */
574                         p = delete_char(p_buf, p, &col, &n, plen);
575                         while ((n > 0) && (*p != ' '))
576                                 p = delete_char(p_buf, p, &col, &n, plen);
577                         continue;
578
579                 case 0x08:                      /* ^H  - backspace      */
580                 case 0x7F:                      /* DEL - backspace      */
581                         p = delete_char(p_buf, p, &col, &n, plen);
582                         continue;
583
584                 default:
585                         /*
586                          * Must be a normal character then
587                          */
588                         if (n < CONFIG_SYS_CBSIZE-2) {
589                                 if (c == '\t') {        /* expand TABs */
590 #ifdef CONFIG_AUTO_COMPLETE
591                                         /*
592                                          * if auto completion triggered just
593                                          * continue
594                                          */
595                                         *p = '\0';
596                                         if (cmd_auto_complete(prompt,
597                                                               console_buffer,
598                                                               &n, &col)) {
599                                                 p = p_buf + n;  /* reset */
600                                                 continue;
601                                         }
602 #endif
603                                         puts(tab_seq + (col & 07));
604                                         col += 8 - (col & 07);
605                                 } else {
606                                         char buf[2];
607
608                                         /*
609                                          * Echo input using puts() to force an
610                                          * LCD flush if we are using an LCD
611                                          */
612                                         ++col;
613                                         buf[0] = c;
614                                         buf[1] = '\0';
615                                         puts(buf);
616                                 }
617                                 *p++ = c;
618                                 ++n;
619                         } else {                        /* Buffer full */
620                                 putc('\a');
621                         }
622                 }
623         }
624 #ifdef CONFIG_CMDLINE_EDITING
625         }
626 #endif
627 }