4 * Copyright (c) 2008 James McKenzie <sympathy@madingley.org>,
9 static char rcsid[] = "$Id: vt102.c,v 1.67 2010/07/27 14:49:35 james Exp $";
13 * Revision 1.67 2010/07/27 14:49:35 james
14 * add support for byte logging
16 * Revision 1.66 2008/03/07 13:16:02 james
17 * *** empty log message ***
19 * Revision 1.65 2008/03/07 12:42:08 james
20 * *** empty log message ***
22 * Revision 1.64 2008/03/07 12:37:04 james
23 * *** empty log message ***
25 * Revision 1.63 2008/03/07 12:00:58 james
26 * *** empty log message ***
28 * Revision 1.62 2008/03/06 21:34:09 james
29 * *** empty log message ***
31 * Revision 1.61 2008/03/06 17:21:41 james
32 * *** empty log message ***
34 * Revision 1.60 2008/03/06 16:49:39 james
35 * *** empty log message ***
37 * Revision 1.59 2008/03/06 16:49:05 james
38 * *** empty log message ***
40 * Revision 1.58 2008/03/06 01:41:48 james
41 * *** empty log message ***
43 * Revision 1.57 2008/03/03 06:20:14 james
44 * *** empty log message ***
46 * Revision 1.56 2008/03/03 06:04:42 james
47 * *** empty log message ***
49 * Revision 1.55 2008/03/02 12:32:57 staffcvs
50 * *** empty log message ***
52 * Revision 1.54 2008/03/02 10:37:56 james
53 * *** empty log message ***
55 * Revision 1.53 2008/02/29 18:33:39 james
56 * *** empty log message ***
58 * Revision 1.52 2008/02/28 16:57:52 james
59 * *** empty log message ***
61 * Revision 1.51 2008/02/28 16:37:16 james
62 * *** empty log message ***
64 * Revision 1.50 2008/02/28 12:12:25 james
65 * *** empty log message ***
67 * Revision 1.49 2008/02/28 11:27:48 james
68 * *** empty log message ***
70 * Revision 1.48 2008/02/27 09:42:53 james
71 * *** empty log message ***
73 * Revision 1.47 2008/02/27 09:42:22 james
74 * *** empty log message ***
76 * Revision 1.46 2008/02/27 01:31:38 james
77 * *** empty log message ***
79 * Revision 1.45 2008/02/27 01:31:14 james
80 * *** empty log message ***
82 * Revision 1.44 2008/02/27 00:54:16 james
83 * *** empty log message ***
85 * Revision 1.43 2008/02/27 00:27:22 james
86 * *** empty log message ***
88 * Revision 1.42 2008/02/26 23:56:12 james
89 * *** empty log message ***
91 * Revision 1.41 2008/02/26 23:23:17 james
92 * *** empty log message ***
94 * Revision 1.40 2008/02/26 19:00:59 james
95 * *** empty log message ***
97 * Revision 1.39 2008/02/26 16:54:06 james
98 * *** empty log message ***
100 * Revision 1.38 2008/02/26 16:53:24 james
101 * *** empty log message ***
103 * Revision 1.37 2008/02/24 12:22:53 james
104 * *** empty log message ***
106 * Revision 1.36 2008/02/24 12:22:42 james
107 * *** empty log message ***
109 * Revision 1.35 2008/02/24 00:42:53 james
110 * *** empty log message ***
112 * Revision 1.34 2008/02/23 11:48:37 james
113 * *** empty log message ***
115 * Revision 1.33 2008/02/22 23:39:27 james
116 * *** empty log message ***
118 * Revision 1.32 2008/02/22 19:12:05 james
119 * *** empty log message ***
121 * Revision 1.31 2008/02/22 17:07:00 james
122 * *** empty log message ***
124 * Revision 1.30 2008/02/22 14:51:54 james
125 * *** empty log message ***
127 * Revision 1.29 2008/02/15 03:32:07 james
128 * *** empty log message ***
130 * Revision 1.28 2008/02/14 10:34:30 james
131 * *** empty log message ***
133 * Revision 1.27 2008/02/14 02:46:45 james
134 * *** empty log message ***
136 * Revision 1.26 2008/02/14 01:55:57 james
137 * *** empty log message ***
139 * Revision 1.25 2008/02/13 16:57:29 james
140 * *** empty log message ***
142 * Revision 1.24 2008/02/13 09:12:21 james
143 * *** empty log message ***
145 * Revision 1.23 2008/02/07 13:26:35 james
146 * *** empty log message ***
148 * Revision 1.22 2008/02/07 13:22:51 james
149 * *** empty log message ***
151 * Revision 1.21 2008/02/07 12:21:16 james
152 * *** empty log message ***
154 * Revision 1.20 2008/02/07 12:16:04 james
155 * *** empty log message ***
157 * Revision 1.19 2008/02/07 11:27:02 james
158 * *** empty log message ***
160 * Revision 1.18 2008/02/07 01:59:25 james
161 * *** empty log message ***
163 * Revision 1.17 2008/02/07 01:58:28 james
164 * *** empty log message ***
166 * Revision 1.16 2008/02/07 01:57:46 james
167 * *** empty log message ***
169 * Revision 1.15 2008/02/07 00:43:27 james
170 * *** empty log message ***
172 * Revision 1.14 2008/02/07 00:40:23 james
173 * *** empty log message ***
175 * Revision 1.13 2008/02/07 00:39:59 james
176 * *** empty log message ***
178 * Revision 1.12 2008/02/07 00:39:13 james
179 * *** empty log message ***
181 * Revision 1.11 2008/02/06 20:26:58 james
182 * *** empty log message ***
184 * Revision 1.10 2008/02/06 17:53:28 james
185 * *** empty log message ***
187 * Revision 1.9 2008/02/06 15:53:22 james
188 * *** empty log message ***
190 * Revision 1.8 2008/02/06 11:49:47 james
191 * *** empty log message ***
193 * Revision 1.7 2008/02/06 11:30:37 james
194 * *** empty log message ***
196 * Revision 1.6 2008/02/05 01:11:46 james
197 * *** empty log message ***
199 * Revision 1.5 2008/02/04 20:23:55 james
200 * *** empty log message ***
202 * Revision 1.4 2008/02/04 05:45:55 james
205 * Revision 1.3 2008/02/04 02:05:06 james
206 * *** empty log message ***
208 * Revision 1.2 2008/02/04 01:32:39 james
209 * *** empty log message ***
211 * Revision 1.1 2008/02/03 23:36:41 james
212 * *** empty log message ***
222 * vt102|dec vt102:\ :mi:\
223 * :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
225 * vt100|vt100-am|dec vt100 (w/advanced video):\ :am:bs:ms:xn:xo:\
226 * :co#80:it#8:li#24:vt#3:\
227 * :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\ :UP=\E[%dA:\
228 * :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
229 * :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
230 * :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
231 * :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
232 * :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
233 * :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
234 * :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
235 * :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
236 * :us=\E[4m:tc=vt100+fnkeys:
238 * vt100+fnkeys|dec vt100 numeric keypad:\
239 * :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
242 * vt100+pfkeys|dec vt100 numeric keypad:\
243 * :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
245 * vt100+keypad|dec vt100 numeric keypad no fkeys:\
246 * :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
251 * so the parser needs to be able to at least do CTRL-G CTRL-H CTRL-I
252 * CTRL-J CTRL-M CTRL-N
254 * CTRL-O ESC7 ESC8 ESCH ESCM ESC>
256 * ESC[%dA ESC[%dB ESC[%dC ESC[%dD ESC[H ESC[%d;%dH ESC[J ESC[K ESC[1K
259 * ESC[3g ESC[4h ESC[4l ESC[m ESC[1m ESC[4m ESC[5m ESC[7m ESC[%d;%dr
262 * ESC[?3l ESC[?4l ESC[?5l ESC[?7h ESC[?7h ESC[?7l ESC[?8h
277 * ansi|ansi/pc-term compatible with color:\
278 * :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
279 * :tc=klone+sgr:tc=ansi-m: ansi-m|ansi-mono|ANSI X3.64-1979 terminal with
280 * ANSI.SYS compatible attributes:\ :5i:\
281 * :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
282 * :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
283 * :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
284 * :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
285 * :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
286 * :s3=\E+B:ta=\E[I:tc=pcansi-m: pcansi-m|pcansi-mono|ibm-pc terminal
287 * programs claiming to be ansi (mono mode):\ :am:bs:mi:ms:\
288 * :co#80:it#8:li#24:\
289 * :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
290 * :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
291 * :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
292 * :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\ :tc=klone+sgr-dumb:
293 * klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11
294 * m):\ :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
295 * :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
296 * klone+acs|alternate character set for ansi.sys displays:\
297 * :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:\
298 * :ae=\E[10m:as=\E[11m: ecma+color|color control for ECMA-48-compatible terminals:\ :Co#8:NC#3:pa#64:\ :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
301 * Ignoreing ones in the VT102 spec
303 * ESC[%d@ ESC[I ESC[....R ESC[%dS ESC[%dT ESC[X ESC[Z
305 * ESC[c *ESC[%db ESC[....d ESC[....f ESC[2g ESC[4i ESC[5i ESC[6n
307 * ESC(B ESC)B ESC*B ESC+B
315 #define TABLE_LENGTH 128
319 static char terminal_id[] = "vt102";
322 * number of aditional chars after \033 ... to complete the escape if it's
325 int vt102_cmd_length[TABLE_LENGTH] = {
336 * method for determining end if if's not fixed length, -ve numbers from
337 * #defines, +ve are literals
340 int vt102_cmd_termination[TABLE_LENGTH] = {
362 if ((c >= 'a') && (c <= 'z'))
364 if ((c >= 'A') && (c <= 'Z'))
372 ctrl_chr (int ch, int term)
374 if ((term > 0) && (ch == term))
378 if ((ch > 0) && (ch < 040))
385 in_margins (VT102 * v, CRT_Pos p)
387 if (v->pos.x < v->top_margin.x)
389 if (v->pos.y < v->top_margin.y)
392 if (v->pos.x > v->bottom_margin.x)
394 if (v->pos.y > v->bottom_margin.y)
401 vt102_crt_update (Context * c)
407 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
409 if (v->current_line.y != v->pos.y) {
410 vt102_log_line (c, v->current_line.y);
411 v->current_line = v->pos;
415 cmd_show_status (c->d, c);
419 vt102_do_resize (Context * c)
424 v->crt.size = v->current_size;
426 v->screen_end = v->current_size;
429 v->top_margin = v->screen_start;
430 v->bottom_margin = v->screen_end;
431 vt102_cursor_home (v);
435 tty_winch (c->t, v->current_size);
437 log_f (c->l, "<size now %dx%d>", v->current_size.x, v->current_size.y);
438 vt102_crt_update (c);
443 vt102_log_line (Context * c, int line)
445 CRT_Pos e = { c->v->current_size.x - 1, line };
446 CRT_Pos p = { 0, line };
447 char logbuf[4 * (VT102_MAX_COLS + 1)], *logptr = logbuf;
449 if (!c->l || c->byte_logging)
452 for (; e.x > 0; --e.x) {
453 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
457 for (; p.x <= e.x; ++p.x) {
458 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
461 logptr += utf8_encode (logptr, ch);
465 c->l->log (c->l, logbuf);
469 * Called for every upward scroll with same args
472 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
475 * Only log if it scrolls off the top
481 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
485 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
500 vt102_cursor_normalize (VT102 * v)
502 CRT_Pos *top, *bottom;
504 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE]) {
505 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
507 vt102_clip_cursor (v, v->screen_start, v->screen_end);
513 vt102_cursor_carriage_return (VT102 * v)
515 v->pos.x = v->top_margin.x;
520 vt102_cursor_advance_line (Context * c)
523 int couldscroll = in_margins (v, v->pos);
525 /* have wraped off end of last line in scrolling region */
526 /* not necessary, but shuts compiler up */
527 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y))
529 vt102_log_line (c, v->pos.y);
531 vt102_history (c, v->top_margin, v->bottom_margin);
533 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
537 if (v->pos.y != v->screen_end.y)
544 vt102_cursor_retreat_line (Context * c)
547 int couldscroll = in_margins (v, v->pos);
549 /* have wraped off end of first line in scrolling region */
550 /* (|| not necessary, but shuts compiler up */
551 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
553 vt102_log_line (c, v->pos.y);
555 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
559 if (v->pos.y != v->screen_start.y)
566 vt102_do_pending_wrap (Context * c)
569 int couldscroll = in_margins (v, v->pos);
570 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
572 if (!v->pending_wrap)
575 /* End of line but no autowrap, nothing to do */
579 /* End of screen and not allowed to scroll, nothing to do */
580 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
584 v->pos.x = v->top_margin.x;
589 vt102_cursor_advance_line (c);
593 vt102_cursor_advance (Context * c)
597 if (v->pos.x < v->bottom_margin.x) {
604 if (!c->v->xn_glitch)
605 vt102_do_pending_wrap (c);
611 vt102_cursor_retreat (VT102 * v)
613 if (v->pos.x != v->top_margin.x) {
621 vt102_reset_tabs (VT102 * v)
625 memset (v->tabs, 0, sizeof (v->tabs));
627 for (i = 0; i < VT102_MAX_COLS; i += 8) {
632 vt102_cursor_advance_tab (VT102 * v)
634 if (v->pos.x == v->bottom_margin.x)
636 while (v->pos.x < v->bottom_margin.x) {
638 if (v->tabs[v->pos.x])
645 vt102_cursor_retreat_tab (VT102 * v)
647 if (v->pos.x == v->top_margin.x)
649 while (v->pos.x > v->top_margin.x) {
651 if (v->tabs[v->pos.x])
657 vt102_cursor_home (VT102 * v)
659 v->pos = v->top_margin;
660 vt102_cursor_normalize (v);
665 vt102_cursor_absolute (VT102 * v, int x, int y)
667 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE]) {
668 v->pos.x = x + v->top_margin.x;
669 v->pos.y = y + v->top_margin.y;
674 vt102_cursor_normalize (v);
678 vt102_cursor_relative (VT102 * v, int x, int y)
682 vt102_cursor_normalize (v);
689 vt102_delete_from_line (VT102 * v, CRT_Pos p)
691 int n = v->bottom_margin.x - p.x;
698 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
699 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
702 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
703 /* But not attr due to vt102 bug */
707 vt102_insert_into_line (VT102 * v, CRT_Pos p)
709 int n = v->bottom_margin.x - p.x;
716 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
717 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
720 v->crt.screen[CRT_ADDR_POS (&p)].chr = ' ';
721 v->crt.screen[CRT_ADDR_POS (&p)].attr = CRT_ATTR_NORMAL;
722 v->crt.screen[CRT_ADDR_POS (&p)].color = CRT_COLOR_NORMAL;
728 vt102_change_mode (Context * c, int private, char *ns, int set)
742 if (m >= VT102_NMODES)
746 v->private_modes[m] = set;
748 case VT102_PRIVATE_MODE_CURSOR_MODE:
749 if (v->application_keypad_mode)
750 v->private_modes[m] = 0;
752 case VT102_PRIVATE_MODE_ORIGIN_MODE:
753 vt102_cursor_home (v);
755 case VT102_PRIVATE_MODE_132COLS:
758 private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132 :
770 vt102_parse_mode_string (Context * c, char *buf, int len)
774 char last = buf[len - 1];
778 memset (num, 0, sizeof (num));
779 o = sizeof (num) - 1;
794 vt102_change_mode (c, private, &num[o], last == 'h');
795 memset (num, 0, sizeof (num));
796 o = sizeof (num) - 1;
811 vt102_change_mode (c, private, &num[o], last == 'h');
817 vt102_change_attr (VT102 * v, char *na)
830 v->attr = CRT_ATTR_NORMAL;
831 v->color = CRT_COLOR_NORMAL;
834 v->attr |= CRT_ATTR_BOLD;
838 v->attr &= ~CRT_ATTR_BOLD;
841 v->attr |= CRT_ATTR_UNDERLINE;
844 v->attr &= ~CRT_ATTR_UNDERLINE;
847 v->attr |= CRT_ATTR_BLINK;
850 v->attr &= ~CRT_ATTR_BLINK;
853 v->attr |= CRT_ATTR_REVERSE;
856 v->attr &= ~CRT_ATTR_REVERSE;
866 v->color &= ~CRT_COLOR_FG_MASK;
867 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
877 v->color &= ~CRT_COLOR_FG_MASK;
880 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
885 v->color &= ~CRT_COLOR_FG_MASK;
887 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
897 v->color &= ~CRT_COLOR_BG_MASK;
898 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
908 v->color &= ~CRT_COLOR_BG_MASK;
911 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
916 v->color &= ~CRT_COLOR_BG_MASK;
918 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
926 vt102_parse_attr_string (VT102 * v, char *buf, int len)
929 char last = buf[len - 1];
933 memset (num, 0, sizeof (num));
934 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)
987 vt102_do_pending_wrap (c);
990 if (v->modes[VT102_MODE_INSERT])
991 vt102_insert_into_line (v, v->pos);
993 v->last_reg_char = ch;
996 if (ch < VT102_CHARSET_SIZE) {
998 if ((cs = vt102_charset_c0[ch])) {
1000 } else if ((cs = charset_from_csid[v->g[v->cs]][ch])) {
1004 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1005 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1006 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1007 vt102_cursor_advance (c);
1011 vt102_send_id (Context * c, char *buf)
1015 int l = strlen (buf);
1016 c->t->xmit (c->t, buf, l);
1021 vt102_scs (Context * c, int g, int s)
1024 int cs = VT102_CSID_US;
1053 vt102_parse_csi (Context * c, char *buf, int len)
1057 char *arg = buf + 1;
1065 last = buf[len - 1];
1078 vt102_insert_into_line (v, v->pos);
1081 vt102_cursor_relative (v, 0, -narg);
1085 vt102_cursor_relative (v, 0, narg);
1089 vt102_cursor_relative (v, narg, 0);
1092 vt102_cursor_relative (v, -narg, 0);
1095 vt102_cursor_relative (v, 0, narg);
1096 vt102_cursor_carriage_return (v);
1099 vt102_cursor_relative (v, 0, -narg);
1100 vt102_cursor_carriage_return (v);
1103 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1112 ptr = index (arg, ';');
1114 x = atoi (ptr + 1) - 1;
1118 vt102_cursor_absolute (v, x, y);
1123 vt102_cursor_advance_tab (c->v);
1126 /* Different default */
1131 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1134 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1137 crt_erase (&v->crt, v->screen_start, v->screen_end, 1, v->color);
1143 CRT_Pos ls = { 0, v->pos.y };
1144 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1145 /* Different default */
1151 crt_erase (&v->crt, v->pos, le, 1, v->color);
1154 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1157 crt_erase (&v->crt, ls, le, 1, v->color);
1164 if ((v->pos.y >= v->top_margin.y)
1165 && (v->pos.y <= v->bottom_margin.y)) {
1167 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1, v->color);
1172 if ((v->pos.y >= v->top_margin.y)
1173 && (v->pos.y <= v->bottom_margin.y)) {
1175 vt102_history (c, v->pos, v->bottom_margin);
1176 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1, v->color);
1182 vt102_delete_from_line (v, v->pos);
1186 vt102_history (c, v->top_margin, v->bottom_margin);
1187 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
1192 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1197 CRT_Pos end = v->pos;
1202 if (end.x > v->bottom_margin.x)
1203 end.x = v->bottom_margin.x;
1205 crt_erase (&v->crt, v->pos, end, 1, v->color);
1210 vt102_cursor_retreat_tab (c->v);
1213 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1217 vt102_regular_char (c, v, v->last_reg_char);
1218 err += vt102_rx_hook (c, v->last_reg_char);
1222 /* For some obscure reason some programs seem to send */
1223 /* CSI [ ? .. c, which we ignore */
1225 vt102_send_id (c, "\033[?2c");
1228 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1231 /* Different default */
1237 v->tabs[v->pos.x] = 0;
1240 // FIXME: - LA120 says current line only WTF?
1242 memset (v->tabs, 0, sizeof (v->tabs));
1253 vt102_parse_mode_string (c, &buf[1], len - 1);
1256 vt102_parse_attr_string (v, &buf[1], len - 1);
1259 // Device status report
1262 vt102_send_id (c, "\033[0n");
1268 i = sprintf (buf, "\033[%d;%dR", v->pos.y + 1, v->pos.x + 1);
1269 c->t->xmit (c->t, buf, i);
1273 // ?15n printer status
1274 log_f (c->l, "<%s:%d unhandled DSR: \\033%s>", __FILE__,
1278 case 'q': /* DECLL */
1281 case 'r': /* DECSTBM */
1282 v->top_margin = v->screen_start;
1283 v->bottom_margin = v->screen_end;
1285 if ((len > 2) && (ptr = index (arg, ';'))) {
1287 v->top_margin.y = narg - 1;
1288 v->bottom_margin.y = atoi (ptr) - 1;
1291 if (v->top_margin.y < v->screen_start.y)
1292 v->top_margin.y = v->screen_start.y;
1293 if (v->top_margin.y > v->screen_end.y)
1294 v->top_margin.y = v->screen_end.y;
1295 if (v->bottom_margin.y < v->screen_start.y)
1296 v->bottom_margin.y = v->screen_start.y;
1297 if (v->bottom_margin.y > v->screen_end.y)
1298 v->bottom_margin.y = v->screen_end.y;
1300 vt102_cursor_home (v);
1303 v->saved.pos = v->pos;
1306 v->pos = v->saved.pos;
1307 vt102_cursor_normalize (v);
1308 v->pending_wrap = 0;
1310 case 'y': /* DECTST */
1311 // Invoke confidence test
1313 case 'z': /* DECVERP */
1314 // request terminal parameters
1318 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__, buf);
1324 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1335 vt102_parse_esc (Context * c)
1339 VT102_parser *p = &v->parser;
1342 p->cmd_buf[p->cmd_ptr] = 0;
1343 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d vt=%d <ESC>%s >", v->pos.x,
1344 v->pos.y, v->pending_wrap,
1345 !v->private_modes[VT102_PRIVATE_MODE_VT52], p->cmd_buf);
1348 if (!v->private_modes[VT102_PRIVATE_MODE_VT52]) {
1351 switch (p->cmd_buf[0]) {
1352 case 'A': /* CURSOR UP */
1353 vt102_cursor_relative (v, 0, -1);
1355 case 'B': /* CURSOR DOWN */
1356 vt102_cursor_relative (v, 0, 1);
1358 case 'C': /* CURSOR RIGHT */
1359 vt102_cursor_relative (v, 1, 0);
1361 case 'D': /* CURSOR LEFT */
1362 vt102_cursor_relative (v, -1, 0);
1364 case 'F': /* ENTER GRAPHICS MODE */
1366 v->g[1] = VT102_CSID_VT52;
1368 case 'G': /* EXIT GRAPHICS MODE */
1370 v->g[0] = VT102_CSID_US;
1372 case 'H': /* CURSOR HOME */
1373 vt102_cursor_absolute (v, 0, 0);
1375 case 'I': /* REVERSE LINE FEED */
1376 vt102_cursor_retreat_line (c);
1378 case 'J': /* ERASE TO END OF SCREEN */
1379 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1381 case 'K': /* ERASE TO END OF LINE */
1383 CRT_Pos le = { v->current_size.x - 1, v->pos.y };
1384 crt_erase (&v->crt, v->pos, le, 1, v->color);
1387 case 'Y': /* DIRECT CURSOR ADDRESS */
1388 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1390 case 'V': /* PRINT CURSOR LINE */
1391 // Print current line
1399 case ']': /* PRINT SCREEN */
1403 vt102_send_id (c, "\033/K");
1411 case '=': /* ENTER ALTERNATE KEYPAD MODE */
1412 v->application_keypad_mode = 1;
1414 case '>': /* EXIT ALTERNATE KEYPAD MODE */
1415 v->application_keypad_mode = 0;
1426 * If you edit this switch don't forget to edit the length and
1427 * termination tables
1429 switch (p->cmd_buf[0]) {
1431 vt102_cursor_advance_line (c);
1435 vt102_cursor_advance_line (c);
1436 v->pos.x = v->top_margin.x;
1437 vt102_cursor_normalize (v);
1438 v->pending_wrap = 0;
1443 v->tabs[v->pos.x]++;
1450 vt102_cursor_retreat_line (c);
1453 // select G2 for one char
1456 // select G3 for one char
1468 case 'Z': /* DECID */
1469 vt102_send_id (c, terminal_id);
1476 v->application_keypad_mode = 1;
1480 v->application_keypad_mode = 0;
1484 switch (p->cmd_buf[1]) {
1485 case '3': /* DECDHL */
1486 // top of double height line
1487 case '4': /* DECDHL */
1488 // bottom of double height line
1489 case '5': /* DECSWL */
1490 // single width line
1491 case '6': /* DECDWL */
1492 // double width line
1494 case '8': /* DECALN */
1497 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1499 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1500 v->crt.screen[i].chr = 'E';
1506 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1507 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1511 case '<': /* DECANM */
1512 // Set ansi mode - ignored
1514 case '7': /* DECSC */
1515 vt102_save_state (v);
1517 case '8': /* DECRC */
1518 vt102_restore_state (v);
1521 /* Set various titles in xterm - ignored */
1524 err += vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1532 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1535 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1536 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1541 p->cmd_buf[p->cmd_ptr] = 0;
1543 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1550 vt102_status_line (VT102 * v, char *str)
1553 CRT_CA *ca = &v->crt.screen[CRT_ADDR (v->current_size.y, 0)];
1555 for (i = 0; i < v->current_size.x; ++i) {
1556 ca->attr = CRT_ATTR_REVERSE;
1557 ca->color = CRT_COLOR_NORMAL;
1558 ca->chr = *str ? *str : ' ';
1564 for (; i < VT102_MAX_COLS; ++i) {
1565 ca->attr = CRT_ATTR_NORMAL;
1566 ca->color = CRT_COLOR_NORMAL;
1575 vt102_parser_reset (VT102_parser * p)
1578 p->cmd_more_bytes = 0;
1579 p->cmd_termination = 0;
1585 vt102_reset_state (Context * c)
1588 vt102_parser_reset (&v->parser);
1590 v->attr = CRT_ATTR_NORMAL;
1591 v->color = CRT_COLOR_NORMAL;
1593 v->application_keypad_mode = 0;
1595 v->current_size = v->original_size;
1597 vt102_do_resize (c);
1599 memset (v->modes, 0, VT102_NMODES);
1600 memset (v->private_modes, 0, VT102_NMODES);
1602 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1603 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1604 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1605 v->private_modes[VT102_PRIVATE_MODE_VT52] = 1;
1606 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1608 vt102_reset_tabs (v);
1610 v->g[0] = v->g[1] = VT102_CSID_US;
1615 pre_parse_cmd (int ch, VT102_parser * p)
1617 if (ch > TABLE_LENGTH)
1622 p->cmd_more_bytes = 0;
1625 p->cmd_termination = vt102_cmd_termination[ch];
1626 if (p->cmd_termination) {
1631 p->cmd_more_bytes = vt102_cmd_length[ch];
1633 if (p->cmd_more_bytes) {
1640 vt102_rx_hook (Context * c, int ch)
1646 return c->r->rx (c->r, ch);
1650 vt102_parse_char (Context * c, int ch)
1654 VT102_parser *p = &v->parser;
1657 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1658 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1659 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1663 if (ch == SYM_CHAR_RESET) {
1664 vt102_reset_state (c);
1665 } else if (p->in_cmd && !ctrl_chr (ch, p->cmd_termination)) {
1666 p->cmd_buf[p->cmd_ptr++] = ch;
1667 if (p->cmd_ptr == VT102_CMD_LEN)
1669 if (p->cmd_more_bytes) {
1670 p->cmd_more_bytes--;
1673 if (!p->cmd_more_bytes == 1)
1677 switch (p->cmd_termination) {
1681 if (p->cmd_termination == ch)
1691 err += vt102_parse_esc (c);
1692 p->cmd_more_bytes = 0;
1693 p->cmd_termination = 0;
1695 } else if (p->in_escape && !ctrl_chr (ch, 0)) {
1697 p->cmd_buf[p->cmd_ptr++] = ch;
1700 pre_parse_cmd (ch, p);
1703 err += vt102_parse_esc (c);
1704 } else if ((ch >= 0x80) && (ch < 0xa0)) {
1707 case 0x80: /* @ PAD */
1708 case 0x81: /* A HOP */
1709 case 0x82: /* B BPH */
1710 case 0x83: /* C NBH */
1711 case 0x84: /* D IND */
1712 case 0x85: /* E NEL */
1713 case 0x86: /* F SSA */
1714 case 0x87: /* G ESA */
1715 case 0x88: /* H HTS */
1716 case 0x89: /* I HTJ */
1717 case 0x8a: /* J VTS */
1718 case 0x8b: /* K PLD */
1719 case 0x8c: /* L PLU */
1720 case 0x8d: /* M RI */
1721 case 0x8e: /* N SS2 */
1722 case 0x8f: /* O SS3 */
1723 case 0x90: /* P DCS */
1724 case 0x91: /* Q PU1 */
1725 case 0x92: /* R PU2 */
1726 case 0x93: /* S STS */
1727 case 0x94: /* T CCH */
1728 case 0x95: /* U MW */
1729 case 0x96: /* V SPA */
1730 case 0x97: /* W EPA */
1731 case 0x98: /* X SOS */
1732 case 0x99: /* Y SGCI */
1733 case 0x9a: /* Z DECID */
1734 case 0x9b: /* [ CSI */
1735 case 0x9c: /* \ ST */
1736 case 0x9d: /* ] OSC */
1737 case 0x9e: /* ^ PM */
1738 case 0x9f: /* _ APC */
1739 /* C1 chars are equavlent to ESC ch-0x40 */
1741 p->cmd_buf[p->cmd_ptr++] = ch;
1744 pre_parse_cmd (ch, p);
1747 err += vt102_parse_esc (c);
1754 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1755 v->pos.x, v->pos.y, v->pending_wrap, ch, ch, safe_ch (ch));
1762 #ifdef HIDE_NON_PRINTABLES
1770 vt102_send_id (c, terminal_id);
1772 #ifdef HIDE_NON_PRINTABLES
1780 vt102_cursor_retreat (c->v);
1781 err += vt102_rx_hook (c, ch);
1784 vt102_cursor_advance_tab (c->v);
1785 err += vt102_rx_hook (c, ch);
1790 vt102_cursor_advance_line (c);
1791 err += vt102_rx_hook (c, 10);
1792 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1795 vt102_cursor_carriage_return (v);
1796 err += vt102_rx_hook (c, 13);
1806 #ifdef HIDE_NON_PRINTABLES
1823 #ifdef HIDE_NON_PRINTABLES
1831 default: /* regular character */
1832 vt102_regular_char (c, v, ch);
1833 err += vt102_rx_hook (c, ch);
1838 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,
1843 vt102_crt_update (c);
1850 vt102_send (Context * c, uint8_t key)
1857 if ((key > 31) && (key < 127)) {
1858 c->t->xmit (c->t, &key, 1);
1876 c->t->xmit (c->t, &key, 1);
1879 c->t->xmit (c->t, &key, 1);
1880 if (c->v->modes[VT102_MODE_NEWLINE_MODE]) {
1882 c->t->xmit (c->t, &ch, 1);
1898 c->t->xmit (c->t, &key, 1);
1906 c->t->xmit (c->t, &key, 1);
1917 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE]) {
1918 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1919 c->t->xmit (c->t, &buf, sizeof (buf));
1921 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1922 c->t->xmit (c->t, &buf, sizeof (buf));
1940 if (c->v->application_keypad_mode) {
1941 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1942 c->t->xmit (c->t, &buf, sizeof (buf));
1944 static char kpoff[KEY_NUM] = {
1963 c->t->xmit (c->t, &kpoff[key], 1);
1967 if (c->v->application_keypad_mode) {
1968 uint8_t buf[] = { 033, 'O', 'M' };
1969 c->t->xmit (c->t, &buf, sizeof (buf));
1972 c->t->xmit (c->t, &ch, 1);
1973 if (c->v->modes[VT102_MODE_NEWLINE_MODE]) {
1975 c->t->xmit (c->t, &ch, 1);
1984 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1985 c->t->xmit (c->t, &buf, sizeof (buf));
1988 case KEY_VT220_HOME:
2017 i = sprintf (buf, "\033[%d~", (key - KEY_180));
2018 c->t->xmit (c->t, &buf, i);
2026 vt102_reset (Context * c)
2029 VT102_parser *p = &v->parser;
2033 v->current_line = v->pos;
2034 v->pending_wrap = 0;
2036 v->screen_start.x = 0;
2037 v->screen_start.y = 0;
2038 v->current_size = v->original_size;
2039 v->crt.size = v->current_size;
2041 v->screen_end = v->current_size;
2045 vt102_cursor_home (v);
2046 vt102_status_line (v, "");
2048 vt102_reset_tabs (v);
2049 v->current_line = v->pos;
2051 vt102_parser_reset (p);
2052 vt102_reset_state (c);
2054 vt102_save_state (v);
2056 v->last_reg_char = ' ';
2060 vt102_new (CRT_Pos * size)
2064 v = (VT102 *) xmalloc (sizeof (VT102));
2070 v->original_size = *size;
2072 if (v->original_size.x < 1)
2073 v->original_size.x = 1;
2074 if (v->original_size.y < 1)
2075 v->original_size.y = 1;
2077 if (v->original_size.x > VT102_MAX_COLS)
2078 v->original_size.x = VT102_MAX_COLS;
2079 if (v->original_size.y > VT102_MAX_ROWS)
2080 v->original_size.y = VT102_MAX_ROWS;
2083 v->original_size.x = VT102_COLS_80;
2084 v->original_size.y = VT102_ROWS_24;
2091 vt102_set_ansi (VT102 * v, int ansi)
2093 v->xn_glitch = ansi ? 0 : 1;
2097 vt102_resize (Context * c, CRT_Pos size)
2099 log_f (c->l, "<size change to %dx%d requested>", size.x, size.y);
2102 size.x = c->v->current_size.x;
2104 size.y = c->v->current_size.y;
2111 if (size.x > VT102_MAX_COLS)
2112 size.x = VT102_MAX_COLS;
2113 if (size.y > VT102_MAX_ROWS)
2114 size.y = VT102_MAX_ROWS;
2116 c->v->current_size = size;
2117 vt102_do_resize (c);
2121 vt102_free (VT102 * v)