]>
Commit | Line | Data |
---|---|---|
d684c216 | 1 | /* |
35edb766 L |
2 | * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de> |
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 | ||
14 | #include "common.h" | |
d684c216 L |
15 | #include <string.h> |
16 | #include <stdio.h> | |
a8b4c964 L |
17 | #include <stdlib.h> |
18 | #include <stdbool.h> | |
8506d791 | 19 | #include <ctype.h> |
d684c216 L |
20 | |
21 | #include "config.h" | |
22 | #include "con-utils.h" | |
8591c65b | 23 | #include "print-utils.h" |
d684c216 L |
24 | #include "command.h" |
25 | #include "cli_readline.h" | |
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 | ||
56 | struct 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 | ||
67 | static const FLASH struct fkey_tbl_s fkey_table[] = { | |
68 | ||
69 | FKEY_TBL_ITEM(B, KEY_DOWN), // Down arrow key | |
70 | FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key | |
71 | FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key | |
72 | FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key | |
73 | FKEY_TBL_ITEM(1~, KEY_HOME), // Home key | |
74 | FKEY_TBL_ITEM(3~, KEY_DC), // Delete character key | |
75 | FKEY_TBL_ITEM(2~, KEY_IC), // Ins char/toggle ins mode key | |
76 | FKEY_TBL_ITEM(6~, KEY_NPAGE), // Next-page key | |
77 | FKEY_TBL_ITEM(5~, KEY_PPAGE), // Previous-page key | |
78 | FKEY_TBL_ITEM(4~, KEY_END), // End key | |
79 | FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key | |
80 | /* VT400: */ | |
81 | FKEY_TBL_ITEM(11~, KEY_F(1)), // Function key F1 | |
82 | FKEY_TBL_ITEM(12~, KEY_F(2)), // Function key F2 | |
83 | FKEY_TBL_ITEM(13~, KEY_F(3)), // Function key F3 | |
84 | FKEY_TBL_ITEM(14~, KEY_F(4)), // Function key F4 | |
85 | FKEY_TBL_ITEM(15~, KEY_F(5)), // Function key F5 | |
86 | /* Linux consoe */ | |
87 | FKEY_TBL_ITEM([A, KEY_F(1)), // Function key F1 | |
88 | FKEY_TBL_ITEM([B, KEY_F(2)), // Function key F2 | |
89 | FKEY_TBL_ITEM([C, KEY_F(3)), // Function key F3 | |
90 | FKEY_TBL_ITEM([D, KEY_F(4)), // Function key F4 | |
91 | FKEY_TBL_ITEM([E, KEY_F(5)), // Function key F5 | |
92 | ||
93 | FKEY_TBL_ITEM(17~, KEY_F(6)), // Function key F6 | |
94 | FKEY_TBL_ITEM(18~, KEY_F(7)), // Function key F7 | |
95 | FKEY_TBL_ITEM(19~, KEY_F(8)), // Function key F8 | |
96 | FKEY_TBL_ITEM(20~, KEY_F(9)), // Function key F9 | |
97 | FKEY_TBL_ITEM(21~, KEY_F(10)), // Function key F10 | |
98 | FKEY_TBL_ITEM(23~, KEY_F(11)), // Function key F11 | |
99 | FKEY_TBL_ITEM(24~, KEY_F(12)), // Function key F12 | |
100 | { NULL } /* Mark end of table */ | |
101 | }; | |
102 | ||
103 | ||
104 | ||
105 | typedef enum { | |
106 | STATE_GROUND, | |
107 | STATE_ESCAPE, | |
108 | STATE_CSI_ENTRY | |
109 | } vtparse_state_t; | |
110 | ||
111 | #define CHB_SIZE 15 | |
112 | ||
113 | static | |
114 | int 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 |
192 | char 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 |
199 | static 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 | ||
a8b4c964 | 216 | #define HIST_MAX 20 |
d684c216 | 217 | |
a8b4c964 L |
218 | static int_fast8_t hist_max; |
219 | static int_fast8_t hist_add_idx; | |
220 | static int_fast8_t hist_cur = -1; | |
d684c216 L |
221 | |
222 | static char *hist_list[HIST_MAX]; | |
d684c216 | 223 | |
d684c216 L |
224 | static void cread_add_to_hist(char *line) |
225 | { | |
a8b4c964 L |
226 | if (hist_max) { |
227 | int_fast8_t last = hist_add_idx; | |
228 | if (--last < 0) | |
229 | last = hist_max; | |
d684c216 | 230 | |
a8b4c964 L |
231 | if (!strcmp(line, hist_list[last])) |
232 | return; | |
233 | } | |
d684c216 | 234 | |
a8b4c964 L |
235 | char *p = strdup(line); |
236 | if (p) { | |
237 | free(hist_list[hist_add_idx]); | |
238 | hist_list[hist_add_idx] = p; | |
d684c216 | 239 | |
a8b4c964 L |
240 | if (++hist_add_idx >= HIST_MAX) |
241 | hist_add_idx = 0; | |
242 | ||
243 | if (hist_add_idx > hist_max) | |
244 | hist_max = hist_add_idx; | |
245 | } | |
d684c216 L |
246 | } |
247 | ||
248 | static char *hist_prev(void) | |
249 | { | |
250 | char *ret; | |
a8b4c964 | 251 | int_fast8_t old_cur; |
d684c216 L |
252 | |
253 | if (hist_cur < 0) | |
254 | return NULL; | |
255 | ||
256 | old_cur = hist_cur; | |
257 | if (--hist_cur < 0) | |
258 | hist_cur = hist_max; | |
259 | ||
260 | if (hist_cur == hist_add_idx) { | |
261 | hist_cur = old_cur; | |
262 | ret = NULL; | |
263 | } else { | |
264 | ret = hist_list[hist_cur]; | |
265 | } | |
266 | ||
267 | return ret; | |
268 | } | |
269 | ||
270 | static char *hist_next(void) | |
271 | { | |
272 | char *ret; | |
273 | ||
274 | if (hist_cur < 0) | |
275 | return NULL; | |
276 | ||
277 | if (hist_cur == hist_add_idx) | |
278 | return NULL; | |
279 | ||
280 | if (++hist_cur > hist_max) | |
281 | hist_cur = 0; | |
282 | ||
283 | if (hist_cur == hist_add_idx) | |
284 | ret = ""; | |
285 | else | |
286 | ret = hist_list[hist_cur]; | |
287 | ||
288 | return ret; | |
289 | } | |
290 | ||
d684c216 | 291 | |
e1a50c19 L |
292 | #define BEGINNING_OF_LINE() { \ |
293 | while (num) { \ | |
294 | getcmd_putch(CTL_BACKSPACE); \ | |
295 | num--; \ | |
296 | } \ | |
d684c216 L |
297 | } |
298 | ||
e1a50c19 L |
299 | #define ERASE_TO_EOL() { \ |
300 | if (num < eol_num) { \ | |
301 | /* printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); */ \ | |
302 | print_blanks(eol_num - num); \ | |
303 | do { \ | |
304 | getcmd_putch(CTL_BACKSPACE); \ | |
305 | } while (--eol_num > num); \ | |
306 | } \ | |
d684c216 L |
307 | } |
308 | ||
e1a50c19 L |
309 | #define REFRESH_TO_EOL() { \ |
310 | if (num < eol_num) { \ | |
311 | wlen = eol_num - num; \ | |
312 | putnstr(buf + num, wlen); \ | |
313 | num = eol_num; \ | |
314 | } \ | |
d684c216 L |
315 | } |
316 | ||
a8b4c964 L |
317 | static void cread_add_char(char ichar, bool insert, uint_fast8_t *num, |
318 | uint_fast8_t *eol_num, char *buf, uint_fast8_t len) | |
d684c216 | 319 | { |
a8b4c964 | 320 | uint_fast8_t wlen; |
d684c216 L |
321 | |
322 | /* room ??? */ | |
323 | if (insert || *num == *eol_num) { | |
324 | if (*eol_num > len - 1) { | |
325 | getcmd_cbeep(); | |
326 | return; | |
327 | } | |
328 | (*eol_num)++; | |
329 | } | |
330 | ||
331 | if (insert) { | |
332 | wlen = *eol_num - *num; | |
333 | if (wlen > 1) | |
334 | memmove(&buf[*num+1], &buf[*num], wlen-1); | |
335 | ||
336 | buf[*num] = ichar; | |
337 | putnstr(buf + *num, wlen); | |
338 | (*num)++; | |
339 | while (--wlen) | |
340 | getcmd_putch(CTL_BACKSPACE); | |
341 | } else { | |
342 | /* echo the character */ | |
343 | wlen = 1; | |
344 | buf[*num] = ichar; | |
345 | putnstr(buf + *num, wlen); | |
346 | (*num)++; | |
347 | } | |
348 | } | |
349 | ||
a8b4c964 L |
350 | static void cread_add_str(char *str, int strsize, bool insert, |
351 | uint_fast8_t *num, uint_fast8_t *eol_num, | |
352 | char *buf, uint_fast8_t len) | |
d684c216 L |
353 | { |
354 | while (strsize--) { | |
355 | cread_add_char(*str, insert, num, eol_num, buf, len); | |
356 | str++; | |
357 | } | |
358 | } | |
359 | ||
a8b4c964 | 360 | static int cread_line(const FLASH char *const prompt, char *buf, uint_fast8_t *len) |
d684c216 | 361 | { |
a8b4c964 L |
362 | uint_fast8_t num = 0; |
363 | uint_fast8_t eol_num = 0; | |
364 | uint_fast8_t wlen; | |
8506d791 | 365 | int ichar; |
a8b4c964 | 366 | bool insert = 1; |
d684c216 L |
367 | int init_len = strlen(buf); |
368 | ||
369 | (void) prompt; | |
370 | ||
371 | if (init_len) | |
372 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); | |
373 | ||
374 | while (1) { | |
375 | ichar = getcmd_getch(); | |
376 | ||
377 | if ((ichar == '\n') || (ichar == '\r')) { | |
378 | putchar('\n'); | |
379 | break; | |
380 | } | |
381 | ||
d684c216 L |
382 | |
383 | switch (ichar) { | |
d684c216 | 384 | |
8506d791 | 385 | case KEY_HOME: |
d684c216 L |
386 | case CTL_CH('a'): |
387 | BEGINNING_OF_LINE(); | |
388 | break; | |
a8b4c964 L |
389 | case CTL_CH('c'): /* ^C - break */ |
390 | *buf = '\0'; /* discard input */ | |
d684c216 | 391 | return -1; |
8506d791 | 392 | case KEY_RIGHT: |
a8b4c964 | 393 | case CTL_CH('f'): /* forward-char */ |
d684c216 L |
394 | if (num < eol_num) { |
395 | getcmd_putch(buf[num]); | |
396 | num++; | |
397 | } | |
398 | break; | |
8506d791 | 399 | case KEY_LEFT: |
a8b4c964 | 400 | case CTL_CH('b'): /* backward-char */ |
d684c216 L |
401 | if (num) { |
402 | getcmd_putch(CTL_BACKSPACE); | |
403 | num--; | |
404 | } | |
405 | break; | |
8506d791 | 406 | case KEY_DC: |
a8b4c964 | 407 | case CTL_CH('d'): /* delete-char */ |
d684c216 L |
408 | if (num < eol_num) { |
409 | wlen = eol_num - num - 1; | |
410 | if (wlen) { | |
411 | memmove(&buf[num], &buf[num+1], wlen); | |
412 | putnstr(buf + num, wlen); | |
413 | } | |
414 | ||
415 | getcmd_putch(' '); | |
416 | do { | |
417 | getcmd_putch(CTL_BACKSPACE); | |
418 | } while (wlen--); | |
419 | eol_num--; | |
420 | } | |
421 | break; | |
a8b4c964 | 422 | case CTL_CH('k'): /* kill-line */ |
d684c216 L |
423 | ERASE_TO_EOL(); |
424 | break; | |
ed7d7fd3 | 425 | case KEY_END: |
d684c216 L |
426 | case CTL_CH('e'): |
427 | REFRESH_TO_EOL(); | |
428 | break; | |
8506d791 | 429 | case KEY_IC: |
d684c216 L |
430 | case CTL_CH('o'): |
431 | insert = !insert; | |
432 | break; | |
433 | case CTL_CH('x'): | |
a8b4c964 | 434 | case CTL_CH('u'): /* kill-whole-line */ |
d684c216 L |
435 | BEGINNING_OF_LINE(); |
436 | ERASE_TO_EOL(); | |
437 | break; | |
438 | case DEL: | |
439 | case DEL7: | |
a8b4c964 | 440 | case 8: /* backward-delete-char */ |
d684c216 L |
441 | if (num) { |
442 | wlen = eol_num - num; | |
443 | num--; | |
444 | memmove(&buf[num], &buf[num+1], wlen); | |
445 | getcmd_putch(CTL_BACKSPACE); | |
446 | putnstr(buf + num, wlen); | |
447 | getcmd_putch(' '); | |
448 | do { | |
449 | getcmd_putch(CTL_BACKSPACE); | |
450 | } while (wlen--); | |
451 | eol_num--; | |
452 | } | |
453 | break; | |
8506d791 | 454 | case KEY_UP: |
a8b4c964 | 455 | case CTL_CH('p'): /* previous-history */ |
8506d791 | 456 | case KEY_DOWN: |
a8b4c964 | 457 | case CTL_CH('n'): /* next-history */ |
d684c216 L |
458 | { |
459 | char *hline; | |
460 | ||
8506d791 | 461 | if (ichar == CTL_CH('p') || ichar == KEY_UP) |
d684c216 L |
462 | hline = hist_prev(); |
463 | else | |
464 | hline = hist_next(); | |
465 | ||
466 | if (!hline) { | |
467 | getcmd_cbeep(); | |
468 | continue; | |
469 | } | |
470 | ||
471 | /* nuke the current line */ | |
472 | /* first, go home */ | |
473 | BEGINNING_OF_LINE(); | |
474 | ||
475 | /* erase to end of line */ | |
476 | ERASE_TO_EOL(); | |
477 | ||
478 | /* copy new line into place and display */ | |
479 | strcpy(buf, hline); | |
480 | eol_num = strlen(buf); | |
481 | REFRESH_TO_EOL(); | |
482 | continue; | |
483 | } | |
484 | #ifdef CONFIG_AUTO_COMPLETE | |
485 | case '\t': { | |
486 | int num2, col; | |
487 | ||
488 | /* do not autocomplete when in the middle */ | |
489 | if (num < eol_num) { | |
490 | getcmd_cbeep(); | |
491 | break; | |
492 | } | |
493 | ||
494 | buf[num] = '\0'; | |
495 | col = strlen_P(prompt) + eol_num; | |
496 | num2 = num; | |
497 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { | |
498 | col = num2 - num; | |
499 | num += col; | |
500 | eol_num += col; | |
501 | } | |
502 | break; | |
503 | } | |
504 | #endif | |
505 | default: | |
8506d791 L |
506 | if (isprint(ichar)) |
507 | cread_add_char(ichar, insert, &num, &eol_num, buf, | |
508 | *len); | |
d684c216 L |
509 | break; |
510 | } | |
511 | } | |
512 | *len = eol_num; | |
513 | buf[eol_num] = '\0'; /* lose the newline */ | |
514 | ||
8506d791 | 515 | if (buf[0] /* && buf[0] != CREAD_HIST_CHAR */) |
d684c216 L |
516 | cread_add_to_hist(buf); |
517 | hist_cur = hist_add_idx; | |
518 | ||
519 | return 0; | |
520 | } | |
521 | ||
d684c216 L |
522 | /****************************************************************************/ |
523 | ||
524 | static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer) | |
525 | { | |
526 | char *p = buffer; | |
a8b4c964 | 527 | uint_fast8_t len = CONFIG_SYS_CBSIZE; |
d684c216 | 528 | int rc; |
d684c216 L |
529 | |
530 | if (prompt) | |
531 | my_puts_P(prompt); | |
532 | ||
533 | rc = cread_line(prompt, p, &len); | |
534 | return rc < 0 ? rc : (int) len; | |
d684c216 L |
535 | } |
536 | ||
537 | int cli_readline(const FLASH char *const prompt) | |
538 | { | |
539 | /* | |
540 | * If console_buffer isn't 0-length the user will be prompted to modify | |
541 | * it instead of entering it from scratch as desired. | |
542 | */ | |
543 | console_buffer[0] = '\0'; | |
544 | ||
545 | return cli_readline_into_buffer(prompt, console_buffer); | |
546 | } |