4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.10 2008/02/06 17:53:28 james
14 * *** empty log message ***
16 * Revision 1.9 2008/02/06 15:53:22 james
17 * *** empty log message ***
19 * Revision 1.8 2008/02/06 11:49:47 james
20 * *** empty log message ***
22 * Revision 1.7 2008/02/06 11:30:37 james
23 * *** empty log message ***
25 * Revision 1.6 2008/02/05 01:11:46 james
26 * *** empty log message ***
28 * Revision 1.5 2008/02/04 20:23:55 james
29 * *** empty log message ***
31 * Revision 1.4 2008/02/04 05:45:55 james
34 * Revision 1.3 2008/02/04 02:05:06 james
35 * *** empty log message ***
37 * Revision 1.2 2008/02/04 01:32:39 james
38 * *** empty log message ***
40 * Revision 1.1 2008/02/03 23:36:41 james
41 * *** empty log message ***
50 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
52 vt100|vt100-am|dec vt100 (w/advanced video):\
54 :co#80:it#8:li#24:vt#3:\
55 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
57 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
58 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
59 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
60 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
61 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
62 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
63 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
64 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
65 :us=\E[4m:tc=vt100+fnkeys:
67 vt100+fnkeys|dec vt100 numeric keypad:\
68 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
71 vt100+pfkeys|dec vt100 numeric keypad:\
72 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
74 vt100+keypad|dec vt100 numeric keypad no fkeys:\
75 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
80 so the parser needs to be able to at least do
153 if ((c >= 'a') && (c <= 'z'))
155 if ((c >= 'A') && (c <= 'Z'))
174 in_margins (VT102 * v, CRT_Pos p)
176 if (v->pos.x < v->top_margin.x)
178 if (v->pos.y < v->top_margin.y)
181 if (v->pos.x > v->bottom_margin.x)
183 if (v->pos.y > v->bottom_margin.y)
190 vt102_log_line (VT102 * v, int line)
193 CRT_Pos e = { VT102_COLS - 1, line };
194 CRT_Pos p = { 0, line };
197 log=fopen("log","a+");
202 for (; e.x > 0; --e.x)
204 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
208 for (; p.x <= e.x; ++p.x)
210 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
221 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
236 vt102_cursor_normalize (VT102 * v)
238 CRT_Pos *top, *bottom;
240 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
242 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
246 vt102_clip_cursor (v, v->screen_start, v->screen_end);
252 vt102_cursor_carriage_return (VT102 * v)
254 /*FISH*/ v->pos.x = v->top_margin.x;
259 vt102_cursor_advance_line (VT102 * v)
261 int couldscroll = in_margins (v, v->pos);
263 /*have wraped off end of last line in scrolling region */
264 /* (|| not necessary, but shuts compiler up */
265 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
268 vt102_log_line (v, v->pos.y);
269 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
279 vt102_cursor_advance (VT102 * v)
282 if (v->pos.x < v->bottom_margin.x)
294 vt102_do_pending_wrap (VT102 * v)
296 int couldscroll = in_margins (v, v->pos);
297 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
299 if (!v->pending_wrap)
303 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
304 v->pos.y, autowrap, couldscroll);
307 /*End of line but no autowrap, nothing to do*/
311 /*End of screen and not allowed to scroll, nothing to do*/
312 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
317 v->pos.x = v->top_margin.x;
324 vt102_cursor_advance_line (v);
329 vt102_cursor_retard (VT102 * v)
331 if (v->pos.x != v->top_margin.x)
338 vt102_reset_tabs (VT102 * v)
342 memset (v->tabs, 0, sizeof (v->tabs));
344 for (i = 0; i < VT102_COLS; i += 8)
350 vt102_cursor_advance_tab (VT102 * v)
352 if (v->pos.x == v->bottom_margin.x)
354 while (v->pos.x < v->bottom_margin.x)
357 if (v->tabs[v->pos.x])
363 vt102_cursor_home (VT102 * v)
365 v->pos = v->top_margin;
366 vt102_cursor_normalize (v);
371 vt102_cursor_absolute (VT102 * v, int x, int y)
373 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
375 v->pos.x = x + v->top_margin.x;
376 v->pos.y = y + v->top_margin.x;
383 vt102_cursor_normalize (v);
387 vt102_cursor_relative (VT102 * v, int x, int y)
391 vt102_cursor_normalize (v);
398 vt102_delete_from_line (VT102 * v, CRT_Pos p)
400 int n = v->bottom_margin.x - p.x;
408 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
409 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
412 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
413 /*But not attr due to vt102 bug*/
417 vt102_insert_into_line (VT102 * v, CRT_Pos p)
419 int n = v->bottom_margin.x - p.x;
427 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
428 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
431 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
432 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
438 vt102_change_mode (VT102 * v, int private, char *ns, int set)
454 if (m >= VT102_NMODES)
459 v->private_modes[m] = set;
462 case VT102_PRIVATE_MODE_ORIGIN_MODE:
463 vt102_cursor_home (v);
472 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
477 vt102_parse_mode_string (VT102 * v, char *buf, int len)
480 char last = buf[len - 1];
484 memset (num, 0, sizeof (num));
485 o = sizeof (num) - 1;
503 vt102_change_mode (v, private, &num[o], last == 'h');
504 memset (num, 0, sizeof (num));
505 o = sizeof (num) - 1;
520 vt102_change_mode (v, private, &num[o], last == 'h');
526 vt102_change_attr (VT102 * v, char *na)
543 v->attr = CRT_ATTR_NORMAL;
546 v->attr |= CRT_ATTR_BOLD;
550 v->attr &= ~CRT_ATTR_BOLD;
553 v->attr |= CRT_ATTR_UNDERLINE;
556 v->attr &= ~CRT_ATTR_UNDERLINE;
559 v->attr |= CRT_ATTR_BLINK;
562 v->attr &= ~CRT_ATTR_BLINK;
565 v->attr |= CRT_ATTR_REVERSE;
568 v->attr &= ~CRT_ATTR_REVERSE;
573 fprintf (stderr, "unhandled SGR %d\n", a);
581 vt102_parse_attr_string (VT102 * v, char *buf, int len)
584 char last = buf[len - 1];
588 memset (num, 0, sizeof (num));
589 o = sizeof (num) - 1;
600 vt102_change_attr (v, &num[o]);
601 memset (num, 0, sizeof (num));
602 o = sizeof (num) - 1;
616 vt102_change_attr (v, &num[o]);
619 void vt102_save_state(VT102 *v)
622 v->saved.attr=v->attr;
623 v->saved.origin_mode=v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
626 void vt102_restore_state(VT102 *v)
629 v->attr=v->saved.attr;
630 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE]=v->saved.origin_mode;
631 vt102_cursor_normalize(v);
636 vt102_parse_esc (VT102 * v, int c)
641 if(v->pos.y==v->bottom_margin.y) {
642 vt102_log_line (v, v->pos.y);
643 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
645 vt102_cursor_relative(v,0,1);
652 if(v->pos.y==v->top_margin.y) {
653 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
655 vt102_cursor_relative(v,0,-1);
662 vt102_restore_state(v);
668 char s[] = { 033, c };
669 /*Horrid hack for the moment in testing change the key mode of the upstream te*/
676 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
683 vt102_parse_csi (VT102 * v, char *buf, int len)
712 vt102_cursor_relative (v, 0, -narg);
715 vt102_cursor_relative (v, 0, narg);
718 vt102_cursor_relative (v, narg, 0);
721 vt102_cursor_relative (v, -narg, 0);
724 vt102_cursor_relative (v, 0, narg);
725 vt102_cursor_carriage_return (v);
728 vt102_cursor_relative (v, 0, -narg);
729 vt102_cursor_carriage_return (v);
732 vt102_cursor_absolute (v, narg-1, v->pos.y);
741 ptr = index (arg, ';');
743 x = atoi (ptr + 1) - 1;
747 vt102_cursor_absolute (v, x, y);
754 crt_erase (&v->crt, v->pos, v->screen_end, 1);
757 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
763 CRT_Pos ls = { 0, v->pos.y };
764 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
766 narg = 0; /*Different default */
771 crt_erase (&v->crt, v->pos, le, 1);
774 crt_erase (&v->crt, ls, v->pos, 1);
777 crt_erase (&v->crt, ls, le, 1);
785 vt102_delete_from_line (v, v->pos);
788 if ((v->pos.y >= v->top_margin.y)
789 && (v->pos.y <= v->bottom_margin.y))
792 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
797 if ((v->pos.y >= v->top_margin.y)
798 && (v->pos.y <= v->bottom_margin.y))
801 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
807 narg = 0; /*Different default */
812 v->tabs[v->pos.x] = 0;
815 memset (v->tabs, 0, sizeof (v->tabs));
822 vt102_parse_mode_string (v, &buf[1], len - 1);
826 vt102_parse_attr_string (v, &buf[1], len - 1);
829 v->top_margin = v->screen_start;
830 v->bottom_margin = v->screen_end;
832 if ((len > 2) && (ptr = index (arg, ';')))
835 v->top_margin.y = narg - 1;
836 v->bottom_margin.y = atoi (ptr) - 1;
839 if (v->top_margin.y < v->screen_start.y)
840 v->top_margin.y = v->screen_start.y;
841 if (v->top_margin.y > v->screen_end.y)
842 v->top_margin.y = v->screen_end.y;
843 if (v->bottom_margin.y < v->screen_start.y)
844 v->bottom_margin.y = v->screen_start.y;
845 if (v->bottom_margin.y > v->screen_end.y)
846 v->bottom_margin.y = v->screen_end.y;
848 vt102_cursor_home (v);
855 vt102_cursor_normalize(v);
861 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
868 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
878 vt102_status_line (VT102 * v, char *str)
881 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
885 ca->attr = CRT_ATTR_REVERSE;
896 vt102_parse_char (VT102 * v, int c)
898 VT102_parser *p = &v->parser;
901 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
902 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
906 p->csi_buf[p->csi_ptr++] = c;
907 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
909 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
913 else if (p->in_escape)
918 p->csi_buf[p->csi_ptr++] = c;
925 vt102_parse_esc (v, c);
940 /*FIXME: Should send ack*/
946 vt102_cursor_retard (v);
949 vt102_cursor_advance_tab (v);
954 vt102_cursor_advance_line (v);
955 if (!v->modes[VT102_MODE_NEWLINE_MODE])
958 vt102_cursor_carriage_return (v);
983 /*regular character */ default:
984 vt102_do_pending_wrap (v);
986 if (v->modes[VT102_MODE_INSERT])
987 vt102_insert_into_line (v, v->pos);
989 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
990 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
991 vt102_cursor_advance (v);
996 v->crt.hide_cursor=v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0:1;
998 if (v->current_line.y != v->pos.y)
1000 vt102_log_line (v, v->current_line.y);
1001 v->current_line = v->pos;
1004 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1008 vt102_parser_reset (VT102_parser * p)
1016 vt102_reset (VT102 * v)
1018 VT102_parser *p = &v->parser;
1020 vt102_parser_reset (p);
1025 v->current_line = v->pos;
1026 v->pending_wrap = 0;
1028 v->screen_start.x = 0;
1029 v->screen_start.y = 0;
1030 v->screen_end.x = VT102_COLS - 1;
1031 v->screen_end.y = VT102_ROWS - 1;
1033 v->top_margin = v->screen_start;
1034 v->bottom_margin = v->screen_end;
1036 memset (v->modes, 0, VT102_NMODES);
1037 memset (v->private_modes, 0, VT102_NMODES);
1039 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1040 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1041 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1043 vt102_cursor_home (v);
1044 vt102_reset_tabs (v);
1045 v->current_line = v->pos;
1047 vt102_save_state(v);
1053 for (i=0;i<200;++i) {
1054 sprintf(buf,"%3d",i);
1055 vt102_parse_char (v,buf[0]);
1056 vt102_parse_char (v,buf[1]);
1057 vt102_parse_char (v,buf[2]);
1058 vt102_parse_char (v,13);
1059 vt102_parse_char (v,27);
1060 vt102_parse_char (v,'M');
1062 vt102_parse_char(v,'f');
1063 vt102_parse_char (v,13);
1064 for (i=0;i<23;++i) {
1065 vt102_parse_char (v,27);
1066 vt102_parse_char (v,'M');
1068 vt102_parse_char(v,'b');
1069 vt102_parse_char (v,13);
1074 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");