1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 2008 Rob Landley <rob@landley.net>
6 * Copyright (C) 2008 Denys Vlasenko <vda.linux@googlemail.com>
8 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
12 int FAST_FUNC
read_key(int fd
, smalluint
*nbuffered
, char *buffer
)
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
,
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
,
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? */
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
,
73 /* If no data, block waiting for input. If we read more
74 * than the minimal ESC sequence size, the "n=0" below
75 * would instead have to figure out how much to keep,
76 * resulting in larger code. */
77 n
= safe_read(fd
, buffer
, 3);
82 /* Grab character to return from buffer */
83 c
= (unsigned char)buffer
[0];
86 memmove(buffer
, buffer
+ 1, n
);
88 /* Only ESC starts ESC sequences */
92 /* Loop through known ESC sequences */
96 while (*seq
!= '\0') {
97 /* n - position in sequence we did not read yet */
98 int i
= 0; /* position in sequence to compare */
100 /* Loop through chars in this sequence */
102 /* So far escape sequence matched up to [i-1] */
104 /* Need more chars, read another one if it wouldn't block.
105 * Note that escape sequences come in as a unit,
106 * so if we block for long it's not really an escape sequence.
107 * Timeout is needed to reconnect escape sequences
108 * split up by transmission over a serial console. */
109 if (safe_poll(&pfd
, 1, 50) == 0) {
111 * Array is sorted from shortest to longest,
112 * we can't match anything later in array,
113 * break out of both loops. */
117 if (safe_read(fd
, buffer
+ n
, 1) <= 0) {
118 /* If EAGAIN, then fd is O_NONBLOCK and poll lied:
119 * in fact, there is no data. */
121 c
= -1; /* otherwise it's EOF/error */
126 if (buffer
[i
] != (seq
[i
] & 0x7f)) {
127 /* This seq doesn't match, go to next */
129 /* Forward to last char */
130 while (!(*seq
& 0x80))
132 /* Skip it and the keycode which follows */
137 /* Entire seq matched */
138 c
= (signed char)seq
[i
+1];
140 /* n -= i; memmove(...);
141 * would be more correct,
142 * but we never read ahead that much,
143 * and n == i here. */
149 /* We did not find matching sequence, it was a bare ESC.
150 * We possibly read and stored more input in buffer[]