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 | * |
| 8 | * Licensed under GPL version 2, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | #include "libbb.h" |
| 11 | |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 12 | int64_t FAST_FUNC read_key(int fd, char *buffer) |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 13 | { |
| 14 | struct pollfd pfd; |
| 15 | const char *seq; |
| 16 | int n; |
| 17 | int c; |
| 18 | |
| 19 | /* Known escape sequences for cursor and function keys */ |
| 20 | static const char esccmds[] ALIGN1 = { |
| 21 | 'O','A' |0x80,KEYCODE_UP , |
| 22 | 'O','B' |0x80,KEYCODE_DOWN , |
| 23 | 'O','C' |0x80,KEYCODE_RIGHT , |
| 24 | 'O','D' |0x80,KEYCODE_LEFT , |
| 25 | 'O','H' |0x80,KEYCODE_HOME , |
| 26 | 'O','F' |0x80,KEYCODE_END , |
| 27 | #if 0 |
| 28 | 'O','P' |0x80,KEYCODE_FUN1 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 29 | /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */ |
Denis Vlasenko | 7462b21 | 2008-10-26 00:19:33 +0000 | [diff] [blame] | 30 | /* Ctrl- seems to not affect sequences */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 31 | 'O','Q' |0x80,KEYCODE_FUN2 , |
| 32 | 'O','R' |0x80,KEYCODE_FUN3 , |
| 33 | 'O','S' |0x80,KEYCODE_FUN4 , |
| 34 | #endif |
| 35 | '[','A' |0x80,KEYCODE_UP , |
| 36 | '[','B' |0x80,KEYCODE_DOWN , |
| 37 | '[','C' |0x80,KEYCODE_RIGHT , |
| 38 | '[','D' |0x80,KEYCODE_LEFT , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame^] | 39 | /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */ |
| 40 | /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> */ |
| 41 | /* ESC [ 1 ; 4 x, where x = A/B/C/D: Alt-Shift-<arrow> */ |
| 42 | /* ESC [ 1 ; 5 x, where x = A/B/C/D: Ctrl-<arrow> - implemented below */ |
| 43 | /* 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] | 44 | '[','H' |0x80,KEYCODE_HOME , /* xterm */ |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 45 | /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 46 | '[','F' |0x80,KEYCODE_END , /* xterm */ |
| 47 | '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 48 | '[','2','~' |0x80,KEYCODE_INSERT , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame^] | 49 | /* ESC [ 2 ; 3 ~ - Alt-Insert */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 50 | '[','3','~' |0x80,KEYCODE_DELETE , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 51 | /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */ |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame^] | 52 | /* ESC [ 3 ; 3 ~ - Alt-Delete */ |
| 53 | /* ESC [ 3 ; 5 ~ - Ctrl-Delete */ |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 54 | '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 55 | '[','5','~' |0x80,KEYCODE_PAGEUP , |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame^] | 56 | /* ESC [ 5 ; 3 ~ - Alt-PgUp */ |
| 57 | /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */ |
| 58 | /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 59 | '[','6','~' |0x80,KEYCODE_PAGEDOWN, |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 60 | '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */ |
| 61 | '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 62 | #if 0 |
| 63 | '[','1','1','~'|0x80,KEYCODE_FUN1 , |
| 64 | '[','1','2','~'|0x80,KEYCODE_FUN2 , |
| 65 | '[','1','3','~'|0x80,KEYCODE_FUN3 , |
| 66 | '[','1','4','~'|0x80,KEYCODE_FUN4 , |
| 67 | '[','1','5','~'|0x80,KEYCODE_FUN5 , |
Denis Vlasenko | bdd2d8f | 2008-10-25 23:59:41 +0000 | [diff] [blame] | 68 | /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */ |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 69 | '[','1','7','~'|0x80,KEYCODE_FUN6 , |
| 70 | '[','1','8','~'|0x80,KEYCODE_FUN7 , |
| 71 | '[','1','9','~'|0x80,KEYCODE_FUN8 , |
| 72 | '[','2','0','~'|0x80,KEYCODE_FUN9 , |
| 73 | '[','2','1','~'|0x80,KEYCODE_FUN10 , |
| 74 | '[','2','3','~'|0x80,KEYCODE_FUN11 , |
| 75 | '[','2','4','~'|0x80,KEYCODE_FUN12 , |
| 76 | #endif |
Denys Vlasenko | a17eeb8 | 2009-10-25 23:50:56 +0100 | [diff] [blame^] | 77 | '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , |
| 78 | '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , |
| 79 | '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT, |
| 80 | '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT , |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 81 | 0 |
| 82 | }; |
| 83 | |
Denys Vlasenko | c15f40c | 2009-05-15 03:27:53 +0200 | [diff] [blame] | 84 | errno = 0; |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 85 | n = (unsigned char) *buffer++; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 86 | if (n == 0) { |
Denys Vlasenko | 4b7db4f | 2009-05-29 10:39:06 +0200 | [diff] [blame] | 87 | /* If no data, block waiting for input. |
| 88 | * It is tempting to read more than one byte here, |
| 89 | * but it breaks pasting. Example: at shell prompt, |
| 90 | * user presses "c","a","t" and then pastes "\nline\n". |
| 91 | * When we were reading 3 bytes here, we were eating |
| 92 | * "li" too, and cat was getting wrong input. |
| 93 | */ |
| 94 | n = safe_read(fd, buffer, 1); |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 95 | if (n <= 0) |
| 96 | return -1; |
| 97 | } |
| 98 | |
| 99 | /* Grab character to return from buffer */ |
| 100 | c = (unsigned char)buffer[0]; |
| 101 | n--; |
| 102 | if (n) |
| 103 | memmove(buffer, buffer + 1, n); |
| 104 | |
| 105 | /* Only ESC starts ESC sequences */ |
| 106 | if (c != 27) |
| 107 | goto ret; |
| 108 | |
| 109 | /* Loop through known ESC sequences */ |
| 110 | pfd.fd = fd; |
| 111 | pfd.events = POLLIN; |
| 112 | seq = esccmds; |
| 113 | while (*seq != '\0') { |
| 114 | /* n - position in sequence we did not read yet */ |
| 115 | int i = 0; /* position in sequence to compare */ |
| 116 | |
| 117 | /* Loop through chars in this sequence */ |
| 118 | while (1) { |
| 119 | /* So far escape sequence matched up to [i-1] */ |
| 120 | if (n <= i) { |
| 121 | /* Need more chars, read another one if it wouldn't block. |
| 122 | * Note that escape sequences come in as a unit, |
| 123 | * so if we block for long it's not really an escape sequence. |
| 124 | * Timeout is needed to reconnect escape sequences |
| 125 | * split up by transmission over a serial console. */ |
| 126 | if (safe_poll(&pfd, 1, 50) == 0) { |
| 127 | /* No more data! |
| 128 | * Array is sorted from shortest to longest, |
| 129 | * we can't match anything later in array, |
| 130 | * break out of both loops. */ |
| 131 | goto ret; |
| 132 | } |
| 133 | errno = 0; |
| 134 | if (safe_read(fd, buffer + n, 1) <= 0) { |
| 135 | /* If EAGAIN, then fd is O_NONBLOCK and poll lied: |
| 136 | * in fact, there is no data. */ |
| 137 | if (errno != EAGAIN) |
| 138 | c = -1; /* otherwise it's EOF/error */ |
| 139 | goto ret; |
| 140 | } |
| 141 | n++; |
| 142 | } |
| 143 | if (buffer[i] != (seq[i] & 0x7f)) { |
| 144 | /* This seq doesn't match, go to next */ |
| 145 | seq += i; |
| 146 | /* Forward to last char */ |
| 147 | while (!(*seq & 0x80)) |
| 148 | seq++; |
| 149 | /* Skip it and the keycode which follows */ |
| 150 | seq += 2; |
| 151 | break; |
| 152 | } |
| 153 | if (seq[i] & 0x80) { |
| 154 | /* Entire seq matched */ |
| 155 | c = (signed char)seq[i+1]; |
| 156 | n = 0; |
| 157 | /* n -= i; memmove(...); |
| 158 | * would be more correct, |
| 159 | * but we never read ahead that much, |
| 160 | * and n == i here. */ |
| 161 | goto ret; |
| 162 | } |
| 163 | i++; |
| 164 | } |
| 165 | } |
| 166 | /* We did not find matching sequence, it was a bare ESC. |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 167 | * We possibly read and stored more input in buffer[] by now. */ |
| 168 | |
| 169 | /* Try to decipher "ESC [ NNN ; NNN R" sequence */ |
| 170 | if (ENABLE_FEATURE_EDITING_ASK_TERMINAL |
| 171 | && n != 0 |
| 172 | && buffer[0] == '[' |
| 173 | ) { |
| 174 | char *end; |
| 175 | unsigned long row, col; |
| 176 | |
| 177 | while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for cnt */ |
| 178 | if (safe_poll(&pfd, 1, 50) == 0) { |
| 179 | /* No more data! */ |
| 180 | break; |
| 181 | } |
| 182 | errno = 0; |
| 183 | if (safe_read(fd, buffer + n, 1) <= 0) { |
| 184 | /* If EAGAIN, then fd is O_NONBLOCK and poll lied: |
| 185 | * in fact, there is no data. */ |
| 186 | if (errno != EAGAIN) |
| 187 | c = -1; /* otherwise it's EOF/error */ |
| 188 | goto ret; |
| 189 | } |
| 190 | if (buffer[n++] == 'R') |
| 191 | goto got_R; |
| 192 | } |
| 193 | goto ret; |
| 194 | got_R: |
| 195 | if (!isdigit(buffer[1])) |
| 196 | goto ret; |
| 197 | row = strtoul(buffer + 1, &end, 10); |
| 198 | if (*end != ';' || !isdigit(end[1])) |
| 199 | goto ret; |
| 200 | col = strtoul(end + 1, &end, 10); |
| 201 | if (*end != 'R') |
| 202 | goto ret; |
| 203 | if (row < 1 || col < 1 || (row | col) > 0x7fff) |
| 204 | goto ret; |
| 205 | |
| 206 | buffer[-1] = 0; |
| 207 | |
| 208 | /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */ |
| 209 | c = (((-1 << 15) | row) << 16) | col; |
| 210 | /* Return it in high-order word */ |
| 211 | return ((int64_t) c << 32) | (uint32_t)KEYCODE_CURSOR_POS; |
| 212 | } |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 213 | |
| 214 | ret: |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 215 | buffer[-1] = n; |
Denis Vlasenko | 39b0135 | 2008-10-25 23:23:32 +0000 | [diff] [blame] | 216 | return c; |
| 217 | } |