]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * Add to readline cmdline-editing by
9 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
11 * SPDX-License-Identifier: GPL-2.0+
22 #include "con-utils.h"
23 #include "print-utils.h"
25 #include "cli_readline.h"
29 /************************************************************************************************/
36 #define KEY_TAB '\t' // TAB key
37 #define KEY_CR '\r' // RETURN key
38 #define KEY_BACKSPACE '\b' // Backspace key
39 #define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
41 #define KEY_DOWN 0x80 // Down arrow key
42 #define KEY_UP 0x81 // Up arrow key
43 #define KEY_LEFT 0x82 // Left arrow key
44 #define KEY_RIGHT 0x83 // Right arrow key
45 #define KEY_HOME 0x84 // Home key
46 #define KEY_DC 0x85 // Delete character key
47 #define KEY_IC 0x86 // Ins char/toggle ins mode key
48 #define KEY_NPAGE 0x87 // Next-page key
49 #define KEY_PPAGE 0x88 // Previous-page key
50 #define KEY_END 0x89 // End key
51 #define KEY_BTAB 0x8A // Back tab key
52 #define KEY_F1 0x8B // Function key F1
53 #define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
57 const FLASH
char *sequence
; /* ESC Sequence */
58 int code
; /* Keycode */
61 //typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
63 #define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
67 static const FLASH
struct fkey_tbl_s fkey_table
[] = {
69 FKEY_TBL_ITEM(B
, KEY_DOWN
), // Down arrow key
70 FKEY_TBL_ITEM(A
, KEY_UP
), // Up arrow key
71 FKEY_TBL_ITEM(D
, KEY_LEFT
), // Left arrow key
72 FKEY_TBL_ITEM(C
, KEY_RIGHT
), // Right arrow key
73 FKEY_TBL_ITEM(1~, KEY_HOME
), // Home key
74 FKEY_TBL_ITEM(3~, KEY_DC
), // Delete character key
75 FKEY_TBL_ITEM(2~, KEY_IC
), // Ins char/toggle ins mode key
76 FKEY_TBL_ITEM(6~, KEY_NPAGE
), // Next-page key
77 FKEY_TBL_ITEM(5~, KEY_PPAGE
), // Previous-page key
78 FKEY_TBL_ITEM(4~, KEY_END
), // End key
79 FKEY_TBL_ITEM(Z
, KEY_BTAB
), // Back tab key
81 FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
82 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
83 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
84 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
85 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
87 FKEY_TBL_ITEM([A
, KEY_F(1)), // Function key F1
88 FKEY_TBL_ITEM([B
, KEY_F(2)), // Function key F2
89 FKEY_TBL_ITEM([C
, KEY_F(3)), // Function key F3
90 FKEY_TBL_ITEM([D
, KEY_F(4)), // Function key F4
91 FKEY_TBL_ITEM([E
, KEY_F(5)), // Function key F5
93 FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
94 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
95 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
96 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
97 FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
98 FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
99 FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
100 { NULL
} /* Mark end of table */
116 static vtparse_state_t state
= STATE_GROUND
;
117 char buf
[CHB_SIZE
+1];
126 // debug_getch(state, ch);
131 state
= STATE_ESCAPE
;
134 if (ch
== 0x7F) // BACKSPACE on VT200 sends DEL char
135 ch
= KEY_BACKSPACE
; // map it to '\b'
142 state
= STATE_CSI_ENTRY
;
143 param
[0] = param
[1] = 0;
148 state
= STATE_GROUND
;
150 case STATE_CSI_ENTRY
:
155 if (i_buf
< CHB_SIZE
)
163 param
[i_param
] = param
[i_param
] * 10 + ch
- '0';
166 if (ch
>= '@' && ch
<= '~' && ch
!= '[') {
169 while (fkey_table
[i
].sequence
) {
170 if (! strcmp_P (buf
, fkey_table
[i
].sequence
)) {
171 ch
= fkey_table
[i
].code
;
176 if (fkey_table
[i
].sequence
== NULL
) {
177 ch
= '$'; /* KEY_ESCAPE; */
180 state
= STATE_GROUND
;
189 /************************************************************************************************/
192 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
194 #ifndef CONFIG_CMDLINE_EDITING
195 static const FLASH
char erase_seq
[] = "\b \b"; /* erase sequence */
196 static const FLASH
char tab_seq
[] = " "; /* used to expand TABs */
198 static char *delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
205 if (*(--p
) == '\t') { /* will retype the whole line */
206 while (*colp
> plen
) {
207 my_puts_P(erase_seq
);
210 for (s
= buffer
; s
< p
; ++s
) {
212 my_puts_P(tab_seq
+ ((*colp
) & 07));
213 *colp
+= 8 - ((*colp
) & 07);
220 my_puts_P(erase_seq
);
227 #endif /* CONFIG_CMDLINE_EDITING */
230 #ifdef CONFIG_CMDLINE_EDITING
233 * cmdline-editing related codes from vivi.
234 * Author: Janghoon Lyu <nandy@mizi.com>
237 static void putnstr(char *str
, int n
)
239 /* printf_P(PSTR("%.*s"), (int)n, str) */
245 #define CTL_CH(c) ((c) - 'a' + 1)
246 #define CTL_BACKSPACE ('\b')
247 #define DEL ((char)255)
248 #define DEL7 ((char)127)
250 #define getcmd_putch(ch) putchar(ch)
251 #define getcmd_getch() vt_parse()
252 #define getcmd_cbeep() getcmd_putch('\a')
256 static int_fast8_t hist_max
;
257 static int_fast8_t hist_add_idx
;
258 static int_fast8_t hist_cur
= -1;
260 static char *hist_list
[HIST_MAX
];
262 static void hist_init(void)
270 for (i
= 0; i
< HIST_MAX
; i
++) {
275 static void cread_add_to_hist(char *line
)
278 int_fast8_t last
= hist_add_idx
;
282 if (!strcmp(line
, hist_list
[last
]))
286 char *p
= strdup(line
);
288 free(hist_list
[hist_add_idx
]);
289 hist_list
[hist_add_idx
] = p
;
291 if (++hist_add_idx
>= HIST_MAX
)
294 if (hist_add_idx
> hist_max
)
295 hist_max
= hist_add_idx
;
299 static char *hist_prev(void)
311 if (hist_cur
== hist_add_idx
) {
315 ret
= hist_list
[hist_cur
];
321 static char *hist_next(void)
328 if (hist_cur
== hist_add_idx
)
331 if (++hist_cur
> hist_max
)
334 if (hist_cur
== hist_add_idx
)
337 ret
= hist_list
[hist_cur
];
343 #define BEGINNING_OF_LINE() { \
345 getcmd_putch(CTL_BACKSPACE); \
350 #define ERASE_TO_EOL() { \
351 if (num < eol_num) { \
352 /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \
353 print_blanks(eol_num - num); \
355 getcmd_putch(CTL_BACKSPACE); \
356 } while (--eol_num > num); \
360 #define REFRESH_TO_EOL() { \
361 if (num < eol_num) { \
362 wlen = eol_num - num; \
363 putnstr(buf + num, wlen); \
368 static void cread_add_char(char ichar
, bool insert
, uint_fast8_t *num
,
369 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
374 if (insert
|| *num
== *eol_num
) {
375 if (*eol_num
> len
- 1) {
383 wlen
= *eol_num
- *num
;
385 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
388 putnstr(buf
+ *num
, wlen
);
391 getcmd_putch(CTL_BACKSPACE
);
393 /* echo the character */
396 putnstr(buf
+ *num
, wlen
);
401 static void cread_add_str(char *str
, int strsize
, bool insert
,
402 uint_fast8_t *num
, uint_fast8_t *eol_num
,
403 char *buf
, uint_fast8_t len
)
406 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
411 static int cread_line(const FLASH
char *const prompt
, char *buf
, uint_fast8_t *len
)
413 uint_fast8_t num
= 0;
414 uint_fast8_t eol_num
= 0;
418 int init_len
= strlen(buf
);
423 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
426 ichar
= getcmd_getch();
428 if ((ichar
== '\n') || (ichar
== '\r')) {
440 case CTL_CH('c'): /* ^C - break */
441 *buf
= '\0'; /* discard input */
444 case CTL_CH('f'): /* forward-char */
446 getcmd_putch(buf
[num
]);
451 case CTL_CH('b'): /* backward-char */
453 getcmd_putch(CTL_BACKSPACE
);
458 case CTL_CH('d'): /* delete-char */
460 wlen
= eol_num
- num
- 1;
462 memmove(&buf
[num
], &buf
[num
+1], wlen
);
463 putnstr(buf
+ num
, wlen
);
468 getcmd_putch(CTL_BACKSPACE
);
473 case CTL_CH('k'): /* kill-line */
485 case CTL_CH('u'): /* kill-whole-line */
491 case 8: /* backward-delete-char */
493 wlen
= eol_num
- num
;
495 memmove(&buf
[num
], &buf
[num
+1], wlen
);
496 getcmd_putch(CTL_BACKSPACE
);
497 putnstr(buf
+ num
, wlen
);
500 getcmd_putch(CTL_BACKSPACE
);
506 case CTL_CH('p'): /* previous-history */
508 case CTL_CH('n'): /* next-history */
512 if (ichar
== CTL_CH('p') || ichar
== KEY_UP
)
522 /* nuke the current line */
526 /* erase to end of line */
529 /* copy new line into place and display */
531 eol_num
= strlen(buf
);
535 #ifdef CONFIG_AUTO_COMPLETE
539 /* do not autocomplete when in the middle */
546 col
= strlen_P(prompt
) + eol_num
;
548 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
558 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
564 buf
[eol_num
] = '\0'; /* lose the newline */
566 if (buf
[0] /* && buf[0] != CREAD_HIST_CHAR */)
567 cread_add_to_hist(buf
);
568 hist_cur
= hist_add_idx
;
573 #endif /* CONFIG_CMDLINE_EDITING */
575 /****************************************************************************/
577 static int cli_readline_into_buffer(const FLASH
char *const prompt
, char *buffer
)
580 #ifdef CONFIG_CMDLINE_EDITING
581 uint_fast8_t len
= CONFIG_SYS_CBSIZE
;
593 rc
= cread_line(prompt
, p
, &len
);
594 return rc
< 0 ? rc
: (int) len
;
596 #else /* CONFIG_CMDLINE_EDITING */
598 int n
= 0; /* buffer index */
599 int plen
= 0; /* prompt length */
600 int col
; /* output column cnt */
605 plen
= strlen_P(prompt
);
615 * Special character handling
618 case '\r': /* Enter */
621 my_puts_P(PSTR("\r\n"));
627 case 0x03: /* ^C - break */
628 p_buf
[0] = '\0'; /* discard input */
631 case 0x15: /* ^U - erase line */
633 my_puts_P(erase_seq
);
640 case 0x17: /* ^W - erase word */
641 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
642 while ((n
> 0) && (*p
!= ' '))
643 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
646 case 0x08: /* ^H - backspace */
647 case 0x7F: /* DEL - backspace */
648 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
653 * Must be a normal character then
655 if (n
< CONFIG_SYS_CBSIZE
-2) {
656 if (c
== '\t') { /* expand TABs */
657 #ifdef CONFIG_AUTO_COMPLETE
659 * if auto completion triggered just
663 if (cmd_auto_complete(prompt
,
666 p
= p_buf
+ n
; /* reset */
670 my_puts_P(tab_seq
+ (col
& 07));
671 col
+= 8 - (col
& 07);
678 } else { /* Buffer full */
683 #endif /* CONFIG_CMDLINE_EDITING */
686 int cli_readline(const FLASH
char *const prompt
)
689 * If console_buffer isn't 0-length the user will be prompted to modify
690 * it instead of entering it from scratch as desired.
692 console_buffer
[0] = '\0';
694 return cli_readline_into_buffer(prompt
, console_buffer
);