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