4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.12 2008/02/07 00:39:13 james
14 * *** empty log message ***
16 * Revision 1.11 2008/02/06 20:26:58 james
17 * *** empty log message ***
19 * Revision 1.10 2008/02/06 17:53:28 james
20 * *** empty log message ***
22 * Revision 1.9 2008/02/06 15:53:22 james
23 * *** empty log message ***
25 * Revision 1.8 2008/02/06 11:49:47 james
26 * *** empty log message ***
28 * Revision 1.7 2008/02/06 11:30:37 james
29 * *** empty log message ***
31 * Revision 1.6 2008/02/05 01:11:46 james
32 * *** empty log message ***
34 * Revision 1.5 2008/02/04 20:23:55 james
35 * *** empty log message ***
37 * Revision 1.4 2008/02/04 05:45:55 james
40 * Revision 1.3 2008/02/04 02:05:06 james
41 * *** empty log message ***
43 * Revision 1.2 2008/02/04 01:32:39 james
44 * *** empty log message ***
46 * Revision 1.1 2008/02/03 23:36:41 james
47 * *** empty log message ***
56 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
58 vt100|vt100-am|dec vt100 (w/advanced video):\
60 :co#80:it#8:li#24:vt#3:\
61 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
63 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
64 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
65 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
66 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
67 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
68 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
69 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
70 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
71 :us=\E[4m:tc=vt100+fnkeys:
73 vt100+fnkeys|dec vt100 numeric keypad:\
74 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
77 vt100+pfkeys|dec vt100 numeric keypad:\
78 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
80 vt100+keypad|dec vt100 numeric keypad no fkeys:\
81 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
86 so the parser needs to be able to at least do
159 if ((c >= 'a') && (c <= 'z'))
161 if ((c >= 'A') && (c <= 'Z'))
180 in_margins (VT102 * v, CRT_Pos p)
182 if (v->pos.x < v->top_margin.x)
184 if (v->pos.y < v->top_margin.y)
187 if (v->pos.x > v->bottom_margin.x)
189 if (v->pos.y > v->bottom_margin.y)
196 vt102_log_line (VT102 * v, int line)
199 CRT_Pos e = { VT102_COLS - 1, line };
200 CRT_Pos p = { 0, line };
203 log = fopen ("log", "a+");
209 for (; e.x > 0; --e.x)
211 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
215 for (; p.x <= e.x; ++p.x)
217 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
228 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
243 vt102_cursor_normalize (VT102 * v)
245 CRT_Pos *top, *bottom;
247 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
249 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
253 vt102_clip_cursor (v, v->screen_start, v->screen_end);
259 vt102_cursor_carriage_return (VT102 * v)
261 /*FISH*/ v->pos.x = v->top_margin.x;
266 vt102_cursor_advance_line (VT102 * v)
268 int couldscroll = in_margins (v, v->pos);
270 /*have wraped off end of last line in scrolling region */
271 /* (|| not necessary, but shuts compiler up */
272 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
275 vt102_log_line (v, v->pos.y);
276 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
286 vt102_cursor_advance (VT102 * v)
289 if (v->pos.x < v->bottom_margin.x)
301 vt102_do_pending_wrap (VT102 * v)
303 int couldscroll = in_margins (v, v->pos);
304 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
306 if (!v->pending_wrap)
310 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
311 v->pos.y, autowrap, couldscroll);
314 /*End of line but no autowrap, nothing to do*/
318 /*End of screen and not allowed to scroll, nothing to do*/
319 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
324 v->pos.x = v->top_margin.x;
331 vt102_cursor_advance_line (v);
336 vt102_cursor_retard (VT102 * v)
338 if (v->pos.x != v->top_margin.x)
345 vt102_reset_tabs (VT102 * v)
349 memset (v->tabs, 0, sizeof (v->tabs));
351 for (i = 0; i < VT102_COLS; i += 8)
357 vt102_cursor_advance_tab (VT102 * v)
359 if (v->pos.x == v->bottom_margin.x)
361 while (v->pos.x < v->bottom_margin.x)
364 if (v->tabs[v->pos.x])
370 vt102_cursor_home (VT102 * v)
372 v->pos = v->top_margin;
373 vt102_cursor_normalize (v);
378 vt102_cursor_absolute (VT102 * v, int x, int y)
380 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
382 v->pos.x = x + v->top_margin.x;
383 v->pos.y = y + v->top_margin.x;
390 vt102_cursor_normalize (v);
394 vt102_cursor_relative (VT102 * v, int x, int y)
398 vt102_cursor_normalize (v);
405 vt102_delete_from_line (VT102 * v, CRT_Pos p)
407 int n = v->bottom_margin.x - p.x;
415 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
416 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
419 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
420 /*But not attr due to vt102 bug*/
424 vt102_insert_into_line (VT102 * v, CRT_Pos p)
426 int n = v->bottom_margin.x - p.x;
434 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
435 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
438 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
439 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
445 vt102_change_mode (VT102 * v, int private, char *ns, int set)
461 if (m >= VT102_NMODES)
466 v->private_modes[m] = set;
469 case VT102_PRIVATE_MODE_CURSOR_MODE:
470 if (v->application_keypad_mode)
471 v->private_modes[m] = 0;
472 fprintf (stderr, "APPLICATION CURSOR MODE %d\n",
473 v->private_modes[m]);
475 case VT102_PRIVATE_MODE_ORIGIN_MODE:
476 vt102_cursor_home (v);
485 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
490 vt102_parse_mode_string (VT102 * v, char *buf, int len)
493 char last = buf[len - 1];
497 memset (num, 0, sizeof (num));
498 o = sizeof (num) - 1;
516 vt102_change_mode (v, private, &num[o], last == 'h');
517 memset (num, 0, sizeof (num));
518 o = sizeof (num) - 1;
533 vt102_change_mode (v, private, &num[o], last == 'h');
539 vt102_change_attr (VT102 * v, char *na)
556 v->attr = CRT_ATTR_NORMAL;
559 v->attr |= CRT_ATTR_BOLD;
563 v->attr &= ~CRT_ATTR_BOLD;
566 v->attr |= CRT_ATTR_UNDERLINE;
569 v->attr &= ~CRT_ATTR_UNDERLINE;
572 v->attr |= CRT_ATTR_BLINK;
575 v->attr &= ~CRT_ATTR_BLINK;
578 v->attr |= CRT_ATTR_REVERSE;
581 v->attr &= ~CRT_ATTR_REVERSE;
586 fprintf (stderr, "unhandled SGR %d\n", a);
594 vt102_parse_attr_string (VT102 * v, char *buf, int len)
597 char last = buf[len - 1];
601 memset (num, 0, sizeof (num));
602 o = sizeof (num) - 1;
613 vt102_change_attr (v, &num[o]);
614 memset (num, 0, sizeof (num));
615 o = sizeof (num) - 1;
629 vt102_change_attr (v, &num[o]);
633 vt102_save_state (VT102 * v)
635 v->saved.pos = v->pos;
636 v->saved.attr = v->attr;
637 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
641 vt102_restore_state (VT102 * v)
643 v->pos = v->saved.pos;
644 v->attr = v->saved.attr;
645 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
646 vt102_cursor_normalize (v);
651 vt102_parse_esc (VT102 * v, int c)
656 if (v->pos.y == v->bottom_margin.y)
658 vt102_log_line (v, v->pos.y);
659 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
663 vt102_cursor_relative (v, 0, 1);
670 if (v->pos.y == v->top_margin.y)
672 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
676 vt102_cursor_relative (v, 0, -1);
680 vt102_save_state (v);
683 vt102_restore_state (v);
686 v->application_keypad_mode = 1;
689 v->application_keypad_mode = 0;
693 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
700 vt102_parse_csi (VT102 * v, char *buf, int len)
729 vt102_cursor_relative (v, 0, -narg);
732 vt102_cursor_relative (v, 0, narg);
735 vt102_cursor_relative (v, narg, 0);
738 vt102_cursor_relative (v, -narg, 0);
741 vt102_cursor_relative (v, 0, narg);
742 vt102_cursor_carriage_return (v);
745 vt102_cursor_relative (v, 0, -narg);
746 vt102_cursor_carriage_return (v);
749 vt102_cursor_absolute (v, narg - 1, v->pos.y);
758 ptr = index (arg, ';');
760 x = atoi (ptr + 1) - 1;
764 vt102_cursor_absolute (v, x, y);
771 crt_erase (&v->crt, v->pos, v->screen_end, 1);
774 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
780 CRT_Pos ls = { 0, v->pos.y };
781 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
783 narg = 0; /*Different default */
788 crt_erase (&v->crt, v->pos, le, 1);
791 crt_erase (&v->crt, ls, v->pos, 1);
794 crt_erase (&v->crt, ls, le, 1);
802 vt102_delete_from_line (v, v->pos);
805 if ((v->pos.y >= v->top_margin.y)
806 && (v->pos.y <= v->bottom_margin.y))
809 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
814 if ((v->pos.y >= v->top_margin.y)
815 && (v->pos.y <= v->bottom_margin.y))
818 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
824 narg = 0; /*Different default */
829 v->tabs[v->pos.x] = 0;
832 memset (v->tabs, 0, sizeof (v->tabs));
839 vt102_parse_mode_string (v, &buf[1], len - 1);
843 vt102_parse_attr_string (v, &buf[1], len - 1);
846 v->top_margin = v->screen_start;
847 v->bottom_margin = v->screen_end;
849 if ((len > 2) && (ptr = index (arg, ';')))
852 v->top_margin.y = narg - 1;
853 v->bottom_margin.y = atoi (ptr) - 1;
856 if (v->top_margin.y < v->screen_start.y)
857 v->top_margin.y = v->screen_start.y;
858 if (v->top_margin.y > v->screen_end.y)
859 v->top_margin.y = v->screen_end.y;
860 if (v->bottom_margin.y < v->screen_start.y)
861 v->bottom_margin.y = v->screen_start.y;
862 if (v->bottom_margin.y > v->screen_end.y)
863 v->bottom_margin.y = v->screen_end.y;
865 vt102_cursor_home (v);
868 v->saved.pos = v->pos;
871 v->pos = v->saved.pos;
872 vt102_cursor_normalize (v);
878 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
885 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
895 vt102_status_line (VT102 * v, char *str)
898 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
902 ca->attr = CRT_ATTR_REVERSE;
913 vt102_parse_char (VT102 * v, int c)
915 VT102_parser *p = &v->parser;
919 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
920 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
924 p->csi_buf[p->csi_ptr++] = c;
925 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
927 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
931 else if (p->in_escape)
936 p->csi_buf[p->csi_ptr++] = c;
943 vt102_parse_esc (v, c);
958 tty_write (v->tty, "vt102", 5);
964 vt102_cursor_retard (v);
967 vt102_cursor_advance_tab (v);
972 vt102_cursor_advance_line (v);
973 if (!v->modes[VT102_MODE_NEWLINE_MODE])
976 vt102_cursor_carriage_return (v);
1001 /*regular character */ default:
1002 vt102_do_pending_wrap (v);
1004 if (v->modes[VT102_MODE_INSERT])
1005 vt102_insert_into_line (v, v->pos);
1007 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1008 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1009 vt102_cursor_advance (v);
1013 v->crt.pos = v->pos;
1014 v->crt.hide_cursor =
1015 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1017 if (v->current_line.y != v->pos.y)
1019 vt102_log_line (v, v->current_line.y);
1020 v->current_line = v->pos;
1023 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1026 vt102_parse (VT102 * v, char *buf, int len)
1029 vt102_parse_char (v, *(buf++));
1034 vt102_parser_reset (VT102_parser * p)
1043 vt102_send (VT102 * v, uint8_t key)
1046 fprintf(stderr,"vts: %d(%c)\n",key,(key>31)?key:' ');
1047 if ((key > 31) && (key < 127))
1049 tty_write (v->tty, &key, 1);
1068 tty_write (v->tty, &key, 1);
1071 tty_write (v->tty, &key, 1);
1072 if (v->modes[VT102_MODE_NEWLINE_MODE])
1075 tty_write (v->tty, &c, 1);
1091 tty_write (v->tty, &key, 1);
1099 tty_write (v->tty, &key, 1);
1110 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1112 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1113 tty_write (v->tty, &buf, sizeof (buf));
1117 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1118 tty_write (v->tty, &buf, sizeof (buf));
1136 if (v->application_keypad_mode)
1138 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1139 tty_write (v->tty, &buf, sizeof (buf));
1143 static char kpoff[KEY_NUM] = {
1162 tty_write (v->tty, &kpoff[key], 1);
1166 if (v->application_keypad_mode)
1168 uint8_t buf[] = { 033, 'O', 'M' };
1169 tty_write (v->tty, &buf, sizeof (buf));
1174 tty_write (v->tty, &c, 1);
1175 if (v->modes[VT102_MODE_NEWLINE_MODE])
1178 tty_write (v->tty, &c, 1);
1187 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1188 tty_write (v->tty, &buf, sizeof (buf));
1196 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1197 tty_write (v->tty, &buf, sizeof (buf));
1205 vt102_reset (VT102 * v)
1207 VT102_parser *p = &v->parser;
1209 vt102_parser_reset (p);
1213 v->application_keypad_mode = 0;
1215 v->current_line = v->pos;
1216 v->pending_wrap = 0;
1218 v->screen_start.x = 0;
1219 v->screen_start.y = 0;
1220 v->screen_end.x = VT102_COLS - 1;
1221 v->screen_end.y = VT102_ROWS - 1;
1223 v->top_margin = v->screen_start;
1224 v->bottom_margin = v->screen_end;
1226 memset (v->modes, 0, VT102_NMODES);
1227 memset (v->private_modes, 0, VT102_NMODES);
1229 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1230 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1231 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1232 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1234 vt102_cursor_home (v);
1235 vt102_reset_tabs (v);
1236 v->current_line = v->pos;
1238 vt102_save_state (v);
1240 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1245 vt102_dispatch (VT102 * v)
1250 red = tty_read (v->tty, buf, sizeof (buf));
1258 vt102_parse (v, buf, red);
1268 v = (VT102 *) malloc (sizeof (VT102));
1278 vt102_free (VT102 * v)