d3832fa6ce316136d63b63df3aded76c9d95f472
[oweals/busybox.git] / libbb / read_key.c
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 GPL version 2, see file LICENSE in this tarball for details.
9  */
10 #include "libbb.h"
11
12 int64_t FAST_FUNC read_key(int fd, char *buffer)
13 {
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                 /* Ctrl-<arrow>: ESC [ 1 ; 5 x, where x = A/B/C/D  */
26                 'O','H'        |0x80,KEYCODE_HOME    ,
27                 'O','F'        |0x80,KEYCODE_END     ,
28 #if 0
29                 'O','P'        |0x80,KEYCODE_FUN1    ,
30                 /* [ESC] ESC O [2] P - [Alt-][Shift-]F1 */
31                 /* Ctrl- seems to not affect sequences */
32                 '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    ,
40                 '[','H'        |0x80,KEYCODE_HOME    , /* xterm */
41                 /* [ESC] ESC [ [2] H - [Alt-][Shift-]Home */
42                 '[','F'        |0x80,KEYCODE_END     , /* xterm */
43                 '[','1','~'    |0x80,KEYCODE_HOME    , /* vt100? linux vt? or what? */
44                 '[','2','~'    |0x80,KEYCODE_INSERT  ,
45                 '[','3','~'    |0x80,KEYCODE_DELETE  ,
46                 /* [ESC] ESC [ 3 [;2] ~ - [Alt-][Shift-]Delete */
47                 '[','4','~'    |0x80,KEYCODE_END     , /* vt100? linux vt? or what? */
48                 '[','5','~'    |0x80,KEYCODE_PAGEUP  ,
49                 '[','6','~'    |0x80,KEYCODE_PAGEDOWN,
50                 '[','7','~'    |0x80,KEYCODE_HOME    , /* vt100? linux vt? or what? */
51                 '[','8','~'    |0x80,KEYCODE_END     , /* vt100? linux vt? or what? */
52 #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    ,
58                 /* [ESC] ESC [ 1 5 [;2] ~ - [Alt-][Shift-]F5 */
59                 '[','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
70         errno = 0;
71         n = (unsigned char) *buffer++;
72         if (n == 0) {
73                 /* 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);
81                 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.
153          * 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         }
199
200  ret:
201         buffer[-1] = n;
202         return c;
203 }