4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.23 2008/02/07 13:26:35 james
14 * *** empty log message ***
16 * Revision 1.22 2008/02/07 13:22:51 james
17 * *** empty log message ***
19 * Revision 1.21 2008/02/07 12:21:16 james
20 * *** empty log message ***
22 * Revision 1.20 2008/02/07 12:16:04 james
23 * *** empty log message ***
25 * Revision 1.19 2008/02/07 11:27:02 james
26 * *** empty log message ***
28 * Revision 1.18 2008/02/07 01:59:25 james
29 * *** empty log message ***
31 * Revision 1.17 2008/02/07 01:58:28 james
32 * *** empty log message ***
34 * Revision 1.16 2008/02/07 01:57:46 james
35 * *** empty log message ***
37 * Revision 1.15 2008/02/07 00:43:27 james
38 * *** empty log message ***
40 * Revision 1.14 2008/02/07 00:40:23 james
41 * *** empty log message ***
43 * Revision 1.13 2008/02/07 00:39:59 james
44 * *** empty log message ***
46 * Revision 1.12 2008/02/07 00:39:13 james
47 * *** empty log message ***
49 * Revision 1.11 2008/02/06 20:26:58 james
50 * *** empty log message ***
52 * Revision 1.10 2008/02/06 17:53:28 james
53 * *** empty log message ***
55 * Revision 1.9 2008/02/06 15:53:22 james
56 * *** empty log message ***
58 * Revision 1.8 2008/02/06 11:49:47 james
59 * *** empty log message ***
61 * Revision 1.7 2008/02/06 11:30:37 james
62 * *** empty log message ***
64 * Revision 1.6 2008/02/05 01:11:46 james
65 * *** empty log message ***
67 * Revision 1.5 2008/02/04 20:23:55 james
68 * *** empty log message ***
70 * Revision 1.4 2008/02/04 05:45:55 james
73 * Revision 1.3 2008/02/04 02:05:06 james
74 * *** empty log message ***
76 * Revision 1.2 2008/02/04 01:32:39 james
77 * *** empty log message ***
79 * Revision 1.1 2008/02/03 23:36:41 james
80 * *** empty log message ***
89 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
91 vt100|vt100-am|dec vt100 (w/advanced video):\
93 :co#80:it#8:li#24:vt#3:\
94 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
96 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
97 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
98 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
99 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
100 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
101 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
102 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
103 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
104 :us=\E[4m:tc=vt100+fnkeys:
106 vt100+fnkeys|dec vt100 numeric keypad:\
107 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
110 vt100+pfkeys|dec vt100 numeric keypad:\
111 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
113 vt100+keypad|dec vt100 numeric keypad no fkeys:\
114 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
119 so the parser needs to be able to at least do
187 if ((c >= 'a') && (c <= 'z'))
189 if ((c >= 'A') && (c <= 'Z'))
218 in_margins (VT102 * v, CRT_Pos p)
220 if (v->pos.x < v->top_margin.x)
222 if (v->pos.y < v->top_margin.y)
225 if (v->pos.x > v->bottom_margin.x)
227 if (v->pos.y > v->bottom_margin.y)
234 vt102_log_line (VT102 * v, int line)
237 CRT_Pos e = { VT102_COLS - 1, line };
238 CRT_Pos p = { 0, line };
241 log = fopen ("log", "a+");
247 for (; e.x > 0; --e.x)
249 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
253 for (; p.x <= e.x; ++p.x)
255 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
266 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
281 vt102_cursor_normalize (VT102 * v)
283 CRT_Pos *top, *bottom;
285 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
287 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
291 vt102_clip_cursor (v, v->screen_start, v->screen_end);
297 vt102_cursor_carriage_return (VT102 * v)
299 /*FISH*/ v->pos.x = v->top_margin.x;
304 vt102_cursor_advance_line (VT102 * v)
306 int couldscroll = in_margins (v, v->pos);
308 /*have wraped off end of last line in scrolling region */
309 /* (|| not necessary, but shuts compiler up */
310 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
313 vt102_log_line (v, v->pos.y);
314 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
324 vt102_cursor_advance (VT102 * v)
327 if (v->pos.x < v->bottom_margin.x)
339 vt102_do_pending_wrap (VT102 * v)
341 int couldscroll = in_margins (v, v->pos);
342 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
344 if (!v->pending_wrap)
348 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
349 v->pos.y, autowrap, couldscroll);
352 /*End of line but no autowrap, nothing to do*/
356 /*End of screen and not allowed to scroll, nothing to do*/
357 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
362 v->pos.x = v->top_margin.x;
369 vt102_cursor_advance_line (v);
374 vt102_cursor_retard (VT102 * v)
376 if (v->pos.x != v->top_margin.x)
383 vt102_reset_tabs (VT102 * v)
387 memset (v->tabs, 0, sizeof (v->tabs));
389 for (i = 0; i < VT102_COLS; i += 8)
395 vt102_cursor_advance_tab (VT102 * v)
397 if (v->pos.x == v->bottom_margin.x)
399 while (v->pos.x < v->bottom_margin.x)
402 if (v->tabs[v->pos.x])
408 vt102_cursor_home (VT102 * v)
410 v->pos = v->top_margin;
411 vt102_cursor_normalize (v);
416 vt102_cursor_absolute (VT102 * v, int x, int y)
418 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
420 v->pos.x = x + v->top_margin.x;
421 v->pos.y = y + v->top_margin.x;
428 vt102_cursor_normalize (v);
432 vt102_cursor_relative (VT102 * v, int x, int y)
436 vt102_cursor_normalize (v);
443 vt102_delete_from_line (VT102 * v, CRT_Pos p)
445 int n = v->bottom_margin.x - p.x;
453 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
454 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
457 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
458 /*But not attr due to vt102 bug*/
462 vt102_insert_into_line (VT102 * v, CRT_Pos p)
464 int n = v->bottom_margin.x - p.x;
472 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
473 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
476 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
477 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
478 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
484 vt102_change_mode (VT102 * v, int private, char *ns, int set)
500 if (m >= VT102_NMODES)
505 v->private_modes[m] = set;
508 case VT102_PRIVATE_MODE_CURSOR_MODE:
509 if (v->application_keypad_mode)
510 v->private_modes[m] = 0;
512 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
513 v->private_modes[m], set);
516 case VT102_PRIVATE_MODE_ORIGIN_MODE:
517 vt102_cursor_home (v);
526 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
531 vt102_parse_mode_string (VT102 * v, char *buf, int len)
534 char last = buf[len - 1];
538 memset (num, 0, sizeof (num));
539 o = sizeof (num) - 1;
557 vt102_change_mode (v, private, &num[o], last == 'h');
558 memset (num, 0, sizeof (num));
559 o = sizeof (num) - 1;
574 vt102_change_mode (v, private, &num[o], last == 'h');
580 vt102_change_attr (VT102 * v, char *na)
597 v->attr = CRT_ATTR_NORMAL;
598 v->color = CRT_COLOR_NORMAL;
601 v->attr |= CRT_ATTR_BOLD;
605 v->attr &= ~CRT_ATTR_BOLD;
608 v->attr |= CRT_ATTR_UNDERLINE;
611 v->attr &= ~CRT_ATTR_UNDERLINE;
614 v->attr |= CRT_ATTR_BLINK;
617 v->attr &= ~CRT_ATTR_BLINK;
620 v->attr |= CRT_ATTR_REVERSE;
623 v->attr &= ~CRT_ATTR_REVERSE;
633 v->color &= ~CRT_COLOR_FG_MASK;
634 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
644 v->color &= ~CRT_COLOR_FG_MASK;
647 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
652 v->color &= ~CRT_COLOR_FG_MASK;
654 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
664 v->color &= ~CRT_COLOR_BG_MASK;
665 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
675 v->color &= ~CRT_COLOR_BG_MASK;
678 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
683 v->color &= ~CRT_COLOR_BG_MASK;
685 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
691 fprintf (stderr, "unhandled SGR %d\n", a);
699 vt102_parse_attr_string (VT102 * v, char *buf, int len)
702 char last = buf[len - 1];
706 memset (num, 0, sizeof (num));
707 o = sizeof (num) - 1;
718 vt102_change_attr (v, &num[o]);
719 memset (num, 0, sizeof (num));
720 o = sizeof (num) - 1;
734 vt102_change_attr (v, &num[o]);
738 vt102_save_state (VT102 * v)
740 v->saved.pos = v->pos;
741 v->saved.attr = v->attr;
742 v->saved.color = v->color;
743 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
747 vt102_restore_state (VT102 * v)
749 v->pos = v->saved.pos;
750 v->attr = v->saved.attr;
751 v->color = v->saved.color;
752 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
753 vt102_cursor_normalize (v);
758 vt102_scs (VT102 * c, int g, int s)
760 /*Ignoring charsets*/
764 vt102_parse_esc (VT102 * v, int c)
769 if (v->pos.y == v->bottom_margin.y)
771 vt102_log_line (v, v->pos.y);
772 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
776 vt102_cursor_relative (v, 0, 1);
783 if (v->pos.y == v->top_margin.y)
785 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
789 vt102_cursor_relative (v, 0, -1);
793 vt102_save_state (v);
796 vt102_restore_state (v);
799 v->application_keypad_mode = 1;
802 v->application_keypad_mode = 0;
806 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
813 vt102_parse_csi (VT102 * v, char *buf, int len)
842 vt102_cursor_relative (v, 0, -narg);
845 vt102_cursor_relative (v, 0, narg);
848 vt102_cursor_relative (v, narg, 0);
851 vt102_cursor_relative (v, -narg, 0);
854 vt102_cursor_relative (v, 0, narg);
855 vt102_cursor_carriage_return (v);
858 vt102_cursor_relative (v, 0, -narg);
859 vt102_cursor_carriage_return (v);
862 vt102_cursor_absolute (v, narg - 1, v->pos.y);
871 ptr = index (arg, ';');
873 x = atoi (ptr + 1) - 1;
877 vt102_cursor_absolute (v, x, y);
884 crt_erase (&v->crt, v->pos, v->screen_end, 1);
887 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
893 CRT_Pos ls = { 0, v->pos.y };
894 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
896 narg = 0; /*Different default */
901 crt_erase (&v->crt, v->pos, le, 1);
904 crt_erase (&v->crt, ls, v->pos, 1);
907 crt_erase (&v->crt, ls, le, 1);
915 vt102_delete_from_line (v, v->pos);
918 if ((v->pos.y >= v->top_margin.y)
919 && (v->pos.y <= v->bottom_margin.y))
922 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
927 if ((v->pos.y >= v->top_margin.y)
928 && (v->pos.y <= v->bottom_margin.y))
931 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
937 narg = 0; /*Different default */
942 v->tabs[v->pos.x] = 0;
945 memset (v->tabs, 0, sizeof (v->tabs));
952 vt102_parse_mode_string (v, &buf[1], len - 1);
956 vt102_parse_attr_string (v, &buf[1], len - 1);
959 v->top_margin = v->screen_start;
960 v->bottom_margin = v->screen_end;
962 if ((len > 2) && (ptr = index (arg, ';')))
965 v->top_margin.y = narg - 1;
966 v->bottom_margin.y = atoi (ptr) - 1;
969 if (v->top_margin.y < v->screen_start.y)
970 v->top_margin.y = v->screen_start.y;
971 if (v->top_margin.y > v->screen_end.y)
972 v->top_margin.y = v->screen_end.y;
973 if (v->bottom_margin.y < v->screen_start.y)
974 v->bottom_margin.y = v->screen_start.y;
975 if (v->bottom_margin.y > v->screen_end.y)
976 v->bottom_margin.y = v->screen_end.y;
978 vt102_cursor_home (v);
981 v->saved.pos = v->pos;
984 v->pos = v->saved.pos;
985 vt102_cursor_normalize (v);
991 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
998 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1008 vt102_status_line (VT102 * v, char *str)
1011 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1015 ca->attr = CRT_ATTR_REVERSE;
1016 ca->color = CRT_COLOR_NORMAL;
1027 vt102_parse_char (VT102 * v, int c, TTY * tty)
1029 VT102_parser *p = &v->parser;
1033 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1034 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1038 p->csi_buf[p->csi_ptr++] = c;
1039 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
1041 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
1045 else if (p->in_escape)
1047 if (csi_starter (c))
1050 p->csi_buf[p->csi_ptr++] = c;
1054 else if (scs_starter (c))
1062 vt102_parse_esc (v, c);
1067 vt102_scs (v, p->in_scs, c);
1082 tty_write (tty, "vt102", 5);
1088 vt102_cursor_retard (v);
1091 vt102_cursor_advance_tab (v);
1096 vt102_cursor_advance_line (v);
1097 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1100 vt102_cursor_carriage_return (v);
1104 /*Ignoring charsets */
1108 /*Ignoring charsets */
1131 /*regular character */ default:
1132 vt102_do_pending_wrap (v);
1134 if (v->modes[VT102_MODE_INSERT])
1135 vt102_insert_into_line (v, v->pos);
1137 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1138 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1139 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1140 vt102_cursor_advance (v);
1144 v->crt.pos = v->pos;
1145 v->crt.hide_cursor =
1146 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1148 if (v->current_line.y != v->pos.y)
1150 vt102_log_line (v, v->current_line.y);
1151 v->current_line = v->pos;
1154 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1157 vt102_parse (VT102 * v, char *buf, int len, TTY * t)
1160 vt102_parse_char (v, *(buf++), t);
1165 vt102_parser_reset (VT102_parser * p)
1175 vt102_send (VT102 * v, uint8_t key, TTY * tty)
1179 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1181 if ((key > 31) && (key < 127))
1183 tty_write (tty, &key, 1);
1202 tty_write (tty, &key, 1);
1205 tty_write (tty, &key, 1);
1206 if (v->modes[VT102_MODE_NEWLINE_MODE])
1209 tty_write (tty, &c, 1);
1225 tty_write (tty, &key, 1);
1233 tty_write (tty, &key, 1);
1244 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1246 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1247 tty_write (tty, &buf, sizeof (buf));
1251 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1252 tty_write (tty, &buf, sizeof (buf));
1270 if (v->application_keypad_mode)
1272 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1273 tty_write (tty, &buf, sizeof (buf));
1277 static char kpoff[KEY_NUM] = {
1296 tty_write (tty, &kpoff[key], 1);
1300 if (v->application_keypad_mode)
1302 uint8_t buf[] = { 033, 'O', 'M' };
1303 tty_write (tty, &buf, sizeof (buf));
1308 tty_write (tty, &c, 1);
1309 if (v->modes[VT102_MODE_NEWLINE_MODE])
1312 tty_write (tty, &c, 1);
1321 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1322 tty_write (tty, &buf, sizeof (buf));
1330 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1331 tty_write (tty, &buf, sizeof (buf));
1339 vt102_reset (VT102 * v)
1341 VT102_parser *p = &v->parser;
1343 vt102_parser_reset (p);
1346 v->attr = CRT_ATTR_NORMAL;
1347 v->color = CRT_COLOR_NORMAL;
1349 v->application_keypad_mode = 0;
1351 v->current_line = v->pos;
1352 v->pending_wrap = 0;
1354 v->screen_start.x = 0;
1355 v->screen_start.y = 0;
1356 v->screen_end.x = VT102_COLS - 1;
1357 v->screen_end.y = VT102_ROWS - 1;
1359 v->top_margin = v->screen_start;
1360 v->bottom_margin = v->screen_end;
1362 memset (v->modes, 0, VT102_NMODES);
1363 memset (v->private_modes, 0, VT102_NMODES);
1365 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1366 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1367 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1368 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1370 vt102_cursor_home (v);
1371 vt102_reset_tabs (v);
1372 v->current_line = v->pos;
1374 vt102_save_state (v);
1376 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1381 vt102_dispatch (VT102 * v, TTY * tty)
1386 red = tty_read (tty, buf, sizeof (buf));
1394 vt102_parse (v, buf, red, tty);
1401 vt102_dispatch_one (VT102 * v, TTY * tty)
1406 red = tty_read (tty, &buf, sizeof (buf));
1413 vt102_parse_char (v, buf, tty);
1423 v = (VT102 *) malloc (sizeof (VT102));
1432 vt102_free (VT102 * v)