4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.14 2008/02/07 00:40:23 james
14 * *** empty log message ***
16 * Revision 1.13 2008/02/07 00:39:59 james
17 * *** empty log message ***
19 * Revision 1.12 2008/02/07 00:39:13 james
20 * *** empty log message ***
22 * Revision 1.11 2008/02/06 20:26:58 james
23 * *** empty log message ***
25 * Revision 1.10 2008/02/06 17:53:28 james
26 * *** empty log message ***
28 * Revision 1.9 2008/02/06 15:53:22 james
29 * *** empty log message ***
31 * Revision 1.8 2008/02/06 11:49:47 james
32 * *** empty log message ***
34 * Revision 1.7 2008/02/06 11:30:37 james
35 * *** empty log message ***
37 * Revision 1.6 2008/02/05 01:11:46 james
38 * *** empty log message ***
40 * Revision 1.5 2008/02/04 20:23:55 james
41 * *** empty log message ***
43 * Revision 1.4 2008/02/04 05:45:55 james
46 * Revision 1.3 2008/02/04 02:05:06 james
47 * *** empty log message ***
49 * Revision 1.2 2008/02/04 01:32:39 james
50 * *** empty log message ***
52 * Revision 1.1 2008/02/03 23:36:41 james
53 * *** empty log message ***
62 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
64 vt100|vt100-am|dec vt100 (w/advanced video):\
66 :co#80:it#8:li#24:vt#3:\
67 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
69 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
70 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
71 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
72 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
73 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
74 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
75 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
76 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
77 :us=\E[4m:tc=vt100+fnkeys:
79 vt100+fnkeys|dec vt100 numeric keypad:\
80 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
83 vt100+pfkeys|dec vt100 numeric keypad:\
84 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
86 vt100+keypad|dec vt100 numeric keypad no fkeys:\
87 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
92 so the parser needs to be able to at least do
165 if ((c >= 'a') && (c <= 'z'))
167 if ((c >= 'A') && (c <= 'Z'))
186 in_margins (VT102 * v, CRT_Pos p)
188 if (v->pos.x < v->top_margin.x)
190 if (v->pos.y < v->top_margin.y)
193 if (v->pos.x > v->bottom_margin.x)
195 if (v->pos.y > v->bottom_margin.y)
202 vt102_log_line (VT102 * v, int line)
205 CRT_Pos e = { VT102_COLS - 1, line };
206 CRT_Pos p = { 0, line };
209 log = fopen ("log", "a+");
215 for (; e.x > 0; --e.x)
217 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
221 for (; p.x <= e.x; ++p.x)
223 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
234 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
249 vt102_cursor_normalize (VT102 * v)
251 CRT_Pos *top, *bottom;
253 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
255 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
259 vt102_clip_cursor (v, v->screen_start, v->screen_end);
265 vt102_cursor_carriage_return (VT102 * v)
267 /*FISH*/ v->pos.x = v->top_margin.x;
272 vt102_cursor_advance_line (VT102 * v)
274 int couldscroll = in_margins (v, v->pos);
276 /*have wraped off end of last line in scrolling region */
277 /* (|| not necessary, but shuts compiler up */
278 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
281 vt102_log_line (v, v->pos.y);
282 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
292 vt102_cursor_advance (VT102 * v)
295 if (v->pos.x < v->bottom_margin.x)
307 vt102_do_pending_wrap (VT102 * v)
309 int couldscroll = in_margins (v, v->pos);
310 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
312 if (!v->pending_wrap)
316 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
317 v->pos.y, autowrap, couldscroll);
320 /*End of line but no autowrap, nothing to do*/
324 /*End of screen and not allowed to scroll, nothing to do*/
325 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
330 v->pos.x = v->top_margin.x;
337 vt102_cursor_advance_line (v);
342 vt102_cursor_retard (VT102 * v)
344 if (v->pos.x != v->top_margin.x)
351 vt102_reset_tabs (VT102 * v)
355 memset (v->tabs, 0, sizeof (v->tabs));
357 for (i = 0; i < VT102_COLS; i += 8)
363 vt102_cursor_advance_tab (VT102 * v)
365 if (v->pos.x == v->bottom_margin.x)
367 while (v->pos.x < v->bottom_margin.x)
370 if (v->tabs[v->pos.x])
376 vt102_cursor_home (VT102 * v)
378 v->pos = v->top_margin;
379 vt102_cursor_normalize (v);
384 vt102_cursor_absolute (VT102 * v, int x, int y)
386 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
388 v->pos.x = x + v->top_margin.x;
389 v->pos.y = y + v->top_margin.x;
396 vt102_cursor_normalize (v);
400 vt102_cursor_relative (VT102 * v, int x, int y)
404 vt102_cursor_normalize (v);
411 vt102_delete_from_line (VT102 * v, CRT_Pos p)
413 int n = v->bottom_margin.x - p.x;
421 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
422 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
425 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
426 /*But not attr due to vt102 bug*/
430 vt102_insert_into_line (VT102 * v, CRT_Pos p)
432 int n = v->bottom_margin.x - p.x;
440 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
441 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
444 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
445 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
451 vt102_change_mode (VT102 * v, int private, char *ns, int set)
467 if (m >= VT102_NMODES)
472 v->private_modes[m] = set;
475 case VT102_PRIVATE_MODE_CURSOR_MODE:
476 if (v->application_keypad_mode)
477 v->private_modes[m] = 0;
479 fprintf (stderr, "APPLICATION CURSOR MODE %d\n",
480 v->private_modes[m]);
483 case VT102_PRIVATE_MODE_ORIGIN_MODE:
484 vt102_cursor_home (v);
493 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
498 vt102_parse_mode_string (VT102 * v, char *buf, int len)
501 char last = buf[len - 1];
505 memset (num, 0, sizeof (num));
506 o = sizeof (num) - 1;
524 vt102_change_mode (v, private, &num[o], last == 'h');
525 memset (num, 0, sizeof (num));
526 o = sizeof (num) - 1;
541 vt102_change_mode (v, private, &num[o], last == 'h');
547 vt102_change_attr (VT102 * v, char *na)
564 v->attr = CRT_ATTR_NORMAL;
567 v->attr |= CRT_ATTR_BOLD;
571 v->attr &= ~CRT_ATTR_BOLD;
574 v->attr |= CRT_ATTR_UNDERLINE;
577 v->attr &= ~CRT_ATTR_UNDERLINE;
580 v->attr |= CRT_ATTR_BLINK;
583 v->attr &= ~CRT_ATTR_BLINK;
586 v->attr |= CRT_ATTR_REVERSE;
589 v->attr &= ~CRT_ATTR_REVERSE;
594 fprintf (stderr, "unhandled SGR %d\n", a);
602 vt102_parse_attr_string (VT102 * v, char *buf, int len)
605 char last = buf[len - 1];
609 memset (num, 0, sizeof (num));
610 o = sizeof (num) - 1;
621 vt102_change_attr (v, &num[o]);
622 memset (num, 0, sizeof (num));
623 o = sizeof (num) - 1;
637 vt102_change_attr (v, &num[o]);
641 vt102_save_state (VT102 * v)
643 v->saved.pos = v->pos;
644 v->saved.attr = v->attr;
645 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
649 vt102_restore_state (VT102 * v)
651 v->pos = v->saved.pos;
652 v->attr = v->saved.attr;
653 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
654 vt102_cursor_normalize (v);
659 vt102_parse_esc (VT102 * v, int c)
664 if (v->pos.y == v->bottom_margin.y)
666 vt102_log_line (v, v->pos.y);
667 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
671 vt102_cursor_relative (v, 0, 1);
678 if (v->pos.y == v->top_margin.y)
680 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
684 vt102_cursor_relative (v, 0, -1);
688 vt102_save_state (v);
691 vt102_restore_state (v);
694 v->application_keypad_mode = 1;
697 v->application_keypad_mode = 0;
701 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
708 vt102_parse_csi (VT102 * v, char *buf, int len)
737 vt102_cursor_relative (v, 0, -narg);
740 vt102_cursor_relative (v, 0, narg);
743 vt102_cursor_relative (v, narg, 0);
746 vt102_cursor_relative (v, -narg, 0);
749 vt102_cursor_relative (v, 0, narg);
750 vt102_cursor_carriage_return (v);
753 vt102_cursor_relative (v, 0, -narg);
754 vt102_cursor_carriage_return (v);
757 vt102_cursor_absolute (v, narg - 1, v->pos.y);
766 ptr = index (arg, ';');
768 x = atoi (ptr + 1) - 1;
772 vt102_cursor_absolute (v, x, y);
779 crt_erase (&v->crt, v->pos, v->screen_end, 1);
782 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
788 CRT_Pos ls = { 0, v->pos.y };
789 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
791 narg = 0; /*Different default */
796 crt_erase (&v->crt, v->pos, le, 1);
799 crt_erase (&v->crt, ls, v->pos, 1);
802 crt_erase (&v->crt, ls, le, 1);
810 vt102_delete_from_line (v, v->pos);
813 if ((v->pos.y >= v->top_margin.y)
814 && (v->pos.y <= v->bottom_margin.y))
817 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
822 if ((v->pos.y >= v->top_margin.y)
823 && (v->pos.y <= v->bottom_margin.y))
826 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
832 narg = 0; /*Different default */
837 v->tabs[v->pos.x] = 0;
840 memset (v->tabs, 0, sizeof (v->tabs));
847 vt102_parse_mode_string (v, &buf[1], len - 1);
851 vt102_parse_attr_string (v, &buf[1], len - 1);
854 v->top_margin = v->screen_start;
855 v->bottom_margin = v->screen_end;
857 if ((len > 2) && (ptr = index (arg, ';')))
860 v->top_margin.y = narg - 1;
861 v->bottom_margin.y = atoi (ptr) - 1;
864 if (v->top_margin.y < v->screen_start.y)
865 v->top_margin.y = v->screen_start.y;
866 if (v->top_margin.y > v->screen_end.y)
867 v->top_margin.y = v->screen_end.y;
868 if (v->bottom_margin.y < v->screen_start.y)
869 v->bottom_margin.y = v->screen_start.y;
870 if (v->bottom_margin.y > v->screen_end.y)
871 v->bottom_margin.y = v->screen_end.y;
873 vt102_cursor_home (v);
876 v->saved.pos = v->pos;
879 v->pos = v->saved.pos;
880 vt102_cursor_normalize (v);
886 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
893 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
903 vt102_status_line (VT102 * v, char *str)
906 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
910 ca->attr = CRT_ATTR_REVERSE;
921 vt102_parse_char (VT102 * v, int c)
923 VT102_parser *p = &v->parser;
927 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
928 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
932 p->csi_buf[p->csi_ptr++] = c;
933 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
935 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
939 else if (p->in_escape)
944 p->csi_buf[p->csi_ptr++] = c;
951 vt102_parse_esc (v, c);
966 tty_write (v->tty, "vt102", 5);
972 vt102_cursor_retard (v);
975 vt102_cursor_advance_tab (v);
980 vt102_cursor_advance_line (v);
981 if (!v->modes[VT102_MODE_NEWLINE_MODE])
984 vt102_cursor_carriage_return (v);
1009 /*regular character */ default:
1010 vt102_do_pending_wrap (v);
1012 if (v->modes[VT102_MODE_INSERT])
1013 vt102_insert_into_line (v, v->pos);
1015 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1016 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1017 vt102_cursor_advance (v);
1021 v->crt.pos = v->pos;
1022 v->crt.hide_cursor =
1023 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1025 if (v->current_line.y != v->pos.y)
1027 vt102_log_line (v, v->current_line.y);
1028 v->current_line = v->pos;
1031 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1034 vt102_parse (VT102 * v, char *buf, int len)
1037 vt102_parse_char (v, *(buf++));
1042 vt102_parser_reset (VT102_parser * p)
1051 vt102_send (VT102 * v, uint8_t key)
1055 fprintf(stderr,"vts: %d(%c)\n",key,(key>31)?key:' ');
1057 if ((key > 31) && (key < 127))
1059 tty_write (v->tty, &key, 1);
1078 tty_write (v->tty, &key, 1);
1081 tty_write (v->tty, &key, 1);
1082 if (v->modes[VT102_MODE_NEWLINE_MODE])
1085 tty_write (v->tty, &c, 1);
1101 tty_write (v->tty, &key, 1);
1109 tty_write (v->tty, &key, 1);
1120 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1122 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1123 tty_write (v->tty, &buf, sizeof (buf));
1127 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1128 tty_write (v->tty, &buf, sizeof (buf));
1146 if (v->application_keypad_mode)
1148 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1149 tty_write (v->tty, &buf, sizeof (buf));
1153 static char kpoff[KEY_NUM] = {
1172 tty_write (v->tty, &kpoff[key], 1);
1176 if (v->application_keypad_mode)
1178 uint8_t buf[] = { 033, 'O', 'M' };
1179 tty_write (v->tty, &buf, sizeof (buf));
1184 tty_write (v->tty, &c, 1);
1185 if (v->modes[VT102_MODE_NEWLINE_MODE])
1188 tty_write (v->tty, &c, 1);
1197 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1198 tty_write (v->tty, &buf, sizeof (buf));
1206 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1207 tty_write (v->tty, &buf, sizeof (buf));
1215 vt102_reset (VT102 * v)
1217 VT102_parser *p = &v->parser;
1219 vt102_parser_reset (p);
1223 v->application_keypad_mode = 0;
1225 v->current_line = v->pos;
1226 v->pending_wrap = 0;
1228 v->screen_start.x = 0;
1229 v->screen_start.y = 0;
1230 v->screen_end.x = VT102_COLS - 1;
1231 v->screen_end.y = VT102_ROWS - 1;
1233 v->top_margin = v->screen_start;
1234 v->bottom_margin = v->screen_end;
1236 memset (v->modes, 0, VT102_NMODES);
1237 memset (v->private_modes, 0, VT102_NMODES);
1239 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1240 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1241 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1242 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1244 vt102_cursor_home (v);
1245 vt102_reset_tabs (v);
1246 v->current_line = v->pos;
1248 vt102_save_state (v);
1250 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1255 vt102_dispatch (VT102 * v)
1260 red = tty_read (v->tty, buf, sizeof (buf));
1268 vt102_parse (v, buf, red);
1278 v = (VT102 *) malloc (sizeof (VT102));
1288 vt102_free (VT102 * v)