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