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