4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.52 2008/02/28 16:57:52 james
14 * *** empty log message ***
16 * Revision 1.51 2008/02/28 16:37:16 james
17 * *** empty log message ***
19 * Revision 1.50 2008/02/28 12:12:25 james
20 * *** empty log message ***
22 * Revision 1.49 2008/02/28 11:27:48 james
23 * *** empty log message ***
25 * Revision 1.48 2008/02/27 09:42:53 james
26 * *** empty log message ***
28 * Revision 1.47 2008/02/27 09:42:22 james
29 * *** empty log message ***
31 * Revision 1.46 2008/02/27 01:31:38 james
32 * *** empty log message ***
34 * Revision 1.45 2008/02/27 01:31:14 james
35 * *** empty log message ***
37 * Revision 1.44 2008/02/27 00:54:16 james
38 * *** empty log message ***
40 * Revision 1.43 2008/02/27 00:27:22 james
41 * *** empty log message ***
43 * Revision 1.42 2008/02/26 23:56:12 james
44 * *** empty log message ***
46 * Revision 1.41 2008/02/26 23:23:17 james
47 * *** empty log message ***
49 * Revision 1.40 2008/02/26 19:00:59 james
50 * *** empty log message ***
52 * Revision 1.39 2008/02/26 16:54:06 james
53 * *** empty log message ***
55 * Revision 1.38 2008/02/26 16:53:24 james
56 * *** empty log message ***
58 * Revision 1.37 2008/02/24 12:22:53 james
59 * *** empty log message ***
61 * Revision 1.36 2008/02/24 12:22:42 james
62 * *** empty log message ***
64 * Revision 1.35 2008/02/24 00:42:53 james
65 * *** empty log message ***
67 * Revision 1.34 2008/02/23 11:48:37 james
68 * *** empty log message ***
70 * Revision 1.33 2008/02/22 23:39:27 james
71 * *** empty log message ***
73 * Revision 1.32 2008/02/22 19:12:05 james
74 * *** empty log message ***
76 * Revision 1.31 2008/02/22 17:07:00 james
77 * *** empty log message ***
79 * Revision 1.30 2008/02/22 14:51:54 james
80 * *** empty log message ***
82 * Revision 1.29 2008/02/15 03:32:07 james
83 * *** empty log message ***
85 * Revision 1.28 2008/02/14 10:34:30 james
86 * *** empty log message ***
88 * Revision 1.27 2008/02/14 02:46:45 james
89 * *** empty log message ***
91 * Revision 1.26 2008/02/14 01:55:57 james
92 * *** empty log message ***
94 * Revision 1.25 2008/02/13 16:57:29 james
95 * *** empty log message ***
97 * Revision 1.24 2008/02/13 09:12:21 james
98 * *** empty log message ***
100 * Revision 1.23 2008/02/07 13:26:35 james
101 * *** empty log message ***
103 * Revision 1.22 2008/02/07 13:22:51 james
104 * *** empty log message ***
106 * Revision 1.21 2008/02/07 12:21:16 james
107 * *** empty log message ***
109 * Revision 1.20 2008/02/07 12:16:04 james
110 * *** empty log message ***
112 * Revision 1.19 2008/02/07 11:27:02 james
113 * *** empty log message ***
115 * Revision 1.18 2008/02/07 01:59:25 james
116 * *** empty log message ***
118 * Revision 1.17 2008/02/07 01:58:28 james
119 * *** empty log message ***
121 * Revision 1.16 2008/02/07 01:57:46 james
122 * *** empty log message ***
124 * Revision 1.15 2008/02/07 00:43:27 james
125 * *** empty log message ***
127 * Revision 1.14 2008/02/07 00:40:23 james
128 * *** empty log message ***
130 * Revision 1.13 2008/02/07 00:39:59 james
131 * *** empty log message ***
133 * Revision 1.12 2008/02/07 00:39:13 james
134 * *** empty log message ***
136 * Revision 1.11 2008/02/06 20:26:58 james
137 * *** empty log message ***
139 * Revision 1.10 2008/02/06 17:53:28 james
140 * *** empty log message ***
142 * Revision 1.9 2008/02/06 15:53:22 james
143 * *** empty log message ***
145 * Revision 1.8 2008/02/06 11:49:47 james
146 * *** empty log message ***
148 * Revision 1.7 2008/02/06 11:30:37 james
149 * *** empty log message ***
151 * Revision 1.6 2008/02/05 01:11:46 james
152 * *** empty log message ***
154 * Revision 1.5 2008/02/04 20:23:55 james
155 * *** empty log message ***
157 * Revision 1.4 2008/02/04 05:45:55 james
160 * Revision 1.3 2008/02/04 02:05:06 james
161 * *** empty log message ***
163 * Revision 1.2 2008/02/04 01:32:39 james
164 * *** empty log message ***
166 * Revision 1.1 2008/02/03 23:36:41 james
167 * *** empty log message ***
178 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
180 vt100|vt100-am|dec vt100 (w/advanced video):\
182 :co#80:it#8:li#24:vt#3:\
183 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
185 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
186 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
187 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
188 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
189 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
190 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
191 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
192 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
193 :us=\E[4m:tc=vt100+fnkeys:
195 vt100+fnkeys|dec vt100 numeric keypad:\
196 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
199 vt100+pfkeys|dec vt100 numeric keypad:\
200 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
202 vt100+keypad|dec vt100 numeric keypad no fkeys:\
203 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
208 so the parser needs to be able to at least do
270 ansi|ansi/pc-term compatible with color:\
271 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
272 :tc=klone+sgr:tc=ansi-m:
273 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
275 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
276 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
277 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
278 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
279 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
280 :s3=\E+B:ta=\E[I:tc=pcansi-m:
281 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
284 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
285 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
286 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
287 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
289 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
290 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
291 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
292 klone+acs|alternate character set for ansi.sys displays:\
293 :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:\
294 :ae=\E[10m:as=\E[11m:
295 ecma+color|color control for ECMA-48-compatible terminals:\
297 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
300 Ignoreing ones in the VT102 spec
330 #define TABLE_LENGTH 128
334 static char terminal_id[] = "vt102";
336 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
337 int vt102_cmd_length[TABLE_LENGTH] = {
347 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
349 int vt102_cmd_termination[TABLE_LENGTH] = {
371 if ((c >= 'a') && (c <= 'z'))
373 if ((c >= 'A') && (c <= 'Z'))
381 ctrl_chr (int ch, int term)
383 if ((term > 0) && (ch == term))
387 if ((ch > 0) && (ch < 040))
394 in_margins (VT102 * v, CRT_Pos p)
396 if (v->pos.x < v->top_margin.x)
398 if (v->pos.y < v->top_margin.y)
401 if (v->pos.x > v->bottom_margin.x)
403 if (v->pos.y > v->bottom_margin.y)
410 vt102_crt_update (Context * c)
416 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
418 if (v->current_line.y != v->pos.y)
420 vt102_log_line (c, v->current_line.y);
421 v->current_line = v->pos;
425 cmd_show_status (c->d, c);
429 vt102_do_resize (Context * c)
434 v->crt.size = v->current_size;
436 v->screen_end = v->current_size;
439 v->top_margin = v->screen_start;
440 v->bottom_margin = v->screen_end;
441 vt102_cursor_home (v);
445 tty_winch (c->t, v->current_size);
447 log_f (c->l, "<size now %dx%d>", v->current_size.x, v->current_size.y);
448 vt102_crt_update (c);
453 vt102_log_line (Context * c, int line)
455 CRT_Pos e = { c->v->current_size.x - 1, line };
456 CRT_Pos p = { 0, line };
457 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
463 for (; e.x > 0; --e.x)
465 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
469 for (; p.x <= e.x; ++p.x)
471 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
474 logptr += utf8_encode (logptr, ch);
478 c->l->log (c->l, logbuf);
481 /*Called for every upward scroll with same args*/
483 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
485 /*Only log if it scrolls off the top*/
490 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
494 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
509 vt102_cursor_normalize (VT102 * v)
511 CRT_Pos *top, *bottom;
513 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
515 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
519 vt102_clip_cursor (v, v->screen_start, v->screen_end);
525 vt102_cursor_carriage_return (VT102 * v)
527 /*FISH*/ v->pos.x = v->top_margin.x;
532 vt102_cursor_advance_line (Context * c)
535 int couldscroll = in_margins (v, v->pos);
537 /*have wraped off end of last line in scrolling region */
538 /* (|| not necessary, but shuts compiler up */
539 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
542 vt102_log_line (c, v->pos.y);
544 vt102_history (c, v->top_margin, v->bottom_margin);
546 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
550 if (v->pos.y != v->screen_end.y)
557 vt102_cursor_retreat_line (Context * c)
560 int couldscroll = in_margins (v, v->pos);
562 /*have wraped off end of last line in scrolling region */
563 /* (|| not necessary, but shuts compiler up */
564 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
567 vt102_log_line (c, v->pos.y);
569 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
573 if (v->pos.y != v->screen_start.y)
580 vt102_do_pending_wrap (Context * c)
583 int couldscroll = in_margins (v, v->pos);
584 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
586 if (!v->pending_wrap)
590 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
591 v->pos.y, autowrap, couldscroll);
594 /*End of line but no autowrap, nothing to do*/
598 /*End of screen and not allowed to scroll, nothing to do*/
599 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
604 v->pos.x = v->top_margin.x;
611 vt102_cursor_advance_line (c);
615 vt102_cursor_advance (Context * c)
619 if (v->pos.x < v->bottom_margin.x)
627 if (!c->v->xn_glitch)
628 vt102_do_pending_wrap (c);
634 vt102_cursor_retreat (VT102 * v)
636 if (v->pos.x != v->top_margin.x)
645 vt102_reset_tabs (VT102 * v)
649 memset (v->tabs, 0, sizeof (v->tabs));
651 for (i = 0; i < VT102_MAX_COLS; i += 8)
657 vt102_cursor_advance_tab (VT102 * v)
659 if (v->pos.x == v->bottom_margin.x)
661 while (v->pos.x < v->bottom_margin.x)
664 if (v->tabs[v->pos.x])
671 vt102_cursor_retreat_tab (VT102 * v)
673 if (v->pos.x == v->top_margin.x)
675 while (v->pos.x > v->top_margin.x)
678 if (v->tabs[v->pos.x])
684 vt102_cursor_home (VT102 * v)
686 v->pos = v->top_margin;
687 vt102_cursor_normalize (v);
692 vt102_cursor_absolute (VT102 * v, int x, int y)
694 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
696 v->pos.x = x + v->top_margin.x;
697 v->pos.y = y + v->top_margin.y;
704 vt102_cursor_normalize (v);
708 vt102_cursor_relative (VT102 * v, int x, int y)
712 vt102_cursor_normalize (v);
719 vt102_delete_from_line (VT102 * v, CRT_Pos p)
721 int n = v->bottom_margin.x - p.x;
729 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
730 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
733 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
734 /*But not attr due to vt102 bug*/
738 vt102_insert_into_line (VT102 * v, CRT_Pos p)
740 int n = v->bottom_margin.x - p.x;
748 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
749 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
752 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
753 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
754 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
760 vt102_change_mode (Context * c, int private, char *ns, int set)
777 if (m >= VT102_NMODES)
782 v->private_modes[m] = set;
785 case VT102_PRIVATE_MODE_CURSOR_MODE:
786 if (v->application_keypad_mode)
787 v->private_modes[m] = 0;
789 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
790 v->private_modes[m], set);
793 case VT102_PRIVATE_MODE_ORIGIN_MODE:
794 vt102_cursor_home (v);
796 case VT102_PRIVATE_MODE_132COLS:
797 /* We don't implement 132 col mode - yet */
801 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
813 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
818 vt102_parse_mode_string (Context * c, char *buf, int len)
822 char last = buf[len - 1];
826 memset (num, 0, sizeof (num));
827 o = sizeof (num) - 1;
845 vt102_change_mode (c, private, &num[o], last == 'h');
846 memset (num, 0, sizeof (num));
847 o = sizeof (num) - 1;
862 vt102_change_mode (c, private, &num[o], last == 'h');
868 vt102_change_attr (VT102 * v, char *na)
885 v->attr = CRT_ATTR_NORMAL;
886 v->color = CRT_COLOR_NORMAL;
889 v->attr |= CRT_ATTR_BOLD;
893 v->attr &= ~CRT_ATTR_BOLD;
896 v->attr |= CRT_ATTR_UNDERLINE;
899 v->attr &= ~CRT_ATTR_UNDERLINE;
902 v->attr |= CRT_ATTR_BLINK;
905 v->attr &= ~CRT_ATTR_BLINK;
908 v->attr |= CRT_ATTR_REVERSE;
911 v->attr &= ~CRT_ATTR_REVERSE;
921 v->color &= ~CRT_COLOR_FG_MASK;
922 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
932 v->color &= ~CRT_COLOR_FG_MASK;
935 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
940 v->color &= ~CRT_COLOR_FG_MASK;
942 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
952 v->color &= ~CRT_COLOR_BG_MASK;
953 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
963 v->color &= ~CRT_COLOR_BG_MASK;
966 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
971 v->color &= ~CRT_COLOR_BG_MASK;
973 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
979 fprintf (stderr, "unhandled SGR %d\n", a);
987 vt102_parse_attr_string (VT102 * v, char *buf, int len)
990 char last = buf[len - 1];
994 memset (num, 0, sizeof (num));
995 o = sizeof (num) - 1;
1006 vt102_change_attr (v, &num[o]);
1007 memset (num, 0, sizeof (num));
1008 o = sizeof (num) - 1;
1022 vt102_change_attr (v, &num[o]);
1026 vt102_save_state (VT102 * v)
1028 v->saved.pos = v->pos;
1029 v->saved.attr = v->attr;
1030 v->saved.color = v->color;
1031 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
1035 vt102_restore_state (VT102 * v)
1037 v->pos = v->saved.pos;
1038 v->attr = v->saved.attr;
1039 v->color = v->saved.color;
1040 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
1041 vt102_cursor_normalize (v);
1042 v->pending_wrap = 0;
1046 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
1050 vt102_do_pending_wrap (c);
1053 if (v->modes[VT102_MODE_INSERT])
1054 vt102_insert_into_line (v, v->pos);
1056 v->last_reg_char = ch;
1059 if (ch < VT102_CHARSET_SIZE)
1062 if ((cs = vt102_charset_c0[ch]))
1066 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1071 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1072 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1073 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1074 vt102_cursor_advance (c);
1078 vt102_send_id (Context * c, char *buf)
1083 int l = strlen (buf);
1084 c->t->xmit (c->t, buf, l);
1089 vt102_scs (Context * c, int g, int s)
1092 int cs = VT102_CSID_US;
1123 vt102_parse_csi (Context * c, char *buf, int len)
1127 char *arg = buf + 1;
1134 last = buf[len - 1];
1155 vt102_insert_into_line (v, v->pos);
1158 vt102_cursor_relative (v, 0, -narg);
1162 vt102_cursor_relative (v, 0, narg);
1166 vt102_cursor_relative (v, narg, 0);
1169 vt102_cursor_relative (v, -narg, 0);
1172 vt102_cursor_relative (v, 0, narg);
1173 vt102_cursor_carriage_return (v);
1176 vt102_cursor_relative (v, 0, -narg);
1177 vt102_cursor_carriage_return (v);
1180 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1189 ptr = index (arg, ';');
1191 x = atoi (ptr + 1) - 1;
1195 vt102_cursor_absolute (v, x, y);
1200 vt102_cursor_advance_tab (c->v);
1204 narg = 0; /*Different default */
1208 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1211 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1214 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1221 CRT_Pos ls = { 0, v->pos.y };
1222 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1224 narg = 0; /*Different default */
1229 crt_erase (&v->crt, v->pos, le, 1, v->color);
1232 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1235 crt_erase (&v->crt, ls, le, 1, v->color);
1242 if ((v->pos.y >= v->top_margin.y)
1243 && (v->pos.y <= v->bottom_margin.y))
1246 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1252 if ((v->pos.y >= v->top_margin.y)
1253 && (v->pos.y <= v->bottom_margin.y))
1257 vt102_history (c, v->pos, v->bottom_margin);
1258 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1265 vt102_delete_from_line (v, v->pos);
1270 vt102_history (c, v->top_margin, v->bottom_margin);
1271 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1277 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1282 CRT_Pos end = v->pos;
1287 if (end.x > v->bottom_margin.x)
1288 end.x = v->bottom_margin.x;
1290 crt_erase (&v->crt, v->pos, end, 1, v->color);
1295 vt102_cursor_retreat_tab (c->v);
1298 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1302 vt102_regular_char (c, v, v->last_reg_char);
1305 vt102_send_id (c, "\033[?2c");
1308 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1312 narg = 0; /*Different default */
1317 v->tabs[v->pos.x] = 0;
1319 case 2: //FIXME: - LA120 says current line only WTF?
1321 memset (v->tabs, 0, sizeof (v->tabs));
1326 case 'i': //Printer commands
1331 vt102_parse_mode_string (c, &buf[1], len - 1);
1336 vt102_parse_attr_string (v, &buf[1], len - 1);
1338 case 'n': //Device status report
1342 vt102_send_id (c, "\033[0n");
1350 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1351 c->t->xmit (c->t, buf, i);
1355 //?15n printer status
1356 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1360 case 'q': //Load LED on off
1363 v->top_margin = v->screen_start;
1364 v->bottom_margin = v->screen_end;
1366 if ((len > 2) && (ptr = index (arg, ';')))
1369 v->top_margin.y = narg - 1;
1370 v->bottom_margin.y = atoi (ptr) - 1;
1373 if (v->top_margin.y < v->screen_start.y)
1374 v->top_margin.y = v->screen_start.y;
1375 if (v->top_margin.y > v->screen_end.y)
1376 v->top_margin.y = v->screen_end.y;
1377 if (v->bottom_margin.y < v->screen_start.y)
1378 v->bottom_margin.y = v->screen_start.y;
1379 if (v->bottom_margin.y > v->screen_end.y)
1380 v->bottom_margin.y = v->screen_end.y;
1382 vt102_cursor_home (v);
1385 v->saved.pos = v->pos;
1388 v->pos = v->saved.pos;
1389 vt102_cursor_normalize (v);
1390 v->pending_wrap = 0;
1392 case 'y': //Invoke confidence test
1394 case 'z': //request terminal parameters
1398 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1405 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1415 vt102_parse_esc (Context * c)
1418 VT102_parser *p = &v->parser;
1421 p->cmd_buf[p->cmd_ptr] = 0;
1422 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1423 v->pos.y, v->pending_wrap,
1424 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1427 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1431 switch (p->cmd_buf[0])
1434 vt102_cursor_relative (v, 0, -1);
1437 vt102_cursor_relative (v, 0, 1);
1440 vt102_cursor_relative (v, 1, 0);
1443 vt102_cursor_relative (v, -1, 0);
1447 v->g[1] = VT102_CSID_VT52;
1451 v->g[0] = VT102_CSID_US;
1454 vt102_cursor_absolute (v, 0, 0);
1457 vt102_cursor_retreat_line (c);
1460 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1464 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1465 crt_erase (&v->crt, v->pos, le, 1, v->color);
1469 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1471 case 'V': //Print current line
1473 case 'W': //Printer on
1475 case 'X': //printer off
1477 case ']': //print screen
1480 vt102_send_id (c, "\033/K");
1482 case '^': //Autoprint on
1484 case '_': //Autoprint off
1487 v->application_keypad_mode = 1;
1490 v->application_keypad_mode = 0;
1500 /*If you edit this switch don't forget to edit the length and termination tables*/
1501 switch (p->cmd_buf[0])
1504 vt102_cursor_advance_line (c);
1508 vt102_cursor_advance_line (c);
1509 v->pos.x = v->top_margin.x;
1510 vt102_cursor_normalize (v);
1511 v->pending_wrap = 0;
1514 v->tabs[v->pos.x]++;
1517 vt102_cursor_retreat_line (c);
1519 case 'N': //select G2 for one char
1521 case 'O': //select G3 for one char
1524 vt102_send_id (c, terminal_id);
1530 v->application_keypad_mode = 1;
1533 v->application_keypad_mode = 0;
1537 switch (p->cmd_buf[1])
1539 case '3': //top of double height line
1540 case '4': //bottom of double height line
1541 case '5': //single width line
1542 case '6': //double width line
1548 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1550 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1551 v->crt.screen[i].chr = 'E';
1557 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1558 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1563 /*Set ansi mode - ignored */
1567 vt102_save_state (v);
1570 vt102_restore_state (v);
1572 case ']': /*Set various titles */
1576 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1578 case '(': /*Charsets */
1583 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1586 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1587 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1592 p->cmd_buf[p->cmd_ptr] = 0;
1594 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1600 vt102_status_line (VT102 * v, char *str)
1603 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1605 for (i = 0; i < v->current_size.x; ++i)
1607 ca->attr = CRT_ATTR_REVERSE;
1608 ca->color = CRT_COLOR_NORMAL;
1609 ca->chr = *str ? *str : ' ';
1615 for (; i < VT102_MAX_COLS; ++i)
1617 ca->attr = CRT_ATTR_NORMAL;
1618 ca->color = CRT_COLOR_NORMAL;
1627 vt102_parser_reset (VT102_parser * p)
1630 p->cmd_more_bytes = 0;
1631 p->cmd_termination = 0;
1637 vt102_reset_state (Context * c)
1640 vt102_parser_reset (&v->parser);
1642 v->attr = CRT_ATTR_NORMAL;
1643 v->color = CRT_COLOR_NORMAL;
1645 v->application_keypad_mode = 0;
1647 v->current_size = v->original_size;
1649 vt102_do_resize (c);
1651 memset (v->modes, 0, VT102_NMODES);
1652 memset (v->private_modes, 0, VT102_NMODES);
1654 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1655 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1656 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1657 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1658 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1660 vt102_reset_tabs (v);
1662 v->g[0] = v->g[1] = VT102_CSID_US;
1667 pre_parse_cmd (int ch, VT102_parser * p)
1669 if (ch > TABLE_LENGTH)
1674 p->cmd_more_bytes = 0;
1677 p->cmd_termination = vt102_cmd_termination[ch];
1678 if (p->cmd_termination)
1684 p->cmd_more_bytes = vt102_cmd_length[ch];
1686 if (p->cmd_more_bytes)
1694 vt102_parse_char (Context * c, int ch)
1697 VT102_parser *p = &v->parser;
1700 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1701 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1702 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1705 /* Turn anything non-ascii into '?' */
1707 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1715 if (p->dca_ptr == 2)
1720 if (ch == SYM_CHAR_RESET)
1722 vt102_reset_state (c);
1724 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1726 p->cmd_buf[p->cmd_ptr++] = ch;
1727 if (p->cmd_ptr == VT102_CMD_LEN)
1729 if (p->cmd_more_bytes)
1731 p->cmd_more_bytes--;
1734 if (!p->cmd_more_bytes == 1)
1738 switch (p->cmd_termination)
1743 if (p->cmd_termination == ch)
1754 vt102_parse_esc (c);
1755 p->cmd_more_bytes = 0;
1756 p->cmd_termination = 0;
1759 else if (p->in_escape && !ctrl_chr (ch, 0))
1762 p->cmd_buf[p->cmd_ptr++] = ch;
1765 pre_parse_cmd (ch, p);
1768 vt102_parse_esc (c);
1770 else if (ch == 0x9b) /*One byte CSI */
1773 p->cmd_buf[p->cmd_ptr++] = '[';
1775 pre_parse_cmd (ch, p);
1782 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1783 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1798 vt102_send_id (c, terminal_id);
1808 vt102_cursor_retreat (c->v);
1811 vt102_cursor_advance_tab (c->v);
1816 vt102_cursor_advance_line (c);
1817 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1820 vt102_cursor_carriage_return (v);
1855 /*regular character */ default:
1856 vt102_regular_char (c, v, ch);
1861 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1866 vt102_crt_update (c);
1872 vt102_send (Context * c, uint8_t key)
1880 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1882 if ((key > 31) && (key < 127))
1884 c->t->xmit (c->t, &key, 1);
1903 c->t->xmit (c->t, &key, 1);
1906 c->t->xmit (c->t, &key, 1);
1907 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1910 c->t->xmit (c->t, &ch, 1);
1926 c->t->xmit (c->t, &key, 1);
1934 c->t->xmit (c->t, &key, 1);
1945 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1947 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1948 c->t->xmit (c->t, &buf, sizeof (buf));
1952 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1953 c->t->xmit (c->t, &buf, sizeof (buf));
1971 if (c->v->application_keypad_mode)
1973 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1974 c->t->xmit (c->t, &buf, sizeof (buf));
1978 static char kpoff[KEY_NUM] = {
1997 c->t->xmit (c->t, &kpoff[key], 1);
2001 if (c->v->application_keypad_mode)
2003 uint8_t buf[] = { 033, 'O', 'M' };
2004 c->t->xmit (c->t, &buf, sizeof (buf));
2009 c->t->xmit (c->t, &ch, 1);
2010 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
2013 c->t->xmit (c->t, &ch, 1);
2022 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
2023 c->t->xmit (c->t, &buf, sizeof (buf));
2031 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
2032 c->t->xmit (c->t, &buf, sizeof (buf));
2040 vt102_reset (Context * c)
2043 VT102_parser *p = &v->parser;
2047 v->current_line = v->pos;
2048 v->pending_wrap = 0;
2050 v->screen_start.x = 0;
2051 v->screen_start.y = 0;
2052 v->current_size = v->original_size;
2053 v->crt.size = v->current_size;
2055 v->screen_end = v->current_size;
2059 vt102_cursor_home (v);
2060 vt102_status_line (v, "");
2062 vt102_reset_tabs (v);
2063 v->current_line = v->pos;
2065 vt102_parser_reset (p);
2066 vt102_reset_state (c);
2068 vt102_save_state (v);
2070 v->last_reg_char = ' ';
2074 vt102_new (CRT_Pos * size)
2078 v = (VT102 *) malloc (sizeof (VT102));
2085 v->original_size = *size;
2087 if (v->original_size.x < 1)
2088 v->original_size.x = 1;
2089 if (v->original_size.y < 1)
2090 v->original_size.y = 1;
2092 if (v->original_size.x > VT102_MAX_COLS)
2093 v->original_size.x = VT102_MAX_COLS;
2094 if (v->original_size.y > VT102_MAX_ROWS)
2095 v->original_size.y = VT102_MAX_ROWS;
2100 v->original_size.x = VT102_COLS_80;
2101 v->original_size.y = VT102_ROWS_24;
2108 vt102_set_ansi (VT102 * v, int ansi)
2110 v->xn_glitch = ansi ? 0 : 1;
2114 vt102_resize (Context * c, CRT_Pos size)
2116 log_f (c->l, "<size change to %dx%d requested>", size.x, size.y);
2119 size.x = c->v->current_size.x;
2121 size.y = c->v->current_size.y;
2128 if (size.x > VT102_MAX_COLS)
2129 size.x = VT102_MAX_COLS;
2130 if (size.y > VT102_MAX_ROWS)
2131 size.y = VT102_MAX_ROWS;
2133 c->v->current_size = size;
2134 vt102_do_resize (c);
2138 vt102_free (VT102 * v)