chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / vt102.c
1 /*
2  * vt102.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.11  2008/02/06 20:26:58  james
14  * *** empty log message ***
15  *
16  * Revision 1.10  2008/02/06 17:53:28  james
17  * *** empty log message ***
18  *
19  * Revision 1.9  2008/02/06 15:53:22  james
20  * *** empty log message ***
21  *
22  * Revision 1.8  2008/02/06 11:49:47  james
23  * *** empty log message ***
24  *
25  * Revision 1.7  2008/02/06 11:30:37  james
26  * *** empty log message ***
27  *
28  * Revision 1.6  2008/02/05 01:11:46  james
29  * *** empty log message ***
30  *
31  * Revision 1.5  2008/02/04 20:23:55  james
32  * *** empty log message ***
33  *
34  * Revision 1.4  2008/02/04 05:45:55  james
35  * ::
36  *
37  * Revision 1.3  2008/02/04 02:05:06  james
38  * *** empty log message ***
39  *
40  * Revision 1.2  2008/02/04 01:32:39  james
41  * *** empty log message ***
42  *
43  * Revision 1.1  2008/02/03 23:36:41  james
44  * *** empty log message ***
45  *
46  */
47
48
49 /* Termcap he say:
50
51 vt102|dec vt102:\
52         :mi:\
53         :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
54
55 vt100|vt100-am|dec vt100 (w/advanced video):\
56         :am:bs:ms:xn:xo:\
57         :co#80:it#8:li#24:vt#3:\
58         :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
59         :UP=\E[%dA:\
60         :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
61         :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
62         :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
63         :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
64         :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
65         :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
66         :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
67         :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
68         :us=\E[4m:tc=vt100+fnkeys:
69
70 vt100+fnkeys|dec vt100 numeric keypad:\
71         :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
72         :tc=vt100+pfkeys:
73
74 vt100+pfkeys|dec vt100 numeric keypad:\
75         :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
76
77 vt100+keypad|dec vt100 numeric keypad no fkeys:\
78         :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
79
80 */
81
82 /*
83 so the parser needs to be able to at least do
84 CTRL-G
85 CTRL-H
86 CTRL-I
87 CTRL-J
88 CTRL-M
89 CTRL-N
90
91 CTRL-O
92 ESC7
93 ESC8
94 ESCH
95 ESCM
96 ESC> 
97
98 ESC[%dA
99 ESC[%dB
100 ESC[%dC
101 ESC[%dD
102 ESC[H
103 ESC[%d;%dH
104 ESC[J
105 ESC[K
106 ESC[1K
107 ESC[L
108 ESC[M
109 ESC[P
110
111 ESC[3g
112 ESC[4h
113 ESC[4l
114 ESC[m 
115 ESC[1m
116 ESC[4m
117 ESC[5m
118 ESC[7m
119 ESC[%d;%dr
120
121
122 ESC[?3l 
123 ESC[?4l 
124 ESC[?5l
125 ESC[?7h
126 ESC[?7h
127 ESC[?7l
128 ESC[?8h
129
130 ESC(B
131 ESC)0
132
133
134 TODO:
135
136 ESC(B
137 ESC)0
138
139 CTRL-O
140
141 ESC7
142 ESC8
143 ESCH
144 ESCM
145 ESC> 
146
147 */
148
149 #include "project.h"
150
151
152
153 static inline int
154 csi_ender (int c)
155 {
156   if ((c >= 'a') && (c <= 'z'))
157     return 1;
158   if ((c >= 'A') && (c <= 'Z'))
159     return 1;
160   return 0;
161 }
162
163 static inline int
164 csi_starter (int c)
165 {
166   switch (c)
167     {
168     case '[':
169     case '(':
170     case ')':
171       return 1;
172     }
173   return 0;
174 }
175
176 static inline int
177 in_margins (VT102 * v, CRT_Pos p)
178 {
179   if (v->pos.x < v->top_margin.x)
180     return 0;
181   if (v->pos.y < v->top_margin.y)
182     return 0;
183
184   if (v->pos.x > v->bottom_margin.x)
185     return 0;
186   if (v->pos.y > v->bottom_margin.y)
187     return 0;
188
189   return 1;
190 }
191
192 void
193 vt102_log_line (VT102 * v, int line)
194 {
195   static FILE *log;
196   CRT_Pos e = { VT102_COLS - 1, line };
197   CRT_Pos p = { 0, line };
198
199   if (!log)
200     log = fopen ("log", "a+");
201
202   if (!log)
203     return;
204
205
206   for (; e.x > 0; --e.x)
207     {
208       if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
209         break;
210     }
211
212   for (; p.x <= e.x; ++p.x)
213     {
214       int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
215       if (c < 32)
216         c = ' ';
217       if (c > 126)
218         c = ' ';
219       fputc (c, log);
220     }
221   fputc ('\n', log);
222 }
223
224 void
225 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
226 {
227   if (v->pos.x < tl.x)
228     v->pos.x = tl.x;
229   if (v->pos.y < tl.y)
230     v->pos.y = tl.y;
231
232   if (v->pos.x > br.x)
233     v->pos.x = br.x;
234   if (v->pos.y > br.y)
235     v->pos.y = br.y;
236 }
237
238
239 void
240 vt102_cursor_normalize (VT102 * v)
241 {
242   CRT_Pos *top, *bottom;
243
244   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
245     {
246       vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
247     }
248   else
249     {
250       vt102_clip_cursor (v, v->screen_start, v->screen_end);
251     }
252 }
253
254
255 void
256 vt102_cursor_carriage_return (VT102 * v)
257 {
258    /*FISH*/ v->pos.x = v->top_margin.x;
259   v->pending_wrap = 0;
260 }
261
262 void
263 vt102_cursor_advance_line (VT102 * v)
264 {
265   int couldscroll = in_margins (v, v->pos);
266
267 /*have wraped off end of last line in scrolling region */
268 /* (|| not necessary, but shuts compiler up */
269   if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
270       (couldscroll))
271     {
272       vt102_log_line (v, v->pos.y);
273       crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
274       return;
275     }
276
277   v->pos.y++;
278   v->pending_wrap = 0;
279 }
280
281
282 void
283 vt102_cursor_advance (VT102 * v)
284 {
285
286   if (v->pos.x < v->bottom_margin.x)
287     {
288 /*Normal advance*/
289       v->pos.x++;
290       v->pending_wrap = 0;
291       return;
292     }
293   v->pending_wrap++;
294 }
295
296
297 void
298 vt102_do_pending_wrap (VT102 * v)
299 {
300   int couldscroll = in_margins (v, v->pos);
301   int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
302
303   if (!v->pending_wrap)
304     return;
305
306 #if 0
307   fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
308            v->pos.y, autowrap, couldscroll);
309 #endif
310
311 /*End of line but no autowrap, nothing to do*/
312   if (!autowrap)
313     return;
314
315 /*End of screen and not allowed to scroll, nothing to do*/
316   if ((v->pos.y == v->screen_end.y) && (!couldscroll))
317     return;
318
319   if (couldscroll)
320     {
321       v->pos.x = v->top_margin.x;
322     }
323   else
324     {
325       v->pos.x = 0;
326     }
327
328   vt102_cursor_advance_line (v);
329 }
330
331
332 void
333 vt102_cursor_retard (VT102 * v)
334 {
335   if (v->pos.x != v->top_margin.x)
336     v->pos.x--;
337
338   v->pending_wrap = 0;
339 }
340
341 void
342 vt102_reset_tabs (VT102 * v)
343 {
344   int i;
345
346   memset (v->tabs, 0, sizeof (v->tabs));
347
348   for (i = 0; i < VT102_COLS; i += 8)
349     {
350       v->tabs[i]++;
351     }
352 }
353 void
354 vt102_cursor_advance_tab (VT102 * v)
355 {
356   if (v->pos.x == v->bottom_margin.x)
357     return;
358   while (v->pos.x < v->bottom_margin.x)
359     {
360       v->pos.x++;
361       if (v->tabs[v->pos.x])
362         break;
363     }
364   v->pending_wrap = 0;
365 }
366
367 vt102_cursor_home (VT102 * v)
368 {
369   v->pos = v->top_margin;
370   vt102_cursor_normalize (v);
371   v->pending_wrap = 0;
372
373 }
374
375 vt102_cursor_absolute (VT102 * v, int x, int y)
376 {
377   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
378     {
379       v->pos.x = x + v->top_margin.x;
380       v->pos.y = y + v->top_margin.x;
381     }
382   else
383     {
384       v->pos.x = x;
385       v->pos.y = y;
386     }
387   vt102_cursor_normalize (v);
388   v->pending_wrap = 0;
389 }
390
391 vt102_cursor_relative (VT102 * v, int x, int y)
392 {
393   v->pos.x += x;
394   v->pos.y += y;
395   vt102_cursor_normalize (v);
396   v->pending_wrap = 0;
397 }
398
399
400
401 void
402 vt102_delete_from_line (VT102 * v, CRT_Pos p)
403 {
404   int n = v->bottom_margin.x - p.x;
405
406   if (n < 0)
407     return;
408
409   if (n)
410     {
411
412       memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
413                &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
414     }
415
416   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
417 /*But not attr due to vt102 bug*/
418 }
419
420 void
421 vt102_insert_into_line (VT102 * v, CRT_Pos p)
422 {
423   int n = v->bottom_margin.x - p.x;
424
425   if (n < 0)
426     return;
427
428   if (n)
429     {
430
431       memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
432                &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
433     }
434
435   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
436   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
437 }
438
439
440
441 void
442 vt102_change_mode (VT102 * v, int private, char *ns, int set)
443 {
444   int m;
445
446
447   if (*ns)
448     {
449       m = atoi (ns);
450     }
451   else
452     {
453       m = 1;
454     }
455
456   if (m < 0)
457     return;
458   if (m >= VT102_NMODES)
459     return;
460
461   if (private)
462     {
463       v->private_modes[m] = set;
464       switch (m)
465         {
466         case VT102_PRIVATE_MODE_ORIGIN_MODE:
467           vt102_cursor_home (v);
468           break;
469         }
470
471     }
472   else
473     v->modes[m] = set;
474
475 #if 0
476   fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
477 #endif
478 }
479
480 void
481 vt102_parse_mode_string (VT102 * v, char *buf, int len)
482 {
483   int private = 0;
484   char last = buf[len - 1];
485   char num[4];
486   int o;
487
488   memset (num, 0, sizeof (num));
489   o = sizeof (num) - 1;
490
491   len--;
492
493   if (*buf == '?')
494     {
495       private++;
496       buf++;
497       len--;
498     }
499
500   if (len < 0)
501     return;
502
503   while (len--)
504     {
505       if (*buf == ';')
506         {
507           vt102_change_mode (v, private, &num[o], last == 'h');
508           memset (num, 0, sizeof (num));
509           o = sizeof (num) - 1;
510           buf++;
511           continue;
512         }
513
514       num[0] = num[1];
515       num[1] = num[2];
516       num[2] = *buf;
517
518       if (o)
519         o--;
520
521       buf++;
522     }
523
524   vt102_change_mode (v, private, &num[o], last == 'h');
525
526 }
527
528
529 void
530 vt102_change_attr (VT102 * v, char *na)
531 {
532   int a;
533
534
535   if (*na)
536     {
537       a = atoi (na);
538     }
539   else
540     {
541       a = 0;
542     }
543
544   switch (a)
545     {
546     case 0:
547       v->attr = CRT_ATTR_NORMAL;
548       break;
549     case 1:
550       v->attr |= CRT_ATTR_BOLD;
551       break;
552     case 21:
553     case 22:
554       v->attr &= ~CRT_ATTR_BOLD;
555       break;
556     case 4:
557       v->attr |= CRT_ATTR_UNDERLINE;
558       break;
559     case 24:
560       v->attr &= ~CRT_ATTR_UNDERLINE;
561       break;
562     case 5:
563       v->attr |= CRT_ATTR_BLINK;
564       break;
565     case 25:
566       v->attr &= ~CRT_ATTR_BLINK;
567       break;
568     case 7:
569       v->attr |= CRT_ATTR_REVERSE;
570       break;
571     case 27:
572       v->attr &= ~CRT_ATTR_REVERSE;
573       break;
574     default:
575       ;
576 #if 0
577       fprintf (stderr, "unhandled SGR %d\n", a);
578 #endif
579     }
580
581 }
582
583
584 void
585 vt102_parse_attr_string (VT102 * v, char *buf, int len)
586 {
587   int private = 0;
588   char last = buf[len - 1];
589   char num[4];
590   int o;
591
592   memset (num, 0, sizeof (num));
593   o = sizeof (num) - 1;
594
595   len--;
596
597   if (len < 0)
598     return;
599
600   while (len--)
601     {
602       if (*buf == ';')
603         {
604           vt102_change_attr (v, &num[o]);
605           memset (num, 0, sizeof (num));
606           o = sizeof (num) - 1;
607           buf++;
608           continue;
609         }
610
611       num[0] = num[1];
612       num[1] = num[2];
613       num[2] = *buf;
614
615       if (o)
616         o--;
617
618       buf++;
619     }
620   vt102_change_attr (v, &num[o]);
621 }
622
623 void
624 vt102_save_state (VT102 * v)
625 {
626   v->saved.pos = v->pos;
627   v->saved.attr = v->attr;
628   v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
629 }
630
631 void
632 vt102_restore_state (VT102 * v)
633 {
634   v->pos = v->saved.pos;
635   v->attr = v->saved.attr;
636   v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
637   vt102_cursor_normalize (v);
638   v->pending_wrap = 0;
639 }
640
641 void
642 vt102_parse_esc (VT102 * v, int c)
643 {
644   switch (c)
645     {
646     case 'E':
647       if (v->pos.y == v->bottom_margin.y)
648         {
649           vt102_log_line (v, v->pos.y);
650           crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
651         }
652       else
653         {
654           vt102_cursor_relative (v, 0, 1);
655         }
656       break;
657     case 'H':
658       v->tabs[v->pos.x]++;
659       break;
660     case 'M':
661       if (v->pos.y == v->top_margin.y)
662         {
663           crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
664         }
665       else
666         {
667           vt102_cursor_relative (v, 0, -1);
668         }
669       break;
670     case '7':
671       vt102_save_state (v);
672       break;
673     case '8':
674       vt102_restore_state (v);
675       break;
676     case '=':
677         v->application_keypad_mode=1;
678       break;
679     case '>':
680         v->application_keypad_mode=0;
681       break;
682     default:
683 #if 0
684       fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
685                (c < 32) ? '?' : c);
686 #endif
687       ;
688     }
689 }
690 void
691 vt102_parse_csi (VT102 * v, char *buf, int len)
692 {
693   char last;
694   char *ptr;
695   char *arg = buf + 1;
696   int narg;
697
698   buf[len] = 0;
699
700   last = buf[len - 1];
701 #if 0
702   buf[len - 1] = 0;
703 #endif
704
705   if (len > 2)
706     {
707       narg = atoi (arg);
708     }
709   else
710     {
711       narg = 1;
712     }
713
714   switch (buf[0])
715     {
716     case '[':
717       switch (last)
718         {
719         case 'A':
720           vt102_cursor_relative (v, 0, -narg);
721           break;
722         case 'B':
723           vt102_cursor_relative (v, 0, narg);
724           break;
725         case 'C':
726           vt102_cursor_relative (v, narg, 0);
727           break;
728         case 'D':
729           vt102_cursor_relative (v, -narg, 0);
730           break;
731         case 'E':
732           vt102_cursor_relative (v, 0, narg);
733           vt102_cursor_carriage_return (v);
734           break;
735         case 'F':
736           vt102_cursor_relative (v, 0, -narg);
737           vt102_cursor_carriage_return (v);
738           break;
739         case 'G':
740           vt102_cursor_absolute (v, narg - 1, v->pos.y);
741           break;
742         case 'H':
743         case 'f':
744           {
745             int x, y;
746
747             y = narg - 1;
748
749             ptr = index (arg, ';');
750             if (ptr)
751               x = atoi (ptr + 1) - 1;
752             else
753               x = 0;
754
755             vt102_cursor_absolute (v, x, y);
756           }
757           break;
758         case 'J':
759           switch (narg)
760             {
761             case 1:
762               crt_erase (&v->crt, v->pos, v->screen_end, 1);
763               break;
764             case 2:
765               crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
766               break;
767             }
768           break;
769         case 'K':
770           {
771             CRT_Pos ls = { 0, v->pos.y };
772             CRT_Pos le = { VT102_COLS - 1, v->pos.y };
773             if (len == 2)
774               narg = 0;         /*Different default */
775
776             switch (narg)
777               {
778               case 0:
779                 crt_erase (&v->crt, v->pos, le, 1);
780                 break;
781               case 1:
782                 crt_erase (&v->crt, ls, v->pos, 1);
783                 break;
784               case 2:
785                 crt_erase (&v->crt, ls, le, 1);
786                 break;
787               }
788           }
789           break;
790
791         case 'P':
792           while (narg--)
793             vt102_delete_from_line (v, v->pos);
794           break;
795         case 'L':
796           if ((v->pos.y >= v->top_margin.y)
797               && (v->pos.y <= v->bottom_margin.y))
798             {
799               while (narg--)
800                 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
801             }
802           break;
803
804         case 'M':
805           if ((v->pos.y >= v->top_margin.y)
806               && (v->pos.y <= v->bottom_margin.y))
807             {
808               while (narg--)
809                 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
810             }
811           break;
812
813         case 'g':
814           if (len == 2)
815             narg = 0;           /*Different default */
816
817           switch (narg)
818             {
819             case 0:
820               v->tabs[v->pos.x] = 0;
821               break;
822             case 3:
823               memset (v->tabs, 0, sizeof (v->tabs));
824               break;
825             }
826           break;
827
828         case 'h':
829         case 'l':
830           vt102_parse_mode_string (v, &buf[1], len - 1);
831           break;
832
833         case 'm':
834           vt102_parse_attr_string (v, &buf[1], len - 1);
835           break;
836         case 'r':
837           v->top_margin = v->screen_start;
838           v->bottom_margin = v->screen_end;
839
840           if ((len > 2) && (ptr = index (arg, ';')))
841             {
842               ptr++;
843               v->top_margin.y = narg - 1;
844               v->bottom_margin.y = atoi (ptr) - 1;
845             }
846
847           if (v->top_margin.y < v->screen_start.y)
848             v->top_margin.y = v->screen_start.y;
849           if (v->top_margin.y > v->screen_end.y)
850             v->top_margin.y = v->screen_end.y;
851           if (v->bottom_margin.y < v->screen_start.y)
852             v->bottom_margin.y = v->screen_start.y;
853           if (v->bottom_margin.y > v->screen_end.y)
854             v->bottom_margin.y = v->screen_end.y;
855
856           vt102_cursor_home (v);
857           break;
858         case 's':
859           v->saved.pos = v->pos;
860           break;
861         case 'u':
862           v->pos = v->saved.pos;
863           vt102_cursor_normalize (v);
864           v->pending_wrap = 0;
865           break;
866
867         default:
868 #if 0
869           fprintf (stderr, "unhandled CSI  \\033%s\n", buf, buf[0]);
870 #endif
871           ;
872         }
873       break;
874     default:
875 #if 0
876       fprintf (stderr, "unhandled CSI  \\033%s\n", buf, buf[0]);
877 #endif
878       ;
879     }
880
881
882
883 }
884
885 void
886 vt102_status_line (VT102 * v, char *str)
887 {
888   int i = VT102_COLS;
889   CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
890
891   while (i--)
892     {
893       ca->attr = CRT_ATTR_REVERSE;
894       ca->chr = *str;
895       if (*str)
896         str++;
897       ca++;
898     }
899
900 }
901
902
903 void
904 vt102_parse_char (VT102 * v, int c)
905 {
906   VT102_parser *p = &v->parser;
907
908 #if 0
909   fprintf (stderr, "char %c pc %d %d %d   %d %d\n", (c < 32) ? 32 : c, c,
910            p->in_csi, p->in_escape, v->pos.x, v->pos.y);
911 #endif
912   if (p->in_csi)
913     {
914       p->csi_buf[p->csi_ptr++] = c;
915       if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
916         {
917           vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
918           p->in_csi = 0;
919         }
920     }
921   else if (p->in_escape)
922     {
923       if (csi_starter (c))
924         {
925           p->csi_ptr = 0;
926           p->csi_buf[p->csi_ptr++] = c;
927           p->in_csi++;
928           p->in_escape = 0;
929         }
930       else
931         {
932           p->in_escape = 0;
933           vt102_parse_esc (v, c);
934         }
935     }
936   else
937     {
938
939       switch (c)
940         {
941          /*NUL*/ case 0:
942          /*SOH*/ case 1:
943          /*STX*/ case 2:
944          /*ETX*/ case 3:
945          /*EOT*/ case 4:
946           break;
947          /*ENQ*/ case 5:
948           tty_write (v->tty, "vt102", 5);
949           break;
950          /*ACK*/ case 6:
951          /*BEL*/ case 7:
952           break;
953          /*BS*/ case 8:
954           vt102_cursor_retard (v);
955           break;
956          /*HT*/ case 9:
957           vt102_cursor_advance_tab (v);
958           break;
959          /*LF*/ case 10:
960          /*VT*/ case 11:
961          /*FF*/ case 12:
962           vt102_cursor_advance_line (v);
963           if (!v->modes[VT102_MODE_NEWLINE_MODE])
964             break;
965          /*CR*/ case 13:
966           vt102_cursor_carriage_return (v);
967           break;
968          /*SO*/ case 14:
969          /*SI*/ case 15:
970          /*DLE*/ case 16:
971         /*DC1 */ case 17:
972         /*DC2 */ case 18:
973         /*DC3 */ case 19:
974         /*DC4 */ case 20:
975          /*NAK*/ case 21:
976          /*SYN*/ case 22:
977          /*ETB*/ case 23:
978          /*CAN*/ case 24:
979          /*EM*/ case 25:
980          /*SUB*/ case 26:
981           break;
982          /*ESC*/ case 27:
983           p->in_escape++;
984           return;
985          /*FS*/ case 28:
986          /*GS*/ case 29:
987          /*RS*/ case 30:
988          /*US*/ case 31:
989          /*DEL*/ case 127:
990           break;
991         /*regular character */ default:
992           vt102_do_pending_wrap (v);
993
994           if (v->modes[VT102_MODE_INSERT])
995             vt102_insert_into_line (v, v->pos);
996
997           v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
998           v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
999           vt102_cursor_advance (v);
1000         }
1001     }
1002
1003   v->crt.pos = v->pos;
1004   v->crt.hide_cursor =
1005     v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1006
1007   if (v->current_line.y != v->pos.y)
1008     {
1009       vt102_log_line (v, v->current_line.y);
1010       v->current_line = v->pos;
1011     }
1012
1013   vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1014 }
1015
1016 vt102_parse(VT102 *v,char *buf,int len)
1017 {
1018 while (len--)
1019         vt102_parse_char(v,*(buf++));
1020 }
1021
1022
1023 void
1024 vt102_parser_reset (VT102_parser * p)
1025 {
1026   p->in_csi = 0;
1027   p->in_escape = 0;
1028   p->csi_ptr = 0;
1029 }
1030
1031
1032 void
1033 vt102_send (VT102 * v, uint8_t key)
1034 {
1035   uint8_t c;
1036   if ((key > 31) || (key < 127))
1037     {
1038       tty_write (v->t, &key, 1);
1039       return;
1040     }
1041
1042   switch (key)
1043     {
1044      /*NUL*/ case 0:
1045      /*SOH*/ case 1:
1046      /*STX*/ case 2:
1047      /*ETX*/ case 3:
1048      /*EOT*/ case 4:
1049      /*ENQ*/ case 5:
1050      /*ACK*/ case 6:
1051      /*BEL*/ case 7:
1052      /*BS*/ case 8:
1053      /*HT*/ case 9:
1054      /*LF*/ case 10:
1055      /*VT*/ case 11:
1056      /*FF*/ case 12:
1057       tty_write (v->t, &key, 1);
1058       break;
1059      /*CR*/ case 13:
1060       tty_write (v->t, &key, 1);
1061       if (v->modes[VT102_MODE_NEWLINE_MODE])
1062         {
1063           c = 10;
1064           tty_write (v->t, &c, 1);
1065         }
1066       break;
1067      /*SO*/ case 14:
1068      /*SI*/ case 15:
1069      /*DLE*/ case 16:
1070     /*DC1 */ case 17:
1071     /*DC2 */ case 18:
1072     /*DC3 */ case 19:
1073     /*DC4 */ case 20:
1074      /*NAK*/ case 21:
1075      /*SYN*/ case 22:
1076      /*ETB*/ case 23:
1077      /*CAN*/ case 24:
1078      /*EM*/ case 25:
1079      /*SUB*/ case 26:
1080       tty_write (v->t, &key, 1);
1081       break;
1082      /*ESC*/ case 27:
1083      /*FS*/ case 28:
1084      /*GS*/ case 29:
1085      /*RS*/ case 30:
1086      /*US*/ case 31:
1087      /*DEL*/ case 127:
1088       tty_write (v->t, &key, 1);
1089       break;
1090
1091     case KEY_UP:
1092     case KEY_DOWN:
1093     case KEY_RIGHT:
1094     case KEY_LEFT:
1095       if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1096         {
1097           uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1098           tty_write (v->t, &key, sizeof (buf));
1099         }
1100       else
1101         {
1102           uint8_t buf[] = { 033, '[', 'O' + (key - KEY_UP) };
1103           tty_write (v->t, &key, sizeof (buf));
1104         }
1105       break;
1106     case KEY_0:
1107     case KEY_1:
1108     case KEY_2:
1109     case KEY_3:
1110     case KEY_4:
1111     case KEY_5:
1112     case KEY_6:
1113     case KEY_7:
1114     case KEY_8:
1115     case KEY_9:
1116       if (v->application_keypad_mode)
1117         {
1118           uint8_t buf[] = { 033, 'O', 'p' + (key - KEY_0) };
1119           tty_write (v->t, &key, sizeof (buf));
1120         }
1121       else
1122         {
1123           c = '0' + (key - KEY_0);
1124           tty_write (v->t, &c, 1);
1125         }
1126       break;
1127     case KEY_MINUS:
1128       if (v->application_keypad_mode)
1129         {
1130           uint8_t buf[] = { 033, 'O', 'm' };
1131           tty_write (v->t, &key, sizeof (buf));
1132         }
1133       else
1134         {
1135           c = '-';
1136           tty_write (v->t, &c, 1);
1137         }
1138       break;
1139     case KEY_COMMA:
1140       if (v->application_keypad_mode)
1141         {
1142           uint8_t buf[] = { 033, 'O', 'l' };
1143           tty_write (v->t, &key, sizeof (buf));
1144         }
1145       else
1146         {
1147           c = ',';
1148           tty_write (v->t, &c, 1);
1149         }
1150       break;
1151     case KEY_PERIOD:
1152       if (v->application_keypad_mode)
1153         {
1154           uint8_t buf[] = { 033, 'O', 'n' };
1155           tty_write (v->t, &key, sizeof (buf));
1156         }
1157       else
1158         {
1159           c = '.';
1160           tty_write (v->t, &c, 1);
1161         }
1162       break;
1163     case KEY_ENTER:
1164       if (v->application_keypad_mode)
1165         {
1166           uint8_t buf[] = { 033, 'O', 'M' };
1167           tty_write (v->t, &key, sizeof (buf));
1168         }
1169       else
1170         {
1171           c = 13;
1172           tty_write (v->t, &c, 1);
1173           if (v->modes[VT102_MODE_NEWLINE_MODE])
1174             {
1175               c = 10;
1176               tty_write (v->t, &c, 1);
1177             }
1178         }
1179       break;
1180     case KEY_PF1:
1181     case KEY_PF2:
1182     case KEY_PF3:
1183     case KEY_PF4:
1184       {
1185         uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1186         tty_write (v->t, &key, sizeof (buf));
1187       }
1188       break;
1189
1190     }
1191
1192 }
1193
1194 void
1195 vt102_reset (VT102 * v)
1196 {
1197   VT102_parser *p = &v->parser;
1198
1199   vt102_parser_reset (p);
1200   crt_cls (&v->crt);
1201
1202
1203   v->application_keypad_mode=0;
1204
1205   v->current_line = v->pos;
1206   v->pending_wrap = 0;
1207
1208   v->screen_start.x = 0;
1209   v->screen_start.y = 0;
1210   v->screen_end.x = VT102_COLS - 1;
1211   v->screen_end.y = VT102_ROWS - 1;
1212
1213   v->top_margin = v->screen_start;
1214   v->bottom_margin = v->screen_end;
1215
1216   memset (v->modes, 0, VT102_NMODES);
1217   memset (v->private_modes, 0, VT102_NMODES);
1218
1219   v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1220   v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1221   v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1222   v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1223
1224   vt102_cursor_home (v);
1225   vt102_reset_tabs (v);
1226   v->current_line = v->pos;
1227
1228   vt102_save_state (v);
1229
1230   vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1231
1232 }
1233
1234 int vt102_dispatch(VT102 *v)
1235 {
1236 char buf[1024];
1237 int red;
1238
1239 red=tty_read(v->t,buf,sizeof(buf));
1240 if (red<0) return -1;
1241 if (!red) return 0;
1242
1243 vt102_parse(v,buf,red);
1244
1245 return 0;
1246 }
1247
1248 VT102 *vt102_new(TTY *t)
1249 {
1250 VT102 *v;
1251
1252 v=(VT102 *) malloc(sizeof(VT102));
1253
1254 vt102_reset(v);
1255
1256 v->tty=t;
1257
1258 return v;
1259 }
1260
1261 void vt102_free(VT102 *v)
1262 {
1263 free(v);
1264 }