chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / ansi.c
1 /*
2  * ansi.c:
3  *
4  * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
5  * All rights reserved.
6  *
7  */
8
9 static char rcsid[] = "$Id$";
10
11 /*
12  * $Log$
13  * Revision 1.32  2008/02/26 23:56:12  james
14  * *** empty log message ***
15  *
16  * Revision 1.31  2008/02/26 23:23:17  james
17  * *** empty log message ***
18  *
19  * Revision 1.30  2008/02/24 00:42:53  james
20  * *** empty log message ***
21  *
22  * Revision 1.29  2008/02/23 11:48:37  james
23  * *** empty log message ***
24  *
25  * Revision 1.28  2008/02/22 17:07:00  james
26  * *** empty log message ***
27  *
28  * Revision 1.27  2008/02/20 22:54:22  staffcvs
29  * *** empty log message ***
30  *
31  * Revision 1.26  2008/02/20 20:16:07  james
32  * *** empty log message ***
33  *
34  * Revision 1.25  2008/02/20 19:44:37  james
35  * @@
36  *
37  * Revision 1.24  2008/02/20 19:36:06  james
38  * @@
39  *
40  * Revision 1.23  2008/02/20 19:25:09  james
41  * *** empty log message ***
42  *
43  * Revision 1.22  2008/02/15 03:32:07  james
44  * *** empty log message ***
45  *
46  * Revision 1.21  2008/02/14 02:46:44  james
47  * *** empty log message ***
48  *
49  * Revision 1.20  2008/02/14 01:55:57  james
50  * *** empty log message ***
51  *
52  * Revision 1.19  2008/02/13 16:57:29  james
53  * *** empty log message ***
54  *
55  * Revision 1.18  2008/02/13 09:12:21  james
56  * *** empty log message ***
57  *
58  * Revision 1.17  2008/02/13 01:08:18  james
59  * *** empty log message ***
60  *
61  * Revision 1.16  2008/02/07 13:22:51  james
62  * *** empty log message ***
63  *
64  * Revision 1.15  2008/02/07 13:19:48  james
65  * *** empty log message ***
66  *
67  * Revision 1.14  2008/02/07 12:21:16  james
68  * *** empty log message ***
69  *
70  * Revision 1.13  2008/02/07 12:16:04  james
71  * *** empty log message ***
72  *
73  * Revision 1.12  2008/02/07 11:32:41  james
74  * *** empty log message ***
75  *
76  * Revision 1.11  2008/02/07 11:11:14  staffcvs
77  * *** empty log message ***
78  *
79  * Revision 1.10  2008/02/07 01:02:52  james
80  * *** empty log message ***
81  *
82  * Revision 1.9  2008/02/07 00:43:27  james
83  * *** empty log message ***
84  *
85  * Revision 1.8  2008/02/07 00:39:13  james
86  * *** empty log message ***
87  *
88  * Revision 1.7  2008/02/06 20:26:57  james
89  * *** empty log message ***
90  *
91  * Revision 1.6  2008/02/06 17:53:28  james
92  * *** empty log message ***
93  *
94  * Revision 1.5  2008/02/06 15:53:22  james
95  * *** empty log message ***
96  *
97  * Revision 1.4  2008/02/04 20:23:55  james
98  * *** empty log message ***
99  *
100  * Revision 1.3  2008/02/04 05:45:55  james
101  * ::
102  *
103  * Revision 1.2  2008/02/04 02:05:06  james
104  * *** empty log message ***
105  *
106  * Revision 1.1  2008/02/03 23:31:25  james
107  * *** empty log message ***
108  *
109  */
110 #include "project.h"
111
112
113 static void
114 ansi_move (ANSI * a, CRT_Pos p)
115 {
116   char buf[16];
117   int n;
118   int dx = p.x - a->pos.x;
119   int dy = p.y - a->pos.y;
120
121 //  a->pos.x = ANSI_INVAL;
122
123   if (a->pos.x != ANSI_INVAL)
124     {
125
126       if ((!dx) && (!dy))
127         return;
128
129       if (!dy)
130         {
131           if (dx == 1)
132             {
133               a->terminal->xmit (a->terminal, "\033[C", 3);
134             }
135           else if (dx == -1)
136             {
137               a->terminal->xmit (a->terminal, "\033[D", 3);
138             }
139           else
140             {
141               n = snprintf (buf, sizeof (buf), "\033[%dG", p.x + 1);
142               a->terminal->xmit (a->terminal, buf, n);
143             }
144         }
145       else if (!dx)
146         {
147           if (dy == -1)
148             {
149               a->terminal->xmit (a->terminal, "\033[A", 3);
150             }
151           else if (dy == 1)
152             {
153               a->terminal->xmit (a->terminal, "\033[B", 3);
154             }
155           else if (dy < 0)
156             {
157               n = snprintf (buf, sizeof (buf), "\033[%dA", -dy);
158               a->terminal->xmit (a->terminal, buf, n);
159             }
160           else
161             {
162               n = snprintf (buf, sizeof (buf), "\033[%dB", dy);
163               a->terminal->xmit (a->terminal, buf, n);
164             }
165         }
166       else if (!p.x)
167         {
168           if (dy == 1)
169             {
170               a->terminal->xmit (a->terminal, "\033[E", 3);
171             }
172           else if (dy == -1)
173             {
174               a->terminal->xmit (a->terminal, "\033[F", 3);
175             }
176           else if (dy > 0)
177             {
178               n = snprintf (buf, sizeof (buf), "\033[%dE", dy);
179               a->terminal->xmit (a->terminal, buf, n);
180             }
181           else
182             {
183               n = snprintf (buf, sizeof (buf), "\033[%dF", -dy);
184               a->terminal->xmit (a->terminal, buf, n);
185             }
186         }
187       else
188         {
189           n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
190           a->terminal->xmit (a->terminal, buf, n);
191         }
192     }
193   else
194     {
195       n = snprintf (buf, sizeof (buf), "\033[%d;%dH", p.y + 1, p.x + 1);
196       a->terminal->xmit (a->terminal, buf, n);
197     }
198
199   a->pos = p;
200 }
201
202
203 static void
204 ansi_showhide_cursor (ANSI * a, int hide)
205 {
206   if (a->hide_cursor == hide)
207     return;
208
209   if (hide)
210     {
211       a->terminal->xmit (a->terminal, "\033[?25l", 6);
212     }
213   else
214     {
215       a->terminal->xmit (a->terminal, "\033[?25h", 6);
216     }
217
218   a->hide_cursor = hide;
219 }
220
221
222 static void
223 ansi_force_attr_normal (ANSI * a)
224 {
225   a->terminal->xmit (a->terminal, "\033[0m", 4);
226   a->attr = CRT_ATTR_NORMAL;
227   a->color = ANSI_INVAL;
228 }
229
230 static void
231 ansi_set_color (ANSI * a, int color)
232 {
233   int dif;
234   char buf[16];
235   int i;
236   int fg, bg;
237
238   if ((a->color == ANSI_INVAL) || (color != a->color))
239     {
240       fg = CRT_COLOR_FG (color);
241       bg = CRT_COLOR_BG (color);
242
243       if (fg & CRT_COLOR_INTENSITY)
244         {
245           fg += 90;
246         }
247       else
248         {
249           fg += 30;
250         }
251
252       if (bg & CRT_COLOR_INTENSITY)
253         {
254           bg += 100;
255         }
256       else
257         {
258           bg += 40;
259         }
260
261       i = sprintf (buf, "\033[%d;%dm", fg, bg);
262 #if 0
263       fprintf (stderr, "Color set to %d %d %x\n", fg, bg, color);
264 #endif
265
266       a->terminal->xmit (a->terminal, buf, i);
267       a->color = color;
268     }
269 }
270
271 static void
272 ansi_set_attr (ANSI * a, int attr)
273 {
274   int dif;
275
276   dif = attr ^ a->attr;
277
278   if (!dif)
279     return;
280
281   a->attr = attr;
282
283 #if 0
284   if (attr == CRT_ATTR_NORMAL)
285     {
286       ansi_force_attr_normal (a);
287       return;
288     }
289 #endif
290
291   if (dif & CRT_ATTR_UNDERLINE)
292     {
293       if (attr & CRT_ATTR_UNDERLINE)
294         {
295           a->terminal->xmit (a->terminal, "\033[4m", 4);
296         }
297       else
298         {
299           a->terminal->xmit (a->terminal, "\033[24m", 5);
300         }
301     }
302   if (dif & CRT_ATTR_REVERSE)
303     {
304       if (attr & CRT_ATTR_REVERSE)
305         {
306           a->terminal->xmit (a->terminal, "\033[7m", 4);
307         }
308       else
309         {
310           a->terminal->xmit (a->terminal, "\033[27m", 5);
311         }
312     }
313   if (dif & CRT_ATTR_BOLD)
314     {
315       if (attr & CRT_ATTR_BOLD)
316         {
317           a->terminal->xmit (a->terminal, "\033[1m", 4);
318         }
319       else
320         {
321           a->terminal->xmit (a->terminal, "\033[21m", 5);
322           a->terminal->xmit (a->terminal, "\033[22m", 5);
323         }
324     }
325
326 }
327
328 static void
329 ascii_emit (TTY * t, uint32_t ch)
330 {
331   int i;
332
333 /*Some quick obvious subsititons for quotation marks*/
334   switch (ch)
335     {
336     case 0x2018:
337       ch = '`';
338       break;
339     case 0x2019:
340       ch = '\'';
341       break;
342     case 0x201c:
343     case 0x201d:
344       ch = '"';
345       break;
346     }
347
348 /*Short cut the easy stuff*/
349   if (ch < 0x7f)
350     {
351       uint8_t c = ch;
352       t->xmit (t, &c, 1);
353       return;
354     }
355
356   for (i = 0; i < VT102_CHARSET_SIZE; ++i)
357     {
358       if (vt102_charset_gl[i] == ch)
359         {
360           uint8_t c[3] = { 016, i, 017 };
361           t->xmit (t, &c, 3);
362           return;
363         }
364     }
365
366   t->xmit (t, "?", 1);
367
368 }
369
370 static void
371 ansi_render (ANSI * a, CRT_CA ca)
372 {
373   int dif;
374
375   if ((ca.chr < VT102_CHARSET_SIZE) && (vt102_charset_c0[ca.chr]))
376     ca.chr = vt102_charset_c0[ca.chr];
377
378   if ((ca.chr >= 0x80) && (ca.chr < 0xa0))
379     ca.chr = ' ';
380
381   ansi_set_attr (a, ca.attr);
382   ansi_set_color (a, ca.color);
383
384   if (a->utf8)
385     utf8_emit (a->terminal, ca.chr);
386   else
387     ascii_emit (a->terminal, ca.chr);
388
389   a->pos.x++;
390
391 /*Can't easily wrap round here as don't know size of destination screen*/
392 /*so invalidate the cached cursor position*/
393
394   if (a->pos.x >= a->size.x)
395     a->pos.x = ANSI_INVAL;
396
397 }
398
399 static void
400 ansi_cls (ANSI * a)
401 {
402   CRT_Pos p = { 0 };
403
404   crt_cls (&a->crt);
405
406   ansi_set_attr (a, CRT_ATTR_NORMAL);
407   ansi_set_color (a, CRT_COLOR_NORMAL);
408   ansi_move (a, p);
409   a->terminal->xmit (a->terminal, "\033[2J", 4);
410 /*different emulators leave cursor in different places after cls differently*/
411   a->pos.x = ANSI_INVAL;
412 }
413
414 #if 0
415 int
416 ansi_scroll_up (ANSI * a, CRT_Pos s, CRT_Pos e)
417 {
418   char buf[16];
419   int i;
420   if (s.x)
421     return -1;
422   if (e.x != (CRT_COLS - 1))
423     return -1;
424   if (s.y)
425     return -1;
426   if (s.y >= a->size.y)
427     return -1;
428
429   ansi_showhide_cursor (a, 1);
430   i = sprintf (buf, "\033[%d;%dr", s.y + 1, e.y + 1);
431   a->terminal->xmit (a->terminal, buf, i);
432   i = sprintf (buf, "\033[%d;%dH", e.y + 1, 0);
433   a->terminal->xmit (a->terminal, buf, i);
434   a->terminal->xmit (a->terminal, "\033D", 2);
435   a->terminal->xmit (a->terminal, "\033[r", 3);
436
437   s.y = e.y;
438   crt_erase (&a->crt, s, e, 1);
439
440   a->pos.x = ANSI_INVAL;
441
442   return 0;
443 }
444
445
446 static void
447 ansi_spot_scroll_up (ANSI * a, CRT * c)
448 {
449   int l, n, p;
450
451   l = c->sh.e.x - c->sh.s.x;
452   l++;
453   l *= sizeof (CRT_CA);
454
455   n = c->sh.e.y - c->sh.s.y;
456   p = CRT_ADDR_POS (&c->sh.s);
457
458   while (n--)
459     {
460       if (memcmp (&c->screen[p], &a->crt.screen[p + CRT_COLS], l))
461         return;
462       p += CRT_COLS;
463     }
464
465   if (ansi_scroll_up (a, c->sh.s, c->sh.e))
466     return;
467
468   n = c->sh.e.y - c->sh.s.y;
469   p = CRT_ADDR_POS (&c->sh.s);
470
471   while (n--)
472     {
473       memcpy (&a->crt.screen[p], &a->crt.screen[p + CRT_COLS], l);
474       p += CRT_COLS;
475     }
476
477   c->sh.dir = 0;                //FIXME: horrid hack
478
479 }
480
481 static void
482 ansi_spot_scroll (ANSI * a, CRT * c)
483 {
484
485   switch (c->sh.dir)
486     {
487     case -1:
488 /*we only care about up for the moment */
489       ansi_spot_scroll_up (a, c);
490       break;
491     }
492
493   return;
494 }
495
496 #endif
497
498
499 static void
500 ansi_draw_line (ANSI * a, CRT_CA * cap, int y)
501 {
502   CRT_Pos p = { 0, y };
503   CRT_CA *acap = &a->crt.screen[CRT_ADDR_POS (&p)];
504
505   for (p.x = 0; p.x < a->crt.width; ++p.x)
506     {
507       if (p.x >= a->size.x)
508         continue;
509
510       if (crt_ca_cmp (*acap, *cap))
511         {
512           ansi_showhide_cursor (a, 1);
513           *acap = *cap;
514
515           ansi_move (a, p);
516           ansi_render (a, *acap);
517         }
518
519       acap++;
520       cap++;
521     }
522 }
523
524 static void
525 ansi_resize_check (ANSI * a, int new_width)
526 {
527
528   if ((new_width && (new_width != a->crt.width))
529       || crt_pos_cmp (a->terminal->size, a->size))
530     {
531
532       terminal_getsize (a->terminal);
533
534       a->size = a->terminal->size;
535
536       a->pos.x = ANSI_INVAL;
537       a->hide_cursor = ANSI_INVAL;
538
539       crt_reset (&a->crt);
540
541       if (new_width)
542         a->crt.width = new_width;
543
544 // FIXME: -- echos back crap?
545 //  a->terminal->xmit (a->terminal, "\033[c", 3);
546
547 // maybe - issue 132 column command if we're 132?
548
549       ansi_cls (a);
550       a->terminal->xmit (a->terminal, "\033=", 2);
551       a->terminal->xmit (a->terminal, "\033[?6l", 5);
552       a->terminal->xmit (a->terminal, "\033[r", 3);
553       if (a->utf8)
554         {
555           a->terminal->xmit (a->terminal, "\033%G", 3);
556         }
557       else
558         {
559           a->terminal->xmit (a->terminal, "\033(B", 3);
560           a->terminal->xmit (a->terminal, "\033)0", 3);
561           a->terminal->xmit (a->terminal, "\017", 1);
562         }
563
564     }
565 }
566
567 #define HISTORY_GUESS_SCROLL 24 /*guess all 24 lines usually scroll */
568 /*if they haven't then ansi_draw will patch it up*/
569
570 static void
571 ansi_history (ANSI * a, History * h)
572 {
573   char buf[32];
574   int i;
575 /*Do we need to catch up on history?*/
576
577   if (a->history_ptr == h->wptr)
578     return;
579   ansi_resize_check (a, 0);
580
581   if ((a->size.x < a->crt.width) || (a->size.y < CRT_ROWS))
582     return;
583
584   ansi_force_attr_normal (a);
585   ansi_set_color (a, CRT_COLOR_NORMAL);
586
587   i = sprintf (buf, "\033[%d;%dr", 1, HISTORY_GUESS_SCROLL);
588   a->terminal->xmit (a->terminal, buf, i);
589
590
591   while (a->history_ptr != h->wptr)
592     {
593
594       History_ent *e = &h->lines[a->history_ptr];
595
596       HISTORY_INC (h, a->history_ptr);
597
598       if (!e->valid)
599         continue;
600
601       /*If so write the line ot the top of the screen */
602       ansi_draw_line (a, e->line, 0);
603
604
605       /*Roll HISTORY_GUESS_SCROLL lines up putting the top line into the xterm's history */
606
607
608       /*Make extra lines a predictable colour */
609       ansi_set_color (a, CRT_COLOR_NORMAL);
610
611       ansi_showhide_cursor (a, 1);
612       i = sprintf (buf, "\033[%d;%dH", HISTORY_GUESS_SCROLL, 1);
613       a->terminal->xmit (a->terminal, buf, i);
614       a->terminal->xmit (a->terminal, "\033D", 2);
615       a->pos.x = ANSI_INVAL;
616
617       /*now do the same in our image of the screen */
618
619       {
620         CRT_Pos s = { 0 }
621         , e =
622         {
623         0};
624
625         /*scroll lines up */
626         for (s.y++; s.y < HISTORY_GUESS_SCROLL; s.y++, e.y++)
627           {
628             memcpy (&a->crt.screen[CRT_ADDR_POS (&e)],
629                     &a->crt.screen[CRT_ADDR_POS (&s)],
630                     sizeof (CRT_CA) * a->crt.width);
631           }
632
633         /* erase new line */
634         s.y = e.y;
635         e.x = CRT_COLS - 1;
636         crt_erase (&a->crt, s, e, 1, CRT_COLOR_NORMAL);
637       }
638
639     }
640 /*reset margins*/
641   a->terminal->xmit (a->terminal, "\033[r", 3);
642   a->pos.x = ANSI_INVAL;
643
644 }
645
646
647
648
649 static void
650 ansi_draw (ANSI * a, CRT * c)
651 {
652   CRT_Pos p;
653   int o;
654   int hidden_cursor = 0;
655
656   ansi_resize_check (a, c->width);
657
658
659   for (p.y = 0; p.y < CRT_ROWS; ++p.y)
660     {
661       if (p.y >= a->size.y)
662         continue;
663
664       ansi_draw_line (a, &c->screen[CRT_ADDR (p.y, 0)], p.y);
665
666     }
667
668
669   if ((c->width > a->size.x) || (CRT_ROWS > a->size.y))
670     {
671       char msg[1024];           // = "Window is too small";
672       int i;
673       p.x = 0;
674       p.y = 0;
675
676       i =
677         sprintf (msg, "Window too small (%dx%d need %dx%d)", a->size.x,
678                  a->size.y, c->width, CRT_ROWS);
679
680       ansi_showhide_cursor (a, 1);
681       ansi_set_attr (a, CRT_ATTR_REVERSE);
682       ansi_set_color (a, CRT_MAKE_COLOR (CRT_COLOR_WHITE, CRT_COLOR_RED));
683       ansi_move (a, p);
684
685       a->terminal->xmit (a->terminal, msg, i);
686       a->pos.x = ANSI_INVAL;
687     }
688
689
690   if ((c->pos.x >= a->size.x) || (c->pos.y >= a->size.y))
691     {
692       ansi_showhide_cursor (a, 1);
693       return;
694     }
695
696   a->crt.pos = c->pos;
697   ansi_move (a, a->crt.pos);
698
699   a->crt.hide_cursor = c->hide_cursor;
700   ansi_showhide_cursor (a, a->crt.hide_cursor);
701 }
702
703 static void
704 ansi_reset (ANSI * a, CRT * c)
705 {
706   a->size.x = -1;
707   ansi_draw (a, c ? c : &a->crt);
708 }
709
710 static void
711 ansi_terminal_reset (ANSI * a)
712 {
713   CRT_Pos p = { 0, CRT_ROWS };
714   ansi_force_attr_normal (a);
715
716   ansi_move (a, p);
717 }
718
719 static void
720 ansi_flush_escape (ANSI * a, Context * c)
721 {
722   ANSI_Parser *p = &a->parser;
723   int i;
724
725   for (i = 0; i < p->escape_ptr; ++i)
726     {
727       keydis_key (c->k, c, p->escape_buf[i]);
728     }
729
730   p->escape_ptr = 0;
731   p->in_escape = 0;
732 }
733
734 static void
735 ansi_parse_deckey (ANSI * a, Context * c)
736 {
737   ANSI_Parser *p = &a->parser;
738   if ((p->escape_buf[1] != '[') && (p->escape_buf[1] != 'O'))
739     {
740       ansi_flush_escape (a, c);
741       return;
742     }
743
744   if ((p->escape_buf[2] >= 'A') || (p->escape_buf[2] <= 'Z'))
745     {
746       keydis_key (c->k, c, KEY_UP + (p->escape_buf[2] - 'A'));
747     }
748   else if ((p->escape_buf[2] >= 'a') || (p->escape_buf[2] <= 'z'))
749     {
750       keydis_key (c->k, c, KEY_154 + (p->escape_buf[2] - 'a'));
751     }
752   else
753     {
754       ansi_flush_escape (a, c);
755       return;
756     }
757   p->in_escape = 0;
758   p->escape_ptr = 0;
759 }
760
761 static void
762 ansi_parse_ansikey (ANSI * a, Context * c)
763 {
764   ANSI_Parser *p = &a->parser;
765
766   if ((p->escape_buf[1] != '[') || (p->escape_buf[3] != '~'))
767     {
768       ansi_flush_escape (a, c);
769       return;
770     }
771   if ((p->escape_buf[2] >= '0') || (p->escape_buf[2] <= '9'))
772     {
773       keydis_key (c->k, c, KEY_180 + (p->escape_buf[2] - '0'));
774     }
775   else
776     {
777       ansi_flush_escape (a, c);
778       return;
779     }
780
781   p->in_escape = 0;
782   p->escape_ptr = 0;
783 }
784
785
786
787 static void
788 ansi_parse_escape (ANSI * a, Context * c)
789 {
790   ANSI_Parser *p = &a->parser;
791   switch (p->escape_ptr)
792     {
793     case 0:
794     case 1:
795       return;
796     case 2:
797       switch (p->escape_buf[1])
798         {
799         case '[':
800         case 'O':
801           break;
802         default:
803           ansi_flush_escape (a, c);
804         }
805       break;
806     case 3:
807       switch (p->escape_buf[1])
808         {
809         case 'O':
810           ansi_parse_deckey (a, c);
811           break;
812         case '[':
813           if ((p->escape_buf[2] >= 'A') && (p->escape_buf[2] <= 'Z'))
814             ansi_parse_deckey (a, c);
815           break;
816         default:
817           ansi_flush_escape (a, c);
818         }
819       break;
820     case 4:
821       switch (p->escape_buf[1])
822         {
823         case '[':
824           ansi_parse_ansikey (a, c);
825           break;
826         default:
827           ansi_flush_escape (a, c);
828         }
829       break;
830     case 5:
831       ansi_flush_escape (a, c);
832     }
833 }
834
835
836 static void
837 ansi_check_escape (ANSI * a, Context * c)
838 {
839   ANSI_Parser *p = &a->parser;
840   struct timeval now, diff;
841   gettimeofday (&now, NULL);
842   timersub (&now, &p->last_escape, &diff);
843
844 #if 0
845   fprintf (stderr, "ie %d tl %d.%06d eb %d\n",
846            p->in_escape, diff.tv_sec, diff.tv_usec, p->escape_ptr);
847 #endif
848
849   if (!p->in_escape)
850     return;
851
852
853   /*Time up? */
854   if (diff.tv_sec || (diff.tv_usec > ANSI_ESCAPE_TIMEOUT))
855     ansi_flush_escape (a, c);
856
857 }
858
859
860 static void
861 ansi_parse_char (ANSI * a, Context * c, int ch)
862 {
863   ANSI_Parser *p = &a->parser;
864
865
866 /*See if it's time to flush the escape*/
867   ansi_check_escape (a, c);
868
869   if (ch == 033)
870     {
871       if (p->in_escape)
872         ansi_flush_escape (a, c);
873
874       p->in_escape++;
875       p->escape_ptr = 0;
876       gettimeofday (&p->last_escape, NULL);
877     }
878
879   if (p->in_escape)
880     {
881       p->escape_buf[p->escape_ptr++] = ch;
882       ansi_parse_escape (a, c);
883     }
884   else
885     {
886       keydis_key (c->k, c, ch);
887     }
888
889 }
890
891 static void
892 ansi_parse (ANSI * a, Context * c, char *buf, int len)
893 {
894   while (len--)
895     ansi_parse_char (a, c, *(buf++));
896 }
897
898 int
899 ansi_dispatch (ANSI * a, Context * c)
900 {
901   char buf[1024];
902   int red;
903
904   ansi_check_escape (a, c);
905
906
907   if (!a->terminal)
908     return 0;
909
910   red = a->terminal->recv (a->terminal, buf, sizeof (buf));
911   if (red <= 0)
912     return red;
913
914 #if 0
915   if (*buf == 3)
916     return -1;
917 #endif
918
919 #if 0
920   if (*buf == 2)
921     {
922 #if  0
923       a->history_ptr = c->h->wptr;
924       HISTORY_INC (c->h, a->history_ptr);
925 #endif
926       return -1;
927     }
928 #endif
929
930
931   ansi_parse (a, c, buf, red);
932
933   return 0;
934 }
935
936
937 static void
938 ansi_update (ANSI * a, Context * c)
939 {
940   ansi_history (a, c->h);
941   ansi_draw (a, &c->v->crt);
942 }
943
944 static void
945 ansi_free (ANSI * a)
946 {
947   a->terminal_reset (a);
948   if (a->terminal)
949     a->terminal->close (a->terminal);
950
951   free (a);
952
953 }
954
955 ANSI *
956 ansi_new_from_terminal (TTY * t, int utf8)
957 {
958   ANSI *ret;
959
960   ret = malloc (sizeof (ANSI));
961   memset (ret, 0, sizeof (ANSI));
962
963   ret->terminal = t;
964
965   ret->utf8 = utf8;
966   ret->update = ansi_update;
967   ret->reset = ansi_reset;
968   ret->terminal_reset = ansi_terminal_reset;
969   ret->close = ansi_free;
970   ret->dispatch = ansi_dispatch;
971
972   return ret;
973 }