]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cli_readline.c
Merge branch 'cmdline_edit'
[z180-stamp.git] / avr / cli_readline.c
CommitLineData
d684c216 1/*
35edb766
L
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
d684c216
L
4 * (C) Copyright 2000
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * Add to readline cmdline-editing by
8 * (C) Copyright 2005
9 * JinHua Luo, GuangDong Linux Center, <luo.jinhua@gd-linux.com>
10 *
11 * SPDX-License-Identifier: GPL-2.0+
12 */
13
14#include "common.h"
d684c216
L
15#include <string.h>
16#include <stdio.h>
a8b4c964
L
17#include <stdlib.h>
18#include <stdbool.h>
8506d791 19#include <ctype.h>
d684c216
L
20
21#include "config.h"
22#include "con-utils.h"
8591c65b 23#include "print-utils.h"
d684c216
L
24#include "command.h"
25#include "cli_readline.h"
26
8506d791
L
27
28
29/************************************************************************************************/
30/* TODO:
31 *
32 */
33
34#define ESC 0x1b
35
36#define KEY_TAB '\t' // TAB key
37#define KEY_CR '\r' // RETURN key
38#define KEY_BACKSPACE '\b' // Backspace key
39#define KEY_ESCAPE 0x1B // ESCAPE (pressed twice)
40
41#define KEY_DOWN 0x80 // Down arrow key
42#define KEY_UP 0x81 // Up arrow key
43#define KEY_LEFT 0x82 // Left arrow key
44#define KEY_RIGHT 0x83 // Right arrow key
45#define KEY_HOME 0x84 // Home key
46#define KEY_DC 0x85 // Delete character key
47#define KEY_IC 0x86 // Ins char/toggle ins mode key
48#define KEY_NPAGE 0x87 // Next-page key
49#define KEY_PPAGE 0x88 // Previous-page key
50#define KEY_END 0x89 // End key
51#define KEY_BTAB 0x8A // Back tab key
52#define KEY_F1 0x8B // Function key F1
53#define KEY_F(n) (KEY_F1+(n)-1) // Space for additional 12 function keys
54
55
56struct fkey_tbl_s {
57 const FLASH char *sequence; /* ESC Sequence */
58 int code; /* Keycode */
59};
60
61//typedef const FLASH struct fkey_tbl_s fkey_tbl_t;
62
63#define FKEY_TBL_ITEM(_seq, _code) { FSTR(#_seq), _code }
64
65
66
67static const FLASH struct fkey_tbl_s fkey_table[] = {
68
69FKEY_TBL_ITEM(B, KEY_DOWN), // Down arrow key
70FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key
71FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key
72FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key
73FKEY_TBL_ITEM(1~, KEY_HOME), // Home key
74FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key
75FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key
76FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key
77FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key
78FKEY_TBL_ITEM(4~, KEY_END), // End key
79FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key
80/* VT400: */
81FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
82FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
83FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
84FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
85FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
86/* Linux consoe */
87FKEY_TBL_ITEM([A, KEY_F(1)), // Function key F1
88FKEY_TBL_ITEM([B, KEY_F(2)), // Function key F2
89FKEY_TBL_ITEM([C, KEY_F(3)), // Function key F3
90FKEY_TBL_ITEM([D, KEY_F(4)), // Function key F4
91FKEY_TBL_ITEM([E, KEY_F(5)), // Function key F5
92
93FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
94FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
95FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
96FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
97FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
98FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
99FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
100{ NULL } /* Mark end of table */
101};
102
103
104
105typedef enum {
106 STATE_GROUND,
107 STATE_ESCAPE,
108 STATE_CSI_ENTRY
109} vtparse_state_t;
110
111#define CHB_SIZE 15
112
113static
114int vt_parse (void)
115{
116 static vtparse_state_t state = STATE_GROUND;
117 char buf[CHB_SIZE+1];
118 uint8_t param[2];
119 uint8_t i_buf;
120 uint8_t i_param;
121 int ch;
122
123
124 while (1) {
125 ch = my_getchar(1);
126// debug_getch(state, ch);
127
128 switch (state) {
129 case STATE_GROUND:
130 if (ch == ESC) {
131 state = STATE_ESCAPE;
132 continue;
133 }
134 if (ch == 0x7F) // BACKSPACE on VT200 sends DEL char
135 ch = KEY_BACKSPACE; // map it to '\b'
136 break;
137 case STATE_ESCAPE:
138 if (ch < 0)
139 continue;
140
141 if (ch == '[') {
142 state = STATE_CSI_ENTRY;
143 param[0] = param[1] = 0;
144 i_buf = 0;
145 i_param = 0;
146 continue;
147 }
148 state = STATE_GROUND;
149 break;
150 case STATE_CSI_ENTRY:
151 if (ch < 0)
152 continue;
153
154 buf[i_buf] = ch;
155 if (i_buf < CHB_SIZE)
156 i_buf++;
157 if (ch == ';') {
158 i_param++;
159 continue;
160 }
161 if (isdigit(ch)) {
162 if (i_param < 2)
163 param[i_param] = param[i_param] * 10 + ch - '0';
164 continue;
165 }
166 if (ch >= '@' && ch <= '~' && ch != '[') {
167 buf[i_buf] = '\0';
168 int_fast8_t i = 0;
169 while (fkey_table[i].sequence) {
170 if (! strcmp_P (buf, fkey_table[i].sequence)) {
171 ch = fkey_table[i].code;
172 break;
173 }
174 i++;
175 }
176 if (fkey_table[i].sequence == NULL) {
177 ch = '$'; /* KEY_ESCAPE; */
178 }
179 }
180 state = STATE_GROUND;
181 break;
182 }
183 break; /* while */
184 }
185
186 return ch;
187}
188
189/************************************************************************************************/
190
191
d684c216
L
192char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
193
8506d791 194#ifndef CONFIG_CMDLINE_EDITING
d684c216
L
195static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */
196static const FLASH char tab_seq[] = " "; /* used to expand TABs */
197
d684c216
L
198static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
199{
200 char *s;
201
202 if (*np == 0)
203 return p;
204
205 if (*(--p) == '\t') { /* will retype the whole line */
206 while (*colp > plen) {
207 my_puts_P(erase_seq);
208 (*colp)--;
209 }
210 for (s = buffer; s < p; ++s) {
211 if (*s == '\t') {
212 my_puts_P(tab_seq + ((*colp) & 07));
213 *colp += 8 - ((*colp) & 07);
214 } else {
215 ++(*colp);
216 putchar(*s);
217 }
218 }
219 } else {
220 my_puts_P(erase_seq);
221 (*colp)--;
222 }
223 (*np)--;
224
225 return p;
226}
227#endif /* CONFIG_CMDLINE_EDITING */
228
229
230#ifdef CONFIG_CMDLINE_EDITING
231
232/*
233 * cmdline-editing related codes from vivi.
234 * Author: Janghoon Lyu <nandy@mizi.com>
235 */
236
e1a50c19
L
237static void putnstr(char *str, int n)
238{
239 /* printf_P(PSTR("%.*s"), (int)n, str) */
8591c65b 240 while (n-- && *str)
e1a50c19
L
241 putchar(*str++);
242}
d684c216 243
e1a50c19
L
244
245#define CTL_CH(c) ((c) - 'a' + 1)
d684c216 246#define CTL_BACKSPACE ('\b')
e1a50c19
L
247#define DEL ((char)255)
248#define DEL7 ((char)127)
d684c216
L
249
250#define getcmd_putch(ch) putchar(ch)
8506d791 251#define getcmd_getch() vt_parse()
d684c216
L
252#define getcmd_cbeep() getcmd_putch('\a')
253
a8b4c964 254#define HIST_MAX 20
d684c216 255
a8b4c964
L
256static int_fast8_t hist_max;
257static int_fast8_t hist_add_idx;
258static int_fast8_t hist_cur = -1;
d684c216
L
259
260static char *hist_list[HIST_MAX];
d684c216
L
261
262static void hist_init(void)
263{
a8b4c964 264 int_fast8_t i;
d684c216
L
265
266 hist_max = 0;
267 hist_add_idx = 0;
268 hist_cur = -1;
d684c216
L
269
270 for (i = 0; i < HIST_MAX; i++) {
a8b4c964 271 hist_list[i] = NULL;
d684c216
L
272 }
273}
274
275static void cread_add_to_hist(char *line)
276{
a8b4c964
L
277 if (hist_max) {
278 int_fast8_t last = hist_add_idx;
279 if (--last < 0)
280 last = hist_max;
d684c216 281
a8b4c964
L
282 if (!strcmp(line, hist_list[last]))
283 return;
284 }
d684c216 285
a8b4c964
L
286 char *p = strdup(line);
287 if (p) {
288 free(hist_list[hist_add_idx]);
289 hist_list[hist_add_idx] = p;
d684c216 290
a8b4c964
L
291 if (++hist_add_idx >= HIST_MAX)
292 hist_add_idx = 0;
293
294 if (hist_add_idx > hist_max)
295 hist_max = hist_add_idx;
296 }
d684c216
L
297}
298
299static char *hist_prev(void)
300{
301 char *ret;
a8b4c964 302 int_fast8_t old_cur;
d684c216
L
303
304 if (hist_cur < 0)
305 return NULL;
306
307 old_cur = hist_cur;
308 if (--hist_cur < 0)
309 hist_cur = hist_max;
310
311 if (hist_cur == hist_add_idx) {
312 hist_cur = old_cur;
313 ret = NULL;
314 } else {
315 ret = hist_list[hist_cur];
316 }
317
318 return ret;
319}
320
321static char *hist_next(void)
322{
323 char *ret;
324
325 if (hist_cur < 0)
326 return NULL;
327
328 if (hist_cur == hist_add_idx)
329 return NULL;
330
331 if (++hist_cur > hist_max)
332 hist_cur = 0;
333
334 if (hist_cur == hist_add_idx)
335 ret = "";
336 else
337 ret = hist_list[hist_cur];
338
339 return ret;
340}
341
d684c216 342
e1a50c19
L
343#define BEGINNING_OF_LINE() { \
344 while (num) { \
345 getcmd_putch(CTL_BACKSPACE); \
346 num--; \
347 } \
d684c216
L
348}
349
e1a50c19
L
350#define ERASE_TO_EOL() { \
351 if (num < eol_num) { \
352 /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \
353 print_blanks(eol_num - num); \
354 do { \
355 getcmd_putch(CTL_BACKSPACE); \
356 } while (--eol_num > num); \
357 } \
d684c216
L
358}
359
e1a50c19
L
360#define REFRESH_TO_EOL() { \
361 if (num < eol_num) { \
362 wlen = eol_num - num; \
363 putnstr(buf + num, wlen); \
364 num = eol_num; \
365 } \
d684c216
L
366}
367
a8b4c964
L
368static void cread_add_char(char ichar, bool insert, uint_fast8_t *num,
369 uint_fast8_t *eol_num, char *buf, uint_fast8_t len)
d684c216 370{
a8b4c964 371 uint_fast8_t wlen;
d684c216
L
372
373 /* room ??? */
374 if (insert || *num == *eol_num) {
375 if (*eol_num > len - 1) {
376 getcmd_cbeep();
377 return;
378 }
379 (*eol_num)++;
380 }
381
382 if (insert) {
383 wlen = *eol_num - *num;
384 if (wlen > 1)
385 memmove(&buf[*num+1], &buf[*num], wlen-1);
386
387 buf[*num] = ichar;
388 putnstr(buf + *num, wlen);
389 (*num)++;
390 while (--wlen)
391 getcmd_putch(CTL_BACKSPACE);
392 } else {
393 /* echo the character */
394 wlen = 1;
395 buf[*num] = ichar;
396 putnstr(buf + *num, wlen);
397 (*num)++;
398 }
399}
400
a8b4c964
L
401static void cread_add_str(char *str, int strsize, bool insert,
402 uint_fast8_t *num, uint_fast8_t *eol_num,
403 char *buf, uint_fast8_t len)
d684c216
L
404{
405 while (strsize--) {
406 cread_add_char(*str, insert, num, eol_num, buf, len);
407 str++;
408 }
409}
410
a8b4c964 411static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len)
d684c216 412{
a8b4c964
L
413 uint_fast8_t num = 0;
414 uint_fast8_t eol_num = 0;
415 uint_fast8_t wlen;
8506d791 416 int ichar;
a8b4c964 417 bool insert = 1;
d684c216
L
418 int init_len = strlen(buf);
419
420 (void) prompt;
421
422 if (init_len)
423 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
424
425 while (1) {
426 ichar = getcmd_getch();
427
428 if ((ichar == '\n') || (ichar == '\r')) {
429 putchar('\n');
430 break;
431 }
432
d684c216
L
433
434 switch (ichar) {
d684c216 435
8506d791 436 case KEY_HOME:
d684c216
L
437 case CTL_CH('a'):
438 BEGINNING_OF_LINE();
439 break;
a8b4c964
L
440 case CTL_CH('c'): /* ^C - break */
441 *buf = '\0'; /* discard input */
d684c216 442 return -1;
8506d791 443 case KEY_RIGHT:
a8b4c964 444 case CTL_CH('f'): /* forward-char */
d684c216
L
445 if (num < eol_num) {
446 getcmd_putch(buf[num]);
447 num++;
448 }
449 break;
8506d791 450 case KEY_LEFT:
a8b4c964 451 case CTL_CH('b'): /* backward-char */
d684c216
L
452 if (num) {
453 getcmd_putch(CTL_BACKSPACE);
454 num--;
455 }
456 break;
8506d791 457 case KEY_DC:
a8b4c964 458 case CTL_CH('d'): /* delete-char */
d684c216
L
459 if (num < eol_num) {
460 wlen = eol_num - num - 1;
461 if (wlen) {
462 memmove(&buf[num], &buf[num+1], wlen);
463 putnstr(buf + num, wlen);
464 }
465
466 getcmd_putch(' ');
467 do {
468 getcmd_putch(CTL_BACKSPACE);
469 } while (wlen--);
470 eol_num--;
471 }
472 break;
a8b4c964 473 case CTL_CH('k'): /* kill-line */
d684c216
L
474 ERASE_TO_EOL();
475 break;
476 case CTL_CH('e'):
477 REFRESH_TO_EOL();
478 break;
8506d791 479 case KEY_IC:
d684c216
L
480 case CTL_CH('o'):
481 insert = !insert;
482 break;
483 case CTL_CH('x'):
a8b4c964 484 case CTL_CH('u'): /* kill-whole-line */
d684c216
L
485 BEGINNING_OF_LINE();
486 ERASE_TO_EOL();
487 break;
488 case DEL:
489 case DEL7:
a8b4c964 490 case 8: /* backward-delete-char */
d684c216
L
491 if (num) {
492 wlen = eol_num - num;
493 num--;
494 memmove(&buf[num], &buf[num+1], wlen);
495 getcmd_putch(CTL_BACKSPACE);
496 putnstr(buf + num, wlen);
497 getcmd_putch(' ');
498 do {
499 getcmd_putch(CTL_BACKSPACE);
500 } while (wlen--);
501 eol_num--;
502 }
503 break;
8506d791 504 case KEY_UP:
a8b4c964 505 case CTL_CH('p'): /* previous-history */
8506d791 506 case KEY_DOWN:
a8b4c964 507 case CTL_CH('n'): /* next-history */
d684c216
L
508 {
509 char *hline;
510
8506d791 511 if (ichar == CTL_CH('p') || ichar == KEY_UP)
d684c216
L
512 hline = hist_prev();
513 else
514 hline = hist_next();
515
516 if (!hline) {
517 getcmd_cbeep();
518 continue;
519 }
520
521 /* nuke the current line */
522 /* first, go home */
523 BEGINNING_OF_LINE();
524
525 /* erase to end of line */
526 ERASE_TO_EOL();
527
528 /* copy new line into place and display */
529 strcpy(buf, hline);
530 eol_num = strlen(buf);
531 REFRESH_TO_EOL();
532 continue;
533 }
534#ifdef CONFIG_AUTO_COMPLETE
535 case '\t': {
536 int num2, col;
537
538 /* do not autocomplete when in the middle */
539 if (num < eol_num) {
540 getcmd_cbeep();
541 break;
542 }
543
544 buf[num] = '\0';
545 col = strlen_P(prompt) + eol_num;
546 num2 = num;
547 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
548 col = num2 - num;
549 num += col;
550 eol_num += col;
551 }
552 break;
553 }
554#endif
555 default:
8506d791
L
556 if (isprint(ichar))
557 cread_add_char(ichar, insert, &num, &eol_num, buf,
558 *len);
d684c216
L
559 break;
560 }
561 }
562 *len = eol_num;
563 buf[eol_num] = '\0'; /* lose the newline */
564
8506d791 565 if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */)
d684c216
L
566 cread_add_to_hist(buf);
567 hist_cur = hist_add_idx;
568
569 return 0;
570}
571
572#endif /* CONFIG_CMDLINE_EDITING */
573
574/****************************************************************************/
575
576static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
577{
578 char *p = buffer;
579#ifdef CONFIG_CMDLINE_EDITING
a8b4c964 580 uint_fast8_t len = CONFIG_SYS_CBSIZE;
d684c216 581 int rc;
a8b4c964 582 static bool initted;
d684c216
L
583
584 if (!initted) {
585 hist_init();
586 initted = 1;
587 }
588
589 if (prompt)
590 my_puts_P(prompt);
591
592 rc = cread_line(prompt, p, &len);
593 return rc < 0 ? rc : (int) len;
594
595#else /* CONFIG_CMDLINE_EDITING */
596 char *p_buf = p;
597 int n = 0; /* buffer index */
598 int plen = 0; /* prompt length */
599 int col; /* output column cnt */
600 char c;
601
602 /* print prompt */
603 if (prompt) {
604 plen = strlen_P(prompt);
605 my_puts_P(prompt);
606 }
607 col = plen;
608
609 for (;;) {
610
424b184a 611 c = my_getchar(1);
d684c216
L
612
613 /*
614 * Special character handling
615 */
616 switch (c) {
617 case '\r': /* Enter */
618 case '\n':
619 *p = '\0';
620 my_puts_P(PSTR("\r\n"));
621 return p - p_buf;
622
623 case '\0': /* nul */
624 continue;
625
626 case 0x03: /* ^C - break */
627 p_buf[0] = '\0'; /* discard input */
628 return -1;
629
630 case 0x15: /* ^U - erase line */
631 while (col > plen) {
632 my_puts_P(erase_seq);
633 --col;
634 }
635 p = p_buf;
636 n = 0;
637 continue;
638
639 case 0x17: /* ^W - erase word */
640 p = delete_char(p_buf, p, &col, &n, plen);
641 while ((n > 0) && (*p != ' '))
642 p = delete_char(p_buf, p, &col, &n, plen);
643 continue;
644
645 case 0x08: /* ^H - backspace */
646 case 0x7F: /* DEL - backspace */
647 p = delete_char(p_buf, p, &col, &n, plen);
648 continue;
649
650 default:
651 /*
652 * Must be a normal character then
653 */
654 if (n < CONFIG_SYS_CBSIZE-2) {
655 if (c == '\t') { /* expand TABs */
656#ifdef CONFIG_AUTO_COMPLETE
657 /*
658 * if auto completion triggered just
659 * continue
660 */
661 *p = '\0';
662 if (cmd_auto_complete(prompt,
663 console_buffer,
664 &n, &col)) {
665 p = p_buf + n; /* reset */
666 continue;
667 }
668#endif
669 my_puts_P(tab_seq + (col & 07));
670 col += 8 - (col & 07);
671 } else {
d684c216 672 ++col;
cb52e1e0 673 putchar(c);
d684c216
L
674 }
675 *p++ = c;
676 ++n;
677 } else { /* Buffer full */
678 putchar('\a');
679 }
680 }
681 }
682#endif /* CONFIG_CMDLINE_EDITING */
683}
684
685int cli_readline(const FLASH char *const prompt)
686{
687 /*
688 * If console_buffer isn't 0-length the user will be prompted to modify
689 * it instead of entering it from scratch as desired.
690 */
691 console_buffer[0] = '\0';
692
693 return cli_readline_into_buffer(prompt, console_buffer);
694}