Codebergdnkl
foot
master
1#include "dcs.h"
2#include <string.h>
4#define LOG_MODULE "dcs"
5#define LOG_ENABLE_DBG 0
6#include "log.h"
7#include "foot-terminfo.h"
8#include "sixel.h"
9#include "util.h"
10#include "vt.h"
11#include "xmalloc.h"
12#include "xsnprintf.h"
14static bool
15ensure_size(struct terminal *term, size_t required_size)
16{
17 if (required_size <= term->vt.dcs.size)
18 return true;
20 uint8_t *new_data = realloc(term->vt.dcs.data, required_size);
21 if (new_data == NULL) {
22 LOG_ERRNO("failed to increase size of DCS buffer");
23 return false;
24 }
26 term->vt.dcs.data = new_data;
27 term->vt.dcs.size = required_size;
28 return true;
29}
31/* Decode hex-encoded string *inline*. NULL terminates */
32static char *
33hex_decode(const char *s, size_t len)
34{
35 if (len % 2)
36 return NULL;
38 char *hex = xmalloc(len / 2 + 1);
39 char *o = hex;
41 /* TODO: error checking */
42 for (size_t i = 0; i < len; i += 2) {
43 uint8_t nib1 = hex2nibble(*s); s++;
44 uint8_t nib2 = hex2nibble(*s); s++;
46 if (nib1 == HEX_DIGIT_INVALID || nib2 == HEX_DIGIT_INVALID)
47 goto err;
49 *o = nib1 << 4 | nib2; o++;
50 }
52 *o = '\0';
53 return hex;
55err:
56 free(hex);
57 return NULL;
58}
60UNITTEST
61{
62 /* Verify table is sorted */
63 const char *p = terminfo_capabilities;
64 size_t left = sizeof(terminfo_capabilities);
66 const char *last_cap = NULL;
68 while (left > 0) {
69 const char *cap = p;
70 const char *val = cap + strlen(cap) + 1;
72 size_t size = strlen(cap) + 1 + strlen(val) + 1;;
73 xassert(size <= left);
74 p += size;
75 left -= size;
77 if (last_cap != NULL)
78 xassert(strcmp(last_cap, cap) < 0);
80 last_cap = cap;
81 }
82}
84static bool
85lookup_capability(const char *name, const char **value)
86{
87 const char *p = terminfo_capabilities;
88 size_t left = sizeof(terminfo_capabilities);
90 while (left > 0) {
91 const char *cap = p;
92 const char *val = cap + strlen(cap) + 1;
94 size_t size = strlen(cap) + 1 + strlen(val) + 1;;
95 xassert(size <= left);
96 p += size;
97 left -= size;
99 int r = strcmp(cap, name);
100 if (r == 0) {
101 *value = val;
102 return true;
103 } else if (r > 0)
104 break;
105 }
107 *value = NULL;
108 return false;
109}
111static void
112xtgettcap_reply(struct terminal *term, const char *hex_cap_name, size_t len)
113{
114 char *name = hex_decode(hex_cap_name, len);
115 if (name == NULL) {
116 LOG_WARN("XTGETTCAP: invalid hex encoding, ignoring capability");
117 return;
118 }
120 const char *value;
121 bool valid_capability = lookup_capability(name, &value);
122 xassert(!valid_capability || value != NULL);
124 LOG_DBG("XTGETTCAP: cap=%s (%.*s), value=%s",
125 name, (int)len, hex_cap_name,
126 valid_capability ? value : "<invalid>");
128 if (!valid_capability)
129 goto err;
131 if (value[0] == '\0') {
132 /* Boolean */
133 term_to_slave(term, "\033P1+r", 5);
134 term_to_slave(term, hex_cap_name, len);
135 term_to_slave(term, "\033\\", 2);
136 goto out;
137 }
139 /*
140 * Reply format:
141 * \EP 1 + r cap=value \E\\
142 * Where 'cap' and 'value are hex encoded ascii strings
143 */
144 char *reply = xmalloc(
145 5 + /* DCS 1 + r (\EP1+r) */
146 len + /* capability name, hex encoded */
147 1 + /* '=' */
148 strlen(value) * 2 + /* capability value, hex encoded */
149 2 + /* ST (\E\\) */
150 1);
152 int idx = sprintf(reply, "\033P1+r%.*s=", (int)len, hex_cap_name);
154 for (const char *c = value; *c != '\0'; c++) {
155 uint8_t nib1 = (uint8_t)*c >> 4;
156 uint8_t nib2 = (uint8_t)*c & 0xf;
158 reply[idx] = nib1 >= 0xa ? 'A' + nib1 - 0xa : '0' + nib1; idx++;
159 reply[idx] = nib2 >= 0xa ? 'A' + nib2 - 0xa : '0' + nib2; idx++;
160 }
162 reply[idx] = '\033'; idx++;
163 reply[idx] = '\\'; idx++;
164 term_to_slave(term, reply, idx);
166 free(reply);
167 goto out;
169err:
170 term_to_slave(term, "\033P0+r", 5);
171 term_to_slave(term, hex_cap_name, len);
172 term_to_slave(term, "\033\\", 2);
174out:
175 free(name);
176}
178static void
179xtgettcap_put(struct terminal *term, uint8_t c)
180{
181 struct vt *vt = &term->vt;
183 /* Grow buffer expontentially */
184 if (vt->dcs.idx >= vt->dcs.size) {
185 size_t new_size = vt->dcs.size * 2;
186 if (new_size == 0)
187 new_size = 128;
189 if (!ensure_size(term, new_size))
190 return;
191 }
193 vt->dcs.data[vt->dcs.idx++] = c;
194}
196static void
197xtgettcap_unhook(struct terminal *term)
198{
199 size_t left = term->vt.dcs.idx;
201 const char *const end = (const char *)&term->vt.dcs.data[left];
202 const char *p = (const char *)term->vt.dcs.data;
204 if (p == NULL) {
205 /* Request is empty; send an error reply, without any capabilities */
206 term_to_slave(term, "\033P0+r\033\\", 7);
207 return;
208 }
210 while (true) {
211 const char *sep = memchr(p, ';', left);
212 size_t cap_len;
214 if (sep == NULL) {
215 /* Last capability */
216 cap_len = end - p;
217 } else {
218 cap_len = sep - p;
219 }
221 xtgettcap_reply(term, p, cap_len);
223 left -= cap_len + 1;
224 p += cap_len + 1;
226 if (sep == NULL)
227 break;
228 }
229}
231static void NOINLINE
232append_sgr_attr_n(char **reply, size_t *len, const char *attr, size_t n)
233{
234 size_t new_len = *len + n + 1;
235 *reply = xrealloc(*reply, new_len);
236 memcpy(&(*reply)[*len], attr, n);
237 (*reply)[new_len - 1] = ';';
238 *len = new_len;
239}
241static void
242decrqss_put(struct terminal *term, uint8_t c)
243{
244 /* Largest request we support is two bytes */
245 if (!ensure_size(term, 2))
246 return;
248 struct vt *vt = &term->vt;
249 if (vt->dcs.idx >= 2)
250 return;
251 vt->dcs.data[vt->dcs.idx++] = c;
252}
254static void
255decrqss_unhook(struct terminal *term)
256{
257 const uint8_t *query = term->vt.dcs.data;
258 const size_t n = term->vt.dcs.idx;
260 /*
261 * A note on the Ps parameter in the reply: many DEC manual
262 * instances (e.g. https://vt100.net/docs/vt510-rm/DECRPSS) claim
263 * that 0 means "request is valid", and 1 means "request is
264 * invalid".
265 *
266 * However, this appears to be a typo; actual hardware inverts the
267 * response (as does XTerm and mlterm):
268 * https://github.com/hackerb9/vt340test/issues/13
269 */
271 if (n == 1 && query[0] == 'r') {
272 /* DECSTBM - Set Top and Bottom Margins */
273 char reply[64];
274 size_t len = xsnprintf(reply, sizeof(reply), "\033P1$r%d;%dr\033\\",
275 term->scroll_region.start + 1,
276 term->scroll_region.end);
277 term_to_slave(term, reply, len);
278 }
280 else if (n == 1 && query[0] == 'm') {
281 /* SGR - Set Graphic Rendition */
282 char *reply = NULL;
283 size_t len = 0;
285 #define append_sgr_attr(num_as_str) \
286 append_sgr_attr_n(&reply, &len, num_as_str, sizeof(num_as_str) - 1)
288 /* Always present, both in the example from the VT510 manual
289 * (https://vt100.net/docs/vt510-rm/DECRPSS), and in XTerm and
290 * mlterm */
291 append_sgr_attr("0");
293 struct attributes *a = &term->vt.attrs;
294 if (a->bold)
295 append_sgr_attr("1");
296 if (a->dim)
297 append_sgr_attr("2");
298 if (a->italic)
299 append_sgr_attr("3");
300 if (a->underline) {
301 if (term->vt.underline.style > UNDERLINE_SINGLE) {
302 char value[4];
303 size_t val_len =
304 xsnprintf(value, sizeof(value), "4:%d", term->vt.underline.style);
305 append_sgr_attr_n(&reply, &len, value, val_len);
306 } else
307 append_sgr_attr("4");
308 }
309 if (a->blink)
310 append_sgr_attr("5");
311 if (a->reverse)
312 append_sgr_attr("7");
313 if (a->conceal)
314 append_sgr_attr("8");
315 if (a->strikethrough)
316 append_sgr_attr("9");
318 switch (a->fg_src) {
319 case COLOR_DEFAULT:
320 break;
322 case COLOR_BASE16: {
323 char value[4];
324 size_t val_len = xsnprintf(
325 value, sizeof(value), "%u",
326 a->fg >= 8 ? a->fg - 8 + 90 : a->fg + 30);
327 append_sgr_attr_n(&reply, &len, value, val_len);
328 break;
329 }
331 case COLOR_BASE256: {
332 char value[16];
333 size_t val_len = xsnprintf(value, sizeof(value), "38:5:%u", a->fg);
334 append_sgr_attr_n(&reply, &len, value, val_len);
335 break;
336 }
338 case COLOR_RGB: {
339 uint8_t r = a->fg >> 16;
340 uint8_t g = a->fg >> 8;
341 uint8_t b = a->fg >> 0;
343 char value[32];
344 size_t val_len = xsnprintf(
345 value, sizeof(value), "38:2::%hhu:%hhu:%hhu", r, g, b);
346 append_sgr_attr_n(&reply, &len, value, val_len);
347 break;
348 }
349 }
351 switch (a->bg_src) {
352 case COLOR_DEFAULT:
353 break;
355 case COLOR_BASE16: {
356 char value[4];
357 size_t val_len = xsnprintf(
358 value, sizeof(value), "%u",
359 a->bg >= 8 ? a->bg - 8 + 100 : a->bg + 40);
360 append_sgr_attr_n(&reply, &len, value, val_len);
361 break;
362 }
364 case COLOR_BASE256: {
365 char value[16];
366 size_t val_len = xsnprintf(value, sizeof(value), "48:5:%u", a->bg);
367 append_sgr_attr_n(&reply, &len, value, val_len);
368 break;
369 }
371 case COLOR_RGB: {
372 uint8_t r = a->bg >> 16;
373 uint8_t g = a->bg >> 8;
374 uint8_t b = a->bg >> 0;
376 char value[32];
377 size_t val_len = xsnprintf(
378 value, sizeof(value), "48:2::%hhu:%hhu:%hhu", r, g, b);
379 append_sgr_attr_n(&reply, &len, value, val_len);
380 break;
381 }
382 }
384 switch (term->vt.underline.color_src) {
385 case COLOR_DEFAULT:
386 case COLOR_BASE16:
387 break;
389 case COLOR_BASE256: {
390 char value[16];
391 size_t val_len = xsnprintf(
392 value, sizeof(value), "58:5:%u", term->vt.underline.color);
393 append_sgr_attr_n(&reply, &len, value, val_len);
394 break;
395 }
397 case COLOR_RGB: {
398 uint8_t r = term->vt.underline.color >> 16;
399 uint8_t g = term->vt.underline.color >> 8;
400 uint8_t b = term->vt.underline.color >> 0;
402 char value[32];
403 size_t val_len = xsnprintf(
404 value, sizeof(value), "58:2::%hhu:%hhu:%hhu", r, g, b);
405 append_sgr_attr_n(&reply, &len, value, val_len);
406 break;
407 }
408 }
410 #undef append_sgr_attr_n
412 reply[len - 1] = 'm';
414 term_to_slave(term, "\033P1$r", 5);
415 term_to_slave(term, reply, len);
416 term_to_slave(term, "\033\\", 2);
417 free(reply);
418 }
420 else if (n == 2 && memcmp(query, " q", 2) == 0) {
421 /* DECSCUSR - Set Cursor Style */
422 int mode;
424 switch (term->cursor_style) {
425 case CURSOR_HOLLOW: /* FALLTHROUGH */
426 case CURSOR_BLOCK: mode = 2; break;
427 case CURSOR_UNDERLINE: mode = 4; break;
428 case CURSOR_BEAM: mode = 6; break;
429 default: BUG("invalid cursor style"); break;
430 }
432 if (term->cursor_blink.deccsusr)
433 mode--;
435 char reply[16];
436 size_t len = xsnprintf(reply, sizeof(reply), "\033P1$r%d q\033\\", mode);
437 term_to_slave(term, reply, len);
438 }
440 else {
441 static const char err[] = "\033P0$r\033\\";
442 term_to_slave(term, err, sizeof(err) - 1);
443 }
444}
446void
447dcs_hook(struct terminal *term, uint8_t final)
448{
449 LOG_DBG("hook: %c (intermediate(s): %.2s, param=%d)", final,
450 (const char *)&term->vt.private, vt_param_get(term, 0, 0));
452 xassert(term->vt.dcs.data == NULL);
453 xassert(term->vt.dcs.size == 0);
454 xassert(term->vt.dcs.put_handler == NULL);
455 xassert(term->vt.dcs.unhook_handler == NULL);
457 switch (term->vt.private) {
458 case 0:
459 switch (final) {
460 case 'q': {
461 if (!term->conf->tweak.sixel) {
462 break;
463 }
464 int p1 = vt_param_get(term, 0, 0);
465 int p2 = vt_param_get(term, 1, 0);
466 int p3 = vt_param_get(term, 2, 0);
468 term->vt.dcs.put_handler = sixel_init(term, p1, p2, p3);
469 term->vt.dcs.unhook_handler = &sixel_unhook;
470 break;
471 }
472 }
473 break;
475 case '$':
476 switch (final) {
477 case 'q':
478 term->vt.dcs.put_handler = &decrqss_put;
479 term->vt.dcs.unhook_handler = &decrqss_unhook;
480 break;
481 }
482 break;
484 case '=':
485 switch (final) {
486 case 's':
487 /* BSU/ESU: https://gitlab.com/gnachman/iterm2/-/wikis/synchronized-updates-spec */
488 switch (vt_param_get(term, 0, 0)) {
489 case 1:
490 term->vt.dcs.unhook_handler = &term_enable_app_sync_updates;
491 return;
492 case 2:
493 term->vt.dcs.unhook_handler = &term_disable_app_sync_updates;
494 return;
495 }
496 break;
497 }
498 break;
500 case '+':
501 switch (final) {
502 case 'q': /* XTGETTCAP */
503 term->vt.dcs.put_handler = &xtgettcap_put;
504 term->vt.dcs.unhook_handler = &xtgettcap_unhook;
505 break;
506 }
507 break;
508 }
509}
511void
512dcs_put(struct terminal *term, uint8_t c)
513{
514 /* LOG_DBG("PUT: %c", c); */
516 if (term->vt.dcs.put_handler != NULL)
517 term->vt.dcs.put_handler(term, c);
518}
520void
521dcs_unhook(struct terminal *term)
522{
523 if (term->vt.dcs.unhook_handler != NULL)
524 term->vt.dcs.unhook_handler(term);
526 term->vt.dcs.unhook_handler = NULL;
527 term->vt.dcs.put_handler = NULL;
529 free(term->vt.dcs.data);
530 term->vt.dcs.data = NULL;
531 term->vt.dcs.size = 0;
532 term->vt.dcs.idx = 0;
533}
Diagnostics5 git calls · 638 ms
CallStatusSizeTime0638 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 KiB617 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=611 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=617 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=2 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 · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 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=10Open pack map
fetch2003.6 KiB · hit19 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=19 git.response.bytes=3644 git.response.cache=HIT git.response.duration_ms=19 git.fetch.wants=376c73bd2bbe05caf95aaac57c4e41e98bbf275c 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=3644 git.pkt.payload.max=3617 git.sideband.band1.bytes=3617 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
- a5e27fa67cef0555
- Request
- 54d559a9…
- 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