blob: a2253ce3e8f3c6493dd0a939d857ea796fd7cd93 [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 *
8 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
9 */
10#include "libbb.h"
11
Denys Vlasenko020f4062009-05-17 16:44:54 +020012int64_t FAST_FUNC read_key(int fd, char *buffer)
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
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 Vlasenkobdd2d8f2008-10-25 23:59:41 +000028 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
Denys Vlasenko9d71fc62009-10-26 00:50:52 +010029 /* 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 Vlasenko39b01352008-10-25 23:23:32 +000034 '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 Vlasenkoa17eeb82009-10-25 23:50:56 +010042 /* ESC [ 1 ; 2 x, where x = A/B/C/D: Shift-<arrow> */
43 /* ESC [ 1 ; 3 x, where x = A/B/C/D: Alt-<arrow> */
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 Vlasenko5f6aaf32008-10-25 23:27:29 +000047 '[','H' |0x80,KEYCODE_HOME , /* xterm */
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000048 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000049 '[','F' |0x80,KEYCODE_END , /* xterm */
50 '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000051 '[','2','~' |0x80,KEYCODE_INSERT ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010052 /* ESC [ 2 ; 3 ~ - Alt-Insert */
Denis Vlasenko39b01352008-10-25 23:23:32 +000053 '[','3','~' |0x80,KEYCODE_DELETE ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000054 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010055 /* ESC [ 3 ; 3 ~ - Alt-Delete */
56 /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000057 '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000058 '[','5','~' |0x80,KEYCODE_PAGEUP ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010059 /* ESC [ 5 ; 3 ~ - Alt-PgUp */
60 /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
61 /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
Denis Vlasenko39b01352008-10-25 23:23:32 +000062 '[','6','~' |0x80,KEYCODE_PAGEDOWN,
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000063 '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
64 '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000065#if 0
66 '[','1','1','~'|0x80,KEYCODE_FUN1 ,
67 '[','1','2','~'|0x80,KEYCODE_FUN2 ,
68 '[','1','3','~'|0x80,KEYCODE_FUN3 ,
69 '[','1','4','~'|0x80,KEYCODE_FUN4 ,
70 '[','1','5','~'|0x80,KEYCODE_FUN5 ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000071 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000072 '[','1','7','~'|0x80,KEYCODE_FUN6 ,
73 '[','1','8','~'|0x80,KEYCODE_FUN7 ,
74 '[','1','9','~'|0x80,KEYCODE_FUN8 ,
75 '[','2','0','~'|0x80,KEYCODE_FUN9 ,
76 '[','2','1','~'|0x80,KEYCODE_FUN10 ,
77 '[','2','3','~'|0x80,KEYCODE_FUN11 ,
78 '[','2','4','~'|0x80,KEYCODE_FUN12 ,
Denys Vlasenko9d71fc62009-10-26 00:50:52 +010079 /* ESC [ 2 4 ; 2 ~ - Shift-F12 */
80 /* ESC [ 2 4 ; 3 ~ - Alt-F12 */
81 /* ESC [ 2 4 ; 4 ~ - Alt-Shift-F12 */
82 /* ESC [ 2 4 ; 5 ~ - Ctrl-F12 */
83 /* ESC [ 2 4 ; 6 ~ - Ctrl-Shift-F12 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000084#endif
Denys Vlasenko180f5852009-10-26 00:59:59 +010085 /* '[','1',';','5','A' |0x80,KEYCODE_CTRL_UP , - unused */
86 /* '[','1',';','5','B' |0x80,KEYCODE_CTRL_DOWN , - unused */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010087 '[','1',';','5','C' |0x80,KEYCODE_CTRL_RIGHT,
88 '[','1',';','5','D' |0x80,KEYCODE_CTRL_LEFT ,
Denis Vlasenko39b01352008-10-25 23:23:32 +000089 0
Denys Vlasenko9d71fc62009-10-26 00:50:52 +010090 /* ESC [ Z - Shift-Tab */
Denis Vlasenko39b01352008-10-25 23:23:32 +000091 };
92
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +010093 buffer++; /* saved chars counter is in buffer[-1] now */
94
95 start_over:
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +020096 errno = 0;
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +010097 n = (unsigned char)buffer[-1];
Denis Vlasenko39b01352008-10-25 23:23:32 +000098 if (n == 0) {
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +020099 /* If no data, block waiting for input.
100 * It is tempting to read more than one byte here,
101 * but it breaks pasting. Example: at shell prompt,
102 * user presses "c","a","t" and then pastes "\nline\n".
103 * When we were reading 3 bytes here, we were eating
104 * "li" too, and cat was getting wrong input.
105 */
106 n = safe_read(fd, buffer, 1);
Denis Vlasenko39b01352008-10-25 23:23:32 +0000107 if (n <= 0)
108 return -1;
109 }
110
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100111 {
112 unsigned char c = buffer[0];
113 n--;
114 if (n)
115 memmove(buffer, buffer + 1, n);
116 /* Only ESC starts ESC sequences */
117 if (c != 27) {
118 buffer[-1] = n;
119 return c;
120 }
121 }
Denis Vlasenko39b01352008-10-25 23:23:32 +0000122
123 /* Loop through known ESC sequences */
124 pfd.fd = fd;
125 pfd.events = POLLIN;
126 seq = esccmds;
127 while (*seq != '\0') {
128 /* n - position in sequence we did not read yet */
129 int i = 0; /* position in sequence to compare */
130
131 /* Loop through chars in this sequence */
132 while (1) {
133 /* So far escape sequence matched up to [i-1] */
134 if (n <= i) {
135 /* Need more chars, read another one if it wouldn't block.
136 * Note that escape sequences come in as a unit,
137 * so if we block for long it's not really an escape sequence.
138 * Timeout is needed to reconnect escape sequences
139 * split up by transmission over a serial console. */
140 if (safe_poll(&pfd, 1, 50) == 0) {
141 /* No more data!
142 * Array is sorted from shortest to longest,
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100143 * we can't match anything later in array -
144 * anything later is longer than this seq.
145 * Break out of both loops. */
146 goto got_all;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000147 }
148 errno = 0;
149 if (safe_read(fd, buffer + n, 1) <= 0) {
150 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
151 * in fact, there is no data. */
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100152 if (errno != EAGAIN) {
153 /* otherwise: it's EOF/error */
154 buffer[-1] = 0;
155 return -1;
156 }
157 goto got_all;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000158 }
159 n++;
160 }
161 if (buffer[i] != (seq[i] & 0x7f)) {
162 /* This seq doesn't match, go to next */
163 seq += i;
164 /* Forward to last char */
165 while (!(*seq & 0x80))
166 seq++;
167 /* Skip it and the keycode which follows */
168 seq += 2;
169 break;
170 }
171 if (seq[i] & 0x80) {
172 /* Entire seq matched */
Denis Vlasenko39b01352008-10-25 23:23:32 +0000173 n = 0;
174 /* n -= i; memmove(...);
175 * would be more correct,
176 * but we never read ahead that much,
177 * and n == i here. */
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100178 buffer[-1] = 0;
179 return (signed char)seq[i+1];
Denis Vlasenko39b01352008-10-25 23:23:32 +0000180 }
181 i++;
182 }
183 }
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100184 /* We did not find matching sequence.
185 * We possibly read and stored more input in buffer[] by now.
186 * n = bytes read. Try to read more until we time out.
187 */
188 while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for count byte at buffer[-1] */
189 if (safe_poll(&pfd, 1, 50) == 0) {
190 /* No more data! */
191 break;
192 }
193 errno = 0;
194 if (safe_read(fd, buffer + n, 1) <= 0) {
195 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
196 * in fact, there is no data. */
197 if (errno != EAGAIN) {
198 /* otherwise: it's EOF/error */
199 buffer[-1] = 0;
200 return -1;
201 }
202 break;
203 }
204 n++;
Denys Vlasenko727e1b52009-10-26 15:23:32 +0100205 /* Try to decipher "ESC [ NNN ; NNN R" sequence */
206 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
207 && n >= 5
208 && buffer[0] == '['
209 && buffer[n-1] == 'R'
210 && isdigit(buffer[1])
211 ) {
212 char *end;
213 unsigned long row, col;
214
215 row = strtoul(buffer + 1, &end, 10);
216 if (*end != ';' || !isdigit(end[1]))
217 continue;
218 col = strtoul(end + 1, &end, 10);
219 if (*end != 'R')
220 continue;
221 if (row < 1 || col < 1 || (row | col) > 0x7fff)
222 continue;
223
224 buffer[-1] = 0;
225 /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
226 col |= (((-1 << 15) | row) << 16);
227 /* Return it in high-order word */
228 return ((int64_t) col << 32) | (uint32_t)KEYCODE_CURSOR_POS;
229 }
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100230 }
231 got_all:
232
233 if (n <= 1) {
234 /* Alt-x is usually returned as ESC x.
235 * Report ESC, x is remembered for the next call.
236 */
237 buffer[-1] = n;
238 return 27;
239 }
Denys Vlasenko020f4062009-05-17 16:44:54 +0200240
Denys Vlasenko0f91b3d2009-10-26 12:09:06 +0100241 /* We were doing "buffer[-1] = n; return c;" here, but this results
242 * in unknown key sequences being interpreted as ESC + garbage.
243 * This was not useful. Pretend there was no key pressed,
244 * go and wait for a new keypress:
245 */
246 buffer[-1] = 0;
247 goto start_over;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000248}