From 58f108eb339957f58d5a6034d82b09c4d50b53e3 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 11 Mar 2010 21:17:55 +0100 Subject: lineedit: fix another corner case with bad unicode input function old new delta read_key 607 646 +39 readit 50 55 +5 getch_nowait 290 295 +5 hash_find 233 234 +1 xstrtoul_range_sfx 231 230 -1 passwd_main 1058 1056 -2 builtin_exit 45 43 -2 cmp_main 649 645 -4 lineedit_read_key 257 245 -12 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 4/5 up/down: 50/-21) Total: 29 bytes Signed-off-by: Denys Vlasenko --- libbb/read_key.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'libbb/read_key.c') diff --git a/libbb/read_key.c b/libbb/read_key.c index 98b3131de..0faa12c97 100644 --- a/libbb/read_key.c +++ b/libbb/read_key.c @@ -9,7 +9,7 @@ */ #include "libbb.h" -int64_t FAST_FUNC read_key(int fd, char *buffer) +int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout) { struct pollfd pfd; const char *seq; @@ -90,14 +90,27 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) /* ESC [ Z - Shift-Tab */ }; + pfd.fd = fd; + pfd.events = POLLIN; + buffer++; /* saved chars counter is in buffer[-1] now */ start_over: errno = 0; n = (unsigned char)buffer[-1]; if (n == 0) { - /* If no data, block waiting for input. - * It is tempting to read more than one byte here, + /* If no data, wait for input. + * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful + * if fd can be in non-blocking mode. + */ + if (timeout >= -1) { + if (safe_poll(&pfd, 1, timeout) == 0) { + /* Timed out */ + errno = EAGAIN; + return -1; + } + } + /* It is tempting to read more than one byte here, * but it breaks pasting. Example: at shell prompt, * user presses "c","a","t" and then pastes "\nline\n". * When we were reading 3 bytes here, we were eating @@ -121,8 +134,6 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) } /* Loop through known ESC sequences */ - pfd.fd = fd; - pfd.events = POLLIN; seq = esccmds; while (*seq != '\0') { /* n - position in sequence we did not read yet */ -- cgit v1.2.3