]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
2 * (C) Copyright 2014-2016 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
14 #include "cli_readline.h"
22 #include "con-utils.h"
23 #include "print-utils.h"
28 #define DEBUG_READLINE 0 /* set to 1 to debug */
30 #define debug_readline(fmt, args...) \
31 debug_cond(DEBUG_READLINE, fmt, ##args)
35 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
37 #define CTL_CH(c) ((c) - 'a' + 1)
38 #define CTL_BACKSPACE ('\b')
39 #define DEL ((char)255)
40 #define DEL7 ((char)127)
43 /************************************************************************************************/
50 #define KEY_TAB '\t' // TAB key
51 #define KEY_CR '\r' // RETURN key
52 #define KEY_BACKSPACE '\b' // Backspace key
53 #define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
55 #define KEY_DOWN 0x80 // Down arrow key
56 #define KEY_UP 0x81 // Up arrow key
57 #define KEY_LEFT 0x82 // Left arrow key
58 #define KEY_RIGHT 0x83 // Right arrow key
59 #define KEY_HOME 0x84 // Home key
60 #define KEY_DC 0x85 // Delete character key
61 #define KEY_IC 0x86 // Ins char/toggle ins mode key
62 #define KEY_NPAGE 0x87 // Next-page key
63 #define KEY_PPAGE 0x88 // Previous-page key
64 #define KEY_END 0x89 // End key
65 #define KEY_BTAB 0x8A // Back tab key
66 #define KEY_F1 0x8B // Function key F1
67 #define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
71 const FLASH
char *sequence
; /* ESC Sequence */
72 int code
; /* Keycode */
75 //typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
77 #define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
81 static const FLASH
struct fkey_tbl_s fkey_table
[] = {
83 FKEY_TBL_ITEM(B
, KEY_DOWN
), // Down arrow key
84 FKEY_TBL_ITEM(A
, KEY_UP
), // Up arrow key
85 FKEY_TBL_ITEM(D
, KEY_LEFT
), // Left arrow key
86 FKEY_TBL_ITEM(C
, KEY_RIGHT
), // Right arrow key
87 FKEY_TBL_ITEM(1~, KEY_HOME
), // Home key
88 FKEY_TBL_ITEM(3~, KEY_DC
), // Delete character key
89 FKEY_TBL_ITEM(2~, KEY_IC
), // Ins char/toggle ins mode key
90 FKEY_TBL_ITEM(6~, KEY_NPAGE
), // Next-page key
91 FKEY_TBL_ITEM(5~, KEY_PPAGE
), // Previous-page key
92 FKEY_TBL_ITEM(4~, KEY_END
), // End key
93 FKEY_TBL_ITEM(Z
, KEY_BTAB
), // Back tab key
95 FKEY_TBL_ITEM(H
, KEY_HOME
), // Home key
96 FKEY_TBL_ITEM(F
, KEY_END
), // End key
98 FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
99 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
100 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
101 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
102 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
104 FKEY_TBL_ITEM([A
, KEY_F(1)), // Function key F1
105 FKEY_TBL_ITEM([B
, KEY_F(2)), // Function key F2
106 FKEY_TBL_ITEM([C
, KEY_F(3)), // Function key F3
107 FKEY_TBL_ITEM([D
, KEY_F(4)), // Function key F4
108 FKEY_TBL_ITEM([E
, KEY_F(5)), // Function key F5
110 FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
111 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
112 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
113 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
114 FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
115 FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
116 FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
117 { NULL
} /* Mark end of table */
134 static vtparse_state_t state
= STATE_GROUND
;
135 char buf
[CHB_SIZE
+1];
144 // debug_getch(state, ch);
149 state
= STATE_ESCAPE
;
152 if (ch
== 0x7F) // BACKSPACE on VT200 sends DEL char
153 ch
= KEY_BACKSPACE
; // map it to '\b'
160 state
= STATE_CSI_ENTRY
;
161 param
[0] = param
[1] = 0;
170 state
= STATE_GROUND
;
177 state
= STATE_GROUND
;
179 case STATE_CSI_ENTRY
:
184 if (i_buf
< CHB_SIZE
)
192 param
[i_param
] = param
[i_param
] * 10 + ch
- '0';
195 if (ch
>= '@' && ch
<= '~' && ch
!= '[') {
198 while (fkey_table
[i
].sequence
) {
199 if (! strcmp_P (buf
, fkey_table
[i
].sequence
)) {
200 ch
= fkey_table
[i
].code
;
205 if (fkey_table
[i
].sequence
== NULL
) {
206 ch
= '$'; /* KEY_ESCAPE; */
209 state
= STATE_GROUND
;
218 /************************************************************************************************/
221 * cmdline-editing related codes from vivi.
222 * Author: Janghoon Lyu <nandy@mizi.com>
226 char histbuf
[CONFIG_SYS_HISTSIZE
+1];
227 #define HISTBUFE (histbuf+CONFIG_SYS_HISTSIZE)
229 static char *hist_head
;
230 static char *hist_cur
;
232 static void hist_reset(void)
234 if (hist_head
== NULL
)
235 hist_head
= HISTBUFE
;
236 hist_cur
= hist_head
;
239 static char *hist_entry_next(char *p
)
247 static char *hist_entry_prev(char *p
)
253 while (p
!= hist_head
&& *(p
-1) != 0)
259 static char *hist_search_entry(char *line
)
263 while (*p
&& strcmp(p
, line
))
264 p
= hist_entry_next(p
);
265 return *p
? p
: NULL
;
268 static char *hist_delete(size_t amount
)
272 p
= HISTBUFE
- amount
;
276 while (p
> hist_head
&& *(p
-1))
280 hist_head
= HISTBUFE
;
282 size_t shift
= HISTBUFE
- p
;
283 size_t len
= p
- hist_head
;
284 hist_head
= memmove(hist_head
+ shift
, hist_head
, len
);
289 static char *hist_delete_entry(char *entry
)
291 size_t shift
= strlen(entry
) + 1;
292 size_t len
= entry
- hist_head
;
294 hist_head
= memmove(hist_head
+ shift
, hist_head
, len
);
300 static char *hist_add_entry(char *line
)
307 char *q
= p
- strlen(line
) - 1;
311 strlcpy(q
, line
, p
- q
);
318 static uint_fast8_t hist_get_count(void)
330 static int hist_get_size(void)
332 return HISTBUFE
- hist_head
;
335 static char *cread_add_to_hist(char *line
)
339 p
= hist_search_entry(line
);
341 hist_delete_entry(p
);
343 size_t free
= hist_head
- histbuf
;
344 if (free
< strlen(line
) + 1)
345 hist_delete(strlen(line
) + 1 - free
);
347 hist_add_entry(line
);
352 static char *hist_prev(void)
358 hist_cur
= hist_entry_next(p
);
363 static char *hist_next(void)
370 p
= hist_entry_prev(p
);
373 return p
== hist_head
? "" : hist_entry_prev(p
);
376 static char *hist_search_backward(char* buf
, uint8_t num
)
383 while (*p
&& strncmp(p
, buf
, num
))
384 p
= hist_entry_next(p
);
386 if(!strncmp(p
, buf
, num
)) {
387 hist_cur
= hist_entry_next(p
);
394 static char *hist_search_forward (char* buf
, uint8_t num
)
398 if(p
!= hist_head
&& (p
= hist_entry_prev(p
)) != NULL
) {
400 p
= hist_entry_prev(p
);
401 } while (p
&& strncmp(p
, buf
, num
) != 0);
403 //if(!strncmp(p, buf, num)) {
405 hist_cur
= hist_entry_next(p
);
413 static void putnstr(char *str
, int n
)
415 /* printf_P(PSTR("%.*s"), (int)n, str) */
420 static void getcmd_putch(int ch
) { putchar(ch
);}
421 static int getcmd_getch(void) { return vt_parse();}
422 static void getcmd_cbeep(void) { getcmd_putch('\a');}
424 static void beginning_of_line(uint8_t *num
)
427 getcmd_putch(CTL_BACKSPACE
);
432 static void erase_to_eol(uint_fast8_t *num
, uint_fast8_t *eol_num
)
434 if (*num
< *eol_num
) {
435 /* printf_P(PSTR("%*S"), (int)(*eol_num - *num), PSTR("")); */
436 print_blanks(*eol_num
- *num
);
438 getcmd_putch(CTL_BACKSPACE
);
439 } while (--(*eol_num
) > *num
);
443 static void refresh_to_eol(char *buf
, uint_fast8_t *num
, uint_fast8_t *eol_num
)
445 if (*num
< *eol_num
) {
446 uint_fast8_t wlen
= *eol_num
- *num
;
447 putnstr(buf
+ *num
, wlen
);
452 static void cread_add_char(char ichar
, bool insert
, uint_fast8_t *num
,
453 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
458 if (insert
|| *num
== *eol_num
) {
459 if (*eol_num
> len
- 1) {
467 wlen
= *eol_num
- *num
;
469 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
472 putnstr(buf
+ *num
, wlen
);
475 getcmd_putch(CTL_BACKSPACE
);
477 /* echo the character */
479 putnstr(buf
+ *num
, 1);
484 static void cread_add_str(char *str
, bool insert
, uint_fast8_t *num
,
485 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
489 while ((c
= *str
++) != '\0')
490 cread_add_char(c
, insert
, num
, eol_num
, buf
, len
);
493 static int cread_line(const FLASH
char *const prompt
, char *buf
,
494 uint_fast8_t len
, bool enable_history
)
496 uint_fast8_t num
= 0;
497 uint_fast8_t eol_num
= 0;
503 cread_add_str(buf
, 1, &num
, &eol_num
, buf
, len
);
508 int ichar
= getcmd_getch();
510 if ((ichar
== '\n') || (ichar
== '\r')) {
520 beginning_of_line(&num
);
522 case CTL_CH('c'): /* ^C - break */
524 *buf
= '\0'; /* discard input */
527 case CTL_CH('f'): /* forward-char */
529 getcmd_putch(buf
[num
]);
534 case CTL_CH('b'): /* backward-char */
536 getcmd_putch(CTL_BACKSPACE
);
541 case CTL_CH('d'): /* delete-char */
543 uint_fast8_t wlen
= eol_num
- num
- 1;
545 memmove(&buf
[num
], &buf
[num
+1], wlen
);
546 putnstr(buf
+ num
, wlen
);
551 getcmd_putch(CTL_BACKSPACE
);
556 case CTL_CH('k'): /* kill-line */
557 erase_to_eol(&num
, &eol_num
);
561 refresh_to_eol(buf
, &num
, &eol_num
);
568 case CTL_CH('u'): /* kill-whole-line */
569 beginning_of_line(&num
);
570 erase_to_eol(&num
, &eol_num
);
574 case 8: /* backward-delete-char */
576 uint_fast8_t wlen
= eol_num
- --num
;
578 memmove(&buf
[num
], &buf
[num
+1], wlen
);
579 getcmd_putch(CTL_BACKSPACE
);
580 putnstr(buf
+ num
, wlen
);
582 getcmd_putch(CTL_BACKSPACE
);
588 case CTL_CH('p'): /* previous-history */
590 case CTL_CH('n'): /* next-history */
591 if (enable_history
) {
594 if (ichar
== CTL_CH('p') || ichar
== KEY_UP
)
601 beginning_of_line(&num
);
602 /* overwrite current line */
603 cread_add_str(hline
, 0, &num
, &eol_num
, buf
, len
);
604 /* erase to end of line */
605 erase_to_eol(&num
, &eol_num
);
614 case KEY_PPAGE
: /* history-search-backward */
615 case KEY_NPAGE
: /* history-search-forward */
616 if (enable_history
) {
618 if (ichar
== KEY_PPAGE
)
619 hline
= hist_search_backward(buf
, num
);
621 hline
= hist_search_forward(buf
, num
);
624 uint_fast8_t num2
= num
;
625 /* overwrite current line from cursor position */
626 cread_add_str(hline
+num
, 0, &num2
, &eol_num
, buf
, len
);
627 /* erase to end of line */
628 erase_to_eol(&num2
, &eol_num
);
631 getcmd_putch(CTL_BACKSPACE
);
639 #ifdef CONFIG_AUTO_COMPLETE
643 /* do not autocomplete when in the middle */
650 col
= strlen_P(prompt
) + eol_num
;
652 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
662 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, len
);
666 while (eol_num
&& buf
[eol_num
-1] == ' ')
667 --eol_num
; /* remove trailing blanks */
668 buf
[eol_num
] = '\0'; /* lose the newline */
671 while (buf
[i
] == ' ')
672 ++i
; /* remove leading blanks */
675 memmove(buf
, buf
+i
, eol_num
+1);
678 debug_readline("### hist_head: %p, hist_cur: %p\n", hist_head
, hist_cur
);
679 if (enable_history
&& buf
[0]) {
680 cread_add_to_hist(buf
);
681 debug_readline("### hist_head: %p, hist_cur: %p, hist_size: %3d, hist_count: %2d\n",
682 hist_head
, hist_cur
, hist_get_size(), hist_get_count());
687 /****************************************************************************/
689 int cli_readline(const FLASH
char *const prompt
, bool enable_history
)
692 * If console_buffer isn't 0-length the user will be prompted to modify
693 * it instead of entering it from scratch as desired.
695 console_buffer
[0] = '\0';
700 return cread_line(prompt
, console_buffer
, CONFIG_SYS_CBSIZE
, enable_history
);