diff options
-rw-r--r-- | avr/cli_readline.c | 83 | ||||
-rw-r--r-- | include/cli_readline.h | 31 |
2 files changed, 35 insertions, 79 deletions
diff --git a/avr/cli_readline.c b/avr/cli_readline.c index 045d16e..be753d5 100644 --- a/avr/cli_readline.c +++ b/avr/cli_readline.c @@ -462,25 +462,23 @@ static void cread_add_char(char ichar, bool insert, uint_fast8_t *num, getcmd_putch(CTL_BACKSPACE); } else { /* echo the character */ - wlen = 1; buf[*num] = ichar; - putnstr(buf + *num, wlen); + putnstr(buf + *num, 1); (*num)++; } } -static void cread_add_str(char *str, uint_fast8_t strsize, bool insert, - uint_fast8_t *num, uint_fast8_t *eol_num, - char *buf, uint_fast8_t len) +static void cread_add_str(char *str, bool insert, uint_fast8_t *num, + uint_fast8_t *eol_num, char *buf, uint_fast8_t len) { - while (strsize--) { - cread_add_char(*str, insert, num, eol_num, buf, len); - str++; - } + char c; + + while ((c = *str++) != '\0') + cread_add_char(c, insert, num, eol_num, buf, len); } static int cread_line(const FLASH char *const prompt, char *buf, - uint_fast8_t *len, bool enable_history) + uint_fast8_t len, bool enable_history) { uint_fast8_t num = 0; uint_fast8_t eol_num = 0; @@ -488,9 +486,8 @@ static int cread_line(const FLASH char *const prompt, char *buf, (void) prompt; - uint_fast8_t init_len = strlen(buf); - if (init_len) - cread_add_str(buf, init_len, 1, &num, &eol_num, buf, *len); + if (buf[0]) + cread_add_str(buf, 1, &num, &eol_num, buf, len); hist_reset(); @@ -510,6 +507,7 @@ static int cread_line(const FLASH char *const prompt, char *buf, beginning_of_line(&num); break; case CTL_CH('c'): /* ^C - break */ + putchar('\n'); *buf = '\0'; /* discard input */ return -1; case KEY_RIGHT: @@ -562,15 +560,14 @@ static int cread_line(const FLASH char *const prompt, char *buf, case DEL7: case 8: /* backward-delete-char */ if (num) { - uint_fast8_t wlen = eol_num - num; - num--; + uint_fast8_t wlen = eol_num - --num; + buf[eol_num] = ' '; memmove(&buf[num], &buf[num+1], wlen); getcmd_putch(CTL_BACKSPACE); putnstr(buf + num, wlen); - getcmd_putch(' '); do { getcmd_putch(CTL_BACKSPACE); - } while (wlen--); + } while (--wlen); eol_num--; } break; @@ -587,17 +584,13 @@ static int cread_line(const FLASH char *const prompt, char *buf, hline = hist_next(); if (hline) { - /* nuke the current line */ /* first, go home */ beginning_of_line(&num); - + /* overwrite current line */ + cread_add_str(hline, 0, &num, &eol_num, buf, len); /* erase to end of line */ erase_to_eol(&num, &eol_num); - /* copy new line into place and display */ - strcpy(buf, hline); - eol_num = strlen(buf); - refresh_to_eol(buf, &num, &eol_num); } else { getcmd_cbeep(); } @@ -609,25 +602,20 @@ static int cread_line(const FLASH char *const prompt, char *buf, case KEY_NPAGE: /* history-search-forward */ if (enable_history) { char *hline; - if (ichar == KEY_PPAGE) hline = hist_search_backward(buf, num); else hline = hist_search_forward(buf, num); if (hline) { + uint_fast8_t num2 = num; + /* overwrite current line from cursor position */ + cread_add_str(hline+num, 0, &num2, &eol_num, buf, len); /* erase to end of line */ - erase_to_eol(&num, &eol_num); - - /* copy new line into place and display */ - strcpy(buf+num, hline+num); - eol_num = strlen(buf); - uint8_t wlen = eol_num - num; - putnstr(buf + num, wlen); - getcmd_putch(' '); - do { + erase_to_eol(&num2, &eol_num); + /* cursor back */ + while (num2-- > num) getcmd_putch(CTL_BACKSPACE); - } while (wlen--); } else { getcmd_cbeep(); } @@ -658,35 +646,21 @@ static int cread_line(const FLASH char *const prompt, char *buf, #endif default: if (isprint(ichar)) - cread_add_char(ichar, insert, &num, &eol_num, buf, - *len); + cread_add_char(ichar, insert, &num, &eol_num, buf, len); break; } } - *len = eol_num; + while (eol_num && buf[eol_num-1] == ' ') + --eol_num; /* remove trailing blanks */ buf[eol_num] = '\0'; /* lose the newline */ if (enable_history && buf[0]) cread_add_to_hist(buf); - return 0; + return eol_num; } /****************************************************************************/ -static int cli_readline_into_buffer(const FLASH char *const prompt, - char *buffer, bool enable_history) -{ - char *p = buffer; - uint_fast8_t len = CONFIG_SYS_CBSIZE; - int rc; - - if (prompt) - my_puts_P(prompt); - - rc = cread_line(prompt, p, &len, enable_history); - return rc < 0 ? rc : (int) len; -} - int cli_readline(const FLASH char *const prompt, bool enable_history) { /* @@ -695,5 +669,8 @@ int cli_readline(const FLASH char *const prompt, bool enable_history) */ console_buffer[0] = '\0'; - return cli_readline_into_buffer(prompt, console_buffer, enable_history); + if (prompt) + my_puts_P(prompt); + + return cread_line(prompt, console_buffer, CONFIG_SYS_CBSIZE, enable_history); } diff --git a/include/cli_readline.h b/include/cli_readline.h index 0ce28a5..f326106 100644 --- a/include/cli_readline.h +++ b/include/cli_readline.h @@ -18,38 +18,17 @@ extern char console_buffer[]; /* console I/O buffer */ /** * cli_readline() - read a line into the console_buffer * - * This is a convenience function which calls cli_readline_into_buffer(). - * - * @prompt: Prompt to display - * @enable_history: Use history buffer if true - * @return command line length excluding terminator, or -ve on error - */ -int cli_readline(const FLASH char *const prompt, bool enable_history); - -/** - * readline_into_buffer() - read a line into a buffer - * - * Display the prompt, then read a command line into @buffer. The + * Display the prompt, then read a command line into console_buffer. The * maximum line length is CONFIG_SYS_CBSIZE including a \0 terminator, which * will always be added. * * The command is echoed as it is typed. Command editing is supported. * Tab auto-complete is supported if CONFIG_AUTO_COMPLETE is defined. - * If CONFIG_BOOT_RETRY_TIME is defined, then a timeout will be applied. - * - * If CONFIG_BOOT_RETRY_TIME is defined and retry_time >= 0, - * time out when time goes past endtime (timebase time in ticks). * - * @prompt: Prompt to display - * @buffer: Place to put the line that is entered - * @timeout: Timeout in milliseconds, 0 if none - * @return command line length excluding terminator, or -ve on error: of the - * timeout is exceeded (either CONFIG_BOOT_RETRY_TIME or the timeout - * parameter), then -2 is returned. If a break is detected (Ctrl-C) then - * -1 is returned. + * @prompt: Prompt to display + * @enable_history: Use history buffer if true + * @return command line length excluding terminator, or -ve on error */ -//int cli_readline_into_buffer(const char *const prompt, char *buffer, -// int timeout, bool enable_history); - +int cli_readline(const FLASH char *const prompt, bool enable_history); #endif /* CLI_READLINE_H */ |