+static void
+ansi_terminal_reset (ANSI * a)
+{
+ CRT_Pos p = { 0, a->crt.size.y };
+ ansi_force_attr_normal (a);
+
+ ansi_move (a, p);
+}
+
+
+static int
+ansi_key (ANSI * a, Context * c, int key)
+{
+ if (!c->d)
+ return c->k->key (c->k, c, key);
+
+ cmd_show_status (c->d, c);
+
+ if (c->d->active) {
+ if (key == CMD_CANCEL_KEY) {
+ return cmd_deactivate (c->d, c);
+ } else if (key == CMD_KEY) {
+ cmd_deactivate (c->d, c);
+ } else {
+ return cmd_key (c->d, c, a, key);
+ }
+ } else if (key == CMD_KEY) {
+ return cmd_activate (c->d, c);
+ }
+
+ return c->k->key (c->k, c, key);
+}
+
+
+static void
+ansi_flush_escape (ANSI * a, Context * c)
+{
+ ANSI_Parser *p = &a->parser;
+ int i;
+
+ for (i = 0; i < p->escape_ptr; ++i) {
+ ansi_key (a, 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')) {
+ ansi_key (a, c, KEY_UP + (p->escape_buf[2] - 'A'));
+ } else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z')) {
+ ansi_key (a, 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;
+ int l = p->escape_ptr - 1;
+ char *end;
+ int k;
+
+ if ((p->escape_buf[1] != '[') || (p->escape_buf[l] != '~')) {
+ ansi_flush_escape (a, c);
+ return;
+ }
+
+ k = strtol (&p->escape_buf[2], &end, 10);
+
+ if (end != &p->escape_buf[l]) {
+ ansi_flush_escape (a, c);
+ return;
+ }
+
+ ansi_key (a, c, KEY_180 + k);
+
+ 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:
+ case 5:
+ case 6:
+ case 7:
+ if (p->escape_buf[p->escape_ptr - 1] == '~')
+ ansi_parse_ansikey (a, c);
+ break;
+ default:
+ 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 {
+ ansi_key (a, c, ch);
+ }
+
+}
+
+static void
+ansi_parse (ANSI * a, Context * c, char *buf, int len)
+{
+ while (len--)
+ ansi_parse_char (a, c, *(buf++));
+}
+
+static 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;
+
+ ansi_parse (a, c, buf, red);
+
+ return 0;
+}
+
+
+
+static int
+ansi_update (ANSI * a, Context * c)
+{
+ int err = 0;
+
+ err += ansi_history (a, c->h);
+ err += ansi_draw (a, &c->v->crt);
+ tty_length (a->terminal, c->v->crt.size.y);
+ return err;
+}
+
+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 = xmalloc (sizeof (ANSI));
+ memset (ret, 0, sizeof (ANSI));
+
+ ret->terminal = t;