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