4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.17 2008/02/07 01:58:28 james
14 * *** empty log message ***
16 * Revision 1.16 2008/02/07 01:57:46 james
17 * *** empty log message ***
19 * Revision 1.15 2008/02/07 00:43:27 james
20 * *** empty log message ***
22 * Revision 1.14 2008/02/07 00:40:23 james
23 * *** empty log message ***
25 * Revision 1.13 2008/02/07 00:39:59 james
26 * *** empty log message ***
28 * Revision 1.12 2008/02/07 00:39:13 james
29 * *** empty log message ***
31 * Revision 1.11 2008/02/06 20:26:58 james
32 * *** empty log message ***
34 * Revision 1.10 2008/02/06 17:53:28 james
35 * *** empty log message ***
37 * Revision 1.9 2008/02/06 15:53:22 james
38 * *** empty log message ***
40 * Revision 1.8 2008/02/06 11:49:47 james
41 * *** empty log message ***
43 * Revision 1.7 2008/02/06 11:30:37 james
44 * *** empty log message ***
46 * Revision 1.6 2008/02/05 01:11:46 james
47 * *** empty log message ***
49 * Revision 1.5 2008/02/04 20:23:55 james
50 * *** empty log message ***
52 * Revision 1.4 2008/02/04 05:45:55 james
55 * Revision 1.3 2008/02/04 02:05:06 james
56 * *** empty log message ***
58 * Revision 1.2 2008/02/04 01:32:39 james
59 * *** empty log message ***
61 * Revision 1.1 2008/02/03 23:36:41 james
62 * *** empty log message ***
71 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
73 vt100|vt100-am|dec vt100 (w/advanced video):\
75 :co#80:it#8:li#24:vt#3:\
76 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
78 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
79 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
80 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
81 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
82 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
83 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
84 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
85 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
86 :us=\E[4m:tc=vt100+fnkeys:
88 vt100+fnkeys|dec vt100 numeric keypad:\
89 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
92 vt100+pfkeys|dec vt100 numeric keypad:\
93 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
95 vt100+keypad|dec vt100 numeric keypad no fkeys:\
96 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
101 so the parser needs to be able to at least do
169 if ((c >= 'a') && (c <= 'z'))
171 if ((c >= 'A') && (c <= 'Z'))
200 in_margins (VT102 * v, CRT_Pos p)
202 if (v->pos.x < v->top_margin.x)
204 if (v->pos.y < v->top_margin.y)
207 if (v->pos.x > v->bottom_margin.x)
209 if (v->pos.y > v->bottom_margin.y)
216 vt102_log_line (VT102 * v, int line)
219 CRT_Pos e = { VT102_COLS - 1, line };
220 CRT_Pos p = { 0, line };
223 log = fopen ("log", "a+");
229 for (; e.x > 0; --e.x)
231 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
235 for (; p.x <= e.x; ++p.x)
237 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
248 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
263 vt102_cursor_normalize (VT102 * v)
265 CRT_Pos *top, *bottom;
267 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
269 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
273 vt102_clip_cursor (v, v->screen_start, v->screen_end);
279 vt102_cursor_carriage_return (VT102 * v)
281 /*FISH*/ v->pos.x = v->top_margin.x;
286 vt102_cursor_advance_line (VT102 * v)
288 int couldscroll = in_margins (v, v->pos);
290 /*have wraped off end of last line in scrolling region */
291 /* (|| not necessary, but shuts compiler up */
292 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
295 vt102_log_line (v, v->pos.y);
296 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
306 vt102_cursor_advance (VT102 * v)
309 if (v->pos.x < v->bottom_margin.x)
321 vt102_do_pending_wrap (VT102 * v)
323 int couldscroll = in_margins (v, v->pos);
324 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
326 if (!v->pending_wrap)
330 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
331 v->pos.y, autowrap, couldscroll);
334 /*End of line but no autowrap, nothing to do*/
338 /*End of screen and not allowed to scroll, nothing to do*/
339 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
344 v->pos.x = v->top_margin.x;
351 vt102_cursor_advance_line (v);
356 vt102_cursor_retard (VT102 * v)
358 if (v->pos.x != v->top_margin.x)
365 vt102_reset_tabs (VT102 * v)
369 memset (v->tabs, 0, sizeof (v->tabs));
371 for (i = 0; i < VT102_COLS; i += 8)
377 vt102_cursor_advance_tab (VT102 * v)
379 if (v->pos.x == v->bottom_margin.x)
381 while (v->pos.x < v->bottom_margin.x)
384 if (v->tabs[v->pos.x])
390 vt102_cursor_home (VT102 * v)
392 v->pos = v->top_margin;
393 vt102_cursor_normalize (v);
398 vt102_cursor_absolute (VT102 * v, int x, int y)
400 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
402 v->pos.x = x + v->top_margin.x;
403 v->pos.y = y + v->top_margin.x;
410 vt102_cursor_normalize (v);
414 vt102_cursor_relative (VT102 * v, int x, int y)
418 vt102_cursor_normalize (v);
425 vt102_delete_from_line (VT102 * v, CRT_Pos p)
427 int n = v->bottom_margin.x - p.x;
435 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
436 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
439 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
440 /*But not attr due to vt102 bug*/
444 vt102_insert_into_line (VT102 * v, CRT_Pos p)
446 int n = v->bottom_margin.x - p.x;
454 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
455 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
458 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
459 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
465 vt102_change_mode (VT102 * v, int private, char *ns, int set)
481 if (m >= VT102_NMODES)
486 v->private_modes[m] = set;
489 case VT102_PRIVATE_MODE_CURSOR_MODE:
490 if (v->application_keypad_mode)
491 v->private_modes[m] = 0;
493 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
494 v->private_modes[m],set);
497 case VT102_PRIVATE_MODE_ORIGIN_MODE:
498 vt102_cursor_home (v);
507 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
512 vt102_parse_mode_string (VT102 * v, char *buf, int len)
515 char last = buf[len - 1];
519 memset (num, 0, sizeof (num));
520 o = sizeof (num) - 1;
538 vt102_change_mode (v, private, &num[o], last == 'h');
539 memset (num, 0, sizeof (num));
540 o = sizeof (num) - 1;
555 vt102_change_mode (v, private, &num[o], last == 'h');
561 vt102_change_attr (VT102 * v, char *na)
578 v->attr = CRT_ATTR_NORMAL;
581 v->attr |= CRT_ATTR_BOLD;
585 v->attr &= ~CRT_ATTR_BOLD;
588 v->attr |= CRT_ATTR_UNDERLINE;
591 v->attr &= ~CRT_ATTR_UNDERLINE;
594 v->attr |= CRT_ATTR_BLINK;
597 v->attr &= ~CRT_ATTR_BLINK;
600 v->attr |= CRT_ATTR_REVERSE;
603 v->attr &= ~CRT_ATTR_REVERSE;
608 fprintf (stderr, "unhandled SGR %d\n", a);
616 vt102_parse_attr_string (VT102 * v, char *buf, int len)
619 char last = buf[len - 1];
623 memset (num, 0, sizeof (num));
624 o = sizeof (num) - 1;
635 vt102_change_attr (v, &num[o]);
636 memset (num, 0, sizeof (num));
637 o = sizeof (num) - 1;
651 vt102_change_attr (v, &num[o]);
655 vt102_save_state (VT102 * v)
657 v->saved.pos = v->pos;
658 v->saved.attr = v->attr;
659 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
663 vt102_restore_state (VT102 * v)
665 v->pos = v->saved.pos;
666 v->attr = v->saved.attr;
667 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
668 vt102_cursor_normalize (v);
672 void vt102_scs(VT102 *c,int g,int s)
674 /*Ignoring charsets*/
678 vt102_parse_esc (VT102 * v, int c)
683 if (v->pos.y == v->bottom_margin.y)
685 vt102_log_line (v, v->pos.y);
686 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
690 vt102_cursor_relative (v, 0, 1);
697 if (v->pos.y == v->top_margin.y)
699 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
703 vt102_cursor_relative (v, 0, -1);
707 vt102_save_state (v);
710 vt102_restore_state (v);
713 v->application_keypad_mode = 1;
716 v->application_keypad_mode = 0;
720 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
727 vt102_parse_csi (VT102 * v, char *buf, int len)
756 vt102_cursor_relative (v, 0, -narg);
759 vt102_cursor_relative (v, 0, narg);
762 vt102_cursor_relative (v, narg, 0);
765 vt102_cursor_relative (v, -narg, 0);
768 vt102_cursor_relative (v, 0, narg);
769 vt102_cursor_carriage_return (v);
772 vt102_cursor_relative (v, 0, -narg);
773 vt102_cursor_carriage_return (v);
776 vt102_cursor_absolute (v, narg - 1, v->pos.y);
785 ptr = index (arg, ';');
787 x = atoi (ptr + 1) - 1;
791 vt102_cursor_absolute (v, x, y);
798 crt_erase (&v->crt, v->pos, v->screen_end, 1);
801 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
807 CRT_Pos ls = { 0, v->pos.y };
808 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
810 narg = 0; /*Different default */
815 crt_erase (&v->crt, v->pos, le, 1);
818 crt_erase (&v->crt, ls, v->pos, 1);
821 crt_erase (&v->crt, ls, le, 1);
829 vt102_delete_from_line (v, v->pos);
832 if ((v->pos.y >= v->top_margin.y)
833 && (v->pos.y <= v->bottom_margin.y))
836 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
841 if ((v->pos.y >= v->top_margin.y)
842 && (v->pos.y <= v->bottom_margin.y))
845 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
851 narg = 0; /*Different default */
856 v->tabs[v->pos.x] = 0;
859 memset (v->tabs, 0, sizeof (v->tabs));
866 vt102_parse_mode_string (v, &buf[1], len - 1);
870 vt102_parse_attr_string (v, &buf[1], len - 1);
873 v->top_margin = v->screen_start;
874 v->bottom_margin = v->screen_end;
876 if ((len > 2) && (ptr = index (arg, ';')))
879 v->top_margin.y = narg - 1;
880 v->bottom_margin.y = atoi (ptr) - 1;
883 if (v->top_margin.y < v->screen_start.y)
884 v->top_margin.y = v->screen_start.y;
885 if (v->top_margin.y > v->screen_end.y)
886 v->top_margin.y = v->screen_end.y;
887 if (v->bottom_margin.y < v->screen_start.y)
888 v->bottom_margin.y = v->screen_start.y;
889 if (v->bottom_margin.y > v->screen_end.y)
890 v->bottom_margin.y = v->screen_end.y;
892 vt102_cursor_home (v);
895 v->saved.pos = v->pos;
898 v->pos = v->saved.pos;
899 vt102_cursor_normalize (v);
905 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
912 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
922 vt102_status_line (VT102 * v, char *str)
925 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
929 ca->attr = CRT_ATTR_REVERSE;
940 vt102_parse_char (VT102 * v, int c)
942 VT102_parser *p = &v->parser;
946 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
947 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
951 p->csi_buf[p->csi_ptr++] = c;
952 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
954 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
958 else if (p->in_escape)
963 p->csi_buf[p->csi_ptr++] = c;
967 else if (scs_starter(c)) {
974 vt102_parse_esc (v, c);
977 else if (p->in_scs) {
978 vt102_scs(v,p->in_scs,c);
993 tty_write (v->tty, "vt102", 5);
999 vt102_cursor_retard (v);
1002 vt102_cursor_advance_tab (v);
1007 vt102_cursor_advance_line (v);
1008 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1011 vt102_cursor_carriage_return (v);
1015 /*Ignoring charsets*/
1019 /*Ignoring charsets*/
1042 /*regular character */ default:
1043 vt102_do_pending_wrap (v);
1045 if (v->modes[VT102_MODE_INSERT])
1046 vt102_insert_into_line (v, v->pos);
1048 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1049 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1050 vt102_cursor_advance (v);
1054 v->crt.pos = v->pos;
1055 v->crt.hide_cursor =
1056 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1058 if (v->current_line.y != v->pos.y)
1060 vt102_log_line (v, v->current_line.y);
1061 v->current_line = v->pos;
1064 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1067 vt102_parse (VT102 * v, char *buf, int len)
1070 vt102_parse_char (v, *(buf++));
1075 vt102_parser_reset (VT102_parser * p)
1085 vt102_send (VT102 * v, uint8_t key)
1089 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1091 if ((key > 31) && (key < 127))
1093 tty_write (v->tty, &key, 1);
1112 tty_write (v->tty, &key, 1);
1115 tty_write (v->tty, &key, 1);
1116 if (v->modes[VT102_MODE_NEWLINE_MODE])
1119 tty_write (v->tty, &c, 1);
1135 tty_write (v->tty, &key, 1);
1143 tty_write (v->tty, &key, 1);
1154 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1156 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1157 tty_write (v->tty, &buf, sizeof (buf));
1161 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1162 tty_write (v->tty, &buf, sizeof (buf));
1180 if (v->application_keypad_mode)
1182 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1183 tty_write (v->tty, &buf, sizeof (buf));
1187 static char kpoff[KEY_NUM] = {
1206 tty_write (v->tty, &kpoff[key], 1);
1210 if (v->application_keypad_mode)
1212 uint8_t buf[] = { 033, 'O', 'M' };
1213 tty_write (v->tty, &buf, sizeof (buf));
1218 tty_write (v->tty, &c, 1);
1219 if (v->modes[VT102_MODE_NEWLINE_MODE])
1222 tty_write (v->tty, &c, 1);
1231 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1232 tty_write (v->tty, &buf, sizeof (buf));
1240 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1241 tty_write (v->tty, &buf, sizeof (buf));
1249 vt102_reset (VT102 * v)
1251 VT102_parser *p = &v->parser;
1253 vt102_parser_reset (p);
1257 v->application_keypad_mode = 0;
1259 v->current_line = v->pos;
1260 v->pending_wrap = 0;
1262 v->screen_start.x = 0;
1263 v->screen_start.y = 0;
1264 v->screen_end.x = VT102_COLS - 1;
1265 v->screen_end.y = VT102_ROWS - 1;
1267 v->top_margin = v->screen_start;
1268 v->bottom_margin = v->screen_end;
1270 memset (v->modes, 0, VT102_NMODES);
1271 memset (v->private_modes, 0, VT102_NMODES);
1273 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1274 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1275 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1276 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1278 vt102_cursor_home (v);
1279 vt102_reset_tabs (v);
1280 v->current_line = v->pos;
1282 vt102_save_state (v);
1284 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1289 vt102_dispatch (VT102 * v)
1294 red = tty_read (v->tty, buf, sizeof (buf));
1302 vt102_parse (v, buf, red);
1312 v = (VT102 *) malloc (sizeof (VT102));
1322 vt102_free (VT102 * v)