src/termbox2.h

filter: Statementstmt 44.1% 509 / 1152 Branchbran 29.3% 262 / 892 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 37.7% 771 / 2044 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Term/Fabulous/Render/Text.pm ↑ Summary src/termbox2.c »
1 /*
2 MIT License
3  
4 Copyright (c) 2015-2026 Adam Saponara <as@php.net>
5 2010-2020 nsf <no.smile.face@gmail.com>
6  
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13  
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16  
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 */
25 #ifndef TERMBOX_H_INCL
26 #define TERMBOX_H_INCL
27  
28 #ifndef _XOPEN_SOURCE
29 #define _XOPEN_SOURCE
30 #endif
31  
32 #ifndef _DEFAULT_SOURCE
33 #define _DEFAULT_SOURCE
34 #endif
35  
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <limits.h>
39 #include <signal.h>
40 #include <stdarg.h>
41 #include <stdint.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/stat.h>
46 #include <sys/types.h>
47  
48 #ifdef _WIN32
49 #ifndef WIN32_LEAN_AND_MEAN
50 #define WIN32_LEAN_AND_MEAN
51 #endif
52 #include <windows.h>
53 #include <io.h>
54 #else
55 #include <sys/ioctl.h>
56 #include <sys/select.h>
57 #include <sys/time.h>
58 #include <termios.h>
59 #include <unistd.h>
60 #endif
61  
62 #ifdef PATH_MAX
63 #define TB_PATH_MAX PATH_MAX
64 #else
65 #define TB_PATH_MAX 4096
66 #endif
67  
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71  
72 // __ffi_start
73  
74 #define TB_VERSION_STR "2.7.0-dev"
75  
76 /* The following compile-time options are supported:
77 *
78 * `TB_OPT_ATTR_W`: Integer width of `fg` and `bg` attributes. Valid values
79 * (assuming system support) are 16, 32, and 64. (See
80 * `uintattr_t`). 32 or 64 enables output mode
81 * `TB_OUTPUT_TRUECOLOR`. 64 enables additional style
82 * attributes. (See `tb_set_output_mode`.) Larger values
83 * consume more memory in exchange for more features.
84 * Defaults to 16.
85 *
86 * `TB_OPT_EGC`: If set, enable extended grapheme cluster support
87 * (`tb_extend_cell`, `tb_set_cell_ex`). Consumes more
88 * memory. Defaults off.
89 *
90 * `TB_OPT_PRINTF_BUF`: Write buffer size for printf operations. Represents the
91 * largest string that can be sent in one call to
92 * `tb_print*` and `tb_send*` functions. Defaults to 4096.
93 *
94 * `TB_OPT_READ_BUF`: Read buffer size for tty reads. Defaults to 64.
95 *
96 * `TB_OPT_LIBC_WCHAR`: If set, use libc's `wcwidth(3)`, `iswprint(3)`, etc
97 * instead of the built-in Unicode-aware versions. Note,
98 * libc's are locale-dependent and the caller must
99 * `setlocale(3)` `LC_CTYPE` to UTF-8. Defaults to
100 * built-in.
101 *
102 * `TB_OPT_TRUECOLOR`: Deprecated. Sets `TB_OPT_ATTR_W` to 32 if not already
103 * set.
104 */
105  
106 #if defined(TB_LIB_OPTS) || 0 // __tb_lib_opts
107 /* Ensure consistent compile-time options when using as a shared library */
108 #undef TB_OPT_ATTR_W
109 #undef TB_OPT_EGC
110 #undef TB_OPT_PRINTF_BUF
111 #undef TB_OPT_READ_BUF
112 #undef TB_OPT_LIBC_WCHAR
113 #define TB_OPT_ATTR_W 64
114 #define TB_OPT_EGC
115 #endif
116  
117 /* Ensure sane `TB_OPT_ATTR_W` (16, 32, or 64) */
118 #if defined TB_OPT_ATTR_W && TB_OPT_ATTR_W == 16
119 #elif defined TB_OPT_ATTR_W && TB_OPT_ATTR_W == 32
120 #elif defined TB_OPT_ATTR_W && TB_OPT_ATTR_W == 64
121 #else
122 #undef TB_OPT_ATTR_W
123 #if defined TB_OPT_TRUECOLOR // Deprecated. Back-compat for old flag.
124 #define TB_OPT_ATTR_W 32
125 #else
126 #define TB_OPT_ATTR_W 16
127 #endif
128 #endif
129  
130 /* Include wchar if opting libc */
131 #ifdef TB_OPT_LIBC_WCHAR
132 #ifdef _WIN32
133 #error "TB_OPT_LIBC_WCHAR is not available on Windows: its libc has no wcwidth(3)"
134 #endif
135 #include <wchar.h>
136 #include <wctype.h>
137 #endif
138  
139 /* ASCII key constants (`tb_event.key`) */
140 #define TB_KEY_CTRL_TILDE 0x00
141 #define TB_KEY_CTRL_2 0x00 // clash with `CTRL_TILDE`
142 #define TB_KEY_CTRL_A 0x01
143 #define TB_KEY_CTRL_B 0x02
144 #define TB_KEY_CTRL_C 0x03
145 #define TB_KEY_CTRL_D 0x04
146 #define TB_KEY_CTRL_E 0x05
147 #define TB_KEY_CTRL_F 0x06
148 #define TB_KEY_CTRL_G 0x07
149 #define TB_KEY_BACKSPACE 0x08
150 #define TB_KEY_CTRL_H 0x08 // clash with `CTRL_BACKSPACE`
151 #define TB_KEY_TAB 0x09
152 #define TB_KEY_CTRL_I 0x09 // clash with `TAB`
153 #define TB_KEY_CTRL_J 0x0a
154 #define TB_KEY_CTRL_K 0x0b
155 #define TB_KEY_CTRL_L 0x0c
156 #define TB_KEY_ENTER 0x0d
157 #define TB_KEY_CTRL_M 0x0d // clash with `ENTER`
158 #define TB_KEY_CTRL_N 0x0e
159 #define TB_KEY_CTRL_O 0x0f
160 #define TB_KEY_CTRL_P 0x10
161 #define TB_KEY_CTRL_Q 0x11
162 #define TB_KEY_CTRL_R 0x12
163 #define TB_KEY_CTRL_S 0x13
164 #define TB_KEY_CTRL_T 0x14
165 #define TB_KEY_CTRL_U 0x15
166 #define TB_KEY_CTRL_V 0x16
167 #define TB_KEY_CTRL_W 0x17
168 #define TB_KEY_CTRL_X 0x18
169 #define TB_KEY_CTRL_Y 0x19
170 #define TB_KEY_CTRL_Z 0x1a
171 #define TB_KEY_ESC 0x1b
172 #define TB_KEY_CTRL_LSQ_BRACKET 0x1b // clash with 'ESC'
173 #define TB_KEY_CTRL_3 0x1b // clash with 'ESC'
174 #define TB_KEY_CTRL_4 0x1c
175 #define TB_KEY_CTRL_BACKSLASH 0x1c // clash with 'CTRL_4'
176 #define TB_KEY_CTRL_5 0x1d
177 #define TB_KEY_CTRL_RSQ_BRACKET 0x1d // clash with 'CTRL_5'
178 #define TB_KEY_CTRL_6 0x1e
179 #define TB_KEY_CTRL_7 0x1f
180 #define TB_KEY_CTRL_SLASH 0x1f // clash with 'CTRL_7'
181 #define TB_KEY_CTRL_UNDERSCORE 0x1f // clash with 'CTRL_7'
182 #define TB_KEY_SPACE 0x20
183 #define TB_KEY_BACKSPACE2 0x7f
184 #define TB_KEY_CTRL_8 0x7f // clash with 'BACKSPACE2'
185  
186 #define tb_key_i(i) 0xffff - (i)
187 /* Terminal-dependent key constants (`tb_event.key`) and terminfo caps */
188 /* BEGIN codegen h */
189 /* Produced by ./codegen.sh on Tue, 03 Sep 2024 04:17:47 +0000 */
190 #define TB_KEY_F1 (0xffff - 0)
191 #define TB_KEY_F2 (0xffff - 1)
192 #define TB_KEY_F3 (0xffff - 2)
193 #define TB_KEY_F4 (0xffff - 3)
194 #define TB_KEY_F5 (0xffff - 4)
195 #define TB_KEY_F6 (0xffff - 5)
196 #define TB_KEY_F7 (0xffff - 6)
197 #define TB_KEY_F8 (0xffff - 7)
198 #define TB_KEY_F9 (0xffff - 8)
199 #define TB_KEY_F10 (0xffff - 9)
200 #define TB_KEY_F11 (0xffff - 10)
201 #define TB_KEY_F12 (0xffff - 11)
202 #define TB_KEY_INSERT (0xffff - 12)
203 #define TB_KEY_DELETE (0xffff - 13)
204 #define TB_KEY_HOME (0xffff - 14)
205 #define TB_KEY_END (0xffff - 15)
206 #define TB_KEY_PGUP (0xffff - 16)
207 #define TB_KEY_PGDN (0xffff - 17)
208 #define TB_KEY_ARROW_UP (0xffff - 18)
209 #define TB_KEY_ARROW_DOWN (0xffff - 19)
210 #define TB_KEY_ARROW_LEFT (0xffff - 20)
211 #define TB_KEY_ARROW_RIGHT (0xffff - 21)
212 #define TB_KEY_BACK_TAB (0xffff - 22)
213 #define TB_KEY_MOUSE_LEFT (0xffff - 23)
214 #define TB_KEY_MOUSE_RIGHT (0xffff - 24)
215 #define TB_KEY_MOUSE_MIDDLE (0xffff - 25)
216 #define TB_KEY_MOUSE_RELEASE (0xffff - 26)
217 #define TB_KEY_MOUSE_WHEEL_UP (0xffff - 27)
218 #define TB_KEY_MOUSE_WHEEL_DOWN (0xffff - 28)
219  
220 #define TB_CAP_F1 0
221 #define TB_CAP_F2 1
222 #define TB_CAP_F3 2
223 #define TB_CAP_F4 3
224 #define TB_CAP_F5 4
225 #define TB_CAP_F6 5
226 #define TB_CAP_F7 6
227 #define TB_CAP_F8 7
228 #define TB_CAP_F9 8
229 #define TB_CAP_F10 9
230 #define TB_CAP_F11 10
231 #define TB_CAP_F12 11
232 #define TB_CAP_INSERT 12
233 #define TB_CAP_DELETE 13
234 #define TB_CAP_HOME 14
235 #define TB_CAP_END 15
236 #define TB_CAP_PGUP 16
237 #define TB_CAP_PGDN 17
238 #define TB_CAP_ARROW_UP 18
239 #define TB_CAP_ARROW_DOWN 19
240 #define TB_CAP_ARROW_LEFT 20
241 #define TB_CAP_ARROW_RIGHT 21
242 #define TB_CAP_BACK_TAB 22
243 #define TB_CAP__COUNT_KEYS 23
244 #define TB_CAP_ENTER_CA 23
245 #define TB_CAP_EXIT_CA 24
246 #define TB_CAP_SHOW_CURSOR 25
247 #define TB_CAP_HIDE_CURSOR 26
248 #define TB_CAP_CLEAR_SCREEN 27
249 #define TB_CAP_SGR0 28
250 #define TB_CAP_UNDERLINE 29
251 #define TB_CAP_BOLD 30
252 #define TB_CAP_BLINK 31
253 #define TB_CAP_ITALIC 32
254 #define TB_CAP_REVERSE 33
255 #define TB_CAP_ENTER_KEYPAD 34
256 #define TB_CAP_EXIT_KEYPAD 35
257 #define TB_CAP_DIM 36
258 #define TB_CAP_INVISIBLE 37
259 #define TB_CAP__COUNT 38
260 /* END codegen h */
261  
262 /* Some hard-coded caps */
263 #define TB_HARDCAP_ENTER_MOUSE "\x1b[?1000h\x1b[?1002h\x1b[?1015h\x1b[?1006h"
264 #define TB_HARDCAP_EXIT_MOUSE "\x1b[?1006l\x1b[?1015l\x1b[?1002l\x1b[?1000l"
265 #define TB_HARDCAP_STRIKEOUT "\x1b[9m"
266 #define TB_HARDCAP_UNDERLINE_2 "\x1b[21m"
267 #define TB_HARDCAP_OVERLINE "\x1b[53m"
268  
269 /* Colors (numeric) and attributes (bitwise) (`tb_cell.fg`, `tb_cell.bg`) */
270 #define TB_DEFAULT 0x0000
271 #define TB_BLACK 0x0001
272 #define TB_RED 0x0002
273 #define TB_GREEN 0x0003
274 #define TB_YELLOW 0x0004
275 #define TB_BLUE 0x0005
276 #define TB_MAGENTA 0x0006
277 #define TB_CYAN 0x0007
278 #define TB_WHITE 0x0008
279  
280 #if TB_OPT_ATTR_W == 16
281 #define TB_BOLD 0x0100
282 #define TB_UNDERLINE 0x0200
283 #define TB_REVERSE 0x0400
284 #define TB_ITALIC 0x0800
285 #define TB_BLINK 0x1000
286 #define TB_HI_BLACK 0x2000
287 #define TB_BRIGHT 0x4000
288 #define TB_DIM 0x8000
289 #define TB_256_BLACK TB_HI_BLACK // `TB_256_BLACK` is deprecated
290 #else
291 // `TB_OPT_ATTR_W` is 32 or 64
292 #define TB_BOLD 0x01000000
293 #define TB_UNDERLINE 0x02000000
294 #define TB_REVERSE 0x04000000
295 #define TB_ITALIC 0x08000000
296 #define TB_BLINK 0x10000000
297 #define TB_HI_BLACK 0x20000000
298 #define TB_BRIGHT 0x40000000
299 #define TB_DIM 0x80000000
300 #define TB_TRUECOLOR_BOLD TB_BOLD // `TB_TRUECOLOR_*` is deprecated
301 #define TB_TRUECOLOR_UNDERLINE TB_UNDERLINE
302 #define TB_TRUECOLOR_REVERSE TB_REVERSE
303 #define TB_TRUECOLOR_ITALIC TB_ITALIC
304 #define TB_TRUECOLOR_BLINK TB_BLINK
305 #define TB_TRUECOLOR_BLACK TB_HI_BLACK
306 #endif
307  
308 #if TB_OPT_ATTR_W == 64
309 #define TB_STRIKEOUT 0x0000000100000000
310 #define TB_UNDERLINE_2 0x0000000200000000
311 #define TB_OVERLINE 0x0000000400000000
312 #define TB_INVISIBLE 0x0000000800000000
313 #endif
314  
315 /* Event types (`tb_event.type`) */
316 #define TB_EVENT_KEY 1
317 #define TB_EVENT_RESIZE 2
318 #define TB_EVENT_MOUSE 3
319  
320 /* Key modifiers (bitwise) (`tb_event.mod`) */
321 #define TB_MOD_ALT 1
322 #define TB_MOD_CTRL 2
323 #define TB_MOD_SHIFT 4
324 #define TB_MOD_MOTION 8
325  
326 /* Input modes (bitwise) (`tb_set_input_mode`) */
327 #define TB_INPUT_CURRENT 0
328 #define TB_INPUT_ESC 1
329 #define TB_INPUT_ALT 2
330 #define TB_INPUT_MOUSE 4
331  
332 /* Output modes (`tb_set_output_mode`) */
333 #define TB_OUTPUT_CURRENT 0
334 #define TB_OUTPUT_NORMAL 1
335 #define TB_OUTPUT_256 2
336 #define TB_OUTPUT_216 3
337 #define TB_OUTPUT_GRAYSCALE 4
338 #if TB_OPT_ATTR_W >= 32
339 #define TB_OUTPUT_TRUECOLOR 5
340 #endif
341  
342 /* Common function return values unless otherwise noted.
343 *
344 * Library behavior is undefined after receiving `TB_ERR_MEM`. Callers may
345 * attempt reinitializing by freeing memory, invoking `tb_shutdown`, then
346 * `tb_init`.
347 */
348 #define TB_OK 0
349 #define TB_ERR -1
350 #define TB_ERR_NEED_MORE -2
351 #define TB_ERR_INIT_ALREADY -3
352 #define TB_ERR_INIT_OPEN -4
353 #define TB_ERR_MEM -5
354 #define TB_ERR_NO_EVENT -6
355 #define TB_ERR_NO_TERM -7
356 #define TB_ERR_NOT_INIT -8
357 #define TB_ERR_OUT_OF_BOUNDS -9
358 #define TB_ERR_READ -10
359 #define TB_ERR_RESIZE_IOCTL -11
360 #define TB_ERR_RESIZE_PIPE -12
361 #define TB_ERR_RESIZE_SIGACTION -13
362 #define TB_ERR_POLL -14
363 #define TB_ERR_TCGETATTR -15
364 #define TB_ERR_TCSETATTR -16
365 #define TB_ERR_UNSUPPORTED_TERM -17
366 #define TB_ERR_RESIZE_WRITE -18
367 #define TB_ERR_RESIZE_POLL -19
368 #define TB_ERR_RESIZE_READ -20
369 #define TB_ERR_RESIZE_SSCANF -21
370 #define TB_ERR_CAP_COLLISION -22
371  
372 #define TB_ERR_SELECT TB_ERR_POLL
373 #define TB_ERR_RESIZE_SELECT TB_ERR_RESIZE_POLL
374  
375 /* Deprecated. Function types to be used with `tb_set_func`. */
376 #define TB_FUNC_EXTRACT_PRE 0
377 #define TB_FUNC_EXTRACT_POST 1
378  
379 /* Define this to set the size of the buffer used in `tb_printf` and
380 * `tb_sendf`.
381 */
382 #ifndef TB_OPT_PRINTF_BUF
383 #define TB_OPT_PRINTF_BUF 4096
384 #endif
385  
386 /* Define this to set the size of the buffer used when reading from the tty. */
387 #ifndef TB_OPT_READ_BUF
388 #define TB_OPT_READ_BUF 64
389 #endif
390  
391 /* Define this for limited back compat with termbox v1. */
392 #ifdef TB_OPT_V1_COMPAT
393 #define tb_change_cell tb_set_cell
394 #define tb_put_cell(x, y, c) tb_set_cell((x), (y), (c)->ch, (c)->fg, (c)->bg)
395 #define tb_set_clear_attributes tb_set_clear_attrs
396 #define tb_select_input_mode tb_set_input_mode
397 #define tb_select_output_mode tb_set_output_mode
398 #endif
399  
400 /* Define these to swap in a different allocator. */
401 #ifndef tb_malloc
402 #define tb_malloc malloc
403 #define tb_realloc realloc
404 #define tb_free free
405 #endif
406  
407 #if TB_OPT_ATTR_W == 64
408 typedef uint64_t uintattr_t;
409 #elif TB_OPT_ATTR_W == 32
410 typedef uint32_t uintattr_t;
411 #else // 16
412 typedef uint16_t uintattr_t;
413 #endif
414  
415 /* A cell in a 2d grid representing the terminal screen.
416 *
417 * The terminal screen is represented as 2d array of cells. The structure is
418 * optimized for dealing with single-width (`wcwidth==1`) Unicode codepoints,
419 * however some support for grapheme clusters (e.g., combining diacritical
420 * marks) and wide codepoints (e.g., Hiragana) is provided through `ech`,
421 * `nech`, and `cech` via `tb_set_cell_ex`. `ech` is only valid when `nech>0`,
422 * otherwise `ch` is used.
423 *
424 * For non-single-width codepoints, given `W=wcwidth(ch || ech)`:
425 *
426 * when `W<=0`: termbox forces a single-width cell. Callers should avoid this
427 * if aiming to render text accurately. Callers may use
428 * `tb_set_cell_ex` or `tb_print*` to render `W==0` combining
429 * characters.
430 *
431 * when `W>=2`: termbox zeroes out the following `W-1` cells and skips sending
432 * them to the tty. So, e.g., if the caller sets `x=0,y=0` to a
433 * `W==2` codepoint, the caller's next set should be at `x=2,y=0`.
434 * Anything set at `x=1,y=0` will be ignored. If there are not
435 * enough columns remaining on the line to render `W` cells,
436 * spaces are sent instead.
437 *
438 * See `tb_present` for implementation.
439 */
440 struct tb_cell {
441 uint32_t ch; // a Unicode codepoint
442 uintattr_t fg; // bitwise foreground attributes
443 uintattr_t bg; // bitwise background attributes
444 #ifdef TB_OPT_EGC
445 uint32_t *ech; // a grapheme cluster of Unicode codepoints, 0-terminated
446 size_t nech; // num elements in ech, 0 means use ch instead of ech
447 size_t cech; // num elements allocated for ech
448 #endif
449 };
450  
451 /* An incoming event from the tty.
452 *
453 * Given the event type, the following fields are relevant:
454 *
455 * when `TB_EVENT_KEY`: `key` xor `ch` (one will be zero) and `mod`. Note
456 * there is overlap between `TB_MOD_CTRL` and
457 * `TB_KEY_CTRL_*`. `TB_MOD_CTRL` and `TB_MOD_SHIFT`
458 * are only set as modifiers to `TB_KEY_ARROW_*`.
459 *
460 * when `TB_EVENT_RESIZE`: `w` and `h`
461 *
462 * when `TB_EVENT_MOUSE`: `key` (`TB_KEY_MOUSE_*`), `x`, and `y`
463 */
464 struct tb_event {
465 uint8_t type; // one of `TB_EVENT_*` constants
466 uint8_t mod; // bitwise `TB_MOD_*` constants
467 uint16_t key; // one of `TB_KEY_*` constants
468 uint32_t ch; // a Unicode codepoint
469 int32_t w; // resize width
470 int32_t h; // resize height
471 int32_t x; // mouse x
472 int32_t y; // mouse y
473 };
474  
475 /* Initialize the termbox library. This function should be called before any
476 * other functions. `tb_init` is equivalent to `tb_init_file("/dev/tty")`.
477 * After successful initialization, the library must be finalized using
478 * `tb_shutdown`.
479 *
480 * On Windows, the terminal is the console: `tb_init_file("/dev/tty")` (and so
481 * `tb_init`) opens `CONIN$` for input and `CONOUT$` for output, any other
482 * path is opened as a file that serves both. `tb_init_rwfd` takes C runtime
483 * file descriptors, as returned by `_open` or `_fileno`; a descriptor whose
484 * handle is a console is driven through the console API, any other
485 * (a pipe, a file) is read and written as a stream. One descriptor cannot
486 * serve both directions of the console, so `tb_init_fd` works only with
487 * descriptors that are not consoles. The console is switched to UTF-8 and
488 * to virtual terminal mode for input and output while termbox is
489 * initialized; keys arrive as the escape sequences an xterm would send.
490 */
491 int tb_init(void);
492 int tb_init_file(const char *path);
493 int tb_init_fd(int ttyfd);
494 int tb_init_rwfd(int rfd, int wfd);
495 int tb_shutdown(void);
496  
497 /* Return the size of the internal back buffer (which is the same as terminal's
498 * window size in rows and columns). The internal buffer can be resized after
499 * `tb_clear` or `tb_present` calls. Both dimensions have an unspecified
500 * negative value when called before `tb_init` or after `tb_shutdown`.
501 */
502 int tb_width(void);
503 int tb_height(void);
504  
505 /* Clear the internal back buffer using `TB_DEFAULT` or the attributes set by
506 * `tb_set_clear_attrs`.
507 */
508 int tb_clear(void);
509 int tb_set_clear_attrs(uintattr_t fg, uintattr_t bg);
510  
511 /* Synchronize the internal back buffer with the terminal by writing to tty. */
512 int tb_present(void);
513  
514 /* Clear the internal front buffer effectively forcing a complete re-render of
515 * the back buffer to the tty. It is not necessary to call this under normal
516 * circumstances.
517 */
518 int tb_invalidate(void);
519  
520 /* Set the position of the cursor. Upper-left cell is (0, 0). */
521 int tb_set_cursor(int cx, int cy);
522 int tb_hide_cursor(void);
523  
524 /* Set cell contents in the internal back buffer at the specified position.
525 *
526 * Use `tb_set_cell_ex` for rendering grapheme clusters (e.g., combining
527 * diacritical marks).
528 *
529 * Calling `tb_set_cell(x, y, ch, fg, bg)` is equivalent to
530 * `tb_set_cell_ex(x, y, &ch, 1, fg, bg)`.
531 *
532 * `tb_extend_cell` is a shortcut for appending 1 codepoint to `tb_cell.ech`.
533 *
534 * Non-printable (`iswprint(3)`) codepoints are replaced with `U+FFFD` at
535 * render time.
536 */
537 int tb_set_cell(int x, int y, uint32_t ch, uintattr_t fg, uintattr_t bg);
538 int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
539 uintattr_t bg);
540 int tb_extend_cell(int x, int y, uint32_t ch);
541  
542 /* Return a pointer to the cell at the specified position.
543 *
544 * Cell memory may be invalid or freed after subsequent library calls, so
545 * callers must copy any data that they need to persist across calls. Modifying
546 * cell memory results in undefined behavior.
547 *
548 * Callers may use pointer math to access cells relative to the requested one.
549 * The cell grid memory layout is a contiguous array indexable by the
550 * expression `(y * width) + x`.
551 *
552 * If `back` is non-zero, return cell from the internal back buffer. Otherwise,
553 * return cell from the front buffer. Note the front buffer is updated on each
554 * call to `tb_present`, whereas the back buffer is updated immediately by
555 * `tb_set_cell` and other functions that modify cell contents.
556 *
557 * If the position is invalid, `TB_ERR_OUT_OF_BOUNDS` is returned.
558 */
559 int tb_get_cell(int x, int y, int back, struct tb_cell **cell);
560  
561 /* Set the input mode. Termbox has two input modes:
562 *
563 * 1. `TB_INPUT_ESC`
564 * When escape (`\x1b`) is in the buffer and there's no match for an escape
565 * sequence, a key event for `TB_KEY_ESC` is returned.
566 *
567 * 2. `TB_INPUT_ALT`
568 * When escape (`\x1b`) is in the buffer and there's no match for an escape
569 * sequence, the next keyboard event is returned with a `TB_MOD_ALT`
570 * modifier.
571 *
572 * You can also apply `TB_INPUT_MOUSE` via bitwise OR operation to either of
573 * the modes (e.g., `TB_INPUT_ESC | TB_INPUT_MOUSE`) to receive
574 * `TB_EVENT_MOUSE` events. If none of the main two modes were set, but the
575 * mouse mode was, `TB_INPUT_ESC` is used. If for some reason you've decided to
576 * use `TB_INPUT_ESC | TB_INPUT_ALT`, it will behave as if only `TB_INPUT_ESC`
577 * was selected.
578 *
579 * If mode is `TB_INPUT_CURRENT`, return the current input mode.
580 *
581 * The default input mode is `TB_INPUT_ESC`.
582 */
583 int tb_set_input_mode(int mode);
584  
585 /* Set the output mode. Termbox has multiple output modes:
586 *
587 * 1. `TB_OUTPUT_NORMAL` => [0..8]
588 *
589 * This mode provides 8 different colors:
590 * `TB_BLACK`, `TB_RED`, `TB_GREEN`, `TB_YELLOW`,
591 * `TB_BLUE`, `TB_MAGENTA`, `TB_CYAN`, `TB_WHITE`
592 *
593 * Plus `TB_DEFAULT` which skips sending a color code (i.e., uses the
594 * terminal's default color).
595 *
596 * Colors (including `TB_DEFAULT`) may be bitwise OR'd with attributes:
597 * `TB_BOLD`, `TB_UNDERLINE`, `TB_REVERSE`, `TB_ITALIC`, `TB_BLINK`,
598 * `TB_BRIGHT`, `TB_DIM`
599 *
600 * The following style attributes are also available if compiled with
601 * `TB_OPT_ATTR_W` set to 64:
602 * `TB_STRIKEOUT`, `TB_UNDERLINE_2`, `TB_OVERLINE`, `TB_INVISIBLE`
603 *
604 * As in all modes, the value 0 is interpreted as `TB_DEFAULT` for
605 * convenience.
606 *
607 * Some notes: `TB_REVERSE` and `TB_BRIGHT` can be applied as either `fg` or
608 * `bg` attributes for the same effect. The rest of the attributes apply to
609 * `fg` only and are ignored as `bg` attributes.
610 *
611 * Example usage: `tb_set_cell(x, y, '@', TB_BLACK | TB_BOLD, TB_RED)`
612 *
613 * 2. `TB_OUTPUT_256` => [0..255] + `TB_HI_BLACK`
614 *
615 * In this mode you get 256 distinct colors (plus default):
616 * 0x00 (1): `TB_DEFAULT`
617 * `TB_HI_BLACK` (1): `TB_BLACK` in `TB_OUTPUT_NORMAL`
618 * 0x01..0x07 (7): the next 7 colors as in `TB_OUTPUT_NORMAL`
619 * 0x08..0x0f (8): bright versions of the above
620 * 0x10..0xe7 (216): 216 different colors
621 * 0xe8..0xff (24): 24 different shades of gray
622 *
623 * All `TB_*` style attributes except `TB_BRIGHT` may be bitwise OR'd as in
624 * `TB_OUTPUT_NORMAL`.
625 *
626 * Note `TB_HI_BLACK` must be used for black, as 0x00 represents default.
627 *
628 * 3. `TB_OUTPUT_216` => [0..216]
629 *
630 * This mode supports the 216-color range of `TB_OUTPUT_256` only, but you
631 * don't need to provide an offset:
632 * 0x00 (1): `TB_DEFAULT`
633 * 0x01..0xd8 (216): 216 different colors
634 *
635 * 4. `TB_OUTPUT_GRAYSCALE` => [0..24]
636 *
637 * This mode supports the 24-color range of `TB_OUTPUT_256` only, but you
638 * don't need to provide an offset:
639 * 0x00 (1): `TB_DEFAULT`
640 * 0x01..0x18 (24): 24 different shades of gray
641 *
642 * 5. `TB_OUTPUT_TRUECOLOR` => [0x000000..0xffffff] + `TB_HI_BLACK`
643 *
644 * This mode provides 24-bit color on supported terminals. The format is
645 * 0xRRGGBB.
646 *
647 * All `TB_*` style attributes except `TB_BRIGHT` may be bitwise OR'd as in
648 * `TB_OUTPUT_NORMAL`.
649 *
650 * Note `TB_HI_BLACK` must be used for black, as 0x000000 represents
651 * default.
652 *
653 * To use the terminal default color (i.e., to not send an escape code), pass
654 * `TB_DEFAULT`. For convenience, the value 0 is interpreted as `TB_DEFAULT` in
655 * all modes.
656 *
657 * Note, cell attributes persist after switching output modes. Any translation
658 * between, for example, `TB_OUTPUT_NORMAL`'s `TB_RED` and
659 * `TB_OUTPUT_TRUECOLOR`'s 0xff0000 must be performed by the caller. Also note
660 * that cells previously rendered in one mode may persist unchanged until the
661 * front buffer is cleared (such as after a resize event) at which point it
662 * will be re-interpreted and flushed according to the current mode. Callers
663 * may invoke `tb_invalidate` if it is desirable to immediately re-interpret
664 * and flush the entire screen according to the current mode.
665 *
666 * Note, not all terminals support all output modes, especially beyond
667 * `TB_OUTPUT_NORMAL`. There is also no very reliable way to determine color
668 * support dynamically. If portability is desired, callers are recommended to
669 * use `TB_OUTPUT_NORMAL` or make output mode end-user configurable. The same
670 * advice applies to style attributes.
671 *
672 * If mode is `TB_OUTPUT_CURRENT`, return the current output mode.
673 *
674 * The default output mode is `TB_OUTPUT_NORMAL`.
675 */
676 int tb_set_output_mode(int mode);
677  
678 /* Wait for an event up to `timeout_ms` milliseconds and populate `event` with
679 * it. If no event is available within the timeout period, `TB_ERR_NO_EVENT`
680 * is returned. On a resize event, the underlying `select(2)` call may be
681 * interrupted, yielding a return code of `TB_ERR_POLL`. In this case, you may
682 * check `errno` via `tb_last_errno`. If it's `EINTR`, you may elect to ignore
683 * that and call `tb_peek_event` again.
684 */
685 int tb_peek_event(struct tb_event *event, int timeout_ms);
686  
687 /* Same as `tb_peek_event` except no timeout. */
688 int tb_poll_event(struct tb_event *event);
689  
690 /* Internal termbox fds that can be used with `poll(2)`, `select(2)`, etc.
691 * externally. Callers must invoke `tb_poll_event` or `tb_peek_event` if
692 * fds become readable.
693 *
694 * On Windows, `ttyfd` is the input descriptor and `resizefd` is -1: resizes
695 * arrive as console input records, and a console descriptor cannot be
696 * watched with `select`. Poll with `tb_peek_event` instead.
697 */
698 int tb_get_fds(int *ttyfd, int *resizefd);
699  
700 /* Print and printf functions. Specify param `out_w` to determine width of
701 * printed string. Strings are interpreted as UTF-8.
702 *
703 * No attempt is made to do proper grapheme cluster parsing. With `TB_OPT_EGC`
704 * enabled, as a very coarse approximation, codepoints of `width==0` are tacked
705 * on to the previous codepoint via `tb_extend_cell`.
706 *
707 * Non-printable characters (`iswprint(3)`) and truncated UTF-8 byte sequences
708 * are replaced with U+FFFD.
709 *
710 * Newlines (`\n`) are supported with the caveat that `out_w` will return the
711 * width of the string as if it were on a single line.
712 *
713 * If the starting coordinate is out of bounds, `TB_ERR_OUT_OF_BOUNDS` is
714 * returned. If the starting coordinate is in bounds, but goes out of bounds,
715 * then the out-of-bounds portions of the string are ignored.
716 *
717 * For finer control, use `tb_set_cell`.
718 */
719 int tb_print(int x, int y, uintattr_t fg, uintattr_t bg, const char *str);
720 int tb_printf(int x, int y, uintattr_t fg, uintattr_t bg, const char *fmt, ...);
721 int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
722 const char *str);
723 int tb_printf_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
724 const char *fmt, ...);
725  
726 /* Send raw bytes to terminal. */
727 int tb_send(const char *buf, size_t nbuf);
728 int tb_sendf(const char *fmt, ...);
729  
730 /* Deprecated. Set custom callbacks. `fn_type` is one of `TB_FUNC_*` constants,
731 * `fn` is a compatible function pointer, or NULL to clear.
732 *
733 * `TB_FUNC_EXTRACT_PRE`:
734 * If specified, invoke this function BEFORE termbox tries to extract any
735 * escape sequences from the input buffer.
736 *
737 * `TB_FUNC_EXTRACT_POST`:
738 * If specified, invoke this function AFTER termbox tries (and fails) to
739 * extract any escape sequences from the input buffer.
740 */
741 int tb_set_func(int fn_type, int (*fn)(struct tb_event *, size_t *));
742  
743 /* Return byte length of codepoint given first byte of UTF-8 sequence (1-6). */
744 int tb_utf8_char_length(char c);
745  
746 /* Convert UTF-8 null-terminated byte sequence to UTF-32 codepoint.
747 *
748 * If `c` is an empty C string, return 0. `out` is left unchanged.
749 *
750 * If a null byte is encountered in the middle of the codepoint, return a
751 * negative number indicating how many bytes were processed. `out` is left
752 * unchanged.
753 *
754 * Otherwise, return byte length of codepoint (1-6).
755 */
756 int tb_utf8_char_to_unicode(uint32_t *out, const char *c);
757  
758 /* Convert UTF-32 codepoint to UTF-8 null-terminated byte sequence.
759 *
760 * `out` must be char[7] or greater. Return byte length of codepoint (1-6).
761 */
762 int tb_utf8_unicode_to_char(char *out, uint32_t c);
763  
764 /* Library utility functions */
765 int tb_last_errno(void);
766 const char *tb_strerror(int err);
767 struct tb_cell *tb_cell_buffer(void); // Deprecated
768 int tb_has_truecolor(void);
769 int tb_has_egc(void);
770 int tb_attr_width(void);
771 const char *tb_version(void);
772 int tb_iswprint(uint32_t ch);
773 int tb_wcwidth(uint32_t ch);
774  
775 /* Deprecation notice!
776 *
777 * The following will be removed in version 3.x (ABI version 3):
778 *
779 * `TB_256_BLACK` (use `TB_HI_BLACK`)
780 * `TB_OPT_TRUECOLOR` (use `TB_OPT_ATTR_W`)
781 * `TB_TRUECOLOR_BOLD` (use `TB_BOLD`)
782 * `TB_TRUECOLOR_UNDERLINE` (use `TB_UNDERLINE`)
783 * `TB_TRUECOLOR_REVERSE` (use `TB_REVERSE`)
784 * `TB_TRUECOLOR_ITALIC` (use `TB_ITALIC`)
785 * `TB_TRUECOLOR_BLINK` (use `TB_BLINK`)
786 * `TB_TRUECOLOR_BLACK` (use `TB_HI_BLACK`)
787 * `tb_cell_buffer`
788 * `tb_set_func`
789 * `TB_FUNC_EXTRACT_PRE`
790 * `TB_FUNC_EXTRACT_POST`
791 */
792  
793 #ifdef __cplusplus
794 }
795 #endif
796  
797 #endif // TERMBOX_H_INCL
798  
799 #ifdef TB_IMPL
800  
801 #define if_err_return(rv, expr) \
802 if (((rv) = (expr)) != TB_OK) return (rv)
803 #define if_err_break(rv, expr) \
804 if (((rv) = (expr)) != TB_OK) break
805 #define if_ok_return(rv, expr) \
806 if (((rv) = (expr)) == TB_OK) return (rv)
807 #define if_ok_or_need_more_return(rv, expr) \
808 if (((rv) = (expr)) == TB_OK || (rv) == TB_ERR_NEED_MORE) return (rv)
809  
810 #define send_literal(rv, a) \
811 if_err_return((rv), bytebuf_nputs(&global.out, (a), sizeof(a) - 1))
812  
813 #define send_num(rv, nbuf, n) \
814 if_err_return((rv), \
815 bytebuf_nputs(&global.out, (nbuf), convert_num((n), (nbuf))))
816  
817 #define snprintf_or_return(rv, str, sz, fmt, ...) \
818 do { \
819 (rv) = snprintf((str), (sz), (fmt), __VA_ARGS__); \
820 if ((rv) < 0 || (rv) >= (int)(sz)) return TB_ERR; \
821 } while (0)
822  
823 #define if_not_init_return() \
824 if (!global.initialized) return TB_ERR_NOT_INIT
825  
826 struct bytebuf {
827 char *buf;
828 size_t len;
829 size_t cap;
830 };
831  
832 struct cellbuf {
833 int width;
834 int height;
835 struct tb_cell *cells;
836 };
837  
838 struct cap_trie {
839 char c;
840 struct cap_trie *children;
841 size_t nchildren;
842 int is_leaf;
843 uint16_t key;
844 uint8_t mod;
845 };
846  
847 struct tb_global {
848 int ttyfd;
849 int rfd;
850 int wfd;
851 int ttyfd_open;
852 int resize_pipefd[2];
853 int width;
854 int height;
855 int cursor_x;
856 int cursor_y;
857 int last_x;
858 int last_y;
859 uintattr_t fg;
860 uintattr_t bg;
861 uintattr_t last_fg;
862 uintattr_t last_bg;
863 int input_mode;
864 int output_mode;
865 char *terminfo;
866 size_t nterminfo;
867 const char *caps[TB_CAP__COUNT];
868 struct cap_trie cap_trie;
869 struct bytebuf in;
870 struct bytebuf out;
871 struct cellbuf back;
872 struct cellbuf front;
873 #ifdef _WIN32
874 HANDLE in_handle;
875 HANDLE out_handle;
876 int in_is_console;
877 int out_is_console;
878 DWORD orig_in_mode;
879 DWORD orig_out_mode;
880 UINT orig_in_cp;
881 UINT orig_out_cp;
882 int has_orig_in_mode;
883 int has_orig_out_mode;
884 int resize_pending;
885 uint16_t pending_high_surrogate;
886 DWORD mouse_buttons;
887 #else
888 struct termios orig_tios;
889 int has_orig_tios;
890 #endif
891 int last_errno;
892 int initialized;
893 int (*fn_extract_esc_pre)(struct tb_event *, size_t *);
894 int (*fn_extract_esc_post)(struct tb_event *, size_t *);
895 char errbuf[1024];
896 };
897  
898 static struct tb_global global = {0};
899  
900 /* BEGIN codegen c */
901 /* Produced by ./codegen.sh on Tue, 03 Sep 2024 04:17:48 +0000 */
902  
903 static const int16_t terminfo_cap_indexes[] = {
904 66, // kf1 (TB_CAP_F1)
905 68, // kf2 (TB_CAP_F2)
906 69, // kf3 (TB_CAP_F3)
907 70, // kf4 (TB_CAP_F4)
908 71, // kf5 (TB_CAP_F5)
909 72, // kf6 (TB_CAP_F6)
910 73, // kf7 (TB_CAP_F7)
911 74, // kf8 (TB_CAP_F8)
912 75, // kf9 (TB_CAP_F9)
913 67, // kf10 (TB_CAP_F10)
914 216, // kf11 (TB_CAP_F11)
915 217, // kf12 (TB_CAP_F12)
916 77, // kich1 (TB_CAP_INSERT)
917 59, // kdch1 (TB_CAP_DELETE)
918 76, // khome (TB_CAP_HOME)
919 164, // kend (TB_CAP_END)
920 82, // kpp (TB_CAP_PGUP)
921 81, // knp (TB_CAP_PGDN)
922 87, // kcuu1 (TB_CAP_ARROW_UP)
923 61, // kcud1 (TB_CAP_ARROW_DOWN)
924 79, // kcub1 (TB_CAP_ARROW_LEFT)
925 83, // kcuf1 (TB_CAP_ARROW_RIGHT)
926 148, // kcbt (TB_CAP_BACK_TAB)
927 28, // smcup (TB_CAP_ENTER_CA)
928 40, // rmcup (TB_CAP_EXIT_CA)
929 16, // cnorm (TB_CAP_SHOW_CURSOR)
930 13, // civis (TB_CAP_HIDE_CURSOR)
931 5, // clear (TB_CAP_CLEAR_SCREEN)
932 39, // sgr0 (TB_CAP_SGR0)
933 36, // smul (TB_CAP_UNDERLINE)
934 27, // bold (TB_CAP_BOLD)
935 26, // blink (TB_CAP_BLINK)
936 311, // sitm (TB_CAP_ITALIC)
937 34, // rev (TB_CAP_REVERSE)
938 89, // smkx (TB_CAP_ENTER_KEYPAD)
939 88, // rmkx (TB_CAP_EXIT_KEYPAD)
940 30, // dim (TB_CAP_DIM)
941 32, // invis (TB_CAP_INVISIBLE)
942 };
943  
944 // xterm
945 static const char *xterm_caps[] = {
946 "\033OP", // kf1 (TB_CAP_F1)
947 "\033OQ", // kf2 (TB_CAP_F2)
948 "\033OR", // kf3 (TB_CAP_F3)
949 "\033OS", // kf4 (TB_CAP_F4)
950 "\033[15~", // kf5 (TB_CAP_F5)
951 "\033[17~", // kf6 (TB_CAP_F6)
952 "\033[18~", // kf7 (TB_CAP_F7)
953 "\033[19~", // kf8 (TB_CAP_F8)
954 "\033[20~", // kf9 (TB_CAP_F9)
955 "\033[21~", // kf10 (TB_CAP_F10)
956 "\033[23~", // kf11 (TB_CAP_F11)
957 "\033[24~", // kf12 (TB_CAP_F12)
958 "\033[2~", // kich1 (TB_CAP_INSERT)
959 "\033[3~", // kdch1 (TB_CAP_DELETE)
960 "\033OH", // khome (TB_CAP_HOME)
961 "\033OF", // kend (TB_CAP_END)
962 "\033[5~", // kpp (TB_CAP_PGUP)
963 "\033[6~", // knp (TB_CAP_PGDN)
964 "\033OA", // kcuu1 (TB_CAP_ARROW_UP)
965 "\033OB", // kcud1 (TB_CAP_ARROW_DOWN)
966 "\033OD", // kcub1 (TB_CAP_ARROW_LEFT)
967 "\033OC", // kcuf1 (TB_CAP_ARROW_RIGHT)
968 "\033[Z", // kcbt (TB_CAP_BACK_TAB)
969 "\033[?1049h\033[22;0;0t", // smcup (TB_CAP_ENTER_CA)
970 "\033[?1049l\033[23;0;0t", // rmcup (TB_CAP_EXIT_CA)
971 "\033[?12l\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
972 "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
973 "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
974 "\033(B\033[m", // sgr0 (TB_CAP_SGR0)
975 "\033[4m", // smul (TB_CAP_UNDERLINE)
976 "\033[1m", // bold (TB_CAP_BOLD)
977 "\033[5m", // blink (TB_CAP_BLINK)
978 "\033[3m", // sitm (TB_CAP_ITALIC)
979 "\033[7m", // rev (TB_CAP_REVERSE)
980 "\033[?1h\033=", // smkx (TB_CAP_ENTER_KEYPAD)
981 "\033[?1l\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
982 "\033[2m", // dim (TB_CAP_DIM)
983 "\033[8m", // invis (TB_CAP_INVISIBLE)
984 };
985  
986 // linux
987 static const char *linux_caps[] = {
988 "\033[[A", // kf1 (TB_CAP_F1)
989 "\033[[B", // kf2 (TB_CAP_F2)
990 "\033[[C", // kf3 (TB_CAP_F3)
991 "\033[[D", // kf4 (TB_CAP_F4)
992 "\033[[E", // kf5 (TB_CAP_F5)
993 "\033[17~", // kf6 (TB_CAP_F6)
994 "\033[18~", // kf7 (TB_CAP_F7)
995 "\033[19~", // kf8 (TB_CAP_F8)
996 "\033[20~", // kf9 (TB_CAP_F9)
997 "\033[21~", // kf10 (TB_CAP_F10)
998 "\033[23~", // kf11 (TB_CAP_F11)
999 "\033[24~", // kf12 (TB_CAP_F12)
1000 "\033[2~", // kich1 (TB_CAP_INSERT)
1001 "\033[3~", // kdch1 (TB_CAP_DELETE)
1002 "\033[1~", // khome (TB_CAP_HOME)
1003 "\033[4~", // kend (TB_CAP_END)
1004 "\033[5~", // kpp (TB_CAP_PGUP)
1005 "\033[6~", // knp (TB_CAP_PGDN)
1006 "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
1007 "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
1008 "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
1009 "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
1010 "\033\011", // kcbt (TB_CAP_BACK_TAB)
1011 "", // smcup (TB_CAP_ENTER_CA)
1012 "", // rmcup (TB_CAP_EXIT_CA)
1013 "\033[?25h\033[?0c", // cnorm (TB_CAP_SHOW_CURSOR)
1014 "\033[?25l\033[?1c", // civis (TB_CAP_HIDE_CURSOR)
1015 "\033[H\033[J", // clear (TB_CAP_CLEAR_SCREEN)
1016 "\033[m\017", // sgr0 (TB_CAP_SGR0)
1017 "\033[4m", // smul (TB_CAP_UNDERLINE)
1018 "\033[1m", // bold (TB_CAP_BOLD)
1019 "\033[5m", // blink (TB_CAP_BLINK)
1020 "", // sitm (TB_CAP_ITALIC)
1021 "\033[7m", // rev (TB_CAP_REVERSE)
1022 "", // smkx (TB_CAP_ENTER_KEYPAD)
1023 "", // rmkx (TB_CAP_EXIT_KEYPAD)
1024 "\033[2m", // dim (TB_CAP_DIM)
1025 "", // invis (TB_CAP_INVISIBLE)
1026 };
1027  
1028 // screen
1029 static const char *screen_caps[] = {
1030 "\033OP", // kf1 (TB_CAP_F1)
1031 "\033OQ", // kf2 (TB_CAP_F2)
1032 "\033OR", // kf3 (TB_CAP_F3)
1033 "\033OS", // kf4 (TB_CAP_F4)
1034 "\033[15~", // kf5 (TB_CAP_F5)
1035 "\033[17~", // kf6 (TB_CAP_F6)
1036 "\033[18~", // kf7 (TB_CAP_F7)
1037 "\033[19~", // kf8 (TB_CAP_F8)
1038 "\033[20~", // kf9 (TB_CAP_F9)
1039 "\033[21~", // kf10 (TB_CAP_F10)
1040 "\033[23~", // kf11 (TB_CAP_F11)
1041 "\033[24~", // kf12 (TB_CAP_F12)
1042 "\033[2~", // kich1 (TB_CAP_INSERT)
1043 "\033[3~", // kdch1 (TB_CAP_DELETE)
1044 "\033[1~", // khome (TB_CAP_HOME)
1045 "\033[4~", // kend (TB_CAP_END)
1046 "\033[5~", // kpp (TB_CAP_PGUP)
1047 "\033[6~", // knp (TB_CAP_PGDN)
1048 "\033OA", // kcuu1 (TB_CAP_ARROW_UP)
1049 "\033OB", // kcud1 (TB_CAP_ARROW_DOWN)
1050 "\033OD", // kcub1 (TB_CAP_ARROW_LEFT)
1051 "\033OC", // kcuf1 (TB_CAP_ARROW_RIGHT)
1052 "\033[Z", // kcbt (TB_CAP_BACK_TAB)
1053 "\033[?1049h", // smcup (TB_CAP_ENTER_CA)
1054 "\033[?1049l", // rmcup (TB_CAP_EXIT_CA)
1055 "\033[34h\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
1056 "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
1057 "\033[H\033[J", // clear (TB_CAP_CLEAR_SCREEN)
1058 "\033[m\017", // sgr0 (TB_CAP_SGR0)
1059 "\033[4m", // smul (TB_CAP_UNDERLINE)
1060 "\033[1m", // bold (TB_CAP_BOLD)
1061 "\033[5m", // blink (TB_CAP_BLINK)
1062 "", // sitm (TB_CAP_ITALIC)
1063 "\033[7m", // rev (TB_CAP_REVERSE)
1064 "\033[?1h\033=", // smkx (TB_CAP_ENTER_KEYPAD)
1065 "\033[?1l\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
1066 "\033[2m", // dim (TB_CAP_DIM)
1067 "", // invis (TB_CAP_INVISIBLE)
1068 };
1069  
1070 // rxvt-256color
1071 static const char *rxvt_256color_caps[] = {
1072 "\033[11~", // kf1 (TB_CAP_F1)
1073 "\033[12~", // kf2 (TB_CAP_F2)
1074 "\033[13~", // kf3 (TB_CAP_F3)
1075 "\033[14~", // kf4 (TB_CAP_F4)
1076 "\033[15~", // kf5 (TB_CAP_F5)
1077 "\033[17~", // kf6 (TB_CAP_F6)
1078 "\033[18~", // kf7 (TB_CAP_F7)
1079 "\033[19~", // kf8 (TB_CAP_F8)
1080 "\033[20~", // kf9 (TB_CAP_F9)
1081 "\033[21~", // kf10 (TB_CAP_F10)
1082 "\033[23~", // kf11 (TB_CAP_F11)
1083 "\033[24~", // kf12 (TB_CAP_F12)
1084 "\033[2~", // kich1 (TB_CAP_INSERT)
1085 "\033[3~", // kdch1 (TB_CAP_DELETE)
1086 "\033[7~", // khome (TB_CAP_HOME)
1087 "\033[8~", // kend (TB_CAP_END)
1088 "\033[5~", // kpp (TB_CAP_PGUP)
1089 "\033[6~", // knp (TB_CAP_PGDN)
1090 "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
1091 "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
1092 "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
1093 "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
1094 "\033[Z", // kcbt (TB_CAP_BACK_TAB)
1095 "\0337\033[?47h", // smcup (TB_CAP_ENTER_CA)
1096 "\033[2J\033[?47l\0338", // rmcup (TB_CAP_EXIT_CA)
1097 "\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
1098 "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
1099 "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
1100 "\033[m\017", // sgr0 (TB_CAP_SGR0)
1101 "\033[4m", // smul (TB_CAP_UNDERLINE)
1102 "\033[1m", // bold (TB_CAP_BOLD)
1103 "\033[5m", // blink (TB_CAP_BLINK)
1104 "", // sitm (TB_CAP_ITALIC)
1105 "\033[7m", // rev (TB_CAP_REVERSE)
1106 "\033=", // smkx (TB_CAP_ENTER_KEYPAD)
1107 "\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
1108 "", // dim (TB_CAP_DIM)
1109 "", // invis (TB_CAP_INVISIBLE)
1110 };
1111  
1112 // rxvt-unicode
1113 static const char *rxvt_unicode_caps[] = {
1114 "\033[11~", // kf1 (TB_CAP_F1)
1115 "\033[12~", // kf2 (TB_CAP_F2)
1116 "\033[13~", // kf3 (TB_CAP_F3)
1117 "\033[14~", // kf4 (TB_CAP_F4)
1118 "\033[15~", // kf5 (TB_CAP_F5)
1119 "\033[17~", // kf6 (TB_CAP_F6)
1120 "\033[18~", // kf7 (TB_CAP_F7)
1121 "\033[19~", // kf8 (TB_CAP_F8)
1122 "\033[20~", // kf9 (TB_CAP_F9)
1123 "\033[21~", // kf10 (TB_CAP_F10)
1124 "\033[23~", // kf11 (TB_CAP_F11)
1125 "\033[24~", // kf12 (TB_CAP_F12)
1126 "\033[2~", // kich1 (TB_CAP_INSERT)
1127 "\033[3~", // kdch1 (TB_CAP_DELETE)
1128 "\033[7~", // khome (TB_CAP_HOME)
1129 "\033[8~", // kend (TB_CAP_END)
1130 "\033[5~", // kpp (TB_CAP_PGUP)
1131 "\033[6~", // knp (TB_CAP_PGDN)
1132 "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
1133 "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
1134 "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
1135 "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
1136 "\033[Z", // kcbt (TB_CAP_BACK_TAB)
1137 "\033[?1049h", // smcup (TB_CAP_ENTER_CA)
1138 "\033[r\033[?1049l", // rmcup (TB_CAP_EXIT_CA)
1139 "\033[?12l\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
1140 "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
1141 "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
1142 "\033[m\033(B", // sgr0 (TB_CAP_SGR0)
1143 "\033[4m", // smul (TB_CAP_UNDERLINE)
1144 "\033[1m", // bold (TB_CAP_BOLD)
1145 "\033[5m", // blink (TB_CAP_BLINK)
1146 "\033[3m", // sitm (TB_CAP_ITALIC)
1147 "\033[7m", // rev (TB_CAP_REVERSE)
1148 "\033=", // smkx (TB_CAP_ENTER_KEYPAD)
1149 "\033>", // rmkx (TB_CAP_EXIT_KEYPAD)
1150 "", // dim (TB_CAP_DIM)
1151 "", // invis (TB_CAP_INVISIBLE)
1152 };
1153  
1154 // Eterm
1155 static const char *eterm_caps[] = {
1156 "\033[11~", // kf1 (TB_CAP_F1)
1157 "\033[12~", // kf2 (TB_CAP_F2)
1158 "\033[13~", // kf3 (TB_CAP_F3)
1159 "\033[14~", // kf4 (TB_CAP_F4)
1160 "\033[15~", // kf5 (TB_CAP_F5)
1161 "\033[17~", // kf6 (TB_CAP_F6)
1162 "\033[18~", // kf7 (TB_CAP_F7)
1163 "\033[19~", // kf8 (TB_CAP_F8)
1164 "\033[20~", // kf9 (TB_CAP_F9)
1165 "\033[21~", // kf10 (TB_CAP_F10)
1166 "\033[23~", // kf11 (TB_CAP_F11)
1167 "\033[24~", // kf12 (TB_CAP_F12)
1168 "\033[2~", // kich1 (TB_CAP_INSERT)
1169 "\033[3~", // kdch1 (TB_CAP_DELETE)
1170 "\033[7~", // khome (TB_CAP_HOME)
1171 "\033[8~", // kend (TB_CAP_END)
1172 "\033[5~", // kpp (TB_CAP_PGUP)
1173 "\033[6~", // knp (TB_CAP_PGDN)
1174 "\033[A", // kcuu1 (TB_CAP_ARROW_UP)
1175 "\033[B", // kcud1 (TB_CAP_ARROW_DOWN)
1176 "\033[D", // kcub1 (TB_CAP_ARROW_LEFT)
1177 "\033[C", // kcuf1 (TB_CAP_ARROW_RIGHT)
1178 "", // kcbt (TB_CAP_BACK_TAB)
1179 "\0337\033[?47h", // smcup (TB_CAP_ENTER_CA)
1180 "\033[2J\033[?47l\0338", // rmcup (TB_CAP_EXIT_CA)
1181 "\033[?25h", // cnorm (TB_CAP_SHOW_CURSOR)
1182 "\033[?25l", // civis (TB_CAP_HIDE_CURSOR)
1183 "\033[H\033[2J", // clear (TB_CAP_CLEAR_SCREEN)
1184 "\033[m\017", // sgr0 (TB_CAP_SGR0)
1185 "\033[4m", // smul (TB_CAP_UNDERLINE)
1186 "\033[1m", // bold (TB_CAP_BOLD)
1187 "\033[5m", // blink (TB_CAP_BLINK)
1188 "", // sitm (TB_CAP_ITALIC)
1189 "\033[7m", // rev (TB_CAP_REVERSE)
1190 "", // smkx (TB_CAP_ENTER_KEYPAD)
1191 "", // rmkx (TB_CAP_EXIT_KEYPAD)
1192 "", // dim (TB_CAP_DIM)
1193 "", // invis (TB_CAP_INVISIBLE)
1194 };
1195  
1196 static struct {
1197 const char *name;
1198 const char **caps;
1199 const char *alias;
1200 } builtin_terms[] = {
1201 {"xterm", xterm_caps, "" },
1202 {"linux", linux_caps, "" },
1203 {"screen", screen_caps, "tmux"},
1204 {"rxvt-256color", rxvt_256color_caps, "" },
1205 {"rxvt-unicode", rxvt_unicode_caps, "rxvt"},
1206 {"Eterm", eterm_caps, "" },
1207 {NULL, NULL, NULL },
1208 };
1209  
1210 /* END codegen c */
1211  
1212 static struct {
1213 const char *cap;
1214 const uint16_t key;
1215 const uint8_t mod;
1216 } builtin_mod_caps[] = {
1217 // xterm arrows
1218 {"\x1b[1;2A", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
1219 {"\x1b[1;3A", TB_KEY_ARROW_UP, TB_MOD_ALT },
1220 {"\x1b[1;4A", TB_KEY_ARROW_UP, TB_MOD_ALT | TB_MOD_SHIFT },
1221 {"\x1b[1;5A", TB_KEY_ARROW_UP, TB_MOD_CTRL },
1222 {"\x1b[1;6A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_SHIFT },
1223 {"\x1b[1;7A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
1224 {"\x1b[1;8A", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1225  
1226 {"\x1b[1;2B", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
1227 {"\x1b[1;3B", TB_KEY_ARROW_DOWN, TB_MOD_ALT },
1228 {"\x1b[1;4B", TB_KEY_ARROW_DOWN, TB_MOD_ALT | TB_MOD_SHIFT },
1229 {"\x1b[1;5B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
1230 {"\x1b[1;6B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_SHIFT },
1231 {"\x1b[1;7B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
1232 {"\x1b[1;8B", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1233  
1234 {"\x1b[1;2C", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
1235 {"\x1b[1;3C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT },
1236 {"\x1b[1;4C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT | TB_MOD_SHIFT },
1237 {"\x1b[1;5C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
1238 {"\x1b[1;6C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_SHIFT },
1239 {"\x1b[1;7C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
1240 {"\x1b[1;8C", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1241  
1242 {"\x1b[1;2D", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
1243 {"\x1b[1;3D", TB_KEY_ARROW_LEFT, TB_MOD_ALT },
1244 {"\x1b[1;4D", TB_KEY_ARROW_LEFT, TB_MOD_ALT | TB_MOD_SHIFT },
1245 {"\x1b[1;5D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
1246 {"\x1b[1;6D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_SHIFT },
1247 {"\x1b[1;7D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
1248 {"\x1b[1;8D", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1249  
1250 // xterm keys
1251 {"\x1b[1;2H", TB_KEY_HOME, TB_MOD_SHIFT },
1252 {"\x1b[1;3H", TB_KEY_HOME, TB_MOD_ALT },
1253 {"\x1b[1;4H", TB_KEY_HOME, TB_MOD_ALT | TB_MOD_SHIFT },
1254 {"\x1b[1;5H", TB_KEY_HOME, TB_MOD_CTRL },
1255 {"\x1b[1;6H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_SHIFT },
1256 {"\x1b[1;7H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT },
1257 {"\x1b[1;8H", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1258  
1259 {"\x1b[1;2F", TB_KEY_END, TB_MOD_SHIFT },
1260 {"\x1b[1;3F", TB_KEY_END, TB_MOD_ALT },
1261 {"\x1b[1;4F", TB_KEY_END, TB_MOD_ALT | TB_MOD_SHIFT },
1262 {"\x1b[1;5F", TB_KEY_END, TB_MOD_CTRL },
1263 {"\x1b[1;6F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_SHIFT },
1264 {"\x1b[1;7F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT },
1265 {"\x1b[1;8F", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1266  
1267 {"\x1b[2;2~", TB_KEY_INSERT, TB_MOD_SHIFT },
1268 {"\x1b[2;3~", TB_KEY_INSERT, TB_MOD_ALT },
1269 {"\x1b[2;4~", TB_KEY_INSERT, TB_MOD_ALT | TB_MOD_SHIFT },
1270 {"\x1b[2;5~", TB_KEY_INSERT, TB_MOD_CTRL },
1271 {"\x1b[2;6~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_SHIFT },
1272 {"\x1b[2;7~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT },
1273 {"\x1b[2;8~", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1274  
1275 {"\x1b[3;2~", TB_KEY_DELETE, TB_MOD_SHIFT },
1276 {"\x1b[3;3~", TB_KEY_DELETE, TB_MOD_ALT },
1277 {"\x1b[3;4~", TB_KEY_DELETE, TB_MOD_ALT | TB_MOD_SHIFT },
1278 {"\x1b[3;5~", TB_KEY_DELETE, TB_MOD_CTRL },
1279 {"\x1b[3;6~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_SHIFT },
1280 {"\x1b[3;7~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT },
1281 {"\x1b[3;8~", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1282  
1283 {"\x1b[5;2~", TB_KEY_PGUP, TB_MOD_SHIFT },
1284 {"\x1b[5;3~", TB_KEY_PGUP, TB_MOD_ALT },
1285 {"\x1b[5;4~", TB_KEY_PGUP, TB_MOD_ALT | TB_MOD_SHIFT },
1286 {"\x1b[5;5~", TB_KEY_PGUP, TB_MOD_CTRL },
1287 {"\x1b[5;6~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_SHIFT },
1288 {"\x1b[5;7~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT },
1289 {"\x1b[5;8~", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1290  
1291 {"\x1b[6;2~", TB_KEY_PGDN, TB_MOD_SHIFT },
1292 {"\x1b[6;3~", TB_KEY_PGDN, TB_MOD_ALT },
1293 {"\x1b[6;4~", TB_KEY_PGDN, TB_MOD_ALT | TB_MOD_SHIFT },
1294 {"\x1b[6;5~", TB_KEY_PGDN, TB_MOD_CTRL },
1295 {"\x1b[6;6~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_SHIFT },
1296 {"\x1b[6;7~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT },
1297 {"\x1b[6;8~", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1298  
1299 {"\x1b[1;2P", TB_KEY_F1, TB_MOD_SHIFT },
1300 {"\x1b[1;3P", TB_KEY_F1, TB_MOD_ALT },
1301 {"\x1b[1;4P", TB_KEY_F1, TB_MOD_ALT | TB_MOD_SHIFT },
1302 {"\x1b[1;5P", TB_KEY_F1, TB_MOD_CTRL },
1303 {"\x1b[1;6P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_SHIFT },
1304 {"\x1b[1;7P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT },
1305 {"\x1b[1;8P", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1306  
1307 {"\x1b[1;2Q", TB_KEY_F2, TB_MOD_SHIFT },
1308 {"\x1b[1;3Q", TB_KEY_F2, TB_MOD_ALT },
1309 {"\x1b[1;4Q", TB_KEY_F2, TB_MOD_ALT | TB_MOD_SHIFT },
1310 {"\x1b[1;5Q", TB_KEY_F2, TB_MOD_CTRL },
1311 {"\x1b[1;6Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_SHIFT },
1312 {"\x1b[1;7Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT },
1313 {"\x1b[1;8Q", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1314  
1315 {"\x1b[1;2R", TB_KEY_F3, TB_MOD_SHIFT },
1316 {"\x1b[1;3R", TB_KEY_F3, TB_MOD_ALT },
1317 {"\x1b[1;4R", TB_KEY_F3, TB_MOD_ALT | TB_MOD_SHIFT },
1318 {"\x1b[1;5R", TB_KEY_F3, TB_MOD_CTRL },
1319 {"\x1b[1;6R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_SHIFT },
1320 {"\x1b[1;7R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT },
1321 {"\x1b[1;8R", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1322  
1323 {"\x1b[1;2S", TB_KEY_F4, TB_MOD_SHIFT },
1324 {"\x1b[1;3S", TB_KEY_F4, TB_MOD_ALT },
1325 {"\x1b[1;4S", TB_KEY_F4, TB_MOD_ALT | TB_MOD_SHIFT },
1326 {"\x1b[1;5S", TB_KEY_F4, TB_MOD_CTRL },
1327 {"\x1b[1;6S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_SHIFT },
1328 {"\x1b[1;7S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT },
1329 {"\x1b[1;8S", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1330  
1331 {"\x1b[15;2~", TB_KEY_F5, TB_MOD_SHIFT },
1332 {"\x1b[15;3~", TB_KEY_F5, TB_MOD_ALT },
1333 {"\x1b[15;4~", TB_KEY_F5, TB_MOD_ALT | TB_MOD_SHIFT },
1334 {"\x1b[15;5~", TB_KEY_F5, TB_MOD_CTRL },
1335 {"\x1b[15;6~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_SHIFT },
1336 {"\x1b[15;7~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT },
1337 {"\x1b[15;8~", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1338  
1339 {"\x1b[17;2~", TB_KEY_F6, TB_MOD_SHIFT },
1340 {"\x1b[17;3~", TB_KEY_F6, TB_MOD_ALT },
1341 {"\x1b[17;4~", TB_KEY_F6, TB_MOD_ALT | TB_MOD_SHIFT },
1342 {"\x1b[17;5~", TB_KEY_F6, TB_MOD_CTRL },
1343 {"\x1b[17;6~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_SHIFT },
1344 {"\x1b[17;7~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT },
1345 {"\x1b[17;8~", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1346  
1347 {"\x1b[18;2~", TB_KEY_F7, TB_MOD_SHIFT },
1348 {"\x1b[18;3~", TB_KEY_F7, TB_MOD_ALT },
1349 {"\x1b[18;4~", TB_KEY_F7, TB_MOD_ALT | TB_MOD_SHIFT },
1350 {"\x1b[18;5~", TB_KEY_F7, TB_MOD_CTRL },
1351 {"\x1b[18;6~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_SHIFT },
1352 {"\x1b[18;7~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT },
1353 {"\x1b[18;8~", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1354  
1355 {"\x1b[19;2~", TB_KEY_F8, TB_MOD_SHIFT },
1356 {"\x1b[19;3~", TB_KEY_F8, TB_MOD_ALT },
1357 {"\x1b[19;4~", TB_KEY_F8, TB_MOD_ALT | TB_MOD_SHIFT },
1358 {"\x1b[19;5~", TB_KEY_F8, TB_MOD_CTRL },
1359 {"\x1b[19;6~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_SHIFT },
1360 {"\x1b[19;7~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT },
1361 {"\x1b[19;8~", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1362  
1363 {"\x1b[20;2~", TB_KEY_F9, TB_MOD_SHIFT },
1364 {"\x1b[20;3~", TB_KEY_F9, TB_MOD_ALT },
1365 {"\x1b[20;4~", TB_KEY_F9, TB_MOD_ALT | TB_MOD_SHIFT },
1366 {"\x1b[20;5~", TB_KEY_F9, TB_MOD_CTRL },
1367 {"\x1b[20;6~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_SHIFT },
1368 {"\x1b[20;7~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT },
1369 {"\x1b[20;8~", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1370  
1371 {"\x1b[21;2~", TB_KEY_F10, TB_MOD_SHIFT },
1372 {"\x1b[21;3~", TB_KEY_F10, TB_MOD_ALT },
1373 {"\x1b[21;4~", TB_KEY_F10, TB_MOD_ALT | TB_MOD_SHIFT },
1374 {"\x1b[21;5~", TB_KEY_F10, TB_MOD_CTRL },
1375 {"\x1b[21;6~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_SHIFT },
1376 {"\x1b[21;7~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT },
1377 {"\x1b[21;8~", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1378  
1379 {"\x1b[23;2~", TB_KEY_F11, TB_MOD_SHIFT },
1380 {"\x1b[23;3~", TB_KEY_F11, TB_MOD_ALT },
1381 {"\x1b[23;4~", TB_KEY_F11, TB_MOD_ALT | TB_MOD_SHIFT },
1382 {"\x1b[23;5~", TB_KEY_F11, TB_MOD_CTRL },
1383 {"\x1b[23;6~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_SHIFT },
1384 {"\x1b[23;7~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT },
1385 {"\x1b[23;8~", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1386  
1387 {"\x1b[24;2~", TB_KEY_F12, TB_MOD_SHIFT },
1388 {"\x1b[24;3~", TB_KEY_F12, TB_MOD_ALT },
1389 {"\x1b[24;4~", TB_KEY_F12, TB_MOD_ALT | TB_MOD_SHIFT },
1390 {"\x1b[24;5~", TB_KEY_F12, TB_MOD_CTRL },
1391 {"\x1b[24;6~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_SHIFT },
1392 {"\x1b[24;7~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT },
1393 {"\x1b[24;8~", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1394  
1395 // rxvt arrows
1396 {"\x1b[a", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
1397 {"\x1b\x1b[A", TB_KEY_ARROW_UP, TB_MOD_ALT },
1398 {"\x1b\x1b[a", TB_KEY_ARROW_UP, TB_MOD_ALT | TB_MOD_SHIFT },
1399 {"\x1bOa", TB_KEY_ARROW_UP, TB_MOD_CTRL },
1400 {"\x1b\x1bOa", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
1401  
1402 {"\x1b[b", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
1403 {"\x1b\x1b[B", TB_KEY_ARROW_DOWN, TB_MOD_ALT },
1404 {"\x1b\x1b[b", TB_KEY_ARROW_DOWN, TB_MOD_ALT | TB_MOD_SHIFT },
1405 {"\x1bOb", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
1406 {"\x1b\x1bOb", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
1407  
1408 {"\x1b[c", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
1409 {"\x1b\x1b[C", TB_KEY_ARROW_RIGHT, TB_MOD_ALT },
1410 {"\x1b\x1b[c", TB_KEY_ARROW_RIGHT, TB_MOD_ALT | TB_MOD_SHIFT },
1411 {"\x1bOc", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
1412 {"\x1b\x1bOc", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
1413  
1414 {"\x1b[d", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
1415 {"\x1b\x1b[D", TB_KEY_ARROW_LEFT, TB_MOD_ALT },
1416 {"\x1b\x1b[d", TB_KEY_ARROW_LEFT, TB_MOD_ALT | TB_MOD_SHIFT },
1417 {"\x1bOd", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
1418 {"\x1b\x1bOd", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
1419  
1420 // rxvt keys
1421 {"\x1b[7$", TB_KEY_HOME, TB_MOD_SHIFT },
1422 {"\x1b\x1b[7~", TB_KEY_HOME, TB_MOD_ALT },
1423 {"\x1b\x1b[7$", TB_KEY_HOME, TB_MOD_ALT | TB_MOD_SHIFT },
1424 {"\x1b[7^", TB_KEY_HOME, TB_MOD_CTRL },
1425 {"\x1b[7@", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_SHIFT },
1426 {"\x1b\x1b[7^", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT },
1427 {"\x1b\x1b[7@", TB_KEY_HOME, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1428  
1429 {"\x1b\x1b[8~", TB_KEY_END, TB_MOD_ALT },
1430 {"\x1b\x1b[8$", TB_KEY_END, TB_MOD_ALT | TB_MOD_SHIFT },
1431 {"\x1b[8^", TB_KEY_END, TB_MOD_CTRL },
1432 {"\x1b\x1b[8^", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT },
1433 {"\x1b\x1b[8@", TB_KEY_END, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1434 {"\x1b[8@", TB_KEY_END, TB_MOD_CTRL | TB_MOD_SHIFT },
1435 {"\x1b[8$", TB_KEY_END, TB_MOD_SHIFT },
1436  
1437 {"\x1b\x1b[2~", TB_KEY_INSERT, TB_MOD_ALT },
1438 {"\x1b\x1b[2$", TB_KEY_INSERT, TB_MOD_ALT | TB_MOD_SHIFT },
1439 {"\x1b[2^", TB_KEY_INSERT, TB_MOD_CTRL },
1440 {"\x1b\x1b[2^", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT },
1441 {"\x1b\x1b[2@", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1442 {"\x1b[2@", TB_KEY_INSERT, TB_MOD_CTRL | TB_MOD_SHIFT },
1443 {"\x1b[2$", TB_KEY_INSERT, TB_MOD_SHIFT },
1444  
1445 {"\x1b\x1b[3~", TB_KEY_DELETE, TB_MOD_ALT },
1446 {"\x1b\x1b[3$", TB_KEY_DELETE, TB_MOD_ALT | TB_MOD_SHIFT },
1447 {"\x1b[3^", TB_KEY_DELETE, TB_MOD_CTRL },
1448 {"\x1b\x1b[3^", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT },
1449 {"\x1b\x1b[3@", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1450 {"\x1b[3@", TB_KEY_DELETE, TB_MOD_CTRL | TB_MOD_SHIFT },
1451 {"\x1b[3$", TB_KEY_DELETE, TB_MOD_SHIFT },
1452  
1453 {"\x1b\x1b[5~", TB_KEY_PGUP, TB_MOD_ALT },
1454 {"\x1b\x1b[5$", TB_KEY_PGUP, TB_MOD_ALT | TB_MOD_SHIFT },
1455 {"\x1b[5^", TB_KEY_PGUP, TB_MOD_CTRL },
1456 {"\x1b\x1b[5^", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT },
1457 {"\x1b\x1b[5@", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1458 {"\x1b[5@", TB_KEY_PGUP, TB_MOD_CTRL | TB_MOD_SHIFT },
1459 {"\x1b[5$", TB_KEY_PGUP, TB_MOD_SHIFT },
1460  
1461 {"\x1b\x1b[6~", TB_KEY_PGDN, TB_MOD_ALT },
1462 {"\x1b\x1b[6$", TB_KEY_PGDN, TB_MOD_ALT | TB_MOD_SHIFT },
1463 {"\x1b[6^", TB_KEY_PGDN, TB_MOD_CTRL },
1464 {"\x1b\x1b[6^", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT },
1465 {"\x1b\x1b[6@", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1466 {"\x1b[6@", TB_KEY_PGDN, TB_MOD_CTRL | TB_MOD_SHIFT },
1467 {"\x1b[6$", TB_KEY_PGDN, TB_MOD_SHIFT },
1468  
1469 {"\x1b\x1b[11~", TB_KEY_F1, TB_MOD_ALT },
1470 {"\x1b\x1b[23~", TB_KEY_F1, TB_MOD_ALT | TB_MOD_SHIFT },
1471 {"\x1b[11^", TB_KEY_F1, TB_MOD_CTRL },
1472 {"\x1b\x1b[11^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT },
1473 {"\x1b\x1b[23^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1474 {"\x1b[23^", TB_KEY_F1, TB_MOD_CTRL | TB_MOD_SHIFT },
1475 {"\x1b[23~", TB_KEY_F1, TB_MOD_SHIFT },
1476  
1477 {"\x1b\x1b[12~", TB_KEY_F2, TB_MOD_ALT },
1478 {"\x1b\x1b[24~", TB_KEY_F2, TB_MOD_ALT | TB_MOD_SHIFT },
1479 {"\x1b[12^", TB_KEY_F2, TB_MOD_CTRL },
1480 {"\x1b\x1b[12^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT },
1481 {"\x1b\x1b[24^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1482 {"\x1b[24^", TB_KEY_F2, TB_MOD_CTRL | TB_MOD_SHIFT },
1483 {"\x1b[24~", TB_KEY_F2, TB_MOD_SHIFT },
1484  
1485 {"\x1b\x1b[13~", TB_KEY_F3, TB_MOD_ALT },
1486 {"\x1b\x1b[25~", TB_KEY_F3, TB_MOD_ALT | TB_MOD_SHIFT },
1487 {"\x1b[13^", TB_KEY_F3, TB_MOD_CTRL },
1488 {"\x1b\x1b[13^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT },
1489 {"\x1b\x1b[25^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1490 {"\x1b[25^", TB_KEY_F3, TB_MOD_CTRL | TB_MOD_SHIFT },
1491 {"\x1b[25~", TB_KEY_F3, TB_MOD_SHIFT },
1492  
1493 {"\x1b\x1b[14~", TB_KEY_F4, TB_MOD_ALT },
1494 {"\x1b\x1b[26~", TB_KEY_F4, TB_MOD_ALT | TB_MOD_SHIFT },
1495 {"\x1b[14^", TB_KEY_F4, TB_MOD_CTRL },
1496 {"\x1b\x1b[14^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT },
1497 {"\x1b\x1b[26^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1498 {"\x1b[26^", TB_KEY_F4, TB_MOD_CTRL | TB_MOD_SHIFT },
1499 {"\x1b[26~", TB_KEY_F4, TB_MOD_SHIFT },
1500  
1501 {"\x1b\x1b[15~", TB_KEY_F5, TB_MOD_ALT },
1502 {"\x1b\x1b[28~", TB_KEY_F5, TB_MOD_ALT | TB_MOD_SHIFT },
1503 {"\x1b[15^", TB_KEY_F5, TB_MOD_CTRL },
1504 {"\x1b\x1b[15^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT },
1505 {"\x1b\x1b[28^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1506 {"\x1b[28^", TB_KEY_F5, TB_MOD_CTRL | TB_MOD_SHIFT },
1507 {"\x1b[28~", TB_KEY_F5, TB_MOD_SHIFT },
1508  
1509 {"\x1b\x1b[17~", TB_KEY_F6, TB_MOD_ALT },
1510 {"\x1b\x1b[29~", TB_KEY_F6, TB_MOD_ALT | TB_MOD_SHIFT },
1511 {"\x1b[17^", TB_KEY_F6, TB_MOD_CTRL },
1512 {"\x1b\x1b[17^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT },
1513 {"\x1b\x1b[29^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1514 {"\x1b[29^", TB_KEY_F6, TB_MOD_CTRL | TB_MOD_SHIFT },
1515 {"\x1b[29~", TB_KEY_F6, TB_MOD_SHIFT },
1516  
1517 {"\x1b\x1b[18~", TB_KEY_F7, TB_MOD_ALT },
1518 {"\x1b\x1b[31~", TB_KEY_F7, TB_MOD_ALT | TB_MOD_SHIFT },
1519 {"\x1b[18^", TB_KEY_F7, TB_MOD_CTRL },
1520 {"\x1b\x1b[18^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT },
1521 {"\x1b\x1b[31^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1522 {"\x1b[31^", TB_KEY_F7, TB_MOD_CTRL | TB_MOD_SHIFT },
1523 {"\x1b[31~", TB_KEY_F7, TB_MOD_SHIFT },
1524  
1525 {"\x1b\x1b[19~", TB_KEY_F8, TB_MOD_ALT },
1526 {"\x1b\x1b[32~", TB_KEY_F8, TB_MOD_ALT | TB_MOD_SHIFT },
1527 {"\x1b[19^", TB_KEY_F8, TB_MOD_CTRL },
1528 {"\x1b\x1b[19^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT },
1529 {"\x1b\x1b[32^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1530 {"\x1b[32^", TB_KEY_F8, TB_MOD_CTRL | TB_MOD_SHIFT },
1531 {"\x1b[32~", TB_KEY_F8, TB_MOD_SHIFT },
1532  
1533 {"\x1b\x1b[20~", TB_KEY_F9, TB_MOD_ALT },
1534 {"\x1b\x1b[33~", TB_KEY_F9, TB_MOD_ALT | TB_MOD_SHIFT },
1535 {"\x1b[20^", TB_KEY_F9, TB_MOD_CTRL },
1536 {"\x1b\x1b[20^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT },
1537 {"\x1b\x1b[33^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1538 {"\x1b[33^", TB_KEY_F9, TB_MOD_CTRL | TB_MOD_SHIFT },
1539 {"\x1b[33~", TB_KEY_F9, TB_MOD_SHIFT },
1540  
1541 {"\x1b\x1b[21~", TB_KEY_F10, TB_MOD_ALT },
1542 {"\x1b\x1b[34~", TB_KEY_F10, TB_MOD_ALT | TB_MOD_SHIFT },
1543 {"\x1b[21^", TB_KEY_F10, TB_MOD_CTRL },
1544 {"\x1b\x1b[21^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT },
1545 {"\x1b\x1b[34^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1546 {"\x1b[34^", TB_KEY_F10, TB_MOD_CTRL | TB_MOD_SHIFT },
1547 {"\x1b[34~", TB_KEY_F10, TB_MOD_SHIFT },
1548  
1549 {"\x1b\x1b[23~", TB_KEY_F11, TB_MOD_ALT },
1550 {"\x1b\x1b[23$", TB_KEY_F11, TB_MOD_ALT | TB_MOD_SHIFT },
1551 {"\x1b[23^", TB_KEY_F11, TB_MOD_CTRL },
1552 {"\x1b\x1b[23^", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT },
1553 {"\x1b\x1b[23@", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1554 {"\x1b[23@", TB_KEY_F11, TB_MOD_CTRL | TB_MOD_SHIFT },
1555 {"\x1b[23$", TB_KEY_F11, TB_MOD_SHIFT },
1556  
1557 {"\x1b\x1b[24~", TB_KEY_F12, TB_MOD_ALT },
1558 {"\x1b\x1b[24$", TB_KEY_F12, TB_MOD_ALT | TB_MOD_SHIFT },
1559 {"\x1b[24^", TB_KEY_F12, TB_MOD_CTRL },
1560 {"\x1b\x1b[24^", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT },
1561 {"\x1b\x1b[24@", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_ALT | TB_MOD_SHIFT},
1562 {"\x1b[24@", TB_KEY_F12, TB_MOD_CTRL | TB_MOD_SHIFT },
1563 {"\x1b[24$", TB_KEY_F12, TB_MOD_SHIFT },
1564  
1565 // linux console/putty arrows
1566 {"\x1b[A", TB_KEY_ARROW_UP, TB_MOD_SHIFT },
1567 {"\x1b[B", TB_KEY_ARROW_DOWN, TB_MOD_SHIFT },
1568 {"\x1b[C", TB_KEY_ARROW_RIGHT, TB_MOD_SHIFT },
1569 {"\x1b[D", TB_KEY_ARROW_LEFT, TB_MOD_SHIFT },
1570  
1571 // more putty arrows
1572 {"\x1bOA", TB_KEY_ARROW_UP, TB_MOD_CTRL },
1573 {"\x1b\x1bOA", TB_KEY_ARROW_UP, TB_MOD_CTRL | TB_MOD_ALT },
1574 {"\x1bOB", TB_KEY_ARROW_DOWN, TB_MOD_CTRL },
1575 {"\x1b\x1bOB", TB_KEY_ARROW_DOWN, TB_MOD_CTRL | TB_MOD_ALT },
1576 {"\x1bOC", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL },
1577 {"\x1b\x1bOC", TB_KEY_ARROW_RIGHT, TB_MOD_CTRL | TB_MOD_ALT },
1578 {"\x1bOD", TB_KEY_ARROW_LEFT, TB_MOD_CTRL },
1579 {"\x1b\x1bOD", TB_KEY_ARROW_LEFT, TB_MOD_CTRL | TB_MOD_ALT },
1580  
1581 {NULL, 0, 0 },
1582 };
1583  
1584 static const unsigned char utf8_length[256] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1585 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1586 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1587 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1588 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1589 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1590 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1591 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1592 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
1593 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
1594 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1};
1595  
1596 static const unsigned char utf8_mask[6] = {0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01};
1597  
1598 #ifndef TB_OPT_LIBC_WCHAR
1599 static struct {
1600 uint32_t range_start;
1601 uint32_t range_end;
1602 int width; // -1 means iswprint==0, otherwise wcwidth value (0, 1, or 2)
1603 } wcwidth_table[] = {
1604 // clang-format off
1605 {0x000001, 0x00001f, -1}, {0x000020, 0x00007e, 1}, {0x00007f, 0x00009f, -1},
1606 {0x0000a0, 0x0002ff, 1}, {0x000300, 0x00036f, 0}, {0x000370, 0x000377, 1},
1607 {0x000378, 0x000379, -1}, {0x00037a, 0x00037f, 1}, {0x000380, 0x000383, -1},
1608 {0x000384, 0x00038a, 1}, {0x00038b, 0x00038b, -1}, {0x00038c, 0x00038c, 1},
1609 {0x00038d, 0x00038d, -1}, {0x00038e, 0x0003a1, 1}, {0x0003a2, 0x0003a2, -1},
1610 {0x0003a3, 0x000482, 1}, {0x000483, 0x000489, 0}, {0x00048a, 0x00052f, 1},
1611 {0x000530, 0x000530, -1}, {0x000531, 0x000556, 1}, {0x000557, 0x000558, -1},
1612 {0x000559, 0x00058a, 1}, {0x00058b, 0x00058c, -1}, {0x00058d, 0x00058f, 1},
1613 {0x000590, 0x000590, -1}, {0x000591, 0x0005bd, 0}, {0x0005be, 0x0005be, 1},
1614 {0x0005bf, 0x0005bf, 0}, {0x0005c0, 0x0005c0, 1}, {0x0005c1, 0x0005c2, 0},
1615 {0x0005c3, 0x0005c3, 1}, {0x0005c4, 0x0005c5, 0}, {0x0005c6, 0x0005c6, 1},
1616 {0x0005c7, 0x0005c7, 0}, {0x0005c8, 0x0005cf, -1}, {0x0005d0, 0x0005ea, 1},
1617 {0x0005eb, 0x0005ee, -1}, {0x0005ef, 0x0005f4, 1}, {0x0005f5, 0x0005ff, -1},
1618 {0x000600, 0x00060f, 1}, {0x000610, 0x00061a, 0}, {0x00061b, 0x00061b, 1},
1619 {0x00061c, 0x00061c, 0}, {0x00061d, 0x00064a, 1}, {0x00064b, 0x00065f, 0},
1620 {0x000660, 0x00066f, 1}, {0x000670, 0x000670, 0}, {0x000671, 0x0006d5, 1},
1621 {0x0006d6, 0x0006dc, 0}, {0x0006dd, 0x0006de, 1}, {0x0006df, 0x0006e4, 0},
1622 {0x0006e5, 0x0006e6, 1}, {0x0006e7, 0x0006e8, 0}, {0x0006e9, 0x0006e9, 1},
1623 {0x0006ea, 0x0006ed, 0}, {0x0006ee, 0x00070d, 1}, {0x00070e, 0x00070e, -1},
1624 {0x00070f, 0x000710, 1}, {0x000711, 0x000711, 0}, {0x000712, 0x00072f, 1},
1625 {0x000730, 0x00074a, 0}, {0x00074b, 0x00074c, -1}, {0x00074d, 0x0007a5, 1},
1626 {0x0007a6, 0x0007b0, 0}, {0x0007b1, 0x0007b1, 1}, {0x0007b2, 0x0007bf, -1},
1627 {0x0007c0, 0x0007ea, 1}, {0x0007eb, 0x0007f3, 0}, {0x0007f4, 0x0007fa, 1},
1628 {0x0007fb, 0x0007fc, -1}, {0x0007fd, 0x0007fd, 0}, {0x0007fe, 0x000815, 1},
1629 {0x000816, 0x000819, 0}, {0x00081a, 0x00081a, 1}, {0x00081b, 0x000823, 0},
1630 {0x000824, 0x000824, 1}, {0x000825, 0x000827, 0}, {0x000828, 0x000828, 1},
1631 {0x000829, 0x00082d, 0}, {0x00082e, 0x00082f, -1}, {0x000830, 0x00083e, 1},
1632 {0x00083f, 0x00083f, -1}, {0x000840, 0x000858, 1}, {0x000859, 0x00085b, 0},
1633 {0x00085c, 0x00085d, -1}, {0x00085e, 0x00085e, 1}, {0x00085f, 0x00085f, -1},
1634 {0x000860, 0x00086a, 1}, {0x00086b, 0x00086f, -1}, {0x000870, 0x00088e, 1},
1635 {0x00088f, 0x00088f, -1}, {0x000890, 0x000891, 1}, {0x000892, 0x000896, -1},
1636 {0x000897, 0x00089f, 0}, {0x0008a0, 0x0008c9, 1}, {0x0008ca, 0x0008e1, 0},
1637 {0x0008e2, 0x0008e2, 1}, {0x0008e3, 0x000902, 0}, {0x000903, 0x000939, 1},
1638 {0x00093a, 0x00093a, 0}, {0x00093b, 0x00093b, 1}, {0x00093c, 0x00093c, 0},
1639 {0x00093d, 0x000940, 1}, {0x000941, 0x000948, 0}, {0x000949, 0x00094c, 1},
1640 {0x00094d, 0x00094d, 0}, {0x00094e, 0x000950, 1}, {0x000951, 0x000957, 0},
1641 {0x000958, 0x000961, 1}, {0x000962, 0x000963, 0}, {0x000964, 0x000980, 1},
1642 {0x000981, 0x000981, 0}, {0x000982, 0x000983, 1}, {0x000984, 0x000984, -1},
1643 {0x000985, 0x00098c, 1}, {0x00098d, 0x00098e, -1}, {0x00098f, 0x000990, 1},
1644 {0x000991, 0x000992, -1}, {0x000993, 0x0009a8, 1}, {0x0009a9, 0x0009a9, -1},
1645 {0x0009aa, 0x0009b0, 1}, {0x0009b1, 0x0009b1, -1}, {0x0009b2, 0x0009b2, 1},
1646 {0x0009b3, 0x0009b5, -1}, {0x0009b6, 0x0009b9, 1}, {0x0009ba, 0x0009bb, -1},
1647 {0x0009bc, 0x0009bc, 0}, {0x0009bd, 0x0009c0, 1}, {0x0009c1, 0x0009c4, 0},
1648 {0x0009c5, 0x0009c6, -1}, {0x0009c7, 0x0009c8, 1}, {0x0009c9, 0x0009ca, -1},
1649 {0x0009cb, 0x0009cc, 1}, {0x0009cd, 0x0009cd, 0}, {0x0009ce, 0x0009ce, 1},
1650 {0x0009cf, 0x0009d6, -1}, {0x0009d7, 0x0009d7, 1}, {0x0009d8, 0x0009db, -1},
1651 {0x0009dc, 0x0009dd, 1}, {0x0009de, 0x0009de, -1}, {0x0009df, 0x0009e1, 1},
1652 {0x0009e2, 0x0009e3, 0}, {0x0009e4, 0x0009e5, -1}, {0x0009e6, 0x0009fd, 1},
1653 {0x0009fe, 0x0009fe, 0}, {0x0009ff, 0x000a00, -1}, {0x000a01, 0x000a02, 0},
1654 {0x000a03, 0x000a03, 1}, {0x000a04, 0x000a04, -1}, {0x000a05, 0x000a0a, 1},
1655 {0x000a0b, 0x000a0e, -1}, {0x000a0f, 0x000a10, 1}, {0x000a11, 0x000a12, -1},
1656 {0x000a13, 0x000a28, 1}, {0x000a29, 0x000a29, -1}, {0x000a2a, 0x000a30, 1},
1657 {0x000a31, 0x000a31, -1}, {0x000a32, 0x000a33, 1}, {0x000a34, 0x000a34, -1},
1658 {0x000a35, 0x000a36, 1}, {0x000a37, 0x000a37, -1}, {0x000a38, 0x000a39, 1},
1659 {0x000a3a, 0x000a3b, -1}, {0x000a3c, 0x000a3c, 0}, {0x000a3d, 0x000a3d, -1},
1660 {0x000a3e, 0x000a40, 1}, {0x000a41, 0x000a42, 0}, {0x000a43, 0x000a46, -1},
1661 {0x000a47, 0x000a48, 0}, {0x000a49, 0x000a4a, -1}, {0x000a4b, 0x000a4d, 0},
1662 {0x000a4e, 0x000a50, -1}, {0x000a51, 0x000a51, 0}, {0x000a52, 0x000a58, -1},
1663 {0x000a59, 0x000a5c, 1}, {0x000a5d, 0x000a5d, -1}, {0x000a5e, 0x000a5e, 1},
1664 {0x000a5f, 0x000a65, -1}, {0x000a66, 0x000a6f, 1}, {0x000a70, 0x000a71, 0},
1665 {0x000a72, 0x000a74, 1}, {0x000a75, 0x000a75, 0}, {0x000a76, 0x000a76, 1},
1666 {0x000a77, 0x000a80, -1}, {0x000a81, 0x000a82, 0}, {0x000a83, 0x000a83, 1},
1667 {0x000a84, 0x000a84, -1}, {0x000a85, 0x000a8d, 1}, {0x000a8e, 0x000a8e, -1},
1668 {0x000a8f, 0x000a91, 1}, {0x000a92, 0x000a92, -1}, {0x000a93, 0x000aa8, 1},
1669 {0x000aa9, 0x000aa9, -1}, {0x000aaa, 0x000ab0, 1}, {0x000ab1, 0x000ab1, -1},
1670 {0x000ab2, 0x000ab3, 1}, {0x000ab4, 0x000ab4, -1}, {0x000ab5, 0x000ab9, 1},
1671 {0x000aba, 0x000abb, -1}, {0x000abc, 0x000abc, 0}, {0x000abd, 0x000ac0, 1},
1672 {0x000ac1, 0x000ac5, 0}, {0x000ac6, 0x000ac6, -1}, {0x000ac7, 0x000ac8, 0},
1673 {0x000ac9, 0x000ac9, 1}, {0x000aca, 0x000aca, -1}, {0x000acb, 0x000acc, 1},
1674 {0x000acd, 0x000acd, 0}, {0x000ace, 0x000acf, -1}, {0x000ad0, 0x000ad0, 1},
1675 {0x000ad1, 0x000adf, -1}, {0x000ae0, 0x000ae1, 1}, {0x000ae2, 0x000ae3, 0},
1676 {0x000ae4, 0x000ae5, -1}, {0x000ae6, 0x000af1, 1}, {0x000af2, 0x000af8, -1},
1677 {0x000af9, 0x000af9, 1}, {0x000afa, 0x000aff, 0}, {0x000b00, 0x000b00, -1},
1678 {0x000b01, 0x000b01, 0}, {0x000b02, 0x000b03, 1}, {0x000b04, 0x000b04, -1},
1679 {0x000b05, 0x000b0c, 1}, {0x000b0d, 0x000b0e, -1}, {0x000b0f, 0x000b10, 1},
1680 {0x000b11, 0x000b12, -1}, {0x000b13, 0x000b28, 1}, {0x000b29, 0x000b29, -1},
1681 {0x000b2a, 0x000b30, 1}, {0x000b31, 0x000b31, -1}, {0x000b32, 0x000b33, 1},
1682 {0x000b34, 0x000b34, -1}, {0x000b35, 0x000b39, 1}, {0x000b3a, 0x000b3b, -1},
1683 {0x000b3c, 0x000b3c, 0}, {0x000b3d, 0x000b3e, 1}, {0x000b3f, 0x000b3f, 0},
1684 {0x000b40, 0x000b40, 1}, {0x000b41, 0x000b44, 0}, {0x000b45, 0x000b46, -1},
1685 {0x000b47, 0x000b48, 1}, {0x000b49, 0x000b4a, -1}, {0x000b4b, 0x000b4c, 1},
1686 {0x000b4d, 0x000b4d, 0}, {0x000b4e, 0x000b54, -1}, {0x000b55, 0x000b56, 0},
1687 {0x000b57, 0x000b57, 1}, {0x000b58, 0x000b5b, -1}, {0x000b5c, 0x000b5d, 1},
1688 {0x000b5e, 0x000b5e, -1}, {0x000b5f, 0x000b61, 1}, {0x000b62, 0x000b63, 0},
1689 {0x000b64, 0x000b65, -1}, {0x000b66, 0x000b77, 1}, {0x000b78, 0x000b81, -1},
1690 {0x000b82, 0x000b82, 0}, {0x000b83, 0x000b83, 1}, {0x000b84, 0x000b84, -1},
1691 {0x000b85, 0x000b8a, 1}, {0x000b8b, 0x000b8d, -1}, {0x000b8e, 0x000b90, 1},
1692 {0x000b91, 0x000b91, -1}, {0x000b92, 0x000b95, 1}, {0x000b96, 0x000b98, -1},
1693 {0x000b99, 0x000b9a, 1}, {0x000b9b, 0x000b9b, -1}, {0x000b9c, 0x000b9c, 1},
1694 {0x000b9d, 0x000b9d, -1}, {0x000b9e, 0x000b9f, 1}, {0x000ba0, 0x000ba2, -1},
1695 {0x000ba3, 0x000ba4, 1}, {0x000ba5, 0x000ba7, -1}, {0x000ba8, 0x000baa, 1},
1696 {0x000bab, 0x000bad, -1}, {0x000bae, 0x000bb9, 1}, {0x000bba, 0x000bbd, -1},
1697 {0x000bbe, 0x000bbf, 1}, {0x000bc0, 0x000bc0, 0}, {0x000bc1, 0x000bc2, 1},
1698 {0x000bc3, 0x000bc5, -1}, {0x000bc6, 0x000bc8, 1}, {0x000bc9, 0x000bc9, -1},
1699 {0x000bca, 0x000bcc, 1}, {0x000bcd, 0x000bcd, 0}, {0x000bce, 0x000bcf, -1},
1700 {0x000bd0, 0x000bd0, 1}, {0x000bd1, 0x000bd6, -1}, {0x000bd7, 0x000bd7, 1},
1701 {0x000bd8, 0x000be5, -1}, {0x000be6, 0x000bfa, 1}, {0x000bfb, 0x000bff, -1},
1702 {0x000c00, 0x000c00, 0}, {0x000c01, 0x000c03, 1}, {0x000c04, 0x000c04, 0},
1703 {0x000c05, 0x000c0c, 1}, {0x000c0d, 0x000c0d, -1}, {0x000c0e, 0x000c10, 1},
1704 {0x000c11, 0x000c11, -1}, {0x000c12, 0x000c28, 1}, {0x000c29, 0x000c29, -1},
1705 {0x000c2a, 0x000c39, 1}, {0x000c3a, 0x000c3b, -1}, {0x000c3c, 0x000c3c, 0},
1706 {0x000c3d, 0x000c3d, 1}, {0x000c3e, 0x000c40, 0}, {0x000c41, 0x000c44, 1},
1707 {0x000c45, 0x000c45, -1}, {0x000c46, 0x000c48, 0}, {0x000c49, 0x000c49, -1},
1708 {0x000c4a, 0x000c4d, 0}, {0x000c4e, 0x000c54, -1}, {0x000c55, 0x000c56, 0},
1709 {0x000c57, 0x000c57, -1}, {0x000c58, 0x000c5a, 1}, {0x000c5b, 0x000c5c, -1},
1710 {0x000c5d, 0x000c5d, 1}, {0x000c5e, 0x000c5f, -1}, {0x000c60, 0x000c61, 1},
1711 {0x000c62, 0x000c63, 0}, {0x000c64, 0x000c65, -1}, {0x000c66, 0x000c6f, 1},
1712 {0x000c70, 0x000c76, -1}, {0x000c77, 0x000c80, 1}, {0x000c81, 0x000c81, 0},
1713 {0x000c82, 0x000c8c, 1}, {0x000c8d, 0x000c8d, -1}, {0x000c8e, 0x000c90, 1},
1714 {0x000c91, 0x000c91, -1}, {0x000c92, 0x000ca8, 1}, {0x000ca9, 0x000ca9, -1},
1715 {0x000caa, 0x000cb3, 1}, {0x000cb4, 0x000cb4, -1}, {0x000cb5, 0x000cb9, 1},
1716 {0x000cba, 0x000cbb, -1}, {0x000cbc, 0x000cbc, 0}, {0x000cbd, 0x000cbe, 1},
1717 {0x000cbf, 0x000cbf, 0}, {0x000cc0, 0x000cc4, 1}, {0x000cc5, 0x000cc5, -1},
1718 {0x000cc6, 0x000cc6, 0}, {0x000cc7, 0x000cc8, 1}, {0x000cc9, 0x000cc9, -1},
1719 {0x000cca, 0x000ccb, 1}, {0x000ccc, 0x000ccd, 0}, {0x000cce, 0x000cd4, -1},
1720 {0x000cd5, 0x000cd6, 1}, {0x000cd7, 0x000cdc, -1}, {0x000cdd, 0x000cde, 1},
1721 {0x000cdf, 0x000cdf, -1}, {0x000ce0, 0x000ce1, 1}, {0x000ce2, 0x000ce3, 0},
1722 {0x000ce4, 0x000ce5, -1}, {0x000ce6, 0x000cef, 1}, {0x000cf0, 0x000cf0, -1},
1723 {0x000cf1, 0x000cf3, 1}, {0x000cf4, 0x000cff, -1}, {0x000d00, 0x000d01, 0},
1724 {0x000d02, 0x000d0c, 1}, {0x000d0d, 0x000d0d, -1}, {0x000d0e, 0x000d10, 1},
1725 {0x000d11, 0x000d11, -1}, {0x000d12, 0x000d3a, 1}, {0x000d3b, 0x000d3c, 0},
1726 {0x000d3d, 0x000d40, 1}, {0x000d41, 0x000d44, 0}, {0x000d45, 0x000d45, -1},
1727 {0x000d46, 0x000d48, 1}, {0x000d49, 0x000d49, -1}, {0x000d4a, 0x000d4c, 1},
1728 {0x000d4d, 0x000d4d, 0}, {0x000d4e, 0x000d4f, 1}, {0x000d50, 0x000d53, -1},
1729 {0x000d54, 0x000d61, 1}, {0x000d62, 0x000d63, 0}, {0x000d64, 0x000d65, -1},
1730 {0x000d66, 0x000d7f, 1}, {0x000d80, 0x000d80, -1}, {0x000d81, 0x000d81, 0},
1731 {0x000d82, 0x000d83, 1}, {0x000d84, 0x000d84, -1}, {0x000d85, 0x000d96, 1},
1732 {0x000d97, 0x000d99, -1}, {0x000d9a, 0x000db1, 1}, {0x000db2, 0x000db2, -1},
1733 {0x000db3, 0x000dbb, 1}, {0x000dbc, 0x000dbc, -1}, {0x000dbd, 0x000dbd, 1},
1734 {0x000dbe, 0x000dbf, -1}, {0x000dc0, 0x000dc6, 1}, {0x000dc7, 0x000dc9, -1},
1735 {0x000dca, 0x000dca, 0}, {0x000dcb, 0x000dce, -1}, {0x000dcf, 0x000dd1, 1},
1736 {0x000dd2, 0x000dd4, 0}, {0x000dd5, 0x000dd5, -1}, {0x000dd6, 0x000dd6, 0},
1737 {0x000dd7, 0x000dd7, -1}, {0x000dd8, 0x000ddf, 1}, {0x000de0, 0x000de5, -1},
1738 {0x000de6, 0x000def, 1}, {0x000df0, 0x000df1, -1}, {0x000df2, 0x000df4, 1},
1739 {0x000df5, 0x000e00, -1}, {0x000e01, 0x000e30, 1}, {0x000e31, 0x000e31, 0},
1740 {0x000e32, 0x000e33, 1}, {0x000e34, 0x000e3a, 0}, {0x000e3b, 0x000e3e, -1},
1741 {0x000e3f, 0x000e46, 1}, {0x000e47, 0x000e4e, 0}, {0x000e4f, 0x000e5b, 1},
1742 {0x000e5c, 0x000e80, -1}, {0x000e81, 0x000e82, 1}, {0x000e83, 0x000e83, -1},
1743 {0x000e84, 0x000e84, 1}, {0x000e85, 0x000e85, -1}, {0x000e86, 0x000e8a, 1},
1744 {0x000e8b, 0x000e8b, -1}, {0x000e8c, 0x000ea3, 1}, {0x000ea4, 0x000ea4, -1},
1745 {0x000ea5, 0x000ea5, 1}, {0x000ea6, 0x000ea6, -1}, {0x000ea7, 0x000eb0, 1},
1746 {0x000eb1, 0x000eb1, 0}, {0x000eb2, 0x000eb3, 1}, {0x000eb4, 0x000ebc, 0},
1747 {0x000ebd, 0x000ebd, 1}, {0x000ebe, 0x000ebf, -1}, {0x000ec0, 0x000ec4, 1},
1748 {0x000ec5, 0x000ec5, -1}, {0x000ec6, 0x000ec6, 1}, {0x000ec7, 0x000ec7, -1},
1749 {0x000ec8, 0x000ece, 0}, {0x000ecf, 0x000ecf, -1}, {0x000ed0, 0x000ed9, 1},
1750 {0x000eda, 0x000edb, -1}, {0x000edc, 0x000edf, 1}, {0x000ee0, 0x000eff, -1},
1751 {0x000f00, 0x000f17, 1}, {0x000f18, 0x000f19, 0}, {0x000f1a, 0x000f34, 1},
1752 {0x000f35, 0x000f35, 0}, {0x000f36, 0x000f36, 1}, {0x000f37, 0x000f37, 0},
1753 {0x000f38, 0x000f38, 1}, {0x000f39, 0x000f39, 0}, {0x000f3a, 0x000f47, 1},
1754 {0x000f48, 0x000f48, -1}, {0x000f49, 0x000f6c, 1}, {0x000f6d, 0x000f70, -1},
1755 {0x000f71, 0x000f7e, 0}, {0x000f7f, 0x000f7f, 1}, {0x000f80, 0x000f84, 0},
1756 {0x000f85, 0x000f85, 1}, {0x000f86, 0x000f87, 0}, {0x000f88, 0x000f8c, 1},
1757 {0x000f8d, 0x000f97, 0}, {0x000f98, 0x000f98, -1}, {0x000f99, 0x000fbc, 0},
1758 {0x000fbd, 0x000fbd, -1}, {0x000fbe, 0x000fc5, 1}, {0x000fc6, 0x000fc6, 0},
1759 {0x000fc7, 0x000fcc, 1}, {0x000fcd, 0x000fcd, -1}, {0x000fce, 0x000fda, 1},
1760 {0x000fdb, 0x000fff, -1}, {0x001000, 0x00102c, 1}, {0x00102d, 0x001030, 0},
1761 {0x001031, 0x001031, 1}, {0x001032, 0x001037, 0}, {0x001038, 0x001038, 1},
1762 {0x001039, 0x00103a, 0}, {0x00103b, 0x00103c, 1}, {0x00103d, 0x00103e, 0},
1763 {0x00103f, 0x001057, 1}, {0x001058, 0x001059, 0}, {0x00105a, 0x00105d, 1},
1764 {0x00105e, 0x001060, 0}, {0x001061, 0x001070, 1}, {0x001071, 0x001074, 0},
1765 {0x001075, 0x001081, 1}, {0x001082, 0x001082, 0}, {0x001083, 0x001084, 1},
1766 {0x001085, 0x001086, 0}, {0x001087, 0x00108c, 1}, {0x00108d, 0x00108d, 0},
1767 {0x00108e, 0x00109c, 1}, {0x00109d, 0x00109d, 0}, {0x00109e, 0x0010c5, 1},
1768 {0x0010c6, 0x0010c6, -1}, {0x0010c7, 0x0010c7, 1}, {0x0010c8, 0x0010cc, -1},
1769 {0x0010cd, 0x0010cd, 1}, {0x0010ce, 0x0010cf, -1}, {0x0010d0, 0x0010ff, 1},
1770 {0x001100, 0x00115f, 2}, {0x001160, 0x0011ff, 0}, {0x001200, 0x001248, 1},
1771 {0x001249, 0x001249, -1}, {0x00124a, 0x00124d, 1}, {0x00124e, 0x00124f, -1},
1772 {0x001250, 0x001256, 1}, {0x001257, 0x001257, -1}, {0x001258, 0x001258, 1},
1773 {0x001259, 0x001259, -1}, {0x00125a, 0x00125d, 1}, {0x00125e, 0x00125f, -1},
1774 {0x001260, 0x001288, 1}, {0x001289, 0x001289, -1}, {0x00128a, 0x00128d, 1},
1775 {0x00128e, 0x00128f, -1}, {0x001290, 0x0012b0, 1}, {0x0012b1, 0x0012b1, -1},
1776 {0x0012b2, 0x0012b5, 1}, {0x0012b6, 0x0012b7, -1}, {0x0012b8, 0x0012be, 1},
1777 {0x0012bf, 0x0012bf, -1}, {0x0012c0, 0x0012c0, 1}, {0x0012c1, 0x0012c1, -1},
1778 {0x0012c2, 0x0012c5, 1}, {0x0012c6, 0x0012c7, -1}, {0x0012c8, 0x0012d6, 1},
1779 {0x0012d7, 0x0012d7, -1}, {0x0012d8, 0x001310, 1}, {0x001311, 0x001311, -1},
1780 {0x001312, 0x001315, 1}, {0x001316, 0x001317, -1}, {0x001318, 0x00135a, 1},
1781 {0x00135b, 0x00135c, -1}, {0x00135d, 0x00135f, 0}, {0x001360, 0x00137c, 1},
1782 {0x00137d, 0x00137f, -1}, {0x001380, 0x001399, 1}, {0x00139a, 0x00139f, -1},
1783 {0x0013a0, 0x0013f5, 1}, {0x0013f6, 0x0013f7, -1}, {0x0013f8, 0x0013fd, 1},
1784 {0x0013fe, 0x0013ff, -1}, {0x001400, 0x00169c, 1}, {0x00169d, 0x00169f, -1},
1785 {0x0016a0, 0x0016f8, 1}, {0x0016f9, 0x0016ff, -1}, {0x001700, 0x001711, 1},
1786 {0x001712, 0x001714, 0}, {0x001715, 0x001715, 1}, {0x001716, 0x00171e, -1},
1787 {0x00171f, 0x001731, 1}, {0x001732, 0x001733, 0}, {0x001734, 0x001736, 1},
1788 {0x001737, 0x00173f, -1}, {0x001740, 0x001751, 1}, {0x001752, 0x001753, 0},
1789 {0x001754, 0x00175f, -1}, {0x001760, 0x00176c, 1}, {0x00176d, 0x00176d, -1},
1790 {0x00176e, 0x001770, 1}, {0x001771, 0x001771, -1}, {0x001772, 0x001773, 0},
1791 {0x001774, 0x00177f, -1}, {0x001780, 0x0017b3, 1}, {0x0017b4, 0x0017b5, 0},
1792 {0x0017b6, 0x0017b6, 1}, {0x0017b7, 0x0017bd, 0}, {0x0017be, 0x0017c5, 1},
1793 {0x0017c6, 0x0017c6, 0}, {0x0017c7, 0x0017c8, 1}, {0x0017c9, 0x0017d3, 0},
1794 {0x0017d4, 0x0017dc, 1}, {0x0017dd, 0x0017dd, 0}, {0x0017de, 0x0017df, -1},
1795 {0x0017e0, 0x0017e9, 1}, {0x0017ea, 0x0017ef, -1}, {0x0017f0, 0x0017f9, 1},
1796 {0x0017fa, 0x0017ff, -1}, {0x001800, 0x00180a, 1}, {0x00180b, 0x00180f, 0},
1797 {0x001810, 0x001819, 1}, {0x00181a, 0x00181f, -1}, {0x001820, 0x001878, 1},
1798 {0x001879, 0x00187f, -1}, {0x001880, 0x001884, 1}, {0x001885, 0x001886, 0},
1799 {0x001887, 0x0018a8, 1}, {0x0018a9, 0x0018a9, 0}, {0x0018aa, 0x0018aa, 1},
1800 {0x0018ab, 0x0018af, -1}, {0x0018b0, 0x0018f5, 1}, {0x0018f6, 0x0018ff, -1},
1801 {0x001900, 0x00191e, 1}, {0x00191f, 0x00191f, -1}, {0x001920, 0x001922, 0},
1802 {0x001923, 0x001926, 1}, {0x001927, 0x001928, 0}, {0x001929, 0x00192b, 1},
1803 {0x00192c, 0x00192f, -1}, {0x001930, 0x001931, 1}, {0x001932, 0x001932, 0},
1804 {0x001933, 0x001938, 1}, {0x001939, 0x00193b, 0}, {0x00193c, 0x00193f, -1},
1805 {0x001940, 0x001940, 1}, {0x001941, 0x001943, -1}, {0x001944, 0x00196d, 1},
1806 {0x00196e, 0x00196f, -1}, {0x001970, 0x001974, 1}, {0x001975, 0x00197f, -1},
1807 {0x001980, 0x0019ab, 1}, {0x0019ac, 0x0019af, -1}, {0x0019b0, 0x0019c9, 1},
1808 {0x0019ca, 0x0019cf, -1}, {0x0019d0, 0x0019da, 1}, {0x0019db, 0x0019dd, -1},
1809 {0x0019de, 0x001a16, 1}, {0x001a17, 0x001a18, 0}, {0x001a19, 0x001a1a, 1},
1810 {0x001a1b, 0x001a1b, 0}, {0x001a1c, 0x001a1d, -1}, {0x001a1e, 0x001a55, 1},
1811 {0x001a56, 0x001a56, 0}, {0x001a57, 0x001a57, 1}, {0x001a58, 0x001a5e, 0},
1812 {0x001a5f, 0x001a5f, -1}, {0x001a60, 0x001a60, 0}, {0x001a61, 0x001a61, 1},
1813 {0x001a62, 0x001a62, 0}, {0x001a63, 0x001a64, 1}, {0x001a65, 0x001a6c, 0},
1814 {0x001a6d, 0x001a72, 1}, {0x001a73, 0x001a7c, 0}, {0x001a7d, 0x001a7e, -1},
1815 {0x001a7f, 0x001a7f, 0}, {0x001a80, 0x001a89, 1}, {0x001a8a, 0x001a8f, -1},
1816 {0x001a90, 0x001a99, 1}, {0x001a9a, 0x001a9f, -1}, {0x001aa0, 0x001aad, 1},
1817 {0x001aae, 0x001aaf, -1}, {0x001ab0, 0x001ace, 0}, {0x001acf, 0x001aff, -1},
1818 {0x001b00, 0x001b03, 0}, {0x001b04, 0x001b33, 1}, {0x001b34, 0x001b34, 0},
1819 {0x001b35, 0x001b35, 1}, {0x001b36, 0x001b3a, 0}, {0x001b3b, 0x001b3b, 1},
1820 {0x001b3c, 0x001b3c, 0}, {0x001b3d, 0x001b41, 1}, {0x001b42, 0x001b42, 0},
1821 {0x001b43, 0x001b4c, 1}, {0x001b4d, 0x001b4d, -1}, {0x001b4e, 0x001b6a, 1},
1822 {0x001b6b, 0x001b73, 0}, {0x001b74, 0x001b7f, 1}, {0x001b80, 0x001b81, 0},
1823 {0x001b82, 0x001ba1, 1}, {0x001ba2, 0x001ba5, 0}, {0x001ba6, 0x001ba7, 1},
1824 {0x001ba8, 0x001ba9, 0}, {0x001baa, 0x001baa, 1}, {0x001bab, 0x001bad, 0},
1825 {0x001bae, 0x001be5, 1}, {0x001be6, 0x001be6, 0}, {0x001be7, 0x001be7, 1},
1826 {0x001be8, 0x001be9, 0}, {0x001bea, 0x001bec, 1}, {0x001bed, 0x001bed, 0},
1827 {0x001bee, 0x001bee, 1}, {0x001bef, 0x001bf1, 0}, {0x001bf2, 0x001bf3, 1},
1828 {0x001bf4, 0x001bfb, -1}, {0x001bfc, 0x001c2b, 1}, {0x001c2c, 0x001c33, 0},
1829 {0x001c34, 0x001c35, 1}, {0x001c36, 0x001c37, 0}, {0x001c38, 0x001c3a, -1},
1830 {0x001c3b, 0x001c49, 1}, {0x001c4a, 0x001c4c, -1}, {0x001c4d, 0x001c8a, 1},
1831 {0x001c8b, 0x001c8f, -1}, {0x001c90, 0x001cba, 1}, {0x001cbb, 0x001cbc, -1},
1832 {0x001cbd, 0x001cc7, 1}, {0x001cc8, 0x001ccf, -1}, {0x001cd0, 0x001cd2, 0},
1833 {0x001cd3, 0x001cd3, 1}, {0x001cd4, 0x001ce0, 0}, {0x001ce1, 0x001ce1, 1},
1834 {0x001ce2, 0x001ce8, 0}, {0x001ce9, 0x001cec, 1}, {0x001ced, 0x001ced, 0},
1835 {0x001cee, 0x001cf3, 1}, {0x001cf4, 0x001cf4, 0}, {0x001cf5, 0x001cf7, 1},
1836 {0x001cf8, 0x001cf9, 0}, {0x001cfa, 0x001cfa, 1}, {0x001cfb, 0x001cff, -1},
1837 {0x001d00, 0x001dbf, 1}, {0x001dc0, 0x001dff, 0}, {0x001e00, 0x001f15, 1},
1838 {0x001f16, 0x001f17, -1}, {0x001f18, 0x001f1d, 1}, {0x001f1e, 0x001f1f, -1},
1839 {0x001f20, 0x001f45, 1}, {0x001f46, 0x001f47, -1}, {0x001f48, 0x001f4d, 1},
1840 {0x001f4e, 0x001f4f, -1}, {0x001f50, 0x001f57, 1}, {0x001f58, 0x001f58, -1},
1841 {0x001f59, 0x001f59, 1}, {0x001f5a, 0x001f5a, -1}, {0x001f5b, 0x001f5b, 1},
1842 {0x001f5c, 0x001f5c, -1}, {0x001f5d, 0x001f5d, 1}, {0x001f5e, 0x001f5e, -1},
1843 {0x001f5f, 0x001f7d, 1}, {0x001f7e, 0x001f7f, -1}, {0x001f80, 0x001fb4, 1},
1844 {0x001fb5, 0x001fb5, -1}, {0x001fb6, 0x001fc4, 1}, {0x001fc5, 0x001fc5, -1},
1845 {0x001fc6, 0x001fd3, 1}, {0x001fd4, 0x001fd5, -1}, {0x001fd6, 0x001fdb, 1},
1846 {0x001fdc, 0x001fdc, -1}, {0x001fdd, 0x001fef, 1}, {0x001ff0, 0x001ff1, -1},
1847 {0x001ff2, 0x001ff4, 1}, {0x001ff5, 0x001ff5, -1}, {0x001ff6, 0x001ffe, 1},
1848 {0x001fff, 0x001fff, -1}, {0x002000, 0x00200a, 1}, {0x00200b, 0x00200f, 0},
1849 {0x002010, 0x002027, 1}, {0x002028, 0x002029, -1}, {0x00202a, 0x00202e, 0},
1850 {0x00202f, 0x00205f, 1}, {0x002060, 0x002064, 0}, {0x002065, 0x002065, -1},
1851 {0x002066, 0x00206f, 0}, {0x002070, 0x002071, 1}, {0x002072, 0x002073, -1},
1852 {0x002074, 0x00208e, 1}, {0x00208f, 0x00208f, -1}, {0x002090, 0x00209c, 1},
1853 {0x00209d, 0x00209f, -1}, {0x0020a0, 0x0020c0, 1}, {0x0020c1, 0x0020cf, -1},
1854 {0x0020d0, 0x0020f0, 0}, {0x0020f1, 0x0020ff, -1}, {0x002100, 0x00218b, 1},
1855 {0x00218c, 0x00218f, -1}, {0x002190, 0x002319, 1}, {0x00231a, 0x00231b, 2},
1856 {0x00231c, 0x002328, 1}, {0x002329, 0x00232a, 2}, {0x00232b, 0x0023e8, 1},
1857 {0x0023e9, 0x0023ec, 2}, {0x0023ed, 0x0023ef, 1}, {0x0023f0, 0x0023f0, 2},
1858 {0x0023f1, 0x0023f2, 1}, {0x0023f3, 0x0023f3, 2}, {0x0023f4, 0x002429, 1},
1859 {0x00242a, 0x00243f, -1}, {0x002440, 0x00244a, 1}, {0x00244b, 0x00245f, -1},
1860 {0x002460, 0x0025fc, 1}, {0x0025fd, 0x0025fe, 2}, {0x0025ff, 0x002613, 1},
1861 {0x002614, 0x002615, 2}, {0x002616, 0x00262f, 1}, {0x002630, 0x002637, 2},
1862 {0x002638, 0x002647, 1}, {0x002648, 0x002653, 2}, {0x002654, 0x00267e, 1},
1863 {0x00267f, 0x00267f, 2}, {0x002680, 0x002689, 1}, {0x00268a, 0x00268f, 2},
1864 {0x002690, 0x002692, 1}, {0x002693, 0x002693, 2}, {0x002694, 0x0026a0, 1},
1865 {0x0026a1, 0x0026a1, 2}, {0x0026a2, 0x0026a9, 1}, {0x0026aa, 0x0026ab, 2},
1866 {0x0026ac, 0x0026bc, 1}, {0x0026bd, 0x0026be, 2}, {0x0026bf, 0x0026c3, 1},
1867 {0x0026c4, 0x0026c5, 2}, {0x0026c6, 0x0026cd, 1}, {0x0026ce, 0x0026ce, 2},
1868 {0x0026cf, 0x0026d3, 1}, {0x0026d4, 0x0026d4, 2}, {0x0026d5, 0x0026e9, 1},
1869 {0x0026ea, 0x0026ea, 2}, {0x0026eb, 0x0026f1, 1}, {0x0026f2, 0x0026f3, 2},
1870 {0x0026f4, 0x0026f4, 1}, {0x0026f5, 0x0026f5, 2}, {0x0026f6, 0x0026f9, 1},
1871 {0x0026fa, 0x0026fa, 2}, {0x0026fb, 0x0026fc, 1}, {0x0026fd, 0x0026fd, 2},
1872 {0x0026fe, 0x002704, 1}, {0x002705, 0x002705, 2}, {0x002706, 0x002709, 1},
1873 {0x00270a, 0x00270b, 2}, {0x00270c, 0x002727, 1}, {0x002728, 0x002728, 2},
1874 {0x002729, 0x00274b, 1}, {0x00274c, 0x00274c, 2}, {0x00274d, 0x00274d, 1},
1875 {0x00274e, 0x00274e, 2}, {0x00274f, 0x002752, 1}, {0x002753, 0x002755, 2},
1876 {0x002756, 0x002756, 1}, {0x002757, 0x002757, 2}, {0x002758, 0x002794, 1},
1877 {0x002795, 0x002797, 2}, {0x002798, 0x0027af, 1}, {0x0027b0, 0x0027b0, 2},
1878 {0x0027b1, 0x0027be, 1}, {0x0027bf, 0x0027bf, 2}, {0x0027c0, 0x002b1a, 1},
1879 {0x002b1b, 0x002b1c, 2}, {0x002b1d, 0x002b4f, 1}, {0x002b50, 0x002b50, 2},
1880 {0x002b51, 0x002b54, 1}, {0x002b55, 0x002b55, 2}, {0x002b56, 0x002b73, 1},
1881 {0x002b74, 0x002b75, -1}, {0x002b76, 0x002b95, 1}, {0x002b96, 0x002b96, -1},
1882 {0x002b97, 0x002cee, 1}, {0x002cef, 0x002cf1, 0}, {0x002cf2, 0x002cf3, 1},
1883 {0x002cf4, 0x002cf8, -1}, {0x002cf9, 0x002d25, 1}, {0x002d26, 0x002d26, -1},
1884 {0x002d27, 0x002d27, 1}, {0x002d28, 0x002d2c, -1}, {0x002d2d, 0x002d2d, 1},
1885 {0x002d2e, 0x002d2f, -1}, {0x002d30, 0x002d67, 1}, {0x002d68, 0x002d6e, -1},
1886 {0x002d6f, 0x002d70, 1}, {0x002d71, 0x002d7e, -1}, {0x002d7f, 0x002d7f, 0},
1887 {0x002d80, 0x002d96, 1}, {0x002d97, 0x002d9f, -1}, {0x002da0, 0x002da6, 1},
1888 {0x002da7, 0x002da7, -1}, {0x002da8, 0x002dae, 1}, {0x002daf, 0x002daf, -1},
1889 {0x002db0, 0x002db6, 1}, {0x002db7, 0x002db7, -1}, {0x002db8, 0x002dbe, 1},
1890 {0x002dbf, 0x002dbf, -1}, {0x002dc0, 0x002dc6, 1}, {0x002dc7, 0x002dc7, -1},
1891 {0x002dc8, 0x002dce, 1}, {0x002dcf, 0x002dcf, -1}, {0x002dd0, 0x002dd6, 1},
1892 {0x002dd7, 0x002dd7, -1}, {0x002dd8, 0x002dde, 1}, {0x002ddf, 0x002ddf, -1},
1893 {0x002de0, 0x002dff, 0}, {0x002e00, 0x002e5d, 1}, {0x002e5e, 0x002e7f, -1},
1894 {0x002e80, 0x002e99, 2}, {0x002e9a, 0x002e9a, -1}, {0x002e9b, 0x002ef3, 2},
1895 {0x002ef4, 0x002eff, -1}, {0x002f00, 0x002fd5, 2}, {0x002fd6, 0x002fef, -1},
1896 {0x002ff0, 0x003029, 2}, {0x00302a, 0x00302d, 0}, {0x00302e, 0x00303e, 2},
1897 {0x00303f, 0x00303f, 1}, {0x003040, 0x003040, -1}, {0x003041, 0x003096, 2},
1898 {0x003097, 0x003098, -1}, {0x003099, 0x00309a, 0}, {0x00309b, 0x0030ff, 2},
1899 {0x003100, 0x003104, -1}, {0x003105, 0x00312f, 2}, {0x003130, 0x003130, -1},
1900 {0x003131, 0x003163, 2}, {0x003164, 0x003164, 0}, {0x003165, 0x00318e, 2},
1901 {0x00318f, 0x00318f, -1}, {0x003190, 0x0031e5, 2}, {0x0031e6, 0x0031ee, -1},
1902 {0x0031ef, 0x00321e, 2}, {0x00321f, 0x00321f, -1}, {0x003220, 0x00a48c, 2},
1903 {0x00a48d, 0x00a48f, -1}, {0x00a490, 0x00a4c6, 2}, {0x00a4c7, 0x00a4cf, -1},
1904 {0x00a4d0, 0x00a62b, 1}, {0x00a62c, 0x00a63f, -1}, {0x00a640, 0x00a66e, 1},
1905 {0x00a66f, 0x00a672, 0}, {0x00a673, 0x00a673, 1}, {0x00a674, 0x00a67d, 0},
1906 {0x00a67e, 0x00a69d, 1}, {0x00a69e, 0x00a69f, 0}, {0x00a6a0, 0x00a6ef, 1},
1907 {0x00a6f0, 0x00a6f1, 0}, {0x00a6f2, 0x00a6f7, 1}, {0x00a6f8, 0x00a6ff, -1},
1908 {0x00a700, 0x00a7cd, 1}, {0x00a7ce, 0x00a7cf, -1}, {0x00a7d0, 0x00a7d1, 1},
1909 {0x00a7d2, 0x00a7d2, -1}, {0x00a7d3, 0x00a7d3, 1}, {0x00a7d4, 0x00a7d4, -1},
1910 {0x00a7d5, 0x00a7dc, 1}, {0x00a7dd, 0x00a7f1, -1}, {0x00a7f2, 0x00a801, 1},
1911 {0x00a802, 0x00a802, 0}, {0x00a803, 0x00a805, 1}, {0x00a806, 0x00a806, 0},
1912 {0x00a807, 0x00a80a, 1}, {0x00a80b, 0x00a80b, 0}, {0x00a80c, 0x00a824, 1},
1913 {0x00a825, 0x00a826, 0}, {0x00a827, 0x00a82b, 1}, {0x00a82c, 0x00a82c, 0},
1914 {0x00a82d, 0x00a82f, -1}, {0x00a830, 0x00a839, 1}, {0x00a83a, 0x00a83f, -1},
1915 {0x00a840, 0x00a877, 1}, {0x00a878, 0x00a87f, -1}, {0x00a880, 0x00a8c3, 1},
1916 {0x00a8c4, 0x00a8c5, 0}, {0x00a8c6, 0x00a8cd, -1}, {0x00a8ce, 0x00a8d9, 1},
1917 {0x00a8da, 0x00a8df, -1}, {0x00a8e0, 0x00a8f1, 0}, {0x00a8f2, 0x00a8fe, 1},
1918 {0x00a8ff, 0x00a8ff, 0}, {0x00a900, 0x00a925, 1}, {0x00a926, 0x00a92d, 0},
1919 {0x00a92e, 0x00a946, 1}, {0x00a947, 0x00a951, 0}, {0x00a952, 0x00a953, 1},
1920 {0x00a954, 0x00a95e, -1}, {0x00a95f, 0x00a95f, 1}, {0x00a960, 0x00a97c, 2},
1921 {0x00a97d, 0x00a97f, -1}, {0x00a980, 0x00a982, 0}, {0x00a983, 0x00a9b2, 1},
1922 {0x00a9b3, 0x00a9b3, 0}, {0x00a9b4, 0x00a9b5, 1}, {0x00a9b6, 0x00a9b9, 0},
1923 {0x00a9ba, 0x00a9bb, 1}, {0x00a9bc, 0x00a9bd, 0}, {0x00a9be, 0x00a9cd, 1},
1924 {0x00a9ce, 0x00a9ce, -1}, {0x00a9cf, 0x00a9d9, 1}, {0x00a9da, 0x00a9dd, -1},
1925 {0x00a9de, 0x00a9e4, 1}, {0x00a9e5, 0x00a9e5, 0}, {0x00a9e6, 0x00a9fe, 1},
1926 {0x00a9ff, 0x00a9ff, -1}, {0x00aa00, 0x00aa28, 1}, {0x00aa29, 0x00aa2e, 0},
1927 {0x00aa2f, 0x00aa30, 1}, {0x00aa31, 0x00aa32, 0}, {0x00aa33, 0x00aa34, 1},
1928 {0x00aa35, 0x00aa36, 0}, {0x00aa37, 0x00aa3f, -1}, {0x00aa40, 0x00aa42, 1},
1929 {0x00aa43, 0x00aa43, 0}, {0x00aa44, 0x00aa4b, 1}, {0x00aa4c, 0x00aa4c, 0},
1930 {0x00aa4d, 0x00aa4d, 1}, {0x00aa4e, 0x00aa4f, -1}, {0x00aa50, 0x00aa59, 1},
1931 {0x00aa5a, 0x00aa5b, -1}, {0x00aa5c, 0x00aa7b, 1}, {0x00aa7c, 0x00aa7c, 0},
1932 {0x00aa7d, 0x00aaaf, 1}, {0x00aab0, 0x00aab0, 0}, {0x00aab1, 0x00aab1, 1},
1933 {0x00aab2, 0x00aab4, 0}, {0x00aab5, 0x00aab6, 1}, {0x00aab7, 0x00aab8, 0},
1934 {0x00aab9, 0x00aabd, 1}, {0x00aabe, 0x00aabf, 0}, {0x00aac0, 0x00aac0, 1},
1935 {0x00aac1, 0x00aac1, 0}, {0x00aac2, 0x00aac2, 1}, {0x00aac3, 0x00aada, -1},
1936 {0x00aadb, 0x00aaeb, 1}, {0x00aaec, 0x00aaed, 0}, {0x00aaee, 0x00aaf5, 1},
1937 {0x00aaf6, 0x00aaf6, 0}, {0x00aaf7, 0x00ab00, -1}, {0x00ab01, 0x00ab06, 1},
1938 {0x00ab07, 0x00ab08, -1}, {0x00ab09, 0x00ab0e, 1}, {0x00ab0f, 0x00ab10, -1},
1939 {0x00ab11, 0x00ab16, 1}, {0x00ab17, 0x00ab1f, -1}, {0x00ab20, 0x00ab26, 1},
1940 {0x00ab27, 0x00ab27, -1}, {0x00ab28, 0x00ab2e, 1}, {0x00ab2f, 0x00ab2f, -1},
1941 {0x00ab30, 0x00ab6b, 1}, {0x00ab6c, 0x00ab6f, -1}, {0x00ab70, 0x00abe4, 1},
1942 {0x00abe5, 0x00abe5, 0}, {0x00abe6, 0x00abe7, 1}, {0x00abe8, 0x00abe8, 0},
1943 {0x00abe9, 0x00abec, 1}, {0x00abed, 0x00abed, 0}, {0x00abee, 0x00abef, -1},
1944 {0x00abf0, 0x00abf9, 1}, {0x00abfa, 0x00abff, -1}, {0x00ac00, 0x00d7a3, 2},
1945 {0x00d7a4, 0x00d7af, -1}, {0x00d7b0, 0x00d7c6, 0}, {0x00d7c7, 0x00d7ca, -1},
1946 {0x00d7cb, 0x00d7fb, 0}, {0x00d7fc, 0x00dfff, -1}, {0x00e000, 0x00f8ff, 1},
1947 {0x00f900, 0x00fa6d, 2}, {0x00fa6e, 0x00fa6f, -1}, {0x00fa70, 0x00fad9, 2},
1948 {0x00fada, 0x00faff, -1}, {0x00fb00, 0x00fb06, 1}, {0x00fb07, 0x00fb12, -1},
1949 {0x00fb13, 0x00fb17, 1}, {0x00fb18, 0x00fb1c, -1}, {0x00fb1d, 0x00fb1d, 1},
1950 {0x00fb1e, 0x00fb1e, 0}, {0x00fb1f, 0x00fb36, 1}, {0x00fb37, 0x00fb37, -1},
1951 {0x00fb38, 0x00fb3c, 1}, {0x00fb3d, 0x00fb3d, -1}, {0x00fb3e, 0x00fb3e, 1},
1952 {0x00fb3f, 0x00fb3f, -1}, {0x00fb40, 0x00fb41, 1}, {0x00fb42, 0x00fb42, -1},
1953 {0x00fb43, 0x00fb44, 1}, {0x00fb45, 0x00fb45, -1}, {0x00fb46, 0x00fbc2, 1},
1954 {0x00fbc3, 0x00fbd2, -1}, {0x00fbd3, 0x00fd8f, 1}, {0x00fd90, 0x00fd91, -1},
1955 {0x00fd92, 0x00fdc7, 1}, {0x00fdc8, 0x00fdce, -1}, {0x00fdcf, 0x00fdcf, 1},
1956 {0x00fdd0, 0x00fdef, -1}, {0x00fdf0, 0x00fdff, 1}, {0x00fe00, 0x00fe0f, 0},
1957 {0x00fe10, 0x00fe19, 2}, {0x00fe1a, 0x00fe1f, -1}, {0x00fe20, 0x00fe2f, 0},
1958 {0x00fe30, 0x00fe52, 2}, {0x00fe53, 0x00fe53, -1}, {0x00fe54, 0x00fe66, 2},
1959 {0x00fe67, 0x00fe67, -1}, {0x00fe68, 0x00fe6b, 2}, {0x00fe6c, 0x00fe6f, -1},
1960 {0x00fe70, 0x00fe74, 1}, {0x00fe75, 0x00fe75, -1}, {0x00fe76, 0x00fefc, 1},
1961 {0x00fefd, 0x00fefe, -1}, {0x00feff, 0x00feff, 0}, {0x00ff00, 0x00ff00, -1},
1962 {0x00ff01, 0x00ff60, 2}, {0x00ff61, 0x00ff9f, 1}, {0x00ffa0, 0x00ffa0, 0},
1963 {0x00ffa1, 0x00ffbe, 1}, {0x00ffbf, 0x00ffc1, -1}, {0x00ffc2, 0x00ffc7, 1},
1964 {0x00ffc8, 0x00ffc9, -1}, {0x00ffca, 0x00ffcf, 1}, {0x00ffd0, 0x00ffd1, -1},
1965 {0x00ffd2, 0x00ffd7, 1}, {0x00ffd8, 0x00ffd9, -1}, {0x00ffda, 0x00ffdc, 1},
1966 {0x00ffdd, 0x00ffdf, -1}, {0x00ffe0, 0x00ffe6, 2}, {0x00ffe7, 0x00ffe7, -1},
1967 {0x00ffe8, 0x00ffee, 1}, {0x00ffef, 0x00fff8, -1}, {0x00fff9, 0x00fffd, 1},
1968 {0x00fffe, 0x00ffff, -1}, {0x010000, 0x01000b, 1}, {0x01000c, 0x01000c, -1},
1969 {0x01000d, 0x010026, 1}, {0x010027, 0x010027, -1}, {0x010028, 0x01003a, 1},
1970 {0x01003b, 0x01003b, -1}, {0x01003c, 0x01003d, 1}, {0x01003e, 0x01003e, -1},
1971 {0x01003f, 0x01004d, 1}, {0x01004e, 0x01004f, -1}, {0x010050, 0x01005d, 1},
1972 {0x01005e, 0x01007f, -1}, {0x010080, 0x0100fa, 1}, {0x0100fb, 0x0100ff, -1},
1973 {0x010100, 0x010102, 1}, {0x010103, 0x010106, -1}, {0x010107, 0x010133, 1},
1974 {0x010134, 0x010136, -1}, {0x010137, 0x01018e, 1}, {0x01018f, 0x01018f, -1},
1975 {0x010190, 0x01019c, 1}, {0x01019d, 0x01019f, -1}, {0x0101a0, 0x0101a0, 1},
1976 {0x0101a1, 0x0101cf, -1}, {0x0101d0, 0x0101fc, 1}, {0x0101fd, 0x0101fd, 0},
1977 {0x0101fe, 0x01027f, -1}, {0x010280, 0x01029c, 1}, {0x01029d, 0x01029f, -1},
1978 {0x0102a0, 0x0102d0, 1}, {0x0102d1, 0x0102df, -1}, {0x0102e0, 0x0102e0, 0},
1979 {0x0102e1, 0x0102fb, 1}, {0x0102fc, 0x0102ff, -1}, {0x010300, 0x010323, 1},
1980 {0x010324, 0x01032c, -1}, {0x01032d, 0x01034a, 1}, {0x01034b, 0x01034f, -1},
1981 {0x010350, 0x010375, 1}, {0x010376, 0x01037a, 0}, {0x01037b, 0x01037f, -1},
1982 {0x010380, 0x01039d, 1}, {0x01039e, 0x01039e, -1}, {0x01039f, 0x0103c3, 1},
1983 {0x0103c4, 0x0103c7, -1}, {0x0103c8, 0x0103d5, 1}, {0x0103d6, 0x0103ff, -1},
1984 {0x010400, 0x01049d, 1}, {0x01049e, 0x01049f, -1}, {0x0104a0, 0x0104a9, 1},
1985 {0x0104aa, 0x0104af, -1}, {0x0104b0, 0x0104d3, 1}, {0x0104d4, 0x0104d7, -1},
1986 {0x0104d8, 0x0104fb, 1}, {0x0104fc, 0x0104ff, -1}, {0x010500, 0x010527, 1},
1987 {0x010528, 0x01052f, -1}, {0x010530, 0x010563, 1}, {0x010564, 0x01056e, -1},
1988 {0x01056f, 0x01057a, 1}, {0x01057b, 0x01057b, -1}, {0x01057c, 0x01058a, 1},
1989 {0x01058b, 0x01058b, -1}, {0x01058c, 0x010592, 1}, {0x010593, 0x010593, -1},
1990 {0x010594, 0x010595, 1}, {0x010596, 0x010596, -1}, {0x010597, 0x0105a1, 1},
1991 {0x0105a2, 0x0105a2, -1}, {0x0105a3, 0x0105b1, 1}, {0x0105b2, 0x0105b2, -1},
1992 {0x0105b3, 0x0105b9, 1}, {0x0105ba, 0x0105ba, -1}, {0x0105bb, 0x0105bc, 1},
1993 {0x0105bd, 0x0105bf, -1}, {0x0105c0, 0x0105f3, 1}, {0x0105f4, 0x0105ff, -1},
1994 {0x010600, 0x010736, 1}, {0x010737, 0x01073f, -1}, {0x010740, 0x010755, 1},
1995 {0x010756, 0x01075f, -1}, {0x010760, 0x010767, 1}, {0x010768, 0x01077f, -1},
1996 {0x010780, 0x010785, 1}, {0x010786, 0x010786, -1}, {0x010787, 0x0107b0, 1},
1997 {0x0107b1, 0x0107b1, -1}, {0x0107b2, 0x0107ba, 1}, {0x0107bb, 0x0107ff, -1},
1998 {0x010800, 0x010805, 1}, {0x010806, 0x010807, -1}, {0x010808, 0x010808, 1},
1999 {0x010809, 0x010809, -1}, {0x01080a, 0x010835, 1}, {0x010836, 0x010836, -1},
2000 {0x010837, 0x010838, 1}, {0x010839, 0x01083b, -1}, {0x01083c, 0x01083c, 1},
2001 {0x01083d, 0x01083e, -1}, {0x01083f, 0x010855, 1}, {0x010856, 0x010856, -1},
2002 {0x010857, 0x01089e, 1}, {0x01089f, 0x0108a6, -1}, {0x0108a7, 0x0108af, 1},
2003 {0x0108b0, 0x0108df, -1}, {0x0108e0, 0x0108f2, 1}, {0x0108f3, 0x0108f3, -1},
2004 {0x0108f4, 0x0108f5, 1}, {0x0108f6, 0x0108fa, -1}, {0x0108fb, 0x01091b, 1},
2005 {0x01091c, 0x01091e, -1}, {0x01091f, 0x010939, 1}, {0x01093a, 0x01093e, -1},
2006 {0x01093f, 0x01093f, 1}, {0x010940, 0x01097f, -1}, {0x010980, 0x0109b7, 1},
2007 {0x0109b8, 0x0109bb, -1}, {0x0109bc, 0x0109cf, 1}, {0x0109d0, 0x0109d1, -1},
2008 {0x0109d2, 0x010a00, 1}, {0x010a01, 0x010a03, 0}, {0x010a04, 0x010a04, -1},
2009 {0x010a05, 0x010a06, 0}, {0x010a07, 0x010a0b, -1}, {0x010a0c, 0x010a0f, 0},
2010 {0x010a10, 0x010a13, 1}, {0x010a14, 0x010a14, -1}, {0x010a15, 0x010a17, 1},
2011 {0x010a18, 0x010a18, -1}, {0x010a19, 0x010a35, 1}, {0x010a36, 0x010a37, -1},
2012 {0x010a38, 0x010a3a, 0}, {0x010a3b, 0x010a3e, -1}, {0x010a3f, 0x010a3f, 0},
2013 {0x010a40, 0x010a48, 1}, {0x010a49, 0x010a4f, -1}, {0x010a50, 0x010a58, 1},
2014 {0x010a59, 0x010a5f, -1}, {0x010a60, 0x010a9f, 1}, {0x010aa0, 0x010abf, -1},
2015 {0x010ac0, 0x010ae4, 1}, {0x010ae5, 0x010ae6, 0}, {0x010ae7, 0x010aea, -1},
2016 {0x010aeb, 0x010af6, 1}, {0x010af7, 0x010aff, -1}, {0x010b00, 0x010b35, 1},
2017 {0x010b36, 0x010b38, -1}, {0x010b39, 0x010b55, 1}, {0x010b56, 0x010b57, -1},
2018 {0x010b58, 0x010b72, 1}, {0x010b73, 0x010b77, -1}, {0x010b78, 0x010b91, 1},
2019 {0x010b92, 0x010b98, -1}, {0x010b99, 0x010b9c, 1}, {0x010b9d, 0x010ba8, -1},
2020 {0x010ba9, 0x010baf, 1}, {0x010bb0, 0x010bff, -1}, {0x010c00, 0x010c48, 1},
2021 {0x010c49, 0x010c7f, -1}, {0x010c80, 0x010cb2, 1}, {0x010cb3, 0x010cbf, -1},
2022 {0x010cc0, 0x010cf2, 1}, {0x010cf3, 0x010cf9, -1}, {0x010cfa, 0x010d23, 1},
2023 {0x010d24, 0x010d27, 0}, {0x010d28, 0x010d2f, -1}, {0x010d30, 0x010d39, 1},
2024 {0x010d3a, 0x010d3f, -1}, {0x010d40, 0x010d65, 1}, {0x010d66, 0x010d68, -1},
2025 {0x010d69, 0x010d6d, 0}, {0x010d6e, 0x010d85, 1}, {0x010d86, 0x010d8d, -1},
2026 {0x010d8e, 0x010d8f, 1}, {0x010d90, 0x010e5f, -1}, {0x010e60, 0x010e7e, 1},
2027 {0x010e7f, 0x010e7f, -1}, {0x010e80, 0x010ea9, 1}, {0x010eaa, 0x010eaa, -1},
2028 {0x010eab, 0x010eac, 0}, {0x010ead, 0x010ead, 1}, {0x010eae, 0x010eaf, -1},
2029 {0x010eb0, 0x010eb1, 1}, {0x010eb2, 0x010ec1, -1}, {0x010ec2, 0x010ec4, 1},
2030 {0x010ec5, 0x010efb, -1}, {0x010efc, 0x010eff, 0}, {0x010f00, 0x010f27, 1},
2031 {0x010f28, 0x010f2f, -1}, {0x010f30, 0x010f45, 1}, {0x010f46, 0x010f50, 0},
2032 {0x010f51, 0x010f59, 1}, {0x010f5a, 0x010f6f, -1}, {0x010f70, 0x010f81, 1},
2033 {0x010f82, 0x010f85, 0}, {0x010f86, 0x010f89, 1}, {0x010f8a, 0x010faf, -1},
2034 {0x010fb0, 0x010fcb, 1}, {0x010fcc, 0x010fdf, -1}, {0x010fe0, 0x010ff6, 1},
2035 {0x010ff7, 0x010fff, -1}, {0x011000, 0x011000, 1}, {0x011001, 0x011001, 0},
2036 {0x011002, 0x011037, 1}, {0x011038, 0x011046, 0}, {0x011047, 0x01104d, 1},
2037 {0x01104e, 0x011051, -1}, {0x011052, 0x01106f, 1}, {0x011070, 0x011070, 0},
2038 {0x011071, 0x011072, 1}, {0x011073, 0x011074, 0}, {0x011075, 0x011075, 1},
2039 {0x011076, 0x01107e, -1}, {0x01107f, 0x011081, 0}, {0x011082, 0x0110b2, 1},
2040 {0x0110b3, 0x0110b6, 0}, {0x0110b7, 0x0110b8, 1}, {0x0110b9, 0x0110ba, 0},
2041 {0x0110bb, 0x0110c1, 1}, {0x0110c2, 0x0110c2, 0}, {0x0110c3, 0x0110cc, -1},
2042 {0x0110cd, 0x0110cd, 1}, {0x0110ce, 0x0110cf, -1}, {0x0110d0, 0x0110e8, 1},
2043 {0x0110e9, 0x0110ef, -1}, {0x0110f0, 0x0110f9, 1}, {0x0110fa, 0x0110ff, -1},
2044 {0x011100, 0x011102, 0}, {0x011103, 0x011126, 1}, {0x011127, 0x01112b, 0},
2045 {0x01112c, 0x01112c, 1}, {0x01112d, 0x011134, 0}, {0x011135, 0x011135, -1},
2046 {0x011136, 0x011147, 1}, {0x011148, 0x01114f, -1}, {0x011150, 0x011172, 1},
2047 {0x011173, 0x011173, 0}, {0x011174, 0x011176, 1}, {0x011177, 0x01117f, -1},
2048 {0x011180, 0x011181, 0}, {0x011182, 0x0111b5, 1}, {0x0111b6, 0x0111be, 0},
2049 {0x0111bf, 0x0111c8, 1}, {0x0111c9, 0x0111cc, 0}, {0x0111cd, 0x0111ce, 1},
2050 {0x0111cf, 0x0111cf, 0}, {0x0111d0, 0x0111df, 1}, {0x0111e0, 0x0111e0, -1},
2051 {0x0111e1, 0x0111f4, 1}, {0x0111f5, 0x0111ff, -1}, {0x011200, 0x011211, 1},
2052 {0x011212, 0x011212, -1}, {0x011213, 0x01122e, 1}, {0x01122f, 0x011231, 0},
2053 {0x011232, 0x011233, 1}, {0x011234, 0x011234, 0}, {0x011235, 0x011235, 1},
2054 {0x011236, 0x011237, 0}, {0x011238, 0x01123d, 1}, {0x01123e, 0x01123e, 0},
2055 {0x01123f, 0x011240, 1}, {0x011241, 0x011241, 0}, {0x011242, 0x01127f, -1},
2056 {0x011280, 0x011286, 1}, {0x011287, 0x011287, -1}, {0x011288, 0x011288, 1},
2057 {0x011289, 0x011289, -1}, {0x01128a, 0x01128d, 1}, {0x01128e, 0x01128e, -1},
2058 {0x01128f, 0x01129d, 1}, {0x01129e, 0x01129e, -1}, {0x01129f, 0x0112a9, 1},
2059 {0x0112aa, 0x0112af, -1}, {0x0112b0, 0x0112de, 1}, {0x0112df, 0x0112df, 0},
2060 {0x0112e0, 0x0112e2, 1}, {0x0112e3, 0x0112ea, 0}, {0x0112eb, 0x0112ef, -1},
2061 {0x0112f0, 0x0112f9, 1}, {0x0112fa, 0x0112ff, -1}, {0x011300, 0x011301, 0},
2062 {0x011302, 0x011303, 1}, {0x011304, 0x011304, -1}, {0x011305, 0x01130c, 1},
2063 {0x01130d, 0x01130e, -1}, {0x01130f, 0x011310, 1}, {0x011311, 0x011312, -1},
2064 {0x011313, 0x011328, 1}, {0x011329, 0x011329, -1}, {0x01132a, 0x011330, 1},
2065 {0x011331, 0x011331, -1}, {0x011332, 0x011333, 1}, {0x011334, 0x011334, -1},
2066 {0x011335, 0x011339, 1}, {0x01133a, 0x01133a, -1}, {0x01133b, 0x01133c, 0},
2067 {0x01133d, 0x01133f, 1}, {0x011340, 0x011340, 0}, {0x011341, 0x011344, 1},
2068 {0x011345, 0x011346, -1}, {0x011347, 0x011348, 1}, {0x011349, 0x01134a, -1},
2069 {0x01134b, 0x01134d, 1}, {0x01134e, 0x01134f, -1}, {0x011350, 0x011350, 1},
2070 {0x011351, 0x011356, -1}, {0x011357, 0x011357, 1}, {0x011358, 0x01135c, -1},
2071 {0x01135d, 0x011363, 1}, {0x011364, 0x011365, -1}, {0x011366, 0x01136c, 0},
2072 {0x01136d, 0x01136f, -1}, {0x011370, 0x011374, 0}, {0x011375, 0x01137f, -1},
2073 {0x011380, 0x011389, 1}, {0x01138a, 0x01138a, -1}, {0x01138b, 0x01138b, 1},
2074 {0x01138c, 0x01138d, -1}, {0x01138e, 0x01138e, 1}, {0x01138f, 0x01138f, -1},
2075 {0x011390, 0x0113b5, 1}, {0x0113b6, 0x0113b6, -1}, {0x0113b7, 0x0113ba, 1},
2076 {0x0113bb, 0x0113c0, 0}, {0x0113c1, 0x0113c1, -1}, {0x0113c2, 0x0113c2, 1},
2077 {0x0113c3, 0x0113c4, -1}, {0x0113c5, 0x0113c5, 1}, {0x0113c6, 0x0113c6, -1},
2078 {0x0113c7, 0x0113ca, 1}, {0x0113cb, 0x0113cb, -1}, {0x0113cc, 0x0113cd, 1},
2079 {0x0113ce, 0x0113ce, 0}, {0x0113cf, 0x0113cf, 1}, {0x0113d0, 0x0113d0, 0},
2080 {0x0113d1, 0x0113d1, 1}, {0x0113d2, 0x0113d2, 0}, {0x0113d3, 0x0113d5, 1},
2081 {0x0113d6, 0x0113d6, -1}, {0x0113d7, 0x0113d8, 1}, {0x0113d9, 0x0113e0, -1},
2082 {0x0113e1, 0x0113e2, 0}, {0x0113e3, 0x0113ff, -1}, {0x011400, 0x011437, 1},
2083 {0x011438, 0x01143f, 0}, {0x011440, 0x011441, 1}, {0x011442, 0x011444, 0},
2084 {0x011445, 0x011445, 1}, {0x011446, 0x011446, 0}, {0x011447, 0x01145b, 1},
2085 {0x01145c, 0x01145c, -1}, {0x01145d, 0x01145d, 1}, {0x01145e, 0x01145e, 0},
2086 {0x01145f, 0x011461, 1}, {0x011462, 0x01147f, -1}, {0x011480, 0x0114b2, 1},
2087 {0x0114b3, 0x0114b8, 0}, {0x0114b9, 0x0114b9, 1}, {0x0114ba, 0x0114ba, 0},
2088 {0x0114bb, 0x0114be, 1}, {0x0114bf, 0x0114c0, 0}, {0x0114c1, 0x0114c1, 1},
2089 {0x0114c2, 0x0114c3, 0}, {0x0114c4, 0x0114c7, 1}, {0x0114c8, 0x0114cf, -1},
2090 {0x0114d0, 0x0114d9, 1}, {0x0114da, 0x01157f, -1}, {0x011580, 0x0115b1, 1},
2091 {0x0115b2, 0x0115b5, 0}, {0x0115b6, 0x0115b7, -1}, {0x0115b8, 0x0115bb, 1},
2092 {0x0115bc, 0x0115bd, 0}, {0x0115be, 0x0115be, 1}, {0x0115bf, 0x0115c0, 0},
2093 {0x0115c1, 0x0115db, 1}, {0x0115dc, 0x0115dd, 0}, {0x0115de, 0x0115ff, -1},
2094 {0x011600, 0x011632, 1}, {0x011633, 0x01163a, 0}, {0x01163b, 0x01163c, 1},
2095 {0x01163d, 0x01163d, 0}, {0x01163e, 0x01163e, 1}, {0x01163f, 0x011640, 0},
2096 {0x011641, 0x011644, 1}, {0x011645, 0x01164f, -1}, {0x011650, 0x011659, 1},
2097 {0x01165a, 0x01165f, -1}, {0x011660, 0x01166c, 1}, {0x01166d, 0x01167f, -1},
2098 {0x011680, 0x0116aa, 1}, {0x0116ab, 0x0116ab, 0}, {0x0116ac, 0x0116ac, 1},
2099 {0x0116ad, 0x0116ad, 0}, {0x0116ae, 0x0116af, 1}, {0x0116b0, 0x0116b5, 0},
2100 {0x0116b6, 0x0116b6, 1}, {0x0116b7, 0x0116b7, 0}, {0x0116b8, 0x0116b9, 1},
2101 {0x0116ba, 0x0116bf, -1}, {0x0116c0, 0x0116c9, 1}, {0x0116ca, 0x0116cf, -1},
2102 {0x0116d0, 0x0116e3, 1}, {0x0116e4, 0x0116ff, -1}, {0x011700, 0x01171a, 1},
2103 {0x01171b, 0x01171c, -1}, {0x01171d, 0x01171d, 0}, {0x01171e, 0x01171e, 1},
2104 {0x01171f, 0x01171f, 0}, {0x011720, 0x011721, 1}, {0x011722, 0x011725, 0},
2105 {0x011726, 0x011726, 1}, {0x011727, 0x01172b, 0}, {0x01172c, 0x01172f, -1},
2106 {0x011730, 0x011746, 1}, {0x011747, 0x0117ff, -1}, {0x011800, 0x01182e, 1},
2107 {0x01182f, 0x011837, 0}, {0x011838, 0x011838, 1}, {0x011839, 0x01183a, 0},
2108 {0x01183b, 0x01183b, 1}, {0x01183c, 0x01189f, -1}, {0x0118a0, 0x0118f2, 1},
2109 {0x0118f3, 0x0118fe, -1}, {0x0118ff, 0x011906, 1}, {0x011907, 0x011908, -1},
2110 {0x011909, 0x011909, 1}, {0x01190a, 0x01190b, -1}, {0x01190c, 0x011913, 1},
2111 {0x011914, 0x011914, -1}, {0x011915, 0x011916, 1}, {0x011917, 0x011917, -1},
2112 {0x011918, 0x011935, 1}, {0x011936, 0x011936, -1}, {0x011937, 0x011938, 1},
2113 {0x011939, 0x01193a, -1}, {0x01193b, 0x01193c, 0}, {0x01193d, 0x01193d, 1},
2114 {0x01193e, 0x01193e, 0}, {0x01193f, 0x011942, 1}, {0x011943, 0x011943, 0},
2115 {0x011944, 0x011946, 1}, {0x011947, 0x01194f, -1}, {0x011950, 0x011959, 1},
2116 {0x01195a, 0x01199f, -1}, {0x0119a0, 0x0119a7, 1}, {0x0119a8, 0x0119a9, -1},
2117 {0x0119aa, 0x0119d3, 1}, {0x0119d4, 0x0119d7, 0}, {0x0119d8, 0x0119d9, -1},
2118 {0x0119da, 0x0119db, 0}, {0x0119dc, 0x0119df, 1}, {0x0119e0, 0x0119e0, 0},
2119 {0x0119e1, 0x0119e4, 1}, {0x0119e5, 0x0119ff, -1}, {0x011a00, 0x011a00, 1},
2120 {0x011a01, 0x011a0a, 0}, {0x011a0b, 0x011a32, 1}, {0x011a33, 0x011a38, 0},
2121 {0x011a39, 0x011a3a, 1}, {0x011a3b, 0x011a3e, 0}, {0x011a3f, 0x011a46, 1},
2122 {0x011a47, 0x011a47, 0}, {0x011a48, 0x011a4f, -1}, {0x011a50, 0x011a50, 1},
2123 {0x011a51, 0x011a56, 0}, {0x011a57, 0x011a58, 1}, {0x011a59, 0x011a5b, 0},
2124 {0x011a5c, 0x011a89, 1}, {0x011a8a, 0x011a96, 0}, {0x011a97, 0x011a97, 1},
2125 {0x011a98, 0x011a99, 0}, {0x011a9a, 0x011aa2, 1}, {0x011aa3, 0x011aaf, -1},
2126 {0x011ab0, 0x011af8, 1}, {0x011af9, 0x011aff, -1}, {0x011b00, 0x011b09, 1},
2127 {0x011b0a, 0x011bbf, -1}, {0x011bc0, 0x011be1, 1}, {0x011be2, 0x011bef, -1},
2128 {0x011bf0, 0x011bf9, 1}, {0x011bfa, 0x011bff, -1}, {0x011c00, 0x011c08, 1},
2129 {0x011c09, 0x011c09, -1}, {0x011c0a, 0x011c2f, 1}, {0x011c30, 0x011c36, 0},
2130 {0x011c37, 0x011c37, -1}, {0x011c38, 0x011c3d, 0}, {0x011c3e, 0x011c3e, 1},
2131 {0x011c3f, 0x011c3f, 0}, {0x011c40, 0x011c45, 1}, {0x011c46, 0x011c4f, -1},
2132 {0x011c50, 0x011c6c, 1}, {0x011c6d, 0x011c6f, -1}, {0x011c70, 0x011c8f, 1},
2133 {0x011c90, 0x011c91, -1}, {0x011c92, 0x011ca7, 0}, {0x011ca8, 0x011ca8, -1},
2134 {0x011ca9, 0x011ca9, 1}, {0x011caa, 0x011cb0, 0}, {0x011cb1, 0x011cb1, 1},
2135 {0x011cb2, 0x011cb3, 0}, {0x011cb4, 0x011cb4, 1}, {0x011cb5, 0x011cb6, 0},
2136 {0x011cb7, 0x011cff, -1}, {0x011d00, 0x011d06, 1}, {0x011d07, 0x011d07, -1},
2137 {0x011d08, 0x011d09, 1}, {0x011d0a, 0x011d0a, -1}, {0x011d0b, 0x011d30, 1},
2138 {0x011d31, 0x011d36, 0}, {0x011d37, 0x011d39, -1}, {0x011d3a, 0x011d3a, 0},
2139 {0x011d3b, 0x011d3b, -1}, {0x011d3c, 0x011d3d, 0}, {0x011d3e, 0x011d3e, -1},
2140 {0x011d3f, 0x011d45, 0}, {0x011d46, 0x011d46, 1}, {0x011d47, 0x011d47, 0},
2141 {0x011d48, 0x011d4f, -1}, {0x011d50, 0x011d59, 1}, {0x011d5a, 0x011d5f, -1},
2142 {0x011d60, 0x011d65, 1}, {0x011d66, 0x011d66, -1}, {0x011d67, 0x011d68, 1},
2143 {0x011d69, 0x011d69, -1}, {0x011d6a, 0x011d8e, 1}, {0x011d8f, 0x011d8f, -1},
2144 {0x011d90, 0x011d91, 0}, {0x011d92, 0x011d92, -1}, {0x011d93, 0x011d94, 1},
2145 {0x011d95, 0x011d95, 0}, {0x011d96, 0x011d96, 1}, {0x011d97, 0x011d97, 0},
2146 {0x011d98, 0x011d98, 1}, {0x011d99, 0x011d9f, -1}, {0x011da0, 0x011da9, 1},
2147 {0x011daa, 0x011edf, -1}, {0x011ee0, 0x011ef2, 1}, {0x011ef3, 0x011ef4, 0},
2148 {0x011ef5, 0x011ef8, 1}, {0x011ef9, 0x011eff, -1}, {0x011f00, 0x011f01, 0},
2149 {0x011f02, 0x011f10, 1}, {0x011f11, 0x011f11, -1}, {0x011f12, 0x011f35, 1},
2150 {0x011f36, 0x011f3a, 0}, {0x011f3b, 0x011f3d, -1}, {0x011f3e, 0x011f3f, 1},
2151 {0x011f40, 0x011f40, 0}, {0x011f41, 0x011f41, 1}, {0x011f42, 0x011f42, 0},
2152 {0x011f43, 0x011f59, 1}, {0x011f5a, 0x011f5a, 0}, {0x011f5b, 0x011faf, -1},
2153 {0x011fb0, 0x011fb0, 1}, {0x011fb1, 0x011fbf, -1}, {0x011fc0, 0x011ff1, 1},
2154 {0x011ff2, 0x011ffe, -1}, {0x011fff, 0x012399, 1}, {0x01239a, 0x0123ff, -1},
2155 {0x012400, 0x01246e, 1}, {0x01246f, 0x01246f, -1}, {0x012470, 0x012474, 1},
2156 {0x012475, 0x01247f, -1}, {0x012480, 0x012543, 1}, {0x012544, 0x012f8f, -1},
2157 {0x012f90, 0x012ff2, 1}, {0x012ff3, 0x012fff, -1}, {0x013000, 0x01343f, 1},
2158 {0x013440, 0x013440, 0}, {0x013441, 0x013446, 1}, {0x013447, 0x013455, 0},
2159 {0x013456, 0x01345f, -1}, {0x013460, 0x0143fa, 1}, {0x0143fb, 0x0143ff, -1},
2160 {0x014400, 0x014646, 1}, {0x014647, 0x0160ff, -1}, {0x016100, 0x01611d, 1},
2161 {0x01611e, 0x016129, 0}, {0x01612a, 0x01612c, 1}, {0x01612d, 0x01612f, 0},
2162 {0x016130, 0x016139, 1}, {0x01613a, 0x0167ff, -1}, {0x016800, 0x016a38, 1},
2163 {0x016a39, 0x016a3f, -1}, {0x016a40, 0x016a5e, 1}, {0x016a5f, 0x016a5f, -1},
2164 {0x016a60, 0x016a69, 1}, {0x016a6a, 0x016a6d, -1}, {0x016a6e, 0x016abe, 1},
2165 {0x016abf, 0x016abf, -1}, {0x016ac0, 0x016ac9, 1}, {0x016aca, 0x016acf, -1},
2166 {0x016ad0, 0x016aed, 1}, {0x016aee, 0x016aef, -1}, {0x016af0, 0x016af4, 0},
2167 {0x016af5, 0x016af5, 1}, {0x016af6, 0x016aff, -1}, {0x016b00, 0x016b2f, 1},
2168 {0x016b30, 0x016b36, 0}, {0x016b37, 0x016b45, 1}, {0x016b46, 0x016b4f, -1},
2169 {0x016b50, 0x016b59, 1}, {0x016b5a, 0x016b5a, -1}, {0x016b5b, 0x016b61, 1},
2170 {0x016b62, 0x016b62, -1}, {0x016b63, 0x016b77, 1}, {0x016b78, 0x016b7c, -1},
2171 {0x016b7d, 0x016b8f, 1}, {0x016b90, 0x016d3f, -1}, {0x016d40, 0x016d79, 1},
2172 {0x016d7a, 0x016e3f, -1}, {0x016e40, 0x016e9a, 1}, {0x016e9b, 0x016eff, -1},
2173 {0x016f00, 0x016f4a, 1}, {0x016f4b, 0x016f4e, -1}, {0x016f4f, 0x016f4f, 0},
2174 {0x016f50, 0x016f87, 1}, {0x016f88, 0x016f8e, -1}, {0x016f8f, 0x016f92, 0},
2175 {0x016f93, 0x016f9f, 1}, {0x016fa0, 0x016fdf, -1}, {0x016fe0, 0x016fe3, 2},
2176 {0x016fe4, 0x016fe4, 0}, {0x016fe5, 0x016fef, -1}, {0x016ff0, 0x016ff1, 2},
2177 {0x016ff2, 0x016fff, -1}, {0x017000, 0x0187f7, 2}, {0x0187f8, 0x0187ff, -1},
2178 {0x018800, 0x018cd5, 2}, {0x018cd6, 0x018cfe, -1}, {0x018cff, 0x018d08, 2},
2179 {0x018d09, 0x01afef, -1}, {0x01aff0, 0x01aff3, 2}, {0x01aff4, 0x01aff4, -1},
2180 {0x01aff5, 0x01affb, 2}, {0x01affc, 0x01affc, -1}, {0x01affd, 0x01affe, 2},
2181 {0x01afff, 0x01afff, -1}, {0x01b000, 0x01b122, 2}, {0x01b123, 0x01b131, -1},
2182 {0x01b132, 0x01b132, 2}, {0x01b133, 0x01b14f, -1}, {0x01b150, 0x01b152, 2},
2183 {0x01b153, 0x01b154, -1}, {0x01b155, 0x01b155, 2}, {0x01b156, 0x01b163, -1},
2184 {0x01b164, 0x01b167, 2}, {0x01b168, 0x01b16f, -1}, {0x01b170, 0x01b2fb, 2},
2185 {0x01b2fc, 0x01bbff, -1}, {0x01bc00, 0x01bc6a, 1}, {0x01bc6b, 0x01bc6f, -1},
2186 {0x01bc70, 0x01bc7c, 1}, {0x01bc7d, 0x01bc7f, -1}, {0x01bc80, 0x01bc88, 1},
2187 {0x01bc89, 0x01bc8f, -1}, {0x01bc90, 0x01bc99, 1}, {0x01bc9a, 0x01bc9b, -1},
2188 {0x01bc9c, 0x01bc9c, 1}, {0x01bc9d, 0x01bc9e, 0}, {0x01bc9f, 0x01bc9f, 1},
2189 {0x01bca0, 0x01bca3, 0}, {0x01bca4, 0x01cbff, -1}, {0x01cc00, 0x01ccf9, 1},
2190 {0x01ccfa, 0x01ccff, -1}, {0x01cd00, 0x01ceb3, 1}, {0x01ceb4, 0x01ceff, -1},
2191 {0x01cf00, 0x01cf2d, 0}, {0x01cf2e, 0x01cf2f, -1}, {0x01cf30, 0x01cf46, 0},
2192 {0x01cf47, 0x01cf4f, -1}, {0x01cf50, 0x01cfc3, 1}, {0x01cfc4, 0x01cfff, -1},
2193 {0x01d000, 0x01d0f5, 1}, {0x01d0f6, 0x01d0ff, -1}, {0x01d100, 0x01d126, 1},
2194 {0x01d127, 0x01d128, -1}, {0x01d129, 0x01d166, 1}, {0x01d167, 0x01d169, 0},
2195 {0x01d16a, 0x01d172, 1}, {0x01d173, 0x01d182, 0}, {0x01d183, 0x01d184, 1},
2196 {0x01d185, 0x01d18b, 0}, {0x01d18c, 0x01d1a9, 1}, {0x01d1aa, 0x01d1ad, 0},
2197 {0x01d1ae, 0x01d1ea, 1}, {0x01d1eb, 0x01d1ff, -1}, {0x01d200, 0x01d241, 1},
2198 {0x01d242, 0x01d244, 0}, {0x01d245, 0x01d245, 1}, {0x01d246, 0x01d2bf, -1},
2199 {0x01d2c0, 0x01d2d3, 1}, {0x01d2d4, 0x01d2df, -1}, {0x01d2e0, 0x01d2f3, 1},
2200 {0x01d2f4, 0x01d2ff, -1}, {0x01d300, 0x01d356, 2}, {0x01d357, 0x01d35f, -1},
2201 {0x01d360, 0x01d376, 2}, {0x01d377, 0x01d378, 1}, {0x01d379, 0x01d3ff, -1},
2202 {0x01d400, 0x01d454, 1}, {0x01d455, 0x01d455, -1}, {0x01d456, 0x01d49c, 1},
2203 {0x01d49d, 0x01d49d, -1}, {0x01d49e, 0x01d49f, 1}, {0x01d4a0, 0x01d4a1, -1},
2204 {0x01d4a2, 0x01d4a2, 1}, {0x01d4a3, 0x01d4a4, -1}, {0x01d4a5, 0x01d4a6, 1},
2205 {0x01d4a7, 0x01d4a8, -1}, {0x01d4a9, 0x01d4ac, 1}, {0x01d4ad, 0x01d4ad, -1},
2206 {0x01d4ae, 0x01d4b9, 1}, {0x01d4ba, 0x01d4ba, -1}, {0x01d4bb, 0x01d4bb, 1},
2207 {0x01d4bc, 0x01d4bc, -1}, {0x01d4bd, 0x01d4c3, 1}, {0x01d4c4, 0x01d4c4, -1},
2208 {0x01d4c5, 0x01d505, 1}, {0x01d506, 0x01d506, -1}, {0x01d507, 0x01d50a, 1},
2209 {0x01d50b, 0x01d50c, -1}, {0x01d50d, 0x01d514, 1}, {0x01d515, 0x01d515, -1},
2210 {0x01d516, 0x01d51c, 1}, {0x01d51d, 0x01d51d, -1}, {0x01d51e, 0x01d539, 1},
2211 {0x01d53a, 0x01d53a, -1}, {0x01d53b, 0x01d53e, 1}, {0x01d53f, 0x01d53f, -1},
2212 {0x01d540, 0x01d544, 1}, {0x01d545, 0x01d545, -1}, {0x01d546, 0x01d546, 1},
2213 {0x01d547, 0x01d549, -1}, {0x01d54a, 0x01d550, 1}, {0x01d551, 0x01d551, -1},
2214 {0x01d552, 0x01d6a5, 1}, {0x01d6a6, 0x01d6a7, -1}, {0x01d6a8, 0x01d7cb, 1},
2215 {0x01d7cc, 0x01d7cd, -1}, {0x01d7ce, 0x01d9ff, 1}, {0x01da00, 0x01da36, 0},
2216 {0x01da37, 0x01da3a, 1}, {0x01da3b, 0x01da6c, 0}, {0x01da6d, 0x01da74, 1},
2217 {0x01da75, 0x01da75, 0}, {0x01da76, 0x01da83, 1}, {0x01da84, 0x01da84, 0},
2218 {0x01da85, 0x01da8b, 1}, {0x01da8c, 0x01da9a, -1}, {0x01da9b, 0x01da9f, 0},
2219 {0x01daa0, 0x01daa0, -1}, {0x01daa1, 0x01daaf, 0}, {0x01dab0, 0x01deff, -1},
2220 {0x01df00, 0x01df1e, 1}, {0x01df1f, 0x01df24, -1}, {0x01df25, 0x01df2a, 1},
2221 {0x01df2b, 0x01dfff, -1}, {0x01e000, 0x01e006, 0}, {0x01e007, 0x01e007, -1},
2222 {0x01e008, 0x01e018, 0}, {0x01e019, 0x01e01a, -1}, {0x01e01b, 0x01e021, 0},
2223 {0x01e022, 0x01e022, -1}, {0x01e023, 0x01e024, 0}, {0x01e025, 0x01e025, -1},
2224 {0x01e026, 0x01e02a, 0}, {0x01e02b, 0x01e02f, -1}, {0x01e030, 0x01e06d, 1},
2225 {0x01e06e, 0x01e08e, -1}, {0x01e08f, 0x01e08f, 0}, {0x01e090, 0x01e0ff, -1},
2226 {0x01e100, 0x01e12c, 1}, {0x01e12d, 0x01e12f, -1}, {0x01e130, 0x01e136, 0},
2227 {0x01e137, 0x01e13d, 1}, {0x01e13e, 0x01e13f, -1}, {0x01e140, 0x01e149, 1},
2228 {0x01e14a, 0x01e14d, -1}, {0x01e14e, 0x01e14f, 1}, {0x01e150, 0x01e28f, -1},
2229 {0x01e290, 0x01e2ad, 1}, {0x01e2ae, 0x01e2ae, 0}, {0x01e2af, 0x01e2bf, -1},
2230 {0x01e2c0, 0x01e2eb, 1}, {0x01e2ec, 0x01e2ef, 0}, {0x01e2f0, 0x01e2f9, 1},
2231 {0x01e2fa, 0x01e2fe, -1}, {0x01e2ff, 0x01e2ff, 1}, {0x01e300, 0x01e4cf, -1},
2232 {0x01e4d0, 0x01e4eb, 1}, {0x01e4ec, 0x01e4ef, 0}, {0x01e4f0, 0x01e4f9, 1},
2233 {0x01e4fa, 0x01e5cf, -1}, {0x01e5d0, 0x01e5ed, 1}, {0x01e5ee, 0x01e5ef, 0},
2234 {0x01e5f0, 0x01e5fa, 1}, {0x01e5fb, 0x01e5fe, -1}, {0x01e5ff, 0x01e5ff, 1},
2235 {0x01e600, 0x01e7df, -1}, {0x01e7e0, 0x01e7e6, 1}, {0x01e7e7, 0x01e7e7, -1},
2236 {0x01e7e8, 0x01e7eb, 1}, {0x01e7ec, 0x01e7ec, -1}, {0x01e7ed, 0x01e7ee, 1},
2237 {0x01e7ef, 0x01e7ef, -1}, {0x01e7f0, 0x01e7fe, 1}, {0x01e7ff, 0x01e7ff, -1},
2238 {0x01e800, 0x01e8c4, 1}, {0x01e8c5, 0x01e8c6, -1}, {0x01e8c7, 0x01e8cf, 1},
2239 {0x01e8d0, 0x01e8d6, 0}, {0x01e8d7, 0x01e8ff, -1}, {0x01e900, 0x01e943, 1},
2240 {0x01e944, 0x01e94a, 0}, {0x01e94b, 0x01e94b, 1}, {0x01e94c, 0x01e94f, -1},
2241 {0x01e950, 0x01e959, 1}, {0x01e95a, 0x01e95d, -1}, {0x01e95e, 0x01e95f, 1},
2242 {0x01e960, 0x01ec70, -1}, {0x01ec71, 0x01ecb4, 1}, {0x01ecb5, 0x01ed00, -1},
2243 {0x01ed01, 0x01ed3d, 1}, {0x01ed3e, 0x01edff, -1}, {0x01ee00, 0x01ee03, 1},
2244 {0x01ee04, 0x01ee04, -1}, {0x01ee05, 0x01ee1f, 1}, {0x01ee20, 0x01ee20, -1},
2245 {0x01ee21, 0x01ee22, 1}, {0x01ee23, 0x01ee23, -1}, {0x01ee24, 0x01ee24, 1},
2246 {0x01ee25, 0x01ee26, -1}, {0x01ee27, 0x01ee27, 1}, {0x01ee28, 0x01ee28, -1},
2247 {0x01ee29, 0x01ee32, 1}, {0x01ee33, 0x01ee33, -1}, {0x01ee34, 0x01ee37, 1},
2248 {0x01ee38, 0x01ee38, -1}, {0x01ee39, 0x01ee39, 1}, {0x01ee3a, 0x01ee3a, -1},
2249 {0x01ee3b, 0x01ee3b, 1}, {0x01ee3c, 0x01ee41, -1}, {0x01ee42, 0x01ee42, 1},
2250 {0x01ee43, 0x01ee46, -1}, {0x01ee47, 0x01ee47, 1}, {0x01ee48, 0x01ee48, -1},
2251 {0x01ee49, 0x01ee49, 1}, {0x01ee4a, 0x01ee4a, -1}, {0x01ee4b, 0x01ee4b, 1},
2252 {0x01ee4c, 0x01ee4c, -1}, {0x01ee4d, 0x01ee4f, 1}, {0x01ee50, 0x01ee50, -1},
2253 {0x01ee51, 0x01ee52, 1}, {0x01ee53, 0x01ee53, -1}, {0x01ee54, 0x01ee54, 1},
2254 {0x01ee55, 0x01ee56, -1}, {0x01ee57, 0x01ee57, 1}, {0x01ee58, 0x01ee58, -1},
2255 {0x01ee59, 0x01ee59, 1}, {0x01ee5a, 0x01ee5a, -1}, {0x01ee5b, 0x01ee5b, 1},
2256 {0x01ee5c, 0x01ee5c, -1}, {0x01ee5d, 0x01ee5d, 1}, {0x01ee5e, 0x01ee5e, -1},
2257 {0x01ee5f, 0x01ee5f, 1}, {0x01ee60, 0x01ee60, -1}, {0x01ee61, 0x01ee62, 1},
2258 {0x01ee63, 0x01ee63, -1}, {0x01ee64, 0x01ee64, 1}, {0x01ee65, 0x01ee66, -1},
2259 {0x01ee67, 0x01ee6a, 1}, {0x01ee6b, 0x01ee6b, -1}, {0x01ee6c, 0x01ee72, 1},
2260 {0x01ee73, 0x01ee73, -1}, {0x01ee74, 0x01ee77, 1}, {0x01ee78, 0x01ee78, -1},
2261 {0x01ee79, 0x01ee7c, 1}, {0x01ee7d, 0x01ee7d, -1}, {0x01ee7e, 0x01ee7e, 1},
2262 {0x01ee7f, 0x01ee7f, -1}, {0x01ee80, 0x01ee89, 1}, {0x01ee8a, 0x01ee8a, -1},
2263 {0x01ee8b, 0x01ee9b, 1}, {0x01ee9c, 0x01eea0, -1}, {0x01eea1, 0x01eea3, 1},
2264 {0x01eea4, 0x01eea4, -1}, {0x01eea5, 0x01eea9, 1}, {0x01eeaa, 0x01eeaa, -1},
2265 {0x01eeab, 0x01eebb, 1}, {0x01eebc, 0x01eeef, -1}, {0x01eef0, 0x01eef1, 1},
2266 {0x01eef2, 0x01efff, -1}, {0x01f000, 0x01f003, 1}, {0x01f004, 0x01f004, 2},
2267 {0x01f005, 0x01f02b, 1}, {0x01f02c, 0x01f02f, -1}, {0x01f030, 0x01f093, 1},
2268 {0x01f094, 0x01f09f, -1}, {0x01f0a0, 0x01f0ae, 1}, {0x01f0af, 0x01f0b0, -1},
2269 {0x01f0b1, 0x01f0bf, 1}, {0x01f0c0, 0x01f0c0, -1}, {0x01f0c1, 0x01f0ce, 1},
2270 {0x01f0cf, 0x01f0cf, 2}, {0x01f0d0, 0x01f0d0, -1}, {0x01f0d1, 0x01f0f5, 1},
2271 {0x01f0f6, 0x01f0ff, -1}, {0x01f100, 0x01f18d, 1}, {0x01f18e, 0x01f18e, 2},
2272 {0x01f18f, 0x01f190, 1}, {0x01f191, 0x01f19a, 2}, {0x01f19b, 0x01f1ad, 1},
2273 {0x01f1ae, 0x01f1e5, -1}, {0x01f1e6, 0x01f1ff, 1}, {0x01f200, 0x01f202, 2},
2274 {0x01f203, 0x01f20f, -1}, {0x01f210, 0x01f23b, 2}, {0x01f23c, 0x01f23f, -1},
2275 {0x01f240, 0x01f248, 2}, {0x01f249, 0x01f24f, -1}, {0x01f250, 0x01f251, 2},
2276 {0x01f252, 0x01f25f, -1}, {0x01f260, 0x01f265, 2}, {0x01f266, 0x01f2ff, -1},
2277 {0x01f300, 0x01f320, 2}, {0x01f321, 0x01f32c, 1}, {0x01f32d, 0x01f335, 2},
2278 {0x01f336, 0x01f336, 1}, {0x01f337, 0x01f37c, 2}, {0x01f37d, 0x01f37d, 1},
2279 {0x01f37e, 0x01f393, 2}, {0x01f394, 0x01f39f, 1}, {0x01f3a0, 0x01f3ca, 2},
2280 {0x01f3cb, 0x01f3ce, 1}, {0x01f3cf, 0x01f3d3, 2}, {0x01f3d4, 0x01f3df, 1},
2281 {0x01f3e0, 0x01f3f0, 2}, {0x01f3f1, 0x01f3f3, 1}, {0x01f3f4, 0x01f3f4, 2},
2282 {0x01f3f5, 0x01f3f7, 1}, {0x01f3f8, 0x01f43e, 2}, {0x01f43f, 0x01f43f, 1},
2283 {0x01f440, 0x01f440, 2}, {0x01f441, 0x01f441, 1}, {0x01f442, 0x01f4fc, 2},
2284 {0x01f4fd, 0x01f4fe, 1}, {0x01f4ff, 0x01f53d, 2}, {0x01f53e, 0x01f54a, 1},
2285 {0x01f54b, 0x01f54e, 2}, {0x01f54f, 0x01f54f, 1}, {0x01f550, 0x01f567, 2},
2286 {0x01f568, 0x01f579, 1}, {0x01f57a, 0x01f57a, 2}, {0x01f57b, 0x01f594, 1},
2287 {0x01f595, 0x01f596, 2}, {0x01f597, 0x01f5a3, 1}, {0x01f5a4, 0x01f5a4, 2},
2288 {0x01f5a5, 0x01f5fa, 1}, {0x01f5fb, 0x01f64f, 2}, {0x01f650, 0x01f67f, 1},
2289 {0x01f680, 0x01f6c5, 2}, {0x01f6c6, 0x01f6cb, 1}, {0x01f6cc, 0x01f6cc, 2},
2290 {0x01f6cd, 0x01f6cf, 1}, {0x01f6d0, 0x01f6d2, 2}, {0x01f6d3, 0x01f6d4, 1},
2291 {0x01f6d5, 0x01f6d7, 2}, {0x01f6d8, 0x01f6db, -1}, {0x01f6dc, 0x01f6df, 2},
2292 {0x01f6e0, 0x01f6ea, 1}, {0x01f6eb, 0x01f6ec, 2}, {0x01f6ed, 0x01f6ef, -1},
2293 {0x01f6f0, 0x01f6f3, 1}, {0x01f6f4, 0x01f6fc, 2}, {0x01f6fd, 0x01f6ff, -1},
2294 {0x01f700, 0x01f776, 1}, {0x01f777, 0x01f77a, -1}, {0x01f77b, 0x01f7d9, 1},
2295 {0x01f7da, 0x01f7df, -1}, {0x01f7e0, 0x01f7eb, 2}, {0x01f7ec, 0x01f7ef, -1},
2296 {0x01f7f0, 0x01f7f0, 2}, {0x01f7f1, 0x01f7ff, -1}, {0x01f800, 0x01f80b, 1},
2297 {0x01f80c, 0x01f80f, -1}, {0x01f810, 0x01f847, 1}, {0x01f848, 0x01f84f, -1},
2298 {0x01f850, 0x01f859, 1}, {0x01f85a, 0x01f85f, -1}, {0x01f860, 0x01f887, 1},
2299 {0x01f888, 0x01f88f, -1}, {0x01f890, 0x01f8ad, 1}, {0x01f8ae, 0x01f8af, -1},
2300 {0x01f8b0, 0x01f8bb, 1}, {0x01f8bc, 0x01f8bf, -1}, {0x01f8c0, 0x01f8c1, 1},
2301 {0x01f8c2, 0x01f8ff, -1}, {0x01f900, 0x01f90b, 1}, {0x01f90c, 0x01f93a, 2},
2302 {0x01f93b, 0x01f93b, 1}, {0x01f93c, 0x01f945, 2}, {0x01f946, 0x01f946, 1},
2303 {0x01f947, 0x01f9ff, 2}, {0x01fa00, 0x01fa53, 1}, {0x01fa54, 0x01fa5f, -1},
2304 {0x01fa60, 0x01fa6d, 1}, {0x01fa6e, 0x01fa6f, -1}, {0x01fa70, 0x01fa7c, 2},
2305 {0x01fa7d, 0x01fa7f, -1}, {0x01fa80, 0x01fa89, 2}, {0x01fa8a, 0x01fa8e, -1},
2306 {0x01fa8f, 0x01fac6, 2}, {0x01fac7, 0x01facd, -1}, {0x01face, 0x01fadc, 2},
2307 {0x01fadd, 0x01fade, -1}, {0x01fadf, 0x01fae9, 2}, {0x01faea, 0x01faef, -1},
2308 {0x01faf0, 0x01faf8, 2}, {0x01faf9, 0x01faff, -1}, {0x01fb00, 0x01fb92, 1},
2309 {0x01fb93, 0x01fb93, -1}, {0x01fb94, 0x01fbf9, 1}, {0x01fbfa, 0x01ffff, -1},
2310 {0x020000, 0x02a6df, 2}, {0x02a6e0, 0x02a6ff, -1}, {0x02a700, 0x02b739, 2},
2311 {0x02b73a, 0x02b73f, -1}, {0x02b740, 0x02b81d, 2}, {0x02b81e, 0x02b81f, -1},
2312 {0x02b820, 0x02cea1, 2}, {0x02cea2, 0x02ceaf, -1}, {0x02ceb0, 0x02ebe0, 2},
2313 {0x02ebe1, 0x02ebef, -1}, {0x02ebf0, 0x02ee5d, 2}, {0x02ee5e, 0x02f7ff, -1},
2314 {0x02f800, 0x02fa1d, 2}, {0x02fa1e, 0x02ffff, -1}, {0x030000, 0x03134a, 2},
2315 {0x03134b, 0x03134f, -1}, {0x031350, 0x0323af, 2}, {0x0323b0, 0x0e0000, -1},
2316 {0x0e0001, 0x0e0001, 0}, {0x0e0002, 0x0e001f, -1}, {0x0e0020, 0x0e007f, 0},
2317 {0x0e0080, 0x0e00ff, -1}, {0x0e0100, 0x0e01ef, 0}, {0x0e01f0, 0x0effff, -1},
2318 {0x0f0000, 0x0ffffd, 1}, {0x0ffffe, 0x0fffff, -1}, {0x100000, 0x10fffd, 1},
2319 {0x10fffe, 0x10ffff, -1},
2320 // clang-format on
2321 };
2322 #define WCWIDTH_TABLE_LENGTH 2143
2323 #endif // ifndef TB_OPT_LIBC_WCHAR
2324  
2325 static int tb_reset(void);
2326 static int tb_printf_inner(int x, int y, uintattr_t fg, uintattr_t bg,
2327 size_t *out_w, const char *fmt, va_list vl);
2328 static int init_term_attrs(void);
2329 static int init_term_caps(void);
2330 static int init_cap_trie(void);
2331 static int cap_trie_add(const char *cap, uint16_t key, uint8_t mod);
2332 static int cap_trie_find(const char *buf, size_t nbuf, struct cap_trie **last,
2333 size_t *depth);
2334 static int cap_trie_deinit(struct cap_trie *node);
2335 static int init_resize_handler(void);
2336 static int send_init_escape_codes(void);
2337 static int send_clear(void);
2338 static int update_term_size(void);
2339 static int init_cellbuf(void);
2340 static int tb_deinit(void);
2341 static int load_terminfo(void);
2342 static int load_terminfo_from_path(const char *path, const char *term);
2343 static int read_terminfo_path(const char *path);
2344 static int parse_terminfo_caps(void);
2345 static int load_builtin_caps(void);
2346 static const char *get_terminfo_string(int16_t offsets_pos, int16_t offsets_len,
2347 int16_t table_pos, int16_t table_size, int16_t index);
2348 static int get_terminfo_int16(int offset, int16_t *val);
2349 static int wait_event(struct tb_event *event, int timeout);
2350 static int extract_event(struct tb_event *event);
2351 static int extract_esc(struct tb_event *event);
2352 static int extract_esc_user(struct tb_event *event, int is_post);
2353 static int extract_esc_cap(struct tb_event *event);
2354 static int extract_esc_mouse(struct tb_event *event);
2355 static int resize_cellbufs(void);
2356 static int send_attr(uintattr_t fg, uintattr_t bg);
2357 static int send_sgr(uint32_t fg, uint32_t bg, int fg_is_default,
2358 int bg_is_default);
2359 static int send_cursor_if(int x, int y);
2360 static int send_char(int x, int y, uint32_t ch);
2361 static int send_cluster(int x, int y, uint32_t *ch, size_t nch);
2362 static int convert_num(uint32_t num, char *buf);
2363 static int cell_cmp(struct tb_cell *a, struct tb_cell *b);
2364 static int cell_copy(struct tb_cell *dst, struct tb_cell *src);
2365 static int cell_set(struct tb_cell *cell, uint32_t *ch, size_t nch,
2366 uintattr_t fg, uintattr_t bg);
2367 static int cell_reserve_ech(struct tb_cell *cell, size_t n);
2368 static int cell_free(struct tb_cell *cell);
2369 static int cellbuf_init(struct cellbuf *c, int w, int h);
2370 static int cellbuf_free(struct cellbuf *c);
2371 static int cellbuf_clear(struct cellbuf *c);
2372 static int cellbuf_get(struct cellbuf *c, int x, int y, struct tb_cell **out);
2373 static int cellbuf_in_bounds(struct cellbuf *c, int x, int y);
2374 static int cellbuf_resize(struct cellbuf *c, int w, int h);
2375 static int bytebuf_puts(struct bytebuf *b, const char *str);
2376 static int bytebuf_nputs(struct bytebuf *b, const char *str, size_t nstr);
2377 static int bytebuf_shift(struct bytebuf *b, size_t n);
2378 static int bytebuf_flush(struct bytebuf *b, int fd);
2379 static int bytebuf_reserve(struct bytebuf *b, size_t sz);
2380 static int bytebuf_free(struct bytebuf *b);
2381 static int tb_iswprint_ex(uint32_t ch, int *width);
2382 static int tb_cluster_width(uint32_t *ch, size_t nch);
2383  
2384 /* The platform layer: the functions that talk to the operating system, each
2385 * defined once per operating system. init_term_attrs, init_resize_handler
2386 * and update_term_size, declared above, belong to it as well and keep their
2387 * place; the rest is defined at the end of this file. */
2388 static int open_tty(const char *path, int *rfd, int *wfd);
2389 static int init_fds(int rfd, int wfd);
2390 static int restore_term_attrs(void);
2391 static int deinit_resize_handler(void);
2392 static int close_opened_fds(void);
2393 static int enable_mouse_input(int enabled);
2394 static int wait_input(int timeout_ms, int *resized);
2395 static int write_output(int fd, const char *buf, size_t len);
2396 static const char *get_term_name(void);
2397 static int64_t now_ms(void);
2398 static void format_last_error(void);
2399 #ifndef _WIN32
2400 static int update_term_size_via_esc(void);
2401 static void handle_resize(int sig);
2402 #else
2403 static int is_console(HANDLE handle);
2404 static int win_error(int rv);
2405 static int read_console_records(void);
2406 static int append_key_record(const KEY_EVENT_RECORD *key);
2407 static int append_utf16_unit(uint16_t unit);
2408 static int append_mouse_record(const MOUSE_EVENT_RECORD *mouse);
2409 static int append_mouse_report(int button, const MOUSE_EVENT_RECORD *mouse,
2410 char final);
2411 static int read_stream_bytes(int timeout_ms);
2412 #endif
2413  
2414 0 int tb_init(void) {
2415 0 return tb_init_file("/dev/tty");
2416 }
2417  
2418 0 int tb_init_file(const char *path) {
2419 int rv, rfd, wfd;
2420 0 ▶ if (global.initialized) return TB_ERR_INIT_ALREADY;
Branch: 1 branch
BranchTrueFalseTotal
if (global.initialized) return TB_ERR_INIT_ALREADY; 0 0 0
2421 0 ▶ if_err_return(rv, open_tty(path, &rfd, &wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, open_tty(path, &rfd, &wfd)); 0 0 0
2422 0 global.ttyfd_open = 1;
2423 0 return tb_init_rwfd(rfd, wfd);
2424 }
2425  
2426 0 int tb_init_fd(int ttyfd) {
2427 0 return tb_init_rwfd(ttyfd, ttyfd);
2428 }
2429  
2430 3 int tb_init_rwfd(int rfd, int wfd) {
2431 int rv;
2432  
2433 3 tb_reset();
2434  
2435 do {
2436 3 ▶ if_err_break(rv, init_fds(rfd, wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_fds(rfd, wfd)); 0 3 3
2437 3 ▶ if_err_break(rv, init_term_attrs());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_term_attrs()); 0 3 3
2438 3 ▶ if_err_break(rv, init_term_caps());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_term_caps()); 0 3 3
2439 3 ▶ if_err_break(rv, init_cap_trie());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_cap_trie()); 0 3 3
2440 3 ▶ if_err_break(rv, init_resize_handler());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_resize_handler()); 0 3 3
2441 3 ▶ if_err_break(rv, send_init_escape_codes());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, send_init_escape_codes()); 0 3 3
2442 3 ▶ if_err_break(rv, send_clear());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, send_clear()); 0 3 3
2443 3 ▶ if_err_break(rv, update_term_size());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, update_term_size()); 0 3 3
2444 3 ▶ if_err_break(rv, init_cellbuf());
Branch: 1 branch
BranchTrueFalseTotal
if_err_break(rv, init_cellbuf()); 0 3 3
2445 3 global.initialized = 1;
2446 } while (0);
2447  
2448 3 ▶ if (rv != TB_OK) tb_deinit();
Branch: 1 branch
BranchTrueFalseTotal
if (rv != TB_OK) tb_deinit(); 0 3 3
2449  
2450 3 return rv;
2451 }
2452  
2453 3 int tb_shutdown(void) {
2454 3 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 3 3
2455 3 tb_deinit();
2456 3 return TB_OK;
2457 }
2458  
2459 1 int tb_width(void) {
2460 1 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 1 0 1
2461 0 return global.width;
2462 }
2463  
2464 0 int tb_height(void) {
2465 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2466 0 return global.height;
2467 }
2468  
2469 0 int tb_clear(void) {
2470 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2471 0 return cellbuf_clear(&global.back);
2472 }
2473  
2474 0 int tb_set_clear_attrs(uintattr_t fg, uintattr_t bg) {
2475 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2476 0 global.fg = fg;
2477 0 global.bg = bg;
2478 0 return TB_OK;
2479 }
2480  
2481 0 int tb_present(void) {
2482 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2483  
2484 int rv;
2485  
2486 // TODO: Assert global.back.(width,height) == global.front.(width,height)
2487  
2488 0 global.last_x = -1;
2489 0 global.last_y = -1;
2490  
2491 int x, y, i;
2492 0 ▶ for (y = 0; y < global.front.height; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < global.front.height; y++) { 0 0 0
2493 0 ▶ for (x = 0; x < global.front.width;) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < global.front.width;) { 0 0 0
2494 struct tb_cell *back, *front;
2495 0 ▶ if_err_return(rv, cellbuf_get(&global.back, x, y, &back));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.back, x, y, &back)); 0 0 0
2496 0 ▶ if_err_return(rv, cellbuf_get(&global.front, x, y, &front));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.front, x, y, &front)); 0 0 0
2497  
2498 int w;
2499 {
2500 #ifdef TB_OPT_EGC
2501 0 ▶ if (back->nech > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (back->nech > 0) 0 0 0
2502 0 w = tb_cluster_width(back->ech, back->nech);
2503 else
2504 #else
2505 (void)tb_cluster_width;
2506 #endif
2507 0 w = tb_wcwidth((wchar_t)back->ch);
2508 }
2509 0 ▶ if (w < 1) w = 1; // wcwidth returns -1 for invalid codepoints
Branch: 1 branch
BranchTrueFalseTotal
if (w < 1) w = 1; // wcwidth returns -1 for invalid codepoints 0 0 0
2510  
2511 0 ▶ if (cell_cmp(back, front) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (cell_cmp(back, front) != 0) { 0 0 0
2512 0 cell_copy(front, back);
2513  
2514 0 send_attr(back->fg, back->bg);
2515 0 ▶ if (w > 1 && x >= global.front.width - (w - 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (w > 1 && x >= global.front.width - (w - 1)) { 0 0 0
if (w > 1 && x >= global.front.width - (w - 1)) { 0 0 0
2516 // Not enough room for wide char, send spaces
2517 0 ▶ for (i = x; i < global.front.width; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = x; i < global.front.width; i++) { 0 0 0
2518 0 send_char(i, y, ' ');
2519 }
2520 } else {
2521 {
2522 #ifdef TB_OPT_EGC
2523 0 ▶ if (back->nech > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (back->nech > 0) 0 0 0
2524 0 send_cluster(x, y, back->ech, back->nech);
2525 else
2526 #endif
2527 0 send_char(x, y, back->ch);
2528 }
2529  
2530 // When wcwidth>1, we need to advance the cursor by more
2531 // than 1, thereby skipping some cells. Set these skipped
2532 // cells to an invalid codepoint in the front buffer, so
2533 // that if this cell is later replaced by a wcwidth==1
2534 // char, we'll get a cell_cmp diff for the skipped cells
2535 // and properly re-render.
2536 0 ▶ for (i = 1; i < w; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < w; i++) { 0 0 0
2537 struct tb_cell *front_wide;
2538 0 uint32_t invalid = -1;
2539 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
2540 cellbuf_get(&global.front, x + i, y, &front_wide));
2541 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
2542 cell_set(front_wide, &invalid, 1, -1, -1));
2543 }
2544 }
2545 }
2546 0 x += w;
2547 }
2548 }
2549  
2550 0 ▶ if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y)); 0 0 0
2551 0 ▶ if_err_return(rv, bytebuf_flush(&global.out, global.wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_flush(&global.out, global.wfd)); 0 0 0
2552  
2553 0 return TB_OK;
2554 }
2555  
2556 0 int tb_invalidate(void) {
2557 int rv;
2558 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2559 0 ▶ if_err_return(rv, resize_cellbufs());
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, resize_cellbufs()); 0 0 0
2560 0 return TB_OK;
2561 }
2562  
2563 0 int tb_set_cursor(int cx, int cy) {
2564 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2565 int rv;
2566 0 ▶ if (cx < 0) cx = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cx < 0) cx = 0; 0 0 0
2567 0 ▶ if (cy < 0) cy = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cy < 0) cy = 0; 0 0 0
2568 0 ▶ if (global.cursor_x == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (global.cursor_x == -1) { 0 0 0
2569 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
2570 bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]));
2571 }
2572 0 ▶ if_err_return(rv, send_cursor_if(cx, cy));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_cursor_if(cx, cy)); 0 0 0
2573 0 global.cursor_x = cx;
2574 0 global.cursor_y = cy;
2575 0 return TB_OK;
2576 }
2577  
2578 0 int tb_hide_cursor(void) {
2579 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2580 int rv;
2581 0 ▶ if (global.cursor_x >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (global.cursor_x >= 0) { 0 0 0
2582 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
2583 bytebuf_puts(&global.out, global.caps[TB_CAP_HIDE_CURSOR]));
2584 }
2585 0 global.cursor_x = -1;
2586 0 global.cursor_y = -1;
2587 0 return TB_OK;
2588 }
2589  
2590 2 int tb_set_cell(int x, int y, uint32_t ch, uintattr_t fg, uintattr_t bg) {
2591 2 return tb_set_cell_ex(x, y, &ch, 1, fg, bg);
2592 }
2593  
2594 2 int tb_set_cell_ex(int x, int y, uint32_t *ch, size_t nch, uintattr_t fg,
2595 uintattr_t bg) {
2596 2 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 2 0 2
2597 int rv;
2598 struct tb_cell *cell;
2599 0 ▶ if_err_return(rv, cellbuf_get(&global.back, x, y, &cell));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.back, x, y, &cell)); 0 0 0
2600 0 ▶ if_err_return(rv, cell_set(cell, ch, nch, fg, bg));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_set(cell, ch, nch, fg, bg)); 0 0 0
2601 0 return TB_OK;
2602 }
2603  
2604 0 int tb_get_cell(int x, int y, int back, struct tb_cell **cell) {
2605 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2606 0 ▶ return cellbuf_get(back ? &global.back : &global.front, x, y, cell);
Branch: 1 branch
BranchTrueFalseTotal
return cellbuf_get(back ? &global.back : &global.front, x, y, cell); 0 0 0
2607 }
2608  
2609 0 int tb_extend_cell(int x, int y, uint32_t ch) {
2610 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2611 #ifdef TB_OPT_EGC
2612 // TODO: iswprint ch?
2613 int rv;
2614 struct tb_cell *cell;
2615 size_t nech;
2616 0 ▶ if_err_return(rv, cellbuf_get(&global.back, x, y, &cell));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(&global.back, x, y, &cell)); 0 0 0
2617 0 ▶ if (cell->nech > 0) { // append to ech
Branch: 1 branch
BranchTrueFalseTotal
if (cell->nech > 0) { // append to ech 0 0 0
2618 0 nech = cell->nech + 1;
2619 0 ▶ if_err_return(rv, cell_reserve_ech(cell, nech + 1));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_reserve_ech(cell, nech + 1)); 0 0 0
2620 0 cell->ech[nech - 1] = ch;
2621 } else { // make new ech
2622 0 nech = 2;
2623 0 ▶ if_err_return(rv, cell_reserve_ech(cell, nech + 1));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_reserve_ech(cell, nech + 1)); 0 0 0
2624 0 cell->ech[0] = cell->ch;
2625 0 cell->ech[1] = ch;
2626 }
2627 0 cell->ech[nech] = '\0';
2628 0 cell->nech = nech;
2629 0 return TB_OK;
2630 #else
2631 (void)x;
2632 (void)y;
2633 (void)ch;
2634 return TB_ERR;
2635 #endif
2636 }
2637  
2638 1 int tb_set_input_mode(int mode) {
2639 int rv;
2640 1 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 1 1
2641  
2642 1 ▶ if (mode == TB_INPUT_CURRENT) return global.input_mode;
Branch: 1 branch
BranchTrueFalseTotal
if (mode == TB_INPUT_CURRENT) return global.input_mode; 0 1 1
2643  
2644 1 int esc_or_alt = TB_INPUT_ESC | TB_INPUT_ALT;
2645 1 ▶ if ((mode & esc_or_alt) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((mode & esc_or_alt) == 0) { 0 1 1
2646 // neither specified; flip on ESC
2647 0 mode |= TB_INPUT_ESC;
2648 1 ▶ } else if ((mode & esc_or_alt) == esc_or_alt) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ((mode & esc_or_alt) == esc_or_alt) { 0 1 1
2649 // both specified; flip off ALT
2650 0 mode &= ~TB_INPUT_ALT;
2651 }
2652  
2653 1 ▶ if (mode & TB_INPUT_MOUSE) {
Branch: 1 branch
BranchTrueFalseTotal
if (mode & TB_INPUT_MOUSE) { 1 0 1
2654 1 bytebuf_puts(&global.out, TB_HARDCAP_ENTER_MOUSE);
2655 1 bytebuf_flush(&global.out, global.wfd);
2656 } else {
2657 0 bytebuf_puts(&global.out, TB_HARDCAP_EXIT_MOUSE);
2658 0 bytebuf_flush(&global.out, global.wfd);
2659 }
2660 1 ▶ if_err_return(rv, enable_mouse_input(mode & TB_INPUT_MOUSE));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, enable_mouse_input(mode & TB_INPUT_MOUSE)); 0 1 1
2661  
2662 1 global.input_mode = mode;
2663 1 return TB_OK;
2664 }
2665  
2666 0 int tb_set_output_mode(int mode) {
2667 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2668 0 switch (mode) {
2669 0 case TB_OUTPUT_CURRENT:
2670 0 return global.output_mode;
2671 0 case TB_OUTPUT_NORMAL:
2672 case TB_OUTPUT_256:
2673 case TB_OUTPUT_216:
2674 case TB_OUTPUT_GRAYSCALE:
2675 #if TB_OPT_ATTR_W >= 32
2676 case TB_OUTPUT_TRUECOLOR:
2677 #endif
2678 0 global.last_fg = ~global.fg;
2679 0 global.last_bg = ~global.bg;
2680 0 global.output_mode = mode;
2681 0 return TB_OK;
2682 }
2683 0 return TB_ERR;
2684 }
2685  
2686 67 int tb_peek_event(struct tb_event *event, int timeout_ms) {
2687 67 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 1 66 67
2688 66 return wait_event(event, timeout_ms);
2689 }
2690  
2691 0 int tb_poll_event(struct tb_event *event) {
2692 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2693 0 return wait_event(event, -1);
2694 }
2695  
2696 0 int tb_get_fds(int *ttyfd, int *resizefd) {
2697 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2698  
2699 0 *ttyfd = global.rfd;
2700 0 *resizefd = global.resize_pipefd[0];
2701  
2702 0 return TB_OK;
2703 }
2704  
2705 0 int tb_print(int x, int y, uintattr_t fg, uintattr_t bg, const char *str) {
2706 0 return tb_print_ex(x, y, fg, bg, NULL, str);
2707 }
2708  
2709 0 int tb_print_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
2710 const char *str) {
2711 int rv, w, ix, x_prev;
2712 uint32_t uni;
2713  
2714 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2715  
2716 0 ▶ if (!cellbuf_in_bounds(&global.back, x, y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cellbuf_in_bounds(&global.back, x, y)) { 0 0 0
2717 0 return TB_ERR_OUT_OF_BOUNDS;
2718 }
2719  
2720 0 ix = x;
2721 0 x_prev = x;
2722 0 ▶ if (out_w) *out_w = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (out_w) *out_w = 0; 0 0 0
2723  
2724 0 ▶ while (*str) {
Branch: 1 branch
BranchTrueFalseTotal
while (*str) { 0 0 0
2725 0 rv = tb_utf8_char_to_unicode(&uni, str);
2726  
2727 0 ▶ if (rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv < 0) { 0 0 0
2728 0 uni = 0xfffd; // replace invalid UTF-8 char with U+FFFD
2729 0 str += rv * -1;
2730 0 ▶ } else if (rv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (rv > 0) { 0 0 0
2731 0 str += rv;
2732 } else {
2733 0 break; // shouldn't get here
2734 }
2735  
2736 0 ▶ if (uni == '\n') { // TODO: \r, \t, \v, \f, etc?
Branch: 1 branch
BranchTrueFalseTotal
if (uni == '\n') { // TODO: \r, \t, \v, \f, etc? 0 0 0
2737 0 x = ix;
2738 0 x_prev = x;
2739 0 y += 1;
2740 0 continue;
2741 0 ▶ } else if (!tb_iswprint_ex(uni, &w)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (!tb_iswprint_ex(uni, &w)) { 0 0 0
2742 0 uni = 0xfffd; // replace non-printable with U+FFFD
2743 0 w = 1;
2744 }
2745  
2746 0 ▶ if (w < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (w < 0) { 0 0 0
2747 0 return TB_ERR; // shouldn't happen if iswprint
2748 0 ▶ } else if (w == 0) { // combining character? TODO: UAX-29
Branch: 1 branch
BranchTrueFalseTotal
} else if (w == 0) { // combining character? TODO: UAX-29 0 0 0
2749 #ifdef TB_OPT_EGC
2750 0 ▶ if (cellbuf_in_bounds(&global.back, x_prev, y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (cellbuf_in_bounds(&global.back, x_prev, y)) { 0 0 0
2751 0 ▶ if_err_return(rv, tb_extend_cell(x_prev, y, uni));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tb_extend_cell(x_prev, y, uni)); 0 0 0
2752 }
2753 #else
2754 (void)x_prev;
2755 #endif
2756 } else {
2757 0 ▶ if (cellbuf_in_bounds(&global.back, x, y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (cellbuf_in_bounds(&global.back, x, y)) { 0 0 0
2758 0 ▶ if_err_return(rv, tb_set_cell(x, y, uni, fg, bg));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, tb_set_cell(x, y, uni, fg, bg)); 0 0 0
2759 }
2760 0 x_prev = x;
2761 0 x += w;
2762 0 ▶ if (out_w) *out_w += w;
Branch: 1 branch
BranchTrueFalseTotal
if (out_w) *out_w += w; 0 0 0
2763 }
2764 }
2765  
2766 0 return TB_OK;
2767 }
2768  
2769 0 int tb_printf(int x, int y, uintattr_t fg, uintattr_t bg, const char *fmt,
2770 ...) {
2771 int rv;
2772 va_list vl;
2773 0 va_start(vl, fmt);
2774 0 rv = tb_printf_inner(x, y, fg, bg, NULL, fmt, vl);
2775 0 va_end(vl);
2776 0 return rv;
2777 }
2778  
2779 0 int tb_printf_ex(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
2780 const char *fmt, ...) {
2781 int rv;
2782 va_list vl;
2783 0 va_start(vl, fmt);
2784 0 rv = tb_printf_inner(x, y, fg, bg, out_w, fmt, vl);
2785 0 va_end(vl);
2786 0 return rv;
2787 }
2788  
2789 0 int tb_send(const char *buf, size_t nbuf) {
2790 0 ▶ if_not_init_return();
Branch: 1 branch
BranchTrueFalseTotal
if_not_init_return(); 0 0 0
2791 0 return bytebuf_nputs(&global.out, buf, nbuf);
2792 }
2793  
2794 0 int tb_sendf(const char *fmt, ...) {
2795 int rv;
2796 char buf[TB_OPT_PRINTF_BUF];
2797 va_list vl;
2798 0 va_start(vl, fmt);
2799 0 rv = vsnprintf(buf, sizeof(buf), fmt, vl);
2800 0 va_end(vl);
2801 0 ▶ if (rv < 0 || rv >= (int)sizeof(buf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv < 0 || rv >= (int)sizeof(buf)) { 0 0 0
if (rv < 0 || rv >= (int)sizeof(buf)) { 0 0 0
2802 0 return TB_ERR;
2803 }
2804 0 return tb_send(buf, (size_t)rv);
2805 }
2806  
2807 1 int tb_set_func(int fn_type, int (*fn)(struct tb_event *, size_t *)) {
2808 1 switch (fn_type) {
2809 1 case TB_FUNC_EXTRACT_PRE:
2810 1 global.fn_extract_esc_pre = fn;
2811 1 return TB_OK;
2812 0 case TB_FUNC_EXTRACT_POST:
2813 0 global.fn_extract_esc_post = fn;
2814 0 return TB_OK;
2815 }
2816 0 return TB_ERR;
2817 }
2818  
2819 0 struct tb_cell *tb_cell_buffer(void) {
2820 0 ▶ if (!global.initialized) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!global.initialized) return NULL; 0 0 0
2821 0 return global.back.cells;
2822 }
2823  
2824 9 int tb_utf8_char_length(char c) {
2825 9 return utf8_length[(unsigned char)c];
2826 }
2827  
2828 4 int tb_utf8_char_to_unicode(uint32_t *out, const char *c) {
2829 4 ▶ if (*c == '\0') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (*c == '\0') return 0; 0 4 4
2830  
2831 int i;
2832 4 unsigned char len = tb_utf8_char_length(*c);
2833 4 unsigned char mask = utf8_mask[len - 1];
2834 4 uint32_t result = c[0] & mask;
2835 5 ▶ for (i = 1; i < len && c[i] != '\0'; ++i) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 1; i < len && c[i] != '\0'; ++i) { 1 4 5
for (i = 1; i < len && c[i] != '\0'; ++i) { 1 0 1
2836 1 result <<= 6;
2837 1 result |= c[i] & 0x3f;
2838 }
2839  
2840 4 ▶ if (i != len) return i * -1;
Branch: 1 branch
BranchTrueFalseTotal
if (i != len) return i * -1; 0 4 4
2841  
2842 4 *out = result;
2843 4 return (int)len;
2844 }
2845  
2846 0 int tb_utf8_unicode_to_char(char *out, uint32_t c) {
2847 0 int len = 0;
2848 int first;
2849 int i;
2850  
2851 0 ▶ if (c < 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if (c < 0x80) { 0 0 0
2852 0 first = 0;
2853 0 len = 1;
2854 0 ▶ } else if (c < 0x800) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c < 0x800) { 0 0 0
2855 0 first = 0xc0;
2856 0 len = 2;
2857 0 ▶ } else if (c < 0x10000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c < 0x10000) { 0 0 0
2858 0 first = 0xe0;
2859 0 len = 3;
2860 0 ▶ } else if (c < 0x200000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c < 0x200000) { 0 0 0
2861 0 first = 0xf0;
2862 0 len = 4;
2863 0 ▶ } else if (c < 0x4000000) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (c < 0x4000000) { 0 0 0
2864 0 first = 0xf8;
2865 0 len = 5;
2866 } else {
2867 0 first = 0xfc;
2868 0 len = 6;
2869 }
2870  
2871 0 ▶ for (i = len - 1; i > 0; --i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = len - 1; i > 0; --i) { 0 0 0
2872 0 out[i] = (c & 0x3f) | 0x80;
2873 0 c >>= 6;
2874 }
2875 0 out[0] = c | first;
2876 0 out[len] = '\0';
2877  
2878 0 return len;
2879 }
2880  
2881 0 int tb_last_errno(void) {
2882 0 return global.last_errno;
2883 }
2884  
2885 1 const char *tb_strerror(int err) {
2886 1 switch (err) {
2887 0 case TB_OK:
2888 0 return "Success";
2889 0 case TB_ERR_NEED_MORE:
2890 0 return "Not enough input";
2891 0 case TB_ERR_INIT_ALREADY:
2892 0 return "Termbox initialized already";
2893 0 case TB_ERR_MEM:
2894 0 return "Out of memory";
2895 0 case TB_ERR_NO_EVENT:
2896 0 return "No event";
2897 0 case TB_ERR_NO_TERM:
2898 0 return "No TERM in environment";
2899 1 case TB_ERR_NOT_INIT:
2900 1 return "Termbox not initialized";
2901 0 case TB_ERR_OUT_OF_BOUNDS:
2902 0 return "Out of bounds";
2903 0 case TB_ERR_UNSUPPORTED_TERM:
2904 0 return "Unsupported terminal";
2905 0 case TB_ERR_CAP_COLLISION:
2906 0 return "Termcaps collision";
2907 0 case TB_ERR_RESIZE_SSCANF:
2908 0 return "Terminal width/height not received by sscanf() after "
2909 "resize";
2910 0 case TB_ERR:
2911 case TB_ERR_INIT_OPEN:
2912 case TB_ERR_READ:
2913 case TB_ERR_RESIZE_IOCTL:
2914 case TB_ERR_RESIZE_PIPE:
2915 case TB_ERR_RESIZE_SIGACTION:
2916 case TB_ERR_POLL:
2917 case TB_ERR_TCGETATTR:
2918 case TB_ERR_TCSETATTR:
2919 case TB_ERR_RESIZE_WRITE:
2920 case TB_ERR_RESIZE_POLL:
2921 case TB_ERR_RESIZE_READ:
2922 default:
2923 0 format_last_error();
2924 0 return (const char *)global.errbuf;
2925 }
2926 }
2927  
2928 1 int tb_has_truecolor(void) {
2929 #if TB_OPT_ATTR_W >= 32
2930 1 return 1;
2931 #else
2932 return 0;
2933 #endif
2934 }
2935  
2936 1 int tb_has_egc(void) {
2937 #ifdef TB_OPT_EGC
2938 1 return 1;
2939 #else
2940 return 0;
2941 #endif
2942 }
2943  
2944 1 int tb_attr_width(void) {
2945 1 return TB_OPT_ATTR_W;
2946 }
2947  
2948 1 const char *tb_version(void) {
2949 1 return TB_VERSION_STR;
2950 }
2951  
2952 6 static int tb_reset(void) {
2953 6 int ttyfd_open = global.ttyfd_open;
2954 6 memset(&global, 0, sizeof(global));
2955 6 global.ttyfd = -1;
2956 6 global.rfd = -1;
2957 6 global.wfd = -1;
2958 6 global.ttyfd_open = ttyfd_open;
2959 6 global.resize_pipefd[0] = -1;
2960 6 global.resize_pipefd[1] = -1;
2961 6 global.width = -1;
2962 6 global.height = -1;
2963 6 global.cursor_x = -1;
2964 6 global.cursor_y = -1;
2965 6 global.last_x = -1;
2966 6 global.last_y = -1;
2967 6 global.fg = TB_DEFAULT;
2968 6 global.bg = TB_DEFAULT;
2969 6 global.last_fg = ~global.fg;
2970 6 global.last_bg = ~global.bg;
2971 6 global.input_mode = TB_INPUT_ESC;
2972 6 global.output_mode = TB_OUTPUT_NORMAL;
2973 6 return TB_OK;
2974 }
2975  
2976 #ifndef _WIN32
2977 3 static int init_term_attrs(void) {
2978 3 ▶ if (global.ttyfd < 0) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (global.ttyfd < 0) return TB_OK; 3 0 3
2979  
2980 0 ▶ if (tcgetattr(global.ttyfd, &global.orig_tios) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tcgetattr(global.ttyfd, &global.orig_tios) != 0) { 0 0 0
2981 0 global.last_errno = errno;
2982 0 return TB_ERR_TCGETATTR;
2983 }
2984  
2985 struct termios tios;
2986 0 memcpy(&tios, &global.orig_tios, sizeof(tios));
2987 0 global.has_orig_tios = 1;
2988  
2989 0 cfmakeraw(&tios);
2990 0 tios.c_cc[VMIN] = 1;
2991 0 tios.c_cc[VTIME] = 0;
2992  
2993 0 ▶ if (tcsetattr(global.ttyfd, TCSAFLUSH, &tios) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (tcsetattr(global.ttyfd, TCSAFLUSH, &tios) != 0) { 0 0 0
2994 0 global.last_errno = errno;
2995 0 return TB_ERR_TCSETATTR;
2996 }
2997  
2998 0 return TB_OK;
2999 }
3000 #else
3001 /* The console's counterpart of raw mode: the console neither buffers lines
3002 * nor echoes nor turns Ctrl-C into a signal, it reports window size changes,
3003 * and it encodes keys as VT sequences (mouse reporting is switched on by
3004 * `tb_set_input_mode`, see enable_mouse_input). Output passes through the
3005 * console's VT parser, and both directions speak UTF-8. Quick edit mode,
3006 * which would swallow the mouse for text selection, is switched off along
3007 * with the other flags. */
3008 static int init_term_attrs(void) {
3009 if (global.in_is_console) {
3010 if (!GetConsoleMode(global.in_handle, &global.orig_in_mode)) {
3011 return win_error(TB_ERR_TCGETATTR);
3012 }
3013 global.has_orig_in_mode = 1;
3014 global.orig_in_cp = GetConsoleCP();
3015 if (!SetConsoleMode(global.in_handle,
3016 ENABLE_VIRTUAL_TERMINAL_INPUT | ENABLE_WINDOW_INPUT |
3017 ENABLE_EXTENDED_FLAGS))
3018 {
3019 return win_error(TB_ERR_UNSUPPORTED_TERM);
3020 }
3021 if (!SetConsoleCP(CP_UTF8)) return win_error(TB_ERR_TCSETATTR);
3022 }
3023  
3024 if (global.out_is_console) {
3025 if (!GetConsoleMode(global.out_handle, &global.orig_out_mode)) {
3026 return win_error(TB_ERR_TCGETATTR);
3027 }
3028 global.has_orig_out_mode = 1;
3029 global.orig_out_cp = GetConsoleOutputCP();
3030 if (!SetConsoleMode(global.out_handle,
3031 ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING |
3032 DISABLE_NEWLINE_AUTO_RETURN))
3033 {
3034 return win_error(TB_ERR_UNSUPPORTED_TERM);
3035 }
3036 if (!SetConsoleOutputCP(CP_UTF8)) return win_error(TB_ERR_TCSETATTR);
3037 }
3038  
3039 return TB_OK;
3040 }
3041 #endif
3042  
3043 0 int tb_printf_inner(int x, int y, uintattr_t fg, uintattr_t bg, size_t *out_w,
3044 const char *fmt, va_list vl) {
3045 int rv;
3046 char buf[TB_OPT_PRINTF_BUF];
3047 0 rv = vsnprintf(buf, sizeof(buf), fmt, vl);
3048 0 ▶ if (rv < 0 || rv >= (int)sizeof(buf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (rv < 0 || rv >= (int)sizeof(buf)) { 0 0 0
if (rv < 0 || rv >= (int)sizeof(buf)) { 0 0 0
3049 0 return TB_ERR;
3050 }
3051 0 return tb_print_ex(x, y, fg, bg, out_w, buf);
3052 }
3053  
3054 3 static int init_term_caps(void) {
3055 3 ▶ if (load_terminfo() == TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (load_terminfo() == TB_OK) { 3 0 3
3056 3 return parse_terminfo_caps();
3057 }
3058 0 return load_builtin_caps();
3059 }
3060  
3061 3 static int init_cap_trie(void) {
3062 int rv, i;
3063  
3064 // Add caps from terminfo or built-in
3065 //
3066 // Collisions are expected as some terminfo entries have dupes. (For
3067 // example, att605-pc collides on TB_CAP_F4 and TB_CAP_DELETE.) First cap
3068 // in TB_CAP_* index order will win.
3069 //
3070 // TODO: Reorder TB_CAP_* so more critical caps come first.
3071 72 ▶ for (i = 0; i < TB_CAP__COUNT_KEYS; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TB_CAP__COUNT_KEYS; i++) { 69 3 72
3072 69 rv = cap_trie_add(global.caps[i], tb_key_i(i), 0);
3073 69 ▶ if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv;
Branch: 2 branches
BranchTrueFalseTotal
if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv; 0 69 69
if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv; 0 0 0
3074 }
3075  
3076 // Add built-in mod caps
3077 //
3078 // Collisions are OK here as well. This can happen if global.caps collides
3079 // with builtin_mod_caps. It is desirable to give precedence to global.caps
3080 // here.
3081 939 ▶ for (i = 0; builtin_mod_caps[i].cap != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; builtin_mod_caps[i].cap != NULL; i++) { 936 3 939
3082 936 rv = cap_trie_add(builtin_mod_caps[i].cap, builtin_mod_caps[i].key,
3083 936 builtin_mod_caps[i].mod);
3084 936 ▶ if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv;
Branch: 2 branches
BranchTrueFalseTotal
if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv; 36 900 936
if (rv != TB_OK && rv != TB_ERR_CAP_COLLISION) return rv; 0 36 36
3085 }
3086  
3087 3 return TB_OK;
3088 }
3089  
3090 1005 static int cap_trie_add(const char *cap, uint16_t key, uint8_t mod) {
3091 1005 struct cap_trie *next, *node = &global.cap_trie;
3092 size_t i, j;
3093  
3094 1005 ▶ if (!cap || strlen(cap) <= 0) return TB_OK; // Nothing to do for empty caps
Branch: 2 branches
BranchTrueFalseTotal
if (!cap || strlen(cap) <= 0) return TB_OK; // Nothing to do for empty caps 1005 0 1005
if (!cap || strlen(cap) <= 0) return TB_OK; // Nothing to do for empty caps 0 1005 1005
3095  
3096 6528 ▶ for (i = 0; cap[i] != '\0'; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; cap[i] != '\0'; i++) { 5523 1005 6528
3097 5523 char c = cap[i];
3098 5523 next = NULL;
3099  
3100 // Check if c is already a child of node
3101 16344 ▶ for (j = 0; j < node->nchildren; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < node->nchildren; j++) { 14883 1461 16344
3102 14883 ▶ if (node->children[j].c == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->children[j].c == c) { 4062 10821 14883
3103 4062 next = &node->children[j];
3104 4062 break;
3105 }
3106 }
3107 5523 ▶ if (!next) {
Branch: 1 branch
BranchTrueFalseTotal
if (!next) { 1461 4062 5523
3108 // We need to add a new child to node
3109 1461 node->nchildren += 1;
3110 1461 node->children = (struct cap_trie *)tb_realloc(node->children,
3111 1461 sizeof(*node) * node->nchildren);
3112 1461 ▶ if (!node->children) {
Branch: 1 branch
BranchTrueFalseTotal
if (!node->children) { 0 1461 1461
3113 0 return TB_ERR_MEM;
3114 }
3115 1461 next = &node->children[node->nchildren - 1];
3116 1461 memset(next, 0, sizeof(*next));
3117 1461 next->c = c;
3118 }
3119  
3120 // Continue
3121 5523 node = next;
3122 }
3123  
3124 1005 ▶ if (node->is_leaf) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->is_leaf) { 36 969 1005
3125 // Already a leaf here
3126 36 return TB_ERR_CAP_COLLISION;
3127 }
3128  
3129 969 node->is_leaf = 1;
3130 969 node->key = key;
3131 969 node->mod = mod;
3132 969 return TB_OK;
3133 }
3134  
3135 1 static int cap_trie_find(const char *buf, size_t nbuf, struct cap_trie **last,
3136 size_t *depth) {
3137 1 struct cap_trie *next, *node = &global.cap_trie;
3138 size_t i, j;
3139 1 *last = node;
3140 1 *depth = 0;
3141 3 ▶ for (i = 0; i < nbuf; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nbuf; i++) { 3 0 3
3142 3 char c = buf[i];
3143 3 next = NULL;
3144  
3145 // Find c in node.children
3146 16 ▶ for (j = 0; j < node->nchildren; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < node->nchildren; j++) { 16 0 16
3147 16 ▶ if (node->children[j].c == c) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->children[j].c == c) { 3 13 16
3148 3 next = &node->children[j];
3149 3 break;
3150 }
3151 }
3152 3 ▶ if (!next) {
Branch: 1 branch
BranchTrueFalseTotal
if (!next) { 0 3 3
3153 // Not found
3154 0 return TB_OK;
3155 }
3156 3 node = next;
3157 3 *last = node;
3158 3 *depth += 1;
3159 3 ▶ if (node->is_leaf && node->nchildren < 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (node->is_leaf && node->nchildren < 1) { 1 2 3
if (node->is_leaf && node->nchildren < 1) { 1 0 1
3160 1 break;
3161 }
3162 }
3163 1 return TB_OK;
3164 }
3165  
3166 1464 static int cap_trie_deinit(struct cap_trie *node) {
3167 size_t j;
3168 2925 ▶ for (j = 0; j < node->nchildren; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < node->nchildren; j++) { 1461 1464 2925
3169 1461 cap_trie_deinit(&node->children[j]);
3170 }
3171 1464 ▶ if (node->children) tb_free(node->children);
Branch: 1 branch
BranchTrueFalseTotal
if (node->children) tb_free(node->children); 495 969 1464
3172 1464 memset(node, 0, sizeof(*node));
3173 1464 return TB_OK;
3174 }
3175  
3176 #ifndef _WIN32
3177 3 static int init_resize_handler(void) {
3178 3 ▶ if (pipe(global.resize_pipefd) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pipe(global.resize_pipefd) != 0) { 0 3 3
3179 0 global.last_errno = errno;
3180 0 return TB_ERR_RESIZE_PIPE;
3181 }
3182  
3183 struct sigaction sa;
3184 3 memset(&sa, 0, sizeof(sa));
3185 3 sa.sa_handler = handle_resize;
3186 3 ▶ if (sigaction(SIGWINCH, &sa, NULL) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (sigaction(SIGWINCH, &sa, NULL) != 0) { 0 3 3
3187 0 global.last_errno = errno;
3188 0 return TB_ERR_RESIZE_SIGACTION;
3189 }
3190  
3191 3 return TB_OK;
3192 }
3193 #else
3194 /* The console reports a changed window size as an input record
3195 * (ENABLE_WINDOW_INPUT, see init_term_attrs), so there is nothing to set up. */
3196 static int init_resize_handler(void) {
3197 return TB_OK;
3198 }
3199 #endif
3200  
3201 3 static int send_init_escape_codes(void) {
3202 int rv;
3203 3 ▶ if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_ENTER_CA]));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_ENTER_CA])); 0 3 3
3204 3 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 3 3
3205 bytebuf_puts(&global.out, global.caps[TB_CAP_ENTER_KEYPAD]));
3206 3 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 3 3
3207 bytebuf_puts(&global.out, global.caps[TB_CAP_HIDE_CURSOR]));
3208 3 return TB_OK;
3209 }
3210  
3211 3 static int send_clear(void) {
3212 int rv;
3213  
3214 3 ▶ if_err_return(rv, send_attr(global.fg, global.bg));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_attr(global.fg, global.bg)); 0 3 3
3215 3 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 3 3
3216 bytebuf_puts(&global.out, global.caps[TB_CAP_CLEAR_SCREEN]));
3217  
3218 3 ▶ if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_cursor_if(global.cursor_x, global.cursor_y)); 0 3 3
3219 3 ▶ if_err_return(rv, bytebuf_flush(&global.out, global.wfd));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_flush(&global.out, global.wfd)); 0 3 3
3220  
3221 3 global.last_x = -1;
3222 3 global.last_y = -1;
3223  
3224 3 return TB_OK;
3225 }
3226  
3227 #ifndef _WIN32
3228 3 static int update_term_size(void) {
3229 int rv, ioctl_errno;
3230  
3231 3 ▶ if (global.ttyfd < 0) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (global.ttyfd < 0) return TB_OK; 3 0 3
3232  
3233 struct winsize sz;
3234 0 memset(&sz, 0, sizeof(sz));
3235  
3236 // Try ioctl TIOCGWINSZ
3237 0 ▶ if (ioctl(global.ttyfd, TIOCGWINSZ, &sz) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ioctl(global.ttyfd, TIOCGWINSZ, &sz) == 0) { 0 0 0
3238 0 global.width = sz.ws_col;
3239 0 global.height = sz.ws_row;
3240 0 return TB_OK;
3241 }
3242 0 ioctl_errno = errno;
3243  
3244 // Try >cursor(9999,9999), >u7, <u6
3245 0 ▶ if_ok_return(rv, update_term_size_via_esc());
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, update_term_size_via_esc()); 0 0 0
3246  
3247 0 global.last_errno = ioctl_errno;
3248 0 return TB_ERR_RESIZE_IOCTL;
3249 }
3250  
3251 0 static int update_term_size_via_esc(void) {
3252 #ifndef TB_RESIZE_FALLBACK_MS
3253 #define TB_RESIZE_FALLBACK_MS 1000
3254 #endif
3255  
3256 0 char move_and_report[] = "\x1b[9999;9999H\x1b[6n";
3257 ssize_t write_rv =
3258 0 write(global.wfd, move_and_report, strlen(move_and_report));
3259 0 ▶ if (write_rv != (ssize_t)strlen(move_and_report)) {
Branch: 1 branch
BranchTrueFalseTotal
if (write_rv != (ssize_t)strlen(move_and_report)) { 0 0 0
3260 0 return TB_ERR_RESIZE_WRITE;
3261 }
3262  
3263 fd_set fds;
3264 0 ▶ FD_ZERO(&fds);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&fds); 0 0 0
3265 0 FD_SET(global.rfd, &fds);
3266  
3267 struct timeval timeout;
3268 0 timeout.tv_sec = 0;
3269 0 timeout.tv_usec = TB_RESIZE_FALLBACK_MS * 1000;
3270  
3271 0 int select_rv = select(global.rfd + 1, &fds, NULL, NULL, &timeout);
3272  
3273 0 ▶ if (select_rv != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (select_rv != 1) { 0 0 0
3274 0 global.last_errno = errno;
3275 0 return TB_ERR_RESIZE_POLL;
3276 }
3277  
3278 char buf[TB_OPT_READ_BUF];
3279 0 ssize_t read_rv = read(global.rfd, buf, sizeof(buf) - 1);
3280 0 ▶ if (read_rv < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_rv < 1) { 0 0 0
3281 0 global.last_errno = errno;
3282 0 return TB_ERR_RESIZE_READ;
3283 }
3284 0 buf[read_rv] = '\0';
3285  
3286 int rw, rh;
3287 0 ▶ if (sscanf(buf, "\x1b[%d;%dR", &rh, &rw) != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (sscanf(buf, "\x1b[%d;%dR", &rh, &rw) != 2) { 0 0 0
3288 0 return TB_ERR_RESIZE_SSCANF;
3289 }
3290  
3291 0 global.width = rw;
3292 0 global.height = rh;
3293 0 return TB_OK;
3294 }
3295 #else
3296 /* The size of the console window, not of its screen buffer: a legacy console
3297 * keeps a scrollback buffer taller than the window, and VT output addresses
3298 * the window. */
3299 static int update_term_size(void) {
3300 CONSOLE_SCREEN_BUFFER_INFO info;
3301  
3302 if (!global.out_is_console) return TB_OK;
3303  
3304 if (!GetConsoleScreenBufferInfo(global.out_handle, &info)) {
3305 return win_error(TB_ERR_RESIZE_IOCTL);
3306 }
3307 global.width = info.srWindow.Right - info.srWindow.Left + 1;
3308 global.height = info.srWindow.Bottom - info.srWindow.Top + 1;
3309 return TB_OK;
3310 }
3311 #endif
3312  
3313 3 static int init_cellbuf(void) {
3314 int rv;
3315 3 ▶ if_err_return(rv, cellbuf_init(&global.back, global.width, global.height));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_init(&global.back, global.width, global.height)); 0 3 3
3316 3 ▶ if_err_return(rv, cellbuf_init(&global.front, global.width, global.height));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_init(&global.front, global.width, global.height)); 0 3 3
3317 3 ▶ if_err_return(rv, cellbuf_clear(&global.back));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_clear(&global.back)); 0 3 3
3318 3 ▶ if_err_return(rv, cellbuf_clear(&global.front));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_clear(&global.front)); 0 3 3
3319 3 return TB_OK;
3320 }
3321  
3322 3 static int tb_deinit(void) {
3323 3 ▶ if (global.caps[0] != NULL && global.wfd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (global.caps[0] != NULL && global.wfd >= 0) { 3 0 3
if (global.caps[0] != NULL && global.wfd >= 0) { 3 0 3
3324 3 bytebuf_puts(&global.out, global.caps[TB_CAP_SHOW_CURSOR]);
3325 3 bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]);
3326 3 bytebuf_puts(&global.out, global.caps[TB_CAP_CLEAR_SCREEN]);
3327 3 bytebuf_puts(&global.out, global.caps[TB_CAP_EXIT_CA]);
3328 3 bytebuf_puts(&global.out, global.caps[TB_CAP_EXIT_KEYPAD]);
3329 3 bytebuf_puts(&global.out, TB_HARDCAP_EXIT_MOUSE);
3330 3 bytebuf_flush(&global.out, global.wfd);
3331 }
3332 3 restore_term_attrs();
3333 3 close_opened_fds();
3334 3 deinit_resize_handler();
3335  
3336 3 cellbuf_free(&global.back);
3337 3 cellbuf_free(&global.front);
3338 3 bytebuf_free(&global.in);
3339 3 bytebuf_free(&global.out);
3340  
3341 3 ▶ if (global.terminfo) tb_free(global.terminfo);
Branch: 1 branch
BranchTrueFalseTotal
if (global.terminfo) tb_free(global.terminfo); 3 0 3
3342  
3343 3 cap_trie_deinit(&global.cap_trie);
3344  
3345 3 tb_reset();
3346 3 return TB_OK;
3347 }
3348  
3349 3 static int load_terminfo(void) {
3350 int rv;
3351 char tmp[TB_PATH_MAX];
3352  
3353 // See terminfo(5) "Fetching Compiled Descriptions" for a description of
3354 // this behavior. Some of these paths are compile-time ncurses options, so
3355 // best guesses are used here.
3356 3 const char *term = get_term_name();
3357 3 ▶ if (!term) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!term) return TB_ERR; 0 3 3
3358  
3359 // If TERMINFO is set, try that directory first
3360 3 const char *terminfo = getenv("TERMINFO");
3361 3 ▶ if (terminfo) if_ok_return(rv, load_terminfo_from_path(terminfo, term));
Branch: 2 branches
BranchTrueFalseTotal
if (terminfo) if_ok_return(rv, load_terminfo_from_path(terminfo, term)); 0 3 3
if (terminfo) if_ok_return(rv, load_terminfo_from_path(terminfo, term)); 0 0 0
3362  
3363 // Next try ~/.terminfo
3364 3 const char *home = getenv("HOME");
3365 3 ▶ if (home) {
Branch: 1 branch
BranchTrueFalseTotal
if (home) { 3 0 3
3366 3 ▶ snprintf_or_return(rv, tmp, sizeof(tmp), "%s/.terminfo", home);
Branch: 2 branches
BranchTrueFalseTotal
snprintf_or_return(rv, tmp, sizeof(tmp), "%s/.terminfo", home); 3 0 3
snprintf_or_return(rv, tmp, sizeof(tmp), "%s/.terminfo", home); 0 3 3
3367 3 ▶ if_ok_return(rv, load_terminfo_from_path(tmp, term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path(tmp, term)); 0 3 3
3368 }
3369  
3370 // Next try TERMINFO_DIRS
3371 //
3372 // Note, empty entries are supposed to be interpretted as the "compiled-in
3373 // default", which is of course system-dependent. Previously /etc/terminfo
3374 // was used here. Let's skip empty entries altogether rather than give
3375 // precedence to a guess, and check common paths after this loop.
3376 3 const char *dirs = getenv("TERMINFO_DIRS");
3377 3 ▶ if (dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (dirs) { 0 3 3
3378 0 ▶ snprintf_or_return(rv, tmp, sizeof(tmp), "%s", dirs);
Branch: 2 branches
BranchTrueFalseTotal
snprintf_or_return(rv, tmp, sizeof(tmp), "%s", dirs); 0 0 0
snprintf_or_return(rv, tmp, sizeof(tmp), "%s", dirs); 0 0 0
3379 0 char *dir = strtok(tmp, ":");
3380 0 ▶ while (dir) {
Branch: 1 branch
BranchTrueFalseTotal
while (dir) { 0 0 0
3381 0 const char *cdir = dir;
3382 0 ▶ if (*cdir != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (*cdir != '\0') { 0 0 0
3383 0 ▶ if_ok_return(rv, load_terminfo_from_path(cdir, term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path(cdir, term)); 0 0 0
3384 }
3385 0 dir = strtok(NULL, ":");
3386 }
3387 }
3388  
3389 #ifdef TB_TERMINFO_DIR
3390 if_ok_return(rv, load_terminfo_from_path(TB_TERMINFO_DIR, term));
3391 #endif
3392 3 ▶ if_ok_return(rv, load_terminfo_from_path("/usr/local/etc/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/usr/local/etc/terminfo", term)); 0 3 3
3393 3 ▶ if_ok_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, 0 3 3
3394 load_terminfo_from_path("/usr/local/share/terminfo", term));
3395 3 ▶ if_ok_return(rv, load_terminfo_from_path("/usr/local/lib/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/usr/local/lib/terminfo", term)); 0 3 3
3396 3 ▶ if_ok_return(rv, load_terminfo_from_path("/etc/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/etc/terminfo", term)); 0 3 3
3397 3 ▶ if_ok_return(rv, load_terminfo_from_path("/usr/share/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/usr/share/terminfo", term)); 3 0 3
3398 0 ▶ if_ok_return(rv, load_terminfo_from_path("/usr/lib/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/usr/lib/terminfo", term)); 0 0 0
3399 0 ▶ if_ok_return(rv, load_terminfo_from_path("/usr/share/lib/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/usr/share/lib/terminfo", term)); 0 0 0
3400 0 ▶ if_ok_return(rv, load_terminfo_from_path("/lib/terminfo", term));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, load_terminfo_from_path("/lib/terminfo", term)); 0 0 0
3401  
3402 0 return TB_ERR;
3403 }
3404  
3405 18 static int load_terminfo_from_path(const char *path, const char *term) {
3406 int rv;
3407 char tmp[TB_PATH_MAX];
3408  
3409 // Look for term at this terminfo location, e.g., <terminfo>/x/xterm
3410 18 ▶ snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term);
Branch: 2 branches
BranchTrueFalseTotal
snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term); 18 0 18
snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%c/%s", path, term[0], term); 0 18 18
3411 18 ▶ if_ok_return(rv, read_terminfo_path(tmp));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, read_terminfo_path(tmp)); 3 15 18
3412  
3413 #ifdef __APPLE__
3414 // Try the Darwin equivalent path, e.g., <terminfo>/78/xterm
3415 snprintf_or_return(rv, tmp, sizeof(tmp), "%s/%x/%s", path, term[0], term);
3416 return read_terminfo_path(tmp);
3417 #endif
3418  
3419 15 return TB_ERR;
3420 }
3421  
3422 18 static int read_terminfo_path(const char *path) {
3423 18 FILE *fp = fopen(path, "rb");
3424 18 ▶ if (!fp) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!fp) return TB_ERR; 15 3 18
3425  
3426 struct stat st;
3427 3 ▶ if (fstat(fileno(fp), &st) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fstat(fileno(fp), &st) != 0) { 0 3 3
3428 0 fclose(fp);
3429 0 return TB_ERR;
3430 }
3431  
3432 3 size_t fsize = st.st_size;
3433 3 char *data = (char *)tb_malloc(fsize + 1);
3434 3 ▶ if (!data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!data) { 0 3 3
3435 0 fclose(fp);
3436 0 return TB_ERR;
3437 }
3438  
3439 3 ▶ if (fread(data, 1, fsize, fp) != fsize) {
Branch: 1 branch
BranchTrueFalseTotal
if (fread(data, 1, fsize, fp) != fsize) { 0 3 3
3440 0 fclose(fp);
3441 0 tb_free(data);
3442 0 return TB_ERR;
3443 }
3444 3 data[fsize] = '\0';
3445  
3446 3 global.terminfo = data;
3447 3 global.nterminfo = fsize;
3448  
3449 3 fclose(fp);
3450 3 return TB_OK;
3451 }
3452  
3453 3 static int parse_terminfo_caps(void) {
3454 // See term(5) "LEGACY STORAGE FORMAT" and "EXTENDED STORAGE FORMAT" for a
3455 // description of this behavior.
3456  
3457 // Ensure there's at least a header's worth of data
3458 3 ▶ if (global.nterminfo < 6 * (int)sizeof(int16_t)) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (global.nterminfo < 6 * (int)sizeof(int16_t)) return TB_ERR; 0 3 3
3459  
3460 int16_t magic_number, nbytes_names, nbytes_bools, num_ints, num_offsets,
3461 nbytes_strings;
3462 3 size_t nbytes_header = 6 * sizeof(int16_t);
3463 // header[0] the magic number (octal 0432 or 01036)
3464 // header[1] the size, in bytes, of the names section
3465 // header[2] the number of bytes in the boolean section
3466 // header[3] the number of short integers in the numbers section
3467 // header[4] the number of offsets (short integers) in the strings section
3468 // header[5] the size, in bytes, of the string table
3469 3 get_terminfo_int16(0 * sizeof(int16_t), &magic_number);
3470 3 get_terminfo_int16(1 * sizeof(int16_t), &nbytes_names);
3471 3 get_terminfo_int16(2 * sizeof(int16_t), &nbytes_bools);
3472 3 get_terminfo_int16(3 * sizeof(int16_t), &num_ints);
3473 3 get_terminfo_int16(4 * sizeof(int16_t), &num_offsets);
3474 3 get_terminfo_int16(5 * sizeof(int16_t), &nbytes_strings);
3475  
3476 // Legacy ints are 16-bit, extended ints are 32-bit
3477 6 const int bytes_per_int = magic_number == 01036 ? 4 // 32-bit
3478 3 ▶ : 2; // 16-bit
Branch: 1 branch
BranchTrueFalseTotal
: 2; // 16-bit 0 3 3
3479  
3480 // > Between the boolean section and the number section, a null byte will
3481 // > be inserted, if necessary, to ensure that the number section begins on
3482 // > an even byte
3483 3 const int align_offset = (nbytes_names + nbytes_bools) % 2 != 0 ? 1 : 0;
3484  
3485 3 const int pos_str_offsets =
3486 nbytes_header // header (12 bytes)
3487 3 + nbytes_names // length of names section
3488 3 + nbytes_bools // length of boolean section
3489 3 + align_offset +
3490 3 (num_ints * bytes_per_int); // length of numbers section
3491  
3492 3 const int pos_str_table =
3493 3 pos_str_offsets +
3494 (num_offsets * sizeof(int16_t)); // length of string offsets table
3495  
3496 // Load caps
3497 int i;
3498 117 ▶ for (i = 0; i < TB_CAP__COUNT; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < TB_CAP__COUNT; i++) { 114 3 117
3499 114 const char *cap = get_terminfo_string(pos_str_offsets, num_offsets,
3500 114 pos_str_table, nbytes_strings, terminfo_cap_indexes[i]);
3501 114 ▶ if (!cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) { 0 114 114
3502 // Something is not right
3503 0 return TB_ERR;
3504 }
3505 114 global.caps[i] = cap;
3506 }
3507  
3508 3 return TB_OK;
3509 }
3510  
3511 0 static int load_builtin_caps(void) {
3512 int i, j;
3513 0 const char *term = get_term_name();
3514  
3515 0 ▶ if (!term) return TB_ERR_NO_TERM;
Branch: 1 branch
BranchTrueFalseTotal
if (!term) return TB_ERR_NO_TERM; 0 0 0
3516  
3517 // Check for exact TERM match
3518 0 ▶ for (i = 0; builtin_terms[i].name != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; builtin_terms[i].name != NULL; i++) { 0 0 0
3519 0 ▶ if (strcmp(term, builtin_terms[i].name) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(term, builtin_terms[i].name) == 0) { 0 0 0
3520 0 ▶ for (j = 0; j < TB_CAP__COUNT; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < TB_CAP__COUNT; j++) { 0 0 0
3521 0 global.caps[j] = builtin_terms[i].caps[j];
3522 }
3523 0 return TB_OK;
3524 }
3525 }
3526  
3527 // Check for partial TERM or alias match
3528 0 ▶ for (i = 0; builtin_terms[i].name != NULL; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; builtin_terms[i].name != NULL; i++) { 0 0 0
3529 0 ▶ if (strstr(term, builtin_terms[i].name) != NULL ||
Branch: 1 branch
BranchTrueFalseTotal
if (strstr(term, builtin_terms[i].name) != NULL || 0 0 0
3530 0 ▶ (*(builtin_terms[i].alias) != '\0' &&
Branch: 1 branch
BranchTrueFalseTotal
(*(builtin_terms[i].alias) != '\0' && 0 0 0
3531 0 ▶ strstr(term, builtin_terms[i].alias) != NULL))
Branch: 1 branch
BranchTrueFalseTotal
strstr(term, builtin_terms[i].alias) != NULL)) 0 0 0
3532 {
3533 0 ▶ for (j = 0; j < TB_CAP__COUNT; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < TB_CAP__COUNT; j++) { 0 0 0
3534 0 global.caps[j] = builtin_terms[i].caps[j];
3535 }
3536 0 return TB_OK;
3537 }
3538 }
3539  
3540 0 return TB_ERR_UNSUPPORTED_TERM;
3541 }
3542  
3543 114 static const char *get_terminfo_string(int16_t offsets_pos, int16_t offsets_len,
3544 int16_t table_pos, int16_t table_size, int16_t index) {
3545 114 ▶ if (index >= offsets_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (index >= offsets_len) { 0 114 114
3546 // An index beyond the offset table indicates absent
3547 // See `convert_strings` in tinfo `read_entry.c`
3548 0 return "";
3549 }
3550  
3551 int16_t table_offset;
3552 114 int table_offset_offset = (int)offsets_pos + (index * (int)sizeof(int16_t));
3553 114 ▶ if (get_terminfo_int16(table_offset_offset, &table_offset) != TB_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (get_terminfo_int16(table_offset_offset, &table_offset) != TB_OK) { 0 114 114
3554 // offset beyond end of terminfo entry
3555 // Truncated/corrupt terminfo entry?
3556 0 return NULL;
3557 }
3558  
3559 114 ▶ if (table_offset < 0 || table_offset >= table_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (table_offset < 0 || table_offset >= table_size) { 114 0 114
if (table_offset < 0 || table_offset >= table_size) { 0 114 114
3560 // A negative offset indicates absent
3561 // An offset beyond the string table indicates absent
3562 // See `convert_strings` in tinfo `read_entry.c`
3563 0 return "";
3564 }
3565  
3566 114 int str_offset = (int)table_pos + (int)table_offset;
3567 114 ▶ if (str_offset >= (int)global.nterminfo) {
Branch: 1 branch
BranchTrueFalseTotal
if (str_offset >= (int)global.nterminfo) { 0 114 114
3568 // string beyond end of terminfo entry
3569 // Truncated/corrupt terminfo entry?
3570 0 return NULL;
3571 }
3572  
3573 114 return (const char *)(global.terminfo + str_offset);
3574 }
3575  
3576 132 static int get_terminfo_int16(int offset, int16_t *val) {
3577 132 ▶ if (offset < 0 || offset + sizeof(int16_t) > global.nterminfo) {
Branch: 2 branches
BranchTrueFalseTotal
if (offset < 0 || offset + sizeof(int16_t) > global.nterminfo) { 132 0 132
if (offset < 0 || offset + sizeof(int16_t) > global.nterminfo) { 0 132 132
3578 0 *val = -1;
3579 0 return TB_ERR;
3580 }
3581 132 memcpy(val, global.terminfo + offset, sizeof(int16_t));
3582 132 return TB_OK;
3583 }
3584  
3585 66 static int wait_event(struct tb_event *event, int timeout) {
3586 int rv, resized;
3587  
3588 66 memset(event, 0, sizeof(*event));
3589 66 ▶ if_ok_return(rv, extract_event(event));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, extract_event(event)); 8 58 66
3590  
3591 do {
3592 58 ▶ if_err_return(rv, wait_input(timeout, &resized));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, wait_input(timeout, &resized)); 29 29 58
3593  
3594 29 ▶ if (resized) {
Branch: 1 branch
BranchTrueFalseTotal
if (resized) { 0 29 29
3595 // TODO: Harden against errors encountered mid-resize
3596 0 ▶ if_err_return(rv, update_term_size());
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, update_term_size()); 0 0 0
3597 0 ▶ if_err_return(rv, resize_cellbufs());
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, resize_cellbufs()); 0 0 0
3598 0 event->type = TB_EVENT_RESIZE;
3599 0 event->w = global.width;
3600 0 event->h = global.height;
3601 0 return TB_OK;
3602 }
3603  
3604 29 memset(event, 0, sizeof(*event));
3605 29 ▶ if_ok_return(rv, extract_event(event));
Branch: 1 branch
BranchTrueFalseTotal
if_ok_return(rv, extract_event(event)); 27 2 29
3606 2 ▶ } while (timeout == -1);
Branch: 1 branch
BranchTrueFalseTotal
} while (timeout == -1); 0 2 2
3607  
3608 2 return rv;
3609 }
3610  
3611 95 static int extract_event(struct tb_event *event) {
3612 int rv;
3613 95 struct bytebuf *in = &global.in;
3614  
3615 95 ▶ if (in->len == 0) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (in->len == 0) return TB_ERR; 56 39 95
3616  
3617 39 ▶ if (in->buf[0] == '\x1b') {
Branch: 1 branch
BranchTrueFalseTotal
if (in->buf[0] == '\x1b') { 37 2 39
3618 // Escape sequence?
3619 // In TB_INPUT_ESC, skip if the buffer is a single escape char
3620 37 ▶ if (!((global.input_mode & TB_INPUT_ESC) && in->len == 1)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!((global.input_mode & TB_INPUT_ESC) && in->len == 1)) { 37 0 37
if (!((global.input_mode & TB_INPUT_ESC) && in->len == 1)) { 36 1 37
3621 36 ▶ if_ok_or_need_more_return(rv, extract_esc(event));
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, extract_esc(event)); 4 32 36
if_ok_or_need_more_return(rv, extract_esc(event)); 4 0 4
3622 }
3623  
3624 // Escape key?
3625 1 ▶ if (global.input_mode & TB_INPUT_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if (global.input_mode & TB_INPUT_ESC) { 1 0 1
3626 1 event->type = TB_EVENT_KEY;
3627 1 event->ch = 0;
3628 1 event->key = TB_KEY_ESC;
3629 1 event->mod = 0;
3630 1 bytebuf_shift(in, 1);
3631 1 return TB_OK;
3632 }
3633  
3634 // Recurse for alt key
3635 0 event->mod |= TB_MOD_ALT;
3636 0 bytebuf_shift(in, 1);
3637 0 return extract_event(event);
3638 }
3639  
3640 // ASCII control key?
3641 2 int is_ctrl =
3642 2 ▶ (uint16_t)in->buf[0] < TB_KEY_SPACE || in->buf[0] == TB_KEY_BACKSPACE2;
Branch: 2 branches
BranchTrueFalseTotal
(uint16_t)in->buf[0] < TB_KEY_SPACE || in->buf[0] == TB_KEY_BACKSPACE2; 2 0 2
(uint16_t)in->buf[0] < TB_KEY_SPACE || in->buf[0] == TB_KEY_BACKSPACE2; 0 2 2
3643 2 ▶ if (is_ctrl) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_ctrl) { 0 2 2
3644 0 event->type = TB_EVENT_KEY;
3645 0 event->ch = 0;
3646 0 event->key = (uint16_t)in->buf[0];
3647 0 event->mod |= TB_MOD_CTRL;
3648 0 bytebuf_shift(in, 1);
3649 0 return TB_OK;
3650 }
3651  
3652 // UTF-8?
3653 2 ▶ if (in->len >= (size_t)tb_utf8_char_length(in->buf[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (in->len >= (size_t)tb_utf8_char_length(in->buf[0])) { 2 0 2
3654 2 event->type = TB_EVENT_KEY;
3655 2 tb_utf8_char_to_unicode(&event->ch, in->buf);
3656 2 event->key = 0;
3657 2 bytebuf_shift(in, tb_utf8_char_length(in->buf[0]));
3658 2 return TB_OK;
3659 }
3660  
3661 // Need more input
3662 0 return TB_ERR;
3663 }
3664  
3665 36 static int extract_esc(struct tb_event *event) {
3666 int rv;
3667 36 ▶ if_ok_or_need_more_return(rv, extract_esc_user(event, 0));
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, extract_esc_user(event, 0)); 5 31 36
if_ok_or_need_more_return(rv, extract_esc_user(event, 0)); 4 1 5
3668 1 ▶ if_ok_or_need_more_return(rv, extract_esc_cap(event));
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, extract_esc_cap(event)); 0 1 1
if_ok_or_need_more_return(rv, extract_esc_cap(event)); 0 0 0
3669 0 ▶ if_ok_or_need_more_return(rv, extract_esc_mouse(event));
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, extract_esc_mouse(event)); 0 0 0
if_ok_or_need_more_return(rv, extract_esc_mouse(event)); 0 0 0
3670 0 ▶ if_ok_or_need_more_return(rv, extract_esc_user(event, 1));
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, extract_esc_user(event, 1)); 0 0 0
if_ok_or_need_more_return(rv, extract_esc_user(event, 1)); 0 0 0
3671 0 return TB_ERR;
3672 }
3673  
3674 36 static int extract_esc_user(struct tb_event *event, int is_post) {
3675 int rv;
3676 36 size_t consumed = 0;
3677 36 struct bytebuf *in = &global.in;
3678 int (*fn)(struct tb_event *, size_t *);
3679  
3680 36 ▶ fn = is_post ? global.fn_extract_esc_post : global.fn_extract_esc_pre;
Branch: 1 branch
BranchTrueFalseTotal
fn = is_post ? global.fn_extract_esc_post : global.fn_extract_esc_pre; 0 36 36
3681  
3682 36 ▶ if (!fn) return TB_ERR;
Branch: 1 branch
BranchTrueFalseTotal
if (!fn) return TB_ERR; 0 36 36
3683  
3684 36 rv = fn(event, &consumed);
3685 36 ▶ if (rv == TB_OK) bytebuf_shift(in, consumed);
Branch: 1 branch
BranchTrueFalseTotal
if (rv == TB_OK) bytebuf_shift(in, consumed); 31 5 36
3686  
3687 36 ▶ if_ok_or_need_more_return(rv, rv);
Branch: 2 branches
BranchTrueFalseTotal
if_ok_or_need_more_return(rv, rv); 5 31 36
if_ok_or_need_more_return(rv, rv); 4 1 5
3688 1 return TB_ERR;
3689 }
3690  
3691 1 static int extract_esc_cap(struct tb_event *event) {
3692 int rv;
3693 1 struct bytebuf *in = &global.in;
3694 struct cap_trie *node;
3695 size_t depth;
3696  
3697 1 ▶ if_err_return(rv, cap_trie_find(in->buf, in->len, &node, &depth));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cap_trie_find(in->buf, in->len, &node, &depth)); 0 1 1
3698 1 ▶ if (node->is_leaf) {
Branch: 1 branch
BranchTrueFalseTotal
if (node->is_leaf) { 1 0 1
3699 // Found a leaf node
3700 1 event->type = TB_EVENT_KEY;
3701 1 event->ch = 0;
3702 1 event->key = node->key;
3703 1 event->mod = node->mod;
3704 1 bytebuf_shift(in, depth);
3705 1 return TB_OK;
3706 0 ▶ } else if (node->nchildren > 0 && in->len <= depth) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (node->nchildren > 0 && in->len <= depth) { 0 0 0
} else if (node->nchildren > 0 && in->len <= depth) { 0 0 0
3707 // Found a branch node (not enough input)
3708 0 return TB_ERR_NEED_MORE;
3709 }
3710  
3711 0 return TB_ERR;
3712 }
3713  
3714 0 static int extract_esc_mouse(struct tb_event *event) {
3715 0 struct bytebuf *in = &global.in;
3716 0 size_t buf_shift = 0;
3717  
3718 // Bail if not enough to determine type
3719 0 ▶ if (in->len < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (in->len < 2) { 0 0 0
3720 0 return TB_ERR_NEED_MORE;
3721 0 ▶ } else if (in->buf[1] != '[') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (in->buf[1] != '[') { 0 0 0
3722 0 return TB_ERR;
3723 0 ▶ } else if (in->len < 3) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (in->len < 3) { 0 0 0
3724 0 return TB_ERR_NEED_MORE;
3725 }
3726  
3727 // Discern type of mouse event from 3rd byte
3728 0 int type = 0;
3729 enum { TYPE_VT200 = 0, TYPE_1006, TYPE_1015, TYPE_MAX };
3730 0 ▶ if (in->buf[2] == 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if (in->buf[2] == 'M') { 0 0 0
3731 // X10 mouse encoding, the simplest one
3732 // \x1b [ M Cb Cx Cy
3733 0 type = TYPE_VT200;
3734 0 ▶ } else if (in->buf[2] == '<') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (in->buf[2] == '<') { 0 0 0
3735 // xterm 1006 extended mode or urxvt 1015 extended mode
3736 // xterm: \x1b [ < Cb ; Cx ; Cy (M or m)
3737 0 type = TYPE_1006;
3738 } else {
3739 // urxvt: \x1b [ Cb ; Cx ; Cy M
3740 0 type = TYPE_1015;
3741 }
3742  
3743 0 switch (type) {
3744 0 case TYPE_VT200: {
3745 // In this mode, we need 6 bytes
3746 0 ▶ if (in->len < 6) return TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (in->len < 6) return TB_ERR_NEED_MORE; 0 0 0
3747  
3748 0 int b = in->buf[3] - 0x20;
3749  
3750 0 switch (b & 3) {
3751 0 case 0:
3752 0 ▶ event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
Branch: 1 branch
BranchTrueFalseTotal
event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP 0 0 0
3753 : TB_KEY_MOUSE_LEFT;
3754 0 break;
3755 0 case 1:
3756 0 ▶ event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
Branch: 1 branch
BranchTrueFalseTotal
event->key = ((b & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN 0 0 0
3757 : TB_KEY_MOUSE_MIDDLE;
3758 0 break;
3759 0 case 2:
3760 0 event->key = TB_KEY_MOUSE_RIGHT;
3761 0 break;
3762 0 case 3:
3763 0 event->key = TB_KEY_MOUSE_RELEASE;
3764 0 break;
3765 0 default:
3766 0 return TB_ERR;
3767 }
3768  
3769 0 ▶ if ((b & 32) != 0) event->mod |= TB_MOD_MOTION;
Branch: 1 branch
BranchTrueFalseTotal
if ((b & 32) != 0) event->mod |= TB_MOD_MOTION; 0 0 0
3770  
3771 // the coord is 1,1 for upper left
3772 0 event->x = ((uint8_t)in->buf[4]) - 0x21;
3773 0 event->y = ((uint8_t)in->buf[5]) - 0x21;
3774  
3775 // Eat 6 bytes
3776 0 buf_shift = 6;
3777 0 break;
3778 }
3779  
3780 0 case TYPE_1006:
3781 // fallthrough
3782  
3783 case TYPE_1015: {
3784 0 int num[3] = {-1, -1, -1};
3785 0 int num_i = 0;
3786 0 int cur_num = -1;
3787 0 char trail = ' ';
3788  
3789 0 size_t i = 2;
3790 0 ▶ if (type == TYPE_1006) ++i; // skip '<'
Branch: 1 branch
BranchTrueFalseTotal
if (type == TYPE_1006) ++i; // skip '<' 0 0 0
3791  
3792 // Parse %d;%d;%d[mM] into `num`
3793 0 ▶ while (i < in->len && num_i < 3) {
Branch: 2 branches
BranchTrueFalseTotal
while (i < in->len && num_i < 3) { 0 0 0
while (i < in->len && num_i < 3) { 0 0 0
3794 0 char c = in->buf[i];
3795 0 ▶ if (c >= '0' && c <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') { 0 0 0
if (c >= '0' && c <= '9') { 0 0 0
3796 // Digit
3797 0 ▶ if (cur_num == -1) cur_num = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (cur_num == -1) cur_num = 0; 0 0 0
3798 0 cur_num *= 10;
3799 0 cur_num += (int)(c - '0');
3800 0 ▶ } else if (cur_num != -1 &&
Branch: 2 branches
BranchTrueFalseTotal
} else if (cur_num != -1 && 0 0 0
} else if (cur_num != -1 && 0 0 0
3801 0 ▶ ((num_i < 2 && c == ';') ||
Branch: 2 branches
BranchTrueFalseTotal
((num_i < 2 && c == ';') || 0 0 0
((num_i < 2 && c == ';') || 0 0 0
3802 0 ▶ (num_i == 2 && (c == 'm' || c == 'M'))))
Branch: 2 branches
BranchTrueFalseTotal
(num_i == 2 && (c == 'm' || c == 'M')))) 0 0 0
(num_i == 2 && (c == 'm' || c == 'M')))) 0 0 0
3803 {
3804 // We're at a semi-colon, 'm', or 'M'
3805 // and we have a number
3806 0 num[num_i] = cur_num;
3807 0 ++num_i;
3808 0 cur_num = -1;
3809 0 trail = c;
3810 } else {
3811 // Something else; not a mouse event
3812 0 return TB_ERR;
3813 }
3814 0 ++i;
3815 }
3816  
3817 // If we didn't get to the 3rd number, we need more
3818 0 ▶ if (num[2] == -1) return TB_ERR_NEED_MORE;
Branch: 1 branch
BranchTrueFalseTotal
if (num[2] == -1) return TB_ERR_NEED_MORE; 0 0 0
3819  
3820 // We have a valid mouse event, eat `i` bytes from the buffer
3821 0 buf_shift = i;
3822  
3823 0 ▶ if (type == TYPE_1015) num[0] -= 0x20;
Branch: 1 branch
BranchTrueFalseTotal
if (type == TYPE_1015) num[0] -= 0x20; 0 0 0
3824  
3825 0 switch (num[0] & 3) {
3826 0 case 0:
3827 0 ▶ event->key = ((num[0] & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP
Branch: 1 branch
BranchTrueFalseTotal
event->key = ((num[0] & 64) != 0) ? TB_KEY_MOUSE_WHEEL_UP 0 0 0
3828 : TB_KEY_MOUSE_LEFT;
3829 0 break;
3830 0 case 1:
3831 0 ▶ event->key = ((num[0] & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN
Branch: 1 branch
BranchTrueFalseTotal
event->key = ((num[0] & 64) != 0) ? TB_KEY_MOUSE_WHEEL_DOWN 0 0 0
3832 : TB_KEY_MOUSE_MIDDLE;
3833 0 break;
3834 0 case 2:
3835 0 event->key = TB_KEY_MOUSE_RIGHT;
3836 0 break;
3837 0 case 3:
3838 0 event->key = TB_KEY_MOUSE_RELEASE;
3839 0 break;
3840 0 default:
3841 0 return TB_ERR;
3842 }
3843  
3844 // on xterm mouse release is signaled by lowercase m
3845 0 ▶ if (trail == 'm') event->key = TB_KEY_MOUSE_RELEASE;
Branch: 1 branch
BranchTrueFalseTotal
if (trail == 'm') event->key = TB_KEY_MOUSE_RELEASE; 0 0 0
3846  
3847 0 ▶ if ((num[0] & 32) != 0) event->mod |= TB_MOD_MOTION;
Branch: 1 branch
BranchTrueFalseTotal
if ((num[0] & 32) != 0) event->mod |= TB_MOD_MOTION; 0 0 0
3848  
3849 0 event->x = (num[1] - 1 < 0) ? 0 : num[1] - 1;
3850 0 event->y = (num[2] - 1 < 0) ? 0 : num[2] - 1;
3851  
3852 0 break;
3853 }
3854 }
3855  
3856 0 ▶ if (buf_shift > 0) bytebuf_shift(in, buf_shift);
Branch: 1 branch
BranchTrueFalseTotal
if (buf_shift > 0) bytebuf_shift(in, buf_shift); 0 0 0
3857  
3858 0 event->type = TB_EVENT_MOUSE;
3859  
3860 0 return TB_OK;
3861 }
3862  
3863 0 static int resize_cellbufs(void) {
3864 int rv;
3865 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3866 cellbuf_resize(&global.back, global.width, global.height));
3867 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3868 cellbuf_resize(&global.front, global.width, global.height));
3869 0 ▶ if_err_return(rv, cellbuf_clear(&global.front));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_clear(&global.front)); 0 0 0
3870 0 ▶ if_err_return(rv, send_clear());
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_clear()); 0 0 0
3871 0 return TB_OK;
3872 }
3873  
3874 #ifndef _WIN32
3875 0 static void handle_resize(int sig) {
3876 0 int errno_copy = errno;
3877 0 write(global.resize_pipefd[1], &sig, sizeof(sig));
3878 0 errno = errno_copy;
3879 0 }
3880 #endif
3881  
3882 3 static int send_attr(uintattr_t fg, uintattr_t bg) {
3883 int rv;
3884  
3885 3 ▶ if (fg == global.last_fg && bg == global.last_bg) {
Branch: 2 branches
BranchTrueFalseTotal
if (fg == global.last_fg && bg == global.last_bg) { 0 3 3
if (fg == global.last_fg && bg == global.last_bg) { 0 0 0
3886 0 return TB_OK;
3887 }
3888  
3889 3 ▶ if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0]));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_SGR0])); 0 3 3
3890  
3891 uint32_t cfg, cbg;
3892 3 switch (global.output_mode) {
3893 3 default:
3894 case TB_OUTPUT_NORMAL:
3895 // The minus 1 below is because our colors are 1-indexed starting
3896 // from black. Black is represented by a 30, 40, 90, or 100 for fg,
3897 // bg, bright fg, or bright bg respectively. Red is 31, 41, 91,
3898 // 101, etc.
3899 3 ▶ cfg = (fg & TB_BRIGHT ? 90 : 30) + (fg & 0x0f) - 1;
Branch: 1 branch
BranchTrueFalseTotal
cfg = (fg & TB_BRIGHT ? 90 : 30) + (fg & 0x0f) - 1; 0 3 3
3900 3 ▶ cbg = (bg & TB_BRIGHT ? 100 : 40) + (bg & 0x0f) - 1;
Branch: 1 branch
BranchTrueFalseTotal
cbg = (bg & TB_BRIGHT ? 100 : 40) + (bg & 0x0f) - 1; 0 3 3
3901 3 break;
3902  
3903 0 case TB_OUTPUT_256:
3904 0 cfg = fg & 0xff;
3905 0 cbg = bg & 0xff;
3906 0 ▶ if (fg & TB_HI_BLACK) cfg = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_HI_BLACK) cfg = 0; 0 0 0
3907 0 ▶ if (bg & TB_HI_BLACK) cbg = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bg & TB_HI_BLACK) cbg = 0; 0 0 0
3908 0 break;
3909  
3910 0 case TB_OUTPUT_216:
3911 0 cfg = fg & 0xff;
3912 0 cbg = bg & 0xff;
3913 0 ▶ if (cfg > 216) cfg = 216;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg > 216) cfg = 216; 0 0 0
3914 0 ▶ if (cbg > 216) cbg = 216;
Branch: 1 branch
BranchTrueFalseTotal
if (cbg > 216) cbg = 216; 0 0 0
3915 0 cfg += 0x0f;
3916 0 cbg += 0x0f;
3917 0 break;
3918  
3919 0 case TB_OUTPUT_GRAYSCALE:
3920 0 cfg = fg & 0xff;
3921 0 cbg = bg & 0xff;
3922 0 ▶ if (cfg > 24) cfg = 24;
Branch: 1 branch
BranchTrueFalseTotal
if (cfg > 24) cfg = 24; 0 0 0
3923 0 ▶ if (cbg > 24) cbg = 24;
Branch: 1 branch
BranchTrueFalseTotal
if (cbg > 24) cbg = 24; 0 0 0
3924 0 cfg += 0xe7;
3925 0 cbg += 0xe7;
3926 0 break;
3927  
3928 #if TB_OPT_ATTR_W >= 32
3929 0 case TB_OUTPUT_TRUECOLOR:
3930 0 cfg = fg & 0xffffff;
3931 0 cbg = bg & 0xffffff;
3932 0 ▶ if (fg & TB_HI_BLACK) cfg = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_HI_BLACK) cfg = 0; 0 0 0
3933 0 ▶ if (bg & TB_HI_BLACK) cbg = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bg & TB_HI_BLACK) cbg = 0; 0 0 0
3934 0 break;
3935 #endif
3936 }
3937  
3938 3 ▶ if (fg & TB_BOLD)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_BOLD) 0 3 3
3939 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BOLD]));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BOLD])); 0 0 0
3940  
3941 3 ▶ if (fg & TB_BLINK)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_BLINK) 0 3 3
3942 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BLINK]));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_BLINK])); 0 0 0
3943  
3944 3 ▶ if (fg & TB_UNDERLINE)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_UNDERLINE) 0 3 3
3945 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3946 bytebuf_puts(&global.out, global.caps[TB_CAP_UNDERLINE]));
3947  
3948 3 ▶ if (fg & TB_ITALIC)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_ITALIC) 0 3 3
3949 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3950 bytebuf_puts(&global.out, global.caps[TB_CAP_ITALIC]));
3951  
3952 3 ▶ if (fg & TB_DIM)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_DIM) 0 3 3
3953 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_DIM]));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, global.caps[TB_CAP_DIM])); 0 0 0
3954  
3955 #if TB_OPT_ATTR_W == 64
3956 3 ▶ if (fg & TB_STRIKEOUT)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_STRIKEOUT) 0 3 3
3957 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_STRIKEOUT));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_STRIKEOUT)); 0 0 0
3958  
3959 3 ▶ if (fg & TB_UNDERLINE_2)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_UNDERLINE_2) 0 3 3
3960 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_UNDERLINE_2));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_UNDERLINE_2)); 0 0 0
3961  
3962 3 ▶ if (fg & TB_OVERLINE)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_OVERLINE) 0 3 3
3963 0 ▶ if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_OVERLINE));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_puts(&global.out, TB_HARDCAP_OVERLINE)); 0 0 0
3964  
3965 3 ▶ if (fg & TB_INVISIBLE)
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_INVISIBLE) 0 3 3
3966 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3967 bytebuf_puts(&global.out, global.caps[TB_CAP_INVISIBLE]));
3968 #endif
3969  
3970 3 ▶ if ((fg & TB_REVERSE) || (bg & TB_REVERSE))
Branch: 2 branches
BranchTrueFalseTotal
if ((fg & TB_REVERSE) || (bg & TB_REVERSE)) 3 0 3
if ((fg & TB_REVERSE) || (bg & TB_REVERSE)) 0 3 3
3971 0 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 0 0
3972 bytebuf_puts(&global.out, global.caps[TB_CAP_REVERSE]));
3973  
3974 3 int fg_is_default = (fg & 0xff) == 0;
3975 3 int bg_is_default = (bg & 0xff) == 0;
3976 3 ▶ if (global.output_mode == TB_OUTPUT_256) {
Branch: 1 branch
BranchTrueFalseTotal
if (global.output_mode == TB_OUTPUT_256) { 0 3 3
3977 0 ▶ if (fg & TB_HI_BLACK) fg_is_default = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (fg & TB_HI_BLACK) fg_is_default = 0; 0 0 0
3978 0 ▶ if (bg & TB_HI_BLACK) bg_is_default = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (bg & TB_HI_BLACK) bg_is_default = 0; 0 0 0
3979 }
3980 #if TB_OPT_ATTR_W >= 32
3981 3 ▶ if (global.output_mode == TB_OUTPUT_TRUECOLOR) {
Branch: 1 branch
BranchTrueFalseTotal
if (global.output_mode == TB_OUTPUT_TRUECOLOR) { 0 3 3
3982 0 ▶ fg_is_default = ((fg & 0xffffff) == 0) && ((fg & TB_HI_BLACK) == 0);
Branch: 2 branches
BranchTrueFalseTotal
fg_is_default = ((fg & 0xffffff) == 0) && ((fg & TB_HI_BLACK) == 0); 0 0 0
fg_is_default = ((fg & 0xffffff) == 0) && ((fg & TB_HI_BLACK) == 0); 0 0 0
3983 0 ▶ bg_is_default = ((bg & 0xffffff) == 0) && ((bg & TB_HI_BLACK) == 0);
Branch: 2 branches
BranchTrueFalseTotal
bg_is_default = ((bg & 0xffffff) == 0) && ((bg & TB_HI_BLACK) == 0); 0 0 0
bg_is_default = ((bg & 0xffffff) == 0) && ((bg & TB_HI_BLACK) == 0); 0 0 0
3984 }
3985 #endif
3986  
3987 3 ▶ if_err_return(rv, send_sgr(cfg, cbg, fg_is_default, bg_is_default));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_sgr(cfg, cbg, fg_is_default, bg_is_default)); 0 3 3
3988  
3989 3 global.last_fg = fg;
3990 3 global.last_bg = bg;
3991  
3992 3 return TB_OK;
3993 }
3994  
3995 3 static int send_sgr(uint32_t cfg, uint32_t cbg, int fg_is_default,
3996 int bg_is_default) {
3997 int rv;
3998 char nbuf[32];
3999  
4000 3 ▶ if (fg_is_default && bg_is_default) {
Branch: 2 branches
BranchTrueFalseTotal
if (fg_is_default && bg_is_default) { 3 0 3
if (fg_is_default && bg_is_default) { 3 0 3
4001 3 return TB_OK;
4002 }
4003  
4004 0 switch (global.output_mode) {
4005 0 default:
4006 case TB_OUTPUT_NORMAL:
4007 0 ▶ send_literal(rv, "\x1b[");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "\x1b["); 0 0 0
4008 0 ▶ if (!fg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fg_is_default) { 0 0 0
4009 0 ▶ send_num(rv, nbuf, cfg);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cfg); 0 0 0
4010 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4011 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4012 }
4013 }
4014 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4015 0 ▶ send_num(rv, nbuf, cbg);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cbg); 0 0 0
4016 }
4017 0 ▶ send_literal(rv, "m");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "m"); 0 0 0
4018 0 break;
4019  
4020 0 case TB_OUTPUT_256:
4021 case TB_OUTPUT_216:
4022 case TB_OUTPUT_GRAYSCALE:
4023 0 ▶ send_literal(rv, "\x1b[");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "\x1b["); 0 0 0
4024 0 ▶ if (!fg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fg_is_default) { 0 0 0
4025 0 ▶ send_literal(rv, "38;5;");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "38;5;"); 0 0 0
4026 0 ▶ send_num(rv, nbuf, cfg);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cfg); 0 0 0
4027 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4028 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4029 }
4030 }
4031 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4032 0 ▶ send_literal(rv, "48;5;");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "48;5;"); 0 0 0
4033 0 ▶ send_num(rv, nbuf, cbg);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cbg); 0 0 0
4034 }
4035 0 ▶ send_literal(rv, "m");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "m"); 0 0 0
4036 0 break;
4037  
4038 #if TB_OPT_ATTR_W >= 32
4039 0 case TB_OUTPUT_TRUECOLOR:
4040 0 ▶ send_literal(rv, "\x1b[");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "\x1b["); 0 0 0
4041 0 ▶ if (!fg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fg_is_default) { 0 0 0
4042 0 ▶ send_literal(rv, "38;2;");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "38;2;"); 0 0 0
4043 0 ▶ send_num(rv, nbuf, (cfg >> 16) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, (cfg >> 16) & 0xff); 0 0 0
4044 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4045 0 ▶ send_num(rv, nbuf, (cfg >> 8) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, (cfg >> 8) & 0xff); 0 0 0
4046 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4047 0 ▶ send_num(rv, nbuf, cfg & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cfg & 0xff); 0 0 0
4048 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4049 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4050 }
4051 }
4052 0 ▶ if (!bg_is_default) {
Branch: 1 branch
BranchTrueFalseTotal
if (!bg_is_default) { 0 0 0
4053 0 ▶ send_literal(rv, "48;2;");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "48;2;"); 0 0 0
4054 0 ▶ send_num(rv, nbuf, (cbg >> 16) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, (cbg >> 16) & 0xff); 0 0 0
4055 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4056 0 ▶ send_num(rv, nbuf, (cbg >> 8) & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, (cbg >> 8) & 0xff); 0 0 0
4057 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4058 0 ▶ send_num(rv, nbuf, cbg & 0xff);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, cbg & 0xff); 0 0 0
4059 }
4060 0 ▶ send_literal(rv, "m");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "m"); 0 0 0
4061 0 break;
4062 #endif
4063 }
4064 0 return TB_OK;
4065 }
4066  
4067 3 static int send_cursor_if(int x, int y) {
4068 int rv;
4069 char nbuf[32];
4070 3 ▶ if (x < 0 || y < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (x < 0 || y < 0) { 0 3 3
if (x < 0 || y < 0) { 0 0 0
4071 3 return TB_OK;
4072 }
4073 0 ▶ send_literal(rv, "\x1b[");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "\x1b["); 0 0 0
4074 0 ▶ send_num(rv, nbuf, y + 1);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, y + 1); 0 0 0
4075 0 ▶ send_literal(rv, ";");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, ";"); 0 0 0
4076 0 ▶ send_num(rv, nbuf, x + 1);
Branch: 1 branch
BranchTrueFalseTotal
send_num(rv, nbuf, x + 1); 0 0 0
4077 0 ▶ send_literal(rv, "H");
Branch: 1 branch
BranchTrueFalseTotal
send_literal(rv, "H"); 0 0 0
4078 0 return TB_OK;
4079 }
4080  
4081 0 static int send_char(int x, int y, uint32_t ch) {
4082 0 return send_cluster(x, y, &ch, 1);
4083 }
4084  
4085 0 static int send_cluster(int x, int y, uint32_t *ch, size_t nch) {
4086 int rv;
4087 char chu8[8];
4088  
4089 0 ▶ if (global.last_x != x - 1 || global.last_y != y) {
Branch: 2 branches
BranchTrueFalseTotal
if (global.last_x != x - 1 || global.last_y != y) { 0 0 0
if (global.last_x != x - 1 || global.last_y != y) { 0 0 0
4090 0 ▶ if_err_return(rv, send_cursor_if(x, y));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, send_cursor_if(x, y)); 0 0 0
4091 }
4092 0 global.last_x = x;
4093 0 global.last_y = y;
4094  
4095 int i;
4096 0 ▶ for (i = 0; i < (int)nch; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < (int)nch; i++) { 0 0 0
4097 0 uint32_t ch32 = *(ch + i);
4098 0 ▶ if (!tb_iswprint(ch32)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tb_iswprint(ch32)) { 0 0 0
4099 0 ch32 = 0xfffd; // replace non-printable codepoints with U+FFFD
4100 }
4101 0 int chu8_len = tb_utf8_unicode_to_char(chu8, ch32);
4102 0 ▶ if_err_return(rv, bytebuf_nputs(&global.out, chu8, (size_t)chu8_len));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_nputs(&global.out, chu8, (size_t)chu8_len)); 0 0 0
4103 }
4104  
4105 0 return TB_OK;
4106 }
4107  
4108 0 static int convert_num(uint32_t num, char *buf) {
4109 0 int i, l = 0;
4110 char ch;
4111 do {
4112 0 buf[l++] = (char)('0' + (num % 10));
4113 0 num /= 10;
4114 0 ▶ } while (num);
Branch: 1 branch
BranchTrueFalseTotal
} while (num); 0 0 0
4115 0 ▶ for (i = 0; i < l / 2; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < l / 2; i++) { 0 0 0
4116 0 ch = buf[i];
4117 0 buf[i] = buf[l - 1 - i];
4118 0 buf[l - 1 - i] = ch;
4119 }
4120 0 return l;
4121 }
4122  
4123 0 static int cell_cmp(struct tb_cell *a, struct tb_cell *b) {
4124 0 ▶ if (a->ch != b->ch || a->fg != b->fg || a->bg != b->bg) {
Branch: 3 branches
BranchTrueFalseTotal
if (a->ch != b->ch || a->fg != b->fg || a->bg != b->bg) { 0 0 0
if (a->ch != b->ch || a->fg != b->fg || a->bg != b->bg) { 0 0 0
if (a->ch != b->ch || a->fg != b->fg || a->bg != b->bg) { 0 0 0
4125 0 return 1;
4126 }
4127 #ifdef TB_OPT_EGC
4128 0 ▶ if (a->nech != b->nech) {
Branch: 1 branch
BranchTrueFalseTotal
if (a->nech != b->nech) { 0 0 0
4129 0 return 1;
4130 0 ▶ } else if (a->nech > 0) { // a->nech == b->nech
Branch: 1 branch
BranchTrueFalseTotal
} else if (a->nech > 0) { // a->nech == b->nech 0 0 0
4131 0 return memcmp(a->ech, b->ech, a->nech * sizeof(*a->ech));
4132 }
4133 #endif
4134 0 return 0;
4135 }
4136  
4137 0 static int cell_copy(struct tb_cell *dst, struct tb_cell *src) {
4138 #ifdef TB_OPT_EGC
4139 0 ▶ if (src->nech > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (src->nech > 0) { 0 0 0
4140 0 return cell_set(dst, src->ech, src->nech, src->fg, src->bg);
4141 }
4142 #endif
4143 0 return cell_set(dst, &src->ch, 1, src->fg, src->bg);
4144 }
4145  
4146 6 static int cell_set(struct tb_cell *cell, uint32_t *ch, size_t nch,
4147 uintattr_t fg, uintattr_t bg) {
4148 // TODO: iswprint ch?
4149 6 ▶ cell->ch = ch ? *ch : 0;
Branch: 1 branch
BranchTrueFalseTotal
cell->ch = ch ? *ch : 0; 6 0 6
4150 6 cell->fg = fg;
4151 6 cell->bg = bg;
4152 #ifdef TB_OPT_EGC
4153 6 ▶ if (nch <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (nch <= 1) { 6 0 6
4154 6 cell->nech = 0;
4155 } else {
4156 int rv;
4157 0 ▶ if_err_return(rv, cell_reserve_ech(cell, nch + 1));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_reserve_ech(cell, nch + 1)); 0 0 0
4158 0 memcpy(cell->ech, ch, sizeof(*ch) * nch);
4159 0 cell->ech[nch] = '\0';
4160 0 cell->nech = nch;
4161 }
4162 #else
4163 (void)nch;
4164 (void)cell_reserve_ech;
4165 #endif
4166 6 return TB_OK;
4167 }
4168  
4169 0 static int cell_reserve_ech(struct tb_cell *cell, size_t n) {
4170 #ifdef TB_OPT_EGC
4171 0 ▶ if (cell->cech >= n) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (cell->cech >= n) return TB_OK; 0 0 0
4172 0 cell->ech = (uint32_t *)tb_realloc(cell->ech, n * sizeof(cell->ch));
4173 0 ▶ if (!cell->ech) return TB_ERR_MEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!cell->ech) return TB_ERR_MEM; 0 0 0
4174 0 cell->cech = n;
4175 0 return TB_OK;
4176 #else
4177 (void)cell;
4178 (void)n;
4179 return TB_ERR;
4180 #endif
4181 }
4182  
4183 6 static int cell_free(struct tb_cell *cell) {
4184 #ifdef TB_OPT_EGC
4185 6 ▶ if (cell->ech) tb_free(cell->ech);
Branch: 1 branch
BranchTrueFalseTotal
if (cell->ech) tb_free(cell->ech); 0 6 6
4186 #endif
4187 6 memset(cell, 0, sizeof(*cell));
4188 6 return TB_OK;
4189 }
4190  
4191 6 static int cellbuf_init(struct cellbuf *c, int w, int h) {
4192 6 c->cells = (struct tb_cell *)tb_malloc(sizeof(struct tb_cell) * w * h);
4193 6 ▶ if (!c->cells) return TB_ERR_MEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!c->cells) return TB_ERR_MEM; 0 6 6
4194 6 memset(c->cells, 0, sizeof(struct tb_cell) * w * h);
4195 6 c->width = w;
4196 6 c->height = h;
4197 6 return TB_OK;
4198 }
4199  
4200 6 static int cellbuf_free(struct cellbuf *c) {
4201 6 ▶ if (c->cells) {
Branch: 1 branch
BranchTrueFalseTotal
if (c->cells) { 6 0 6
4202 int i;
4203 12 ▶ for (i = 0; i < c->width * c->height; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->width * c->height; i++) { 6 6 12
4204 6 cell_free(&c->cells[i]);
4205 }
4206 6 tb_free(c->cells);
4207 }
4208 6 memset(c, 0, sizeof(*c));
4209 6 return TB_OK;
4210 }
4211  
4212 6 static int cellbuf_clear(struct cellbuf *c) {
4213 int rv, i;
4214 6 uint32_t space = (uint32_t)' ';
4215 12 ▶ for (i = 0; i < c->width * c->height; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < c->width * c->height; i++) { 6 6 12
4216 6 ▶ if_err_return(rv,
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, 0 6 6
4217 cell_set(&c->cells[i], &space, 1, global.fg, global.bg));
4218 }
4219 6 return TB_OK;
4220 }
4221  
4222 0 static int cellbuf_get(struct cellbuf *c, int x, int y,
4223 struct tb_cell **out) {
4224 0 ▶ if (!cellbuf_in_bounds(c, x, y)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cellbuf_in_bounds(c, x, y)) { 0 0 0
4225 0 *out = NULL;
4226 0 return TB_ERR_OUT_OF_BOUNDS;
4227 }
4228 0 *out = &c->cells[(y * c->width) + x];
4229 0 return TB_OK;
4230 }
4231  
4232 0 static int cellbuf_in_bounds(struct cellbuf *c, int x, int y) {
4233 0 ▶ if (x < 0 || x >= c->width || y < 0 || y >= c->height) {
Branch: 4 branches
BranchTrueFalseTotal
if (x < 0 || x >= c->width || y < 0 || y >= c->height) { 0 0 0
if (x < 0 || x >= c->width || y < 0 || y >= c->height) { 0 0 0
if (x < 0 || x >= c->width || y < 0 || y >= c->height) { 0 0 0
if (x < 0 || x >= c->width || y < 0 || y >= c->height) { 0 0 0
4234 0 return 0;
4235 }
4236 0 return 1;
4237 }
4238  
4239 0 static int cellbuf_resize(struct cellbuf *c, int w, int h) {
4240 int rv;
4241  
4242 0 int ow = c->width;
4243 0 int oh = c->height;
4244  
4245 0 ▶ if (ow == w && oh == h) {
Branch: 2 branches
BranchTrueFalseTotal
if (ow == w && oh == h) { 0 0 0
if (ow == w && oh == h) { 0 0 0
4246 0 return TB_OK;
4247 }
4248  
4249 0 w = w < 1 ? 1 : w;
4250 0 h = h < 1 ? 1 : h;
4251  
4252 0 int minw = (w < ow) ? w : ow;
4253 0 int minh = (h < oh) ? h : oh;
4254  
4255 0 struct tb_cell *prev = c->cells;
4256  
4257 0 ▶ if_err_return(rv, cellbuf_init(c, w, h));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_init(c, w, h)); 0 0 0
4258 0 ▶ if_err_return(rv, cellbuf_clear(c));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_clear(c)); 0 0 0
4259  
4260 int x, y;
4261 0 ▶ for (x = 0; x < minw; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < minw; x++) { 0 0 0
4262 0 ▶ for (y = 0; y < minh; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < minh; y++) { 0 0 0
4263 struct tb_cell *src, *dst;
4264 0 src = &prev[(y * ow) + x];
4265 0 ▶ if_err_return(rv, cellbuf_get(c, x, y, &dst));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cellbuf_get(c, x, y, &dst)); 0 0 0
4266 0 ▶ if_err_return(rv, cell_copy(dst, src));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, cell_copy(dst, src)); 0 0 0
4267 }
4268 }
4269  
4270 0 struct cellbuf prevbuf = {0};
4271 0 prevbuf.width = ow;
4272 0 prevbuf.height = oh;
4273 0 prevbuf.cells = prev;
4274 0 cellbuf_free(&prevbuf);
4275  
4276 0 return TB_OK;
4277 }
4278  
4279 41 static int bytebuf_puts(struct bytebuf *b, const char *str) {
4280 41 ▶ if (!str || strlen(str) <= 0) return TB_OK; // Nothing to do for empty caps
Branch: 2 branches
BranchTrueFalseTotal
if (!str || strlen(str) <= 0) return TB_OK; // Nothing to do for empty caps 41 0 41
if (!str || strlen(str) <= 0) return TB_OK; // Nothing to do for empty caps 0 41 41
4281 41 return bytebuf_nputs(b, str, (size_t)strlen(str));
4282 }
4283  
4284 74 static int bytebuf_nputs(struct bytebuf *b, const char *str, size_t nstr) {
4285 int rv;
4286 74 ▶ if_err_return(rv, bytebuf_reserve(b, b->len + nstr + 1));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_reserve(b, b->len + nstr + 1)); 0 74 74
4287 74 memcpy(b->buf + b->len, str, nstr);
4288 74 b->len += nstr;
4289 74 b->buf[b->len] = '\0';
4290 74 return TB_OK;
4291 }
4292  
4293 35 static int bytebuf_shift(struct bytebuf *b, size_t n) {
4294 35 ▶ if (n > b->len) n = b->len;
Branch: 1 branch
BranchTrueFalseTotal
if (n > b->len) n = b->len; 0 35 35
4295 35 size_t nmove = b->len - n;
4296 35 memmove(b->buf, b->buf + n, nmove);
4297 35 b->len -= n;
4298 35 return TB_OK;
4299 }
4300  
4301 14 static int bytebuf_flush(struct bytebuf *b, int fd) {
4302 int rv;
4303 14 ▶ if (b->len <= 0) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (b->len <= 0) return TB_OK; 0 14 14
4304 14 ▶ if_err_return(rv, write_output(fd, b->buf, b->len));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, write_output(fd, b->buf, b->len)); 0 14 14
4305 14 b->len = 0;
4306 14 return TB_OK;
4307 }
4308  
4309 74 static int bytebuf_reserve(struct bytebuf *b, size_t sz) {
4310 74 ▶ if (b->cap >= sz) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cap >= sz) return TB_OK; 60 14 74
4311  
4312 14 ▶ size_t newcap = b->cap > 0 ? b->cap : 1;
Branch: 1 branch
BranchTrueFalseTotal
size_t newcap = b->cap > 0 ? b->cap : 1; 8 6 14
4313 50 ▶ while (newcap < sz) {
Branch: 1 branch
BranchTrueFalseTotal
while (newcap < sz) { 36 14 50
4314 36 newcap *= 2;
4315 }
4316  
4317 char *newbuf;
4318 14 ▶ if (b->buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (b->buf) { 8 6 14
4319 8 newbuf = (char *)tb_realloc(b->buf, newcap);
4320 } else {
4321 6 newbuf = (char *)tb_malloc(newcap);
4322 }
4323 14 ▶ if (!newbuf) return TB_ERR_MEM;
Branch: 1 branch
BranchTrueFalseTotal
if (!newbuf) return TB_ERR_MEM; 0 14 14
4324  
4325 14 b->buf = newbuf;
4326 14 b->cap = newcap;
4327 14 return TB_OK;
4328 }
4329  
4330 6 static int bytebuf_free(struct bytebuf *b) {
4331 6 ▶ if (b->buf) tb_free(b->buf);
Branch: 1 branch
BranchTrueFalseTotal
if (b->buf) tb_free(b->buf); 6 0 6
4332 6 memset(b, 0, sizeof(*b));
4333 6 return TB_OK;
4334 }
4335  
4336 /* The rest of the platform layer (init_term_attrs, init_resize_handler and
4337 * update_term_size have their per-system definitions above, where upstream
4338 * keeps them). The functions below open the terminal, restore it, wait for
4339 * and read its input, write its output, read the clock and spell out
4340 * errors. `wait_input` is the heart of it: it waits
4341 * up to `timeout_ms` milliseconds (forever when negative) for input, appends
4342 * the bytes that arrived to `global.in` and returns `TB_OK`, or
4343 * `TB_ERR_NO_EVENT` when the time ran out. A terminal at end of file wakes
4344 * it with nothing to append. With `resized` set, a window size change wakes
4345 * it too and sets `*resized`; with `resized` NULL, size changes stay pending
4346 * for a later call. */
4347  
4348 #ifndef _WIN32
4349  
4350 0 static int open_tty(const char *path, int *rfd, int *wfd) {
4351 0 int fd = open(path, O_RDWR);
4352 0 ▶ if (fd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (fd < 0) { 0 0 0
4353 0 global.last_errno = errno;
4354 0 return TB_ERR_INIT_OPEN;
4355 }
4356 0 *rfd = fd;
4357 0 *wfd = fd;
4358 0 return TB_OK;
4359 }
4360  
4361 3 static int init_fds(int rfd, int wfd) {
4362 3 ▶ global.ttyfd = isatty(rfd) ? rfd : (wfd != rfd && isatty(wfd) ? wfd : -1);
Branch: 3 branches
BranchTrueFalseTotal
global.ttyfd = isatty(rfd) ? rfd : (wfd != rfd && isatty(wfd) ? wfd : -1); 3 0 3
global.ttyfd = isatty(rfd) ? rfd : (wfd != rfd && isatty(wfd) ? wfd : -1); 3 0 3
global.ttyfd = isatty(rfd) ? rfd : (wfd != rfd && isatty(wfd) ? wfd : -1); 0 3 3
4363 3 global.rfd = rfd;
4364 3 global.wfd = wfd;
4365 3 return TB_OK;
4366 }
4367  
4368 3 static int restore_term_attrs(void) {
4369 3 ▶ if (global.has_orig_tios && global.ttyfd >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (global.has_orig_tios && global.ttyfd >= 0) { 0 3 3
if (global.has_orig_tios && global.ttyfd >= 0) { 0 0 0
4370 0 tcsetattr(global.ttyfd, TCSAFLUSH, &global.orig_tios);
4371 }
4372 3 return TB_OK;
4373 }
4374  
4375 3 static int deinit_resize_handler(void) {
4376 struct sigaction sa;
4377 3 memset(&sa, 0, sizeof(sa));
4378 3 sa.sa_handler = SIG_DFL;
4379 3 sigaction(SIGWINCH, &sa, NULL);
4380 3 ▶ if (global.resize_pipefd[0] >= 0) close(global.resize_pipefd[0]);
Branch: 1 branch
BranchTrueFalseTotal
if (global.resize_pipefd[0] >= 0) close(global.resize_pipefd[0]); 3 0 3
4381 3 ▶ if (global.resize_pipefd[1] >= 0) close(global.resize_pipefd[1]);
Branch: 1 branch
BranchTrueFalseTotal
if (global.resize_pipefd[1] >= 0) close(global.resize_pipefd[1]); 3 0 3
4382 3 return TB_OK;
4383 }
4384  
4385 3 static int close_opened_fds(void) {
4386 3 ▶ if (!global.ttyfd_open) return TB_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (!global.ttyfd_open) return TB_OK; 3 0 3
4387 0 close(global.rfd);
4388 0 ▶ if (global.wfd != global.rfd) close(global.wfd);
Branch: 1 branch
BranchTrueFalseTotal
if (global.wfd != global.rfd) close(global.wfd); 0 0 0
4389 0 global.ttyfd_open = 0;
4390 0 return TB_OK;
4391 }
4392  
4393 /* The escape sequences of `tb_set_input_mode` switch mouse reporting. */
4394 1 static int enable_mouse_input(int enabled) {
4395 (void)enabled;
4396 1 return TB_OK;
4397 }
4398  
4399 65 static int wait_input(int timeout_ms, int *resized) {
4400 int rv, maxfd, select_rv;
4401 char buf[TB_OPT_READ_BUF];
4402 fd_set fds;
4403 struct timeval tv;
4404  
4405 65 ▶ if (resized) *resized = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (resized) *resized = 0; 58 7 65
4406  
4407 1105 ▶ FD_ZERO(&fds);
Branch: 1 branch
BranchTrueFalseTotal
FD_ZERO(&fds); 1040 65 1105
4408 65 FD_SET(global.rfd, &fds);
4409 65 maxfd = global.rfd;
4410 65 ▶ if (resized) {
Branch: 1 branch
BranchTrueFalseTotal
if (resized) { 58 7 65
4411 58 FD_SET(global.resize_pipefd[0], &fds);
4412 58 ▶ if (global.resize_pipefd[0] > maxfd) maxfd = global.resize_pipefd[0];
Branch: 1 branch
BranchTrueFalseTotal
if (global.resize_pipefd[0] > maxfd) maxfd = global.resize_pipefd[0]; 58 0 58
4413 }
4414  
4415 65 tv.tv_sec = timeout_ms / 1000;
4416 65 tv.tv_usec = (timeout_ms % 1000) * 1000;
4417 65 ▶ select_rv = select(maxfd + 1, &fds, NULL, NULL, timeout_ms < 0 ? NULL : &tv);
Branch: 1 branch
BranchTrueFalseTotal
select_rv = select(maxfd + 1, &fds, NULL, NULL, timeout_ms < 0 ? NULL : &tv); 65 0 65
4418  
4419 65 ▶ if (select_rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select_rv < 0) { 0 65 65
4420 // Let EINTR/EAGAIN bubble up
4421 0 global.last_errno = errno;
4422 0 return TB_ERR_POLL;
4423 65 ▶ } else if (select_rv == 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (select_rv == 0) { 31 34 65
4424 31 return TB_ERR_NO_EVENT;
4425 }
4426  
4427 34 ▶ if (FD_ISSET(global.rfd, &fds)) {
Branch: 1 branch
BranchTrueFalseTotal
if (FD_ISSET(global.rfd, &fds)) { 34 0 34
4428 34 ssize_t read_rv = read(global.rfd, buf, sizeof(buf));
4429 34 ▶ if (read_rv < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (read_rv < 0) { 0 34 34
4430 0 global.last_errno = errno;
4431 0 return TB_ERR_READ;
4432 34 ▶ } else if (read_rv > 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (read_rv > 0) { 33 1 34
4433 33 ▶ if_err_return(rv, bytebuf_nputs(&global.in, buf, (size_t)read_rv));
Branch: 1 branch
BranchTrueFalseTotal
if_err_return(rv, bytebuf_nputs(&global.in, buf, (size_t)read_rv)); 0 33 33
4434 }
4435 }
4436  
4437 34 ▶ if (resized && FD_ISSET(global.resize_pipefd[0], &fds)) {
Branch: 2 branches
BranchTrueFalseTotal
if (resized && FD_ISSET(global.resize_pipefd[0], &fds)) { 29 5 34
if (resized && FD_ISSET(global.resize_pipefd[0], &fds)) { 0 29 29
4438 0 int ignore = 0;
4439 0 read(global.resize_pipefd[0], &ignore, sizeof(ignore));
4440 0 *resized = 1;
4441 }
4442  
4443 34 return TB_OK;
4444 }
4445  
4446 14 static int write_output(int fd, const char *buf, size_t len) {
4447 14 ssize_t write_rv = write(fd, buf, len);
4448 14 ▶ if (write_rv < 0 || (size_t)write_rv != len) {
Branch: 2 branches
BranchTrueFalseTotal
if (write_rv < 0 || (size_t)write_rv != len) { 14 0 14
if (write_rv < 0 || (size_t)write_rv != len) { 0 14 14
4449 // Note, errno will be 0 on partial write
4450 0 global.last_errno = errno;
4451 0 return TB_ERR;
4452 }
4453 14 return TB_OK;
4454 }
4455  
4456 3 static const char *get_term_name(void) {
4457 3 return getenv("TERM");
4458 }
4459  
4460 14 static int64_t now_ms(void) {
4461 struct timeval now;
4462 14 gettimeofday(&now, NULL);
4463 14 return (int64_t)now.tv_sec * 1000 + now.tv_usec / 1000;
4464 }
4465  
4466 0 static void format_last_error(void) {
4467 0 strerror_r(global.last_errno, global.errbuf, sizeof(global.errbuf));
4468 0 }
4469  
4470 #else // _WIN32
4471  
4472 /* `global.last_errno` holds a Win32 error code (`GetLastError`) on Windows,
4473 * also for failures of the C runtime (`_doserrno`). */
4474  
4475 static int win_error(int rv) {
4476 global.last_errno = (int)GetLastError();
4477 return rv;
4478 }
4479  
4480 static int is_console(HANDLE handle) {
4481 DWORD mode;
4482 return GetConsoleMode(handle, &mode) != 0;
4483 }
4484  
4485 static int open_tty(const char *path, int *rfd, int *wfd) {
4486 if (strcmp(path, "/dev/tty") == 0 || _stricmp(path, "CON") == 0) {
4487 *rfd = _open("CONIN$", _O_RDWR | _O_BINARY);
4488 if (*rfd < 0) {
4489 global.last_errno = (int)_doserrno;
4490 return TB_ERR_INIT_OPEN;
4491 }
4492 *wfd = _open("CONOUT$", _O_RDWR | _O_BINARY);
4493 if (*wfd < 0) {
4494 global.last_errno = (int)_doserrno;
4495 _close(*rfd);
4496 return TB_ERR_INIT_OPEN;
4497 }
4498 return TB_OK;
4499 }
4500  
4501 *rfd = _open(path, _O_RDWR | _O_BINARY);
4502 if (*rfd < 0) {
4503 global.last_errno = (int)_doserrno;
4504 return TB_ERR_INIT_OPEN;
4505 }
4506 *wfd = *rfd;
4507 return TB_OK;
4508 }
4509  
4510 static int init_fds(int rfd, int wfd) {
4511 global.rfd = rfd;
4512 global.wfd = wfd;
4513 global.in_handle = (HANDLE)_get_osfhandle(rfd);
4514 global.out_handle = (HANDLE)_get_osfhandle(wfd);
4515 if (global.in_handle == INVALID_HANDLE_VALUE ||
4516 global.out_handle == INVALID_HANDLE_VALUE)
4517 {
4518 global.last_errno = ERROR_INVALID_HANDLE;
4519 return TB_ERR_INIT_OPEN;
4520 }
4521 global.in_is_console = is_console(global.in_handle);
4522 global.out_is_console = is_console(global.out_handle);
4523 // Only mirrors the POSIX meaning (the descriptor that is a terminal);
4524 // the Windows code works with the handles and the two flags above.
4525 global.ttyfd = global.out_is_console ? wfd
4526 : global.in_is_console ? rfd
4527 : -1;
4528 return TB_OK;
4529 }
4530  
4531 static int restore_term_attrs(void) {
4532 if (global.has_orig_in_mode) {
4533 SetConsoleMode(global.in_handle, global.orig_in_mode);
4534 SetConsoleCP(global.orig_in_cp);
4535 }
4536 if (global.has_orig_out_mode) {
4537 SetConsoleMode(global.out_handle, global.orig_out_mode);
4538 SetConsoleOutputCP(global.orig_out_cp);
4539 }
4540 return TB_OK;
4541 }
4542  
4543 static int deinit_resize_handler(void) {
4544 return TB_OK;
4545 }
4546  
4547 static int close_opened_fds(void) {
4548 if (!global.ttyfd_open) return TB_OK;
4549 _close(global.rfd);
4550 if (global.wfd != global.rfd) _close(global.wfd);
4551 global.ttyfd_open = 0;
4552 return TB_OK;
4553 }
4554  
4555 /* The console reports the mouse only while asked to, and a pseudo console
4556 * asks its terminal to report it as soon as the console is asked: the flag
4557 * follows the input mode, so a program without mouse support gets no mouse
4558 * reports. */
4559 static int enable_mouse_input(int enabled) {
4560 DWORD mode;
4561 if (!global.in_is_console) return TB_OK;
4562 if (!GetConsoleMode(global.in_handle, &mode)) {
4563 return win_error(TB_ERR_TCGETATTR);
4564 }
4565 mode = enabled ? (mode | ENABLE_MOUSE_INPUT) : (mode & ~(DWORD)ENABLE_MOUSE_INPUT);
4566 if (!SetConsoleMode(global.in_handle, mode)) {
4567 return win_error(TB_ERR_TCSETATTR);
4568 }
4569 return TB_OK;
4570 }
4571  
4572 /* A console delivers input records: keys, which the console has encoded as
4573 * the bytes an xterm would send (ENABLE_VIRTUAL_TERMINAL_INPUT), the mouse
4574 * and window size changes. Records that mean nothing to termbox (key
4575 * releases, focus and menu events) are read and dropped, so the wait goes
4576 * on until something arrived or the time ran out. Anything else (a pipe, a
4577 * file) is a stream of bytes. */
4578 static int wait_input(int timeout_ms, int *resized) {
4579 int rv;
4580 int64_t deadline = timeout_ms < 0 ? 0 : now_ms() + timeout_ms;
4581  
4582 if (resized) {
4583 *resized = global.resize_pending;
4584 global.resize_pending = 0;
4585 if (*resized) return TB_OK;
4586 }
4587  
4588 if (!global.in_is_console) return read_stream_bytes(timeout_ms);
4589  
4590 for (;;) {
4591 size_t len_before = global.in.len;
4592 int64_t left = timeout_ms < 0 ? INFINITE : deadline - now_ms();
4593 DWORD wait_rv;
4594  
4595 if (left < 0) left = 0;
4596 wait_rv = WaitForSingleObject(global.in_handle, (DWORD)left);
4597 if (wait_rv == WAIT_TIMEOUT) return TB_ERR_NO_EVENT;
4598 if (wait_rv != WAIT_OBJECT_0) return win_error(TB_ERR_POLL);
4599  
4600 if_err_return(rv, read_console_records());
4601 if (global.in.len > len_before) return TB_OK;
4602 if (resized && global.resize_pending) {
4603 *resized = 1;
4604 global.resize_pending = 0;
4605 return TB_OK;
4606 }
4607 }
4608 }
4609  
4610 static int read_console_records(void) {
4611 INPUT_RECORD records[TB_OPT_READ_BUF];
4612 DWORD count, i;
4613 int rv;
4614  
4615 if (!ReadConsoleInputW(global.in_handle, records, TB_OPT_READ_BUF, &count)) {
4616 return win_error(TB_ERR_READ);
4617 }
4618  
4619 for (i = 0; i < count; i++) {
4620 switch (records[i].EventType) {
4621 case KEY_EVENT:
4622 if_err_return(rv, append_key_record(&records[i].Event.KeyEvent));
4623 break;
4624 case MOUSE_EVENT:
4625 if_err_return(rv,
4626 append_mouse_record(&records[i].Event.MouseEvent));
4627 break;
4628 case WINDOW_BUFFER_SIZE_EVENT:
4629 global.resize_pending = 1;
4630 break;
4631 default:
4632 break;
4633 }
4634 }
4635  
4636 return TB_OK;
4637 }
4638  
4639 /* The text of a key, repeated as often as the record says. A key release
4640 * carries text only for a character typed as Alt plus its number on the
4641 * keypad, which the console delivers when Alt comes up. Modifier keys and
4642 * dead keys carry no text. */
4643 static int append_key_record(const KEY_EVENT_RECORD *key) {
4644 int rv;
4645 WORD repeat;
4646 uint16_t unit = (uint16_t)key->uChar.UnicodeChar;
4647  
4648 if (unit == 0) return TB_OK;
4649 if (!key->bKeyDown && key->wVirtualKeyCode != VK_MENU) return TB_OK;
4650  
4651 for (repeat = key->wRepeatCount > 0 ? key->wRepeatCount : 1; repeat > 0; repeat--) {
4652 if_err_return(rv, append_utf16_unit(unit));
4653 }
4654 return TB_OK;
4655 }
4656  
4657 /* The console speaks UTF-16; a character beyond the basic plane comes as a
4658 * surrogate pair in two records. */
4659 static int append_utf16_unit(uint16_t unit) {
4660 uint32_t codepoint;
4661 char utf8[8];
4662 int len;
4663  
4664 if (unit >= 0xd800 && unit <= 0xdbff) {
4665 global.pending_high_surrogate = unit;
4666 return TB_OK;
4667 }
4668 if (unit >= 0xdc00 && unit <= 0xdfff) {
4669 if (global.pending_high_surrogate == 0) return TB_OK; // a stray low half
4670 codepoint = 0x10000 +
4671 (((uint32_t)global.pending_high_surrogate - 0xd800) << 10) +
4672 ((uint32_t)unit - 0xdc00);
4673 global.pending_high_surrogate = 0;
4674 } else {
4675 global.pending_high_surrogate = 0;
4676 codepoint = unit;
4677 }
4678  
4679 len = tb_utf8_unicode_to_char(utf8, codepoint);
4680 return bytebuf_nputs(&global.in, utf8, (size_t)len);
4681 }
4682  
4683 /* A mouse record, as the SGR mouse report (`ESC [ < button ; column ; row M`,
4684 * `m` for a release) an xterm sends in mode 1006 with motion reporting
4685 * (mode 1003): a terminal that reports the mouse itself delivers the same
4686 * bytes through key records instead, so termbox parses one encoding only.
4687 * Button state changes are told apart from the last record: a button that
4688 * is newly down is pressed, one that is newly up is released. The console
4689 * reports every motion; a motion with a button down is a drag with that
4690 * button. Positions count from the console window, not from its scrollback
4691 * buffer. */
4692 static int append_mouse_record(const MOUSE_EVENT_RECORD *mouse) {
4693 int rv;
4694 DWORD buttons = mouse->dwButtonState & 0x7; // left, right, middle
4695 DWORD was_down = global.mouse_buttons;
4696 DWORD flags = mouse->dwEventFlags;
4697 static const int button_of_bit[3] = {0, 2, 1}; // SGR: 0 left, 1 middle, 2 right
4698 int bit;
4699  
4700 if (!(global.input_mode & TB_INPUT_MOUSE)) return TB_OK;
4701  
4702 if (flags & MOUSE_WHEELED) {
4703 int up = (SHORT)HIWORD(mouse->dwButtonState) > 0;
4704 return append_mouse_report(up ? 64 : 65, mouse, 'M');
4705 }
4706 if (flags & MOUSE_HWHEELED) {
4707 int right = (SHORT)HIWORD(mouse->dwButtonState) > 0;
4708 return append_mouse_report(right ? 67 : 66, mouse, 'M');
4709 }
4710 // The button state is compared first, also for a motion record: a
4711 // release the console never delivered (the focus left during a drag)
4712 // must not hide the next press of that button.
4713 for (bit = 0; bit < 3; bit++) {
4714 DWORD mask = 1u << bit;
4715 if ((buttons & mask) && !(was_down & mask)) {
4716 if_err_return(rv, append_mouse_report(button_of_bit[bit], mouse, 'M'));
4717 } else if (!(buttons & mask) && (was_down & mask)) {
4718 if_err_return(rv, append_mouse_report(button_of_bit[bit], mouse, 'm'));
4719 }
4720 }
4721 global.mouse_buttons = buttons;
4722  
4723 if (flags & MOUSE_MOVED) {
4724 int button = 3; // no button
4725 for (bit = 0; bit < 3; bit++) {
4726 if (buttons & (1u << bit)) {
4727 button = button_of_bit[bit];
4728 break;
4729 }
4730 }
4731 return append_mouse_report(32 + button, mouse, 'M');
4732 }
4733 return TB_OK;
4734 }
4735  
4736 static int append_mouse_report(int button, const MOUSE_EVENT_RECORD *mouse,
4737 char final) {
4738 CONSOLE_SCREEN_BUFFER_INFO info;
4739 char report[32];
4740 int x = mouse->dwMousePosition.X;
4741 int y = mouse->dwMousePosition.Y;
4742 int len;
4743  
4744 if (mouse->dwControlKeyState & SHIFT_PRESSED) button |= 4;
4745 if (mouse->dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
4746 button |= 8;
4747 }
4748 if (mouse->dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
4749 button |= 16;
4750 }
4751  
4752 if (global.out_is_console &&
4753 GetConsoleScreenBufferInfo(global.out_handle, &info))
4754 {
4755 x -= info.srWindow.Left;
4756 y -= info.srWindow.Top;
4757 }
4758  
4759 len = snprintf(report, sizeof(report), "\x1b[<%d;%d;%d%c", button, x + 1,
4760 y + 1, final);
4761 if (len < 0 || len >= (int)sizeof(report)) return TB_ERR;
4762 return bytebuf_nputs(&global.in, report, (size_t)len);
4763 }
4764  
4765 /* Bytes from a pipe or a file. A pipe cannot be waited on, so it is asked
4766 * every few milliseconds whether bytes arrived; a file is always readable.
4767 * A pipe whose writer went away, or a file at its end, is end of file:
4768 * nothing is appended, as a POSIX read of 0 bytes appends nothing. */
4769 static int read_stream_bytes(int timeout_ms) {
4770 char buf[TB_OPT_READ_BUF];
4771 DWORD count;
4772 int64_t deadline = timeout_ms < 0 ? 0 : now_ms() + timeout_ms;
4773  
4774 if (GetFileType(global.in_handle) == FILE_TYPE_PIPE) {
4775 for (;;) {
4776 DWORD available = 0;
4777 if (!PeekNamedPipe(global.in_handle, NULL, 0, NULL, &available, NULL)) {
4778 DWORD error = GetLastError();
4779 if (error == ERROR_BROKEN_PIPE ||
4780 error == ERROR_PIPE_NOT_CONNECTED)
4781 {
4782 return TB_OK; // end of file
4783 }
4784 global.last_errno = (int)error;
4785 return TB_ERR_POLL;
4786 }
4787 if (available > 0) break;
4788 if (timeout_ms >= 0 && now_ms() >= deadline) return TB_ERR_NO_EVENT;
4789 Sleep(5);
4790 }
4791 }
4792  
4793 if (!ReadFile(global.in_handle, buf, sizeof(buf), &count, NULL)) {
4794 DWORD error = GetLastError();
4795 if (error == ERROR_BROKEN_PIPE || error == ERROR_HANDLE_EOF) {
4796 return TB_OK; // end of file
4797 }
4798 global.last_errno = (int)error;
4799 return TB_ERR_READ;
4800 }
4801 if (count == 0) return TB_OK; // end of file
4802 return bytebuf_nputs(&global.in, buf, (size_t)count);
4803 }
4804  
4805 /* `WriteFile` to a console takes at most some tens of kilobytes at once. */
4806 static int write_output(int fd, const char *buf, size_t len) {
4807 HANDLE handle = (HANDLE)_get_osfhandle(fd);
4808 if (handle == INVALID_HANDLE_VALUE) {
4809 global.last_errno = ERROR_INVALID_HANDLE;
4810 return TB_ERR;
4811 }
4812 while (len > 0) {
4813 DWORD chunk = len > 32768 ? 32768 : (DWORD)len;
4814 DWORD written = 0;
4815 if (!WriteFile(handle, buf, chunk, &written, NULL)) {
4816 return win_error(TB_ERR);
4817 }
4818 if (written == 0) {
4819 global.last_errno = ERROR_WRITE_FAULT;
4820 return TB_ERR;
4821 }
4822 buf += written;
4823 len -= written;
4824 }
4825 return TB_OK;
4826 }
4827  
4828 /* The console understands the sequences of an xterm; TERM is rarely set. */
4829 static const char *get_term_name(void) {
4830 const char *term = getenv("TERM");
4831 return term ? term : "xterm-256color";
4832 }
4833  
4834 static int64_t now_ms(void) {
4835 return (int64_t)GetTickCount64();
4836 }
4837  
4838 /* The message in UTF-8, like everything else termbox2 says, not in the
4839 * ANSI code page of the system. */
4840 static void format_last_error(void) {
4841 WCHAR message[512];
4842 DWORD len = FormatMessageW(
4843 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
4844 (DWORD)global.last_errno, 0, message,
4845 sizeof(message) / sizeof(message[0]), NULL);
4846 int bytes = 0;
4847  
4848 while (len > 0 && (message[len - 1] == L'\r' || message[len - 1] == L'\n' ||
4849 message[len - 1] == L' '))
4850 {
4851 len--;
4852 }
4853 if (len > 0) {
4854 bytes = WideCharToMultiByte(CP_UTF8, 0, message, (int)len,
4855 global.errbuf, (int)sizeof(global.errbuf) - 1, NULL, NULL);
4856 }
4857 if (bytes <= 0) {
4858 snprintf(global.errbuf, sizeof(global.errbuf), "Windows error %d",
4859 global.last_errno);
4860 return;
4861 }
4862 global.errbuf[bytes] = '\0';
4863 }
4864  
4865 #endif // _WIN32
4866  
4867 2 int tb_iswprint(uint32_t ch) {
4868 #ifdef TB_OPT_LIBC_WCHAR
4869 return iswprint((wint_t)ch);
4870 #else
4871 2 return tb_iswprint_ex(ch, NULL);
4872 #endif
4873 }
4874  
4875 72 int tb_wcwidth(uint32_t ch) {
4876 int w;
4877 #ifdef TB_OPT_LIBC_WCHAR
4878 w = wcwidth((wchar_t)ch);
4879 #else
4880 72 tb_iswprint_ex(ch, &w);
4881 #endif
4882 72 return w;
4883 }
4884  
4885 59 static int tb_cluster_width(uint32_t *ch, size_t nch) {
4886 59 int wmax = -1;
4887 59 int vs15 = 0, vs16 = 0, ri = 0, zwj = 0;
4888 59 size_t i = 0;
4889 127 ▶ for (i = 0; i < nch; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < nch; i++) { 68 59 127
4890 68 uint32_t c = ch[i];
4891 68 switch (c) {
4892 2 case 0xfe0e: ++vs15; break;
4893 2 case 0xfe0f: ++vs16; break;
4894 2 case 0x200d: ++zwj; break;
4895 62 ▶ default: if (c >= 0x1f1e6 && c <= 0x1f1ff) ++ri;
Branch: 2 branches
BranchTrueFalseTotal
default: if (c >= 0x1f1e6 && c <= 0x1f1ff) ++ri; 4 58 62
default: if (c >= 0x1f1e6 && c <= 0x1f1ff) ++ri; 2 2 4
4896 }
4897 68 int w = tb_wcwidth(c);
4898 68 ▶ if (w > wmax) wmax = w;
Branch: 1 branch
BranchTrueFalseTotal
if (w > wmax) wmax = w; 59 9 68
4899 }
4900 59 ▶ if (wmax >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (wmax >= 1) { 58 1 59
4901 58 ▶ if (vs15) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (vs15) return 1; 2 56 58
4902 56 ▶ else if (vs16 || zwj || ri >= 2) return 2;
Branch: 3 branches
BranchTrueFalseTotal
else if (vs16 || zwj || ri >= 2) return 2; 54 2 56
else if (vs16 || zwj || ri >= 2) return 2; 52 2 54
else if (vs16 || zwj || ri >= 2) return 2; 1 51 52
4903 }
4904 52 return wmax;
4905 }
4906  
4907 74 static int tb_iswprint_ex(uint32_t ch, int *w) {
4908 #ifdef TB_OPT_LIBC_WCHAR
4909 if (w) *w = wcwidth((wint_t)ch);
4910 return iswprint(ch);
4911 #else
4912 // Fast path for 1-byte codepoints
4913 74 ▶ if ((ch >= 0x20 && ch <= 0x7e) || (ch >= 0xa0 && ch <= 0xff)) {
Branch: 4 branches
BranchTrueFalseTotal
if ((ch >= 0x20 && ch <= 0x7e) || (ch >= 0xa0 && ch <= 0xff)) { 72 2 74
if ((ch >= 0x20 && ch <= 0x7e) || (ch >= 0xa0 && ch <= 0xff)) { 36 36 72
if ((ch >= 0x20 && ch <= 0x7e) || (ch >= 0xa0 && ch <= 0xff)) { 36 2 38
if ((ch >= 0x20 && ch <= 0x7e) || (ch >= 0xa0 && ch <= 0xff)) { 0 36 36
4914 36 ▶ if (w) *w = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (w) *w = 1; 35 1 36
4915 36 return 1;
4916 38 ▶ } else if (ch <= 0xff) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ch <= 0xff) { 2 36 38
4917 2 ▶ if (w) *w = ch == 0 ? 0 : -1;
Branch: 2 branches
BranchTrueFalseTotal
if (w) *w = ch == 0 ? 0 : -1; 1 1 2
if (w) *w = ch == 0 ? 0 : -1; 0 1 1
4918 2 return 0;
4919 }
4920  
4921 36 int lo = 0, hi = WCWIDTH_TABLE_LENGTH - 1;
4922 373 ▶ while (lo <= hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo <= hi) { 373 0 373
4923 373 int i = (lo + hi) / 2;
4924 373 ▶ if (ch < wcwidth_table[i].range_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (ch < wcwidth_table[i].range_start) { 162 211 373
4925 162 hi = i - 1;
4926 211 ▶ } else if (ch > wcwidth_table[i].range_end) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (ch > wcwidth_table[i].range_end) { 175 36 211
4927 175 lo = i + 1;
4928 } else {
4929 36 ▶ if (w) *w = wcwidth_table[i].width;
Branch: 1 branch
BranchTrueFalseTotal
if (w) *w = wcwidth_table[i].width; 36 0 36
4930 36 return wcwidth_table[i].width >= 0 ? 1 : 0;
4931 }
4932 }
4933 0 ▶ if (w) *w = -1; // Invalid codepoint
Branch: 1 branch
BranchTrueFalseTotal
if (w) *w = -1; // Invalid codepoint 0 0 0
4934 0 return 0;
4935 #endif
4936 }
4937  
4938 #endif // TB_IMPL
« blib/lib/Term/Fabulous/Render/Text.pm ↑ Summary src/termbox2.c »