4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.53 2008/02/29 18:33:39 james
14 * *** empty log message ***
16 * Revision 1.52 2008/02/28 16:57:52 james
17 * *** empty log message ***
19 * Revision 1.51 2008/02/28 16:37:16 james
20 * *** empty log message ***
22 * Revision 1.50 2008/02/28 12:12:25 james
23 * *** empty log message ***
25 * Revision 1.49 2008/02/28 11:27:48 james
26 * *** empty log message ***
28 * Revision 1.48 2008/02/27 09:42:53 james
29 * *** empty log message ***
31 * Revision 1.47 2008/02/27 09:42:22 james
32 * *** empty log message ***
34 * Revision 1.46 2008/02/27 01:31:38 james
35 * *** empty log message ***
37 * Revision 1.45 2008/02/27 01:31:14 james
38 * *** empty log message ***
40 * Revision 1.44 2008/02/27 00:54:16 james
41 * *** empty log message ***
43 * Revision 1.43 2008/02/27 00:27:22 james
44 * *** empty log message ***
46 * Revision 1.42 2008/02/26 23:56:12 james
47 * *** empty log message ***
49 * Revision 1.41 2008/02/26 23:23:17 james
50 * *** empty log message ***
52 * Revision 1.40 2008/02/26 19:00:59 james
53 * *** empty log message ***
55 * Revision 1.39 2008/02/26 16:54:06 james
56 * *** empty log message ***
58 * Revision 1.38 2008/02/26 16:53:24 james
59 * *** empty log message ***
61 * Revision 1.37 2008/02/24 12:22:53 james
62 * *** empty log message ***
64 * Revision 1.36 2008/02/24 12:22:42 james
65 * *** empty log message ***
67 * Revision 1.35 2008/02/24 00:42:53 james
68 * *** empty log message ***
70 * Revision 1.34 2008/02/23 11:48:37 james
71 * *** empty log message ***
73 * Revision 1.33 2008/02/22 23:39:27 james
74 * *** empty log message ***
76 * Revision 1.32 2008/02/22 19:12:05 james
77 * *** empty log message ***
79 * Revision 1.31 2008/02/22 17:07:00 james
80 * *** empty log message ***
82 * Revision 1.30 2008/02/22 14:51:54 james
83 * *** empty log message ***
85 * Revision 1.29 2008/02/15 03:32:07 james
86 * *** empty log message ***
88 * Revision 1.28 2008/02/14 10:34:30 james
89 * *** empty log message ***
91 * Revision 1.27 2008/02/14 02:46:45 james
92 * *** empty log message ***
94 * Revision 1.26 2008/02/14 01:55:57 james
95 * *** empty log message ***
97 * Revision 1.25 2008/02/13 16:57:29 james
98 * *** empty log message ***
100 * Revision 1.24 2008/02/13 09:12:21 james
101 * *** empty log message ***
103 * Revision 1.23 2008/02/07 13:26:35 james
104 * *** empty log message ***
106 * Revision 1.22 2008/02/07 13:22:51 james
107 * *** empty log message ***
109 * Revision 1.21 2008/02/07 12:21:16 james
110 * *** empty log message ***
112 * Revision 1.20 2008/02/07 12:16:04 james
113 * *** empty log message ***
115 * Revision 1.19 2008/02/07 11:27:02 james
116 * *** empty log message ***
118 * Revision 1.18 2008/02/07 01:59:25 james
119 * *** empty log message ***
121 * Revision 1.17 2008/02/07 01:58:28 james
122 * *** empty log message ***
124 * Revision 1.16 2008/02/07 01:57:46 james
125 * *** empty log message ***
127 * Revision 1.15 2008/02/07 00:43:27 james
128 * *** empty log message ***
130 * Revision 1.14 2008/02/07 00:40:23 james
131 * *** empty log message ***
133 * Revision 1.13 2008/02/07 00:39:59 james
134 * *** empty log message ***
136 * Revision 1.12 2008/02/07 00:39:13 james
137 * *** empty log message ***
139 * Revision 1.11 2008/02/06 20:26:58 james
140 * *** empty log message ***
142 * Revision 1.10 2008/02/06 17:53:28 james
143 * *** empty log message ***
145 * Revision 1.9 2008/02/06 15:53:22 james
146 * *** empty log message ***
148 * Revision 1.8 2008/02/06 11:49:47 james
149 * *** empty log message ***
151 * Revision 1.7 2008/02/06 11:30:37 james
152 * *** empty log message ***
154 * Revision 1.6 2008/02/05 01:11:46 james
155 * *** empty log message ***
157 * Revision 1.5 2008/02/04 20:23:55 james
158 * *** empty log message ***
160 * Revision 1.4 2008/02/04 05:45:55 james
163 * Revision 1.3 2008/02/04 02:05:06 james
164 * *** empty log message ***
166 * Revision 1.2 2008/02/04 01:32:39 james
167 * *** empty log message ***
169 * Revision 1.1 2008/02/03 23:36:41 james
170 * *** empty log message ***
181 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
183 vt100|vt100-am|dec vt100 (w/advanced video):\
185 :co#80:it#8:li#24:vt#3:\
186 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
188 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
189 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
190 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
191 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
192 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
193 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
194 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
195 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
196 :us=\E[4m:tc=vt100+fnkeys:
198 vt100+fnkeys|dec vt100 numeric keypad:\
199 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
202 vt100+pfkeys|dec vt100 numeric keypad:\
203 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
205 vt100+keypad|dec vt100 numeric keypad no fkeys:\
206 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
211 so the parser needs to be able to at least do
273 ansi|ansi/pc-term compatible with color:\
274 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
275 :tc=klone+sgr:tc=ansi-m:
276 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
278 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
279 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
280 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
281 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
282 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
283 :s3=\E+B:ta=\E[I:tc=pcansi-m:
284 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
287 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
288 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
289 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
290 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
292 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
293 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
294 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
295 klone+acs|alternate character set for ansi.sys displays:\
296 :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:\
297 :ae=\E[10m:as=\E[11m:
298 ecma+color|color control for ECMA-48-compatible terminals:\
300 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
303 Ignoreing ones in the VT102 spec
333 #define TABLE_LENGTH 128
337 static char terminal_id[] = "vt102";
339 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
340 int vt102_cmd_length[TABLE_LENGTH] = {
350 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
352 int vt102_cmd_termination[TABLE_LENGTH] = {
374 if ((c >= 'a') && (c <= 'z'))
376 if ((c >= 'A') && (c <= 'Z'))
384 ctrl_chr (int ch, int term)
386 if ((term > 0) && (ch == term))
390 if ((ch > 0) && (ch < 040))
397 in_margins (VT102 * v, CRT_Pos p)
399 if (v->pos.x < v->top_margin.x)
401 if (v->pos.y < v->top_margin.y)
404 if (v->pos.x > v->bottom_margin.x)
406 if (v->pos.y > v->bottom_margin.y)
413 vt102_crt_update (Context * c)
419 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
421 if (v->current_line.y != v->pos.y)
423 vt102_log_line (c, v->current_line.y);
424 v->current_line = v->pos;
428 cmd_show_status (c->d, c);
432 vt102_do_resize (Context * c)
437 v->crt.size = v->current_size;
439 v->screen_end = v->current_size;
442 v->top_margin = v->screen_start;
443 v->bottom_margin = v->screen_end;
444 vt102_cursor_home (v);
448 tty_winch (c->t, v->current_size);
450 log_f (c->l, "<size now %dx%d>", v->current_size.x, v->current_size.y);
451 vt102_crt_update (c);
456 vt102_log_line (Context * c, int line)
458 CRT_Pos e = { c->v->current_size.x - 1, line };
459 CRT_Pos p = { 0, line };
460 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
466 for (; e.x > 0; --e.x)
468 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
472 for (; p.x <= e.x; ++p.x)
474 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
477 logptr += utf8_encode (logptr, ch);
481 c->l->log (c->l, logbuf);
484 /*Called for every upward scroll with same args*/
486 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
488 /*Only log if it scrolls off the top*/
493 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
497 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
512 vt102_cursor_normalize (VT102 * v)
514 CRT_Pos *top, *bottom;
516 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
518 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
522 vt102_clip_cursor (v, v->screen_start, v->screen_end);
528 vt102_cursor_carriage_return (VT102 * v)
530 /*FISH*/ v->pos.x = v->top_margin.x;
535 vt102_cursor_advance_line (Context * c)
538 int couldscroll = in_margins (v, v->pos);
540 /*have wraped off end of last line in scrolling region */
541 /* (|| not necessary, but shuts compiler up */
542 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
545 vt102_log_line (c, v->pos.y);
547 vt102_history (c, v->top_margin, v->bottom_margin);
549 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
553 if (v->pos.y != v->screen_end.y)
560 vt102_cursor_retreat_line (Context * c)
563 int couldscroll = in_margins (v, v->pos);
565 /*have wraped off end of last line in scrolling region */
566 /* (|| not necessary, but shuts compiler up */
567 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
570 vt102_log_line (c, v->pos.y);
572 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
576 if (v->pos.y != v->screen_start.y)
583 vt102_do_pending_wrap (Context * c)
586 int couldscroll = in_margins (v, v->pos);
587 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
589 if (!v->pending_wrap)
593 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
594 v->pos.y, autowrap, couldscroll);
597 /*End of line but no autowrap, nothing to do*/
601 /*End of screen and not allowed to scroll, nothing to do*/
602 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
607 v->pos.x = v->top_margin.x;
614 vt102_cursor_advance_line (c);
618 vt102_cursor_advance (Context * c)
622 if (v->pos.x < v->bottom_margin.x)
630 if (!c->v->xn_glitch)
631 vt102_do_pending_wrap (c);
637 vt102_cursor_retreat (VT102 * v)
639 if (v->pos.x != v->top_margin.x)
648 vt102_reset_tabs (VT102 * v)
652 memset (v->tabs, 0, sizeof (v->tabs));
654 for (i = 0; i < VT102_MAX_COLS; i += 8)
660 vt102_cursor_advance_tab (VT102 * v)
662 if (v->pos.x == v->bottom_margin.x)
664 while (v->pos.x < v->bottom_margin.x)
667 if (v->tabs[v->pos.x])
674 vt102_cursor_retreat_tab (VT102 * v)
676 if (v->pos.x == v->top_margin.x)
678 while (v->pos.x > v->top_margin.x)
681 if (v->tabs[v->pos.x])
687 vt102_cursor_home (VT102 * v)
689 v->pos = v->top_margin;
690 vt102_cursor_normalize (v);
695 vt102_cursor_absolute (VT102 * v, int x, int y)
697 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
699 v->pos.x = x + v->top_margin.x;
700 v->pos.y = y + v->top_margin.y;
707 vt102_cursor_normalize (v);
711 vt102_cursor_relative (VT102 * v, int x, int y)
715 vt102_cursor_normalize (v);
722 vt102_delete_from_line (VT102 * v, CRT_Pos p)
724 int n = v->bottom_margin.x - p.x;
732 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
733 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
736 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
737 /*But not attr due to vt102 bug*/
741 vt102_insert_into_line (VT102 * v, CRT_Pos p)
743 int n = v->bottom_margin.x - p.x;
751 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
752 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
755 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
756 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
757 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
763 vt102_change_mode (Context * c, int private, char *ns, int set)
780 if (m >= VT102_NMODES)
785 v->private_modes[m] = set;
788 case VT102_PRIVATE_MODE_CURSOR_MODE:
789 if (v->application_keypad_mode)
790 v->private_modes[m] = 0;
792 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
793 v->private_modes[m], set);
796 case VT102_PRIVATE_MODE_ORIGIN_MODE:
797 vt102_cursor_home (v);
799 case VT102_PRIVATE_MODE_132COLS:
800 /* We don't implement 132 col mode - yet */
804 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
816 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
821 vt102_parse_mode_string (Context * c, char *buf, int len)
825 char last = buf[len - 1];
829 memset (num, 0, sizeof (num));
830 o = sizeof (num) - 1;
848 vt102_change_mode (c, private, &num[o], last == 'h');
849 memset (num, 0, sizeof (num));
850 o = sizeof (num) - 1;
865 vt102_change_mode (c, private, &num[o], last == 'h');
871 vt102_change_attr (VT102 * v, char *na)
888 v->attr = CRT_ATTR_NORMAL;
889 v->color = CRT_COLOR_NORMAL;
892 v->attr |= CRT_ATTR_BOLD;
896 v->attr &= ~CRT_ATTR_BOLD;
899 v->attr |= CRT_ATTR_UNDERLINE;
902 v->attr &= ~CRT_ATTR_UNDERLINE;
905 v->attr |= CRT_ATTR_BLINK;
908 v->attr &= ~CRT_ATTR_BLINK;
911 v->attr |= CRT_ATTR_REVERSE;
914 v->attr &= ~CRT_ATTR_REVERSE;
924 v->color &= ~CRT_COLOR_FG_MASK;
925 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
935 v->color &= ~CRT_COLOR_FG_MASK;
938 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
943 v->color &= ~CRT_COLOR_FG_MASK;
945 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
955 v->color &= ~CRT_COLOR_BG_MASK;
956 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
966 v->color &= ~CRT_COLOR_BG_MASK;
969 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
974 v->color &= ~CRT_COLOR_BG_MASK;
976 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
982 fprintf (stderr, "unhandled SGR %d\n", a);
990 vt102_parse_attr_string (VT102 * v, char *buf, int len)
993 char last = buf[len - 1];
997 memset (num, 0, sizeof (num));
998 o = sizeof (num) - 1;
1009 vt102_change_attr (v, &num[o]);
1010 memset (num, 0, sizeof (num));
1011 o = sizeof (num) - 1;
1025 vt102_change_attr (v, &num[o]);
1029 vt102_save_state (VT102 * v)
1031 v->saved.pos = v->pos;
1032 v->saved.attr = v->attr;
1033 v->saved.color = v->color;
1034 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
1038 vt102_restore_state (VT102 * v)
1040 v->pos = v->saved.pos;
1041 v->attr = v->saved.attr;
1042 v->color = v->saved.color;
1043 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
1044 vt102_cursor_normalize (v);
1045 v->pending_wrap = 0;
1049 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
1053 vt102_do_pending_wrap (c);
1056 if (v->modes[VT102_MODE_INSERT])
1057 vt102_insert_into_line (v, v->pos);
1059 v->last_reg_char = ch;
1062 if (ch < VT102_CHARSET_SIZE)
1065 if ((cs = vt102_charset_c0[ch]))
1069 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1074 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1075 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1076 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1077 vt102_cursor_advance (c);
1081 vt102_send_id (Context * c, char *buf)
1086 int l = strlen (buf);
1087 c->t->xmit (c->t, buf, l);
1092 vt102_scs (Context * c, int g, int s)
1095 int cs = VT102_CSID_US;
1126 vt102_parse_csi (Context * c, char *buf, int len)
1130 char *arg = buf + 1;
1137 last = buf[len - 1];
1158 vt102_insert_into_line (v, v->pos);
1161 vt102_cursor_relative (v, 0, -narg);
1165 vt102_cursor_relative (v, 0, narg);
1169 vt102_cursor_relative (v, narg, 0);
1172 vt102_cursor_relative (v, -narg, 0);
1175 vt102_cursor_relative (v, 0, narg);
1176 vt102_cursor_carriage_return (v);
1179 vt102_cursor_relative (v, 0, -narg);
1180 vt102_cursor_carriage_return (v);
1183 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1192 ptr = index (arg, ';');
1194 x = atoi (ptr + 1) - 1;
1198 vt102_cursor_absolute (v, x, y);
1203 vt102_cursor_advance_tab (c->v);
1207 narg = 0; /*Different default */
1211 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1214 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1217 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1224 CRT_Pos ls = { 0, v->pos.y };
1225 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1227 narg = 0; /*Different default */
1232 crt_erase (&v->crt, v->pos, le, 1, v->color);
1235 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1238 crt_erase (&v->crt, ls, le, 1, v->color);
1245 if ((v->pos.y >= v->top_margin.y)
1246 && (v->pos.y <= v->bottom_margin.y))
1249 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1255 if ((v->pos.y >= v->top_margin.y)
1256 && (v->pos.y <= v->bottom_margin.y))
1260 vt102_history (c, v->pos, v->bottom_margin);
1261 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1268 vt102_delete_from_line (v, v->pos);
1273 vt102_history (c, v->top_margin, v->bottom_margin);
1274 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1280 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1285 CRT_Pos end = v->pos;
1290 if (end.x > v->bottom_margin.x)
1291 end.x = v->bottom_margin.x;
1293 crt_erase (&v->crt, v->pos, end, 1, v->color);
1298 vt102_cursor_retreat_tab (c->v);
1301 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1305 vt102_regular_char (c, v, v->last_reg_char);
1308 /* For some obscure reason some programs seem to send */
1309 /* CSI [ ? .. c, which we ignore */
1311 vt102_send_id (c, "\033[?2c");
1314 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1318 narg = 0; /*Different default */
1323 v->tabs[v->pos.x] = 0;
1325 case 2: //FIXME: - LA120 says current line only WTF?
1327 memset (v->tabs, 0, sizeof (v->tabs));
1332 case 'i': //Printer commands
1337 vt102_parse_mode_string (c, &buf[1], len - 1);
1342 vt102_parse_attr_string (v, &buf[1], len - 1);
1344 case 'n': //Device status report
1348 vt102_send_id (c, "\033[0n");
1356 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1357 c->t->xmit (c->t, buf, i);
1361 //?15n printer status
1362 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1366 case 'q': //Load LED on off
1369 v->top_margin = v->screen_start;
1370 v->bottom_margin = v->screen_end;
1372 if ((len > 2) && (ptr = index (arg, ';')))
1375 v->top_margin.y = narg - 1;
1376 v->bottom_margin.y = atoi (ptr) - 1;
1379 if (v->top_margin.y < v->screen_start.y)
1380 v->top_margin.y = v->screen_start.y;
1381 if (v->top_margin.y > v->screen_end.y)
1382 v->top_margin.y = v->screen_end.y;
1383 if (v->bottom_margin.y < v->screen_start.y)
1384 v->bottom_margin.y = v->screen_start.y;
1385 if (v->bottom_margin.y > v->screen_end.y)
1386 v->bottom_margin.y = v->screen_end.y;
1388 vt102_cursor_home (v);
1391 v->saved.pos = v->pos;
1394 v->pos = v->saved.pos;
1395 vt102_cursor_normalize (v);
1396 v->pending_wrap = 0;
1398 case 'y': //Invoke confidence test
1400 case 'z': //request terminal parameters
1404 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1411 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1421 vt102_parse_esc (Context * c)
1424 VT102_parser *p = &v->parser;
1427 p->cmd_buf[p->cmd_ptr] = 0;
1428 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1429 v->pos.y, v->pending_wrap,
1430 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1433 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1437 switch (p->cmd_buf[0])
1440 vt102_cursor_relative (v, 0, -1);
1443 vt102_cursor_relative (v, 0, 1);
1446 vt102_cursor_relative (v, 1, 0);
1449 vt102_cursor_relative (v, -1, 0);
1453 v->g[1] = VT102_CSID_VT52;
1457 v->g[0] = VT102_CSID_US;
1460 vt102_cursor_absolute (v, 0, 0);
1463 vt102_cursor_retreat_line (c);
1466 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1470 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1471 crt_erase (&v->crt, v->pos, le, 1, v->color);
1475 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1477 case 'V': //Print current line
1479 case 'W': //Printer on
1481 case 'X': //printer off
1483 case ']': //print screen
1486 vt102_send_id (c, "\033/K");
1488 case '^': //Autoprint on
1490 case '_': //Autoprint off
1493 v->application_keypad_mode = 1;
1496 v->application_keypad_mode = 0;
1506 /*If you edit this switch don't forget to edit the length and termination tables*/
1507 switch (p->cmd_buf[0])
1510 vt102_cursor_advance_line (c);
1514 vt102_cursor_advance_line (c);
1515 v->pos.x = v->top_margin.x;
1516 vt102_cursor_normalize (v);
1517 v->pending_wrap = 0;
1520 v->tabs[v->pos.x]++;
1523 vt102_cursor_retreat_line (c);
1525 case 'N': //select G2 for one char
1527 case 'O': //select G3 for one char
1530 vt102_send_id (c, terminal_id);
1536 v->application_keypad_mode = 1;
1539 v->application_keypad_mode = 0;
1543 switch (p->cmd_buf[1])
1545 case '3': //top of double height line
1546 case '4': //bottom of double height line
1547 case '5': //single width line
1548 case '6': //double width line
1554 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1556 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1557 v->crt.screen[i].chr = 'E';
1563 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1564 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1569 /*Set ansi mode - ignored */
1573 vt102_save_state (v);
1576 vt102_restore_state (v);
1578 case ']': /*Set various titles */
1582 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1584 case '(': /*Charsets */
1589 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1592 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1593 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1598 p->cmd_buf[p->cmd_ptr] = 0;
1600 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1606 vt102_status_line (VT102 * v, char *str)
1609 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1611 for (i = 0; i < v->current_size.x; ++i)
1613 ca->attr = CRT_ATTR_REVERSE;
1614 ca->color = CRT_COLOR_NORMAL;
1615 ca->chr = *str ? *str : ' ';
1621 for (; i < VT102_MAX_COLS; ++i)
1623 ca->attr = CRT_ATTR_NORMAL;
1624 ca->color = CRT_COLOR_NORMAL;
1633 vt102_parser_reset (VT102_parser * p)
1636 p->cmd_more_bytes = 0;
1637 p->cmd_termination = 0;
1643 vt102_reset_state (Context * c)
1646 vt102_parser_reset (&v->parser);
1648 v->attr = CRT_ATTR_NORMAL;
1649 v->color = CRT_COLOR_NORMAL;
1651 v->application_keypad_mode = 0;
1653 v->current_size = v->original_size;
1655 vt102_do_resize (c);
1657 memset (v->modes, 0, VT102_NMODES);
1658 memset (v->private_modes, 0, VT102_NMODES);
1660 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1661 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1662 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1663 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1664 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1666 vt102_reset_tabs (v);
1668 v->g[0] = v->g[1] = VT102_CSID_US;
1673 pre_parse_cmd (int ch, VT102_parser * p)
1675 if (ch > TABLE_LENGTH)
1680 p->cmd_more_bytes = 0;
1683 p->cmd_termination = vt102_cmd_termination[ch];
1684 if (p->cmd_termination)
1690 p->cmd_more_bytes = vt102_cmd_length[ch];
1692 if (p->cmd_more_bytes)
1700 vt102_parse_char (Context * c, int ch)
1703 VT102_parser *p = &v->parser;
1706 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1707 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1708 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1711 /* Turn anything non-ascii into '?' */
1713 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1721 if (p->dca_ptr == 2)
1726 if (ch == SYM_CHAR_RESET)
1728 vt102_reset_state (c);
1730 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1732 p->cmd_buf[p->cmd_ptr++] = ch;
1733 if (p->cmd_ptr == VT102_CMD_LEN)
1735 if (p->cmd_more_bytes)
1737 p->cmd_more_bytes--;
1740 if (!p->cmd_more_bytes == 1)
1744 switch (p->cmd_termination)
1749 if (p->cmd_termination == ch)
1760 vt102_parse_esc (c);
1761 p->cmd_more_bytes = 0;
1762 p->cmd_termination = 0;
1765 else if (p->in_escape && !ctrl_chr (ch, 0))
1768 p->cmd_buf[p->cmd_ptr++] = ch;
1771 pre_parse_cmd (ch, p);
1774 vt102_parse_esc (c);
1776 else if (ch == 0x9b) /*One byte CSI */
1779 p->cmd_buf[p->cmd_ptr++] = '[';
1781 pre_parse_cmd (ch, p);
1788 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1789 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1804 vt102_send_id (c, terminal_id);
1814 vt102_cursor_retreat (c->v);
1817 vt102_cursor_advance_tab (c->v);
1822 vt102_cursor_advance_line (c);
1823 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1826 vt102_cursor_carriage_return (v);
1861 /*regular character */ default:
1862 vt102_regular_char (c, v, ch);
1867 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1872 vt102_crt_update (c);
1878 vt102_send (Context * c, uint8_t key)
1886 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1888 if ((key > 31) && (key < 127))
1890 c->t->xmit (c->t, &key, 1);
1909 c->t->xmit (c->t, &key, 1);
1912 c->t->xmit (c->t, &key, 1);
1913 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1916 c->t->xmit (c->t, &ch, 1);
1932 c->t->xmit (c->t, &key, 1);
1940 c->t->xmit (c->t, &key, 1);
1951 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1953 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1954 c->t->xmit (c->t, &buf, sizeof (buf));
1958 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1959 c->t->xmit (c->t, &buf, sizeof (buf));
1977 if (c->v->application_keypad_mode)
1979 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1980 c->t->xmit (c->t, &buf, sizeof (buf));
1984 static char kpoff[KEY_NUM] = {
2003 c->t->xmit (c->t, &kpoff[key], 1);
2007 if (c->v->application_keypad_mode)
2009 uint8_t buf[] = { 033, 'O', 'M' };
2010 c->t->xmit (c->t, &buf, sizeof (buf));
2015 c->t->xmit (c->t, &ch, 1);
2016 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
2019 c->t->xmit (c->t, &ch, 1);
2028 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
2029 c->t->xmit (c->t, &buf, sizeof (buf));
2037 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
2038 c->t->xmit (c->t, &buf, sizeof (buf));
2046 vt102_reset (Context * c)
2049 VT102_parser *p = &v->parser;
2053 v->current_line = v->pos;
2054 v->pending_wrap = 0;
2056 v->screen_start.x = 0;
2057 v->screen_start.y = 0;
2058 v->current_size = v->original_size;
2059 v->crt.size = v->current_size;
2061 v->screen_end = v->current_size;
2065 vt102_cursor_home (v);
2066 vt102_status_line (v, "");
2068 vt102_reset_tabs (v);
2069 v->current_line = v->pos;
2071 vt102_parser_reset (p);
2072 vt102_reset_state (c);
2074 vt102_save_state (v);
2076 v->last_reg_char = ' ';
2080 vt102_new (CRT_Pos * size)
2084 v = (VT102 *) malloc (sizeof (VT102));
2091 v->original_size = *size;
2093 if (v->original_size.x < 1)
2094 v->original_size.x = 1;
2095 if (v->original_size.y < 1)
2096 v->original_size.y = 1;
2098 if (v->original_size.x > VT102_MAX_COLS)
2099 v->original_size.x = VT102_MAX_COLS;
2100 if (v->original_size.y > VT102_MAX_ROWS)
2101 v->original_size.y = VT102_MAX_ROWS;
2106 v->original_size.x = VT102_COLS_80;
2107 v->original_size.y = VT102_ROWS_24;
2114 vt102_set_ansi (VT102 * v, int ansi)
2116 v->xn_glitch = ansi ? 0 : 1;
2120 vt102_resize (Context * c, CRT_Pos size)
2122 log_f (c->l, "<size change to %dx%d requested>", size.x, size.y);
2125 size.x = c->v->current_size.x;
2127 size.y = c->v->current_size.y;
2134 if (size.x > VT102_MAX_COLS)
2135 size.x = VT102_MAX_COLS;
2136 if (size.y > VT102_MAX_ROWS)
2137 size.y = VT102_MAX_ROWS;
2139 c->v->current_size = size;
2140 vt102_do_resize (c);
2144 vt102_free (VT102 * v)