blob: d3832fa6ce316136d63b63df3aded76c9d95f472 [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 ,
Denys Vlasenkoa68bd4b2009-10-14 11:52:01 +020025 /* Ctrl-<arrow>: ESC [ 1 ; 5 x, where x = A/B/C/D */
Denis Vlasenko39b01352008-10-25 23:23:32 +000026 'O','H' |0x80,KEYCODE_HOME ,
27 'O','F' |0x80,KEYCODE_END ,
28#if 0
29 'O','P' |0x80,KEYCODE_FUN1 ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000030 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
Denis Vlasenko7462b212008-10-26 00:19:33 +000031 /* Ctrl- seems to not affect sequences */
Denis Vlasenko39b01352008-10-25 23:23:32 +000032 'O','Q' |0x80,KEYCODE_FUN2 ,
33 'O','R' |0x80,KEYCODE_FUN3 ,
34 'O','S' |0x80,KEYCODE_FUN4 ,
35#endif
36 '[','A' |0x80,KEYCODE_UP ,
37 '[','B' |0x80,KEYCODE_DOWN ,
38 '[','C' |0x80,KEYCODE_RIGHT ,
39 '[','D' |0x80,KEYCODE_LEFT ,
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000040 '[','H' |0x80,KEYCODE_HOME , /* xterm */
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000041 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000042 '[','F' |0x80,KEYCODE_END , /* xterm */
43 '[','1','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000044 '[','2','~' |0x80,KEYCODE_INSERT ,
45 '[','3','~' |0x80,KEYCODE_DELETE ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000046 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000047 '[','4','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000048 '[','5','~' |0x80,KEYCODE_PAGEUP ,
49 '[','6','~' |0x80,KEYCODE_PAGEDOWN,
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +000050 '[','7','~' |0x80,KEYCODE_HOME , /* vt100? linux vt? or what? */
51 '[','8','~' |0x80,KEYCODE_END , /* vt100? linux vt? or what? */
Denis Vlasenko39b01352008-10-25 23:23:32 +000052#if 0
53 '[','1','1','~'|0x80,KEYCODE_FUN1 ,
54 '[','1','2','~'|0x80,KEYCODE_FUN2 ,
55 '[','1','3','~'|0x80,KEYCODE_FUN3 ,
56 '[','1','4','~'|0x80,KEYCODE_FUN4 ,
57 '[','1','5','~'|0x80,KEYCODE_FUN5 ,
Denis Vlasenkobdd2d8f2008-10-25 23:59:41 +000058 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
Denis Vlasenko39b01352008-10-25 23:23:32 +000059 '[','1','7','~'|0x80,KEYCODE_FUN6 ,
60 '[','1','8','~'|0x80,KEYCODE_FUN7 ,
61 '[','1','9','~'|0x80,KEYCODE_FUN8 ,
62 '[','2','0','~'|0x80,KEYCODE_FUN9 ,
63 '[','2','1','~'|0x80,KEYCODE_FUN10 ,
64 '[','2','3','~'|0x80,KEYCODE_FUN11 ,
65 '[','2','4','~'|0x80,KEYCODE_FUN12 ,
66#endif
67 0
68 };
69
Denys Vlasenkoc15f40c2009-05-15 03:27:53 +020070 errno = 0;
Denys Vlasenko020f4062009-05-17 16:44:54 +020071 n = (unsigned char) *buffer++;
Denis Vlasenko39b01352008-10-25 23:23:32 +000072 if (n == 0) {
Denys Vlasenko4b7db4f2009-05-29 10:39:06 +020073 /* If no data, block waiting for input.
74 * It is tempting to read more than one byte here,
75 * but it breaks pasting. Example: at shell prompt,
76 * user presses "c","a","t" and then pastes "\nline\n".
77 * When we were reading 3 bytes here, we were eating
78 * "li" too, and cat was getting wrong input.
79 */
80 n = safe_read(fd, buffer, 1);
Denis Vlasenko39b01352008-10-25 23:23:32 +000081 if (n <= 0)
82 return -1;
83 }
84
85 /* Grab character to return from buffer */
86 c = (unsigned char)buffer[0];
87 n--;
88 if (n)
89 memmove(buffer, buffer + 1, n);
90
91 /* Only ESC starts ESC sequences */
92 if (c != 27)
93 goto ret;
94
95 /* Loop through known ESC sequences */
96 pfd.fd = fd;
97 pfd.events = POLLIN;
98 seq = esccmds;
99 while (*seq != '\0') {
100 /* n - position in sequence we did not read yet */
101 int i = 0; /* position in sequence to compare */
102
103 /* Loop through chars in this sequence */
104 while (1) {
105 /* So far escape sequence matched up to [i-1] */
106 if (n <= i) {
107 /* Need more chars, read another one if it wouldn't block.
108 * Note that escape sequences come in as a unit,
109 * so if we block for long it's not really an escape sequence.
110 * Timeout is needed to reconnect escape sequences
111 * split up by transmission over a serial console. */
112 if (safe_poll(&pfd, 1, 50) == 0) {
113 /* No more data!
114 * Array is sorted from shortest to longest,
115 * we can't match anything later in array,
116 * break out of both loops. */
117 goto ret;
118 }
119 errno = 0;
120 if (safe_read(fd, buffer + n, 1) <= 0) {
121 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
122 * in fact, there is no data. */
123 if (errno != EAGAIN)
124 c = -1; /* otherwise it's EOF/error */
125 goto ret;
126 }
127 n++;
128 }
129 if (buffer[i] != (seq[i] & 0x7f)) {
130 /* This seq doesn't match, go to next */
131 seq += i;
132 /* Forward to last char */
133 while (!(*seq & 0x80))
134 seq++;
135 /* Skip it and the keycode which follows */
136 seq += 2;
137 break;
138 }
139 if (seq[i] & 0x80) {
140 /* Entire seq matched */
141 c = (signed char)seq[i+1];
142 n = 0;
143 /* n -= i; memmove(...);
144 * would be more correct,
145 * but we never read ahead that much,
146 * and n == i here. */
147 goto ret;
148 }
149 i++;
150 }
151 }
152 /* We did not find matching sequence, it was a bare ESC.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200153 * We possibly read and stored more input in buffer[] by now. */
154
155 /* Try to decipher "ESC [ NNN ; NNN R" sequence */
156 if (ENABLE_FEATURE_EDITING_ASK_TERMINAL
157 && n != 0
158 && buffer[0] == '['
159 ) {
160 char *end;
161 unsigned long row, col;
162
163 while (n < KEYCODE_BUFFER_SIZE-1) { /* 1 for cnt */
164 if (safe_poll(&pfd, 1, 50) == 0) {
165 /* No more data! */
166 break;
167 }
168 errno = 0;
169 if (safe_read(fd, buffer + n, 1) <= 0) {
170 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
171 * in fact, there is no data. */
172 if (errno != EAGAIN)
173 c = -1; /* otherwise it's EOF/error */
174 goto ret;
175 }
176 if (buffer[n++] == 'R')
177 goto got_R;
178 }
179 goto ret;
180 got_R:
181 if (!isdigit(buffer[1]))
182 goto ret;
183 row = strtoul(buffer + 1, &end, 10);
184 if (*end != ';' || !isdigit(end[1]))
185 goto ret;
186 col = strtoul(end + 1, &end, 10);
187 if (*end != 'R')
188 goto ret;
189 if (row < 1 || col < 1 || (row | col) > 0x7fff)
190 goto ret;
191
192 buffer[-1] = 0;
193
194 /* Pack into "1 <row15bits> <col16bits>" 32-bit sequence */
195 c = (((-1 << 15) | row) << 16) | col;
196 /* Return it in high-order word */
197 return ((int64_t) c << 32) | (uint32_t)KEYCODE_CURSOR_POS;
198 }
Denis Vlasenko39b01352008-10-25 23:23:32 +0000199
200 ret:
Denys Vlasenko020f4062009-05-17 16:44:54 +0200201 buffer[-1] = n;
Denis Vlasenko39b01352008-10-25 23:23:32 +0000202 return c;
203}