Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Denis Vlasenko | 7462b21 | 2008-10-26 00:19:33 +0000 | [diff] [blame] | 5 | * Copyright (C) 2008 Rob Landley <rob@landley.net> |
| 6 | * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com> |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 9 | */ |
| 10 | #include "libbb.h" |
| 11 | |
Denys Vlasenko | 58f108e | 2010-03-11 21:17:55 +0100 | [diff] [blame] | 12 | int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout) |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 13 | { |
| 14 | struct pollfd pfd; |
| 15 | const char *seq; |
| 16 | int n; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 17 | |
Denys Vlasenko | 0ccae4d | 2012-06-11 14:40:17 +0200 | [diff] [blame] | 18 | /* Known escape sequences for cursor and function keys. |
| 19 | * See "Xterm Control Sequences" |
| 20 | * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html |
Rostislav Skudnov | 2e4ef38 | 2016-11-24 15:04:00 +0100 | [diff] [blame] | 21 | * Array should be sorted from shortest to longest. |
Denys Vlasenko | 0ccae4d | 2012-06-11 14:40:17 +0200 | [diff] [blame] | 22 | */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 23 | static const char esccmds[] ALIGN1 = { |
Rostislav Skudnov | 2e4ef38 | 2016-11-24 15:04:00 +0100 | [diff] [blame] | 24 | '\x7f' |0x80,KEYCODE_ALT_BACKSPACE, |
| 25 | '\b' |0x80,KEYCODE_ALT_BACKSPACE, |
| 26 | 'd' |0x80,KEYCODE_ALT_D , |
| 27 | /* lineedit mimics bash: Alt-f and Alt-b are forward/backward |
| 28 | * word jumps. We cheat here and make them return ALT_LEFT/RIGHT |
| 29 | * keycodes. This way, lineedit need no special code to handle them. |
| 30 | * If we'll need to distinguish them, introduce new ALT_F/B keycodes, |
| 31 | * and update lineedit to react to them. |
| 32 | */ |
| 33 | 'f' |0x80,KEYCODE_ALT_RIGHT, |
| 34 | 'b' |0x80,KEYCODE_ALT_LEFT, |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 35 | 'O','A' |0x80,KEYCODE_UP , |
| 36 | 'O','B' |0x80,KEYCODE_DOWN , |
| 37 | 'O','C' |0x80,KEYCODE_RIGHT , |
| 38 | 'O','D' |0x80,KEYCODE_LEFT , |
| 39 | 'O','H' |0x80,KEYCODE_HOME , |
| 40 | 'O','F' |0x80,KEYCODE_END , |
| 41 | #if 0 |
| 42 | 'O','P' |0x80,KEYCODE_FUN1 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 43 | /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */ |
Denys Vlasenko | 9d71fc6 | 2009-10-26 00:50:52 +0100 | [diff] [blame] | 44 | /* ESC [ O 1 ; 2 P - Shift-F1 */ |
| 45 | /* ESC [ O 1 ; 3 P - Alt-F1 */ |
| 46 | /* ESC [ O 1 ; 4 P - Alt-Shift-F1 */ |
| 47 | /* ESC [ O 1 ; 5 P - Ctrl-F1 */ |
| 48 | /* ESC [ O 1 ; 6 P - Ctrl-Shift-F1 */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 49 | 'O','Q' |0x80,KEYCODE_FUN2 , |
| 50 | 'O','R' |0x80,KEYCODE_FUN3 , |
| 51 | 'O','S' |0x80,KEYCODE_FUN4 , |
| 52 | #endif |
| 53 | '[','A' |0x80,KEYCODE_UP , |
| 54 | '[','B' |0x80,KEYCODE_DOWN , |
| 55 | '[','C' |0x80,KEYCODE_RIGHT , |
| 56 | '[','D' |0x80,KEYCODE_LEFT , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 57 | /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */ |
Denys Vlasenko | 9ce09bc | 2011-11-03 13:28:22 +0100 | [diff] [blame] | 58 | /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below */ |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 59 | /* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */ |
| 60 | /* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */ |
| 61 | /* ESC [ 1 ; 6 x, where x = A/B/C/D: Ctrl-Shift-<arrow> */ |
Denys Vlasenko | 0ccae4d | 2012-06-11 14:40:17 +0200 | [diff] [blame] | 62 | /* ESC [ 1 ; 7 x, where x = A/B/C/D: Ctrl-Alt-<arrow> */ |
| 63 | /* ESC [ 1 ; 8 x, where x = A/B/C/D: Ctrl-Alt-Shift-<arrow> */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 64 | '[','H' |0x80,KEYCODE_HOME , /* xterm */ |
| 65 | '[','F' |0x80,KEYCODE_END , /* xterm */ |
Denys Vlasenko | 9ce09bc | 2011-11-03 13:28:22 +0100 | [diff] [blame] | 66 | /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */ |
| 67 | /* '[','Z' |0x80,KEYCODE_SHIFT_TAB, */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 68 | '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 69 | '[','2','~' |0x80,KEYCODE_INSERT , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 70 | /* ESC [ 2 ; 3 ~ - Alt-Insert */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 71 | '[','3','~' |0x80,KEYCODE_DELETE , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 72 | /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */ |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 73 | /* ESC [ 3 ; 3 ~ - Alt-Delete */ |
| 74 | /* ESC [ 3 ; 5 ~ - Ctrl-Delete */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 75 | '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 76 | '[','5','~' |0x80,KEYCODE_PAGEUP , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 77 | /* ESC [ 5 ; 3 ~ - Alt-PgUp */ |
| 78 | /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */ |
| 79 | /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 80 | '[','6','~' |0x80,KEYCODE_PAGEDOWN, |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 81 | '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
| 82 | '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 83 | #if 0 |
Denys Vlasenko | 0ccae4d | 2012-06-11 14:40:17 +0200 | [diff] [blame] | 84 | '[','1','1','~'|0x80,KEYCODE_FUN1 , /* old xterm, deprecated by ESC O P */ |
| 85 | '[','1','2','~'|0x80,KEYCODE_FUN2 , /* old xterm... */ |
| 86 | '[','1','3','~'|0x80,KEYCODE_FUN3 , /* old xterm... */ |
| 87 | '[','1','4','~'|0x80,KEYCODE_FUN4 , /* old xterm... */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 88 | '[','1','5','~'|0x80,KEYCODE_FUN5 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 89 | /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 90 | '[','1','7','~'|0x80,KEYCODE_FUN6 , |
| 91 | '[','1','8','~'|0x80,KEYCODE_FUN7 , |
| 92 | '[','1','9','~'|0x80,KEYCODE_FUN8 , |
| 93 | '[','2','0','~'|0x80,KEYCODE_FUN9 , |
| 94 | '[','2','1','~'|0x80,KEYCODE_FUN10 , |
| 95 | '[','2','3','~'|0x80,KEYCODE_FUN11 , |
| 96 | '[','2','4','~'|0x80,KEYCODE_FUN12 , |
Denys Vlasenko | 9d71fc6 | 2009-10-26 00:50:52 +0100 | [diff] [blame] | 97 | /* ESC [ 2 4 ; 2 ~ - Shift-F12 */ |
| 98 | /* ESC [ 2 4 ; 3 ~ - Alt-F12 */ |
| 99 | /* ESC [ 2 4 ; 4 ~ - Alt-Shift-F12 */ |
| 100 | /* ESC [ 2 4 ; 5 ~ - Ctrl-F12 */ |
| 101 | /* ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12 */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 102 | #endif |
Denys Vlasenko | 180f585 | 2009-10-26 00:59:59 +0100 | [diff] [blame] | 103 | /* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */ |
| 104 | /* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */ |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame] | 105 | '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT, |
| 106 | '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT , |
Denys Vlasenko | 9ce09bc | 2011-11-03 13:28:22 +0100 | [diff] [blame] | 107 | /* '[','1',';','3','A' |0x80,KEYCODE_ALT_UP , - unused */ |
| 108 | /* '[','1',';','3','B' |0x80,KEYCODE_ALT_DOWN , - unused */ |
| 109 | '[','1',';','3','C' |0x80,KEYCODE_ALT_RIGHT, |
| 110 | '[','1',';','3','D' |0x80,KEYCODE_ALT_LEFT , |
| 111 | /* '[','3',';','3','~' |0x80,KEYCODE_ALT_DELETE, - unused */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 112 | 0 |
| 113 | }; |
| 114 | |
Denys Vlasenko | 58f108e | 2010-03-11 21:17:55 +0100 | [diff] [blame] | 115 | pfd.fd = fd; |
| 116 | pfd.events = POLLIN; |
| 117 | |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 118 | buffer++; /* saved chars counter is in buffer[-1] now */ |
| 119 | |
| 120 | start_over: |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 121 | errno = 0; |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 122 | n = (unsigned char)buffer[-1]; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 123 | if (n == 0) { |
Denys Vlasenko | 58f108e | 2010-03-11 21:17:55 +0100 | [diff] [blame] | 124 | /* If no data, wait for input. |
| 125 | * If requested, wait TIMEOUT ms. TIMEOUT = -1 is useful |
| 126 | * if fd can be in non-blocking mode. |
| 127 | */ |
| 128 | if (timeout >= -1) { |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 129 | n = poll(&pfd, 1, timeout); |
| 130 | if (n < 0 && errno == EINTR) |
| 131 | return n; |
| 132 | if (n == 0) { |
Denys Vlasenko | 58f108e | 2010-03-11 21:17:55 +0100 | [diff] [blame] | 133 | /* Timed out */ |
| 134 | errno = EAGAIN; |
| 135 | return -1; |
| 136 | } |
| 137 | } |
| 138 | /* It is tempting to read more than one byte here, |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 139 | * but it breaks pasting. Example: at shell prompt, |
| 140 | * user presses "c","a","t" and then pastes "\nline\n". |
| 141 | * When we were reading 3 bytes here, we were eating |
| 142 | * "li" too, and cat was getting wrong input. |
| 143 | */ |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 144 | n = read(fd, buffer, 1); |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 145 | if (n <= 0) |
| 146 | return -1; |
| 147 | } |
| 148 | |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 149 | { |
| 150 | unsigned char c = buffer[0]; |
| 151 | n--; |
| 152 | if (n) |
| 153 | memmove(buffer, buffer + 1, n); |
| 154 | /* Only ESC starts ESC sequences */ |
| 155 | if (c != 27) { |
| 156 | buffer[-1] = n; |
| 157 | return c; |
| 158 | } |
| 159 | } |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 160 | |
| 161 | /* Loop through known ESC sequences */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 162 | seq = esccmds; |
| 163 | while (*seq != '\0') { |
| 164 | /* n - position in sequence we did not read yet */ |
| 165 | int i = 0; /* position in sequence to compare */ |
| 166 | |
| 167 | /* Loop through chars in this sequence */ |
| 168 | while (1) { |
| 169 | /* So far escape sequence matched up to [i-1] */ |
| 170 | if (n <= i) { |
| 171 | /* Need more chars, read another one if it wouldn't block. |
| 172 | * Note that escape sequences come in as a unit, |
| 173 | * so if we block for long it's not really an escape sequence. |
| 174 | * Timeout is needed to reconnect escape sequences |
| 175 | * split up by transmission over a serial console. */ |
| 176 | if (safe_poll(&pfd, 1, 50) == 0) { |
| 177 | /* No more data! |
| 178 | * Array is sorted from shortest to longest, |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 179 | * we can't match anything later in array - |
| 180 | * anything later is longer than this seq. |
| 181 | * Break out of both loops. */ |
| 182 | goto got_all; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 183 | } |
| 184 | errno = 0; |
| 185 | if (safe_read(fd, buffer + n, 1) <= 0) { |
| 186 | /* If EAGAIN, then fd is O_NONBLOCK and poll lied: |
| 187 | * in fact, there is no data. */ |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 188 | if (errno != EAGAIN) { |
| 189 | /* otherwise: it's EOF/error */ |
| 190 | buffer[-1] = 0; |
| 191 | return -1; |
| 192 | } |
| 193 | goto got_all; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 194 | } |
| 195 | n++; |
| 196 | } |
| 197 | if (buffer[i] != (seq[i] & 0x7f)) { |
| 198 | /* This seq doesn't match, go to next */ |
| 199 | seq += i; |
| 200 | /* Forward to last char */ |
| 201 | while (!(*seq & 0x80)) |
| 202 | seq++; |
| 203 | /* Skip it and the keycode which follows */ |
| 204 | seq += 2; |
| 205 | break; |
| 206 | } |
| 207 | if (seq[i] & 0x80) { |
| 208 | /* Entire seq matched */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 209 | n = 0; |
| 210 | /* n -= i; memmove(...); |
| 211 | * would be more correct, |
| 212 | * but we never read ahead that much, |
| 213 | * and n == i here. */ |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 214 | buffer[-1] = 0; |
| 215 | return (signed char)seq[i+1]; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 216 | } |
| 217 | i++; |
| 218 | } |
| 219 | } |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 220 | /* We did not find matching sequence. |
| 221 | * We possibly read and stored more input in buffer[] by now. |
| 222 | * n = bytes read. Try to read more until we time out. |
| 223 | */ |
| 224 | while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */ |
| 225 | if (safe_poll(&pfd, 1, 50) == 0) { |
| 226 | /* No more data! */ |
| 227 | break; |
| 228 | } |
| 229 | errno = 0; |
| 230 | if (safe_read(fd, buffer + n, 1) <= 0) { |
| 231 | /* If EAGAIN, then fd is O_NONBLOCK and poll lied: |
| 232 | * in fact, there is no data. */ |
| 233 | if (errno != EAGAIN) { |
| 234 | /* otherwise: it's EOF/error */ |
| 235 | buffer[-1] = 0; |
| 236 | return -1; |
| 237 | } |
| 238 | break; |
| 239 | } |
| 240 | n++; |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 241 | /* Try to decipher "ESC [ NNN ; NNN R" sequence */ |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 242 | if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL |
| 243 | || ENABLE_FEATURE_VI_ASK_TERMINAL |
| 244 | || ENABLE_FEATURE_LESS_ASK_TERMINAL |
| 245 | ) |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 246 | && n >= 5 |
| 247 | && buffer[0] == '[' |
| 248 | && buffer[n-1] == 'R' |
| 249 | && isdigit(buffer[1]) |
| 250 | ) { |
| 251 | char *end; |
| 252 | unsigned long row, col; |
| 253 | |
| 254 | row = strtoul(buffer + 1, &end, 10); |
| 255 | if (*end != ';' || !isdigit(end[1])) |
| 256 | continue; |
| 257 | col = strtoul(end + 1, &end, 10); |
| 258 | if (*end != 'R') |
| 259 | continue; |
| 260 | if (row < 1 || col < 1 || (row | col) > 0x7fff) |
| 261 | continue; |
| 262 | |
| 263 | buffer[-1] = 0; |
| 264 | /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */ |
Denys Vlasenko | f39a718 | 2019-10-25 17:40:57 +0200 | [diff] [blame] | 265 | row |= ((unsigned)(-1) << 15); |
| 266 | col |= (row << 16); |
Denys Vlasenko | 727e1b5 | 2009-10-26 15:23:32 +0100 | [diff] [blame] | 267 | /* Return it in high-order word */ |
| 268 | return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS; |
| 269 | } |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 270 | } |
| 271 | got_all: |
| 272 | |
| 273 | if (n <= 1) { |
| 274 | /* Alt-x is usually returned as ESC x. |
| 275 | * Report ESC, x is remembered for the next call. |
| 276 | */ |
| 277 | buffer[-1] = n; |
| 278 | return 27; |
| 279 | } |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 280 | |
Denys Vlasenko | 0f91b3d | 2009-10-26 12:09:06 +0100 | [diff] [blame] | 281 | /* We were doing "buffer[-1] = n; return c;" here, but this results |
| 282 | * in unknown key sequences being interpreted as ESC + garbage. |
| 283 | * This was not useful. Pretend there was no key pressed, |
| 284 | * go and wait for a new keypress: |
| 285 | */ |
| 286 | buffer[-1] = 0; |
| 287 | goto start_over; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 288 | } |
Tomas Heinrich | d2b0405 | 2010-03-09 14:09:24 +0100 | [diff] [blame] | 289 | |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 290 | int64_t FAST_FUNC safe_read_key(int fd, char *buffer, int timeout) |
| 291 | { |
| 292 | int64_t r; |
| 293 | do { |
Denys Vlasenko | 1e825ac | 2022-01-18 00:31:27 +0100 | [diff] [blame] | 294 | /* errno = 0; - read_key does this itself */ |
Denys Vlasenko | 12566e7 | 2022-01-17 03:02:40 +0100 | [diff] [blame] | 295 | r = read_key(fd, buffer, timeout); |
| 296 | } while (errno == EINTR); |
| 297 | return r; |
| 298 | } |
| 299 | |
Tomas Heinrich | d2b0405 | 2010-03-09 14:09:24 +0100 | [diff] [blame] | 300 | void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len) |
| 301 | { |
| 302 | unsigned cur_len = (unsigned char)buffer[0]; |
| 303 | if (len > KEYCODE_BUFFER_SIZE-1 - cur_len) |
| 304 | len = KEYCODE_BUFFER_SIZE-1 - cur_len; |
| 305 | memcpy(buffer + 1 + cur_len, str, len); |
Tomas Heinrich | 0358daf | 2010-04-16 23:59:51 +0200 | [diff] [blame] | 306 | buffer[0] += len; |
Tomas Heinrich | d2b0405 | 2010-03-09 14:09:24 +0100 | [diff] [blame] | 307 | } |