menu: Make use of CLI character processing

Avoid duplicating some of the escape-sequence processing here and use the
CLI function instead.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/common/cli_getch.c b/common/cli_getch.c
index 9eeea7f..87c23ed 100644
--- a/common/cli_getch.c
+++ b/common/cli_getch.c
@@ -140,10 +140,11 @@
 	 * sequence
 	 */
 	if (!ichar) {
-		if (cch->emit_upto) {
+		if (cch->emitting) {
 			if (cch->emit_upto < cch->esc_len)
 				return cch->esc_save[cch->emit_upto++];
 			cch->emit_upto = 0;
+			cch->emitting = false;
 		}
 		return 0;
 	} else if (ichar == -ETIMEDOUT) {
@@ -174,18 +175,21 @@
 		case ESC_SAVE:
 			/* save this character and return nothing */
 			cch->esc_save[cch->esc_len++] = ichar;
-			return 0;
+			ichar = 0;
+			break;
 		case ESC_REJECT:
 			/*
 			 * invalid escape sequence, start returning the
 			 * characters in it
 			 */
 			cch->esc_save[cch->esc_len++] = ichar;
-			return cch->esc_save[cch->emit_upto++];
+			ichar = cch->esc_save[cch->emit_upto++];
+			cch->emitting = true;
+			break;
 		case ESC_CONVERTED:
 			/* valid escape sequence, return the resulting char */
 			cch->esc_len = 0;
-			return ichar;
+			break;
 		}
 	}
 
diff --git a/common/menu.c b/common/menu.c
index 7db9894..45f36ae 100644
--- a/common/menu.c
+++ b/common/menu.c
@@ -15,6 +15,8 @@
 
 #include "menu.h"
 
+#define ansi 0
+
 /*
  * Internally, each item in a menu is represented by a struct menu_item.
  *
@@ -425,15 +427,19 @@
 	return 1;
 }
 
-enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc)
+enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
+					 struct cli_ch_state *cch)
 {
 	enum bootmenu_key key = BKEY_NONE;
 	int i, c;
 
 	while (menu->delay > 0) {
-		printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
+		if (ansi)
+			printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
 		printf("Hit any key to stop autoboot: %d ", menu->delay);
 		for (i = 0; i < 100; ++i) {
+			int ichar;
+
 			if (!tstc()) {
 				schedule();
 				mdelay(10);
@@ -443,12 +449,13 @@
 			menu->delay = -1;
 			c = getchar();
 
-			switch (c) {
-			case '\e':
-				*esc = 1;
+			ichar = cli_ch_process(cch, c);
+
+			switch (ichar) {
+			case '\0':
 				key = BKEY_NONE;
 				break;
-			case '\r':
+			case '\n':
 				key = BKEY_SELECT;
 				break;
 			case 0x3: /* ^C */
@@ -458,7 +465,6 @@
 				key = BKEY_NONE;
 				break;
 			}
-
 			break;
 		}
 
@@ -468,7 +474,8 @@
 		--menu->delay;
 	}
 
-	printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
+	if (ansi)
+		printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
 
 	if (menu->delay == 0)
 		key = BKEY_SELECT;
@@ -476,79 +483,32 @@
 	return key;
 }
 
-enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc)
+enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
+				struct cli_ch_state *cch)
 {
 	enum bootmenu_key key = BKEY_NONE;
 	int c;
 
-	if (*esc == 1) {
-		if (tstc()) {
+	c = cli_ch_process(cch, 0);
+	if (!c) {
+		while (!c && !tstc()) {
+			schedule();
+			mdelay(10);
+			c = cli_ch_process(cch, -ETIMEDOUT);
+		}
+		if (!c) {
 			c = getchar();
-		} else {
-			schedule();
-			mdelay(10);
-			if (tstc())
-				c = getchar();
-			else
-				c = '\e';
+			c = cli_ch_process(cch, c);
 		}
-	} else {
-		while (!tstc()) {
-			schedule();
-			mdelay(10);
-		}
-		c = getchar();
-	}
-
-	switch (*esc) {
-	case 0:
-		/* First char of ANSI escape sequence '\e' */
-		if (c == '\e') {
-			*esc = 1;
-			key = BKEY_NONE;
-		}
-		break;
-	case 1:
-		/* Second char of ANSI '[' */
-		if (c == '[') {
-			*esc = 2;
-			key = BKEY_NONE;
-		} else {
-		/* Alone ESC key was pressed */
-			key = BKEY_QUIT;
-			*esc = (c == '\e') ? 1 : 0;
-		}
-		break;
-	case 2:
-	case 3:
-		/* Third char of ANSI (number '1') - optional */
-		if (*esc == 2 && c == '1') {
-			*esc = 3;
-			key = BKEY_NONE;
-			break;
-		}
-
-		*esc = 0;
-
-		/* ANSI 'A' - key up was pressed */
-		if (c == 'A')
-			key = BKEY_UP;
-		/* ANSI 'B' - key down was pressed */
-		else if (c == 'B')
-			key = BKEY_DOWN;
-		/* other key was pressed */
-		else
-			key = BKEY_NONE;
-
-		break;
 	}
 
 	switch (c) {
-	case '\r':
+	case '\n':
 		/* enter key was pressed */
 		key = BKEY_SELECT;
 		break;
 	case CTL_CH('c'):
+	case '\e':
 		/* ^C was pressed */
 		key = BKEY_QUIT;
 		break;