chiark / gitweb /
Remove build products
[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: vt102.c,v 1.67 2010/07/27 14:49:35 james Exp $";
10
11 /* 
12  * $Log: vt102.c,v $
13  * Revision 1.67  2010/07/27 14:49:35  james
14  * add support for byte logging
15  *
16  * Revision 1.66  2008/03/07 13:16:02  james
17  * *** empty log message ***
18  *
19  * Revision 1.65  2008/03/07 12:42:08  james
20  * *** empty log message ***
21  *
22  * Revision 1.64  2008/03/07 12:37:04  james
23  * *** empty log message ***
24  *
25  * Revision 1.63  2008/03/07 12:00:58  james
26  * *** empty log message ***
27  *
28  * Revision 1.62  2008/03/06 21:34:09  james
29  * *** empty log message ***
30  *
31  * Revision 1.61  2008/03/06 17:21:41  james
32  * *** empty log message ***
33  *
34  * Revision 1.60  2008/03/06 16:49:39  james
35  * *** empty log message ***
36  *
37  * Revision 1.59  2008/03/06 16:49:05  james
38  * *** empty log message ***
39  *
40  * Revision 1.58  2008/03/06 01:41:48  james
41  * *** empty log message ***
42  *
43  * Revision 1.57  2008/03/03 06:20:14  james
44  * *** empty log message ***
45  *
46  * Revision 1.56  2008/03/03 06:04:42  james
47  * *** empty log message ***
48  *
49  * Revision 1.55  2008/03/02 12:32:57  staffcvs
50  * *** empty log message ***
51  *
52  * Revision 1.54  2008/03/02 10:37:56  james
53  * *** empty log message ***
54  *
55  * Revision 1.53  2008/02/29 18:33:39  james
56  * *** empty log message ***
57  *
58  * Revision 1.52  2008/02/28 16:57:52  james
59  * *** empty log message ***
60  *
61  * Revision 1.51  2008/02/28 16:37:16  james
62  * *** empty log message ***
63  *
64  * Revision 1.50  2008/02/28 12:12:25  james
65  * *** empty log message ***
66  *
67  * Revision 1.49  2008/02/28 11:27:48  james
68  * *** empty log message ***
69  *
70  * Revision 1.48  2008/02/27 09:42:53  james
71  * *** empty log message ***
72  *
73  * Revision 1.47  2008/02/27 09:42:22  james
74  * *** empty log message ***
75  *
76  * Revision 1.46  2008/02/27 01:31:38  james
77  * *** empty log message ***
78  *
79  * Revision 1.45  2008/02/27 01:31:14  james
80  * *** empty log message ***
81  *
82  * Revision 1.44  2008/02/27 00:54:16  james
83  * *** empty log message ***
84  *
85  * Revision 1.43  2008/02/27 00:27:22  james
86  * *** empty log message ***
87  *
88  * Revision 1.42  2008/02/26 23:56:12  james
89  * *** empty log message ***
90  *
91  * Revision 1.41  2008/02/26 23:23:17  james
92  * *** empty log message ***
93  *
94  * Revision 1.40  2008/02/26 19:00:59  james
95  * *** empty log message ***
96  *
97  * Revision 1.39  2008/02/26 16:54:06  james
98  * *** empty log message ***
99  *
100  * Revision 1.38  2008/02/26 16:53:24  james
101  * *** empty log message ***
102  *
103  * Revision 1.37  2008/02/24 12:22:53  james
104  * *** empty log message ***
105  *
106  * Revision 1.36  2008/02/24 12:22:42  james
107  * *** empty log message ***
108  *
109  * Revision 1.35  2008/02/24 00:42:53  james
110  * *** empty log message ***
111  *
112  * Revision 1.34  2008/02/23 11:48:37  james
113  * *** empty log message ***
114  *
115  * Revision 1.33  2008/02/22 23:39:27  james
116  * *** empty log message ***
117  *
118  * Revision 1.32  2008/02/22 19:12:05  james
119  * *** empty log message ***
120  *
121  * Revision 1.31  2008/02/22 17:07:00  james
122  * *** empty log message ***
123  *
124  * Revision 1.30  2008/02/22 14:51:54  james
125  * *** empty log message ***
126  *
127  * Revision 1.29  2008/02/15 03:32:07  james
128  * *** empty log message ***
129  *
130  * Revision 1.28  2008/02/14 10:34:30  james
131  * *** empty log message ***
132  *
133  * Revision 1.27  2008/02/14 02:46:45  james
134  * *** empty log message ***
135  *
136  * Revision 1.26  2008/02/14 01:55:57  james
137  * *** empty log message ***
138  *
139  * Revision 1.25  2008/02/13 16:57:29  james
140  * *** empty log message ***
141  *
142  * Revision 1.24  2008/02/13 09:12:21  james
143  * *** empty log message ***
144  *
145  * Revision 1.23  2008/02/07 13:26:35  james
146  * *** empty log message ***
147  *
148  * Revision 1.22  2008/02/07 13:22:51  james
149  * *** empty log message ***
150  *
151  * Revision 1.21  2008/02/07 12:21:16  james
152  * *** empty log message ***
153  *
154  * Revision 1.20  2008/02/07 12:16:04  james
155  * *** empty log message ***
156  *
157  * Revision 1.19  2008/02/07 11:27:02  james
158  * *** empty log message ***
159  *
160  * Revision 1.18  2008/02/07 01:59:25  james
161  * *** empty log message ***
162  *
163  * Revision 1.17  2008/02/07 01:58:28  james
164  * *** empty log message ***
165  *
166  * Revision 1.16  2008/02/07 01:57:46  james
167  * *** empty log message ***
168  *
169  * Revision 1.15  2008/02/07 00:43:27  james
170  * *** empty log message ***
171  *
172  * Revision 1.14  2008/02/07 00:40:23  james
173  * *** empty log message ***
174  *
175  * Revision 1.13  2008/02/07 00:39:59  james
176  * *** empty log message ***
177  *
178  * Revision 1.12  2008/02/07 00:39:13  james
179  * *** empty log message ***
180  *
181  * Revision 1.11  2008/02/06 20:26:58  james
182  * *** empty log message ***
183  *
184  * Revision 1.10  2008/02/06 17:53:28  james
185  * *** empty log message ***
186  *
187  * Revision 1.9  2008/02/06 15:53:22  james
188  * *** empty log message ***
189  *
190  * Revision 1.8  2008/02/06 11:49:47  james
191  * *** empty log message ***
192  *
193  * Revision 1.7  2008/02/06 11:30:37  james
194  * *** empty log message ***
195  *
196  * Revision 1.6  2008/02/05 01:11:46  james
197  * *** empty log message ***
198  *
199  * Revision 1.5  2008/02/04 20:23:55  james
200  * *** empty log message ***
201  *
202  * Revision 1.4  2008/02/04 05:45:55  james
203  * ::
204  *
205  * Revision 1.3  2008/02/04 02:05:06  james
206  * *** empty log message ***
207  *
208  * Revision 1.2  2008/02/04 01:32:39  james
209  * *** empty log message ***
210  *
211  * Revision 1.1  2008/02/03 23:36:41  james
212  * *** empty log message ***
213  *
214  */
215
216
217 /* 
218  * Termcap he say:
219  * 
220  * VT102:
221  * 
222  * vt102|dec vt102:\ :mi:\
223  * :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
224  * 
225  * vt100|vt100-am|dec vt100 (w/advanced video):\ :am:bs:ms:xn:xo:\
226  * :co#80:it#8:li#24:vt#3:\
227  * :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\ :UP=\E[%dA:\
228  * :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
229  * :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
230  * :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
231  * :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
232  * :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
233  * :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
234  * :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
235  * :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
236  * :us=\E[4m:tc=vt100+fnkeys:
237  * 
238  * vt100+fnkeys|dec vt100 numeric keypad:\
239  * :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
240  * :tc=vt100+pfkeys:
241  * 
242  * vt100+pfkeys|dec vt100 numeric keypad:\
243  * :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
244  * 
245  * vt100+keypad|dec vt100 numeric keypad no fkeys:\
246  * :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
247  * 
248  */
249
250 /* 
251  * so the parser needs to be able to at least do CTRL-G CTRL-H CTRL-I
252  * CTRL-J CTRL-M CTRL-N
253  * 
254  * CTRL-O ESC7 ESC8 ESCH ESCM ESC>
255  * 
256  * ESC[%dA ESC[%dB ESC[%dC ESC[%dD ESC[H ESC[%d;%dH ESC[J ESC[K ESC[1K
257  * ESC[L ESC[M ESC[P
258  * 
259  * ESC[3g ESC[4h ESC[4l ESC[m ESC[1m ESC[4m ESC[5m ESC[7m ESC[%d;%dr
260  * 
261  * 
262  * ESC[?3l ESC[?4l ESC[?5l ESC[?7h ESC[?7h ESC[?7l ESC[?8h
263  * 
264  * ESC(B ESC)0
265  * 
266  * 
267  * TODO:
268  * 
269  * ESC(B ESC)0
270  * 
271  * CTRL-O
272  * 
273  * ANSI:
274  * 
275  * 
276  * 
277  * ansi|ansi/pc-term compatible with color:\
278  * :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
279  * :tc=klone+sgr:tc=ansi-m: ansi-m|ansi-mono|ANSI X3.64-1979 terminal with 
280  * ANSI.SYS compatible attributes:\ :5i:\
281  * :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
282  * :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
283  * :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
284  * :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
285  * :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
286  * :s3=\E+B:ta=\E[I:tc=pcansi-m: pcansi-m|pcansi-mono|ibm-pc terminal
287  * programs claiming to be ansi (mono mode):\ :am:bs:mi:ms:\
288  * :co#80:it#8:li#24:\
289  * :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
290  * :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
291  * :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
292  * :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\ :tc=klone+sgr-dumb:
293  * klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11
294  * m):\ :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
295  * :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
296  * klone+acs|alternate character set for ansi.sys displays:\
297  * :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:\
298  * :ae=\E[10m:as=\E[11m: ecma+color|color control for ECMA-48-compatible terminals:\ :Co#8:NC#3:pa#64:\ :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
299  * 
300  * 
301  * Ignoreing ones in the VT102 spec
302  * 
303  * ESC[%d@ ESC[I ESC[....R ESC[%dS ESC[%dT ESC[X ESC[Z
304  * 
305  * ESC[c *ESC[%db ESC[....d ESC[....f ESC[2g ESC[4i ESC[5i ESC[6n
306  * 
307  * ESC(B ESC)B ESC*B ESC+B
308  * 
309  * 
310  */
311
312 #include "project.h"
313 #include "syslog.h"
314
315 #define TABLE_LENGTH 128
316 #undef DEBUG
317
318
319 static char terminal_id[] = "vt102";
320
321 /* 
322  * number of aditional chars after \033 ... to complete the escape if it's
323  * fixed length 
324  */
325 int vt102_cmd_length[TABLE_LENGTH] = {
326   ['('] = 1,
327   [')'] = 1,
328   ['+'] = 1,
329   ['*'] = 1,
330   ['%'] = 1,
331   ['#'] = 1,
332   ['Y'] = 2,
333 };
334
335 /* 
336  * method for determining end if if's not fixed length, -ve numbers from
337  * #defines, +ve are literals 
338  */
339 #define CSI_ENDER -1
340 int vt102_cmd_termination[TABLE_LENGTH] = {
341   [']'] = 7,
342   ['['] = CSI_ENDER,
343 };
344
345
346
347
348
349 static inline int
350 safe_ch (int c)
351 {
352   if (c < 32)
353     return ' ';
354   if (c > 126)
355     return ' ';
356   return c;
357 }
358
359 static inline int
360 csi_ender (int c)
361 {
362   if ((c >= 'a') && (c <= 'z'))
363     return 1;
364   if ((c >= 'A') && (c <= 'Z'))
365     return 1;
366   if ((c == '@'))
367     return 1;
368   return 0;
369 }
370
371 static inline int
372 ctrl_chr (int ch, int term)
373 {
374   if ((term > 0) && (ch == term))
375     return 0;
376   if (ch == 033)
377     return 0;
378   if ((ch > 0) && (ch < 040))
379     return 1;
380   return 0;
381 }
382
383
384 static inline int
385 in_margins (VT102 * v, CRT_Pos p)
386 {
387   if (v->pos.x < v->top_margin.x)
388     return 0;
389   if (v->pos.y < v->top_margin.y)
390     return 0;
391
392   if (v->pos.x > v->bottom_margin.x)
393     return 0;
394   if (v->pos.y > v->bottom_margin.y)
395     return 0;
396
397   return 1;
398 }
399
400 void
401 vt102_crt_update (Context * c)
402 {
403   VT102 *v = c->v;
404
405   v->crt.pos = v->pos;
406   v->crt.hide_cursor =
407     v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
408
409   if (v->current_line.y != v->pos.y) {
410     vt102_log_line (c, v->current_line.y);
411     v->current_line = v->pos;
412   }
413
414   if (c->d)
415     cmd_show_status (c->d, c);
416 }
417
418 void
419 vt102_do_resize (Context * c)
420 {
421
422   VT102 *v = c->v;
423
424   v->crt.size = v->current_size;
425   v->crt.size.y++;
426   v->screen_end = v->current_size;
427   v->screen_end.x--;
428   v->screen_end.y--;
429   v->top_margin = v->screen_start;
430   v->bottom_margin = v->screen_end;
431   vt102_cursor_home (v);
432   crt_cls (&v->crt);
433
434   if (c->t)
435     tty_winch (c->t, v->current_size);
436
437   log_f (c->l, "<size now %dx%d>", v->current_size.x, v->current_size.y);
438   vt102_crt_update (c);
439 }
440
441
442 void
443 vt102_log_line (Context * c, int line)
444 {
445   CRT_Pos e = { c->v->current_size.x - 1, line };
446   CRT_Pos p = { 0, line };
447   char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
448
449   if (!c->l || c->byte_logging)
450     return;
451
452   for (; e.x > 0; --e.x) {
453     if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
454       break;
455   }
456
457   for (; p.x <= e.x; ++p.x) {
458     int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
459     if (ch < 32)
460       ch = ' ';
461     logptr += utf8_encode (logptr, ch);
462   }
463   *logptr = 0;
464
465   c->l->log (c->l, logbuf);
466 }
467
468 /* 
469  * Called for every upward scroll with same args 
470  */
471 void
472 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
473 {
474   /* 
475    * Only log if it scrolls off the top 
476    */
477   if (t.y)
478     return;
479
480   t.x = 0;
481   history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
482 }
483
484 void
485 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
486 {
487   if (v->pos.x < tl.x)
488     v->pos.x = tl.x;
489   if (v->pos.y < tl.y)
490     v->pos.y = tl.y;
491
492   if (v->pos.x > br.x)
493     v->pos.x = br.x;
494   if (v->pos.y > br.y)
495     v->pos.y = br.y;
496 }
497
498
499 void
500 vt102_cursor_normalize (VT102 * v)
501 {
502   CRT_Pos *top, *bottom;
503
504   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE]) {
505     vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
506   } else {
507     vt102_clip_cursor (v, v->screen_start, v->screen_end);
508   }
509 }
510
511
512 void
513 vt102_cursor_carriage_return (VT102 * v)
514 {
515   v->pos.x = v->top_margin.x;
516   v->pending_wrap = 0;
517 }
518
519 void
520 vt102_cursor_advance_line (Context * c)
521 {
522   VT102 *v = c->v;
523   int couldscroll = in_margins (v, v->pos);
524
525   /* have wraped off end of last line in scrolling region */
526   /* not necessary, but shuts compiler up */
527   if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y))
528       && (couldscroll)) {
529     vt102_log_line (c, v->pos.y);
530
531     vt102_history (c, v->top_margin, v->bottom_margin);
532
533     crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
534     return;
535   }
536
537   if (v->pos.y != v->screen_end.y)
538     v->pos.y++;
539   v->pending_wrap = 0;
540 }
541
542
543 void
544 vt102_cursor_retreat_line (Context * c)
545 {
546   VT102 *v = c->v;
547   int couldscroll = in_margins (v, v->pos);
548
549   /* have wraped off end of first line in scrolling region */
550   /* (|| not necessary, but shuts compiler up */
551   if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
552       (couldscroll)) {
553     vt102_log_line (c, v->pos.y);
554
555     crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
556     return;
557   }
558
559   if (v->pos.y != v->screen_start.y)
560     v->pos.y--;
561   v->pending_wrap = 0;
562 }
563
564
565 void
566 vt102_do_pending_wrap (Context * c)
567 {
568   VT102 *v = c->v;
569   int couldscroll = in_margins (v, v->pos);
570   int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
571
572   if (!v->pending_wrap)
573     return;
574
575   /* End of line but no autowrap, nothing to do */
576   if (!autowrap)
577     return;
578
579   /* End of screen and not allowed to scroll, nothing to do */
580   if ((v->pos.y == v->screen_end.y) && (!couldscroll))
581     return;
582
583   if (couldscroll) {
584     v->pos.x = v->top_margin.x;
585   } else {
586     v->pos.x = 0;
587   }
588
589   vt102_cursor_advance_line (c);
590 }
591
592 void
593 vt102_cursor_advance (Context * c)
594 {
595   VT102 *v = c->v;
596
597   if (v->pos.x < v->bottom_margin.x) {
598     /* Normal advance */
599     v->pos.x++;
600     v->pending_wrap = 0;
601     return;
602   }
603   v->pending_wrap++;
604   if (!c->v->xn_glitch)
605     vt102_do_pending_wrap (c);
606 }
607
608
609
610 void
611 vt102_cursor_retreat (VT102 * v)
612 {
613   if (v->pos.x != v->top_margin.x) {
614     v->pos.x--;
615   }
616
617   v->pending_wrap = 0;
618 }
619
620 void
621 vt102_reset_tabs (VT102 * v)
622 {
623   int i;
624
625   memset (v->tabs, 0, sizeof (v->tabs));
626
627   for (i = 0; i < VT102_MAX_COLS; i += 8) {
628     v->tabs[i]++;
629   }
630 }
631 void
632 vt102_cursor_advance_tab (VT102 * v)
633 {
634   if (v->pos.x == v->bottom_margin.x)
635     return;
636   while (v->pos.x < v->bottom_margin.x) {
637     v->pos.x++;
638     if (v->tabs[v->pos.x])
639       break;
640   }
641   v->pending_wrap = 0;
642 }
643
644 void
645 vt102_cursor_retreat_tab (VT102 * v)
646 {
647   if (v->pos.x == v->top_margin.x)
648     return;
649   while (v->pos.x > v->top_margin.x) {
650     v->pos.x--;
651     if (v->tabs[v->pos.x])
652       break;
653   }
654   v->pending_wrap = 0;
655 }
656
657 vt102_cursor_home (VT102 * v)
658 {
659   v->pos = v->top_margin;
660   vt102_cursor_normalize (v);
661   v->pending_wrap = 0;
662
663 }
664
665 vt102_cursor_absolute (VT102 * v, int x, int y)
666 {
667   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE]) {
668     v->pos.x = x + v->top_margin.x;
669     v->pos.y = y + v->top_margin.y;
670   } else {
671     v->pos.x = x;
672     v->pos.y = y;
673   }
674   vt102_cursor_normalize (v);
675   v->pending_wrap = 0;
676 }
677
678 vt102_cursor_relative (VT102 * v, int x, int y)
679 {
680   v->pos.x += x;
681   v->pos.y += y;
682   vt102_cursor_normalize (v);
683   v->pending_wrap = 0;
684 }
685
686
687
688 void
689 vt102_delete_from_line (VT102 * v, CRT_Pos p)
690 {
691   int n = v->bottom_margin.x - p.x;
692
693   if (n < 0)
694     return;
695
696   if (n) {
697
698     memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
699              &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
700   }
701
702   v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
703   /* But not attr due to vt102 bug */
704 }
705
706 void
707 vt102_insert_into_line (VT102 * v, CRT_Pos p)
708 {
709   int n = v->bottom_margin.x - p.x;
710
711   if (n < 0)
712     return;
713
714   if (n) {
715
716     memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
717              &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
718   }
719
720   v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
721   v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
722   v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
723 }
724
725
726
727 void
728 vt102_change_mode (Context * c, int private, char *ns, int set)
729 {
730   VT102 *v = c->v;
731   int m;
732
733
734   if (*ns) {
735     m = atoi (ns);
736   } else {
737     m = 1;
738   }
739
740   if (m < 0)
741     return;
742   if (m >= VT102_NMODES)
743     return;
744
745   if (private) {
746     v->private_modes[m] = set;
747     switch (m) {
748     case VT102_PRIVATE_MODE_CURSOR_MODE:
749       if (v->application_keypad_mode)
750         v->private_modes[m] = 0;
751       break;
752     case VT102_PRIVATE_MODE_ORIGIN_MODE:
753       vt102_cursor_home (v);
754       break;
755     case VT102_PRIVATE_MODE_132COLS:
756       v->current_size.x =
757         v->
758         private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
759         VT102_COLS_80;
760
761       vt102_do_resize (c);
762       break;
763     }
764
765   } else
766     v->modes[m] = set;
767 }
768
769 void
770 vt102_parse_mode_string (Context * c, char *buf, int len)
771 {
772   VT102 *v = c->v;
773   int private = 0;
774   char last = buf[len - 1];
775   char num[4];
776   int o;
777
778   memset (num, 0, sizeof (num));
779   o = sizeof (num) - 1;
780
781   len--;
782
783   if (*buf == '?') {
784     private++;
785     buf++;
786     len--;
787   }
788
789   if (len < 0)
790     return;
791
792   while (len--) {
793     if (*buf == ';') {
794       vt102_change_mode (c, private, &num[o], last == 'h');
795       memset (num, 0, sizeof (num));
796       o = sizeof (num) - 1;
797       buf++;
798       continue;
799     }
800
801     num[0] = num[1];
802     num[1] = num[2];
803     num[2] = *buf;
804
805     if (o)
806       o--;
807
808     buf++;
809   }
810
811   vt102_change_mode (c, private, &num[o], last == 'h');
812
813 }
814
815
816 void
817 vt102_change_attr (VT102 * v, char *na)
818 {
819   int a;
820
821
822   if (*na) {
823     a = atoi (na);
824   } else {
825     a = 0;
826   }
827
828   switch (a) {
829   case 0:
830     v->attr = CRT_ATTR_NORMAL;
831     v->color = CRT_COLOR_NORMAL;
832     break;
833   case 1:
834     v->attr |= CRT_ATTR_BOLD;
835     break;
836   case 21:
837   case 22:
838     v->attr &= ~CRT_ATTR_BOLD;
839     break;
840   case 4:
841     v->attr |= CRT_ATTR_UNDERLINE;
842     break;
843   case 24:
844     v->attr &= ~CRT_ATTR_UNDERLINE;
845     break;
846   case 5:
847     v->attr |= CRT_ATTR_BLINK;
848     break;
849   case 25:
850     v->attr &= ~CRT_ATTR_BLINK;
851     break;
852   case 7:
853     v->attr |= CRT_ATTR_REVERSE;
854     break;
855   case 27:
856     v->attr &= ~CRT_ATTR_REVERSE;
857     break;
858   case 30:
859   case 31:
860   case 32:
861   case 33:
862   case 34:
863   case 35:
864   case 36:
865   case 37:
866     v->color &= ~CRT_COLOR_FG_MASK;
867     v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
868     break;
869   case 90:
870   case 91:
871   case 92:
872   case 93:
873   case 94:
874   case 95:
875   case 96:
876   case 97:
877     v->color &= ~CRT_COLOR_FG_MASK;
878     v->color |=
879       (((a -
880          90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
881       CRT_COLOR_FG_MASK;
882     break;
883   case 39:
884   case 99:
885     v->color &= ~CRT_COLOR_FG_MASK;
886     v->color |=
887       (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
888     break;
889   case 40:
890   case 41:
891   case 42:
892   case 43:
893   case 44:
894   case 45:
895   case 46:
896   case 47:
897     v->color &= ~CRT_COLOR_BG_MASK;
898     v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
899     break;
900   case 100:
901   case 101:
902   case 102:
903   case 103:
904   case 104:
905   case 105:
906   case 106:
907   case 107:
908     v->color &= ~CRT_COLOR_BG_MASK;
909     v->color |=
910       (((a -
911          100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
912       CRT_COLOR_BG_MASK;
913     break;
914   case 49:
915   case 109:
916     v->color &= ~CRT_COLOR_BG_MASK;
917     v->color |=
918       (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
919     break;
920   }
921
922 }
923
924
925 void
926 vt102_parse_attr_string (VT102 * v, char *buf, int len)
927 {
928   int private = 0;
929   char last = buf[len - 1];
930   char num[4];
931   int o;
932
933   memset (num, 0, sizeof (num));
934   o = sizeof (num) - 1;
935
936   len--;
937
938   if (len < 0)
939     return;
940
941   while (len--) {
942     if (*buf == ';') {
943       vt102_change_attr (v, &num[o]);
944       memset (num, 0, sizeof (num));
945       o = sizeof (num) - 1;
946       buf++;
947       continue;
948     }
949
950     num[0] = num[1];
951     num[1] = num[2];
952     num[2] = *buf;
953
954     if (o)
955       o--;
956
957     buf++;
958   }
959   vt102_change_attr (v, &num[o]);
960 }
961
962 void
963 vt102_save_state (VT102 * v)
964 {
965   v->saved.pos = v->pos;
966   v->saved.attr = v->attr;
967   v->saved.color = v->color;
968   v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
969 }
970
971 void
972 vt102_restore_state (VT102 * v)
973 {
974   v->pos = v->saved.pos;
975   v->attr = v->saved.attr;
976   v->color = v->saved.color;
977   v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
978   vt102_cursor_normalize (v);
979   v->pending_wrap = 0;
980 }
981
982 void
983 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
984 {
985
986
987   vt102_do_pending_wrap (c);
988
989
990   if (v->modes[VT102_MODE_INSERT])
991     vt102_insert_into_line (v, v->pos);
992
993   v->last_reg_char = ch;
994
995
996   if (ch < VT102_CHARSET_SIZE) {
997     int cs;
998     if ((cs = vt102_charset_c0[ch])) {
999       ch = cs;
1000     } else if ((cs = charset_from_csid[v->g[v->cs]][ch])) {
1001       ch = cs;
1002     }
1003   }
1004   v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1005   v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1006   v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1007   vt102_cursor_advance (c);
1008
1009 }
1010
1011 vt102_send_id (Context * c, char *buf)
1012 {
1013
1014   if (c->t) {
1015     int l = strlen (buf);
1016     c->t->xmit (c->t, buf, l);
1017   }
1018 }
1019
1020 void
1021 vt102_scs (Context * c, int g, int s)
1022 {
1023   VT102 *v = c->v;
1024   int cs = VT102_CSID_US;
1025
1026
1027   switch (s) {
1028   case 'A':
1029     cs = VT102_CSID_UK;
1030     break;
1031   case '1':
1032   case '2':
1033   case 'B':
1034     cs = VT102_CSID_US;
1035     break;
1036   case '0':
1037     cs = VT102_CSID_GL;
1038     break;
1039   }
1040
1041   switch (g) {
1042   case '(':
1043     v->g[0] = cs;
1044     break;
1045   case ')':
1046     v->g[1] = cs;
1047     break;
1048   }
1049
1050 }
1051
1052 static int
1053 vt102_parse_csi (Context * c, char *buf, int len)
1054 {
1055   char last;
1056   char *ptr;
1057   char *arg = buf + 1;
1058   int narg;
1059   int err = 0;
1060
1061   VT102 *v = c->v;
1062
1063   buf[len] = 0;
1064
1065   last = buf[len - 1];
1066
1067   if (len > 2) {
1068     narg = atoi (arg);
1069   } else {
1070     narg = 1;
1071   }
1072
1073   switch (buf[0]) {
1074   case '[':                    /* CSI */
1075     switch (last) {
1076     case '@':                  /* ICH */
1077       while (narg--)
1078         vt102_insert_into_line (v, v->pos);
1079       break;
1080     case 'A':                  /* CUU */
1081       vt102_cursor_relative (v, 0, -narg);
1082       break;
1083     case 'e':                  /* VPR */
1084     case 'B':                  /* CUD */
1085       vt102_cursor_relative (v, 0, narg);
1086       break;
1087     case 'a':                  /* HPR */
1088     case 'C':                  /* CUF */
1089       vt102_cursor_relative (v, narg, 0);
1090       break;
1091     case 'D':                  /* CUB */
1092       vt102_cursor_relative (v, -narg, 0);
1093       break;
1094     case 'E':                  /* CNL */
1095       vt102_cursor_relative (v, 0, narg);
1096       vt102_cursor_carriage_return (v);
1097       break;
1098     case 'F':                  /* CPL */
1099       vt102_cursor_relative (v, 0, -narg);
1100       vt102_cursor_carriage_return (v);
1101       break;
1102     case 'G':                  /* CHG */
1103       vt102_cursor_absolute (v, narg - 1, v->pos.y);
1104       break;
1105     case 'H':                  /* CUP */
1106     case 'f':                  /* HVP */
1107       {
1108         int x, y;
1109
1110         y = narg - 1;
1111
1112         ptr = index (arg, ';');
1113         if (ptr)
1114           x = atoi (ptr + 1) - 1;
1115         else
1116           x = 0;
1117
1118         vt102_cursor_absolute (v, x, y);
1119       }
1120       break;
1121     case 'I':                  /* CHT */
1122       while (narg--)
1123         vt102_cursor_advance_tab (c->v);
1124       break;
1125     case 'J':                  /* ED */
1126       /* Different default */
1127       if (len == 2)
1128         narg = 0;
1129       switch (narg) {
1130       case 0:
1131         crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1132         break;
1133       case 1:
1134         crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1135         break;
1136       case 2:
1137         crt_erase (&v->crt, v->screen_start, v->screen_end, 1, v->color);
1138         break;
1139       }
1140       break;
1141     case 'K':                  /* EL */
1142       {
1143         CRT_Pos ls = { 0, v->pos.y };
1144         CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1145         /* Different default */
1146         if (len == 2)
1147           narg = 0;
1148
1149         switch (narg) {
1150         case 0:
1151           crt_erase (&v->crt, v->pos, le, 1, v->color);
1152           break;
1153         case 1:
1154           crt_erase (&v->crt, ls, v->pos, 1, v->color);
1155           break;
1156         case 2:
1157           crt_erase (&v->crt, ls, le, 1, v->color);
1158           break;
1159         }
1160       }
1161       break;
1162
1163     case 'L':                  /* IL */
1164       if ((v->pos.y >= v->top_margin.y)
1165           && (v->pos.y <= v->bottom_margin.y)) {
1166         while (narg--)
1167           crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1, v->color);
1168       }
1169       break;
1170
1171     case 'M':                  /* DL */
1172       if ((v->pos.y >= v->top_margin.y)
1173           && (v->pos.y <= v->bottom_margin.y)) {
1174         while (narg--) {
1175           vt102_history (c, v->pos, v->bottom_margin);
1176           crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1, v->color);
1177         }
1178       }
1179       break;
1180     case 'P':                  /* DCH */
1181       while (narg--)
1182         vt102_delete_from_line (v, v->pos);
1183       break;
1184     case 'S':                  /* SU */
1185       while (narg--) {
1186         vt102_history (c, v->top_margin, v->bottom_margin);
1187         crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
1188       }
1189       break;
1190     case 'T':                  /* SD */
1191       while (narg--)
1192         crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1193                          v->color);
1194       break;
1195     case 'X':                  /* ECH */
1196       {
1197         CRT_Pos end = v->pos;
1198         if (!narg)
1199           narg++;
1200
1201         end.x += narg - 1;
1202         if (end.x > v->bottom_margin.x)
1203           end.x = v->bottom_margin.x;
1204
1205         crt_erase (&v->crt, v->pos, end, 1, v->color);
1206       }
1207       break;
1208     case 'Z':                  /* CBT */
1209       while (narg--)
1210         vt102_cursor_retreat_tab (c->v);
1211       break;
1212     case '`':                  /* HPA */
1213       vt102_cursor_absolute (v, narg - 1, v->pos.y);
1214       break;
1215     case 'b':                  /* REP */
1216       while (narg--) {
1217         vt102_regular_char (c, v, v->last_reg_char);
1218         err += vt102_rx_hook (c, v->last_reg_char);
1219       }
1220       break;
1221     case 'c':                  /* DA */
1222       /* For some obscure reason some programs seem to send */
1223       /* CSI [ ? .. c, which we ignore */
1224       if (buf[1] != '?')
1225         vt102_send_id (c, "\033[?2c");
1226       break;
1227     case 'd':                  /* VPA */
1228       vt102_cursor_absolute (v, v->pos.x, narg - 1);
1229       break;
1230     case 'g':                  /* TBC */
1231       /* Different default */
1232       if (len == 2)
1233         narg = 0;
1234
1235       switch (narg) {
1236       case 0:
1237         v->tabs[v->pos.x] = 0;
1238         break;
1239       case 2:
1240         // FIXME: - LA120 says current line only WTF?
1241       case 3:
1242         memset (v->tabs, 0, sizeof (v->tabs));
1243         break;
1244       }
1245       break;
1246
1247     case 'i':                  /* MC */
1248       // Printer commands
1249       // FIXME
1250       break;
1251     case 'h':                  /* SM */
1252     case 'l':                  /* RM */
1253       vt102_parse_mode_string (c, &buf[1], len - 1);
1254       break;
1255     case 'm':                  /* SGR */
1256       vt102_parse_attr_string (v, &buf[1], len - 1);
1257       break;
1258     case 'n':                  /* DSR */
1259       // Device status report
1260       switch (buf[1]) {
1261       case '5':
1262         vt102_send_id (c, "\033[0n");
1263         break;
1264       case '6':                /* CPR */
1265         if (c->t) {
1266           char buf[16];
1267           int i;
1268           i = sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1269           c->t->xmit (c->t, buf, i);
1270         }
1271         break;
1272       default:
1273         // ?15n printer status
1274         log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1275                __LINE__, buf);
1276       }
1277       break;
1278     case 'q':                  /* DECLL */
1279       // Load LED on off 
1280       break;
1281     case 'r':                  /* DECSTBM */
1282       v->top_margin = v->screen_start;
1283       v->bottom_margin = v->screen_end;
1284
1285       if ((len > 2) && (ptr = index (arg, ';'))) {
1286         ptr++;
1287         v->top_margin.y = narg - 1;
1288         v->bottom_margin.y = atoi (ptr) - 1;
1289       }
1290
1291       if (v->top_margin.y < v->screen_start.y)
1292         v->top_margin.y = v->screen_start.y;
1293       if (v->top_margin.y > v->screen_end.y)
1294         v->top_margin.y = v->screen_end.y;
1295       if (v->bottom_margin.y < v->screen_start.y)
1296         v->bottom_margin.y = v->screen_start.y;
1297       if (v->bottom_margin.y > v->screen_end.y)
1298         v->bottom_margin.y = v->screen_end.y;
1299
1300       vt102_cursor_home (v);
1301       break;
1302     case 's':                  /* SCP */
1303       v->saved.pos = v->pos;
1304       break;
1305     case 'u':                  /* RCP */
1306       v->pos = v->saved.pos;
1307       vt102_cursor_normalize (v);
1308       v->pending_wrap = 0;
1309       break;
1310     case 'y':                  /* DECTST */
1311       // Invoke confidence test
1312       break;
1313     case 'z':                  /* DECVERP */
1314       // request terminal parameters
1315       break;
1316
1317     default:
1318       log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__, buf);
1319
1320       ;
1321     }
1322     break;
1323   default:
1324     log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1325            __LINE__, buf);
1326     ;
1327   }
1328
1329
1330
1331   return err;
1332 }
1333
1334 static int
1335 vt102_parse_esc (Context * c)
1336 {
1337   int err = 0;
1338   VT102 *v = c->v;
1339   VT102_parser *p = &v->parser;
1340
1341 #ifdef DEBUG
1342   p->cmd_buf[p->cmd_ptr] = 0;
1343   log_f (c->l, "<cmd in  x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1344          v->pos.y, v->pending_wrap,
1345          !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1346 #endif
1347
1348   if (!v->private_modes[VT102_PRIVATE_MODE_VT52]) {
1349     int ate = 1;
1350
1351     switch (p->cmd_buf[0]) {
1352     case 'A':                  /* CURSOR UP */
1353       vt102_cursor_relative (v, 0, -1);
1354       break;
1355     case 'B':                  /* CURSOR DOWN */
1356       vt102_cursor_relative (v, 0, 1);
1357       break;
1358     case 'C':                  /* CURSOR RIGHT */
1359       vt102_cursor_relative (v, 1, 0);
1360       break;
1361     case 'D':                  /* CURSOR LEFT */
1362       vt102_cursor_relative (v, -1, 0);
1363       break;
1364     case 'F':                  /* ENTER GRAPHICS MODE */
1365       v->cs = 1;
1366       v->g[1] = VT102_CSID_VT52;
1367       break;
1368     case 'G':                  /* EXIT GRAPHICS MODE */
1369       v->cs = 0;
1370       v->g[0] = VT102_CSID_US;
1371       break;
1372     case 'H':                  /* CURSOR HOME */
1373       vt102_cursor_absolute (v, 0, 0);
1374       break;
1375     case 'I':                  /* REVERSE LINE FEED */
1376       vt102_cursor_retreat_line (c);
1377       break;
1378     case 'J':                  /* ERASE TO END OF SCREEN */
1379       crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1380       break;
1381     case 'K':                  /* ERASE TO END OF LINE */
1382       {
1383         CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1384         crt_erase (&v->crt, v->pos, le, 1, v->color);
1385       }
1386       break;
1387     case 'Y':                  /* DIRECT CURSOR ADDRESS */
1388       vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1389       break;
1390     case 'V':                  /* PRINT CURSOR LINE */
1391       // Print current line
1392       break;
1393     case 'W':
1394       // Printer on
1395       break;
1396     case 'X':
1397       // printer off
1398       break;
1399     case ']':                  /* PRINT SCREEN */
1400       // print screen
1401       break;
1402     case 'Z':                  // ID
1403       vt102_send_id (c, "\033/K");
1404       break;
1405     case '^':
1406       // Autoprint on
1407       break;
1408     case '_':
1409       // Autoprint off
1410       break;
1411     case '=':                  /* ENTER ALTERNATE KEYPAD MODE */
1412       v->application_keypad_mode = 1;
1413       break;
1414     case '>':                  /* EXIT ALTERNATE KEYPAD MODE */
1415       v->application_keypad_mode = 0;
1416       break;
1417     default:
1418       ate = 0;
1419     }
1420     if (ate)
1421       return;
1422   }
1423
1424
1425   /* 
1426    * If you edit this switch don't forget to edit the length and
1427    * termination tables 
1428    */
1429   switch (p->cmd_buf[0]) {
1430   case 'D':                    /* IND */
1431     vt102_cursor_advance_line (c);
1432     break;
1433
1434   case 'E':                    /* NEL */
1435     vt102_cursor_advance_line (c);
1436     v->pos.x = v->top_margin.x;
1437     vt102_cursor_normalize (v);
1438     v->pending_wrap = 0;
1439     break;
1440     /* F SSA */
1441     /* G ESA */
1442   case 'H':                    /* HTS */
1443     v->tabs[v->pos.x]++;
1444     break;
1445     /* I HTJ */
1446     /* J VTS */
1447     /* K PLD */
1448     /* L PLU */
1449   case 'M':                    /* RI */
1450     vt102_cursor_retreat_line (c);
1451     break;
1452   case 'N':                    /* SS2 */
1453     // select G2 for one char
1454     break;
1455   case 'O':                    /* SS3 */
1456     // select G3 for one char
1457     break;
1458     /* P DCS */
1459     /* Q PU1 */
1460     /* R PU2 */
1461     /* S STS */
1462     /* T CCH */
1463     /* U MW */
1464     /* V SPA */
1465     /* W EPA */
1466     /* X SOS */
1467     /* Y SGCI */
1468   case 'Z':                    /* DECID */
1469     vt102_send_id (c, terminal_id);
1470     break;
1471   case 'c':                    /* RIS */
1472     vt102_reset (c);
1473     break;
1474   case '=':
1475     /* DECKPAM */
1476     v->application_keypad_mode = 1;
1477     break;
1478   case '>':
1479     /* DECKPNM */
1480     v->application_keypad_mode = 0;
1481     break;
1482
1483   case '#':
1484     switch (p->cmd_buf[1]) {
1485     case '3':                  /* DECDHL */
1486       // top of double height line
1487     case '4':                  /* DECDHL */
1488       // bottom of double height line
1489     case '5':                  /* DECSWL */
1490       // single width line
1491     case '6':                  /* DECDWL */
1492       // double width line
1493       break;
1494     case '8':                  /* DECALN */
1495       {
1496         int i;
1497         crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1498                    CRT_COLOR_NORMAL);
1499         for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1500           v->crt.screen[i].chr = 'E';
1501       }
1502       break;
1503     default:
1504
1505       log_f (c->l,
1506              "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1507              __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1508
1509     }
1510     break;
1511   case '<':                    /* DECANM */
1512     // Set ansi mode - ignored 
1513     break;
1514   case '7':                    /* DECSC */
1515     vt102_save_state (v);
1516     break;
1517   case '8':                    /* DECRC */
1518     vt102_restore_state (v);
1519     break;
1520   case ']':
1521     /* Set various titles in xterm - ignored */
1522     break;
1523   case '[':                    /* CSI */
1524     err += vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1525     break;
1526     /* Charsets */
1527   case '(':                    /* SCS */
1528   case ')':                    /* SCS */
1529   case '+':                    /* SCS */
1530   case '*':                    /* SCS */
1531   case '%':                    /* SCS */
1532     vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1533     break;
1534   default:
1535     log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1536            __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1537
1538     ;
1539   }
1540
1541   p->cmd_buf[p->cmd_ptr] = 0;
1542 #ifdef DEBUG
1543   log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1544          v->pending_wrap);
1545 #endif
1546   return err;
1547 }
1548
1549 void
1550 vt102_status_line (VT102 * v, char *str)
1551 {
1552   int i;
1553   CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1554
1555   for (i = 0; i < v->current_size.x; ++i) {
1556     ca->attr = CRT_ATTR_REVERSE;
1557     ca->color = CRT_COLOR_NORMAL;
1558     ca->chr = *str ? *str : ' ';
1559     if (*str)
1560       str++;
1561     ca++;
1562   }
1563
1564   for (; i < VT102_MAX_COLS; ++i) {
1565     ca->attr = CRT_ATTR_NORMAL;
1566     ca->color = CRT_COLOR_NORMAL;
1567     ca->chr = ' ';
1568     ca++;
1569   }
1570
1571 }
1572
1573
1574 void
1575 vt102_parser_reset (VT102_parser * p)
1576 {
1577   p->in_cmd = 0;
1578   p->cmd_more_bytes = 0;
1579   p->cmd_termination = 0;
1580   p->in_escape = 0;
1581 }
1582
1583
1584 void
1585 vt102_reset_state (Context * c)
1586 {
1587   VT102 *v = c->v;
1588   vt102_parser_reset (&v->parser);
1589
1590   v->attr = CRT_ATTR_NORMAL;
1591   v->color = CRT_COLOR_NORMAL;
1592
1593   v->application_keypad_mode = 0;
1594
1595   v->current_size = v->original_size;
1596
1597   vt102_do_resize (c);
1598
1599   memset (v->modes, 0, VT102_NMODES);
1600   memset (v->private_modes, 0, VT102_NMODES);
1601
1602   v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1603   v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1604   v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1605   v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1606   v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1607
1608   vt102_reset_tabs (v);
1609
1610   v->g[0] = v->g[1] = VT102_CSID_US;
1611   v->cs = 0;
1612 }
1613
1614 static void
1615 pre_parse_cmd (int ch, VT102_parser * p)
1616 {
1617   if (ch > TABLE_LENGTH)
1618     return;
1619   if (ch < 0)
1620     return;
1621
1622   p->cmd_more_bytes = 0;
1623   p->in_cmd = 0;
1624
1625   p->cmd_termination = vt102_cmd_termination[ch];
1626   if (p->cmd_termination) {
1627     p->in_cmd++;
1628     return;
1629   }
1630
1631   p->cmd_more_bytes = vt102_cmd_length[ch];
1632
1633   if (p->cmd_more_bytes) {
1634     p->in_cmd++;
1635     return;
1636   }
1637 }
1638
1639 int
1640 vt102_rx_hook (Context * c, int ch)
1641 {
1642   if (!c->r)
1643     return 0;
1644   if (!c->r->rx)
1645     return 0;
1646   return c->r->rx (c->r, ch);
1647 }
1648
1649 int
1650 vt102_parse_char (Context * c, int ch)
1651 {
1652   int err = 0;
1653   VT102 *v = c->v;
1654   VT102_parser *p = &v->parser;
1655
1656 #ifdef DEBUG
1657   log_f (c->l, "char %3d %c   ie=%d ic=%d cmb=%d ct=%3d    %2d %2d %d", ch,
1658          safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1659          p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1660 #endif
1661
1662
1663   if (ch == SYM_CHAR_RESET) {
1664     vt102_reset_state (c);
1665   } else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination)) {
1666     p->cmd_buf[p->cmd_ptr++] = ch;
1667     if (p->cmd_ptr == VT102_CMD_LEN)
1668       p->in_cmd = 0;
1669     if (p->cmd_more_bytes) {
1670       p->cmd_more_bytes--;
1671
1672
1673       if (!p->cmd_more_bytes == 1)
1674         p->in_cmd = 0;
1675     }
1676
1677     switch (p->cmd_termination) {
1678     case 0:
1679       break;
1680     default:
1681       if (p->cmd_termination == ch)
1682         p->in_cmd = 0;
1683       break;
1684     case CSI_ENDER:
1685       if (csi_ender (ch))
1686         p->in_cmd = 0;
1687       break;
1688     }
1689
1690     if (!p->in_cmd) {
1691       err += vt102_parse_esc (c);
1692       p->cmd_more_bytes = 0;
1693       p->cmd_termination = 0;
1694     }
1695   } else if (p->in_escape && !ctrl_chr (ch, 0)) {
1696     p->cmd_ptr = 0;
1697     p->cmd_buf[p->cmd_ptr++] = ch;
1698     p->in_escape = 0;
1699
1700     pre_parse_cmd (ch, p);
1701
1702     if (!p->in_cmd)
1703       err += vt102_parse_esc (c);
1704   } else if ((ch >= 0x80) && (ch < 0xa0)) {
1705     /* C1 characters */
1706     switch (ch) {
1707     case 0x80:                 /* @ PAD */
1708     case 0x81:                 /* A HOP */
1709     case 0x82:                 /* B BPH */
1710     case 0x83:                 /* C NBH */
1711     case 0x84:                 /* D IND */
1712     case 0x85:                 /* E NEL */
1713     case 0x86:                 /* F SSA */
1714     case 0x87:                 /* G ESA */
1715     case 0x88:                 /* H HTS */
1716     case 0x89:                 /* I HTJ */
1717     case 0x8a:                 /* J VTS */
1718     case 0x8b:                 /* K PLD */
1719     case 0x8c:                 /* L PLU */
1720     case 0x8d:                 /* M RI */
1721     case 0x8e:                 /* N SS2 */
1722     case 0x8f:                 /* O SS3 */
1723     case 0x90:                 /* P DCS */
1724     case 0x91:                 /* Q PU1 */
1725     case 0x92:                 /* R PU2 */
1726     case 0x93:                 /* S STS */
1727     case 0x94:                 /* T CCH */
1728     case 0x95:                 /* U MW */
1729     case 0x96:                 /* V SPA */
1730     case 0x97:                 /* W EPA */
1731     case 0x98:                 /* X SOS */
1732     case 0x99:                 /* Y SGCI */
1733     case 0x9a:                 /* Z DECID */
1734     case 0x9b:                 /* [ CSI */
1735     case 0x9c:                 /* \ ST */
1736     case 0x9d:                 /* ] OSC */
1737     case 0x9e:                 /* ^ PM */
1738     case 0x9f:                 /* _ APC */
1739       /* C1 chars are equavlent to ESC ch-0x40 */
1740       p->cmd_ptr = 0;
1741       p->cmd_buf[p->cmd_ptr++] = ch;
1742       p->in_escape = 0;
1743
1744       pre_parse_cmd (ch, p);
1745
1746       if (!p->in_cmd)
1747         err += vt102_parse_esc (c);
1748       break;
1749     }
1750   } else {
1751 #ifdef DEBUG
1752     if (ch != 27)
1753       log_f (c->l,
1754              "<chr in  x=%3d y=%2d aw=%d  \\%03o %3d %c",
1755              v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1756
1757 #endif
1758
1759     switch (ch) {
1760     case 0:                    /* NUL */
1761       break;
1762 #ifdef HIDE_NON_PRINTABLES
1763     case 1:                    /* SOH */
1764     case 2:                    /* STX */
1765     case 3:                    /* ETX */
1766     case 4:                    /* EOT */
1767       break;
1768 #endif
1769     case 5:                    /* ENQ */
1770       vt102_send_id (c, terminal_id);
1771       break;
1772 #ifdef HIDE_NON_PRINTABLES
1773     case 6:                    /* ACK */
1774       break;
1775 #endif
1776     case 7:                    /* BEL */
1777       // FIXME beep
1778       break;
1779     case 8:                    /* BS */
1780       vt102_cursor_retreat (c->v);
1781       err += vt102_rx_hook (c, ch);
1782       break;
1783     case 9:                    /* HT */
1784       vt102_cursor_advance_tab (c->v);
1785       err += vt102_rx_hook (c, ch);
1786       break;
1787     case 10:                   /* LF */
1788     case 11:                   /* VT */
1789     case 12:                   /* FF */
1790       vt102_cursor_advance_line (c);
1791       err += vt102_rx_hook (c, 10);
1792       if (!v->modes[VT102_MODE_NEWLINE_MODE])
1793         break;
1794     case 13:                   /* CR */
1795       vt102_cursor_carriage_return (v);
1796       err += vt102_rx_hook (c, 13);
1797       break;
1798     case 14:                   /* SO */
1799       /* select G1 */
1800       v->cs = 1;
1801       break;
1802     case 15:                   /* SI */
1803       /* select G0 */
1804       v->cs = 0;
1805       break;
1806 #ifdef HIDE_NON_PRINTABLES
1807     case 16:                   /* DLE */
1808     case 17:                   /* DC1 */
1809     case 18:                   /* DC2 */
1810     case 19:                   /* DC3 */
1811     case 20:                   /* DC4 */
1812     case 21:                   /* NAK */
1813     case 22:                   /* SYN */
1814     case 23:                   /* ETB */
1815     case 24:                   /* CAN */
1816     case 25:                   /* EM */
1817     case 26:                   /* SUB */
1818       break;
1819 #endif
1820     case 27:                   /* ESC */
1821       p->in_escape++;
1822       break;
1823 #ifdef HIDE_NON_PRINTABLES
1824     case 28:                   /* FS */
1825     case 29:                   /* GS */
1826     case 30:                   /* RS */
1827     case 31:                   /* US */
1828     case 127:                  /* DEL */
1829       break;
1830 #endif
1831     default:                   /* regular character */
1832       vt102_regular_char (c, v, ch);
1833       err += vt102_rx_hook (c, ch);
1834     }
1835
1836 #ifdef DEBUG
1837     if (ch != 27)
1838       log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1839              v->pending_wrap);
1840 #endif
1841   }
1842
1843   vt102_crt_update (c);
1844
1845   return err;
1846 }
1847
1848
1849 void
1850 vt102_send (Context * c, uint8_t key)
1851 {
1852   uint8_t ch;
1853
1854   if (!c->t)
1855     return;
1856
1857   if ((key > 31) && (key < 127)) {
1858     c->t->xmit (c->t, &key, 1);
1859     return;
1860   }
1861
1862   switch (key) {
1863   case 0:                      /* NUL */
1864   case 1:                      /* SOH */
1865   case 2:                      /* STX */
1866   case 3:                      /* ETX */
1867   case 4:                      /* EOT */
1868   case 5:                      /* ENQ */
1869   case 6:                      /* ACK */
1870   case 7:                      /* BEL */
1871   case 8:                      /* BS */
1872   case 9:                      /* HT */
1873   case 10:                     /* LF */
1874   case 11:                     /* VT */
1875   case 12:                     /* FF */
1876     c->t->xmit (c->t, &key, 1);
1877     break;
1878   case 13:                     /* CR */
1879     c->t->xmit (c->t, &key, 1);
1880     if (c->v->modes[VT102_MODE_NEWLINE_MODE]) {
1881       ch = 10;
1882       c->t->xmit (c->t, &ch, 1);
1883     }
1884     break;
1885   case 14:                     /* SO */
1886   case 15:                     /* SI */
1887   case 16:                     /* DLE */
1888   case 17:                     /* DC1 */
1889   case 18:                     /* DC2 */
1890   case 19:                     /* DC3 */
1891   case 20:                     /* DC4 */
1892   case 21:                     /* NAK */
1893   case 22:                     /* SYN */
1894   case 23:                     /* ETB */
1895   case 24:                     /* CAN */
1896   case 25:                     /* EM */
1897   case 26:                     /* SUB */
1898     c->t->xmit (c->t, &key, 1);
1899     break;
1900   case 27:                     /* ESC */
1901   case 28:                     /* FS */
1902   case 29:                     /* GS */
1903   case 30:                     /* RS */
1904   case 31:                     /* US */
1905   case 127:                    /* DEL */
1906     c->t->xmit (c->t, &key, 1);
1907     break;
1908
1909   case KEY_UP:
1910   case KEY_DOWN:
1911   case KEY_RIGHT:
1912   case KEY_LEFT:
1913   case KEY_HOME:
1914   case KEY_MIDDLE:
1915   case KEY_END:
1916
1917     if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE]) {
1918       uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1919       c->t->xmit (c->t, &buf, sizeof (buf));
1920     } else {
1921       uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1922       c->t->xmit (c->t, &buf, sizeof (buf));
1923     }
1924     break;
1925   case KEY_STAR:
1926   case KEY_PLUS:
1927   case KEY_COMMA:
1928   case KEY_PERIOD:
1929   case KEY_DIVIDE:
1930   case KEY_0:
1931   case KEY_1:
1932   case KEY_2:
1933   case KEY_3:
1934   case KEY_4:
1935   case KEY_5:
1936   case KEY_6:
1937   case KEY_7:
1938   case KEY_8:
1939   case KEY_9:
1940     if (c->v->application_keypad_mode) {
1941       uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1942       c->t->xmit (c->t, &buf, sizeof (buf));
1943     } else {
1944       static char kpoff[KEY_NUM] = {
1945         [KEY_STAR] = '*',
1946         [KEY_PLUS] = '+',
1947         [KEY_COMMA] = ',',
1948         [KEY_MINUS] = '-',
1949         [KEY_PERIOD] = '.',
1950         [KEY_DIVIDE] = '/',
1951         [KEY_0] = '0',
1952         [KEY_1] = '1',
1953         [KEY_2] = '2',
1954         [KEY_3] = '3',
1955         [KEY_4] = '4',
1956         [KEY_5] = '5',
1957         [KEY_6] = '6',
1958         [KEY_7] = '7',
1959         [KEY_8] = '8',
1960         [KEY_9] = '9'
1961       };
1962
1963       c->t->xmit (c->t, &kpoff[key], 1);
1964     }
1965     break;
1966   case KEY_ENTER:
1967     if (c->v->application_keypad_mode) {
1968       uint8_t buf[] = { 033, 'O', 'M' };
1969       c->t->xmit (c->t, &buf, sizeof (buf));
1970     } else {
1971       ch = 13;
1972       c->t->xmit (c->t, &ch, 1);
1973       if (c->v->modes[VT102_MODE_NEWLINE_MODE]) {
1974         ch = 10;
1975         c->t->xmit (c->t, &ch, 1);
1976       }
1977     }
1978     break;
1979   case KEY_PF1:
1980   case KEY_PF2:
1981   case KEY_PF3:
1982   case KEY_PF4:
1983     {
1984       uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1985       c->t->xmit (c->t, &buf, sizeof (buf));
1986     }
1987     break;
1988   case KEY_VT220_HOME:
1989   case KEY_INSERT:
1990   case KEY_DELETE:
1991   case KEY_VT220_END:
1992   case KEY_PGUP:
1993   case KEY_PGDN:
1994   case KEY_F1:
1995   case KEY_F2:
1996   case KEY_F3:
1997   case KEY_F4:
1998   case KEY_F5:
1999   case KEY_F6:
2000   case KEY_F7:
2001   case KEY_F8:
2002   case KEY_F9:
2003   case KEY_F10:
2004   case KEY_F11:
2005   case KEY_F12:
2006   case KEY_F13:
2007   case KEY_F14:
2008   case KEY_F15:
2009   case KEY_F16:
2010   case KEY_F17:
2011   case KEY_F18:
2012   case KEY_F19:
2013   case KEY_F20:
2014     {
2015       uint8_t buf[16];
2016       int i;
2017       i = sprintf (buf, "\033[%d~", (key - KEY_180));
2018       c->t->xmit (c->t, &buf, i);
2019     }
2020     break;
2021   }
2022
2023 }
2024
2025 void
2026 vt102_reset (Context * c)
2027 {
2028   VT102 *v = c->v;
2029   VT102_parser *p = &v->parser;
2030
2031
2032   crt_cls (&v->crt);
2033   v->current_line = v->pos;
2034   v->pending_wrap = 0;
2035
2036   v->screen_start.x = 0;
2037   v->screen_start.y = 0;
2038   v->current_size = v->original_size;
2039   v->crt.size = v->current_size;
2040   v->crt.size.y++;
2041   v->screen_end = v->current_size;
2042   v->screen_end.x--;
2043   v->screen_end.y--;
2044
2045   vt102_cursor_home (v);
2046   vt102_status_line (v, "");
2047
2048   vt102_reset_tabs (v);
2049   v->current_line = v->pos;
2050
2051   vt102_parser_reset (p);
2052   vt102_reset_state (c);
2053
2054   vt102_save_state (v);
2055
2056   v->last_reg_char = ' ';
2057 }
2058
2059 VT102 *
2060 vt102_new (CRT_Pos * size)
2061 {
2062   VT102 *v;
2063
2064   v = (VT102 *) xmalloc (sizeof (VT102));
2065
2066   v->xn_glitch = 1;
2067
2068
2069   if (size) {
2070     v->original_size = *size;
2071
2072     if (v->original_size.x < 1)
2073       v->original_size.x = 1;
2074     if (v->original_size.y < 1)
2075       v->original_size.y = 1;
2076
2077     if (v->original_size.x > VT102_MAX_COLS)
2078       v->original_size.x = VT102_MAX_COLS;
2079     if (v->original_size.y > VT102_MAX_ROWS)
2080       v->original_size.y = VT102_MAX_ROWS;
2081
2082   } else {
2083     v->original_size.x = VT102_COLS_80;
2084     v->original_size.y = VT102_ROWS_24;
2085   }
2086
2087   return v;
2088 }
2089
2090 void
2091 vt102_set_ansi (VT102 * v, int ansi)
2092 {
2093   v->xn_glitch = ansi ? 0 : 1;
2094 }
2095
2096 void
2097 vt102_resize (Context * c, CRT_Pos size)
2098 {
2099   log_f (c->l, "<size change to %dx%d requested>", size.x, size.y);
2100
2101   if (!size.x)
2102     size.x = c->v->current_size.x;
2103   if (!size.y)
2104     size.y = c->v->current_size.y;
2105
2106   if (size.x < 1)
2107     size.x = 1;
2108   if (size.y < 1)
2109     size.y = 1;
2110
2111   if (size.x > VT102_MAX_COLS)
2112     size.x = VT102_MAX_COLS;
2113   if (size.y > VT102_MAX_ROWS)
2114     size.y = VT102_MAX_ROWS;
2115
2116   c->v->current_size = size;
2117   vt102_do_resize (c);
2118 }
2119
2120 void
2121 vt102_free (VT102 * v)
2122 {
2123   free (v);
2124 }