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