]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
17d549464a2ae9ccedf7b9c4faf403a7ac7af53c
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+
14 #include <avr/pgmspace.h>
19 #include "con-utils.h"
21 #include "cli_readline.h"
23 char console_buffer
[CONFIG_SYS_CBSIZE
+ 1]; /* console I/O buffer */
25 static const FLASH
char erase_seq
[] = "\b \b"; /* erase sequence */
26 static const FLASH
char tab_seq
[] = " "; /* used to expand TABs */
28 #ifndef CONFIG_CMDLINE_EDITING
29 static char *delete_char (char *buffer
, char *p
, int *colp
, int *np
, int plen
)
36 if (*(--p
) == '\t') { /* will retype the whole line */
37 while (*colp
> plen
) {
41 for (s
= buffer
; s
< p
; ++s
) {
43 my_puts_P(tab_seq
+ ((*colp
) & 07));
44 *colp
+= 8 - ((*colp
) & 07);
58 #endif /* CONFIG_CMDLINE_EDITING */
61 #ifdef CONFIG_CMDLINE_EDITING
64 * cmdline-editing related codes from vivi.
65 * Author: Janghoon Lyu <nandy@mizi.com>
68 #define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str)
70 #define CTL_CH(c) ((c) - 'a' + 1)
71 #define CTL_BACKSPACE ('\b')
72 #define DEL ((char)255)
73 #define DEL7 ((char)127)
74 #define CREAD_HIST_CHAR ('!')
76 #define getcmd_putch(ch) putchar(ch)
77 #define getcmd_getch() my_getchar()
78 #define getcmd_cbeep() getcmd_putch('\a')
81 #define HIST_SIZE CONFIG_SYS_CBSIZE
84 static int hist_add_idx
;
85 static int hist_cur
= -1;
86 static unsigned hist_num
;
88 static char *hist_list
[HIST_MAX
];
89 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
91 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
93 static void hist_init(void)
102 for (i
= 0; i
< HIST_MAX
; i
++) {
103 hist_list
[i
] = hist_lines
[i
];
104 hist_list
[i
][0] = '\0';
108 static void cread_add_to_hist(char *line
)
110 strcpy(hist_list
[hist_add_idx
], line
);
112 if (++hist_add_idx
>= HIST_MAX
)
115 if (hist_add_idx
> hist_max
)
116 hist_max
= hist_add_idx
;
121 static char *hist_prev(void)
133 if (hist_cur
== hist_add_idx
) {
137 ret
= hist_list
[hist_cur
];
143 static char *hist_next(void)
150 if (hist_cur
== hist_add_idx
)
153 if (++hist_cur
> hist_max
)
156 if (hist_cur
== hist_add_idx
)
159 ret
= hist_list
[hist_cur
];
164 #ifndef CONFIG_CMDLINE_EDITING
165 static void cread_print_hist_list(void)
170 n
= hist_num
- hist_max
;
172 i
= hist_add_idx
+ 1;
176 if (i
== hist_add_idx
)
178 printf_P(PSTR("%s\n"), hist_list
[i
]);
183 #endif /* CONFIG_CMDLINE_EDITING */
185 #define BEGINNING_OF_LINE() { \
187 getcmd_putch(CTL_BACKSPACE); \
192 #define ERASE_TO_EOL() { \
193 if (num < eol_num) { \
194 printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \
196 getcmd_putch(CTL_BACKSPACE); \
197 } while (--eol_num > num); \
201 #define REFRESH_TO_EOL() { \
202 if (num < eol_num) { \
203 wlen = eol_num - num; \
204 putnstr(buf + num, wlen); \
209 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
210 unsigned long *eol_num
, char *buf
, unsigned long len
)
215 if (insert
|| *num
== *eol_num
) {
216 if (*eol_num
> len
- 1) {
224 wlen
= *eol_num
- *num
;
226 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
229 putnstr(buf
+ *num
, wlen
);
232 getcmd_putch(CTL_BACKSPACE
);
234 /* echo the character */
237 putnstr(buf
+ *num
, wlen
);
242 static void cread_add_str(char *str
, int strsize
, int insert
,
243 unsigned long *num
, unsigned long *eol_num
,
244 char *buf
, unsigned long len
)
247 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
252 static int cread_line(const FLASH
char *const prompt
, char *buf
, unsigned int *len
)
254 unsigned long num
= 0;
255 unsigned long eol_num
= 0;
261 int init_len
= strlen(buf
);
266 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
269 ichar
= getcmd_getch();
271 if ((ichar
== '\n') || (ichar
== '\r')) {
277 * handle standard linux xterm esc sequences for arrow key, etc.
282 esc_save
[esc_len
] = ichar
;
285 cread_add_str(esc_save
, esc_len
,
286 insert
, &num
, &eol_num
,
294 case 'D': /* <- key */
298 case 'C': /* -> key */
301 break; /* pass off to ^F handler */
302 case 'H': /* Home key */
305 break; /* pass off to ^A handler */
306 case 'A': /* up arrow */
309 break; /* pass off to ^P handler */
310 case 'B': /* down arrow */
313 break; /* pass off to ^N handler */
315 esc_save
[esc_len
++] = ichar
;
316 cread_add_str(esc_save
, esc_len
, insert
,
317 &num
, &eol_num
, buf
, *len
);
326 esc_save
[esc_len
] = ichar
;
329 my_puts_P(PSTR("impossible condition #876\n"));
337 case CTL_CH('c'): /* ^C - break */
338 *buf
= '\0'; /* discard input */
342 getcmd_putch(buf
[num
]);
348 getcmd_putch(CTL_BACKSPACE
);
354 wlen
= eol_num
- num
- 1;
356 memmove(&buf
[num
], &buf
[num
+1], wlen
);
357 putnstr(buf
+ num
, wlen
);
362 getcmd_putch(CTL_BACKSPACE
);
385 wlen
= eol_num
- num
;
387 memmove(&buf
[num
], &buf
[num
+1], wlen
);
388 getcmd_putch(CTL_BACKSPACE
);
389 putnstr(buf
+ num
, wlen
);
392 getcmd_putch(CTL_BACKSPACE
);
404 if (ichar
== CTL_CH('p'))
414 /* nuke the current line */
418 /* erase to end of line */
421 /* copy new line into place and display */
423 eol_num
= strlen(buf
);
427 #ifdef CONFIG_AUTO_COMPLETE
431 /* do not autocomplete when in the middle */
438 col
= strlen_P(prompt
) + eol_num
;
440 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
449 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
455 buf
[eol_num
] = '\0'; /* lose the newline */
457 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
458 cread_add_to_hist(buf
);
459 hist_cur
= hist_add_idx
;
464 #endif /* CONFIG_CMDLINE_EDITING */
466 /****************************************************************************/
468 static int cli_readline_into_buffer(const FLASH
char *const prompt
, char *buffer
)
471 #ifdef CONFIG_CMDLINE_EDITING
472 unsigned int len
= CONFIG_SYS_CBSIZE
;
484 rc
= cread_line(prompt
, p
, &len
);
485 return rc
< 0 ? rc
: (int) len
;
487 #else /* CONFIG_CMDLINE_EDITING */
489 int n
= 0; /* buffer index */
490 int plen
= 0; /* prompt length */
491 int col
; /* output column cnt */
496 plen
= strlen_P(prompt
);
506 * Special character handling
509 case '\r': /* Enter */
512 my_puts_P(PSTR("\r\n"));
518 case 0x03: /* ^C - break */
519 p_buf
[0] = '\0'; /* discard input */
522 case 0x15: /* ^U - erase line */
524 my_puts_P(erase_seq
);
531 case 0x17: /* ^W - erase word */
532 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
533 while ((n
> 0) && (*p
!= ' '))
534 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
537 case 0x08: /* ^H - backspace */
538 case 0x7F: /* DEL - backspace */
539 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
544 * Must be a normal character then
546 if (n
< CONFIG_SYS_CBSIZE
-2) {
547 if (c
== '\t') { /* expand TABs */
548 #ifdef CONFIG_AUTO_COMPLETE
550 * if auto completion triggered just
554 if (cmd_auto_complete(prompt
,
557 p
= p_buf
+ n
; /* reset */
561 my_puts_P(tab_seq
+ (col
& 07));
562 col
+= 8 - (col
& 07);
567 * Echo input using puts() to force an
568 * LCD flush if we are using an LCD
577 } else { /* Buffer full */
582 #endif /* CONFIG_CMDLINE_EDITING */
585 int cli_readline(const FLASH
char *const prompt
)
588 * If console_buffer isn't 0-length the user will be prompted to modify
589 * it instead of entering it from scratch as desired.
591 console_buffer
[0] = '\0';
593 return cli_readline_into_buffer(prompt
, console_buffer
);