4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.28 2008/02/14 10:34:30 james
14 * *** empty log message ***
16 * Revision 1.27 2008/02/14 02:46:45 james
17 * *** empty log message ***
19 * Revision 1.26 2008/02/14 01:55:57 james
20 * *** empty log message ***
22 * Revision 1.25 2008/02/13 16:57:29 james
23 * *** empty log message ***
25 * Revision 1.24 2008/02/13 09:12:21 james
26 * *** empty log message ***
28 * Revision 1.23 2008/02/07 13:26:35 james
29 * *** empty log message ***
31 * Revision 1.22 2008/02/07 13:22:51 james
32 * *** empty log message ***
34 * Revision 1.21 2008/02/07 12:21:16 james
35 * *** empty log message ***
37 * Revision 1.20 2008/02/07 12:16:04 james
38 * *** empty log message ***
40 * Revision 1.19 2008/02/07 11:27:02 james
41 * *** empty log message ***
43 * Revision 1.18 2008/02/07 01:59:25 james
44 * *** empty log message ***
46 * Revision 1.17 2008/02/07 01:58:28 james
47 * *** empty log message ***
49 * Revision 1.16 2008/02/07 01:57:46 james
50 * *** empty log message ***
52 * Revision 1.15 2008/02/07 00:43:27 james
53 * *** empty log message ***
55 * Revision 1.14 2008/02/07 00:40:23 james
56 * *** empty log message ***
58 * Revision 1.13 2008/02/07 00:39:59 james
59 * *** empty log message ***
61 * Revision 1.12 2008/02/07 00:39:13 james
62 * *** empty log message ***
64 * Revision 1.11 2008/02/06 20:26:58 james
65 * *** empty log message ***
67 * Revision 1.10 2008/02/06 17:53:28 james
68 * *** empty log message ***
70 * Revision 1.9 2008/02/06 15:53:22 james
71 * *** empty log message ***
73 * Revision 1.8 2008/02/06 11:49:47 james
74 * *** empty log message ***
76 * Revision 1.7 2008/02/06 11:30:37 james
77 * *** empty log message ***
79 * Revision 1.6 2008/02/05 01:11:46 james
80 * *** empty log message ***
82 * Revision 1.5 2008/02/04 20:23:55 james
83 * *** empty log message ***
85 * Revision 1.4 2008/02/04 05:45:55 james
88 * Revision 1.3 2008/02/04 02:05:06 james
89 * *** empty log message ***
91 * Revision 1.2 2008/02/04 01:32:39 james
92 * *** empty log message ***
94 * Revision 1.1 2008/02/03 23:36:41 james
95 * *** empty log message ***
104 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
106 vt100|vt100-am|dec vt100 (w/advanced video):\
108 :co#80:it#8:li#24:vt#3:\
109 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
111 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
112 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
113 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
114 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
115 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
116 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
117 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
118 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
119 :us=\E[4m:tc=vt100+fnkeys:
121 vt100+fnkeys|dec vt100 numeric keypad:\
122 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
125 vt100+pfkeys|dec vt100 numeric keypad:\
126 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
128 vt100+keypad|dec vt100 numeric keypad no fkeys:\
129 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
134 so the parser needs to be able to at least do
202 if ((c >= 'a') && (c <= 'z'))
204 if ((c >= 'A') && (c <= 'Z'))
233 in_margins (VT102 * v, CRT_Pos p)
235 if (v->pos.x < v->top_margin.x)
237 if (v->pos.y < v->top_margin.y)
240 if (v->pos.x > v->bottom_margin.x)
242 if (v->pos.y > v->bottom_margin.y)
249 vt102_log_line (Context * c, int line)
251 CRT_Pos e = { VT102_COLS - 1, line };
252 CRT_Pos p = { 0, line };
253 char logbuf[VT102_COLS + 1];
259 for (; e.x > 0; --e.x)
261 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
265 for (; p.x <= e.x; ++p.x)
267 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
276 c->l->log (c->l, logbuf);
279 /*Called for every upward scroll with same args*/
281 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
283 /*Only log if it scrolls off the top*/
288 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
292 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
307 vt102_cursor_normalize (VT102 * v)
309 CRT_Pos *top, *bottom;
311 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
313 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
317 vt102_clip_cursor (v, v->screen_start, v->screen_end);
323 vt102_cursor_carriage_return (VT102 * v)
325 /*FISH*/ v->pos.x = v->top_margin.x;
330 vt102_cursor_advance_line (Context * c)
333 int couldscroll = in_margins (v, v->pos);
335 /*have wraped off end of last line in scrolling region */
336 /* (|| not necessary, but shuts compiler up */
337 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
340 vt102_log_line (c, v->pos.y);
342 vt102_history (c, v->top_margin, v->bottom_margin);
344 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
354 vt102_cursor_advance (Context * c)
358 if (v->pos.x < v->bottom_margin.x)
370 vt102_do_pending_wrap (Context * c)
373 int couldscroll = in_margins (v, v->pos);
374 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
376 if (!v->pending_wrap)
380 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
381 v->pos.y, autowrap, couldscroll);
384 /*End of line but no autowrap, nothing to do*/
388 /*End of screen and not allowed to scroll, nothing to do*/
389 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
394 v->pos.x = v->top_margin.x;
401 vt102_cursor_advance_line (c);
406 vt102_cursor_retard (VT102 * v)
408 if (v->pos.x != v->top_margin.x)
415 vt102_reset_tabs (VT102 * v)
419 memset (v->tabs, 0, sizeof (v->tabs));
421 for (i = 0; i < VT102_COLS; i += 8)
427 vt102_cursor_advance_tab (VT102 * v)
429 if (v->pos.x == v->bottom_margin.x)
431 while (v->pos.x < v->bottom_margin.x)
434 if (v->tabs[v->pos.x])
440 vt102_cursor_home (VT102 * v)
442 v->pos = v->top_margin;
443 vt102_cursor_normalize (v);
448 vt102_cursor_absolute (VT102 * v, int x, int y)
450 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
452 v->pos.x = x + v->top_margin.x;
453 v->pos.y = y + v->top_margin.x;
460 vt102_cursor_normalize (v);
464 vt102_cursor_relative (VT102 * v, int x, int y)
468 vt102_cursor_normalize (v);
475 vt102_delete_from_line (VT102 * v, CRT_Pos p)
477 int n = v->bottom_margin.x - p.x;
485 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
486 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
489 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
490 /*But not attr due to vt102 bug*/
494 vt102_insert_into_line (VT102 * v, CRT_Pos p)
496 int n = v->bottom_margin.x - p.x;
504 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
505 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
508 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
509 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
510 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
516 vt102_change_mode (VT102 * v, int private, char *ns, int set)
532 if (m >= VT102_NMODES)
537 v->private_modes[m] = set;
540 case VT102_PRIVATE_MODE_CURSOR_MODE:
541 if (v->application_keypad_mode)
542 v->private_modes[m] = 0;
544 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
545 v->private_modes[m], set);
548 case VT102_PRIVATE_MODE_ORIGIN_MODE:
549 vt102_cursor_home (v);
558 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
563 vt102_parse_mode_string (VT102 * v, char *buf, int len)
566 char last = buf[len - 1];
570 memset (num, 0, sizeof (num));
571 o = sizeof (num) - 1;
589 vt102_change_mode (v, private, &num[o], last == 'h');
590 memset (num, 0, sizeof (num));
591 o = sizeof (num) - 1;
606 vt102_change_mode (v, private, &num[o], last == 'h');
612 vt102_change_attr (VT102 * v, char *na)
629 v->attr = CRT_ATTR_NORMAL;
630 v->color = CRT_COLOR_NORMAL;
633 v->attr |= CRT_ATTR_BOLD;
637 v->attr &= ~CRT_ATTR_BOLD;
640 v->attr |= CRT_ATTR_UNDERLINE;
643 v->attr &= ~CRT_ATTR_UNDERLINE;
646 v->attr |= CRT_ATTR_BLINK;
649 v->attr &= ~CRT_ATTR_BLINK;
652 v->attr |= CRT_ATTR_REVERSE;
655 v->attr &= ~CRT_ATTR_REVERSE;
665 v->color &= ~CRT_COLOR_FG_MASK;
666 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
676 v->color &= ~CRT_COLOR_FG_MASK;
679 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
684 v->color &= ~CRT_COLOR_FG_MASK;
686 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
696 v->color &= ~CRT_COLOR_BG_MASK;
697 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
707 v->color &= ~CRT_COLOR_BG_MASK;
710 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
715 v->color &= ~CRT_COLOR_BG_MASK;
717 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
723 fprintf (stderr, "unhandled SGR %d\n", a);
731 vt102_parse_attr_string (VT102 * v, char *buf, int len)
734 char last = buf[len - 1];
738 memset (num, 0, sizeof (num));
739 o = sizeof (num) - 1;
750 vt102_change_attr (v, &num[o]);
751 memset (num, 0, sizeof (num));
752 o = sizeof (num) - 1;
766 vt102_change_attr (v, &num[o]);
770 vt102_save_state (VT102 * v)
772 v->saved.pos = v->pos;
773 v->saved.attr = v->attr;
774 v->saved.color = v->color;
775 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
779 vt102_restore_state (VT102 * v)
781 v->pos = v->saved.pos;
782 v->attr = v->saved.attr;
783 v->color = v->saved.color;
784 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
785 vt102_cursor_normalize (v);
790 vt102_scs (Context * c, int g, int s)
792 /*Ignoring charsets*/
796 vt102_parse_esc (Context * c, int ch)
802 if (v->pos.y == v->bottom_margin.y)
804 vt102_log_line (c, v->pos.y);
805 vt102_history (c, v->top_margin, v->bottom_margin);
806 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
810 vt102_cursor_relative (v, 0, 1);
817 if (v->pos.y == v->top_margin.y)
819 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
823 vt102_cursor_relative (v, 0, -1);
827 vt102_save_state (v);
830 vt102_restore_state (v);
833 v->application_keypad_mode = 1;
836 v->application_keypad_mode = 0;
840 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
841 (ch < 32) ? '?' : ch);
847 vt102_parse_csi (Context * c, char *buf, int len)
878 vt102_cursor_relative (v, 0, -narg);
881 vt102_cursor_relative (v, 0, narg);
884 vt102_cursor_relative (v, narg, 0);
887 vt102_cursor_relative (v, -narg, 0);
890 vt102_cursor_relative (v, 0, narg);
891 vt102_cursor_carriage_return (v);
894 vt102_cursor_relative (v, 0, -narg);
895 vt102_cursor_carriage_return (v);
898 vt102_cursor_absolute (v, narg - 1, v->pos.y);
907 ptr = index (arg, ';');
909 x = atoi (ptr + 1) - 1;
913 vt102_cursor_absolute (v, x, y);
920 crt_erase (&v->crt, v->pos, v->screen_end, 1);
923 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
929 CRT_Pos ls = { 0, v->pos.y };
930 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
932 narg = 0; /*Different default */
937 crt_erase (&v->crt, v->pos, le, 1);
940 crt_erase (&v->crt, ls, v->pos, 1);
943 crt_erase (&v->crt, ls, le, 1);
951 vt102_delete_from_line (v, v->pos);
954 if ((v->pos.y >= v->top_margin.y)
955 && (v->pos.y <= v->bottom_margin.y))
958 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
963 if ((v->pos.y >= v->top_margin.y)
964 && (v->pos.y <= v->bottom_margin.y))
968 vt102_history (c, v->pos, v->bottom_margin);
969 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1);
976 narg = 0; /*Different default */
981 v->tabs[v->pos.x] = 0;
984 memset (v->tabs, 0, sizeof (v->tabs));
991 vt102_parse_mode_string (v, &buf[1], len - 1);
995 vt102_parse_attr_string (v, &buf[1], len - 1);
998 v->top_margin = v->screen_start;
999 v->bottom_margin = v->screen_end;
1001 if ((len > 2) && (ptr = index (arg, ';')))
1004 v->top_margin.y = narg - 1;
1005 v->bottom_margin.y = atoi (ptr) - 1;
1008 if (v->top_margin.y < v->screen_start.y)
1009 v->top_margin.y = v->screen_start.y;
1010 if (v->top_margin.y > v->screen_end.y)
1011 v->top_margin.y = v->screen_end.y;
1012 if (v->bottom_margin.y < v->screen_start.y)
1013 v->bottom_margin.y = v->screen_start.y;
1014 if (v->bottom_margin.y > v->screen_end.y)
1015 v->bottom_margin.y = v->screen_end.y;
1017 vt102_cursor_home (v);
1020 v->saved.pos = v->pos;
1023 v->pos = v->saved.pos;
1024 vt102_cursor_normalize (v);
1025 v->pending_wrap = 0;
1030 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1037 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1047 vt102_status_line (VT102 * v, char *str)
1050 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1054 ca->attr = CRT_ATTR_REVERSE;
1055 ca->color = CRT_COLOR_NORMAL;
1066 vt102_parse_char (Context * c, int ch)
1069 VT102_parser *p = &v->parser;
1072 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1073 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1077 p->csi_buf[p->csi_ptr++] = ch;
1078 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1080 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1084 else if (p->in_escape)
1086 if (csi_starter (ch))
1089 p->csi_buf[p->csi_ptr++] = ch;
1093 else if (scs_starter (ch))
1101 vt102_parse_esc (c, ch);
1106 vt102_scs (c, p->in_scs, ch);
1121 c->t->xmit (c->t, "vt102", 5);
1127 vt102_cursor_retard (c->v);
1130 vt102_cursor_advance_tab (c->v);
1135 vt102_cursor_advance_line (c);
1136 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1139 vt102_cursor_carriage_return (v);
1143 /*Ignoring charsets */
1147 /*Ignoring charsets */
1170 /*regular character */ default:
1171 vt102_do_pending_wrap (c);
1173 if (v->modes[VT102_MODE_INSERT])
1174 vt102_insert_into_line (v, v->pos);
1176 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1177 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1178 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1179 vt102_cursor_advance (c);
1183 v->crt.pos = v->pos;
1184 v->crt.hide_cursor =
1185 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1187 if (v->current_line.y != v->pos.y)
1189 vt102_log_line (c, v->current_line.y);
1190 v->current_line = v->pos;
1195 vt102_parse (Context * c, char *buf, int len)
1198 vt102_parse_char (c, *(buf++));
1203 vt102_parser_reset (VT102_parser * p)
1213 vt102_send (Context * c, uint8_t key)
1221 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1223 if ((key > 31) && (key < 127))
1225 c->t->xmit (c->t, &key, 1);
1244 c->t->xmit (c->t, &key, 1);
1247 c->t->xmit (c->t, &key, 1);
1248 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1251 c->t->xmit (c->t, &ch, 1);
1267 c->t->xmit (c->t, &key, 1);
1275 c->t->xmit (c->t, &key, 1);
1286 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1288 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1289 c->t->xmit (c->t, &buf, sizeof (buf));
1293 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1294 c->t->xmit (c->t, &buf, sizeof (buf));
1312 if (c->v->application_keypad_mode)
1314 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1315 c->t->xmit (c->t, &buf, sizeof (buf));
1319 static char kpoff[KEY_NUM] = {
1338 c->t->xmit (c->t, &kpoff[key], 1);
1342 if (c->v->application_keypad_mode)
1344 uint8_t buf[] = { 033, 'O', 'M' };
1345 c->t->xmit (c->t, &buf, sizeof (buf));
1350 c->t->xmit (c->t, &ch, 1);
1351 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1354 c->t->xmit (c->t, &ch, 1);
1363 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1364 c->t->xmit (c->t, &buf, sizeof (buf));
1372 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1373 c->t->xmit (c->t, &buf, sizeof (buf));
1381 vt102_reset (VT102 * v)
1383 VT102_parser *p = &v->parser;
1385 vt102_parser_reset (p);
1388 v->attr = CRT_ATTR_NORMAL;
1389 v->color = CRT_COLOR_NORMAL;
1391 v->application_keypad_mode = 0;
1393 v->current_line = v->pos;
1394 v->pending_wrap = 0;
1396 v->screen_start.x = 0;
1397 v->screen_start.y = 0;
1398 v->screen_end.x = VT102_COLS - 1;
1399 v->screen_end.y = VT102_ROWS - 1;
1401 v->top_margin = v->screen_start;
1402 v->bottom_margin = v->screen_end;
1404 memset (v->modes, 0, VT102_NMODES);
1405 memset (v->private_modes, 0, VT102_NMODES);
1407 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1408 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1409 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1410 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1412 vt102_cursor_home (v);
1413 vt102_reset_tabs (v);
1414 v->current_line = v->pos;
1416 vt102_save_state (v);
1418 vt102_status_line (v, "");
1423 vt102_dispatch (Context * c)
1428 red = c->t->recv (c->t, buf, sizeof (buf));
1436 vt102_parse (c, buf, red);
1443 vt102_dispatch_one (Context * c)
1448 red = c->t->recv (c->t, &buf, sizeof (buf));
1455 vt102_parse_char (c, buf);
1465 v = (VT102 *) malloc (sizeof (VT102));
1474 vt102_free (VT102 * v)