4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.41 2008/02/26 23:23:17 james
14 * *** empty log message ***
16 * Revision 1.40 2008/02/26 19:00:59 james
17 * *** empty log message ***
19 * Revision 1.39 2008/02/26 16:54:06 james
20 * *** empty log message ***
22 * Revision 1.38 2008/02/26 16:53:24 james
23 * *** empty log message ***
25 * Revision 1.37 2008/02/24 12:22:53 james
26 * *** empty log message ***
28 * Revision 1.36 2008/02/24 12:22:42 james
29 * *** empty log message ***
31 * Revision 1.35 2008/02/24 00:42:53 james
32 * *** empty log message ***
34 * Revision 1.34 2008/02/23 11:48:37 james
35 * *** empty log message ***
37 * Revision 1.33 2008/02/22 23:39:27 james
38 * *** empty log message ***
40 * Revision 1.32 2008/02/22 19:12:05 james
41 * *** empty log message ***
43 * Revision 1.31 2008/02/22 17:07:00 james
44 * *** empty log message ***
46 * Revision 1.30 2008/02/22 14:51:54 james
47 * *** empty log message ***
49 * Revision 1.29 2008/02/15 03:32:07 james
50 * *** empty log message ***
52 * Revision 1.28 2008/02/14 10:34:30 james
53 * *** empty log message ***
55 * Revision 1.27 2008/02/14 02:46:45 james
56 * *** empty log message ***
58 * Revision 1.26 2008/02/14 01:55:57 james
59 * *** empty log message ***
61 * Revision 1.25 2008/02/13 16:57:29 james
62 * *** empty log message ***
64 * Revision 1.24 2008/02/13 09:12:21 james
65 * *** empty log message ***
67 * Revision 1.23 2008/02/07 13:26:35 james
68 * *** empty log message ***
70 * Revision 1.22 2008/02/07 13:22:51 james
71 * *** empty log message ***
73 * Revision 1.21 2008/02/07 12:21:16 james
74 * *** empty log message ***
76 * Revision 1.20 2008/02/07 12:16:04 james
77 * *** empty log message ***
79 * Revision 1.19 2008/02/07 11:27:02 james
80 * *** empty log message ***
82 * Revision 1.18 2008/02/07 01:59:25 james
83 * *** empty log message ***
85 * Revision 1.17 2008/02/07 01:58:28 james
86 * *** empty log message ***
88 * Revision 1.16 2008/02/07 01:57:46 james
89 * *** empty log message ***
91 * Revision 1.15 2008/02/07 00:43:27 james
92 * *** empty log message ***
94 * Revision 1.14 2008/02/07 00:40:23 james
95 * *** empty log message ***
97 * Revision 1.13 2008/02/07 00:39:59 james
98 * *** empty log message ***
100 * Revision 1.12 2008/02/07 00:39:13 james
101 * *** empty log message ***
103 * Revision 1.11 2008/02/06 20:26:58 james
104 * *** empty log message ***
106 * Revision 1.10 2008/02/06 17:53:28 james
107 * *** empty log message ***
109 * Revision 1.9 2008/02/06 15:53:22 james
110 * *** empty log message ***
112 * Revision 1.8 2008/02/06 11:49:47 james
113 * *** empty log message ***
115 * Revision 1.7 2008/02/06 11:30:37 james
116 * *** empty log message ***
118 * Revision 1.6 2008/02/05 01:11:46 james
119 * *** empty log message ***
121 * Revision 1.5 2008/02/04 20:23:55 james
122 * *** empty log message ***
124 * Revision 1.4 2008/02/04 05:45:55 james
127 * Revision 1.3 2008/02/04 02:05:06 james
128 * *** empty log message ***
130 * Revision 1.2 2008/02/04 01:32:39 james
131 * *** empty log message ***
133 * Revision 1.1 2008/02/03 23:36:41 james
134 * *** empty log message ***
145 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
147 vt100|vt100-am|dec vt100 (w/advanced video):\
149 :co#80:it#8:li#24:vt#3:\
150 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
152 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
153 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
154 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
155 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
156 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
157 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
158 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
159 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
160 :us=\E[4m:tc=vt100+fnkeys:
162 vt100+fnkeys|dec vt100 numeric keypad:\
163 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
166 vt100+pfkeys|dec vt100 numeric keypad:\
167 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
169 vt100+keypad|dec vt100 numeric keypad no fkeys:\
170 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
175 so the parser needs to be able to at least do
237 ansi|ansi/pc-term compatible with color:\
238 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
239 :tc=klone+sgr:tc=ansi-m:
240 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
242 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
243 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
244 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
245 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
246 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
247 :s3=\E+B:ta=\E[I:tc=pcansi-m:
248 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
251 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
252 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
253 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
254 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
256 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
257 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
258 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
259 klone+acs|alternate character set for ansi.sys displays:\
260 :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:\
261 :ae=\E[10m:as=\E[11m:
262 ecma+color|color control for ECMA-48-compatible terminals:\
264 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
267 Ignoreing ones in the VT102 spec
297 //static char terminal_reply[]={033,0133,077,066,0143};
299 #define TABLE_LENGTH 128
301 static char terminal_id[] = "vt102";
303 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
304 int vt102_cmd_length[TABLE_LENGTH] = {
314 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
316 int vt102_cmd_termination[TABLE_LENGTH] = {
338 if ((c >= 'a') && (c <= 'z'))
340 if ((c >= 'A') && (c <= 'Z'))
347 static inline int ctrl_chr(int ch,int term)
349 if ((term>0) && (ch==term)) return 0;
350 if (ch==033) return 0;
351 if ((ch>0) && (ch<040)) return 1;
357 in_margins (VT102 * v, CRT_Pos p)
359 if (v->pos.x < v->top_margin.x)
361 if (v->pos.y < v->top_margin.y)
364 if (v->pos.x > v->bottom_margin.x)
366 if (v->pos.y > v->bottom_margin.y)
373 vt102_log_line (Context * c, int line)
375 CRT_Pos e = { c->v->current_width - 1, line };
376 CRT_Pos p = { 0, line };
377 char logbuf[VT102_MAX_COLS + 1];
383 for (; e.x > 0; --e.x)
385 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
389 for (; p.x <= e.x; ++p.x)
391 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
400 c->l->log (c->l, logbuf);
403 /*Called for every upward scroll with same args*/
405 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
407 /*Only log if it scrolls off the top*/
412 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
416 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
431 vt102_cursor_normalize (VT102 * v)
433 CRT_Pos *top, *bottom;
435 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
437 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
441 vt102_clip_cursor (v, v->screen_start, v->screen_end);
447 vt102_cursor_carriage_return (VT102 * v)
449 /*FISH*/ v->pos.x = v->top_margin.x;
454 vt102_cursor_advance_line (Context * c)
457 int couldscroll = in_margins (v, v->pos);
459 /*have wraped off end of last line in scrolling region */
460 /* (|| not necessary, but shuts compiler up */
461 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
464 vt102_log_line (c, v->pos.y);
466 vt102_history (c, v->top_margin, v->bottom_margin);
468 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
472 if (v->pos.y != v->screen_end.y)
479 vt102_cursor_retreat_line (Context * c)
482 int couldscroll = in_margins (v, v->pos);
484 /*have wraped off end of last line in scrolling region */
485 /* (|| not necessary, but shuts compiler up */
486 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
489 vt102_log_line (c, v->pos.y);
491 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
495 if (v->pos.y != v->screen_start.y)
502 vt102_do_pending_wrap (Context * c)
505 int couldscroll = in_margins (v, v->pos);
506 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
508 if (!v->pending_wrap)
512 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
513 v->pos.y, autowrap, couldscroll);
516 /*End of line but no autowrap, nothing to do*/
520 /*End of screen and not allowed to scroll, nothing to do*/
521 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
526 v->pos.x = v->top_margin.x;
533 vt102_cursor_advance_line (c);
537 vt102_cursor_advance (Context * c)
541 if (v->pos.x < v->bottom_margin.x)
549 if (!c->v->xn_glitch)
550 vt102_do_pending_wrap (c);
556 vt102_cursor_retreat (VT102 * v)
558 if (v->pos.x != v->top_margin.x) {
566 vt102_reset_tabs (VT102 * v)
570 memset (v->tabs, 0, sizeof (v->tabs));
572 for (i = 0; i < VT102_MAX_COLS; i += 8)
578 vt102_cursor_advance_tab (VT102 * v)
580 if (v->pos.x == v->bottom_margin.x)
582 while (v->pos.x < v->bottom_margin.x)
585 if (v->tabs[v->pos.x])
592 vt102_cursor_retreat_tab (VT102 * v)
594 if (v->pos.x == v->top_margin.x)
596 while (v->pos.x > v->top_margin.x)
599 if (v->tabs[v->pos.x])
605 vt102_cursor_home (VT102 * v)
607 v->pos = v->top_margin;
608 vt102_cursor_normalize (v);
613 vt102_cursor_absolute (VT102 * v, int x, int y)
615 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
617 v->pos.x = x + v->top_margin.x;
618 v->pos.y = y + v->top_margin.y;
625 vt102_cursor_normalize (v);
629 vt102_cursor_relative (VT102 * v, int x, int y)
633 vt102_cursor_normalize (v);
640 vt102_delete_from_line (VT102 * v, CRT_Pos p)
642 int n = v->bottom_margin.x - p.x;
650 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
651 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
654 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
655 /*But not attr due to vt102 bug*/
659 vt102_insert_into_line (VT102 * v, CRT_Pos p)
661 int n = v->bottom_margin.x - p.x;
669 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
670 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
673 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
674 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
675 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
681 vt102_change_mode (VT102 * v, int private, char *ns, int set)
697 if (m >= VT102_NMODES)
702 v->private_modes[m] = set;
705 case VT102_PRIVATE_MODE_CURSOR_MODE:
706 if (v->application_keypad_mode)
707 v->private_modes[m] = 0;
709 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
710 v->private_modes[m], set);
713 case VT102_PRIVATE_MODE_ORIGIN_MODE:
714 vt102_cursor_home (v);
716 case VT102_PRIVATE_MODE_132COLS:
717 /* We don't implement 132 col mode - yet */
719 v->current_width=v->private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132:VT102_COLS_80;
721 v->crt.width=v->current_width;
722 v->screen_end.x=v->current_width-1;
723 v->top_margin = v->screen_start;
724 v->bottom_margin = v->screen_end;
725 vt102_cursor_home (v);
737 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
742 vt102_parse_mode_string (VT102 * v, char *buf, int len)
745 char last = buf[len - 1];
749 memset (num, 0, sizeof (num));
750 o = sizeof (num) - 1;
768 vt102_change_mode (v, private, &num[o], last == 'h');
769 memset (num, 0, sizeof (num));
770 o = sizeof (num) - 1;
785 vt102_change_mode (v, private, &num[o], last == 'h');
791 vt102_change_attr (VT102 * v, char *na)
808 v->attr = CRT_ATTR_NORMAL;
809 v->color = CRT_COLOR_NORMAL;
812 v->attr |= CRT_ATTR_BOLD;
816 v->attr &= ~CRT_ATTR_BOLD;
819 v->attr |= CRT_ATTR_UNDERLINE;
822 v->attr &= ~CRT_ATTR_UNDERLINE;
825 v->attr |= CRT_ATTR_BLINK;
828 v->attr &= ~CRT_ATTR_BLINK;
831 v->attr |= CRT_ATTR_REVERSE;
834 v->attr &= ~CRT_ATTR_REVERSE;
844 v->color &= ~CRT_COLOR_FG_MASK;
845 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
855 v->color &= ~CRT_COLOR_FG_MASK;
858 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
863 v->color &= ~CRT_COLOR_FG_MASK;
865 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
875 v->color &= ~CRT_COLOR_BG_MASK;
876 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
886 v->color &= ~CRT_COLOR_BG_MASK;
889 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
894 v->color &= ~CRT_COLOR_BG_MASK;
896 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
902 fprintf (stderr, "unhandled SGR %d\n", a);
910 vt102_parse_attr_string (VT102 * v, char *buf, int len)
913 char last = buf[len - 1];
917 memset (num, 0, sizeof (num));
918 o = sizeof (num) - 1;
929 vt102_change_attr (v, &num[o]);
930 memset (num, 0, sizeof (num));
931 o = sizeof (num) - 1;
945 vt102_change_attr (v, &num[o]);
949 vt102_save_state (VT102 * v)
951 v->saved.pos = v->pos;
952 v->saved.attr = v->attr;
953 v->saved.color = v->color;
954 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
958 vt102_restore_state (VT102 * v)
960 v->pos = v->saved.pos;
961 v->attr = v->saved.attr;
962 v->color = v->saved.color;
963 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
964 vt102_cursor_normalize (v);
969 vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
972 vt102_do_pending_wrap (c);
974 if (v->modes[VT102_MODE_INSERT])
975 vt102_insert_into_line (v, v->pos);
977 if (ch < VT102_CHARSET_SIZE ){
978 int cs=charset_from_csid[v->g[v->cs]][ch];
979 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = cs ?cs:ch;
981 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr=ch;
983 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
984 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
985 vt102_cursor_advance (c);
987 v->last_reg_char = ch;
990 vt102_send_id (Context * c, char *buf)
995 int l = strlen (buf);
996 c->t->xmit (c->t, buf, l);
1001 vt102_scs (Context * c, int g, int s)
1004 int cs=VT102_CSID_US;
1033 vt102_parse_csi (Context * c, char *buf, int len)
1037 char *arg = buf + 1;
1044 last = buf[len - 1];
1065 vt102_insert_into_line (v, v->pos);
1068 vt102_cursor_relative (v, 0, -narg);
1072 vt102_cursor_relative (v, 0, narg);
1076 vt102_cursor_relative (v, narg, 0);
1079 vt102_cursor_relative (v, -narg, 0);
1082 vt102_cursor_relative (v, 0, narg);
1083 vt102_cursor_carriage_return (v);
1086 vt102_cursor_relative (v, 0, -narg);
1087 vt102_cursor_carriage_return (v);
1090 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1099 ptr = index (arg, ';');
1101 x = atoi (ptr + 1) - 1;
1105 vt102_cursor_absolute (v, x, y);
1110 vt102_cursor_advance_tab (c->v);
1114 narg = 0; /*Different default */
1118 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1121 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1124 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1131 CRT_Pos ls = { 0, v->pos.y };
1132 CRT_Pos le = { v->current_width -1, v->pos.y };
1134 narg = 0; /*Different default */
1139 crt_erase (&v->crt, v->pos, le, 1, v->color);
1142 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1145 crt_erase (&v->crt, ls, le, 1, v->color);
1152 if ((v->pos.y >= v->top_margin.y)
1153 && (v->pos.y <= v->bottom_margin.y))
1156 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1162 if ((v->pos.y >= v->top_margin.y)
1163 && (v->pos.y <= v->bottom_margin.y))
1167 vt102_history (c, v->pos, v->bottom_margin);
1168 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1175 vt102_delete_from_line (v, v->pos);
1178 //FIXME: cursor position report - does anything use that?
1183 vt102_history (c, v->top_margin, v->bottom_margin);
1184 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1190 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1195 CRT_Pos end = v->pos;
1200 if (end.x > v->bottom_margin.x)
1201 end.x = v->bottom_margin.x;
1203 crt_erase (&v->crt, v->pos, end, 1, v->color);
1208 vt102_cursor_retreat_tab (c->v);
1211 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1215 vt102_regular_char (c, v, v->last_reg_char);
1218 vt102_send_id (c, terminal_id);
1221 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1225 narg = 0; /*Different default */
1230 v->tabs[v->pos.x] = 0;
1232 case 2: //FIXME: - LA120 says current line only WTF?
1234 memset (v->tabs, 0, sizeof (v->tabs));
1239 case 'i': //Printer commands
1244 vt102_parse_mode_string (v, &buf[1], len - 1);
1249 vt102_parse_attr_string (v, &buf[1], len - 1);
1251 case 'n': //Device status report
1252 //5 requestion status
1253 // 6 request cursor position FIXME
1254 //?15n printer status
1256 case 'q': //Load LED on off
1259 v->top_margin = v->screen_start;
1260 v->bottom_margin = v->screen_end;
1262 if ((len > 2) && (ptr = index (arg, ';')))
1265 v->top_margin.y = narg - 1;
1266 v->bottom_margin.y = atoi (ptr) - 1;
1269 if (v->top_margin.y < v->screen_start.y)
1270 v->top_margin.y = v->screen_start.y;
1271 if (v->top_margin.y > v->screen_end.y)
1272 v->top_margin.y = v->screen_end.y;
1273 if (v->bottom_margin.y < v->screen_start.y)
1274 v->bottom_margin.y = v->screen_start.y;
1275 if (v->bottom_margin.y > v->screen_end.y)
1276 v->bottom_margin.y = v->screen_end.y;
1278 vt102_cursor_home (v);
1281 v->saved.pos = v->pos;
1284 v->pos = v->saved.pos;
1285 vt102_cursor_normalize (v);
1286 v->pending_wrap = 0;
1288 case 'y': //Invoke confidence test
1292 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1299 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1309 vt102_parse_esc (Context * c)
1312 VT102_parser *p = &v->parser;
1315 p->cmd_buf[p->cmd_ptr] = 0;
1316 log_f (c->l, "<cmd in x=%3d y=%2d aw=%d <ESC>%s >", v->pos.x, v->pos.y,v->pending_wrap,
1320 if (v->private_modes[VT102_PRIVATE_MODE_VT52]) {
1323 switch (p->cmd_buf[0])
1326 vt102_cursor_relative (v, 0, -1);
1329 vt102_cursor_relative (v, 0, 1);
1332 vt102_cursor_relative (v, 1, 0);
1335 vt102_cursor_relative (v, -1, 0);
1339 v->g[1]=VT102_CSID_VT52;
1343 v->g[0]=VT102_CSID_US;
1346 vt102_cursor_absolute (v, 0, 0);
1349 vt102_cursor_retreat_line (c);
1352 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1356 CRT_Pos le = { v->current_width - 1, v->pos.y };
1357 crt_erase (&v->crt, v->pos, le, 1, v->color);
1361 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1363 case 'V': //Print current line
1365 case 'W': //Printer on
1367 case 'X': //printer off
1369 case ']': //print screen
1372 vt102_send_id (c, "\033/Z");
1374 case '^': //Autoprint on
1376 case '_': //Autoprint off
1379 v->application_keypad_mode = 1;
1382 v->application_keypad_mode = 0;
1391 /*If you edit this switch don't forget to edit the length and termination tables*/
1392 switch (p->cmd_buf[0])
1395 vt102_cursor_advance_line (c);
1399 vt102_cursor_advance_line (c);
1400 v->pos.x = v->top_margin.x;
1401 vt102_cursor_normalize (v);
1402 v->pending_wrap = 0;
1405 v->tabs[v->pos.x]++;
1408 vt102_cursor_retreat_line (c);
1410 case 'N': //select G2 for one char
1412 case 'O': //select G3 for one char
1415 vt102_send_id (c, terminal_id);
1423 v->application_keypad_mode = 1;
1426 v->application_keypad_mode = 0;
1430 switch (p->cmd_buf[1])
1432 case '3': //top of double height line
1433 case '4': //bottom of double height line
1434 case '5': //single width line
1435 case '6': //double width line
1441 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1443 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1444 v->crt.screen[i].chr = 'E';
1450 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1451 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1456 /*Set ansi mode - ignored */
1460 vt102_save_state (v);
1463 vt102_restore_state (v);
1465 case ']': /*Set various titles */
1469 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1471 case '(': /*Charsets */
1476 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1479 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1480 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1485 p->cmd_buf[p->cmd_ptr] = 0;
1486 log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,v->pending_wrap);
1490 vt102_status_line (VT102 * v, char *str)
1492 int i = v->current_width -1;
1493 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1495 for (i=0;i<v->current_width;++i)
1497 ca->attr = CRT_ATTR_REVERSE;
1498 ca->color = CRT_COLOR_NORMAL;
1499 ca->chr = *str ? *str:' ';
1505 for (;i<VT102_MAX_COLS;++i) {
1506 ca->attr = CRT_ATTR_NORMAL;
1507 ca->color = CRT_COLOR_NORMAL;
1516 vt102_parser_reset (VT102_parser * p)
1519 p->cmd_more_bytes = 0;
1520 p->cmd_termination = 0;
1526 vt102_reset_state (VT102 * v)
1528 vt102_parser_reset (&v->parser);
1530 v->attr = CRT_ATTR_NORMAL;
1531 v->color = CRT_COLOR_NORMAL;
1533 v->application_keypad_mode = 0;
1535 v->current_width=VT102_COLS_80;
1536 v->crt.width=v->current_width;
1537 v->screen_end.x = v->current_width -1;
1539 v->top_margin = v->screen_start;
1540 v->bottom_margin = v->screen_end;
1542 memset (v->modes, 0, VT102_NMODES);
1543 memset (v->private_modes, 0, VT102_NMODES);
1545 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1546 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1547 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1548 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1550 vt102_reset_tabs (v);
1552 v->g[0]=v->g[1]=VT102_CSID_US;
1557 pre_parse_cmd (int ch, VT102_parser * p)
1559 if (ch > TABLE_LENGTH)
1564 p->cmd_more_bytes = 0;
1567 p->cmd_termination = vt102_cmd_termination[ch];
1568 if (p->cmd_termination)
1574 p->cmd_more_bytes = vt102_cmd_length[ch];
1576 if (p->cmd_more_bytes)
1584 vt102_parse_char (Context * c, int ch)
1587 VT102_parser *p = &v->parser;
1590 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1591 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1592 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1595 /* Turn anything non-ascii into '?' */
1597 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1605 if (p->dca_ptr == 2)
1610 if (ch == SYM_CHAR_RESET)
1612 vt102_reset_state (v);
1614 else if (p->in_cmd && !ctrl_chr(ch,p->cmd_termination))
1616 p->cmd_buf[p->cmd_ptr++] = ch;
1617 if (p->cmd_ptr==VT102_CMD_LEN)
1619 if (p->cmd_more_bytes)
1621 p->cmd_more_bytes--;
1624 if (!p->cmd_more_bytes == 1)
1628 switch (p->cmd_termination)
1633 if (p->cmd_termination == ch)
1644 vt102_parse_esc (c);
1645 p->cmd_more_bytes = 0;
1646 p->cmd_termination = 0;
1649 else if (p->in_escape && !ctrl_chr(ch,0))
1652 p->cmd_buf[p->cmd_ptr++] = ch;
1655 pre_parse_cmd (ch, p);
1658 vt102_parse_esc (c);
1660 else if (ch == 0x9b) /*One byte CSI */
1663 p->cmd_buf[p->cmd_ptr++] = '[';
1665 pre_parse_cmd (ch, p);
1672 "<chr in x=%3d y=%2d aw=%d \\%03o %3d %c",
1673 v->pos.x, v->pos.y, v->pending_wrap,ch,ch,safe_ch(ch));
1686 vt102_send_id (c, terminal_id);
1692 vt102_cursor_retreat (c->v);
1695 vt102_cursor_advance_tab (c->v);
1700 vt102_cursor_advance_line (c);
1701 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1704 vt102_cursor_carriage_return (v);
1735 /*regular character */ default:
1736 vt102_regular_char (c, v, ch);
1741 log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,v->pending_wrap);
1745 v->crt.pos = v->pos;
1746 v->crt.hide_cursor =
1747 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1749 if (v->current_line.y != v->pos.y)
1751 vt102_log_line (c, v->current_line.y);
1752 v->current_line = v->pos;
1756 cmd_show_status (c->d, c);
1760 vt102_send (Context * c, uint8_t key)
1768 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1770 if ((key > 31) && (key < 127))
1772 c->t->xmit (c->t, &key, 1);
1791 c->t->xmit (c->t, &key, 1);
1794 c->t->xmit (c->t, &key, 1);
1795 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1798 c->t->xmit (c->t, &ch, 1);
1814 c->t->xmit (c->t, &key, 1);
1822 c->t->xmit (c->t, &key, 1);
1833 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1835 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1836 c->t->xmit (c->t, &buf, sizeof (buf));
1840 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1841 c->t->xmit (c->t, &buf, sizeof (buf));
1859 if (c->v->application_keypad_mode)
1861 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1862 c->t->xmit (c->t, &buf, sizeof (buf));
1866 static char kpoff[KEY_NUM] = {
1885 c->t->xmit (c->t, &kpoff[key], 1);
1889 if (c->v->application_keypad_mode)
1891 uint8_t buf[] = { 033, 'O', 'M' };
1892 c->t->xmit (c->t, &buf, sizeof (buf));
1897 c->t->xmit (c->t, &ch, 1);
1898 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1901 c->t->xmit (c->t, &ch, 1);
1910 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1911 c->t->xmit (c->t, &buf, sizeof (buf));
1919 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1920 c->t->xmit (c->t, &buf, sizeof (buf));
1928 vt102_reset (VT102 * v)
1930 VT102_parser *p = &v->parser;
1934 v->current_line = v->pos;
1935 v->pending_wrap = 0;
1937 v->screen_start.x = 0;
1938 v->screen_start.y = 0;
1939 v->current_width=VT102_COLS_80;
1940 v->crt.width=v->current_width;
1941 v->screen_end.x = v->current_width -1;
1942 v->screen_end.y = VT102_ROWS - 1;
1944 vt102_cursor_home (v);
1945 vt102_status_line (v, "");
1947 vt102_reset_tabs (v);
1948 v->current_line = v->pos;
1950 vt102_parser_reset (p);
1951 vt102_reset_state (v);
1953 vt102_save_state (v);
1955 v->last_reg_char = ' ';
1963 v = (VT102 *) malloc (sizeof (VT102));
1974 vt102_set_ansi (VT102 * v, int ansi)
1976 v->xn_glitch = ansi ? 0 : 1;
1980 vt102_free (VT102 * v)