Line | Branch | Exec | Source |
---|---|---|---|
1 | // Parser for escape sequences sent by terminals to clients. | ||
2 | // Copyright © Craig Barnes. | ||
3 | // SPDX-License-Identifier: GPL-2.0-only | ||
4 | // See also: | ||
5 | // • https://invisible-island.net/xterm/ctlseqs/ctlseqs.html | ||
6 | // • https://sw.kovidgoyal.net/kitty/keyboard-protocol/ | ||
7 | // • ECMA-48 §5.4 (https://ecma-international.org/publications-and-standards/standards/ecma-48/) | ||
8 | // • https://vt100.net/emu/dec_ansi_parser | ||
9 | |||
10 | #include "parse.h" | ||
11 | #include "query.h" | ||
12 | #include "util/ascii.h" | ||
13 | #include "util/debug.h" | ||
14 | #include "util/log.h" | ||
15 | #include "util/unicode.h" | ||
16 | |||
17 | typedef enum { | ||
18 | BYTE_CONTROL, // 0x00..0x1F | ||
19 | BYTE_INTERMEDIATE, // 0x20..0x2F | ||
20 | BYTE_PARAMETER, // 0x30..0x3F | ||
21 | BYTE_FINAL, // 0x40..0x6F | ||
22 | BYTE_FINAL_PRIVATE, // 0x70..0x7E | ||
23 | BYTE_DELETE, // 0x7F | ||
24 | BYTE_OTHER, // 0x80..0xFF | ||
25 | } ByteType; | ||
26 | |||
27 | // https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-functional-keys | ||
28 | 501 | static KeyCode decode_key_from_final_byte(uint8_t byte) | |
29 | { | ||
30 |
14/14✓ Branch 0 (2→3) taken 43 times.
✓ Branch 1 (2→4) taken 43 times.
✓ Branch 2 (2→5) taken 43 times.
✓ Branch 3 (2→6) taken 43 times.
✓ Branch 4 (2→7) taken 43 times.
✓ Branch 5 (2→8) taken 46 times.
✓ Branch 6 (2→9) taken 1 times.
✓ Branch 7 (2→10) taken 1 times.
✓ Branch 8 (2→11) taken 42 times.
✓ Branch 9 (2→12) taken 42 times.
✓ Branch 10 (2→13) taken 42 times.
✓ Branch 11 (2→14) taken 42 times.
✓ Branch 12 (2→15) taken 25 times.
✓ Branch 13 (2→16) taken 45 times.
|
501 | switch (byte) { |
31 | case 'A': return KEY_UP; | ||
32 | 43 | case 'B': return KEY_DOWN; | |
33 | 43 | case 'C': return KEY_RIGHT; | |
34 | 43 | case 'D': return KEY_LEFT; | |
35 | 43 | case 'E': return KEY_BEGIN; // (keypad '5') | |
36 | 43 | case 'F': return KEY_END; | |
37 | 46 | case 'H': return KEY_HOME; | |
38 | 1 | case 'L': return KEY_INSERT; | |
39 | 1 | case 'M': return KEY_ENTER; | |
40 | 42 | case 'P': return KEY_F1; | |
41 | 42 | case 'Q': return KEY_F2; | |
42 | 42 | case 'R': return KEY_F3; | |
43 | 42 | case 'S': return KEY_F4; | |
44 | } | ||
45 | 25 | return KEY_IGNORE; | |
46 | } | ||
47 | |||
48 | // https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-functional-keys | ||
49 | // https://gitlab.com/craigbarnes/dte/-/commit/f540904cfdbb04b4cafdff0d7b15e3fd188395d4 | ||
50 | // https://gitlab.com/craigbarnes/dte/-/issues/121 | ||
51 | 995 | static KeyCode decode_key_from_param(uint32_t param) | |
52 | { | ||
53 |
28/28✓ Branch 0 (2→3) taken 41 times.
✓ Branch 1 (2→4) taken 41 times.
✓ Branch 2 (2→5) taken 1 times.
✓ Branch 3 (2→6) taken 41 times.
✓ Branch 4 (2→7) taken 44 times.
✓ Branch 5 (2→8) taken 1 times.
✓ Branch 6 (2→9) taken 41 times.
✓ Branch 7 (2→10) taken 41 times.
✓ Branch 8 (2→11) taken 41 times.
✓ Branch 9 (2→12) taken 41 times.
✓ Branch 10 (2→13) taken 41 times.
✓ Branch 11 (2→14) taken 41 times.
✓ Branch 12 (2→15) taken 41 times.
✓ Branch 13 (2→16) taken 41 times.
✓ Branch 14 (2→17) taken 41 times.
✓ Branch 15 (2→18) taken 41 times.
✓ Branch 16 (2→19) taken 41 times.
✓ Branch 17 (2→20) taken 41 times.
✓ Branch 18 (2→21) taken 41 times.
✓ Branch 19 (2→22) taken 41 times.
✓ Branch 20 (2→23) taken 41 times.
✓ Branch 21 (2→24) taken 41 times.
✓ Branch 22 (2→25) taken 41 times.
✓ Branch 23 (2→26) taken 40 times.
✓ Branch 24 (2→27) taken 40 times.
✓ Branch 25 (2→28) taken 41 times.
✓ Branch 26 (2→29) taken 6 times.
✓ Branch 27 (2→30) taken 2 times.
|
995 | switch (param) { |
54 | case 1: return KEY_HOME; | ||
55 | 41 | case 2: return KEY_INSERT; | |
56 | 41 | case 3: return KEY_DELETE; | |
57 | 1 | case 4: return KEY_END; | |
58 | 41 | case 5: return KEY_PAGE_UP; | |
59 | 44 | case 6: return KEY_PAGE_DOWN; | |
60 | case 7: return KEY_HOME; | ||
61 | 1 | case 8: return KEY_END; | |
62 | 41 | case 11: return KEY_F1; | |
63 | 41 | case 12: return KEY_F2; | |
64 | 41 | case 13: return KEY_F3; | |
65 | 41 | case 14: return KEY_F4; | |
66 | 41 | case 15: return KEY_F5; | |
67 | 41 | case 17: return KEY_F6; | |
68 | 41 | case 18: return KEY_F7; | |
69 | 41 | case 19: return KEY_F8; | |
70 | 41 | case 20: return KEY_F9; | |
71 | 41 | case 21: return KEY_F10; | |
72 | 41 | case 23: return KEY_F11; | |
73 | 41 | case 24: return KEY_F12; | |
74 | 41 | case 25: return KEY_F13; | |
75 | 41 | case 26: return KEY_F14; | |
76 | 41 | case 28: return KEY_F15; | |
77 | 41 | case 29: return KEY_F16; | |
78 | 41 | case 31: return KEY_F17; | |
79 | 40 | case 32: return KEY_F18; | |
80 | 40 | case 33: return KEY_F19; | |
81 | 41 | case 34: return KEY_F20; | |
82 | } | ||
83 | 6 | return KEY_IGNORE; | |
84 | } | ||
85 | |||
86 | // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/#modifiers | ||
87 | // ---------------------------- | ||
88 | // Shift 0b1 (1) | ||
89 | // Alt 0b10 (2) | ||
90 | // Ctrl 0b100 (4) | ||
91 | // Super 0b1000 (8) | ||
92 | // Hyper 0b10000 (16) | ||
93 | // Meta 0b100000 (32) | ||
94 | // Capslock 0b1000000 (64) | ||
95 | // Numlock 0b10000000 (128) | ||
96 | // ---------------------------- | ||
97 | 1664 | static KeyCode decode_modifiers(uint32_t n) | |
98 | { | ||
99 | 1664 | n--; | |
100 |
2/2✓ Branch 0 (2→3) taken 1453 times.
✓ Branch 1 (2→4) taken 211 times.
|
1664 | if (unlikely(n > 255)) { |
101 | return KEY_IGNORE; | ||
102 | } | ||
103 | |||
104 | 1453 | static_assert(1 == MOD_SHIFT >> KEYCODE_MODIFIER_OFFSET); | |
105 | 1453 | static_assert(2 == MOD_META >> KEYCODE_MODIFIER_OFFSET); | |
106 | 1453 | static_assert(4 == MOD_CTRL >> KEYCODE_MODIFIER_OFFSET); | |
107 | 1453 | static_assert(8 == MOD_SUPER >> KEYCODE_MODIFIER_OFFSET); | |
108 | 1453 | static_assert(16 == MOD_HYPER >> KEYCODE_MODIFIER_OFFSET); | |
109 | 1453 | static_assert(31 == MOD_MASK >> KEYCODE_MODIFIER_OFFSET); | |
110 | |||
111 | // Decode Meta and/or Alt as MOD_META and ignore Capslock/Numlock | ||
112 | 1453 | KeyCode mods = (n & 31) | ((n & 32) >> 4); | |
113 | 1453 | return mods << KEYCODE_MODIFIER_OFFSET; | |
114 | } | ||
115 | |||
116 | // Normalize KeyCode values originating from `CSI u` sequences. | ||
117 | // Note that, unlike normalize_csi_27_tilde_keycode(), the `mods` | ||
118 | // parameter is only passed in for context and not expected to be | ||
119 | // included in the returned value. | ||
120 | 62 | static KeyCode normalize_csi_u_keycode(KeyCode mods, KeyCode key) | |
121 | { | ||
122 | // https://sw.kovidgoyal.net/kitty/keyboard-protocol/#functional-key-definitions | ||
123 |
44/46✓ Branch 0 (2→3) taken 1 times.
✓ Branch 1 (2→4) taken 1 times.
✗ Branch 2 (2→5) not taken.
✓ Branch 3 (2→6) taken 3 times.
✓ Branch 4 (2→7) taken 1 times.
✓ Branch 5 (2→8) taken 2 times.
✓ Branch 6 (2→9) taken 1 times.
✓ Branch 7 (2→10) taken 1 times.
✓ Branch 8 (2→11) taken 1 times.
✓ Branch 9 (2→12) taken 1 times.
✓ Branch 10 (2→13) taken 1 times.
✓ Branch 11 (2→14) taken 1 times.
✓ Branch 12 (2→15) taken 1 times.
✓ Branch 13 (2→16) taken 1 times.
✓ Branch 14 (2→17) taken 1 times.
✓ Branch 15 (2→18) taken 1 times.
✓ Branch 16 (2→19) taken 1 times.
✓ Branch 17 (2→20) taken 1 times.
✓ Branch 18 (2→21) taken 1 times.
✓ Branch 19 (2→22) taken 1 times.
✓ Branch 20 (2→23) taken 1 times.
✓ Branch 21 (2→24) taken 1 times.
✓ Branch 22 (2→25) taken 1 times.
✓ Branch 23 (2→26) taken 1 times.
✓ Branch 24 (2→27) taken 1 times.
✓ Branch 25 (2→28) taken 1 times.
✓ Branch 26 (2→29) taken 1 times.
✓ Branch 27 (2→30) taken 1 times.
✓ Branch 28 (2→31) taken 1 times.
✓ Branch 29 (2→32) taken 1 times.
✓ Branch 30 (2→33) taken 1 times.
✓ Branch 31 (2→34) taken 1 times.
✓ Branch 32 (2→35) taken 1 times.
✓ Branch 33 (2→36) taken 1 times.
✓ Branch 34 (2→37) taken 1 times.
✓ Branch 35 (2→38) taken 1 times.
✓ Branch 36 (2→39) taken 1 times.
✓ Branch 37 (2→40) taken 1 times.
✓ Branch 38 (2→41) taken 1 times.
✓ Branch 39 (2→42) taken 1 times.
✓ Branch 40 (2→43) taken 1 times.
✓ Branch 41 (2→44) taken 1 times.
✓ Branch 42 (2→45) taken 1 times.
✓ Branch 43 (2→46) taken 1 times.
✓ Branch 44 (2→47) taken 16 times.
✗ Branch 45 (2→51) not taken.
|
62 | switch (key) { |
124 | case '\b': return KEY_BACKSPACE; // BS; Kitty never emits this, but (buggy) WezTerm does | ||
125 | 1 | case '\r': return KEY_ENTER; | |
126 | 1 | case '\t': return KEY_TAB; | |
127 | ✗ | case '\n': return KEY_ENTER; // Kitty never emits this | |
128 | 3 | case 27: return KEY_ESCAPE; // ESC | |
129 | case 127: return KEY_BACKSPACE; // DEL | ||
130 | 1 | case 57359: return KEY_SCROLL_LOCK; | |
131 | 2 | case 57361: return KEY_PRINT_SCREEN; | |
132 | 1 | case 57362: return KEY_PAUSE; | |
133 | 1 | case 57363: return KEY_MENU; | |
134 | 1 | case 57376: return KEY_F13; | |
135 | 1 | case 57377: return KEY_F14; | |
136 | 1 | case 57378: return KEY_F15; | |
137 | 1 | case 57379: return KEY_F16; | |
138 | 1 | case 57380: return KEY_F17; | |
139 | 1 | case 57381: return KEY_F18; | |
140 | 1 | case 57382: return KEY_F19; | |
141 | 1 | case 57383: return KEY_F20; | |
142 | 1 | case 57399: return '0'; | |
143 | 1 | case 57400: return '1'; | |
144 | 1 | case 57401: return '2'; | |
145 | 1 | case 57402: return '3'; | |
146 | 1 | case 57403: return '4'; | |
147 | 1 | case 57404: return '5'; | |
148 | 1 | case 57405: return '6'; | |
149 | 1 | case 57406: return '7'; | |
150 | 1 | case 57407: return '8'; | |
151 | 1 | case 57408: return '9'; | |
152 | 1 | case 57409: return '.'; | |
153 | 1 | case 57410: return '/'; | |
154 | 1 | case 57411: return '*'; | |
155 | 1 | case 57412: return '-'; | |
156 | 1 | case 57413: return '+'; | |
157 | 1 | case 57414: return KEY_ENTER; | |
158 | 1 | case 57415: return '='; | |
159 | 1 | case 57417: return KEY_LEFT; | |
160 | 1 | case 57418: return KEY_RIGHT; | |
161 | 1 | case 57419: return KEY_UP; | |
162 | 1 | case 57420: return KEY_DOWN; | |
163 | 1 | case 57421: return KEY_PAGE_UP; | |
164 | 1 | case 57422: return KEY_PAGE_DOWN; | |
165 | 1 | case 57423: return KEY_HOME; | |
166 | 1 | case 57424: return KEY_END; | |
167 | 1 | case 57425: return KEY_INSERT; | |
168 | 1 | case 57426: return KEY_DELETE; | |
169 | 1 | case 57427: return KEY_BEGIN; | |
170 | } | ||
171 | |||
172 |
2/2✓ Branch 0 (47→48) taken 15 times.
✓ Branch 1 (47→51) taken 1 times.
|
16 | if (unlikely(key < 32 || (key >= 57344 && key <= 63743))) { |
173 | // Ignore values (not already handled above) that correspond | ||
174 | // to C0 controls or the Kitty private use range | ||
175 | return KEY_IGNORE; | ||
176 | } | ||
177 | |||
178 |
3/4✓ Branch 0 (48→49) taken 1 times.
✓ Branch 1 (48→51) taken 14 times.
✗ Branch 2 (49→50) not taken.
✓ Branch 3 (49→51) taken 1 times.
|
15 | if (u_is_ascii_upper(key) && (mods & MOD_CTRL)) { |
179 | // This is done only for the sake of (older versions of) iTerm2 | ||
180 | // See also: | ||
181 | // • https://gitlab.com/craigbarnes/dte/-/issues/130#note_870592688 | ||
182 | // • https://gitlab.com/craigbarnes/dte/-/issues/130#note_864512674 | ||
183 | // • https://gitlab.com/gnachman/iterm2/-/issues/10017 | ||
184 | // • https://gitlab.com/gnachman/iterm2/-/commit/9cd0241afd0655024153c8730d5b3ed1fe41faf7 | ||
185 | // • https://gitlab.com/gnachman/iterm2/-/issues/7440#note_129599012 | ||
186 | ✗ | return ascii_tolower(key); | |
187 | } | ||
188 | |||
189 | return key; | ||
190 | } | ||
191 | |||
192 | // Normalize KeyCode values originating from xterm-style "modifyOtherKeys" | ||
193 | // sequences (CSI 27 ; <modifiers> ; <key> ~) | ||
194 | 18 | static KeyCode normalize_csi_27_tilde_keycode(KeyCode mods, KeyCode key) | |
195 | { | ||
196 |
6/7✓ Branch 0 (2→3) taken 3 times.
✓ Branch 1 (2→4) taken 2 times.
✓ Branch 2 (2→5) taken 1 times.
✗ Branch 3 (2→6) not taken.
✓ Branch 4 (2→7) taken 3 times.
✓ Branch 5 (2→8) taken 3 times.
✓ Branch 6 (2→9) taken 6 times.
|
18 | switch (key) { |
197 | // https://codeberg.org/dnkl/foot/pulls/791#issuecomment-279784 | ||
198 | 3 | case '\b': return mods | KEY_BACKSPACE; | |
199 | 2 | case '\r': return mods | KEY_ENTER; | |
200 | 1 | case '\t': return mods | KEY_TAB; | |
201 | ✗ | case '\n': return mods | KEY_ENTER; | |
202 | 3 | case 27: return mods | KEY_ESCAPE; // ESC | |
203 | 3 | case 127: return mods | KEY_BACKSPACE; // DEL | |
204 | } | ||
205 | |||
206 |
1/2✓ Branch 0 (9→10) taken 6 times.
✗ Branch 1 (9→14) not taken.
|
6 | if (unlikely(key < 32)) { |
207 | return KEY_IGNORE; | ||
208 | } | ||
209 | |||
210 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→13) taken 4 times.
|
6 | if (u_is_ascii_upper(key)) { |
211 |
1/2✓ Branch 0 (11→12) taken 2 times.
✗ Branch 1 (11→14) not taken.
|
2 | if ((mods & ~MOD_SHIFT) == 0) { |
212 | // [A-Z] and Shift+[A-Z] should be encoded as just [A-Z] | ||
213 | return key; | ||
214 | } | ||
215 | // [A-Z] with any other combination of modifiers should be | ||
216 | // converted to lowercase and have the MOD_SHIFT bit set. | ||
217 | // This is done in a "blanket" fashion and covers sequences | ||
218 | // that xterm never emits, because some terminals (e.g. tmux) | ||
219 | // emulate the protocol imperfectly. | ||
220 | 2 | return mods | MOD_SHIFT | key | 0x20; | |
221 | } | ||
222 | |||
223 | 4 | return mods | key; | |
224 | } | ||
225 | |||
226 | 73 | static ssize_t parse_ss3(const char *buf, size_t length, size_t i, KeyCode *k) | |
227 | { | ||
228 |
2/2✓ Branch 0 (2→3) taken 36 times.
✓ Branch 1 (2→13) taken 37 times.
|
73 | if (unlikely(i >= length)) { |
229 | return TPARSE_PARTIAL_MATCH; | ||
230 | } | ||
231 | |||
232 | 36 | const char ch = buf[i++]; | |
233 | 36 | KeyCode key = decode_key_from_final_byte(ch); | |
234 |
2/2✓ Branch 0 (3→4) taken 12 times.
✓ Branch 1 (3→5) taken 24 times.
|
36 | if (key != KEY_IGNORE) { |
235 | 12 | *k = key; | |
236 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 23 times.
|
24 | } else if (ch == 'X') { |
237 | 1 | *k = '='; | |
238 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 22 times.
|
23 | } else if (ch == ' ') { |
239 | 1 | *k = KEY_SPACE; | |
240 |
2/2✓ Branch 0 (9→10) taken 17 times.
✓ Branch 1 (9→11) taken 5 times.
|
22 | } else if ((ch >= 'j' && ch <= 'y') || ch == 'I') { |
241 | 17 | *k = ch - 64; | |
242 | } else { | ||
243 | 5 | *k = KEY_IGNORE; | |
244 | } | ||
245 | |||
246 | 36 | return i; | |
247 | } | ||
248 | |||
249 | 1930 | static ByteType get_byte_type(unsigned char byte) | |
250 | { | ||
251 | 1930 | enum { | |
252 | C = BYTE_CONTROL, | ||
253 | I = BYTE_INTERMEDIATE, | ||
254 | P = BYTE_PARAMETER, | ||
255 | F = BYTE_FINAL, | ||
256 | f = BYTE_FINAL_PRIVATE, | ||
257 | x = BYTE_OTHER, | ||
258 | }; | ||
259 | |||
260 | // ECMA 48 divides bytes ("bit combinations") into rows of 16 columns. | ||
261 | // The byte classifications mostly fall into their own rows: | ||
262 | 1930 | static const uint8_t rows[16] = { | |
263 | C, C, I, P, F, F, F, f, | ||
264 | x, x, x, x, x, x, x, x | ||
265 | }; | ||
266 | |||
267 | // ... with the exception of byte 127 (DEL), which falls into rows[7] | ||
268 | // but isn't a final byte like the others in that row: | ||
269 |
1/2✓ Branch 0 (2→3) taken 1930 times.
✗ Branch 1 (2→4) not taken.
|
1930 | if (unlikely(byte == 127)) { |
270 | return BYTE_DELETE; | ||
271 | } | ||
272 | |||
273 | 1930 | return rows[(byte >> 4) & 0xF]; | |
274 | } | ||
275 | |||
276 | #define UNHANDLED(var, ...) unhandled(var, __LINE__, __VA_ARGS__) | ||
277 | |||
278 | PRINTF(3) | ||
279 | 395 | static void unhandled(bool *var, int line, const char *fmt, ...) | |
280 | { | ||
281 |
2/2✓ Branch 0 (2→3) taken 89 times.
✓ Branch 1 (2→5) taken 306 times.
|
395 | if (*var) { |
282 | // Only log the first error in a sequence | ||
283 | return; | ||
284 | } | ||
285 | |||
286 | 89 | *var = true; | |
287 | 89 | if (DEBUG >= 2) { | |
288 | 89 | va_list ap; | |
289 | 89 | va_start(ap, fmt); | |
290 | 89 | log_msgv(LOG_LEVEL_DEBUG, __FILE__, line, fmt, ap); | |
291 | 89 | va_end(ap); | |
292 | } | ||
293 | } | ||
294 | |||
295 | 7344 | size_t term_parse_csi_params(const char *buf, size_t len, size_t i, TermControlParams *csi) | |
296 | { | ||
297 | 7344 | size_t nparams = 0; | |
298 | 7344 | size_t nr_intermediate = 0; | |
299 | 7344 | size_t sub = 0; | |
300 | 7344 | size_t digits = 0; | |
301 | 7344 | uint32_t num = 0; | |
302 | 7344 | bool have_subparams = false; | |
303 | 7344 | bool err = false; | |
304 | |||
305 |
2/2✓ Branch 0 (36→3) taken 23290 times.
✓ Branch 1 (36→37) taken 5526 times.
|
28816 | while (i < len) { |
306 | 23290 | const char ch = buf[i++]; | |
307 |
4/4✓ Branch 0 (3→4) taken 16823 times.
✓ Branch 1 (3→7) taken 4503 times.
✓ Branch 2 (3→11) taken 34 times.
✓ Branch 3 (3→15) taken 1930 times.
|
23290 | switch (ch) { |
308 | 16823 | case '0': case '1': case '2': case '3': case '4': | |
309 | case '5': case '6': case '7': case '8': case '9': | ||
310 | 16823 | num = (num * 10) + (ch - '0'); | |
311 |
2/2✓ Branch 0 (4→5) taken 370 times.
✓ Branch 1 (4→6) taken 16453 times.
|
16823 | if (unlikely(num > UNICODE_MAX_VALID_CODEPOINT)) { |
312 | 370 | UNHANDLED(&err, "CSI param overflow"); | |
313 | } | ||
314 | 16823 | digits++; | |
315 | 16823 | continue; | |
316 | 4503 | case ';': | |
317 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 4503 times.
|
4503 | if (unlikely(nparams >= ARRAYLEN(csi->params))) { |
318 | ✗ | UNHANDLED(&err, "too many params in CSI sequence"); | |
319 | ✗ | continue; | |
320 | } | ||
321 | 4503 | csi->nsub[nparams] = sub + 1; | |
322 | 4503 | csi->params[nparams++][sub] = num; | |
323 | 4503 | num = 0; | |
324 | 4503 | digits = 0; | |
325 | 4503 | sub = 0; | |
326 | 4503 | continue; | |
327 | 34 | case ':': | |
328 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 34 times.
|
34 | if (unlikely(sub >= ARRAYLEN(csi->params[0]))) { |
329 | ✗ | UNHANDLED(&err, "too many sub-params in CSI sequence"); | |
330 | ✗ | continue; | |
331 | } | ||
332 | 34 | csi->params[nparams][sub++] = num; | |
333 | 34 | num = 0; | |
334 | 34 | digits = 0; | |
335 | 34 | have_subparams = true; | |
336 | 34 | continue; | |
337 | } | ||
338 | |||
339 |
4/7✓ Branch 0 (15→16) taken 100 times.
✓ Branch 1 (15→20) taken 1816 times.
✓ Branch 2 (15→25) taken 12 times.
✓ Branch 3 (15→27) taken 2 times.
✗ Branch 4 (15→31) not taken.
✗ Branch 5 (15→32) not taken.
✗ Branch 6 (15→34) not taken.
|
1930 | switch (get_byte_type(ch)) { |
340 | 100 | case BYTE_INTERMEDIATE: | |
341 |
2/2✓ Branch 0 (16→17) taken 11 times.
✓ Branch 1 (16→18) taken 89 times.
|
100 | if (unlikely(nr_intermediate >= ARRAYLEN(csi->intermediate))) { |
342 | 11 | UNHANDLED(&err, "too many intermediate bytes in CSI sequence"); | |
343 | } else { | ||
344 | 89 | csi->intermediate[nr_intermediate++] = ch; | |
345 | } | ||
346 | 100 | continue; | |
347 | 1816 | case BYTE_FINAL: | |
348 | case BYTE_FINAL_PRIVATE: | ||
349 | 1816 | csi->final_byte = ch; | |
350 |
2/2✓ Branch 0 (20→21) taken 1799 times.
✓ Branch 1 (20→24) taken 17 times.
|
1816 | if (digits > 0) { |
351 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 1799 times.
|
1799 | if (unlikely( |
352 | nparams >= ARRAYLEN(csi->params) | ||
353 | || sub >= ARRAYLEN(csi->params[0]) | ||
354 | )) { | ||
355 | ✗ | UNHANDLED(&err, "too many params/sub-params in CSI sequence"); | |
356 | } else { | ||
357 | 1799 | csi->nsub[nparams] = sub + 1; | |
358 | 1799 | csi->params[nparams++][sub] = num; | |
359 | } | ||
360 | } | ||
361 | 1816 | goto exit_loop; | |
362 | 12 | case BYTE_PARAMETER: | |
363 | // ECMA-48 §5.4.2: "bit combinations 03/12 to 03/15 are | ||
364 | // reserved for future standardization except when used | ||
365 | // as the first bit combination of the parameter string." | ||
366 | // (03/12 to 03/15 == '<' to '?') | ||
367 | 12 | UNHANDLED(&err, "unhandled CSI param byte: '%c'", ch); | |
368 | 12 | continue; | |
369 | 2 | case BYTE_CONTROL: | |
370 |
2/3✓ Branch 0 (27→28) taken 1 times.
✓ Branch 1 (27→29) taken 1 times.
✗ Branch 2 (27→31) not taken.
|
2 | switch (ch) { |
371 | 1 | case 0x1B: // ESC | |
372 | // Don't consume ESC; it's the start of another sequence | ||
373 | 1 | i--; | |
374 | // Fallthrough | ||
375 | 2 | case 0x18: // CAN | |
376 | case 0x1A: // SUB | ||
377 | 2 | UNHANDLED(&err, "CSI sequence cancelled by 0x%02hhx", ch); | |
378 | 2 | csi->final_byte = ch; | |
379 | 2 | goto exit_loop; | |
380 | } | ||
381 | // Fallthrough | ||
382 | case BYTE_DELETE: | ||
383 | ✗ | continue; | |
384 | ✗ | case BYTE_OTHER: | |
385 | ✗ | UNHANDLED(&err, "unhandled byte in CSI sequence: 0x%02hhx", ch); | |
386 | ✗ | continue; | |
387 | } | ||
388 | |||
389 | − | BUG("unhandled byte type"); | |
390 | err = true; | ||
391 | } | ||
392 | |||
393 | 5526 | exit_loop: | |
394 | 7344 | csi->nparams = nparams; | |
395 | 7344 | csi->nr_intermediate = nr_intermediate; | |
396 | 7344 | csi->have_subparams = have_subparams; | |
397 | 7344 | csi->unhandled_bytes = err; | |
398 | 7344 | return i; | |
399 | } | ||
400 | |||
401 | 7339 | static ssize_t parse_csi(const char *buf, size_t len, size_t i, KeyCode *k) | |
402 | { | ||
403 | 7339 | TermControlParams csi = {.nparams = 0}; | |
404 |
6/6✓ Branch 0 (2→3) taken 6308 times.
✓ Branch 1 (2→6) taken 1031 times.
✓ Branch 2 (3→4) taken 267 times.
✓ Branch 3 (3→6) taken 6041 times.
✓ Branch 4 (4→5) taken 253 times.
✓ Branch 5 (4→6) taken 14 times.
|
7339 | bool maybe_query_reply = (i < len && buf[i] >= '<' && buf[i] <= '?'); |
405 | 7592 | uint8_t param_prefix = maybe_query_reply ? buf[i] : 0; | |
406 | 7339 | i = term_parse_csi_params(buf, len, i + (maybe_query_reply ? 1 : 0), &csi); | |
407 | |||
408 |
2/2✓ Branch 0 (7→8) taken 5526 times.
✓ Branch 1 (7→10) taken 1813 times.
|
7339 | if (unlikely(csi.final_byte == 0)) { |
409 | 5526 | BUG_ON(i < len); | |
410 | return TPARSE_PARTIAL_MATCH; | ||
411 | } | ||
412 |
2/2✓ Branch 0 (10→11) taken 14 times.
✓ Branch 1 (10→12) taken 1799 times.
|
1813 | if (unlikely(csi.unhandled_bytes)) { |
413 | 14 | goto ignore; | |
414 | } | ||
415 | |||
416 |
2/2✓ Branch 0 (12→13) taken 38 times.
✓ Branch 1 (12→15) taken 1761 times.
|
1799 | if (maybe_query_reply) { |
417 | 38 | *k = parse_csi_query_reply(&csi, param_prefix); | |
418 | 38 | return i; | |
419 | } | ||
420 | |||
421 |
2/2✓ Branch 0 (15→16) taken 4 times.
✓ Branch 1 (15→17) taken 1757 times.
|
1761 | if (unlikely(csi.nr_intermediate)) { |
422 | 4 | goto ignore; | |
423 | } | ||
424 | |||
425 | /* | ||
426 | * This handles the basic CSI u ("fixterms") encoding and also the | ||
427 | * extended kitty keyboard encoding. | ||
428 | * | ||
429 | * • https://www.leonerd.org.uk/hacks/fixterms/ | ||
430 | * • https://sw.kovidgoyal.net/kitty/keyboard-protocol/ | ||
431 | * • https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:formatOtherKeys | ||
432 | * | ||
433 | * kitty params: key:unshifted-key:base-layout-key ; mods:event-type ; text | ||
434 | */ | ||
435 | 1757 | KeyCode key, mods = 0; | |
436 |
2/2✓ Branch 0 (17→18) taken 64 times.
✓ Branch 1 (17→33) taken 1693 times.
|
1757 | if (csi.final_byte == 'u') { |
437 |
3/6✓ Branch 0 (18→19) taken 64 times.
✗ Branch 1 (18→21) not taken.
✓ Branch 2 (19→20) taken 64 times.
✗ Branch 3 (19→21) not taken.
✗ Branch 4 (20→21) not taken.
✓ Branch 5 (20→22) taken 64 times.
|
64 | if (unlikely(csi.nsub[0] > 3 || csi.nsub[1] > 2 || csi.nsub[2] > 1)) { |
438 | // Don't allow unknown sub-params | ||
439 | ✗ | goto ignore; | |
440 | } | ||
441 | // Use the "base layout key", if present | ||
442 |
2/2✓ Branch 0 (22→23) taken 63 times.
✓ Branch 1 (22→24) taken 1 times.
|
64 | key = csi.params[0][csi.nsub[0] == 3 ? 2 : 0]; |
443 |
2/3✓ Branch 0 (24→25) taken 22 times.
✓ Branch 1 (24→29) taken 42 times.
✗ Branch 2 (24→32) not taken.
|
64 | switch (csi.nparams) { |
444 | 22 | case 3: | |
445 | case 2: | ||
446 |
2/2✓ Branch 0 (25→26) taken 1 times.
✓ Branch 1 (25→27) taken 21 times.
|
22 | if (unlikely(csi.params[1][1] > 2)) { |
447 | // Key release event | ||
448 | 1 | goto ignore; | |
449 | } | ||
450 | 21 | mods = decode_modifiers(csi.params[1][0]); | |
451 |
2/2✓ Branch 0 (27→28) taken 1 times.
✓ Branch 1 (27→29) taken 20 times.
|
21 | if (unlikely(mods == KEY_IGNORE)) { |
452 | 1 | goto ignore; | |
453 | } | ||
454 | // Fallthrough | ||
455 | case 1: | ||
456 | 62 | key = normalize_csi_u_keycode(mods, key); | |
457 |
2/2✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→31) taken 61 times.
|
62 | if (unlikely(key == KEY_IGNORE)) { |
458 | 1 | goto ignore; | |
459 | } | ||
460 | 61 | *k = mods | key; | |
461 | 61 | return i; | |
462 | } | ||
463 | ✗ | goto ignore; | |
464 | } | ||
465 | |||
466 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 1693 times.
|
1693 | if (unlikely(csi.have_subparams)) { |
467 | ✗ | goto ignore; | |
468 | } | ||
469 | |||
470 |
3/4✓ Branch 0 (35→36) taken 1157 times.
✗ Branch 1 (35→51) not taken.
✓ Branch 2 (35→53) taken 2 times.
✓ Branch 3 (35→56) taken 534 times.
|
1693 | switch (csi.final_byte) { |
471 | 1157 | case '~': | |
472 |
3/4✓ Branch 0 (36→37) taken 18 times.
✓ Branch 1 (36→42) taken 1107 times.
✓ Branch 2 (36→44) taken 32 times.
✗ Branch 3 (36→50) not taken.
|
1157 | switch (csi.nparams) { |
473 | 18 | case 3: | |
474 |
1/2✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→39) taken 18 times.
|
18 | if (unlikely(csi.params[0][0] != 27)) { |
475 | ✗ | goto ignore; | |
476 | } | ||
477 | 18 | mods = decode_modifiers(csi.params[1][0]); | |
478 |
1/2✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 18 times.
|
18 | if (unlikely(mods == KEY_IGNORE)) { |
479 | ✗ | goto ignore; | |
480 | } | ||
481 | // xterm-style modifyOtherKeys encoding | ||
482 | 18 | key = csi.params[2][0]; | |
483 | 18 | *k = normalize_csi_27_tilde_keycode(mods, key); | |
484 | 18 | return i; | |
485 | 1107 | case 2: | |
486 | 1107 | mods = decode_modifiers(csi.params[1][0]); | |
487 |
2/2✓ Branch 0 (42→43) taken 144 times.
✓ Branch 1 (42→44) taken 963 times.
|
1107 | if (unlikely(mods == KEY_IGNORE)) { |
488 | 144 | goto ignore; | |
489 | } | ||
490 | // Fallthrough | ||
491 | case 1: | ||
492 | 995 | key = decode_key_from_param(csi.params[0][0]); | |
493 |
2/2✓ Branch 0 (44→45) taken 6 times.
✓ Branch 1 (44→49) taken 989 times.
|
995 | if (key == KEY_IGNORE) { |
494 |
1/4✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→48) taken 6 times.
✗ Branch 2 (46→47) not taken.
✗ Branch 3 (46→48) not taken.
|
6 | if (csi.params[0][0] == 200 && mods == 0) { |
495 | ✗ | *k = KEYCODE_BRACKETED_PASTE; | |
496 | ✗ | return i; | |
497 | } | ||
498 | 6 | goto ignore; | |
499 | } | ||
500 | 989 | *k = mods | key; | |
501 | 989 | return i; | |
502 | } | ||
503 | ✗ | goto ignore; | |
504 | ✗ | case 't': | |
505 | ✗ | *k = parse_xtwinops_query_reply(&csi); | |
506 | ✗ | return i; | |
507 | 2 | case 'Z': | |
508 |
2/2✓ Branch 0 (53→54) taken 1 times.
✓ Branch 1 (53→55) taken 1 times.
|
2 | if (unlikely(csi.nparams != 0)) { |
509 | 1 | goto ignore; | |
510 | } | ||
511 | 1 | *k = MOD_SHIFT | KEY_TAB; | |
512 | 1 | return i; | |
513 | } | ||
514 | |||
515 |
3/3✓ Branch 0 (56→57) taken 518 times.
✓ Branch 1 (56→60) taken 13 times.
✓ Branch 2 (56→63) taken 3 times.
|
534 | switch (csi.nparams) { |
516 | 518 | case 2: | |
517 | 518 | mods = decode_modifiers(csi.params[1][0]); | |
518 |
3/4✓ Branch 0 (57→58) taken 452 times.
✓ Branch 1 (57→59) taken 66 times.
✗ Branch 2 (58→59) not taken.
✓ Branch 3 (58→60) taken 452 times.
|
518 | if (unlikely(mods == KEY_IGNORE || csi.params[0][0] != 1)) { |
519 | 66 | goto ignore; | |
520 | } | ||
521 | // Fallthrough | ||
522 | case 0: | ||
523 | 465 | key = decode_key_from_final_byte(csi.final_byte); | |
524 |
2/2✓ Branch 0 (60→61) taken 1 times.
✓ Branch 1 (60→62) taken 464 times.
|
465 | if (unlikely(key == KEY_IGNORE)) { |
525 | 1 | goto ignore; | |
526 | } | ||
527 | 464 | *k = mods | key; | |
528 | 464 | return i; | |
529 | } | ||
530 | |||
531 | 242 | ignore: | |
532 | 242 | *k = KEY_IGNORE; | |
533 | 242 | return i; | |
534 | } | ||
535 | |||
536 | 29 | static ssize_t parse_osc(const char *buf, size_t len, size_t i, KeyCode *k) | |
537 | { | ||
538 | 29 | char data[4096]; | |
539 |
2/2✓ Branch 0 (13→3) taken 87 times.
✓ Branch 1 (13→14) taken 25 times.
|
112 | for (size_t pos = 0; i < len; ) { |
540 | 87 | unsigned char ch = buf[i++]; | |
541 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→10) taken 83 times.
|
87 | if (unlikely(ch < 0x20)) { |
542 |
2/4✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 2 times.
✓ Branch 2 (4→7) taken 2 times.
✗ Branch 3 (4→9) not taken.
|
4 | switch (ch) { |
543 | ✗ | case 0x18: // CAN | |
544 | case 0x1A: // SUB | ||
545 | ✗ | *k = KEY_IGNORE; | |
546 | ✗ | return i; | |
547 | 2 | case 0x1B: // ESC (https://vt100.net/emu/dec_ansi_parser#STESC) | |
548 | 2 | i--; | |
549 | // Fallthrough | ||
550 | 4 | case 0x07: // BEL | |
551 | 4 | *k = parse_osc_query_reply(data, pos, pos >= sizeof(data)); | |
552 | 4 | return i; | |
553 | } | ||
554 | ✗ | continue; | |
555 | } | ||
556 | // Collect 0x20..0xFF (UTF-8 allowed) | ||
557 |
1/2✓ Branch 0 (10→11) taken 83 times.
✗ Branch 1 (10→12) not taken.
|
83 | if (likely(pos < sizeof(data))) { |
558 | 83 | data[pos++] = ch; | |
559 | } | ||
560 | } | ||
561 | |||
562 | // Unterminated sequence (possibly truncated) | ||
563 | return TPARSE_PARTIAL_MATCH; | ||
564 | } | ||
565 | |||
566 | 200 | static ssize_t parse_dcs(const char *buf, size_t len, size_t i, KeyCode *k) | |
567 | { | ||
568 | 200 | char data[4096]; | |
569 |
2/2✓ Branch 0 (12→3) taken 1653 times.
✓ Branch 1 (12→13) taken 185 times.
|
1838 | for (size_t pos = 0; i < len; ) { |
570 | 1653 | unsigned char ch = buf[i++]; | |
571 |
2/2✓ Branch 0 (3→4) taken 15 times.
✓ Branch 1 (3→9) taken 1638 times.
|
1653 | if (unlikely(ch < 0x20 || ch == 0x7F)) { |
572 |
1/3✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 15 times.
✗ Branch 2 (4→8) not taken.
|
15 | switch (ch) { |
573 | ✗ | case 0x18: // CAN | |
574 | case 0x1A: // SUB | ||
575 | ✗ | *k = KEY_IGNORE; | |
576 | ✗ | return i; | |
577 | 15 | case 0x1B: // ESC (https://vt100.net/emu/dec_ansi_parser#STESC) | |
578 | 15 | *k = parse_dcs_query_reply(data, pos, pos >= sizeof(data)); | |
579 | 15 | return i - 1; | |
580 | } | ||
581 | ✗ | continue; | |
582 | } | ||
583 | // Collect 0x20..0xFF (excluding 0x7F) | ||
584 |
1/2✓ Branch 0 (9→10) taken 1638 times.
✗ Branch 1 (9→11) not taken.
|
1638 | if (likely(pos < sizeof(data))) { |
585 | 1638 | data[pos++] = ch; | |
586 | } | ||
587 | } | ||
588 | |||
589 | // Unterminated sequence (possibly truncated) | ||
590 | return TPARSE_PARTIAL_MATCH; | ||
591 | } | ||
592 | |||
593 | /* | ||
594 | * Some terminals emit output resembling CSI/SS3 sequences without properly | ||
595 | * conforming to ECMA-48 §5.4, so we use an approach that accommodates | ||
596 | * terminal-specific special cases. This more or less precludes the use of | ||
597 | * a state machine (like e.g. the "dec_ansi_parser" mentioned at the top of | ||
598 | * this file). There's no real standard for "terminal to host" communications, | ||
599 | * although conforming to ECMA-48 §5.4 has been a de facto standard since the | ||
600 | * DEC VTs, with only a few (unnecessary and most likely not deliberate) | ||
601 | * exceptions in some emulators. | ||
602 | * | ||
603 | * See also: rxvt.c and linux.c | ||
604 | */ | ||
605 | 8782 | ssize_t term_parse_sequence(const char *buf, size_t length, KeyCode *k) | |
606 | { | ||
607 |
4/4✓ Branch 0 (2→3) taken 8781 times.
✓ Branch 1 (2→11) taken 1 times.
✓ Branch 2 (3→4) taken 8780 times.
✓ Branch 3 (3→11) taken 1 times.
|
8782 | if (unlikely(length == 0 || buf[0] != '\033')) { |
608 | return 0; | ||
609 |
2/2✓ Branch 0 (4→5) taken 7643 times.
✓ Branch 1 (4→11) taken 1137 times.
|
8780 | } else if (unlikely(length == 1)) { |
610 | return TPARSE_PARTIAL_MATCH; | ||
611 | } | ||
612 | |||
613 |
6/6✓ Branch 0 (5→6) taken 73 times.
✓ Branch 1 (5→7) taken 200 times.
✓ Branch 2 (5→8) taken 7339 times.
✓ Branch 3 (5→9) taken 29 times.
✓ Branch 4 (5→10) taken 1 times.
✓ Branch 5 (5→11) taken 1 times.
|
7643 | switch (buf[1]) { |
614 | 73 | case 'O': return parse_ss3(buf, length, 2, k); | |
615 | 200 | case 'P': return parse_dcs(buf, length, 2, k); | |
616 | 7339 | case '[': return parse_csi(buf, length, 2, k); | |
617 | 29 | case ']': return parse_osc(buf, length, 2, k); | |
618 | // String Terminator (https://vt100.net/emu/dec_ansi_parser#STESC) | ||
619 | 1 | case '\\': *k = KEY_IGNORE; return 2; | |
620 | } | ||
621 | |||
622 | return 0; | ||
623 | } | ||
624 |