4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.51 2008/02/28 16:37:16 james
14 * *** empty log message ***
16 * Revision 1.50 2008/02/28 12:12:25 james
17 * *** empty log message ***
19 * Revision 1.49 2008/02/28 11:27:48 james
20 * *** empty log message ***
22 * Revision 1.48 2008/02/27 09:42:53 james
23 * *** empty log message ***
25 * Revision 1.47 2008/02/27 09:42:22 james
26 * *** empty log message ***
28 * Revision 1.46 2008/02/27 01:31:38 james
29 * *** empty log message ***
31 * Revision 1.45 2008/02/27 01:31:14 james
32 * *** empty log message ***
34 * Revision 1.44 2008/02/27 00:54:16 james
35 * *** empty log message ***
37 * Revision 1.43 2008/02/27 00:27:22 james
38 * *** empty log message ***
40 * Revision 1.42 2008/02/26 23:56:12 james
41 * *** empty log message ***
43 * Revision 1.41 2008/02/26 23:23:17 james
44 * *** empty log message ***
46 * Revision 1.40 2008/02/26 19:00:59 james
47 * *** empty log message ***
49 * Revision 1.39 2008/02/26 16:54:06 james
50 * *** empty log message ***
52 * Revision 1.38 2008/02/26 16:53:24 james
53 * *** empty log message ***
55 * Revision 1.37 2008/02/24 12:22:53 james
56 * *** empty log message ***
58 * Revision 1.36 2008/02/24 12:22:42 james
59 * *** empty log message ***
61 * Revision 1.35 2008/02/24 00:42:53 james
62 * *** empty log message ***
64 * Revision 1.34 2008/02/23 11:48:37 james
65 * *** empty log message ***
67 * Revision 1.33 2008/02/22 23:39:27 james
68 * *** empty log message ***
70 * Revision 1.32 2008/02/22 19:12:05 james
71 * *** empty log message ***
73 * Revision 1.31 2008/02/22 17:07:00 james
74 * *** empty log message ***
76 * Revision 1.30 2008/02/22 14:51:54 james
77 * *** empty log message ***
79 * Revision 1.29 2008/02/15 03:32:07 james
80 * *** empty log message ***
82 * Revision 1.28 2008/02/14 10:34:30 james
83 * *** empty log message ***
85 * Revision 1.27 2008/02/14 02:46:45 james
86 * *** empty log message ***
88 * Revision 1.26 2008/02/14 01:55:57 james
89 * *** empty log message ***
91 * Revision 1.25 2008/02/13 16:57:29 james
92 * *** empty log message ***
94 * Revision 1.24 2008/02/13 09:12:21 james
95 * *** empty log message ***
97 * Revision 1.23 2008/02/07 13:26:35 james
98 * *** empty log message ***
100 * Revision 1.22 2008/02/07 13:22:51 james
101 * *** empty log message ***
103 * Revision 1.21 2008/02/07 12:21:16 james
104 * *** empty log message ***
106 * Revision 1.20 2008/02/07 12:16:04 james
107 * *** empty log message ***
109 * Revision 1.19 2008/02/07 11:27:02 james
110 * *** empty log message ***
112 * Revision 1.18 2008/02/07 01:59:25 james
113 * *** empty log message ***
115 * Revision 1.17 2008/02/07 01:58:28 james
116 * *** empty log message ***
118 * Revision 1.16 2008/02/07 01:57:46 james
119 * *** empty log message ***
121 * Revision 1.15 2008/02/07 00:43:27 james
122 * *** empty log message ***
124 * Revision 1.14 2008/02/07 00:40:23 james
125 * *** empty log message ***
127 * Revision 1.13 2008/02/07 00:39:59 james
128 * *** empty log message ***
130 * Revision 1.12 2008/02/07 00:39:13 james
131 * *** empty log message ***
133 * Revision 1.11 2008/02/06 20:26:58 james
134 * *** empty log message ***
136 * Revision 1.10 2008/02/06 17:53:28 james
137 * *** empty log message ***
139 * Revision 1.9 2008/02/06 15:53:22 james
140 * *** empty log message ***
142 * Revision 1.8 2008/02/06 11:49:47 james
143 * *** empty log message ***
145 * Revision 1.7 2008/02/06 11:30:37 james
146 * *** empty log message ***
148 * Revision 1.6 2008/02/05 01:11:46 james
149 * *** empty log message ***
151 * Revision 1.5 2008/02/04 20:23:55 james
152 * *** empty log message ***
154 * Revision 1.4 2008/02/04 05:45:55 james
157 * Revision 1.3 2008/02/04 02:05:06 james
158 * *** empty log message ***
160 * Revision 1.2 2008/02/04 01:32:39 james
161 * *** empty log message ***
163 * Revision 1.1 2008/02/03 23:36:41 james
164 * *** empty log message ***
175 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
177 vt100|vt100-am|dec vt100 (w/advanced video):\
179 :co#80:it#8:li#24:vt#3:\
180 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
182 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
183 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
184 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
185 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
186 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
187 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
188 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
189 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
190 :us=\E[4m:tc=vt100+fnkeys:
192 vt100+fnkeys|dec vt100 numeric keypad:\
193 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
196 vt100+pfkeys|dec vt100 numeric keypad:\
197 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
199 vt100+keypad|dec vt100 numeric keypad no fkeys:\
200 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
205 so the parser needs to be able to at least do
267 ansi|ansi/pc-term compatible with color:\
268 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
269 :tc=klone+sgr:tc=ansi-m:
270 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
272 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
273 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
274 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
275 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
276 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
277 :s3=\E+B:ta=\E[I:tc=pcansi-m:
278 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
281 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
282 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
283 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
284 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
286 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
287 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
288 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
289 klone+acs|alternate character set for ansi.sys displays:\
290 :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:\
291 :ae=\E[10m:as=\E[11m:
292 ecma+color|color control for ECMA-48-compatible terminals:\
294 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
297 Ignoreing ones in the VT102 spec
327 #define TABLE_LENGTH 128
331 static char terminal_id[] = "vt102";
333 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
334 int vt102_cmd_length[TABLE_LENGTH] = {
344 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
346 int vt102_cmd_termination[TABLE_LENGTH] = {
368 if ((c >= 'a') && (c <= 'z'))
370 if ((c >= 'A') && (c <= 'Z'))
378 ctrl_chr (int ch, int term)
380 if ((term > 0) && (ch == term))
384 if ((ch > 0) && (ch < 040))
391 in_margins (VT102 * v, CRT_Pos p)
393 if (v->pos.x < v->top_margin.x)
395 if (v->pos.y < v->top_margin.y)
398 if (v->pos.x > v->bottom_margin.x)
400 if (v->pos.y > v->bottom_margin.y)
405 void vt102_crt_update(Context *c)
411 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
413 if (v->current_line.y != v->pos.y)
415 vt102_log_line (c, v->current_line.y);
416 v->current_line = v->pos;
420 cmd_show_status (c->d, c);
422 void vt102_do_resize(Context *c)
427 v->crt.size = v->current_size;
429 v->screen_end = v->current_size;
432 v->top_margin = v->screen_start;
433 v->bottom_margin = v->screen_end;
434 vt102_cursor_home (v);
438 tty_winch(c->t,v->current_size);
440 log_f(c->l,"<size now %dx%d>", v->current_size.x,v->current_size.y);
446 vt102_log_line (Context * c, int line)
448 CRT_Pos e = { c->v->current_size.x - 1, line };
449 CRT_Pos p = { 0, line };
450 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
456 for (; e.x > 0; --e.x)
458 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
462 for (; p.x <= e.x; ++p.x)
464 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
467 logptr += utf8_encode (logptr, ch);
471 c->l->log (c->l, logbuf);
474 /*Called for every upward scroll with same args*/
476 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
478 /*Only log if it scrolls off the top*/
483 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
487 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
502 vt102_cursor_normalize (VT102 * v)
504 CRT_Pos *top, *bottom;
506 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
508 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
512 vt102_clip_cursor (v, v->screen_start, v->screen_end);
518 vt102_cursor_carriage_return (VT102 * v)
520 /*FISH*/ v->pos.x = v->top_margin.x;
525 vt102_cursor_advance_line (Context * c)
528 int couldscroll = in_margins (v, v->pos);
530 /*have wraped off end of last line in scrolling region */
531 /* (|| not necessary, but shuts compiler up */
532 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
535 vt102_log_line (c, v->pos.y);
537 vt102_history (c, v->top_margin, v->bottom_margin);
539 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
543 if (v->pos.y != v->screen_end.y)
550 vt102_cursor_retreat_line (Context * c)
553 int couldscroll = in_margins (v, v->pos);
555 /*have wraped off end of last line in scrolling region */
556 /* (|| not necessary, but shuts compiler up */
557 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
560 vt102_log_line (c, v->pos.y);
562 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
566 if (v->pos.y != v->screen_start.y)
573 vt102_do_pending_wrap (Context * c)
576 int couldscroll = in_margins (v, v->pos);
577 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
579 if (!v->pending_wrap)
583 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
584 v->pos.y, autowrap, couldscroll);
587 /*End of line but no autowrap, nothing to do*/
591 /*End of screen and not allowed to scroll, nothing to do*/
592 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
597 v->pos.x = v->top_margin.x;
604 vt102_cursor_advance_line (c);
608 vt102_cursor_advance (Context * c)
612 if (v->pos.x < v->bottom_margin.x)
620 if (!c->v->xn_glitch)
621 vt102_do_pending_wrap (c);
627 vt102_cursor_retreat (VT102 * v)
629 if (v->pos.x != v->top_margin.x)
638 vt102_reset_tabs (VT102 * v)
642 memset (v->tabs, 0, sizeof (v->tabs));
644 for (i = 0; i < VT102_MAX_COLS; i += 8)
650 vt102_cursor_advance_tab (VT102 * v)
652 if (v->pos.x == v->bottom_margin.x)
654 while (v->pos.x < v->bottom_margin.x)
657 if (v->tabs[v->pos.x])
664 vt102_cursor_retreat_tab (VT102 * v)
666 if (v->pos.x == v->top_margin.x)
668 while (v->pos.x > v->top_margin.x)
671 if (v->tabs[v->pos.x])
677 vt102_cursor_home (VT102 * v)
679 v->pos = v->top_margin;
680 vt102_cursor_normalize (v);
685 vt102_cursor_absolute (VT102 * v, int x, int y)
687 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
689 v->pos.x = x + v->top_margin.x;
690 v->pos.y = y + v->top_margin.y;
697 vt102_cursor_normalize (v);
701 vt102_cursor_relative (VT102 * v, int x, int y)
705 vt102_cursor_normalize (v);
712 vt102_delete_from_line (VT102 * v, CRT_Pos p)
714 int n = v->bottom_margin.x - p.x;
722 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
723 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
726 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
727 /*But not attr due to vt102 bug*/
731 vt102_insert_into_line (VT102 * v, CRT_Pos p)
733 int n = v->bottom_margin.x - p.x;
741 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
742 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
745 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
746 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
747 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
753 vt102_change_mode (Context *c, int private, char *ns, int set)
770 if (m >= VT102_NMODES)
775 v->private_modes[m] = set;
778 case VT102_PRIVATE_MODE_CURSOR_MODE:
779 if (v->application_keypad_mode)
780 v->private_modes[m] = 0;
782 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
783 v->private_modes[m], set);
786 case VT102_PRIVATE_MODE_ORIGIN_MODE:
787 vt102_cursor_home (v);
789 case VT102_PRIVATE_MODE_132COLS:
790 /* We don't implement 132 col mode - yet */
794 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
806 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
811 vt102_parse_mode_string (Context *c, char *buf, int len)
815 char last = buf[len - 1];
819 memset (num, 0, sizeof (num));
820 o = sizeof (num) - 1;
838 vt102_change_mode (c, private, &num[o], last == 'h');
839 memset (num, 0, sizeof (num));
840 o = sizeof (num) - 1;
855 vt102_change_mode (c, private, &num[o], last == 'h');
861 vt102_change_attr (VT102 * v, char *na)
878 v->attr = CRT_ATTR_NORMAL;
879 v->color = CRT_COLOR_NORMAL;
882 v->attr |= CRT_ATTR_BOLD;
886 v->attr &= ~CRT_ATTR_BOLD;
889 v->attr |= CRT_ATTR_UNDERLINE;
892 v->attr &= ~CRT_ATTR_UNDERLINE;
895 v->attr |= CRT_ATTR_BLINK;
898 v->attr &= ~CRT_ATTR_BLINK;
901 v->attr |= CRT_ATTR_REVERSE;
904 v->attr &= ~CRT_ATTR_REVERSE;
914 v->color &= ~CRT_COLOR_FG_MASK;
915 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
925 v->color &= ~CRT_COLOR_FG_MASK;
928 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
933 v->color &= ~CRT_COLOR_FG_MASK;
935 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
945 v->color &= ~CRT_COLOR_BG_MASK;
946 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
956 v->color &= ~CRT_COLOR_BG_MASK;
959 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
964 v->color &= ~CRT_COLOR_BG_MASK;
966 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
972 fprintf (stderr, "unhandled SGR %d\n", a);
980 vt102_parse_attr_string (VT102 * v, char *buf, int len)
983 char last = buf[len - 1];
987 memset (num, 0, sizeof (num));
988 o = sizeof (num) - 1;
999 vt102_change_attr (v, &num[o]);
1000 memset (num, 0, sizeof (num));
1001 o = sizeof (num) - 1;
1015 vt102_change_attr (v, &num[o]);
1019 vt102_save_state (VT102 * v)
1021 v->saved.pos = v->pos;
1022 v->saved.attr = v->attr;
1023 v->saved.color = v->color;
1024 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
1028 vt102_restore_state (VT102 * v)
1030 v->pos = v->saved.pos;
1031 v->attr = v->saved.attr;
1032 v->color = v->saved.color;
1033 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
1034 vt102_cursor_normalize (v);
1035 v->pending_wrap = 0;
1039 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
1043 vt102_do_pending_wrap (c);
1046 if (v->modes[VT102_MODE_INSERT])
1047 vt102_insert_into_line (v, v->pos);
1049 v->last_reg_char = ch;
1052 if (ch < VT102_CHARSET_SIZE)
1055 if ((cs = vt102_charset_c0[ch]))
1059 else if ((cs = charset_from_csid[v->g[v->cs]][ch]))
1064 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1065 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1066 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1067 vt102_cursor_advance (c);
1071 vt102_send_id (Context * c, char *buf)
1076 int l = strlen (buf);
1077 c->t->xmit (c->t, buf, l);
1082 vt102_scs (Context * c, int g, int s)
1085 int cs = VT102_CSID_US;
1116 vt102_parse_csi (Context * c, char *buf, int len)
1120 char *arg = buf + 1;
1127 last = buf[len - 1];
1148 vt102_insert_into_line (v, v->pos);
1151 vt102_cursor_relative (v, 0, -narg);
1155 vt102_cursor_relative (v, 0, narg);
1159 vt102_cursor_relative (v, narg, 0);
1162 vt102_cursor_relative (v, -narg, 0);
1165 vt102_cursor_relative (v, 0, narg);
1166 vt102_cursor_carriage_return (v);
1169 vt102_cursor_relative (v, 0, -narg);
1170 vt102_cursor_carriage_return (v);
1173 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1182 ptr = index (arg, ';');
1184 x = atoi (ptr + 1) - 1;
1188 vt102_cursor_absolute (v, x, y);
1193 vt102_cursor_advance_tab (c->v);
1197 narg = 0; /*Different default */
1201 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1204 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1207 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1214 CRT_Pos ls = { 0, v->pos.y };
1215 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1217 narg = 0; /*Different default */
1222 crt_erase (&v->crt, v->pos, le, 1, v->color);
1225 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1228 crt_erase (&v->crt, ls, le, 1, v->color);
1235 if ((v->pos.y >= v->top_margin.y)
1236 && (v->pos.y <= v->bottom_margin.y))
1239 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1245 if ((v->pos.y >= v->top_margin.y)
1246 && (v->pos.y <= v->bottom_margin.y))
1250 vt102_history (c, v->pos, v->bottom_margin);
1251 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1258 vt102_delete_from_line (v, v->pos);
1263 vt102_history (c, v->top_margin, v->bottom_margin);
1264 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1270 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1275 CRT_Pos end = v->pos;
1280 if (end.x > v->bottom_margin.x)
1281 end.x = v->bottom_margin.x;
1283 crt_erase (&v->crt, v->pos, end, 1, v->color);
1288 vt102_cursor_retreat_tab (c->v);
1291 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1295 vt102_regular_char (c, v, v->last_reg_char);
1298 vt102_send_id (c, "\033[?2c");
1301 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1305 narg = 0; /*Different default */
1310 v->tabs[v->pos.x] = 0;
1312 case 2: //FIXME: - LA120 says current line only WTF?
1314 memset (v->tabs, 0, sizeof (v->tabs));
1319 case 'i': //Printer commands
1324 vt102_parse_mode_string (c, &buf[1], len - 1);
1329 vt102_parse_attr_string (v, &buf[1], len - 1);
1331 case 'n': //Device status report
1335 vt102_send_id (c, "\033[0n");
1343 sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1344 c->t->xmit (c->t, buf, i);
1348 //?15n printer status
1349 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1353 case 'q': //Load LED on off
1356 v->top_margin = v->screen_start;
1357 v->bottom_margin = v->screen_end;
1359 if ((len > 2) && (ptr = index (arg, ';')))
1362 v->top_margin.y = narg - 1;
1363 v->bottom_margin.y = atoi (ptr) - 1;
1366 if (v->top_margin.y < v->screen_start.y)
1367 v->top_margin.y = v->screen_start.y;
1368 if (v->top_margin.y > v->screen_end.y)
1369 v->top_margin.y = v->screen_end.y;
1370 if (v->bottom_margin.y < v->screen_start.y)
1371 v->bottom_margin.y = v->screen_start.y;
1372 if (v->bottom_margin.y > v->screen_end.y)
1373 v->bottom_margin.y = v->screen_end.y;
1375 vt102_cursor_home (v);
1378 v->saved.pos = v->pos;
1381 v->pos = v->saved.pos;
1382 vt102_cursor_normalize (v);
1383 v->pending_wrap = 0;
1385 case 'y': //Invoke confidence test
1387 case 'z': //request terminal parameters
1391 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1398 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1408 vt102_parse_esc (Context * c)
1411 VT102_parser *p = &v->parser;
1414 p->cmd_buf[p->cmd_ptr] = 0;
1415 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1416 v->pos.y, v->pending_wrap,
1417 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1420 if (!v->private_modes[VT102_PRIVATE_MODE_VT52])
1424 switch (p->cmd_buf[0])
1427 vt102_cursor_relative (v, 0, -1);
1430 vt102_cursor_relative (v, 0, 1);
1433 vt102_cursor_relative (v, 1, 0);
1436 vt102_cursor_relative (v, -1, 0);
1440 v->g[1] = VT102_CSID_VT52;
1444 v->g[0] = VT102_CSID_US;
1447 vt102_cursor_absolute (v, 0, 0);
1450 vt102_cursor_retreat_line (c);
1453 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1457 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1458 crt_erase (&v->crt, v->pos, le, 1, v->color);
1462 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1464 case 'V': //Print current line
1466 case 'W': //Printer on
1468 case 'X': //printer off
1470 case ']': //print screen
1473 vt102_send_id (c, "\033/K");
1475 case '^': //Autoprint on
1477 case '_': //Autoprint off
1480 v->application_keypad_mode = 1;
1483 v->application_keypad_mode = 0;
1493 /*If you edit this switch don't forget to edit the length and termination tables*/
1494 switch (p->cmd_buf[0])
1497 vt102_cursor_advance_line (c);
1501 vt102_cursor_advance_line (c);
1502 v->pos.x = v->top_margin.x;
1503 vt102_cursor_normalize (v);
1504 v->pending_wrap = 0;
1507 v->tabs[v->pos.x]++;
1510 vt102_cursor_retreat_line (c);
1512 case 'N': //select G2 for one char
1514 case 'O': //select G3 for one char
1517 vt102_send_id (c, terminal_id);
1523 v->application_keypad_mode = 1;
1526 v->application_keypad_mode = 0;
1530 switch (p->cmd_buf[1])
1532 case '3': //top of double height line
1533 case '4': //bottom of double height line
1534 case '5': //single width line
1535 case '6': //double width line
1541 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1543 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1544 v->crt.screen[i].chr = 'E';
1550 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1551 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1556 /*Set ansi mode - ignored */
1560 vt102_save_state (v);
1563 vt102_restore_state (v);
1565 case ']': /*Set various titles */
1569 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1571 case '(': /*Charsets */
1576 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1579 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1580 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1585 p->cmd_buf[p->cmd_ptr] = 0;
1587 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1593 vt102_status_line (VT102 * v, char *str)
1596 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1598 for (i = 0; i < v->current_size.x; ++i)
1600 ca->attr = CRT_ATTR_REVERSE;
1601 ca->color = CRT_COLOR_NORMAL;
1602 ca->chr = *str ? *str : ' ';
1608 for (; i < VT102_MAX_COLS; ++i)
1610 ca->attr = CRT_ATTR_NORMAL;
1611 ca->color = CRT_COLOR_NORMAL;
1620 vt102_parser_reset (VT102_parser * p)
1623 p->cmd_more_bytes = 0;
1624 p->cmd_termination = 0;
1630 vt102_reset_state (Context *c)
1633 vt102_parser_reset (&v->parser);
1635 v->attr = CRT_ATTR_NORMAL;
1636 v->color = CRT_COLOR_NORMAL;
1638 v->application_keypad_mode = 0;
1640 v->current_size = v->original_size;
1644 memset (v->modes, 0, VT102_NMODES);
1645 memset (v->private_modes, 0, VT102_NMODES);
1647 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1648 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1649 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1650 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1651 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1653 vt102_reset_tabs (v);
1655 v->g[0] = v->g[1] = VT102_CSID_US;
1660 pre_parse_cmd (int ch, VT102_parser * p)
1662 if (ch > TABLE_LENGTH)
1667 p->cmd_more_bytes = 0;
1670 p->cmd_termination = vt102_cmd_termination[ch];
1671 if (p->cmd_termination)
1677 p->cmd_more_bytes = vt102_cmd_length[ch];
1679 if (p->cmd_more_bytes)
1687 vt102_parse_char (Context * c, int ch)
1690 VT102_parser *p = &v->parser;
1693 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1694 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1695 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1698 /* Turn anything non-ascii into '?' */
1700 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1708 if (p->dca_ptr == 2)
1713 if (ch == SYM_CHAR_RESET)
1715 vt102_reset_state (c);
1717 else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination))
1719 p->cmd_buf[p->cmd_ptr++] = ch;
1720 if (p->cmd_ptr == VT102_CMD_LEN)
1722 if (p->cmd_more_bytes)
1724 p->cmd_more_bytes--;
1727 if (!p->cmd_more_bytes == 1)
1731 switch (p->cmd_termination)
1736 if (p->cmd_termination == ch)
1747 vt102_parse_esc (c);
1748 p->cmd_more_bytes = 0;
1749 p->cmd_termination = 0;
1752 else if (p->in_escape && !ctrl_chr (ch, 0))
1755 p->cmd_buf[p->cmd_ptr++] = ch;
1758 pre_parse_cmd (ch, p);
1761 vt102_parse_esc (c);
1763 else if (ch == 0x9b) /*One byte CSI */
1766 p->cmd_buf[p->cmd_ptr++] = '[';
1768 pre_parse_cmd (ch, p);
1775 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1776 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1791 vt102_send_id (c, terminal_id);
1801 vt102_cursor_retreat (c->v);
1804 vt102_cursor_advance_tab (c->v);
1809 vt102_cursor_advance_line (c);
1810 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1813 vt102_cursor_carriage_return (v);
1848 /*regular character */ default:
1849 vt102_regular_char (c, v, ch);
1854 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1859 vt102_crt_update(c);
1865 vt102_send (Context * c, uint8_t key)
1873 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1875 if ((key > 31) && (key < 127))
1877 c->t->xmit (c->t, &key, 1);
1896 c->t->xmit (c->t, &key, 1);
1899 c->t->xmit (c->t, &key, 1);
1900 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1903 c->t->xmit (c->t, &ch, 1);
1919 c->t->xmit (c->t, &key, 1);
1927 c->t->xmit (c->t, &key, 1);
1938 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1940 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1941 c->t->xmit (c->t, &buf, sizeof (buf));
1945 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1946 c->t->xmit (c->t, &buf, sizeof (buf));
1964 if (c->v->application_keypad_mode)
1966 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1967 c->t->xmit (c->t, &buf, sizeof (buf));
1971 static char kpoff[KEY_NUM] = {
1990 c->t->xmit (c->t, &kpoff[key], 1);
1994 if (c->v->application_keypad_mode)
1996 uint8_t buf[] = { 033, 'O', 'M' };
1997 c->t->xmit (c->t, &buf, sizeof (buf));
2002 c->t->xmit (c->t, &ch, 1);
2003 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
2006 c->t->xmit (c->t, &ch, 1);
2015 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
2016 c->t->xmit (c->t, &buf, sizeof (buf));
2024 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
2025 c->t->xmit (c->t, &buf, sizeof (buf));
2033 vt102_reset (Context * c)
2036 VT102_parser *p = &v->parser;
2040 v->current_line = v->pos;
2041 v->pending_wrap = 0;
2043 v->screen_start.x = 0;
2044 v->screen_start.y = 0;
2045 v->current_size = v->original_size;
2046 v->crt.size = v->current_size;
2048 v->screen_end = v->current_size;
2052 vt102_cursor_home (v);
2053 vt102_status_line (v, "");
2055 vt102_reset_tabs (v);
2056 v->current_line = v->pos;
2058 vt102_parser_reset (p);
2059 vt102_reset_state (c);
2061 vt102_save_state (v);
2063 v->last_reg_char = ' ';
2067 vt102_new (CRT_Pos * size)
2071 v = (VT102 *) malloc (sizeof (VT102));
2078 v->original_size = *size;
2080 if (v->original_size.x < 1)
2081 v->original_size.x = 1;
2082 if (v->original_size.y < 1)
2083 v->original_size.y = 1;
2085 if (v->original_size.x > VT102_MAX_COLS)
2086 v->original_size.x = VT102_MAX_COLS;
2087 if (v->original_size.y > VT102_ROWS)
2088 v->original_size.y = VT102_ROWS;
2093 v->original_size.x = VT102_COLS_80;
2094 v->original_size.y = VT102_ROWS;
2101 vt102_set_ansi (VT102 * v, int ansi)
2103 v->xn_glitch = ansi ? 0 : 1;
2106 void vt102_resize(Context *c,CRT_Pos size)
2108 log_f(c->l,"<size change to %dx%d requested>", size.x,size.y);
2110 if (!size.x) size.x=c->v->current_size.x;
2111 if (!size.y) size.y=c->v->current_size.y;
2118 if (size.x > VT102_MAX_COLS)
2119 size.x = VT102_MAX_COLS;
2120 if (size.y > VT102_ROWS)
2121 size.y = VT102_ROWS;
2123 c->v->current_size=size;
2128 vt102_free (VT102 * v)