4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.19 2008/02/07 11:27:02 james
14 * *** empty log message ***
16 * Revision 1.18 2008/02/07 01:59:25 james
17 * *** empty log message ***
19 * Revision 1.17 2008/02/07 01:58:28 james
20 * *** empty log message ***
22 * Revision 1.16 2008/02/07 01:57:46 james
23 * *** empty log message ***
25 * Revision 1.15 2008/02/07 00:43:27 james
26 * *** empty log message ***
28 * Revision 1.14 2008/02/07 00:40:23 james
29 * *** empty log message ***
31 * Revision 1.13 2008/02/07 00:39:59 james
32 * *** empty log message ***
34 * Revision 1.12 2008/02/07 00:39:13 james
35 * *** empty log message ***
37 * Revision 1.11 2008/02/06 20:26:58 james
38 * *** empty log message ***
40 * Revision 1.10 2008/02/06 17:53:28 james
41 * *** empty log message ***
43 * Revision 1.9 2008/02/06 15:53:22 james
44 * *** empty log message ***
46 * Revision 1.8 2008/02/06 11:49:47 james
47 * *** empty log message ***
49 * Revision 1.7 2008/02/06 11:30:37 james
50 * *** empty log message ***
52 * Revision 1.6 2008/02/05 01:11:46 james
53 * *** empty log message ***
55 * Revision 1.5 2008/02/04 20:23:55 james
56 * *** empty log message ***
58 * Revision 1.4 2008/02/04 05:45:55 james
61 * Revision 1.3 2008/02/04 02:05:06 james
62 * *** empty log message ***
64 * Revision 1.2 2008/02/04 01:32:39 james
65 * *** empty log message ***
67 * Revision 1.1 2008/02/03 23:36:41 james
68 * *** empty log message ***
77 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
79 vt100|vt100-am|dec vt100 (w/advanced video):\
81 :co#80:it#8:li#24:vt#3:\
82 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
84 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
85 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
86 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
87 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
88 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
89 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
90 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
91 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
92 :us=\E[4m:tc=vt100+fnkeys:
94 vt100+fnkeys|dec vt100 numeric keypad:\
95 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
98 vt100+pfkeys|dec vt100 numeric keypad:\
99 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
101 vt100+keypad|dec vt100 numeric keypad no fkeys:\
102 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
107 so the parser needs to be able to at least do
175 if ((c >= 'a') && (c <= 'z'))
177 if ((c >= 'A') && (c <= 'Z'))
206 in_margins (VT102 * v, CRT_Pos p)
208 if (v->pos.x < v->top_margin.x)
210 if (v->pos.y < v->top_margin.y)
213 if (v->pos.x > v->bottom_margin.x)
215 if (v->pos.y > v->bottom_margin.y)
222 vt102_log_line (VT102 * v, int line)
225 CRT_Pos e = { VT102_COLS - 1, line };
226 CRT_Pos p = { 0, line };
229 log = fopen ("log", "a+");
235 for (; e.x > 0; --e.x)
237 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
241 for (; p.x <= e.x; ++p.x)
243 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
254 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
269 vt102_cursor_normalize (VT102 * v)
271 CRT_Pos *top, *bottom;
273 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
275 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
279 vt102_clip_cursor (v, v->screen_start, v->screen_end);
285 vt102_cursor_carriage_return (VT102 * v)
287 /*FISH*/ v->pos.x = v->top_margin.x;
292 vt102_cursor_advance_line (VT102 * v)
294 int couldscroll = in_margins (v, v->pos);
296 /*have wraped off end of last line in scrolling region */
297 /* (|| not necessary, but shuts compiler up */
298 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
301 vt102_log_line (v, v->pos.y);
302 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
312 vt102_cursor_advance (VT102 * v)
315 if (v->pos.x < v->bottom_margin.x)
327 vt102_do_pending_wrap (VT102 * v)
329 int couldscroll = in_margins (v, v->pos);
330 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
332 if (!v->pending_wrap)
336 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
337 v->pos.y, autowrap, couldscroll);
340 /*End of line but no autowrap, nothing to do*/
344 /*End of screen and not allowed to scroll, nothing to do*/
345 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
350 v->pos.x = v->top_margin.x;
357 vt102_cursor_advance_line (v);
362 vt102_cursor_retard (VT102 * v)
364 if (v->pos.x != v->top_margin.x)
371 vt102_reset_tabs (VT102 * v)
375 memset (v->tabs, 0, sizeof (v->tabs));
377 for (i = 0; i < VT102_COLS; i += 8)
383 vt102_cursor_advance_tab (VT102 * v)
385 if (v->pos.x == v->bottom_margin.x)
387 while (v->pos.x < v->bottom_margin.x)
390 if (v->tabs[v->pos.x])
396 vt102_cursor_home (VT102 * v)
398 v->pos = v->top_margin;
399 vt102_cursor_normalize (v);
404 vt102_cursor_absolute (VT102 * v, int x, int y)
406 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
408 v->pos.x = x + v->top_margin.x;
409 v->pos.y = y + v->top_margin.x;
416 vt102_cursor_normalize (v);
420 vt102_cursor_relative (VT102 * v, int x, int y)
424 vt102_cursor_normalize (v);
431 vt102_delete_from_line (VT102 * v, CRT_Pos p)
433 int n = v->bottom_margin.x - p.x;
441 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
442 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
445 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
446 /*But not attr due to vt102 bug*/
450 vt102_insert_into_line (VT102 * v, CRT_Pos p)
452 int n = v->bottom_margin.x - p.x;
460 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
461 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
464 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
465 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
471 vt102_change_mode (VT102 * v, int private, char *ns, int set)
487 if (m >= VT102_NMODES)
492 v->private_modes[m] = set;
495 case VT102_PRIVATE_MODE_CURSOR_MODE:
496 if (v->application_keypad_mode)
497 v->private_modes[m] = 0;
499 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
500 v->private_modes[m],set);
503 case VT102_PRIVATE_MODE_ORIGIN_MODE:
504 vt102_cursor_home (v);
513 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
518 vt102_parse_mode_string (VT102 * v, char *buf, int len)
521 char last = buf[len - 1];
525 memset (num, 0, sizeof (num));
526 o = sizeof (num) - 1;
544 vt102_change_mode (v, private, &num[o], last == 'h');
545 memset (num, 0, sizeof (num));
546 o = sizeof (num) - 1;
561 vt102_change_mode (v, private, &num[o], last == 'h');
567 vt102_change_attr (VT102 * v, char *na)
584 v->attr = CRT_ATTR_NORMAL;
587 v->attr |= CRT_ATTR_BOLD;
591 v->attr &= ~CRT_ATTR_BOLD;
594 v->attr |= CRT_ATTR_UNDERLINE;
597 v->attr &= ~CRT_ATTR_UNDERLINE;
600 v->attr |= CRT_ATTR_BLINK;
603 v->attr &= ~CRT_ATTR_BLINK;
606 v->attr |= CRT_ATTR_REVERSE;
609 v->attr &= ~CRT_ATTR_REVERSE;
614 fprintf (stderr, "unhandled SGR %d\n", a);
622 vt102_parse_attr_string (VT102 * v, char *buf, int len)
625 char last = buf[len - 1];
629 memset (num, 0, sizeof (num));
630 o = sizeof (num) - 1;
641 vt102_change_attr (v, &num[o]);
642 memset (num, 0, sizeof (num));
643 o = sizeof (num) - 1;
657 vt102_change_attr (v, &num[o]);
661 vt102_save_state (VT102 * v)
663 v->saved.pos = v->pos;
664 v->saved.attr = v->attr;
665 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
669 vt102_restore_state (VT102 * v)
671 v->pos = v->saved.pos;
672 v->attr = v->saved.attr;
673 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
674 vt102_cursor_normalize (v);
678 void vt102_scs(VT102 *c,int g,int s)
680 /*Ignoring charsets*/
684 vt102_parse_esc (VT102 * v, int c)
689 if (v->pos.y == v->bottom_margin.y)
691 vt102_log_line (v, v->pos.y);
692 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
696 vt102_cursor_relative (v, 0, 1);
703 if (v->pos.y == v->top_margin.y)
705 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
709 vt102_cursor_relative (v, 0, -1);
713 vt102_save_state (v);
716 vt102_restore_state (v);
719 v->application_keypad_mode = 1;
722 v->application_keypad_mode = 0;
726 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
733 vt102_parse_csi (VT102 * v, char *buf, int len)
762 vt102_cursor_relative (v, 0, -narg);
765 vt102_cursor_relative (v, 0, narg);
768 vt102_cursor_relative (v, narg, 0);
771 vt102_cursor_relative (v, -narg, 0);
774 vt102_cursor_relative (v, 0, narg);
775 vt102_cursor_carriage_return (v);
778 vt102_cursor_relative (v, 0, -narg);
779 vt102_cursor_carriage_return (v);
782 vt102_cursor_absolute (v, narg - 1, v->pos.y);
791 ptr = index (arg, ';');
793 x = atoi (ptr + 1) - 1;
797 vt102_cursor_absolute (v, x, y);
804 crt_erase (&v->crt, v->pos, v->screen_end, 1);
807 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
813 CRT_Pos ls = { 0, v->pos.y };
814 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
816 narg = 0; /*Different default */
821 crt_erase (&v->crt, v->pos, le, 1);
824 crt_erase (&v->crt, ls, v->pos, 1);
827 crt_erase (&v->crt, ls, le, 1);
835 vt102_delete_from_line (v, v->pos);
838 if ((v->pos.y >= v->top_margin.y)
839 && (v->pos.y <= v->bottom_margin.y))
842 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
847 if ((v->pos.y >= v->top_margin.y)
848 && (v->pos.y <= v->bottom_margin.y))
851 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
857 narg = 0; /*Different default */
862 v->tabs[v->pos.x] = 0;
865 memset (v->tabs, 0, sizeof (v->tabs));
872 vt102_parse_mode_string (v, &buf[1], len - 1);
876 vt102_parse_attr_string (v, &buf[1], len - 1);
879 v->top_margin = v->screen_start;
880 v->bottom_margin = v->screen_end;
882 if ((len > 2) && (ptr = index (arg, ';')))
885 v->top_margin.y = narg - 1;
886 v->bottom_margin.y = atoi (ptr) - 1;
889 if (v->top_margin.y < v->screen_start.y)
890 v->top_margin.y = v->screen_start.y;
891 if (v->top_margin.y > v->screen_end.y)
892 v->top_margin.y = v->screen_end.y;
893 if (v->bottom_margin.y < v->screen_start.y)
894 v->bottom_margin.y = v->screen_start.y;
895 if (v->bottom_margin.y > v->screen_end.y)
896 v->bottom_margin.y = v->screen_end.y;
898 vt102_cursor_home (v);
901 v->saved.pos = v->pos;
904 v->pos = v->saved.pos;
905 vt102_cursor_normalize (v);
911 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
918 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
928 vt102_status_line (VT102 * v, char *str)
931 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
935 ca->attr = CRT_ATTR_REVERSE;
946 vt102_parse_char (VT102 * v, int c,TTY *tty)
948 VT102_parser *p = &v->parser;
952 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
953 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
957 p->csi_buf[p->csi_ptr++] = c;
958 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
960 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
964 else if (p->in_escape)
969 p->csi_buf[p->csi_ptr++] = c;
973 else if (scs_starter(c)) {
980 vt102_parse_esc (v, c);
983 else if (p->in_scs) {
984 vt102_scs(v,p->in_scs,c);
999 tty_write (tty, "vt102", 5);
1005 vt102_cursor_retard (v);
1008 vt102_cursor_advance_tab (v);
1013 vt102_cursor_advance_line (v);
1014 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1017 vt102_cursor_carriage_return (v);
1021 /*Ignoring charsets*/
1025 /*Ignoring charsets*/
1048 /*regular character */ default:
1049 vt102_do_pending_wrap (v);
1051 if (v->modes[VT102_MODE_INSERT])
1052 vt102_insert_into_line (v, v->pos);
1054 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1055 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1056 vt102_cursor_advance (v);
1060 v->crt.pos = v->pos;
1061 v->crt.hide_cursor =
1062 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1064 if (v->current_line.y != v->pos.y)
1066 vt102_log_line (v, v->current_line.y);
1067 v->current_line = v->pos;
1070 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1073 vt102_parse (VT102 * v, char *buf, int len,TTY *t)
1076 vt102_parse_char (v, *(buf++),t);
1081 vt102_parser_reset (VT102_parser * p)
1091 vt102_send (VT102 * v, uint8_t key,TTY *tty)
1095 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1097 if ((key > 31) && (key < 127))
1099 tty_write (tty, &key, 1);
1118 tty_write (tty, &key, 1);
1121 tty_write (tty, &key, 1);
1122 if (v->modes[VT102_MODE_NEWLINE_MODE])
1125 tty_write (tty, &c, 1);
1141 tty_write (tty, &key, 1);
1149 tty_write (tty, &key, 1);
1160 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1162 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1163 tty_write (tty, &buf, sizeof (buf));
1167 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1168 tty_write (tty, &buf, sizeof (buf));
1186 if (v->application_keypad_mode)
1188 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1189 tty_write (tty, &buf, sizeof (buf));
1193 static char kpoff[KEY_NUM] = {
1212 tty_write (tty, &kpoff[key], 1);
1216 if (v->application_keypad_mode)
1218 uint8_t buf[] = { 033, 'O', 'M' };
1219 tty_write (tty, &buf, sizeof (buf));
1224 tty_write (tty, &c, 1);
1225 if (v->modes[VT102_MODE_NEWLINE_MODE])
1228 tty_write (tty, &c, 1);
1237 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1238 tty_write (tty, &buf, sizeof (buf));
1246 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1247 tty_write (tty, &buf, sizeof (buf));
1255 vt102_reset (VT102 * v)
1257 VT102_parser *p = &v->parser;
1259 vt102_parser_reset (p);
1263 v->application_keypad_mode = 0;
1265 v->current_line = v->pos;
1266 v->pending_wrap = 0;
1268 v->screen_start.x = 0;
1269 v->screen_start.y = 0;
1270 v->screen_end.x = VT102_COLS - 1;
1271 v->screen_end.y = VT102_ROWS - 1;
1273 v->top_margin = v->screen_start;
1274 v->bottom_margin = v->screen_end;
1276 memset (v->modes, 0, VT102_NMODES);
1277 memset (v->private_modes, 0, VT102_NMODES);
1279 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1280 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1281 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1282 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1284 vt102_cursor_home (v);
1285 vt102_reset_tabs (v);
1286 v->current_line = v->pos;
1288 vt102_save_state (v);
1290 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1295 vt102_dispatch (VT102 * v,TTY *tty)
1300 red = tty_read (tty, buf, sizeof (buf));
1308 vt102_parse (v, buf, red,tty);
1318 v = (VT102 *) malloc (sizeof (VT102));
1327 vt102_free (VT102 * v)