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