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