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