blob: 951786869c25e8104ca84bd4abfd4cf495539790 [file] [log] [blame]
Denis Vlasenko39b01352008-10-25 23:23:32 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Denis Vlasenko7462b212008-10-26 00:19:33 +00005 * Copyright (C) 2008 Rob Landley <rob@landley.net>
6 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
Denis Vlasenko39b01352008-10-25 23:23:32 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko39b01352008-10-25 23:23:32 +00009 */
10#include "libbb.h"
11
Denys Vlasenko58f108e2010-03-11 21:17:55 +010012int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
Denis Vlasenko39b01352008-10-25 23:23:32 +000013{
14 struct pollfd pfd;
15 const char *seq;
16 int n;
Denis Vlasenko39b01352008-10-25 23:23:32 +000017
Denys Vlasenko0ccae4d2012-06-11 14:40:17 +020018 /* Known escape sequences for cursor and function keys.
19 * See "Xterm Control Sequences"
20 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +010021 * Array should be sorted from shortest to longest.
Denys Vlasenko0ccae4d2012-06-11 14:40:17 +020022 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000023 static const char esccmds[] ALIGN1 = {
Rostislav Skudnov2e4ef382016-11-24 15:04:00 +010024 '\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 Vlasenko39b01352008-10-25 23:23:32 +000035 '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 Vlasenkobdd2d8f2008-10-25 23:59:41 +000043 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
Denys Vlasenko9d71fc62009-10-26 00:50:52 +010044 /* 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 Vlasenko39b01352008-10-25 23:23:32 +000049 '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 Vlasenkoa17eeb82009-10-25 23:50:56 +010057 /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +010058 /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010059 /* 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 Vlasenko0ccae4d2012-06-11 14:40:17 +020062 /* 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 Vlasenko5f6aaf32008-10-25 23:27:29 +000064 '[','H' |0x80,KEYCODE_HOME , /* xterm */
65 '[','F' |0x80,KEYCODE_END , /* xterm */
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +010066 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */
67 /* '[','Z' |0x80,KEYCODE_SHIFT_TAB, */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000068 '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000069 '[','2','~' |0x80,KEYCODE_INSERT ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010070 /* ESC [ 2 ; 3 ~ - Alt-Insert */
Denis Vlasenko39b01352008-10-25 23:23:32 +000071 '[','3','~' |0x80,KEYCODE_DELETE ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000072 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010073 /* ESC [ 3 ; 3 ~ - Alt-Delete */
74 /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000075 '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000076 '[','5','~' |0x80,KEYCODE_PAGEUP ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010077 /* ESC [ 5 ; 3 ~ - Alt-PgUp */
78 /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
79 /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
Denis Vlasenko39b01352008-10-25 23:23:32 +000080 '[','6','~' |0x80,KEYCODE_PAGEDOWN,
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000081 '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
82 '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000083#if 0
Denys Vlasenko0ccae4d2012-06-11 14:40:17 +020084 '[','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 Vlasenko39b01352008-10-25 23:23:32 +000088 '[','1','5','~'|0x80,KEYCODE_FUN5 ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000089 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000090 '[','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 Vlasenko9d71fc62009-10-26 00:50:52 +010097 /* 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 Vlasenko39b01352008-10-25 23:23:32 +0000102#endif
Denys Vlasenko180f5852009-10-26 00:59:59 +0100103 /* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */
104 /* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +0100105 '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
106 '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
Denys Vlasenko9ce09bc2011-11-03 13:28:22 +0100107 /* '[','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 Vlasenko39b01352008-10-25 23:23:32 +0000112 0
113 };
114
Denys Vlasenko58f108e2010-03-11 21:17:55 +0100115 pfd.fd = fd;
116 pfd.events = POLLIN;
117
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100118 buffer++; /* saved chars counter is in buffer[-1] now */
119
120 start_over:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +0200121 errno = 0;
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100122 n = (unsigned char)buffer[-1];
Denis Vlasenko39b01352008-10-25 23:23:32 +0000123 if (n == 0) {
Denys Vlasenko58f108e2010-03-11 21:17:55 +0100124 /* 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) {
129 if (safe_poll(&pfd, 1, timeout) == 0) {
130 /* Timed out */
131 errno = EAGAIN;
132 return -1;
133 }
134 }
135 /* It is tempting to read more than one byte here,
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +0200136 * but it breaks pasting. Example: at shell prompt,
137 * user presses "c","a","t" and then pastes "\nline\n".
138 * When we were reading 3 bytes here, we were eating
139 * "li" too, and cat was getting wrong input.
140 */
141 n = safe_read(fd, buffer, 1);
Denis Vlasenko39b01352008-10-25 23:23:32 +0000142 if (n <= 0)
143 return -1;
144 }
145
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100146 {
147 unsigned char c = buffer[0];
148 n--;
149 if (n)
150 memmove(buffer, buffer + 1, n);
151 /* Only ESC starts ESC sequences */
152 if (c != 27) {
153 buffer[-1] = n;
154 return c;
155 }
156 }
Denis Vlasenko39b01352008-10-25 23:23:32 +0000157
158 /* Loop through known ESC sequences */
Denis Vlasenko39b01352008-10-25 23:23:32 +0000159 seq = esccmds;
160 while (*seq != '\0') {
161 /* n - position in sequence we did not read yet */
162 int i = 0; /* position in sequence to compare */
163
164 /* Loop through chars in this sequence */
165 while (1) {
166 /* So far escape sequence matched up to [i-1] */
167 if (n <= i) {
168 /* Need more chars, read another one if it wouldn't block.
169 * Note that escape sequences come in as a unit,
170 * so if we block for long it's not really an escape sequence.
171 * Timeout is needed to reconnect escape sequences
172 * split up by transmission over a serial console. */
173 if (safe_poll(&pfd, 1, 50) == 0) {
174 /* No more data!
175 * Array is sorted from shortest to longest,
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100176 * we can't match anything later in array -
177 * anything later is longer than this seq.
178 * Break out of both loops. */
179 goto got_all;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000180 }
181 errno = 0;
182 if (safe_read(fd, buffer + n, 1) <= 0) {
183 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
184 * in fact, there is no data. */
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100185 if (errno != EAGAIN) {
186 /* otherwise: it's EOF/error */
187 buffer[-1] = 0;
188 return -1;
189 }
190 goto got_all;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000191 }
192 n++;
193 }
194 if (buffer[i] != (seq[i] & 0x7f)) {
195 /* This seq doesn't match, go to next */
196 seq += i;
197 /* Forward to last char */
198 while (!(*seq & 0x80))
199 seq++;
200 /* Skip it and the keycode which follows */
201 seq += 2;
202 break;
203 }
204 if (seq[i] & 0x80) {
205 /* Entire seq matched */
Denis Vlasenko39b01352008-10-25 23:23:32 +0000206 n = 0;
207 /* n -= i; memmove(...);
208 * would be more correct,
209 * but we never read ahead that much,
210 * and n == i here. */
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100211 buffer[-1] = 0;
212 return (signed char)seq[i+1];
Denis Vlasenko39b01352008-10-25 23:23:32 +0000213 }
214 i++;
215 }
216 }
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100217 /* We did not find matching sequence.
218 * We possibly read and stored more input in buffer[] by now.
219 * n = bytes read. Try to read more until we time out.
220 */
221 while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
222 if (safe_poll(&pfd, 1, 50) == 0) {
223 /* No more data! */
224 break;
225 }
226 errno = 0;
227 if (safe_read(fd, buffer + n, 1) <= 0) {
228 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
229 * in fact, there is no data. */
230 if (errno != EAGAIN) {
231 /* otherwise: it's EOF/error */
232 buffer[-1] = 0;
233 return -1;
234 }
235 break;
236 }
237 n++;
Denys Vlasenko727e1b52009-10-26 15:23:32 +0100238 /* Try to decipher "ESC [ NNN ; NNN R" sequence */
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200239 if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL
240 || ENABLE_FEATURE_VI_ASK_TERMINAL
241 || ENABLE_FEATURE_LESS_ASK_TERMINAL
242 )
Denys Vlasenko727e1b52009-10-26 15:23:32 +0100243 && n >= 5
244 && buffer[0] == '['
245 && buffer[n-1] == 'R'
246 && isdigit(buffer[1])
247 ) {
248 char *end;
249 unsigned long row, col;
250
251 row = strtoul(buffer + 1, &end, 10);
252 if (*end != ';' || !isdigit(end[1]))
253 continue;
254 col = strtoul(end + 1, &end, 10);
255 if (*end != 'R')
256 continue;
257 if (row < 1 || col < 1 || (row | col) > 0x7fff)
258 continue;
259
260 buffer[-1] = 0;
261 /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
262 col |= (((-1 << 15) | row) << 16);
263 /* Return it in high-order word */
264 return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
265 }
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100266 }
267 got_all:
268
269 if (n <= 1) {
270 /* Alt-x is usually returned as ESC x.
271 * Report ESC, x is remembered for the next call.
272 */
273 buffer[-1] = n;
274 return 27;
275 }
Denys Vlasenko020f4062009-05-17 16:44:54 +0200276
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100277 /* We were doing "buffer[-1] = n; return c;" here, but this results
278 * in unknown key sequences being interpreted as ESC + garbage.
279 * This was not useful. Pretend there was no key pressed,
280 * go and wait for a new keypress:
281 */
282 buffer[-1] = 0;
283 goto start_over;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000284}
Tomas Heinrichd2b04052010-03-09 14:09:24 +0100285
286void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len)
287{
288 unsigned cur_len = (unsigned char)buffer[0];
289 if (len > KEYCODE_BUFFER_SIZE-1 - cur_len)
290 len = KEYCODE_BUFFER_SIZE-1 - cur_len;
291 memcpy(buffer + 1 + cur_len, str, len);
Tomas Heinrich0358daf2010-04-16 23:59:51 +0200292 buffer[0] += len;
Tomas Heinrichd2b04052010-03-09 14:09:24 +0100293}