]>
cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
1dbc73b1e027c3687c2086135a21aba853972d05
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 #define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str)
69 #define CTL_CH(c) ((c) - 'a' + 1)
70 #define CTL_BACKSPACE ('\b')
71 #define DEL ((char)255)
72 #define DEL7 ((char)127)
73 #define CREAD_HIST_CHAR ('!')
75 #define getcmd_putch(ch) putchar(ch)
76 #define getcmd_getch() my_getchar(1)
77 #define getcmd_cbeep() getcmd_putch('\a')
80 #define HIST_SIZE CONFIG_SYS_CBSIZE
83 static int hist_add_idx
;
84 static int hist_cur
= -1;
85 static unsigned hist_num
;
87 static char *hist_list
[HIST_MAX
];
88 static char hist_lines
[HIST_MAX
][HIST_SIZE
+ 1]; /* Save room for NULL */
90 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
92 static void hist_init(void)
101 for (i
= 0; i
< HIST_MAX
; i
++) {
102 hist_list
[i
] = hist_lines
[i
];
103 hist_list
[i
][0] = '\0';
107 static void cread_add_to_hist(char *line
)
109 strcpy(hist_list
[hist_add_idx
], line
);
111 if (++hist_add_idx
>= HIST_MAX
)
114 if (hist_add_idx
> hist_max
)
115 hist_max
= hist_add_idx
;
120 static char *hist_prev(void)
132 if (hist_cur
== hist_add_idx
) {
136 ret
= hist_list
[hist_cur
];
142 static char *hist_next(void)
149 if (hist_cur
== hist_add_idx
)
152 if (++hist_cur
> hist_max
)
155 if (hist_cur
== hist_add_idx
)
158 ret
= hist_list
[hist_cur
];
163 #ifndef CONFIG_CMDLINE_EDITING
164 static void cread_print_hist_list(void)
169 n
= hist_num
- hist_max
;
171 i
= hist_add_idx
+ 1;
175 if (i
== hist_add_idx
)
177 printf_P(PSTR("%s\n"), hist_list
[i
]);
182 #endif /* CONFIG_CMDLINE_EDITING */
184 #define BEGINNING_OF_LINE() { \
186 getcmd_putch(CTL_BACKSPACE); \
191 #define ERASE_TO_EOL() { \
192 if (num < eol_num) { \
193 printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \
195 getcmd_putch(CTL_BACKSPACE); \
196 } while (--eol_num > num); \
200 #define REFRESH_TO_EOL() { \
201 if (num < eol_num) { \
202 wlen = eol_num - num; \
203 putnstr(buf + num, wlen); \
208 static void cread_add_char(char ichar
, int insert
, unsigned long *num
,
209 unsigned long *eol_num
, char *buf
, unsigned long len
)
214 if (insert
|| *num
== *eol_num
) {
215 if (*eol_num
> len
- 1) {
223 wlen
= *eol_num
- *num
;
225 memmove(&buf
[*num
+1], &buf
[*num
], wlen
-1);
228 putnstr(buf
+ *num
, wlen
);
231 getcmd_putch(CTL_BACKSPACE
);
233 /* echo the character */
236 putnstr(buf
+ *num
, wlen
);
241 static void cread_add_str(char *str
, int strsize
, int insert
,
242 unsigned long *num
, unsigned long *eol_num
,
243 char *buf
, unsigned long len
)
246 cread_add_char(*str
, insert
, num
, eol_num
, buf
, len
);
251 static int cread_line(const FLASH
char *const prompt
, char *buf
, unsigned int *len
)
253 unsigned long num
= 0;
254 unsigned long eol_num
= 0;
260 int init_len
= strlen(buf
);
265 cread_add_str(buf
, init_len
, 1, &num
, &eol_num
, buf
, *len
);
268 ichar
= getcmd_getch();
270 if ((ichar
== '\n') || (ichar
== '\r')) {
276 * handle standard linux xterm esc sequences for arrow key, etc.
281 esc_save
[esc_len
] = ichar
;
284 cread_add_str(esc_save
, esc_len
,
285 insert
, &num
, &eol_num
,
293 case 'D': /* <- key */
297 case 'C': /* -> key */
300 break; /* pass off to ^F handler */
301 case 'H': /* Home key */
304 break; /* pass off to ^A handler */
305 case 'A': /* up arrow */
308 break; /* pass off to ^P handler */
309 case 'B': /* down arrow */
312 break; /* pass off to ^N handler */
314 esc_save
[esc_len
++] = ichar
;
315 cread_add_str(esc_save
, esc_len
, insert
,
316 &num
, &eol_num
, buf
, *len
);
325 esc_save
[esc_len
] = ichar
;
328 my_puts_P(PSTR("impossible condition #876\n"));
336 case CTL_CH('c'): /* ^C - break */
337 *buf
= '\0'; /* discard input */
341 getcmd_putch(buf
[num
]);
347 getcmd_putch(CTL_BACKSPACE
);
353 wlen
= eol_num
- num
- 1;
355 memmove(&buf
[num
], &buf
[num
+1], wlen
);
356 putnstr(buf
+ num
, wlen
);
361 getcmd_putch(CTL_BACKSPACE
);
384 wlen
= eol_num
- num
;
386 memmove(&buf
[num
], &buf
[num
+1], wlen
);
387 getcmd_putch(CTL_BACKSPACE
);
388 putnstr(buf
+ num
, wlen
);
391 getcmd_putch(CTL_BACKSPACE
);
403 if (ichar
== CTL_CH('p'))
413 /* nuke the current line */
417 /* erase to end of line */
420 /* copy new line into place and display */
422 eol_num
= strlen(buf
);
426 #ifdef CONFIG_AUTO_COMPLETE
430 /* do not autocomplete when in the middle */
437 col
= strlen_P(prompt
) + eol_num
;
439 if (cmd_auto_complete(prompt
, buf
, &num2
, &col
)) {
448 cread_add_char(ichar
, insert
, &num
, &eol_num
, buf
,
454 buf
[eol_num
] = '\0'; /* lose the newline */
456 if (buf
[0] && buf
[0] != CREAD_HIST_CHAR
)
457 cread_add_to_hist(buf
);
458 hist_cur
= hist_add_idx
;
463 #endif /* CONFIG_CMDLINE_EDITING */
465 /****************************************************************************/
467 static int cli_readline_into_buffer(const FLASH
char *const prompt
, char *buffer
)
470 #ifdef CONFIG_CMDLINE_EDITING
471 unsigned int len
= CONFIG_SYS_CBSIZE
;
483 rc
= cread_line(prompt
, p
, &len
);
484 return rc
< 0 ? rc
: (int) len
;
486 #else /* CONFIG_CMDLINE_EDITING */
488 int n
= 0; /* buffer index */
489 int plen
= 0; /* prompt length */
490 int col
; /* output column cnt */
495 plen
= strlen_P(prompt
);
505 * Special character handling
508 case '\r': /* Enter */
511 my_puts_P(PSTR("\r\n"));
517 case 0x03: /* ^C - break */
518 p_buf
[0] = '\0'; /* discard input */
521 case 0x15: /* ^U - erase line */
523 my_puts_P(erase_seq
);
530 case 0x17: /* ^W - erase word */
531 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
532 while ((n
> 0) && (*p
!= ' '))
533 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
536 case 0x08: /* ^H - backspace */
537 case 0x7F: /* DEL - backspace */
538 p
= delete_char(p_buf
, p
, &col
, &n
, plen
);
543 * Must be a normal character then
545 if (n
< CONFIG_SYS_CBSIZE
-2) {
546 if (c
== '\t') { /* expand TABs */
547 #ifdef CONFIG_AUTO_COMPLETE
549 * if auto completion triggered just
553 if (cmd_auto_complete(prompt
,
556 p
= p_buf
+ n
; /* reset */
560 my_puts_P(tab_seq
+ (col
& 07));
561 col
+= 8 - (col
& 07);
566 * Echo input using puts() to force an
567 * LCD flush if we are using an LCD
576 } else { /* Buffer full */
581 #endif /* CONFIG_CMDLINE_EDITING */
584 int cli_readline(const FLASH
char *const prompt
)
587 * If console_buffer isn't 0-length the user will be prompted to modify
588 * it instead of entering it from scratch as desired.
590 console_buffer
[0] = '\0';
592 return cli_readline_into_buffer(prompt
, console_buffer
);