+ tputc(&c, 1);
+ } else {
+ break;
+ }
+ }
+ if(len)
+ tputc(buf, len);
+}
+
+void
+tputc(char *c, int len) {
+ uchar ascii = *c;
+ bool control = ascii < '\x20' || ascii == 0177;
+
+ 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);
+ return;
+ case '\033': /* ESC */
+ csireset();
+ term.esc = ESC_START;
+ return;
+ case '\016': /* SO */
+ case '\017': /* SI */
+ /*
+ * Different charsets are hard to handle. Applications
+ * should use the right alt charset escapes for the
+ * only reason they still exist: line drawing. The
+ * rest is incompatible history st should not support.
+ */
+ 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) {
+ switch(ascii) {
+ case '0': /* Line drawing set */
+ term.c.attr.mode |= ATTR_GFX;
+ break;
+ case 'B': /* USASCII */
+ term.c.attr.mode &= ~ATTR_GFX;
+ break;
+ case 'A': /* UK (IGNORED) */
+ case '<': /* multinational charset (IGNORED) */
+ case '5': /* Finnish (IGNORED) */
+ case 'C': /* Finnish (IGNORED) */
+ case 'K': /* German (IGNORED) */