Codebergdnkl

foot

master
ply5 git calls519 ms34 KiB3 cachedprotocol v2
1133 lines41 KiBRaw
1#include "vt.h"
3#include <stdlib.h>
4#include <string.h>
5#include <unistd.h>
7#if defined(FOOT_GRAPHEME_CLUSTERING)
8 #include <utf8proc.h>
9#endif
11#define LOG_MODULE "vt"
12#define LOG_ENABLE_DBG 0
13#include "log.h"
14#include "char32.h"
15#include "config.h"
16#include "csi.h"
17#include "dcs.h"
18#include "debug.h"
19#include "osc.h"
20#include "sixel.h"
21#include "util.h"
22#include "xmalloc.h"
24#define UNHANDLED() LOG_DBG("unhandled: %s", esc_as_string(term, final))
26/* https://vt100.net/emu/dec_ansi_parser */
28enum state {
29 STATE_GROUND,
30 STATE_ESCAPE,
31 STATE_ESCAPE_INTERMEDIATE,
33 STATE_CSI_ENTRY,
34 STATE_CSI_PARAM,
35 STATE_CSI_INTERMEDIATE,
36 STATE_CSI_IGNORE,
38 STATE_OSC_STRING,
40 STATE_DCS_ENTRY,
41 STATE_DCS_PARAM,
42 STATE_DCS_INTERMEDIATE,
43 STATE_DCS_IGNORE,
44 STATE_DCS_PASSTHROUGH,
46 STATE_SOS_PM_APC_STRING,
48 STATE_UTF8_21,
49 STATE_UTF8_31,
50 STATE_UTF8_32,
51 STATE_UTF8_41,
52 STATE_UTF8_42,
53 STATE_UTF8_43,
54};
56#if defined(_DEBUG) && defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG && 0
57static const char *const state_names[] = {
58 [STATE_GROUND] = "ground",
60 [STATE_ESCAPE] = "escape",
61 [STATE_ESCAPE_INTERMEDIATE] = "escape intermediate",
63 [STATE_CSI_ENTRY] = "CSI entry",
64 [STATE_CSI_PARAM] = "CSI param",
65 [STATE_CSI_INTERMEDIATE] = "CSI intermediate",
66 [STATE_CSI_IGNORE] = "CSI ignore",
68 [STATE_OSC_STRING] = "OSC string",
70 [STATE_DCS_ENTRY] = "DCS entry",
71 [STATE_DCS_PARAM] = "DCS param",
72 [STATE_DCS_INTERMEDIATE] = "DCS intermediate",
73 [STATE_DCS_IGNORE] = "DCS ignore",
74 [STATE_DCS_PASSTHROUGH] = "DCS passthrough",
76 [STATE_SOS_PM_APC_STRING] = "sos/pm/apc string",
78 [STATE_UTF8_21] = "UTF8 2-byte 1/2",
79 [STATE_UTF8_31] = "UTF8 3-byte 1/3",
80 [STATE_UTF8_32] = "UTF8 3-byte 2/3",
81};
82#endif
84#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
85static const char *
86esc_as_string(struct terminal *term, uint8_t final)
88 static char msg[1024];
89 int c = snprintf(msg, sizeof(msg), "\\E");
91 for (size_t i = 0; i < sizeof(term->vt.private); i++) {
92 char value = (term->vt.private >> (i * 8)) & 0xff;
93 if (value == 0)
94 break;
95 c += snprintf(&msg[c], sizeof(msg) - c, "%c", value);
96 }
98 xassert(term->vt.params.idx == 0);
100 snprintf(&msg[c], sizeof(msg) - c, "%c", final);
101 return msg;
104#endif
106static void
107action_ignore(struct terminal *term)
111static void
112action_clear(struct terminal *term)
114 term->vt.params.idx = 0;
115 term->vt.private = 0;
118static void
119action_execute(struct terminal *term, uint8_t c)
121 LOG_DBG("execute: 0x%02x", c);
122 switch (c) {
124 /*
125 * 7-bit C0 control characters
126 */
128 case '\0':
129 break;
131 case '\a':
132 /* BEL - bell */
133 term_bell(term);
134 break;
136 case '\b':
137 /* backspace */
138#if 0
139 /*
140 * This is the "correct" BS behavior. However, it doesn't play
141 * nicely with bw/auto_left_margin, hence the alternative
142 * implementation below.
143 *
144 * Note that it breaks vttest "1. Test of cursor movements ->
145 * Test of autowrap"
146 */
147 term_cursor_left(term, 1);
148#else
149 if (term->grid->cursor.lcf)
150 term->grid->cursor.lcf = false;
151 else {
152 /* Reverse wrap */
153 if (unlikely(term->grid->cursor.point.col == 0) &&
154 likely(term->reverse_wrap && term->auto_margin))
155 {
156 if (term->grid->cursor.point.row <= term->scroll_region.start) {
157 /* Don't wrap past, or inside, the scrolling region(?) */
158 } else
159 term_cursor_to(
160 term,
161 term->grid->cursor.point.row - 1,
162 term->cols - 1);
163 } else
164 term_cursor_left(term, 1);
165 }
166#endif
167 break;
169 case '\t': {
170 /* HT - horizontal tab */
171 int start_col = term->grid->cursor.point.col;
172 int new_col = term->cols - 1;
174 tll_foreach(term->tab_stops, it) {
175 if (it->item > start_col) {
176 new_col = it->item;
177 break;
178 }
179 }
180 xassert(new_col >= start_col);
181 xassert(new_col < term->cols);
183 struct row *row = term->grid->cur_row;
185 bool emit_tab_char = (row->cells[start_col].wc == 0 ||
186 row->cells[start_col].wc == U' ');
188 /* Check if all cells from here until the next tab stop are empty */
189 for (const struct cell *cell = &row->cells[start_col + 1];
190 cell < &row->cells[new_col];
191 cell++)
192 {
193 if (!(cell->wc == 0 || cell->wc == U' ')) {
194 emit_tab_char = false;
195 break;
196 }
197 }
199 /*
200 * Emit a tab in current cell, and write spaces to the
201 * subsequent cells, all the way until the next tab stop.
202 */
203 if (emit_tab_char) {
204 row->dirty = true;
206 row->cells[start_col].wc = U'\t';
207 row->cells[start_col].attrs.clean = 0;
209 for (struct cell *cell = &row->cells[start_col + 1];
210 cell < &row->cells[new_col];
211 cell++)
212 {
213 cell->wc = U' ';
214 cell->attrs.clean = 0;
215 }
216 }
218 /* According to the specification, HT _should_ cancel LCF. But
219 * XTerm, and nearly all other emulators, don't. So we follow
220 * suit */
221 bool lcf = term->grid->cursor.lcf;
222 term_cursor_right(term, new_col - start_col);
223 term->grid->cursor.lcf = lcf;
224 break;
225 }
227 case '\n':
228 case '\v':
229 case '\f':
230 /* LF - \n - line feed */
231 /* VT - \v - vertical tab */
232 /* FF - \f - form feed */
233 term_linefeed(term);
234 break;
236 case '\r':
237 /* CR - carriage ret */
238 term_carriage_return(term);
239 break;
241 case '\x0e':
242 /* SO - shift out */
243 term->charsets.selected = G1;
244 term->bits_affecting_ascii_printer.charset =
245 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
246 term_update_ascii_printer(term);
247 break;
249 case '\x0f':
250 /* SI - shift in */
251 term->charsets.selected = G0;
252 term->bits_affecting_ascii_printer.charset =
253 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
254 term_update_ascii_printer(term);
255 break;
257 /*
258 * 8-bit C1 control characters
259 *
260 * We ignore these, but keep them here for reference, along
261 * with their corresponding 7-bit variants.
262 *
263 * As far as I can tell, XTerm also ignores these _when in
264 * UTF-8 mode_. Which would be the normal mode of operation
265 * these days. And since we _only_ support UTF-8...
266 */
268#if 0
269 case '\x84': /* IND -> ESC D */
270 case '\x85': /* NEL -> ESC E */
271 case '\x88': /* Tab Set -> ESC H */
272 case '\x8d': /* RI -> ESC M */
273 case '\x8e': /* SS2 -> ESC N */
274 case '\x8f': /* SS3 -> ESC O */
275 case '\x90': /* DCS -> ESC P */
276 case '\x96': /* SPA -> ESC V */
277 case '\x97': /* EPA -> ESC W */
278 case '\x98': /* SOS -> ESC X */
279 case '\x9a': /* DECID -> ESC Z (obsolete form of CSI c) */
280 case '\x9b': /* CSI -> ESC [ */
281 case '\x9c': /* ST -> ESC \ */
282 case '\x9d': /* OSC -> ESC ] */
283 case '\x9e': /* PM -> ESC ^ */
284 case '\x9f': /* APC -> ESC _ */
285 break;
286#endif
288 default:
289 break;
290 }
293static void
294action_print(struct terminal *term, uint8_t c)
296 term_reset_grapheme_state(term);
297 term->ascii_printer(term, c);
300static void
301action_param_lazy_init(struct terminal *term)
303 if (term->vt.params.idx == 0) {
304 struct vt_param *param = &term->vt.params.v[0];
306 term->vt.params.cur = param;
307 param->value = 0;
308 param->sub.idx = 0;
309 param->sub.cur = NULL;
310 term->vt.params.idx = 1;
311 }
314static void
315action_param_new(struct terminal *term, uint8_t c)
317 xassert(c == ';');
318 action_param_lazy_init(term);
320 const size_t max_params
321 = sizeof(term->vt.params.v) / sizeof(term->vt.params.v[0]);
323 struct vt_param *param;
325 if (unlikely(term->vt.params.idx >= max_params)) {
326 static bool have_warned = false;
327 if (!have_warned) {
328 have_warned = true;
329 LOG_WARN(
330 "unsupported: escape with more than %zu parameters "
331 "(will not warn again)",
332 sizeof(term->vt.params.v) / sizeof(term->vt.params.v[0]));
333 }
334 param = &term->vt.params.dummy;
335 } else
336 param = &term->vt.params.v[term->vt.params.idx++];
338 term->vt.params.cur = param;
339 param->value = 0;
340 param->sub.idx = 0;
341 param->sub.cur = NULL;
344static void
345action_param_new_subparam(struct terminal *term, uint8_t c)
347 xassert(c == ':');
348 action_param_lazy_init(term);
350 const size_t max_sub_params
351 = sizeof(term->vt.params.v[0].sub.value) / sizeof(term->vt.params.v[0].sub.value[0]);
353 struct vt_param *param = term->vt.params.cur;
354 unsigned *sub_param_value;
356 if (unlikely(param->sub.idx >= max_sub_params)) {
357 static bool have_warned = false;
358 if (!have_warned) {
359 have_warned = true;
360 LOG_WARN(
361 "unsupported: escape with more than %zu sub-parameters "
362 "(will not warn again)",
363 sizeof(term->vt.params.v[0].sub.value) / sizeof(term->vt.params.v[0].sub.value[0]));
364 }
366 sub_param_value = &param->sub.dummy;
367 } else
368 sub_param_value = &param->sub.value[param->sub.idx++];
370 param->sub.cur = sub_param_value;
371 *sub_param_value = 0;
374static void
375action_param(struct terminal *term, uint8_t c)
377 action_param_lazy_init(term);
378 xassert(term->vt.params.cur != NULL);
380 struct vt_param *param = term->vt.params.cur;
381 unsigned *value;
383 if (unlikely(param->sub.cur != NULL))
384 value = param->sub.cur;
385 else
386 value = &param->value;
388 unsigned v = *value;
389 v *= 10;
390 v += c - '0';
391 *value = v;
394static void
395action_collect(struct terminal *term, uint8_t c)
397 LOG_DBG("collect: %c", c);
399 /*
400 * Having more than one private is *very* rare. Foot only supports
401 * a *single* escape with two privates, and none with three or
402 * more.
403 *
404 * As such, we optimize *reading* the private(s), and *resetting*
405 * them (in action_clear()). Writing is ok if it's a bit slow.
406 */
408 if ((term->vt.private & 0xff) == 0)
409 term->vt.private = c;
410 else if (((term->vt.private >> 8) & 0xff) == 0)
411 term->vt.private |= c << 8;
412 else if (((term->vt.private >> 16) & 0xff) == 0)
413 term->vt.private |= c << 16;
414 else if (((term->vt.private >> 24) & 0xff) == 0)
415 term->vt.private |= c << 24;
416 else
417 LOG_WARN("only four private/intermediate characters supported");
420UNITTEST
422 struct terminal term = {.vt = {.private = 0}};
423 uint32_t expected = ' ';
424 action_collect(&term, ' ');
425 xassert(term.vt.private == expected);
427 expected |= '/' << 8;
428 action_collect(&term, '/');
429 xassert(term.vt.private == expected);
431 expected |= '<' << 16;
432 action_collect(&term, '<');
433 xassert(term.vt.private == expected);
435 expected |= '?' << 24;
436 action_collect(&term, '?');
437 xassert(term.vt.private == expected);
439 action_collect(&term, '?');
440 xassert(term.vt.private == expected);
443static void
444tab_set(struct terminal *term)
446 int col = term->grid->cursor.point.col;
448 if (tll_length(term->tab_stops) == 0 || tll_back(term->tab_stops) < col) {
449 tll_push_back(term->tab_stops, col);
450 return;
451 }
453 tll_foreach(term->tab_stops, it) {
454 if (it->item < col) {
455 continue;
456 }
457 if (it->item > col) {
458 tll_insert_before(term->tab_stops, it, col);
459 }
460 break;
461 }
464static void
465action_esc_dispatch(struct terminal *term, uint8_t final)
467 LOG_DBG("ESC: %s", esc_as_string(term, final));
469 switch (term->vt.private) {
470 case 0:
471 switch (final) {
472 case '7':
473 term_save_cursor(term);
474 break;
476 case '8':
477 term_restore_cursor(term, &term->grid->saved_cursor);
478 break;
480 case 'c':
481 term_reset(term, true);
482 break;
484 case 'n':
485 /* LS2 - Locking Shift 2 */
486 term->charsets.selected = G2;
487 term->bits_affecting_ascii_printer.charset =
488 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
489 term_update_ascii_printer(term);
490 break;
492 case 'o':
493 /* LS3 - Locking Shift 3 */
494 term->charsets.selected = G3;
495 term->bits_affecting_ascii_printer.charset =
496 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
497 term_update_ascii_printer(term);
498 break;
500 case 'D':
501 term_linefeed(term);
502 break;
504 case 'E':
505 term_carriage_return(term);
506 term_linefeed(term);
507 break;
509 case 'H':
510 tab_set(term);
511 break;
513 case 'M':
514 term_reverse_index(term);
515 break;
517 case 'N':
518 /* SS2 - Single Shift 2 */
519 term_single_shift(term, G2);
520 break;
522 case 'O':
523 /* SS3 - Single Shift 3 */
524 term_single_shift(term, G3);
525 break;
527 case '\\':
528 /* ST - String Terminator */
529 break;
531 case '=':
532 term->keypad_keys_mode = KEYPAD_APPLICATION;
533 break;
535 case '>':
536 term->keypad_keys_mode = KEYPAD_NUMERICAL;
537 break;
539 default:
540 UNHANDLED();
541 break;
542 }
543 break; /* private[0] == 0 */
545 // Designate character set
546 case '(': // G0
547 case ')': // G1
548 case '*': // G2
549 case '+': // G3
550 switch (final) {
551 case '0': {
552 size_t idx = term->vt.private - '(';
553 xassert(idx <= G3);
554 term->charsets.set[idx] = CHARSET_GRAPHIC;
555 term->bits_affecting_ascii_printer.charset =
556 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
557 term_update_ascii_printer(term);
558 break;
559 }
561 case 'B': {
562 size_t idx = term->vt.private - '(';
563 xassert(idx <= G3);
564 term->charsets.set[idx] = CHARSET_ASCII;
565 term->bits_affecting_ascii_printer.charset =
566 term->charsets.set[term->charsets.selected] != CHARSET_ASCII;
567 term_update_ascii_printer(term);
568 break;
569 }
570 }
571 break;
573 case '#':
574 switch (final) {
575 case '8': /* DECALN */
576 sixel_overwrite_by_rectangle(term, 0, 0, term->rows, term->cols);
578 term->scroll_region.start = 0;
579 term->scroll_region.end = term->rows;
581 for (int r = 0; r < term->rows; r++)
582 term_fill(term, r, 0, 'E', term->cols, false);
584 term_cursor_home(term);
585 break;
586 }
587 break; /* private[0] == '#' */
589 }
592static void
593action_csi_dispatch(struct terminal *term, uint8_t c)
595 csi_dispatch(term, c);
598static void
599action_osc_start(struct terminal *term, uint8_t c)
601 term->vt.osc.idx = 0;
604static void
605action_osc_end(struct terminal *term, uint8_t c)
607 struct vt *vt = &term->vt;
609 if (!osc_ensure_size(term, vt->osc.idx + 1))
610 return;
612 vt->osc.data[vt->osc.idx] = '\0';
613 vt->osc.bel = c == '\a';
614 osc_dispatch(term);
616 if (unlikely(vt->osc.idx >= 4096)) {
617 free(vt->osc.data);
618 vt->osc.data = NULL;
619 vt->osc.size = 0;
620 }
623static void
624action_osc_put(struct terminal *term, uint8_t c)
626 if (!osc_ensure_size(term, term->vt.osc.idx + 1))
627 return;
628 term->vt.osc.data[term->vt.osc.idx++] = c;
631static void
632action_hook(struct terminal *term, uint8_t c)
634 dcs_hook(term, c);
637static void
638action_unhook(struct terminal *term, uint8_t c)
640 dcs_unhook(term);
643static void
644action_put(struct terminal *term, uint8_t c)
646 dcs_put(term, c);
649static void
650action_utf8_print(struct terminal *term, char32_t wc)
652 term_process_and_print_non_ascii(term, wc);
655static void
656action_utf8_21(struct terminal *term, uint8_t c)
658 // wc = ((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f)
659 term->vt.utf8 = (c & 0x1f) << 6;
662static void
663action_utf8_22(struct terminal *term, uint8_t c)
665 // wc = ((utf8[0] & 0x1f) << 6) | (utf8[1] & 0x3f)
666 term->vt.utf8 |= c & 0x3f;
667 action_utf8_print(term, term->vt.utf8);
670static void
671action_utf8_31(struct terminal *term, uint8_t c)
673 // wc = ((utf8[0] & 0xf) << 12) | ((utf8[1] & 0x3f) << 6) | (utf8[2] & 0x3f)
674 term->vt.utf8 = (c & 0x0f) << 12;
677static void
678action_utf8_32(struct terminal *term, uint8_t c)
680 // wc = ((utf8[0] & 0xf) << 12) | ((utf8[1] & 0x3f) << 6) | (utf8[2] & 0x3f)
681 term->vt.utf8 |= (c & 0x3f) << 6;
684static void
685action_utf8_33(struct terminal *term, uint8_t c)
687 // wc = ((utf8[0] & 0xf) << 12) | ((utf8[1] & 0x3f) << 6) | (utf8[2] & 0x3f)
688 term->vt.utf8 |= c & 0x3f;
690 const char32_t utf32 = term->vt.utf8;
691 if (unlikely(utf32 >= 0xd800 && utf32 <= 0xdfff)) {
692 /* Invalid sequence - invalid UTF-16 surrogate halves */
693 return;
694 }
696 /* Note: the E0 range contains overlong encodings. We don't try to
697 detect, as they'll still decode to valid UTF-32. */
699 action_utf8_print(term, term->vt.utf8);
702static void
703action_utf8_41(struct terminal *term, uint8_t c)
705 // wc = ((utf8[0] & 7) << 18) | ((utf8[1] & 0x3f) << 12) | ((utf8[2] & 0x3f) << 6) | (utf8[3] & 0x3f);
706 term->vt.utf8 = (c & 0x07) << 18;
709static void
710action_utf8_42(struct terminal *term, uint8_t c)
712 // wc = ((utf8[0] & 7) << 18) | ((utf8[1] & 0x3f) << 12) | ((utf8[2] & 0x3f) << 6) | (utf8[3] & 0x3f);
713 term->vt.utf8 |= (c & 0x3f) << 12;
716static void
717action_utf8_43(struct terminal *term, uint8_t c)
719 // wc = ((utf8[0] & 7) << 18) | ((utf8[1] & 0x3f) << 12) | ((utf8[2] & 0x3f) << 6) | (utf8[3] & 0x3f);
720 term->vt.utf8 |= (c & 0x3f) << 6;
723static void
724action_utf8_44(struct terminal *term, uint8_t c)
726 // wc = ((utf8[0] & 7) << 18) | ((utf8[1] & 0x3f) << 12) | ((utf8[2] & 0x3f) << 6) | (utf8[3] & 0x3f);
727 term->vt.utf8 |= c & 0x3f;
729 const char32_t utf32 = term->vt.utf8;
731 if (unlikely(utf32 > 0x10FFFF)) {
732 /* Invalid UTF-8 */
733 return;
734 }
736 /* Note: the F0 range contains overlong encodings. We don't try to
737 detect, as they'll still decode to valid UTF-32. */
739 action_utf8_print(term, term->vt.utf8);
742IGNORE_WARNING("-Wpedantic")
744static enum state
745anywhere(struct terminal *term, uint8_t data)
747 switch (data) {
748 /* exit current enter new state */
749 case 0x18: action_execute(term, data); return STATE_GROUND;
750 case 0x1a: action_execute(term, data); return STATE_GROUND;
751 case 0x1b: action_clear(term); return STATE_ESCAPE;
753 /* 8-bit C1 control characters (not supported) */
754 case 0x80 ... 0x9f: return STATE_GROUND;
755 }
757 return term->vt.state;
760static enum state
761state_ground_switch(struct terminal *term, uint8_t data)
763 switch (data) {
764 /* exit current enter new state */
765 case 0x00 ... 0x17:
766 case 0x19:
767 case 0x1c ... 0x1f: action_execute(term, data); return STATE_GROUND;
769 /* modified from 0x20..0x7f to 0x20..0x7e, since 0x7f is DEL, which is a zero-width character */
770 case 0x20 ... 0x7e: action_print(term, data); return STATE_GROUND;
772 case 0xc2 ... 0xdf: action_utf8_21(term, data); return STATE_UTF8_21;
773 case 0xe0 ... 0xef: action_utf8_31(term, data); return STATE_UTF8_31;
774 case 0xf0 ... 0xf4: action_utf8_41(term, data); return STATE_UTF8_41;
775 }
777 return anywhere(term, data);
780static enum state
781state_escape_switch(struct terminal *term, uint8_t data)
783 switch (data) {
784 /* exit current enter new state */
785 case 0x00 ... 0x17:
786 case 0x19:
787 case 0x1c ... 0x1f: action_execute(term, data); return STATE_ESCAPE;
789 case 0x20 ... 0x2f: action_collect(term, data); return STATE_ESCAPE_INTERMEDIATE;
790 case 0x30 ... 0x4f: action_esc_dispatch(term, data); return STATE_GROUND;
791 case 0x50: action_clear(term); return STATE_DCS_ENTRY;
792 case 0x51 ... 0x57: action_esc_dispatch(term, data); return STATE_GROUND;
793 case 0x58: return STATE_SOS_PM_APC_STRING;
794 case 0x59: action_esc_dispatch(term, data); return STATE_GROUND;
795 case 0x5a: action_esc_dispatch(term, data); return STATE_GROUND;
796 case 0x5b: action_clear(term); return STATE_CSI_ENTRY;
797 case 0x5c: action_esc_dispatch(term, data); return STATE_GROUND;
798 case 0x5d: action_osc_start(term, data); return STATE_OSC_STRING;
799 case 0x5e ... 0x5f: return STATE_SOS_PM_APC_STRING;
800 case 0x60 ... 0x7e: action_esc_dispatch(term, data); return STATE_GROUND;
801 case 0x7f: action_ignore(term); return STATE_ESCAPE;
802 }
804 return anywhere(term, data);
807static enum state
808state_escape_intermediate_switch(struct terminal *term, uint8_t data)
810 switch (data) {
811 /* exit current enter new state */
812 case 0x00 ... 0x17:
813 case 0x19:
814 case 0x1c ... 0x1f: action_execute(term, data); return STATE_ESCAPE_INTERMEDIATE;
816 case 0x20 ... 0x2f: action_collect(term, data); return STATE_ESCAPE_INTERMEDIATE;
817 case 0x30 ... 0x7e: action_esc_dispatch(term, data); return STATE_GROUND;
818 case 0x7f: action_ignore(term); return STATE_ESCAPE_INTERMEDIATE;
819 }
821 return anywhere(term, data);
824static enum state
825state_csi_entry_switch(struct terminal *term, uint8_t data)
827 switch (data) {
828 /* exit current enter new state */
829 case 0x00 ... 0x17:
830 case 0x19:
831 case 0x1c ... 0x1f: action_execute(term, data); return STATE_CSI_ENTRY;
833 case 0x20 ... 0x2f: action_collect(term, data); return STATE_CSI_INTERMEDIATE;
834 case 0x30 ... 0x39: action_param(term, data); return STATE_CSI_PARAM;
835 case 0x3a: action_param_new_subparam(term, data); return STATE_CSI_PARAM;
836 case 0x3b: action_param_new(term, data); return STATE_CSI_PARAM;
838 case 0x3c ... 0x3f: action_collect(term, data); return STATE_CSI_PARAM;
839 case 0x40 ... 0x7e: action_csi_dispatch(term, data); return STATE_GROUND;
840 case 0x7f: action_ignore(term); return STATE_CSI_ENTRY;
841 }
843 return anywhere(term, data);
846static enum state
847state_csi_param_switch(struct terminal *term, uint8_t data)
849 switch (data) {
850 /* exit current enter new state */
851 case 0x00 ... 0x17:
852 case 0x19:
853 case 0x1c ... 0x1f: action_execute(term, data); return STATE_CSI_PARAM;
855 case 0x20 ... 0x2f: action_collect(term, data); return STATE_CSI_INTERMEDIATE;
857 case 0x30 ... 0x39: action_param(term, data); return STATE_CSI_PARAM;
858 case 0x3a: action_param_new_subparam(term, data); return STATE_CSI_PARAM;
859 case 0x3b: action_param_new(term, data); return STATE_CSI_PARAM;
861 case 0x3c ... 0x3f: return STATE_CSI_IGNORE;
862 case 0x40 ... 0x7e: action_csi_dispatch(term, data); return STATE_GROUND;
863 case 0x7f: action_ignore(term); return STATE_CSI_PARAM;
864 }
866 return anywhere(term, data);
869static enum state
870state_csi_intermediate_switch(struct terminal *term, uint8_t data)
872 switch (data) {
873 /* exit current enter new state */
874 case 0x00 ... 0x17:
875 case 0x19:
876 case 0x1c ... 0x1f: action_execute(term, data); return STATE_CSI_INTERMEDIATE;
878 case 0x20 ... 0x2f: action_collect(term, data); return STATE_CSI_INTERMEDIATE;
879 case 0x30 ... 0x3f: return STATE_CSI_IGNORE;
880 case 0x40 ... 0x7e: action_csi_dispatch(term, data); return STATE_GROUND;
881 case 0x7f: action_ignore(term); return STATE_CSI_INTERMEDIATE;
882 }
884 return anywhere(term, data);
887static enum state
888state_csi_ignore_switch(struct terminal *term, uint8_t data)
890 switch (data) {
891 /* exit current enter new state */
892 case 0x00 ... 0x17:
893 case 0x19:
894 case 0x1c ... 0x1f: action_execute(term, data); return STATE_CSI_IGNORE;
896 case 0x20 ... 0x3f: action_ignore(term); return STATE_CSI_IGNORE;
897 case 0x40 ... 0x7e: return STATE_GROUND;
898 case 0x7f: action_ignore(term); return STATE_CSI_IGNORE;
899 }
901 return anywhere(term, data);
904static enum state
905state_osc_string_switch(struct terminal *term, uint8_t data)
907 switch (data) {
908 /* exit current enter new state */
910 /* Note: original was 20-7f, but I changed to 20-ff to include utf-8. Don't forget to add EXECUTE to 8-bit C1 if we implement that. */
911 default: action_osc_put(term, data); return STATE_OSC_STRING;
913 case 0x07: action_osc_end(term, data); return STATE_GROUND;
915 case 0x00 ... 0x06:
916 case 0x08 ... 0x17:
917 case 0x19:
918 case 0x1c ... 0x1f: action_ignore(term); return STATE_OSC_STRING;
921 case 0x18:
922 case 0x1a: action_osc_end(term, data); action_execute(term, data); return STATE_GROUND;
924 case 0x1b: action_osc_end(term, data); action_clear(term); return STATE_ESCAPE;
925 }
928static enum state
929state_dcs_entry_switch(struct terminal *term, uint8_t data)
931 switch (data) {
932 /* exit current enter new state */
933 case 0x00 ... 0x17:
934 case 0x19:
935 case 0x1c ... 0x1f: action_ignore(term); return STATE_DCS_ENTRY;
937 case 0x20 ... 0x2f: action_collect(term, data); return STATE_DCS_INTERMEDIATE;
938 case 0x30 ... 0x39: action_param(term, data); return STATE_DCS_PARAM;
939 case 0x3a: return STATE_DCS_IGNORE;
940 case 0x3b: action_param_new(term, data); return STATE_DCS_PARAM;
941 case 0x3c ... 0x3f: action_collect(term, data); return STATE_DCS_PARAM;
942 case 0x40 ... 0x7e: action_hook(term, data); return STATE_DCS_PASSTHROUGH;
943 case 0x7f: action_ignore(term); return STATE_DCS_ENTRY;
944 }
946 return anywhere(term, data);
949static enum state
950state_dcs_param_switch(struct terminal *term, uint8_t data)
952 switch (data) {
953 /* exit current enter new state */
954 case 0x00 ... 0x17:
955 case 0x19:
956 case 0x1c ... 0x1f: action_ignore(term); return STATE_DCS_PARAM;
958 case 0x20 ... 0x2f: action_collect(term, data); return STATE_DCS_INTERMEDIATE;
959 case 0x30 ... 0x39: action_param(term, data); return STATE_DCS_PARAM;
960 case 0x3a: return STATE_DCS_IGNORE;
961 case 0x3b: action_param_new(term, data); return STATE_DCS_PARAM;
962 case 0x3c ... 0x3f: return STATE_DCS_IGNORE;
963 case 0x40 ... 0x7e: action_hook(term, data); return STATE_DCS_PASSTHROUGH;
964 case 0x7f: action_ignore(term); return STATE_DCS_PARAM;
965 }
967 return anywhere(term, data);
970static enum state
971state_dcs_intermediate_switch(struct terminal *term, uint8_t data)
973 switch (data) {
974 /* exit current enter new state */
975 case 0x00 ... 0x17:
976 case 0x19:
977 case 0x1c ... 0x1f: action_ignore(term); return STATE_DCS_INTERMEDIATE;
979 case 0x20 ... 0x2f: action_collect(term, data); return STATE_DCS_INTERMEDIATE;
980 case 0x30 ... 0x3f: return STATE_DCS_IGNORE;
981 case 0x40 ... 0x7e: action_hook(term, data); return STATE_DCS_PASSTHROUGH;
982 case 0x7f: action_ignore(term); return STATE_DCS_INTERMEDIATE;
983 }
985 return anywhere(term, data);
988static enum state
989state_dcs_ignore_switch(struct terminal *term, uint8_t data)
991 switch (data) {
992 /* exit current enter new state */
993 case 0x00 ... 0x17:
994 case 0x19:
995 case 0x1c ... 0x1f:
996 case 0x20 ... 0x7f: action_ignore(term); return STATE_DCS_IGNORE;
997 }
999 return anywhere(term, data);
1002static enum state
1003state_dcs_passthrough_switch(struct terminal *term, uint8_t data)
1005 switch (data) {
1006 /* exit current enter new state */
1007 case 0x00 ... 0x17:
1008 case 0x19:
1009 case 0x1c ... 0x7e: action_put(term, data); return STATE_DCS_PASSTHROUGH;
1011 case 0x7f: action_ignore(term); return STATE_DCS_PASSTHROUGH;
1013 /* Anywhere */
1014 case 0x18: action_unhook(term, data); action_execute(term, data); return STATE_GROUND;
1015 case 0x1a: action_unhook(term, data); action_execute(term, data); return STATE_GROUND;
1016 case 0x1b: action_unhook(term, data); action_clear(term); return STATE_ESCAPE;
1018 /* 8-bit C1 control characters (not supported) */
1019 case 0x80 ... 0x9f: action_unhook(term, data); return STATE_GROUND;
1021 default: return STATE_DCS_PASSTHROUGH;
1025static enum state
1026state_sos_pm_apc_string_switch(struct terminal *term, uint8_t data)
1028 switch (data) {
1029 /* exit current enter new state */
1030 case 0x00 ... 0x17:
1031 case 0x19:
1032 case 0x1c ... 0x7f: action_ignore(term); return STATE_SOS_PM_APC_STRING;
1035 return anywhere(term, data);
1038static enum state
1039state_utf8_21_switch(struct terminal *term, uint8_t data)
1041 switch (data) {
1042 /* exit current enter new state */
1043 case 0x80 ... 0xbf: action_utf8_22(term, data); return STATE_GROUND;
1044 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1048static enum state
1049state_utf8_31_switch(struct terminal *term, uint8_t data)
1051 switch (data) {
1052 /* exit current enter new state */
1053 case 0x80 ... 0xbf: action_utf8_32(term, data); return STATE_UTF8_32;
1054 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1058static enum state
1059state_utf8_32_switch(struct terminal *term, uint8_t data)
1061 switch (data) {
1062 /* exit current enter new state */
1063 case 0x80 ... 0xbf: action_utf8_33(term, data); return STATE_GROUND;
1064 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1068static enum state
1069state_utf8_41_switch(struct terminal *term, uint8_t data)
1071 switch (data) {
1072 /* exit current enter new state */
1073 case 0x80 ... 0xbf: action_utf8_42(term, data); return STATE_UTF8_42;
1074 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1078static enum state
1079state_utf8_42_switch(struct terminal *term, uint8_t data)
1081 switch (data) {
1082 /* exit current enter new state */
1083 case 0x80 ... 0xbf: action_utf8_43(term, data); return STATE_UTF8_43;
1084 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1088static enum state
1089state_utf8_43_switch(struct terminal *term, uint8_t data)
1091 switch (data) {
1092 /* exit current enter new state */
1093 case 0x80 ... 0xbf: action_utf8_44(term, data); return STATE_GROUND;
1094 default: action_utf8_print(term, 0xfffd); return state_ground_switch(term, data);
1098UNIGNORE_WARNINGS
1100void
1101vt_from_slave(struct terminal *term, const uint8_t *data, size_t len)
1103 enum state current_state = term->vt.state;
1105 const uint8_t *p = data;
1106 for (size_t i = 0; i < len; i++, p++) {
1107 switch (current_state) {
1108 case STATE_GROUND: current_state = state_ground_switch(term, *p); break;
1109 case STATE_ESCAPE: current_state = state_escape_switch(term, *p); break;
1110 case STATE_ESCAPE_INTERMEDIATE: current_state = state_escape_intermediate_switch(term, *p); break;
1111 case STATE_CSI_ENTRY: current_state = state_csi_entry_switch(term, *p); break;
1112 case STATE_CSI_PARAM: current_state = state_csi_param_switch(term, *p); break;
1113 case STATE_CSI_INTERMEDIATE: current_state = state_csi_intermediate_switch(term, *p); break;
1114 case STATE_CSI_IGNORE: current_state = state_csi_ignore_switch(term, *p); break;
1115 case STATE_OSC_STRING: current_state = state_osc_string_switch(term, *p); break;
1116 case STATE_DCS_ENTRY: current_state = state_dcs_entry_switch(term, *p); break;
1117 case STATE_DCS_PARAM: current_state = state_dcs_param_switch(term, *p); break;
1118 case STATE_DCS_INTERMEDIATE: current_state = state_dcs_intermediate_switch(term, *p); break;
1119 case STATE_DCS_IGNORE: current_state = state_dcs_ignore_switch(term, *p); break;
1120 case STATE_DCS_PASSTHROUGH: current_state = state_dcs_passthrough_switch(term, *p); break;
1121 case STATE_SOS_PM_APC_STRING: current_state = state_sos_pm_apc_string_switch(term, *p); break;
1123 case STATE_UTF8_21: current_state = state_utf8_21_switch(term, *p); break;
1124 case STATE_UTF8_31: current_state = state_utf8_31_switch(term, *p); break;
1125 case STATE_UTF8_32: current_state = state_utf8_32_switch(term, *p); break;
1126 case STATE_UTF8_41: current_state = state_utf8_41_switch(term, *p); break;
1127 case STATE_UTF8_42: current_state = state_utf8_42_switch(term, *p); break;
1128 case STATE_UTF8_43: current_state = state_utf8_43_switch(term, *p); break;
1131 term->vt.state = current_state;
Diagnostics5 git calls · 519 ms
CallStatusSizeTime0519 ms
discover200178 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=3
git.response.bytes=178
git.server.agent=git/2.56.1-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=178
git.pkt.payload.max=35
ls-refs20010 KiB493 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=417
git.response.bytes=10322
git.response.cache=EXPIRED
git.response.duration_ms=493
git.protocol.version=2
git.pkt.frames=108
git.pkt.data=107
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=10322
git.pkt.payload.max=106
git.ls_refs.count=107
git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=8830
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=8830
git.pkt.payload.max=8730
git.sideband.band1.bytes=8730
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=14
git.pack.bytes=8730
git.fetch.wide.objects=14
git.fetch.wide.extra_bytes=9909
git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=9489
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=9489
git.pkt.payload.max=9389
git.sideband.band1.bytes=9389
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=9389
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a
git.pack.full=10
Open pack map
fetch2006.0 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=23
git.response.bytes=6177
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=1d8297beee1075bc60af3511d44581d498e5a977
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=6177
git.pkt.payload.max=6150
git.sideband.band1.bytes=6150
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://codeberg.org/dnkl/foot
Ref
refs/heads/master
Commit
c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
cf-ray
a5e25e88684c224d
Request
04ad977c…
Protocol
v2 · sha1
Agent
git/2.56.1-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format