4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.27 2008/02/14 02:46:45 james
14 * *** empty log message ***
16 * Revision 1.26 2008/02/14 01:55:57 james
17 * *** empty log message ***
19 * Revision 1.25 2008/02/13 16:57:29 james
20 * *** empty log message ***
22 * Revision 1.24 2008/02/13 09:12:21 james
23 * *** empty log message ***
25 * Revision 1.23 2008/02/07 13:26:35 james
26 * *** empty log message ***
28 * Revision 1.22 2008/02/07 13:22:51 james
29 * *** empty log message ***
31 * Revision 1.21 2008/02/07 12:21:16 james
32 * *** empty log message ***
34 * Revision 1.20 2008/02/07 12:16:04 james
35 * *** empty log message ***
37 * Revision 1.19 2008/02/07 11:27:02 james
38 * *** empty log message ***
40 * Revision 1.18 2008/02/07 01:59:25 james
41 * *** empty log message ***
43 * Revision 1.17 2008/02/07 01:58:28 james
44 * *** empty log message ***
46 * Revision 1.16 2008/02/07 01:57:46 james
47 * *** empty log message ***
49 * Revision 1.15 2008/02/07 00:43:27 james
50 * *** empty log message ***
52 * Revision 1.14 2008/02/07 00:40:23 james
53 * *** empty log message ***
55 * Revision 1.13 2008/02/07 00:39:59 james
56 * *** empty log message ***
58 * Revision 1.12 2008/02/07 00:39:13 james
59 * *** empty log message ***
61 * Revision 1.11 2008/02/06 20:26:58 james
62 * *** empty log message ***
64 * Revision 1.10 2008/02/06 17:53:28 james
65 * *** empty log message ***
67 * Revision 1.9 2008/02/06 15:53:22 james
68 * *** empty log message ***
70 * Revision 1.8 2008/02/06 11:49:47 james
71 * *** empty log message ***
73 * Revision 1.7 2008/02/06 11:30:37 james
74 * *** empty log message ***
76 * Revision 1.6 2008/02/05 01:11:46 james
77 * *** empty log message ***
79 * Revision 1.5 2008/02/04 20:23:55 james
80 * *** empty log message ***
82 * Revision 1.4 2008/02/04 05:45:55 james
85 * Revision 1.3 2008/02/04 02:05:06 james
86 * *** empty log message ***
88 * Revision 1.2 2008/02/04 01:32:39 james
89 * *** empty log message ***
91 * Revision 1.1 2008/02/03 23:36:41 james
92 * *** empty log message ***
101 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
103 vt100|vt100-am|dec vt100 (w/advanced video):\
105 :co#80:it#8:li#24:vt#3:\
106 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
108 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
109 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
110 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
111 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
112 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
113 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
114 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
115 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
116 :us=\E[4m:tc=vt100+fnkeys:
118 vt100+fnkeys|dec vt100 numeric keypad:\
119 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
122 vt100+pfkeys|dec vt100 numeric keypad:\
123 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
125 vt100+keypad|dec vt100 numeric keypad no fkeys:\
126 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
131 so the parser needs to be able to at least do
199 if ((c >= 'a') && (c <= 'z'))
201 if ((c >= 'A') && (c <= 'Z'))
230 in_margins (VT102 * v, CRT_Pos p)
232 if (v->pos.x < v->top_margin.x)
234 if (v->pos.y < v->top_margin.y)
237 if (v->pos.x > v->bottom_margin.x)
239 if (v->pos.y > v->bottom_margin.y)
246 vt102_log_line (Context * c, int line)
248 CRT_Pos e = { VT102_COLS - 1, line };
249 CRT_Pos p = { 0, line };
250 char logbuf[VT102_COLS + 1];
256 for (; e.x > 0; --e.x)
258 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
262 for (; p.x <= e.x; ++p.x)
264 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
273 c->l->log (c->l, logbuf);
276 /*Called for every upward scroll with same args*/
278 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
280 /*Only log if it scrolls off the top*/
285 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
289 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
304 vt102_cursor_normalize (VT102 * v)
306 CRT_Pos *top, *bottom;
308 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
310 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
314 vt102_clip_cursor (v, v->screen_start, v->screen_end);
320 vt102_cursor_carriage_return (VT102 * v)
322 /*FISH*/ v->pos.x = v->top_margin.x;
327 vt102_cursor_advance_line (Context * c)
330 int couldscroll = in_margins (v, v->pos);
332 /*have wraped off end of last line in scrolling region */
333 /* (|| not necessary, but shuts compiler up */
334 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
337 vt102_log_line (c, v->pos.y);
339 vt102_history (c, v->top_margin, v->bottom_margin);
341 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
351 vt102_cursor_advance (Context * c)
355 if (v->pos.x < v->bottom_margin.x)
367 vt102_do_pending_wrap (Context * c)
370 int couldscroll = in_margins (v, v->pos);
371 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
373 if (!v->pending_wrap)
377 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
378 v->pos.y, autowrap, couldscroll);
381 /*End of line but no autowrap, nothing to do*/
385 /*End of screen and not allowed to scroll, nothing to do*/
386 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
391 v->pos.x = v->top_margin.x;
398 vt102_cursor_advance_line (c);
403 vt102_cursor_retard (VT102 * v)
405 if (v->pos.x != v->top_margin.x)
412 vt102_reset_tabs (VT102 * v)
416 memset (v->tabs, 0, sizeof (v->tabs));
418 for (i = 0; i < VT102_COLS; i += 8)
424 vt102_cursor_advance_tab (VT102 * v)
426 if (v->pos.x == v->bottom_margin.x)
428 while (v->pos.x < v->bottom_margin.x)
431 if (v->tabs[v->pos.x])
437 vt102_cursor_home (VT102 * v)
439 v->pos = v->top_margin;
440 vt102_cursor_normalize (v);
445 vt102_cursor_absolute (VT102 * v, int x, int y)
447 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
449 v->pos.x = x + v->top_margin.x;
450 v->pos.y = y + v->top_margin.x;
457 vt102_cursor_normalize (v);
461 vt102_cursor_relative (VT102 * v, int x, int y)
465 vt102_cursor_normalize (v);
472 vt102_delete_from_line (VT102 * v, CRT_Pos p)
474 int n = v->bottom_margin.x - p.x;
482 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
483 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
486 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
487 /*But not attr due to vt102 bug*/
491 vt102_insert_into_line (VT102 * v, CRT_Pos p)
493 int n = v->bottom_margin.x - p.x;
501 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
502 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
505 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
506 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
507 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
513 vt102_change_mode (VT102 * v, int private, char *ns, int set)
529 if (m >= VT102_NMODES)
534 v->private_modes[m] = set;
537 case VT102_PRIVATE_MODE_CURSOR_MODE:
538 if (v->application_keypad_mode)
539 v->private_modes[m] = 0;
541 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
542 v->private_modes[m], set);
545 case VT102_PRIVATE_MODE_ORIGIN_MODE:
546 vt102_cursor_home (v);
555 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
560 vt102_parse_mode_string (VT102 * v, char *buf, int len)
563 char last = buf[len - 1];
567 memset (num, 0, sizeof (num));
568 o = sizeof (num) - 1;
586 vt102_change_mode (v, private, &num[o], last == 'h');
587 memset (num, 0, sizeof (num));
588 o = sizeof (num) - 1;
603 vt102_change_mode (v, private, &num[o], last == 'h');
609 vt102_change_attr (VT102 * v, char *na)
626 v->attr = CRT_ATTR_NORMAL;
627 v->color = CRT_COLOR_NORMAL;
630 v->attr |= CRT_ATTR_BOLD;
634 v->attr &= ~CRT_ATTR_BOLD;
637 v->attr |= CRT_ATTR_UNDERLINE;
640 v->attr &= ~CRT_ATTR_UNDERLINE;
643 v->attr |= CRT_ATTR_BLINK;
646 v->attr &= ~CRT_ATTR_BLINK;
649 v->attr |= CRT_ATTR_REVERSE;
652 v->attr &= ~CRT_ATTR_REVERSE;
662 v->color &= ~CRT_COLOR_FG_MASK;
663 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
673 v->color &= ~CRT_COLOR_FG_MASK;
676 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
681 v->color &= ~CRT_COLOR_FG_MASK;
683 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
693 v->color &= ~CRT_COLOR_BG_MASK;
694 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
704 v->color &= ~CRT_COLOR_BG_MASK;
707 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
712 v->color &= ~CRT_COLOR_BG_MASK;
714 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
720 fprintf (stderr, "unhandled SGR %d\n", a);
728 vt102_parse_attr_string (VT102 * v, char *buf, int len)
731 char last = buf[len - 1];
735 memset (num, 0, sizeof (num));
736 o = sizeof (num) - 1;
747 vt102_change_attr (v, &num[o]);
748 memset (num, 0, sizeof (num));
749 o = sizeof (num) - 1;
763 vt102_change_attr (v, &num[o]);
767 vt102_save_state (VT102 * v)
769 v->saved.pos = v->pos;
770 v->saved.attr = v->attr;
771 v->saved.color = v->color;
772 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
776 vt102_restore_state (VT102 * v)
778 v->pos = v->saved.pos;
779 v->attr = v->saved.attr;
780 v->color = v->saved.color;
781 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
782 vt102_cursor_normalize (v);
787 vt102_scs (Context * c, int g, int s)
789 /*Ignoring charsets*/
793 vt102_parse_esc (Context * c, int ch)
799 if (v->pos.y == v->bottom_margin.y)
801 vt102_log_line (c, v->pos.y);
802 vt102_history (c, v->top_margin, v->bottom_margin);
803 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
807 vt102_cursor_relative (v, 0, 1);
814 if (v->pos.y == v->top_margin.y)
816 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
820 vt102_cursor_relative (v, 0, -1);
824 vt102_save_state (v);
827 vt102_restore_state (v);
830 v->application_keypad_mode = 1;
833 v->application_keypad_mode = 0;
837 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
838 (ch < 32) ? '?' : ch);
844 vt102_parse_csi (Context * c, char *buf, int len)
875 vt102_cursor_relative (v, 0, -narg);
878 vt102_cursor_relative (v, 0, narg);
881 vt102_cursor_relative (v, narg, 0);
884 vt102_cursor_relative (v, -narg, 0);
887 vt102_cursor_relative (v, 0, narg);
888 vt102_cursor_carriage_return (v);
891 vt102_cursor_relative (v, 0, -narg);
892 vt102_cursor_carriage_return (v);
895 vt102_cursor_absolute (v, narg - 1, v->pos.y);
904 ptr = index (arg, ';');
906 x = atoi (ptr + 1) - 1;
910 vt102_cursor_absolute (v, x, y);
917 crt_erase (&v->crt, v->pos, v->screen_end, 1);
920 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
926 CRT_Pos ls = { 0, v->pos.y };
927 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
929 narg = 0; /*Different default */
934 crt_erase (&v->crt, v->pos, le, 1);
937 crt_erase (&v->crt, ls, v->pos, 1);
940 crt_erase (&v->crt, ls, le, 1);
948 vt102_delete_from_line (v, v->pos);
951 if ((v->pos.y >= v->top_margin.y)
952 && (v->pos.y <= v->bottom_margin.y))
955 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
960 if ((v->pos.y >= v->top_margin.y)
961 && (v->pos.y <= v->bottom_margin.y))
965 vt102_history (c, v->pos, v->bottom_margin);
966 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1);
973 narg = 0; /*Different default */
978 v->tabs[v->pos.x] = 0;
981 memset (v->tabs, 0, sizeof (v->tabs));
988 vt102_parse_mode_string (v, &buf[1], len - 1);
992 vt102_parse_attr_string (v, &buf[1], len - 1);
995 v->top_margin = v->screen_start;
996 v->bottom_margin = v->screen_end;
998 if ((len > 2) && (ptr = index (arg, ';')))
1001 v->top_margin.y = narg - 1;
1002 v->bottom_margin.y = atoi (ptr) - 1;
1005 if (v->top_margin.y < v->screen_start.y)
1006 v->top_margin.y = v->screen_start.y;
1007 if (v->top_margin.y > v->screen_end.y)
1008 v->top_margin.y = v->screen_end.y;
1009 if (v->bottom_margin.y < v->screen_start.y)
1010 v->bottom_margin.y = v->screen_start.y;
1011 if (v->bottom_margin.y > v->screen_end.y)
1012 v->bottom_margin.y = v->screen_end.y;
1014 vt102_cursor_home (v);
1017 v->saved.pos = v->pos;
1020 v->pos = v->saved.pos;
1021 vt102_cursor_normalize (v);
1022 v->pending_wrap = 0;
1027 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1034 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1044 vt102_status_line (VT102 * v, char *str)
1047 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1051 ca->attr = CRT_ATTR_REVERSE;
1052 ca->color = CRT_COLOR_NORMAL;
1063 vt102_parse_char (Context * c, int ch)
1066 VT102_parser *p = &v->parser;
1069 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1070 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1074 p->csi_buf[p->csi_ptr++] = ch;
1075 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1077 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1081 else if (p->in_escape)
1083 if (csi_starter (ch))
1086 p->csi_buf[p->csi_ptr++] = ch;
1090 else if (scs_starter (ch))
1098 vt102_parse_esc (c, ch);
1103 vt102_scs (c, p->in_scs, ch);
1118 c->t->xmit (c->t, "vt102", 5);
1124 vt102_cursor_retard (c->v);
1127 vt102_cursor_advance_tab (c->v);
1132 vt102_cursor_advance_line (c);
1133 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1136 vt102_cursor_carriage_return (v);
1140 /*Ignoring charsets */
1144 /*Ignoring charsets */
1167 /*regular character */ default:
1168 vt102_do_pending_wrap (c);
1170 if (v->modes[VT102_MODE_INSERT])
1171 vt102_insert_into_line (v, v->pos);
1173 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1174 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1175 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1176 vt102_cursor_advance (c);
1180 v->crt.pos = v->pos;
1181 v->crt.hide_cursor =
1182 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1184 if (v->current_line.y != v->pos.y)
1186 vt102_log_line (c, v->current_line.y);
1187 v->current_line = v->pos;
1190 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1193 vt102_parse (Context * c, char *buf, int len)
1196 vt102_parse_char (c, *(buf++));
1201 vt102_parser_reset (VT102_parser * p)
1211 vt102_send (Context * c, uint8_t key)
1219 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1221 if ((key > 31) && (key < 127))
1223 c->t->xmit (c->t, &key, 1);
1242 c->t->xmit (c->t, &key, 1);
1245 c->t->xmit (c->t, &key, 1);
1246 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1249 c->t->xmit (c->t, &ch, 1);
1265 c->t->xmit (c->t, &key, 1);
1273 c->t->xmit (c->t, &key, 1);
1284 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1286 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1287 c->t->xmit (c->t, &buf, sizeof (buf));
1291 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1292 c->t->xmit (c->t, &buf, sizeof (buf));
1310 if (c->v->application_keypad_mode)
1312 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1313 c->t->xmit (c->t, &buf, sizeof (buf));
1317 static char kpoff[KEY_NUM] = {
1336 c->t->xmit (c->t, &kpoff[key], 1);
1340 if (c->v->application_keypad_mode)
1342 uint8_t buf[] = { 033, 'O', 'M' };
1343 c->t->xmit (c->t, &buf, sizeof (buf));
1348 c->t->xmit (c->t, &ch, 1);
1349 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1352 c->t->xmit (c->t, &ch, 1);
1361 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1362 c->t->xmit (c->t, &buf, sizeof (buf));
1370 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1371 c->t->xmit (c->t, &buf, sizeof (buf));
1379 vt102_reset (VT102 * v)
1381 VT102_parser *p = &v->parser;
1383 vt102_parser_reset (p);
1386 v->attr = CRT_ATTR_NORMAL;
1387 v->color = CRT_COLOR_NORMAL;
1389 v->application_keypad_mode = 0;
1391 v->current_line = v->pos;
1392 v->pending_wrap = 0;
1394 v->screen_start.x = 0;
1395 v->screen_start.y = 0;
1396 v->screen_end.x = VT102_COLS - 1;
1397 v->screen_end.y = VT102_ROWS - 1;
1399 v->top_margin = v->screen_start;
1400 v->bottom_margin = v->screen_end;
1402 memset (v->modes, 0, VT102_NMODES);
1403 memset (v->private_modes, 0, VT102_NMODES);
1405 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1406 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1407 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1408 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1410 vt102_cursor_home (v);
1411 vt102_reset_tabs (v);
1412 v->current_line = v->pos;
1414 vt102_save_state (v);
1416 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1421 vt102_dispatch (Context * c)
1426 red = c->t->recv (c->t, buf, sizeof (buf));
1434 vt102_parse (c, buf, red);
1441 vt102_dispatch_one (Context * c)
1446 red = c->t->recv (c->t, &buf, sizeof (buf));
1453 vt102_parse_char (c, buf);
1463 v = (VT102 *) malloc (sizeof (VT102));
1472 vt102_free (VT102 * v)