1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2008 Rob Landley <rob@landley.net>
6 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
12 int64_t FAST_FUNC read_key(int fd, char *buffer, int timeout)
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.
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.
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 ,
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 ,
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? */
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 */
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 */
118 buffer++; /* saved chars counter is in buffer[-1] now */
122 n = (unsigned char)buffer[-1];
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.
129 if (safe_poll(&pfd, 1, timeout) == 0) {
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.
141 n = safe_read(fd, buffer, 1);
147 unsigned char c = buffer[0];
150 memmove(buffer, buffer + 1, n);
151 /* Only ESC starts ESC sequences */
158 /* Loop through known ESC sequences */
160 while (*seq != '\0') {
161 /* n - position in sequence we did not read yet */
162 int i = 0; /* position in sequence to compare */
164 /* Loop through chars in this sequence */
166 /* So far escape sequence matched up to [i-1] */
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) {
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. */
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 */
194 if (buffer[i] != (seq[i] & 0x7f)) {
195 /* This seq doesn't match, go to next */
197 /* Forward to last char */
198 while (!(*seq & 0x80))
200 /* Skip it and the keycode which follows */
205 /* Entire seq matched */
207 /* n -= i; memmove(...);
208 * would be more correct,
209 * but we never read ahead that much,
210 * and n == i here. */
212 return (signed char)seq[i+1];
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.
221 while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
222 if (safe_poll(&pfd, 1, 50) == 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 */
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
245 && buffer[n-1] == 'R'
246 && isdigit(buffer[1])
249 unsigned long row, col;
251 row = strtoul(buffer + 1, &end, 10);
252 if (*end != ';' || !isdigit(end[1]))
254 col = strtoul(end + 1, &end, 10);
257 if (row < 1 || col < 1 || (row | col) > 0x7fff)
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;
270 /* Alt-x is usually returned as ESC x.
271 * Report ESC, x is remembered for the next call.
277 /* 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:
286 void FAST_FUNC read_key_ungets(char *buffer, const char *str, unsigned len)
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);