]>
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"
23 #include "con-utils.h"
24 #include "print-utils.h"
29 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
31 #define CTL_CH(c) ((c) - 'a' + 1)
32 #define CTL_BACKSPACE ('\b')
33 #define DEL ((char)255)
34 #define DEL7 ((char)127)
37 /************************************************************************************************/
44 #define KEY_TAB '\t' // TAB key
45 #define KEY_CR '\r' // RETURN key
46 #define KEY_BACKSPACE '\b' // Backspace key
47 #define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
49 #define KEY_DOWN 0x80 // Down arrow key
50 #define KEY_UP 0x81 // Up arrow key
51 #define KEY_LEFT 0x82 // Left arrow key
52 #define KEY_RIGHT 0x83 // Right arrow key
53 #define KEY_HOME 0x84 // Home key
54 #define KEY_DC 0x85 // Delete character key
55 #define KEY_IC 0x86 // Ins char/toggle ins mode key
56 #define KEY_NPAGE 0x87 // Next-page key
57 #define KEY_PPAGE 0x88 // Previous-page key
58 #define KEY_END 0x89 // End key
59 #define KEY_BTAB 0x8A // Back tab key
60 #define KEY_F1 0x8B // Function key F1
61 #define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
65 const FLASH
char *sequence
; /* ESC Sequence */
66 int code
; /* Keycode */
69 //typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
71 #define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
75 static const FLASH
struct fkey_tbl_s fkey_table
[] = {
77 FKEY_TBL_ITEM(B
, KEY_DOWN
), // Down arrow key
78 FKEY_TBL_ITEM(A
, KEY_UP
), // Up arrow key
79 FKEY_TBL_ITEM(D
, KEY_LEFT
), // Left arrow key
80 FKEY_TBL_ITEM(C
, KEY_RIGHT
), // Right arrow key
81 FKEY_TBL_ITEM(1~, KEY_HOME
), // Home key
82 FKEY_TBL_ITEM(3~, KEY_DC
), // Delete character key
83 FKEY_TBL_ITEM(2~, KEY_IC
), // Ins char/toggle ins mode key
84 FKEY_TBL_ITEM(6~, KEY_NPAGE
), // Next-page key
85 FKEY_TBL_ITEM(5~, KEY_PPAGE
), // Previous-page key
86 FKEY_TBL_ITEM(4~, KEY_END
), // End key
87 FKEY_TBL_ITEM(Z
, KEY_BTAB
), // Back tab key
89 FKEY_TBL_ITEM(H
, KEY_HOME
), // Home key
90 FKEY_TBL_ITEM(F
, KEY_END
), // End key
92 FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
93 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
94 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
95 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
96 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
98 FKEY_TBL_ITEM([A
, KEY_F(1)), // Function key F1
99 FKEY_TBL_ITEM([B
, KEY_F(2)), // Function key F2
100 FKEY_TBL_ITEM([C
, KEY_F(3)), // Function key F3
101 FKEY_TBL_ITEM([D
, KEY_F(4)), // Function key F4
102 FKEY_TBL_ITEM([E
, KEY_F(5)), // Function key F5
104 FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
105 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
106 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
107 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
108 FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
109 FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
110 FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
111 { NULL
} /* Mark end of table */
128 static vtparse_state_t state
= STATE_GROUND
;
129 char buf
[CHB_SIZE
+1];
138 // debug_getch(state, ch);
143 state
= STATE_ESCAPE
;
146 if (ch
== 0x7F) // BACKSPACE on VT200 sends DEL char
147 ch
= KEY_BACKSPACE
; // map it to '\b'
154 state
= STATE_CSI_ENTRY
;
155 param
[0] = param
[1] = 0;
164 state
= STATE_GROUND
;
171 state
= STATE_GROUND
;
173 case STATE_CSI_ENTRY
:
178 if (i_buf
< CHB_SIZE
)
186 param
[i_param
] = param
[i_param
] * 10 + ch
- '0';
189 if (ch
>= '@' && ch
<= '~' && ch
!= '[') {
192 while (fkey_table
[i
].sequence
) {
193 if (! strcmp_P (buf
, fkey_table
[i
].sequence
)) {
194 ch
= fkey_table
[i
].code
;
199 if (fkey_table
[i
].sequence
== NULL
) {
200 ch
= '$'; /* KEY_ESCAPE; */
203 state
= STATE_GROUND
;
212 /************************************************************************************************/
215 * cmdline-editing related codes from vivi.
216 * Author: Janghoon Lyu <nandy@mizi.com>
221 struct hist_node_s
*next
;
224 typedef struct hist_node_s hist_node
;
227 static hist_node
*hist_head
;
228 static hist_node
*hist_cur
;
230 static void hist_reset(void)
232 hist_cur
= hist_head
;
235 static hist_node
*hist_search_node(char *line
)
237 hist_node
*p
= hist_head
;
239 while (p
&& strcmp(p
->line
, line
))
245 static hist_node
*hist_insert(char *line
)
247 hist_node
*p
= (hist_node
*) malloc(sizeof (hist_node
) + strlen(line
) + 1);
250 strcpy(p
->line
, line
);
258 static hist_node
*hist_new(char *line
)
260 hist_node
*p
= (hist_node
*) malloc(sizeof (hist_node
) + strlen(line
) + 1);
263 strcpy(p
->line
, line
);
269 static hist_node
*hist_delete(void)
272 hist_node
*q
= hist_head
;
286 static hist_node
*hist_unlink(hist_node
*pos
)
289 hist_node
*q
= hist_head
;
291 while(q
&& q
!= pos
) {
305 static uint_fast8_t hist_count(void)
307 hist_node
*p
= hist_head
;
317 static hist_node
*cread_add_to_hist(char *line
)
321 p
= hist_search_node(line
);
332 if (hist_count() > CONFIG_SYS_HIST_MAX
)
337 static char *hist_prev(void)
339 hist_node
*p
= hist_cur
;
348 static char *hist_next(void)
351 hist_node
*q
= hist_head
;
356 while(q
->next
!= hist_cur
) {
362 return p
? p
->line
: "";
365 static char *hist_search_backward(char* buf
, uint8_t num
)
367 hist_node
*p
= hist_cur
;
372 while (p
->next
&& strncmp(p
->line
, buf
, num
))
375 if(!strncmp(p
->line
, buf
, num
)) {
382 static char *hist_search_forward (char* buf
, uint8_t num
)
385 hist_node
*match
= NULL
;
386 hist_node
*q
= hist_head
;
391 while(q
->next
!= hist_cur
) {
394 if (p
&& !strncmp(p
->line
, buf
, num
))
399 hist_cur
= match
->next
;
405 static void putnstr(char *str
, int n
)
407 /* printf_P(PSTR("%.*s"), (int)n, str) */
412 static void getcmd_putch(int ch
) { putchar(ch
);}
413 static int getcmd_getch(void) { return vt_parse();}
414 static void getcmd_cbeep(void) { getcmd_putch('\a');}
416 static void beginning_of_line(uint8_t *num
)
419 getcmd_putch(CTL_BACKSPACE
);
424 static void erase_to_eol(uint_fast8_t *num
, uint_fast8_t *eol_num
)
426 if (*num
< *eol_num
) {
427 /* printf_P(PSTR("%*S"), (int)(*eol_num - *num), PSTR("")); */
428 print_blanks(*eol_num
- *num
);
430 getcmd_putch(CTL_BACKSPACE
);
431 } while (--(*eol_num
) > *num
);
435 static void refresh_to_eol(char *buf
, uint_fast8_t *num
, uint_fast8_t *eol_num
)
437 if (*num
< *eol_num
) {
438 uint_fast8_t wlen
= *eol_num
- *num
;
439 putnstr(buf
+ *num
, wlen
);
444 static void cread_add_char(char ichar
, bool insert
, uint_fast8_t *num
,
445 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
450 if (insert
|| *num
== *eol_num
) {
451 if (*eol_num
> len
- 1) {
459 wlen
= *eol_num
- *num
;
461 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
464 putnstr(buf
+ *num
, wlen
);
467 getcmd_putch(CTL_BACKSPACE
);
469 /* echo the character */
471 putnstr(buf
+ *num
, 1);
476 static void cread_add_str(char *str
, bool insert
, uint_fast8_t *num
,
477 uint_fast8_t *eol_num
, char *buf
, uint_fast8_t len
)
481 while ((c
= *str
++) != '\0')
482 cread_add_char(c
, insert
, num
, eol_num
, buf
, len
);
485 static int cread_line(const FLASH
char *const prompt
, char *buf
,
486 uint_fast8_t len
, bool enable_history
)
488 uint_fast8_t num
= 0;
489 uint_fast8_t eol_num
= 0;
495 cread_add_str(buf
, 1, &num
, &eol_num
, buf
, len
);
500 int ichar
= getcmd_getch();
502 if ((ichar
== '\n') || (ichar
== '\r')) {
512 beginning_of_line(&num
);
514 case CTL_CH('c'): /* ^C - break */
516 *buf
= '\0'; /* discard input */
519 case CTL_CH('f'): /* forward-char */
521 getcmd_putch(buf
[num
]);
526 case CTL_CH('b'): /* backward-char */
528 getcmd_putch(CTL_BACKSPACE
);
533 case CTL_CH('d'): /* delete-char */
535 uint_fast8_t wlen
= eol_num
- num
- 1;
537 memmove(&buf
[num
], &buf
[num
+1], wlen
);
538 putnstr(buf
+ num
, wlen
);
543 getcmd_putch(CTL_BACKSPACE
);
548 case CTL_CH('k'): /* kill-line */
549 erase_to_eol(&num
, &eol_num
);
553 refresh_to_eol(buf
, &num
, &eol_num
);
560 case CTL_CH('u'): /* kill-whole-line */
561 beginning_of_line(&num
);
562 erase_to_eol(&num
, &eol_num
);
566 case 8: /* backward-delete-char */
568 uint_fast8_t wlen
= eol_num
- --num
;
570 memmove(&buf
[num
], &buf
[num
+1], wlen
);
571 getcmd_putch(CTL_BACKSPACE
);
572 putnstr(buf
+ num
, wlen
);
574 getcmd_putch(CTL_BACKSPACE
);
580 case CTL_CH('p'): /* previous-history */
582 case CTL_CH('n'): /* next-history */
583 if (enable_history
) {
586 if (ichar
== CTL_CH('p') || ichar
== KEY_UP
)
593 beginning_of_line(&num
);
594 /* overwrite current line */
595 cread_add_str(hline
, 0, &num
, &eol_num
, buf
, len
);
596 /* erase to end of line */
597 erase_to_eol(&num
, &eol_num
);
606 case KEY_PPAGE
: /* history-search-backward */
607 case KEY_NPAGE
: /* history-search-forward */
608 if (enable_history
) {
610 if (ichar
== KEY_PPAGE
)
611 hline
= hist_search_backward(buf
, num
);
613 hline
= hist_search_forward(buf
, num
);
616 uint_fast8_t num2
= num
;
617 /* overwrite current line from cursor position */
618 cread_add_str(hline
+num
, 0, &num2
, &eol_num
, buf
, len
);
619 /* erase to end of line */
620 erase_to_eol(&num2
, &eol_num
);
623 getcmd_putch(CTL_BACKSPACE
);
631 #ifdef CONFIG_AUTO_COMPLETE
635 /* do not autocomplete when in the middle */
642 col
= strlen_P(prompt
) + eol_num
;
644 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
654 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
, len
);
658 while (eol_num
&& buf
[eol_num
-1] == ' ')
659 --eol_num
; /* remove trailing blanks */
660 buf
[eol_num
] = '\0'; /* lose the newline */
662 if (enable_history
&& buf
[0])
663 cread_add_to_hist(buf
);
667 /****************************************************************************/
669 int cli_readline(const FLASH
char *const prompt
, bool enable_history
)
672 * If console_buffer isn't 0-length the user will be prompted to modify
673 * it instead of entering it from scratch as desired.
675 console_buffer
[0] = '\0';
680 return cread_line(prompt
, console_buffer
, CONFIG_SYS_CBSIZE
, enable_history
);