+ printf("(%02x)", c);
+ }
+ }
+ printf("ESC\\\n");
+}
+
+void
+strreset(void) {
+ memset(&strescseq, 0, sizeof(strescseq));
+}
+
+void
+tputtab(bool forward) {
+ uint x = term.c.x;
+
+ if(forward) {
+ if(x == term.col)
+ return;
+ for(++x; x < term.col && !term.tabs[x]; ++x)
+ /* nothing */ ;
+ } else {
+ if(x == 0)
+ return;
+ for(--x; x > 0 && !term.tabs[x]; --x)
+ /* nothing */ ;
+ }
+ tmoveto(x, term.c.y);
+}
+
+void
+techo(char *buf, int len) {
+ for(; len > 0; buf++, len--) {
+ char c = *buf;
+
+ if(c == '\033') { /* escape */
+ tputc("^", 1);
+ tputc("[", 1);
+ } else if(c < '\x20') { /* control code */
+ if(c != '\n' && c != '\r' && c != '\t') {
+ c |= '\x40';
+ tputc("^", 1);
+ }
+ tputc(&c, 1);
+ } else {
+ break;
+ }
+ }
+ if(len)
+ tputc(buf, len);
+}
+
+void
+tdeftran(char ascii) {
+ char c, (*bp)[2];
+ static char tbl[][2] = {
+ {'0', CS_GRAPHIC0}, {'1', CS_GRAPHIC1}, {'A', CS_UK},
+ {'B', CS_USA}, {'<', CS_MULTI}, {'K', CS_GER},
+ {'5', CS_FIN}, {'C', CS_FIN},
+ {0, 0}
+ };
+
+ for (bp = &tbl[0]; (c = (*bp)[0]) && c != ascii; ++bp)
+ /* nothing */;
+
+ if (c == 0)
+ fprintf(stderr, "esc unhandled charset: ESC ( %c\n", ascii);
+ else
+ term.trantbl[term.icharset] = (*bp)[1];
+}
+
+void
+tselcs(void) {
+ if (term.trantbl[term.charset] == CS_GRAPHIC0)
+ term.c.attr.mode |= ATTR_GFX;
+ else
+ term.c.attr.mode &= ~ATTR_GFX;
+}
+
+void
+tputc(char *c, int len) {
+ uchar ascii = *c;
+ bool control = ascii < '\x20' || ascii == 0177;
+ long u8char;
+ int width;
+
+ if(len == 1) {
+ width = 1;
+ } else {
+ utf8decode(c, &u8char);
+ width = wcwidth(u8char);
+ }
+
+ if(iofd != -1) {
+ if(xwrite(iofd, c, len) < 0) {
+ fprintf(stderr, "Error writing in %s:%s\n",
+ opt_io, strerror(errno));
+ close(iofd);
+ iofd = -1;
+ }
+ }
+
+ /*
+ * STR sequences must be checked before anything else
+ * because it can use some control codes as part of the sequence.
+ */
+ if(term.esc & ESC_STR) {
+ switch(ascii) {
+ case '\033':
+ term.esc = ESC_START | ESC_STR_END;
+ break;
+ case '\a': /* backwards compatibility to xterm */
+ term.esc = 0;
+ strhandle();
+ break;
+ default:
+ if(strescseq.len + len < sizeof(strescseq.buf) - 1) {
+ memmove(&strescseq.buf[strescseq.len], c, len);
+ strescseq.len += len;
+ } else {
+ /*
+ * Here is a bug in terminals. If the user never sends
+ * some code to stop the str or esc command, then st
+ * will stop responding. But this is better than
+ * silently failing with unknown characters. At least
+ * then users will report back.
+ *
+ * In the case users ever get fixed, here is the code:
+ */
+ /*
+ * term.esc = 0;
+ * strhandle();
+ */
+ }
+ }
+ return;
+ }
+
+ /*
+ * Actions of control codes must be performed as soon they arrive
+ * because they can be embedded inside a control sequence, and
+ * they must not cause conflicts with sequences.
+ */
+ if(control) {
+ switch(ascii) {
+ case '\t': /* HT */
+ tputtab(1);
+ return;
+ case '\b': /* BS */
+ tmoveto(term.c.x-1, term.c.y);
+ return;
+ case '\r': /* CR */
+ tmoveto(0, term.c.y);
+ return;
+ case '\f': /* LF */
+ case '\v': /* VT */
+ case '\n': /* LF */
+ /* go to first col if the mode is set */
+ tnewline(IS_SET(MODE_CRLF));
+ return;
+ case '\a': /* BEL */
+ if(!(xw.state & WIN_FOCUSED))
+ xseturgency(1);
+ if (bellvolume)
+ XBell(xw.dpy, bellvolume);
+ return;
+ case '\033': /* ESC */
+ csireset();
+ term.esc = ESC_START;
+ return;
+ case '\016': /* SO */
+ term.charset = 0;
+ tselcs();
+ return;
+ case '\017': /* SI */
+ term.charset = 1;
+ tselcs();
+ return;
+ case '\032': /* SUB */
+ case '\030': /* CAN */
+ csireset();
+ return;
+ case '\005': /* ENQ (IGNORED) */
+ case '\000': /* NUL (IGNORED) */
+ case '\021': /* XON (IGNORED) */
+ case '\023': /* XOFF (IGNORED) */
+ case 0177: /* DEL (IGNORED) */
+ return;
+ }
+ } else if(term.esc & ESC_START) {
+ if(term.esc & ESC_CSI) {
+ csiescseq.buf[csiescseq.len++] = ascii;
+ if(BETWEEN(ascii, 0x40, 0x7E)
+ || csiescseq.len >= \
+ sizeof(csiescseq.buf)-1) {
+ term.esc = 0;
+ csiparse();
+ csihandle();
+ }
+ } else if(term.esc & ESC_STR_END) {
+ term.esc = 0;
+ if(ascii == '\\')
+ strhandle();
+ } else if(term.esc & ESC_ALTCHARSET) {
+ tdeftran(ascii);
+ tselcs();
+ term.esc = 0;
+ } else if(term.esc & ESC_TEST) {
+ if(ascii == '8') { /* DEC screen alignment test. */
+ char E[UTF_SIZ] = "E";
+ int x, y;
+
+ for(x = 0; x < term.col; ++x) {
+ for(y = 0; y < term.row; ++y)
+ tsetchar(E, &term.c.attr, x, y);
+ }
+ }
+ term.esc = 0;
+ } else {
+ switch(ascii) {
+ case '[':
+ term.esc |= ESC_CSI;
+ break;
+ case '#':
+ term.esc |= ESC_TEST;
+ break;
+ case 'P': /* DCS -- Device Control String */
+ case '_': /* APC -- Application Program Command */
+ case '^': /* PM -- Privacy Message */
+ case ']': /* OSC -- Operating System Command */
+ case 'k': /* old title set compatibility */
+ strreset();
+ strescseq.type = ascii;
+ term.esc |= ESC_STR;
+ break;
+ case '(': /* set primary charset G0 */
+ case ')': /* set secondary charset G1 */
+ case '*': /* set tertiary charset G2 */
+ case '+': /* set quaternary charset G3 */
+ term.icharset = ascii - '(';
+ term.esc |= ESC_ALTCHARSET;
+ break;
+ case 'D': /* IND -- Linefeed */
+ if(term.c.y == term.bot) {
+ tscrollup(term.top, 1);
+ } else {
+ tmoveto(term.c.x, term.c.y+1);
+ }
+ term.esc = 0;
+ break;