Fast and reasonably complete (framebuffer) terminal emulator (Zig fork)
1/* Copyright (C) 2022-2025 mintsuki and contributors.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain the above copyright notice,
7 * this list of conditions and the following disclaimer.
8 *
9 * 2. Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifdef __cplusplus
27#error "Please do not compile Flanterm as C++ code! Flanterm should be compiled as C99 or newer."
28#endif
29
30#ifndef __STDC_VERSION__
31#error "Flanterm must be compiled as C99 or newer."
32#endif
33
34#include <stdint.h>
35#include <stddef.h>
36#include <stdbool.h>
37
38#define FLANTERM_IN_FLANTERM
39
40#include "flanterm.h"
41
42// Tries to implement this standard for terminfo
43// https://man7.org/linux/man-pages/man4/console_codes.4.html
44
45static const uint32_t col256[] = {
46 0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f,
47 0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af,
48 0x0087d7, 0x0087ff, 0x00af00, 0x00af5f, 0x00af87, 0x00afaf, 0x00afd7, 0x00afff,
49 0x00d700, 0x00d75f, 0x00d787, 0x00d7af, 0x00d7d7, 0x00d7ff, 0x00ff00, 0x00ff5f,
50 0x00ff87, 0x00ffaf, 0x00ffd7, 0x00ffff, 0x5f0000, 0x5f005f, 0x5f0087, 0x5f00af,
51 0x5f00d7, 0x5f00ff, 0x5f5f00, 0x5f5f5f, 0x5f5f87, 0x5f5faf, 0x5f5fd7, 0x5f5fff,
52 0x5f8700, 0x5f875f, 0x5f8787, 0x5f87af, 0x5f87d7, 0x5f87ff, 0x5faf00, 0x5faf5f,
53 0x5faf87, 0x5fafaf, 0x5fafd7, 0x5fafff, 0x5fd700, 0x5fd75f, 0x5fd787, 0x5fd7af,
54 0x5fd7d7, 0x5fd7ff, 0x5fff00, 0x5fff5f, 0x5fff87, 0x5fffaf, 0x5fffd7, 0x5fffff,
55 0x870000, 0x87005f, 0x870087, 0x8700af, 0x8700d7, 0x8700ff, 0x875f00, 0x875f5f,
56 0x875f87, 0x875faf, 0x875fd7, 0x875fff, 0x878700, 0x87875f, 0x878787, 0x8787af,
57 0x8787d7, 0x8787ff, 0x87af00, 0x87af5f, 0x87af87, 0x87afaf, 0x87afd7, 0x87afff,
58 0x87d700, 0x87d75f, 0x87d787, 0x87d7af, 0x87d7d7, 0x87d7ff, 0x87ff00, 0x87ff5f,
59 0x87ff87, 0x87ffaf, 0x87ffd7, 0x87ffff, 0xaf0000, 0xaf005f, 0xaf0087, 0xaf00af,
60 0xaf00d7, 0xaf00ff, 0xaf5f00, 0xaf5f5f, 0xaf5f87, 0xaf5faf, 0xaf5fd7, 0xaf5fff,
61 0xaf8700, 0xaf875f, 0xaf8787, 0xaf87af, 0xaf87d7, 0xaf87ff, 0xafaf00, 0xafaf5f,
62 0xafaf87, 0xafafaf, 0xafafd7, 0xafafff, 0xafd700, 0xafd75f, 0xafd787, 0xafd7af,
63 0xafd7d7, 0xafd7ff, 0xafff00, 0xafff5f, 0xafff87, 0xafffaf, 0xafffd7, 0xafffff,
64 0xd70000, 0xd7005f, 0xd70087, 0xd700af, 0xd700d7, 0xd700ff, 0xd75f00, 0xd75f5f,
65 0xd75f87, 0xd75faf, 0xd75fd7, 0xd75fff, 0xd78700, 0xd7875f, 0xd78787, 0xd787af,
66 0xd787d7, 0xd787ff, 0xd7af00, 0xd7af5f, 0xd7af87, 0xd7afaf, 0xd7afd7, 0xd7afff,
67 0xd7d700, 0xd7d75f, 0xd7d787, 0xd7d7af, 0xd7d7d7, 0xd7d7ff, 0xd7ff00, 0xd7ff5f,
68 0xd7ff87, 0xd7ffaf, 0xd7ffd7, 0xd7ffff, 0xff0000, 0xff005f, 0xff0087, 0xff00af,
69 0xff00d7, 0xff00ff, 0xff5f00, 0xff5f5f, 0xff5f87, 0xff5faf, 0xff5fd7, 0xff5fff,
70 0xff8700, 0xff875f, 0xff8787, 0xff87af, 0xff87d7, 0xff87ff, 0xffaf00, 0xffaf5f,
71 0xffaf87, 0xffafaf, 0xffafd7, 0xffafff, 0xffd700, 0xffd75f, 0xffd787, 0xffd7af,
72 0xffd7d7, 0xffd7ff, 0xffff00, 0xffff5f, 0xffff87, 0xffffaf, 0xffffd7, 0xffffff,
73 0x080808, 0x121212, 0x1c1c1c, 0x262626, 0x303030, 0x3a3a3a, 0x444444, 0x4e4e4e,
74 0x585858, 0x626262, 0x6c6c6c, 0x767676, 0x808080, 0x8a8a8a, 0x949494, 0x9e9e9e,
75 0xa8a8a8, 0xb2b2b2, 0xbcbcbc, 0xc6c6c6, 0xd0d0d0, 0xdadada, 0xe4e4e4, 0xeeeeee
76};
77
78#define CHARSET_DEFAULT 0
79#define CHARSET_DEC_SPECIAL 1
80
81void flanterm_context_reinit(struct flanterm_context *ctx) {
82 ctx->tab_size = 8;
83 ctx->autoflush = true;
84 ctx->cursor_enabled = true;
85 ctx->scroll_enabled = true;
86 ctx->control_sequence = false;
87 ctx->escape = false;
88 ctx->osc = false;
89 ctx->osc_escape = false;
90 ctx->rrr = false;
91 ctx->discard_next = false;
92 ctx->bold = false;
93 ctx->bg_bold = false;
94 ctx->reverse_video = false;
95 ctx->dec_private = false;
96 ctx->insert_mode = false;
97 ctx->unicode_remaining = 0;
98 ctx->g_select = 0;
99 ctx->charsets[0] = CHARSET_DEFAULT;
100 ctx->charsets[1] = CHARSET_DEC_SPECIAL;
101 ctx->current_charset = 0;
102 ctx->escape_offset = 0;
103 ctx->esc_values_i = 0;
104 ctx->saved_cursor_x = 0;
105 ctx->saved_cursor_y = 0;
106 ctx->current_primary = (size_t)-1;
107 ctx->current_bg = (size_t)-1;
108 ctx->scroll_top_margin = 0;
109 ctx->scroll_bottom_margin = ctx->rows;
110 ctx->oob_output = FLANTERM_OOB_OUTPUT_ONLCR;
111}
112
113static void flanterm_putchar(struct flanterm_context *ctx, uint8_t c);
114
115void flanterm_write(struct flanterm_context *ctx, const char *buf, size_t count) {
116 for (size_t i = 0; i < count; i++) {
117 flanterm_putchar(ctx, buf[i]);
118 }
119
120 if (ctx->autoflush) {
121 ctx->double_buffer_flush(ctx);
122 }
123}
124
125static void sgr(struct flanterm_context *ctx) {
126 size_t i = 0;
127
128 if (!ctx->esc_values_i)
129 goto def;
130
131 for (; i < ctx->esc_values_i; i++) {
132 size_t offset;
133
134 if (ctx->esc_values[i] == 0) {
135def:
136 if (ctx->reverse_video) {
137 ctx->reverse_video = false;
138 ctx->swap_palette(ctx);
139 }
140 ctx->bold = false;
141 ctx->bg_bold = false;
142 ctx->current_primary = (size_t)-1;
143 ctx->current_bg = (size_t)-1;
144 ctx->set_text_bg_default(ctx);
145 ctx->set_text_fg_default(ctx);
146 continue;
147 }
148
149 else if (ctx->esc_values[i] == 1) {
150 ctx->bold = true;
151 if (ctx->current_primary != (size_t)-1) {
152 if (!ctx->reverse_video) {
153 ctx->set_text_fg_bright(ctx, ctx->current_primary);
154 } else {
155 ctx->set_text_bg_bright(ctx, ctx->current_primary);
156 }
157 } else {
158 if (!ctx->reverse_video) {
159 ctx->set_text_fg_default_bright(ctx);
160 } else {
161 ctx->set_text_bg_default_bright(ctx);
162 }
163 }
164 continue;
165 }
166
167 else if (ctx->esc_values[i] == 5) {
168 ctx->bg_bold = true;
169 if (ctx->current_bg != (size_t)-1) {
170 if (!ctx->reverse_video) {
171 ctx->set_text_bg_bright(ctx, ctx->current_bg);
172 } else {
173 ctx->set_text_fg_bright(ctx, ctx->current_bg);
174 }
175 } else {
176 if (!ctx->reverse_video) {
177 ctx->set_text_bg_default_bright(ctx);
178 } else {
179 ctx->set_text_fg_default_bright(ctx);
180 }
181 }
182 continue;
183 }
184
185 else if (ctx->esc_values[i] == 22) {
186 ctx->bold = false;
187 if (ctx->current_primary != (size_t)-1) {
188 if (!ctx->reverse_video) {
189 ctx->set_text_fg(ctx, ctx->current_primary);
190 } else {
191 ctx->set_text_bg(ctx, ctx->current_primary);
192 }
193 } else {
194 if (!ctx->reverse_video) {
195 ctx->set_text_fg_default(ctx);
196 } else {
197 ctx->set_text_bg_default(ctx);
198 }
199 }
200 continue;
201 }
202
203 else if (ctx->esc_values[i] == 25) {
204 ctx->bg_bold = false;
205 if (ctx->current_bg != (size_t)-1) {
206 if (!ctx->reverse_video) {
207 ctx->set_text_bg(ctx, ctx->current_bg);
208 } else {
209 ctx->set_text_fg(ctx, ctx->current_bg);
210 }
211 } else {
212 if (!ctx->reverse_video) {
213 ctx->set_text_bg_default(ctx);
214 } else {
215 ctx->set_text_fg_default(ctx);
216 }
217 }
218 continue;
219 }
220
221 else if (ctx->esc_values[i] >= 30 && ctx->esc_values[i] <= 37) {
222 offset = 30;
223 ctx->current_primary = ctx->esc_values[i] - offset;
224
225 if (ctx->reverse_video) {
226 goto set_bg;
227 }
228
229set_fg:
230 if ((ctx->bold && !ctx->reverse_video)
231 || (ctx->bg_bold && ctx->reverse_video)) {
232 ctx->set_text_fg_bright(ctx, ctx->esc_values[i] - offset);
233 } else {
234 ctx->set_text_fg(ctx, ctx->esc_values[i] - offset);
235 }
236 continue;
237 }
238
239 else if (ctx->esc_values[i] >= 40 && ctx->esc_values[i] <= 47) {
240 offset = 40;
241 ctx->current_bg = ctx->esc_values[i] - offset;
242
243 if (ctx->reverse_video) {
244 goto set_fg;
245 }
246
247set_bg:
248 if ((ctx->bold && ctx->reverse_video)
249 || (ctx->bg_bold && !ctx->reverse_video)) {
250 ctx->set_text_bg_bright(ctx, ctx->esc_values[i] - offset);
251 } else {
252 ctx->set_text_bg(ctx, ctx->esc_values[i] - offset);
253 }
254 continue;
255 }
256
257 else if (ctx->esc_values[i] >= 90 && ctx->esc_values[i] <= 97) {
258 offset = 90;
259 ctx->current_primary = ctx->esc_values[i] - offset;
260
261 if (ctx->reverse_video) {
262 goto set_bg_bright;
263 }
264
265set_fg_bright:
266 ctx->set_text_fg_bright(ctx, ctx->esc_values[i] - offset);
267 continue;
268 }
269
270 else if (ctx->esc_values[i] >= 100 && ctx->esc_values[i] <= 107) {
271 offset = 100;
272 ctx->current_bg = ctx->esc_values[i] - offset;
273
274 if (ctx->reverse_video) {
275 goto set_fg_bright;
276 }
277
278set_bg_bright:
279 ctx->set_text_bg_bright(ctx, ctx->esc_values[i] - offset);
280 continue;
281 }
282
283 else if (ctx->esc_values[i] == 39) {
284 ctx->current_primary = (size_t)-1;
285
286 if (ctx->reverse_video) {
287 ctx->swap_palette(ctx);
288 }
289
290 if (!ctx->bold) {
291 ctx->set_text_fg_default(ctx);
292 } else {
293 ctx->set_text_fg_default_bright(ctx);
294 }
295
296 if (ctx->reverse_video) {
297 ctx->swap_palette(ctx);
298 }
299
300 continue;
301 }
302
303 else if (ctx->esc_values[i] == 49) {
304 ctx->current_bg = (size_t)-1;
305
306 if (ctx->reverse_video) {
307 ctx->swap_palette(ctx);
308 }
309
310 if (!ctx->bg_bold) {
311 ctx->set_text_bg_default(ctx);
312 } else {
313 ctx->set_text_bg_default_bright(ctx);
314 }
315
316 if (ctx->reverse_video) {
317 ctx->swap_palette(ctx);
318 }
319
320 continue;
321 }
322
323 else if (ctx->esc_values[i] == 7) {
324 if (!ctx->reverse_video) {
325 ctx->reverse_video = true;
326 ctx->swap_palette(ctx);
327 }
328 continue;
329 }
330
331 else if (ctx->esc_values[i] == 27) {
332 if (ctx->reverse_video) {
333 ctx->reverse_video = false;
334 ctx->swap_palette(ctx);
335 }
336 continue;
337 }
338
339 // 256/RGB
340 else if (ctx->esc_values[i] == 38 || ctx->esc_values[i] == 48) {
341 bool fg = ctx->esc_values[i] == 38;
342
343 i++;
344 if (i >= ctx->esc_values_i) {
345 break;
346 }
347
348 switch (ctx->esc_values[i]) {
349 case 2: { // RGB
350 if (i + 3 >= ctx->esc_values_i) {
351 goto out;
352 }
353
354 uint32_t rgb_value = 0;
355
356 rgb_value |= ctx->esc_values[i + 1] << 16;
357 rgb_value |= ctx->esc_values[i + 2] << 8;
358 rgb_value |= ctx->esc_values[i + 3];
359
360 i += 3;
361
362 (fg ? ctx->set_text_fg_rgb : ctx->set_text_bg_rgb)(ctx, rgb_value);
363
364 break;
365 }
366 case 5: { // 256 colors
367 if (i + 1 >= ctx->esc_values_i) {
368 goto out;
369 }
370
371 uint32_t col = ctx->esc_values[i + 1];
372
373 i++;
374
375 if (col < 8) {
376 (fg ? ctx->set_text_fg : ctx->set_text_bg)(ctx, col);
377 } else if (col < 16) {
378 (fg ? ctx->set_text_fg_bright : ctx->set_text_bg_bright)(ctx, col - 8);
379 } else if (col < 256) {
380 uint32_t rgb_value = col256[col - 16];
381 (fg ? ctx->set_text_fg_rgb : ctx->set_text_bg_rgb)(ctx, rgb_value);
382 }
383
384 break;
385 }
386 default: continue;
387 }
388 }
389 }
390
391out:;
392}
393
394static void dec_private_parse(struct flanterm_context *ctx, uint8_t c) {
395 ctx->dec_private = false;
396
397 if (ctx->esc_values_i == 0) {
398 return;
399 }
400
401 bool set;
402
403 switch (c) {
404 case 'h':
405 set = true; break;
406 case 'l':
407 set = false; break;
408 default:
409 return;
410 }
411
412 switch (ctx->esc_values[0]) {
413 case 25: {
414 if (set) {
415 ctx->cursor_enabled = true;
416 } else {
417 ctx->cursor_enabled = false;
418 }
419 return;
420 }
421 }
422
423 if (ctx->callback != NULL) {
424 ctx->callback(ctx, FLANTERM_CB_DEC, ctx->esc_values_i, (uintptr_t)ctx->esc_values, c);
425 }
426}
427
428static void linux_private_parse(struct flanterm_context *ctx) {
429 if (ctx->esc_values_i == 0) {
430 return;
431 }
432
433 if (ctx->callback != NULL) {
434 ctx->callback(ctx, FLANTERM_CB_LINUX, ctx->esc_values_i, (uintptr_t)ctx->esc_values, 0);
435 }
436}
437
438static void mode_toggle(struct flanterm_context *ctx, uint8_t c) {
439 if (ctx->esc_values_i == 0) {
440 return;
441 }
442
443 bool set;
444
445 switch (c) {
446 case 'h':
447 set = true; break;
448 case 'l':
449 set = false; break;
450 default:
451 return;
452 }
453
454 switch (ctx->esc_values[0]) {
455 case 4:
456 ctx->insert_mode = set; return;
457 }
458
459 if (ctx->callback != NULL) {
460 ctx->callback(ctx, FLANTERM_CB_MODE, ctx->esc_values_i, (uintptr_t)ctx->esc_values, c);
461 }
462}
463
464static void osc_parse(struct flanterm_context *ctx, uint8_t c) {
465 if (ctx->osc_escape && c == '\\') {
466 goto cleanup;
467 }
468
469 ctx->osc_escape = false;
470
471 switch (c) {
472 case 0x1b:
473 ctx->osc_escape = true;
474 return;
475 case '\a':
476 default:
477 break;
478 }
479
480cleanup:
481 ctx->osc_escape = false;
482 ctx->osc = false;
483 ctx->escape = false;
484}
485
486static void control_sequence_parse(struct flanterm_context *ctx, uint8_t c) {
487 if (ctx->escape_offset == 2) {
488 switch (c) {
489 case '[':
490 ctx->discard_next = true;
491 goto cleanup;
492 case '?':
493 ctx->dec_private = true;
494 return;
495 }
496 }
497
498 if (c >= '0' && c <= '9') {
499 if (ctx->esc_values_i == FLANTERM_MAX_ESC_VALUES) {
500 return;
501 }
502 ctx->rrr = true;
503 ctx->esc_values[ctx->esc_values_i] *= 10;
504 ctx->esc_values[ctx->esc_values_i] += c - '0';
505 return;
506 }
507
508 if (ctx->rrr == true) {
509 ctx->esc_values_i++;
510 ctx->rrr = false;
511 if (c == ';')
512 return;
513 } else if (c == ';') {
514 if (ctx->esc_values_i == FLANTERM_MAX_ESC_VALUES) {
515 return;
516 }
517 ctx->esc_values[ctx->esc_values_i] = 0;
518 ctx->esc_values_i++;
519 return;
520 }
521
522 size_t esc_default;
523 switch (c) {
524 case 'J': case 'K': case 'q':
525 esc_default = 0; break;
526 default:
527 esc_default = 1; break;
528 }
529
530 for (size_t i = ctx->esc_values_i; i < FLANTERM_MAX_ESC_VALUES; i++) {
531 ctx->esc_values[i] = esc_default;
532 }
533
534 if (ctx->dec_private == true) {
535 dec_private_parse(ctx, c);
536 goto cleanup;
537 }
538
539 bool r = ctx->scroll_enabled;
540 ctx->scroll_enabled = false;
541 size_t x, y;
542 ctx->get_cursor_pos(ctx, &x, &y);
543
544 switch (c) {
545 case 'F':
546 x = 0;
547 // FALLTHRU
548 case 'A': {
549 if (ctx->esc_values[0] > y)
550 ctx->esc_values[0] = y;
551 size_t orig_y = y;
552 size_t dest_y = y - ctx->esc_values[0];
553 bool will_be_in_scroll_region = false;
554 if ((ctx->scroll_top_margin >= dest_y && ctx->scroll_top_margin <= orig_y)
555 || (ctx->scroll_bottom_margin >= dest_y && ctx->scroll_bottom_margin <= orig_y)) {
556 will_be_in_scroll_region = true;
557 }
558 if (will_be_in_scroll_region && dest_y < ctx->scroll_top_margin) {
559 dest_y = ctx->scroll_top_margin;
560 }
561 ctx->set_cursor_pos(ctx, x, dest_y);
562 break;
563 }
564 case 'E':
565 x = 0;
566 // FALLTHRU
567 case 'e':
568 case 'B': {
569 if (y + ctx->esc_values[0] > ctx->rows - 1)
570 ctx->esc_values[0] = (ctx->rows - 1) - y;
571 size_t orig_y = y;
572 size_t dest_y = y + ctx->esc_values[0];
573 bool will_be_in_scroll_region = false;
574 if ((ctx->scroll_top_margin >= orig_y && ctx->scroll_top_margin <= dest_y)
575 || (ctx->scroll_bottom_margin >= orig_y && ctx->scroll_bottom_margin <= dest_y)) {
576 will_be_in_scroll_region = true;
577 }
578 if (will_be_in_scroll_region && dest_y >= ctx->scroll_bottom_margin) {
579 dest_y = ctx->scroll_bottom_margin - 1;
580 }
581 ctx->set_cursor_pos(ctx, x, dest_y);
582 break;
583 }
584 case 'a':
585 case 'C':
586 if (x + ctx->esc_values[0] > ctx->cols - 1)
587 ctx->esc_values[0] = (ctx->cols - 1) - x;
588 ctx->set_cursor_pos(ctx, x + ctx->esc_values[0], y);
589 break;
590 case 'D':
591 if (ctx->esc_values[0] > x)
592 ctx->esc_values[0] = x;
593 ctx->set_cursor_pos(ctx, x - ctx->esc_values[0], y);
594 break;
595 case 'c':
596 if (ctx->callback != NULL) {
597 ctx->callback(ctx, FLANTERM_CB_PRIVATE_ID, 0, 0, 0);
598 }
599 break;
600 case 'd':
601 ctx->esc_values[0] -= 1;
602 if (ctx->esc_values[0] >= ctx->rows)
603 ctx->esc_values[0] = ctx->rows - 1;
604 ctx->set_cursor_pos(ctx, x, ctx->esc_values[0]);
605 break;
606 case 'G':
607 case '`':
608 ctx->esc_values[0] -= 1;
609 if (ctx->esc_values[0] >= ctx->cols)
610 ctx->esc_values[0] = ctx->cols - 1;
611 ctx->set_cursor_pos(ctx, ctx->esc_values[0], y);
612 break;
613 case 'H':
614 case 'f':
615 if (ctx->esc_values[0] != 0) {
616 ctx->esc_values[0]--;
617 }
618 if (ctx->esc_values[1] != 0) {
619 ctx->esc_values[1]--;
620 }
621 if (ctx->esc_values[1] >= ctx->cols)
622 ctx->esc_values[1] = ctx->cols - 1;
623 if (ctx->esc_values[0] >= ctx->rows)
624 ctx->esc_values[0] = ctx->rows - 1;
625 ctx->set_cursor_pos(ctx, ctx->esc_values[1], ctx->esc_values[0]);
626 break;
627 case 'M': {
628 size_t count = ctx->esc_values[0] > ctx->rows ? ctx->rows : ctx->esc_values[0];
629 for (size_t i = 0; i < count; i++) {
630 ctx->scroll(ctx);
631 }
632 break;
633 }
634 case 'L': {
635 size_t old_scroll_top_margin = ctx->scroll_top_margin;
636 ctx->scroll_top_margin = y;
637 size_t count = ctx->esc_values[0] > ctx->rows ? ctx->rows : ctx->esc_values[0];
638 for (size_t i = 0; i < count; i++) {
639 ctx->revscroll(ctx);
640 }
641 ctx->scroll_top_margin = old_scroll_top_margin;
642 break;
643 }
644 case 'n':
645 switch (ctx->esc_values[0]) {
646 case 5:
647 if (ctx->callback != NULL) {
648 ctx->callback(ctx, FLANTERM_CB_STATUS_REPORT, 0, 0, 0);
649 }
650 break;
651 case 6:
652 if (ctx->callback != NULL) {
653 ctx->callback(ctx, FLANTERM_CB_POS_REPORT, x + 1, y + 1, 0);
654 }
655 break;
656 }
657 break;
658 case 'q':
659 if (ctx->callback != NULL) {
660 ctx->callback(ctx, FLANTERM_CB_KBD_LEDS, ctx->esc_values[0], 0, 0);
661 }
662 break;
663 case 'J':
664 switch (ctx->esc_values[0]) {
665 case 0: {
666 size_t rows_remaining = ctx->rows - (y + 1);
667 size_t cols_diff = ctx->cols - (x + 1);
668 size_t to_clear = rows_remaining * ctx->cols + cols_diff + 1;
669 for (size_t i = 0; i < to_clear; i++) {
670 ctx->raw_putchar(ctx, ' ');
671 }
672 ctx->set_cursor_pos(ctx, x, y);
673 break;
674 }
675 case 1: {
676 ctx->set_cursor_pos(ctx, 0, 0);
677 bool b = false;
678 for (size_t yc = 0; yc < ctx->rows; yc++) {
679 for (size_t xc = 0; xc < ctx->cols; xc++) {
680 ctx->raw_putchar(ctx, ' ');
681 if (xc == x && yc == y) {
682 ctx->set_cursor_pos(ctx, x, y);
683 b = true;
684 break;
685 }
686 }
687 if (b == true)
688 break;
689 }
690 break;
691 }
692 case 2:
693 case 3:
694 ctx->clear(ctx, false);
695 break;
696 }
697 break;
698 case '@':
699 for (size_t i = ctx->cols - 1; ; i--) {
700 ctx->move_character(ctx, i + ctx->esc_values[0], y, i, y);
701 ctx->set_cursor_pos(ctx, i, y);
702 ctx->raw_putchar(ctx, ' ');
703 if (i == x) {
704 break;
705 }
706 }
707 ctx->set_cursor_pos(ctx, x, y);
708 break;
709 case 'P':
710 for (size_t i = x + ctx->esc_values[0]; i < ctx->cols; i++)
711 ctx->move_character(ctx, i - ctx->esc_values[0], y, i, y);
712 ctx->set_cursor_pos(ctx, ctx->cols - ctx->esc_values[0], y);
713 // FALLTHRU
714 case 'X': {
715 size_t count = ctx->esc_values[0] > ctx->cols ? ctx->cols : ctx->esc_values[0];
716 for (size_t i = 0; i < count; i++)
717 ctx->raw_putchar(ctx, ' ');
718 ctx->set_cursor_pos(ctx, x, y);
719 break;
720 }
721 case 'm':
722 sgr(ctx);
723 break;
724 case 's':
725 ctx->get_cursor_pos(ctx, &ctx->saved_cursor_x, &ctx->saved_cursor_y);
726 break;
727 case 'u':
728 ctx->set_cursor_pos(ctx, ctx->saved_cursor_x, ctx->saved_cursor_y);
729 break;
730 case 'K':
731 switch (ctx->esc_values[0]) {
732 case 0: {
733 for (size_t i = x; i < ctx->cols; i++)
734 ctx->raw_putchar(ctx, ' ');
735 ctx->set_cursor_pos(ctx, x, y);
736 break;
737 }
738 case 1: {
739 ctx->set_cursor_pos(ctx, 0, y);
740 for (size_t i = 0; i < x; i++)
741 ctx->raw_putchar(ctx, ' ');
742 break;
743 }
744 case 2: {
745 ctx->set_cursor_pos(ctx, 0, y);
746 for (size_t i = 0; i < ctx->cols; i++)
747 ctx->raw_putchar(ctx, ' ');
748 ctx->set_cursor_pos(ctx, x, y);
749 break;
750 }
751 }
752 break;
753 case 'r':
754 if (ctx->esc_values[0] == 0) {
755 ctx->esc_values[0] = 1;
756 }
757 if (ctx->esc_values[1] == 0) {
758 ctx->esc_values[1] = 1;
759 }
760 ctx->scroll_top_margin = 0;
761 ctx->scroll_bottom_margin = ctx->rows;
762 if (ctx->esc_values_i > 0) {
763 ctx->scroll_top_margin = ctx->esc_values[0] - 1;
764 }
765 if (ctx->esc_values_i > 1) {
766 ctx->scroll_bottom_margin = ctx->esc_values[1];
767 }
768 if (ctx->scroll_top_margin >= ctx->rows
769 || ctx->scroll_bottom_margin > ctx->rows
770 || ctx->scroll_top_margin >= (ctx->scroll_bottom_margin - 1)) {
771 ctx->scroll_top_margin = 0;
772 ctx->scroll_bottom_margin = ctx->rows;
773 }
774 ctx->set_cursor_pos(ctx, 0, 0);
775 break;
776 case 'l':
777 case 'h':
778 mode_toggle(ctx, c);
779 break;
780 case ']':
781 linux_private_parse(ctx);
782 break;
783 }
784
785 ctx->scroll_enabled = r;
786
787cleanup:
788 ctx->control_sequence = false;
789 ctx->escape = false;
790}
791
792static void restore_state(struct flanterm_context *ctx) {
793 ctx->bold = ctx->saved_state_bold;
794 ctx->bg_bold = ctx->saved_state_bg_bold;
795 ctx->reverse_video = ctx->saved_state_reverse_video;
796 ctx->current_charset = ctx->saved_state_current_charset;
797 ctx->current_primary = ctx->saved_state_current_primary;
798 ctx->current_bg = ctx->saved_state_current_bg;
799
800 ctx->restore_state(ctx);
801}
802
803static void save_state(struct flanterm_context *ctx) {
804 ctx->save_state(ctx);
805
806 ctx->saved_state_bold = ctx->bold;
807 ctx->saved_state_bg_bold = ctx->bg_bold;
808 ctx->saved_state_reverse_video = ctx->reverse_video;
809 ctx->saved_state_current_charset = ctx->current_charset;
810 ctx->saved_state_current_primary = ctx->current_primary;
811 ctx->saved_state_current_bg = ctx->current_bg;
812}
813
814static void escape_parse(struct flanterm_context *ctx, uint8_t c) {
815 ctx->escape_offset++;
816
817 if (ctx->osc == true) {
818 osc_parse(ctx, c);
819 return;
820 }
821
822 if (ctx->control_sequence == true) {
823 control_sequence_parse(ctx, c);
824 return;
825 }
826
827 size_t x, y;
828 ctx->get_cursor_pos(ctx, &x, &y);
829
830 switch (c) {
831 case ']':
832 ctx->osc_escape = false;
833 ctx->osc = true;
834 return;
835 case '[':
836 for (size_t i = 0; i < FLANTERM_MAX_ESC_VALUES; i++)
837 ctx->esc_values[i] = 0;
838 ctx->esc_values_i = 0;
839 ctx->rrr = false;
840 ctx->control_sequence = true;
841 return;
842 case '7':
843 save_state(ctx);
844 break;
845 case '8':
846 restore_state(ctx);
847 break;
848 case 'c':
849 flanterm_context_reinit(ctx);
850 ctx->clear(ctx, true);
851 break;
852 case 'D':
853 if (y == ctx->scroll_bottom_margin - 1) {
854 ctx->scroll(ctx);
855 ctx->set_cursor_pos(ctx, x, y);
856 } else {
857 ctx->set_cursor_pos(ctx, x, y + 1);
858 }
859 break;
860 case 'E':
861 if (y == ctx->scroll_bottom_margin - 1) {
862 ctx->scroll(ctx);
863 ctx->set_cursor_pos(ctx, 0, y);
864 } else {
865 ctx->set_cursor_pos(ctx, 0, y + 1);
866 }
867 break;
868 case 'M':
869 // "Reverse linefeed"
870 if (y == ctx->scroll_top_margin) {
871 ctx->revscroll(ctx);
872 ctx->set_cursor_pos(ctx, 0, y);
873 } else {
874 ctx->set_cursor_pos(ctx, 0, y - 1);
875 }
876 break;
877 case 'Z':
878 if (ctx->callback != NULL) {
879 ctx->callback(ctx, FLANTERM_CB_PRIVATE_ID, 0, 0, 0);
880 }
881 break;
882 case '(':
883 case ')':
884 ctx->g_select = c - '\'';
885 break;
886 }
887
888 ctx->escape = false;
889}
890
891static bool dec_special_print(struct flanterm_context *ctx, uint8_t c) {
892#define FLANTERM_DEC_SPCL_PRN(C) ctx->raw_putchar(ctx, (C)); return true;
893 switch (c) {
894 case '`': FLANTERM_DEC_SPCL_PRN(0x04)
895 case '0': FLANTERM_DEC_SPCL_PRN(0xdb)
896 case '-': FLANTERM_DEC_SPCL_PRN(0x18)
897 case ',': FLANTERM_DEC_SPCL_PRN(0x1b)
898 case '.': FLANTERM_DEC_SPCL_PRN(0x19)
899 case 'a': FLANTERM_DEC_SPCL_PRN(0xb1)
900 case 'f': FLANTERM_DEC_SPCL_PRN(0xf8)
901 case 'g': FLANTERM_DEC_SPCL_PRN(0xf1)
902 case 'h': FLANTERM_DEC_SPCL_PRN(0xb0)
903 case 'j': FLANTERM_DEC_SPCL_PRN(0xd9)
904 case 'k': FLANTERM_DEC_SPCL_PRN(0xbf)
905 case 'l': FLANTERM_DEC_SPCL_PRN(0xda)
906 case 'm': FLANTERM_DEC_SPCL_PRN(0xc0)
907 case 'n': FLANTERM_DEC_SPCL_PRN(0xc5)
908 case 'q': FLANTERM_DEC_SPCL_PRN(0xc4)
909 case 's': FLANTERM_DEC_SPCL_PRN(0x5f)
910 case 't': FLANTERM_DEC_SPCL_PRN(0xc3)
911 case 'u': FLANTERM_DEC_SPCL_PRN(0xb4)
912 case 'v': FLANTERM_DEC_SPCL_PRN(0xc1)
913 case 'w': FLANTERM_DEC_SPCL_PRN(0xc2)
914 case 'x': FLANTERM_DEC_SPCL_PRN(0xb3)
915 case 'y': FLANTERM_DEC_SPCL_PRN(0xf3)
916 case 'z': FLANTERM_DEC_SPCL_PRN(0xf2)
917 case '~': FLANTERM_DEC_SPCL_PRN(0xfa)
918 case '_': FLANTERM_DEC_SPCL_PRN(0xff)
919 case '+': FLANTERM_DEC_SPCL_PRN(0x1a)
920 case '{': FLANTERM_DEC_SPCL_PRN(0xe3)
921 case '}': FLANTERM_DEC_SPCL_PRN(0x9c)
922 }
923#undef FLANTERM_DEC_SPCL_PRN
924
925 return false;
926}
927
928// Following wcwidth related code inherited from:
929// https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
930
931struct interval {
932 uint32_t first;
933 uint32_t last;
934};
935
936/* auxiliary function for binary search in interval table */
937static int bisearch(uint32_t ucs, const struct interval *table, int max) {
938 int min = 0;
939 int mid;
940
941 if (ucs < table[0].first || ucs > table[max].last)
942 return 0;
943 while (max >= min) {
944 mid = (min + max) / 2;
945 if (ucs > table[mid].last)
946 min = mid + 1;
947 else if (ucs < table[mid].first)
948 max = mid - 1;
949 else
950 return 1;
951 }
952
953 return 0;
954}
955
956int mk_wcwidth(uint32_t ucs) {
957 /* sorted list of non-overlapping intervals of non-spacing characters */
958 /* generated by "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c" */
959 static const struct interval combining[] = {
960 { 0x0300, 0x036F }, { 0x0483, 0x0486 }, { 0x0488, 0x0489 },
961 { 0x0591, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
962 { 0x05C4, 0x05C5 }, { 0x05C7, 0x05C7 }, { 0x0600, 0x0603 },
963 { 0x0610, 0x0615 }, { 0x064B, 0x065E }, { 0x0670, 0x0670 },
964 { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
965 { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
966 { 0x07A6, 0x07B0 }, { 0x07EB, 0x07F3 }, { 0x0901, 0x0902 },
967 { 0x093C, 0x093C }, { 0x0941, 0x0948 }, { 0x094D, 0x094D },
968 { 0x0951, 0x0954 }, { 0x0962, 0x0963 }, { 0x0981, 0x0981 },
969 { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD },
970 { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 }, { 0x0A3C, 0x0A3C },
971 { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D },
972 { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC },
973 { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD },
974 { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 }, { 0x0B3C, 0x0B3C },
975 { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 }, { 0x0B4D, 0x0B4D },
976 { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 }, { 0x0BC0, 0x0BC0 },
977 { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 }, { 0x0C46, 0x0C48 },
978 { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 }, { 0x0CBC, 0x0CBC },
979 { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
980 { 0x0CE2, 0x0CE3 }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
981 { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
982 { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
983 { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
984 { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
985 { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
986 { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
987 { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
988 { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
989 { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x135F, 0x135F },
990 { 0x1712, 0x1714 }, { 0x1732, 0x1734 }, { 0x1752, 0x1753 },
991 { 0x1772, 0x1773 }, { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD },
992 { 0x17C6, 0x17C6 }, { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD },
993 { 0x180B, 0x180D }, { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 },
994 { 0x1927, 0x1928 }, { 0x1932, 0x1932 }, { 0x1939, 0x193B },
995 { 0x1A17, 0x1A18 }, { 0x1B00, 0x1B03 }, { 0x1B34, 0x1B34 },
996 { 0x1B36, 0x1B3A }, { 0x1B3C, 0x1B3C }, { 0x1B42, 0x1B42 },
997 { 0x1B6B, 0x1B73 }, { 0x1DC0, 0x1DCA }, { 0x1DFE, 0x1DFF },
998 { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x2060, 0x2063 },
999 { 0x206A, 0x206F }, { 0x20D0, 0x20EF }, { 0x302A, 0x302F },
1000 { 0x3099, 0x309A }, { 0xA806, 0xA806 }, { 0xA80B, 0xA80B },
1001 { 0xA825, 0xA826 }, { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F },
1002 { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB },
1003 { 0x10A01, 0x10A03 }, { 0x10A05, 0x10A06 }, { 0x10A0C, 0x10A0F },
1004 { 0x10A38, 0x10A3A }, { 0x10A3F, 0x10A3F }, { 0x1D167, 0x1D169 },
1005 { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B }, { 0x1D1AA, 0x1D1AD },
1006 { 0x1D242, 0x1D244 }, { 0xE0001, 0xE0001 }, { 0xE0020, 0xE007F },
1007 { 0xE0100, 0xE01EF }
1008 };
1009
1010 /* test for 8-bit control characters */
1011 if (ucs == 0)
1012 return 0;
1013 if (ucs < 32 || (ucs >= 0x7f && ucs < 0xa0))
1014 return 1;
1015
1016 /* binary search in table of non-spacing characters */
1017 if (bisearch(ucs, combining,
1018 sizeof(combining) / sizeof(struct interval) - 1))
1019 return 0;
1020
1021 /* if we arrive here, ucs is not a combining or C0/C1 control character */
1022
1023 return 1 +
1024 (ucs >= 0x1100 &&
1025 (ucs <= 0x115f || /* Hangul Jamo init. consonants */
1026 ucs == 0x2329 || ucs == 0x232a ||
1027 (ucs >= 0x2e80 && ucs <= 0xa4cf &&
1028 ucs != 0x303f) || /* CJK ... Yi */
1029 (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
1030 (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
1031 (ucs >= 0xfe10 && ucs <= 0xfe19) || /* Vertical forms */
1032 (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
1033 (ucs >= 0xff00 && ucs <= 0xff60) || /* Fullwidth Forms */
1034 (ucs >= 0xffe0 && ucs <= 0xffe6) ||
1035 (ucs >= 0x20000 && ucs <= 0x2fffd) ||
1036 (ucs >= 0x30000 && ucs <= 0x3fffd)));
1037}
1038
1039// End of https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c inherited code
1040
1041static int unicode_to_cp437(uint64_t code_point) {
1042 switch (code_point) {
1043 case 0x263a: return 1;
1044 case 0x263b: return 2;
1045 case 0x2665: return 3;
1046 case 0x2666: return 4;
1047 case 0x2663: return 5;
1048 case 0x2660: return 6;
1049 case 0x2022: return 7;
1050 case 0x25d8: return 8;
1051 case 0x25cb: return 9;
1052 case 0x25d9: return 10;
1053 case 0x2642: return 11;
1054 case 0x2640: return 12;
1055 case 0x266a: return 13;
1056 case 0x266b: return 14;
1057 case 0x263c: return 15;
1058 case 0x25ba: return 16;
1059 case 0x25c4: return 17;
1060 case 0x2195: return 18;
1061 case 0x203c: return 19;
1062 case 0x00b6: return 20;
1063 case 0x00a7: return 21;
1064 case 0x25ac: return 22;
1065 case 0x21a8: return 23;
1066 case 0x2191: return 24;
1067 case 0x2193: return 25;
1068 case 0x2192: return 26;
1069 case 0x2190: return 27;
1070 case 0x221f: return 28;
1071 case 0x2194: return 29;
1072 case 0x25b2: return 30;
1073 case 0x25bc: return 31;
1074
1075 case 0x2302: return 127;
1076 case 0x00c7: return 128;
1077 case 0x00fc: return 129;
1078 case 0x00e9: return 130;
1079 case 0x00e2: return 131;
1080 case 0x00e4: return 132;
1081 case 0x00e0: return 133;
1082 case 0x00e5: return 134;
1083 case 0x00e7: return 135;
1084 case 0x00ea: return 136;
1085 case 0x00eb: return 137;
1086 case 0x00e8: return 138;
1087 case 0x00ef: return 139;
1088 case 0x00ee: return 140;
1089 case 0x00ec: return 141;
1090 case 0x00c4: return 142;
1091 case 0x00c5: return 143;
1092 case 0x00c9: return 144;
1093 case 0x00e6: return 145;
1094 case 0x00c6: return 146;
1095 case 0x00f4: return 147;
1096 case 0x00f6: return 148;
1097 case 0x00f2: return 149;
1098 case 0x00fb: return 150;
1099 case 0x00f9: return 151;
1100 case 0x00ff: return 152;
1101 case 0x00d6: return 153;
1102 case 0x00dc: return 154;
1103 case 0x00a2: return 155;
1104 case 0x00a3: return 156;
1105 case 0x00a5: return 157;
1106 case 0x20a7: return 158;
1107 case 0x0192: return 159;
1108 case 0x00e1: return 160;
1109 case 0x00ed: return 161;
1110 case 0x00f3: return 162;
1111 case 0x00fa: return 163;
1112 case 0x00f1: return 164;
1113 case 0x00d1: return 165;
1114 case 0x00aa: return 166;
1115 case 0x00ba: return 167;
1116 case 0x00bf: return 168;
1117 case 0x2310: return 169;
1118 case 0x00ac: return 170;
1119 case 0x00bd: return 171;
1120 case 0x00bc: return 172;
1121 case 0x00a1: return 173;
1122 case 0x00ab: return 174;
1123 case 0x00bb: return 175;
1124 case 0x2591: return 176;
1125 case 0x2592: return 177;
1126 case 0x2593: return 178;
1127 case 0x2502: return 179;
1128 case 0x2524: return 180;
1129 case 0x2561: return 181;
1130 case 0x2562: return 182;
1131 case 0x2556: return 183;
1132 case 0x2555: return 184;
1133 case 0x2563: return 185;
1134 case 0x2551: return 186;
1135 case 0x2557: return 187;
1136 case 0x255d: return 188;
1137 case 0x255c: return 189;
1138 case 0x255b: return 190;
1139 case 0x2510: return 191;
1140 case 0x2514: return 192;
1141 case 0x2534: return 193;
1142 case 0x252c: return 194;
1143 case 0x251c: return 195;
1144 case 0x2500: return 196;
1145 case 0x253c: return 197;
1146 case 0x255e: return 198;
1147 case 0x255f: return 199;
1148 case 0x255a: return 200;
1149 case 0x2554: return 201;
1150 case 0x2569: return 202;
1151 case 0x2566: return 203;
1152 case 0x2560: return 204;
1153 case 0x2550: return 205;
1154 case 0x256c: return 206;
1155 case 0x2567: return 207;
1156 case 0x2568: return 208;
1157 case 0x2564: return 209;
1158 case 0x2565: return 210;
1159 case 0x2559: return 211;
1160 case 0x2558: return 212;
1161 case 0x2552: return 213;
1162 case 0x2553: return 214;
1163 case 0x256b: return 215;
1164 case 0x256a: return 216;
1165 case 0x2518: return 217;
1166 case 0x250c: return 218;
1167 case 0x2588: return 219;
1168 case 0x2584: return 220;
1169 case 0x258c: return 221;
1170 case 0x2590: return 222;
1171 case 0x2580: return 223;
1172 case 0x03b1: return 224;
1173 case 0x00df: return 225;
1174 case 0x0393: return 226;
1175 case 0x03c0: return 227;
1176 case 0x03a3: return 228;
1177 case 0x03c3: return 229;
1178 case 0x00b5: return 230;
1179 case 0x03c4: return 231;
1180 case 0x03a6: return 232;
1181 case 0x0398: return 233;
1182 case 0x03a9: return 234;
1183 case 0x03b4: return 235;
1184 case 0x221e: return 236;
1185 case 0x03c6: return 237;
1186 case 0x03b5: return 238;
1187 case 0x2229: return 239;
1188 case 0x2261: return 240;
1189 case 0x00b1: return 241;
1190 case 0x2265: return 242;
1191 case 0x2264: return 243;
1192 case 0x2320: return 244;
1193 case 0x2321: return 245;
1194 case 0x00f7: return 246;
1195 case 0x2248: return 247;
1196 case 0x00b0: return 248;
1197 case 0x2219: return 249;
1198 case 0x00b7: return 250;
1199 case 0x221a: return 251;
1200 case 0x207f: return 252;
1201 case 0x00b2: return 253;
1202 case 0x25a0: return 254;
1203 }
1204
1205 return -1;
1206}
1207
1208static void flanterm_putchar(struct flanterm_context *ctx, uint8_t c) {
1209 if (ctx->discard_next || (c == 0x18 || c == 0x1a)) {
1210 ctx->discard_next = false;
1211 ctx->escape = false;
1212 ctx->control_sequence = false;
1213 ctx->unicode_remaining = 0;
1214 ctx->osc = false;
1215 ctx->osc_escape = false;
1216 ctx->g_select = 0;
1217 return;
1218 }
1219
1220 if (ctx->unicode_remaining != 0) {
1221 if ((c & 0xc0) != 0x80) {
1222 ctx->unicode_remaining = 0;
1223 goto unicode_error;
1224 }
1225
1226 ctx->unicode_remaining--;
1227 ctx->code_point |= (uint64_t)(c & 0x3f) << (6 * ctx->unicode_remaining);
1228 if (ctx->unicode_remaining != 0) {
1229 return;
1230 }
1231
1232 int cc = unicode_to_cp437(ctx->code_point);
1233
1234 if (cc == -1) {
1235 size_t replacement_width = (size_t)mk_wcwidth(ctx->code_point);
1236 if (replacement_width > 0) {
1237 ctx->raw_putchar(ctx, 0xfe);
1238 }
1239 for (size_t i = 1; i < replacement_width; i++) {
1240 ctx->raw_putchar(ctx, ' ');
1241 }
1242 } else {
1243 ctx->raw_putchar(ctx, cc);
1244 }
1245 return;
1246 }
1247
1248unicode_error:
1249 if (c >= 0xc0 && c <= 0xf7) {
1250 if (c >= 0xc0 && c <= 0xdf) {
1251 ctx->unicode_remaining = 1;
1252 ctx->code_point = (uint64_t)(c & 0x1f) << 6;
1253 } else if (c >= 0xe0 && c <= 0xef) {
1254 ctx->unicode_remaining = 2;
1255 ctx->code_point = (uint64_t)(c & 0x0f) << (6 * 2);
1256 } else if (c >= 0xf0 && c <= 0xf7) {
1257 ctx->unicode_remaining = 3;
1258 ctx->code_point = (uint64_t)(c & 0x07) << (6 * 3);
1259 }
1260 return;
1261 }
1262
1263 if (ctx->escape == true) {
1264 escape_parse(ctx, c);
1265 return;
1266 }
1267
1268 if (ctx->g_select) {
1269 ctx->g_select--;
1270 switch (c) {
1271 case 'B':
1272 ctx->charsets[ctx->g_select] = CHARSET_DEFAULT; break;
1273 case '0':
1274 ctx->charsets[ctx->g_select] = CHARSET_DEC_SPECIAL; break;
1275 }
1276 ctx->g_select = 0;
1277 return;
1278 }
1279
1280 size_t x, y;
1281 ctx->get_cursor_pos(ctx, &x, &y);
1282
1283 switch (c) {
1284 case 0x00:
1285 case 0x7f:
1286 return;
1287 case 0x1b:
1288 ctx->escape_offset = 0;
1289 ctx->escape = true;
1290 return;
1291 case '\t':
1292 if ((x / ctx->tab_size + 1) >= ctx->cols) {
1293 ctx->set_cursor_pos(ctx, ctx->cols - 1, y);
1294 return;
1295 }
1296 ctx->set_cursor_pos(ctx, (x / ctx->tab_size + 1) * ctx->tab_size, y);
1297 return;
1298 case 0x0b:
1299 case 0x0c:
1300 case '\n':
1301 if (y == ctx->scroll_bottom_margin - 1) {
1302 ctx->scroll(ctx);
1303 ctx->set_cursor_pos(ctx, (ctx->oob_output & FLANTERM_OOB_OUTPUT_ONLCR) ? 0 : x, y);
1304 } else {
1305 ctx->set_cursor_pos(ctx, (ctx->oob_output & FLANTERM_OOB_OUTPUT_ONLCR) ? 0 : x, y + 1);
1306 }
1307 return;
1308 case '\b':
1309 ctx->set_cursor_pos(ctx, x - 1, y);
1310 return;
1311 case '\r':
1312 ctx->set_cursor_pos(ctx, 0, y);
1313 return;
1314 case '\a':
1315 // The bell is handled by the kernel
1316 if (ctx->callback != NULL) {
1317 ctx->callback(ctx, FLANTERM_CB_BELL, 0, 0, 0);
1318 }
1319 return;
1320 case 14:
1321 // Move to G1 set
1322 ctx->current_charset = 1;
1323 return;
1324 case 15:
1325 // Move to G0 set
1326 ctx->current_charset = 0;
1327 return;
1328 }
1329
1330 if (ctx->insert_mode == true) {
1331 for (size_t i = ctx->cols - 1; ; i--) {
1332 ctx->move_character(ctx, i + 1, y, i, y);
1333 if (i == x) {
1334 break;
1335 }
1336 }
1337 }
1338
1339 // Translate character set
1340 switch (ctx->charsets[ctx->current_charset]) {
1341 case CHARSET_DEFAULT:
1342 break;
1343 case CHARSET_DEC_SPECIAL:
1344 if (dec_special_print(ctx, c)) {
1345 return;
1346 }
1347 break;
1348 }
1349
1350 if (c >= 0x20 && c <= 0x7e) {
1351 ctx->raw_putchar(ctx, c);
1352 } else {
1353 ctx->raw_putchar(ctx, 0xfe);
1354 }
1355}
1356
1357void flanterm_flush(struct flanterm_context *ctx) {
1358 ctx->double_buffer_flush(ctx);
1359}
1360
1361void flanterm_full_refresh(struct flanterm_context *ctx) {
1362 ctx->full_refresh(ctx);
1363}
1364
1365void flanterm_deinit(struct flanterm_context *ctx, void (*_free)(void *, size_t)) {
1366 ctx->deinit(ctx, _free);
1367}
1368
1369void flanterm_get_dimensions(struct flanterm_context *ctx, size_t *cols, size_t *rows) {
1370 *cols = ctx->cols;
1371 *rows = ctx->rows;
1372}
1373
1374void flanterm_set_autoflush(struct flanterm_context *ctx, bool state) {
1375 ctx->autoflush = state;
1376}
1377
1378void flanterm_set_callback(struct flanterm_context *ctx, void (*callback)(struct flanterm_context *, uint64_t, uint64_t, uint64_t, uint64_t)) {
1379 ctx->callback = callback;
1380}
1381
1382uint64_t flanterm_get_oob_output(struct flanterm_context *ctx) {
1383 return ctx->oob_output;
1384}
1385
1386void flanterm_set_oob_output(struct flanterm_context *ctx, uint64_t oob_output) {
1387 ctx->oob_output = oob_output;
1388}