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