4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.46 2008/02/27 01:31:38 james
14 * *** empty log message ***
16 * Revision 1.45 2008/02/27 01:31:14 james
17 * *** empty log message ***
19 * Revision 1.44 2008/02/27 00:54:16 james
20 * *** empty log message ***
22 * Revision 1.43 2008/02/27 00:27:22 james
23 * *** empty log message ***
25 * Revision 1.42 2008/02/26 23:56:12 james
26 * *** empty log message ***
28 * Revision 1.41 2008/02/26 23:23:17 james
29 * *** empty log message ***
31 * Revision 1.40 2008/02/26 19:00:59 james
32 * *** empty log message ***
34 * Revision 1.39 2008/02/26 16:54:06 james
35 * *** empty log message ***
37 * Revision 1.38 2008/02/26 16:53:24 james
38 * *** empty log message ***
40 * Revision 1.37 2008/02/24 12:22:53 james
41 * *** empty log message ***
43 * Revision 1.36 2008/02/24 12:22:42 james
44 * *** empty log message ***
46 * Revision 1.35 2008/02/24 00:42:53 james
47 * *** empty log message ***
49 * Revision 1.34 2008/02/23 11:48:37 james
50 * *** empty log message ***
52 * Revision 1.33 2008/02/22 23:39:27 james
53 * *** empty log message ***
55 * Revision 1.32 2008/02/22 19:12:05 james
56 * *** empty log message ***
58 * Revision 1.31 2008/02/22 17:07:00 james
59 * *** empty log message ***
61 * Revision 1.30 2008/02/22 14:51:54 james
62 * *** empty log message ***
64 * Revision 1.29 2008/02/15 03:32:07 james
65 * *** empty log message ***
67 * Revision 1.28 2008/02/14 10:34:30 james
68 * *** empty log message ***
70 * Revision 1.27 2008/02/14 02:46:45 james
71 * *** empty log message ***
73 * Revision 1.26 2008/02/14 01:55:57 james
74 * *** empty log message ***
76 * Revision 1.25 2008/02/13 16:57:29 james
77 * *** empty log message ***
79 * Revision 1.24 2008/02/13 09:12:21 james
80 * *** empty log message ***
82 * Revision 1.23 2008/02/07 13:26:35 james
83 * *** empty log message ***
85 * Revision 1.22 2008/02/07 13:22:51 james
86 * *** empty log message ***
88 * Revision 1.21 2008/02/07 12:21:16 james
89 * *** empty log message ***
91 * Revision 1.20 2008/02/07 12:16:04 james
92 * *** empty log message ***
94 * Revision 1.19 2008/02/07 11:27:02 james
95 * *** empty log message ***
97 * Revision 1.18 2008/02/07 01:59:25 james
98 * *** empty log message ***
100 * Revision 1.17 2008/02/07 01:58:28 james
101 * *** empty log message ***
103 * Revision 1.16 2008/02/07 01:57:46 james
104 * *** empty log message ***
106 * Revision 1.15 2008/02/07 00:43:27 james
107 * *** empty log message ***
109 * Revision 1.14 2008/02/07 00:40:23 james
110 * *** empty log message ***
112 * Revision 1.13 2008/02/07 00:39:59 james
113 * *** empty log message ***
115 * Revision 1.12 2008/02/07 00:39:13 james
116 * *** empty log message ***
118 * Revision 1.11 2008/02/06 20:26:58 james
119 * *** empty log message ***
121 * Revision 1.10 2008/02/06 17:53:28 james
122 * *** empty log message ***
124 * Revision 1.9 2008/02/06 15:53:22 james
125 * *** empty log message ***
127 * Revision 1.8 2008/02/06 11:49:47 james
128 * *** empty log message ***
130 * Revision 1.7 2008/02/06 11:30:37 james
131 * *** empty log message ***
133 * Revision 1.6 2008/02/05 01:11:46 james
134 * *** empty log message ***
136 * Revision 1.5 2008/02/04 20:23:55 james
137 * *** empty log message ***
139 * Revision 1.4 2008/02/04 05:45:55 james
142 * Revision 1.3 2008/02/04 02:05:06 james
143 * *** empty log message ***
145 * Revision 1.2 2008/02/04 01:32:39 james
146 * *** empty log message ***
148 * Revision 1.1 2008/02/03 23:36:41 james
149 * *** empty log message ***
160 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
162 vt100|vt100-am|dec vt100 (w/advanced video):\
164 :co#80:it#8:li#24:vt#3:\
165 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
167 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
168 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
169 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
170 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
171 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
172 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
173 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
174 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
175 :us=\E[4m:tc=vt100+fnkeys:
177 vt100+fnkeys|dec vt100 numeric keypad:\
178 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
181 vt100+pfkeys|dec vt100 numeric keypad:\
182 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
184 vt100+keypad|dec vt100 numeric keypad no fkeys:\
185 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
190 so the parser needs to be able to at least do
252 ansi|ansi/pc-term compatible with color:\
253 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
254 :tc=klone+sgr:tc=ansi-m:
255 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
257 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
258 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
259 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
260 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
261 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
262 :s3=\E+B:ta=\E[I:tc=pcansi-m:
263 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
266 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
267 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
268 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
269 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
271 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
272 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
273 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
274 klone+acs|alternate character set for ansi.sys displays:\
275 :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:\
276 :ae=\E[10m:as=\E[11m:
277 ecma+color|color control for ECMA-48-compatible terminals:\
279 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
282 Ignoreing ones in the VT102 spec
312 #define TABLE_LENGTH 128
316 static char terminal_id[] = "vt102";
318 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
319 int vt102_cmd_length[TABLE_LENGTH] = {
329 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
331 int vt102_cmd_termination[TABLE_LENGTH] = {
353 if ((c >= 'a') && (c <= 'z'))
355 if ((c >= 'A') && (c <= 'Z'))
363 ctrl_chr (int ch, int term)
365 if ((term > 0) && (ch == term))
369 if ((ch > 0) && (ch < 040))
376 in_margins (VT102 * v, CRT_Pos p)
378 if (v->pos.x < v->top_margin.x)
380 if (v->pos.y < v->top_margin.y)
383 if (v->pos.x > v->bottom_margin.x)
385 if (v->pos.y > v->bottom_margin.y)
392 vt102_log_line (Context * c, int line)
394 CRT_Pos e = { c->v->current_width - 1, line };
395 CRT_Pos p = { 0, line };
396 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
402 for (; e.x > 0; --e.x)
404 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
408 for (; p.x <= e.x; ++p.x)
410 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
413 logptr += utf8_encode (logptr, ch);
417 c->l->log (c->l, logbuf);
420 /*Called for every upward scroll with same args*/
422 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
424 /*Only log if it scrolls off the top*/
429 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
433 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
448 vt102_cursor_normalize (VT102 * v)
450 CRT_Pos *top, *bottom;
452 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
454 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
458 vt102_clip_cursor (v, v->screen_start, v->screen_end);
464 vt102_cursor_carriage_return (VT102 * v)
466 /*FISH*/ v->pos.x = v->top_margin.x;
471 vt102_cursor_advance_line (Context * c)
474 int couldscroll = in_margins (v, v->pos);
476 /*have wraped off end of last line in scrolling region */
477 /* (|| not necessary, but shuts compiler up */
478 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
481 vt102_log_line (c, v->pos.y);
483 vt102_history (c, v->top_margin, v->bottom_margin);
485 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
489 if (v->pos.y != v->screen_end.y)
496 vt102_cursor_retreat_line (Context * c)
499 int couldscroll = in_margins (v, v->pos);
501 /*have wraped off end of last line in scrolling region */
502 /* (|| not necessary, but shuts compiler up */
503 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
506 vt102_log_line (c, v->pos.y);
508 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
512 if (v->pos.y != v->screen_start.y)
519 vt102_do_pending_wrap (Context * c)
522 int couldscroll = in_margins (v, v->pos);
523 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
525 if (!v->pending_wrap)
529 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
530 v->pos.y, autowrap, couldscroll);
533 /*End of line but no autowrap, nothing to do*/
537 /*End of screen and not allowed to scroll, nothing to do*/
538 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
543 v->pos.x = v->top_margin.x;
550 vt102_cursor_advance_line (c);
554 vt102_cursor_advance (Context * c)
558 if (v->pos.x < v->bottom_margin.x)
566 if (!c->v->xn_glitch)
567 vt102_do_pending_wrap (c);
573 vt102_cursor_retreat (VT102 * v)
575 if (v->pos.x != v->top_margin.x)
584 vt102_reset_tabs (VT102 * v)
588 memset (v->tabs, 0, sizeof (v->tabs));
590 for (i = 0; i < VT102_MAX_COLS; i += 8)
596 vt102_cursor_advance_tab (VT102 * v)
598 if (v->pos.x == v->bottom_margin.x)
600 while (v->pos.x < v->bottom_margin.x)
603 if (v->tabs[v->pos.x])
610 vt102_cursor_retreat_tab (VT102 * v)
612 if (v->pos.x == v->top_margin.x)
614 while (v->pos.x > v->top_margin.x)
617 if (v->tabs[v->pos.x])
623 vt102_cursor_home (VT102 * v)
625 v->pos = v->top_margin;
626 vt102_cursor_normalize (v);
631 vt102_cursor_absolute (VT102 * v, int x, int y)
633 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
635 v->pos.x = x + v->top_margin.x;
636 v->pos.y = y + v->top_margin.y;
643 vt102_cursor_normalize (v);
647 vt102_cursor_relative (VT102 * v, int x, int y)
651 vt102_cursor_normalize (v);
658 vt102_delete_from_line (VT102 * v, CRT_Pos p)
660 int n = v->bottom_margin.x - p.x;
668 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
669 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
672 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
673 /*But not attr due to vt102 bug*/
677 vt102_insert_into_line (VT102 * v, CRT_Pos p)
679 int n = v->bottom_margin.x - p.x;
687 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
688 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
691 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
692 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
693 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
699 vt102_change_mode (VT102 * v, int private, char *ns, int set)
715 if (m >= VT102_NMODES)
720 v->private_modes[m] = set;
723 case VT102_PRIVATE_MODE_CURSOR_MODE:
724 if (v->application_keypad_mode)
725 v->private_modes[m] = 0;
727 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
728 v->private_modes[m], set);
731 case VT102_PRIVATE_MODE_ORIGIN_MODE:
732 vt102_cursor_home (v);
734 case VT102_PRIVATE_MODE_132COLS:
735 /* We don't implement 132 col mode - yet */
739 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
742 v->crt.width = v->current_width;
743 v->screen_end.x = v->current_width - 1;
744 v->top_margin = v->screen_start;
745 v->bottom_margin = v->screen_end;
746 vt102_cursor_home (v);
758 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
763 vt102_parse_mode_string (VT102 * v, char *buf, int len)
766 char last = buf[len - 1];
770 memset (num, 0, sizeof (num));
771 o = sizeof (num) - 1;
789 vt102_change_mode (v, private, &num[o], last == 'h');
790 memset (num, 0, sizeof (num));
791 o = sizeof (num) - 1;
806 vt102_change_mode (v, private, &num[o], last == 'h');
812 vt102_change_attr (VT102 * v, char *na)
829 v->attr = CRT_ATTR_NORMAL;
830 v->color = CRT_COLOR_NORMAL;
833 v->attr |= CRT_ATTR_BOLD;
837 v->attr &= ~CRT_ATTR_BOLD;
840 v->attr |= CRT_ATTR_UNDERLINE;
843 v->attr &= ~CRT_ATTR_UNDERLINE;
846 v->attr |= CRT_ATTR_BLINK;
849 v->attr &= ~CRT_ATTR_BLINK;
852 v->attr |= CRT_ATTR_REVERSE;
855 v->attr &= ~CRT_ATTR_REVERSE;
865 v->color &= ~CRT_COLOR_FG_MASK;
866 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
876 v->color &= ~CRT_COLOR_FG_MASK;
879 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
884 v->color &= ~CRT_COLOR_FG_MASK;
886 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
896 v->color &= ~CRT_COLOR_BG_MASK;
897 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
907 v->color &= ~CRT_COLOR_BG_MASK;
910 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
915 v->color &= ~CRT_COLOR_BG_MASK;
917 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
923 fprintf (stderr, "unhandled SGR %d\n", a);
931 vt102_parse_attr_string (VT102 * v, char *buf, int len)
934 char last = buf[len - 1];
938 memset (num, 0, sizeof (num));
939 o = sizeof (num) - 1;
950 vt102_change_attr (v, &num[o]);
951 memset (num, 0, sizeof (num));
952 o = sizeof (num) - 1;
966 vt102_change_attr (v, &num[o]);
970 vt102_save_state (VT102 * v)
972 v->saved.pos = v->pos;
973 v->saved.attr = v->attr;
974 v->saved.color = v->color;
975 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
979 vt102_restore_state (VT102 * v)
981 v->pos = v->saved.pos;
982 v->attr = v->saved.attr;
983 v->color = v->saved.color;
984 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
985 vt102_cursor_normalize (v);
990 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
994 vt102_do_pending_wrap (c);
997 if (v->modes[VT102_MODE_INSERT])
998 vt102_insert_into_line (v, v->pos);
1000 v->last_reg_char = ch;
1003 if (ch < VT102_CHARSET_SIZE)
1006 if ((cs = vt102_charset_c0[ch]))
1010 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1015 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1016 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1017 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1018 vt102_cursor_advance (c);
1022 vt102_send_id (Context * c, char *buf)
1027 int l = strlen (buf);
1028 c->t->xmit (c->t, buf, l);
1033 vt102_scs (Context * c, int g, int s)
1036 int cs = VT102_CSID_US;
1067 vt102_parse_csi (Context * c, char *buf, int len)
1071 char *arg = buf + 1;
1078 last = buf[len - 1];
1099 vt102_insert_into_line (v, v->pos);
1102 vt102_cursor_relative (v, 0, -narg);
1106 vt102_cursor_relative (v, 0, narg);
1110 vt102_cursor_relative (v, narg, 0);
1113 vt102_cursor_relative (v, -narg, 0);
1116 vt102_cursor_relative (v, 0, narg);
1117 vt102_cursor_carriage_return (v);
1120 vt102_cursor_relative (v, 0, -narg);
1121 vt102_cursor_carriage_return (v);
1124 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1133 ptr = index (arg, ';');
1135 x = atoi (ptr + 1) - 1;
1139 vt102_cursor_absolute (v, x, y);
1144 vt102_cursor_advance_tab (c->v);
1148 narg = 0; /*Different default */
1152 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1155 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1158 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1165 CRT_Pos ls = { 0, v->pos.y };
1166 CRT_Pos le = { v->current_width - 1, v->pos.y };
1168 narg = 0; /*Different default */
1173 crt_erase (&v->crt, v->pos, le, 1, v->color);
1176 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1179 crt_erase (&v->crt, ls, le, 1, v->color);
1186 if ((v->pos.y >= v->top_margin.y)
1187 && (v->pos.y <= v->bottom_margin.y))
1190 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1196 if ((v->pos.y >= v->top_margin.y)
1197 && (v->pos.y <= v->bottom_margin.y))
1201 vt102_history (c, v->pos, v->bottom_margin);
1202 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1209 vt102_delete_from_line (v, v->pos);
1214 vt102_history (c, v->top_margin, v->bottom_margin);
1215 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1221 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1226 CRT_Pos end = v->pos;
1231 if (end.x > v->bottom_margin.x)
1232 end.x = v->bottom_margin.x;
1234 crt_erase (&v->crt, v->pos, end, 1, v->color);
1239 vt102_cursor_retreat_tab (c->v);
1242 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1246 vt102_regular_char (c, v, v->last_reg_char);
1249 vt102_send_id (c, "\033[?2c");
1252 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1256 narg = 0; /*Different default */
1261 v->tabs[v->pos.x] = 0;
1263 case 2: //FIXME: - LA120 says current line only WTF?
1265 memset (v->tabs, 0, sizeof (v->tabs));
1270 case 'i': //Printer commands
1275 vt102_parse_mode_string (v, &buf[1], len - 1);
1280 vt102_parse_attr_string (v, &buf[1], len - 1);
1282 case 'n': //Device status report
1286 vt102_send_id (c, "\033[0n");
1294 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1295 c->t->xmit (c->t, buf, i);
1299 //?15n printer status
1300 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1304 case 'q': //Load LED on off
1307 v->top_margin = v->screen_start;
1308 v->bottom_margin = v->screen_end;
1310 if ((len > 2) && (ptr = index (arg, ';')))
1313 v->top_margin.y = narg - 1;
1314 v->bottom_margin.y = atoi (ptr) - 1;
1317 if (v->top_margin.y < v->screen_start.y)
1318 v->top_margin.y = v->screen_start.y;
1319 if (v->top_margin.y > v->screen_end.y)
1320 v->top_margin.y = v->screen_end.y;
1321 if (v->bottom_margin.y < v->screen_start.y)
1322 v->bottom_margin.y = v->screen_start.y;
1323 if (v->bottom_margin.y > v->screen_end.y)
1324 v->bottom_margin.y = v->screen_end.y;
1326 vt102_cursor_home (v);
1329 v->saved.pos = v->pos;
1332 v->pos = v->saved.pos;
1333 vt102_cursor_normalize (v);
1334 v->pending_wrap = 0;
1336 case 'y': //Invoke confidence test
1338 case 'z': //request terminal parameters
1342 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1349 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1359 vt102_parse_esc (Context * c)
1362 VT102_parser *p = &v->parser;
1365 p->cmd_buf[p->cmd_ptr] = 0;
1366 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1367 v->pos.y, v->pending_wrap,
1368 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1371 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1375 switch (p->cmd_buf[0])
1378 vt102_cursor_relative (v, 0, -1);
1381 vt102_cursor_relative (v, 0, 1);
1384 vt102_cursor_relative (v, 1, 0);
1387 vt102_cursor_relative (v, -1, 0);
1391 v->g[1] = VT102_CSID_VT52;
1395 v->g[0] = VT102_CSID_US;
1398 vt102_cursor_absolute (v, 0, 0);
1401 vt102_cursor_retreat_line (c);
1404 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1408 CRT_Pos le = { v->current_width - 1, v->pos.y };
1409 crt_erase (&v->crt, v->pos, le, 1, v->color);
1413 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1415 case 'V': //Print current line
1417 case 'W': //Printer on
1419 case 'X': //printer off
1421 case ']': //print screen
1424 vt102_send_id (c, "\033/K");
1426 case '^': //Autoprint on
1428 case '_': //Autoprint off
1431 v->application_keypad_mode = 1;
1434 v->application_keypad_mode = 0;
1444 /*If you edit this switch don't forget to edit the length and termination tables*/
1445 switch (p->cmd_buf[0])
1448 vt102_cursor_advance_line (c);
1452 vt102_cursor_advance_line (c);
1453 v->pos.x = v->top_margin.x;
1454 vt102_cursor_normalize (v);
1455 v->pending_wrap = 0;
1458 v->tabs[v->pos.x]++;
1461 vt102_cursor_retreat_line (c);
1463 case 'N': //select G2 for one char
1465 case 'O': //select G3 for one char
1468 vt102_send_id (c, terminal_id);
1476 v->application_keypad_mode = 1;
1479 v->application_keypad_mode = 0;
1483 switch (p->cmd_buf[1])
1485 case '3': //top of double height line
1486 case '4': //bottom of double height line
1487 case '5': //single width line
1488 case '6': //double width line
1494 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1496 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1497 v->crt.screen[i].chr = 'E';
1503 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1504 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1509 /*Set ansi mode - ignored */
1513 vt102_save_state (v);
1516 vt102_restore_state (v);
1518 case ']': /*Set various titles */
1522 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1524 case '(': /*Charsets */
1529 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1532 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1533 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1538 p->cmd_buf[p->cmd_ptr] = 0;
1540 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1546 vt102_status_line (VT102 * v, char *str)
1548 int i = v->current_width - 1;
1549 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1551 for (i = 0; i < v->current_width; ++i)
1553 ca->attr = CRT_ATTR_REVERSE;
1554 ca->color = CRT_COLOR_NORMAL;
1555 ca->chr = *str ? *str : ' ';
1561 for (; i < VT102_MAX_COLS; ++i)
1563 ca->attr = CRT_ATTR_NORMAL;
1564 ca->color = CRT_COLOR_NORMAL;
1573 vt102_parser_reset (VT102_parser * p)
1576 p->cmd_more_bytes = 0;
1577 p->cmd_termination = 0;
1583 vt102_reset_state (VT102 * v)
1585 vt102_parser_reset (&v->parser);
1587 v->attr = CRT_ATTR_NORMAL;
1588 v->color = CRT_COLOR_NORMAL;
1590 v->application_keypad_mode = 0;
1592 v->current_width = VT102_COLS_80;
1593 v->crt.width = v->current_width;
1594 v->screen_end.x = v->current_width - 1;
1596 v->top_margin = v->screen_start;
1597 v->bottom_margin = v->screen_end;
1599 memset (v->modes, 0, VT102_NMODES);
1600 memset (v->private_modes, 0, VT102_NMODES);
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;
1608 vt102_reset_tabs (v);
1610 v->g[0] = v->g[1] = VT102_CSID_US;
1615 pre_parse_cmd (int ch, VT102_parser * p)
1617 if (ch > TABLE_LENGTH)
1622 p->cmd_more_bytes = 0;
1625 p->cmd_termination = vt102_cmd_termination[ch];
1626 if (p->cmd_termination)
1632 p->cmd_more_bytes = vt102_cmd_length[ch];
1634 if (p->cmd_more_bytes)
1642 vt102_parse_char (Context * c, int ch)
1645 VT102_parser *p = &v->parser;
1648 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1649 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1650 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1653 /* Turn anything non-ascii into '?' */
1655 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1663 if (p->dca_ptr == 2)
1668 if (ch == SYM_CHAR_RESET)
1670 vt102_reset_state (v);
1672 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1674 p->cmd_buf[p->cmd_ptr++] = ch;
1675 if (p->cmd_ptr == VT102_CMD_LEN)
1677 if (p->cmd_more_bytes)
1679 p->cmd_more_bytes--;
1682 if (!p->cmd_more_bytes == 1)
1686 switch (p->cmd_termination)
1691 if (p->cmd_termination == ch)
1702 vt102_parse_esc (c);
1703 p->cmd_more_bytes = 0;
1704 p->cmd_termination = 0;
1707 else if (p->in_escape && !ctrl_chr (ch, 0))
1710 p->cmd_buf[p->cmd_ptr++] = ch;
1713 pre_parse_cmd (ch, p);
1716 vt102_parse_esc (c);
1718 else if (ch == 0x9b) /*One byte CSI */
1721 p->cmd_buf[p->cmd_ptr++] = '[';
1723 pre_parse_cmd (ch, p);
1730 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1731 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1746 vt102_send_id (c, terminal_id);
1756 vt102_cursor_retreat (c->v);
1759 vt102_cursor_advance_tab (c->v);
1764 vt102_cursor_advance_line (c);
1765 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1768 vt102_cursor_carriage_return (v);
1803 /*regular character */ default:
1804 vt102_regular_char (c, v, ch);
1809 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1814 v->crt.pos = v->pos;
1815 v->crt.hide_cursor =
1816 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1818 if (v->current_line.y != v->pos.y)
1820 vt102_log_line (c, v->current_line.y);
1821 v->current_line = v->pos;
1825 cmd_show_status (c->d, c);
1829 vt102_send (Context * c, uint8_t key)
1837 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1839 if ((key > 31) && (key < 127))
1841 c->t->xmit (c->t, &key, 1);
1860 c->t->xmit (c->t, &key, 1);
1863 c->t->xmit (c->t, &key, 1);
1864 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1867 c->t->xmit (c->t, &ch, 1);
1883 c->t->xmit (c->t, &key, 1);
1891 c->t->xmit (c->t, &key, 1);
1902 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1904 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1905 c->t->xmit (c->t, &buf, sizeof (buf));
1909 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1910 c->t->xmit (c->t, &buf, sizeof (buf));
1928 if (c->v->application_keypad_mode)
1930 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1931 c->t->xmit (c->t, &buf, sizeof (buf));
1935 static char kpoff[KEY_NUM] = {
1954 c->t->xmit (c->t, &kpoff[key], 1);
1958 if (c->v->application_keypad_mode)
1960 uint8_t buf[] = { 033, 'O', 'M' };
1961 c->t->xmit (c->t, &buf, sizeof (buf));
1966 c->t->xmit (c->t, &ch, 1);
1967 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1970 c->t->xmit (c->t, &ch, 1);
1979 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1980 c->t->xmit (c->t, &buf, sizeof (buf));
1988 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1989 c->t->xmit (c->t, &buf, sizeof (buf));
1997 vt102_reset (VT102 * v)
1999 VT102_parser *p = &v->parser;
2003 v->current_line = v->pos;
2004 v->pending_wrap = 0;
2006 v->screen_start.x = 0;
2007 v->screen_start.y = 0;
2008 v->current_width = VT102_COLS_80;
2009 v->crt.width = v->current_width;
2010 v->screen_end.x = v->current_width - 1;
2011 v->screen_end.y = VT102_ROWS - 1;
2013 vt102_cursor_home (v);
2014 vt102_status_line (v, "");
2016 vt102_reset_tabs (v);
2017 v->current_line = v->pos;
2019 vt102_parser_reset (p);
2020 vt102_reset_state (v);
2022 vt102_save_state (v);
2024 v->last_reg_char = ' ';
2028 vt102_new (int width)
2032 v = (VT102 *) malloc (sizeof (VT102));
2040 v->current_width = width;
2041 v->crt.width = v->current_width;
2042 v->screen_end.x = v->current_width - 1;
2043 v->top_margin = v->screen_start;
2044 v->bottom_margin = v->screen_end;
2045 vt102_cursor_home (v);
2052 vt102_set_ansi (VT102 * v, int ansi)
2054 v->xn_glitch = ansi ? 0 : 1;
2058 vt102_free (VT102 * v)