4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.22 2008/02/07 13:22:51 james
14 * *** empty log message ***
16 * Revision 1.21 2008/02/07 12:21:16 james
17 * *** empty log message ***
19 * Revision 1.20 2008/02/07 12:16:04 james
20 * *** empty log message ***
22 * Revision 1.19 2008/02/07 11:27:02 james
23 * *** empty log message ***
25 * Revision 1.18 2008/02/07 01:59:25 james
26 * *** empty log message ***
28 * Revision 1.17 2008/02/07 01:58:28 james
29 * *** empty log message ***
31 * Revision 1.16 2008/02/07 01:57:46 james
32 * *** empty log message ***
34 * Revision 1.15 2008/02/07 00:43:27 james
35 * *** empty log message ***
37 * Revision 1.14 2008/02/07 00:40:23 james
38 * *** empty log message ***
40 * Revision 1.13 2008/02/07 00:39:59 james
41 * *** empty log message ***
43 * Revision 1.12 2008/02/07 00:39:13 james
44 * *** empty log message ***
46 * Revision 1.11 2008/02/06 20:26:58 james
47 * *** empty log message ***
49 * Revision 1.10 2008/02/06 17:53:28 james
50 * *** empty log message ***
52 * Revision 1.9 2008/02/06 15:53:22 james
53 * *** empty log message ***
55 * Revision 1.8 2008/02/06 11:49:47 james
56 * *** empty log message ***
58 * Revision 1.7 2008/02/06 11:30:37 james
59 * *** empty log message ***
61 * Revision 1.6 2008/02/05 01:11:46 james
62 * *** empty log message ***
64 * Revision 1.5 2008/02/04 20:23:55 james
65 * *** empty log message ***
67 * Revision 1.4 2008/02/04 05:45:55 james
70 * Revision 1.3 2008/02/04 02:05:06 james
71 * *** empty log message ***
73 * Revision 1.2 2008/02/04 01:32:39 james
74 * *** empty log message ***
76 * Revision 1.1 2008/02/03 23:36:41 james
77 * *** empty log message ***
86 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
88 vt100|vt100-am|dec vt100 (w/advanced video):\
90 :co#80:it#8:li#24:vt#3:\
91 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
93 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
94 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
95 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
96 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
97 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
98 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
99 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
100 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
101 :us=\E[4m:tc=vt100+fnkeys:
103 vt100+fnkeys|dec vt100 numeric keypad:\
104 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
107 vt100+pfkeys|dec vt100 numeric keypad:\
108 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
110 vt100+keypad|dec vt100 numeric keypad no fkeys:\
111 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
116 so the parser needs to be able to at least do
184 if ((c >= 'a') && (c <= 'z'))
186 if ((c >= 'A') && (c <= 'Z'))
215 in_margins (VT102 * v, CRT_Pos p)
217 if (v->pos.x < v->top_margin.x)
219 if (v->pos.y < v->top_margin.y)
222 if (v->pos.x > v->bottom_margin.x)
224 if (v->pos.y > v->bottom_margin.y)
231 vt102_log_line (VT102 * v, int line)
234 CRT_Pos e = { VT102_COLS - 1, line };
235 CRT_Pos p = { 0, line };
238 log = fopen ("log", "a+");
244 for (; e.x > 0; --e.x)
246 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
250 for (; p.x <= e.x; ++p.x)
252 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
263 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
278 vt102_cursor_normalize (VT102 * v)
280 CRT_Pos *top, *bottom;
282 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
284 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
288 vt102_clip_cursor (v, v->screen_start, v->screen_end);
294 vt102_cursor_carriage_return (VT102 * v)
296 /*FISH*/ v->pos.x = v->top_margin.x;
301 vt102_cursor_advance_line (VT102 * v)
303 int couldscroll = in_margins (v, v->pos);
305 /*have wraped off end of last line in scrolling region */
306 /* (|| not necessary, but shuts compiler up */
307 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
310 vt102_log_line (v, v->pos.y);
311 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
321 vt102_cursor_advance (VT102 * v)
324 if (v->pos.x < v->bottom_margin.x)
336 vt102_do_pending_wrap (VT102 * v)
338 int couldscroll = in_margins (v, v->pos);
339 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
341 if (!v->pending_wrap)
345 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
346 v->pos.y, autowrap, couldscroll);
349 /*End of line but no autowrap, nothing to do*/
353 /*End of screen and not allowed to scroll, nothing to do*/
354 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
359 v->pos.x = v->top_margin.x;
366 vt102_cursor_advance_line (v);
371 vt102_cursor_retard (VT102 * v)
373 if (v->pos.x != v->top_margin.x)
380 vt102_reset_tabs (VT102 * v)
384 memset (v->tabs, 0, sizeof (v->tabs));
386 for (i = 0; i < VT102_COLS; i += 8)
392 vt102_cursor_advance_tab (VT102 * v)
394 if (v->pos.x == v->bottom_margin.x)
396 while (v->pos.x < v->bottom_margin.x)
399 if (v->tabs[v->pos.x])
405 vt102_cursor_home (VT102 * v)
407 v->pos = v->top_margin;
408 vt102_cursor_normalize (v);
413 vt102_cursor_absolute (VT102 * v, int x, int y)
415 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
417 v->pos.x = x + v->top_margin.x;
418 v->pos.y = y + v->top_margin.x;
425 vt102_cursor_normalize (v);
429 vt102_cursor_relative (VT102 * v, int x, int y)
433 vt102_cursor_normalize (v);
440 vt102_delete_from_line (VT102 * v, CRT_Pos p)
442 int n = v->bottom_margin.x - p.x;
450 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
451 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
454 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
455 /*But not attr due to vt102 bug*/
459 vt102_insert_into_line (VT102 * v, CRT_Pos p)
461 int n = v->bottom_margin.x - p.x;
469 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
470 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
473 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
474 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
475 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
481 vt102_change_mode (VT102 * v, int private, char *ns, int set)
497 if (m >= VT102_NMODES)
502 v->private_modes[m] = set;
505 case VT102_PRIVATE_MODE_CURSOR_MODE:
506 if (v->application_keypad_mode)
507 v->private_modes[m] = 0;
509 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
510 v->private_modes[m], set);
513 case VT102_PRIVATE_MODE_ORIGIN_MODE:
514 vt102_cursor_home (v);
523 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
528 vt102_parse_mode_string (VT102 * v, char *buf, int len)
531 char last = buf[len - 1];
535 memset (num, 0, sizeof (num));
536 o = sizeof (num) - 1;
554 vt102_change_mode (v, private, &num[o], last == 'h');
555 memset (num, 0, sizeof (num));
556 o = sizeof (num) - 1;
571 vt102_change_mode (v, private, &num[o], last == 'h');
577 vt102_change_attr (VT102 * v, char *na)
594 v->attr = CRT_ATTR_NORMAL;
595 v->color = CRT_COLOR_NORMAL;
598 v->attr |= CRT_ATTR_BOLD;
602 v->attr &= ~CRT_ATTR_BOLD;
605 v->attr |= CRT_ATTR_UNDERLINE;
608 v->attr &= ~CRT_ATTR_UNDERLINE;
611 v->attr |= CRT_ATTR_BLINK;
614 v->attr &= ~CRT_ATTR_BLINK;
617 v->attr |= CRT_ATTR_REVERSE;
620 v->attr &= ~CRT_ATTR_REVERSE;
630 v->color &= ~CRT_COLOR_FG_MASK;
631 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
641 v->color &= ~CRT_COLOR_FG_MASK;
644 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
649 v->color &= ~CRT_COLOR_FG_MASK;
651 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
661 v->color &= ~CRT_COLOR_BG_MASK;
662 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
672 v->color &= ~CRT_COLOR_BG_MASK;
675 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
680 v->color &= ~CRT_COLOR_BG_MASK;
682 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
688 fprintf (stderr, "unhandled SGR %d\n", a);
696 vt102_parse_attr_string (VT102 * v, char *buf, int len)
699 char last = buf[len - 1];
703 memset (num, 0, sizeof (num));
704 o = sizeof (num) - 1;
715 vt102_change_attr (v, &num[o]);
716 memset (num, 0, sizeof (num));
717 o = sizeof (num) - 1;
731 vt102_change_attr (v, &num[o]);
735 vt102_save_state (VT102 * v)
737 v->saved.pos = v->pos;
738 v->saved.attr = v->attr;
739 v->saved.color = v->color;
740 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
744 vt102_restore_state (VT102 * v)
746 v->pos = v->saved.pos;
747 v->attr = v->saved.attr;
748 v->color = v->saved.color;
749 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
750 vt102_cursor_normalize (v);
755 vt102_scs (VT102 * c, int g, int s)
757 /*Ignoring charsets*/
761 vt102_parse_esc (VT102 * v, int c)
766 if (v->pos.y == v->bottom_margin.y)
768 vt102_log_line (v, v->pos.y);
769 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
773 vt102_cursor_relative (v, 0, 1);
780 if (v->pos.y == v->top_margin.y)
782 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
786 vt102_cursor_relative (v, 0, -1);
790 vt102_save_state (v);
793 vt102_restore_state (v);
796 v->application_keypad_mode = 1;
799 v->application_keypad_mode = 0;
803 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
810 vt102_parse_csi (VT102 * v, char *buf, int len)
839 vt102_cursor_relative (v, 0, -narg);
842 vt102_cursor_relative (v, 0, narg);
845 vt102_cursor_relative (v, narg, 0);
848 vt102_cursor_relative (v, -narg, 0);
851 vt102_cursor_relative (v, 0, narg);
852 vt102_cursor_carriage_return (v);
855 vt102_cursor_relative (v, 0, -narg);
856 vt102_cursor_carriage_return (v);
859 vt102_cursor_absolute (v, narg - 1, v->pos.y);
868 ptr = index (arg, ';');
870 x = atoi (ptr + 1) - 1;
874 vt102_cursor_absolute (v, x, y);
881 crt_erase (&v->crt, v->pos, v->screen_end, 1);
884 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
890 CRT_Pos ls = { 0, v->pos.y };
891 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
893 narg = 0; /*Different default */
898 crt_erase (&v->crt, v->pos, le, 1);
901 crt_erase (&v->crt, ls, v->pos, 1);
904 crt_erase (&v->crt, ls, le, 1);
912 vt102_delete_from_line (v, v->pos);
915 if ((v->pos.y >= v->top_margin.y)
916 && (v->pos.y <= v->bottom_margin.y))
919 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
924 if ((v->pos.y >= v->top_margin.y)
925 && (v->pos.y <= v->bottom_margin.y))
928 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
934 narg = 0; /*Different default */
939 v->tabs[v->pos.x] = 0;
942 memset (v->tabs, 0, sizeof (v->tabs));
949 vt102_parse_mode_string (v, &buf[1], len - 1);
953 vt102_parse_attr_string (v, &buf[1], len - 1);
956 v->top_margin = v->screen_start;
957 v->bottom_margin = v->screen_end;
959 if ((len > 2) && (ptr = index (arg, ';')))
962 v->top_margin.y = narg - 1;
963 v->bottom_margin.y = atoi (ptr) - 1;
966 if (v->top_margin.y < v->screen_start.y)
967 v->top_margin.y = v->screen_start.y;
968 if (v->top_margin.y > v->screen_end.y)
969 v->top_margin.y = v->screen_end.y;
970 if (v->bottom_margin.y < v->screen_start.y)
971 v->bottom_margin.y = v->screen_start.y;
972 if (v->bottom_margin.y > v->screen_end.y)
973 v->bottom_margin.y = v->screen_end.y;
975 vt102_cursor_home (v);
978 v->saved.pos = v->pos;
981 v->pos = v->saved.pos;
982 vt102_cursor_normalize (v);
988 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
995 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1005 vt102_status_line (VT102 * v, char *str)
1008 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1012 ca->attr = CRT_ATTR_REVERSE;
1013 ca->color = CRT_COLOR_NORMAL;
1024 vt102_parse_char (VT102 * v, int c, TTY * tty)
1026 VT102_parser *p = &v->parser;
1030 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1031 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1035 p->csi_buf[p->csi_ptr++] = c;
1036 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
1038 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
1042 else if (p->in_escape)
1044 if (csi_starter (c))
1047 p->csi_buf[p->csi_ptr++] = c;
1051 else if (scs_starter (c))
1059 vt102_parse_esc (v, c);
1064 vt102_scs (v, p->in_scs, c);
1079 tty_write (tty, "vt102", 5);
1085 vt102_cursor_retard (v);
1088 vt102_cursor_advance_tab (v);
1093 vt102_cursor_advance_line (v);
1094 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1097 vt102_cursor_carriage_return (v);
1101 /*Ignoring charsets */
1105 /*Ignoring charsets */
1128 /*regular character */ default:
1129 vt102_do_pending_wrap (v);
1131 if (v->modes[VT102_MODE_INSERT])
1132 vt102_insert_into_line (v, v->pos);
1134 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1135 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1136 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1137 vt102_cursor_advance (v);
1141 v->crt.pos = v->pos;
1142 v->crt.hide_cursor =
1143 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1145 if (v->current_line.y != v->pos.y)
1147 vt102_log_line (v, v->current_line.y);
1148 v->current_line = v->pos;
1151 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1154 vt102_parse (VT102 * v, char *buf, int len, TTY * t)
1157 vt102_parse_char (v, *(buf++), t);
1162 vt102_parser_reset (VT102_parser * p)
1172 vt102_send (VT102 * v, uint8_t key, TTY * tty)
1176 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1178 if ((key > 31) && (key < 127))
1180 tty_write (tty, &key, 1);
1199 tty_write (tty, &key, 1);
1202 tty_write (tty, &key, 1);
1203 if (v->modes[VT102_MODE_NEWLINE_MODE])
1206 tty_write (tty, &c, 1);
1222 tty_write (tty, &key, 1);
1230 tty_write (tty, &key, 1);
1241 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1243 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1244 tty_write (tty, &buf, sizeof (buf));
1248 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1249 tty_write (tty, &buf, sizeof (buf));
1267 if (v->application_keypad_mode)
1269 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1270 tty_write (tty, &buf, sizeof (buf));
1274 static char kpoff[KEY_NUM] = {
1293 tty_write (tty, &kpoff[key], 1);
1297 if (v->application_keypad_mode)
1299 uint8_t buf[] = { 033, 'O', 'M' };
1300 tty_write (tty, &buf, sizeof (buf));
1305 tty_write (tty, &c, 1);
1306 if (v->modes[VT102_MODE_NEWLINE_MODE])
1309 tty_write (tty, &c, 1);
1318 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1319 tty_write (tty, &buf, sizeof (buf));
1327 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1328 tty_write (tty, &buf, sizeof (buf));
1336 vt102_reset (VT102 * v)
1338 VT102_parser *p = &v->parser;
1340 vt102_parser_reset (p);
1343 v->attr = CRT_ATTR_NORMAL;
1344 v->color = CRT_COLOR_NORMAL;
1346 v->application_keypad_mode = 0;
1348 v->current_line = v->pos;
1349 v->pending_wrap = 0;
1351 v->screen_start.x = 0;
1352 v->screen_start.y = 0;
1353 v->screen_end.x = VT102_COLS - 1;
1354 v->screen_end.y = VT102_ROWS - 1;
1356 v->top_margin = v->screen_start;
1357 v->bottom_margin = v->screen_end;
1359 memset (v->modes, 0, VT102_NMODES);
1360 memset (v->private_modes, 0, VT102_NMODES);
1362 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1363 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1364 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1365 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1367 vt102_cursor_home (v);
1368 vt102_reset_tabs (v);
1369 v->current_line = v->pos;
1371 vt102_save_state (v);
1373 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1378 vt102_dispatch (VT102 * v, TTY * tty)
1383 red = tty_read (tty, buf, sizeof (buf));
1391 vt102_parse (v, buf, red, tty);
1401 v = (VT102 *) malloc (sizeof (VT102));
1410 vt102_free (VT102 * v)