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 | } Ecma48ByteType; | ||
26 | |||
27 | // https://sw.kovidgoyal.net/kitty/keyboard-protocol/#legacy-functional-keys | ||
28 | 503 | 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 47 times.
|
503 | 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 | 1666 | static KeyCode decode_modifiers(uint32_t n) | |
98 | { | ||
99 | 1666 | n--; | |
100 |
2/2✓ Branch 0 (2→3) taken 1455 times.
✓ Branch 1 (2→4) taken 211 times.
|
1666 | if (unlikely(n > 255)) { |
101 | return KEY_IGNORE; | ||
102 | } | ||
103 | |||
104 | 1455 | static_assert(1 == MOD_SHIFT >> KEYCODE_MODIFIER_OFFSET); | |
105 | 1455 | static_assert(2 == MOD_META >> KEYCODE_MODIFIER_OFFSET); | |
106 | 1455 | static_assert(4 == MOD_CTRL >> KEYCODE_MODIFIER_OFFSET); | |
107 | 1455 | static_assert(8 == MOD_SUPER >> KEYCODE_MODIFIER_OFFSET); | |
108 | 1455 | static_assert(16 == MOD_HYPER >> KEYCODE_MODIFIER_OFFSET); | |
109 | 1455 | static_assert(31 == MOD_MASK >> KEYCODE_MODIFIER_OFFSET); | |
110 | |||
111 | // Decode Meta and/or Alt as MOD_META and ignore Capslock/Numlock | ||
112 | 1455 | KeyCode mods = (n & 31) | ((n & 32) >> 4); | |
113 | 1455 | 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 was originally done for the sake of iTerm2's `CSI u` mode, | ||
180 | // which could be activated with `CSI > 1 u` but didn't fully | ||
181 | // conform to Kitty's key encoding scheme. This mode has seemingly | ||
182 | // been replaced with more complete support for the Kitty protocol | ||
183 | // and explicit support for activating the old mode has been also | ||
184 | // been removed from dte. However, this code is retained for now, | ||
185 | // since the `CSI u` encoding pre-dates the Kitty keyboard protocol | ||
186 | // and it's not clear yet whether removing it would regress support | ||
187 | // in less modern terminals and/or improve correctness for exotic | ||
188 | // keyboard layouts in modern (Kitty protocol supporting) terminals. | ||
189 | // See also: | ||
190 | // • https://gitlab.com/craigbarnes/dte/-/issues/130#note_870592688 | ||
191 | // • https://gitlab.com/craigbarnes/dte/-/issues/130#note_864512674 | ||
192 | // • https://gitlab.com/gnachman/iterm2/-/issues/10017 | ||
193 | // • https://gitlab.com/gnachman/iterm2/-/commit/9cd0241afd0655024153c8730d5b3ed1fe41faf7 | ||
194 | // • https://gitlab.com/gnachman/iterm2/-/commit/9cd0241afd0655024153c8730d5b3ed1fe41faf7#1d96fc7f79950509a8bc22bc59a1a82a438c890d_0_17 | ||
195 | // • https://gitlab.com/gnachman/iterm2/-/issues/7440#note_129599012 | ||
196 | // • https://sw.kovidgoyal.net/kitty/keyboard-protocol/#bugs-in-fixterms:~:text=Incorrectly%20encoding%20shifted%20keys%20when%20shift%20modifier%20is%20used | ||
197 | ✗ | return ascii_tolower(key); | |
198 | } | ||
199 | |||
200 | return key; | ||
201 | } | ||
202 | |||
203 | // Normalize KeyCode values originating from xterm-style "modifyOtherKeys" | ||
204 | // sequences (CSI 27 ; <modifiers> ; <key> ~) | ||
205 | 18 | static KeyCode normalize_csi_27_tilde_keycode(KeyCode mods, KeyCode key) | |
206 | { | ||
207 |
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) { |
208 | // https://codeberg.org/dnkl/foot/pulls/791#issuecomment-279784 | ||
209 | 3 | case '\b': return mods | KEY_BACKSPACE; | |
210 | 2 | case '\r': return mods | KEY_ENTER; | |
211 | 1 | case '\t': return mods | KEY_TAB; | |
212 | ✗ | case '\n': return mods | KEY_ENTER; | |
213 | 3 | case 27: return mods | KEY_ESCAPE; // ESC | |
214 | 3 | case 127: return mods | KEY_BACKSPACE; // DEL | |
215 | } | ||
216 | |||
217 |
1/2✓ Branch 0 (9→10) taken 6 times.
✗ Branch 1 (9→14) not taken.
|
6 | if (unlikely(key < 32)) { |
218 | return KEY_IGNORE; | ||
219 | } | ||
220 | |||
221 |
2/2✓ Branch 0 (10→11) taken 2 times.
✓ Branch 1 (10→13) taken 4 times.
|
6 | if (u_is_ascii_upper(key)) { |
222 |
1/2✓ Branch 0 (11→12) taken 2 times.
✗ Branch 1 (11→14) not taken.
|
2 | if ((mods & ~MOD_SHIFT) == 0) { |
223 | // [A-Z] and Shift+[A-Z] should be encoded as just [A-Z] | ||
224 | return key; | ||
225 | } | ||
226 | // [A-Z] with any other combination of modifiers should be | ||
227 | // converted to lowercase and have the MOD_SHIFT bit set. | ||
228 | // This is done in a "blanket" fashion and covers sequences | ||
229 | // that xterm never emits, because some terminals (e.g. tmux) | ||
230 | // emulate the protocol imperfectly. | ||
231 | 2 | return mods | MOD_SHIFT | key | 0x20; | |
232 | } | ||
233 | |||
234 | 4 | return mods | key; | |
235 | } | ||
236 | |||
237 | 73 | static ssize_t parse_ss3(const char *buf, size_t length, size_t i, KeyCode *k) | |
238 | { | ||
239 |
2/2✓ Branch 0 (2→3) taken 36 times.
✓ Branch 1 (2→13) taken 37 times.
|
73 | if (unlikely(i >= length)) { |
240 | return TPARSE_PARTIAL_MATCH; | ||
241 | } | ||
242 | |||
243 | 36 | const char ch = buf[i++]; | |
244 | 36 | KeyCode key = decode_key_from_final_byte(ch); | |
245 |
2/2✓ Branch 0 (3→4) taken 12 times.
✓ Branch 1 (3→5) taken 24 times.
|
36 | if (key != KEY_IGNORE) { |
246 | 12 | *k = key; | |
247 |
2/2✓ Branch 0 (5→6) taken 1 times.
✓ Branch 1 (5→7) taken 23 times.
|
24 | } else if (ch == 'X') { |
248 | 1 | *k = '='; | |
249 |
2/2✓ Branch 0 (7→8) taken 1 times.
✓ Branch 1 (7→9) taken 22 times.
|
23 | } else if (ch == ' ') { |
250 | 1 | *k = KEY_SPACE; | |
251 |
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') { |
252 | 17 | *k = ch - 64; | |
253 | } else { | ||
254 | 5 | *k = KEY_IGNORE; | |
255 | } | ||
256 | |||
257 | 36 | return i; | |
258 | } | ||
259 | |||
260 | 1943 | static Ecma48ByteType get_byte_type(unsigned char byte) | |
261 | { | ||
262 | 1943 | enum { | |
263 | C = BYTE_CONTROL, | ||
264 | I = BYTE_INTERMEDIATE, | ||
265 | P = BYTE_PARAMETER, | ||
266 | F = BYTE_FINAL, | ||
267 | f = BYTE_FINAL_PRIVATE, | ||
268 | x = BYTE_OTHER, | ||
269 | }; | ||
270 | |||
271 | // ECMA-48 divides bytes ("bit combinations") into rows of 16 columns. | ||
272 | // The byte classifications mostly fall into their own rows: | ||
273 | 1943 | static const uint8_t rows[16] = { | |
274 | C, C, I, P, F, F, F, f, | ||
275 | x, x, x, x, x, x, x, x | ||
276 | }; | ||
277 | |||
278 | // ... with the exception of byte 127 (DEL), which falls into rows[7] | ||
279 | // but isn't a private final byte like the others in that row: | ||
280 | 1943 | static_assert(BYTE_FINAL_PRIVATE + 1 == BYTE_DELETE); | |
281 | 1943 | unsigned int del_offset = (byte == 127); | |
282 | 1943 | return rows[byte >> 4] + del_offset; | |
283 | } | ||
284 | |||
285 | #define UNHANDLED(var, ...) unhandled(var, __LINE__, __VA_ARGS__) | ||
286 | |||
287 | PRINTF(3) | ||
288 | 398 | static void unhandled(bool *var, int line, const char *fmt, ...) | |
289 | { | ||
290 |
2/2✓ Branch 0 (2→3) taken 92 times.
✓ Branch 1 (2→5) taken 306 times.
|
398 | if (*var) { |
291 | // Only log the first error in a sequence | ||
292 | return; | ||
293 | } | ||
294 | |||
295 | 92 | *var = true; | |
296 | 92 | if (DEBUG_LOGGING_ENABLED) { | |
297 | 92 | va_list ap; | |
298 | 92 | va_start(ap, fmt); | |
299 | 92 | log_msgv(LOG_LEVEL_DEBUG, __FILE__, line, fmt, ap); | |
300 | 92 | va_end(ap); | |
301 | } | ||
302 | } | ||
303 | |||
304 | 7364 | size_t term_parse_csi_params(const char *buf, size_t len, size_t i, TermControlParams *csi) | |
305 | { | ||
306 | 7364 | size_t nparams = 0; | |
307 | 7364 | size_t nr_intermediate = 0; | |
308 | 7364 | size_t sub = 0; | |
309 | 7364 | size_t digits = 0; | |
310 | 7364 | uint32_t num = 0; | |
311 | 7364 | bool have_subparams = false; | |
312 | 7364 | bool err = false; | |
313 | |||
314 |
2/2✓ Branch 0 (36→3) taken 23328 times.
✓ Branch 1 (36→37) taken 5541 times.
|
28869 | while (i < len) { |
315 | 23328 | const char ch = buf[i++]; | |
316 |
4/4✓ Branch 0 (3→4) taken 16842 times.
✓ Branch 1 (3→7) taken 4509 times.
✓ Branch 2 (3→11) taken 34 times.
✓ Branch 3 (3→15) taken 1943 times.
|
23328 | switch (ch) { |
317 | 16842 | case '0': case '1': case '2': case '3': case '4': | |
318 | case '5': case '6': case '7': case '8': case '9': | ||
319 | 16842 | num = (num * 10) + (ch - '0'); | |
320 |
2/2✓ Branch 0 (4→5) taken 370 times.
✓ Branch 1 (4→6) taken 16472 times.
|
16842 | if (unlikely(num > UNICODE_MAX_VALID_CODEPOINT)) { |
321 | 370 | UNHANDLED(&err, "CSI param overflow"); | |
322 | } | ||
323 | 16842 | digits++; | |
324 | 16842 | continue; | |
325 | 4509 | case ';': | |
326 |
1/2✗ Branch 0 (7→8) not taken.
✓ Branch 1 (7→10) taken 4509 times.
|
4509 | if (unlikely(nparams >= ARRAYLEN(csi->params))) { |
327 | ✗ | UNHANDLED(&err, "too many params in CSI sequence"); | |
328 | ✗ | continue; | |
329 | } | ||
330 | 4509 | csi->nsub[nparams] = sub + 1; | |
331 | 4509 | csi->params[nparams++][sub] = num; | |
332 | 4509 | num = 0; | |
333 | 4509 | digits = 0; | |
334 | 4509 | sub = 0; | |
335 | 4509 | continue; | |
336 | 34 | case ':': | |
337 |
1/2✗ Branch 0 (11→12) not taken.
✓ Branch 1 (11→14) taken 34 times.
|
34 | if (unlikely(sub >= ARRAYLEN(csi->params[0]))) { |
338 | ✗ | UNHANDLED(&err, "too many sub-params in CSI sequence"); | |
339 | ✗ | continue; | |
340 | } | ||
341 | 34 | csi->params[nparams][sub++] = num; | |
342 | 34 | num = 0; | |
343 | 34 | digits = 0; | |
344 | 34 | have_subparams = true; | |
345 | 34 | continue; | |
346 | } | ||
347 | |||
348 |
5/7✓ Branch 0 (15→16) taken 100 times.
✓ Branch 1 (15→20) taken 1818 times.
✓ Branch 2 (15→25) taken 12 times.
✓ Branch 3 (15→27) taken 9 times.
✓ Branch 4 (15→31) taken 4 times.
✗ Branch 5 (15→32) not taken.
✗ Branch 6 (15→34) not taken.
|
1943 | switch (get_byte_type(ch)) { |
349 | 100 | case BYTE_INTERMEDIATE: | |
350 |
2/2✓ Branch 0 (16→17) taken 11 times.
✓ Branch 1 (16→18) taken 89 times.
|
100 | if (unlikely(nr_intermediate >= ARRAYLEN(csi->intermediate))) { |
351 | 11 | UNHANDLED(&err, "too many intermediate bytes in CSI sequence"); | |
352 | } else { | ||
353 | 89 | csi->intermediate[nr_intermediate++] = ch; | |
354 | } | ||
355 | 100 | continue; | |
356 | 1818 | case BYTE_FINAL: | |
357 | case BYTE_FINAL_PRIVATE: | ||
358 | 1818 | csi->final_byte = ch; | |
359 |
2/2✓ Branch 0 (20→21) taken 1801 times.
✓ Branch 1 (20→24) taken 17 times.
|
1818 | if (digits > 0) { |
360 |
1/2✗ Branch 0 (21→22) not taken.
✓ Branch 1 (21→23) taken 1801 times.
|
1801 | if (unlikely( |
361 | nparams >= ARRAYLEN(csi->params) | ||
362 | || sub >= ARRAYLEN(csi->params[0]) | ||
363 | )) { | ||
364 | ✗ | UNHANDLED(&err, "too many params/sub-params in CSI sequence"); | |
365 | } else { | ||
366 | 1801 | csi->nsub[nparams] = sub + 1; | |
367 | 1801 | csi->params[nparams++][sub] = num; | |
368 | } | ||
369 | } | ||
370 | 1818 | goto exit_loop; | |
371 | 12 | case BYTE_PARAMETER: | |
372 | // ECMA-48 §5.4.2: "bit combinations 03/12 to 03/15 are | ||
373 | // reserved for future standardization except when used | ||
374 | // as the first bit combination of the parameter string." | ||
375 | // (03/12 to 03/15 == '<' to '?') | ||
376 | 12 | UNHANDLED(&err, "unhandled CSI param byte: '%c'", ch); | |
377 | 12 | continue; | |
378 | 9 | case BYTE_CONTROL: | |
379 |
3/3✓ Branch 0 (27→28) taken 2 times.
✓ Branch 1 (27→29) taken 3 times.
✓ Branch 2 (27→31) taken 4 times.
|
9 | switch (ch) { |
380 | 2 | case 0x1B: // ESC | |
381 | // Don't consume ESC; it's the start of another sequence | ||
382 | 2 | i--; | |
383 | // Fallthrough | ||
384 | 5 | case 0x18: // CAN | |
385 | case 0x1A: // SUB | ||
386 | 5 | UNHANDLED(&err, "CSI sequence cancelled by 0x%02hhx", ch); | |
387 | 5 | csi->final_byte = ch; | |
388 | 5 | goto exit_loop; | |
389 | } | ||
390 | // Fallthrough | ||
391 | case BYTE_DELETE: | ||
392 | 8 | continue; | |
393 | ✗ | case BYTE_OTHER: | |
394 | ✗ | UNHANDLED(&err, "unhandled byte in CSI sequence: 0x%02hhx", ch); | |
395 | ✗ | continue; | |
396 | } | ||
397 | |||
398 | − | BUG("unhandled byte type"); | |
399 | err = true; | ||
400 | } | ||
401 | |||
402 | 5541 | exit_loop: | |
403 | 7364 | csi->nparams = nparams; | |
404 | 7364 | csi->nr_intermediate = nr_intermediate; | |
405 | 7364 | csi->have_subparams = have_subparams; | |
406 | 7364 | csi->unhandled_bytes = err; | |
407 | 7364 | return i; | |
408 | } | ||
409 | |||
410 | 7359 | static ssize_t parse_csi(const char *buf, size_t len, size_t i, KeyCode *k) | |
411 | { | ||
412 | 7359 | TermControlParams csi = {.nparams = 0}; | |
413 |
6/6✓ Branch 0 (2→3) taken 6323 times.
✓ Branch 1 (2→6) taken 1036 times.
✓ Branch 2 (3→4) taken 267 times.
✓ Branch 3 (3→6) taken 6056 times.
✓ Branch 4 (4→5) taken 253 times.
✓ Branch 5 (4→6) taken 14 times.
|
7359 | bool maybe_query_reply = (i < len && buf[i] >= '<' && buf[i] <= '?'); |
414 | 253 | uint8_t param_prefix = maybe_query_reply ? buf[i] : 0; | |
415 | 7359 | i = term_parse_csi_params(buf, len, i + (maybe_query_reply ? 1 : 0), &csi); | |
416 | |||
417 |
2/2✓ Branch 0 (7→8) taken 5541 times.
✓ Branch 1 (7→10) taken 1818 times.
|
7359 | if (unlikely(csi.final_byte == 0)) { |
418 | 5541 | BUG_ON(i < len); | |
419 | return TPARSE_PARTIAL_MATCH; | ||
420 | } | ||
421 |
2/2✓ Branch 0 (10→11) taken 17 times.
✓ Branch 1 (10→12) taken 1801 times.
|
1818 | if (unlikely(csi.unhandled_bytes)) { |
422 | 17 | goto ignore; | |
423 | } | ||
424 | |||
425 |
2/2✓ Branch 0 (12→13) taken 38 times.
✓ Branch 1 (12→15) taken 1763 times.
|
1801 | if (maybe_query_reply) { |
426 | 38 | *k = parse_csi_query_reply(&csi, param_prefix); | |
427 | 38 | return i; | |
428 | } | ||
429 | |||
430 |
2/2✓ Branch 0 (15→16) taken 4 times.
✓ Branch 1 (15→17) taken 1759 times.
|
1763 | if (unlikely(csi.nr_intermediate)) { |
431 | 4 | goto ignore; | |
432 | } | ||
433 | |||
434 | /* | ||
435 | * This handles the basic CSI u ("fixterms") encoding and also the | ||
436 | * extended kitty keyboard encoding. | ||
437 | * | ||
438 | * • https://www.leonerd.org.uk/hacks/fixterms/ | ||
439 | * • https://sw.kovidgoyal.net/kitty/keyboard-protocol/ | ||
440 | * • https://invisible-island.net/xterm/manpage/xterm.html#VT100-Widget-Resources:formatOtherKeys | ||
441 | * | ||
442 | * kitty params: key:unshifted-key:base-layout-key ; mods:event-type ; text | ||
443 | */ | ||
444 | 1759 | KeyCode key, mods = 0; | |
445 |
2/2✓ Branch 0 (17→18) taken 64 times.
✓ Branch 1 (17→33) taken 1695 times.
|
1759 | if (csi.final_byte == 'u') { |
446 |
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)) { |
447 | // Don't allow unknown sub-params | ||
448 | ✗ | goto ignore; | |
449 | } | ||
450 | // Use the "base layout key", if present | ||
451 |
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]; |
452 |
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) { |
453 | 22 | case 3: | |
454 | case 2: | ||
455 |
2/2✓ Branch 0 (25→26) taken 1 times.
✓ Branch 1 (25→27) taken 21 times.
|
22 | if (unlikely(csi.params[1][1] > 2)) { |
456 | // Key release event | ||
457 | 1 | goto ignore; | |
458 | } | ||
459 | 21 | mods = decode_modifiers(csi.params[1][0]); | |
460 |
2/2✓ Branch 0 (27→28) taken 1 times.
✓ Branch 1 (27→29) taken 20 times.
|
21 | if (unlikely(mods == KEY_IGNORE)) { |
461 | 1 | goto ignore; | |
462 | } | ||
463 | // Fallthrough | ||
464 | case 1: | ||
465 | 62 | key = normalize_csi_u_keycode(mods, key); | |
466 |
2/2✓ Branch 0 (29→30) taken 1 times.
✓ Branch 1 (29→31) taken 61 times.
|
62 | if (unlikely(key == KEY_IGNORE)) { |
467 | 1 | goto ignore; | |
468 | } | ||
469 | 61 | *k = mods | key; | |
470 | 61 | return i; | |
471 | } | ||
472 | ✗ | goto ignore; | |
473 | } | ||
474 | |||
475 |
1/2✗ Branch 0 (33→34) not taken.
✓ Branch 1 (33→35) taken 1695 times.
|
1695 | if (unlikely(csi.have_subparams)) { |
476 | ✗ | goto ignore; | |
477 | } | ||
478 | |||
479 |
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 536 times.
|
1695 | switch (csi.final_byte) { |
480 | 1157 | case '~': | |
481 |
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) { |
482 | 18 | case 3: | |
483 |
1/2✗ Branch 0 (37→38) not taken.
✓ Branch 1 (37→39) taken 18 times.
|
18 | if (unlikely(csi.params[0][0] != 27)) { |
484 | ✗ | goto ignore; | |
485 | } | ||
486 | 18 | mods = decode_modifiers(csi.params[1][0]); | |
487 |
1/2✗ Branch 0 (39→40) not taken.
✓ Branch 1 (39→41) taken 18 times.
|
18 | if (unlikely(mods == KEY_IGNORE)) { |
488 | ✗ | goto ignore; | |
489 | } | ||
490 | // xterm-style modifyOtherKeys encoding | ||
491 | 18 | key = csi.params[2][0]; | |
492 | 18 | *k = normalize_csi_27_tilde_keycode(mods, key); | |
493 | 18 | return i; | |
494 | 1107 | case 2: | |
495 | 1107 | mods = decode_modifiers(csi.params[1][0]); | |
496 |
2/2✓ Branch 0 (42→43) taken 144 times.
✓ Branch 1 (42→44) taken 963 times.
|
1107 | if (unlikely(mods == KEY_IGNORE)) { |
497 | 144 | goto ignore; | |
498 | } | ||
499 | // Fallthrough | ||
500 | case 1: | ||
501 | 995 | key = decode_key_from_param(csi.params[0][0]); | |
502 |
2/2✓ Branch 0 (44→45) taken 6 times.
✓ Branch 1 (44→49) taken 989 times.
|
995 | if (key == KEY_IGNORE) { |
503 |
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) { |
504 | ✗ | *k = KEYCODE_BRACKETED_PASTE; | |
505 | ✗ | return i; | |
506 | } | ||
507 | 6 | goto ignore; | |
508 | } | ||
509 | 989 | *k = mods | key; | |
510 | 989 | return i; | |
511 | } | ||
512 | ✗ | goto ignore; | |
513 | ✗ | case 't': | |
514 | ✗ | *k = parse_xtwinops_query_reply(&csi); | |
515 | ✗ | return i; | |
516 | 2 | case 'Z': | |
517 |
2/2✓ Branch 0 (53→54) taken 1 times.
✓ Branch 1 (53→55) taken 1 times.
|
2 | if (unlikely(csi.nparams != 0)) { |
518 | 1 | goto ignore; | |
519 | } | ||
520 | 1 | *k = MOD_SHIFT | KEY_TAB; | |
521 | 1 | return i; | |
522 | } | ||
523 | |||
524 |
3/3✓ Branch 0 (56→57) taken 520 times.
✓ Branch 1 (56→60) taken 13 times.
✓ Branch 2 (56→63) taken 3 times.
|
536 | switch (csi.nparams) { |
525 | 520 | case 2: | |
526 | 520 | mods = decode_modifiers(csi.params[1][0]); | |
527 |
3/4✓ Branch 0 (57→58) taken 454 times.
✓ Branch 1 (57→59) taken 66 times.
✗ Branch 2 (58→59) not taken.
✓ Branch 3 (58→60) taken 454 times.
|
520 | if (unlikely(mods == KEY_IGNORE || csi.params[0][0] != 1)) { |
528 | 66 | goto ignore; | |
529 | } | ||
530 | // Fallthrough | ||
531 | case 0: | ||
532 | 467 | key = decode_key_from_final_byte(csi.final_byte); | |
533 |
2/2✓ Branch 0 (60→61) taken 1 times.
✓ Branch 1 (60→62) taken 466 times.
|
467 | if (unlikely(key == KEY_IGNORE)) { |
534 | 1 | goto ignore; | |
535 | } | ||
536 | 466 | *k = mods | key; | |
537 | 466 | return i; | |
538 | } | ||
539 | |||
540 | 245 | ignore: | |
541 | 245 | *k = KEY_IGNORE; | |
542 | 245 | return i; | |
543 | } | ||
544 | |||
545 | 29 | static ssize_t parse_osc(const char *buf, size_t len, size_t i, KeyCode *k) | |
546 | { | ||
547 | 29 | char data[4096]; | |
548 |
2/2✓ Branch 0 (13→3) taken 87 times.
✓ Branch 1 (13→14) taken 25 times.
|
112 | for (size_t pos = 0; i < len; ) { |
549 | 87 | unsigned char ch = buf[i++]; | |
550 |
2/2✓ Branch 0 (3→4) taken 4 times.
✓ Branch 1 (3→10) taken 83 times.
|
87 | if (unlikely(ch < 0x20)) { |
551 |
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) { |
552 | ✗ | case 0x18: // CAN | |
553 | case 0x1A: // SUB | ||
554 | ✗ | *k = KEY_IGNORE; | |
555 | ✗ | return i; | |
556 | 2 | case 0x1B: // ESC (https://vt100.net/emu/dec_ansi_parser#STESC) | |
557 | 2 | i--; | |
558 | // Fallthrough | ||
559 | 4 | case 0x07: // BEL | |
560 | 4 | *k = parse_osc_query_reply(data, pos, pos >= sizeof(data)); | |
561 | 4 | return i; | |
562 | } | ||
563 | ✗ | continue; | |
564 | } | ||
565 | // Collect 0x20..0xFF (UTF-8 allowed) | ||
566 |
1/2✓ Branch 0 (10→11) taken 83 times.
✗ Branch 1 (10→12) not taken.
|
83 | if (likely(pos < sizeof(data))) { |
567 | 83 | data[pos++] = ch; | |
568 | } | ||
569 | } | ||
570 | |||
571 | // Unterminated sequence (possibly truncated) | ||
572 | return TPARSE_PARTIAL_MATCH; | ||
573 | } | ||
574 | |||
575 | 200 | static ssize_t parse_dcs(const char *buf, size_t len, size_t i, KeyCode *k) | |
576 | { | ||
577 | 200 | char data[4096]; | |
578 |
2/2✓ Branch 0 (12→3) taken 1653 times.
✓ Branch 1 (12→13) taken 185 times.
|
1838 | for (size_t pos = 0; i < len; ) { |
579 | 1653 | unsigned char ch = buf[i++]; | |
580 |
2/2✓ Branch 0 (3→4) taken 15 times.
✓ Branch 1 (3→9) taken 1638 times.
|
1653 | if (unlikely(ch < 0x20 || ch == 0x7F)) { |
581 |
1/3✗ Branch 0 (4→5) not taken.
✓ Branch 1 (4→6) taken 15 times.
✗ Branch 2 (4→8) not taken.
|
15 | switch (ch) { |
582 | ✗ | case 0x18: // CAN | |
583 | case 0x1A: // SUB | ||
584 | ✗ | *k = KEY_IGNORE; | |
585 | ✗ | return i; | |
586 | 15 | case 0x1B: // ESC (https://vt100.net/emu/dec_ansi_parser#STESC) | |
587 | 15 | *k = parse_dcs_query_reply(data, pos, pos >= sizeof(data)); | |
588 | 15 | return i - 1; | |
589 | } | ||
590 | ✗ | continue; | |
591 | } | ||
592 | // Collect 0x20..0xFF (excluding 0x7F) | ||
593 |
1/2✓ Branch 0 (9→10) taken 1638 times.
✗ Branch 1 (9→11) not taken.
|
1638 | if (likely(pos < sizeof(data))) { |
594 | 1638 | data[pos++] = ch; | |
595 | } | ||
596 | } | ||
597 | |||
598 | // Unterminated sequence (possibly truncated) | ||
599 | return TPARSE_PARTIAL_MATCH; | ||
600 | } | ||
601 | |||
602 | /* | ||
603 | * Some terminals emit output resembling CSI/SS3 sequences without properly | ||
604 | * conforming to ECMA-48 §5.4, so we use an approach that accommodates | ||
605 | * terminal-specific special cases. This more or less precludes the use of | ||
606 | * a state machine (like e.g. the "dec_ansi_parser" mentioned at the top of | ||
607 | * this file). There's no real standard for "terminal to host" communications, | ||
608 | * although conforming to ECMA-48 §5.4 has been a de facto standard since the | ||
609 | * DEC VTs, with only a few (unnecessary and most likely not deliberate) | ||
610 | * exceptions in some emulators. | ||
611 | * | ||
612 | * See also: rxvt.c and linux.c | ||
613 | */ | ||
614 | 8807 | ssize_t term_parse_sequence(const char *buf, size_t length, KeyCode *k) | |
615 | { | ||
616 |
4/4✓ Branch 0 (2→3) taken 8806 times.
✓ Branch 1 (2→11) taken 1 times.
✓ Branch 2 (3→4) taken 8805 times.
✓ Branch 3 (3→11) taken 1 times.
|
8807 | if (unlikely(length == 0 || buf[0] != '\033')) { |
617 | return 0; | ||
618 |
2/2✓ Branch 0 (4→5) taken 7663 times.
✓ Branch 1 (4→11) taken 1142 times.
|
8805 | } else if (unlikely(length == 1)) { |
619 | return TPARSE_PARTIAL_MATCH; | ||
620 | } | ||
621 | |||
622 |
6/6✓ Branch 0 (5→6) taken 73 times.
✓ Branch 1 (5→7) taken 200 times.
✓ Branch 2 (5→8) taken 7359 times.
✓ Branch 3 (5→9) taken 29 times.
✓ Branch 4 (5→10) taken 1 times.
✓ Branch 5 (5→11) taken 1 times.
|
7663 | switch (buf[1]) { |
623 | 73 | case 'O': return parse_ss3(buf, length, 2, k); | |
624 | 200 | case 'P': return parse_dcs(buf, length, 2, k); | |
625 | 7359 | case '[': return parse_csi(buf, length, 2, k); | |
626 | 29 | case ']': return parse_osc(buf, length, 2, k); | |
627 | // String Terminator (https://vt100.net/emu/dec_ansi_parser#STESC) | ||
628 | 1 | case '\\': *k = KEY_IGNORE; return 2; | |
629 | } | ||
630 | |||
631 | return 0; | ||
632 | } | ||
633 |