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