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