4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.9 2008/02/06 15:53:22 james
14 * *** empty log message ***
16 * Revision 1.8 2008/02/06 11:49:47 james
17 * *** empty log message ***
19 * Revision 1.7 2008/02/06 11:30:37 james
20 * *** empty log message ***
22 * Revision 1.6 2008/02/05 01:11:46 james
23 * *** empty log message ***
25 * Revision 1.5 2008/02/04 20:23:55 james
26 * *** empty log message ***
28 * Revision 1.4 2008/02/04 05:45:55 james
31 * Revision 1.3 2008/02/04 02:05:06 james
32 * *** empty log message ***
34 * Revision 1.2 2008/02/04 01:32:39 james
35 * *** empty log message ***
37 * Revision 1.1 2008/02/03 23:36:41 james
38 * *** empty log message ***
47 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
49 vt100|vt100-am|dec vt100 (w/advanced video):\
51 :co#80:it#8:li#24:vt#3:\
52 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
54 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
55 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
56 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
57 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
58 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
59 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
60 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
61 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
62 :us=\E[4m:tc=vt100+fnkeys:
64 vt100+fnkeys|dec vt100 numeric keypad:\
65 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
68 vt100+pfkeys|dec vt100 numeric keypad:\
69 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
71 vt100+keypad|dec vt100 numeric keypad no fkeys:\
72 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
77 so the parser needs to be able to at least do
150 if ((c >= 'a') && (c <= 'z'))
152 if ((c >= 'A') && (c <= 'Z'))
171 in_margins (VT102 * v, CRT_Pos p)
173 if (v->pos.x < v->top_margin.x)
175 if (v->pos.y < v->top_margin.y)
178 if (v->pos.x > v->bottom_margin.x)
180 if (v->pos.y > v->bottom_margin.y)
187 vt102_log_line (VT102 * v, int line)
189 CRT_Pos e = { VT102_COLS - 1, line };
190 CRT_Pos p = { 0, line };
192 for (; e.x > 0; --e.x)
194 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
198 for (; p.x <= e.x; ++p.x)
200 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
207 fputc ('\n', stderr);
211 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
226 vt102_cursor_normalize (VT102 * v)
228 CRT_Pos *top, *bottom;
230 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
232 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
236 vt102_clip_cursor (v, v->screen_start, v->screen_end);
242 vt102_cursor_carriage_return (VT102 * v)
244 /*FISH*/ v->pos.x = v->top_margin.x;
249 vt102_cursor_advance_line (VT102 * v)
251 int couldscroll = in_margins (v, v->pos);
253 /*have wraped off end of last line in scrolling region */
254 /* (|| not necessary, but shuts compiler up */
255 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
258 vt102_log_line (v, v->pos.y);
259 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
269 vt102_cursor_advance (VT102 * v)
272 if (v->pos.x < v->bottom_margin.x)
284 vt102_do_pending_wrap (VT102 * v)
286 int couldscroll = in_margins (v, v->pos);
287 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
289 if (!v->pending_wrap)
293 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
294 v->pos.y, autowrap, couldscroll);
297 /*End of line but no autowrap, nothing to do*/
301 /*End of screen and not allowed to scroll, nothing to do*/
302 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
307 v->pos.x = v->top_margin.x;
314 vt102_cursor_advance_line (v);
319 vt102_cursor_retard (VT102 * v)
321 if (v->pos.x != v->top_margin.x)
328 vt102_reset_tabs (VT102 * v)
332 memset (v->tabs, 0, sizeof (v->tabs));
334 for (i = 0; i < VT102_COLS; i += 8)
340 vt102_cursor_advance_tab (VT102 * v)
342 if (v->pos.x == v->bottom_margin.x)
344 while (v->pos.x < v->bottom_margin.x)
347 if (v->tabs[v->pos.x])
353 vt102_cursor_home (VT102 * v)
355 v->pos = v->top_margin;
356 vt102_cursor_normalize (v);
360 vt102_cursor_absolute (VT102 * v, int x, int y)
362 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
364 v->pos.x = x + v->top_margin.x;
365 v->pos.y = y + v->top_margin.x;
372 vt102_cursor_normalize (v);
376 vt102_cursor_relative (VT102 * v, int x, int y)
380 vt102_cursor_normalize (v);
387 vt102_delete_from_line (VT102 * v, CRT_Pos p)
389 int n = v->bottom_margin.x - p.x;
397 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
398 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
401 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
402 /*But not attr due to vt102 bug*/
406 vt102_insert_into_line (VT102 * v, CRT_Pos p)
408 int n = v->bottom_margin.x - p.x;
416 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
417 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
420 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
421 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
427 vt102_change_mode (VT102 * v, int private, char *ns, int set)
443 if (m >= VT102_NMODES)
448 v->private_modes[m] = set;
451 case VT102_PRIVATE_MODE_ORIGIN_MODE:
452 vt102_cursor_home (v);
461 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
466 vt102_parse_mode_string (VT102 * v, char *buf, int len)
469 char last = buf[len - 1];
473 memset (num, 0, sizeof (num));
474 o = sizeof (num) - 1;
492 vt102_change_mode (v, private, &num[o], last == 'h');
493 memset (num, 0, sizeof (num));
494 o = sizeof (num) - 1;
509 vt102_change_mode (v, private, &num[o], last == 'h');
515 vt102_change_attr (VT102 * v, char *na)
532 v->attr = CRT_ATTR_NORMAL;
535 v->attr |= CRT_ATTR_BOLD;
538 v->attr &= ~CRT_ATTR_BOLD;
541 v->attr |= CRT_ATTR_UNDERLINE;
544 v->attr &= ~CRT_ATTR_UNDERLINE;
547 v->attr |= CRT_ATTR_BLINK;
550 v->attr &= ~CRT_ATTR_BLINK;
553 v->attr |= CRT_ATTR_REVERSE;
556 v->attr &= ~CRT_ATTR_REVERSE;
559 fprintf (stderr, "unhandled SGR %d\n", a);
566 vt102_parse_attr_string (VT102 * v, char *buf, int len)
569 char last = buf[len - 1];
573 memset (num, 0, sizeof (num));
574 o = sizeof (num) - 1;
585 vt102_change_attr (v, &num[o]);
586 memset (num, 0, sizeof (num));
587 o = sizeof (num) - 1;
601 vt102_change_attr (v, &num[o]);
605 vt102_parse_esc (VT102 * v, int c)
614 char s[] = { 033, c };
615 /*Horrid hack for the moment in testing change the key mode of the upstream te*/
621 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
626 vt102_parse_csi (VT102 * v, char *buf, int len)
655 vt102_cursor_relative (v, 0, -narg);
658 vt102_cursor_relative (v, 0, narg);
661 vt102_cursor_relative (v, narg, 0);
664 vt102_cursor_relative (v, -narg, 0);
672 ptr = index (arg, ';');
674 x = atoi (ptr + 1) - 1;
678 vt102_cursor_absolute (v, x, y);
685 crt_erase (&v->crt, v->pos, v->screen_end, 1);
688 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
694 CRT_Pos ls = { 0, v->pos.y };
695 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
697 narg = 0; /*Different default */
702 crt_erase (&v->crt, v->pos, le, 1);
705 crt_erase (&v->crt, ls, v->pos, 1);
708 crt_erase (&v->crt, ls, le, 1);
716 vt102_delete_from_line (v, v->pos);
719 if ((v->pos.y >= v->top_margin.y)
720 && (v->pos.y <= v->bottom_margin.y))
723 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
728 if ((v->pos.y >= v->top_margin.y)
729 && (v->pos.y <= v->bottom_margin.y))
732 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
738 narg = 0; /*Different default */
743 v->tabs[v->pos.x] = 0;
746 memset (v->tabs, 0, sizeof (v->tabs));
753 vt102_parse_mode_string (v, &buf[1], len - 1);
757 vt102_parse_attr_string (v, &buf[1], len - 1);
760 v->top_margin = v->screen_start;
761 v->bottom_margin = v->screen_end;
763 if ((len > 2) && (ptr = index (arg, ';')))
766 v->top_margin.y = narg - 1;
767 v->bottom_margin.y = atoi (ptr) - 1;
770 if (v->top_margin.y < v->screen_start.y)
771 v->top_margin.y = v->screen_start.y;
772 if (v->top_margin.y > v->screen_end.y)
773 v->top_margin.y = v->screen_end.y;
774 if (v->bottom_margin.y < v->screen_start.y)
775 v->bottom_margin.y = v->screen_start.y;
776 if (v->bottom_margin.y > v->screen_end.y)
777 v->bottom_margin.y = v->screen_end.y;
779 vt102_cursor_home (v);
783 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
787 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
795 vt102_status_line (VT102 * v, char *str)
798 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
802 ca->attr = CRT_ATTR_REVERSE;
813 vt102_parse_char (VT102 * v, int c)
815 VT102_parser *p = &v->parser;
818 fprintf (stderr, "%c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
819 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
823 p->csi_buf[p->csi_ptr++] = c;
824 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
826 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
830 else if (p->in_escape)
835 p->csi_buf[p->csi_ptr++] = c;
842 vt102_parse_esc (v, c);
860 vt102_cursor_retard (v);
863 vt102_cursor_advance_tab (v);
868 vt102_cursor_advance_line (v);
869 if (!v->modes[VT102_MODE_NEWLINE_MODE])
872 vt102_cursor_carriage_return (v);
897 /*regular character */ default:
898 vt102_do_pending_wrap (v);
900 if (v->modes[VT102_MODE_INSERT])
901 vt102_insert_into_line (v, v->pos);
903 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
904 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
905 vt102_cursor_advance (v);
911 if (v->current_line.y != v->pos.y)
913 vt102_log_line (v, v->current_line.y);
914 v->current_line = v->pos;
917 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
921 vt102_parser_reset (VT102_parser * p)
929 vt102_reset (VT102 * v)
931 VT102_parser *p = &v->parser;
933 vt102_parser_reset (p);
938 v->current_line = v->pos;
941 v->screen_start.x = 0;
942 v->screen_start.y = 0;
943 v->screen_end.x = VT102_COLS - 1;
944 v->screen_end.y = VT102_ROWS - 1;
946 v->top_margin = v->screen_start;
947 v->bottom_margin = v->screen_end;
949 memset (v->modes, 0, VT102_NMODES);
950 memset (v->private_modes, 0, VT102_NMODES);
952 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
953 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
955 vt102_cursor_home (v);
956 vt102_reset_tabs (v);
957 v->current_line = v->pos;