]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
81230e02d86e092abddde298e6ec6d2823ac9045
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"
23 #include "con-utils.h"
24 #include "print-utils.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 */
117 static vtparse_state_t state
= STATE_GROUND
;
118 char buf
[CHB_SIZE
+1];
127 // debug_getch(state, ch);
132 state
= STATE_ESCAPE
;
135 if (ch
== 0x7F) // BACKSPACE on VT200 sends DEL char
136 ch
= KEY_BACKSPACE
; // map it to '\b'
143 state
= STATE_CSI_ENTRY
;
144 param
[0] = param
[1] = 0;
153 state
= STATE_GROUND
;
156 if (ch
== 'F') /* weird */
158 state
= STATE_GROUND
;
160 case STATE_CSI_ENTRY
:
165 if (i_buf
< CHB_SIZE
)
173 param
[i_param
] = param
[i_param
] * 10 + ch
- '0';
176 if (ch
>= '@' && ch
<= '~' && ch
!= '[') {
179 while (fkey_table
[i
].sequence
) {
180 if (! strcmp_P (buf
, fkey_table
[i
].sequence
)) {
181 ch
= fkey_table
[i
].code
;
186 if (fkey_table
[i
].sequence
== NULL
) {
187 ch
= '$'; /* KEY_ESCAPE; */
190 state
= STATE_GROUND
;
199 /************************************************************************************************/
202 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
205 * cmdline-editing related codes from vivi.
206 * Author: Janghoon Lyu <nandy@mizi.com>
209 static void putnstr(char *str
, int n
)
211 /* printf_P(PSTR("%.*s"), (int)n, str) */
217 #define CTL_CH(c) ((c) - 'a' + 1)
218 #define CTL_BACKSPACE ('\b')
219 #define DEL ((char)255)
220 #define DEL7 ((char)127)
222 #define getcmd_putch(ch) putchar(ch)
223 #define getcmd_getch() vt_parse()
224 #define getcmd_cbeep() getcmd_putch('\a')
228 struct hist_node_s
*next
;
231 typedef struct hist_node_s hist_node
;
234 static hist_node
*hist_head
;
235 static hist_node
*hist_cur
;
237 static hist_node
*hist_search_node(char *line
)
239 hist_node
*p
= hist_head
;
241 while (p
&& strcmp(p
->line
, line
))
247 static hist_node
*hist_insert(char *line
)
249 hist_node
*p
= (hist_node
*) malloc(sizeof (hist_node
) + strlen(line
) + 1);
252 strcpy(p
->line
, line
);
260 static hist_node
*hist_new(char *line
)
262 hist_node
*p
= (hist_node
*) malloc(sizeof (hist_node
) + strlen(line
) + 1);
265 strcpy(p
->line
, line
);
271 static hist_node
*hist_delete(void)
274 hist_node
*q
= hist_head
;
288 static hist_node
*hist_unlink(hist_node
*pos
)
291 hist_node
*q
= hist_head
;
293 while(q
&& q
!= pos
) {
307 static uint_fast8_t hist_count(void)
309 hist_node
*p
= hist_head
;
319 static hist_node
*cread_add_to_hist(char *line
)
323 p
= hist_search_node(line
);
334 if (hist_count() > CONFIG_SYS_HIST_MAX
)
339 static char *hist_prev(void)
341 hist_node
*p
= hist_cur
;
346 hist_cur
= hist_cur
->next
;
351 static char *hist_next(void)
354 hist_node
*q
= hist_head
;
359 while(q
->next
!= hist_cur
) {
365 return p
? p
->line
: "";
369 #define BEGINNING_OF_LINE() { \
371 getcmd_putch(CTL_BACKSPACE); \
376 #define ERASE_TO_EOL() { \
377 if (num < eol_num) { \
378 /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \
379 print_blanks(eol_num - num); \
381 getcmd_putch(CTL_BACKSPACE); \
382 } while (--eol_num > num); \
386 #define REFRESH_TO_EOL() { \
387 if (num < eol_num) { \
388 uint_fast8_t wlen = eol_num - num; \
389 putnstr(buf + num, wlen); \
394 static void cread_add_char(char ichar
, bool insert
, uint_fast8_t *num
,
395 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
400 if (insert
|| *num
== *eol_num
) {
401 if (*eol_num
> len
- 1) {
409 wlen
= *eol_num
- *num
;
411 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
414 putnstr(buf
+ *num
, wlen
);
417 getcmd_putch(CTL_BACKSPACE
);
419 /* echo the character */
422 putnstr(buf
+ *num
, wlen
);
427 static void cread_add_str(char *str
, uint_fast8_t strsize
, bool insert
,
428 uint_fast8_t *num
, uint_fast8_t *eol_num
,
429 char *buf
, uint_fast8_t len
)
432 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
437 static int cread_line(const FLASH
char *const prompt
, char *buf
,
438 uint_fast8_t *len
, bool enable_history
)
440 uint_fast8_t num
= 0;
441 uint_fast8_t eol_num
= 0;
446 uint_fast8_t init_len
= strlen(buf
);
448 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
451 int ichar
= getcmd_getch();
453 if ((ichar
== '\n') || (ichar
== '\r')) {
465 case CTL_CH('c'): /* ^C - break */
466 *buf
= '\0'; /* discard input */
469 case CTL_CH('f'): /* forward-char */
471 getcmd_putch(buf
[num
]);
476 case CTL_CH('b'): /* backward-char */
478 getcmd_putch(CTL_BACKSPACE
);
483 case CTL_CH('d'): /* delete-char */
485 uint_fast8_t wlen
= eol_num
- num
- 1;
487 memmove(&buf
[num
], &buf
[num
+1], wlen
);
488 putnstr(buf
+ num
, wlen
);
493 getcmd_putch(CTL_BACKSPACE
);
498 case CTL_CH('k'): /* kill-line */
510 case CTL_CH('u'): /* kill-whole-line */
516 case 8: /* backward-delete-char */
518 uint_fast8_t wlen
= eol_num
- num
;
520 memmove(&buf
[num
], &buf
[num
+1], wlen
);
521 getcmd_putch(CTL_BACKSPACE
);
522 putnstr(buf
+ num
, wlen
);
525 getcmd_putch(CTL_BACKSPACE
);
531 case CTL_CH('p'): /* previous-history */
533 case CTL_CH('n'): /* next-history */
534 if (enable_history
) {
537 if (ichar
== CTL_CH('p') || ichar
== KEY_UP
)
543 /* nuke the current line */
547 /* erase to end of line */
550 /* copy new line into place and display */
552 eol_num
= strlen(buf
);
562 case KEY_PPAGE
: /* history-search-backward */
563 case KEY_NPAGE
: /* history-search-forward */
564 if (enable_history
) {
567 if (ichar
== KEY_PPAGE
)
568 hline
= hist_search_backward();
570 hline
= hist_search_forward();
573 /* nuke the current line */
577 /* erase to end of line */
580 /* copy new line into place and display */
582 eol_num
= strlen(buf
);
592 #ifdef CONFIG_AUTO_COMPLETE
596 /* do not autocomplete when in the middle */
603 col
= strlen_P(prompt
) + eol_num
;
605 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
615 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
621 buf
[eol_num
] = '\0'; /* lose the newline */
623 if (enable_history
) {
625 hist_cur
= cread_add_to_hist(buf
);
630 /****************************************************************************/
632 static int cli_readline_into_buffer(const FLASH
char *const prompt
,
633 char *buffer
, bool enable_history
)
636 uint_fast8_t len
= CONFIG_SYS_CBSIZE
;
642 rc
= cread_line(prompt
, p
, &len
, enable_history
);
643 return rc
< 0 ? rc
: (int) len
;
646 int cli_readline(const FLASH
char *const prompt
, bool enable_history
)
649 * If console_buffer isn't 0-length the user will be prompted to modify
650 * it instead of entering it from scratch as desired.
652 console_buffer
[0] = '\0';
654 return cli_readline_into_buffer(prompt
, console_buffer
, enable_history
);