+static void
+ansi_terminal_reset (ANSI * a)
+{
+ CRT_Pos p = { 0, CRT_ROWS };
+ ansi_force_attr_normal (a);
+
+ ansi_move (a, p);
+}
+
+static void
+ansi_flush_escape (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+ int i;
+
+ for (i = 0; i < p->escape_ptr; ++i)
+ {
+ keydis_key (c->k, c, p->escape_buf[i]);
+ }
+
+ p->escape_ptr = 0;
+ p->in_escape = 0;
+}
+
+static void
+ansi_parse_deckey (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+ if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
+ {
+ ansi_flush_escape (a, c);
+ return;
+ }
+
+ if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
+ {
+ keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
+ }
+ else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
+ {
+ keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
+ }
+ else
+ {
+ ansi_flush_escape (a, c);
+ return;
+ }
+ p->in_escape = 0;
+ p->escape_ptr = 0;
+}
+
+static void
+ansi_parse_ansikey (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+
+ if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
+ {
+ ansi_flush_escape (a, c);
+ return;
+ }
+ if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
+ {
+ keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
+ }
+ else
+ {
+ ansi_flush_escape (a, c);
+ return;
+ }
+
+ p->in_escape = 0;
+ p->escape_ptr = 0;
+}
+
+
+
+static void
+ansi_parse_escape (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+ switch (p->escape_ptr)
+ {
+ case 0:
+ case 1:
+ return;
+ case 2:
+ switch (p->escape_buf[1])
+ {
+ case '[':
+ case 'O':
+ break;
+ default:
+ ansi_flush_escape (a, c);
+ }
+ break;
+ case 3:
+ switch (p->escape_buf[1])
+ {
+ case 'O':
+ ansi_parse_deckey (a, c);
+ break;
+ case '[':
+ if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
+ ansi_parse_deckey (a, c);
+ break;
+ default:
+ ansi_flush_escape (a, c);
+ }
+ break;
+ case 4:
+ switch (p->escape_buf[1])
+ {
+ case '[':
+ ansi_parse_ansikey (a, c);
+ break;
+ default:
+ ansi_flush_escape (a, c);
+ }
+ break;
+ case 5:
+ ansi_flush_escape (a, c);
+ }
+}
+
+
+static void
+ansi_check_escape (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+ struct timeval now, diff;
+ gettimeofday (&now, NULL);
+ timersub (&now, &p->last_escape, &diff);
+
+#if 0
+ fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
+ p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
+#endif
+
+ if (!p->in_escape)
+ return;
+
+
+ /*Time up? */
+ if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
+ ansi_flush_escape (a, c);
+
+}
+
+
+static void
+ansi_parse_char (ANSI * a, Context * c, int ch)
+{
+ ANSI_Parser *p = &a->parser;
+
+
+/*See if it's time to flush the escape*/
+ ansi_check_escape (a, c);
+
+ if (ch == 033)
+ {
+ if (p->in_escape)
+ ansi_flush_escape (a, c);
+
+ p->in_escape++;
+ p->escape_ptr = 0;
+ gettimeofday (&p->last_escape, NULL);
+ }
+
+ if (p->in_escape)
+ {
+ p->escape_buf[p->escape_ptr++] = ch;
+ ansi_parse_escape (a, c);
+ }
+ else
+ {
+ keydis_key (c->k, c, ch);
+ }
+
+}
+
+static void
+ansi_parse (ANSI * a, Context * c, char *buf, int len)
+{
+ while (len--)
+ ansi_parse_char (a, c, *(buf++));
+}
+
+int
+ansi_dispatch (ANSI * a, Context * c)
+{
+ char buf[1024];
+ int red;
+
+ ansi_check_escape (a, c);
+
+
+ if (!a->terminal)
+ return 0;
+
+ red = a->terminal->recv (a->terminal, buf, sizeof (buf));
+ if (red <= 0)
+ return red;
+
+#if 0
+ if (*buf == 3)
+ return -1;
+#endif
+
+#if 0
+ if (*buf == 2)
+ {
+#if 0
+ a->history_ptr = c->h->wptr;
+ HISTORY_INC (c->h, a->history_ptr);
+#endif
+ return -1;
+ }
+#endif
+
+
+ ansi_parse (a, c, buf, red);
+
+ return 0;
+}
+
+
+static void
+ansi_update (ANSI * a, Context * c)
+{
+ ansi_history (a, c->h);
+ ansi_draw (a, &c->v->crt);
+}
+
+static void
+ansi_free (ANSI * a)
+{
+ a->terminal_reset (a);
+ if (a->terminal)
+ a->terminal->close (a->terminal);
+
+ free (a);
+
+}
+
+ANSI *
+ansi_new_from_terminal (TTY * t, int utf8)
+{
+ ANSI *ret;
+
+ ret = malloc (sizeof (ANSI));
+ memset (ret, 0, sizeof (ANSI));
+
+ ret->terminal = t;