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