1 /* vi: set sw=4 ts=4: */
2 /*
3 * Utility routines.
4 *
5 * Copyright (C) 2008 Rob Landley <rob@landley.net>
6 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
7 *
8 * Licensed under GPLv2, see file LICENSE in this source tree.
9 */
10 #include "libbb.h"
11
read_key(int fd,char * buffer,int timeout)12 int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
13 {
14 struct pollfd pfd;
15 const char *seq;
16 int n;
17
18 /* Known escape sequences for cursor and function keys.
19 * See "Xterm Control Sequences"
20 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
21 * Array should be sorted from shortest to longest.
22 */
23 static const char esccmds[] ALIGN1 = {
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,
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 ,
43 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
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 */
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 ,
57 /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
58 /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> - implemented below */
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> */
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> */
64 '[','H' |0x80,KEYCODE_HOME , /* xterm */
65 '[','F' |0x80,KEYCODE_END , /* xterm */
66 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home (End similarly?) */
67 /* '[','Z' |0x80,KEYCODE_SHIFT_TAB, */
68 '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
69 '[','2','~' |0x80,KEYCODE_INSERT ,
70 /* ESC [ 2 ; 3 ~ - Alt-Insert */
71 '[','3','~' |0x80,KEYCODE_DELETE ,
72 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
73 /* ESC [ 3 ; 3 ~ - Alt-Delete */
74 /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
75 '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
76 '[','5','~' |0x80,KEYCODE_PAGEUP ,
77 /* ESC [ 5 ; 3 ~ - Alt-PgUp */
78 /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
79 /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
80 '[','6','~' |0x80,KEYCODE_PAGEDOWN,
81 '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
82 '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
83 #if 0
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... */
88 '[','1','5','~'|0x80,KEYCODE_FUN5 ,
89 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
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 ,
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 */
102 #endif
103 /* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */
104 /* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */
105 '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
106 '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
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 */
112 0
113 };
114
115 pfd.fd = fd;
116 pfd.events = POLLIN;
117
118 buffer++; /* saved chars counter is in buffer[-1] now */
119
120 start_over:
121 errno = 0;
122 n = (unsigned char)buffer[-1];
123 if (n == 0) {
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) {
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,
136 * 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);
142 if (n <= 0)
143 return -1;
144 }
145
146 {
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 }
157
158 /* Loop through known ESC sequences */
159 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,
176 * 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;
180 }
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. */
185 if (errno != EAGAIN) {
186 /* otherwise: it's EOF/error */
187 buffer[-1] = 0;
188 return -1;
189 }
190 goto got_all;
191 }
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 */
206 n = 0;
207 /* n -= i; memmove(...);
208 * would be more correct,
209 * but we never read ahead that much,
210 * and n == i here. */
211 buffer[-1] = 0;
212 return (signed char)seq[i+1];
213 }
214 i++;
215 }
216 }
217 /* 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++;
238 /* Try to decipher "ESC [ NNN ; NNN R" sequence */
239 if ((ENABLE_FEATURE_EDITING_ASK_TERMINAL
240 || ENABLE_FEATURE_VI_ASK_TERMINAL
241 || ENABLE_FEATURE_LESS_ASK_TERMINAL
242 )
243 && 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 row |= ((unsigned)(-1) << 15);
263 col |= (row << 16);
264 /* Return it in high-order word */
265 return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
266 }
267 }
268 got_all:
269
270 if (n <= 1) {
271 /* Alt-x is usually returned as ESC x.
272 * Report ESC, x is remembered for the next call.
273 */
274 buffer[-1] = n;
275 return 27;
276 }
277
278 /* We were doing "buffer[-1] = n; return c;" here, but this results
279 * in unknown key sequences being interpreted as ESC + garbage.
280 * This was not useful. Pretend there was no key pressed,
281 * go and wait for a new keypress:
282 */
283 buffer[-1] = 0;
284 goto start_over;
285 }
286
read_key_ungets(char * buffer,const char * str,unsigned len)287 void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len)
288 {
289 unsigned cur_len = (unsigned char)buffer[0];
290 if (len > KEYCODE_BUFFER_SIZE-1 - cur_len)
291 len = KEYCODE_BUFFER_SIZE-1 - cur_len;
292 memcpy(buffer + 1 + cur_len, str, len);
293 buffer[0] += len;
294 }
295