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