]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * (C) Copyright 2014-2016 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 "cli_readline.h" | |
15 | #include "common.h" | |
16 | #include <string.h> | |
17 | #include <stdio.h> | |
18 | #include <stdbool.h> | |
19 | #include <ctype.h> | |
20 | ||
21 | #include "config.h" | |
22 | #include "con-utils.h" | |
23 | #include "print-utils.h" | |
24 | #include "command.h" | |
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) | |
32 | ||
33 | ||
34 | ||
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 | ||
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 | |
84 | FKEY_TBL_ITEM(A, KEY_UP), // Up arrow key | |
85 | FKEY_TBL_ITEM(D, KEY_LEFT), // Left arrow key | |
86 | FKEY_TBL_ITEM(C, KEY_RIGHT), // Right arrow key | |
87 | FKEY_TBL_ITEM(1~, KEY_HOME), // Home key | |
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 | |
92 | FKEY_TBL_ITEM(4~, KEY_END), // End key | |
93 | FKEY_TBL_ITEM(Z, KEY_BTAB), // Back tab key | |
94 | /* */ | |
95 | FKEY_TBL_ITEM(H, KEY_HOME), // Home key | |
96 | FKEY_TBL_ITEM(F, KEY_END), // End key | |
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 | |
103 | /* Linux console */ | |
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 | |
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 | |
117 | { NULL } /* Mark end of table */ | |
118 | }; | |
119 | ||
120 | ||
121 | ||
122 | typedef enum { | |
123 | STATE_GROUND, | |
124 | STATE_ESCAPE, | |
125 | STATE_CSI_ENTRY, | |
126 | STATE_SS3 | |
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 | } | |
166 | if (ch == 'O') { | |
167 | state = STATE_SS3; | |
168 | continue; | |
169 | } | |
170 | state = STATE_GROUND; | |
171 | break; | |
172 | case STATE_SS3: | |
173 | if (ch == 'F') | |
174 | ch = KEY_END; | |
175 | if (ch == 'H') | |
176 | ch = KEY_HOME; | |
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 | ||
220 | /* | |
221 | * cmdline-editing related codes from vivi. | |
222 | * Author: Janghoon Lyu <nandy@mizi.com> | |
223 | */ | |
224 | ||
225 | ||
226 | char histbuf[CONFIG_SYS_HISTSIZE+1]; | |
227 | #define HISTBUFE (histbuf+CONFIG_SYS_HISTSIZE) | |
228 | ||
229 | static char *hist_head; | |
230 | static char *hist_cur; | |
231 | ||
232 | static void hist_reset(void) | |
233 | { | |
234 | if (hist_head == NULL) | |
235 | hist_head = HISTBUFE; | |
236 | hist_cur = hist_head; | |
237 | } | |
238 | ||
239 | static char *hist_entry_next(char *p) | |
240 | { | |
241 | if (*p) | |
242 | while (*p++); | |
243 | ||
244 | return p; | |
245 | } | |
246 | ||
247 | static char *hist_entry_prev(char *p) | |
248 | { | |
249 | if (p == hist_head) | |
250 | return NULL; | |
251 | ||
252 | --p; | |
253 | while (p != hist_head && *(p-1) != 0) | |
254 | --p; | |
255 | ||
256 | return p; | |
257 | } | |
258 | ||
259 | static char *hist_search_entry(char *line) | |
260 | { | |
261 | char *p = hist_head; | |
262 | ||
263 | while (*p && strcmp(p, line)) | |
264 | p = hist_entry_next(p); | |
265 | return *p ? p : NULL; | |
266 | } | |
267 | ||
268 | static char *hist_delete(size_t amount) | |
269 | { | |
270 | char *p; | |
271 | ||
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); | |
285 | } | |
286 | return hist_head; | |
287 | } | |
288 | ||
289 | static char *hist_delete_entry(char *entry) | |
290 | { | |
291 | size_t shift = strlen(entry) + 1; | |
292 | size_t len = entry - hist_head; | |
293 | ||
294 | hist_head = memmove(hist_head + shift, hist_head, len); | |
295 | ||
296 | return hist_head; | |
297 | } | |
298 | ||
299 | ||
300 | static char *hist_add_entry(char *line) | |
301 | { | |
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; | |
321 | uint_fast8_t n = 0; | |
322 | ||
323 | while (*p) { | |
324 | ++n; | |
325 | while (*p++); | |
326 | } | |
327 | return n; | |
328 | } | |
329 | ||
330 | static int hist_get_size(void) | |
331 | { | |
332 | return HISTBUFE - hist_head; | |
333 | } | |
334 | ||
335 | static char *cread_add_to_hist(char *line) | |
336 | { | |
337 | char *p; | |
338 | ||
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); | |
346 | } | |
347 | hist_add_entry(line); | |
348 | ||
349 | return p; | |
350 | } | |
351 | ||
352 | static char *hist_prev(void) | |
353 | { | |
354 | char *p = hist_cur; | |
355 | ||
356 | if (*p == '\0') | |
357 | return NULL; | |
358 | hist_cur = hist_entry_next(p); | |
359 | ||
360 | return p; | |
361 | } | |
362 | ||
363 | static char *hist_next(void) | |
364 | { | |
365 | char *p = hist_cur; | |
366 | ||
367 | if(p == hist_head) | |
368 | return NULL; | |
369 | ||
370 | p = hist_entry_prev(p); | |
371 | hist_cur = p; | |
372 | ||
373 | return p == hist_head ? "" : hist_entry_prev(p); | |
374 | } | |
375 | ||
376 | static char *hist_search_backward(char* buf, uint8_t num) | |
377 | { | |
378 | char *p = hist_cur; | |
379 | ||
380 | if (*p == '\0') | |
381 | return NULL; | |
382 | ||
383 | while (*p && strncmp(p, buf, num)) | |
384 | p = hist_entry_next(p); | |
385 | ||
386 | if(!strncmp(p, buf, num)) { | |
387 | hist_cur = hist_entry_next(p); | |
388 | return p; | |
389 | } | |
390 | ||
391 | return NULL; | |
392 | } | |
393 | ||
394 | static char *hist_search_forward (char* buf, uint8_t num) | |
395 | { | |
396 | char *p = hist_cur; | |
397 | ||
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); | |
402 | ||
403 | //if(!strncmp(p, buf, num)) { | |
404 | if(p) { | |
405 | hist_cur = hist_entry_next(p); | |
406 | return p; | |
407 | } | |
408 | } | |
409 | ||
410 | return NULL; | |
411 | } | |
412 | ||
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 | } | |
450 | } | |
451 | ||
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) | |
454 | { | |
455 | uint_fast8_t wlen; | |
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 */ | |
478 | buf[*num] = ichar; | |
479 | putnstr(buf + *num, 1); | |
480 | (*num)++; | |
481 | } | |
482 | } | |
483 | ||
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) | |
486 | { | |
487 | char c; | |
488 | ||
489 | while ((c = *str++) != '\0') | |
490 | cread_add_char(c, insert, num, eol_num, buf, len); | |
491 | } | |
492 | ||
493 | static int cread_line(const FLASH char *const prompt, char *buf, | |
494 | uint_fast8_t len, bool enable_history) | |
495 | { | |
496 | uint_fast8_t num = 0; | |
497 | uint_fast8_t eol_num = 0; | |
498 | bool insert = 1; | |
499 | ||
500 | (void) prompt; | |
501 | ||
502 | if (buf[0]) | |
503 | cread_add_str(buf, 1, &num, &eol_num, buf, len); | |
504 | ||
505 | hist_reset(); | |
506 | ||
507 | while (1) { | |
508 | int ichar = getcmd_getch(); | |
509 | ||
510 | if ((ichar == '\n') || (ichar == '\r')) { | |
511 | putchar('\n'); | |
512 | break; | |
513 | } | |
514 | ||
515 | ||
516 | switch (ichar) { | |
517 | ||
518 | case KEY_HOME: | |
519 | case CTL_CH('a'): | |
520 | beginning_of_line(&num); | |
521 | break; | |
522 | case CTL_CH('c'): /* ^C - break */ | |
523 | putchar('\n'); | |
524 | *buf = '\0'; /* discard input */ | |
525 | return -1; | |
526 | case KEY_RIGHT: | |
527 | case CTL_CH('f'): /* forward-char */ | |
528 | if (num < eol_num) { | |
529 | getcmd_putch(buf[num]); | |
530 | num++; | |
531 | } | |
532 | break; | |
533 | case KEY_LEFT: | |
534 | case CTL_CH('b'): /* backward-char */ | |
535 | if (num) { | |
536 | getcmd_putch(CTL_BACKSPACE); | |
537 | num--; | |
538 | } | |
539 | break; | |
540 | case KEY_DC: | |
541 | case CTL_CH('d'): /* delete-char */ | |
542 | if (num < eol_num) { | |
543 | uint_fast8_t wlen = eol_num - num - 1; | |
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; | |
556 | case CTL_CH('k'): /* kill-line */ | |
557 | erase_to_eol(&num, &eol_num); | |
558 | break; | |
559 | case KEY_END: | |
560 | case CTL_CH('e'): | |
561 | refresh_to_eol(buf, &num, &eol_num); | |
562 | break; | |
563 | case KEY_IC: | |
564 | case CTL_CH('o'): | |
565 | insert = !insert; | |
566 | break; | |
567 | case CTL_CH('x'): | |
568 | case CTL_CH('u'): /* kill-whole-line */ | |
569 | beginning_of_line(&num); | |
570 | erase_to_eol(&num, &eol_num); | |
571 | break; | |
572 | case DEL: | |
573 | case DEL7: | |
574 | case 8: /* backward-delete-char */ | |
575 | if (num) { | |
576 | uint_fast8_t wlen = eol_num - --num; | |
577 | buf[eol_num] = ' '; | |
578 | memmove(&buf[num], &buf[num+1], wlen); | |
579 | getcmd_putch(CTL_BACKSPACE); | |
580 | putnstr(buf + num, wlen); | |
581 | do { | |
582 | getcmd_putch(CTL_BACKSPACE); | |
583 | } while (--wlen); | |
584 | eol_num--; | |
585 | } | |
586 | break; | |
587 | case KEY_UP: | |
588 | case CTL_CH('p'): /* previous-history */ | |
589 | case KEY_DOWN: | |
590 | case CTL_CH('n'): /* next-history */ | |
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) { | |
600 | /* first, go home */ | |
601 | beginning_of_line(&num); | |
602 | /* overwrite current line */ | |
603 | cread_add_str(hline, 0, &num, &eol_num, buf, len); | |
604 | /* erase to end of line */ | |
605 | erase_to_eol(&num, &eol_num); | |
606 | ||
607 | } else { | |
608 | getcmd_cbeep(); | |
609 | } | |
610 | } else { | |
611 | getcmd_cbeep(); | |
612 | } | |
613 | break; | |
614 | case KEY_PPAGE: /* history-search-backward */ | |
615 | case KEY_NPAGE: /* history-search-forward */ | |
616 | if (enable_history) { | |
617 | char *hline; | |
618 | if (ichar == KEY_PPAGE) | |
619 | hline = hist_search_backward(buf, num); | |
620 | else | |
621 | hline = hist_search_forward(buf, num); | |
622 | ||
623 | if (hline) { | |
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); | |
627 | /* erase to end of line */ | |
628 | erase_to_eol(&num2, &eol_num); | |
629 | /* cursor back */ | |
630 | while (num2-- > num) | |
631 | getcmd_putch(CTL_BACKSPACE); | |
632 | } else { | |
633 | getcmd_cbeep(); | |
634 | } | |
635 | } else { | |
636 | getcmd_cbeep(); | |
637 | } | |
638 | break; | |
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: | |
661 | if (isprint(ichar)) | |
662 | cread_add_char(ichar, insert, &num, &eol_num, buf, len); | |
663 | break; | |
664 | } | |
665 | } | |
666 | while (eol_num && buf[eol_num-1] == ' ') | |
667 | --eol_num; /* remove trailing blanks */ | |
668 | buf[eol_num] = '\0'; /* lose the newline */ | |
669 | ||
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]) { | |
680 | cread_add_to_hist(buf); | |
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 | } | |
684 | return eol_num; | |
685 | } | |
686 | ||
687 | /****************************************************************************/ | |
688 | ||
689 | int cli_readline(const FLASH char *const prompt, bool enable_history) | |
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 | ||
697 | if (prompt) | |
698 | my_puts_P(prompt); | |
699 | ||
700 | return cread_line(prompt, console_buffer, CONFIG_SYS_CBSIZE, enable_history); | |
701 | } |