4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.11 2008/02/06 20:26:58 james
14 * *** empty log message ***
16 * Revision 1.10 2008/02/06 17:53:28 james
17 * *** empty log message ***
19 * Revision 1.9 2008/02/06 15:53:22 james
20 * *** empty log message ***
22 * Revision 1.8 2008/02/06 11:49:47 james
23 * *** empty log message ***
25 * Revision 1.7 2008/02/06 11:30:37 james
26 * *** empty log message ***
28 * Revision 1.6 2008/02/05 01:11:46 james
29 * *** empty log message ***
31 * Revision 1.5 2008/02/04 20:23:55 james
32 * *** empty log message ***
34 * Revision 1.4 2008/02/04 05:45:55 james
37 * Revision 1.3 2008/02/04 02:05:06 james
38 * *** empty log message ***
40 * Revision 1.2 2008/02/04 01:32:39 james
41 * *** empty log message ***
43 * Revision 1.1 2008/02/03 23:36:41 james
44 * *** empty log message ***
53 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
55 vt100|vt100-am|dec vt100 (w/advanced video):\
57 :co#80:it#8:li#24:vt#3:\
58 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
60 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
61 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
62 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
63 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
64 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
65 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
66 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
67 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
68 :us=\E[4m:tc=vt100+fnkeys:
70 vt100+fnkeys|dec vt100 numeric keypad:\
71 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
74 vt100+pfkeys|dec vt100 numeric keypad:\
75 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
77 vt100+keypad|dec vt100 numeric keypad no fkeys:\
78 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
83 so the parser needs to be able to at least do
156 if ((c >= 'a') && (c <= 'z'))
158 if ((c >= 'A') && (c <= 'Z'))
177 in_margins (VT102 * v, CRT_Pos p)
179 if (v->pos.x < v->top_margin.x)
181 if (v->pos.y < v->top_margin.y)
184 if (v->pos.x > v->bottom_margin.x)
186 if (v->pos.y > v->bottom_margin.y)
193 vt102_log_line (VT102 * v, int line)
196 CRT_Pos e = { VT102_COLS - 1, line };
197 CRT_Pos p = { 0, line };
200 log = fopen ("log", "a+");
206 for (; e.x > 0; --e.x)
208 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
212 for (; p.x <= e.x; ++p.x)
214 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
225 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
240 vt102_cursor_normalize (VT102 * v)
242 CRT_Pos *top, *bottom;
244 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
246 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
250 vt102_clip_cursor (v, v->screen_start, v->screen_end);
256 vt102_cursor_carriage_return (VT102 * v)
258 /*FISH*/ v->pos.x = v->top_margin.x;
263 vt102_cursor_advance_line (VT102 * v)
265 int couldscroll = in_margins (v, v->pos);
267 /*have wraped off end of last line in scrolling region */
268 /* (|| not necessary, but shuts compiler up */
269 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
272 vt102_log_line (v, v->pos.y);
273 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
283 vt102_cursor_advance (VT102 * v)
286 if (v->pos.x < v->bottom_margin.x)
298 vt102_do_pending_wrap (VT102 * v)
300 int couldscroll = in_margins (v, v->pos);
301 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
303 if (!v->pending_wrap)
307 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
308 v->pos.y, autowrap, couldscroll);
311 /*End of line but no autowrap, nothing to do*/
315 /*End of screen and not allowed to scroll, nothing to do*/
316 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
321 v->pos.x = v->top_margin.x;
328 vt102_cursor_advance_line (v);
333 vt102_cursor_retard (VT102 * v)
335 if (v->pos.x != v->top_margin.x)
342 vt102_reset_tabs (VT102 * v)
346 memset (v->tabs, 0, sizeof (v->tabs));
348 for (i = 0; i < VT102_COLS; i += 8)
354 vt102_cursor_advance_tab (VT102 * v)
356 if (v->pos.x == v->bottom_margin.x)
358 while (v->pos.x < v->bottom_margin.x)
361 if (v->tabs[v->pos.x])
367 vt102_cursor_home (VT102 * v)
369 v->pos = v->top_margin;
370 vt102_cursor_normalize (v);
375 vt102_cursor_absolute (VT102 * v, int x, int y)
377 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
379 v->pos.x = x + v->top_margin.x;
380 v->pos.y = y + v->top_margin.x;
387 vt102_cursor_normalize (v);
391 vt102_cursor_relative (VT102 * v, int x, int y)
395 vt102_cursor_normalize (v);
402 vt102_delete_from_line (VT102 * v, CRT_Pos p)
404 int n = v->bottom_margin.x - p.x;
412 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
413 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
416 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
417 /*But not attr due to vt102 bug*/
421 vt102_insert_into_line (VT102 * v, CRT_Pos p)
423 int n = v->bottom_margin.x - p.x;
431 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
432 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
435 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
436 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
442 vt102_change_mode (VT102 * v, int private, char *ns, int set)
458 if (m >= VT102_NMODES)
463 v->private_modes[m] = set;
466 case VT102_PRIVATE_MODE_ORIGIN_MODE:
467 vt102_cursor_home (v);
476 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
481 vt102_parse_mode_string (VT102 * v, char *buf, int len)
484 char last = buf[len - 1];
488 memset (num, 0, sizeof (num));
489 o = sizeof (num) - 1;
507 vt102_change_mode (v, private, &num[o], last == 'h');
508 memset (num, 0, sizeof (num));
509 o = sizeof (num) - 1;
524 vt102_change_mode (v, private, &num[o], last == 'h');
530 vt102_change_attr (VT102 * v, char *na)
547 v->attr = CRT_ATTR_NORMAL;
550 v->attr |= CRT_ATTR_BOLD;
554 v->attr &= ~CRT_ATTR_BOLD;
557 v->attr |= CRT_ATTR_UNDERLINE;
560 v->attr &= ~CRT_ATTR_UNDERLINE;
563 v->attr |= CRT_ATTR_BLINK;
566 v->attr &= ~CRT_ATTR_BLINK;
569 v->attr |= CRT_ATTR_REVERSE;
572 v->attr &= ~CRT_ATTR_REVERSE;
577 fprintf (stderr, "unhandled SGR %d\n", a);
585 vt102_parse_attr_string (VT102 * v, char *buf, int len)
588 char last = buf[len - 1];
592 memset (num, 0, sizeof (num));
593 o = sizeof (num) - 1;
604 vt102_change_attr (v, &num[o]);
605 memset (num, 0, sizeof (num));
606 o = sizeof (num) - 1;
620 vt102_change_attr (v, &num[o]);
624 vt102_save_state (VT102 * v)
626 v->saved.pos = v->pos;
627 v->saved.attr = v->attr;
628 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
632 vt102_restore_state (VT102 * v)
634 v->pos = v->saved.pos;
635 v->attr = v->saved.attr;
636 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
637 vt102_cursor_normalize (v);
642 vt102_parse_esc (VT102 * v, int c)
647 if (v->pos.y == v->bottom_margin.y)
649 vt102_log_line (v, v->pos.y);
650 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
654 vt102_cursor_relative (v, 0, 1);
661 if (v->pos.y == v->top_margin.y)
663 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
667 vt102_cursor_relative (v, 0, -1);
671 vt102_save_state (v);
674 vt102_restore_state (v);
677 v->application_keypad_mode=1;
680 v->application_keypad_mode=0;
684 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
691 vt102_parse_csi (VT102 * v, char *buf, int len)
720 vt102_cursor_relative (v, 0, -narg);
723 vt102_cursor_relative (v, 0, narg);
726 vt102_cursor_relative (v, narg, 0);
729 vt102_cursor_relative (v, -narg, 0);
732 vt102_cursor_relative (v, 0, narg);
733 vt102_cursor_carriage_return (v);
736 vt102_cursor_relative (v, 0, -narg);
737 vt102_cursor_carriage_return (v);
740 vt102_cursor_absolute (v, narg - 1, v->pos.y);
749 ptr = index (arg, ';');
751 x = atoi (ptr + 1) - 1;
755 vt102_cursor_absolute (v, x, y);
762 crt_erase (&v->crt, v->pos, v->screen_end, 1);
765 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
771 CRT_Pos ls = { 0, v->pos.y };
772 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
774 narg = 0; /*Different default */
779 crt_erase (&v->crt, v->pos, le, 1);
782 crt_erase (&v->crt, ls, v->pos, 1);
785 crt_erase (&v->crt, ls, le, 1);
793 vt102_delete_from_line (v, v->pos);
796 if ((v->pos.y >= v->top_margin.y)
797 && (v->pos.y <= v->bottom_margin.y))
800 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
805 if ((v->pos.y >= v->top_margin.y)
806 && (v->pos.y <= v->bottom_margin.y))
809 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
815 narg = 0; /*Different default */
820 v->tabs[v->pos.x] = 0;
823 memset (v->tabs, 0, sizeof (v->tabs));
830 vt102_parse_mode_string (v, &buf[1], len - 1);
834 vt102_parse_attr_string (v, &buf[1], len - 1);
837 v->top_margin = v->screen_start;
838 v->bottom_margin = v->screen_end;
840 if ((len > 2) && (ptr = index (arg, ';')))
843 v->top_margin.y = narg - 1;
844 v->bottom_margin.y = atoi (ptr) - 1;
847 if (v->top_margin.y < v->screen_start.y)
848 v->top_margin.y = v->screen_start.y;
849 if (v->top_margin.y > v->screen_end.y)
850 v->top_margin.y = v->screen_end.y;
851 if (v->bottom_margin.y < v->screen_start.y)
852 v->bottom_margin.y = v->screen_start.y;
853 if (v->bottom_margin.y > v->screen_end.y)
854 v->bottom_margin.y = v->screen_end.y;
856 vt102_cursor_home (v);
859 v->saved.pos = v->pos;
862 v->pos = v->saved.pos;
863 vt102_cursor_normalize (v);
869 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
876 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
886 vt102_status_line (VT102 * v, char *str)
889 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
893 ca->attr = CRT_ATTR_REVERSE;
904 vt102_parse_char (VT102 * v, int c)
906 VT102_parser *p = &v->parser;
909 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
910 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
914 p->csi_buf[p->csi_ptr++] = c;
915 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
917 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
921 else if (p->in_escape)
926 p->csi_buf[p->csi_ptr++] = c;
933 vt102_parse_esc (v, c);
948 tty_write (v->tty, "vt102", 5);
954 vt102_cursor_retard (v);
957 vt102_cursor_advance_tab (v);
962 vt102_cursor_advance_line (v);
963 if (!v->modes[VT102_MODE_NEWLINE_MODE])
966 vt102_cursor_carriage_return (v);
991 /*regular character */ default:
992 vt102_do_pending_wrap (v);
994 if (v->modes[VT102_MODE_INSERT])
995 vt102_insert_into_line (v, v->pos);
997 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
998 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
999 vt102_cursor_advance (v);
1003 v->crt.pos = v->pos;
1004 v->crt.hide_cursor =
1005 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1007 if (v->current_line.y != v->pos.y)
1009 vt102_log_line (v, v->current_line.y);
1010 v->current_line = v->pos;
1013 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1016 vt102_parse(VT102 *v,char *buf,int len)
1019 vt102_parse_char(v,*(buf++));
1024 vt102_parser_reset (VT102_parser * p)
1033 vt102_send (VT102 * v, uint8_t key)
1036 if ((key > 31) || (key < 127))
1038 tty_write (v->t, &key, 1);
1057 tty_write (v->t, &key, 1);
1060 tty_write (v->t, &key, 1);
1061 if (v->modes[VT102_MODE_NEWLINE_MODE])
1064 tty_write (v->t, &c, 1);
1080 tty_write (v->t, &key, 1);
1088 tty_write (v->t, &key, 1);
1095 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1097 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1098 tty_write (v->t, &key, sizeof (buf));
1102 uint8_t buf[] = { 033, '[', 'O' + (key - KEY_UP) };
1103 tty_write (v->t, &key, sizeof (buf));
1116 if (v->application_keypad_mode)
1118 uint8_t buf[] = { 033, 'O', 'p' + (key - KEY_0) };
1119 tty_write (v->t, &key, sizeof (buf));
1123 c = '0' + (key - KEY_0);
1124 tty_write (v->t, &c, 1);
1128 if (v->application_keypad_mode)
1130 uint8_t buf[] = { 033, 'O', 'm' };
1131 tty_write (v->t, &key, sizeof (buf));
1136 tty_write (v->t, &c, 1);
1140 if (v->application_keypad_mode)
1142 uint8_t buf[] = { 033, 'O', 'l' };
1143 tty_write (v->t, &key, sizeof (buf));
1148 tty_write (v->t, &c, 1);
1152 if (v->application_keypad_mode)
1154 uint8_t buf[] = { 033, 'O', 'n' };
1155 tty_write (v->t, &key, sizeof (buf));
1160 tty_write (v->t, &c, 1);
1164 if (v->application_keypad_mode)
1166 uint8_t buf[] = { 033, 'O', 'M' };
1167 tty_write (v->t, &key, sizeof (buf));
1172 tty_write (v->t, &c, 1);
1173 if (v->modes[VT102_MODE_NEWLINE_MODE])
1176 tty_write (v->t, &c, 1);
1185 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1186 tty_write (v->t, &key, sizeof (buf));
1195 vt102_reset (VT102 * v)
1197 VT102_parser *p = &v->parser;
1199 vt102_parser_reset (p);
1203 v->application_keypad_mode=0;
1205 v->current_line = v->pos;
1206 v->pending_wrap = 0;
1208 v->screen_start.x = 0;
1209 v->screen_start.y = 0;
1210 v->screen_end.x = VT102_COLS - 1;
1211 v->screen_end.y = VT102_ROWS - 1;
1213 v->top_margin = v->screen_start;
1214 v->bottom_margin = v->screen_end;
1216 memset (v->modes, 0, VT102_NMODES);
1217 memset (v->private_modes, 0, VT102_NMODES);
1219 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1220 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1221 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1222 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1224 vt102_cursor_home (v);
1225 vt102_reset_tabs (v);
1226 v->current_line = v->pos;
1228 vt102_save_state (v);
1230 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1234 int vt102_dispatch(VT102 *v)
1239 red=tty_read(v->t,buf,sizeof(buf));
1240 if (red<0) return -1;
1243 vt102_parse(v,buf,red);
1248 VT102 *vt102_new(TTY *t)
1252 v=(VT102 *) malloc(sizeof(VT102));
1261 void vt102_free(VT102 *v)