]>
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"
18 #include "con-utils.h"
19 #include "print-utils.h"
24 #define DEBUG_READLINE 0 /* set to 1 to debug */
26 #define debug_readline(fmt, args...) \
27 debug_cond(DEBUG_READLINE, fmt, ##args)
31 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
33 #define CTL_CH(c) ((c) - 'a' + 1)
34 #define CTL_BACKSPACE ('\b')
35 #define DEL ((char)255)
36 #define DEL7 ((char)127)
39 /************************************************************************************************/
46 #define KEY_TAB '\t' // TAB key
47 #define KEY_CR '\r' // RETURN key
48 #define KEY_BACKSPACE '\b' // Backspace key
49 #define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
51 #define KEY_DOWN 0x80 // Down arrow key
52 #define KEY_UP 0x81 // Up arrow key
53 #define KEY_LEFT 0x82 // Left arrow key
54 #define KEY_RIGHT 0x83 // Right arrow key
55 #define KEY_HOME 0x84 // Home key
56 #define KEY_DC 0x85 // Delete character key
57 #define KEY_IC 0x86 // Ins char/toggle ins mode key
58 #define KEY_NPAGE 0x87 // Next-page key
59 #define KEY_PPAGE 0x88 // Previous-page key
60 #define KEY_END 0x89 // End key
61 #define KEY_BTAB 0x8A // Back tab key
62 #define KEY_F1 0x8B // Function key F1
63 #define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
67 const FLASH
char *sequence
; /* ESC Sequence */
68 int code
; /* Keycode */
71 //typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
73 #define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
77 static const FLASH
struct fkey_tbl_s fkey_table
[] = {
79 FKEY_TBL_ITEM(B
, KEY_DOWN
), // Down arrow key
80 FKEY_TBL_ITEM(A
, KEY_UP
), // Up arrow key
81 FKEY_TBL_ITEM(D
, KEY_LEFT
), // Left arrow key
82 FKEY_TBL_ITEM(C
, KEY_RIGHT
), // Right arrow key
83 FKEY_TBL_ITEM(1~, KEY_HOME
), // Home key
84 FKEY_TBL_ITEM(3~, KEY_DC
), // Delete character key
85 FKEY_TBL_ITEM(2~, KEY_IC
), // Ins char/toggle ins mode key
86 FKEY_TBL_ITEM(6~, KEY_NPAGE
), // Next-page key
87 FKEY_TBL_ITEM(5~, KEY_PPAGE
), // Previous-page key
88 FKEY_TBL_ITEM(4~, KEY_END
), // End key
89 FKEY_TBL_ITEM(Z
, KEY_BTAB
), // Back tab key
91 FKEY_TBL_ITEM(H
, KEY_HOME
), // Home key
92 FKEY_TBL_ITEM(F
, KEY_END
), // End key
94 FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
95 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
96 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
97 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
98 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
100 FKEY_TBL_ITEM([A
, KEY_F(1)), // Function key F1
101 FKEY_TBL_ITEM([B
, KEY_F(2)), // Function key F2
102 FKEY_TBL_ITEM([C
, KEY_F(3)), // Function key F3
103 FKEY_TBL_ITEM([D
, KEY_F(4)), // Function key F4
104 FKEY_TBL_ITEM([E
, KEY_F(5)), // Function key F5
106 FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
107 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
108 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
109 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
110 FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
111 FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
112 FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
113 { NULL
} /* Mark end of table */
130 static vtparse_state_t state
= STATE_GROUND
;
131 char buf
[CHB_SIZE
+1];
140 // debug_getch(state, ch);
145 state
= STATE_ESCAPE
;
148 if (ch
== 0x7F) // BACKSPACE on VT200 sends DEL char
149 ch
= KEY_BACKSPACE
; // map it to '\b'
156 state
= STATE_CSI_ENTRY
;
157 param
[0] = param
[1] = 0;
166 state
= STATE_GROUND
;
173 state
= STATE_GROUND
;
175 case STATE_CSI_ENTRY
:
180 if (i_buf
< CHB_SIZE
)
188 param
[i_param
] = param
[i_param
] * 10 + ch
- '0';
191 if (ch
>= '@' && ch
<= '~' && ch
!= '[') {
194 while (fkey_table
[i
].sequence
) {
195 if (! strcmp_P (buf
, fkey_table
[i
].sequence
)) {
196 ch
= fkey_table
[i
].code
;
201 if (fkey_table
[i
].sequence
== NULL
) {
202 ch
= '$'; /* KEY_ESCAPE; */
205 state
= STATE_GROUND
;
214 /************************************************************************************************/
217 * cmdline-editing related codes from vivi.
218 * Author: Janghoon Lyu <nandy@mizi.com>
222 char histbuf
[CONFIG_SYS_HISTSIZE
+1];
223 #define HISTBUFE (histbuf+CONFIG_SYS_HISTSIZE)
225 static char *hist_head
;
226 static char *hist_cur
;
228 static void hist_reset(void)
230 if (hist_head
== NULL
)
231 hist_head
= HISTBUFE
;
232 hist_cur
= hist_head
;
235 static char *hist_entry_next(char *p
)
243 static char *hist_entry_prev(char *p
)
249 while (p
!= hist_head
&& *(p
-1) != 0)
255 static char *hist_search_entry(char *line
)
259 while (*p
&& strcmp(p
, line
))
260 p
= hist_entry_next(p
);
261 return *p
? p
: NULL
;
264 static char *hist_delete(size_t amount
)
268 p
= HISTBUFE
- amount
;
272 while (p
> hist_head
&& *(p
-1))
276 hist_head
= HISTBUFE
;
278 size_t shift
= HISTBUFE
- p
;
279 size_t len
= p
- hist_head
;
280 hist_head
= memmove(hist_head
+ shift
, hist_head
, len
);
285 static char *hist_delete_entry(char *entry
)
287 size_t shift
= strlen(entry
) + 1;
288 size_t len
= entry
- hist_head
;
290 hist_head
= memmove(hist_head
+ shift
, hist_head
, len
);
296 static char *hist_add_entry(char *line
)
303 char *q
= p
- strlen(line
) - 1;
307 strlcpy(q
, line
, p
- q
);
314 static uint_fast8_t hist_get_count(void)
326 static int hist_get_size(void)
328 return HISTBUFE
- hist_head
;
331 static char *cread_add_to_hist(char *line
)
335 p
= hist_search_entry(line
);
337 hist_delete_entry(p
);
339 size_t free
= hist_head
- histbuf
;
340 if (free
< strlen(line
) + 1)
341 hist_delete(strlen(line
) + 1 - free
);
343 hist_add_entry(line
);
348 static char *hist_prev(void)
354 hist_cur
= hist_entry_next(p
);
359 static char *hist_next(void)
366 p
= hist_entry_prev(p
);
369 return p
== hist_head
? "" : hist_entry_prev(p
);
372 static char *hist_search_backward(char* buf
, uint8_t num
)
379 while (*p
&& strncmp(p
, buf
, num
))
380 p
= hist_entry_next(p
);
382 if(!strncmp(p
, buf
, num
)) {
383 hist_cur
= hist_entry_next(p
);
390 static char *hist_search_forward (char* buf
, uint8_t num
)
394 if(p
!= hist_head
&& (p
= hist_entry_prev(p
)) != NULL
) {
396 p
= hist_entry_prev(p
);
397 } while (p
&& strncmp(p
, buf
, num
) != 0);
399 //if(!strncmp(p, buf, num)) {
401 hist_cur
= hist_entry_next(p
);
409 static void putnstr(char *str
, int n
)
411 /* printf_P(PSTR("%.*s"), (int)n, str) */
416 static void getcmd_putch(int ch
) { putchar(ch
);}
417 static int getcmd_getch(void) { return vt_parse();}
418 static void getcmd_cbeep(void) { getcmd_putch('\a');}
420 static void beginning_of_line(uint8_t *num
)
423 getcmd_putch(CTL_BACKSPACE
);
428 static void erase_to_eol(uint_fast8_t *num
, uint_fast8_t *eol_num
)
430 if (*num
< *eol_num
) {
431 /* printf_P(PSTR("%*S"), (int)(*eol_num - *num), PSTR("")); */
432 print_blanks(*eol_num
- *num
);
434 getcmd_putch(CTL_BACKSPACE
);
435 } while (--(*eol_num
) > *num
);
439 static void refresh_to_eol(char *buf
, uint_fast8_t *num
, uint_fast8_t *eol_num
)
441 if (*num
< *eol_num
) {
442 uint_fast8_t wlen
= *eol_num
- *num
;
443 putnstr(buf
+ *num
, wlen
);
448 static void cread_add_char(char ichar
, bool insert
, uint_fast8_t *num
,
449 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
454 if (insert
|| *num
== *eol_num
) {
455 if (*eol_num
> len
- 1) {
463 wlen
= *eol_num
- *num
;
465 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
468 putnstr(buf
+ *num
, wlen
);
471 getcmd_putch(CTL_BACKSPACE
);
473 /* echo the character */
475 putnstr(buf
+ *num
, 1);
480 static void cread_add_str(char *str
, bool insert
, uint_fast8_t *num
,
481 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
485 while ((c
= *str
++) != '\0')
486 cread_add_char(c
, insert
, num
, eol_num
, buf
, len
);
489 static int cread_line(const FLASH
char *const prompt
, char *buf
,
490 uint_fast8_t len
, bool enable_history
)
492 uint_fast8_t num
= 0;
493 uint_fast8_t eol_num
= 0;
499 cread_add_str(buf
, 1, &num
, &eol_num
, buf
, len
);
504 int ichar
= getcmd_getch();
506 if ((ichar
== '\n') || (ichar
== '\r')) {
516 beginning_of_line(&num
);
518 case CTL_CH('c'): /* ^C - break */
520 *buf
= '\0'; /* discard input */
523 case CTL_CH('f'): /* forward-char */
525 getcmd_putch(buf
[num
]);
530 case CTL_CH('b'): /* backward-char */
532 getcmd_putch(CTL_BACKSPACE
);
537 case CTL_CH('d'): /* delete-char */
539 uint_fast8_t wlen
= eol_num
- num
- 1;
541 memmove(&buf
[num
], &buf
[num
+1], wlen
);
542 putnstr(buf
+ num
, wlen
);
547 getcmd_putch(CTL_BACKSPACE
);
552 case CTL_CH('k'): /* kill-line */
553 erase_to_eol(&num
, &eol_num
);
557 refresh_to_eol(buf
, &num
, &eol_num
);
564 case CTL_CH('u'): /* kill-whole-line */
565 beginning_of_line(&num
);
566 erase_to_eol(&num
, &eol_num
);
570 case 8: /* backward-delete-char */
572 uint_fast8_t wlen
= eol_num
- --num
;
574 memmove(&buf
[num
], &buf
[num
+1], wlen
);
575 getcmd_putch(CTL_BACKSPACE
);
576 putnstr(buf
+ num
, wlen
);
578 getcmd_putch(CTL_BACKSPACE
);
584 case CTL_CH('p'): /* previous-history */
586 case CTL_CH('n'): /* next-history */
587 if (enable_history
) {
590 if (ichar
== CTL_CH('p') || ichar
== KEY_UP
)
597 beginning_of_line(&num
);
598 /* overwrite current line */
599 cread_add_str(hline
, 0, &num
, &eol_num
, buf
, len
);
600 /* erase to end of line */
601 erase_to_eol(&num
, &eol_num
);
610 case KEY_PPAGE
: /* history-search-backward */
611 case KEY_NPAGE
: /* history-search-forward */
612 if (enable_history
) {
614 if (ichar
== KEY_PPAGE
)
615 hline
= hist_search_backward(buf
, num
);
617 hline
= hist_search_forward(buf
, num
);
620 uint_fast8_t num2
= num
;
621 /* overwrite current line from cursor position */
622 cread_add_str(hline
+num
, 0, &num2
, &eol_num
, buf
, len
);
623 /* erase to end of line */
624 erase_to_eol(&num2
, &eol_num
);
627 getcmd_putch(CTL_BACKSPACE
);
635 #ifdef CONFIG_AUTO_COMPLETE
639 /* do not autocomplete when in the middle */
646 col
= strlen_P(prompt
) + eol_num
;
648 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
658 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, len
);
662 while (eol_num
&& buf
[eol_num
-1] == ' ')
663 --eol_num
; /* remove trailing blanks */
664 buf
[eol_num
] = '\0'; /* lose the newline */
667 while (buf
[i
] == ' ')
668 ++i
; /* remove leading blanks */
671 memmove(buf
, buf
+i
, eol_num
+1);
674 debug_readline("### hist_head: %p, hist_cur: %p\n", hist_head
, hist_cur
);
675 if (enable_history
&& buf
[0]) {
676 cread_add_to_hist(buf
);
677 debug_readline("### hist_head: %p, hist_cur: %p, hist_size: %3d, hist_count: %2d\n",
678 hist_head
, hist_cur
, hist_get_size(), hist_get_count());
683 /****************************************************************************/
685 int cli_readline(const FLASH
char *const prompt
, bool enable_history
)
688 * If console_buffer isn't 0-length the user will be prompted to modify
689 * it instead of entering it from scratch as desired.
691 console_buffer
[0] = '\0';
696 return cread_line(prompt
, console_buffer
, CONFIG_SYS_CBSIZE
, enable_history
);