]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
clean up includes
[z180-stamp.git] / avr / cli_readline.c
1 /*
2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
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 "cli_readline.h"
15 #include "common.h"
16
17 #include "config.h"
18 #include "con-utils.h"
19 #include "print-utils.h"
20 #include "command.h"
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)
28
29
30
31 char 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
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
66 struct 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
77 static const FLASH struct fkey_tbl_s fkey_table[] = {
78
79 FKEY_TBL_ITEM(B, KEY_DOWN), // Down arrow key
80 FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key
81 FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key
82 FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key
83 FKEY_TBL_ITEM(1~, KEY_HOME), // Home key
84 FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key
85 FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key
86 FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key
87 FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key
88 FKEY_TBL_ITEM(4~, KEY_END), // End key
89 FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key
90 /* */
91 FKEY_TBL_ITEM(H, KEY_HOME), // Home key
92 FKEY_TBL_ITEM(F, KEY_END), // End key
93 /* VT400: */
94 FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1
95 FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2
96 FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3
97 FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4
98 FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5
99 /* Linux console */
100 FKEY_TBL_ITEM([A, KEY_F(1)), // Function key F1
101 FKEY_TBL_ITEM([B, KEY_F(2)), // Function key F2
102 FKEY_TBL_ITEM([C, KEY_F(3)), // Function key F3
103 FKEY_TBL_ITEM([D, KEY_F(4)), // Function key F4
104 FKEY_TBL_ITEM([E, KEY_F(5)), // Function key F5
105
106 FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6
107 FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7
108 FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8
109 FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9
110 FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10
111 FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11
112 FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12
113 { NULL } /* Mark end of table */
114 };
115
116
117
118 typedef enum {
119 STATE_GROUND,
120 STATE_ESCAPE,
121 STATE_CSI_ENTRY,
122 STATE_SS3
123 } vtparse_state_t;
124
125 #define CHB_SIZE 15
126
127 static
128 int vt_parse (void)
129 {
130 static vtparse_state_t state = STATE_GROUND;
131 char buf[CHB_SIZE+1];
132 uint8_t param[2];
133 uint8_t i_buf = 0;
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 }
162 if (ch == 'O') {
163 state = STATE_SS3;
164 continue;
165 }
166 state = STATE_GROUND;
167 break;
168 case STATE_SS3:
169 if (ch == 'F')
170 ch = KEY_END;
171 if (ch == 'H')
172 ch = KEY_HOME;
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
216 /*
217 * cmdline-editing related codes from vivi.
218 * Author: Janghoon Lyu <nandy@mizi.com>
219 */
220
221
222 char histbuf[CONFIG_SYS_HISTSIZE+1];
223 #define HISTBUFE (histbuf+CONFIG_SYS_HISTSIZE)
224
225 static char *hist_head;
226 static char *hist_cur;
227
228 static void hist_reset(void)
229 {
230 if (hist_head == NULL)
231 hist_head = HISTBUFE;
232 hist_cur = hist_head;
233 }
234
235 static char *hist_entry_next(char *p)
236 {
237 if (*p)
238 while (*p++);
239
240 return p;
241 }
242
243 static char *hist_entry_prev(char *p)
244 {
245 if (p == hist_head)
246 return NULL;
247
248 --p;
249 while (p != hist_head && *(p-1) != 0)
250 --p;
251
252 return p;
253 }
254
255 static char *hist_search_entry(char *line)
256 {
257 char *p = hist_head;
258
259 while (*p && strcmp(p, line))
260 p = hist_entry_next(p);
261 return *p ? p : NULL;
262 }
263
264 static char *hist_delete(size_t amount)
265 {
266 char *p;
267
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);
281 }
282 return hist_head;
283 }
284
285 static char *hist_delete_entry(char *entry)
286 {
287 size_t shift = strlen(entry) + 1;
288 size_t len = entry - hist_head;
289
290 hist_head = memmove(hist_head + shift, hist_head, len);
291
292 return hist_head;
293 }
294
295
296 static char *hist_add_entry(char *line)
297 {
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
314 static uint_fast8_t hist_get_count(void)
315 {
316 char *p = hist_head;
317 uint_fast8_t n = 0;
318
319 while (*p) {
320 ++n;
321 while (*p++);
322 }
323 return n;
324 }
325
326 static int hist_get_size(void)
327 {
328 return HISTBUFE - hist_head;
329 }
330
331 static char *cread_add_to_hist(char *line)
332 {
333 char *p;
334
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);
342 }
343 hist_add_entry(line);
344
345 return p;
346 }
347
348 static char *hist_prev(void)
349 {
350 char *p = hist_cur;
351
352 if (*p == '\0')
353 return NULL;
354 hist_cur = hist_entry_next(p);
355
356 return p;
357 }
358
359 static char *hist_next(void)
360 {
361 char *p = hist_cur;
362
363 if(p == hist_head)
364 return NULL;
365
366 p = hist_entry_prev(p);
367 hist_cur = p;
368
369 return p == hist_head ? "" : hist_entry_prev(p);
370 }
371
372 static char *hist_search_backward(char* buf, uint8_t num)
373 {
374 char *p = hist_cur;
375
376 if (*p == '\0')
377 return NULL;
378
379 while (*p && strncmp(p, buf, num))
380 p = hist_entry_next(p);
381
382 if(!strncmp(p, buf, num)) {
383 hist_cur = hist_entry_next(p);
384 return p;
385 }
386
387 return NULL;
388 }
389
390 static char *hist_search_forward (char* buf, uint8_t num)
391 {
392 char *p = hist_cur;
393
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);
398
399 //if(!strncmp(p, buf, num)) {
400 if(p) {
401 hist_cur = hist_entry_next(p);
402 return p;
403 }
404 }
405
406 return NULL;
407 }
408
409 static void putnstr(char *str, int n)
410 {
411 /* printf_P(PSTR("%.*s"), (int)n, str) */
412 while (n-- && *str)
413 putchar(*str++);
414 }
415
416 static void getcmd_putch(int ch) { putchar(ch);}
417 static int getcmd_getch(void) { return vt_parse();}
418 static void getcmd_cbeep(void) { getcmd_putch('\a');}
419
420 static void beginning_of_line(uint8_t *num)
421 {
422 while (*num) {
423 getcmd_putch(CTL_BACKSPACE);
424 (*num)--;
425 }
426 }
427
428 static 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
439 static 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 }
446 }
447
448 static void cread_add_char(char ichar, bool insert, uint_fast8_t *num,
449 uint_fast8_t *eol_num, char *buf, uint_fast8_t len)
450 {
451 uint_fast8_t wlen;
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 */
474 buf[*num] = ichar;
475 putnstr(buf + *num, 1);
476 (*num)++;
477 }
478 }
479
480 static void cread_add_str(char *str, bool insert, uint_fast8_t *num,
481 uint_fast8_t *eol_num, char *buf, uint_fast8_t len)
482 {
483 char c;
484
485 while ((c = *str++) != '\0')
486 cread_add_char(c, insert, num, eol_num, buf, len);
487 }
488
489 static int cread_line(const FLASH char *const prompt, char *buf,
490 uint_fast8_t len, bool enable_history)
491 {
492 uint_fast8_t num = 0;
493 uint_fast8_t eol_num = 0;
494 bool insert = 1;
495
496 (void) prompt;
497
498 if (buf[0])
499 cread_add_str(buf, 1, &num, &eol_num, buf, len);
500
501 hist_reset();
502
503 while (1) {
504 int ichar = getcmd_getch();
505
506 if ((ichar == '\n') || (ichar == '\r')) {
507 putchar('\n');
508 break;
509 }
510
511
512 switch (ichar) {
513
514 case KEY_HOME:
515 case CTL_CH('a'):
516 beginning_of_line(&num);
517 break;
518 case CTL_CH('c'): /* ^C - break */
519 putchar('\n');
520 *buf = '\0'; /* discard input */
521 return -1;
522 case KEY_RIGHT:
523 case CTL_CH('f'): /* forward-char */
524 if (num < eol_num) {
525 getcmd_putch(buf[num]);
526 num++;
527 }
528 break;
529 case KEY_LEFT:
530 case CTL_CH('b'): /* backward-char */
531 if (num) {
532 getcmd_putch(CTL_BACKSPACE);
533 num--;
534 }
535 break;
536 case KEY_DC:
537 case CTL_CH('d'): /* delete-char */
538 if (num < eol_num) {
539 uint_fast8_t wlen = eol_num - num - 1;
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;
552 case CTL_CH('k'): /* kill-line */
553 erase_to_eol(&num, &eol_num);
554 break;
555 case KEY_END:
556 case CTL_CH('e'):
557 refresh_to_eol(buf, &num, &eol_num);
558 break;
559 case KEY_IC:
560 case CTL_CH('o'):
561 insert = !insert;
562 break;
563 case CTL_CH('x'):
564 case CTL_CH('u'): /* kill-whole-line */
565 beginning_of_line(&num);
566 erase_to_eol(&num, &eol_num);
567 break;
568 case DEL:
569 case DEL7:
570 case 8: /* backward-delete-char */
571 if (num) {
572 uint_fast8_t wlen = eol_num - --num;
573 buf[eol_num] = ' ';
574 memmove(&buf[num], &buf[num+1], wlen);
575 getcmd_putch(CTL_BACKSPACE);
576 putnstr(buf + num, wlen);
577 do {
578 getcmd_putch(CTL_BACKSPACE);
579 } while (--wlen);
580 eol_num--;
581 }
582 break;
583 case KEY_UP:
584 case CTL_CH('p'): /* previous-history */
585 case KEY_DOWN:
586 case CTL_CH('n'): /* next-history */
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) {
596 /* first, go home */
597 beginning_of_line(&num);
598 /* overwrite current line */
599 cread_add_str(hline, 0, &num, &eol_num, buf, len);
600 /* erase to end of line */
601 erase_to_eol(&num, &eol_num);
602
603 } else {
604 getcmd_cbeep();
605 }
606 } else {
607 getcmd_cbeep();
608 }
609 break;
610 case KEY_PPAGE: /* history-search-backward */
611 case KEY_NPAGE: /* history-search-forward */
612 if (enable_history) {
613 char *hline;
614 if (ichar == KEY_PPAGE)
615 hline = hist_search_backward(buf, num);
616 else
617 hline = hist_search_forward(buf, num);
618
619 if (hline) {
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);
623 /* erase to end of line */
624 erase_to_eol(&num2, &eol_num);
625 /* cursor back */
626 while (num2-- > num)
627 getcmd_putch(CTL_BACKSPACE);
628 } else {
629 getcmd_cbeep();
630 }
631 } else {
632 getcmd_cbeep();
633 }
634 break;
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:
657 if (isprint(ichar))
658 cread_add_char(ichar, insert, &num, &eol_num, buf, len);
659 break;
660 }
661 }
662 while (eol_num && buf[eol_num-1] == ' ')
663 --eol_num; /* remove trailing blanks */
664 buf[eol_num] = '\0'; /* lose the newline */
665
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]) {
676 cread_add_to_hist(buf);
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 }
680 return eol_num;
681 }
682
683 /****************************************************************************/
684
685 int cli_readline(const FLASH char *const prompt, bool enable_history)
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 if (prompt)
694 my_puts_P(prompt);
695
696 return cread_line(prompt, console_buffer, CONFIG_SYS_CBSIZE, enable_history);
697 }