4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.47 2008/02/27 09:42:22 james
14 * *** empty log message ***
16 * Revision 1.46 2008/02/27 01:31:38 james
17 * *** empty log message ***
19 * Revision 1.45 2008/02/27 01:31:14 james
20 * *** empty log message ***
22 * Revision 1.44 2008/02/27 00:54:16 james
23 * *** empty log message ***
25 * Revision 1.43 2008/02/27 00:27:22 james
26 * *** empty log message ***
28 * Revision 1.42 2008/02/26 23:56:12 james
29 * *** empty log message ***
31 * Revision 1.41 2008/02/26 23:23:17 james
32 * *** empty log message ***
34 * Revision 1.40 2008/02/26 19:00:59 james
35 * *** empty log message ***
37 * Revision 1.39 2008/02/26 16:54:06 james
38 * *** empty log message ***
40 * Revision 1.38 2008/02/26 16:53:24 james
41 * *** empty log message ***
43 * Revision 1.37 2008/02/24 12:22:53 james
44 * *** empty log message ***
46 * Revision 1.36 2008/02/24 12:22:42 james
47 * *** empty log message ***
49 * Revision 1.35 2008/02/24 00:42:53 james
50 * *** empty log message ***
52 * Revision 1.34 2008/02/23 11:48:37 james
53 * *** empty log message ***
55 * Revision 1.33 2008/02/22 23:39:27 james
56 * *** empty log message ***
58 * Revision 1.32 2008/02/22 19:12:05 james
59 * *** empty log message ***
61 * Revision 1.31 2008/02/22 17:07:00 james
62 * *** empty log message ***
64 * Revision 1.30 2008/02/22 14:51:54 james
65 * *** empty log message ***
67 * Revision 1.29 2008/02/15 03:32:07 james
68 * *** empty log message ***
70 * Revision 1.28 2008/02/14 10:34:30 james
71 * *** empty log message ***
73 * Revision 1.27 2008/02/14 02:46:45 james
74 * *** empty log message ***
76 * Revision 1.26 2008/02/14 01:55:57 james
77 * *** empty log message ***
79 * Revision 1.25 2008/02/13 16:57:29 james
80 * *** empty log message ***
82 * Revision 1.24 2008/02/13 09:12:21 james
83 * *** empty log message ***
85 * Revision 1.23 2008/02/07 13:26:35 james
86 * *** empty log message ***
88 * Revision 1.22 2008/02/07 13:22:51 james
89 * *** empty log message ***
91 * Revision 1.21 2008/02/07 12:21:16 james
92 * *** empty log message ***
94 * Revision 1.20 2008/02/07 12:16:04 james
95 * *** empty log message ***
97 * Revision 1.19 2008/02/07 11:27:02 james
98 * *** empty log message ***
100 * Revision 1.18 2008/02/07 01:59:25 james
101 * *** empty log message ***
103 * Revision 1.17 2008/02/07 01:58:28 james
104 * *** empty log message ***
106 * Revision 1.16 2008/02/07 01:57:46 james
107 * *** empty log message ***
109 * Revision 1.15 2008/02/07 00:43:27 james
110 * *** empty log message ***
112 * Revision 1.14 2008/02/07 00:40:23 james
113 * *** empty log message ***
115 * Revision 1.13 2008/02/07 00:39:59 james
116 * *** empty log message ***
118 * Revision 1.12 2008/02/07 00:39:13 james
119 * *** empty log message ***
121 * Revision 1.11 2008/02/06 20:26:58 james
122 * *** empty log message ***
124 * Revision 1.10 2008/02/06 17:53:28 james
125 * *** empty log message ***
127 * Revision 1.9 2008/02/06 15:53:22 james
128 * *** empty log message ***
130 * Revision 1.8 2008/02/06 11:49:47 james
131 * *** empty log message ***
133 * Revision 1.7 2008/02/06 11:30:37 james
134 * *** empty log message ***
136 * Revision 1.6 2008/02/05 01:11:46 james
137 * *** empty log message ***
139 * Revision 1.5 2008/02/04 20:23:55 james
140 * *** empty log message ***
142 * Revision 1.4 2008/02/04 05:45:55 james
145 * Revision 1.3 2008/02/04 02:05:06 james
146 * *** empty log message ***
148 * Revision 1.2 2008/02/04 01:32:39 james
149 * *** empty log message ***
151 * Revision 1.1 2008/02/03 23:36:41 james
152 * *** empty log message ***
163 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
165 vt100|vt100-am|dec vt100 (w/advanced video):\
167 :co#80:it#8:li#24:vt#3:\
168 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
170 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
171 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
172 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
173 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
174 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
175 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
176 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
177 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
178 :us=\E[4m:tc=vt100+fnkeys:
180 vt100+fnkeys|dec vt100 numeric keypad:\
181 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
184 vt100+pfkeys|dec vt100 numeric keypad:\
185 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
187 vt100+keypad|dec vt100 numeric keypad no fkeys:\
188 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
193 so the parser needs to be able to at least do
255 ansi|ansi/pc-term compatible with color:\
256 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
257 :tc=klone+sgr:tc=ansi-m:
258 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
260 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
261 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
262 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
263 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
264 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
265 :s3=\E+B:ta=\E[I:tc=pcansi-m:
266 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
269 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
270 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
271 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
272 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
274 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
275 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
276 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
277 klone+acs|alternate character set for ansi.sys displays:\
278 :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:\
279 :ae=\E[10m:as=\E[11m:
280 ecma+color|color control for ECMA-48-compatible terminals:\
282 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
285 Ignoreing ones in the VT102 spec
315 #define TABLE_LENGTH 128
319 static char terminal_id[] = "vt102";
321 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
322 int vt102_cmd_length[TABLE_LENGTH] = {
332 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
334 int vt102_cmd_termination[TABLE_LENGTH] = {
356 if ((c >= 'a') && (c <= 'z'))
358 if ((c >= 'A') && (c <= 'Z'))
366 ctrl_chr (int ch, int term)
368 if ((term > 0) && (ch == term))
372 if ((ch > 0) && (ch < 040))
379 in_margins (VT102 * v, CRT_Pos p)
381 if (v->pos.x < v->top_margin.x)
383 if (v->pos.y < v->top_margin.y)
386 if (v->pos.x > v->bottom_margin.x)
388 if (v->pos.y > v->bottom_margin.y)
395 vt102_log_line (Context * c, int line)
397 CRT_Pos e = { c->v->current_size.x - 1, line };
398 CRT_Pos p = { 0, line };
399 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
405 for (; e.x > 0; --e.x)
407 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
411 for (; p.x <= e.x; ++p.x)
413 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
416 logptr += utf8_encode (logptr, ch);
420 c->l->log (c->l, logbuf);
423 /*Called for every upward scroll with same args*/
425 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
427 /*Only log if it scrolls off the top*/
432 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
436 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
451 vt102_cursor_normalize (VT102 * v)
453 CRT_Pos *top, *bottom;
455 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
457 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
461 vt102_clip_cursor (v, v->screen_start, v->screen_end);
467 vt102_cursor_carriage_return (VT102 * v)
469 /*FISH*/ v->pos.x = v->top_margin.x;
474 vt102_cursor_advance_line (Context * c)
477 int couldscroll = in_margins (v, v->pos);
479 /*have wraped off end of last line in scrolling region */
480 /* (|| not necessary, but shuts compiler up */
481 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
484 vt102_log_line (c, v->pos.y);
486 vt102_history (c, v->top_margin, v->bottom_margin);
488 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
492 if (v->pos.y != v->screen_end.y)
499 vt102_cursor_retreat_line (Context * c)
502 int couldscroll = in_margins (v, v->pos);
504 /*have wraped off end of last line in scrolling region */
505 /* (|| not necessary, but shuts compiler up */
506 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
509 vt102_log_line (c, v->pos.y);
511 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
515 if (v->pos.y != v->screen_start.y)
522 vt102_do_pending_wrap (Context * c)
525 int couldscroll = in_margins (v, v->pos);
526 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
528 if (!v->pending_wrap)
532 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
533 v->pos.y, autowrap, couldscroll);
536 /*End of line but no autowrap, nothing to do*/
540 /*End of screen and not allowed to scroll, nothing to do*/
541 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
546 v->pos.x = v->top_margin.x;
553 vt102_cursor_advance_line (c);
557 vt102_cursor_advance (Context * c)
561 if (v->pos.x < v->bottom_margin.x)
569 if (!c->v->xn_glitch)
570 vt102_do_pending_wrap (c);
576 vt102_cursor_retreat (VT102 * v)
578 if (v->pos.x != v->top_margin.x)
587 vt102_reset_tabs (VT102 * v)
591 memset (v->tabs, 0, sizeof (v->tabs));
593 for (i = 0; i < VT102_MAX_COLS; i += 8)
599 vt102_cursor_advance_tab (VT102 * v)
601 if (v->pos.x == v->bottom_margin.x)
603 while (v->pos.x < v->bottom_margin.x)
606 if (v->tabs[v->pos.x])
613 vt102_cursor_retreat_tab (VT102 * v)
615 if (v->pos.x == v->top_margin.x)
617 while (v->pos.x > v->top_margin.x)
620 if (v->tabs[v->pos.x])
626 vt102_cursor_home (VT102 * v)
628 v->pos = v->top_margin;
629 vt102_cursor_normalize (v);
634 vt102_cursor_absolute (VT102 * v, int x, int y)
636 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
638 v->pos.x = x + v->top_margin.x;
639 v->pos.y = y + v->top_margin.y;
646 vt102_cursor_normalize (v);
650 vt102_cursor_relative (VT102 * v, int x, int y)
654 vt102_cursor_normalize (v);
661 vt102_delete_from_line (VT102 * v, CRT_Pos p)
663 int n = v->bottom_margin.x - p.x;
671 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
672 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
675 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
676 /*But not attr due to vt102 bug*/
680 vt102_insert_into_line (VT102 * v, CRT_Pos p)
682 int n = v->bottom_margin.x - p.x;
690 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
691 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
694 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
695 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
696 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
702 vt102_change_mode (VT102 * v, int private, char *ns, int set)
718 if (m >= VT102_NMODES)
723 v->private_modes[m] = set;
726 case VT102_PRIVATE_MODE_CURSOR_MODE:
727 if (v->application_keypad_mode)
728 v->private_modes[m] = 0;
730 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
731 v->private_modes[m], set);
734 case VT102_PRIVATE_MODE_ORIGIN_MODE:
735 vt102_cursor_home (v);
737 case VT102_PRIVATE_MODE_132COLS:
738 /* We don't implement 132 col mode - yet */
742 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
745 v->crt.size = v->current_size;
747 v->screen_end=v->current_size;
750 v->top_margin = v->screen_start;
751 v->bottom_margin = v->screen_end;
752 vt102_cursor_home (v);
764 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
769 vt102_parse_mode_string (VT102 * v, char *buf, int len)
772 char last = buf[len - 1];
776 memset (num, 0, sizeof (num));
777 o = sizeof (num) - 1;
795 vt102_change_mode (v, private, &num[o], last == 'h');
796 memset (num, 0, sizeof (num));
797 o = sizeof (num) - 1;
812 vt102_change_mode (v, private, &num[o], last == 'h');
818 vt102_change_attr (VT102 * v, char *na)
835 v->attr = CRT_ATTR_NORMAL;
836 v->color = CRT_COLOR_NORMAL;
839 v->attr |= CRT_ATTR_BOLD;
843 v->attr &= ~CRT_ATTR_BOLD;
846 v->attr |= CRT_ATTR_UNDERLINE;
849 v->attr &= ~CRT_ATTR_UNDERLINE;
852 v->attr |= CRT_ATTR_BLINK;
855 v->attr &= ~CRT_ATTR_BLINK;
858 v->attr |= CRT_ATTR_REVERSE;
861 v->attr &= ~CRT_ATTR_REVERSE;
871 v->color &= ~CRT_COLOR_FG_MASK;
872 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
882 v->color &= ~CRT_COLOR_FG_MASK;
885 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
890 v->color &= ~CRT_COLOR_FG_MASK;
892 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
902 v->color &= ~CRT_COLOR_BG_MASK;
903 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
913 v->color &= ~CRT_COLOR_BG_MASK;
916 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
921 v->color &= ~CRT_COLOR_BG_MASK;
923 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
929 fprintf (stderr, "unhandled SGR %d\n", a);
937 vt102_parse_attr_string (VT102 * v, char *buf, int len)
940 char last = buf[len - 1];
944 memset (num, 0, sizeof (num));
945 o = sizeof (num) - 1;
956 vt102_change_attr (v, &num[o]);
957 memset (num, 0, sizeof (num));
958 o = sizeof (num) - 1;
972 vt102_change_attr (v, &num[o]);
976 vt102_save_state (VT102 * v)
978 v->saved.pos = v->pos;
979 v->saved.attr = v->attr;
980 v->saved.color = v->color;
981 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
985 vt102_restore_state (VT102 * v)
987 v->pos = v->saved.pos;
988 v->attr = v->saved.attr;
989 v->color = v->saved.color;
990 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
991 vt102_cursor_normalize (v);
996 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
1000 vt102_do_pending_wrap (c);
1003 if (v->modes[VT102_MODE_INSERT])
1004 vt102_insert_into_line (v, v->pos);
1006 v->last_reg_char = ch;
1009 if (ch < VT102_CHARSET_SIZE)
1012 if ((cs = vt102_charset_c0[ch]))
1016 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1021 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1022 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1023 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1024 vt102_cursor_advance (c);
1028 vt102_send_id (Context * c, char *buf)
1033 int l = strlen (buf);
1034 c->t->xmit (c->t, buf, l);
1039 vt102_scs (Context * c, int g, int s)
1042 int cs = VT102_CSID_US;
1073 vt102_parse_csi (Context * c, char *buf, int len)
1077 char *arg = buf + 1;
1084 last = buf[len - 1];
1105 vt102_insert_into_line (v, v->pos);
1108 vt102_cursor_relative (v, 0, -narg);
1112 vt102_cursor_relative (v, 0, narg);
1116 vt102_cursor_relative (v, narg, 0);
1119 vt102_cursor_relative (v, -narg, 0);
1122 vt102_cursor_relative (v, 0, narg);
1123 vt102_cursor_carriage_return (v);
1126 vt102_cursor_relative (v, 0, -narg);
1127 vt102_cursor_carriage_return (v);
1130 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1139 ptr = index (arg, ';');
1141 x = atoi (ptr + 1) - 1;
1145 vt102_cursor_absolute (v, x, y);
1150 vt102_cursor_advance_tab (c->v);
1154 narg = 0; /*Different default */
1158 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1161 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1164 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1171 CRT_Pos ls = { 0, v->pos.y };
1172 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1174 narg = 0; /*Different default */
1179 crt_erase (&v->crt, v->pos, le, 1, v->color);
1182 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1185 crt_erase (&v->crt, ls, le, 1, v->color);
1192 if ((v->pos.y >= v->top_margin.y)
1193 && (v->pos.y <= v->bottom_margin.y))
1196 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1202 if ((v->pos.y >= v->top_margin.y)
1203 && (v->pos.y <= v->bottom_margin.y))
1207 vt102_history (c, v->pos, v->bottom_margin);
1208 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1215 vt102_delete_from_line (v, v->pos);
1220 vt102_history (c, v->top_margin, v->bottom_margin);
1221 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1227 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1232 CRT_Pos end = v->pos;
1237 if (end.x > v->bottom_margin.x)
1238 end.x = v->bottom_margin.x;
1240 crt_erase (&v->crt, v->pos, end, 1, v->color);
1245 vt102_cursor_retreat_tab (c->v);
1248 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1252 vt102_regular_char (c, v, v->last_reg_char);
1255 vt102_send_id (c, "\033[?2c");
1258 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1262 narg = 0; /*Different default */
1267 v->tabs[v->pos.x] = 0;
1269 case 2: //FIXME: - LA120 says current line only WTF?
1271 memset (v->tabs, 0, sizeof (v->tabs));
1276 case 'i': //Printer commands
1281 vt102_parse_mode_string (v, &buf[1], len - 1);
1286 vt102_parse_attr_string (v, &buf[1], len - 1);
1288 case 'n': //Device status report
1292 vt102_send_id (c, "\033[0n");
1300 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1301 c->t->xmit (c->t, buf, i);
1305 //?15n printer status
1306 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1310 case 'q': //Load LED on off
1313 v->top_margin = v->screen_start;
1314 v->bottom_margin = v->screen_end;
1316 if ((len > 2) && (ptr = index (arg, ';')))
1319 v->top_margin.y = narg - 1;
1320 v->bottom_margin.y = atoi (ptr) - 1;
1323 if (v->top_margin.y < v->screen_start.y)
1324 v->top_margin.y = v->screen_start.y;
1325 if (v->top_margin.y > v->screen_end.y)
1326 v->top_margin.y = v->screen_end.y;
1327 if (v->bottom_margin.y < v->screen_start.y)
1328 v->bottom_margin.y = v->screen_start.y;
1329 if (v->bottom_margin.y > v->screen_end.y)
1330 v->bottom_margin.y = v->screen_end.y;
1332 vt102_cursor_home (v);
1335 v->saved.pos = v->pos;
1338 v->pos = v->saved.pos;
1339 vt102_cursor_normalize (v);
1340 v->pending_wrap = 0;
1342 case 'y': //Invoke confidence test
1344 case 'z': //request terminal parameters
1348 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1355 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1365 vt102_parse_esc (Context * c)
1368 VT102_parser *p = &v->parser;
1371 p->cmd_buf[p->cmd_ptr] = 0;
1372 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1373 v->pos.y, v->pending_wrap,
1374 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1377 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1381 switch (p->cmd_buf[0])
1384 vt102_cursor_relative (v, 0, -1);
1387 vt102_cursor_relative (v, 0, 1);
1390 vt102_cursor_relative (v, 1, 0);
1393 vt102_cursor_relative (v, -1, 0);
1397 v->g[1] = VT102_CSID_VT52;
1401 v->g[0] = VT102_CSID_US;
1404 vt102_cursor_absolute (v, 0, 0);
1407 vt102_cursor_retreat_line (c);
1410 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1414 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1415 crt_erase (&v->crt, v->pos, le, 1, v->color);
1419 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1421 case 'V': //Print current line
1423 case 'W': //Printer on
1425 case 'X': //printer off
1427 case ']': //print screen
1430 vt102_send_id (c, "\033/K");
1432 case '^': //Autoprint on
1434 case '_': //Autoprint off
1437 v->application_keypad_mode = 1;
1440 v->application_keypad_mode = 0;
1450 /*If you edit this switch don't forget to edit the length and termination tables*/
1451 switch (p->cmd_buf[0])
1454 vt102_cursor_advance_line (c);
1458 vt102_cursor_advance_line (c);
1459 v->pos.x = v->top_margin.x;
1460 vt102_cursor_normalize (v);
1461 v->pending_wrap = 0;
1464 v->tabs[v->pos.x]++;
1467 vt102_cursor_retreat_line (c);
1469 case 'N': //select G2 for one char
1471 case 'O': //select G3 for one char
1474 vt102_send_id (c, terminal_id);
1482 v->application_keypad_mode = 1;
1485 v->application_keypad_mode = 0;
1489 switch (p->cmd_buf[1])
1491 case '3': //top of double height line
1492 case '4': //bottom of double height line
1493 case '5': //single width line
1494 case '6': //double width line
1500 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1502 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1503 v->crt.screen[i].chr = 'E';
1509 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1510 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1515 /*Set ansi mode - ignored */
1519 vt102_save_state (v);
1522 vt102_restore_state (v);
1524 case ']': /*Set various titles */
1528 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1530 case '(': /*Charsets */
1535 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1538 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1539 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1544 p->cmd_buf[p->cmd_ptr] = 0;
1546 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1552 vt102_status_line (VT102 * v, char *str)
1555 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1557 for (i = 0; i < v->current_size.x; ++i)
1559 ca->attr = CRT_ATTR_REVERSE;
1560 ca->color = CRT_COLOR_NORMAL;
1561 ca->chr = *str ? *str : ' ';
1567 for (; i < VT102_MAX_COLS; ++i)
1569 ca->attr = CRT_ATTR_NORMAL;
1570 ca->color = CRT_COLOR_NORMAL;
1579 vt102_parser_reset (VT102_parser * p)
1582 p->cmd_more_bytes = 0;
1583 p->cmd_termination = 0;
1589 vt102_reset_state (VT102 * v)
1591 vt102_parser_reset (&v->parser);
1593 v->attr = CRT_ATTR_NORMAL;
1594 v->color = CRT_COLOR_NORMAL;
1596 v->application_keypad_mode = 0;
1598 v->current_size=v->original_size;
1599 v->crt.size = v->current_size;
1601 v->screen_end = v->current_size;
1605 v->top_margin = v->screen_start;
1606 v->bottom_margin = v->screen_end;
1608 memset (v->modes, 0, VT102_NMODES);
1609 memset (v->private_modes, 0, VT102_NMODES);
1611 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1612 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1613 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1614 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1615 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1617 vt102_reset_tabs (v);
1619 v->g[0] = v->g[1] = VT102_CSID_US;
1624 pre_parse_cmd (int ch, VT102_parser * p)
1626 if (ch > TABLE_LENGTH)
1631 p->cmd_more_bytes = 0;
1634 p->cmd_termination = vt102_cmd_termination[ch];
1635 if (p->cmd_termination)
1641 p->cmd_more_bytes = vt102_cmd_length[ch];
1643 if (p->cmd_more_bytes)
1651 vt102_parse_char (Context * c, int ch)
1654 VT102_parser *p = &v->parser;
1657 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1658 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1659 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1662 /* Turn anything non-ascii into '?' */
1664 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1672 if (p->dca_ptr == 2)
1677 if (ch == SYM_CHAR_RESET)
1679 vt102_reset_state (v);
1681 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1683 p->cmd_buf[p->cmd_ptr++] = ch;
1684 if (p->cmd_ptr == VT102_CMD_LEN)
1686 if (p->cmd_more_bytes)
1688 p->cmd_more_bytes--;
1691 if (!p->cmd_more_bytes == 1)
1695 switch (p->cmd_termination)
1700 if (p->cmd_termination == ch)
1711 vt102_parse_esc (c);
1712 p->cmd_more_bytes = 0;
1713 p->cmd_termination = 0;
1716 else if (p->in_escape && !ctrl_chr (ch, 0))
1719 p->cmd_buf[p->cmd_ptr++] = ch;
1722 pre_parse_cmd (ch, p);
1725 vt102_parse_esc (c);
1727 else if (ch == 0x9b) /*One byte CSI */
1730 p->cmd_buf[p->cmd_ptr++] = '[';
1732 pre_parse_cmd (ch, p);
1739 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1740 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1755 vt102_send_id (c, terminal_id);
1765 vt102_cursor_retreat (c->v);
1768 vt102_cursor_advance_tab (c->v);
1773 vt102_cursor_advance_line (c);
1774 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1777 vt102_cursor_carriage_return (v);
1812 /*regular character */ default:
1813 vt102_regular_char (c, v, ch);
1818 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1823 v->crt.pos = v->pos;
1824 v->crt.hide_cursor =
1825 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1827 if (v->current_line.y != v->pos.y)
1829 vt102_log_line (c, v->current_line.y);
1830 v->current_line = v->pos;
1834 cmd_show_status (c->d, c);
1838 vt102_send (Context * c, uint8_t key)
1846 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1848 if ((key > 31) && (key < 127))
1850 c->t->xmit (c->t, &key, 1);
1869 c->t->xmit (c->t, &key, 1);
1872 c->t->xmit (c->t, &key, 1);
1873 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1876 c->t->xmit (c->t, &ch, 1);
1892 c->t->xmit (c->t, &key, 1);
1900 c->t->xmit (c->t, &key, 1);
1911 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1913 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1914 c->t->xmit (c->t, &buf, sizeof (buf));
1918 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1919 c->t->xmit (c->t, &buf, sizeof (buf));
1937 if (c->v->application_keypad_mode)
1939 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1940 c->t->xmit (c->t, &buf, sizeof (buf));
1944 static char kpoff[KEY_NUM] = {
1963 c->t->xmit (c->t, &kpoff[key], 1);
1967 if (c->v->application_keypad_mode)
1969 uint8_t buf[] = { 033, 'O', 'M' };
1970 c->t->xmit (c->t, &buf, sizeof (buf));
1975 c->t->xmit (c->t, &ch, 1);
1976 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1979 c->t->xmit (c->t, &ch, 1);
1988 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1989 c->t->xmit (c->t, &buf, sizeof (buf));
1997 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1998 c->t->xmit (c->t, &buf, sizeof (buf));
2006 vt102_reset (VT102 * v)
2008 VT102_parser *p = &v->parser;
2012 v->current_line = v->pos;
2013 v->pending_wrap = 0;
2015 v->screen_start.x = 0;
2016 v->screen_start.y = 0;
2017 v->current_size=v->original_size;
2018 v->crt.size=v->current_size;
2020 v->screen_end=v->current_size;
2024 vt102_cursor_home (v);
2025 vt102_status_line (v, "");
2027 vt102_reset_tabs (v);
2028 v->current_line = v->pos;
2030 vt102_parser_reset (p);
2031 vt102_reset_state (v);
2033 vt102_save_state (v);
2035 v->last_reg_char = ' ';
2039 vt102_new (CRT_Pos *size)
2043 v = (VT102 *) malloc (sizeof (VT102));
2049 v->original_size = *size;
2051 if (v->original_size.x<1) v->original_size.x=1;
2052 if (v->original_size.y<1) v->original_size.y=1;
2054 if (v->original_size.x>VT102_MAX_COLS) v->original_size.x=VT102_MAX_COLS;
2055 if (v->original_size.y>VT102_ROWS) v->original_size.y=VT102_ROWS;
2058 v->original_size.x=VT102_COLS_80;
2059 v->original_size.y=VT102_ROWS;
2068 vt102_set_ansi (VT102 * v, int ansi)
2070 v->xn_glitch = ansi ? 0 : 1;
2074 vt102_free (VT102 * v)