]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cli_readline.c
Merge branch 'master' into cmdline_edit
[z180-stamp.git] / avr / cli_readline.c
CommitLineData
d684c216 1/*
35edb766
L
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
d684c216
L
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * Add to readline cmdline-editing by
8 * (C) Copyright 2005
9 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include "common.h"
d684c216
L
15#include <string.h>
16#include <stdio.h>
8506d791 17#include <ctype.h>
d684c216
L
18
19#include "config.h"
20#include "con-utils.h"
8591c65b 21#include "print-utils.h"
d684c216
L
22#include "command.h"
23#include "cli_readline.h"
24
8506d791
L
25
26
27/************************************************************************************************/
28/* TODO:
29 *
30 */
31
32#define ESC 0x1b
33
34#define KEY_TAB '\t' // TAB key
35#define KEY_CR '\r' // RETURN key
36#define KEY_BACKSPACE '\b' // Backspace key
37#define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
38
39#define KEY_DOWN 0x80 // Down arrow key
40#define KEY_UP 0x81 // Up arrow key
41#define KEY_LEFT 0x82 // Left arrow key
42#define KEY_RIGHT 0x83 // Right arrow key
43#define KEY_HOME 0x84 // Home key
44#define KEY_DC 0x85 // Delete character key
45#define KEY_IC 0x86 // Ins char/toggle ins mode key
46#define KEY_NPAGE 0x87 // Next-page key
47#define KEY_PPAGE 0x88 // Previous-page key
48#define KEY_END 0x89 // End key
49#define KEY_BTAB 0x8A // Back tab key
50#define KEY_F1 0x8B // Function key F1
51#define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
52
53
54struct fkey_tbl_s {
55 const FLASH char *sequence; /* ESC Sequence */
56 int code; /* Keycode */
57};
58
59//typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
60
61#define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
62
63
64
65static const FLASH struct fkey_tbl_s fkey_table[] = {
66
67FKEY_TBL_ITEM(B, KEY_DOWN), // Down arrow key
68FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key
69FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key
70FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key
71FKEY_TBL_ITEM(1~, KEY_HOME), // Home key
72FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key
73FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key
74FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key
75FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key
76FKEY_TBL_ITEM(4~, KEY_END), // End key
77FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key
78/* VT400: */
79FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
80FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
81FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
82FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
83FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
84/* Linux consoe */
85FKEY_TBL_ITEM([A, KEY_F(1)), // Function key F1
86FKEY_TBL_ITEM([B, KEY_F(2)), // Function key F2
87FKEY_TBL_ITEM([C, KEY_F(3)), // Function key F3
88FKEY_TBL_ITEM([D, KEY_F(4)), // Function key F4
89FKEY_TBL_ITEM([E, KEY_F(5)), // Function key F5
90
91FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
92FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
93FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
94FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
95FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
96FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
97FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
98{ NULL } /* Mark end of table */
99};
100
101
102
103typedef enum {
104 STATE_GROUND,
105 STATE_ESCAPE,
106 STATE_CSI_ENTRY
107} vtparse_state_t;
108
109#define CHB_SIZE 15
110
111static
112int vt_parse (void)
113{
114 static vtparse_state_t state = STATE_GROUND;
115 char buf[CHB_SIZE+1];
116 uint8_t param[2];
117 uint8_t i_buf;
118 uint8_t i_param;
119 int ch;
120
121
122 while (1) {
123 ch = my_getchar(1);
124// debug_getch(state, ch);
125
126 switch (state) {
127 case STATE_GROUND:
128 if (ch == ESC) {
129 state = STATE_ESCAPE;
130 continue;
131 }
132 if (ch == 0x7F) // BACKSPACE on VT200 sends DEL char
133 ch = KEY_BACKSPACE; // map it to '\b'
134 break;
135 case STATE_ESCAPE:
136 if (ch < 0)
137 continue;
138
139 if (ch == '[') {
140 state = STATE_CSI_ENTRY;
141 param[0] = param[1] = 0;
142 i_buf = 0;
143 i_param = 0;
144 continue;
145 }
146 state = STATE_GROUND;
147 break;
148 case STATE_CSI_ENTRY:
149 if (ch < 0)
150 continue;
151
152 buf[i_buf] = ch;
153 if (i_buf < CHB_SIZE)
154 i_buf++;
155 if (ch == ';') {
156 i_param++;
157 continue;
158 }
159 if (isdigit(ch)) {
160 if (i_param < 2)
161 param[i_param] = param[i_param] * 10 + ch - '0';
162 continue;
163 }
164 if (ch >= '@' && ch <= '~' && ch != '[') {
165 buf[i_buf] = '\0';
166 int_fast8_t i = 0;
167 while (fkey_table[i].sequence) {
168 if (! strcmp_P (buf, fkey_table[i].sequence)) {
169 ch = fkey_table[i].code;
170 break;
171 }
172 i++;
173 }
174 if (fkey_table[i].sequence == NULL) {
175 ch = '$'; /* KEY_ESCAPE; */
176 }
177 }
178 state = STATE_GROUND;
179 break;
180 }
181 break; /* while */
182 }
183
184 return ch;
185}
186
187/************************************************************************************************/
188
189
190
191
192
d684c216
L
193char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
194
8506d791 195#ifndef CONFIG_CMDLINE_EDITING
d684c216
L
196static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */
197static const FLASH char tab_seq[] = " "; /* used to expand TABs */
198
d684c216
L
199static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
200{
201 char *s;
202
203 if (*np == 0)
204 return p;
205
206 if (*(--p) == '\t') { /* will retype the whole line */
207 while (*colp > plen) {
208 my_puts_P(erase_seq);
209 (*colp)--;
210 }
211 for (s = buffer; s < p; ++s) {
212 if (*s == '\t') {
213 my_puts_P(tab_seq + ((*colp) & 07));
214 *colp += 8 - ((*colp) & 07);
215 } else {
216 ++(*colp);
217 putchar(*s);
218 }
219 }
220 } else {
221 my_puts_P(erase_seq);
222 (*colp)--;
223 }
224 (*np)--;
225
226 return p;
227}
228#endif /* CONFIG_CMDLINE_EDITING */
229
230
231#ifdef CONFIG_CMDLINE_EDITING
232
233/*
234 * cmdline-editing related codes from vivi.
235 * Author: Janghoon Lyu <nandy@mizi.com>
236 */
237
e1a50c19
L
238static void putnstr(char *str, int n)
239{
240 /* printf_P(PSTR("%.*s"), (int)n, str) */
8591c65b 241 while (n-- && *str)
e1a50c19
L
242 putchar(*str++);
243}
d684c216 244
e1a50c19
L
245
246#define CTL_CH(c) ((c) - 'a' + 1)
d684c216 247#define CTL_BACKSPACE ('\b')
e1a50c19
L
248#define DEL ((char)255)
249#define DEL7 ((char)127)
d684c216
L
250#define CREAD_HIST_CHAR ('!')
251
252#define getcmd_putch(ch) putchar(ch)
8506d791 253#define getcmd_getch() vt_parse()
d684c216
L
254#define getcmd_cbeep() getcmd_putch('\a')
255
e1a50c19
L
256#define HIST_MAX 5
257#define HIST_SIZE CONFIG_SYS_CBSIZE
d684c216
L
258
259static int hist_max;
260static int hist_add_idx;
261static int hist_cur = -1;
262static unsigned hist_num;
263
264static char *hist_list[HIST_MAX];
265static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
266
267#define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
268
269static void hist_init(void)
270{
271 int i;
272
273 hist_max = 0;
274 hist_add_idx = 0;
275 hist_cur = -1;
276 hist_num = 0;
277
278 for (i = 0; i < HIST_MAX; i++) {
279 hist_list[i] = hist_lines[i];
280 hist_list[i][0] = '\0';
281 }
282}
283
284static void cread_add_to_hist(char *line)
285{
286 strcpy(hist_list[hist_add_idx], line);
287
288 if (++hist_add_idx >= HIST_MAX)
289 hist_add_idx = 0;
290
291 if (hist_add_idx > hist_max)
292 hist_max = hist_add_idx;
293
294 hist_num++;
295}
296
297static char *hist_prev(void)
298{
299 char *ret;
300 int old_cur;
301
302 if (hist_cur < 0)
303 return NULL;
304
305 old_cur = hist_cur;
306 if (--hist_cur < 0)
307 hist_cur = hist_max;
308
309 if (hist_cur == hist_add_idx) {
310 hist_cur = old_cur;
311 ret = NULL;
312 } else {
313 ret = hist_list[hist_cur];
314 }
315
316 return ret;
317}
318
319static char *hist_next(void)
320{
321 char *ret;
322
323 if (hist_cur < 0)
324 return NULL;
325
326 if (hist_cur == hist_add_idx)
327 return NULL;
328
329 if (++hist_cur > hist_max)
330 hist_cur = 0;
331
332 if (hist_cur == hist_add_idx)
333 ret = "";
334 else
335 ret = hist_list[hist_cur];
336
337 return ret;
338}
339
d684c216 340
e1a50c19
L
341#define BEGINNING_OF_LINE() { \
342 while (num) { \
343 getcmd_putch(CTL_BACKSPACE); \
344 num--; \
345 } \
d684c216
L
346}
347
e1a50c19
L
348#define ERASE_TO_EOL() { \
349 if (num < eol_num) { \
350 /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \
351 print_blanks(eol_num - num); \
352 do { \
353 getcmd_putch(CTL_BACKSPACE); \
354 } while (--eol_num > num); \
355 } \
d684c216
L
356}
357
e1a50c19
L
358#define REFRESH_TO_EOL() { \
359 if (num < eol_num) { \
360 wlen = eol_num - num; \
361 putnstr(buf + num, wlen); \
362 num = eol_num; \
363 } \
d684c216
L
364}
365
8591c65b
L
366static void cread_add_char(char ichar, int insert, unsigned int *num,
367 unsigned int *eol_num, char *buf, unsigned int len)
d684c216 368{
8591c65b 369 unsigned int wlen;
d684c216
L
370
371 /* room ??? */
372 if (insert || *num == *eol_num) {
373 if (*eol_num > len - 1) {
374 getcmd_cbeep();
375 return;
376 }
377 (*eol_num)++;
378 }
379
380 if (insert) {
381 wlen = *eol_num - *num;
382 if (wlen > 1)
383 memmove(&buf[*num+1], &buf[*num], wlen-1);
384
385 buf[*num] = ichar;
386 putnstr(buf + *num, wlen);
387 (*num)++;
388 while (--wlen)
389 getcmd_putch(CTL_BACKSPACE);
390 } else {
391 /* echo the character */
392 wlen = 1;
393 buf[*num] = ichar;
394 putnstr(buf + *num, wlen);
395 (*num)++;
396 }
397}
398
399static void cread_add_str(char *str, int strsize, int insert,
8591c65b
L
400 unsigned int *num, unsigned int *eol_num,
401 char *buf, unsigned int len)
d684c216
L
402{
403 while (strsize--) {
404 cread_add_char(*str, insert, num, eol_num, buf, len);
405 str++;
406 }
407}
408
409static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *len)
410{
8591c65b
L
411 unsigned int num = 0;
412 unsigned int eol_num = 0;
413 unsigned int wlen;
8506d791 414 int ichar;
d684c216 415 int insert = 1;
d684c216
L
416 int init_len = strlen(buf);
417
418 (void) prompt;
419
420 if (init_len)
421 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
422
423 while (1) {
424 ichar = getcmd_getch();
425
426 if ((ichar == '\n') || (ichar == '\r')) {
427 putchar('\n');
428 break;
429 }
430
d684c216
L
431
432 switch (ichar) {
d684c216 433
8506d791 434 case KEY_HOME:
d684c216
L
435 case CTL_CH('a'):
436 BEGINNING_OF_LINE();
437 break;
438 case CTL_CH('c'): /* ^C - break */
439 *buf = '\0'; /* discard input */
440 return -1;
8506d791 441 case KEY_RIGHT:
d684c216
L
442 case CTL_CH('f'):
443 if (num < eol_num) {
444 getcmd_putch(buf[num]);
445 num++;
446 }
447 break;
8506d791 448 case KEY_LEFT:
d684c216
L
449 case CTL_CH('b'):
450 if (num) {
451 getcmd_putch(CTL_BACKSPACE);
452 num--;
453 }
454 break;
8506d791 455 case KEY_DC:
d684c216
L
456 case CTL_CH('d'):
457 if (num < eol_num) {
458 wlen = eol_num - num - 1;
459 if (wlen) {
460 memmove(&buf[num], &buf[num+1], wlen);
461 putnstr(buf + num, wlen);
462 }
463
464 getcmd_putch(' ');
465 do {
466 getcmd_putch(CTL_BACKSPACE);
467 } while (wlen--);
468 eol_num--;
469 }
470 break;
471 case CTL_CH('k'):
472 ERASE_TO_EOL();
473 break;
474 case CTL_CH('e'):
475 REFRESH_TO_EOL();
476 break;
8506d791 477 case KEY_IC:
d684c216
L
478 case CTL_CH('o'):
479 insert = !insert;
480 break;
481 case CTL_CH('x'):
482 case CTL_CH('u'):
483 BEGINNING_OF_LINE();
484 ERASE_TO_EOL();
485 break;
486 case DEL:
487 case DEL7:
488 case 8:
489 if (num) {
490 wlen = eol_num - num;
491 num--;
492 memmove(&buf[num], &buf[num+1], wlen);
493 getcmd_putch(CTL_BACKSPACE);
494 putnstr(buf + num, wlen);
495 getcmd_putch(' ');
496 do {
497 getcmd_putch(CTL_BACKSPACE);
498 } while (wlen--);
499 eol_num--;
500 }
501 break;
8506d791 502 case KEY_UP:
d684c216 503 case CTL_CH('p'):
8506d791 504 case KEY_DOWN:
d684c216
L
505 case CTL_CH('n'):
506 {
507 char *hline;
508
8506d791 509 if (ichar == CTL_CH('p') || ichar == KEY_UP)
d684c216
L
510 hline = hist_prev();
511 else
512 hline = hist_next();
513
514 if (!hline) {
515 getcmd_cbeep();
516 continue;
517 }
518
519 /* nuke the current line */
520 /* first, go home */
521 BEGINNING_OF_LINE();
522
523 /* erase to end of line */
524 ERASE_TO_EOL();
525
526 /* copy new line into place and display */
527 strcpy(buf, hline);
528 eol_num = strlen(buf);
529 REFRESH_TO_EOL();
530 continue;
531 }
532#ifdef CONFIG_AUTO_COMPLETE
533 case '\t': {
534 int num2, col;
535
536 /* do not autocomplete when in the middle */
537 if (num < eol_num) {
538 getcmd_cbeep();
539 break;
540 }
541
542 buf[num] = '\0';
543 col = strlen_P(prompt) + eol_num;
544 num2 = num;
545 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
546 col = num2 - num;
547 num += col;
548 eol_num += col;
549 }
550 break;
551 }
552#endif
553 default:
8506d791
L
554 if (isprint(ichar))
555 cread_add_char(ichar, insert, &num, &eol_num, buf,
556 *len);
d684c216
L
557 break;
558 }
559 }
560 *len = eol_num;
561 buf[eol_num] = '\0'; /* lose the newline */
562
8506d791 563 if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */)
d684c216
L
564 cread_add_to_hist(buf);
565 hist_cur = hist_add_idx;
566
567 return 0;
568}
569
570#endif /* CONFIG_CMDLINE_EDITING */
571
572/****************************************************************************/
573
574static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
575{
576 char *p = buffer;
577#ifdef CONFIG_CMDLINE_EDITING
578 unsigned int len = CONFIG_SYS_CBSIZE;
579 int rc;
580 static int initted;
581
582 if (!initted) {
583 hist_init();
584 initted = 1;
585 }
586
587 if (prompt)
588 my_puts_P(prompt);
589
590 rc = cread_line(prompt, p, &len);
591 return rc < 0 ? rc : (int) len;
592
593#else /* CONFIG_CMDLINE_EDITING */
594 char *p_buf = p;
595 int n = 0; /* buffer index */
596 int plen = 0; /* prompt length */
597 int col; /* output column cnt */
598 char c;
599
600 /* print prompt */
601 if (prompt) {
602 plen = strlen_P(prompt);
603 my_puts_P(prompt);
604 }
605 col = plen;
606
607 for (;;) {
608
424b184a 609 c = my_getchar(1);
d684c216
L
610
611 /*
612 * Special character handling
613 */
614 switch (c) {
615 case '\r': /* Enter */
616 case '\n':
617 *p = '\0';
618 my_puts_P(PSTR("\r\n"));
619 return p - p_buf;
620
621 case '\0': /* nul */
622 continue;
623
624 case 0x03: /* ^C - break */
625 p_buf[0] = '\0'; /* discard input */
626 return -1;
627
628 case 0x15: /* ^U - erase line */
629 while (col > plen) {
630 my_puts_P(erase_seq);
631 --col;
632 }
633 p = p_buf;
634 n = 0;
635 continue;
636
637 case 0x17: /* ^W - erase word */
638 p = delete_char(p_buf, p, &col, &n, plen);
639 while ((n > 0) && (*p != ' '))
640 p = delete_char(p_buf, p, &col, &n, plen);
641 continue;
642
643 case 0x08: /* ^H - backspace */
644 case 0x7F: /* DEL - backspace */
645 p = delete_char(p_buf, p, &col, &n, plen);
646 continue;
647
648 default:
649 /*
650 * Must be a normal character then
651 */
652 if (n < CONFIG_SYS_CBSIZE-2) {
653 if (c == '\t') { /* expand TABs */
654#ifdef CONFIG_AUTO_COMPLETE
655 /*
656 * if auto completion triggered just
657 * continue
658 */
659 *p = '\0';
660 if (cmd_auto_complete(prompt,
661 console_buffer,
662 &n, &col)) {
663 p = p_buf + n; /* reset */
664 continue;
665 }
666#endif
667 my_puts_P(tab_seq + (col & 07));
668 col += 8 - (col & 07);
669 } else {
d684c216 670 ++col;
cb52e1e0 671 putchar(c);
d684c216
L
672 }
673 *p++ = c;
674 ++n;
675 } else { /* Buffer full */
676 putchar('\a');
677 }
678 }
679 }
680#endif /* CONFIG_CMDLINE_EDITING */
681}
682
683int cli_readline(const FLASH char *const prompt)
684{
685 /*
686 * If console_buffer isn't 0-length the user will be prompted to modify
687 * it instead of entering it from scratch as desired.
688 */
689 console_buffer[0] = '\0';
690
691 return cli_readline_into_buffer(prompt, console_buffer);
692}