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