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