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