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