]>
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 | * | |
11 | * SPDX-License-Identifier: GPL-2.0+ | |
12 | */ | |
13 | ||
14 | #include "common.h" | |
15 | ||
d684c216 L |
16 | #include <string.h> |
17 | #include <stdio.h> | |
18 | ||
19 | #include "config.h" | |
20 | #include "con-utils.h" | |
21 | #include "command.h" | |
22 | #include "cli_readline.h" | |
23 | ||
24 | char console_buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ | |
25 | ||
26 | static const FLASH char erase_seq[] = "\b \b"; /* erase sequence */ | |
27 | static const FLASH char tab_seq[] = " "; /* used to expand TABs */ | |
28 | ||
29 | #ifndef CONFIG_CMDLINE_EDITING | |
30 | static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) | |
31 | { | |
32 | char *s; | |
33 | ||
34 | if (*np == 0) | |
35 | return p; | |
36 | ||
37 | if (*(--p) == '\t') { /* will retype the whole line */ | |
38 | while (*colp > plen) { | |
39 | my_puts_P(erase_seq); | |
40 | (*colp)--; | |
41 | } | |
42 | for (s = buffer; s < p; ++s) { | |
43 | if (*s == '\t') { | |
44 | my_puts_P(tab_seq + ((*colp) & 07)); | |
45 | *colp += 8 - ((*colp) & 07); | |
46 | } else { | |
47 | ++(*colp); | |
48 | putchar(*s); | |
49 | } | |
50 | } | |
51 | } else { | |
52 | my_puts_P(erase_seq); | |
53 | (*colp)--; | |
54 | } | |
55 | (*np)--; | |
56 | ||
57 | return p; | |
58 | } | |
59 | #endif /* CONFIG_CMDLINE_EDITING */ | |
60 | ||
61 | ||
62 | #ifdef CONFIG_CMDLINE_EDITING | |
63 | ||
64 | /* | |
65 | * cmdline-editing related codes from vivi. | |
66 | * Author: Janghoon Lyu <nandy@mizi.com> | |
67 | */ | |
68 | ||
69 | #define putnstr(str, n) printf_P(PSTR("%.*s"), (int)n, str) | |
70 | ||
71 | #define CTL_CH(c) ((c) - 'a' + 1) | |
72 | #define CTL_BACKSPACE ('\b') | |
73 | #define DEL ((char)255) | |
74 | #define DEL7 ((char)127) | |
75 | #define CREAD_HIST_CHAR ('!') | |
76 | ||
77 | #define getcmd_putch(ch) putchar(ch) | |
424b184a | 78 | #define getcmd_getch() my_getchar(1) |
d684c216 L |
79 | #define getcmd_cbeep() getcmd_putch('\a') |
80 | ||
81 | #define HIST_MAX 5 | |
82 | #define HIST_SIZE CONFIG_SYS_CBSIZE | |
83 | ||
84 | static int hist_max; | |
85 | static int hist_add_idx; | |
86 | static int hist_cur = -1; | |
87 | static unsigned hist_num; | |
88 | ||
89 | static char *hist_list[HIST_MAX]; | |
90 | static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ | |
91 | ||
92 | #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) | |
93 | ||
94 | static void hist_init(void) | |
95 | { | |
96 | int i; | |
97 | ||
98 | hist_max = 0; | |
99 | hist_add_idx = 0; | |
100 | hist_cur = -1; | |
101 | hist_num = 0; | |
102 | ||
103 | for (i = 0; i < HIST_MAX; i++) { | |
104 | hist_list[i] = hist_lines[i]; | |
105 | hist_list[i][0] = '\0'; | |
106 | } | |
107 | } | |
108 | ||
109 | static void cread_add_to_hist(char *line) | |
110 | { | |
111 | strcpy(hist_list[hist_add_idx], line); | |
112 | ||
113 | if (++hist_add_idx >= HIST_MAX) | |
114 | hist_add_idx = 0; | |
115 | ||
116 | if (hist_add_idx > hist_max) | |
117 | hist_max = hist_add_idx; | |
118 | ||
119 | hist_num++; | |
120 | } | |
121 | ||
122 | static char *hist_prev(void) | |
123 | { | |
124 | char *ret; | |
125 | int old_cur; | |
126 | ||
127 | if (hist_cur < 0) | |
128 | return NULL; | |
129 | ||
130 | old_cur = hist_cur; | |
131 | if (--hist_cur < 0) | |
132 | hist_cur = hist_max; | |
133 | ||
134 | if (hist_cur == hist_add_idx) { | |
135 | hist_cur = old_cur; | |
136 | ret = NULL; | |
137 | } else { | |
138 | ret = hist_list[hist_cur]; | |
139 | } | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | static char *hist_next(void) | |
145 | { | |
146 | char *ret; | |
147 | ||
148 | if (hist_cur < 0) | |
149 | return NULL; | |
150 | ||
151 | if (hist_cur == hist_add_idx) | |
152 | return NULL; | |
153 | ||
154 | if (++hist_cur > hist_max) | |
155 | hist_cur = 0; | |
156 | ||
157 | if (hist_cur == hist_add_idx) | |
158 | ret = ""; | |
159 | else | |
160 | ret = hist_list[hist_cur]; | |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
165 | #ifndef CONFIG_CMDLINE_EDITING | |
166 | static void cread_print_hist_list(void) | |
167 | { | |
168 | int i; | |
169 | unsigned n; | |
170 | ||
171 | n = hist_num - hist_max; | |
172 | ||
173 | i = hist_add_idx + 1; | |
174 | while (1) { | |
175 | if (i > hist_max) | |
176 | i = 0; | |
177 | if (i == hist_add_idx) | |
178 | break; | |
179 | printf_P(PSTR("%s\n"), hist_list[i]); | |
180 | n++; | |
181 | i++; | |
182 | } | |
183 | } | |
184 | #endif /* CONFIG_CMDLINE_EDITING */ | |
185 | ||
186 | #define BEGINNING_OF_LINE() { \ | |
187 | while (num) { \ | |
188 | getcmd_putch(CTL_BACKSPACE); \ | |
189 | num--; \ | |
190 | } \ | |
191 | } | |
192 | ||
193 | #define ERASE_TO_EOL() { \ | |
194 | if (num < eol_num) { \ | |
195 | printf_P(PSTR("%*S"), (int)(eol_num - num), PSTR("")); \ | |
196 | do { \ | |
197 | getcmd_putch(CTL_BACKSPACE); \ | |
198 | } while (--eol_num > num); \ | |
199 | } \ | |
200 | } | |
201 | ||
202 | #define REFRESH_TO_EOL() { \ | |
203 | if (num < eol_num) { \ | |
204 | wlen = eol_num - num; \ | |
205 | putnstr(buf + num, wlen); \ | |
206 | num = eol_num; \ | |
207 | } \ | |
208 | } | |
209 | ||
210 | static void cread_add_char(char ichar, int insert, unsigned long *num, | |
211 | unsigned long *eol_num, char *buf, unsigned long len) | |
212 | { | |
213 | unsigned long wlen; | |
214 | ||
215 | /* room ??? */ | |
216 | if (insert || *num == *eol_num) { | |
217 | if (*eol_num > len - 1) { | |
218 | getcmd_cbeep(); | |
219 | return; | |
220 | } | |
221 | (*eol_num)++; | |
222 | } | |
223 | ||
224 | if (insert) { | |
225 | wlen = *eol_num - *num; | |
226 | if (wlen > 1) | |
227 | memmove(&buf[*num+1], &buf[*num], wlen-1); | |
228 | ||
229 | buf[*num] = ichar; | |
230 | putnstr(buf + *num, wlen); | |
231 | (*num)++; | |
232 | while (--wlen) | |
233 | getcmd_putch(CTL_BACKSPACE); | |
234 | } else { | |
235 | /* echo the character */ | |
236 | wlen = 1; | |
237 | buf[*num] = ichar; | |
238 | putnstr(buf + *num, wlen); | |
239 | (*num)++; | |
240 | } | |
241 | } | |
242 | ||
243 | static void cread_add_str(char *str, int strsize, int insert, | |
244 | unsigned long *num, unsigned long *eol_num, | |
245 | char *buf, unsigned long len) | |
246 | { | |
247 | while (strsize--) { | |
248 | cread_add_char(*str, insert, num, eol_num, buf, len); | |
249 | str++; | |
250 | } | |
251 | } | |
252 | ||
253 | static int cread_line(const FLASH char *const prompt, char *buf, unsigned int *len) | |
254 | { | |
255 | unsigned long num = 0; | |
256 | unsigned long eol_num = 0; | |
257 | unsigned long wlen; | |
258 | char ichar; | |
259 | int insert = 1; | |
260 | int esc_len = 0; | |
261 | char esc_save[8]; | |
262 | int init_len = strlen(buf); | |
263 | ||
264 | (void) prompt; | |
265 | ||
266 | if (init_len) | |
267 | cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); | |
268 | ||
269 | while (1) { | |
270 | ichar = getcmd_getch(); | |
271 | ||
272 | if ((ichar == '\n') || (ichar == '\r')) { | |
273 | putchar('\n'); | |
274 | break; | |
275 | } | |
276 | ||
277 | /* | |
278 | * handle standard linux xterm esc sequences for arrow key, etc. | |
279 | */ | |
280 | if (esc_len != 0) { | |
281 | if (esc_len == 1) { | |
282 | if (ichar == '[') { | |
283 | esc_save[esc_len] = ichar; | |
284 | esc_len = 2; | |
285 | } else { | |
286 | cread_add_str(esc_save, esc_len, | |
287 | insert, &num, &eol_num, | |
288 | buf, *len); | |
289 | esc_len = 0; | |
290 | } | |
291 | continue; | |
292 | } | |
293 | ||
294 | switch (ichar) { | |
295 | case 'D': /* <- key */ | |
296 | ichar = CTL_CH('b'); | |
297 | esc_len = 0; | |
298 | break; | |
299 | case 'C': /* -> key */ | |
300 | ichar = CTL_CH('f'); | |
301 | esc_len = 0; | |
302 | break; /* pass off to ^F handler */ | |
303 | case 'H': /* Home key */ | |
304 | ichar = CTL_CH('a'); | |
305 | esc_len = 0; | |
306 | break; /* pass off to ^A handler */ | |
307 | case 'A': /* up arrow */ | |
308 | ichar = CTL_CH('p'); | |
309 | esc_len = 0; | |
310 | break; /* pass off to ^P handler */ | |
311 | case 'B': /* down arrow */ | |
312 | ichar = CTL_CH('n'); | |
313 | esc_len = 0; | |
314 | break; /* pass off to ^N handler */ | |
315 | default: | |
316 | esc_save[esc_len++] = ichar; | |
317 | cread_add_str(esc_save, esc_len, insert, | |
318 | &num, &eol_num, buf, *len); | |
319 | esc_len = 0; | |
320 | continue; | |
321 | } | |
322 | } | |
323 | ||
324 | switch (ichar) { | |
325 | case 0x1b: | |
326 | if (esc_len == 0) { | |
327 | esc_save[esc_len] = ichar; | |
328 | esc_len = 1; | |
329 | } else { | |
330 | my_puts_P(PSTR("impossible condition #876\n")); | |
331 | esc_len = 0; | |
332 | } | |
333 | break; | |
334 | ||
335 | case CTL_CH('a'): | |
336 | BEGINNING_OF_LINE(); | |
337 | break; | |
338 | case CTL_CH('c'): /* ^C - break */ | |
339 | *buf = '\0'; /* discard input */ | |
340 | return -1; | |
341 | case CTL_CH('f'): | |
342 | if (num < eol_num) { | |
343 | getcmd_putch(buf[num]); | |
344 | num++; | |
345 | } | |
346 | break; | |
347 | case CTL_CH('b'): | |
348 | if (num) { | |
349 | getcmd_putch(CTL_BACKSPACE); | |
350 | num--; | |
351 | } | |
352 | break; | |
353 | case CTL_CH('d'): | |
354 | if (num < eol_num) { | |
355 | wlen = eol_num - num - 1; | |
356 | if (wlen) { | |
357 | memmove(&buf[num], &buf[num+1], wlen); | |
358 | putnstr(buf + num, wlen); | |
359 | } | |
360 | ||
361 | getcmd_putch(' '); | |
362 | do { | |
363 | getcmd_putch(CTL_BACKSPACE); | |
364 | } while (wlen--); | |
365 | eol_num--; | |
366 | } | |
367 | break; | |
368 | case CTL_CH('k'): | |
369 | ERASE_TO_EOL(); | |
370 | break; | |
371 | case CTL_CH('e'): | |
372 | REFRESH_TO_EOL(); | |
373 | break; | |
374 | case CTL_CH('o'): | |
375 | insert = !insert; | |
376 | break; | |
377 | case CTL_CH('x'): | |
378 | case CTL_CH('u'): | |
379 | BEGINNING_OF_LINE(); | |
380 | ERASE_TO_EOL(); | |
381 | break; | |
382 | case DEL: | |
383 | case DEL7: | |
384 | case 8: | |
385 | if (num) { | |
386 | wlen = eol_num - num; | |
387 | num--; | |
388 | memmove(&buf[num], &buf[num+1], wlen); | |
389 | getcmd_putch(CTL_BACKSPACE); | |
390 | putnstr(buf + num, wlen); | |
391 | getcmd_putch(' '); | |
392 | do { | |
393 | getcmd_putch(CTL_BACKSPACE); | |
394 | } while (wlen--); | |
395 | eol_num--; | |
396 | } | |
397 | break; | |
398 | case CTL_CH('p'): | |
399 | case CTL_CH('n'): | |
400 | { | |
401 | char *hline; | |
402 | ||
403 | esc_len = 0; | |
404 | ||
405 | if (ichar == CTL_CH('p')) | |
406 | hline = hist_prev(); | |
407 | else | |
408 | hline = hist_next(); | |
409 | ||
410 | if (!hline) { | |
411 | getcmd_cbeep(); | |
412 | continue; | |
413 | } | |
414 | ||
415 | /* nuke the current line */ | |
416 | /* first, go home */ | |
417 | BEGINNING_OF_LINE(); | |
418 | ||
419 | /* erase to end of line */ | |
420 | ERASE_TO_EOL(); | |
421 | ||
422 | /* copy new line into place and display */ | |
423 | strcpy(buf, hline); | |
424 | eol_num = strlen(buf); | |
425 | REFRESH_TO_EOL(); | |
426 | continue; | |
427 | } | |
428 | #ifdef CONFIG_AUTO_COMPLETE | |
429 | case '\t': { | |
430 | int num2, col; | |
431 | ||
432 | /* do not autocomplete when in the middle */ | |
433 | if (num < eol_num) { | |
434 | getcmd_cbeep(); | |
435 | break; | |
436 | } | |
437 | ||
438 | buf[num] = '\0'; | |
439 | col = strlen_P(prompt) + eol_num; | |
440 | num2 = num; | |
441 | if (cmd_auto_complete(prompt, buf, &num2, &col)) { | |
442 | col = num2 - num; | |
443 | num += col; | |
444 | eol_num += col; | |
445 | } | |
446 | break; | |
447 | } | |
448 | #endif | |
449 | default: | |
450 | cread_add_char(ichar, insert, &num, &eol_num, buf, | |
451 | *len); | |
452 | break; | |
453 | } | |
454 | } | |
455 | *len = eol_num; | |
456 | buf[eol_num] = '\0'; /* lose the newline */ | |
457 | ||
458 | if (buf[0] && buf[0] != CREAD_HIST_CHAR) | |
459 | cread_add_to_hist(buf); | |
460 | hist_cur = hist_add_idx; | |
461 | ||
462 | return 0; | |
463 | } | |
464 | ||
465 | #endif /* CONFIG_CMDLINE_EDITING */ | |
466 | ||
467 | /****************************************************************************/ | |
468 | ||
469 | static int cli_readline_into_buffer(const FLASH char *const prompt, char *buffer) | |
470 | { | |
471 | char *p = buffer; | |
472 | #ifdef CONFIG_CMDLINE_EDITING | |
473 | unsigned int len = CONFIG_SYS_CBSIZE; | |
474 | int rc; | |
475 | static int initted; | |
476 | ||
477 | if (!initted) { | |
478 | hist_init(); | |
479 | initted = 1; | |
480 | } | |
481 | ||
482 | if (prompt) | |
483 | my_puts_P(prompt); | |
484 | ||
485 | rc = cread_line(prompt, p, &len); | |
486 | return rc < 0 ? rc : (int) len; | |
487 | ||
488 | #else /* CONFIG_CMDLINE_EDITING */ | |
489 | char *p_buf = p; | |
490 | int n = 0; /* buffer index */ | |
491 | int plen = 0; /* prompt length */ | |
492 | int col; /* output column cnt */ | |
493 | char c; | |
494 | ||
495 | /* print prompt */ | |
496 | if (prompt) { | |
497 | plen = strlen_P(prompt); | |
498 | my_puts_P(prompt); | |
499 | } | |
500 | col = plen; | |
501 | ||
502 | for (;;) { | |
503 | ||
424b184a | 504 | c = my_getchar(1); |
d684c216 L |
505 | |
506 | /* | |
507 | * Special character handling | |
508 | */ | |
509 | switch (c) { | |
510 | case '\r': /* Enter */ | |
511 | case '\n': | |
512 | *p = '\0'; | |
513 | my_puts_P(PSTR("\r\n")); | |
514 | return p - p_buf; | |
515 | ||
516 | case '\0': /* nul */ | |
517 | continue; | |
518 | ||
519 | case 0x03: /* ^C - break */ | |
520 | p_buf[0] = '\0'; /* discard input */ | |
521 | return -1; | |
522 | ||
523 | case 0x15: /* ^U - erase line */ | |
524 | while (col > plen) { | |
525 | my_puts_P(erase_seq); | |
526 | --col; | |
527 | } | |
528 | p = p_buf; | |
529 | n = 0; | |
530 | continue; | |
531 | ||
532 | case 0x17: /* ^W - erase word */ | |
533 | p = delete_char(p_buf, p, &col, &n, plen); | |
534 | while ((n > 0) && (*p != ' ')) | |
535 | p = delete_char(p_buf, p, &col, &n, plen); | |
536 | continue; | |
537 | ||
538 | case 0x08: /* ^H - backspace */ | |
539 | case 0x7F: /* DEL - backspace */ | |
540 | p = delete_char(p_buf, p, &col, &n, plen); | |
541 | continue; | |
542 | ||
543 | default: | |
544 | /* | |
545 | * Must be a normal character then | |
546 | */ | |
547 | if (n < CONFIG_SYS_CBSIZE-2) { | |
548 | if (c == '\t') { /* expand TABs */ | |
549 | #ifdef CONFIG_AUTO_COMPLETE | |
550 | /* | |
551 | * if auto completion triggered just | |
552 | * continue | |
553 | */ | |
554 | *p = '\0'; | |
555 | if (cmd_auto_complete(prompt, | |
556 | console_buffer, | |
557 | &n, &col)) { | |
558 | p = p_buf + n; /* reset */ | |
559 | continue; | |
560 | } | |
561 | #endif | |
562 | my_puts_P(tab_seq + (col & 07)); | |
563 | col += 8 - (col & 07); | |
564 | } else { | |
565 | char buf[2]; | |
566 | ||
567 | /* | |
568 | * Echo input using puts() to force an | |
569 | * LCD flush if we are using an LCD | |
570 | */ | |
571 | ++col; | |
572 | buf[0] = c; | |
573 | buf[1] = '\0'; | |
574 | my_puts(buf); | |
575 | } | |
576 | *p++ = c; | |
577 | ++n; | |
578 | } else { /* Buffer full */ | |
579 | putchar('\a'); | |
580 | } | |
581 | } | |
582 | } | |
583 | #endif /* CONFIG_CMDLINE_EDITING */ | |
584 | } | |
585 | ||
586 | int cli_readline(const FLASH char *const prompt) | |
587 | { | |
588 | /* | |
589 | * If console_buffer isn't 0-length the user will be prompted to modify | |
590 | * it instead of entering it from scratch as desired. | |
591 | */ | |
592 | console_buffer[0] = '\0'; | |
593 | ||
594 | return cli_readline_into_buffer(prompt, console_buffer); | |
595 | } |