Commit 53b63136 authored by Daniel Thompson's avatar Daniel Thompson

kdb: Tidy up code to handle escape sequences

kdb_read_get_key() has extremely complex break/continue control flow
managed by state variables and is very hard to review or modify. In
particular the way the escape sequence handling interacts with the
general control flow is hard to follow. Separate out the escape key
handling, without changing the control flow. This makes the main body of
the code easier to review.
Signed-off-by: default avatarDaniel Thompson <daniel.thompson@linaro.org>
Reviewed-by: default avatarDouglas Anderson <dianders@chromium.org>
Link: https://lore.kernel.org/r/20191025073328.643-2-daniel.thompson@linaro.org
parent d07ce4e3
......@@ -49,6 +49,65 @@ static int kgdb_transition_check(char *buffer)
return 0;
}
/**
* kdb_handle_escape() - validity check on an accumulated escape sequence.
* @buf: Accumulated escape characters to be examined. Note that buf
* is not a string, it is an array of characters and need not be
* nil terminated.
* @sz: Number of accumulated escape characters.
*
* Return: -1 if the escape sequence is unwanted, 0 if it is incomplete,
* otherwise it returns a mapped key value to pass to the upper layers.
*/
static int kdb_handle_escape(char *buf, size_t sz)
{
char *lastkey = buf + sz - 1;
switch (sz) {
case 1:
if (*lastkey == '\e')
return 0;
break;
case 2: /* \e<something> */
if (*lastkey == '[')
return 0;
break;
case 3:
switch (*lastkey) {
case 'A': /* \e[A, up arrow */
return 16;
case 'B': /* \e[B, down arrow */
return 14;
case 'C': /* \e[C, right arrow */
return 6;
case 'D': /* \e[D, left arrow */
return 2;
case '1': /* \e[<1,3,4>], may be home, del, end */
case '3':
case '4':
return 0;
}
break;
case 4:
if (*lastkey == '~') {
switch (buf[2]) {
case '1': /* \e[1~, home */
return 1;
case '3': /* \e[3~, del */
return 4;
case '4': /* \e[4~, end */
return 5;
}
}
break;
}
return -1;
}
static int kdb_read_get_key(char *buffer, size_t bufsize)
{
#define ESCAPE_UDELAY 1000
......@@ -102,68 +161,15 @@ static int kdb_read_get_key(char *buffer, size_t bufsize)
escape_delay = 2;
continue;
}
if (ped - escape_data == 1) {
/* \e */
continue;
} else if (ped - escape_data == 2) {
/* \e<something> */
if (key != '[')
escape_delay = 2;
continue;
} else if (ped - escape_data == 3) {
/* \e[<something> */
int mapkey = 0;
switch (key) {
case 'A': /* \e[A, up arrow */
mapkey = 16;
break;
case 'B': /* \e[B, down arrow */
mapkey = 14;
break;
case 'C': /* \e[C, right arrow */
mapkey = 6;
break;
case 'D': /* \e[D, left arrow */
mapkey = 2;
break;
case '1': /* dropthrough */
case '3': /* dropthrough */
/* \e[<1,3,4>], may be home, del, end */
case '4':
mapkey = -1;
break;
}
if (mapkey != -1) {
if (mapkey > 0) {
escape_data[0] = mapkey;
escape_data[1] = '\0';
}
escape_delay = 2;
}
continue;
} else if (ped - escape_data == 4) {
/* \e[<1,3,4><something> */
int mapkey = 0;
if (key == '~') {
switch (escape_data[2]) {
case '1': /* \e[1~, home */
mapkey = 1;
break;
case '3': /* \e[3~, del */
mapkey = 4;
break;
case '4': /* \e[4~, end */
mapkey = 5;
break;
}
}
if (mapkey > 0) {
escape_data[0] = mapkey;
escape_data[1] = '\0';
}
escape_delay = 2;
continue;
key = kdb_handle_escape(escape_data, ped - escape_data);
if (key > 0) {
escape_data[0] = key;
escape_data[1] = '\0';
}
if (key)
escape_delay = 2;
continue;
}
break; /* A key to process */
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment