4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.25 2008/02/13 16:57:29 james
14 * *** empty log message ***
16 * Revision 1.24 2008/02/13 09:12:21 james
17 * *** empty log message ***
19 * Revision 1.23 2008/02/07 13:26:35 james
20 * *** empty log message ***
22 * Revision 1.22 2008/02/07 13:22:51 james
23 * *** empty log message ***
25 * Revision 1.21 2008/02/07 12:21:16 james
26 * *** empty log message ***
28 * Revision 1.20 2008/02/07 12:16:04 james
29 * *** empty log message ***
31 * Revision 1.19 2008/02/07 11:27:02 james
32 * *** empty log message ***
34 * Revision 1.18 2008/02/07 01:59:25 james
35 * *** empty log message ***
37 * Revision 1.17 2008/02/07 01:58:28 james
38 * *** empty log message ***
40 * Revision 1.16 2008/02/07 01:57:46 james
41 * *** empty log message ***
43 * Revision 1.15 2008/02/07 00:43:27 james
44 * *** empty log message ***
46 * Revision 1.14 2008/02/07 00:40:23 james
47 * *** empty log message ***
49 * Revision 1.13 2008/02/07 00:39:59 james
50 * *** empty log message ***
52 * Revision 1.12 2008/02/07 00:39:13 james
53 * *** empty log message ***
55 * Revision 1.11 2008/02/06 20:26:58 james
56 * *** empty log message ***
58 * Revision 1.10 2008/02/06 17:53:28 james
59 * *** empty log message ***
61 * Revision 1.9 2008/02/06 15:53:22 james
62 * *** empty log message ***
64 * Revision 1.8 2008/02/06 11:49:47 james
65 * *** empty log message ***
67 * Revision 1.7 2008/02/06 11:30:37 james
68 * *** empty log message ***
70 * Revision 1.6 2008/02/05 01:11:46 james
71 * *** empty log message ***
73 * Revision 1.5 2008/02/04 20:23:55 james
74 * *** empty log message ***
76 * Revision 1.4 2008/02/04 05:45:55 james
79 * Revision 1.3 2008/02/04 02:05:06 james
80 * *** empty log message ***
82 * Revision 1.2 2008/02/04 01:32:39 james
83 * *** empty log message ***
85 * Revision 1.1 2008/02/03 23:36:41 james
86 * *** empty log message ***
95 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
97 vt100|vt100-am|dec vt100 (w/advanced video):\
99 :co#80:it#8:li#24:vt#3:\
100 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
102 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
103 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
104 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
105 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
106 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
107 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
108 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
109 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
110 :us=\E[4m:tc=vt100+fnkeys:
112 vt100+fnkeys|dec vt100 numeric keypad:\
113 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
116 vt100+pfkeys|dec vt100 numeric keypad:\
117 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
119 vt100+keypad|dec vt100 numeric keypad no fkeys:\
120 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
125 so the parser needs to be able to at least do
193 if ((c >= 'a') && (c <= 'z'))
195 if ((c >= 'A') && (c <= 'Z'))
224 in_margins (VT102 * v, CRT_Pos p)
226 if (v->pos.x < v->top_margin.x)
228 if (v->pos.y < v->top_margin.y)
231 if (v->pos.x > v->bottom_margin.x)
233 if (v->pos.y > v->bottom_margin.y)
240 vt102_log_line (Context * c, int line)
242 CRT_Pos e = { VT102_COLS - 1, line };
243 CRT_Pos p = { 0, line };
244 char logbuf[VT102_COLS + 1];
250 for (; e.x > 0; --e.x)
252 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
256 for (; p.x <= e.x; ++p.x)
258 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
267 c->l->log (c->l, logbuf);
270 /*Called for every upward scroll with same args*/
272 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
274 /*Only log if it scrolls off the top*/
279 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
283 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
298 vt102_cursor_normalize (VT102 * v)
300 CRT_Pos *top, *bottom;
302 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
304 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
308 vt102_clip_cursor (v, v->screen_start, v->screen_end);
314 vt102_cursor_carriage_return (VT102 * v)
316 /*FISH*/ v->pos.x = v->top_margin.x;
321 vt102_cursor_advance_line (Context * c)
324 int couldscroll = in_margins (v, v->pos);
326 /*have wraped off end of last line in scrolling region */
327 /* (|| not necessary, but shuts compiler up */
328 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
331 vt102_log_line (c, v->pos.y);
333 vt102_history (c, v->top_margin, v->bottom_margin);
335 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
345 vt102_cursor_advance (Context * c)
349 if (v->pos.x < v->bottom_margin.x)
361 vt102_do_pending_wrap (Context * c)
364 int couldscroll = in_margins (v, v->pos);
365 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
367 if (!v->pending_wrap)
371 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
372 v->pos.y, autowrap, couldscroll);
375 /*End of line but no autowrap, nothing to do*/
379 /*End of screen and not allowed to scroll, nothing to do*/
380 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
385 v->pos.x = v->top_margin.x;
392 vt102_cursor_advance_line (c);
397 vt102_cursor_retard (VT102 * v)
399 if (v->pos.x != v->top_margin.x)
406 vt102_reset_tabs (VT102 * v)
410 memset (v->tabs, 0, sizeof (v->tabs));
412 for (i = 0; i < VT102_COLS; i += 8)
418 vt102_cursor_advance_tab (VT102 * v)
420 if (v->pos.x == v->bottom_margin.x)
422 while (v->pos.x < v->bottom_margin.x)
425 if (v->tabs[v->pos.x])
431 vt102_cursor_home (VT102 * v)
433 v->pos = v->top_margin;
434 vt102_cursor_normalize (v);
439 vt102_cursor_absolute (VT102 * v, int x, int y)
441 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
443 v->pos.x = x + v->top_margin.x;
444 v->pos.y = y + v->top_margin.x;
451 vt102_cursor_normalize (v);
455 vt102_cursor_relative (VT102 * v, int x, int y)
459 vt102_cursor_normalize (v);
466 vt102_delete_from_line (VT102 * v, CRT_Pos p)
468 int n = v->bottom_margin.x - p.x;
476 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
477 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
480 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
481 /*But not attr due to vt102 bug*/
485 vt102_insert_into_line (VT102 * v, CRT_Pos p)
487 int n = v->bottom_margin.x - p.x;
495 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
496 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
499 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
500 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
501 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
507 vt102_change_mode (VT102 * v, int private, char *ns, int set)
523 if (m >= VT102_NMODES)
528 v->private_modes[m] = set;
531 case VT102_PRIVATE_MODE_CURSOR_MODE:
532 if (v->application_keypad_mode)
533 v->private_modes[m] = 0;
535 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
536 v->private_modes[m], set);
539 case VT102_PRIVATE_MODE_ORIGIN_MODE:
540 vt102_cursor_home (v);
549 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
554 vt102_parse_mode_string (VT102 * v, char *buf, int len)
557 char last = buf[len - 1];
561 memset (num, 0, sizeof (num));
562 o = sizeof (num) - 1;
580 vt102_change_mode (v, private, &num[o], last == 'h');
581 memset (num, 0, sizeof (num));
582 o = sizeof (num) - 1;
597 vt102_change_mode (v, private, &num[o], last == 'h');
603 vt102_change_attr (VT102 * v, char *na)
620 v->attr = CRT_ATTR_NORMAL;
621 v->color = CRT_COLOR_NORMAL;
624 v->attr |= CRT_ATTR_BOLD;
628 v->attr &= ~CRT_ATTR_BOLD;
631 v->attr |= CRT_ATTR_UNDERLINE;
634 v->attr &= ~CRT_ATTR_UNDERLINE;
637 v->attr |= CRT_ATTR_BLINK;
640 v->attr &= ~CRT_ATTR_BLINK;
643 v->attr |= CRT_ATTR_REVERSE;
646 v->attr &= ~CRT_ATTR_REVERSE;
656 v->color &= ~CRT_COLOR_FG_MASK;
657 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
667 v->color &= ~CRT_COLOR_FG_MASK;
670 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
675 v->color &= ~CRT_COLOR_FG_MASK;
677 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
687 v->color &= ~CRT_COLOR_BG_MASK;
688 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
698 v->color &= ~CRT_COLOR_BG_MASK;
701 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
706 v->color &= ~CRT_COLOR_BG_MASK;
708 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
714 fprintf (stderr, "unhandled SGR %d\n", a);
722 vt102_parse_attr_string (VT102 * v, char *buf, int len)
725 char last = buf[len - 1];
729 memset (num, 0, sizeof (num));
730 o = sizeof (num) - 1;
741 vt102_change_attr (v, &num[o]);
742 memset (num, 0, sizeof (num));
743 o = sizeof (num) - 1;
757 vt102_change_attr (v, &num[o]);
761 vt102_save_state (VT102 * v)
763 v->saved.pos = v->pos;
764 v->saved.attr = v->attr;
765 v->saved.color = v->color;
766 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
770 vt102_restore_state (VT102 * v)
772 v->pos = v->saved.pos;
773 v->attr = v->saved.attr;
774 v->color = v->saved.color;
775 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
776 vt102_cursor_normalize (v);
781 vt102_scs (Context * c, int g, int s)
783 /*Ignoring charsets*/
787 vt102_parse_esc (Context * c, int ch)
793 if (v->pos.y == v->bottom_margin.y)
795 vt102_log_line (c, v->pos.y);
796 vt102_history (c, v->top_margin, v->bottom_margin);
797 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
801 vt102_cursor_relative (v, 0, 1);
808 if (v->pos.y == v->top_margin.y)
810 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
814 vt102_cursor_relative (v, 0, -1);
818 vt102_save_state (v);
821 vt102_restore_state (v);
824 v->application_keypad_mode = 1;
827 v->application_keypad_mode = 0;
831 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
832 (ch < 32) ? '?' : ch);
838 vt102_parse_csi (Context * c, char *buf, int len)
869 vt102_cursor_relative (v, 0, -narg);
872 vt102_cursor_relative (v, 0, narg);
875 vt102_cursor_relative (v, narg, 0);
878 vt102_cursor_relative (v, -narg, 0);
881 vt102_cursor_relative (v, 0, narg);
882 vt102_cursor_carriage_return (v);
885 vt102_cursor_relative (v, 0, -narg);
886 vt102_cursor_carriage_return (v);
889 vt102_cursor_absolute (v, narg - 1, v->pos.y);
898 ptr = index (arg, ';');
900 x = atoi (ptr + 1) - 1;
904 vt102_cursor_absolute (v, x, y);
911 crt_erase (&v->crt, v->pos, v->screen_end, 1);
914 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
920 CRT_Pos ls = { 0, v->pos.y };
921 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
923 narg = 0; /*Different default */
928 crt_erase (&v->crt, v->pos, le, 1);
931 crt_erase (&v->crt, ls, v->pos, 1);
934 crt_erase (&v->crt, ls, le, 1);
942 vt102_delete_from_line (v, v->pos);
945 if ((v->pos.y >= v->top_margin.y)
946 && (v->pos.y <= v->bottom_margin.y))
949 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
954 if ((v->pos.y >= v->top_margin.y)
955 && (v->pos.y <= v->bottom_margin.y))
959 vt102_history (c, v->pos, v->bottom_margin);
960 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1);
967 narg = 0; /*Different default */
972 v->tabs[v->pos.x] = 0;
975 memset (v->tabs, 0, sizeof (v->tabs));
982 vt102_parse_mode_string (v, &buf[1], len - 1);
986 vt102_parse_attr_string (v, &buf[1], len - 1);
989 v->top_margin = v->screen_start;
990 v->bottom_margin = v->screen_end;
992 if ((len > 2) && (ptr = index (arg, ';')))
995 v->top_margin.y = narg - 1;
996 v->bottom_margin.y = atoi (ptr) - 1;
999 if (v->top_margin.y < v->screen_start.y)
1000 v->top_margin.y = v->screen_start.y;
1001 if (v->top_margin.y > v->screen_end.y)
1002 v->top_margin.y = v->screen_end.y;
1003 if (v->bottom_margin.y < v->screen_start.y)
1004 v->bottom_margin.y = v->screen_start.y;
1005 if (v->bottom_margin.y > v->screen_end.y)
1006 v->bottom_margin.y = v->screen_end.y;
1008 vt102_cursor_home (v);
1011 v->saved.pos = v->pos;
1014 v->pos = v->saved.pos;
1015 vt102_cursor_normalize (v);
1016 v->pending_wrap = 0;
1021 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1028 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1038 vt102_status_line (VT102 * v, char *str)
1041 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1045 ca->attr = CRT_ATTR_REVERSE;
1046 ca->color = CRT_COLOR_NORMAL;
1057 vt102_parse_char (Context * c, int ch)
1060 VT102_parser *p = &v->parser;
1063 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1064 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1068 p->csi_buf[p->csi_ptr++] = ch;
1069 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1071 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1075 else if (p->in_escape)
1077 if (csi_starter (ch))
1080 p->csi_buf[p->csi_ptr++] = ch;
1084 else if (scs_starter (ch))
1092 vt102_parse_esc (c, ch);
1097 vt102_scs (c, p->in_scs, ch);
1112 c->t->xmit (c->t, "vt102", 5);
1118 vt102_cursor_retard (c->v);
1121 vt102_cursor_advance_tab (c->v);
1126 vt102_cursor_advance_line (c);
1127 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1130 vt102_cursor_carriage_return (v);
1134 /*Ignoring charsets */
1138 /*Ignoring charsets */
1161 /*regular character */ default:
1162 vt102_do_pending_wrap (c);
1164 if (v->modes[VT102_MODE_INSERT])
1165 vt102_insert_into_line (v, v->pos);
1167 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1168 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1169 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1170 vt102_cursor_advance (c);
1174 v->crt.pos = v->pos;
1175 v->crt.hide_cursor =
1176 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1178 if (v->current_line.y != v->pos.y)
1180 vt102_log_line (c, v->current_line.y);
1181 v->current_line = v->pos;
1184 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1187 vt102_parse (Context * c, char *buf, int len)
1190 vt102_parse_char (c, *(buf++));
1195 vt102_parser_reset (VT102_parser * p)
1205 vt102_send (Context * c, uint8_t key)
1209 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1211 if ((key > 31) && (key < 127))
1213 c->t->xmit (c->t, &key, 1);
1232 c->t->xmit (c->t, &key, 1);
1235 c->t->xmit (c->t, &key, 1);
1236 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1239 c->t->xmit (c->t, &ch, 1);
1255 c->t->xmit (c->t, &key, 1);
1263 c->t->xmit (c->t, &key, 1);
1274 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1276 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1277 c->t->xmit (c->t, &buf, sizeof (buf));
1281 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1282 c->t->xmit (c->t, &buf, sizeof (buf));
1300 if (c->v->application_keypad_mode)
1302 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1303 c->t->xmit (c->t, &buf, sizeof (buf));
1307 static char kpoff[KEY_NUM] = {
1326 c->t->xmit (c->t, &kpoff[key], 1);
1330 if (c->v->application_keypad_mode)
1332 uint8_t buf[] = { 033, 'O', 'M' };
1333 c->t->xmit (c->t, &buf, sizeof (buf));
1338 c->t->xmit (c->t, &ch, 1);
1339 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1342 c->t->xmit (c->t, &ch, 1);
1351 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1352 c->t->xmit (c->t, &buf, sizeof (buf));
1360 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1361 c->t->xmit (c->t, &buf, sizeof (buf));
1369 vt102_reset (VT102 * v)
1371 VT102_parser *p = &v->parser;
1373 vt102_parser_reset (p);
1376 v->attr = CRT_ATTR_NORMAL;
1377 v->color = CRT_COLOR_NORMAL;
1379 v->application_keypad_mode = 0;
1381 v->current_line = v->pos;
1382 v->pending_wrap = 0;
1384 v->screen_start.x = 0;
1385 v->screen_start.y = 0;
1386 v->screen_end.x = VT102_COLS - 1;
1387 v->screen_end.y = VT102_ROWS - 1;
1389 v->top_margin = v->screen_start;
1390 v->bottom_margin = v->screen_end;
1392 memset (v->modes, 0, VT102_NMODES);
1393 memset (v->private_modes, 0, VT102_NMODES);
1395 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1396 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1397 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1398 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1400 vt102_cursor_home (v);
1401 vt102_reset_tabs (v);
1402 v->current_line = v->pos;
1404 vt102_save_state (v);
1406 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1411 vt102_dispatch (Context * c)
1416 red = c->t->recv (c->t, buf, sizeof (buf));
1424 vt102_parse (c, buf, red);
1431 vt102_dispatch_one (Context * c)
1436 red = c->t->recv (c->t, &buf, sizeof (buf));
1443 vt102_parse_char (c, buf);
1453 v = (VT102 *) malloc (sizeof (VT102));
1462 vt102_free (VT102 * v)