4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.24 2008/02/13 09:12:21 james
14 * *** empty log message ***
16 * Revision 1.23 2008/02/07 13:26:35 james
17 * *** empty log message ***
19 * Revision 1.22 2008/02/07 13:22:51 james
20 * *** empty log message ***
22 * Revision 1.21 2008/02/07 12:21:16 james
23 * *** empty log message ***
25 * Revision 1.20 2008/02/07 12:16:04 james
26 * *** empty log message ***
28 * Revision 1.19 2008/02/07 11:27:02 james
29 * *** empty log message ***
31 * Revision 1.18 2008/02/07 01:59:25 james
32 * *** empty log message ***
34 * Revision 1.17 2008/02/07 01:58:28 james
35 * *** empty log message ***
37 * Revision 1.16 2008/02/07 01:57:46 james
38 * *** empty log message ***
40 * Revision 1.15 2008/02/07 00:43:27 james
41 * *** empty log message ***
43 * Revision 1.14 2008/02/07 00:40:23 james
44 * *** empty log message ***
46 * Revision 1.13 2008/02/07 00:39:59 james
47 * *** empty log message ***
49 * Revision 1.12 2008/02/07 00:39:13 james
50 * *** empty log message ***
52 * Revision 1.11 2008/02/06 20:26:58 james
53 * *** empty log message ***
55 * Revision 1.10 2008/02/06 17:53:28 james
56 * *** empty log message ***
58 * Revision 1.9 2008/02/06 15:53:22 james
59 * *** empty log message ***
61 * Revision 1.8 2008/02/06 11:49:47 james
62 * *** empty log message ***
64 * Revision 1.7 2008/02/06 11:30:37 james
65 * *** empty log message ***
67 * Revision 1.6 2008/02/05 01:11:46 james
68 * *** empty log message ***
70 * Revision 1.5 2008/02/04 20:23:55 james
71 * *** empty log message ***
73 * Revision 1.4 2008/02/04 05:45:55 james
76 * Revision 1.3 2008/02/04 02:05:06 james
77 * *** empty log message ***
79 * Revision 1.2 2008/02/04 01:32:39 james
80 * *** empty log message ***
82 * Revision 1.1 2008/02/03 23:36:41 james
83 * *** empty log message ***
92 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
94 vt100|vt100-am|dec vt100 (w/advanced video):\
96 :co#80:it#8:li#24:vt#3:\
97 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
99 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
100 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
101 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
102 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
103 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
104 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
105 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
106 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
107 :us=\E[4m:tc=vt100+fnkeys:
109 vt100+fnkeys|dec vt100 numeric keypad:\
110 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
113 vt100+pfkeys|dec vt100 numeric keypad:\
114 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
116 vt100+keypad|dec vt100 numeric keypad no fkeys:\
117 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
122 so the parser needs to be able to at least do
190 if ((c >= 'a') && (c <= 'z'))
192 if ((c >= 'A') && (c <= 'Z'))
221 in_margins (VT102 * v, CRT_Pos p)
223 if (v->pos.x < v->top_margin.x)
225 if (v->pos.y < v->top_margin.y)
228 if (v->pos.x > v->bottom_margin.x)
230 if (v->pos.y > v->bottom_margin.y)
237 vt102_log_line (Context * c, int line)
239 CRT_Pos e = { VT102_COLS - 1, line };
240 CRT_Pos p = { 0, line };
241 char logbuf[VT102_COLS + 1];
247 for (; e.x > 0; --e.x)
249 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
253 for (; p.x <= e.x; ++p.x)
255 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
264 c->l->log (c->l, logbuf);
268 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
283 vt102_cursor_normalize (VT102 * v)
285 CRT_Pos *top, *bottom;
287 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
289 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
293 vt102_clip_cursor (v, v->screen_start, v->screen_end);
299 vt102_cursor_carriage_return (VT102 * v)
301 /*FISH*/ v->pos.x = v->top_margin.x;
306 vt102_cursor_advance_line (Context * c)
309 int couldscroll = in_margins (v, v->pos);
311 /*have wraped off end of last line in scrolling region */
312 /* (|| not necessary, but shuts compiler up */
313 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
316 vt102_log_line (c, v->pos.y);
317 //FIXME call out to history
318 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
328 vt102_cursor_advance (Context * c)
332 if (v->pos.x < v->bottom_margin.x)
344 vt102_do_pending_wrap (Context * c)
347 int couldscroll = in_margins (v, v->pos);
348 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
350 if (!v->pending_wrap)
354 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
355 v->pos.y, autowrap, couldscroll);
358 /*End of line but no autowrap, nothing to do*/
362 /*End of screen and not allowed to scroll, nothing to do*/
363 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
368 v->pos.x = v->top_margin.x;
375 vt102_cursor_advance_line (c);
380 vt102_cursor_retard (VT102 * v)
382 if (v->pos.x != v->top_margin.x)
389 vt102_reset_tabs (VT102 * v)
393 memset (v->tabs, 0, sizeof (v->tabs));
395 for (i = 0; i < VT102_COLS; i += 8)
401 vt102_cursor_advance_tab (VT102 * v)
403 if (v->pos.x == v->bottom_margin.x)
405 while (v->pos.x < v->bottom_margin.x)
408 if (v->tabs[v->pos.x])
414 vt102_cursor_home (VT102 * v)
416 v->pos = v->top_margin;
417 vt102_cursor_normalize (v);
422 vt102_cursor_absolute (VT102 * v, int x, int y)
424 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
426 v->pos.x = x + v->top_margin.x;
427 v->pos.y = y + v->top_margin.x;
434 vt102_cursor_normalize (v);
438 vt102_cursor_relative (VT102 * v, int x, int y)
442 vt102_cursor_normalize (v);
449 vt102_delete_from_line (VT102 * v, CRT_Pos p)
451 int n = v->bottom_margin.x - p.x;
459 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
460 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
463 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
464 /*But not attr due to vt102 bug*/
468 vt102_insert_into_line (VT102 * v, CRT_Pos p)
470 int n = v->bottom_margin.x - p.x;
478 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
479 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
482 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
483 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
484 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
490 vt102_change_mode (VT102 * v, int private, char *ns, int set)
506 if (m >= VT102_NMODES)
511 v->private_modes[m] = set;
514 case VT102_PRIVATE_MODE_CURSOR_MODE:
515 if (v->application_keypad_mode)
516 v->private_modes[m] = 0;
518 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
519 v->private_modes[m], set);
522 case VT102_PRIVATE_MODE_ORIGIN_MODE:
523 vt102_cursor_home (v);
532 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
537 vt102_parse_mode_string (VT102 * v, char *buf, int len)
540 char last = buf[len - 1];
544 memset (num, 0, sizeof (num));
545 o = sizeof (num) - 1;
563 vt102_change_mode (v, private, &num[o], last == 'h');
564 memset (num, 0, sizeof (num));
565 o = sizeof (num) - 1;
580 vt102_change_mode (v, private, &num[o], last == 'h');
586 vt102_change_attr (VT102 * v, char *na)
603 v->attr = CRT_ATTR_NORMAL;
604 v->color = CRT_COLOR_NORMAL;
607 v->attr |= CRT_ATTR_BOLD;
611 v->attr &= ~CRT_ATTR_BOLD;
614 v->attr |= CRT_ATTR_UNDERLINE;
617 v->attr &= ~CRT_ATTR_UNDERLINE;
620 v->attr |= CRT_ATTR_BLINK;
623 v->attr &= ~CRT_ATTR_BLINK;
626 v->attr |= CRT_ATTR_REVERSE;
629 v->attr &= ~CRT_ATTR_REVERSE;
639 v->color &= ~CRT_COLOR_FG_MASK;
640 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
650 v->color &= ~CRT_COLOR_FG_MASK;
653 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
658 v->color &= ~CRT_COLOR_FG_MASK;
660 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
670 v->color &= ~CRT_COLOR_BG_MASK;
671 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
681 v->color &= ~CRT_COLOR_BG_MASK;
684 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
689 v->color &= ~CRT_COLOR_BG_MASK;
691 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
697 fprintf (stderr, "unhandled SGR %d\n", a);
705 vt102_parse_attr_string (VT102 * v, char *buf, int len)
708 char last = buf[len - 1];
712 memset (num, 0, sizeof (num));
713 o = sizeof (num) - 1;
724 vt102_change_attr (v, &num[o]);
725 memset (num, 0, sizeof (num));
726 o = sizeof (num) - 1;
740 vt102_change_attr (v, &num[o]);
744 vt102_save_state (VT102 * v)
746 v->saved.pos = v->pos;
747 v->saved.attr = v->attr;
748 v->saved.color = v->color;
749 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
753 vt102_restore_state (VT102 * v)
755 v->pos = v->saved.pos;
756 v->attr = v->saved.attr;
757 v->color = v->saved.color;
758 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
759 vt102_cursor_normalize (v);
764 vt102_scs (Context * c, int g, int s)
766 /*Ignoring charsets*/
770 vt102_parse_esc (Context * c, int ch)
776 if (v->pos.y == v->bottom_margin.y)
778 vt102_log_line (c, v->pos.y);
779 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
783 vt102_cursor_relative (v, 0, 1);
790 if (v->pos.y == v->top_margin.y)
792 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
796 vt102_cursor_relative (v, 0, -1);
800 vt102_save_state (v);
803 vt102_restore_state (v);
806 v->application_keypad_mode = 1;
809 v->application_keypad_mode = 0;
813 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
814 (ch < 32) ? '?' : ch);
820 vt102_parse_csi (Context * c, char *buf, int len)
851 vt102_cursor_relative (v, 0, -narg);
854 vt102_cursor_relative (v, 0, narg);
857 vt102_cursor_relative (v, narg, 0);
860 vt102_cursor_relative (v, -narg, 0);
863 vt102_cursor_relative (v, 0, narg);
864 vt102_cursor_carriage_return (v);
867 vt102_cursor_relative (v, 0, -narg);
868 vt102_cursor_carriage_return (v);
871 vt102_cursor_absolute (v, narg - 1, v->pos.y);
880 ptr = index (arg, ';');
882 x = atoi (ptr + 1) - 1;
886 vt102_cursor_absolute (v, x, y);
893 crt_erase (&v->crt, v->pos, v->screen_end, 1);
896 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
902 CRT_Pos ls = { 0, v->pos.y };
903 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
905 narg = 0; /*Different default */
910 crt_erase (&v->crt, v->pos, le, 1);
913 crt_erase (&v->crt, ls, v->pos, 1);
916 crt_erase (&v->crt, ls, le, 1);
924 vt102_delete_from_line (v, v->pos);
927 if ((v->pos.y >= v->top_margin.y)
928 && (v->pos.y <= v->bottom_margin.y))
931 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
936 if ((v->pos.y >= v->top_margin.y)
937 && (v->pos.y <= v->bottom_margin.y))
941 //FIXME call out to history
942 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
949 narg = 0; /*Different default */
954 v->tabs[v->pos.x] = 0;
957 memset (v->tabs, 0, sizeof (v->tabs));
964 vt102_parse_mode_string (v, &buf[1], len - 1);
968 vt102_parse_attr_string (v, &buf[1], len - 1);
971 v->top_margin = v->screen_start;
972 v->bottom_margin = v->screen_end;
974 if ((len > 2) && (ptr = index (arg, ';')))
977 v->top_margin.y = narg - 1;
978 v->bottom_margin.y = atoi (ptr) - 1;
981 if (v->top_margin.y < v->screen_start.y)
982 v->top_margin.y = v->screen_start.y;
983 if (v->top_margin.y > v->screen_end.y)
984 v->top_margin.y = v->screen_end.y;
985 if (v->bottom_margin.y < v->screen_start.y)
986 v->bottom_margin.y = v->screen_start.y;
987 if (v->bottom_margin.y > v->screen_end.y)
988 v->bottom_margin.y = v->screen_end.y;
990 vt102_cursor_home (v);
993 v->saved.pos = v->pos;
996 v->pos = v->saved.pos;
997 vt102_cursor_normalize (v);
1003 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1010 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1020 vt102_status_line (VT102 * v, char *str)
1023 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1027 ca->attr = CRT_ATTR_REVERSE;
1028 ca->color = CRT_COLOR_NORMAL;
1039 vt102_parse_char (Context * c, int ch)
1042 VT102_parser *p = &v->parser;
1045 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1046 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1050 p->csi_buf[p->csi_ptr++] = ch;
1051 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1053 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1057 else if (p->in_escape)
1059 if (csi_starter (ch))
1062 p->csi_buf[p->csi_ptr++] = ch;
1066 else if (scs_starter (ch))
1074 vt102_parse_esc (c, ch);
1079 vt102_scs (c, p->in_scs, ch);
1094 c->t->xmit (c->t, "vt102", 5);
1100 vt102_cursor_retard (c->v);
1103 vt102_cursor_advance_tab (c->v);
1108 vt102_cursor_advance_line (c);
1109 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1112 vt102_cursor_carriage_return (v);
1116 /*Ignoring charsets */
1120 /*Ignoring charsets */
1143 /*regular character */ default:
1144 vt102_do_pending_wrap (c);
1146 if (v->modes[VT102_MODE_INSERT])
1147 vt102_insert_into_line (v, v->pos);
1149 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1150 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1151 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1152 vt102_cursor_advance (c);
1156 v->crt.pos = v->pos;
1157 v->crt.hide_cursor =
1158 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1160 if (v->current_line.y != v->pos.y)
1162 vt102_log_line (c, v->current_line.y);
1163 v->current_line = v->pos;
1166 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1169 vt102_parse (Context * c, char *buf, int len)
1172 vt102_parse_char (c, *(buf++));
1177 vt102_parser_reset (VT102_parser * p)
1187 vt102_send (Context * c, uint8_t key)
1191 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1193 if ((key > 31) && (key < 127))
1195 c->t->xmit (c->t, &key, 1);
1214 c->t->xmit (c->t, &key, 1);
1217 c->t->xmit (c->t, &key, 1);
1218 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1221 c->t->xmit (c->t, &ch, 1);
1237 c->t->xmit (c->t, &key, 1);
1245 c->t->xmit (c->t, &key, 1);
1256 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1258 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1259 c->t->xmit (c->t, &buf, sizeof (buf));
1263 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1264 c->t->xmit (c->t, &buf, sizeof (buf));
1282 if (c->v->application_keypad_mode)
1284 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1285 c->t->xmit (c->t, &buf, sizeof (buf));
1289 static char kpoff[KEY_NUM] = {
1308 c->t->xmit (c->t, &kpoff[key], 1);
1312 if (c->v->application_keypad_mode)
1314 uint8_t buf[] = { 033, 'O', 'M' };
1315 c->t->xmit (c->t, &buf, sizeof (buf));
1320 c->t->xmit (c->t, &ch, 1);
1321 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1324 c->t->xmit (c->t, &ch, 1);
1333 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1334 c->t->xmit (c->t, &buf, sizeof (buf));
1342 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1343 c->t->xmit (c->t, &buf, sizeof (buf));
1351 vt102_reset (VT102 * v)
1353 VT102_parser *p = &v->parser;
1355 vt102_parser_reset (p);
1358 v->attr = CRT_ATTR_NORMAL;
1359 v->color = CRT_COLOR_NORMAL;
1361 v->application_keypad_mode = 0;
1363 v->current_line = v->pos;
1364 v->pending_wrap = 0;
1366 v->screen_start.x = 0;
1367 v->screen_start.y = 0;
1368 v->screen_end.x = VT102_COLS - 1;
1369 v->screen_end.y = VT102_ROWS - 1;
1371 v->top_margin = v->screen_start;
1372 v->bottom_margin = v->screen_end;
1374 memset (v->modes, 0, VT102_NMODES);
1375 memset (v->private_modes, 0, VT102_NMODES);
1377 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1378 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1379 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1380 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1382 vt102_cursor_home (v);
1383 vt102_reset_tabs (v);
1384 v->current_line = v->pos;
1386 vt102_save_state (v);
1388 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1393 vt102_dispatch (Context * c)
1398 red = c->t->recv (c->t, buf, sizeof (buf));
1406 vt102_parse (c, buf, red);
1413 vt102_dispatch_one (Context * c)
1418 red = c->t->recv (c->t, &buf, sizeof (buf));
1425 vt102_parse_char (c, buf);
1435 v = (VT102 *) malloc (sizeof (VT102));
1444 vt102_free (VT102 * v)