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