cli: Correct several bugs in cli_getch()

This function does not behave as expected when unknown escape sequences
are sent to it:

- it fails to store (and thus echo) the last character of the invalid
  sequence
- it fails to set esc_len to 0 when it finishes emitting the invalid
  sequence, meaning that the following character will appear to be part
  of a new escape sequence
- it processes the first character of the rejected sequence as a valid
  character, just starting the sequence all over again

The last two bugs conspire to produce an "impossible condition #876"
message which is the main symptom of this behaviour.

Fix these bugs and add a test to verify the behaviour.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reported-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
diff --git a/test/common/Makefile b/test/common/Makefile
index cc918f6..a5ab10f 100644
--- a/test/common/Makefile
+++ b/test/common/Makefile
@@ -3,3 +3,4 @@
 obj-$(CONFIG_AUTOBOOT) += test_autoboot.o
 obj-$(CONFIG_CYCLIC) += cyclic.o
 obj-$(CONFIG_EVENT) += event.o
+obj-y += cread.o
diff --git a/test/common/cread.c b/test/common/cread.c
new file mode 100644
index 0000000..3dce4bd
--- /dev/null
+++ b/test/common/cread.c
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2023 Google LLC
+ */
+
+#include <common.h>
+#include <cli.h>
+#include <test/common.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+static int cli_ch_test(struct unit_test_state *uts)
+{
+	struct cli_ch_state s_cch, *cch = &s_cch;
+
+	cli_ch_init(cch);
+
+	/* should be nothing to return at first */
+	ut_asserteq(0, cli_ch_process(cch, 0));
+
+	/* check normal entry */
+	ut_asserteq('a', cli_ch_process(cch, 'a'));
+	ut_asserteq('b', cli_ch_process(cch, 'b'));
+	ut_asserteq('c', cli_ch_process(cch, 'c'));
+	ut_asserteq(0, cli_ch_process(cch, 0));
+
+	/* send an invalid escape sequence */
+	ut_asserteq(0, cli_ch_process(cch, '\e'));
+	ut_asserteq(0, cli_ch_process(cch, '['));
+
+	/*
+	 * with the next char it sees that the sequence is invalid, so starts
+	 * emitting it
+	 */
+	ut_asserteq('\e', cli_ch_process(cch, 'X'));
+
+	/* now we set 0 bytes to empty the buffer */
+	ut_asserteq('[', cli_ch_process(cch, 0));
+	ut_asserteq('X', cli_ch_process(cch, 0));
+	ut_asserteq(0, cli_ch_process(cch, 0));
+
+	/* things are normal again */
+	ut_asserteq('a', cli_ch_process(cch, 'a'));
+	ut_asserteq(0, cli_ch_process(cch, 0));
+
+	return 0;
+}
+COMMON_TEST(cli_ch_test, 0);