]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * Add to readline cmdline-editing by
7 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
9 * SPDX-License-Identifier: GPL-2.0+
18 #include "con-utils.h"
20 #include "cli_readline.h"
22 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
24 static const FLASH
char erase_seq
[] = "\b \b"; /* erase sequence */
25 static const FLASH
char tab_seq
[] = " "; /* used to expand TABs */
27 #ifndef CONFIG_CMDLINE_EDITING
28 static char *delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
35 if (*(--p
) == '\t') { /* will retype the whole line */
36 while (*colp
> plen
) {
40 for (s
= buffer
; s
< p
; ++s
) {
42 my_puts_P(tab_seq
+ ((*colp
) & 07));
43 *colp
+= 8 - ((*colp
) & 07);
57 #endif /* CONFIG_CMDLINE_EDITING */
60 #ifdef CONFIG_CMDLINE_EDITING
63 * cmdline-editing related codes from vivi.
64 * Author: Janghoon Lyu <nandy@mizi.com>
67 static void putnstr(char *str
, int n
)
69 /* printf_P(PSTR("%.*s"), (int)n, str) */
75 #define CTL_CH(c) ((c) - 'a' + 1)
76 #define CTL_BACKSPACE ('\b')
77 #define DEL ((char)255)
78 #define DEL7 ((char)127)
79 #define CREAD_HIST_CHAR ('!')
81 #define getcmd_putch(ch) putchar(ch)
82 #define getcmd_getch() my_getchar(1)
83 #define getcmd_cbeep() getcmd_putch('\a')
86 #define HIST_SIZE CONFIG_SYS_CBSIZE
89 static int hist_add_idx
;
90 static int hist_cur
= -1;
91 static unsigned hist_num
;
93 static char *hist_list
[HIST_MAX
];
94 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
96 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
98 static void hist_init(void)
107 for (i
= 0; i
< HIST_MAX
; i
++) {
108 hist_list
[i
] = hist_lines
[i
];
109 hist_list
[i
][0] = '\0';
113 static void cread_add_to_hist(char *line
)
115 strcpy(hist_list
[hist_add_idx
], line
);
117 if (++hist_add_idx
>= HIST_MAX
)
120 if (hist_add_idx
> hist_max
)
121 hist_max
= hist_add_idx
;
126 static char *hist_prev(void)
138 if (hist_cur
== hist_add_idx
) {
142 ret
= hist_list
[hist_cur
];
148 static char *hist_next(void)
155 if (hist_cur
== hist_add_idx
)
158 if (++hist_cur
> hist_max
)
161 if (hist_cur
== hist_add_idx
)
164 ret
= hist_list
[hist_cur
];
169 #ifndef CONFIG_CMDLINE_EDITING
170 static void cread_print_hist_list(void)
175 n
= hist_num
- hist_max
;
177 i
= hist_add_idx
+ 1;
181 if (i
== hist_add_idx
)
183 printf_P(PSTR("%s\n"), hist_list
[i
]);
188 #endif /* CONFIG_CMDLINE_EDITING */
190 #define BEGINNING_OF_LINE() { \
192 getcmd_putch(CTL_BACKSPACE); \
197 #define ERASE_TO_EOL() { \
198 if (num < eol_num) { \
199 /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \
200 print_blanks(eol_num - num); \
202 getcmd_putch(CTL_BACKSPACE); \
203 } while (--eol_num > num); \
207 #define REFRESH_TO_EOL() { \
208 if (num < eol_num) { \
209 wlen = eol_num - num; \
210 putnstr(buf + num, wlen); \
215 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
216 unsigned long *eol_num
, char *buf
, unsigned long len
)
221 if (insert
|| *num
== *eol_num
) {
222 if (*eol_num
> len
- 1) {
230 wlen
= *eol_num
- *num
;
232 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
235 putnstr(buf
+ *num
, wlen
);
238 getcmd_putch(CTL_BACKSPACE
);
240 /* echo the character */
243 putnstr(buf
+ *num
, wlen
);
248 static void cread_add_str(char *str
, int strsize
, int insert
,
249 unsigned long *num
, unsigned long *eol_num
,
250 char *buf
, unsigned long len
)
253 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
258 static int cread_line(const FLASH
char *const prompt
, char *buf
, unsigned int *len
)
260 unsigned long num
= 0;
261 unsigned long eol_num
= 0;
267 int init_len
= strlen(buf
);
272 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
275 ichar
= getcmd_getch();
277 if ((ichar
== '\n') || (ichar
== '\r')) {
283 * handle standard linux xterm esc sequences for arrow key, etc.
288 esc_save
[esc_len
] = ichar
;
291 cread_add_str(esc_save
, esc_len
,
292 insert
, &num
, &eol_num
,
300 case 'D': /* <- key */
304 case 'C': /* -> key */
307 break; /* pass off to ^F handler */
308 case 'H': /* Home key */
311 break; /* pass off to ^A handler */
312 case 'A': /* up arrow */
315 break; /* pass off to ^P handler */
316 case 'B': /* down arrow */
319 break; /* pass off to ^N handler */
321 esc_save
[esc_len
++] = ichar
;
322 cread_add_str(esc_save
, esc_len
, insert
,
323 &num
, &eol_num
, buf
, *len
);
332 esc_save
[esc_len
] = ichar
;
335 my_puts_P(PSTR("impossible condition #876\n"));
343 case CTL_CH('c'): /* ^C - break */
344 *buf
= '\0'; /* discard input */
348 getcmd_putch(buf
[num
]);
354 getcmd_putch(CTL_BACKSPACE
);
360 wlen
= eol_num
- num
- 1;
362 memmove(&buf
[num
], &buf
[num
+1], wlen
);
363 putnstr(buf
+ num
, wlen
);
368 getcmd_putch(CTL_BACKSPACE
);
391 wlen
= eol_num
- num
;
393 memmove(&buf
[num
], &buf
[num
+1], wlen
);
394 getcmd_putch(CTL_BACKSPACE
);
395 putnstr(buf
+ num
, wlen
);
398 getcmd_putch(CTL_BACKSPACE
);
410 if (ichar
== CTL_CH('p'))
420 /* nuke the current line */
424 /* erase to end of line */
427 /* copy new line into place and display */
429 eol_num
= strlen(buf
);
433 #ifdef CONFIG_AUTO_COMPLETE
437 /* do not autocomplete when in the middle */
444 col
= strlen_P(prompt
) + eol_num
;
446 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
455 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
461 buf
[eol_num
] = '\0'; /* lose the newline */
463 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
464 cread_add_to_hist(buf
);
465 hist_cur
= hist_add_idx
;
470 #endif /* CONFIG_CMDLINE_EDITING */
472 /****************************************************************************/
474 static int cli_readline_into_buffer(const FLASH
char *const prompt
, char *buffer
)
477 #ifdef CONFIG_CMDLINE_EDITING
478 unsigned int len
= CONFIG_SYS_CBSIZE
;
490 rc
= cread_line(prompt
, p
, &len
);
491 return rc
< 0 ? rc
: (int) len
;
493 #else /* CONFIG_CMDLINE_EDITING */
495 int n
= 0; /* buffer index */
496 int plen
= 0; /* prompt length */
497 int col
; /* output column cnt */
502 plen
= strlen_P(prompt
);
512 * Special character handling
515 case '\r': /* Enter */
518 my_puts_P(PSTR("\r\n"));
524 case 0x03: /* ^C - break */
525 p_buf
[0] = '\0'; /* discard input */
528 case 0x15: /* ^U - erase line */
530 my_puts_P(erase_seq
);
537 case 0x17: /* ^W - erase word */
538 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
539 while ((n
> 0) && (*p
!= ' '))
540 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
543 case 0x08: /* ^H - backspace */
544 case 0x7F: /* DEL - backspace */
545 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
550 * Must be a normal character then
552 if (n
< CONFIG_SYS_CBSIZE
-2) {
553 if (c
== '\t') { /* expand TABs */
554 #ifdef CONFIG_AUTO_COMPLETE
556 * if auto completion triggered just
560 if (cmd_auto_complete(prompt
,
563 p
= p_buf
+ n
; /* reset */
567 my_puts_P(tab_seq
+ (col
& 07));
568 col
+= 8 - (col
& 07);
573 * Echo input using puts() to force an
574 * LCD flush if we are using an LCD
583 } else { /* Buffer full */
588 #endif /* CONFIG_CMDLINE_EDITING */
591 int cli_readline(const FLASH
char *const prompt
)
594 * If console_buffer isn't 0-length the user will be prompted to modify
595 * it instead of entering it from scratch as desired.
597 console_buffer
[0] = '\0';
599 return cli_readline_into_buffer(prompt
, console_buffer
);