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