+/*
+ * Handle data from a local tty in PARMRK format.
+ */
+static void from_tty(void *vbuf, unsigned len)
+{
+ char *p, *q, *end, *buf = vbuf;
+ static enum {NORMAL, FF, FF00} state = NORMAL;
+
+ p = buf; end = buf + len;
+ while (p < end) {
+ switch (state) {
+ case NORMAL:
+ if (*p == '\xff') {
+ p++;
+ state = FF;
+ } else {
+ q = memchr(p, '\xff', end - p);
+ if (q == NULL) q = end;
+ back->send(backhandle, p, q - p);
+ p = q;
+ }
+ break;
+ case FF:
+ if (*p == '\xff') {
+ back->send(backhandle, p, 1);
+ p++;
+ state = NORMAL;
+ } else if (*p == '\0') {
+ p++;
+ state = FF00;
+ } else abort();
+ break;
+ case FF00:
+ if (*p == '\0') {
+ back->special(backhandle, TS_BRK);
+ } else {
+ /*
+ * Pretend that PARMRK wasn't set. This involves
+ * faking what INPCK and IGNPAR would have done if
+ * we hadn't overridden them. Unfortunately, we
+ * can't do this entirely correctly because INPCK
+ * distinguishes between framing and parity
+ * errors, but PARMRK format represents both in
+ * the same way. We assume that parity errors are
+ * more common than framing errors, and hence
+ * treat all input errors as being subject to
+ * INPCK.
+ */
+ if (orig_termios.c_iflag & INPCK) {
+ /* If IGNPAR is set, we throw away the character. */
+ if (!(orig_termios.c_iflag & IGNPAR)) {
+ /* PE/FE get passed on as NUL. */
+ *p = 0;
+ back->send(backhandle, p, 1);
+ }
+ } else {
+ /* INPCK not set. Assume we got a parity error. */
+ back->send(backhandle, p, 1);
+ }
+ }
+ p++;
+ state = NORMAL;
+ }
+ }