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.39  2008/02/26 16:54:06  james
14  * *** empty log message ***
15  *
16  * Revision 1.38  2008/02/26 16:53:24  james
17  * *** empty log message ***
18  *
19  * Revision 1.37  2008/02/24 12:22:53  james
20  * *** empty log message ***
21  *
22  * Revision 1.36  2008/02/24 12:22:42  james
23  * *** empty log message ***
24  *
25  * Revision 1.35  2008/02/24 00:42:53  james
26  * *** empty log message ***
27  *
28  * Revision 1.34  2008/02/23 11:48:37  james
29  * *** empty log message ***
30  *
31  * Revision 1.33  2008/02/22 23:39:27  james
32  * *** empty log message ***
33  *
34  * Revision 1.32  2008/02/22 19:12:05  james
35  * *** empty log message ***
36  *
37  * Revision 1.31  2008/02/22 17:07:00  james
38  * *** empty log message ***
39  *
40  * Revision 1.30  2008/02/22 14:51:54  james
41  * *** empty log message ***
42  *
43  * Revision 1.29  2008/02/15 03:32:07  james
44  * *** empty log message ***
45  *
46  * Revision 1.28  2008/02/14 10:34:30  james
47  * *** empty log message ***
48  *
49  * Revision 1.27  2008/02/14 02:46:45  james
50  * *** empty log message ***
51  *
52  * Revision 1.26  2008/02/14 01:55:57  james
53  * *** empty log message ***
54  *
55  * Revision 1.25  2008/02/13 16:57:29  james
56  * *** empty log message ***
57  *
58  * Revision 1.24  2008/02/13 09:12:21  james
59  * *** empty log message ***
60  *
61  * Revision 1.23  2008/02/07 13:26:35  james
62  * *** empty log message ***
63  *
64  * Revision 1.22  2008/02/07 13:22:51  james
65  * *** empty log message ***
66  *
67  * Revision 1.21  2008/02/07 12:21:16  james
68  * *** empty log message ***
69  *
70  * Revision 1.20  2008/02/07 12:16:04  james
71  * *** empty log message ***
72  *
73  * Revision 1.19  2008/02/07 11:27:02  james
74  * *** empty log message ***
75  *
76  * Revision 1.18  2008/02/07 01:59:25  james
77  * *** empty log message ***
78  *
79  * Revision 1.17  2008/02/07 01:58:28  james
80  * *** empty log message ***
81  *
82  * Revision 1.16  2008/02/07 01:57:46  james
83  * *** empty log message ***
84  *
85  * Revision 1.15  2008/02/07 00:43:27  james
86  * *** empty log message ***
87  *
88  * Revision 1.14  2008/02/07 00:40:23  james
89  * *** empty log message ***
90  *
91  * Revision 1.13  2008/02/07 00:39:59  james
92  * *** empty log message ***
93  *
94  * Revision 1.12  2008/02/07 00:39:13  james
95  * *** empty log message ***
96  *
97  * Revision 1.11  2008/02/06 20:26:58  james
98  * *** empty log message ***
99  *
100  * Revision 1.10  2008/02/06 17:53:28  james
101  * *** empty log message ***
102  *
103  * Revision 1.9  2008/02/06 15:53:22  james
104  * *** empty log message ***
105  *
106  * Revision 1.8  2008/02/06 11:49:47  james
107  * *** empty log message ***
108  *
109  * Revision 1.7  2008/02/06 11:30:37  james
110  * *** empty log message ***
111  *
112  * Revision 1.6  2008/02/05 01:11:46  james
113  * *** empty log message ***
114  *
115  * Revision 1.5  2008/02/04 20:23:55  james
116  * *** empty log message ***
117  *
118  * Revision 1.4  2008/02/04 05:45:55  james
119  * ::
120  *
121  * Revision 1.3  2008/02/04 02:05:06  james
122  * *** empty log message ***
123  *
124  * Revision 1.2  2008/02/04 01:32:39  james
125  * *** empty log message ***
126  *
127  * Revision 1.1  2008/02/03 23:36:41  james
128  * *** empty log message ***
129  *
130  */
131
132
133 /* Termcap he say:
134
135 VT102:
136
137 vt102|dec vt102:\
138         :mi:\
139         :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
140
141 vt100|vt100-am|dec vt100 (w/advanced video):\
142         :am:bs:ms:xn:xo:\
143         :co#80:it#8:li#24:vt#3:\
144         :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
145         :UP=\E[%dA:\
146         :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
147         :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
148         :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
149         :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
150         :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
151         :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
152         :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
153         :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
154         :us=\E[4m:tc=vt100+fnkeys:
155
156 vt100+fnkeys|dec vt100 numeric keypad:\
157         :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
158         :tc=vt100+pfkeys:
159
160 vt100+pfkeys|dec vt100 numeric keypad:\
161         :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
162
163 vt100+keypad|dec vt100 numeric keypad no fkeys:\
164         :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
165
166 */
167
168 /*
169 so the parser needs to be able to at least do
170 CTRL-G
171 CTRL-H
172 CTRL-I
173 CTRL-J
174 CTRL-M
175 CTRL-N
176
177 CTRL-O
178 ESC7
179 ESC8
180 ESCH
181 ESCM
182 ESC> 
183
184 ESC[%dA
185 ESC[%dB
186 ESC[%dC
187 ESC[%dD
188 ESC[H
189 ESC[%d;%dH
190 ESC[J
191 ESC[K
192 ESC[1K
193 ESC[L
194 ESC[M
195 ESC[P
196
197 ESC[3g
198 ESC[4h
199 ESC[4l
200 ESC[m 
201 ESC[1m
202 ESC[4m
203 ESC[5m
204 ESC[7m
205 ESC[%d;%dr
206
207
208 ESC[?3l 
209 ESC[?4l 
210 ESC[?5l
211 ESC[?7h
212 ESC[?7h
213 ESC[?7l
214 ESC[?8h
215
216 ESC(B
217 ESC)0
218
219
220 TODO:
221
222 ESC(B
223 ESC)0
224
225 CTRL-O
226
227 ANSI:
228
229
230
231 ansi|ansi/pc-term compatible with color:\
232         :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
233         :tc=klone+sgr:tc=ansi-m:
234 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
235         :5i:\
236         :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
237         :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
238         :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
239         :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
240         :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
241         :s3=\E+B:ta=\E[I:tc=pcansi-m:
242 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
243         :am:bs:mi:ms:\
244         :co#80:it#8:li#24:\
245         :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
246         :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
247         :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
248         :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
249         :tc=klone+sgr-dumb:
250 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
251         :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
252         :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
253 klone+acs|alternate character set for ansi.sys displays:\
254         :ac=+\020,\021-\030.^Y0\333`\004a\261f\370g\361h\260j\331k\277l\332m\300n\305o~p\304q\304r\304s_t\303u\264v\301w\302x\263y\363z\362{\343|\330}\234~\376:\
255         :ae=\E[10m:as=\E[11m:
256 ecma+color|color control for ECMA-48-compatible terminals:\
257         :Co#8:NC#3:pa#64:\
258         :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
259
260
261 Ignoreing ones in the VT102 spec
262
263 ESC[%d@
264 ESC[I
265 ESC[....R
266 ESC[%dS
267 ESC[%dT
268 ESC[X
269 ESC[Z
270
271 ESC[c
272 *ESC[%db
273 ESC[....d
274 ESC[....f
275 ESC[2g
276 ESC[4i
277 ESC[5i
278 ESC[6n
279
280 ESC(B
281 ESC)B
282 ESC*B
283 ESC+B
284
285
286 */
287
288 #include "project.h"
289 #include "syslog.h"
290
291 //static char terminal_reply[]={033,0133,077,066,0143};
292 static char terminal_reply[] = { 'v', 't', '1', '0', '2' };
293
294 #define TABLE_LENGTH 128
295
296 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
297 int vt102_cmd_length[TABLE_LENGTH] = {
298   ['('] = 1,
299   [')'] = 1,
300   ['+'] = 1,
301   ['*'] = 1,
302   ['%'] = 1,
303   ['#'] = 1,
304   ['Y'] = 2,
305 };
306
307 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
308 #define CSI_ENDER -1
309 int vt102_cmd_termination[TABLE_LENGTH] = {
310   [']'] = 7,
311   ['['] = CSI_ENDER,
312 };
313
314
315
316
317
318 static inline int
319 safe_ch (int c)
320 {
321   if (c < 32)
322     return ' ';
323   if (c > 126)
324     return ' ';
325   return c;
326 }
327
328 static inline int
329 csi_ender (int c)
330 {
331   if ((c >= 'a') && (c <= 'z'))
332     return 1;
333   if ((c >= 'A') && (c <= 'Z'))
334     return 1;
335   if ((c == '@'))
336     return 1;
337   return 0;
338 }
339
340
341
342 /*Stay or enter command acquision and exit after n more chars*/
343 void
344 redispatch_with_more (VT102_parser * p, int n)
345 {
346 }
347
348 static inline int
349 in_margins (VT102 * v, CRT_Pos p)
350 {
351   if (v->pos.x < v->top_margin.x)
352     return 0;
353   if (v->pos.y < v->top_margin.y)
354     return 0;
355
356   if (v->pos.x > v->bottom_margin.x)
357     return 0;
358   if (v->pos.y > v->bottom_margin.y)
359     return 0;
360
361   return 1;
362 }
363
364 void
365 vt102_log_line (Context * c, int line)
366 {
367   CRT_Pos e = { VT102_COLS - 1, line };
368   CRT_Pos p = { 0, line };
369   char logbuf[VT102_COLS + 1];
370
371   if (!c->l)
372     return;
373
374
375   for (; e.x > 0; --e.x)
376     {
377       if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
378         break;
379     }
380
381   for (; p.x <= e.x; ++p.x)
382     {
383       int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
384       if (ch < 32)
385         ch = ' ';
386       if (ch > 126)
387         ch = ' ';
388       logbuf[p.x] = ch;
389     }
390   logbuf[p.x] = 0;
391
392   c->l->log (c->l, logbuf);
393 }
394
395 /*Called for every upward scroll with same args*/
396 void
397 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
398 {
399 /*Only log if it scrolls off the top*/
400   if (t.y)
401     return;
402
403   t.x = 0;
404   history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
405 }
406
407 void
408 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
409 {
410   if (v->pos.x < tl.x)
411     v->pos.x = tl.x;
412   if (v->pos.y < tl.y)
413     v->pos.y = tl.y;
414
415   if (v->pos.x > br.x)
416     v->pos.x = br.x;
417   if (v->pos.y > br.y)
418     v->pos.y = br.y;
419 }
420
421
422 void
423 vt102_cursor_normalize (VT102 * v)
424 {
425   CRT_Pos *top, *bottom;
426
427   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
428     {
429       vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
430     }
431   else
432     {
433       vt102_clip_cursor (v, v->screen_start, v->screen_end);
434     }
435 }
436
437
438 void
439 vt102_cursor_carriage_return (VT102 * v)
440 {
441    /*FISH*/ v->pos.x = v->top_margin.x;
442   v->pending_wrap = 0;
443 }
444
445 void
446 vt102_cursor_advance_line (Context * c)
447 {
448   VT102 *v = c->v;
449   int couldscroll = in_margins (v, v->pos);
450
451 /*have wraped off end of last line in scrolling region */
452 /* (|| not necessary, but shuts compiler up */
453   if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
454       (couldscroll))
455     {
456       vt102_log_line (c, v->pos.y);
457
458       vt102_history (c, v->top_margin, v->bottom_margin);
459
460       crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
461       return;
462     }
463
464   if (v->pos.y != v->screen_end.y)
465     v->pos.y++;
466   v->pending_wrap = 0;
467 }
468
469
470 void
471 vt102_cursor_retreat_line (Context * c)
472 {
473   VT102 *v = c->v;
474   int couldscroll = in_margins (v, v->pos);
475
476 /*have wraped off end of last line in scrolling region */
477 /* (|| not necessary, but shuts compiler up */
478   if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
479       (couldscroll))
480     {
481       vt102_log_line (c, v->pos.y);
482
483       crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
484       return;
485     }
486
487   if (v->pos.y != v->screen_start.y)
488     v->pos.y--;
489   v->pending_wrap = 0;
490 }
491
492
493 void
494 vt102_do_pending_wrap (Context * c)
495 {
496   VT102 *v = c->v;
497   int couldscroll = in_margins (v, v->pos);
498   int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
499
500   if (!v->pending_wrap)
501     return;
502
503 #if 0
504   fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
505            v->pos.y, autowrap, couldscroll);
506 #endif
507
508 /*End of line but no autowrap, nothing to do*/
509   if (!autowrap)
510     return;
511
512 /*End of screen and not allowed to scroll, nothing to do*/
513   if ((v->pos.y == v->screen_end.y) && (!couldscroll))
514     return;
515
516   if (couldscroll)
517     {
518       v->pos.x = v->top_margin.x;
519     }
520   else
521     {
522       v->pos.x = 0;
523     }
524
525   vt102_cursor_advance_line (c);
526 }
527
528 void
529 vt102_cursor_advance (Context * c)
530 {
531   VT102 *v = c->v;
532
533   if (v->pos.x < v->bottom_margin.x)
534     {
535 /*Normal advance*/
536       v->pos.x++;
537       v->pending_wrap = 0;
538       return;
539     }
540   v->pending_wrap++;
541   if (!c->v->xn_glitch)
542     vt102_do_pending_wrap (c);
543 }
544
545
546
547 void
548 vt102_cursor_retreat (VT102 * v)
549 {
550   if (v->pos.x != v->top_margin.x)
551     v->pos.x--;
552
553   v->pending_wrap = 0;
554 }
555
556 void
557 vt102_reset_tabs (VT102 * v)
558 {
559   int i;
560
561   memset (v->tabs, 0, sizeof (v->tabs));
562
563   for (i = 0; i < VT102_COLS; i += 8)
564     {
565       v->tabs[i]++;
566     }
567 }
568 void
569 vt102_cursor_advance_tab (VT102 * v)
570 {
571   if (v->pos.x == v->bottom_margin.x)
572     return;
573   while (v->pos.x < v->bottom_margin.x)
574     {
575       v->pos.x++;
576       if (v->tabs[v->pos.x])
577         break;
578     }
579   v->pending_wrap = 0;
580 }
581
582 void
583 vt102_cursor_retreat_tab (VT102 * v)
584 {
585   if (v->pos.x == v->top_margin.x)
586     return;
587   while (v->pos.x > v->top_margin.x)
588     {
589       v->pos.x--;
590       if (v->tabs[v->pos.x])
591         break;
592     }
593   v->pending_wrap = 0;
594 }
595
596 vt102_cursor_home (VT102 * v)
597 {
598   v->pos = v->top_margin;
599   vt102_cursor_normalize (v);
600   v->pending_wrap = 0;
601
602 }
603
604 vt102_cursor_absolute (VT102 * v, int x, int y)
605 {
606   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
607     {
608       v->pos.x = x + v->top_margin.x;
609       v->pos.y = y + v->top_margin.x;
610     }
611   else
612     {
613       v->pos.x = x;
614       v->pos.y = y;
615     }
616   vt102_cursor_normalize (v);
617   v->pending_wrap = 0;
618 }
619
620 vt102_cursor_relative (VT102 * v, int x, int y)
621 {
622   v->pos.x += x;
623   v->pos.y += y;
624   vt102_cursor_normalize (v);
625   v->pending_wrap = 0;
626 }
627
628
629
630 void
631 vt102_delete_from_line (VT102 * v, CRT_Pos p)
632 {
633   int n = v->bottom_margin.x - p.x;
634
635   if (n < 0)
636     return;
637
638   if (n)
639     {
640
641       memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
642                &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
643     }
644
645   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
646 /*But not attr due to vt102 bug*/
647 }
648
649 void
650 vt102_insert_into_line (VT102 * v, CRT_Pos p)
651 {
652   int n = v->bottom_margin.x - p.x;
653
654   if (n < 0)
655     return;
656
657   if (n)
658     {
659
660       memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
661                &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
662     }
663
664   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
665   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
666   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
667 }
668
669
670
671 void
672 vt102_change_mode (VT102 * v, int private, char *ns, int set)
673 {
674   int m;
675
676
677   if (*ns)
678     {
679       m = atoi (ns);
680     }
681   else
682     {
683       m = 1;
684     }
685
686   if (m < 0)
687     return;
688   if (m >= VT102_NMODES)
689     return;
690
691   if (private)
692     {
693       v->private_modes[m] = set;
694       switch (m)
695         {
696         case VT102_PRIVATE_MODE_CURSOR_MODE:
697           if (v->application_keypad_mode)
698             v->private_modes[m] = 0;
699 #if 0
700           fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
701                    v->private_modes[m], set);
702 #endif
703           break;
704         case VT102_PRIVATE_MODE_ORIGIN_MODE:
705           vt102_cursor_home (v);
706           break;
707         }
708
709     }
710   else
711     v->modes[m] = set;
712
713 #if 0
714   fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
715 #endif
716 }
717
718 void
719 vt102_parse_mode_string (VT102 * v, char *buf, int len)
720 {
721   int private = 0;
722   char last = buf[len - 1];
723   char num[4];
724   int o;
725
726   memset (num, 0, sizeof (num));
727   o = sizeof (num) - 1;
728
729   len--;
730
731   if (*buf == '?')
732     {
733       private++;
734       buf++;
735       len--;
736     }
737
738   if (len < 0)
739     return;
740
741   while (len--)
742     {
743       if (*buf == ';')
744         {
745           vt102_change_mode (v, private, &num[o], last == 'h');
746           memset (num, 0, sizeof (num));
747           o = sizeof (num) - 1;
748           buf++;
749           continue;
750         }
751
752       num[0] = num[1];
753       num[1] = num[2];
754       num[2] = *buf;
755
756       if (o)
757         o--;
758
759       buf++;
760     }
761
762   vt102_change_mode (v, private, &num[o], last == 'h');
763
764 }
765
766
767 void
768 vt102_change_attr (VT102 * v, char *na)
769 {
770   int a;
771
772
773   if (*na)
774     {
775       a = atoi (na);
776     }
777   else
778     {
779       a = 0;
780     }
781
782   switch (a)
783     {
784     case 0:
785       v->attr = CRT_ATTR_NORMAL;
786       v->color = CRT_COLOR_NORMAL;
787       break;
788     case 1:
789       v->attr |= CRT_ATTR_BOLD;
790       break;
791     case 21:
792     case 22:
793       v->attr &= ~CRT_ATTR_BOLD;
794       break;
795     case 4:
796       v->attr |= CRT_ATTR_UNDERLINE;
797       break;
798     case 24:
799       v->attr &= ~CRT_ATTR_UNDERLINE;
800       break;
801     case 5:
802       v->attr |= CRT_ATTR_BLINK;
803       break;
804     case 25:
805       v->attr &= ~CRT_ATTR_BLINK;
806       break;
807     case 7:
808       v->attr |= CRT_ATTR_REVERSE;
809       break;
810     case 27:
811       v->attr &= ~CRT_ATTR_REVERSE;
812       break;
813     case 30:
814     case 31:
815     case 32:
816     case 33:
817     case 34:
818     case 35:
819     case 36:
820     case 37:
821       v->color &= ~CRT_COLOR_FG_MASK;
822       v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
823       break;
824     case 90:
825     case 91:
826     case 92:
827     case 93:
828     case 94:
829     case 95:
830     case 96:
831     case 97:
832       v->color &= ~CRT_COLOR_FG_MASK;
833       v->color |=
834         (((a -
835            90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
836         CRT_COLOR_FG_MASK;
837       break;
838     case 39:
839     case 99:
840       v->color &= ~CRT_COLOR_FG_MASK;
841       v->color |=
842         (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
843       break;
844     case 40:
845     case 41:
846     case 42:
847     case 43:
848     case 44:
849     case 45:
850     case 46:
851     case 47:
852       v->color &= ~CRT_COLOR_BG_MASK;
853       v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
854       break;
855     case 100:
856     case 101:
857     case 102:
858     case 103:
859     case 104:
860     case 105:
861     case 106:
862     case 107:
863       v->color &= ~CRT_COLOR_BG_MASK;
864       v->color |=
865         (((a -
866            100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
867         CRT_COLOR_BG_MASK;
868       break;
869     case 49:
870     case 109:
871       v->color &= ~CRT_COLOR_BG_MASK;
872       v->color |=
873         (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
874       break;
875
876     default:
877       ;
878 #if 0
879       fprintf (stderr, "unhandled SGR %d\n", a);
880 #endif
881     }
882
883 }
884
885
886 void
887 vt102_parse_attr_string (VT102 * v, char *buf, int len)
888 {
889   int private = 0;
890   char last = buf[len - 1];
891   char num[4];
892   int o;
893
894   memset (num, 0, sizeof (num));
895   o = sizeof (num) - 1;
896
897   len--;
898
899   if (len < 0)
900     return;
901
902   while (len--)
903     {
904       if (*buf == ';')
905         {
906           vt102_change_attr (v, &num[o]);
907           memset (num, 0, sizeof (num));
908           o = sizeof (num) - 1;
909           buf++;
910           continue;
911         }
912
913       num[0] = num[1];
914       num[1] = num[2];
915       num[2] = *buf;
916
917       if (o)
918         o--;
919
920       buf++;
921     }
922   vt102_change_attr (v, &num[o]);
923 }
924
925 void
926 vt102_save_state (VT102 * v)
927 {
928   v->saved.pos = v->pos;
929   v->saved.attr = v->attr;
930   v->saved.color = v->color;
931   v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
932 }
933
934 void
935 vt102_restore_state (VT102 * v)
936 {
937   v->pos = v->saved.pos;
938   v->attr = v->saved.attr;
939   v->color = v->saved.color;
940   v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
941   vt102_cursor_normalize (v);
942   v->pending_wrap = 0;
943 }
944
945 void
946 vt102_regular_char (Context * c, VT102 * v, int ch)
947 {
948
949   vt102_do_pending_wrap (c);
950
951   if (v->modes[VT102_MODE_INSERT])
952     vt102_insert_into_line (v, v->pos);
953
954   v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
955   v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
956   v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
957   vt102_cursor_advance (c);
958
959   v->last_reg_char = ch;
960 }
961
962 void
963 vt102_scs (Context * c, int g, int s)
964 {
965 /*Ignoring charsets*/
966 }
967
968 void
969 vt102_parse_esc (Context * c)
970 {
971   VT102 *v = c->v;
972   VT102_parser *p = &v->parser;
973
974 /*If you edit this switch don't forget to edit the length and termination tables*/
975   switch (p->cmd_buf[0])
976     {
977     case 'A':
978       vt102_cursor_relative (v, 0, -1);
979       break;
980     case 'B':
981       vt102_cursor_relative (v, 0, 1);
982       break;
983     case 'C':
984       vt102_cursor_relative (v, 1, 0);
985       break;
986     case 'D':
987       vt102_cursor_relative (v, -1, 0);
988       break;
989     case 'E':
990       if (v->pos.y == v->bottom_margin.y)
991         {
992           vt102_log_line (c, v->pos.y);
993           vt102_history (c, v->top_margin, v->bottom_margin);
994           crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
995                          v->color);
996         }
997       else
998         {
999           vt102_cursor_relative (v, 0, 1);
1000         }
1001       break;
1002     case 'H':
1003       v->tabs[v->pos.x]++;
1004       break;
1005     case 'I':
1006       vt102_cursor_retreat_line (c);
1007       break;
1008     case 'J':
1009       crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1010       break;
1011     case 'K':
1012       {
1013         CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1014         crt_erase (&v->crt, v->pos, le, 1, v->color);
1015       }
1016       break;
1017     case 'M':
1018       if (v->pos.y == v->top_margin.y)
1019         {
1020           crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1021                            v->color);
1022         }
1023       else
1024         {
1025           vt102_cursor_relative (v, 0, -1);
1026         }
1027       break;
1028
1029     case 'Y':
1030       vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1031       break;
1032     case 'Z':
1033       if (c->t)
1034         {
1035           c->t->xmit (c->t, terminal_reply, sizeof (terminal_reply));
1036         }
1037       break;
1038     case '#':
1039         switch (p->cmd_buf[1])
1040           {
1041           case '8':
1042             /*DECALN*/
1043             {
1044               int i;
1045               crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1046                          CRT_NORMAL_COLOR);
1047               for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1048                 v->crt.screen[i].chr = 'E';
1049             }
1050           default:
1051
1052             log_f (c->l,
1053                    "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1054                    __FILE__, __LINE__, p->cmd_buf[1],
1055                    safe_ch (p->cmd_buf[1]));
1056
1057           }
1058         break;
1059     case '<':
1060         /*Set ansi mode - ignored */
1061         break;
1062
1063     case '7':
1064         vt102_save_state (v);
1065         break;
1066     case '8':
1067         vt102_restore_state (v);
1068         break;
1069     case '=':
1070         v->application_keypad_mode = 1;
1071         break;
1072     case '>':
1073         v->application_keypad_mode = 0;
1074         break;
1075     case ']':                  /*Set various titles */
1076         //FIXME:
1077         break;
1078     case '[':
1079         vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1080         break;
1081     case '(':                  /*Charsets */
1082     case ')':
1083     case '+':
1084     case '*':
1085     case '%':
1086         vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1087         break;
1088     default:
1089         log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1090                __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1091
1092         ;
1093       }
1094     }
1095   void vt102_parse_csi (Context * c, char *buf, int len)
1096   {
1097     char last;
1098     char *ptr;
1099     char *arg = buf + 1;
1100     int narg;
1101
1102     VT102 *v = c->v;
1103
1104     buf[len] = 0;
1105
1106     last = buf[len - 1];
1107 #if 0
1108     buf[len - 1] = 0;
1109 #endif
1110
1111     if (len > 2)
1112       {
1113         narg = atoi (arg);
1114       }
1115     else
1116       {
1117         narg = 1;
1118       }
1119
1120     switch (buf[0])
1121       {
1122       case '[':
1123         switch (last)
1124           {
1125           case '@':
1126             while (narg--)
1127               vt102_insert_into_line (v, v->pos);
1128             break;
1129           case 'A':
1130             vt102_cursor_relative (v, 0, -narg);
1131             break;
1132           case 'e':
1133           case 'B':
1134             vt102_cursor_relative (v, 0, narg);
1135             break;
1136           case 'a':
1137           case 'C':
1138             vt102_cursor_relative (v, narg, 0);
1139             break;
1140           case 'D':
1141             vt102_cursor_relative (v, -narg, 0);
1142             break;
1143           case 'E':
1144             vt102_cursor_relative (v, 0, narg);
1145             vt102_cursor_carriage_return (v);
1146             break;
1147           case 'F':
1148             vt102_cursor_relative (v, 0, -narg);
1149             vt102_cursor_carriage_return (v);
1150             break;
1151           case 'G':
1152             vt102_cursor_absolute (v, narg - 1, v->pos.y);
1153             break;
1154           case 'H':
1155           case 'f':
1156             {
1157               int x, y;
1158
1159               y = narg - 1;
1160
1161               ptr = index (arg, ';');
1162               if (ptr)
1163                 x = atoi (ptr + 1) - 1;
1164               else
1165                 x = 0;
1166
1167               vt102_cursor_absolute (v, x, y);
1168             }
1169             break;
1170           case 'I':
1171             while (narg--)
1172               vt102_cursor_advance_tab (c->v);
1173             break;
1174           case 'J':
1175             if (len == 2)
1176               narg = 0;         /*Different default */
1177             switch (narg)
1178               {
1179               case 0:
1180                 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1181                 break;
1182               case 1:
1183                 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1184                 break;
1185               case 2:
1186                 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1187                            v->color);
1188                 break;
1189               }
1190             break;
1191           case 'K':
1192             {
1193               CRT_Pos ls = { 0, v->pos.y };
1194               CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1195               if (len == 2)
1196                 narg = 0;       /*Different default */
1197
1198               switch (narg)
1199                 {
1200                 case 0:
1201                   crt_erase (&v->crt, v->pos, le, 1, v->color);
1202                   break;
1203                 case 1:
1204                   crt_erase (&v->crt, ls, v->pos, 1, v->color);
1205                   break;
1206                 case 2:
1207                   crt_erase (&v->crt, ls, le, 1, v->color);
1208                   break;
1209                 }
1210             }
1211             break;
1212
1213           case 'L':
1214             if ((v->pos.y >= v->top_margin.y)
1215                 && (v->pos.y <= v->bottom_margin.y))
1216               {
1217                 while (narg--)
1218                   crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1219                                    v->color);
1220               }
1221             break;
1222
1223           case 'M':
1224             if ((v->pos.y >= v->top_margin.y)
1225                 && (v->pos.y <= v->bottom_margin.y))
1226               {
1227                 while (narg--)
1228                   {
1229                     vt102_history (c, v->pos, v->bottom_margin);
1230                     crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1231                                    v->color);
1232                   }
1233               }
1234             break;
1235           case 'P':
1236             while (narg--)
1237               vt102_delete_from_line (v, v->pos);
1238             break;
1239           case 'R':
1240             //FIXME: cursor position report - does anything use that?
1241             break;
1242           case 'S':
1243             while (narg--)
1244               {
1245                 vt102_history (c, v->top_margin, v->bottom_margin);
1246                 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1247                                v->color);
1248               }
1249             break;
1250           case 'T':
1251             while (narg--)
1252               crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1253                                v->color);
1254             break;
1255           case 'X':
1256             {
1257               CRT_Pos end = v->pos;
1258               if (!narg)
1259                 narg++;
1260
1261               end.x += narg - 1;
1262               if (end.x > v->bottom_margin.x)
1263                 end.x = v->bottom_margin.x;
1264
1265               crt_erase (&v->crt, v->pos, end, 1, v->color);
1266             }
1267             break;
1268           case 'Z':
1269             while (narg--)
1270               vt102_cursor_retreat_tab (c->v);
1271             break;
1272           case '`':
1273             vt102_cursor_absolute (v, narg - 1, v->pos.y);
1274             break;
1275           case 'b':
1276             while (narg--)
1277               vt102_regular_char (c, v, v->last_reg_char);
1278             break;
1279           case 'c':
1280             //FIXME:
1281             break;
1282           case 'd':
1283             vt102_cursor_absolute (v, v->pos.x, narg - 1);
1284             break;
1285           case 'g':
1286             if (len == 2)
1287               narg = 0;         /*Different default */
1288
1289             switch (narg)
1290               {
1291               case 0:
1292                 v->tabs[v->pos.x] = 0;
1293                 break;
1294               case 2:          //FIXME: - LA120 says current line only WTF?
1295               case 3:
1296                 memset (v->tabs, 0, sizeof (v->tabs));
1297                 break;
1298               }
1299             break;
1300
1301           case 'i':            //4,5 Turn printer on and off WTF?
1302             //FIXME
1303             break;
1304           case 'h':
1305           case 'l':
1306             vt102_parse_mode_string (v, &buf[1], len - 1);
1307             break;
1308
1309
1310           case 'm':
1311             vt102_parse_attr_string (v, &buf[1], len - 1);
1312             break;
1313           case 'n':            // 6 request cursor position
1314             //FIXME 
1315             break;
1316           case 'r':
1317             v->top_margin = v->screen_start;
1318             v->bottom_margin = v->screen_end;
1319
1320             if ((len > 2) && (ptr = index (arg, ';')))
1321               {
1322                 ptr++;
1323                 v->top_margin.y = narg - 1;
1324                 v->bottom_margin.y = atoi (ptr) - 1;
1325               }
1326
1327             if (v->top_margin.y < v->screen_start.y)
1328               v->top_margin.y = v->screen_start.y;
1329             if (v->top_margin.y > v->screen_end.y)
1330               v->top_margin.y = v->screen_end.y;
1331             if (v->bottom_margin.y < v->screen_start.y)
1332               v->bottom_margin.y = v->screen_start.y;
1333             if (v->bottom_margin.y > v->screen_end.y)
1334               v->bottom_margin.y = v->screen_end.y;
1335
1336             vt102_cursor_home (v);
1337             break;
1338           case 's':
1339             v->saved.pos = v->pos;
1340             break;
1341           case 'u':
1342             v->pos = v->saved.pos;
1343             vt102_cursor_normalize (v);
1344             v->pending_wrap = 0;
1345             break;
1346
1347           default:
1348             log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1349                    buf);
1350
1351             ;
1352           }
1353         break;
1354       default:
1355         log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1356                __LINE__, buf);
1357         ;
1358       }
1359
1360
1361
1362   }
1363
1364   void vt102_status_line (VT102 * v, char *str)
1365   {
1366     int i = VT102_COLS;
1367     CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1368
1369     while (i--)
1370       {
1371         ca->attr = CRT_ATTR_REVERSE;
1372         ca->color = CRT_COLOR_NORMAL;
1373         ca->chr = *str;
1374         if (*str)
1375           str++;
1376         ca++;
1377       }
1378
1379   }
1380
1381
1382   void vt102_parser_reset (VT102_parser * p)
1383   {
1384     p->in_cmd = 0;
1385     p->cmd_more_bytes = 0;
1386     p->cmd_termination = 0;
1387     p->in_escape = 0;
1388   }
1389
1390
1391   void vt102_reset_state (VT102 * v)
1392   {
1393     vt102_parser_reset (&v->parser);
1394
1395     v->attr = CRT_ATTR_NORMAL;
1396     v->color = CRT_COLOR_NORMAL;
1397
1398     v->application_keypad_mode = 0;
1399
1400     v->top_margin = v->screen_start;
1401     v->bottom_margin = v->screen_end;
1402
1403     memset (v->modes, 0, VT102_NMODES);
1404     memset (v->private_modes, 0, VT102_NMODES);
1405
1406     v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1407     v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1408     v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1409     v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1410
1411     vt102_reset_tabs (v);
1412   }
1413
1414   static void pre_parse_cmd (int ch, VT102_parser * p)
1415   {
1416     if (ch > TABLE_LENGTH)
1417       return;
1418     if (ch < 0)
1419       return;
1420
1421     p->cmd_more_bytes = 0;
1422     p->in_cmd = 0;
1423
1424     p->cmd_termination = vt102_cmd_termination[ch];
1425     if (p->cmd_termination)
1426       {
1427         p->in_cmd++;
1428         return;
1429       }
1430
1431     p->cmd_more_bytes = vt102_cmd_length[ch];
1432
1433     if (p->cmd_more_bytes)
1434       {
1435         p->in_cmd++;
1436         return;
1437       }
1438   }
1439
1440   void vt102_parse_char (Context * c, int ch)
1441   {
1442     VT102 *v = c->v;
1443     VT102_parser *p = &v->parser;
1444
1445 #if 1
1446     log_f (c->l, "char %3d %c   ie=%d ic=%d cmb=%d ct=%d    %2d %2d %d", ch,
1447            safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1448            p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1449 #endif
1450
1451 /* Turn anything non-ascii into '?' */
1452 #if 0
1453     if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1454       {
1455         ch = '?';
1456       }
1457 #endif
1458
1459
1460 #if 0
1461     if (p->dca_ptr == 2)
1462       {
1463         p->in_dca = 0;
1464       }
1465 #endif
1466     if (ch == SYM_CHAR_RESET)
1467       {
1468         vt102_reset_state (v);
1469       }
1470     else if (p->in_cmd)
1471       {
1472         p->cmd_buf[p->cmd_ptr++] = ch;
1473         if (p->cmd_more_bytes)
1474           p->cmd_more_bytes--;
1475
1476
1477         if (p->cmd_more_bytes == 1)
1478           p->in_cmd = 0;
1479
1480         switch (p->cmd_termination)
1481           {
1482           case 0:
1483             break;
1484           default:
1485             if (p->cmd_termination == ch)
1486               p->in_cmd = 0;
1487             break;
1488           case CSI_ENDER:
1489             if (csi_ender (ch))
1490               p->in_cmd = 0;
1491             break;
1492           }
1493
1494         if (!p->in_cmd)
1495           {
1496             vt102_parse_esc (c);
1497             p->cmd_more_bytes = 0;
1498           }
1499       }
1500     else if (p->in_escape)
1501       {
1502
1503         p->cmd_ptr = 0;
1504         p->cmd_buf[p->cmd_ptr++] = ch;
1505         p->in_escape = 0;
1506
1507         pre_parse_cmd (ch, p);
1508
1509         if (!p->in_cmd)
1510           vt102_parse_esc (c);
1511       }
1512     else if (ch == 0x9b)        /*One byte CSI */
1513       {
1514         p->cmd_ptr = 0;
1515         p->cmd_buf[p->cmd_ptr++] = '[';
1516
1517         pre_parse_cmd (ch, p);
1518       }
1519     else
1520       {
1521
1522         switch (ch)
1523           {
1524            /*NUL*/ case 0:
1525            /*SOH*/ case 1:
1526            /*STX*/ case 2:
1527            /*ETX*/ case 3:
1528            /*EOT*/ case 4:
1529             break;
1530            /*ENQ*/ case 5:
1531             if (c->t)
1532               {
1533                 c->t->xmit (c->t, "vt102", 5);
1534               }
1535             break;
1536            /*ACK*/ case 6:
1537            /*BEL*/ case 7:
1538             break;
1539            /*BS*/ case 8:
1540             vt102_cursor_retreat (c->v);
1541             break;
1542            /*HT*/ case 9:
1543             vt102_cursor_advance_tab (c->v);
1544             break;
1545            /*LF*/ case 10:
1546            /*VT*/ case 11:
1547            /*FF*/ case 12:
1548             vt102_cursor_advance_line (c);
1549             if (!v->modes[VT102_MODE_NEWLINE_MODE])
1550               break;
1551            /*CR*/ case 13:
1552             vt102_cursor_carriage_return (v);
1553             break;
1554            /*SO*/ case 14:
1555             /*select G1 */
1556             /*Ignoring charsets */
1557             break;
1558            /*SI*/ case 15:
1559             /*select G0 */
1560             /*Ignoring charsets */
1561             break;
1562            /*DLE*/ case 16:
1563           /*DC1 */ case 17:
1564           /*DC2 */ case 18:
1565           /*DC3 */ case 19:
1566           /*DC4 */ case 20:
1567            /*NAK*/ case 21:
1568            /*SYN*/ case 22:
1569            /*ETB*/ case 23:
1570            /*CAN*/ case 24:
1571            /*EM*/ case 25:
1572            /*SUB*/ case 26:
1573             break;
1574            /*ESC*/ case 27:
1575             p->in_escape++;
1576             return;
1577            /*FS*/ case 28:
1578            /*GS*/ case 29:
1579            /*RS*/ case 30:
1580            /*US*/ case 31:
1581            /*DEL*/ case 127:
1582             break;
1583           /*regular character */ default:
1584             vt102_regular_char (c, v, ch);
1585           }
1586       }
1587
1588     v->crt.pos = v->pos;
1589     v->crt.hide_cursor =
1590       v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1591
1592     if (v->current_line.y != v->pos.y)
1593       {
1594         vt102_log_line (c, v->current_line.y);
1595         v->current_line = v->pos;
1596       }
1597
1598     if (c->d)
1599       cmd_show_status (c->d, c);
1600   }
1601
1602   void vt102_send (Context * c, uint8_t key)
1603   {
1604     uint8_t ch;
1605
1606     if (!c->t)
1607       return;
1608
1609 #if 0
1610     fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1611 #endif
1612     if ((key > 31) && (key < 127))
1613       {
1614         c->t->xmit (c->t, &key, 1);
1615         return;
1616       }
1617
1618     switch (key)
1619       {
1620        /*NUL*/ case 0:
1621        /*SOH*/ case 1:
1622        /*STX*/ case 2:
1623        /*ETX*/ case 3:
1624        /*EOT*/ case 4:
1625        /*ENQ*/ case 5:
1626        /*ACK*/ case 6:
1627        /*BEL*/ case 7:
1628        /*BS*/ case 8:
1629        /*HT*/ case 9:
1630        /*LF*/ case 10:
1631        /*VT*/ case 11:
1632        /*FF*/ case 12:
1633         c->t->xmit (c->t, &key, 1);
1634         break;
1635        /*CR*/ case 13:
1636         c->t->xmit (c->t, &key, 1);
1637         if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1638           {
1639             ch = 10;
1640             c->t->xmit (c->t, &ch, 1);
1641           }
1642         break;
1643        /*SO*/ case 14:
1644        /*SI*/ case 15:
1645        /*DLE*/ case 16:
1646       /*DC1 */ case 17:
1647       /*DC2 */ case 18:
1648       /*DC3 */ case 19:
1649       /*DC4 */ case 20:
1650        /*NAK*/ case 21:
1651        /*SYN*/ case 22:
1652        /*ETB*/ case 23:
1653        /*CAN*/ case 24:
1654        /*EM*/ case 25:
1655        /*SUB*/ case 26:
1656         c->t->xmit (c->t, &key, 1);
1657         break;
1658        /*ESC*/ case 27:
1659        /*FS*/ case 28:
1660        /*GS*/ case 29:
1661        /*RS*/ case 30:
1662        /*US*/ case 31:
1663        /*DEL*/ case 127:
1664         c->t->xmit (c->t, &key, 1);
1665         break;
1666
1667       case KEY_UP:
1668       case KEY_DOWN:
1669       case KEY_RIGHT:
1670       case KEY_LEFT:
1671       case KEY_HOME:
1672       case KEY_MIDDLE:
1673       case KEY_END:
1674
1675         if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1676           {
1677             uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1678             c->t->xmit (c->t, &buf, sizeof (buf));
1679           }
1680         else
1681           {
1682             uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1683             c->t->xmit (c->t, &buf, sizeof (buf));
1684           }
1685         break;
1686       case KEY_STAR:
1687       case KEY_PLUS:
1688       case KEY_COMMA:
1689       case KEY_PERIOD:
1690       case KEY_DIVIDE:
1691       case KEY_0:
1692       case KEY_1:
1693       case KEY_2:
1694       case KEY_3:
1695       case KEY_4:
1696       case KEY_5:
1697       case KEY_6:
1698       case KEY_7:
1699       case KEY_8:
1700       case KEY_9:
1701         if (c->v->application_keypad_mode)
1702           {
1703             uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1704             c->t->xmit (c->t, &buf, sizeof (buf));
1705           }
1706         else
1707           {
1708             static char kpoff[KEY_NUM] = {
1709               [KEY_STAR] = '*',
1710               [KEY_PLUS] = '+',
1711               [KEY_COMMA] = ',',
1712               [KEY_MINUS] = '-',
1713               [KEY_PERIOD] = '.',
1714               [KEY_DIVIDE] = '/',
1715               [KEY_0] = '0',
1716               [KEY_1] = '1',
1717               [KEY_2] = '2',
1718               [KEY_3] = '3',
1719               [KEY_4] = '4',
1720               [KEY_5] = '5',
1721               [KEY_6] = '6',
1722               [KEY_7] = '7',
1723               [KEY_8] = '8',
1724               [KEY_9] = '9'
1725             };
1726
1727             c->t->xmit (c->t, &kpoff[key], 1);
1728           }
1729         break;
1730       case KEY_ENTER:
1731         if (c->v->application_keypad_mode)
1732           {
1733             uint8_t buf[] = { 033, 'O', 'M' };
1734             c->t->xmit (c->t, &buf, sizeof (buf));
1735           }
1736         else
1737           {
1738             ch = 13;
1739             c->t->xmit (c->t, &ch, 1);
1740             if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1741               {
1742                 ch = 10;
1743                 c->t->xmit (c->t, &ch, 1);
1744               }
1745           }
1746         break;
1747       case KEY_PF1:
1748       case KEY_PF2:
1749       case KEY_PF3:
1750       case KEY_PF4:
1751         {
1752           uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1753           c->t->xmit (c->t, &buf, sizeof (buf));
1754         }
1755         break;
1756       case KEY_INSERT:
1757       case KEY_DELETE:
1758       case KEY_PGUP:
1759       case KEY_PGDN:
1760         {
1761           uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1762           c->t->xmit (c->t, &buf, sizeof (buf));
1763         }
1764         break;
1765       }
1766
1767   }
1768
1769   void vt102_reset (VT102 * v)
1770   {
1771     VT102_parser *p = &v->parser;
1772
1773
1774     crt_cls (&v->crt);
1775     v->current_line = v->pos;
1776     v->pending_wrap = 0;
1777
1778     v->screen_start.x = 0;
1779     v->screen_start.y = 0;
1780     v->screen_end.x = VT102_COLS - 1;
1781     v->screen_end.y = VT102_ROWS - 1;
1782
1783     vt102_cursor_home (v);
1784     vt102_status_line (v, "");
1785
1786     vt102_reset_tabs (v);
1787     v->current_line = v->pos;
1788
1789     vt102_parser_reset (p);
1790     vt102_reset_state (v);
1791
1792     vt102_save_state (v);
1793
1794     v->last_reg_char = ' ';
1795   }
1796
1797   VT102 *vt102_new (void)
1798   {
1799     VT102 *v;
1800
1801     v = (VT102 *) malloc (sizeof (VT102));
1802
1803     v->xn_glitch = 1;
1804
1805     vt102_reset (v);
1806
1807
1808     return v;
1809   }
1810
1811   void vt102_set_ansi (VT102 * v, int ansi)
1812   {
1813     v->xn_glitch = ansi ? 0 : 1;
1814   }
1815
1816   void vt102_free (VT102 * v)
1817   {
1818     free (v);
1819   }