4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.16 2008/02/07 01:57:46 james
14 * *** empty log message ***
16 * Revision 1.15 2008/02/07 00:43:27 james
17 * *** empty log message ***
19 * Revision 1.14 2008/02/07 00:40:23 james
20 * *** empty log message ***
22 * Revision 1.13 2008/02/07 00:39:59 james
23 * *** empty log message ***
25 * Revision 1.12 2008/02/07 00:39:13 james
26 * *** empty log message ***
28 * Revision 1.11 2008/02/06 20:26:58 james
29 * *** empty log message ***
31 * Revision 1.10 2008/02/06 17:53:28 james
32 * *** empty log message ***
34 * Revision 1.9 2008/02/06 15:53:22 james
35 * *** empty log message ***
37 * Revision 1.8 2008/02/06 11:49:47 james
38 * *** empty log message ***
40 * Revision 1.7 2008/02/06 11:30:37 james
41 * *** empty log message ***
43 * Revision 1.6 2008/02/05 01:11:46 james
44 * *** empty log message ***
46 * Revision 1.5 2008/02/04 20:23:55 james
47 * *** empty log message ***
49 * Revision 1.4 2008/02/04 05:45:55 james
52 * Revision 1.3 2008/02/04 02:05:06 james
53 * *** empty log message ***
55 * Revision 1.2 2008/02/04 01:32:39 james
56 * *** empty log message ***
58 * Revision 1.1 2008/02/03 23:36:41 james
59 * *** empty log message ***
68 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
70 vt100|vt100-am|dec vt100 (w/advanced video):\
72 :co#80:it#8:li#24:vt#3:\
73 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
75 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
76 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
77 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
78 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
79 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
80 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
81 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
82 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
83 :us=\E[4m:tc=vt100+fnkeys:
85 vt100+fnkeys|dec vt100 numeric keypad:\
86 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
89 vt100+pfkeys|dec vt100 numeric keypad:\
90 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
92 vt100+keypad|dec vt100 numeric keypad no fkeys:\
93 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
98 so the parser needs to be able to at least do
166 if ((c >= 'a') && (c <= 'z'))
168 if ((c >= 'A') && (c <= 'Z'))
185 in_margins (VT102 * v, CRT_Pos p)
187 if (v->pos.x < v->top_margin.x)
189 if (v->pos.y < v->top_margin.y)
192 if (v->pos.x > v->bottom_margin.x)
194 if (v->pos.y > v->bottom_margin.y)
201 vt102_log_line (VT102 * v, int line)
204 CRT_Pos e = { VT102_COLS - 1, line };
205 CRT_Pos p = { 0, line };
208 log = fopen ("log", "a+");
214 for (; e.x > 0; --e.x)
216 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
220 for (; p.x <= e.x; ++p.x)
222 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
233 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
248 vt102_cursor_normalize (VT102 * v)
250 CRT_Pos *top, *bottom;
252 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
254 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
258 vt102_clip_cursor (v, v->screen_start, v->screen_end);
264 vt102_cursor_carriage_return (VT102 * v)
266 /*FISH*/ v->pos.x = v->top_margin.x;
271 vt102_cursor_advance_line (VT102 * v)
273 int couldscroll = in_margins (v, v->pos);
275 /*have wraped off end of last line in scrolling region */
276 /* (|| not necessary, but shuts compiler up */
277 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
280 vt102_log_line (v, v->pos.y);
281 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
291 vt102_cursor_advance (VT102 * v)
294 if (v->pos.x < v->bottom_margin.x)
306 vt102_do_pending_wrap (VT102 * v)
308 int couldscroll = in_margins (v, v->pos);
309 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
311 if (!v->pending_wrap)
315 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
316 v->pos.y, autowrap, couldscroll);
319 /*End of line but no autowrap, nothing to do*/
323 /*End of screen and not allowed to scroll, nothing to do*/
324 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
329 v->pos.x = v->top_margin.x;
336 vt102_cursor_advance_line (v);
341 vt102_cursor_retard (VT102 * v)
343 if (v->pos.x != v->top_margin.x)
350 vt102_reset_tabs (VT102 * v)
354 memset (v->tabs, 0, sizeof (v->tabs));
356 for (i = 0; i < VT102_COLS; i += 8)
362 vt102_cursor_advance_tab (VT102 * v)
364 if (v->pos.x == v->bottom_margin.x)
366 while (v->pos.x < v->bottom_margin.x)
369 if (v->tabs[v->pos.x])
375 vt102_cursor_home (VT102 * v)
377 v->pos = v->top_margin;
378 vt102_cursor_normalize (v);
383 vt102_cursor_absolute (VT102 * v, int x, int y)
385 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
387 v->pos.x = x + v->top_margin.x;
388 v->pos.y = y + v->top_margin.x;
395 vt102_cursor_normalize (v);
399 vt102_cursor_relative (VT102 * v, int x, int y)
403 vt102_cursor_normalize (v);
410 vt102_delete_from_line (VT102 * v, CRT_Pos p)
412 int n = v->bottom_margin.x - p.x;
420 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
421 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
424 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
425 /*But not attr due to vt102 bug*/
429 vt102_insert_into_line (VT102 * v, CRT_Pos p)
431 int n = v->bottom_margin.x - p.x;
439 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
440 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
443 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
444 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
450 vt102_change_mode (VT102 * v, int private, char *ns, int set)
466 if (m >= VT102_NMODES)
471 v->private_modes[m] = set;
474 case VT102_PRIVATE_MODE_CURSOR_MODE:
475 if (v->application_keypad_mode)
476 v->private_modes[m] = 0;
478 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
479 v->private_modes[m],set);
482 case VT102_PRIVATE_MODE_ORIGIN_MODE:
483 vt102_cursor_home (v);
492 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
497 vt102_parse_mode_string (VT102 * v, char *buf, int len)
500 char last = buf[len - 1];
504 memset (num, 0, sizeof (num));
505 o = sizeof (num) - 1;
523 vt102_change_mode (v, private, &num[o], last == 'h');
524 memset (num, 0, sizeof (num));
525 o = sizeof (num) - 1;
540 vt102_change_mode (v, private, &num[o], last == 'h');
546 vt102_change_attr (VT102 * v, char *na)
563 v->attr = CRT_ATTR_NORMAL;
566 v->attr |= CRT_ATTR_BOLD;
570 v->attr &= ~CRT_ATTR_BOLD;
573 v->attr |= CRT_ATTR_UNDERLINE;
576 v->attr &= ~CRT_ATTR_UNDERLINE;
579 v->attr |= CRT_ATTR_BLINK;
582 v->attr &= ~CRT_ATTR_BLINK;
585 v->attr |= CRT_ATTR_REVERSE;
588 v->attr &= ~CRT_ATTR_REVERSE;
593 fprintf (stderr, "unhandled SGR %d\n", a);
601 vt102_parse_attr_string (VT102 * v, char *buf, int len)
604 char last = buf[len - 1];
608 memset (num, 0, sizeof (num));
609 o = sizeof (num) - 1;
620 vt102_change_attr (v, &num[o]);
621 memset (num, 0, sizeof (num));
622 o = sizeof (num) - 1;
636 vt102_change_attr (v, &num[o]);
640 vt102_save_state (VT102 * v)
642 v->saved.pos = v->pos;
643 v->saved.attr = v->attr;
644 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
648 vt102_restore_state (VT102 * v)
650 v->pos = v->saved.pos;
651 v->attr = v->saved.attr;
652 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
653 vt102_cursor_normalize (v);
657 void vt102_scs(VT102 *c,int g,int s)
659 /*Ignoring charsets*/
663 vt102_parse_esc (VT102 * v, int c)
668 if (v->pos.y == v->bottom_margin.y)
670 vt102_log_line (v, v->pos.y);
671 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
675 vt102_cursor_relative (v, 0, 1);
682 if (v->pos.y == v->top_margin.y)
684 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
688 vt102_cursor_relative (v, 0, -1);
692 vt102_save_state (v);
695 vt102_restore_state (v);
698 v->application_keypad_mode = 1;
701 v->application_keypad_mode = 0;
705 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
712 vt102_parse_csi (VT102 * v, char *buf, int len)
741 vt102_cursor_relative (v, 0, -narg);
744 vt102_cursor_relative (v, 0, narg);
747 vt102_cursor_relative (v, narg, 0);
750 vt102_cursor_relative (v, -narg, 0);
753 vt102_cursor_relative (v, 0, narg);
754 vt102_cursor_carriage_return (v);
757 vt102_cursor_relative (v, 0, -narg);
758 vt102_cursor_carriage_return (v);
761 vt102_cursor_absolute (v, narg - 1, v->pos.y);
770 ptr = index (arg, ';');
772 x = atoi (ptr + 1) - 1;
776 vt102_cursor_absolute (v, x, y);
783 crt_erase (&v->crt, v->pos, v->screen_end, 1);
786 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
792 CRT_Pos ls = { 0, v->pos.y };
793 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
795 narg = 0; /*Different default */
800 crt_erase (&v->crt, v->pos, le, 1);
803 crt_erase (&v->crt, ls, v->pos, 1);
806 crt_erase (&v->crt, ls, le, 1);
814 vt102_delete_from_line (v, v->pos);
817 if ((v->pos.y >= v->top_margin.y)
818 && (v->pos.y <= v->bottom_margin.y))
821 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
826 if ((v->pos.y >= v->top_margin.y)
827 && (v->pos.y <= v->bottom_margin.y))
830 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
836 narg = 0; /*Different default */
841 v->tabs[v->pos.x] = 0;
844 memset (v->tabs, 0, sizeof (v->tabs));
851 vt102_parse_mode_string (v, &buf[1], len - 1);
855 vt102_parse_attr_string (v, &buf[1], len - 1);
858 v->top_margin = v->screen_start;
859 v->bottom_margin = v->screen_end;
861 if ((len > 2) && (ptr = index (arg, ';')))
864 v->top_margin.y = narg - 1;
865 v->bottom_margin.y = atoi (ptr) - 1;
868 if (v->top_margin.y < v->screen_start.y)
869 v->top_margin.y = v->screen_start.y;
870 if (v->top_margin.y > v->screen_end.y)
871 v->top_margin.y = v->screen_end.y;
872 if (v->bottom_margin.y < v->screen_start.y)
873 v->bottom_margin.y = v->screen_start.y;
874 if (v->bottom_margin.y > v->screen_end.y)
875 v->bottom_margin.y = v->screen_end.y;
877 vt102_cursor_home (v);
880 v->saved.pos = v->pos;
883 v->pos = v->saved.pos;
884 vt102_cursor_normalize (v);
890 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
897 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
907 vt102_status_line (VT102 * v, char *str)
910 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
914 ca->attr = CRT_ATTR_REVERSE;
925 vt102_parse_char (VT102 * v, int c)
927 VT102_parser *p = &v->parser;
931 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
932 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
936 p->csi_buf[p->csi_ptr++] = c;
937 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
939 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
943 else if (p->in_escape)
948 p->csi_buf[p->csi_ptr++] = c;
952 else if (scs_starter(c)) {
959 vt102_parse_esc (v, c);
962 else if (p->in_scs) {
963 vt102_scs(v,p->in_scs,c);
978 tty_write (v->tty, "vt102", 5);
984 vt102_cursor_retard (v);
987 vt102_cursor_advance_tab (v);
992 vt102_cursor_advance_line (v);
993 if (!v->modes[VT102_MODE_NEWLINE_MODE])
996 vt102_cursor_carriage_return (v);
1000 /*Ignoring charsets*/
1004 /*Ignoring charsets*/
1027 /*regular character */ default:
1028 vt102_do_pending_wrap (v);
1030 if (v->modes[VT102_MODE_INSERT])
1031 vt102_insert_into_line (v, v->pos);
1033 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1034 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1035 vt102_cursor_advance (v);
1039 v->crt.pos = v->pos;
1040 v->crt.hide_cursor =
1041 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1043 if (v->current_line.y != v->pos.y)
1045 vt102_log_line (v, v->current_line.y);
1046 v->current_line = v->pos;
1049 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1052 vt102_parse (VT102 * v, char *buf, int len)
1055 vt102_parse_char (v, *(buf++));
1060 vt102_parser_reset (VT102_parser * p)
1070 vt102_send (VT102 * v, uint8_t key)
1074 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1076 if ((key > 31) && (key < 127))
1078 tty_write (v->tty, &key, 1);
1097 tty_write (v->tty, &key, 1);
1100 tty_write (v->tty, &key, 1);
1101 if (v->modes[VT102_MODE_NEWLINE_MODE])
1104 tty_write (v->tty, &c, 1);
1120 tty_write (v->tty, &key, 1);
1128 tty_write (v->tty, &key, 1);
1139 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1141 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1142 tty_write (v->tty, &buf, sizeof (buf));
1146 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1147 tty_write (v->tty, &buf, sizeof (buf));
1165 if (v->application_keypad_mode)
1167 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1168 tty_write (v->tty, &buf, sizeof (buf));
1172 static char kpoff[KEY_NUM] = {
1191 tty_write (v->tty, &kpoff[key], 1);
1195 if (v->application_keypad_mode)
1197 uint8_t buf[] = { 033, 'O', 'M' };
1198 tty_write (v->tty, &buf, sizeof (buf));
1203 tty_write (v->tty, &c, 1);
1204 if (v->modes[VT102_MODE_NEWLINE_MODE])
1207 tty_write (v->tty, &c, 1);
1216 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1217 tty_write (v->tty, &buf, sizeof (buf));
1225 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1226 tty_write (v->tty, &buf, sizeof (buf));
1234 vt102_reset (VT102 * v)
1236 VT102_parser *p = &v->parser;
1238 vt102_parser_reset (p);
1242 v->application_keypad_mode = 0;
1244 v->current_line = v->pos;
1245 v->pending_wrap = 0;
1247 v->screen_start.x = 0;
1248 v->screen_start.y = 0;
1249 v->screen_end.x = VT102_COLS - 1;
1250 v->screen_end.y = VT102_ROWS - 1;
1252 v->top_margin = v->screen_start;
1253 v->bottom_margin = v->screen_end;
1255 memset (v->modes, 0, VT102_NMODES);
1256 memset (v->private_modes, 0, VT102_NMODES);
1258 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1259 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1260 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1261 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1263 vt102_cursor_home (v);
1264 vt102_reset_tabs (v);
1265 v->current_line = v->pos;
1267 vt102_save_state (v);
1269 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1274 vt102_dispatch (VT102 * v)
1279 red = tty_read (v->tty, buf, sizeof (buf));
1287 vt102_parse (v, buf, red);
1297 v = (VT102 *) malloc (sizeof (VT102));
1307 vt102_free (VT102 * v)