]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
04d32b126eae45efde86017e347e2d62f0decb25
2 * (C) Copyright 2014 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+
20 #include "con-utils.h"
22 #include "cli_readline.h"
24 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
26 static const FLASH
char erase_seq
[] = "\b \b"; /* erase sequence */
27 static const FLASH
char tab_seq
[] = " "; /* used to expand TABs */
29 #ifndef CONFIG_CMDLINE_EDITING
30 static char *delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
37 if (*(--p
) == '\t') { /* will retype the whole line */
38 while (*colp
> plen
) {
42 for (s
= buffer
; s
< p
; ++s
) {
44 my_puts_P(tab_seq
+ ((*colp
) & 07));
45 *colp
+= 8 - ((*colp
) & 07);
59 #endif /* CONFIG_CMDLINE_EDITING */
62 #ifdef CONFIG_CMDLINE_EDITING
65 * cmdline-editing related codes from vivi.
66 * Author: Janghoon Lyu <nandy@mizi.com>
69 #define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str)
71 #define CTL_CH(c) ((c) - 'a' + 1)
72 #define CTL_BACKSPACE ('\b')
73 #define DEL ((char)255)
74 #define DEL7 ((char)127)
75 #define CREAD_HIST_CHAR ('!')
77 #define getcmd_putch(ch) putchar(ch)
78 #define getcmd_getch() my_getchar(1)
79 #define getcmd_cbeep() getcmd_putch('\a')
82 #define HIST_SIZE CONFIG_SYS_CBSIZE
85 static int hist_add_idx
;
86 static int hist_cur
= -1;
87 static unsigned hist_num
;
89 static char *hist_list
[HIST_MAX
];
90 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
92 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
94 static void hist_init(void)
103 for (i
= 0; i
< HIST_MAX
; i
++) {
104 hist_list
[i
] = hist_lines
[i
];
105 hist_list
[i
][0] = '\0';
109 static void cread_add_to_hist(char *line
)
111 strcpy(hist_list
[hist_add_idx
], line
);
113 if (++hist_add_idx
>= HIST_MAX
)
116 if (hist_add_idx
> hist_max
)
117 hist_max
= hist_add_idx
;
122 static char *hist_prev(void)
134 if (hist_cur
== hist_add_idx
) {
138 ret
= hist_list
[hist_cur
];
144 static char *hist_next(void)
151 if (hist_cur
== hist_add_idx
)
154 if (++hist_cur
> hist_max
)
157 if (hist_cur
== hist_add_idx
)
160 ret
= hist_list
[hist_cur
];
165 #ifndef CONFIG_CMDLINE_EDITING
166 static void cread_print_hist_list(void)
171 n
= hist_num
- hist_max
;
173 i
= hist_add_idx
+ 1;
177 if (i
== hist_add_idx
)
179 printf_P(PSTR("%s\n"), hist_list
[i
]);
184 #endif /* CONFIG_CMDLINE_EDITING */
186 #define BEGINNING_OF_LINE() { \
188 getcmd_putch(CTL_BACKSPACE); \
193 #define ERASE_TO_EOL() { \
194 if (num < eol_num) { \
195 printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \
197 getcmd_putch(CTL_BACKSPACE); \
198 } while (--eol_num > num); \
202 #define REFRESH_TO_EOL() { \
203 if (num < eol_num) { \
204 wlen = eol_num - num; \
205 putnstr(buf + num, wlen); \
210 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
211 unsigned long *eol_num
, char *buf
, unsigned long len
)
216 if (insert
|| *num
== *eol_num
) {
217 if (*eol_num
> len
- 1) {
225 wlen
= *eol_num
- *num
;
227 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
230 putnstr(buf
+ *num
, wlen
);
233 getcmd_putch(CTL_BACKSPACE
);
235 /* echo the character */
238 putnstr(buf
+ *num
, wlen
);
243 static void cread_add_str(char *str
, int strsize
, int insert
,
244 unsigned long *num
, unsigned long *eol_num
,
245 char *buf
, unsigned long len
)
248 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
253 static int cread_line(const FLASH
char *const prompt
, char *buf
, unsigned int *len
)
255 unsigned long num
= 0;
256 unsigned long eol_num
= 0;
262 int init_len
= strlen(buf
);
267 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
270 ichar
= getcmd_getch();
272 if ((ichar
== '\n') || (ichar
== '\r')) {
278 * handle standard linux xterm esc sequences for arrow key, etc.
283 esc_save
[esc_len
] = ichar
;
286 cread_add_str(esc_save
, esc_len
,
287 insert
, &num
, &eol_num
,
295 case 'D': /* <- key */
299 case 'C': /* -> key */
302 break; /* pass off to ^F handler */
303 case 'H': /* Home key */
306 break; /* pass off to ^A handler */
307 case 'A': /* up arrow */
310 break; /* pass off to ^P handler */
311 case 'B': /* down arrow */
314 break; /* pass off to ^N handler */
316 esc_save
[esc_len
++] = ichar
;
317 cread_add_str(esc_save
, esc_len
, insert
,
318 &num
, &eol_num
, buf
, *len
);
327 esc_save
[esc_len
] = ichar
;
330 my_puts_P(PSTR("impossible condition #876\n"));
338 case CTL_CH('c'): /* ^C - break */
339 *buf
= '\0'; /* discard input */
343 getcmd_putch(buf
[num
]);
349 getcmd_putch(CTL_BACKSPACE
);
355 wlen
= eol_num
- num
- 1;
357 memmove(&buf
[num
], &buf
[num
+1], wlen
);
358 putnstr(buf
+ num
, wlen
);
363 getcmd_putch(CTL_BACKSPACE
);
386 wlen
= eol_num
- num
;
388 memmove(&buf
[num
], &buf
[num
+1], wlen
);
389 getcmd_putch(CTL_BACKSPACE
);
390 putnstr(buf
+ num
, wlen
);
393 getcmd_putch(CTL_BACKSPACE
);
405 if (ichar
== CTL_CH('p'))
415 /* nuke the current line */
419 /* erase to end of line */
422 /* copy new line into place and display */
424 eol_num
= strlen(buf
);
428 #ifdef CONFIG_AUTO_COMPLETE
432 /* do not autocomplete when in the middle */
439 col
= strlen_P(prompt
) + eol_num
;
441 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
450 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
456 buf
[eol_num
] = '\0'; /* lose the newline */
458 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
459 cread_add_to_hist(buf
);
460 hist_cur
= hist_add_idx
;
465 #endif /* CONFIG_CMDLINE_EDITING */
467 /****************************************************************************/
469 static int cli_readline_into_buffer(const FLASH
char *const prompt
, char *buffer
)
472 #ifdef CONFIG_CMDLINE_EDITING
473 unsigned int len
= CONFIG_SYS_CBSIZE
;
485 rc
= cread_line(prompt
, p
, &len
);
486 return rc
< 0 ? rc
: (int) len
;
488 #else /* CONFIG_CMDLINE_EDITING */
490 int n
= 0; /* buffer index */
491 int plen
= 0; /* prompt length */
492 int col
; /* output column cnt */
497 plen
= strlen_P(prompt
);
507 * Special character handling
510 case '\r': /* Enter */
513 my_puts_P(PSTR("\r\n"));
519 case 0x03: /* ^C - break */
520 p_buf
[0] = '\0'; /* discard input */
523 case 0x15: /* ^U - erase line */
525 my_puts_P(erase_seq
);
532 case 0x17: /* ^W - erase word */
533 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
534 while ((n
> 0) && (*p
!= ' '))
535 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
538 case 0x08: /* ^H - backspace */
539 case 0x7F: /* DEL - backspace */
540 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
545 * Must be a normal character then
547 if (n
< CONFIG_SYS_CBSIZE
-2) {
548 if (c
== '\t') { /* expand TABs */
549 #ifdef CONFIG_AUTO_COMPLETE
551 * if auto completion triggered just
555 if (cmd_auto_complete(prompt
,
558 p
= p_buf
+ n
; /* reset */
562 my_puts_P(tab_seq
+ (col
& 07));
563 col
+= 8 - (col
& 07);
568 * Echo input using puts() to force an
569 * LCD flush if we are using an LCD
578 } else { /* Buffer full */
583 #endif /* CONFIG_CMDLINE_EDITING */
586 int cli_readline(const FLASH
char *const prompt
)
589 * If console_buffer isn't 0-length the user will be prompted to modify
590 * it instead of entering it from scratch as desired.
592 console_buffer
[0] = '\0';
594 return cli_readline_into_buffer(prompt
, console_buffer
);