4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.40 2008/02/26 19:00:59 james
14 * *** empty log message ***
16 * Revision 1.39 2008/02/26 16:54:06 james
17 * *** empty log message ***
19 * Revision 1.38 2008/02/26 16:53:24 james
20 * *** empty log message ***
22 * Revision 1.37 2008/02/24 12:22:53 james
23 * *** empty log message ***
25 * Revision 1.36 2008/02/24 12:22:42 james
26 * *** empty log message ***
28 * Revision 1.35 2008/02/24 00:42:53 james
29 * *** empty log message ***
31 * Revision 1.34 2008/02/23 11:48:37 james
32 * *** empty log message ***
34 * Revision 1.33 2008/02/22 23:39:27 james
35 * *** empty log message ***
37 * Revision 1.32 2008/02/22 19:12:05 james
38 * *** empty log message ***
40 * Revision 1.31 2008/02/22 17:07:00 james
41 * *** empty log message ***
43 * Revision 1.30 2008/02/22 14:51:54 james
44 * *** empty log message ***
46 * Revision 1.29 2008/02/15 03:32:07 james
47 * *** empty log message ***
49 * Revision 1.28 2008/02/14 10:34:30 james
50 * *** empty log message ***
52 * Revision 1.27 2008/02/14 02:46:45 james
53 * *** empty log message ***
55 * Revision 1.26 2008/02/14 01:55:57 james
56 * *** empty log message ***
58 * Revision 1.25 2008/02/13 16:57:29 james
59 * *** empty log message ***
61 * Revision 1.24 2008/02/13 09:12:21 james
62 * *** empty log message ***
64 * Revision 1.23 2008/02/07 13:26:35 james
65 * *** empty log message ***
67 * Revision 1.22 2008/02/07 13:22:51 james
68 * *** empty log message ***
70 * Revision 1.21 2008/02/07 12:21:16 james
71 * *** empty log message ***
73 * Revision 1.20 2008/02/07 12:16:04 james
74 * *** empty log message ***
76 * Revision 1.19 2008/02/07 11:27:02 james
77 * *** empty log message ***
79 * Revision 1.18 2008/02/07 01:59:25 james
80 * *** empty log message ***
82 * Revision 1.17 2008/02/07 01:58:28 james
83 * *** empty log message ***
85 * Revision 1.16 2008/02/07 01:57:46 james
86 * *** empty log message ***
88 * Revision 1.15 2008/02/07 00:43:27 james
89 * *** empty log message ***
91 * Revision 1.14 2008/02/07 00:40:23 james
92 * *** empty log message ***
94 * Revision 1.13 2008/02/07 00:39:59 james
95 * *** empty log message ***
97 * Revision 1.12 2008/02/07 00:39:13 james
98 * *** empty log message ***
100 * Revision 1.11 2008/02/06 20:26:58 james
101 * *** empty log message ***
103 * Revision 1.10 2008/02/06 17:53:28 james
104 * *** empty log message ***
106 * Revision 1.9 2008/02/06 15:53:22 james
107 * *** empty log message ***
109 * Revision 1.8 2008/02/06 11:49:47 james
110 * *** empty log message ***
112 * Revision 1.7 2008/02/06 11:30:37 james
113 * *** empty log message ***
115 * Revision 1.6 2008/02/05 01:11:46 james
116 * *** empty log message ***
118 * Revision 1.5 2008/02/04 20:23:55 james
119 * *** empty log message ***
121 * Revision 1.4 2008/02/04 05:45:55 james
124 * Revision 1.3 2008/02/04 02:05:06 james
125 * *** empty log message ***
127 * Revision 1.2 2008/02/04 01:32:39 james
128 * *** empty log message ***
130 * Revision 1.1 2008/02/03 23:36:41 james
131 * *** empty log message ***
142 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
144 vt100|vt100-am|dec vt100 (w/advanced video):\
146 :co#80:it#8:li#24:vt#3:\
147 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
149 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
150 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
151 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
152 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
153 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
154 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
155 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
156 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
157 :us=\E[4m:tc=vt100+fnkeys:
159 vt100+fnkeys|dec vt100 numeric keypad:\
160 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
163 vt100+pfkeys|dec vt100 numeric keypad:\
164 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
166 vt100+keypad|dec vt100 numeric keypad no fkeys:\
167 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
172 so the parser needs to be able to at least do
234 ansi|ansi/pc-term compatible with color:\
235 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
236 :tc=klone+sgr:tc=ansi-m:
237 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
239 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
240 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
241 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
242 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
243 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
244 :s3=\E+B:ta=\E[I:tc=pcansi-m:
245 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
248 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
249 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
250 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
251 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
253 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
254 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
255 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
256 klone+acs|alternate character set for ansi.sys displays:\
257 :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:\
258 :ae=\E[10m:as=\E[11m:
259 ecma+color|color control for ECMA-48-compatible terminals:\
261 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
264 Ignoreing ones in the VT102 spec
294 //static char terminal_reply[]={033,0133,077,066,0143};
296 #define TABLE_LENGTH 128
298 static char terminal_id[]="vt102";
300 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
301 int vt102_cmd_length[TABLE_LENGTH] = {
311 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
313 int vt102_cmd_termination[TABLE_LENGTH] = {
335 if ((c >= 'a') && (c <= 'z'))
337 if ((c >= 'A') && (c <= 'Z'))
346 /*Stay or enter command acquision and exit after n more chars*/
348 redispatch_with_more (VT102_parser * p, int n)
353 in_margins (VT102 * v, CRT_Pos p)
355 if (v->pos.x < v->top_margin.x)
357 if (v->pos.y < v->top_margin.y)
360 if (v->pos.x > v->bottom_margin.x)
362 if (v->pos.y > v->bottom_margin.y)
369 vt102_log_line (Context * c, int line)
371 CRT_Pos e = { VT102_COLS - 1, line };
372 CRT_Pos p = { 0, line };
373 char logbuf[VT102_COLS + 1];
379 for (; e.x > 0; --e.x)
381 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
385 for (; p.x <= e.x; ++p.x)
387 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
396 c->l->log (c->l, logbuf);
399 /*Called for every upward scroll with same args*/
401 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
403 /*Only log if it scrolls off the top*/
408 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
412 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
427 vt102_cursor_normalize (VT102 * v)
429 CRT_Pos *top, *bottom;
431 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
433 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
437 vt102_clip_cursor (v, v->screen_start, v->screen_end);
443 vt102_cursor_carriage_return (VT102 * v)
445 /*FISH*/ v->pos.x = v->top_margin.x;
450 vt102_cursor_advance_line (Context * c)
453 int couldscroll = in_margins (v, v->pos);
455 /*have wraped off end of last line in scrolling region */
456 /* (|| not necessary, but shuts compiler up */
457 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
460 vt102_log_line (c, v->pos.y);
462 vt102_history (c, v->top_margin, v->bottom_margin);
464 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
468 if (v->pos.y != v->screen_end.y)
475 vt102_cursor_retreat_line (Context * c)
478 int couldscroll = in_margins (v, v->pos);
480 /*have wraped off end of last line in scrolling region */
481 /* (|| not necessary, but shuts compiler up */
482 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
485 vt102_log_line (c, v->pos.y);
487 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
491 if (v->pos.y != v->screen_start.y)
498 vt102_do_pending_wrap (Context * c)
501 int couldscroll = in_margins (v, v->pos);
502 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
504 if (!v->pending_wrap)
508 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
509 v->pos.y, autowrap, couldscroll);
512 /*End of line but no autowrap, nothing to do*/
516 /*End of screen and not allowed to scroll, nothing to do*/
517 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
522 v->pos.x = v->top_margin.x;
529 vt102_cursor_advance_line (c);
533 vt102_cursor_advance (Context * c)
537 if (v->pos.x < v->bottom_margin.x)
545 if (!c->v->xn_glitch)
546 vt102_do_pending_wrap (c);
552 vt102_cursor_retreat (VT102 * v)
554 if (v->pos.x != v->top_margin.x)
561 vt102_reset_tabs (VT102 * v)
565 memset (v->tabs, 0, sizeof (v->tabs));
567 for (i = 0; i < VT102_COLS; i += 8)
573 vt102_cursor_advance_tab (VT102 * v)
575 if (v->pos.x == v->bottom_margin.x)
577 while (v->pos.x < v->bottom_margin.x)
580 if (v->tabs[v->pos.x])
587 vt102_cursor_retreat_tab (VT102 * v)
589 if (v->pos.x == v->top_margin.x)
591 while (v->pos.x > v->top_margin.x)
594 if (v->tabs[v->pos.x])
600 vt102_cursor_home (VT102 * v)
602 v->pos = v->top_margin;
603 vt102_cursor_normalize (v);
608 vt102_cursor_absolute (VT102 * v, int x, int y)
610 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
612 v->pos.x = x + v->top_margin.x;
613 v->pos.y = y + v->top_margin.x;
620 vt102_cursor_normalize (v);
624 vt102_cursor_relative (VT102 * v, int x, int y)
628 vt102_cursor_normalize (v);
635 vt102_delete_from_line (VT102 * v, CRT_Pos p)
637 int n = v->bottom_margin.x - p.x;
645 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
646 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
649 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
650 /*But not attr due to vt102 bug*/
654 vt102_insert_into_line (VT102 * v, CRT_Pos p)
656 int n = v->bottom_margin.x - p.x;
664 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
665 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
668 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
669 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
670 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
676 vt102_change_mode (VT102 * v, int private, char *ns, int set)
692 if (m >= VT102_NMODES)
697 v->private_modes[m] = set;
700 case VT102_PRIVATE_MODE_CURSOR_MODE:
701 if (v->application_keypad_mode)
702 v->private_modes[m] = 0;
704 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
705 v->private_modes[m], set);
708 case VT102_PRIVATE_MODE_ORIGIN_MODE:
709 vt102_cursor_home (v);
711 case VT102_PRIVATE_MODE_132COLS:
712 /* We don't implement 132 col mode - yet*/
713 v->top_margin = v->screen_start;
714 v->bottom_margin = v->screen_end;
715 vt102_cursor_home(v);
727 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
732 vt102_parse_mode_string (VT102 * v, char *buf, int len)
735 char last = buf[len - 1];
739 memset (num, 0, sizeof (num));
740 o = sizeof (num) - 1;
758 vt102_change_mode (v, private, &num[o], last == 'h');
759 memset (num, 0, sizeof (num));
760 o = sizeof (num) - 1;
775 vt102_change_mode (v, private, &num[o], last == 'h');
781 vt102_change_attr (VT102 * v, char *na)
798 v->attr = CRT_ATTR_NORMAL;
799 v->color = CRT_COLOR_NORMAL;
802 v->attr |= CRT_ATTR_BOLD;
806 v->attr &= ~CRT_ATTR_BOLD;
809 v->attr |= CRT_ATTR_UNDERLINE;
812 v->attr &= ~CRT_ATTR_UNDERLINE;
815 v->attr |= CRT_ATTR_BLINK;
818 v->attr &= ~CRT_ATTR_BLINK;
821 v->attr |= CRT_ATTR_REVERSE;
824 v->attr &= ~CRT_ATTR_REVERSE;
834 v->color &= ~CRT_COLOR_FG_MASK;
835 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
845 v->color &= ~CRT_COLOR_FG_MASK;
848 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
853 v->color &= ~CRT_COLOR_FG_MASK;
855 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
865 v->color &= ~CRT_COLOR_BG_MASK;
866 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
876 v->color &= ~CRT_COLOR_BG_MASK;
879 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
884 v->color &= ~CRT_COLOR_BG_MASK;
886 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
892 fprintf (stderr, "unhandled SGR %d\n", a);
900 vt102_parse_attr_string (VT102 * v, char *buf, int len)
903 char last = buf[len - 1];
907 memset (num, 0, sizeof (num));
908 o = sizeof (num) - 1;
919 vt102_change_attr (v, &num[o]);
920 memset (num, 0, sizeof (num));
921 o = sizeof (num) - 1;
935 vt102_change_attr (v, &num[o]);
939 vt102_save_state (VT102 * v)
941 v->saved.pos = v->pos;
942 v->saved.attr = v->attr;
943 v->saved.color = v->color;
944 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
948 vt102_restore_state (VT102 * v)
950 v->pos = v->saved.pos;
951 v->attr = v->saved.attr;
952 v->color = v->saved.color;
953 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
954 vt102_cursor_normalize (v);
959 vt102_regular_char (Context * c, VT102 * v, int ch)
962 vt102_do_pending_wrap (c);
964 if (v->modes[VT102_MODE_INSERT])
965 vt102_insert_into_line (v, v->pos);
967 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
968 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
969 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
970 vt102_cursor_advance (c);
972 v->last_reg_char = ch;
975 vt102_send_id(Context *c,char *buf)
981 c->t->xmit (c->t, buf,l);
986 vt102_scs (Context * c, int g, int s)
988 /*Ignoring charsets*/
992 vt102_parse_csi (Context * c, char *buf, int len)
1003 last = buf[len - 1];
1024 vt102_insert_into_line (v, v->pos);
1027 vt102_cursor_relative (v, 0, -narg);
1031 vt102_cursor_relative (v, 0, narg);
1035 vt102_cursor_relative (v, narg, 0);
1038 vt102_cursor_relative (v, -narg, 0);
1041 vt102_cursor_relative (v, 0, narg);
1042 vt102_cursor_carriage_return (v);
1045 vt102_cursor_relative (v, 0, -narg);
1046 vt102_cursor_carriage_return (v);
1049 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1058 ptr = index (arg, ';');
1060 x = atoi (ptr + 1) - 1;
1064 vt102_cursor_absolute (v, x, y);
1069 vt102_cursor_advance_tab (c->v);
1073 narg = 0; /*Different default */
1077 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1080 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1083 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1090 CRT_Pos ls = { 0, v->pos.y };
1091 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1093 narg = 0; /*Different default */
1098 crt_erase (&v->crt, v->pos, le, 1, v->color);
1101 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1104 crt_erase (&v->crt, ls, le, 1, v->color);
1111 if ((v->pos.y >= v->top_margin.y)
1112 && (v->pos.y <= v->bottom_margin.y))
1115 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1121 if ((v->pos.y >= v->top_margin.y)
1122 && (v->pos.y <= v->bottom_margin.y))
1126 vt102_history (c, v->pos, v->bottom_margin);
1127 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1134 vt102_delete_from_line (v, v->pos);
1137 //FIXME: cursor position report - does anything use that?
1142 vt102_history (c, v->top_margin, v->bottom_margin);
1143 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1149 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1154 CRT_Pos end = v->pos;
1159 if (end.x > v->bottom_margin.x)
1160 end.x = v->bottom_margin.x;
1162 crt_erase (&v->crt, v->pos, end, 1, v->color);
1167 vt102_cursor_retreat_tab (c->v);
1170 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1174 vt102_regular_char (c, v, v->last_reg_char);
1177 vt102_send_id(c,terminal_id);
1180 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1184 narg = 0; /*Different default */
1189 v->tabs[v->pos.x] = 0;
1191 case 2: //FIXME: - LA120 says current line only WTF?
1193 memset (v->tabs, 0, sizeof (v->tabs));
1198 case 'i': //Printer commands
1203 vt102_parse_mode_string (v, &buf[1], len - 1);
1208 vt102_parse_attr_string (v, &buf[1], len - 1);
1210 case 'n': //Device status report
1211 //5 requestion status
1212 // 6 request cursor position FIXME
1213 //?15n printer status
1215 case 'q': //Load LED on off
1218 v->top_margin = v->screen_start;
1219 v->bottom_margin = v->screen_end;
1221 if ((len > 2) && (ptr = index (arg, ';')))
1224 v->top_margin.y = narg - 1;
1225 v->bottom_margin.y = atoi (ptr) - 1;
1228 if (v->top_margin.y < v->screen_start.y)
1229 v->top_margin.y = v->screen_start.y;
1230 if (v->top_margin.y > v->screen_end.y)
1231 v->top_margin.y = v->screen_end.y;
1232 if (v->bottom_margin.y < v->screen_start.y)
1233 v->bottom_margin.y = v->screen_start.y;
1234 if (v->bottom_margin.y > v->screen_end.y)
1235 v->bottom_margin.y = v->screen_end.y;
1237 vt102_cursor_home (v);
1240 v->saved.pos = v->pos;
1243 v->pos = v->saved.pos;
1244 vt102_cursor_normalize (v);
1245 v->pending_wrap = 0;
1247 case 'y': //Invoke confidence test
1251 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1258 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1268 vt102_parse_esc (Context * c)
1271 VT102_parser *p = &v->parser;
1274 p->cmd_buf[p->cmd_ptr]=0;
1275 log_f (c->l, "<cmd in x=%d y=%d <ESC>%s >",v->pos.x,v->pos.y,p->cmd_buf);
1279 switch (p->cmd_buf[0])
1282 vt102_cursor_relative (v, 0, -1);
1285 vt102_cursor_relative (v, 0, 1);
1288 vt102_cursor_relative (v, 1, 0);
1291 vt102_cursor_relative (v, -1, 0);
1297 //Quit graphics mode
1300 vt102_cursor_absolute (v, 0, 0);
1303 vt102_cursor_retreat_line (c);
1306 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1310 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1311 crt_erase (&v->crt, v->pos, le, 1, v->color);
1315 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1317 case 'V': //Print current line
1319 case 'W': //Printer on
1321 case 'X': //printer off
1323 case ']'://print screen
1326 vt102_send_id(c,"\033/Z");
1329 case '^': //Autoprint on
1331 case '_'://Autoprint off
1334 v->application_keypad_mode = 1;
1337 v->application_keypad_mode = 0;
1342 /*If you edit this switch don't forget to edit the length and termination tables*/
1343 switch (p->cmd_buf[0])
1346 vt102_cursor_advance_line(c);
1350 vt102_cursor_advance_line(c);
1351 v->pos.x=v->top_margin.x;
1352 vt102_cursor_normalize (v);
1353 v->pending_wrap = 0;
1356 v->tabs[v->pos.x]++;
1359 vt102_cursor_retreat_line (c);
1361 case 'N': //select G2 for one char
1363 case 'O': //select G3 for one char
1366 vt102_send_id(c,terminal_id);
1374 v->application_keypad_mode = 1;
1377 v->application_keypad_mode = 0;
1381 switch (p->cmd_buf[1])
1383 case '3': //top of double height line
1384 case '4': //bottom of double height line
1385 case '5': //single width line
1386 case '6': //double width line
1392 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1394 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1395 v->crt.screen[i].chr = 'E';
1401 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1402 __FILE__, __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1407 /*Set ansi mode - ignored */
1411 vt102_save_state (v);
1414 vt102_restore_state (v);
1416 case ']': /*Set various titles */
1420 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1422 case '(': /*Charsets */
1427 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1430 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1431 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1436 p->cmd_buf[p->cmd_ptr]=0;
1437 log_f (c->l, "<cmd out x=%d y=%d>",v->pos.x,v->pos.y);
1441 vt102_status_line (VT102 * v, char *str)
1444 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1448 ca->attr = CRT_ATTR_REVERSE;
1449 ca->color = CRT_COLOR_NORMAL;
1460 vt102_parser_reset (VT102_parser * p)
1463 p->cmd_more_bytes = 0;
1464 p->cmd_termination = 0;
1470 vt102_reset_state (VT102 * v)
1472 vt102_parser_reset (&v->parser);
1474 v->attr = CRT_ATTR_NORMAL;
1475 v->color = CRT_COLOR_NORMAL;
1477 v->application_keypad_mode = 0;
1479 v->top_margin = v->screen_start;
1480 v->bottom_margin = v->screen_end;
1482 memset (v->modes, 0, VT102_NMODES);
1483 memset (v->private_modes, 0, VT102_NMODES);
1485 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1486 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1487 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1488 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1490 vt102_reset_tabs (v);
1494 pre_parse_cmd (int ch, VT102_parser * p)
1496 if (ch > TABLE_LENGTH)
1501 p->cmd_more_bytes = 0;
1504 p->cmd_termination = vt102_cmd_termination[ch];
1505 if (p->cmd_termination)
1511 p->cmd_more_bytes = vt102_cmd_length[ch];
1513 if (p->cmd_more_bytes)
1521 vt102_parse_char (Context * c, int ch)
1524 VT102_parser *p = &v->parser;
1527 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1528 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1529 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1532 /* Turn anything non-ascii into '?' */
1534 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1542 if (p->dca_ptr == 2)
1547 if (ch == SYM_CHAR_RESET)
1549 vt102_reset_state (v);
1553 p->cmd_buf[p->cmd_ptr++] = ch;
1554 if (p->cmd_more_bytes) {
1555 p->cmd_more_bytes--;
1558 if (!p->cmd_more_bytes == 1)
1562 switch (p->cmd_termination)
1567 if (p->cmd_termination == ch)
1578 vt102_parse_esc (c);
1579 p->cmd_more_bytes = 0;
1580 p->cmd_termination = 0;
1583 else if (p->in_escape)
1587 p->cmd_buf[p->cmd_ptr++] = ch;
1590 pre_parse_cmd (ch, p);
1593 vt102_parse_esc (c);
1595 else if (ch == 0x9b) /*One byte CSI */
1598 p->cmd_buf[p->cmd_ptr++] = '[';
1600 pre_parse_cmd (ch, p);
1614 vt102_send_id(c,terminal_id);
1620 vt102_cursor_retreat (c->v);
1623 vt102_cursor_advance_tab (c->v);
1628 vt102_cursor_advance_line (c);
1629 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1632 vt102_cursor_carriage_return (v);
1636 /*Ignoring charsets */
1640 /*Ignoring charsets */
1663 /*regular character */ default:
1665 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%3d %2d %2d %d", ch,
1666 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1667 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1669 vt102_regular_char (c, v, ch);
1673 v->crt.pos = v->pos;
1674 v->crt.hide_cursor =
1675 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1677 if (v->current_line.y != v->pos.y)
1679 vt102_log_line (c, v->current_line.y);
1680 v->current_line = v->pos;
1684 cmd_show_status (c->d, c);
1688 vt102_send (Context * c, uint8_t key)
1696 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1698 if ((key > 31) && (key < 127))
1700 c->t->xmit (c->t, &key, 1);
1719 c->t->xmit (c->t, &key, 1);
1722 c->t->xmit (c->t, &key, 1);
1723 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1726 c->t->xmit (c->t, &ch, 1);
1742 c->t->xmit (c->t, &key, 1);
1750 c->t->xmit (c->t, &key, 1);
1761 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1763 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1764 c->t->xmit (c->t, &buf, sizeof (buf));
1768 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1769 c->t->xmit (c->t, &buf, sizeof (buf));
1787 if (c->v->application_keypad_mode)
1789 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1790 c->t->xmit (c->t, &buf, sizeof (buf));
1794 static char kpoff[KEY_NUM] = {
1813 c->t->xmit (c->t, &kpoff[key], 1);
1817 if (c->v->application_keypad_mode)
1819 uint8_t buf[] = { 033, 'O', 'M' };
1820 c->t->xmit (c->t, &buf, sizeof (buf));
1825 c->t->xmit (c->t, &ch, 1);
1826 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1829 c->t->xmit (c->t, &ch, 1);
1838 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1839 c->t->xmit (c->t, &buf, sizeof (buf));
1847 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1848 c->t->xmit (c->t, &buf, sizeof (buf));
1856 vt102_reset (VT102 * v)
1858 VT102_parser *p = &v->parser;
1862 v->current_line = v->pos;
1863 v->pending_wrap = 0;
1865 v->screen_start.x = 0;
1866 v->screen_start.y = 0;
1867 v->screen_end.x = VT102_COLS - 1;
1868 v->screen_end.y = VT102_ROWS - 1;
1870 vt102_cursor_home (v);
1871 vt102_status_line (v, "");
1873 vt102_reset_tabs (v);
1874 v->current_line = v->pos;
1876 vt102_parser_reset (p);
1877 vt102_reset_state (v);
1879 vt102_save_state (v);
1881 v->last_reg_char = ' ';
1889 v = (VT102 *) malloc (sizeof (VT102));
1900 vt102_set_ansi (VT102 * v, int ansi)
1902 v->xn_glitch = ansi ? 0 : 1;
1906 vt102_free (VT102 * v)