4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.29 2008/02/15 03:32:07 james
14 * *** empty log message ***
16 * Revision 1.28 2008/02/14 10:34:30 james
17 * *** empty log message ***
19 * Revision 1.27 2008/02/14 02:46:45 james
20 * *** empty log message ***
22 * Revision 1.26 2008/02/14 01:55:57 james
23 * *** empty log message ***
25 * Revision 1.25 2008/02/13 16:57:29 james
26 * *** empty log message ***
28 * Revision 1.24 2008/02/13 09:12:21 james
29 * *** empty log message ***
31 * Revision 1.23 2008/02/07 13:26:35 james
32 * *** empty log message ***
34 * Revision 1.22 2008/02/07 13:22:51 james
35 * *** empty log message ***
37 * Revision 1.21 2008/02/07 12:21:16 james
38 * *** empty log message ***
40 * Revision 1.20 2008/02/07 12:16:04 james
41 * *** empty log message ***
43 * Revision 1.19 2008/02/07 11:27:02 james
44 * *** empty log message ***
46 * Revision 1.18 2008/02/07 01:59:25 james
47 * *** empty log message ***
49 * Revision 1.17 2008/02/07 01:58:28 james
50 * *** empty log message ***
52 * Revision 1.16 2008/02/07 01:57:46 james
53 * *** empty log message ***
55 * Revision 1.15 2008/02/07 00:43:27 james
56 * *** empty log message ***
58 * Revision 1.14 2008/02/07 00:40:23 james
59 * *** empty log message ***
61 * Revision 1.13 2008/02/07 00:39:59 james
62 * *** empty log message ***
64 * Revision 1.12 2008/02/07 00:39:13 james
65 * *** empty log message ***
67 * Revision 1.11 2008/02/06 20:26:58 james
68 * *** empty log message ***
70 * Revision 1.10 2008/02/06 17:53:28 james
71 * *** empty log message ***
73 * Revision 1.9 2008/02/06 15:53:22 james
74 * *** empty log message ***
76 * Revision 1.8 2008/02/06 11:49:47 james
77 * *** empty log message ***
79 * Revision 1.7 2008/02/06 11:30:37 james
80 * *** empty log message ***
82 * Revision 1.6 2008/02/05 01:11:46 james
83 * *** empty log message ***
85 * Revision 1.5 2008/02/04 20:23:55 james
86 * *** empty log message ***
88 * Revision 1.4 2008/02/04 05:45:55 james
91 * Revision 1.3 2008/02/04 02:05:06 james
92 * *** empty log message ***
94 * Revision 1.2 2008/02/04 01:32:39 james
95 * *** empty log message ***
97 * Revision 1.1 2008/02/03 23:36:41 james
98 * *** empty log message ***
107 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
109 vt100|vt100-am|dec vt100 (w/advanced video):\
111 :co#80:it#8:li#24:vt#3:\
112 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
114 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
115 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
116 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
117 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
118 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
119 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
120 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
121 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
122 :us=\E[4m:tc=vt100+fnkeys:
124 vt100+fnkeys|dec vt100 numeric keypad:\
125 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
128 vt100+pfkeys|dec vt100 numeric keypad:\
129 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
131 vt100+keypad|dec vt100 numeric keypad no fkeys:\
132 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
137 so the parser needs to be able to at least do
205 if ((c >= 'a') && (c <= 'z'))
207 if ((c >= 'A') && (c <= 'Z'))
236 in_margins (VT102 * v, CRT_Pos p)
238 if (v->pos.x < v->top_margin.x)
240 if (v->pos.y < v->top_margin.y)
243 if (v->pos.x > v->bottom_margin.x)
245 if (v->pos.y > v->bottom_margin.y)
252 vt102_log_line (Context * c, int line)
254 CRT_Pos e = { VT102_COLS - 1, line };
255 CRT_Pos p = { 0, line };
256 char logbuf[VT102_COLS + 1];
262 for (; e.x > 0; --e.x)
264 if (c->v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
268 for (; p.x <= e.x; ++p.x)
270 int ch = c->v->crt.screen[CRT_ADDR_POS (&p)].chr;
279 c->l->log (c->l, logbuf);
282 /*Called for every upward scroll with same args*/
284 vt102_history (Context * c, CRT_Pos t, CRT_Pos b)
286 /*Only log if it scrolls off the top*/
291 history_add (c->h, &(c->v->crt.screen[CRT_ADDR_POS (&t)]));
295 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
310 vt102_cursor_normalize (VT102 * v)
312 CRT_Pos *top, *bottom;
314 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
316 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
320 vt102_clip_cursor (v, v->screen_start, v->screen_end);
326 vt102_cursor_carriage_return (VT102 * v)
328 /*FISH*/ v->pos.x = v->top_margin.x;
333 vt102_cursor_advance_line (Context * c)
336 int couldscroll = in_margins (v, v->pos);
338 /*have wraped off end of last line in scrolling region */
339 /* (|| not necessary, but shuts compiler up */
340 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
343 vt102_log_line (c, v->pos.y);
345 vt102_history (c, v->top_margin, v->bottom_margin);
347 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
357 vt102_cursor_advance (Context * c)
361 if (v->pos.x < v->bottom_margin.x)
373 vt102_do_pending_wrap (Context * c)
376 int couldscroll = in_margins (v, v->pos);
377 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
379 if (!v->pending_wrap)
383 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
384 v->pos.y, autowrap, couldscroll);
387 /*End of line but no autowrap, nothing to do*/
391 /*End of screen and not allowed to scroll, nothing to do*/
392 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
397 v->pos.x = v->top_margin.x;
404 vt102_cursor_advance_line (c);
409 vt102_cursor_retard (VT102 * v)
411 if (v->pos.x != v->top_margin.x)
418 vt102_reset_tabs (VT102 * v)
422 memset (v->tabs, 0, sizeof (v->tabs));
424 for (i = 0; i < VT102_COLS; i += 8)
430 vt102_cursor_advance_tab (VT102 * v)
432 if (v->pos.x == v->bottom_margin.x)
434 while (v->pos.x < v->bottom_margin.x)
437 if (v->tabs[v->pos.x])
443 vt102_cursor_home (VT102 * v)
445 v->pos = v->top_margin;
446 vt102_cursor_normalize (v);
451 vt102_cursor_absolute (VT102 * v, int x, int y)
453 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
455 v->pos.x = x + v->top_margin.x;
456 v->pos.y = y + v->top_margin.x;
463 vt102_cursor_normalize (v);
467 vt102_cursor_relative (VT102 * v, int x, int y)
471 vt102_cursor_normalize (v);
478 vt102_delete_from_line (VT102 * v, CRT_Pos p)
480 int n = v->bottom_margin.x - p.x;
488 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
489 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
492 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
493 /*But not attr due to vt102 bug*/
497 vt102_insert_into_line (VT102 * v, CRT_Pos p)
499 int n = v->bottom_margin.x - p.x;
507 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
508 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
511 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
512 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
513 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
519 vt102_change_mode (VT102 * v, int private, char *ns, int set)
535 if (m >= VT102_NMODES)
540 v->private_modes[m] = set;
543 case VT102_PRIVATE_MODE_CURSOR_MODE:
544 if (v->application_keypad_mode)
545 v->private_modes[m] = 0;
547 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
548 v->private_modes[m], set);
551 case VT102_PRIVATE_MODE_ORIGIN_MODE:
552 vt102_cursor_home (v);
561 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
566 vt102_parse_mode_string (VT102 * v, char *buf, int len)
569 char last = buf[len - 1];
573 memset (num, 0, sizeof (num));
574 o = sizeof (num) - 1;
592 vt102_change_mode (v, private, &num[o], last == 'h');
593 memset (num, 0, sizeof (num));
594 o = sizeof (num) - 1;
609 vt102_change_mode (v, private, &num[o], last == 'h');
615 vt102_change_attr (VT102 * v, char *na)
632 v->attr = CRT_ATTR_NORMAL;
633 v->color = CRT_COLOR_NORMAL;
636 v->attr |= CRT_ATTR_BOLD;
640 v->attr &= ~CRT_ATTR_BOLD;
643 v->attr |= CRT_ATTR_UNDERLINE;
646 v->attr &= ~CRT_ATTR_UNDERLINE;
649 v->attr |= CRT_ATTR_BLINK;
652 v->attr &= ~CRT_ATTR_BLINK;
655 v->attr |= CRT_ATTR_REVERSE;
658 v->attr &= ~CRT_ATTR_REVERSE;
668 v->color &= ~CRT_COLOR_FG_MASK;
669 v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
679 v->color &= ~CRT_COLOR_FG_MASK;
682 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
687 v->color &= ~CRT_COLOR_FG_MASK;
689 (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
699 v->color &= ~CRT_COLOR_BG_MASK;
700 v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
710 v->color &= ~CRT_COLOR_BG_MASK;
713 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
718 v->color &= ~CRT_COLOR_BG_MASK;
720 (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
726 fprintf (stderr, "unhandled SGR %d\n", a);
734 vt102_parse_attr_string (VT102 * v, char *buf, int len)
737 char last = buf[len - 1];
741 memset (num, 0, sizeof (num));
742 o = sizeof (num) - 1;
753 vt102_change_attr (v, &num[o]);
754 memset (num, 0, sizeof (num));
755 o = sizeof (num) - 1;
769 vt102_change_attr (v, &num[o]);
773 vt102_save_state (VT102 * v)
775 v->saved.pos = v->pos;
776 v->saved.attr = v->attr;
777 v->saved.color = v->color;
778 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
782 vt102_restore_state (VT102 * v)
784 v->pos = v->saved.pos;
785 v->attr = v->saved.attr;
786 v->color = v->saved.color;
787 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
788 vt102_cursor_normalize (v);
793 vt102_scs (Context * c, int g, int s)
795 /*Ignoring charsets*/
799 vt102_parse_esc (Context * c, int ch)
805 if (v->pos.y == v->bottom_margin.y)
807 vt102_log_line (c, v->pos.y);
808 vt102_history (c, v->top_margin, v->bottom_margin);
809 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
813 vt102_cursor_relative (v, 0, 1);
820 if (v->pos.y == v->top_margin.y)
822 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
826 vt102_cursor_relative (v, 0, -1);
830 vt102_save_state (v);
833 vt102_restore_state (v);
836 v->application_keypad_mode = 1;
839 v->application_keypad_mode = 0;
843 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", ch,
844 (ch < 32) ? '?' : ch);
850 vt102_parse_csi (Context * c, char *buf, int len)
881 vt102_cursor_relative (v, 0, -narg);
884 vt102_cursor_relative (v, 0, narg);
887 vt102_cursor_relative (v, narg, 0);
890 vt102_cursor_relative (v, -narg, 0);
893 vt102_cursor_relative (v, 0, narg);
894 vt102_cursor_carriage_return (v);
897 vt102_cursor_relative (v, 0, -narg);
898 vt102_cursor_carriage_return (v);
901 vt102_cursor_absolute (v, narg - 1, v->pos.y);
910 ptr = index (arg, ';');
912 x = atoi (ptr + 1) - 1;
916 vt102_cursor_absolute (v, x, y);
923 crt_erase (&v->crt, v->pos, v->screen_end, 1);
926 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
932 CRT_Pos ls = { 0, v->pos.y };
933 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
935 narg = 0; /*Different default */
940 crt_erase (&v->crt, v->pos, le, 1);
943 crt_erase (&v->crt, ls, v->pos, 1);
946 crt_erase (&v->crt, ls, le, 1);
954 vt102_delete_from_line (v, v->pos);
957 if ((v->pos.y >= v->top_margin.y)
958 && (v->pos.y <= v->bottom_margin.y))
961 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
966 if ((v->pos.y >= v->top_margin.y)
967 && (v->pos.y <= v->bottom_margin.y))
971 vt102_history (c, v->pos, v->bottom_margin);
972 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 1);
979 narg = 0; /*Different default */
984 v->tabs[v->pos.x] = 0;
987 memset (v->tabs, 0, sizeof (v->tabs));
994 vt102_parse_mode_string (v, &buf[1], len - 1);
998 vt102_parse_attr_string (v, &buf[1], len - 1);
1001 v->top_margin = v->screen_start;
1002 v->bottom_margin = v->screen_end;
1004 if ((len > 2) && (ptr = index (arg, ';')))
1007 v->top_margin.y = narg - 1;
1008 v->bottom_margin.y = atoi (ptr) - 1;
1011 if (v->top_margin.y < v->screen_start.y)
1012 v->top_margin.y = v->screen_start.y;
1013 if (v->top_margin.y > v->screen_end.y)
1014 v->top_margin.y = v->screen_end.y;
1015 if (v->bottom_margin.y < v->screen_start.y)
1016 v->bottom_margin.y = v->screen_start.y;
1017 if (v->bottom_margin.y > v->screen_end.y)
1018 v->bottom_margin.y = v->screen_end.y;
1020 vt102_cursor_home (v);
1023 v->saved.pos = v->pos;
1026 v->pos = v->saved.pos;
1027 vt102_cursor_normalize (v);
1028 v->pending_wrap = 0;
1033 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1040 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
1050 vt102_status_line (VT102 * v, char *str)
1053 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1057 ca->attr = CRT_ATTR_REVERSE;
1058 ca->color = CRT_COLOR_NORMAL;
1069 vt102_parse_char (Context * c, int ch)
1072 VT102_parser *p = &v->parser;
1075 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1076 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1080 p->csi_buf[p->csi_ptr++] = ch;
1081 if (csi_ender (ch) || (p->csi_ptr == VT102_CSI_LEN))
1083 vt102_parse_csi (c, p->csi_buf, p->csi_ptr);
1087 else if (p->in_escape)
1089 if (csi_starter (ch))
1092 p->csi_buf[p->csi_ptr++] = ch;
1096 else if (scs_starter (ch))
1104 vt102_parse_esc (c, ch);
1109 vt102_scs (c, p->in_scs, ch);
1124 c->t->xmit (c->t, "vt102", 5);
1130 vt102_cursor_retard (c->v);
1133 vt102_cursor_advance_tab (c->v);
1138 vt102_cursor_advance_line (c);
1139 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1142 vt102_cursor_carriage_return (v);
1146 /*Ignoring charsets */
1150 /*Ignoring charsets */
1173 /*regular character */ default:
1174 vt102_do_pending_wrap (c);
1176 if (v->modes[VT102_MODE_INSERT])
1177 vt102_insert_into_line (v, v->pos);
1179 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
1180 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1181 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1182 vt102_cursor_advance (c);
1186 v->crt.pos = v->pos;
1187 v->crt.hide_cursor =
1188 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1190 if (v->current_line.y != v->pos.y)
1192 vt102_log_line (c, v->current_line.y);
1193 v->current_line = v->pos;
1197 cmd_show_status (c->d, c);
1200 vt102_parse (Context * c, char *buf, int len)
1203 vt102_parse_char (c, *(buf++));
1208 vt102_parser_reset (VT102_parser * p)
1218 vt102_send (Context * c, uint8_t key)
1226 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1228 if ((key > 31) && (key < 127))
1230 c->t->xmit (c->t, &key, 1);
1249 c->t->xmit (c->t, &key, 1);
1252 c->t->xmit (c->t, &key, 1);
1253 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1256 c->t->xmit (c->t, &ch, 1);
1272 c->t->xmit (c->t, &key, 1);
1280 c->t->xmit (c->t, &key, 1);
1291 if (c->v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1293 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1294 c->t->xmit (c->t, &buf, sizeof (buf));
1298 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1299 c->t->xmit (c->t, &buf, sizeof (buf));
1317 if (c->v->application_keypad_mode)
1319 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1320 c->t->xmit (c->t, &buf, sizeof (buf));
1324 static char kpoff[KEY_NUM] = {
1343 c->t->xmit (c->t, &kpoff[key], 1);
1347 if (c->v->application_keypad_mode)
1349 uint8_t buf[] = { 033, 'O', 'M' };
1350 c->t->xmit (c->t, &buf, sizeof (buf));
1355 c->t->xmit (c->t, &ch, 1);
1356 if (c->v->modes[VT102_MODE_NEWLINE_MODE])
1359 c->t->xmit (c->t, &ch, 1);
1368 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1369 c->t->xmit (c->t, &buf, sizeof (buf));
1377 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1378 c->t->xmit (c->t, &buf, sizeof (buf));
1386 vt102_reset (VT102 * v)
1388 VT102_parser *p = &v->parser;
1390 vt102_parser_reset (p);
1393 v->attr = CRT_ATTR_NORMAL;
1394 v->color = CRT_COLOR_NORMAL;
1396 v->application_keypad_mode = 0;
1398 v->current_line = v->pos;
1399 v->pending_wrap = 0;
1401 v->screen_start.x = 0;
1402 v->screen_start.y = 0;
1403 v->screen_end.x = VT102_COLS - 1;
1404 v->screen_end.y = VT102_ROWS - 1;
1406 v->top_margin = v->screen_start;
1407 v->bottom_margin = v->screen_end;
1409 memset (v->modes, 0, VT102_NMODES);
1410 memset (v->private_modes, 0, VT102_NMODES);
1412 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1413 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1414 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1415 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1417 vt102_cursor_home (v);
1418 vt102_reset_tabs (v);
1419 v->current_line = v->pos;
1421 vt102_save_state (v);
1423 vt102_status_line (v, "");
1428 vt102_dispatch (Context * c)
1433 red = c->t->recv (c->t, buf, sizeof (buf));
1441 vt102_parse (c, buf, red);
1448 vt102_dispatch_one (Context * c)
1453 red = c->t->recv (c->t, &buf, sizeof (buf));
1460 vt102_parse_char (c, buf);
1470 v = (VT102 *) malloc (sizeof (VT102));
1479 vt102_free (VT102 * v)