4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.42 2008/02/26 23:56:12 james
14 * *** empty log message ***
16 * Revision 1.41 2008/02/26 23:23:17 james
17 * *** empty log message ***
19 * Revision 1.40 2008/02/26 19:00:59 james
20 * *** empty log message ***
22 * Revision 1.39 2008/02/26 16:54:06 james
23 * *** empty log message ***
25 * Revision 1.38 2008/02/26 16:53:24 james
26 * *** empty log message ***
28 * Revision 1.37 2008/02/24 12:22:53 james
29 * *** empty log message ***
31 * Revision 1.36 2008/02/24 12:22:42 james
32 * *** empty log message ***
34 * Revision 1.35 2008/02/24 00:42:53 james
35 * *** empty log message ***
37 * Revision 1.34 2008/02/23 11:48:37 james
38 * *** empty log message ***
40 * Revision 1.33 2008/02/22 23:39:27 james
41 * *** empty log message ***
43 * Revision 1.32 2008/02/22 19:12:05 james
44 * *** empty log message ***
46 * Revision 1.31 2008/02/22 17:07:00 james
47 * *** empty log message ***
49 * Revision 1.30 2008/02/22 14:51:54 james
50 * *** empty log message ***
52 * Revision 1.29 2008/02/15 03:32:07 james
53 * *** empty log message ***
55 * Revision 1.28 2008/02/14 10:34:30 james
56 * *** empty log message ***
58 * Revision 1.27 2008/02/14 02:46:45 james
59 * *** empty log message ***
61 * Revision 1.26 2008/02/14 01:55:57 james
62 * *** empty log message ***
64 * Revision 1.25 2008/02/13 16:57:29 james
65 * *** empty log message ***
67 * Revision 1.24 2008/02/13 09:12:21 james
68 * *** empty log message ***
70 * Revision 1.23 2008/02/07 13:26:35 james
71 * *** empty log message ***
73 * Revision 1.22 2008/02/07 13:22:51 james
74 * *** empty log message ***
76 * Revision 1.21 2008/02/07 12:21:16 james
77 * *** empty log message ***
79 * Revision 1.20 2008/02/07 12:16:04 james
80 * *** empty log message ***
82 * Revision 1.19 2008/02/07 11:27:02 james
83 * *** empty log message ***
85 * Revision 1.18 2008/02/07 01:59:25 james
86 * *** empty log message ***
88 * Revision 1.17 2008/02/07 01:58:28 james
89 * *** empty log message ***
91 * Revision 1.16 2008/02/07 01:57:46 james
92 * *** empty log message ***
94 * Revision 1.15 2008/02/07 00:43:27 james
95 * *** empty log message ***
97 * Revision 1.14 2008/02/07 00:40:23 james
98 * *** empty log message ***
100 * Revision 1.13 2008/02/07 00:39:59 james
101 * *** empty log message ***
103 * Revision 1.12 2008/02/07 00:39:13 james
104 * *** empty log message ***
106 * Revision 1.11 2008/02/06 20:26:58 james
107 * *** empty log message ***
109 * Revision 1.10 2008/02/06 17:53:28 james
110 * *** empty log message ***
112 * Revision 1.9 2008/02/06 15:53:22 james
113 * *** empty log message ***
115 * Revision 1.8 2008/02/06 11:49:47 james
116 * *** empty log message ***
118 * Revision 1.7 2008/02/06 11:30:37 james
119 * *** empty log message ***
121 * Revision 1.6 2008/02/05 01:11:46 james
122 * *** empty log message ***
124 * Revision 1.5 2008/02/04 20:23:55 james
125 * *** empty log message ***
127 * Revision 1.4 2008/02/04 05:45:55 james
130 * Revision 1.3 2008/02/04 02:05:06 james
131 * *** empty log message ***
133 * Revision 1.2 2008/02/04 01:32:39 james
134 * *** empty log message ***
136 * Revision 1.1 2008/02/03 23:36:41 james
137 * *** empty log message ***
148 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
150 vt100|vt100-am|dec vt100 (w/advanced video):\
152 :co#80:it#8:li#24:vt#3:\
153 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
155 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
156 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
157 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
158 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
159 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
160 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
161 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
162 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
163 :us=\E[4m:tc=vt100+fnkeys:
165 vt100+fnkeys|dec vt100 numeric keypad:\
166 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
169 vt100+pfkeys|dec vt100 numeric keypad:\
170 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
172 vt100+keypad|dec vt100 numeric keypad no fkeys:\
173 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
178 so the parser needs to be able to at least do
240 ansi|ansi/pc-term compatible with color:\
241 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
242 :tc=klone+sgr:tc=ansi-m:
243 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
245 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
246 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
247 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
248 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
249 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
250 :s3=\E+B:ta=\E[I:tc=pcansi-m:
251 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
254 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
255 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
256 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
257 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
259 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
260 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
261 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
262 klone+acs|alternate character set for ansi.sys displays:\
263 :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:\
264 :ae=\E[10m:as=\E[11m:
265 ecma+color|color control for ECMA-48-compatible terminals:\
267 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
270 Ignoreing ones in the VT102 spec
300 //static char terminal_reply[]={033,0133,077,066,0143};
302 #define TABLE_LENGTH 128
304 static char terminal_id[] = "vt102";
306 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
307 int vt102_cmd_length[TABLE_LENGTH] = {
317 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
319 int vt102_cmd_termination[TABLE_LENGTH] = {
341 if ((c >= 'a') && (c <= 'z'))
343 if ((c >= 'A') && (c <= 'Z'))
351 ctrl_chr (int ch, int term)
353 if ((term > 0) && (ch == term))
357 if ((ch > 0) && (ch < 040))
364 in_margins (VT102 * v, CRT_Pos p)
366 if (v->pos.x < v->top_margin.x)
368 if (v->pos.y < v->top_margin.y)
371 if (v->pos.x > v->bottom_margin.x)
373 if (v->pos.y > v->bottom_margin.y)
380 vt102_log_line (Context * c, int line)
382 CRT_Pos e = { c->v->current_width - 1, line };
383 CRT_Pos p = { 0, line };
384 char logbuf[VT102_MAX_COLS + 1];
390 for (; e.x > 0; --e.x)
392 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
396 for (; p.x <= e.x; ++p.x)
398 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
407 c->l->log (c->l, logbuf);
410 /*Called for every upward scroll with same args*/
412 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
414 /*Only log if it scrolls off the top*/
419 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
423 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
438 vt102_cursor_normalize (VT102 * v)
440 CRT_Pos *top, *bottom;
442 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
444 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
448 vt102_clip_cursor (v, v->screen_start, v->screen_end);
454 vt102_cursor_carriage_return (VT102 * v)
456 /*FISH*/ v->pos.x = v->top_margin.x;
461 vt102_cursor_advance_line (Context * c)
464 int couldscroll = in_margins (v, v->pos);
466 /*have wraped off end of last line in scrolling region */
467 /* (|| not necessary, but shuts compiler up */
468 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
471 vt102_log_line (c, v->pos.y);
473 vt102_history (c, v->top_margin, v->bottom_margin);
475 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
479 if (v->pos.y != v->screen_end.y)
486 vt102_cursor_retreat_line (Context * c)
489 int couldscroll = in_margins (v, v->pos);
491 /*have wraped off end of last line in scrolling region */
492 /* (|| not necessary, but shuts compiler up */
493 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
496 vt102_log_line (c, v->pos.y);
498 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
502 if (v->pos.y != v->screen_start.y)
509 vt102_do_pending_wrap (Context * c)
512 int couldscroll = in_margins (v, v->pos);
513 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
515 if (!v->pending_wrap)
519 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
520 v->pos.y, autowrap, couldscroll);
523 /*End of line but no autowrap, nothing to do*/
527 /*End of screen and not allowed to scroll, nothing to do*/
528 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
533 v->pos.x = v->top_margin.x;
540 vt102_cursor_advance_line (c);
544 vt102_cursor_advance (Context * c)
548 if (v->pos.x < v->bottom_margin.x)
556 if (!c->v->xn_glitch)
557 vt102_do_pending_wrap (c);
563 vt102_cursor_retreat (VT102 * v)
565 if (v->pos.x != v->top_margin.x)
574 vt102_reset_tabs (VT102 * v)
578 memset (v->tabs, 0, sizeof (v->tabs));
580 for (i = 0; i < VT102_MAX_COLS; i += 8)
586 vt102_cursor_advance_tab (VT102 * v)
588 if (v->pos.x == v->bottom_margin.x)
590 while (v->pos.x < v->bottom_margin.x)
593 if (v->tabs[v->pos.x])
600 vt102_cursor_retreat_tab (VT102 * v)
602 if (v->pos.x == v->top_margin.x)
604 while (v->pos.x > v->top_margin.x)
607 if (v->tabs[v->pos.x])
613 vt102_cursor_home (VT102 * v)
615 v->pos = v->top_margin;
616 vt102_cursor_normalize (v);
621 vt102_cursor_absolute (VT102 * v, int x, int y)
623 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
625 v->pos.x = x + v->top_margin.x;
626 v->pos.y = y + v->top_margin.y;
633 vt102_cursor_normalize (v);
637 vt102_cursor_relative (VT102 * v, int x, int y)
641 vt102_cursor_normalize (v);
648 vt102_delete_from_line (VT102 * v, CRT_Pos p)
650 int n = v->bottom_margin.x - p.x;
658 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
659 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
662 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
663 /*But not attr due to vt102 bug*/
667 vt102_insert_into_line (VT102 * v, CRT_Pos p)
669 int n = v->bottom_margin.x - p.x;
677 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
678 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
681 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
682 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
683 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
689 vt102_change_mode (VT102 * v, int private, char *ns, int set)
705 if (m >= VT102_NMODES)
710 v->private_modes[m] = set;
713 case VT102_PRIVATE_MODE_CURSOR_MODE:
714 if (v->application_keypad_mode)
715 v->private_modes[m] = 0;
717 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
718 v->private_modes[m], set);
721 case VT102_PRIVATE_MODE_ORIGIN_MODE:
722 vt102_cursor_home (v);
724 case VT102_PRIVATE_MODE_132COLS:
725 /* We don't implement 132 col mode - yet */
729 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
732 v->crt.width = v->current_width;
733 v->screen_end.x = v->current_width - 1;
734 v->top_margin = v->screen_start;
735 v->bottom_margin = v->screen_end;
736 vt102_cursor_home (v);
748 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
753 vt102_parse_mode_string (VT102 * v, char *buf, int len)
756 char last = buf[len - 1];
760 memset (num, 0, sizeof (num));
761 o = sizeof (num) - 1;
779 vt102_change_mode (v, private, &num[o], last == 'h');
780 memset (num, 0, sizeof (num));
781 o = sizeof (num) - 1;
796 vt102_change_mode (v, private, &num[o], last == 'h');
802 vt102_change_attr (VT102 * v, char *na)
819 v->attr = CRT_ATTR_NORMAL;
820 v->color = CRT_COLOR_NORMAL;
823 v->attr |= CRT_ATTR_BOLD;
827 v->attr &= ~CRT_ATTR_BOLD;
830 v->attr |= CRT_ATTR_UNDERLINE;
833 v->attr &= ~CRT_ATTR_UNDERLINE;
836 v->attr |= CRT_ATTR_BLINK;
839 v->attr &= ~CRT_ATTR_BLINK;
842 v->attr |= CRT_ATTR_REVERSE;
845 v->attr &= ~CRT_ATTR_REVERSE;
855 v->color &= ~CRT_COLOR_FG_MASK;
856 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
866 v->color &= ~CRT_COLOR_FG_MASK;
869 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
874 v->color &= ~CRT_COLOR_FG_MASK;
876 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
886 v->color &= ~CRT_COLOR_BG_MASK;
887 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
897 v->color &= ~CRT_COLOR_BG_MASK;
900 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
905 v->color &= ~CRT_COLOR_BG_MASK;
907 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
913 fprintf (stderr, "unhandled SGR %d\n", a);
921 vt102_parse_attr_string (VT102 * v, char *buf, int len)
924 char last = buf[len - 1];
928 memset (num, 0, sizeof (num));
929 o = sizeof (num) - 1;
940 vt102_change_attr (v, &num[o]);
941 memset (num, 0, sizeof (num));
942 o = sizeof (num) - 1;
956 vt102_change_attr (v, &num[o]);
960 vt102_save_state (VT102 * v)
962 v->saved.pos = v->pos;
963 v->saved.attr = v->attr;
964 v->saved.color = v->color;
965 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
969 vt102_restore_state (VT102 * v)
971 v->pos = v->saved.pos;
972 v->attr = v->saved.attr;
973 v->color = v->saved.color;
974 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
975 vt102_cursor_normalize (v);
980 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
983 vt102_do_pending_wrap (c);
985 if (v->modes[VT102_MODE_INSERT])
986 vt102_insert_into_line (v, v->pos);
988 if (ch < VT102_CHARSET_SIZE)
990 int cs = charset_from_csid[v->g[v->cs]][ch];
991 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = cs ? cs : ch;
995 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
997 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
998 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
999 vt102_cursor_advance (c);
1001 v->last_reg_char = ch;
1004 vt102_send_id (Context * c, char *buf)
1009 int l = strlen (buf);
1010 c->t->xmit (c->t, buf, l);
1015 vt102_scs (Context * c, int g, int s)
1018 int cs = VT102_CSID_US;
1049 vt102_parse_csi (Context * c, char *buf, int len)
1053 char *arg = buf + 1;
1060 last = buf[len - 1];
1081 vt102_insert_into_line (v, v->pos);
1084 vt102_cursor_relative (v, 0, -narg);
1088 vt102_cursor_relative (v, 0, narg);
1092 vt102_cursor_relative (v, narg, 0);
1095 vt102_cursor_relative (v, -narg, 0);
1098 vt102_cursor_relative (v, 0, narg);
1099 vt102_cursor_carriage_return (v);
1102 vt102_cursor_relative (v, 0, -narg);
1103 vt102_cursor_carriage_return (v);
1106 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1115 ptr = index (arg, ';');
1117 x = atoi (ptr + 1) - 1;
1121 vt102_cursor_absolute (v, x, y);
1126 vt102_cursor_advance_tab (c->v);
1130 narg = 0; /*Different default */
1134 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1137 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1140 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1147 CRT_Pos ls = { 0, v->pos.y };
1148 CRT_Pos le = { v->current_width - 1, v->pos.y };
1150 narg = 0; /*Different default */
1155 crt_erase (&v->crt, v->pos, le, 1, v->color);
1158 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1161 crt_erase (&v->crt, ls, le, 1, v->color);
1168 if ((v->pos.y >= v->top_margin.y)
1169 && (v->pos.y <= v->bottom_margin.y))
1172 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1178 if ((v->pos.y >= v->top_margin.y)
1179 && (v->pos.y <= v->bottom_margin.y))
1183 vt102_history (c, v->pos, v->bottom_margin);
1184 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1191 vt102_delete_from_line (v, v->pos);
1196 vt102_history (c, v->top_margin, v->bottom_margin);
1197 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1203 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1208 CRT_Pos end = v->pos;
1213 if (end.x > v->bottom_margin.x)
1214 end.x = v->bottom_margin.x;
1216 crt_erase (&v->crt, v->pos, end, 1, v->color);
1221 vt102_cursor_retreat_tab (c->v);
1224 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1228 vt102_regular_char (c, v, v->last_reg_char);
1231 vt102_send_id (c, "\033[?2c");
1234 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1238 narg = 0; /*Different default */
1243 v->tabs[v->pos.x] = 0;
1245 case 2: //FIXME: - LA120 says current line only WTF?
1247 memset (v->tabs, 0, sizeof (v->tabs));
1252 case 'i': //Printer commands
1257 vt102_parse_mode_string (v, &buf[1], len - 1);
1262 vt102_parse_attr_string (v, &buf[1], len - 1);
1264 case 'n': //Device status report
1267 vt102_send_id (c, "\033[0n");
1273 i=sprintf(buf,"\033[%d;%dR",v->pos.y+1,v->pos.x+1);
1274 c->t->xmit (c->t, buf, i);
1278 //?15n printer status
1279 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__, __LINE__,
1283 case 'q': //Load LED on off
1286 v->top_margin = v->screen_start;
1287 v->bottom_margin = v->screen_end;
1289 if ((len > 2) && (ptr = index (arg, ';')))
1292 v->top_margin.y = narg - 1;
1293 v->bottom_margin.y = atoi (ptr) - 1;
1296 if (v->top_margin.y < v->screen_start.y)
1297 v->top_margin.y = v->screen_start.y;
1298 if (v->top_margin.y > v->screen_end.y)
1299 v->top_margin.y = v->screen_end.y;
1300 if (v->bottom_margin.y < v->screen_start.y)
1301 v->bottom_margin.y = v->screen_start.y;
1302 if (v->bottom_margin.y > v->screen_end.y)
1303 v->bottom_margin.y = v->screen_end.y;
1305 vt102_cursor_home (v);
1308 v->saved.pos = v->pos;
1311 v->pos = v->saved.pos;
1312 vt102_cursor_normalize (v);
1313 v->pending_wrap = 0;
1315 case 'y': //Invoke confidence test
1317 case 'z': //request terminal parameters
1321 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1328 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1338 vt102_parse_esc (Context * c)
1341 VT102_parser *p = &v->parser;
1344 p->cmd_buf[p->cmd_ptr] = 0;
1345 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x, v->pos.y,
1346 v->pending_wrap, !v->private_modes[VT102_PRIVATE_MODE_VT52],p->cmd_buf);
1349 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1353 switch (p->cmd_buf[0])
1356 vt102_cursor_relative (v, 0, -1);
1359 vt102_cursor_relative (v, 0, 1);
1362 vt102_cursor_relative (v, 1, 0);
1365 vt102_cursor_relative (v, -1, 0);
1369 v->g[1] = VT102_CSID_VT52;
1373 v->g[0] = VT102_CSID_US;
1376 vt102_cursor_absolute (v, 0, 0);
1379 vt102_cursor_retreat_line (c);
1382 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1386 CRT_Pos le = { v->current_width - 1, v->pos.y };
1387 crt_erase (&v->crt, v->pos, le, 1, v->color);
1391 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1393 case 'V': //Print current line
1395 case 'W': //Printer on
1397 case 'X': //printer off
1399 case ']': //print screen
1402 vt102_send_id (c, "\033/K");
1404 case '^': //Autoprint on
1406 case '_': //Autoprint off
1409 v->application_keypad_mode = 1;
1412 v->application_keypad_mode = 0;
1422 /*If you edit this switch don't forget to edit the length and termination tables*/
1423 switch (p->cmd_buf[0])
1426 vt102_cursor_advance_line (c);
1430 vt102_cursor_advance_line (c);
1431 v->pos.x = v->top_margin.x;
1432 vt102_cursor_normalize (v);
1433 v->pending_wrap = 0;
1436 v->tabs[v->pos.x]++;
1439 vt102_cursor_retreat_line (c);
1441 case 'N': //select G2 for one char
1443 case 'O': //select G3 for one char
1446 vt102_send_id (c, terminal_id);
1454 v->application_keypad_mode = 1;
1457 v->application_keypad_mode = 0;
1461 switch (p->cmd_buf[1])
1463 case '3': //top of double height line
1464 case '4': //bottom of double height line
1465 case '5': //single width line
1466 case '6': //double width line
1472 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1474 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1475 v->crt.screen[i].chr = 'E';
1481 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1482 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1487 /*Set ansi mode - ignored */
1491 vt102_save_state (v);
1494 vt102_restore_state (v);
1496 case ']': /*Set various titles */
1500 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1502 case '(': /*Charsets */
1507 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1510 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1511 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1516 p->cmd_buf[p->cmd_ptr] = 0;
1517 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1522 vt102_status_line (VT102 * v, char *str)
1524 int i = v->current_width - 1;
1525 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1527 for (i = 0; i < v->current_width; ++i)
1529 ca->attr = CRT_ATTR_REVERSE;
1530 ca->color = CRT_COLOR_NORMAL;
1531 ca->chr = *str ? *str : ' ';
1537 for (; i < VT102_MAX_COLS; ++i)
1539 ca->attr = CRT_ATTR_NORMAL;
1540 ca->color = CRT_COLOR_NORMAL;
1549 vt102_parser_reset (VT102_parser * p)
1552 p->cmd_more_bytes = 0;
1553 p->cmd_termination = 0;
1559 vt102_reset_state (VT102 * v)
1561 vt102_parser_reset (&v->parser);
1563 v->attr = CRT_ATTR_NORMAL;
1564 v->color = CRT_COLOR_NORMAL;
1566 v->application_keypad_mode = 0;
1568 v->current_width = VT102_COLS_80;
1569 v->crt.width = v->current_width;
1570 v->screen_end.x = v->current_width - 1;
1572 v->top_margin = v->screen_start;
1573 v->bottom_margin = v->screen_end;
1575 memset (v->modes, 0, VT102_NMODES);
1576 memset (v->private_modes, 0, VT102_NMODES);
1578 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1579 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1580 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1581 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1582 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1584 vt102_reset_tabs (v);
1586 v->g[0] = v->g[1] = VT102_CSID_US;
1591 pre_parse_cmd (int ch, VT102_parser * p)
1593 if (ch > TABLE_LENGTH)
1598 p->cmd_more_bytes = 0;
1601 p->cmd_termination = vt102_cmd_termination[ch];
1602 if (p->cmd_termination)
1608 p->cmd_more_bytes = vt102_cmd_length[ch];
1610 if (p->cmd_more_bytes)
1618 vt102_parse_char (Context * c, int ch)
1621 VT102_parser *p = &v->parser;
1624 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1625 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1626 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1629 /* Turn anything non-ascii into '?' */
1631 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1639 if (p->dca_ptr == 2)
1644 if (ch == SYM_CHAR_RESET)
1646 vt102_reset_state (v);
1648 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1650 p->cmd_buf[p->cmd_ptr++] = ch;
1651 if (p->cmd_ptr == VT102_CMD_LEN)
1653 if (p->cmd_more_bytes)
1655 p->cmd_more_bytes--;
1658 if (!p->cmd_more_bytes == 1)
1662 switch (p->cmd_termination)
1667 if (p->cmd_termination == ch)
1678 vt102_parse_esc (c);
1679 p->cmd_more_bytes = 0;
1680 p->cmd_termination = 0;
1683 else if (p->in_escape && !ctrl_chr (ch, 0))
1686 p->cmd_buf[p->cmd_ptr++] = ch;
1689 pre_parse_cmd (ch, p);
1692 vt102_parse_esc (c);
1694 else if (ch == 0x9b) /*One byte CSI */
1697 p->cmd_buf[p->cmd_ptr++] = '[';
1699 pre_parse_cmd (ch, p);
1706 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1707 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1720 vt102_send_id (c, terminal_id);
1726 vt102_cursor_retreat (c->v);
1729 vt102_cursor_advance_tab (c->v);
1734 vt102_cursor_advance_line (c);
1735 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1738 vt102_cursor_carriage_return (v);
1769 /*regular character */ default:
1770 vt102_regular_char (c, v, ch);
1775 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1780 v->crt.pos = v->pos;
1781 v->crt.hide_cursor =
1782 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1784 if (v->current_line.y != v->pos.y)
1786 vt102_log_line (c, v->current_line.y);
1787 v->current_line = v->pos;
1791 cmd_show_status (c->d, c);
1795 vt102_send (Context * c, uint8_t key)
1803 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1805 if ((key > 31) && (key < 127))
1807 c->t->xmit (c->t, &key, 1);
1826 c->t->xmit (c->t, &key, 1);
1829 c->t->xmit (c->t, &key, 1);
1830 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1833 c->t->xmit (c->t, &ch, 1);
1849 c->t->xmit (c->t, &key, 1);
1857 c->t->xmit (c->t, &key, 1);
1868 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1870 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1871 c->t->xmit (c->t, &buf, sizeof (buf));
1875 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1876 c->t->xmit (c->t, &buf, sizeof (buf));
1894 if (c->v->application_keypad_mode)
1896 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1897 c->t->xmit (c->t, &buf, sizeof (buf));
1901 static char kpoff[KEY_NUM] = {
1920 c->t->xmit (c->t, &kpoff[key], 1);
1924 if (c->v->application_keypad_mode)
1926 uint8_t buf[] = { 033, 'O', 'M' };
1927 c->t->xmit (c->t, &buf, sizeof (buf));
1932 c->t->xmit (c->t, &ch, 1);
1933 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1936 c->t->xmit (c->t, &ch, 1);
1945 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1946 c->t->xmit (c->t, &buf, sizeof (buf));
1954 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1955 c->t->xmit (c->t, &buf, sizeof (buf));
1963 vt102_reset (VT102 * v)
1965 VT102_parser *p = &v->parser;
1969 v->current_line = v->pos;
1970 v->pending_wrap = 0;
1972 v->screen_start.x = 0;
1973 v->screen_start.y = 0;
1974 v->current_width = VT102_COLS_80;
1975 v->crt.width = v->current_width;
1976 v->screen_end.x = v->current_width - 1;
1977 v->screen_end.y = VT102_ROWS - 1;
1979 vt102_cursor_home (v);
1980 vt102_status_line (v, "");
1982 vt102_reset_tabs (v);
1983 v->current_line = v->pos;
1985 vt102_parser_reset (p);
1986 vt102_reset_state (v);
1988 vt102_save_state (v);
1990 v->last_reg_char = ' ';
1998 v = (VT102 *) malloc (sizeof (VT102));
2009 vt102_set_ansi (VT102 * v, int ansi)
2011 v->xn_glitch = ansi ? 0 : 1;
2015 vt102_free (VT102 * v)