4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.18 2008/02/07 01:59:25 james
14 * *** empty log message ***
16 * Revision 1.17 2008/02/07 01:58:28 james
17 * *** empty log message ***
19 * Revision 1.16 2008/02/07 01:57:46 james
20 * *** empty log message ***
22 * Revision 1.15 2008/02/07 00:43:27 james
23 * *** empty log message ***
25 * Revision 1.14 2008/02/07 00:40:23 james
26 * *** empty log message ***
28 * Revision 1.13 2008/02/07 00:39:59 james
29 * *** empty log message ***
31 * Revision 1.12 2008/02/07 00:39:13 james
32 * *** empty log message ***
34 * Revision 1.11 2008/02/06 20:26:58 james
35 * *** empty log message ***
37 * Revision 1.10 2008/02/06 17:53:28 james
38 * *** empty log message ***
40 * Revision 1.9 2008/02/06 15:53:22 james
41 * *** empty log message ***
43 * Revision 1.8 2008/02/06 11:49:47 james
44 * *** empty log message ***
46 * Revision 1.7 2008/02/06 11:30:37 james
47 * *** empty log message ***
49 * Revision 1.6 2008/02/05 01:11:46 james
50 * *** empty log message ***
52 * Revision 1.5 2008/02/04 20:23:55 james
53 * *** empty log message ***
55 * Revision 1.4 2008/02/04 05:45:55 james
58 * Revision 1.3 2008/02/04 02:05:06 james
59 * *** empty log message ***
61 * Revision 1.2 2008/02/04 01:32:39 james
62 * *** empty log message ***
64 * Revision 1.1 2008/02/03 23:36:41 james
65 * *** empty log message ***
74 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
76 vt100|vt100-am|dec vt100 (w/advanced video):\
78 :co#80:it#8:li#24:vt#3:\
79 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
81 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
82 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
83 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
84 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
85 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
86 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
87 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
88 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
89 :us=\E[4m:tc=vt100+fnkeys:
91 vt100+fnkeys|dec vt100 numeric keypad:\
92 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
95 vt100+pfkeys|dec vt100 numeric keypad:\
96 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
98 vt100+keypad|dec vt100 numeric keypad no fkeys:\
99 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
104 so the parser needs to be able to at least do
172 if ((c >= 'a') && (c <= 'z'))
174 if ((c >= 'A') && (c <= 'Z'))
203 in_margins (VT102 * v, CRT_Pos p)
205 if (v->pos.x < v->top_margin.x)
207 if (v->pos.y < v->top_margin.y)
210 if (v->pos.x > v->bottom_margin.x)
212 if (v->pos.y > v->bottom_margin.y)
219 vt102_log_line (VT102 * v, int line)
222 CRT_Pos e = { VT102_COLS - 1, line };
223 CRT_Pos p = { 0, line };
226 log = fopen ("log", "a+");
232 for (; e.x > 0; --e.x)
234 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
238 for (; p.x <= e.x; ++p.x)
240 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
251 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
266 vt102_cursor_normalize (VT102 * v)
268 CRT_Pos *top, *bottom;
270 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
272 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
276 vt102_clip_cursor (v, v->screen_start, v->screen_end);
282 vt102_cursor_carriage_return (VT102 * v)
284 /*FISH*/ v->pos.x = v->top_margin.x;
289 vt102_cursor_advance_line (VT102 * v)
291 int couldscroll = in_margins (v, v->pos);
293 /*have wraped off end of last line in scrolling region */
294 /* (|| not necessary, but shuts compiler up */
295 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
298 vt102_log_line (v, v->pos.y);
299 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
309 vt102_cursor_advance (VT102 * v)
312 if (v->pos.x < v->bottom_margin.x)
324 vt102_do_pending_wrap (VT102 * v)
326 int couldscroll = in_margins (v, v->pos);
327 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
329 if (!v->pending_wrap)
333 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
334 v->pos.y, autowrap, couldscroll);
337 /*End of line but no autowrap, nothing to do*/
341 /*End of screen and not allowed to scroll, nothing to do*/
342 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
347 v->pos.x = v->top_margin.x;
354 vt102_cursor_advance_line (v);
359 vt102_cursor_retard (VT102 * v)
361 if (v->pos.x != v->top_margin.x)
368 vt102_reset_tabs (VT102 * v)
372 memset (v->tabs, 0, sizeof (v->tabs));
374 for (i = 0; i < VT102_COLS; i += 8)
380 vt102_cursor_advance_tab (VT102 * v)
382 if (v->pos.x == v->bottom_margin.x)
384 while (v->pos.x < v->bottom_margin.x)
387 if (v->tabs[v->pos.x])
393 vt102_cursor_home (VT102 * v)
395 v->pos = v->top_margin;
396 vt102_cursor_normalize (v);
401 vt102_cursor_absolute (VT102 * v, int x, int y)
403 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
405 v->pos.x = x + v->top_margin.x;
406 v->pos.y = y + v->top_margin.x;
413 vt102_cursor_normalize (v);
417 vt102_cursor_relative (VT102 * v, int x, int y)
421 vt102_cursor_normalize (v);
428 vt102_delete_from_line (VT102 * v, CRT_Pos p)
430 int n = v->bottom_margin.x - p.x;
438 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
439 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
442 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
443 /*But not attr due to vt102 bug*/
447 vt102_insert_into_line (VT102 * v, CRT_Pos p)
449 int n = v->bottom_margin.x - p.x;
457 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
458 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
461 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
462 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
468 vt102_change_mode (VT102 * v, int private, char *ns, int set)
484 if (m >= VT102_NMODES)
489 v->private_modes[m] = set;
492 case VT102_PRIVATE_MODE_CURSOR_MODE:
493 if (v->application_keypad_mode)
494 v->private_modes[m] = 0;
496 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
497 v->private_modes[m],set);
500 case VT102_PRIVATE_MODE_ORIGIN_MODE:
501 vt102_cursor_home (v);
510 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
515 vt102_parse_mode_string (VT102 * v, char *buf, int len)
518 char last = buf[len - 1];
522 memset (num, 0, sizeof (num));
523 o = sizeof (num) - 1;
541 vt102_change_mode (v, private, &num[o], last == 'h');
542 memset (num, 0, sizeof (num));
543 o = sizeof (num) - 1;
558 vt102_change_mode (v, private, &num[o], last == 'h');
564 vt102_change_attr (VT102 * v, char *na)
581 v->attr = CRT_ATTR_NORMAL;
584 v->attr |= CRT_ATTR_BOLD;
588 v->attr &= ~CRT_ATTR_BOLD;
591 v->attr |= CRT_ATTR_UNDERLINE;
594 v->attr &= ~CRT_ATTR_UNDERLINE;
597 v->attr |= CRT_ATTR_BLINK;
600 v->attr &= ~CRT_ATTR_BLINK;
603 v->attr |= CRT_ATTR_REVERSE;
606 v->attr &= ~CRT_ATTR_REVERSE;
611 fprintf (stderr, "unhandled SGR %d\n", a);
619 vt102_parse_attr_string (VT102 * v, char *buf, int len)
622 char last = buf[len - 1];
626 memset (num, 0, sizeof (num));
627 o = sizeof (num) - 1;
638 vt102_change_attr (v, &num[o]);
639 memset (num, 0, sizeof (num));
640 o = sizeof (num) - 1;
654 vt102_change_attr (v, &num[o]);
658 vt102_save_state (VT102 * v)
660 v->saved.pos = v->pos;
661 v->saved.attr = v->attr;
662 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
666 vt102_restore_state (VT102 * v)
668 v->pos = v->saved.pos;
669 v->attr = v->saved.attr;
670 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
671 vt102_cursor_normalize (v);
675 void vt102_scs(VT102 *c,int g,int s)
677 /*Ignoring charsets*/
681 vt102_parse_esc (VT102 * v, int c)
686 if (v->pos.y == v->bottom_margin.y)
688 vt102_log_line (v, v->pos.y);
689 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
693 vt102_cursor_relative (v, 0, 1);
700 if (v->pos.y == v->top_margin.y)
702 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
706 vt102_cursor_relative (v, 0, -1);
710 vt102_save_state (v);
713 vt102_restore_state (v);
716 v->application_keypad_mode = 1;
719 v->application_keypad_mode = 0;
723 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
730 vt102_parse_csi (VT102 * v, char *buf, int len)
759 vt102_cursor_relative (v, 0, -narg);
762 vt102_cursor_relative (v, 0, narg);
765 vt102_cursor_relative (v, narg, 0);
768 vt102_cursor_relative (v, -narg, 0);
771 vt102_cursor_relative (v, 0, narg);
772 vt102_cursor_carriage_return (v);
775 vt102_cursor_relative (v, 0, -narg);
776 vt102_cursor_carriage_return (v);
779 vt102_cursor_absolute (v, narg - 1, v->pos.y);
788 ptr = index (arg, ';');
790 x = atoi (ptr + 1) - 1;
794 vt102_cursor_absolute (v, x, y);
801 crt_erase (&v->crt, v->pos, v->screen_end, 1);
804 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
810 CRT_Pos ls = { 0, v->pos.y };
811 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
813 narg = 0; /*Different default */
818 crt_erase (&v->crt, v->pos, le, 1);
821 crt_erase (&v->crt, ls, v->pos, 1);
824 crt_erase (&v->crt, ls, le, 1);
832 vt102_delete_from_line (v, v->pos);
835 if ((v->pos.y >= v->top_margin.y)
836 && (v->pos.y <= v->bottom_margin.y))
839 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
844 if ((v->pos.y >= v->top_margin.y)
845 && (v->pos.y <= v->bottom_margin.y))
848 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
854 narg = 0; /*Different default */
859 v->tabs[v->pos.x] = 0;
862 memset (v->tabs, 0, sizeof (v->tabs));
869 vt102_parse_mode_string (v, &buf[1], len - 1);
873 vt102_parse_attr_string (v, &buf[1], len - 1);
876 v->top_margin = v->screen_start;
877 v->bottom_margin = v->screen_end;
879 if ((len > 2) && (ptr = index (arg, ';')))
882 v->top_margin.y = narg - 1;
883 v->bottom_margin.y = atoi (ptr) - 1;
886 if (v->top_margin.y < v->screen_start.y)
887 v->top_margin.y = v->screen_start.y;
888 if (v->top_margin.y > v->screen_end.y)
889 v->top_margin.y = v->screen_end.y;
890 if (v->bottom_margin.y < v->screen_start.y)
891 v->bottom_margin.y = v->screen_start.y;
892 if (v->bottom_margin.y > v->screen_end.y)
893 v->bottom_margin.y = v->screen_end.y;
895 vt102_cursor_home (v);
898 v->saved.pos = v->pos;
901 v->pos = v->saved.pos;
902 vt102_cursor_normalize (v);
908 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
915 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
925 vt102_status_line (VT102 * v, char *str)
928 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
932 ca->attr = CRT_ATTR_REVERSE;
943 vt102_parse_char (VT102 * v, int c)
945 VT102_parser *p = &v->parser;
949 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
950 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
954 p->csi_buf[p->csi_ptr++] = c;
955 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
957 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
961 else if (p->in_escape)
966 p->csi_buf[p->csi_ptr++] = c;
970 else if (scs_starter(c)) {
977 vt102_parse_esc (v, c);
980 else if (p->in_scs) {
981 vt102_scs(v,p->in_scs,c);
996 tty_write (v->tty, "vt102", 5);
1002 vt102_cursor_retard (v);
1005 vt102_cursor_advance_tab (v);
1010 vt102_cursor_advance_line (v);
1011 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1014 vt102_cursor_carriage_return (v);
1018 /*Ignoring charsets*/
1022 /*Ignoring charsets*/
1045 /*regular character */ default:
1046 vt102_do_pending_wrap (v);
1048 if (v->modes[VT102_MODE_INSERT])
1049 vt102_insert_into_line (v, v->pos);
1051 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1052 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1053 vt102_cursor_advance (v);
1057 v->crt.pos = v->pos;
1058 v->crt.hide_cursor =
1059 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1061 if (v->current_line.y != v->pos.y)
1063 vt102_log_line (v, v->current_line.y);
1064 v->current_line = v->pos;
1067 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1070 vt102_parse (VT102 * v, char *buf, int len)
1073 vt102_parse_char (v, *(buf++));
1078 vt102_parser_reset (VT102_parser * p)
1088 vt102_send (VT102 * v, uint8_t key)
1092 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1094 if ((key > 31) && (key < 127))
1096 tty_write (v->tty, &key, 1);
1115 tty_write (v->tty, &key, 1);
1118 tty_write (v->tty, &key, 1);
1119 if (v->modes[VT102_MODE_NEWLINE_MODE])
1122 tty_write (v->tty, &c, 1);
1138 tty_write (v->tty, &key, 1);
1146 tty_write (v->tty, &key, 1);
1157 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1159 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1160 tty_write (v->tty, &buf, sizeof (buf));
1164 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1165 tty_write (v->tty, &buf, sizeof (buf));
1183 if (v->application_keypad_mode)
1185 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1186 tty_write (v->tty, &buf, sizeof (buf));
1190 static char kpoff[KEY_NUM] = {
1209 tty_write (v->tty, &kpoff[key], 1);
1213 if (v->application_keypad_mode)
1215 uint8_t buf[] = { 033, 'O', 'M' };
1216 tty_write (v->tty, &buf, sizeof (buf));
1221 tty_write (v->tty, &c, 1);
1222 if (v->modes[VT102_MODE_NEWLINE_MODE])
1225 tty_write (v->tty, &c, 1);
1234 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1235 tty_write (v->tty, &buf, sizeof (buf));
1243 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1244 tty_write (v->tty, &buf, sizeof (buf));
1252 vt102_reset (VT102 * v)
1254 VT102_parser *p = &v->parser;
1256 vt102_parser_reset (p);
1260 v->application_keypad_mode = 0;
1262 v->current_line = v->pos;
1263 v->pending_wrap = 0;
1265 v->screen_start.x = 0;
1266 v->screen_start.y = 0;
1267 v->screen_end.x = VT102_COLS - 1;
1268 v->screen_end.y = VT102_ROWS - 1;
1270 v->top_margin = v->screen_start;
1271 v->bottom_margin = v->screen_end;
1273 memset (v->modes, 0, VT102_NMODES);
1274 memset (v->private_modes, 0, VT102_NMODES);
1276 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1277 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1278 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1279 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1281 vt102_cursor_home (v);
1282 vt102_reset_tabs (v);
1283 v->current_line = v->pos;
1285 vt102_save_state (v);
1287 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1292 vt102_dispatch (VT102 * v)
1297 red = tty_read (v->tty, buf, sizeof (buf));
1305 vt102_parse (v, buf, red);
1315 v = (VT102 *) malloc (sizeof (VT102));
1325 vt102_free (VT102 * v)