4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.38 2008/02/26 16:53:24 james
14 * *** empty log message ***
16 * Revision 1.37 2008/02/24 12:22:53 james
17 * *** empty log message ***
19 * Revision 1.36 2008/02/24 12:22:42 james
20 * *** empty log message ***
22 * Revision 1.35 2008/02/24 00:42:53 james
23 * *** empty log message ***
25 * Revision 1.34 2008/02/23 11:48:37 james
26 * *** empty log message ***
28 * Revision 1.33 2008/02/22 23:39:27 james
29 * *** empty log message ***
31 * Revision 1.32 2008/02/22 19:12:05 james
32 * *** empty log message ***
34 * Revision 1.31 2008/02/22 17:07:00 james
35 * *** empty log message ***
37 * Revision 1.30 2008/02/22 14:51:54 james
38 * *** empty log message ***
40 * Revision 1.29 2008/02/15 03:32:07 james
41 * *** empty log message ***
43 * Revision 1.28 2008/02/14 10:34:30 james
44 * *** empty log message ***
46 * Revision 1.27 2008/02/14 02:46:45 james
47 * *** empty log message ***
49 * Revision 1.26 2008/02/14 01:55:57 james
50 * *** empty log message ***
52 * Revision 1.25 2008/02/13 16:57:29 james
53 * *** empty log message ***
55 * Revision 1.24 2008/02/13 09:12:21 james
56 * *** empty log message ***
58 * Revision 1.23 2008/02/07 13:26:35 james
59 * *** empty log message ***
61 * Revision 1.22 2008/02/07 13:22:51 james
62 * *** empty log message ***
64 * Revision 1.21 2008/02/07 12:21:16 james
65 * *** empty log message ***
67 * Revision 1.20 2008/02/07 12:16:04 james
68 * *** empty log message ***
70 * Revision 1.19 2008/02/07 11:27:02 james
71 * *** empty log message ***
73 * Revision 1.18 2008/02/07 01:59:25 james
74 * *** empty log message ***
76 * Revision 1.17 2008/02/07 01:58:28 james
77 * *** empty log message ***
79 * Revision 1.16 2008/02/07 01:57:46 james
80 * *** empty log message ***
82 * Revision 1.15 2008/02/07 00:43:27 james
83 * *** empty log message ***
85 * Revision 1.14 2008/02/07 00:40:23 james
86 * *** empty log message ***
88 * Revision 1.13 2008/02/07 00:39:59 james
89 * *** empty log message ***
91 * Revision 1.12 2008/02/07 00:39:13 james
92 * *** empty log message ***
94 * Revision 1.11 2008/02/06 20:26:58 james
95 * *** empty log message ***
97 * Revision 1.10 2008/02/06 17:53:28 james
98 * *** empty log message ***
100 * Revision 1.9 2008/02/06 15:53:22 james
101 * *** empty log message ***
103 * Revision 1.8 2008/02/06 11:49:47 james
104 * *** empty log message ***
106 * Revision 1.7 2008/02/06 11:30:37 james
107 * *** empty log message ***
109 * Revision 1.6 2008/02/05 01:11:46 james
110 * *** empty log message ***
112 * Revision 1.5 2008/02/04 20:23:55 james
113 * *** empty log message ***
115 * Revision 1.4 2008/02/04 05:45:55 james
118 * Revision 1.3 2008/02/04 02:05:06 james
119 * *** empty log message ***
121 * Revision 1.2 2008/02/04 01:32:39 james
122 * *** empty log message ***
124 * Revision 1.1 2008/02/03 23:36:41 james
125 * *** empty log message ***
136 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
138 vt100|vt100-am|dec vt100 (w/advanced video):\
140 :co#80:it#8:li#24:vt#3:\
141 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
143 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
144 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
145 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
146 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
147 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
148 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
149 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
150 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
151 :us=\E[4m:tc=vt100+fnkeys:
153 vt100+fnkeys|dec vt100 numeric keypad:\
154 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
157 vt100+pfkeys|dec vt100 numeric keypad:\
158 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
160 vt100+keypad|dec vt100 numeric keypad no fkeys:\
161 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
166 so the parser needs to be able to at least do
228 ansi|ansi/pc-term compatible with color:\
229 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
230 :tc=klone+sgr:tc=ansi-m:
231 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
233 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
234 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
235 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
236 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
237 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
238 :s3=\E+B:ta=\E[I:tc=pcansi-m:
239 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
242 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
243 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
244 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
245 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
247 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
248 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
249 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
250 klone+acs|alternate character set for ansi.sys displays:\
251 :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:\
252 :ae=\E[10m:as=\E[11m:
253 ecma+color|color control for ECMA-48-compatible terminals:\
255 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
258 Ignoreing ones in the VT102 spec
288 //static char terminal_reply[]={033,0133,077,066,0143};
289 static char terminal_reply[] = { 'v', 't', '1', '0', '2' };
291 #define TABLE_LENGTH 128
293 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
294 int vt102_cmd_length[TABLE_LENGTH]={
304 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
306 int vt102_cmd_termination[TABLE_LENGTH]={
328 if ((c >= 'a') && (c <= 'z'))
330 if ((c >= 'A') && (c <= 'Z'))
339 /*Stay or enter command acquision and exit after n more chars*/
340 void redispatch_with_more(VT102_parser *p,int n)
345 in_margins (VT102 * v, CRT_Pos p)
347 if (v->pos.x < v->top_margin.x)
349 if (v->pos.y < v->top_margin.y)
352 if (v->pos.x > v->bottom_margin.x)
354 if (v->pos.y > v->bottom_margin.y)
361 vt102_log_line (Context * c, int line)
363 CRT_Pos e = { VT102_COLS - 1, line };
364 CRT_Pos p = { 0, line };
365 char logbuf[VT102_COLS + 1];
371 for (; e.x > 0; --e.x)
373 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
377 for (; p.x <= e.x; ++p.x)
379 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
388 c->l->log (c->l, logbuf);
391 /*Called for every upward scroll with same args*/
393 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
395 /*Only log if it scrolls off the top*/
400 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
404 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
419 vt102_cursor_normalize (VT102 * v)
421 CRT_Pos *top, *bottom;
423 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
425 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
429 vt102_clip_cursor (v, v->screen_start, v->screen_end);
435 vt102_cursor_carriage_return (VT102 * v)
437 /*FISH*/ v->pos.x = v->top_margin.x;
442 vt102_cursor_advance_line (Context * c)
445 int couldscroll = in_margins (v, v->pos);
447 /*have wraped off end of last line in scrolling region */
448 /* (|| not necessary, but shuts compiler up */
449 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
452 vt102_log_line (c, v->pos.y);
454 vt102_history (c, v->top_margin, v->bottom_margin);
456 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
460 if (v->pos.y != v->screen_end.y)
467 vt102_cursor_retreat_line (Context * c)
470 int couldscroll = in_margins (v, v->pos);
472 /*have wraped off end of last line in scrolling region */
473 /* (|| not necessary, but shuts compiler up */
474 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
477 vt102_log_line (c, v->pos.y);
479 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
483 if (v->pos.y != v->screen_start.y)
490 vt102_do_pending_wrap (Context * c)
493 int couldscroll = in_margins (v, v->pos);
494 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
496 if (!v->pending_wrap)
500 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
501 v->pos.y, autowrap, couldscroll);
504 /*End of line but no autowrap, nothing to do*/
508 /*End of screen and not allowed to scroll, nothing to do*/
509 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
514 v->pos.x = v->top_margin.x;
521 vt102_cursor_advance_line (c);
525 vt102_cursor_advance (Context * c)
529 if (v->pos.x < v->bottom_margin.x)
537 if (!c->v->xn_glitch)
538 vt102_do_pending_wrap (c);
544 vt102_cursor_retreat (VT102 * v)
546 if (v->pos.x != v->top_margin.x)
553 vt102_reset_tabs (VT102 * v)
557 memset (v->tabs, 0, sizeof (v->tabs));
559 for (i = 0; i < VT102_COLS; i += 8)
565 vt102_cursor_advance_tab (VT102 * v)
567 if (v->pos.x == v->bottom_margin.x)
569 while (v->pos.x < v->bottom_margin.x)
572 if (v->tabs[v->pos.x])
579 vt102_cursor_retreat_tab (VT102 * v)
581 if (v->pos.x == v->top_margin.x)
583 while (v->pos.x > v->top_margin.x)
586 if (v->tabs[v->pos.x])
592 vt102_cursor_home (VT102 * v)
594 v->pos = v->top_margin;
595 vt102_cursor_normalize (v);
600 vt102_cursor_absolute (VT102 * v, int x, int y)
602 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
604 v->pos.x = x + v->top_margin.x;
605 v->pos.y = y + v->top_margin.x;
612 vt102_cursor_normalize (v);
616 vt102_cursor_relative (VT102 * v, int x, int y)
620 vt102_cursor_normalize (v);
627 vt102_delete_from_line (VT102 * v, CRT_Pos p)
629 int n = v->bottom_margin.x - p.x;
637 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
638 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
641 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
642 /*But not attr due to vt102 bug*/
646 vt102_insert_into_line (VT102 * v, CRT_Pos p)
648 int n = v->bottom_margin.x - p.x;
656 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
657 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
660 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
661 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
662 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
668 vt102_change_mode (VT102 * v, int private, char *ns, int set)
684 if (m >= VT102_NMODES)
689 v->private_modes[m] = set;
692 case VT102_PRIVATE_MODE_CURSOR_MODE:
693 if (v->application_keypad_mode)
694 v->private_modes[m] = 0;
696 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
697 v->private_modes[m], set);
700 case VT102_PRIVATE_MODE_ORIGIN_MODE:
701 vt102_cursor_home (v);
710 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
715 vt102_parse_mode_string (VT102 * v, char *buf, int len)
718 char last = buf[len - 1];
722 memset (num, 0, sizeof (num));
723 o = sizeof (num) - 1;
741 vt102_change_mode (v, private, &num[o], last == 'h');
742 memset (num, 0, sizeof (num));
743 o = sizeof (num) - 1;
758 vt102_change_mode (v, private, &num[o], last == 'h');
764 vt102_change_attr (VT102 * v, char *na)
781 v->attr = CRT_ATTR_NORMAL;
782 v->color = CRT_COLOR_NORMAL;
785 v->attr |= CRT_ATTR_BOLD;
789 v->attr &= ~CRT_ATTR_BOLD;
792 v->attr |= CRT_ATTR_UNDERLINE;
795 v->attr &= ~CRT_ATTR_UNDERLINE;
798 v->attr |= CRT_ATTR_BLINK;
801 v->attr &= ~CRT_ATTR_BLINK;
804 v->attr |= CRT_ATTR_REVERSE;
807 v->attr &= ~CRT_ATTR_REVERSE;
817 v->color &= ~CRT_COLOR_FG_MASK;
818 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
828 v->color &= ~CRT_COLOR_FG_MASK;
831 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
836 v->color &= ~CRT_COLOR_FG_MASK;
838 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
848 v->color &= ~CRT_COLOR_BG_MASK;
849 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
859 v->color &= ~CRT_COLOR_BG_MASK;
862 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
867 v->color &= ~CRT_COLOR_BG_MASK;
869 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
875 fprintf (stderr, "unhandled SGR %d\n", a);
883 vt102_parse_attr_string (VT102 * v, char *buf, int len)
886 char last = buf[len - 1];
890 memset (num, 0, sizeof (num));
891 o = sizeof (num) - 1;
902 vt102_change_attr (v, &num[o]);
903 memset (num, 0, sizeof (num));
904 o = sizeof (num) - 1;
918 vt102_change_attr (v, &num[o]);
922 vt102_save_state (VT102 * v)
924 v->saved.pos = v->pos;
925 v->saved.attr = v->attr;
926 v->saved.color = v->color;
927 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
931 vt102_restore_state (VT102 * v)
933 v->pos = v->saved.pos;
934 v->attr = v->saved.attr;
935 v->color = v->saved.color;
936 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
937 vt102_cursor_normalize (v);
942 vt102_regular_char (Context * c, VT102 * v, int ch)
945 vt102_do_pending_wrap (c);
947 if (v->modes[VT102_MODE_INSERT])
948 vt102_insert_into_line (v, v->pos);
950 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
951 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
952 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
953 vt102_cursor_advance (c);
955 v->last_reg_char = ch;
959 vt102_scs (Context * c, int g, int s)
961 /*Ignoring charsets*/
965 vt102_parse_esc (Context * c)
968 VT102_parser *p=&v->parser;
970 /*If you edit this switch don't forget to edit the length and termination tables*/
971 switch(p->cmd_buf[0]) {
973 vt102_cursor_relative (v, 0, -1);
976 vt102_cursor_relative (v, 0, 1);
979 vt102_cursor_relative (v, 1, 0);
982 vt102_cursor_relative (v, -1, 0);
985 if (v->pos.y == v->bottom_margin.y)
987 vt102_log_line (c, v->pos.y);
988 vt102_history (c, v->top_margin, v->bottom_margin);
989 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
994 vt102_cursor_relative (v, 0, 1);
1001 vt102_cursor_retreat_line (c);
1004 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1008 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1009 crt_erase (&v->crt, v->pos, le, 1, v->color);
1013 if (v->pos.y == v->top_margin.y)
1015 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1020 vt102_cursor_relative (v, 0, -1);
1025 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1030 c->t->xmit (c->t, terminal_reply, sizeof (terminal_reply));
1035 switch(p->cmd_buf[1]) {
1039 crt_erase (&v->crt, v->screen_start, v->screen_end, 1, CRT_NORMAL_COLOR);
1040 for (i=0;i<CRT_ADDR_POS(&v->screen_end);++i)
1041 v->crt.screen[i].chr='E';
1045 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>", __FILE__,
1046 __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
1051 /*Set ansi mode - ignored */
1055 vt102_save_state (v);
1058 vt102_restore_state (v);
1061 v->application_keypad_mode = 1;
1064 v->application_keypad_mode = 0;
1066 case ']': /*Set various titles*/
1070 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1072 case '(': /*Charsets*/
1077 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1080 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1081 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1087 vt102_parse_csi (Context * c, char *buf, int len)
1091 char *arg = buf + 1;
1098 last = buf[len - 1];
1119 vt102_insert_into_line (v, v->pos);
1122 vt102_cursor_relative (v, 0, -narg);
1126 vt102_cursor_relative (v, 0, narg);
1130 vt102_cursor_relative (v, narg, 0);
1133 vt102_cursor_relative (v, -narg, 0);
1136 vt102_cursor_relative (v, 0, narg);
1137 vt102_cursor_carriage_return (v);
1140 vt102_cursor_relative (v, 0, -narg);
1141 vt102_cursor_carriage_return (v);
1144 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1153 ptr = index (arg, ';');
1155 x = atoi (ptr + 1) - 1;
1159 vt102_cursor_absolute (v, x, y);
1164 vt102_cursor_advance_tab (c->v);
1168 narg = 0; /*Different default */
1172 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1175 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1178 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1185 CRT_Pos ls = { 0, v->pos.y };
1186 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1188 narg = 0; /*Different default */
1193 crt_erase (&v->crt, v->pos, le, 1, v->color);
1196 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1199 crt_erase (&v->crt, ls, le, 1, v->color);
1206 if ((v->pos.y >= v->top_margin.y)
1207 && (v->pos.y <= v->bottom_margin.y))
1210 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1216 if ((v->pos.y >= v->top_margin.y)
1217 && (v->pos.y <= v->bottom_margin.y))
1221 vt102_history (c, v->pos, v->bottom_margin);
1222 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1229 vt102_delete_from_line (v, v->pos);
1232 //FIXME: cursor position report - does anything use that?
1237 vt102_history (c, v->top_margin, v->bottom_margin);
1238 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1244 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1249 CRT_Pos end = v->pos;
1254 if (end.x > v->bottom_margin.x)
1255 end.x = v->bottom_margin.x;
1257 crt_erase (&v->crt, v->pos, end, 1, v->color);
1262 vt102_cursor_retreat_tab (c->v);
1265 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1269 vt102_regular_char (c, v, v->last_reg_char);
1275 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1279 narg = 0; /*Different default */
1284 v->tabs[v->pos.x] = 0;
1286 case 2: //FIXME: - LA120 says current line only WTF?
1288 memset (v->tabs, 0, sizeof (v->tabs));
1293 case 'i': //4,5 Turn printer on and off WTF?
1298 vt102_parse_mode_string (v, &buf[1], len - 1);
1303 vt102_parse_attr_string (v, &buf[1], len - 1);
1305 case 'n': // 6 request cursor position
1309 v->top_margin = v->screen_start;
1310 v->bottom_margin = v->screen_end;
1312 if ((len > 2) && (ptr = index (arg, ';')))
1315 v->top_margin.y = narg - 1;
1316 v->bottom_margin.y = atoi (ptr) - 1;
1319 if (v->top_margin.y < v->screen_start.y)
1320 v->top_margin.y = v->screen_start.y;
1321 if (v->top_margin.y > v->screen_end.y)
1322 v->top_margin.y = v->screen_end.y;
1323 if (v->bottom_margin.y < v->screen_start.y)
1324 v->bottom_margin.y = v->screen_start.y;
1325 if (v->bottom_margin.y > v->screen_end.y)
1326 v->bottom_margin.y = v->screen_end.y;
1328 vt102_cursor_home (v);
1331 v->saved.pos = v->pos;
1334 v->pos = v->saved.pos;
1335 vt102_cursor_normalize (v);
1336 v->pending_wrap = 0;
1340 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1347 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__, __LINE__,
1357 vt102_status_line (VT102 * v, char *str)
1360 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1364 ca->attr = CRT_ATTR_REVERSE;
1365 ca->color = CRT_COLOR_NORMAL;
1376 vt102_parser_reset (VT102_parser * p)
1379 p->cmd_more_bytes=0;
1380 p->cmd_termination=0;
1386 vt102_reset_state (VT102 * v)
1388 vt102_parser_reset (&v->parser);
1390 v->attr = CRT_ATTR_NORMAL;
1391 v->color = CRT_COLOR_NORMAL;
1393 v->application_keypad_mode = 0;
1395 v->top_margin = v->screen_start;
1396 v->bottom_margin = v->screen_end;
1398 memset (v->modes, 0, VT102_NMODES);
1399 memset (v->private_modes, 0, VT102_NMODES);
1401 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1402 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1403 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1404 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1406 vt102_reset_tabs (v);
1409 static void pre_parse_cmd(int ch,VT102_parser *p)
1411 if (ch>TABLE_LENGTH) return;
1414 p->cmd_more_bytes=0;
1417 p->cmd_termination=vt102_cmd_termination[ch];
1418 if (p->cmd_termination) {
1423 p->cmd_more_bytes=vt102_cmd_length[ch];
1425 if (p->cmd_more_bytes) {
1432 vt102_parse_char (Context * c, int ch)
1435 VT102_parser *p = &v->parser;
1438 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%d %2d %2d %d", ch, safe_ch (ch),
1439 p->in_escape, p->in_cmd, p->cmd_more_bytes,p->cmd_termination, v->pos.x, v->pos.y,
1443 /* Turn anything non-ascii into '?' */
1445 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1453 if (p->dca_ptr == 2)
1458 if (ch == SYM_CHAR_RESET)
1460 vt102_reset_state (v);
1464 p->cmd_buf[p->cmd_ptr++] = ch;
1465 if (p->cmd_more_bytes) p->cmd_more_bytes--;
1468 if (p->cmd_more_bytes==1) p->in_cmd=0;
1470 switch (p->cmd_termination) {
1474 if (p->cmd_termination==ch) p->in_cmd=0;
1477 if (csi_ender(ch)) p->in_cmd=0;
1483 p->cmd_more_bytes=0;
1486 else if (p->in_escape)
1490 p->cmd_buf[p->cmd_ptr++] = ch;
1493 pre_parse_cmd(ch,p);
1498 else if (ch == 0x9b) /*One byte CSI */
1501 p->cmd_buf[p->cmd_ptr++] = '[';
1503 pre_parse_cmd(ch,p);
1519 c->t->xmit (c->t, "vt102", 5);
1526 vt102_cursor_retreat (c->v);
1529 vt102_cursor_advance_tab (c->v);
1534 vt102_cursor_advance_line (c);
1535 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1538 vt102_cursor_carriage_return (v);
1542 /*Ignoring charsets */
1546 /*Ignoring charsets */
1569 /*regular character */ default:
1570 vt102_regular_char (c, v, ch);
1574 v->crt.pos = v->pos;
1575 v->crt.hide_cursor =
1576 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1578 if (v->current_line.y != v->pos.y)
1580 vt102_log_line (c, v->current_line.y);
1581 v->current_line = v->pos;
1585 cmd_show_status (c->d, c);
1589 vt102_send (Context * c, uint8_t key)
1597 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1599 if ((key > 31) && (key < 127))
1601 c->t->xmit (c->t, &key, 1);
1620 c->t->xmit (c->t, &key, 1);
1623 c->t->xmit (c->t, &key, 1);
1624 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1627 c->t->xmit (c->t, &ch, 1);
1643 c->t->xmit (c->t, &key, 1);
1651 c->t->xmit (c->t, &key, 1);
1662 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1664 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1665 c->t->xmit (c->t, &buf, sizeof (buf));
1669 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1670 c->t->xmit (c->t, &buf, sizeof (buf));
1688 if (c->v->application_keypad_mode)
1690 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1691 c->t->xmit (c->t, &buf, sizeof (buf));
1695 static char kpoff[KEY_NUM] = {
1714 c->t->xmit (c->t, &kpoff[key], 1);
1718 if (c->v->application_keypad_mode)
1720 uint8_t buf[] = { 033, 'O', 'M' };
1721 c->t->xmit (c->t, &buf, sizeof (buf));
1726 c->t->xmit (c->t, &ch, 1);
1727 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1730 c->t->xmit (c->t, &ch, 1);
1739 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1740 c->t->xmit (c->t, &buf, sizeof (buf));
1748 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1749 c->t->xmit (c->t, &buf, sizeof (buf));
1757 vt102_reset (VT102 * v)
1759 VT102_parser *p = &v->parser;
1763 v->current_line = v->pos;
1764 v->pending_wrap = 0;
1766 v->screen_start.x = 0;
1767 v->screen_start.y = 0;
1768 v->screen_end.x = VT102_COLS - 1;
1769 v->screen_end.y = VT102_ROWS - 1;
1771 vt102_cursor_home (v);
1772 vt102_status_line (v, "");
1774 vt102_reset_tabs (v);
1775 v->current_line = v->pos;
1777 vt102_parser_reset (p);
1778 vt102_reset_state (v);
1780 vt102_save_state (v);
1782 v->last_reg_char = ' ';
1790 v = (VT102 *) malloc (sizeof (VT102));
1801 vt102_set_ansi (VT102 * v, int ansi)
1803 v->xn_glitch = ansi ? 0 : 1;
1807 vt102_free (VT102 * v)