diff options
Diffstat (limited to 'libbb/read_key.c')
-rw-r--r-- | libbb/read_key.c | 21 |
1 files changed, 16 insertions, 5 deletions
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 @@ | |||
9 | */ | 9 | */ |
10 | #include "libbb.h" | 10 | #include "libbb.h" |
11 | 11 | ||
12 | int64_t FAST_FUNC read_key(int fd, char *buffer) | 12 | int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout) |
13 | { | 13 | { |
14 | struct pollfd pfd; | 14 | struct pollfd pfd; |
15 | const char *seq; | 15 | const char *seq; |
@@ -90,14 +90,27 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) | |||
90 | /* ESC [ Z - Shift-Tab */ | 90 | /* ESC [ Z - Shift-Tab */ |
91 | }; | 91 | }; |
92 | 92 | ||
93 | pfd.fd = fd; | ||
94 | pfd.events = POLLIN; | ||
95 | |||
93 | buffer++; /* saved chars counter is in buffer[-1] now */ | 96 | buffer++; /* saved chars counter is in buffer[-1] now */ |
94 | 97 | ||
95 | start_over: | 98 | start_over: |
96 | errno = 0; | 99 | errno = 0; |
97 | n = (unsigned char)buffer[-1]; | 100 | n = (unsigned char)buffer[-1]; |
98 | if (n == 0) { | 101 | if (n == 0) { |
99 | /* If no data, block waiting for input. | 102 | /* If no data, wait for input. |
100 | * It is tempting to read more than one byte here, | 103 | * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful |
104 | * if fd can be in non-blocking mode. | ||
105 | */ | ||
106 | if (timeout >= -1) { | ||
107 | if (safe_poll(&pfd, 1, timeout) == 0) { | ||
108 | /* Timed out */ | ||
109 | errno = EAGAIN; | ||
110 | return -1; | ||
111 | } | ||
112 | } | ||
113 | /* It is tempting to read more than one byte here, | ||
101 | * but it breaks pasting. Example: at shell prompt, | 114 | * but it breaks pasting. Example: at shell prompt, |
102 | * user presses "c","a","t" and then pastes "\nline\n". | 115 | * user presses "c","a","t" and then pastes "\nline\n". |
103 | * When we were reading 3 bytes here, we were eating | 116 | * When we were reading 3 bytes here, we were eating |
@@ -121,8 +134,6 @@ int64_t FAST_FUNC read_key(int fd, char *buffer) | |||
121 | } | 134 | } |
122 | 135 | ||
123 | /* Loop through known ESC sequences */ | 136 | /* Loop through known ESC sequences */ |
124 | pfd.fd = fd; | ||
125 | pfd.events = POLLIN; | ||
126 | seq = esccmds; | 137 | seq = esccmds; |
127 | while (*seq != '\0') { | 138 | while (*seq != '\0') { |
128 | /* n - position in sequence we did not read yet */ | 139 | /* n - position in sequence we did not read yet */ |