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