4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.39 2008/02/26 16:54:06 james
14 * *** empty log message ***
16 * Revision 1.38 2008/02/26 16:53:24 james
17 * *** empty log message ***
19 * Revision 1.37 2008/02/24 12:22:53 james
20 * *** empty log message ***
22 * Revision 1.36 2008/02/24 12:22:42 james
23 * *** empty log message ***
25 * Revision 1.35 2008/02/24 00:42:53 james
26 * *** empty log message ***
28 * Revision 1.34 2008/02/23 11:48:37 james
29 * *** empty log message ***
31 * Revision 1.33 2008/02/22 23:39:27 james
32 * *** empty log message ***
34 * Revision 1.32 2008/02/22 19:12:05 james
35 * *** empty log message ***
37 * Revision 1.31 2008/02/22 17:07:00 james
38 * *** empty log message ***
40 * Revision 1.30 2008/02/22 14:51:54 james
41 * *** empty log message ***
43 * Revision 1.29 2008/02/15 03:32:07 james
44 * *** empty log message ***
46 * Revision 1.28 2008/02/14 10:34:30 james
47 * *** empty log message ***
49 * Revision 1.27 2008/02/14 02:46:45 james
50 * *** empty log message ***
52 * Revision 1.26 2008/02/14 01:55:57 james
53 * *** empty log message ***
55 * Revision 1.25 2008/02/13 16:57:29 james
56 * *** empty log message ***
58 * Revision 1.24 2008/02/13 09:12:21 james
59 * *** empty log message ***
61 * Revision 1.23 2008/02/07 13:26:35 james
62 * *** empty log message ***
64 * Revision 1.22 2008/02/07 13:22:51 james
65 * *** empty log message ***
67 * Revision 1.21 2008/02/07 12:21:16 james
68 * *** empty log message ***
70 * Revision 1.20 2008/02/07 12:16:04 james
71 * *** empty log message ***
73 * Revision 1.19 2008/02/07 11:27:02 james
74 * *** empty log message ***
76 * Revision 1.18 2008/02/07 01:59:25 james
77 * *** empty log message ***
79 * Revision 1.17 2008/02/07 01:58:28 james
80 * *** empty log message ***
82 * Revision 1.16 2008/02/07 01:57:46 james
83 * *** empty log message ***
85 * Revision 1.15 2008/02/07 00:43:27 james
86 * *** empty log message ***
88 * Revision 1.14 2008/02/07 00:40:23 james
89 * *** empty log message ***
91 * Revision 1.13 2008/02/07 00:39:59 james
92 * *** empty log message ***
94 * Revision 1.12 2008/02/07 00:39:13 james
95 * *** empty log message ***
97 * Revision 1.11 2008/02/06 20:26:58 james
98 * *** empty log message ***
100 * Revision 1.10 2008/02/06 17:53:28 james
101 * *** empty log message ***
103 * Revision 1.9 2008/02/06 15:53:22 james
104 * *** empty log message ***
106 * Revision 1.8 2008/02/06 11:49:47 james
107 * *** empty log message ***
109 * Revision 1.7 2008/02/06 11:30:37 james
110 * *** empty log message ***
112 * Revision 1.6 2008/02/05 01:11:46 james
113 * *** empty log message ***
115 * Revision 1.5 2008/02/04 20:23:55 james
116 * *** empty log message ***
118 * Revision 1.4 2008/02/04 05:45:55 james
121 * Revision 1.3 2008/02/04 02:05:06 james
122 * *** empty log message ***
124 * Revision 1.2 2008/02/04 01:32:39 james
125 * *** empty log message ***
127 * Revision 1.1 2008/02/03 23:36:41 james
128 * *** empty log message ***
139 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
141 vt100|vt100-am|dec vt100 (w/advanced video):\
143 :co#80:it#8:li#24:vt#3:\
144 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
146 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
147 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
148 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
149 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
150 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
151 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
152 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
153 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
154 :us=\E[4m:tc=vt100+fnkeys:
156 vt100+fnkeys|dec vt100 numeric keypad:\
157 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
160 vt100+pfkeys|dec vt100 numeric keypad:\
161 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
163 vt100+keypad|dec vt100 numeric keypad no fkeys:\
164 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
169 so the parser needs to be able to at least do
231 ansi|ansi/pc-term compatible with color:\
232 :u6=\E[%i%d;%dR:u7=\E[6n:u9=\E[c:tc=ecma+color:\
233 :tc=klone+sgr:tc=ansi-m:
234 ansi-m|ansi-mono|ANSI X3.64-1979 terminal with ANSI.SYS compatible attributes:\
236 :AL=\E[%dL:DC=\E[%dP:DL=\E[%dM:DO=\E[%dB:IC=\E[%d@:\
237 :LE=\E[%dD:RI=\E[%dC:SF=\E[%dS:SR=\E[%dT:UP=\E[%dA:\
238 :cb=\E[1K:ch=\E[%i%dG:ct=\E[2g:cv=\E[%i%dd:ec=\E[%dX:ei=:\
239 :im=:kB=\E[Z:kI=\E[L:kb=^H:kd=\E[B:kl=\E[D:kr=\E[C:ku=\E[A:\
240 :nw=\r\E[S:pf=\E[4i:po=\E[5i:s0=\E(B:s1=\E)B:s2=\E*B:\
241 :s3=\E+B:ta=\E[I:tc=pcansi-m:
242 pcansi-m|pcansi-mono|ibm-pc terminal programs claiming to be ansi (mono mode):\
245 :al=\E[L:bl=^G:bt=\E[Z:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
246 :cm=\E[%i%d;%dH:cr=^M:ct=\E[2g:dc=\E[P:dl=\E[M:do=\E[B:\
247 :ho=\E[H:kb=^H:kd=\E[B:kh=\E[H:kl=\E[D:kr=\E[C:ku=\E[A:\
248 :le=\E[D:nd=\E[C:sf=^J:st=\EH:ta=^I:up=\E[A:\
250 klone+sgr-dumb|attribute control for ansi.sys displays (no ESC [ 11 m):\
251 :as=\E[12m:mb=\E[5m:md=\E[1m:me=\E[0;10m:mk=\E[8m:\
252 :mr=\E[7m:se=\E[m:so=\E[7m:ue=\E[m:us=\E[4m:tc=klone+acs:
253 klone+acs|alternate character set for ansi.sys displays:\
254 :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:\
255 :ae=\E[10m:as=\E[11m:
256 ecma+color|color control for ECMA-48-compatible terminals:\
258 :AB=\E[4%dm:AF=\E[3%dm:op=\E[39;49m:
261 Ignoreing ones in the VT102 spec
291 //static char terminal_reply[]={033,0133,077,066,0143};
292 static char terminal_reply[] = { 'v', 't', '1', '0', '2' };
294 #define TABLE_LENGTH 128
296 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
297 int vt102_cmd_length[TABLE_LENGTH] = {
307 /* method for determining end if if's not fixed length, -ve numbers from #defines, +ve are literals */
309 int vt102_cmd_termination[TABLE_LENGTH] = {
331 if ((c >= 'a') && (c <= 'z'))
333 if ((c >= 'A') && (c <= 'Z'))
342 /*Stay or enter command acquision and exit after n more chars*/
344 redispatch_with_more (VT102_parser * p, int n)
349 in_margins (VT102 * v, CRT_Pos p)
351 if (v->pos.x < v->top_margin.x)
353 if (v->pos.y < v->top_margin.y)
356 if (v->pos.x > v->bottom_margin.x)
358 if (v->pos.y > v->bottom_margin.y)
365 vt102_log_line (Context * c, int line)
367 CRT_Pos e = { VT102_COLS - 1, line };
368 CRT_Pos p = { 0, line };
369 char logbuf[VT102_COLS + 1];
375 for (; e.x > 0; --e.x)
377 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
381 for (; p.x <= e.x; ++p.x)
383 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
392 c->l->log (c->l, logbuf);
395 /*Called for every upward scroll with same args*/
397 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
399 /*Only log if it scrolls off the top*/
404 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
408 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
423 vt102_cursor_normalize (VT102 * v)
425 CRT_Pos *top, *bottom;
427 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
429 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
433 vt102_clip_cursor (v, v->screen_start, v->screen_end);
439 vt102_cursor_carriage_return (VT102 * v)
441 /*FISH*/ v->pos.x = v->top_margin.x;
446 vt102_cursor_advance_line (Context * c)
449 int couldscroll = in_margins (v, v->pos);
451 /*have wraped off end of last line in scrolling region */
452 /* (|| not necessary, but shuts compiler up */
453 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
456 vt102_log_line (c, v->pos.y);
458 vt102_history (c, v->top_margin, v->bottom_margin);
460 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
464 if (v->pos.y != v->screen_end.y)
471 vt102_cursor_retreat_line (Context * c)
474 int couldscroll = in_margins (v, v->pos);
476 /*have wraped off end of last line in scrolling region */
477 /* (|| not necessary, but shuts compiler up */
478 if (((v->pos.y == v->top_margin.y) || (v->pos.y == v->screen_start.y)) &&
481 vt102_log_line (c, v->pos.y);
483 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1, v->color);
487 if (v->pos.y != v->screen_start.y)
494 vt102_do_pending_wrap (Context * c)
497 int couldscroll = in_margins (v, v->pos);
498 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
500 if (!v->pending_wrap)
504 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
505 v->pos.y, autowrap, couldscroll);
508 /*End of line but no autowrap, nothing to do*/
512 /*End of screen and not allowed to scroll, nothing to do*/
513 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
518 v->pos.x = v->top_margin.x;
525 vt102_cursor_advance_line (c);
529 vt102_cursor_advance (Context * c)
533 if (v->pos.x < v->bottom_margin.x)
541 if (!c->v->xn_glitch)
542 vt102_do_pending_wrap (c);
548 vt102_cursor_retreat (VT102 * v)
550 if (v->pos.x != v->top_margin.x)
557 vt102_reset_tabs (VT102 * v)
561 memset (v->tabs, 0, sizeof (v->tabs));
563 for (i = 0; i < VT102_COLS; i += 8)
569 vt102_cursor_advance_tab (VT102 * v)
571 if (v->pos.x == v->bottom_margin.x)
573 while (v->pos.x < v->bottom_margin.x)
576 if (v->tabs[v->pos.x])
583 vt102_cursor_retreat_tab (VT102 * v)
585 if (v->pos.x == v->top_margin.x)
587 while (v->pos.x > v->top_margin.x)
590 if (v->tabs[v->pos.x])
596 vt102_cursor_home (VT102 * v)
598 v->pos = v->top_margin;
599 vt102_cursor_normalize (v);
604 vt102_cursor_absolute (VT102 * v, int x, int y)
606 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
608 v->pos.x = x + v->top_margin.x;
609 v->pos.y = y + v->top_margin.x;
616 vt102_cursor_normalize (v);
620 vt102_cursor_relative (VT102 * v, int x, int y)
624 vt102_cursor_normalize (v);
631 vt102_delete_from_line (VT102 * v, CRT_Pos p)
633 int n = v->bottom_margin.x - p.x;
641 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
642 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
645 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
646 /*But not attr due to vt102 bug*/
650 vt102_insert_into_line (VT102 * v, CRT_Pos p)
652 int n = v->bottom_margin.x - p.x;
660 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
661 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
664 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
665 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
666 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
672 vt102_change_mode (VT102 * v, int private, char *ns, int set)
688 if (m >= VT102_NMODES)
693 v->private_modes[m] = set;
696 case VT102_PRIVATE_MODE_CURSOR_MODE:
697 if (v->application_keypad_mode)
698 v->private_modes[m] = 0;
700 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
701 v->private_modes[m], set);
704 case VT102_PRIVATE_MODE_ORIGIN_MODE:
705 vt102_cursor_home (v);
714 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
719 vt102_parse_mode_string (VT102 * v, char *buf, int len)
722 char last = buf[len - 1];
726 memset (num, 0, sizeof (num));
727 o = sizeof (num) - 1;
745 vt102_change_mode (v, private, &num[o], last == 'h');
746 memset (num, 0, sizeof (num));
747 o = sizeof (num) - 1;
762 vt102_change_mode (v, private, &num[o], last == 'h');
768 vt102_change_attr (VT102 * v, char *na)
785 v->attr = CRT_ATTR_NORMAL;
786 v->color = CRT_COLOR_NORMAL;
789 v->attr |= CRT_ATTR_BOLD;
793 v->attr &= ~CRT_ATTR_BOLD;
796 v->attr |= CRT_ATTR_UNDERLINE;
799 v->attr &= ~CRT_ATTR_UNDERLINE;
802 v->attr |= CRT_ATTR_BLINK;
805 v->attr &= ~CRT_ATTR_BLINK;
808 v->attr |= CRT_ATTR_REVERSE;
811 v->attr &= ~CRT_ATTR_REVERSE;
821 v->color &= ~CRT_COLOR_FG_MASK;
822 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
832 v->color &= ~CRT_COLOR_FG_MASK;
835 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
840 v->color &= ~CRT_COLOR_FG_MASK;
842 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
852 v->color &= ~CRT_COLOR_BG_MASK;
853 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
863 v->color &= ~CRT_COLOR_BG_MASK;
866 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
871 v->color &= ~CRT_COLOR_BG_MASK;
873 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
879 fprintf (stderr, "unhandled SGR %d\n", a);
887 vt102_parse_attr_string (VT102 * v, char *buf, int len)
890 char last = buf[len - 1];
894 memset (num, 0, sizeof (num));
895 o = sizeof (num) - 1;
906 vt102_change_attr (v, &num[o]);
907 memset (num, 0, sizeof (num));
908 o = sizeof (num) - 1;
922 vt102_change_attr (v, &num[o]);
926 vt102_save_state (VT102 * v)
928 v->saved.pos = v->pos;
929 v->saved.attr = v->attr;
930 v->saved.color = v->color;
931 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
935 vt102_restore_state (VT102 * v)
937 v->pos = v->saved.pos;
938 v->attr = v->saved.attr;
939 v->color = v->saved.color;
940 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
941 vt102_cursor_normalize (v);
946 vt102_regular_char (Context * c, VT102 * v, int ch)
949 vt102_do_pending_wrap (c);
951 if (v->modes[VT102_MODE_INSERT])
952 vt102_insert_into_line (v, v->pos);
954 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
955 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
956 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
957 vt102_cursor_advance (c);
959 v->last_reg_char = ch;
963 vt102_scs (Context * c, int g, int s)
965 /*Ignoring charsets*/
969 vt102_parse_esc (Context * c)
972 VT102_parser *p = &v->parser;
974 /*If you edit this switch don't forget to edit the length and termination tables*/
975 switch (p->cmd_buf[0])
978 vt102_cursor_relative (v, 0, -1);
981 vt102_cursor_relative (v, 0, 1);
984 vt102_cursor_relative (v, 1, 0);
987 vt102_cursor_relative (v, -1, 0);
990 if (v->pos.y == v->bottom_margin.y)
992 vt102_log_line (c, v->pos.y);
993 vt102_history (c, v->top_margin, v->bottom_margin);
994 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
999 vt102_cursor_relative (v, 0, 1);
1003 v->tabs[v->pos.x]++;
1006 vt102_cursor_retreat_line (c);
1009 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1013 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1014 crt_erase (&v->crt, v->pos, le, 1, v->color);
1018 if (v->pos.y == v->top_margin.y)
1020 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1025 vt102_cursor_relative (v, 0, -1);
1030 vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
1035 c->t->xmit (c->t, terminal_reply, sizeof (terminal_reply));
1039 switch (p->cmd_buf[1])
1045 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1047 for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
1048 v->crt.screen[i].chr = 'E';
1053 "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>",
1054 __FILE__, __LINE__, p->cmd_buf[1],
1055 safe_ch (p->cmd_buf[1]));
1060 /*Set ansi mode - ignored */
1064 vt102_save_state (v);
1067 vt102_restore_state (v);
1070 v->application_keypad_mode = 1;
1073 v->application_keypad_mode = 0;
1075 case ']': /*Set various titles */
1079 vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
1081 case '(': /*Charsets */
1086 vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
1089 log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
1090 __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
1095 void vt102_parse_csi (Context * c, char *buf, int len)
1099 char *arg = buf + 1;
1106 last = buf[len - 1];
1127 vt102_insert_into_line (v, v->pos);
1130 vt102_cursor_relative (v, 0, -narg);
1134 vt102_cursor_relative (v, 0, narg);
1138 vt102_cursor_relative (v, narg, 0);
1141 vt102_cursor_relative (v, -narg, 0);
1144 vt102_cursor_relative (v, 0, narg);
1145 vt102_cursor_carriage_return (v);
1148 vt102_cursor_relative (v, 0, -narg);
1149 vt102_cursor_carriage_return (v);
1152 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1161 ptr = index (arg, ';');
1163 x = atoi (ptr + 1) - 1;
1167 vt102_cursor_absolute (v, x, y);
1172 vt102_cursor_advance_tab (c->v);
1176 narg = 0; /*Different default */
1180 crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
1183 crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
1186 crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
1193 CRT_Pos ls = { 0, v->pos.y };
1194 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
1196 narg = 0; /*Different default */
1201 crt_erase (&v->crt, v->pos, le, 1, v->color);
1204 crt_erase (&v->crt, ls, v->pos, 1, v->color);
1207 crt_erase (&v->crt, ls, le, 1, v->color);
1214 if ((v->pos.y >= v->top_margin.y)
1215 && (v->pos.y <= v->bottom_margin.y))
1218 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1,
1224 if ((v->pos.y >= v->top_margin.y)
1225 && (v->pos.y <= v->bottom_margin.y))
1229 vt102_history (c, v->pos, v->bottom_margin);
1230 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1,
1237 vt102_delete_from_line (v, v->pos);
1240 //FIXME: cursor position report - does anything use that?
1245 vt102_history (c, v->top_margin, v->bottom_margin);
1246 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
1252 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
1257 CRT_Pos end = v->pos;
1262 if (end.x > v->bottom_margin.x)
1263 end.x = v->bottom_margin.x;
1265 crt_erase (&v->crt, v->pos, end, 1, v->color);
1270 vt102_cursor_retreat_tab (c->v);
1273 vt102_cursor_absolute (v, narg - 1, v->pos.y);
1277 vt102_regular_char (c, v, v->last_reg_char);
1283 vt102_cursor_absolute (v, v->pos.x, narg - 1);
1287 narg = 0; /*Different default */
1292 v->tabs[v->pos.x] = 0;
1294 case 2: //FIXME: - LA120 says current line only WTF?
1296 memset (v->tabs, 0, sizeof (v->tabs));
1301 case 'i': //4,5 Turn printer on and off WTF?
1306 vt102_parse_mode_string (v, &buf[1], len - 1);
1311 vt102_parse_attr_string (v, &buf[1], len - 1);
1313 case 'n': // 6 request cursor position
1317 v->top_margin = v->screen_start;
1318 v->bottom_margin = v->screen_end;
1320 if ((len > 2) && (ptr = index (arg, ';')))
1323 v->top_margin.y = narg - 1;
1324 v->bottom_margin.y = atoi (ptr) - 1;
1327 if (v->top_margin.y < v->screen_start.y)
1328 v->top_margin.y = v->screen_start.y;
1329 if (v->top_margin.y > v->screen_end.y)
1330 v->top_margin.y = v->screen_end.y;
1331 if (v->bottom_margin.y < v->screen_start.y)
1332 v->bottom_margin.y = v->screen_start.y;
1333 if (v->bottom_margin.y > v->screen_end.y)
1334 v->bottom_margin.y = v->screen_end.y;
1336 vt102_cursor_home (v);
1339 v->saved.pos = v->pos;
1342 v->pos = v->saved.pos;
1343 vt102_cursor_normalize (v);
1344 v->pending_wrap = 0;
1348 log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
1355 log_f (c->l, "<%s:%d unhandled sequence: \\033%s>", __FILE__,
1364 void vt102_status_line (VT102 * v, char *str)
1367 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1371 ca->attr = CRT_ATTR_REVERSE;
1372 ca->color = CRT_COLOR_NORMAL;
1382 void vt102_parser_reset (VT102_parser * p)
1385 p->cmd_more_bytes = 0;
1386 p->cmd_termination = 0;
1391 void vt102_reset_state (VT102 * v)
1393 vt102_parser_reset (&v->parser);
1395 v->attr = CRT_ATTR_NORMAL;
1396 v->color = CRT_COLOR_NORMAL;
1398 v->application_keypad_mode = 0;
1400 v->top_margin = v->screen_start;
1401 v->bottom_margin = v->screen_end;
1403 memset (v->modes, 0, VT102_NMODES);
1404 memset (v->private_modes, 0, VT102_NMODES);
1406 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1407 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1408 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1409 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1411 vt102_reset_tabs (v);
1414 static void pre_parse_cmd (int ch, VT102_parser * p)
1416 if (ch > TABLE_LENGTH)
1421 p->cmd_more_bytes = 0;
1424 p->cmd_termination = vt102_cmd_termination[ch];
1425 if (p->cmd_termination)
1431 p->cmd_more_bytes = vt102_cmd_length[ch];
1433 if (p->cmd_more_bytes)
1440 void vt102_parse_char (Context * c, int ch)
1443 VT102_parser *p = &v->parser;
1446 log_f (c->l, "char %3d %c ie=%d ic=%d cmb=%d ct=%d %2d %2d %d", ch,
1447 safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
1448 p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
1451 /* Turn anything non-ascii into '?' */
1453 if ((ch != SYM_CHAR_RESET) && (ch != 0xb9) && (ch > 127))
1461 if (p->dca_ptr == 2)
1466 if (ch == SYM_CHAR_RESET)
1468 vt102_reset_state (v);
1472 p->cmd_buf[p->cmd_ptr++] = ch;
1473 if (p->cmd_more_bytes)
1474 p->cmd_more_bytes--;
1477 if (p->cmd_more_bytes == 1)
1480 switch (p->cmd_termination)
1485 if (p->cmd_termination == ch)
1496 vt102_parse_esc (c);
1497 p->cmd_more_bytes = 0;
1500 else if (p->in_escape)
1504 p->cmd_buf[p->cmd_ptr++] = ch;
1507 pre_parse_cmd (ch, p);
1510 vt102_parse_esc (c);
1512 else if (ch == 0x9b) /*One byte CSI */
1515 p->cmd_buf[p->cmd_ptr++] = '[';
1517 pre_parse_cmd (ch, p);
1533 c->t->xmit (c->t, "vt102", 5);
1540 vt102_cursor_retreat (c->v);
1543 vt102_cursor_advance_tab (c->v);
1548 vt102_cursor_advance_line (c);
1549 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1552 vt102_cursor_carriage_return (v);
1556 /*Ignoring charsets */
1560 /*Ignoring charsets */
1583 /*regular character */ default:
1584 vt102_regular_char (c, v, ch);
1588 v->crt.pos = v->pos;
1589 v->crt.hide_cursor =
1590 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1592 if (v->current_line.y != v->pos.y)
1594 vt102_log_line (c, v->current_line.y);
1595 v->current_line = v->pos;
1599 cmd_show_status (c->d, c);
1602 void vt102_send (Context * c, uint8_t key)
1610 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1612 if ((key > 31) && (key < 127))
1614 c->t->xmit (c->t, &key, 1);
1633 c->t->xmit (c->t, &key, 1);
1636 c->t->xmit (c->t, &key, 1);
1637 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1640 c->t->xmit (c->t, &ch, 1);
1656 c->t->xmit (c->t, &key, 1);
1664 c->t->xmit (c->t, &key, 1);
1675 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1677 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1678 c->t->xmit (c->t, &buf, sizeof (buf));
1682 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1683 c->t->xmit (c->t, &buf, sizeof (buf));
1701 if (c->v->application_keypad_mode)
1703 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1704 c->t->xmit (c->t, &buf, sizeof (buf));
1708 static char kpoff[KEY_NUM] = {
1727 c->t->xmit (c->t, &kpoff[key], 1);
1731 if (c->v->application_keypad_mode)
1733 uint8_t buf[] = { 033, 'O', 'M' };
1734 c->t->xmit (c->t, &buf, sizeof (buf));
1739 c->t->xmit (c->t, &ch, 1);
1740 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1743 c->t->xmit (c->t, &ch, 1);
1752 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1753 c->t->xmit (c->t, &buf, sizeof (buf));
1761 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1762 c->t->xmit (c->t, &buf, sizeof (buf));
1769 void vt102_reset (VT102 * v)
1771 VT102_parser *p = &v->parser;
1775 v->current_line = v->pos;
1776 v->pending_wrap = 0;
1778 v->screen_start.x = 0;
1779 v->screen_start.y = 0;
1780 v->screen_end.x = VT102_COLS - 1;
1781 v->screen_end.y = VT102_ROWS - 1;
1783 vt102_cursor_home (v);
1784 vt102_status_line (v, "");
1786 vt102_reset_tabs (v);
1787 v->current_line = v->pos;
1789 vt102_parser_reset (p);
1790 vt102_reset_state (v);
1792 vt102_save_state (v);
1794 v->last_reg_char = ' ';
1797 VT102 *vt102_new (void)
1801 v = (VT102 *) malloc (sizeof (VT102));
1811 void vt102_set_ansi (VT102 * v, int ansi)
1813 v->xn_glitch = ansi ? 0 : 1;
1816 void vt102_free (VT102 * v)