4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.13 2008/02/07 00:39:59 james
14 * *** empty log message ***
16 * Revision 1.12 2008/02/07 00:39:13 james
17 * *** empty log message ***
19 * Revision 1.11 2008/02/06 20:26:58 james
20 * *** empty log message ***
22 * Revision 1.10 2008/02/06 17:53:28 james
23 * *** empty log message ***
25 * Revision 1.9 2008/02/06 15:53:22 james
26 * *** empty log message ***
28 * Revision 1.8 2008/02/06 11:49:47 james
29 * *** empty log message ***
31 * Revision 1.7 2008/02/06 11:30:37 james
32 * *** empty log message ***
34 * Revision 1.6 2008/02/05 01:11:46 james
35 * *** empty log message ***
37 * Revision 1.5 2008/02/04 20:23:55 james
38 * *** empty log message ***
40 * Revision 1.4 2008/02/04 05:45:55 james
43 * Revision 1.3 2008/02/04 02:05:06 james
44 * *** empty log message ***
46 * Revision 1.2 2008/02/04 01:32:39 james
47 * *** empty log message ***
49 * Revision 1.1 2008/02/03 23:36:41 james
50 * *** empty log message ***
59 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
61 vt100|vt100-am|dec vt100 (w/advanced video):\
63 :co#80:it#8:li#24:vt#3:\
64 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
66 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
67 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
68 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
69 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
70 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
71 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
72 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
73 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
74 :us=\E[4m:tc=vt100+fnkeys:
76 vt100+fnkeys|dec vt100 numeric keypad:\
77 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
80 vt100+pfkeys|dec vt100 numeric keypad:\
81 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
83 vt100+keypad|dec vt100 numeric keypad no fkeys:\
84 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
89 so the parser needs to be able to at least do
162 if ((c >= 'a') && (c <= 'z'))
164 if ((c >= 'A') && (c <= 'Z'))
183 in_margins (VT102 * v, CRT_Pos p)
185 if (v->pos.x < v->top_margin.x)
187 if (v->pos.y < v->top_margin.y)
190 if (v->pos.x > v->bottom_margin.x)
192 if (v->pos.y > v->bottom_margin.y)
199 vt102_log_line (VT102 * v, int line)
202 CRT_Pos e = { VT102_COLS - 1, line };
203 CRT_Pos p = { 0, line };
206 log = fopen ("log", "a+");
212 for (; e.x > 0; --e.x)
214 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
218 for (; p.x <= e.x; ++p.x)
220 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
231 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
246 vt102_cursor_normalize (VT102 * v)
248 CRT_Pos *top, *bottom;
250 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
252 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
256 vt102_clip_cursor (v, v->screen_start, v->screen_end);
262 vt102_cursor_carriage_return (VT102 * v)
264 /*FISH*/ v->pos.x = v->top_margin.x;
269 vt102_cursor_advance_line (VT102 * v)
271 int couldscroll = in_margins (v, v->pos);
273 /*have wraped off end of last line in scrolling region */
274 /* (|| not necessary, but shuts compiler up */
275 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
278 vt102_log_line (v, v->pos.y);
279 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
289 vt102_cursor_advance (VT102 * v)
292 if (v->pos.x < v->bottom_margin.x)
304 vt102_do_pending_wrap (VT102 * v)
306 int couldscroll = in_margins (v, v->pos);
307 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
309 if (!v->pending_wrap)
313 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
314 v->pos.y, autowrap, couldscroll);
317 /*End of line but no autowrap, nothing to do*/
321 /*End of screen and not allowed to scroll, nothing to do*/
322 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
327 v->pos.x = v->top_margin.x;
334 vt102_cursor_advance_line (v);
339 vt102_cursor_retard (VT102 * v)
341 if (v->pos.x != v->top_margin.x)
348 vt102_reset_tabs (VT102 * v)
352 memset (v->tabs, 0, sizeof (v->tabs));
354 for (i = 0; i < VT102_COLS; i += 8)
360 vt102_cursor_advance_tab (VT102 * v)
362 if (v->pos.x == v->bottom_margin.x)
364 while (v->pos.x < v->bottom_margin.x)
367 if (v->tabs[v->pos.x])
373 vt102_cursor_home (VT102 * v)
375 v->pos = v->top_margin;
376 vt102_cursor_normalize (v);
381 vt102_cursor_absolute (VT102 * v, int x, int y)
383 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
385 v->pos.x = x + v->top_margin.x;
386 v->pos.y = y + v->top_margin.x;
393 vt102_cursor_normalize (v);
397 vt102_cursor_relative (VT102 * v, int x, int y)
401 vt102_cursor_normalize (v);
408 vt102_delete_from_line (VT102 * v, CRT_Pos p)
410 int n = v->bottom_margin.x - p.x;
418 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
419 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
422 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
423 /*But not attr due to vt102 bug*/
427 vt102_insert_into_line (VT102 * v, CRT_Pos p)
429 int n = v->bottom_margin.x - p.x;
437 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
438 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
441 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
442 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
448 vt102_change_mode (VT102 * v, int private, char *ns, int set)
464 if (m >= VT102_NMODES)
469 v->private_modes[m] = set;
472 case VT102_PRIVATE_MODE_CURSOR_MODE:
473 if (v->application_keypad_mode)
474 v->private_modes[m] = 0;
475 fprintf (stderr, "APPLICATION CURSOR MODE %d\n",
476 v->private_modes[m]);
478 case VT102_PRIVATE_MODE_ORIGIN_MODE:
479 vt102_cursor_home (v);
488 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
493 vt102_parse_mode_string (VT102 * v, char *buf, int len)
496 char last = buf[len - 1];
500 memset (num, 0, sizeof (num));
501 o = sizeof (num) - 1;
519 vt102_change_mode (v, private, &num[o], last == 'h');
520 memset (num, 0, sizeof (num));
521 o = sizeof (num) - 1;
536 vt102_change_mode (v, private, &num[o], last == 'h');
542 vt102_change_attr (VT102 * v, char *na)
559 v->attr = CRT_ATTR_NORMAL;
562 v->attr |= CRT_ATTR_BOLD;
566 v->attr &= ~CRT_ATTR_BOLD;
569 v->attr |= CRT_ATTR_UNDERLINE;
572 v->attr &= ~CRT_ATTR_UNDERLINE;
575 v->attr |= CRT_ATTR_BLINK;
578 v->attr &= ~CRT_ATTR_BLINK;
581 v->attr |= CRT_ATTR_REVERSE;
584 v->attr &= ~CRT_ATTR_REVERSE;
589 fprintf (stderr, "unhandled SGR %d\n", a);
597 vt102_parse_attr_string (VT102 * v, char *buf, int len)
600 char last = buf[len - 1];
604 memset (num, 0, sizeof (num));
605 o = sizeof (num) - 1;
616 vt102_change_attr (v, &num[o]);
617 memset (num, 0, sizeof (num));
618 o = sizeof (num) - 1;
632 vt102_change_attr (v, &num[o]);
636 vt102_save_state (VT102 * v)
638 v->saved.pos = v->pos;
639 v->saved.attr = v->attr;
640 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
644 vt102_restore_state (VT102 * v)
646 v->pos = v->saved.pos;
647 v->attr = v->saved.attr;
648 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
649 vt102_cursor_normalize (v);
654 vt102_parse_esc (VT102 * v, int c)
659 if (v->pos.y == v->bottom_margin.y)
661 vt102_log_line (v, v->pos.y);
662 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
666 vt102_cursor_relative (v, 0, 1);
673 if (v->pos.y == v->top_margin.y)
675 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
679 vt102_cursor_relative (v, 0, -1);
683 vt102_save_state (v);
686 vt102_restore_state (v);
689 v->application_keypad_mode = 1;
692 v->application_keypad_mode = 0;
696 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
703 vt102_parse_csi (VT102 * v, char *buf, int len)
732 vt102_cursor_relative (v, 0, -narg);
735 vt102_cursor_relative (v, 0, narg);
738 vt102_cursor_relative (v, narg, 0);
741 vt102_cursor_relative (v, -narg, 0);
744 vt102_cursor_relative (v, 0, narg);
745 vt102_cursor_carriage_return (v);
748 vt102_cursor_relative (v, 0, -narg);
749 vt102_cursor_carriage_return (v);
752 vt102_cursor_absolute (v, narg - 1, v->pos.y);
761 ptr = index (arg, ';');
763 x = atoi (ptr + 1) - 1;
767 vt102_cursor_absolute (v, x, y);
774 crt_erase (&v->crt, v->pos, v->screen_end, 1);
777 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
783 CRT_Pos ls = { 0, v->pos.y };
784 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
786 narg = 0; /*Different default */
791 crt_erase (&v->crt, v->pos, le, 1);
794 crt_erase (&v->crt, ls, v->pos, 1);
797 crt_erase (&v->crt, ls, le, 1);
805 vt102_delete_from_line (v, v->pos);
808 if ((v->pos.y >= v->top_margin.y)
809 && (v->pos.y <= v->bottom_margin.y))
812 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
817 if ((v->pos.y >= v->top_margin.y)
818 && (v->pos.y <= v->bottom_margin.y))
821 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
827 narg = 0; /*Different default */
832 v->tabs[v->pos.x] = 0;
835 memset (v->tabs, 0, sizeof (v->tabs));
842 vt102_parse_mode_string (v, &buf[1], len - 1);
846 vt102_parse_attr_string (v, &buf[1], len - 1);
849 v->top_margin = v->screen_start;
850 v->bottom_margin = v->screen_end;
852 if ((len > 2) && (ptr = index (arg, ';')))
855 v->top_margin.y = narg - 1;
856 v->bottom_margin.y = atoi (ptr) - 1;
859 if (v->top_margin.y < v->screen_start.y)
860 v->top_margin.y = v->screen_start.y;
861 if (v->top_margin.y > v->screen_end.y)
862 v->top_margin.y = v->screen_end.y;
863 if (v->bottom_margin.y < v->screen_start.y)
864 v->bottom_margin.y = v->screen_start.y;
865 if (v->bottom_margin.y > v->screen_end.y)
866 v->bottom_margin.y = v->screen_end.y;
868 vt102_cursor_home (v);
871 v->saved.pos = v->pos;
874 v->pos = v->saved.pos;
875 vt102_cursor_normalize (v);
881 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
888 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
898 vt102_status_line (VT102 * v, char *str)
901 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
905 ca->attr = CRT_ATTR_REVERSE;
916 vt102_parse_char (VT102 * v, int c)
918 VT102_parser *p = &v->parser;
922 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
923 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
927 p->csi_buf[p->csi_ptr++] = c;
928 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
930 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
934 else if (p->in_escape)
939 p->csi_buf[p->csi_ptr++] = c;
946 vt102_parse_esc (v, c);
961 tty_write (v->tty, "vt102", 5);
967 vt102_cursor_retard (v);
970 vt102_cursor_advance_tab (v);
975 vt102_cursor_advance_line (v);
976 if (!v->modes[VT102_MODE_NEWLINE_MODE])
979 vt102_cursor_carriage_return (v);
1004 /*regular character */ default:
1005 vt102_do_pending_wrap (v);
1007 if (v->modes[VT102_MODE_INSERT])
1008 vt102_insert_into_line (v, v->pos);
1010 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1011 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1012 vt102_cursor_advance (v);
1016 v->crt.pos = v->pos;
1017 v->crt.hide_cursor =
1018 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1020 if (v->current_line.y != v->pos.y)
1022 vt102_log_line (v, v->current_line.y);
1023 v->current_line = v->pos;
1026 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1029 vt102_parse (VT102 * v, char *buf, int len)
1032 vt102_parse_char (v, *(buf++));
1037 vt102_parser_reset (VT102_parser * p)
1046 vt102_send (VT102 * v, uint8_t key)
1050 fprintf(stderr,"vts: %d(%c)\n",key,(key>31)?key:' ');
1052 if ((key > 31) && (key < 127))
1054 tty_write (v->tty, &key, 1);
1073 tty_write (v->tty, &key, 1);
1076 tty_write (v->tty, &key, 1);
1077 if (v->modes[VT102_MODE_NEWLINE_MODE])
1080 tty_write (v->tty, &c, 1);
1096 tty_write (v->tty, &key, 1);
1104 tty_write (v->tty, &key, 1);
1115 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1117 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1118 tty_write (v->tty, &buf, sizeof (buf));
1122 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1123 tty_write (v->tty, &buf, sizeof (buf));
1141 if (v->application_keypad_mode)
1143 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1144 tty_write (v->tty, &buf, sizeof (buf));
1148 static char kpoff[KEY_NUM] = {
1167 tty_write (v->tty, &kpoff[key], 1);
1171 if (v->application_keypad_mode)
1173 uint8_t buf[] = { 033, 'O', 'M' };
1174 tty_write (v->tty, &buf, sizeof (buf));
1179 tty_write (v->tty, &c, 1);
1180 if (v->modes[VT102_MODE_NEWLINE_MODE])
1183 tty_write (v->tty, &c, 1);
1192 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1193 tty_write (v->tty, &buf, sizeof (buf));
1201 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1202 tty_write (v->tty, &buf, sizeof (buf));
1210 vt102_reset (VT102 * v)
1212 VT102_parser *p = &v->parser;
1214 vt102_parser_reset (p);
1218 v->application_keypad_mode = 0;
1220 v->current_line = v->pos;
1221 v->pending_wrap = 0;
1223 v->screen_start.x = 0;
1224 v->screen_start.y = 0;
1225 v->screen_end.x = VT102_COLS - 1;
1226 v->screen_end.y = VT102_ROWS - 1;
1228 v->top_margin = v->screen_start;
1229 v->bottom_margin = v->screen_end;
1231 memset (v->modes, 0, VT102_NMODES);
1232 memset (v->private_modes, 0, VT102_NMODES);
1234 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1235 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1236 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1237 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1239 vt102_cursor_home (v);
1240 vt102_reset_tabs (v);
1241 v->current_line = v->pos;
1243 vt102_save_state (v);
1245 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1250 vt102_dispatch (VT102 * v)
1255 red = tty_read (v->tty, buf, sizeof (buf));
1263 vt102_parse (v, buf, red);
1273 v = (VT102 *) malloc (sizeof (VT102));
1283 vt102_free (VT102 * v)