diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-26 15:23:32 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-10-26 15:23:32 +0100 |
commit | 727e1b536e5478b8f28c93990a5bf34091144608 (patch) | |
tree | c8993291a1a03a06fcb7a2382e6cc211e6d7861e /libbb/read_key.c | |
parent | d31a8793ebedb53b686c9ea01be33d3d564760b9 (diff) | |
download | busybox-w32-727e1b536e5478b8f28c93990a5bf34091144608.tar.gz busybox-w32-727e1b536e5478b8f28c93990a5bf34091144608.tar.bz2 busybox-w32-727e1b536e5478b8f28c93990a5bf34091144608.zip |
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 <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/read_key.c')
-rw-r--r-- | libbb/read_key.c | 53 |
1 files changed, 25 insertions, 28 deletions
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) | |||
202 | break; | 202 | break; |
203 | } | 203 | } |
204 | n++; | 204 | n++; |
205 | /* Try to decipher "ESC [ NNN ; NNN R" sequence */ | ||
206 | if (ENABLE_FEATURE_EDITING_ASK_TERMINAL | ||
207 | && n >= 5 | ||
208 | && buffer[0] == '[' | ||
209 | && buffer[n-1] == 'R' | ||
210 | && isdigit(buffer[1]) | ||
211 | ) { | ||
212 | char *end; | ||
213 | unsigned long row, col; | ||
214 | |||
215 | row = strtoul(buffer + 1, &end, 10); | ||
216 | if (*end != ';' || !isdigit(end[1])) | ||
217 | continue; | ||
218 | col = strtoul(end + 1, &end, 10); | ||
219 | if (*end != 'R') | ||
220 | continue; | ||
221 | if (row < 1 || col < 1 || (row | col) > 0x7fff) | ||
222 | continue; | ||
223 | |||
224 | buffer[-1] = 0; | ||
225 | /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */ | ||
226 | col |= (((-1 << 15) | row) << 16); | ||
227 | /* Return it in high-order word */ | ||
228 | return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS; | ||
229 | } | ||
205 | } | 230 | } |
206 | got_all: | 231 | got_all: |
207 | 232 | ||
@@ -213,34 +238,6 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) | |||
213 | return 27; | 238 | return 27; |
214 | } | 239 | } |
215 | 240 | ||
216 | /* Try to decipher "ESC [ NNN ; NNN R" sequence */ | ||
217 | if (ENABLE_FEATURE_EDITING_ASK_TERMINAL | ||
218 | && n >= 5 | ||
219 | && buffer[0] == '[' | ||
220 | && isdigit(buffer[1]) | ||
221 | && buffer[n-1] == 'R' | ||
222 | ) { | ||
223 | char *end; | ||
224 | unsigned long row, col; | ||
225 | |||
226 | row = strtoul(buffer + 1, &end, 10); | ||
227 | if (*end != ';' || !isdigit(end[1])) | ||
228 | goto not_R; | ||
229 | col = strtoul(end + 1, &end, 10); | ||
230 | if (*end != 'R') | ||
231 | goto not_R; | ||
232 | if (row < 1 || col < 1 || (row | col) > 0x7fff) | ||
233 | goto not_R; | ||
234 | |||
235 | buffer[-1] = 0; | ||
236 | |||
237 | /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */ | ||
238 | col |= (((-1 << 15) | row) << 16); | ||
239 | /* Return it in high-order word */ | ||
240 | return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS; | ||
241 | } | ||
242 | not_R: | ||
243 | |||
244 | /* We were doing "buffer[-1] = n; return c;" here, but this results | 241 | /* We were doing "buffer[-1] = n; return c;" here, but this results |
245 | * in unknown key sequences being interpreted as ESC + garbage. | 242 | * in unknown key sequences being interpreted as ESC + garbage. |
246 | * This was not useful. Pretend there was no key pressed, | 243 | * This was not useful. Pretend there was no key pressed, |