4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.44 2008/02/27 00:54:16 james
14 * *** empty log message ***
16 * Revision 1.43 2008/02/27 00:27:22 james
17 * *** empty log message ***
19 * Revision 1.42 2008/02/26 23:56:12 james
20 * *** empty log message ***
22 * Revision 1.41 2008/02/26 23:23:17 james
23 * *** empty log message ***
25 * Revision 1.40 2008/02/26 19:00:59 james
26 * *** empty log message ***
28 * Revision 1.39 2008/02/26 16:54:06 james
29 * *** empty log message ***
31 * Revision 1.38 2008/02/26 16:53:24 james
32 * *** empty log message ***
34 * Revision 1.37 2008/02/24 12:22:53 james
35 * *** empty log message ***
37 * Revision 1.36 2008/02/24 12:22:42 james
38 * *** empty log message ***
40 * Revision 1.35 2008/02/24 00:42:53 james
41 * *** empty log message ***
43 * Revision 1.34 2008/02/23 11:48:37 james
44 * *** empty log message ***
46 * Revision 1.33 2008/02/22 23:39:27 james
47 * *** empty log message ***
49 * Revision 1.32 2008/02/22 19:12:05 james
50 * *** empty log message ***
52 * Revision 1.31 2008/02/22 17:07:00 james
53 * *** empty log message ***
55 * Revision 1.30 2008/02/22 14:51:54 james
56 * *** empty log message ***
58 * Revision 1.29 2008/02/15 03:32:07 james
59 * *** empty log message ***
61 * Revision 1.28 2008/02/14 10:34:30 james
62 * *** empty log message ***
64 * Revision 1.27 2008/02/14 02:46:45 james
65 * *** empty log message ***
67 * Revision 1.26 2008/02/14 01:55:57 james
68 * *** empty log message ***
70 * Revision 1.25 2008/02/13 16:57:29 james
71 * *** empty log message ***
73 * Revision 1.24 2008/02/13 09:12:21 james
74 * *** empty log message ***
76 * Revision 1.23 2008/02/07 13:26:35 james
77 * *** empty log message ***
79 * Revision 1.22 2008/02/07 13:22:51 james
80 * *** empty log message ***
82 * Revision 1.21 2008/02/07 12:21:16 james
83 * *** empty log message ***
85 * Revision 1.20 2008/02/07 12:16:04 james
86 * *** empty log message ***
88 * Revision 1.19 2008/02/07 11:27:02 james
89 * *** empty log message ***
91 * Revision 1.18 2008/02/07 01:59:25 james
92 * *** empty log message ***
94 * Revision 1.17 2008/02/07 01:58:28 james
95 * *** empty log message ***
97 * Revision 1.16 2008/02/07 01:57:46 james
98 * *** empty log message ***
100 * Revision 1.15 2008/02/07 00:43:27 james
101 * *** empty log message ***
103 * Revision 1.14 2008/02/07 00:40:23 james
104 * *** empty log message ***
106 * Revision 1.13 2008/02/07 00:39:59 james
107 * *** empty log message ***
109 * Revision 1.12 2008/02/07 00:39:13 james
110 * *** empty log message ***
112 * Revision 1.11 2008/02/06 20:26:58 james
113 * *** empty log message ***
115 * Revision 1.10 2008/02/06 17:53:28 james
116 * *** empty log message ***
118 * Revision 1.9 2008/02/06 15:53:22 james
119 * *** empty log message ***
121 * Revision 1.8 2008/02/06 11:49:47 james
122 * *** empty log message ***
124 * Revision 1.7 2008/02/06 11:30:37 james
125 * *** empty log message ***
127 * Revision 1.6 2008/02/05 01:11:46 james
128 * *** empty log message ***
130 * Revision 1.5 2008/02/04 20:23:55 james
131 * *** empty log message ***
133 * Revision 1.4 2008/02/04 05:45:55 james
136 * Revision 1.3 2008/02/04 02:05:06 james
137 * *** empty log message ***
139 * Revision 1.2 2008/02/04 01:32:39 james
140 * *** empty log message ***
142 * Revision 1.1 2008/02/03 23:36:41 james
143 * *** empty log message ***
154 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
156 vt100|vt100-am|dec vt100 (w/advanced video):\
158 :co#80:it#8:li#24:vt#3:\
159 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
161 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
162 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
163 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
164 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
165 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
166 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
167 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
168 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
169 :us=\E[4m:tc=vt100+fnkeys:
171 vt100+fnkeys|dec vt100 numeric keypad:\
172 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
175 vt100+pfkeys|dec vt100 numeric keypad:\
176 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
178 vt100+keypad|dec vt100 numeric keypad no fkeys:\
179 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
184 so the parser needs to be able to at least do
246 ansi|ansi/pc-term compatible with color:\
247 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
248 :tc=klone+sgr:tc=ansi-m:
249 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
251 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
252 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
253 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
254 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
255 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
256 :s3=\E+B:ta=\E[I:tc=pcansi-m:
257 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
260 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
261 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
262 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
263 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
265 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
266 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
267 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
268 klone+acs|alternate character set for ansi.sys displays:\
269 :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:\
270 :ae=\E[10m:as=\E[11m:
271 ecma+color|color control for ECMA-48-compatible terminals:\
273 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
276 Ignoreing ones in the VT102 spec
306 #define TABLE_LENGTH 128
310 static char terminal_id[] = "vt102";
312 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
313 int vt102_cmd_length[TABLE_LENGTH] = {
323 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
325 int vt102_cmd_termination[TABLE_LENGTH] = {
347 if ((c >= 'a') && (c <= 'z'))
349 if ((c >= 'A') && (c <= 'Z'))
357 ctrl_chr (int ch, int term)
359 if ((term > 0) && (ch == term))
363 if ((ch > 0) && (ch < 040))
370 in_margins (VT102 * v, CRT_Pos p)
372 if (v->pos.x < v->top_margin.x)
374 if (v->pos.y < v->top_margin.y)
377 if (v->pos.x > v->bottom_margin.x)
379 if (v->pos.y > v->bottom_margin.y)
386 vt102_log_line (Context * c, int line)
388 CRT_Pos e = { c->v->current_width - 1, line };
389 CRT_Pos p = { 0, line };
390 char logbuf[4*(VT102_MAX_COLS + 1)],*logptr=logbuf;
396 for (; e.x > 0; --e.x)
398 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
402 for (; p.x <= e.x; ++p.x)
404 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
407 logptr+=utf8_encode(logptr,ch);
411 c->l->log (c->l, logbuf);
414 /*Called for every upward scroll with same args*/
416 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
418 /*Only log if it scrolls off the top*/
423 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
427 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
442 vt102_cursor_normalize (VT102 * v)
444 CRT_Pos *top, *bottom;
446 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
448 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
452 vt102_clip_cursor (v, v->screen_start, v->screen_end);
458 vt102_cursor_carriage_return (VT102 * v)
460 /*FISH*/ v->pos.x = v->top_margin.x;
465 vt102_cursor_advance_line (Context * c)
468 int couldscroll = in_margins (v, v->pos);
470 /*have wraped off end of last line in scrolling region */
471 /* (|| not necessary, but shuts compiler up */
472 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
475 vt102_log_line (c, v->pos.y);
477 vt102_history (c, v->top_margin, v->bottom_margin);
479 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
483 if (v->pos.y != v->screen_end.y)
490 vt102_cursor_retreat_line (Context * c)
493 int couldscroll = in_margins (v, v->pos);
495 /*have wraped off end of last line in scrolling region */
496 /* (|| not necessary, but shuts compiler up */
497 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
500 vt102_log_line (c, v->pos.y);
502 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
506 if (v->pos.y != v->screen_start.y)
513 vt102_do_pending_wrap (Context * c)
516 int couldscroll = in_margins (v, v->pos);
517 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
519 if (!v->pending_wrap)
523 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
524 v->pos.y, autowrap, couldscroll);
527 /*End of line but no autowrap, nothing to do*/
531 /*End of screen and not allowed to scroll, nothing to do*/
532 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
537 v->pos.x = v->top_margin.x;
544 vt102_cursor_advance_line (c);
548 vt102_cursor_advance (Context * c)
552 if (v->pos.x < v->bottom_margin.x)
560 if (!c->v->xn_glitch)
561 vt102_do_pending_wrap (c);
567 vt102_cursor_retreat (VT102 * v)
569 if (v->pos.x != v->top_margin.x)
578 vt102_reset_tabs (VT102 * v)
582 memset (v->tabs, 0, sizeof (v->tabs));
584 for (i = 0; i < VT102_MAX_COLS; i += 8)
590 vt102_cursor_advance_tab (VT102 * v)
592 if (v->pos.x == v->bottom_margin.x)
594 while (v->pos.x < v->bottom_margin.x)
597 if (v->tabs[v->pos.x])
604 vt102_cursor_retreat_tab (VT102 * v)
606 if (v->pos.x == v->top_margin.x)
608 while (v->pos.x > v->top_margin.x)
611 if (v->tabs[v->pos.x])
617 vt102_cursor_home (VT102 * v)
619 v->pos = v->top_margin;
620 vt102_cursor_normalize (v);
625 vt102_cursor_absolute (VT102 * v, int x, int y)
627 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
629 v->pos.x = x + v->top_margin.x;
630 v->pos.y = y + v->top_margin.y;
637 vt102_cursor_normalize (v);
641 vt102_cursor_relative (VT102 * v, int x, int y)
645 vt102_cursor_normalize (v);
652 vt102_delete_from_line (VT102 * v, CRT_Pos p)
654 int n = v->bottom_margin.x - p.x;
662 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
663 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
666 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
667 /*But not attr due to vt102 bug*/
671 vt102_insert_into_line (VT102 * v, CRT_Pos p)
673 int n = v->bottom_margin.x - p.x;
681 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
682 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
685 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
686 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
687 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
693 vt102_change_mode (VT102 * v, int private, char *ns, int set)
709 if (m >= VT102_NMODES)
714 v->private_modes[m] = set;
717 case VT102_PRIVATE_MODE_CURSOR_MODE:
718 if (v->application_keypad_mode)
719 v->private_modes[m] = 0;
721 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
722 v->private_modes[m], set);
725 case VT102_PRIVATE_MODE_ORIGIN_MODE:
726 vt102_cursor_home (v);
728 case VT102_PRIVATE_MODE_132COLS:
729 /* We don't implement 132 col mode - yet */
733 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
736 v->crt.width = v->current_width;
737 v->screen_end.x = v->current_width - 1;
738 v->top_margin = v->screen_start;
739 v->bottom_margin = v->screen_end;
740 vt102_cursor_home (v);
752 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
757 vt102_parse_mode_string (VT102 * v, char *buf, int len)
760 char last = buf[len - 1];
764 memset (num, 0, sizeof (num));
765 o = sizeof (num) - 1;
783 vt102_change_mode (v, private, &num[o], last == 'h');
784 memset (num, 0, sizeof (num));
785 o = sizeof (num) - 1;
800 vt102_change_mode (v, private, &num[o], last == 'h');
806 vt102_change_attr (VT102 * v, char *na)
823 v->attr = CRT_ATTR_NORMAL;
824 v->color = CRT_COLOR_NORMAL;
827 v->attr |= CRT_ATTR_BOLD;
831 v->attr &= ~CRT_ATTR_BOLD;
834 v->attr |= CRT_ATTR_UNDERLINE;
837 v->attr &= ~CRT_ATTR_UNDERLINE;
840 v->attr |= CRT_ATTR_BLINK;
843 v->attr &= ~CRT_ATTR_BLINK;
846 v->attr |= CRT_ATTR_REVERSE;
849 v->attr &= ~CRT_ATTR_REVERSE;
859 v->color &= ~CRT_COLOR_FG_MASK;
860 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
870 v->color &= ~CRT_COLOR_FG_MASK;
873 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
878 v->color &= ~CRT_COLOR_FG_MASK;
880 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
890 v->color &= ~CRT_COLOR_BG_MASK;
891 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
901 v->color &= ~CRT_COLOR_BG_MASK;
904 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
909 v->color &= ~CRT_COLOR_BG_MASK;
911 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
917 fprintf (stderr, "unhandled SGR %d\n", a);
925 vt102_parse_attr_string (VT102 * v, char *buf, int len)
928 char last = buf[len - 1];
932 memset (num, 0, sizeof (num));
933 o = sizeof (num) - 1;
944 vt102_change_attr (v, &num[o]);
945 memset (num, 0, sizeof (num));
946 o = sizeof (num) - 1;
960 vt102_change_attr (v, &num[o]);
964 vt102_save_state (VT102 * v)
966 v->saved.pos = v->pos;
967 v->saved.attr = v->attr;
968 v->saved.color = v->color;
969 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
973 vt102_restore_state (VT102 * v)
975 v->pos = v->saved.pos;
976 v->attr = v->saved.attr;
977 v->color = v->saved.color;
978 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
979 vt102_cursor_normalize (v);
984 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
988 vt102_do_pending_wrap (c);
991 if (v->modes[VT102_MODE_INSERT])
992 vt102_insert_into_line (v, v->pos);
994 v->last_reg_char = ch;
997 if (ch < VT102_CHARSET_SIZE)
1000 if ((cs=vt102_charset_c0[ch])) {
1002 } else if ((cs=charset_from_csid[v->g[v->cs]][ch])) {
1006 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1007 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1008 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1009 vt102_cursor_advance (c);
1013 vt102_send_id (Context * c, char *buf)
1018 int l = strlen (buf);
1019 c->t->xmit (c->t, buf, l);
1024 vt102_scs (Context * c, int g, int s)
1027 int cs = VT102_CSID_US;
1058 vt102_parse_csi (Context * c, char *buf, int len)
1062 char *arg = buf + 1;
1069 last = buf[len - 1];
1090 vt102_insert_into_line (v, v->pos);
1093 vt102_cursor_relative (v, 0, -narg);
1097 vt102_cursor_relative (v, 0, narg);
1101 vt102_cursor_relative (v, narg, 0);
1104 vt102_cursor_relative (v, -narg, 0);
1107 vt102_cursor_relative (v, 0, narg);
1108 vt102_cursor_carriage_return (v);
1111 vt102_cursor_relative (v, 0, -narg);
1112 vt102_cursor_carriage_return (v);
1115 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1124 ptr = index (arg, ';');
1126 x = atoi (ptr + 1) - 1;
1130 vt102_cursor_absolute (v, x, y);
1135 vt102_cursor_advance_tab (c->v);
1139 narg = 0; /*Different default */
1143 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1146 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1149 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1156 CRT_Pos ls = { 0, v->pos.y };
1157 CRT_Pos le = { v->current_width - 1, v->pos.y };
1159 narg = 0; /*Different default */
1164 crt_erase (&v->crt, v->pos, le, 1, v->color);
1167 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1170 crt_erase (&v->crt, ls, le, 1, v->color);
1177 if ((v->pos.y >= v->top_margin.y)
1178 && (v->pos.y <= v->bottom_margin.y))
1181 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1187 if ((v->pos.y >= v->top_margin.y)
1188 && (v->pos.y <= v->bottom_margin.y))
1192 vt102_history (c, v->pos, v->bottom_margin);
1193 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1200 vt102_delete_from_line (v, v->pos);
1205 vt102_history (c, v->top_margin, v->bottom_margin);
1206 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1212 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1217 CRT_Pos end = v->pos;
1222 if (end.x > v->bottom_margin.x)
1223 end.x = v->bottom_margin.x;
1225 crt_erase (&v->crt, v->pos, end, 1, v->color);
1230 vt102_cursor_retreat_tab (c->v);
1233 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1237 vt102_regular_char (c, v, v->last_reg_char);
1240 vt102_send_id (c, "\033[?2c");
1243 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1247 narg = 0; /*Different default */
1252 v->tabs[v->pos.x] = 0;
1254 case 2: //FIXME: - LA120 says current line only WTF?
1256 memset (v->tabs, 0, sizeof (v->tabs));
1261 case 'i': //Printer commands
1266 vt102_parse_mode_string (v, &buf[1], len - 1);
1271 vt102_parse_attr_string (v, &buf[1], len - 1);
1273 case 'n': //Device status report
1277 vt102_send_id (c, "\033[0n");
1285 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1286 c->t->xmit (c->t, buf, i);
1290 //?15n printer status
1291 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1295 case 'q': //Load LED on off
1298 v->top_margin = v->screen_start;
1299 v->bottom_margin = v->screen_end;
1301 if ((len > 2) && (ptr = index (arg, ';')))
1304 v->top_margin.y = narg - 1;
1305 v->bottom_margin.y = atoi (ptr) - 1;
1308 if (v->top_margin.y < v->screen_start.y)
1309 v->top_margin.y = v->screen_start.y;
1310 if (v->top_margin.y > v->screen_end.y)
1311 v->top_margin.y = v->screen_end.y;
1312 if (v->bottom_margin.y < v->screen_start.y)
1313 v->bottom_margin.y = v->screen_start.y;
1314 if (v->bottom_margin.y > v->screen_end.y)
1315 v->bottom_margin.y = v->screen_end.y;
1317 vt102_cursor_home (v);
1320 v->saved.pos = v->pos;
1323 v->pos = v->saved.pos;
1324 vt102_cursor_normalize (v);
1325 v->pending_wrap = 0;
1327 case 'y': //Invoke confidence test
1329 case 'z': //request terminal parameters
1333 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1340 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1350 vt102_parse_esc (Context * c)
1353 VT102_parser *p = &v->parser;
1356 p->cmd_buf[p->cmd_ptr] = 0;
1357 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1358 v->pos.y, v->pending_wrap,
1359 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1362 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1366 switch (p->cmd_buf[0])
1369 vt102_cursor_relative (v, 0, -1);
1372 vt102_cursor_relative (v, 0, 1);
1375 vt102_cursor_relative (v, 1, 0);
1378 vt102_cursor_relative (v, -1, 0);
1382 v->g[1] = VT102_CSID_VT52;
1386 v->g[0] = VT102_CSID_US;
1389 vt102_cursor_absolute (v, 0, 0);
1392 vt102_cursor_retreat_line (c);
1395 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1399 CRT_Pos le = { v->current_width - 1, v->pos.y };
1400 crt_erase (&v->crt, v->pos, le, 1, v->color);
1404 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1406 case 'V': //Print current line
1408 case 'W': //Printer on
1410 case 'X': //printer off
1412 case ']': //print screen
1415 vt102_send_id (c, "\033/K");
1417 case '^': //Autoprint on
1419 case '_': //Autoprint off
1422 v->application_keypad_mode = 1;
1425 v->application_keypad_mode = 0;
1435 /*If you edit this switch don't forget to edit the length and termination tables*/
1436 switch (p->cmd_buf[0])
1439 vt102_cursor_advance_line (c);
1443 vt102_cursor_advance_line (c);
1444 v->pos.x = v->top_margin.x;
1445 vt102_cursor_normalize (v);
1446 v->pending_wrap = 0;
1449 v->tabs[v->pos.x]++;
1452 vt102_cursor_retreat_line (c);
1454 case 'N': //select G2 for one char
1456 case 'O': //select G3 for one char
1459 vt102_send_id (c, terminal_id);
1467 v->application_keypad_mode = 1;
1470 v->application_keypad_mode = 0;
1474 switch (p->cmd_buf[1])
1476 case '3': //top of double height line
1477 case '4': //bottom of double height line
1478 case '5': //single width line
1479 case '6': //double width line
1485 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1487 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1488 v->crt.screen[i].chr = 'E';
1494 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1495 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1500 /*Set ansi mode - ignored */
1504 vt102_save_state (v);
1507 vt102_restore_state (v);
1509 case ']': /*Set various titles */
1513 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1515 case '(': /*Charsets */
1520 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1523 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1524 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1529 p->cmd_buf[p->cmd_ptr] = 0;
1531 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1537 vt102_status_line (VT102 * v, char *str)
1539 int i = v->current_width - 1;
1540 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1542 for (i = 0; i < v->current_width; ++i)
1544 ca->attr = CRT_ATTR_REVERSE;
1545 ca->color = CRT_COLOR_NORMAL;
1546 ca->chr = *str ? *str : ' ';
1552 for (; i < VT102_MAX_COLS; ++i)
1554 ca->attr = CRT_ATTR_NORMAL;
1555 ca->color = CRT_COLOR_NORMAL;
1564 vt102_parser_reset (VT102_parser * p)
1567 p->cmd_more_bytes = 0;
1568 p->cmd_termination = 0;
1574 vt102_reset_state (VT102 * v)
1576 vt102_parser_reset (&v->parser);
1578 v->attr = CRT_ATTR_NORMAL;
1579 v->color = CRT_COLOR_NORMAL;
1581 v->application_keypad_mode = 0;
1583 v->current_width = VT102_COLS_80;
1584 v->crt.width = v->current_width;
1585 v->screen_end.x = v->current_width - 1;
1587 v->top_margin = v->screen_start;
1588 v->bottom_margin = v->screen_end;
1590 memset (v->modes, 0, VT102_NMODES);
1591 memset (v->private_modes, 0, VT102_NMODES);
1593 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1594 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1595 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1596 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1597 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1599 vt102_reset_tabs (v);
1601 v->g[0] = v->g[1] = VT102_CSID_US;
1606 pre_parse_cmd (int ch, VT102_parser * p)
1608 if (ch > TABLE_LENGTH)
1613 p->cmd_more_bytes = 0;
1616 p->cmd_termination = vt102_cmd_termination[ch];
1617 if (p->cmd_termination)
1623 p->cmd_more_bytes = vt102_cmd_length[ch];
1625 if (p->cmd_more_bytes)
1633 vt102_parse_char (Context * c, int ch)
1636 VT102_parser *p = &v->parser;
1639 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1640 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1641 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1644 /* Turn anything non-ascii into '?' */
1646 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1654 if (p->dca_ptr == 2)
1659 if (ch == SYM_CHAR_RESET)
1661 vt102_reset_state (v);
1663 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1665 p->cmd_buf[p->cmd_ptr++] = ch;
1666 if (p->cmd_ptr == VT102_CMD_LEN)
1668 if (p->cmd_more_bytes)
1670 p->cmd_more_bytes--;
1673 if (!p->cmd_more_bytes == 1)
1677 switch (p->cmd_termination)
1682 if (p->cmd_termination == ch)
1693 vt102_parse_esc (c);
1694 p->cmd_more_bytes = 0;
1695 p->cmd_termination = 0;
1698 else if (p->in_escape && !ctrl_chr (ch, 0))
1701 p->cmd_buf[p->cmd_ptr++] = ch;
1704 pre_parse_cmd (ch, p);
1707 vt102_parse_esc (c);
1709 else if (ch == 0x9b) /*One byte CSI */
1712 p->cmd_buf[p->cmd_ptr++] = '[';
1714 pre_parse_cmd (ch, p);
1721 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1722 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1737 vt102_send_id (c, terminal_id);
1747 vt102_cursor_retreat (c->v);
1750 vt102_cursor_advance_tab (c->v);
1755 vt102_cursor_advance_line (c);
1756 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1759 vt102_cursor_carriage_return (v);
1794 /*regular character */ default:
1795 vt102_regular_char (c, v, ch);
1800 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1805 v->crt.pos = v->pos;
1806 v->crt.hide_cursor =
1807 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1809 if (v->current_line.y != v->pos.y)
1811 vt102_log_line (c, v->current_line.y);
1812 v->current_line = v->pos;
1816 cmd_show_status (c->d, c);
1820 vt102_send (Context * c, uint8_t key)
1828 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1830 if ((key > 31) && (key < 127))
1832 c->t->xmit (c->t, &key, 1);
1851 c->t->xmit (c->t, &key, 1);
1854 c->t->xmit (c->t, &key, 1);
1855 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1858 c->t->xmit (c->t, &ch, 1);
1874 c->t->xmit (c->t, &key, 1);
1882 c->t->xmit (c->t, &key, 1);
1893 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1895 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1896 c->t->xmit (c->t, &buf, sizeof (buf));
1900 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1901 c->t->xmit (c->t, &buf, sizeof (buf));
1919 if (c->v->application_keypad_mode)
1921 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1922 c->t->xmit (c->t, &buf, sizeof (buf));
1926 static char kpoff[KEY_NUM] = {
1945 c->t->xmit (c->t, &kpoff[key], 1);
1949 if (c->v->application_keypad_mode)
1951 uint8_t buf[] = { 033, 'O', 'M' };
1952 c->t->xmit (c->t, &buf, sizeof (buf));
1957 c->t->xmit (c->t, &ch, 1);
1958 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1961 c->t->xmit (c->t, &ch, 1);
1970 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1971 c->t->xmit (c->t, &buf, sizeof (buf));
1979 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1980 c->t->xmit (c->t, &buf, sizeof (buf));
1988 vt102_reset (VT102 * v)
1990 VT102_parser *p = &v->parser;
1994 v->current_line = v->pos;
1995 v->pending_wrap = 0;
1997 v->screen_start.x = 0;
1998 v->screen_start.y = 0;
1999 v->current_width = VT102_COLS_80;
2000 v->crt.width = v->current_width;
2001 v->screen_end.x = v->current_width - 1;
2002 v->screen_end.y = VT102_ROWS - 1;
2004 vt102_cursor_home (v);
2005 vt102_status_line (v, "");
2007 vt102_reset_tabs (v);
2008 v->current_line = v->pos;
2010 vt102_parser_reset (p);
2011 vt102_reset_state (v);
2013 vt102_save_state (v);
2015 v->last_reg_char = ' ';
2023 v = (VT102 *) malloc (sizeof (VT102));
2034 vt102_set_ansi (VT102 * v, int ansi)
2036 v->xn_glitch = ansi ? 0 : 1;
2040 vt102_free (VT102 * v)