]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cli_readline.c
change common base from C000 to F000. loadcpm3 bugfixes
[z180-stamp.git] / avr / cli_readline.c
1 /*
2 * (C) Copyright 2014 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 "common.h"
15 #include <string.h>
16 #include <stdio.h>
17
18 #include "config.h"
19 #include "con-utils.h"
20 #include "command.h"
21 #include "cli_readline.h"
22
23 char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */
24
25 static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */
26 static const FLASH char tab_seq[] = " "; /* used to expand TABs */
27
28 #ifndef CONFIG_CMDLINE_EDITING
29 static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen)
30 {
31 char *s;
32
33 if (*np == 0)
34 return p;
35
36 if (*(--p) == '\t') { /* will retype the whole line */
37 while (*colp > plen) {
38 my_puts_P(erase_seq);
39 (*colp)--;
40 }
41 for (s = buffer; s < p; ++s) {
42 if (*s == '\t') {
43 my_puts_P(tab_seq + ((*colp) & 07));
44 *colp += 8 - ((*colp) & 07);
45 } else {
46 ++(*colp);
47 putchar(*s);
48 }
49 }
50 } else {
51 my_puts_P(erase_seq);
52 (*colp)--;
53 }
54 (*np)--;
55
56 return p;
57 }
58 #endif /* CONFIG_CMDLINE_EDITING */
59
60
61 #ifdef CONFIG_CMDLINE_EDITING
62
63 /*
64 * cmdline-editing related codes from vivi.
65 * Author: Janghoon Lyu <nandy@mizi.com>
66 */
67
68 #define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str)
69
70 #define CTL_CH(c) ((c) - 'a' + 1)
71 #define CTL_BACKSPACE ('\b')
72 #define DEL ((char)255)
73 #define DEL7 ((char)127)
74 #define CREAD_HIST_CHAR ('!')
75
76 #define getcmd_putch(ch) putchar(ch)
77 #define getcmd_getch() my_getchar(1)
78 #define getcmd_cbeep() getcmd_putch('\a')
79
80 #define HIST_MAX 5
81 #define HIST_SIZE CONFIG_SYS_CBSIZE
82
83 static int hist_max;
84 static int hist_add_idx;
85 static int hist_cur = -1;
86 static unsigned hist_num;
87
88 static char *hist_list[HIST_MAX];
89 static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */
90
91 #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1)
92
93 static void hist_init(void)
94 {
95 int i;
96
97 hist_max = 0;
98 hist_add_idx = 0;
99 hist_cur = -1;
100 hist_num = 0;
101
102 for (i = 0; i < HIST_MAX; i++) {
103 hist_list[i] = hist_lines[i];
104 hist_list[i][0] = '\0';
105 }
106 }
107
108 static void cread_add_to_hist(char *line)
109 {
110 strcpy(hist_list[hist_add_idx], line);
111
112 if (++hist_add_idx >= HIST_MAX)
113 hist_add_idx = 0;
114
115 if (hist_add_idx > hist_max)
116 hist_max = hist_add_idx;
117
118 hist_num++;
119 }
120
121 static char *hist_prev(void)
122 {
123 char *ret;
124 int old_cur;
125
126 if (hist_cur < 0)
127 return NULL;
128
129 old_cur = hist_cur;
130 if (--hist_cur < 0)
131 hist_cur = hist_max;
132
133 if (hist_cur == hist_add_idx) {
134 hist_cur = old_cur;
135 ret = NULL;
136 } else {
137 ret = hist_list[hist_cur];
138 }
139
140 return ret;
141 }
142
143 static char *hist_next(void)
144 {
145 char *ret;
146
147 if (hist_cur < 0)
148 return NULL;
149
150 if (hist_cur == hist_add_idx)
151 return NULL;
152
153 if (++hist_cur > hist_max)
154 hist_cur = 0;
155
156 if (hist_cur == hist_add_idx)
157 ret = "";
158 else
159 ret = hist_list[hist_cur];
160
161 return ret;
162 }
163
164 #ifndef CONFIG_CMDLINE_EDITING
165 static void cread_print_hist_list(void)
166 {
167 int i;
168 unsigned n;
169
170 n = hist_num - hist_max;
171
172 i = hist_add_idx + 1;
173 while (1) {
174 if (i > hist_max)
175 i = 0;
176 if (i == hist_add_idx)
177 break;
178 printf_P(PSTR("%s\n"), hist_list[i]);
179 n++;
180 i++;
181 }
182 }
183 #endif /* CONFIG_CMDLINE_EDITING */
184
185 #define BEGINNING_OF_LINE() { \
186 while (num) { \
187 getcmd_putch(CTL_BACKSPACE); \
188 num--; \
189 } \
190 }
191
192 #define ERASE_TO_EOL() { \
193 if (num < eol_num) { \
194 printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \
195 do { \
196 getcmd_putch(CTL_BACKSPACE); \
197 } while (--eol_num > num); \
198 } \
199 }
200
201 #define REFRESH_TO_EOL() { \
202 if (num < eol_num) { \
203 wlen = eol_num - num; \
204 putnstr(buf + num, wlen); \
205 num = eol_num; \
206 } \
207 }
208
209 static void cread_add_char(char ichar, int insert, unsigned long *num,
210 unsigned long *eol_num, char *buf, unsigned long len)
211 {
212 unsigned long wlen;
213
214 /* room ??? */
215 if (insert || *num == *eol_num) {
216 if (*eol_num > len - 1) {
217 getcmd_cbeep();
218 return;
219 }
220 (*eol_num)++;
221 }
222
223 if (insert) {
224 wlen = *eol_num - *num;
225 if (wlen > 1)
226 memmove(&buf[*num+1], &buf[*num], wlen-1);
227
228 buf[*num] = ichar;
229 putnstr(buf + *num, wlen);
230 (*num)++;
231 while (--wlen)
232 getcmd_putch(CTL_BACKSPACE);
233 } else {
234 /* echo the character */
235 wlen = 1;
236 buf[*num] = ichar;
237 putnstr(buf + *num, wlen);
238 (*num)++;
239 }
240 }
241
242 static void cread_add_str(char *str, int strsize, int insert,
243 unsigned long *num, unsigned long *eol_num,
244 char *buf, unsigned long len)
245 {
246 while (strsize--) {
247 cread_add_char(*str, insert, num, eol_num, buf, len);
248 str++;
249 }
250 }
251
252 static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *len)
253 {
254 unsigned long num = 0;
255 unsigned long eol_num = 0;
256 unsigned long wlen;
257 char ichar;
258 int insert = 1;
259 int esc_len = 0;
260 char esc_save[8];
261 int init_len = strlen(buf);
262
263 (void) prompt;
264
265 if (init_len)
266 cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len);
267
268 while (1) {
269 ichar = getcmd_getch();
270
271 if ((ichar == '\n') || (ichar == '\r')) {
272 putchar('\n');
273 break;
274 }
275
276 /*
277 * handle standard linux xterm esc sequences for arrow key, etc.
278 */
279 if (esc_len != 0) {
280 if (esc_len == 1) {
281 if (ichar == '[') {
282 esc_save[esc_len] = ichar;
283 esc_len = 2;
284 } else {
285 cread_add_str(esc_save, esc_len,
286 insert, &num, &eol_num,
287 buf, *len);
288 esc_len = 0;
289 }
290 continue;
291 }
292
293 switch (ichar) {
294 case 'D': /* <- key */
295 ichar = CTL_CH('b');
296 esc_len = 0;
297 break;
298 case 'C': /* -> key */
299 ichar = CTL_CH('f');
300 esc_len = 0;
301 break; /* pass off to ^F handler */
302 case 'H': /* Home key */
303 ichar = CTL_CH('a');
304 esc_len = 0;
305 break; /* pass off to ^A handler */
306 case 'A': /* up arrow */
307 ichar = CTL_CH('p');
308 esc_len = 0;
309 break; /* pass off to ^P handler */
310 case 'B': /* down arrow */
311 ichar = CTL_CH('n');
312 esc_len = 0;
313 break; /* pass off to ^N handler */
314 default:
315 esc_save[esc_len++] = ichar;
316 cread_add_str(esc_save, esc_len, insert,
317 &num, &eol_num, buf, *len);
318 esc_len = 0;
319 continue;
320 }
321 }
322
323 switch (ichar) {
324 case 0x1b:
325 if (esc_len == 0) {
326 esc_save[esc_len] = ichar;
327 esc_len = 1;
328 } else {
329 my_puts_P(PSTR("impossible condition #876\n"));
330 esc_len = 0;
331 }
332 break;
333
334 case CTL_CH('a'):
335 BEGINNING_OF_LINE();
336 break;
337 case CTL_CH('c'): /* ^C - break */
338 *buf = '\0'; /* discard input */
339 return -1;
340 case CTL_CH('f'):
341 if (num < eol_num) {
342 getcmd_putch(buf[num]);
343 num++;
344 }
345 break;
346 case CTL_CH('b'):
347 if (num) {
348 getcmd_putch(CTL_BACKSPACE);
349 num--;
350 }
351 break;
352 case CTL_CH('d'):
353 if (num < eol_num) {
354 wlen = eol_num - num - 1;
355 if (wlen) {
356 memmove(&buf[num], &buf[num+1], wlen);
357 putnstr(buf + num, wlen);
358 }
359
360 getcmd_putch(' ');
361 do {
362 getcmd_putch(CTL_BACKSPACE);
363 } while (wlen--);
364 eol_num--;
365 }
366 break;
367 case CTL_CH('k'):
368 ERASE_TO_EOL();
369 break;
370 case CTL_CH('e'):
371 REFRESH_TO_EOL();
372 break;
373 case CTL_CH('o'):
374 insert = !insert;
375 break;
376 case CTL_CH('x'):
377 case CTL_CH('u'):
378 BEGINNING_OF_LINE();
379 ERASE_TO_EOL();
380 break;
381 case DEL:
382 case DEL7:
383 case 8:
384 if (num) {
385 wlen = eol_num - num;
386 num--;
387 memmove(&buf[num], &buf[num+1], wlen);
388 getcmd_putch(CTL_BACKSPACE);
389 putnstr(buf + num, wlen);
390 getcmd_putch(' ');
391 do {
392 getcmd_putch(CTL_BACKSPACE);
393 } while (wlen--);
394 eol_num--;
395 }
396 break;
397 case CTL_CH('p'):
398 case CTL_CH('n'):
399 {
400 char *hline;
401
402 esc_len = 0;
403
404 if (ichar == CTL_CH('p'))
405 hline = hist_prev();
406 else
407 hline = hist_next();
408
409 if (!hline) {
410 getcmd_cbeep();
411 continue;
412 }
413
414 /* nuke the current line */
415 /* first, go home */
416 BEGINNING_OF_LINE();
417
418 /* erase to end of line */
419 ERASE_TO_EOL();
420
421 /* copy new line into place and display */
422 strcpy(buf, hline);
423 eol_num = strlen(buf);
424 REFRESH_TO_EOL();
425 continue;
426 }
427 #ifdef CONFIG_AUTO_COMPLETE
428 case '\t': {
429 int num2, col;
430
431 /* do not autocomplete when in the middle */
432 if (num < eol_num) {
433 getcmd_cbeep();
434 break;
435 }
436
437 buf[num] = '\0';
438 col = strlen_P(prompt) + eol_num;
439 num2 = num;
440 if (cmd_auto_complete(prompt, buf, &num2, &col)) {
441 col = num2 - num;
442 num += col;
443 eol_num += col;
444 }
445 break;
446 }
447 #endif
448 default:
449 cread_add_char(ichar, insert, &num, &eol_num, buf,
450 *len);
451 break;
452 }
453 }
454 *len = eol_num;
455 buf[eol_num] = '\0'; /* lose the newline */
456
457 if (buf[0] && buf[0] != CREAD_HIST_CHAR)
458 cread_add_to_hist(buf);
459 hist_cur = hist_add_idx;
460
461 return 0;
462 }
463
464 #endif /* CONFIG_CMDLINE_EDITING */
465
466 /****************************************************************************/
467
468 static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer)
469 {
470 char *p = buffer;
471 #ifdef CONFIG_CMDLINE_EDITING
472 unsigned int len = CONFIG_SYS_CBSIZE;
473 int rc;
474 static int initted;
475
476 if (!initted) {
477 hist_init();
478 initted = 1;
479 }
480
481 if (prompt)
482 my_puts_P(prompt);
483
484 rc = cread_line(prompt, p, &len);
485 return rc < 0 ? rc : (int) len;
486
487 #else /* CONFIG_CMDLINE_EDITING */
488 char *p_buf = p;
489 int n = 0; /* buffer index */
490 int plen = 0; /* prompt length */
491 int col; /* output column cnt */
492 char c;
493
494 /* print prompt */
495 if (prompt) {
496 plen = strlen_P(prompt);
497 my_puts_P(prompt);
498 }
499 col = plen;
500
501 for (;;) {
502
503 c = my_getchar(1);
504
505 /*
506 * Special character handling
507 */
508 switch (c) {
509 case '\r': /* Enter */
510 case '\n':
511 *p = '\0';
512 my_puts_P(PSTR("\r\n"));
513 return p - p_buf;
514
515 case '\0': /* nul */
516 continue;
517
518 case 0x03: /* ^C - break */
519 p_buf[0] = '\0'; /* discard input */
520 return -1;
521
522 case 0x15: /* ^U - erase line */
523 while (col > plen) {
524 my_puts_P(erase_seq);
525 --col;
526 }
527 p = p_buf;
528 n = 0;
529 continue;
530
531 case 0x17: /* ^W - erase word */
532 p = delete_char(p_buf, p, &col, &n, plen);
533 while ((n > 0) && (*p != ' '))
534 p = delete_char(p_buf, p, &col, &n, plen);
535 continue;
536
537 case 0x08: /* ^H - backspace */
538 case 0x7F: /* DEL - backspace */
539 p = delete_char(p_buf, p, &col, &n, plen);
540 continue;
541
542 default:
543 /*
544 * Must be a normal character then
545 */
546 if (n < CONFIG_SYS_CBSIZE-2) {
547 if (c == '\t') { /* expand TABs */
548 #ifdef CONFIG_AUTO_COMPLETE
549 /*
550 * if auto completion triggered just
551 * continue
552 */
553 *p = '\0';
554 if (cmd_auto_complete(prompt,
555 console_buffer,
556 &n, &col)) {
557 p = p_buf + n; /* reset */
558 continue;
559 }
560 #endif
561 my_puts_P(tab_seq + (col & 07));
562 col += 8 - (col & 07);
563 } else {
564 ++col;
565 putchar(c);
566 }
567 *p++ = c;
568 ++n;
569 } else { /* Buffer full */
570 putchar('\a');
571 }
572 }
573 }
574 #endif /* CONFIG_CMDLINE_EDITING */
575 }
576
577 int cli_readline(const FLASH char *const prompt)
578 {
579 /*
580 * If console_buffer isn't 0-length the user will be prompted to modify
581 * it instead of entering it from scratch as desired.
582 */
583 console_buffer[0] = '\0';
584
585 return cli_readline_into_buffer(prompt, console_buffer);
586 }