From 727e1b536e5478b8f28c93990a5bf34091144608 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Mon, 26 Oct 2009 15:23:32 +0100 Subject: read_key,lineeedit: parse position answerback faster; sanitize its use it's still not reliable, and probably cannot be made so... added comment with explanation. function old new delta put_prompt 52 110 +58 read_key 601 607 +6 lineedit_read_key 201 207 +6 win_changed 108 104 -4 read_line_input 4824 4809 -15 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 3/2 up/down: 70/-19) Total: 51 bytes Signed-off-by: Denys Vlasenko --- libbb/read_key.c | 53 +++++++++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) (limited to 'libbb/read_key.c') diff --git a/libbb/read_key.c b/libbb/read_key.c index ec1b3a4a8..a2253ce3e 100644 --- a/libbb/read_key.c +++ b/libbb/read_key.c @@ -202,6 +202,31 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) break; } n++; + /* Try to decipher "ESC [ NNN ; NNN R" sequence */ + if (ENABLE_FEATURE_EDITING_ASK_TERMINAL + && n >= 5 + && buffer[0] == '[' + && buffer[n-1] == 'R' + && isdigit(buffer[1]) + ) { + char *end; + unsigned long row, col; + + row = strtoul(buffer + 1, &end, 10); + if (*end != ';' || !isdigit(end[1])) + continue; + col = strtoul(end + 1, &end, 10); + if (*end != 'R') + continue; + if (row < 1 || col < 1 || (row | col) > 0x7fff) + continue; + + buffer[-1] = 0; + /* Pack into "1 " 32-bit sequence */ + col |= (((-1 << 15) | row) << 16); + /* Return it in high-order word */ + return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS; + } } got_all: @@ -213,34 +238,6 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) return 27; } - /* Try to decipher "ESC [ NNN ; NNN R" sequence */ - if (ENABLE_FEATURE_EDITING_ASK_TERMINAL - && n >= 5 - && buffer[0] == '[' - && isdigit(buffer[1]) - && buffer[n-1] == 'R' - ) { - char *end; - unsigned long row, col; - - row = strtoul(buffer + 1, &end, 10); - if (*end != ';' || !isdigit(end[1])) - goto not_R; - col = strtoul(end + 1, &end, 10); - if (*end != 'R') - goto not_R; - if (row < 1 || col < 1 || (row | col) > 0x7fff) - goto not_R; - - buffer[-1] = 0; - - /* Pack into "1 " 32-bit sequence */ - col |= (((-1 << 15) | row) << 16); - /* Return it in high-order word */ - return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS; - } - not_R: - /* We were doing "buffer[-1] = n; return c;" here, but this results * in unknown key sequences being interpreted as ESC + garbage. * This was not useful. Pretend there was no key pressed, -- cgit v1.2.3