blob: 410a999958747a391cbcfbd488acf400f9481edc [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;
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 Vlasenkobdd2d8f2008-10-25 23:59:41 +000029 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
Denis Vlasenko7462b212008-10-26 00:19:33 +000030 /* Ctrl- seems to not affect sequences */
Denis Vlasenko39b01352008-10-25 23:23:32 +000031 '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 Vlasenkoa17eeb82009-10-25 23:50:56 +010039 /* 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 Vlasenko5f6aaf32008-10-25 23:27:29 +000044 '[','H' |0x80,KEYCODE_HOME , /* xterm */
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000045 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000046 '[','F' |0x80,KEYCODE_END , /* xterm */
47 '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000048 '[','2','~' |0x80,KEYCODE_INSERT ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010049 /* ESC [ 2 ; 3 ~ - Alt-Insert */
Denis Vlasenko39b01352008-10-25 23:23:32 +000050 '[','3','~' |0x80,KEYCODE_DELETE ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000051 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010052 /* ESC [ 3 ; 3 ~ - Alt-Delete */
53 /* ESC [ 3 ; 5 ~ - Ctrl-Delete */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000054 '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000055 '[','5','~' |0x80,KEYCODE_PAGEUP ,
Denys Vlasenkoa17eeb82009-10-25 23:50:56 +010056 /* ESC [ 5 ; 3 ~ - Alt-PgUp */
57 /* ESC [ 5 ; 5 ~ - Ctrl-PgUp */
58 /* ESC [ 5 ; 7 ~ - Ctrl-Alt-PgUp */
Denis Vlasenko39b01352008-10-25 23:23:32 +000059 '[','6','~' |0x80,KEYCODE_PAGEDOWN,
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000060 '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
61 '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000062#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 Vlasenkobdd2d8f2008-10-25 23:59:41 +000068 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000069 '[','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 Vlasenkoa17eeb82009-10-25 23:50:56 +010077 '[','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 Vlasenko39b01352008-10-25 23:23:32 +000081 0
82 };
83
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +020084 errno = 0;
Denys Vlasenko020f4062009-05-17 16:44:54 +020085 n = (unsigned char) *buffer++;
Denis Vlasenko39b01352008-10-25 23:23:32 +000086 if (n == 0) {
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +020087 /* 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 Vlasenko39b01352008-10-25 23:23:32 +000095 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 Vlasenko020f4062009-05-17 16:44:54 +0200167 * 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 Vlasenko39b01352008-10-25 23:23:32 +0000213
214 ret:
Denys Vlasenko020f4062009-05-17 16:44:54 +0200215 buffer[-1] = n;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000216 return c;
217}