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