4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.43 2008/02/27 00:27:22 james
14 * *** empty log message ***
16 * Revision 1.42 2008/02/26 23:56:12 james
17 * *** empty log message ***
19 * Revision 1.41 2008/02/26 23:23:17 james
20 * *** empty log message ***
22 * Revision 1.40 2008/02/26 19:00:59 james
23 * *** empty log message ***
25 * Revision 1.39 2008/02/26 16:54:06 james
26 * *** empty log message ***
28 * Revision 1.38 2008/02/26 16:53:24 james
29 * *** empty log message ***
31 * Revision 1.37 2008/02/24 12:22:53 james
32 * *** empty log message ***
34 * Revision 1.36 2008/02/24 12:22:42 james
35 * *** empty log message ***
37 * Revision 1.35 2008/02/24 00:42:53 james
38 * *** empty log message ***
40 * Revision 1.34 2008/02/23 11:48:37 james
41 * *** empty log message ***
43 * Revision 1.33 2008/02/22 23:39:27 james
44 * *** empty log message ***
46 * Revision 1.32 2008/02/22 19:12:05 james
47 * *** empty log message ***
49 * Revision 1.31 2008/02/22 17:07:00 james
50 * *** empty log message ***
52 * Revision 1.30 2008/02/22 14:51:54 james
53 * *** empty log message ***
55 * Revision 1.29 2008/02/15 03:32:07 james
56 * *** empty log message ***
58 * Revision 1.28 2008/02/14 10:34:30 james
59 * *** empty log message ***
61 * Revision 1.27 2008/02/14 02:46:45 james
62 * *** empty log message ***
64 * Revision 1.26 2008/02/14 01:55:57 james
65 * *** empty log message ***
67 * Revision 1.25 2008/02/13 16:57:29 james
68 * *** empty log message ***
70 * Revision 1.24 2008/02/13 09:12:21 james
71 * *** empty log message ***
73 * Revision 1.23 2008/02/07 13:26:35 james
74 * *** empty log message ***
76 * Revision 1.22 2008/02/07 13:22:51 james
77 * *** empty log message ***
79 * Revision 1.21 2008/02/07 12:21:16 james
80 * *** empty log message ***
82 * Revision 1.20 2008/02/07 12:16:04 james
83 * *** empty log message ***
85 * Revision 1.19 2008/02/07 11:27:02 james
86 * *** empty log message ***
88 * Revision 1.18 2008/02/07 01:59:25 james
89 * *** empty log message ***
91 * Revision 1.17 2008/02/07 01:58:28 james
92 * *** empty log message ***
94 * Revision 1.16 2008/02/07 01:57:46 james
95 * *** empty log message ***
97 * Revision 1.15 2008/02/07 00:43:27 james
98 * *** empty log message ***
100 * Revision 1.14 2008/02/07 00:40:23 james
101 * *** empty log message ***
103 * Revision 1.13 2008/02/07 00:39:59 james
104 * *** empty log message ***
106 * Revision 1.12 2008/02/07 00:39:13 james
107 * *** empty log message ***
109 * Revision 1.11 2008/02/06 20:26:58 james
110 * *** empty log message ***
112 * Revision 1.10 2008/02/06 17:53:28 james
113 * *** empty log message ***
115 * Revision 1.9 2008/02/06 15:53:22 james
116 * *** empty log message ***
118 * Revision 1.8 2008/02/06 11:49:47 james
119 * *** empty log message ***
121 * Revision 1.7 2008/02/06 11:30:37 james
122 * *** empty log message ***
124 * Revision 1.6 2008/02/05 01:11:46 james
125 * *** empty log message ***
127 * Revision 1.5 2008/02/04 20:23:55 james
128 * *** empty log message ***
130 * Revision 1.4 2008/02/04 05:45:55 james
133 * Revision 1.3 2008/02/04 02:05:06 james
134 * *** empty log message ***
136 * Revision 1.2 2008/02/04 01:32:39 james
137 * *** empty log message ***
139 * Revision 1.1 2008/02/03 23:36:41 james
140 * *** empty log message ***
151 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
153 vt100|vt100-am|dec vt100 (w/advanced video):\
155 :co#80:it#8:li#24:vt#3:\
156 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
158 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
159 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
160 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
161 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
162 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
163 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
164 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
165 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
166 :us=\E[4m:tc=vt100+fnkeys:
168 vt100+fnkeys|dec vt100 numeric keypad:\
169 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
172 vt100+pfkeys|dec vt100 numeric keypad:\
173 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
175 vt100+keypad|dec vt100 numeric keypad no fkeys:\
176 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
181 so the parser needs to be able to at least do
243 ansi|ansi/pc-term compatible with color:\
244 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
245 :tc=klone+sgr:tc=ansi-m:
246 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
248 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
249 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
250 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
251 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
252 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
253 :s3=\E+B:ta=\E[I:tc=pcansi-m:
254 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
257 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
258 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
259 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
260 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
262 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
263 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
264 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
265 klone+acs|alternate character set for ansi.sys displays:\
266 :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:\
267 :ae=\E[10m:as=\E[11m:
268 ecma+color|color control for ECMA-48-compatible terminals:\
270 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
273 Ignoreing ones in the VT102 spec
303 #define TABLE_LENGTH 128
307 static char terminal_id[] = "vt102";
309 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
310 int vt102_cmd_length[TABLE_LENGTH] = {
320 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
322 int vt102_cmd_termination[TABLE_LENGTH] = {
344 if ((c >= 'a') && (c <= 'z'))
346 if ((c >= 'A') && (c <= 'Z'))
354 ctrl_chr (int ch, int term)
356 if ((term > 0) && (ch == term))
360 if ((ch > 0) && (ch < 040))
367 in_margins (VT102 * v, CRT_Pos p)
369 if (v->pos.x < v->top_margin.x)
371 if (v->pos.y < v->top_margin.y)
374 if (v->pos.x > v->bottom_margin.x)
376 if (v->pos.y > v->bottom_margin.y)
383 vt102_log_line (Context * c, int line)
385 CRT_Pos e = { c->v->current_width - 1, line };
386 CRT_Pos p = { 0, line };
387 char logbuf[VT102_MAX_COLS + 1];
393 for (; e.x > 0; --e.x)
395 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
399 for (; p.x <= e.x; ++p.x)
401 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
410 c->l->log (c->l, logbuf);
413 /*Called for every upward scroll with same args*/
415 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
417 /*Only log if it scrolls off the top*/
422 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
426 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
441 vt102_cursor_normalize (VT102 * v)
443 CRT_Pos *top, *bottom;
445 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
447 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
451 vt102_clip_cursor (v, v->screen_start, v->screen_end);
457 vt102_cursor_carriage_return (VT102 * v)
459 /*FISH*/ v->pos.x = v->top_margin.x;
464 vt102_cursor_advance_line (Context * c)
467 int couldscroll = in_margins (v, v->pos);
469 /*have wraped off end of last line in scrolling region */
470 /* (|| not necessary, but shuts compiler up */
471 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
474 vt102_log_line (c, v->pos.y);
476 vt102_history (c, v->top_margin, v->bottom_margin);
478 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
482 if (v->pos.y != v->screen_end.y)
489 vt102_cursor_retreat_line (Context * c)
492 int couldscroll = in_margins (v, v->pos);
494 /*have wraped off end of last line in scrolling region */
495 /* (|| not necessary, but shuts compiler up */
496 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
499 vt102_log_line (c, v->pos.y);
501 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
505 if (v->pos.y != v->screen_start.y)
512 vt102_do_pending_wrap (Context * c)
515 int couldscroll = in_margins (v, v->pos);
516 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
518 if (!v->pending_wrap)
522 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
523 v->pos.y, autowrap, couldscroll);
526 /*End of line but no autowrap, nothing to do*/
530 /*End of screen and not allowed to scroll, nothing to do*/
531 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
536 v->pos.x = v->top_margin.x;
543 vt102_cursor_advance_line (c);
547 vt102_cursor_advance (Context * c)
551 if (v->pos.x < v->bottom_margin.x)
559 if (!c->v->xn_glitch)
560 vt102_do_pending_wrap (c);
566 vt102_cursor_retreat (VT102 * v)
568 if (v->pos.x != v->top_margin.x)
577 vt102_reset_tabs (VT102 * v)
581 memset (v->tabs, 0, sizeof (v->tabs));
583 for (i = 0; i < VT102_MAX_COLS; i += 8)
589 vt102_cursor_advance_tab (VT102 * v)
591 if (v->pos.x == v->bottom_margin.x)
593 while (v->pos.x < v->bottom_margin.x)
596 if (v->tabs[v->pos.x])
603 vt102_cursor_retreat_tab (VT102 * v)
605 if (v->pos.x == v->top_margin.x)
607 while (v->pos.x > v->top_margin.x)
610 if (v->tabs[v->pos.x])
616 vt102_cursor_home (VT102 * v)
618 v->pos = v->top_margin;
619 vt102_cursor_normalize (v);
624 vt102_cursor_absolute (VT102 * v, int x, int y)
626 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
628 v->pos.x = x + v->top_margin.x;
629 v->pos.y = y + v->top_margin.y;
636 vt102_cursor_normalize (v);
640 vt102_cursor_relative (VT102 * v, int x, int y)
644 vt102_cursor_normalize (v);
651 vt102_delete_from_line (VT102 * v, CRT_Pos p)
653 int n = v->bottom_margin.x - p.x;
661 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
662 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
665 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
666 /*But not attr due to vt102 bug*/
670 vt102_insert_into_line (VT102 * v, CRT_Pos p)
672 int n = v->bottom_margin.x - p.x;
680 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
681 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
684 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
685 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
686 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
692 vt102_change_mode (VT102 * v, int private, char *ns, int set)
708 if (m >= VT102_NMODES)
713 v->private_modes[m] = set;
716 case VT102_PRIVATE_MODE_CURSOR_MODE:
717 if (v->application_keypad_mode)
718 v->private_modes[m] = 0;
720 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
721 v->private_modes[m], set);
724 case VT102_PRIVATE_MODE_ORIGIN_MODE:
725 vt102_cursor_home (v);
727 case VT102_PRIVATE_MODE_132COLS:
728 /* We don't implement 132 col mode - yet */
732 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
735 v->crt.width = v->current_width;
736 v->screen_end.x = v->current_width - 1;
737 v->top_margin = v->screen_start;
738 v->bottom_margin = v->screen_end;
739 vt102_cursor_home (v);
751 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
756 vt102_parse_mode_string (VT102 * v, char *buf, int len)
759 char last = buf[len - 1];
763 memset (num, 0, sizeof (num));
764 o = sizeof (num) - 1;
782 vt102_change_mode (v, private, &num[o], last == 'h');
783 memset (num, 0, sizeof (num));
784 o = sizeof (num) - 1;
799 vt102_change_mode (v, private, &num[o], last == 'h');
805 vt102_change_attr (VT102 * v, char *na)
822 v->attr = CRT_ATTR_NORMAL;
823 v->color = CRT_COLOR_NORMAL;
826 v->attr |= CRT_ATTR_BOLD;
830 v->attr &= ~CRT_ATTR_BOLD;
833 v->attr |= CRT_ATTR_UNDERLINE;
836 v->attr &= ~CRT_ATTR_UNDERLINE;
839 v->attr |= CRT_ATTR_BLINK;
842 v->attr &= ~CRT_ATTR_BLINK;
845 v->attr |= CRT_ATTR_REVERSE;
848 v->attr &= ~CRT_ATTR_REVERSE;
858 v->color &= ~CRT_COLOR_FG_MASK;
859 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
869 v->color &= ~CRT_COLOR_FG_MASK;
872 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
877 v->color &= ~CRT_COLOR_FG_MASK;
879 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
889 v->color &= ~CRT_COLOR_BG_MASK;
890 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
900 v->color &= ~CRT_COLOR_BG_MASK;
903 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
908 v->color &= ~CRT_COLOR_BG_MASK;
910 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
916 fprintf (stderr, "unhandled SGR %d\n", a);
924 vt102_parse_attr_string (VT102 * v, char *buf, int len)
927 char last = buf[len - 1];
931 memset (num, 0, sizeof (num));
932 o = sizeof (num) - 1;
943 vt102_change_attr (v, &num[o]);
944 memset (num, 0, sizeof (num));
945 o = sizeof (num) - 1;
959 vt102_change_attr (v, &num[o]);
963 vt102_save_state (VT102 * v)
965 v->saved.pos = v->pos;
966 v->saved.attr = v->attr;
967 v->saved.color = v->color;
968 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
972 vt102_restore_state (VT102 * v)
974 v->pos = v->saved.pos;
975 v->attr = v->saved.attr;
976 v->color = v->saved.color;
977 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
978 vt102_cursor_normalize (v);
983 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
986 vt102_do_pending_wrap (c);
988 if (v->modes[VT102_MODE_INSERT])
989 vt102_insert_into_line (v, v->pos);
991 if (ch < VT102_CHARSET_SIZE)
993 int cs = charset_from_csid[v->g[v->cs]][ch];
994 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = cs ? cs : ch;
998 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1000 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1001 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1002 vt102_cursor_advance (c);
1004 v->last_reg_char = ch;
1007 vt102_send_id (Context * c, char *buf)
1012 int l = strlen (buf);
1013 c->t->xmit (c->t, buf, l);
1018 vt102_scs (Context * c, int g, int s)
1021 int cs = VT102_CSID_US;
1052 vt102_parse_csi (Context * c, char *buf, int len)
1056 char *arg = buf + 1;
1063 last = buf[len - 1];
1084 vt102_insert_into_line (v, v->pos);
1087 vt102_cursor_relative (v, 0, -narg);
1091 vt102_cursor_relative (v, 0, narg);
1095 vt102_cursor_relative (v, narg, 0);
1098 vt102_cursor_relative (v, -narg, 0);
1101 vt102_cursor_relative (v, 0, narg);
1102 vt102_cursor_carriage_return (v);
1105 vt102_cursor_relative (v, 0, -narg);
1106 vt102_cursor_carriage_return (v);
1109 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1118 ptr = index (arg, ';');
1120 x = atoi (ptr + 1) - 1;
1124 vt102_cursor_absolute (v, x, y);
1129 vt102_cursor_advance_tab (c->v);
1133 narg = 0; /*Different default */
1137 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1140 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1143 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1150 CRT_Pos ls = { 0, v->pos.y };
1151 CRT_Pos le = { v->current_width - 1, v->pos.y };
1153 narg = 0; /*Different default */
1158 crt_erase (&v->crt, v->pos, le, 1, v->color);
1161 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1164 crt_erase (&v->crt, ls, le, 1, v->color);
1171 if ((v->pos.y >= v->top_margin.y)
1172 && (v->pos.y <= v->bottom_margin.y))
1175 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1181 if ((v->pos.y >= v->top_margin.y)
1182 && (v->pos.y <= v->bottom_margin.y))
1186 vt102_history (c, v->pos, v->bottom_margin);
1187 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1194 vt102_delete_from_line (v, v->pos);
1199 vt102_history (c, v->top_margin, v->bottom_margin);
1200 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1206 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1211 CRT_Pos end = v->pos;
1216 if (end.x > v->bottom_margin.x)
1217 end.x = v->bottom_margin.x;
1219 crt_erase (&v->crt, v->pos, end, 1, v->color);
1224 vt102_cursor_retreat_tab (c->v);
1227 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1231 vt102_regular_char (c, v, v->last_reg_char);
1234 vt102_send_id (c, "\033[?2c");
1237 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1241 narg = 0; /*Different default */
1246 v->tabs[v->pos.x] = 0;
1248 case 2: //FIXME: - LA120 says current line only WTF?
1250 memset (v->tabs, 0, sizeof (v->tabs));
1255 case 'i': //Printer commands
1260 vt102_parse_mode_string (v, &buf[1], len - 1);
1265 vt102_parse_attr_string (v, &buf[1], len - 1);
1267 case 'n': //Device status report
1271 vt102_send_id (c, "\033[0n");
1279 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1280 c->t->xmit (c->t, buf, i);
1284 //?15n printer status
1285 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1289 case 'q': //Load LED on off
1292 v->top_margin = v->screen_start;
1293 v->bottom_margin = v->screen_end;
1295 if ((len > 2) && (ptr = index (arg, ';')))
1298 v->top_margin.y = narg - 1;
1299 v->bottom_margin.y = atoi (ptr) - 1;
1302 if (v->top_margin.y < v->screen_start.y)
1303 v->top_margin.y = v->screen_start.y;
1304 if (v->top_margin.y > v->screen_end.y)
1305 v->top_margin.y = v->screen_end.y;
1306 if (v->bottom_margin.y < v->screen_start.y)
1307 v->bottom_margin.y = v->screen_start.y;
1308 if (v->bottom_margin.y > v->screen_end.y)
1309 v->bottom_margin.y = v->screen_end.y;
1311 vt102_cursor_home (v);
1314 v->saved.pos = v->pos;
1317 v->pos = v->saved.pos;
1318 vt102_cursor_normalize (v);
1319 v->pending_wrap = 0;
1321 case 'y': //Invoke confidence test
1323 case 'z': //request terminal parameters
1327 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1334 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1344 vt102_parse_esc (Context * c)
1347 VT102_parser *p = &v->parser;
1350 p->cmd_buf[p->cmd_ptr] = 0;
1351 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1352 v->pos.y, v->pending_wrap,
1353 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1356 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1360 switch (p->cmd_buf[0])
1363 vt102_cursor_relative (v, 0, -1);
1366 vt102_cursor_relative (v, 0, 1);
1369 vt102_cursor_relative (v, 1, 0);
1372 vt102_cursor_relative (v, -1, 0);
1376 v->g[1] = VT102_CSID_VT52;
1380 v->g[0] = VT102_CSID_US;
1383 vt102_cursor_absolute (v, 0, 0);
1386 vt102_cursor_retreat_line (c);
1389 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1393 CRT_Pos le = { v->current_width - 1, v->pos.y };
1394 crt_erase (&v->crt, v->pos, le, 1, v->color);
1398 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1400 case 'V': //Print current line
1402 case 'W': //Printer on
1404 case 'X': //printer off
1406 case ']': //print screen
1409 vt102_send_id (c, "\033/K");
1411 case '^': //Autoprint on
1413 case '_': //Autoprint off
1416 v->application_keypad_mode = 1;
1419 v->application_keypad_mode = 0;
1429 /*If you edit this switch don't forget to edit the length and termination tables*/
1430 switch (p->cmd_buf[0])
1433 vt102_cursor_advance_line (c);
1437 vt102_cursor_advance_line (c);
1438 v->pos.x = v->top_margin.x;
1439 vt102_cursor_normalize (v);
1440 v->pending_wrap = 0;
1443 v->tabs[v->pos.x]++;
1446 vt102_cursor_retreat_line (c);
1448 case 'N': //select G2 for one char
1450 case 'O': //select G3 for one char
1453 vt102_send_id (c, terminal_id);
1461 v->application_keypad_mode = 1;
1464 v->application_keypad_mode = 0;
1468 switch (p->cmd_buf[1])
1470 case '3': //top of double height line
1471 case '4': //bottom of double height line
1472 case '5': //single width line
1473 case '6': //double width line
1479 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1481 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1482 v->crt.screen[i].chr = 'E';
1488 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1489 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1494 /*Set ansi mode - ignored */
1498 vt102_save_state (v);
1501 vt102_restore_state (v);
1503 case ']': /*Set various titles */
1507 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1509 case '(': /*Charsets */
1514 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1517 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1518 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1523 p->cmd_buf[p->cmd_ptr] = 0;
1525 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1531 vt102_status_line (VT102 * v, char *str)
1533 int i = v->current_width - 1;
1534 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1536 for (i = 0; i < v->current_width; ++i)
1538 ca->attr = CRT_ATTR_REVERSE;
1539 ca->color = CRT_COLOR_NORMAL;
1540 ca->chr = *str ? *str : ' ';
1546 for (; i < VT102_MAX_COLS; ++i)
1548 ca->attr = CRT_ATTR_NORMAL;
1549 ca->color = CRT_COLOR_NORMAL;
1558 vt102_parser_reset (VT102_parser * p)
1561 p->cmd_more_bytes = 0;
1562 p->cmd_termination = 0;
1568 vt102_reset_state (VT102 * v)
1570 vt102_parser_reset (&v->parser);
1572 v->attr = CRT_ATTR_NORMAL;
1573 v->color = CRT_COLOR_NORMAL;
1575 v->application_keypad_mode = 0;
1577 v->current_width = VT102_COLS_80;
1578 v->crt.width = v->current_width;
1579 v->screen_end.x = v->current_width - 1;
1581 v->top_margin = v->screen_start;
1582 v->bottom_margin = v->screen_end;
1584 memset (v->modes, 0, VT102_NMODES);
1585 memset (v->private_modes, 0, VT102_NMODES);
1587 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1588 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1589 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1590 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1591 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1593 vt102_reset_tabs (v);
1595 v->g[0] = v->g[1] = VT102_CSID_US;
1600 pre_parse_cmd (int ch, VT102_parser * p)
1602 if (ch > TABLE_LENGTH)
1607 p->cmd_more_bytes = 0;
1610 p->cmd_termination = vt102_cmd_termination[ch];
1611 if (p->cmd_termination)
1617 p->cmd_more_bytes = vt102_cmd_length[ch];
1619 if (p->cmd_more_bytes)
1627 vt102_parse_char (Context * c, int ch)
1630 VT102_parser *p = &v->parser;
1633 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1634 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1635 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1638 /* Turn anything non-ascii into '?' */
1640 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1648 if (p->dca_ptr == 2)
1653 if (ch == SYM_CHAR_RESET)
1655 vt102_reset_state (v);
1657 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1659 p->cmd_buf[p->cmd_ptr++] = ch;
1660 if (p->cmd_ptr == VT102_CMD_LEN)
1662 if (p->cmd_more_bytes)
1664 p->cmd_more_bytes--;
1667 if (!p->cmd_more_bytes == 1)
1671 switch (p->cmd_termination)
1676 if (p->cmd_termination == ch)
1687 vt102_parse_esc (c);
1688 p->cmd_more_bytes = 0;
1689 p->cmd_termination = 0;
1692 else if (p->in_escape && !ctrl_chr (ch, 0))
1695 p->cmd_buf[p->cmd_ptr++] = ch;
1698 pre_parse_cmd (ch, p);
1701 vt102_parse_esc (c);
1703 else if (ch == 0x9b) /*One byte CSI */
1706 p->cmd_buf[p->cmd_ptr++] = '[';
1708 pre_parse_cmd (ch, p);
1715 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1716 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1729 vt102_send_id (c, terminal_id);
1735 vt102_cursor_retreat (c->v);
1738 vt102_cursor_advance_tab (c->v);
1743 vt102_cursor_advance_line (c);
1744 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1747 vt102_cursor_carriage_return (v);
1778 /*regular character */ default:
1779 vt102_regular_char (c, v, ch);
1784 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1789 v->crt.pos = v->pos;
1790 v->crt.hide_cursor =
1791 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1793 if (v->current_line.y != v->pos.y)
1795 vt102_log_line (c, v->current_line.y);
1796 v->current_line = v->pos;
1800 cmd_show_status (c->d, c);
1804 vt102_send (Context * c, uint8_t key)
1812 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1814 if ((key > 31) && (key < 127))
1816 c->t->xmit (c->t, &key, 1);
1835 c->t->xmit (c->t, &key, 1);
1838 c->t->xmit (c->t, &key, 1);
1839 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1842 c->t->xmit (c->t, &ch, 1);
1858 c->t->xmit (c->t, &key, 1);
1866 c->t->xmit (c->t, &key, 1);
1877 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1879 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1880 c->t->xmit (c->t, &buf, sizeof (buf));
1884 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1885 c->t->xmit (c->t, &buf, sizeof (buf));
1903 if (c->v->application_keypad_mode)
1905 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1906 c->t->xmit (c->t, &buf, sizeof (buf));
1910 static char kpoff[KEY_NUM] = {
1929 c->t->xmit (c->t, &kpoff[key], 1);
1933 if (c->v->application_keypad_mode)
1935 uint8_t buf[] = { 033, 'O', 'M' };
1936 c->t->xmit (c->t, &buf, sizeof (buf));
1941 c->t->xmit (c->t, &ch, 1);
1942 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1945 c->t->xmit (c->t, &ch, 1);
1954 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1955 c->t->xmit (c->t, &buf, sizeof (buf));
1963 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1964 c->t->xmit (c->t, &buf, sizeof (buf));
1972 vt102_reset (VT102 * v)
1974 VT102_parser *p = &v->parser;
1978 v->current_line = v->pos;
1979 v->pending_wrap = 0;
1981 v->screen_start.x = 0;
1982 v->screen_start.y = 0;
1983 v->current_width = VT102_COLS_80;
1984 v->crt.width = v->current_width;
1985 v->screen_end.x = v->current_width - 1;
1986 v->screen_end.y = VT102_ROWS - 1;
1988 vt102_cursor_home (v);
1989 vt102_status_line (v, "");
1991 vt102_reset_tabs (v);
1992 v->current_line = v->pos;
1994 vt102_parser_reset (p);
1995 vt102_reset_state (v);
1997 vt102_save_state (v);
1999 v->last_reg_char = ' ';
2007 v = (VT102 *) malloc (sizeof (VT102));
2018 vt102_set_ansi (VT102 * v, int ansi)
2020 v->xn_glitch = ansi ? 0 : 1;
2024 vt102_free (VT102 * v)