4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.26 2008/02/14 01:55:57 james
14 * *** empty log message ***
16 * Revision 1.25 2008/02/13 16:57:29 james
17 * *** empty log message ***
19 * Revision 1.24 2008/02/13 09:12:21 james
20 * *** empty log message ***
22 * Revision 1.23 2008/02/07 13:26:35 james
23 * *** empty log message ***
25 * Revision 1.22 2008/02/07 13:22:51 james
26 * *** empty log message ***
28 * Revision 1.21 2008/02/07 12:21:16 james
29 * *** empty log message ***
31 * Revision 1.20 2008/02/07 12:16:04 james
32 * *** empty log message ***
34 * Revision 1.19 2008/02/07 11:27:02 james
35 * *** empty log message ***
37 * Revision 1.18 2008/02/07 01:59:25 james
38 * *** empty log message ***
40 * Revision 1.17 2008/02/07 01:58:28 james
41 * *** empty log message ***
43 * Revision 1.16 2008/02/07 01:57:46 james
44 * *** empty log message ***
46 * Revision 1.15 2008/02/07 00:43:27 james
47 * *** empty log message ***
49 * Revision 1.14 2008/02/07 00:40:23 james
50 * *** empty log message ***
52 * Revision 1.13 2008/02/07 00:39:59 james
53 * *** empty log message ***
55 * Revision 1.12 2008/02/07 00:39:13 james
56 * *** empty log message ***
58 * Revision 1.11 2008/02/06 20:26:58 james
59 * *** empty log message ***
61 * Revision 1.10 2008/02/06 17:53:28 james
62 * *** empty log message ***
64 * Revision 1.9 2008/02/06 15:53:22 james
65 * *** empty log message ***
67 * Revision 1.8 2008/02/06 11:49:47 james
68 * *** empty log message ***
70 * Revision 1.7 2008/02/06 11:30:37 james
71 * *** empty log message ***
73 * Revision 1.6 2008/02/05 01:11:46 james
74 * *** empty log message ***
76 * Revision 1.5 2008/02/04 20:23:55 james
77 * *** empty log message ***
79 * Revision 1.4 2008/02/04 05:45:55 james
82 * Revision 1.3 2008/02/04 02:05:06 james
83 * *** empty log message ***
85 * Revision 1.2 2008/02/04 01:32:39 james
86 * *** empty log message ***
88 * Revision 1.1 2008/02/03 23:36:41 james
89 * *** empty log message ***
98 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
100 vt100|vt100-am|dec vt100 (w/advanced video):\
102 :co#80:it#8:li#24:vt#3:\
103 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
105 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
106 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
107 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
108 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
109 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
110 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
111 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
112 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
113 :us=\E[4m:tc=vt100+fnkeys:
115 vt100+fnkeys|dec vt100 numeric keypad:\
116 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
119 vt100+pfkeys|dec vt100 numeric keypad:\
120 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
122 vt100+keypad|dec vt100 numeric keypad no fkeys:\
123 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
128 so the parser needs to be able to at least do
196 if ((c >= 'a') && (c <= 'z'))
198 if ((c >= 'A') && (c <= 'Z'))
227 in_margins (VT102 * v, CRT_Pos p)
229 if (v->pos.x < v->top_margin.x)
231 if (v->pos.y < v->top_margin.y)
234 if (v->pos.x > v->bottom_margin.x)
236 if (v->pos.y > v->bottom_margin.y)
243 vt102_log_line (Context * c, int line)
245 CRT_Pos e = { VT102_COLS - 1, line };
246 CRT_Pos p = { 0, line };
247 char logbuf[VT102_COLS + 1];
253 for (; e.x > 0; --e.x)
255 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
259 for (; p.x <= e.x; ++p.x)
261 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
270 c->l->log (c->l, logbuf);
273 /*Called for every upward scroll with same args*/
275 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
277 /*Only log if it scrolls off the top*/
282 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
286 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
301 vt102_cursor_normalize (VT102 * v)
303 CRT_Pos *top, *bottom;
305 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
307 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
311 vt102_clip_cursor (v, v->screen_start, v->screen_end);
317 vt102_cursor_carriage_return (VT102 * v)
319 /*FISH*/ v->pos.x = v->top_margin.x;
324 vt102_cursor_advance_line (Context * c)
327 int couldscroll = in_margins (v, v->pos);
329 /*have wraped off end of last line in scrolling region */
330 /* (|| not necessary, but shuts compiler up */
331 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
334 vt102_log_line (c, v->pos.y);
336 vt102_history (c, v->top_margin, v->bottom_margin);
338 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
348 vt102_cursor_advance (Context * c)
352 if (v->pos.x < v->bottom_margin.x)
364 vt102_do_pending_wrap (Context * c)
367 int couldscroll = in_margins (v, v->pos);
368 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
370 if (!v->pending_wrap)
374 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
375 v->pos.y, autowrap, couldscroll);
378 /*End of line but no autowrap, nothing to do*/
382 /*End of screen and not allowed to scroll, nothing to do*/
383 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
388 v->pos.x = v->top_margin.x;
395 vt102_cursor_advance_line (c);
400 vt102_cursor_retard (VT102 * v)
402 if (v->pos.x != v->top_margin.x)
409 vt102_reset_tabs (VT102 * v)
413 memset (v->tabs, 0, sizeof (v->tabs));
415 for (i = 0; i < VT102_COLS; i += 8)
421 vt102_cursor_advance_tab (VT102 * v)
423 if (v->pos.x == v->bottom_margin.x)
425 while (v->pos.x < v->bottom_margin.x)
428 if (v->tabs[v->pos.x])
434 vt102_cursor_home (VT102 * v)
436 v->pos = v->top_margin;
437 vt102_cursor_normalize (v);
442 vt102_cursor_absolute (VT102 * v, int x, int y)
444 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
446 v->pos.x = x + v->top_margin.x;
447 v->pos.y = y + v->top_margin.x;
454 vt102_cursor_normalize (v);
458 vt102_cursor_relative (VT102 * v, int x, int y)
462 vt102_cursor_normalize (v);
469 vt102_delete_from_line (VT102 * v, CRT_Pos p)
471 int n = v->bottom_margin.x - p.x;
479 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
480 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
483 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
484 /*But not attr due to vt102 bug*/
488 vt102_insert_into_line (VT102 * v, CRT_Pos p)
490 int n = v->bottom_margin.x - p.x;
498 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
499 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
502 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
503 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
504 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
510 vt102_change_mode (VT102 * v, int private, char *ns, int set)
526 if (m >= VT102_NMODES)
531 v->private_modes[m] = set;
534 case VT102_PRIVATE_MODE_CURSOR_MODE:
535 if (v->application_keypad_mode)
536 v->private_modes[m] = 0;
538 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
539 v->private_modes[m], set);
542 case VT102_PRIVATE_MODE_ORIGIN_MODE:
543 vt102_cursor_home (v);
552 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
557 vt102_parse_mode_string (VT102 * v, char *buf, int len)
560 char last = buf[len - 1];
564 memset (num, 0, sizeof (num));
565 o = sizeof (num) - 1;
583 vt102_change_mode (v, private, &num[o], last == 'h');
584 memset (num, 0, sizeof (num));
585 o = sizeof (num) - 1;
600 vt102_change_mode (v, private, &num[o], last == 'h');
606 vt102_change_attr (VT102 * v, char *na)
623 v->attr = CRT_ATTR_NORMAL;
624 v->color = CRT_COLOR_NORMAL;
627 v->attr |= CRT_ATTR_BOLD;
631 v->attr &= ~CRT_ATTR_BOLD;
634 v->attr |= CRT_ATTR_UNDERLINE;
637 v->attr &= ~CRT_ATTR_UNDERLINE;
640 v->attr |= CRT_ATTR_BLINK;
643 v->attr &= ~CRT_ATTR_BLINK;
646 v->attr |= CRT_ATTR_REVERSE;
649 v->attr &= ~CRT_ATTR_REVERSE;
659 v->color &= ~CRT_COLOR_FG_MASK;
660 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
670 v->color &= ~CRT_COLOR_FG_MASK;
673 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
678 v->color &= ~CRT_COLOR_FG_MASK;
680 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
690 v->color &= ~CRT_COLOR_BG_MASK;
691 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
701 v->color &= ~CRT_COLOR_BG_MASK;
704 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
709 v->color &= ~CRT_COLOR_BG_MASK;
711 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
717 fprintf (stderr, "unhandled SGR %d\n", a);
725 vt102_parse_attr_string (VT102 * v, char *buf, int len)
728 char last = buf[len - 1];
732 memset (num, 0, sizeof (num));
733 o = sizeof (num) - 1;
744 vt102_change_attr (v, &num[o]);
745 memset (num, 0, sizeof (num));
746 o = sizeof (num) - 1;
760 vt102_change_attr (v, &num[o]);
764 vt102_save_state (VT102 * v)
766 v->saved.pos = v->pos;
767 v->saved.attr = v->attr;
768 v->saved.color = v->color;
769 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
773 vt102_restore_state (VT102 * v)
775 v->pos = v->saved.pos;
776 v->attr = v->saved.attr;
777 v->color = v->saved.color;
778 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
779 vt102_cursor_normalize (v);
784 vt102_scs (Context * c, int g, int s)
786 /*Ignoring charsets*/
790 vt102_parse_esc (Context * c, int ch)
796 if (v->pos.y == v->bottom_margin.y)
798 vt102_log_line (c, v->pos.y);
799 vt102_history (c, v->top_margin, v->bottom_margin);
800 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
804 vt102_cursor_relative (v, 0, 1);
811 if (v->pos.y == v->top_margin.y)
813 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
817 vt102_cursor_relative (v, 0, -1);
821 vt102_save_state (v);
824 vt102_restore_state (v);
827 v->application_keypad_mode = 1;
830 v->application_keypad_mode = 0;
834 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
835 (ch < 32) ? '?' : ch);
841 vt102_parse_csi (Context * c, char *buf, int len)
872 vt102_cursor_relative (v, 0, -narg);
875 vt102_cursor_relative (v, 0, narg);
878 vt102_cursor_relative (v, narg, 0);
881 vt102_cursor_relative (v, -narg, 0);
884 vt102_cursor_relative (v, 0, narg);
885 vt102_cursor_carriage_return (v);
888 vt102_cursor_relative (v, 0, -narg);
889 vt102_cursor_carriage_return (v);
892 vt102_cursor_absolute (v, narg - 1, v->pos.y);
901 ptr = index (arg, ';');
903 x = atoi (ptr + 1) - 1;
907 vt102_cursor_absolute (v, x, y);
914 crt_erase (&v->crt, v->pos, v->screen_end, 1);
917 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
923 CRT_Pos ls = { 0, v->pos.y };
924 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
926 narg = 0; /*Different default */
931 crt_erase (&v->crt, v->pos, le, 1);
934 crt_erase (&v->crt, ls, v->pos, 1);
937 crt_erase (&v->crt, ls, le, 1);
945 vt102_delete_from_line (v, v->pos);
948 if ((v->pos.y >= v->top_margin.y)
949 && (v->pos.y <= v->bottom_margin.y))
952 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
957 if ((v->pos.y >= v->top_margin.y)
958 && (v->pos.y <= v->bottom_margin.y))
962 vt102_history (c, v->pos, v->bottom_margin);
963 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1);
970 narg = 0; /*Different default */
975 v->tabs[v->pos.x] = 0;
978 memset (v->tabs, 0, sizeof (v->tabs));
985 vt102_parse_mode_string (v, &buf[1], len - 1);
989 vt102_parse_attr_string (v, &buf[1], len - 1);
992 v->top_margin = v->screen_start;
993 v->bottom_margin = v->screen_end;
995 if ((len > 2) && (ptr = index (arg, ';')))
998 v->top_margin.y = narg - 1;
999 v->bottom_margin.y = atoi (ptr) - 1;
1002 if (v->top_margin.y < v->screen_start.y)
1003 v->top_margin.y = v->screen_start.y;
1004 if (v->top_margin.y > v->screen_end.y)
1005 v->top_margin.y = v->screen_end.y;
1006 if (v->bottom_margin.y < v->screen_start.y)
1007 v->bottom_margin.y = v->screen_start.y;
1008 if (v->bottom_margin.y > v->screen_end.y)
1009 v->bottom_margin.y = v->screen_end.y;
1011 vt102_cursor_home (v);
1014 v->saved.pos = v->pos;
1017 v->pos = v->saved.pos;
1018 vt102_cursor_normalize (v);
1019 v->pending_wrap = 0;
1024 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1031 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1041 vt102_status_line (VT102 * v, char *str)
1044 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1048 ca->attr = CRT_ATTR_REVERSE;
1049 ca->color = CRT_COLOR_NORMAL;
1060 vt102_parse_char (Context * c, int ch)
1063 VT102_parser *p = &v->parser;
1066 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1067 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1071 p->csi_buf[p->csi_ptr++] = ch;
1072 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1074 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1078 else if (p->in_escape)
1080 if (csi_starter (ch))
1083 p->csi_buf[p->csi_ptr++] = ch;
1087 else if (scs_starter (ch))
1095 vt102_parse_esc (c, ch);
1100 vt102_scs (c, p->in_scs, ch);
1115 c->t->xmit (c->t, "vt102", 5);
1121 vt102_cursor_retard (c->v);
1124 vt102_cursor_advance_tab (c->v);
1129 vt102_cursor_advance_line (c);
1130 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1133 vt102_cursor_carriage_return (v);
1137 /*Ignoring charsets */
1141 /*Ignoring charsets */
1164 /*regular character */ default:
1165 vt102_do_pending_wrap (c);
1167 if (v->modes[VT102_MODE_INSERT])
1168 vt102_insert_into_line (v, v->pos);
1170 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1171 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1172 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1173 vt102_cursor_advance (c);
1177 v->crt.pos = v->pos;
1178 v->crt.hide_cursor =
1179 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1181 if (v->current_line.y != v->pos.y)
1183 vt102_log_line (c, v->current_line.y);
1184 v->current_line = v->pos;
1187 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1190 vt102_parse (Context * c, char *buf, int len)
1193 vt102_parse_char (c, *(buf++));
1198 vt102_parser_reset (VT102_parser * p)
1208 vt102_send (Context * c, uint8_t key)
1215 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1217 if ((key > 31) && (key < 127))
1219 c->t->xmit (c->t, &key, 1);
1238 c->t->xmit (c->t, &key, 1);
1241 c->t->xmit (c->t, &key, 1);
1242 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1245 c->t->xmit (c->t, &ch, 1);
1261 c->t->xmit (c->t, &key, 1);
1269 c->t->xmit (c->t, &key, 1);
1280 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1282 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1283 c->t->xmit (c->t, &buf, sizeof (buf));
1287 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1288 c->t->xmit (c->t, &buf, sizeof (buf));
1306 if (c->v->application_keypad_mode)
1308 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1309 c->t->xmit (c->t, &buf, sizeof (buf));
1313 static char kpoff[KEY_NUM] = {
1332 c->t->xmit (c->t, &kpoff[key], 1);
1336 if (c->v->application_keypad_mode)
1338 uint8_t buf[] = { 033, 'O', 'M' };
1339 c->t->xmit (c->t, &buf, sizeof (buf));
1344 c->t->xmit (c->t, &ch, 1);
1345 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1348 c->t->xmit (c->t, &ch, 1);
1357 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1358 c->t->xmit (c->t, &buf, sizeof (buf));
1366 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1367 c->t->xmit (c->t, &buf, sizeof (buf));
1375 vt102_reset (VT102 * v)
1377 VT102_parser *p = &v->parser;
1379 vt102_parser_reset (p);
1382 v->attr = CRT_ATTR_NORMAL;
1383 v->color = CRT_COLOR_NORMAL;
1385 v->application_keypad_mode = 0;
1387 v->current_line = v->pos;
1388 v->pending_wrap = 0;
1390 v->screen_start.x = 0;
1391 v->screen_start.y = 0;
1392 v->screen_end.x = VT102_COLS - 1;
1393 v->screen_end.y = VT102_ROWS - 1;
1395 v->top_margin = v->screen_start;
1396 v->bottom_margin = v->screen_end;
1398 memset (v->modes, 0, VT102_NMODES);
1399 memset (v->private_modes, 0, VT102_NMODES);
1401 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1402 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1403 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1404 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1406 vt102_cursor_home (v);
1407 vt102_reset_tabs (v);
1408 v->current_line = v->pos;
1410 vt102_save_state (v);
1412 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1417 vt102_dispatch (Context * c)
1422 red = c->t->recv (c->t, buf, sizeof (buf));
1430 vt102_parse (c, buf, red);
1437 vt102_dispatch_one (Context * c)
1442 red = c->t->recv (c->t, &buf, sizeof (buf));
1449 vt102_parse_char (c, buf);
1459 v = (VT102 *) malloc (sizeof (VT102));
1468 vt102_free (VT102 * v)