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