4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.15 2008/02/07 00:43:27 james
14 * *** empty log message ***
16 * Revision 1.14 2008/02/07 00:40:23 james
17 * *** empty log message ***
19 * Revision 1.13 2008/02/07 00:39:59 james
20 * *** empty log message ***
22 * Revision 1.12 2008/02/07 00:39:13 james
23 * *** empty log message ***
25 * Revision 1.11 2008/02/06 20:26:58 james
26 * *** empty log message ***
28 * Revision 1.10 2008/02/06 17:53:28 james
29 * *** empty log message ***
31 * Revision 1.9 2008/02/06 15:53:22 james
32 * *** empty log message ***
34 * Revision 1.8 2008/02/06 11:49:47 james
35 * *** empty log message ***
37 * Revision 1.7 2008/02/06 11:30:37 james
38 * *** empty log message ***
40 * Revision 1.6 2008/02/05 01:11:46 james
41 * *** empty log message ***
43 * Revision 1.5 2008/02/04 20:23:55 james
44 * *** empty log message ***
46 * Revision 1.4 2008/02/04 05:45:55 james
49 * Revision 1.3 2008/02/04 02:05:06 james
50 * *** empty log message ***
52 * Revision 1.2 2008/02/04 01:32:39 james
53 * *** empty log message ***
55 * Revision 1.1 2008/02/03 23:36:41 james
56 * *** empty log message ***
65 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
67 vt100|vt100-am|dec vt100 (w/advanced video):\
69 :co#80:it#8:li#24:vt#3:\
70 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
72 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
73 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
74 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
75 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
76 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
77 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
78 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
79 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
80 :us=\E[4m:tc=vt100+fnkeys:
82 vt100+fnkeys|dec vt100 numeric keypad:\
83 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
86 vt100+pfkeys|dec vt100 numeric keypad:\
87 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
89 vt100+keypad|dec vt100 numeric keypad no fkeys:\
90 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
95 so the parser needs to be able to at least do
168 if ((c >= 'a') && (c <= 'z'))
170 if ((c >= 'A') && (c <= 'Z'))
189 in_margins (VT102 * v, CRT_Pos p)
191 if (v->pos.x < v->top_margin.x)
193 if (v->pos.y < v->top_margin.y)
196 if (v->pos.x > v->bottom_margin.x)
198 if (v->pos.y > v->bottom_margin.y)
205 vt102_log_line (VT102 * v, int line)
208 CRT_Pos e = { VT102_COLS - 1, line };
209 CRT_Pos p = { 0, line };
212 log = fopen ("log", "a+");
218 for (; e.x > 0; --e.x)
220 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
224 for (; p.x <= e.x; ++p.x)
226 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
237 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
252 vt102_cursor_normalize (VT102 * v)
254 CRT_Pos *top, *bottom;
256 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
258 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
262 vt102_clip_cursor (v, v->screen_start, v->screen_end);
268 vt102_cursor_carriage_return (VT102 * v)
270 /*FISH*/ v->pos.x = v->top_margin.x;
275 vt102_cursor_advance_line (VT102 * v)
277 int couldscroll = in_margins (v, v->pos);
279 /*have wraped off end of last line in scrolling region */
280 /* (|| not necessary, but shuts compiler up */
281 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
284 vt102_log_line (v, v->pos.y);
285 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
295 vt102_cursor_advance (VT102 * v)
298 if (v->pos.x < v->bottom_margin.x)
310 vt102_do_pending_wrap (VT102 * v)
312 int couldscroll = in_margins (v, v->pos);
313 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
315 if (!v->pending_wrap)
319 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
320 v->pos.y, autowrap, couldscroll);
323 /*End of line but no autowrap, nothing to do*/
327 /*End of screen and not allowed to scroll, nothing to do*/
328 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
333 v->pos.x = v->top_margin.x;
340 vt102_cursor_advance_line (v);
345 vt102_cursor_retard (VT102 * v)
347 if (v->pos.x != v->top_margin.x)
354 vt102_reset_tabs (VT102 * v)
358 memset (v->tabs, 0, sizeof (v->tabs));
360 for (i = 0; i < VT102_COLS; i += 8)
366 vt102_cursor_advance_tab (VT102 * v)
368 if (v->pos.x == v->bottom_margin.x)
370 while (v->pos.x < v->bottom_margin.x)
373 if (v->tabs[v->pos.x])
379 vt102_cursor_home (VT102 * v)
381 v->pos = v->top_margin;
382 vt102_cursor_normalize (v);
387 vt102_cursor_absolute (VT102 * v, int x, int y)
389 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
391 v->pos.x = x + v->top_margin.x;
392 v->pos.y = y + v->top_margin.x;
399 vt102_cursor_normalize (v);
403 vt102_cursor_relative (VT102 * v, int x, int y)
407 vt102_cursor_normalize (v);
414 vt102_delete_from_line (VT102 * v, CRT_Pos p)
416 int n = v->bottom_margin.x - p.x;
424 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
425 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
428 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
429 /*But not attr due to vt102 bug*/
433 vt102_insert_into_line (VT102 * v, CRT_Pos p)
435 int n = v->bottom_margin.x - p.x;
443 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
444 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
447 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
448 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
454 vt102_change_mode (VT102 * v, int private, char *ns, int set)
470 if (m >= VT102_NMODES)
475 v->private_modes[m] = set;
478 case VT102_PRIVATE_MODE_CURSOR_MODE:
479 if (v->application_keypad_mode)
480 v->private_modes[m] = 0;
482 fprintf (stderr, "APPLICATION CURSOR MODE %d\n",
483 v->private_modes[m]);
486 case VT102_PRIVATE_MODE_ORIGIN_MODE:
487 vt102_cursor_home (v);
496 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
501 vt102_parse_mode_string (VT102 * v, char *buf, int len)
504 char last = buf[len - 1];
508 memset (num, 0, sizeof (num));
509 o = sizeof (num) - 1;
527 vt102_change_mode (v, private, &num[o], last == 'h');
528 memset (num, 0, sizeof (num));
529 o = sizeof (num) - 1;
544 vt102_change_mode (v, private, &num[o], last == 'h');
550 vt102_change_attr (VT102 * v, char *na)
567 v->attr = CRT_ATTR_NORMAL;
570 v->attr |= CRT_ATTR_BOLD;
574 v->attr &= ~CRT_ATTR_BOLD;
577 v->attr |= CRT_ATTR_UNDERLINE;
580 v->attr &= ~CRT_ATTR_UNDERLINE;
583 v->attr |= CRT_ATTR_BLINK;
586 v->attr &= ~CRT_ATTR_BLINK;
589 v->attr |= CRT_ATTR_REVERSE;
592 v->attr &= ~CRT_ATTR_REVERSE;
597 fprintf (stderr, "unhandled SGR %d\n", a);
605 vt102_parse_attr_string (VT102 * v, char *buf, int len)
608 char last = buf[len - 1];
612 memset (num, 0, sizeof (num));
613 o = sizeof (num) - 1;
624 vt102_change_attr (v, &num[o]);
625 memset (num, 0, sizeof (num));
626 o = sizeof (num) - 1;
640 vt102_change_attr (v, &num[o]);
644 vt102_save_state (VT102 * v)
646 v->saved.pos = v->pos;
647 v->saved.attr = v->attr;
648 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
652 vt102_restore_state (VT102 * v)
654 v->pos = v->saved.pos;
655 v->attr = v->saved.attr;
656 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
657 vt102_cursor_normalize (v);
662 vt102_parse_esc (VT102 * v, int c)
667 if (v->pos.y == v->bottom_margin.y)
669 vt102_log_line (v, v->pos.y);
670 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
674 vt102_cursor_relative (v, 0, 1);
681 if (v->pos.y == v->top_margin.y)
683 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
687 vt102_cursor_relative (v, 0, -1);
691 vt102_save_state (v);
694 vt102_restore_state (v);
697 v->application_keypad_mode = 1;
700 v->application_keypad_mode = 0;
704 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
711 vt102_parse_csi (VT102 * v, char *buf, int len)
740 vt102_cursor_relative (v, 0, -narg);
743 vt102_cursor_relative (v, 0, narg);
746 vt102_cursor_relative (v, narg, 0);
749 vt102_cursor_relative (v, -narg, 0);
752 vt102_cursor_relative (v, 0, narg);
753 vt102_cursor_carriage_return (v);
756 vt102_cursor_relative (v, 0, -narg);
757 vt102_cursor_carriage_return (v);
760 vt102_cursor_absolute (v, narg - 1, v->pos.y);
769 ptr = index (arg, ';');
771 x = atoi (ptr + 1) - 1;
775 vt102_cursor_absolute (v, x, y);
782 crt_erase (&v->crt, v->pos, v->screen_end, 1);
785 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
791 CRT_Pos ls = { 0, v->pos.y };
792 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
794 narg = 0; /*Different default */
799 crt_erase (&v->crt, v->pos, le, 1);
802 crt_erase (&v->crt, ls, v->pos, 1);
805 crt_erase (&v->crt, ls, le, 1);
813 vt102_delete_from_line (v, v->pos);
816 if ((v->pos.y >= v->top_margin.y)
817 && (v->pos.y <= v->bottom_margin.y))
820 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
825 if ((v->pos.y >= v->top_margin.y)
826 && (v->pos.y <= v->bottom_margin.y))
829 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
835 narg = 0; /*Different default */
840 v->tabs[v->pos.x] = 0;
843 memset (v->tabs, 0, sizeof (v->tabs));
850 vt102_parse_mode_string (v, &buf[1], len - 1);
854 vt102_parse_attr_string (v, &buf[1], len - 1);
857 v->top_margin = v->screen_start;
858 v->bottom_margin = v->screen_end;
860 if ((len > 2) && (ptr = index (arg, ';')))
863 v->top_margin.y = narg - 1;
864 v->bottom_margin.y = atoi (ptr) - 1;
867 if (v->top_margin.y < v->screen_start.y)
868 v->top_margin.y = v->screen_start.y;
869 if (v->top_margin.y > v->screen_end.y)
870 v->top_margin.y = v->screen_end.y;
871 if (v->bottom_margin.y < v->screen_start.y)
872 v->bottom_margin.y = v->screen_start.y;
873 if (v->bottom_margin.y > v->screen_end.y)
874 v->bottom_margin.y = v->screen_end.y;
876 vt102_cursor_home (v);
879 v->saved.pos = v->pos;
882 v->pos = v->saved.pos;
883 vt102_cursor_normalize (v);
889 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
896 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
906 vt102_status_line (VT102 * v, char *str)
909 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
913 ca->attr = CRT_ATTR_REVERSE;
924 vt102_parse_char (VT102 * v, int c)
926 VT102_parser *p = &v->parser;
930 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
931 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
935 p->csi_buf[p->csi_ptr++] = c;
936 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
938 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
942 else if (p->in_escape)
947 p->csi_buf[p->csi_ptr++] = c;
954 vt102_parse_esc (v, c);
969 tty_write (v->tty, "vt102", 5);
975 vt102_cursor_retard (v);
978 vt102_cursor_advance_tab (v);
983 vt102_cursor_advance_line (v);
984 if (!v->modes[VT102_MODE_NEWLINE_MODE])
987 vt102_cursor_carriage_return (v);
1012 /*regular character */ default:
1013 vt102_do_pending_wrap (v);
1015 if (v->modes[VT102_MODE_INSERT])
1016 vt102_insert_into_line (v, v->pos);
1018 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1019 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1020 vt102_cursor_advance (v);
1024 v->crt.pos = v->pos;
1025 v->crt.hide_cursor =
1026 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1028 if (v->current_line.y != v->pos.y)
1030 vt102_log_line (v, v->current_line.y);
1031 v->current_line = v->pos;
1034 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1037 vt102_parse (VT102 * v, char *buf, int len)
1040 vt102_parse_char (v, *(buf++));
1045 vt102_parser_reset (VT102_parser * p)
1054 vt102_send (VT102 * v, uint8_t key)
1058 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1060 if ((key > 31) && (key < 127))
1062 tty_write (v->tty, &key, 1);
1081 tty_write (v->tty, &key, 1);
1084 tty_write (v->tty, &key, 1);
1085 if (v->modes[VT102_MODE_NEWLINE_MODE])
1088 tty_write (v->tty, &c, 1);
1104 tty_write (v->tty, &key, 1);
1112 tty_write (v->tty, &key, 1);
1123 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1125 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1126 tty_write (v->tty, &buf, sizeof (buf));
1130 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1131 tty_write (v->tty, &buf, sizeof (buf));
1149 if (v->application_keypad_mode)
1151 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1152 tty_write (v->tty, &buf, sizeof (buf));
1156 static char kpoff[KEY_NUM] = {
1175 tty_write (v->tty, &kpoff[key], 1);
1179 if (v->application_keypad_mode)
1181 uint8_t buf[] = { 033, 'O', 'M' };
1182 tty_write (v->tty, &buf, sizeof (buf));
1187 tty_write (v->tty, &c, 1);
1188 if (v->modes[VT102_MODE_NEWLINE_MODE])
1191 tty_write (v->tty, &c, 1);
1200 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1201 tty_write (v->tty, &buf, sizeof (buf));
1209 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1210 tty_write (v->tty, &buf, sizeof (buf));
1218 vt102_reset (VT102 * v)
1220 VT102_parser *p = &v->parser;
1222 vt102_parser_reset (p);
1226 v->application_keypad_mode = 0;
1228 v->current_line = v->pos;
1229 v->pending_wrap = 0;
1231 v->screen_start.x = 0;
1232 v->screen_start.y = 0;
1233 v->screen_end.x = VT102_COLS - 1;
1234 v->screen_end.y = VT102_ROWS - 1;
1236 v->top_margin = v->screen_start;
1237 v->bottom_margin = v->screen_end;
1239 memset (v->modes, 0, VT102_NMODES);
1240 memset (v->private_modes, 0, VT102_NMODES);
1242 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1243 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1244 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1245 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1247 vt102_cursor_home (v);
1248 vt102_reset_tabs (v);
1249 v->current_line = v->pos;
1251 vt102_save_state (v);
1253 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1258 vt102_dispatch (VT102 * v)
1263 red = tty_read (v->tty, buf, sizeof (buf));
1271 vt102_parse (v, buf, red);
1281 v = (VT102 *) malloc (sizeof (VT102));
1291 vt102_free (VT102 * v)