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