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(); From patchwork Sun Jan 12 16:33:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Heinrich Schuchardt X-Patchwork-Id: 239491 List-Id: U-Boot discussion From: xypron.glpk at gmx.de (Heinrich Schuchardt) Date: Sun, 12 Jan 2020 17:33:39 +0100 Subject: [PATCH 2/2] test: 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-3-xypron.glpk@gmx.de> Provide a unit test checking that CTRL-V can be used to add control characters to the line buffer. Signed-off-by: Heinrich Schuchardt --- test/dm/Makefile | 2 +- test/dm/usb_console.c | 150 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 test/dm/usb_console.c -- 2.24.1 diff --git a/test/dm/Makefile b/test/dm/Makefile index dd1ceff86c..0261424168 100644 --- a/test/dm/Makefile +++ b/test/dm/Makefile @@ -51,7 +51,7 @@ obj-$(CONFIG_DM_SPI_FLASH) += sf.o obj-$(CONFIG_SMEM) += smem.o obj-$(CONFIG_DM_SPI) += spi.o obj-y += syscon.o -obj-$(CONFIG_DM_USB) += usb.o +obj-$(CONFIG_DM_USB) += usb.o usb_console.o obj-$(CONFIG_DM_PMIC) += pmic.o obj-$(CONFIG_DM_REGULATOR) += regulator.o obj-$(CONFIG_TIMER) += timer.o diff --git a/test/dm/usb_console.c b/test/dm/usb_console.c new file mode 100644 index 0000000000..c62a05291d --- /dev/null +++ b/test/dm/usb_console.c @@ -0,0 +1,150 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2019 Heinrich Schuchardt + * + * Tests for the command line interface using the USB keyboard as input. + */ + +#include +#include +#include +#include +#include +#include +#include + +#define SCAN_CODE_END_OF_BUFFER 0xff + +struct console_test_data { + const char modifiers; + const unsigned char scancode; +}; + +/** + * put_buffer() - copy key strokes to USB keyboard buffer + * + * @key_strokes: key strokes + * Return: 0 = ok + */ +static int put_buffer(const struct console_test_data *key_strokes, + struct unit_test_state *uts) +{ + struct udevice *dev; + const struct console_test_data *pos; + + ut_assertok(uclass_get_device_by_name(UCLASS_USB_EMUL, "keyb at 3", + &dev)); + for (pos = key_strokes; pos->scancode != SCAN_CODE_END_OF_BUFFER; + ++pos) { + char scancodes[USB_KBD_BOOT_REPORT_SIZE] = {0}; + + scancodes[0] = pos->modifiers; + scancodes[2] = pos->scancode; + + ut_assertok(sandbox_usb_keyb_add_string(dev, scancodes)); + } + ut_asserteq(1, tstc()); + + return 0; +} + +/** + * usb_console_setup() - setup for usb keyboard test + * + * Return: 0 = ok + */ +static int usb_console_setup(struct unit_test_state *uts) +{ + state_set_skip_delays(true); + ut_assertok(usb_init()); + + /* Initially there should be no characters */ + ut_asserteq(0, tstc()); + + return 0; +} + +/** + * usb_console_teardown() - tear down after usb keyboard test + * + * Return: 0 = ok + */ +static int usb_console_teardown(struct unit_test_state *uts) +{ + ut_assertok(usb_stop()); + return 0; +} + +/** + * dm_test_usb_console_verbatim() - test verbatim entry in console + * + * This test copies the key strokes + * + * setenv error '[33;1mError[0m' + * + * into the buffer of the USB keyboard emulation driver. It then checks if the + * string is read into a command line interface buffer as + * + * "setenv error '\x1b[33;1mError\x1b[0m'" + * + * This confirms that can be used for verbatim input of control + * characters. + * + * @uts: unit test state + * Return: 0 on success + */ +static int dm_test_usb_console_verbatim(struct unit_test_state *uts) +{ + char buffer[CONFIG_SYS_CBSIZE + 1]; /* console I/O buffer */ + const struct console_test_data con_test_data[] = { + {0x00, 0x16}, /* s */ + {0x00, 0x08}, /* e */ + {0x00, 0x17}, /* t */ + {0x00, 0x08}, /* e */ + {0x00, 0x11}, /* n */ + {0x00, 0x19}, /* v */ + {0x00, 0x2c}, /* */ + {0x00, 0x08}, /* e */ + {0x00, 0x15}, /* r */ + {0x00, 0x00}, /* */ + {0x00, 0x15}, /* r */ + {0x00, 0x12}, /* o */ + {0x00, 0x15}, /* r */ + {0x00, 0x2c}, /* */ + {0x00, 0x34}, /* ' */ + {0x01, 0x19}, /* */ + {0x00, 0x29}, /* */ + {0x00, 0x2F}, /* [ */ + {0x00, 0x20}, /* 3 */ + {0x00, 0x00}, /* */ + {0x00, 0x20}, /* 3 */ + {0x00, 0x33}, /* ; */ + {0x00, 0x1E}, /* 1 */ + {0x00, 0x10}, /* m */ + {0x20, 0x08}, /* E */ + {0x00, 0x15}, /* r */ + {0x00, 0x00}, /* */ + {0x00, 0x15}, /* r */ + {0x00, 0x12}, /* o */ + {0x00, 0x15}, /* r */ + {0x01, 0x19}, /* */ + {0x00, 0x29}, /* */ + {0x00, 0x2F}, /* [ */ + {0x00, 0x27}, /* 0 */ + {0x00, 0x10}, /* m */ + {0x00, 0x34}, /* ' */ + {0x00, 0x28}, /* */ + /* End of list */ + {0x00, SCAN_CODE_END_OF_BUFFER} + }; + + ut_assertok(usb_console_setup(uts)); + ut_assertok(put_buffer(con_test_data, uts)); + ut_asserteq(31, cli_readline_into_buffer("", buffer, 0)); + ut_asserteq_str("setenv error '\x1b[33;1mError\x1b[0m'", buffer); + ut_asserteq(0, tstc()); + ut_assertok(usb_console_teardown(uts)); + + return 0; +} +DM_TEST(dm_test_usb_console_verbatim, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);