From patchwork Sun Jan 12 16:33:38 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 239490 List-Id: U-Boot discussion From: xypron.glpk at gmx.de (Heinrich Schuchardt) Date: Sun, 12 Jan 2020 17:33:38 +0100 Subject: [PATCH 1/2] cli: allow verbatim character entry with CTRL-v In-Reply-To: <20200112163339.218205-1-xypron.glpk@gmx.de> References: <20200112163339.218205-1-xypron.glpk@gmx.de> Message-ID: <20200112163339.218205-2-xypron.glpk@gmx.de> Up to now the U-Boot console does not allow to add a control character to the line buffer. In Bash and Vim we can use CTRL-v for this purpose. Add support for CTRL-v for verbatim entry of characters. To keep things simple, while editing control characters are displayed as simple characters, e.g. ESC (0x1b) is displayed as [ and not as ^[. Signed-off-by: Heinrich Schuchardt Reviewed-by: Simon Glass --- common/cli_readline.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) -- 2.24.1 diff --git a/common/cli_readline.c b/common/cli_readline.c index 6ef7a3e564..402f15a361 100644 --- a/common/cli_readline.c +++ b/common/cli_readline.c @@ -58,8 +58,6 @@ static char *delete_char (char *buffer, char *p, int *colp, int *np, int plen) * Author: Janghoon Lyu */ -#define putnstr(str, n) printf("%.*s", (int)n, str) - #define CTL_CH(c) ((c) - 'a' + 1) #define CTL_BACKSPACE ('\b') #define DEL ((char)255) @@ -83,6 +81,34 @@ static char hist_lines[HIST_MAX][HIST_SIZE + 1]; /* Save room for NULL */ #define add_idx_minus_one() ((hist_add_idx == 0) ? hist_max : hist_add_idx-1) +/** + * putchar() - output single character + * + * Outputs single character. Control characters are converted, + * e.g. CTR-c is displayed as C. + */ +static void putchar(char c) +{ + if (c < ' ') + c += '@'; + putc(c); +} + +/** + * putnstr() - output string buffer + * + * Outputs n characters for buffer str. Control characters are converted, e.g. + * CTRL-c is displayed as C. + * + * @str: string buffer + * @n: number of characters to print + */ +static void putnstr(const char *str, size_t n) +{ + for (; n; --n) + putchar(*str++); +} + static void hist_init(void) { int i; @@ -385,7 +411,7 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, return -1; case CTL_CH('f'): if (num < eol_num) { - getcmd_putch(buf[num]); + putchar(buf[num]); num++; } break; @@ -419,6 +445,12 @@ static int cread_line(const char *const prompt, char *buf, unsigned int *len, case CTL_CH('o'): insert = !insert; break; + case CTL_CH('v'): + /* Add next character verbatim */ + ichar = getcmd_getch(); + cread_add_char(ichar, insert, &num, &eol_num, buf, + *len); + break; case CTL_CH('x'): case CTL_CH('u'): BEGINNING_OF_LINE();