4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.49 2008/02/28 11:27:48 james
14 * *** empty log message ***
16 * Revision 1.48 2008/02/27 09:42:53 james
17 * *** empty log message ***
19 * Revision 1.47 2008/02/27 09:42:22 james
20 * *** empty log message ***
22 * Revision 1.46 2008/02/27 01:31:38 james
23 * *** empty log message ***
25 * Revision 1.45 2008/02/27 01:31:14 james
26 * *** empty log message ***
28 * Revision 1.44 2008/02/27 00:54:16 james
29 * *** empty log message ***
31 * Revision 1.43 2008/02/27 00:27:22 james
32 * *** empty log message ***
34 * Revision 1.42 2008/02/26 23:56:12 james
35 * *** empty log message ***
37 * Revision 1.41 2008/02/26 23:23:17 james
38 * *** empty log message ***
40 * Revision 1.40 2008/02/26 19:00:59 james
41 * *** empty log message ***
43 * Revision 1.39 2008/02/26 16:54:06 james
44 * *** empty log message ***
46 * Revision 1.38 2008/02/26 16:53:24 james
47 * *** empty log message ***
49 * Revision 1.37 2008/02/24 12:22:53 james
50 * *** empty log message ***
52 * Revision 1.36 2008/02/24 12:22:42 james
53 * *** empty log message ***
55 * Revision 1.35 2008/02/24 00:42:53 james
56 * *** empty log message ***
58 * Revision 1.34 2008/02/23 11:48:37 james
59 * *** empty log message ***
61 * Revision 1.33 2008/02/22 23:39:27 james
62 * *** empty log message ***
64 * Revision 1.32 2008/02/22 19:12:05 james
65 * *** empty log message ***
67 * Revision 1.31 2008/02/22 17:07:00 james
68 * *** empty log message ***
70 * Revision 1.30 2008/02/22 14:51:54 james
71 * *** empty log message ***
73 * Revision 1.29 2008/02/15 03:32:07 james
74 * *** empty log message ***
76 * Revision 1.28 2008/02/14 10:34:30 james
77 * *** empty log message ***
79 * Revision 1.27 2008/02/14 02:46:45 james
80 * *** empty log message ***
82 * Revision 1.26 2008/02/14 01:55:57 james
83 * *** empty log message ***
85 * Revision 1.25 2008/02/13 16:57:29 james
86 * *** empty log message ***
88 * Revision 1.24 2008/02/13 09:12:21 james
89 * *** empty log message ***
91 * Revision 1.23 2008/02/07 13:26:35 james
92 * *** empty log message ***
94 * Revision 1.22 2008/02/07 13:22:51 james
95 * *** empty log message ***
97 * Revision 1.21 2008/02/07 12:21:16 james
98 * *** empty log message ***
100 * Revision 1.20 2008/02/07 12:16:04 james
101 * *** empty log message ***
103 * Revision 1.19 2008/02/07 11:27:02 james
104 * *** empty log message ***
106 * Revision 1.18 2008/02/07 01:59:25 james
107 * *** empty log message ***
109 * Revision 1.17 2008/02/07 01:58:28 james
110 * *** empty log message ***
112 * Revision 1.16 2008/02/07 01:57:46 james
113 * *** empty log message ***
115 * Revision 1.15 2008/02/07 00:43:27 james
116 * *** empty log message ***
118 * Revision 1.14 2008/02/07 00:40:23 james
119 * *** empty log message ***
121 * Revision 1.13 2008/02/07 00:39:59 james
122 * *** empty log message ***
124 * Revision 1.12 2008/02/07 00:39:13 james
125 * *** empty log message ***
127 * Revision 1.11 2008/02/06 20:26:58 james
128 * *** empty log message ***
130 * Revision 1.10 2008/02/06 17:53:28 james
131 * *** empty log message ***
133 * Revision 1.9 2008/02/06 15:53:22 james
134 * *** empty log message ***
136 * Revision 1.8 2008/02/06 11:49:47 james
137 * *** empty log message ***
139 * Revision 1.7 2008/02/06 11:30:37 james
140 * *** empty log message ***
142 * Revision 1.6 2008/02/05 01:11:46 james
143 * *** empty log message ***
145 * Revision 1.5 2008/02/04 20:23:55 james
146 * *** empty log message ***
148 * Revision 1.4 2008/02/04 05:45:55 james
151 * Revision 1.3 2008/02/04 02:05:06 james
152 * *** empty log message ***
154 * Revision 1.2 2008/02/04 01:32:39 james
155 * *** empty log message ***
157 * Revision 1.1 2008/02/03 23:36:41 james
158 * *** empty log message ***
169 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
171 vt100|vt100-am|dec vt100 (w/advanced video):\
173 :co#80:it#8:li#24:vt#3:\
174 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
176 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
177 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
178 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
179 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
180 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
181 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
182 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
183 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
184 :us=\E[4m:tc=vt100+fnkeys:
186 vt100+fnkeys|dec vt100 numeric keypad:\
187 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
190 vt100+pfkeys|dec vt100 numeric keypad:\
191 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
193 vt100+keypad|dec vt100 numeric keypad no fkeys:\
194 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
199 so the parser needs to be able to at least do
261 ansi|ansi/pc-term compatible with color:\
262 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
263 :tc=klone+sgr:tc=ansi-m:
264 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
266 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
267 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
268 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
269 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
270 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
271 :s3=\E+B:ta=\E[I:tc=pcansi-m:
272 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
275 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
276 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
277 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
278 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
280 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
281 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
282 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
283 klone+acs|alternate character set for ansi.sys displays:\
284 :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:\
285 :ae=\E[10m:as=\E[11m:
286 ecma+color|color control for ECMA-48-compatible terminals:\
288 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
291 Ignoreing ones in the VT102 spec
321 #define TABLE_LENGTH 128
325 static char terminal_id[] = "vt102";
327 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
328 int vt102_cmd_length[TABLE_LENGTH] = {
338 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
340 int vt102_cmd_termination[TABLE_LENGTH] = {
362 if ((c >= 'a') && (c <= 'z'))
364 if ((c >= 'A') && (c <= 'Z'))
372 ctrl_chr (int ch, int term)
374 if ((term > 0) && (ch == term))
378 if ((ch > 0) && (ch < 040))
385 in_margins (VT102 * v, CRT_Pos p)
387 if (v->pos.x < v->top_margin.x)
389 if (v->pos.y < v->top_margin.y)
392 if (v->pos.x > v->bottom_margin.x)
394 if (v->pos.y > v->bottom_margin.y)
400 void vt102_do_resize(Context *c)
405 v->crt.size = v->current_size;
407 v->screen_end = v->current_size;
410 v->top_margin = v->screen_start;
411 v->bottom_margin = v->screen_end;
412 vt102_cursor_home (v);
416 tty_winch(c->t,v->current_size);
421 vt102_log_line (Context * c, int line)
423 CRT_Pos e = { c->v->current_size.x - 1, line };
424 CRT_Pos p = { 0, line };
425 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
431 for (; e.x > 0; --e.x)
433 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
437 for (; p.x <= e.x; ++p.x)
439 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
442 logptr += utf8_encode (logptr, ch);
446 c->l->log (c->l, logbuf);
449 /*Called for every upward scroll with same args*/
451 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
453 /*Only log if it scrolls off the top*/
458 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
462 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
477 vt102_cursor_normalize (VT102 * v)
479 CRT_Pos *top, *bottom;
481 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
483 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
487 vt102_clip_cursor (v, v->screen_start, v->screen_end);
493 vt102_cursor_carriage_return (VT102 * v)
495 /*FISH*/ v->pos.x = v->top_margin.x;
500 vt102_cursor_advance_line (Context * c)
503 int couldscroll = in_margins (v, v->pos);
505 /*have wraped off end of last line in scrolling region */
506 /* (|| not necessary, but shuts compiler up */
507 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
510 vt102_log_line (c, v->pos.y);
512 vt102_history (c, v->top_margin, v->bottom_margin);
514 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
518 if (v->pos.y != v->screen_end.y)
525 vt102_cursor_retreat_line (Context * c)
528 int couldscroll = in_margins (v, v->pos);
530 /*have wraped off end of last line in scrolling region */
531 /* (|| not necessary, but shuts compiler up */
532 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
535 vt102_log_line (c, v->pos.y);
537 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
541 if (v->pos.y != v->screen_start.y)
548 vt102_do_pending_wrap (Context * c)
551 int couldscroll = in_margins (v, v->pos);
552 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
554 if (!v->pending_wrap)
558 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
559 v->pos.y, autowrap, couldscroll);
562 /*End of line but no autowrap, nothing to do*/
566 /*End of screen and not allowed to scroll, nothing to do*/
567 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
572 v->pos.x = v->top_margin.x;
579 vt102_cursor_advance_line (c);
583 vt102_cursor_advance (Context * c)
587 if (v->pos.x < v->bottom_margin.x)
595 if (!c->v->xn_glitch)
596 vt102_do_pending_wrap (c);
602 vt102_cursor_retreat (VT102 * v)
604 if (v->pos.x != v->top_margin.x)
613 vt102_reset_tabs (VT102 * v)
617 memset (v->tabs, 0, sizeof (v->tabs));
619 for (i = 0; i < VT102_MAX_COLS; i += 8)
625 vt102_cursor_advance_tab (VT102 * v)
627 if (v->pos.x == v->bottom_margin.x)
629 while (v->pos.x < v->bottom_margin.x)
632 if (v->tabs[v->pos.x])
639 vt102_cursor_retreat_tab (VT102 * v)
641 if (v->pos.x == v->top_margin.x)
643 while (v->pos.x > v->top_margin.x)
646 if (v->tabs[v->pos.x])
652 vt102_cursor_home (VT102 * v)
654 v->pos = v->top_margin;
655 vt102_cursor_normalize (v);
660 vt102_cursor_absolute (VT102 * v, int x, int y)
662 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
664 v->pos.x = x + v->top_margin.x;
665 v->pos.y = y + v->top_margin.y;
672 vt102_cursor_normalize (v);
676 vt102_cursor_relative (VT102 * v, int x, int y)
680 vt102_cursor_normalize (v);
687 vt102_delete_from_line (VT102 * v, CRT_Pos p)
689 int n = v->bottom_margin.x - p.x;
697 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
698 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
701 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
702 /*But not attr due to vt102 bug*/
706 vt102_insert_into_line (VT102 * v, CRT_Pos p)
708 int n = v->bottom_margin.x - p.x;
716 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
717 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
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;
728 vt102_change_mode (Context *c, int private, char *ns, int set)
745 if (m >= VT102_NMODES)
750 v->private_modes[m] = set;
753 case VT102_PRIVATE_MODE_CURSOR_MODE:
754 if (v->application_keypad_mode)
755 v->private_modes[m] = 0;
757 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
758 v->private_modes[m], set);
761 case VT102_PRIVATE_MODE_ORIGIN_MODE:
762 vt102_cursor_home (v);
764 case VT102_PRIVATE_MODE_132COLS:
765 /* We don't implement 132 col mode - yet */
769 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
781 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
786 vt102_parse_mode_string (Context *c, char *buf, int len)
790 char last = buf[len - 1];
794 memset (num, 0, sizeof (num));
795 o = sizeof (num) - 1;
813 vt102_change_mode (c, private, &num[o], last == 'h');
814 memset (num, 0, sizeof (num));
815 o = sizeof (num) - 1;
830 vt102_change_mode (c, private, &num[o], last == 'h');
836 vt102_change_attr (VT102 * v, char *na)
853 v->attr = CRT_ATTR_NORMAL;
854 v->color = CRT_COLOR_NORMAL;
857 v->attr |= CRT_ATTR_BOLD;
861 v->attr &= ~CRT_ATTR_BOLD;
864 v->attr |= CRT_ATTR_UNDERLINE;
867 v->attr &= ~CRT_ATTR_UNDERLINE;
870 v->attr |= CRT_ATTR_BLINK;
873 v->attr &= ~CRT_ATTR_BLINK;
876 v->attr |= CRT_ATTR_REVERSE;
879 v->attr &= ~CRT_ATTR_REVERSE;
889 v->color &= ~CRT_COLOR_FG_MASK;
890 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
900 v->color &= ~CRT_COLOR_FG_MASK;
903 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
908 v->color &= ~CRT_COLOR_FG_MASK;
910 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
920 v->color &= ~CRT_COLOR_BG_MASK;
921 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
931 v->color &= ~CRT_COLOR_BG_MASK;
934 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
939 v->color &= ~CRT_COLOR_BG_MASK;
941 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
947 fprintf (stderr, "unhandled SGR %d\n", a);
955 vt102_parse_attr_string (VT102 * v, char *buf, int len)
958 char last = buf[len - 1];
962 memset (num, 0, sizeof (num));
963 o = sizeof (num) - 1;
974 vt102_change_attr (v, &num[o]);
975 memset (num, 0, sizeof (num));
976 o = sizeof (num) - 1;
990 vt102_change_attr (v, &num[o]);
994 vt102_save_state (VT102 * v)
996 v->saved.pos = v->pos;
997 v->saved.attr = v->attr;
998 v->saved.color = v->color;
999 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
1003 vt102_restore_state (VT102 * v)
1005 v->pos = v->saved.pos;
1006 v->attr = v->saved.attr;
1007 v->color = v->saved.color;
1008 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
1009 vt102_cursor_normalize (v);
1010 v->pending_wrap = 0;
1014 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
1018 vt102_do_pending_wrap (c);
1021 if (v->modes[VT102_MODE_INSERT])
1022 vt102_insert_into_line (v, v->pos);
1024 v->last_reg_char = ch;
1027 if (ch < VT102_CHARSET_SIZE)
1030 if ((cs = vt102_charset_c0[ch]))
1034 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1039 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1040 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1041 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1042 vt102_cursor_advance (c);
1046 vt102_send_id (Context * c, char *buf)
1051 int l = strlen (buf);
1052 c->t->xmit (c->t, buf, l);
1057 vt102_scs (Context * c, int g, int s)
1060 int cs = VT102_CSID_US;
1091 vt102_parse_csi (Context * c, char *buf, int len)
1095 char *arg = buf + 1;
1102 last = buf[len - 1];
1123 vt102_insert_into_line (v, v->pos);
1126 vt102_cursor_relative (v, 0, -narg);
1130 vt102_cursor_relative (v, 0, narg);
1134 vt102_cursor_relative (v, narg, 0);
1137 vt102_cursor_relative (v, -narg, 0);
1140 vt102_cursor_relative (v, 0, narg);
1141 vt102_cursor_carriage_return (v);
1144 vt102_cursor_relative (v, 0, -narg);
1145 vt102_cursor_carriage_return (v);
1148 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1157 ptr = index (arg, ';');
1159 x = atoi (ptr + 1) - 1;
1163 vt102_cursor_absolute (v, x, y);
1168 vt102_cursor_advance_tab (c->v);
1172 narg = 0; /*Different default */
1176 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1179 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1182 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1189 CRT_Pos ls = { 0, v->pos.y };
1190 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1192 narg = 0; /*Different default */
1197 crt_erase (&v->crt, v->pos, le, 1, v->color);
1200 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1203 crt_erase (&v->crt, ls, le, 1, v->color);
1210 if ((v->pos.y >= v->top_margin.y)
1211 && (v->pos.y <= v->bottom_margin.y))
1214 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1220 if ((v->pos.y >= v->top_margin.y)
1221 && (v->pos.y <= v->bottom_margin.y))
1225 vt102_history (c, v->pos, v->bottom_margin);
1226 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1233 vt102_delete_from_line (v, v->pos);
1238 vt102_history (c, v->top_margin, v->bottom_margin);
1239 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1245 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1250 CRT_Pos end = v->pos;
1255 if (end.x > v->bottom_margin.x)
1256 end.x = v->bottom_margin.x;
1258 crt_erase (&v->crt, v->pos, end, 1, v->color);
1263 vt102_cursor_retreat_tab (c->v);
1266 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1270 vt102_regular_char (c, v, v->last_reg_char);
1273 vt102_send_id (c, "\033[?2c");
1276 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1280 narg = 0; /*Different default */
1285 v->tabs[v->pos.x] = 0;
1287 case 2: //FIXME: - LA120 says current line only WTF?
1289 memset (v->tabs, 0, sizeof (v->tabs));
1294 case 'i': //Printer commands
1299 vt102_parse_mode_string (c, &buf[1], len - 1);
1304 vt102_parse_attr_string (v, &buf[1], len - 1);
1306 case 'n': //Device status report
1310 vt102_send_id (c, "\033[0n");
1318 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1319 c->t->xmit (c->t, buf, i);
1323 //?15n printer status
1324 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1328 case 'q': //Load LED on off
1331 v->top_margin = v->screen_start;
1332 v->bottom_margin = v->screen_end;
1334 if ((len > 2) && (ptr = index (arg, ';')))
1337 v->top_margin.y = narg - 1;
1338 v->bottom_margin.y = atoi (ptr) - 1;
1341 if (v->top_margin.y < v->screen_start.y)
1342 v->top_margin.y = v->screen_start.y;
1343 if (v->top_margin.y > v->screen_end.y)
1344 v->top_margin.y = v->screen_end.y;
1345 if (v->bottom_margin.y < v->screen_start.y)
1346 v->bottom_margin.y = v->screen_start.y;
1347 if (v->bottom_margin.y > v->screen_end.y)
1348 v->bottom_margin.y = v->screen_end.y;
1350 vt102_cursor_home (v);
1353 v->saved.pos = v->pos;
1356 v->pos = v->saved.pos;
1357 vt102_cursor_normalize (v);
1358 v->pending_wrap = 0;
1360 case 'y': //Invoke confidence test
1362 case 'z': //request terminal parameters
1366 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1373 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1383 vt102_parse_esc (Context * c)
1386 VT102_parser *p = &v->parser;
1389 p->cmd_buf[p->cmd_ptr] = 0;
1390 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1391 v->pos.y, v->pending_wrap,
1392 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1395 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1399 switch (p->cmd_buf[0])
1402 vt102_cursor_relative (v, 0, -1);
1405 vt102_cursor_relative (v, 0, 1);
1408 vt102_cursor_relative (v, 1, 0);
1411 vt102_cursor_relative (v, -1, 0);
1415 v->g[1] = VT102_CSID_VT52;
1419 v->g[0] = VT102_CSID_US;
1422 vt102_cursor_absolute (v, 0, 0);
1425 vt102_cursor_retreat_line (c);
1428 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1432 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1433 crt_erase (&v->crt, v->pos, le, 1, v->color);
1437 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1439 case 'V': //Print current line
1441 case 'W': //Printer on
1443 case 'X': //printer off
1445 case ']': //print screen
1448 vt102_send_id (c, "\033/K");
1450 case '^': //Autoprint on
1452 case '_': //Autoprint off
1455 v->application_keypad_mode = 1;
1458 v->application_keypad_mode = 0;
1468 /*If you edit this switch don't forget to edit the length and termination tables*/
1469 switch (p->cmd_buf[0])
1472 vt102_cursor_advance_line (c);
1476 vt102_cursor_advance_line (c);
1477 v->pos.x = v->top_margin.x;
1478 vt102_cursor_normalize (v);
1479 v->pending_wrap = 0;
1482 v->tabs[v->pos.x]++;
1485 vt102_cursor_retreat_line (c);
1487 case 'N': //select G2 for one char
1489 case 'O': //select G3 for one char
1492 vt102_send_id (c, terminal_id);
1498 v->application_keypad_mode = 1;
1501 v->application_keypad_mode = 0;
1505 switch (p->cmd_buf[1])
1507 case '3': //top of double height line
1508 case '4': //bottom of double height line
1509 case '5': //single width line
1510 case '6': //double width line
1516 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1518 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1519 v->crt.screen[i].chr = 'E';
1525 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1526 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1531 /*Set ansi mode - ignored */
1535 vt102_save_state (v);
1538 vt102_restore_state (v);
1540 case ']': /*Set various titles */
1544 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1546 case '(': /*Charsets */
1551 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1554 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1555 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1560 p->cmd_buf[p->cmd_ptr] = 0;
1562 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1568 vt102_status_line (VT102 * v, char *str)
1571 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1573 for (i = 0; i < v->current_size.x; ++i)
1575 ca->attr = CRT_ATTR_REVERSE;
1576 ca->color = CRT_COLOR_NORMAL;
1577 ca->chr = *str ? *str : ' ';
1583 for (; i < VT102_MAX_COLS; ++i)
1585 ca->attr = CRT_ATTR_NORMAL;
1586 ca->color = CRT_COLOR_NORMAL;
1595 vt102_parser_reset (VT102_parser * p)
1598 p->cmd_more_bytes = 0;
1599 p->cmd_termination = 0;
1605 vt102_reset_state (Context *c)
1608 vt102_parser_reset (&v->parser);
1610 v->attr = CRT_ATTR_NORMAL;
1611 v->color = CRT_COLOR_NORMAL;
1613 v->application_keypad_mode = 0;
1615 v->current_size = v->original_size;
1619 memset (v->modes, 0, VT102_NMODES);
1620 memset (v->private_modes, 0, VT102_NMODES);
1622 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1623 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1624 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1625 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1626 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1628 vt102_reset_tabs (v);
1630 v->g[0] = v->g[1] = VT102_CSID_US;
1635 pre_parse_cmd (int ch, VT102_parser * p)
1637 if (ch > TABLE_LENGTH)
1642 p->cmd_more_bytes = 0;
1645 p->cmd_termination = vt102_cmd_termination[ch];
1646 if (p->cmd_termination)
1652 p->cmd_more_bytes = vt102_cmd_length[ch];
1654 if (p->cmd_more_bytes)
1662 vt102_parse_char (Context * c, int ch)
1665 VT102_parser *p = &v->parser;
1668 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1669 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1670 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1673 /* Turn anything non-ascii into '?' */
1675 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1683 if (p->dca_ptr == 2)
1688 if (ch == SYM_CHAR_RESET)
1690 vt102_reset_state (c);
1692 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1694 p->cmd_buf[p->cmd_ptr++] = ch;
1695 if (p->cmd_ptr == VT102_CMD_LEN)
1697 if (p->cmd_more_bytes)
1699 p->cmd_more_bytes--;
1702 if (!p->cmd_more_bytes == 1)
1706 switch (p->cmd_termination)
1711 if (p->cmd_termination == ch)
1722 vt102_parse_esc (c);
1723 p->cmd_more_bytes = 0;
1724 p->cmd_termination = 0;
1727 else if (p->in_escape && !ctrl_chr (ch, 0))
1730 p->cmd_buf[p->cmd_ptr++] = ch;
1733 pre_parse_cmd (ch, p);
1736 vt102_parse_esc (c);
1738 else if (ch == 0x9b) /*One byte CSI */
1741 p->cmd_buf[p->cmd_ptr++] = '[';
1743 pre_parse_cmd (ch, p);
1750 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1751 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1766 vt102_send_id (c, terminal_id);
1776 vt102_cursor_retreat (c->v);
1779 vt102_cursor_advance_tab (c->v);
1784 vt102_cursor_advance_line (c);
1785 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1788 vt102_cursor_carriage_return (v);
1823 /*regular character */ default:
1824 vt102_regular_char (c, v, ch);
1829 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1834 v->crt.pos = v->pos;
1835 v->crt.hide_cursor =
1836 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1838 if (v->current_line.y != v->pos.y)
1840 vt102_log_line (c, v->current_line.y);
1841 v->current_line = v->pos;
1845 cmd_show_status (c->d, c);
1849 vt102_send (Context * c, uint8_t key)
1857 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1859 if ((key > 31) && (key < 127))
1861 c->t->xmit (c->t, &key, 1);
1880 c->t->xmit (c->t, &key, 1);
1883 c->t->xmit (c->t, &key, 1);
1884 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1887 c->t->xmit (c->t, &ch, 1);
1903 c->t->xmit (c->t, &key, 1);
1911 c->t->xmit (c->t, &key, 1);
1922 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1924 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1925 c->t->xmit (c->t, &buf, sizeof (buf));
1929 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1930 c->t->xmit (c->t, &buf, sizeof (buf));
1948 if (c->v->application_keypad_mode)
1950 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1951 c->t->xmit (c->t, &buf, sizeof (buf));
1955 static char kpoff[KEY_NUM] = {
1974 c->t->xmit (c->t, &kpoff[key], 1);
1978 if (c->v->application_keypad_mode)
1980 uint8_t buf[] = { 033, 'O', 'M' };
1981 c->t->xmit (c->t, &buf, sizeof (buf));
1986 c->t->xmit (c->t, &ch, 1);
1987 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1990 c->t->xmit (c->t, &ch, 1);
1999 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
2000 c->t->xmit (c->t, &buf, sizeof (buf));
2008 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
2009 c->t->xmit (c->t, &buf, sizeof (buf));
2017 vt102_reset (Context * c)
2020 VT102_parser *p = &v->parser;
2024 v->current_line = v->pos;
2025 v->pending_wrap = 0;
2027 v->screen_start.x = 0;
2028 v->screen_start.y = 0;
2029 v->current_size = v->original_size;
2030 v->crt.size = v->current_size;
2032 v->screen_end = v->current_size;
2036 vt102_cursor_home (v);
2037 vt102_status_line (v, "");
2039 vt102_reset_tabs (v);
2040 v->current_line = v->pos;
2042 vt102_parser_reset (p);
2043 vt102_reset_state (c);
2045 vt102_save_state (v);
2047 v->last_reg_char = ' ';
2051 vt102_new (CRT_Pos * size)
2055 v = (VT102 *) malloc (sizeof (VT102));
2062 v->original_size = *size;
2064 if (v->original_size.x < 1)
2065 v->original_size.x = 1;
2066 if (v->original_size.y < 1)
2067 v->original_size.y = 1;
2069 if (v->original_size.x > VT102_MAX_COLS)
2070 v->original_size.x = VT102_MAX_COLS;
2071 if (v->original_size.y > VT102_ROWS)
2072 v->original_size.y = VT102_ROWS;
2077 v->original_size.x = VT102_COLS_80;
2078 v->original_size.y = VT102_ROWS;
2085 vt102_set_ansi (VT102 * v, int ansi)
2087 v->xn_glitch = ansi ? 0 : 1;
2090 void vt102_resize(Context *c,CRT_Pos size)
2099 if (size.x > VT102_MAX_COLS)
2100 size.x = VT102_MAX_COLS;
2101 if (size.y > VT102_ROWS)
2102 size.y = VT102_ROWS;
2104 c->v->current_size=size;
2109 vt102_free (VT102 * v)