4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.21 2008/02/07 12:21:16 james
14 * *** empty log message ***
16 * Revision 1.20 2008/02/07 12:16:04 james
17 * *** empty log message ***
19 * Revision 1.19 2008/02/07 11:27:02 james
20 * *** empty log message ***
22 * Revision 1.18 2008/02/07 01:59:25 james
23 * *** empty log message ***
25 * Revision 1.17 2008/02/07 01:58:28 james
26 * *** empty log message ***
28 * Revision 1.16 2008/02/07 01:57:46 james
29 * *** empty log message ***
31 * Revision 1.15 2008/02/07 00:43:27 james
32 * *** empty log message ***
34 * Revision 1.14 2008/02/07 00:40:23 james
35 * *** empty log message ***
37 * Revision 1.13 2008/02/07 00:39:59 james
38 * *** empty log message ***
40 * Revision 1.12 2008/02/07 00:39:13 james
41 * *** empty log message ***
43 * Revision 1.11 2008/02/06 20:26:58 james
44 * *** empty log message ***
46 * Revision 1.10 2008/02/06 17:53:28 james
47 * *** empty log message ***
49 * Revision 1.9 2008/02/06 15:53:22 james
50 * *** empty log message ***
52 * Revision 1.8 2008/02/06 11:49:47 james
53 * *** empty log message ***
55 * Revision 1.7 2008/02/06 11:30:37 james
56 * *** empty log message ***
58 * Revision 1.6 2008/02/05 01:11:46 james
59 * *** empty log message ***
61 * Revision 1.5 2008/02/04 20:23:55 james
62 * *** empty log message ***
64 * Revision 1.4 2008/02/04 05:45:55 james
67 * Revision 1.3 2008/02/04 02:05:06 james
68 * *** empty log message ***
70 * Revision 1.2 2008/02/04 01:32:39 james
71 * *** empty log message ***
73 * Revision 1.1 2008/02/03 23:36:41 james
74 * *** empty log message ***
83 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
85 vt100|vt100-am|dec vt100 (w/advanced video):\
87 :co#80:it#8:li#24:vt#3:\
88 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
90 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
91 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
92 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
93 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
94 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
95 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
96 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
97 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
98 :us=\E[4m:tc=vt100+fnkeys:
100 vt100+fnkeys|dec vt100 numeric keypad:\
101 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
104 vt100+pfkeys|dec vt100 numeric keypad:\
105 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
107 vt100+keypad|dec vt100 numeric keypad no fkeys:\
108 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
113 so the parser needs to be able to at least do
181 if ((c >= 'a') && (c <= 'z'))
183 if ((c >= 'A') && (c <= 'Z'))
212 in_margins (VT102 * v, CRT_Pos p)
214 if (v->pos.x < v->top_margin.x)
216 if (v->pos.y < v->top_margin.y)
219 if (v->pos.x > v->bottom_margin.x)
221 if (v->pos.y > v->bottom_margin.y)
228 vt102_log_line (VT102 * v, int line)
231 CRT_Pos e = { VT102_COLS - 1, line };
232 CRT_Pos p = { 0, line };
235 log = fopen ("log", "a+");
241 for (; e.x > 0; --e.x)
243 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
247 for (; p.x <= e.x; ++p.x)
249 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
260 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
275 vt102_cursor_normalize (VT102 * v)
277 CRT_Pos *top, *bottom;
279 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
281 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
285 vt102_clip_cursor (v, v->screen_start, v->screen_end);
291 vt102_cursor_carriage_return (VT102 * v)
293 /*FISH*/ v->pos.x = v->top_margin.x;
298 vt102_cursor_advance_line (VT102 * v)
300 int couldscroll = in_margins (v, v->pos);
302 /*have wraped off end of last line in scrolling region */
303 /* (|| not necessary, but shuts compiler up */
304 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
307 vt102_log_line (v, v->pos.y);
308 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
318 vt102_cursor_advance (VT102 * v)
321 if (v->pos.x < v->bottom_margin.x)
333 vt102_do_pending_wrap (VT102 * v)
335 int couldscroll = in_margins (v, v->pos);
336 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
338 if (!v->pending_wrap)
342 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
343 v->pos.y, autowrap, couldscroll);
346 /*End of line but no autowrap, nothing to do*/
350 /*End of screen and not allowed to scroll, nothing to do*/
351 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
356 v->pos.x = v->top_margin.x;
363 vt102_cursor_advance_line (v);
368 vt102_cursor_retard (VT102 * v)
370 if (v->pos.x != v->top_margin.x)
377 vt102_reset_tabs (VT102 * v)
381 memset (v->tabs, 0, sizeof (v->tabs));
383 for (i = 0; i < VT102_COLS; i += 8)
389 vt102_cursor_advance_tab (VT102 * v)
391 if (v->pos.x == v->bottom_margin.x)
393 while (v->pos.x < v->bottom_margin.x)
396 if (v->tabs[v->pos.x])
402 vt102_cursor_home (VT102 * v)
404 v->pos = v->top_margin;
405 vt102_cursor_normalize (v);
410 vt102_cursor_absolute (VT102 * v, int x, int y)
412 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
414 v->pos.x = x + v->top_margin.x;
415 v->pos.y = y + v->top_margin.x;
422 vt102_cursor_normalize (v);
426 vt102_cursor_relative (VT102 * v, int x, int y)
430 vt102_cursor_normalize (v);
437 vt102_delete_from_line (VT102 * v, CRT_Pos p)
439 int n = v->bottom_margin.x - p.x;
447 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
448 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
451 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
452 /*But not attr due to vt102 bug*/
456 vt102_insert_into_line (VT102 * v, CRT_Pos p)
458 int n = v->bottom_margin.x - p.x;
466 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
467 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
470 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
471 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
472 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
478 vt102_change_mode (VT102 * v, int private, char *ns, int set)
494 if (m >= VT102_NMODES)
499 v->private_modes[m] = set;
502 case VT102_PRIVATE_MODE_CURSOR_MODE:
503 if (v->application_keypad_mode)
504 v->private_modes[m] = 0;
506 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
507 v->private_modes[m],set);
510 case VT102_PRIVATE_MODE_ORIGIN_MODE:
511 vt102_cursor_home (v);
520 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
525 vt102_parse_mode_string (VT102 * v, char *buf, int len)
528 char last = buf[len - 1];
532 memset (num, 0, sizeof (num));
533 o = sizeof (num) - 1;
551 vt102_change_mode (v, private, &num[o], last == 'h');
552 memset (num, 0, sizeof (num));
553 o = sizeof (num) - 1;
568 vt102_change_mode (v, private, &num[o], last == 'h');
574 vt102_change_attr (VT102 * v, char *na)
591 v->attr = CRT_ATTR_NORMAL;
592 v->color= CRT_COLOR_NORMAL;
595 v->attr |= CRT_ATTR_BOLD;
599 v->attr &= ~CRT_ATTR_BOLD;
602 v->attr |= CRT_ATTR_UNDERLINE;
605 v->attr &= ~CRT_ATTR_UNDERLINE;
608 v->attr |= CRT_ATTR_BLINK;
611 v->attr &= ~CRT_ATTR_BLINK;
614 v->attr |= CRT_ATTR_REVERSE;
617 v->attr &= ~CRT_ATTR_REVERSE;
627 v->color &= ~CRT_COLOR_FG_MASK;
628 v->color |= ((a-30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
638 v->color &= ~CRT_COLOR_FG_MASK;
639 v->color |= (((a-90)|CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
643 v->color &= ~CRT_COLOR_FG_MASK;
644 v->color |= (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
654 v->color &= ~CRT_COLOR_BG_MASK;
655 v->color |= ((a-40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
665 v->color &= ~CRT_COLOR_BG_MASK;
666 v->color |= (((a-100)|CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
670 v->color &= ~CRT_COLOR_BG_MASK;
671 v->color |= (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
677 fprintf (stderr, "unhandled SGR %d\n", a);
685 vt102_parse_attr_string (VT102 * v, char *buf, int len)
688 char last = buf[len - 1];
692 memset (num, 0, sizeof (num));
693 o = sizeof (num) - 1;
704 vt102_change_attr (v, &num[o]);
705 memset (num, 0, sizeof (num));
706 o = sizeof (num) - 1;
720 vt102_change_attr (v, &num[o]);
724 vt102_save_state (VT102 * v)
726 v->saved.pos = v->pos;
727 v->saved.attr = v->attr;
728 v->saved.color= v->color;
729 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
733 vt102_restore_state (VT102 * v)
735 v->pos = v->saved.pos;
736 v->attr = v->saved.attr;
737 v->color= v->saved.color;
738 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
739 vt102_cursor_normalize (v);
743 void vt102_scs(VT102 *c,int g,int s)
745 /*Ignoring charsets*/
749 vt102_parse_esc (VT102 * v, int c)
754 if (v->pos.y == v->bottom_margin.y)
756 vt102_log_line (v, v->pos.y);
757 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
761 vt102_cursor_relative (v, 0, 1);
768 if (v->pos.y == v->top_margin.y)
770 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
774 vt102_cursor_relative (v, 0, -1);
778 vt102_save_state (v);
781 vt102_restore_state (v);
784 v->application_keypad_mode = 1;
787 v->application_keypad_mode = 0;
791 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
798 vt102_parse_csi (VT102 * v, char *buf, int len)
827 vt102_cursor_relative (v, 0, -narg);
830 vt102_cursor_relative (v, 0, narg);
833 vt102_cursor_relative (v, narg, 0);
836 vt102_cursor_relative (v, -narg, 0);
839 vt102_cursor_relative (v, 0, narg);
840 vt102_cursor_carriage_return (v);
843 vt102_cursor_relative (v, 0, -narg);
844 vt102_cursor_carriage_return (v);
847 vt102_cursor_absolute (v, narg - 1, v->pos.y);
856 ptr = index (arg, ';');
858 x = atoi (ptr + 1) - 1;
862 vt102_cursor_absolute (v, x, y);
869 crt_erase (&v->crt, v->pos, v->screen_end, 1);
872 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
878 CRT_Pos ls = { 0, v->pos.y };
879 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
881 narg = 0; /*Different default */
886 crt_erase (&v->crt, v->pos, le, 1);
889 crt_erase (&v->crt, ls, v->pos, 1);
892 crt_erase (&v->crt, ls, le, 1);
900 vt102_delete_from_line (v, v->pos);
903 if ((v->pos.y >= v->top_margin.y)
904 && (v->pos.y <= v->bottom_margin.y))
907 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
912 if ((v->pos.y >= v->top_margin.y)
913 && (v->pos.y <= v->bottom_margin.y))
916 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
922 narg = 0; /*Different default */
927 v->tabs[v->pos.x] = 0;
930 memset (v->tabs, 0, sizeof (v->tabs));
937 vt102_parse_mode_string (v, &buf[1], len - 1);
941 vt102_parse_attr_string (v, &buf[1], len - 1);
944 v->top_margin = v->screen_start;
945 v->bottom_margin = v->screen_end;
947 if ((len > 2) && (ptr = index (arg, ';')))
950 v->top_margin.y = narg - 1;
951 v->bottom_margin.y = atoi (ptr) - 1;
954 if (v->top_margin.y < v->screen_start.y)
955 v->top_margin.y = v->screen_start.y;
956 if (v->top_margin.y > v->screen_end.y)
957 v->top_margin.y = v->screen_end.y;
958 if (v->bottom_margin.y < v->screen_start.y)
959 v->bottom_margin.y = v->screen_start.y;
960 if (v->bottom_margin.y > v->screen_end.y)
961 v->bottom_margin.y = v->screen_end.y;
963 vt102_cursor_home (v);
966 v->saved.pos = v->pos;
969 v->pos = v->saved.pos;
970 vt102_cursor_normalize (v);
976 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
983 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
993 vt102_status_line (VT102 * v, char *str)
996 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
1000 ca->attr = CRT_ATTR_REVERSE;
1001 ca->color=CRT_COLOR_NORMAL;
1012 vt102_parse_char (VT102 * v, int c,TTY *tty)
1014 VT102_parser *p = &v->parser;
1018 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1019 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1023 p->csi_buf[p->csi_ptr++] = c;
1024 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
1026 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
1030 else if (p->in_escape)
1032 if (csi_starter (c))
1035 p->csi_buf[p->csi_ptr++] = c;
1039 else if (scs_starter(c)) {
1046 vt102_parse_esc (v, c);
1049 else if (p->in_scs) {
1050 vt102_scs(v,p->in_scs,c);
1065 tty_write (tty, "vt102", 5);
1071 vt102_cursor_retard (v);
1074 vt102_cursor_advance_tab (v);
1079 vt102_cursor_advance_line (v);
1080 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1083 vt102_cursor_carriage_return (v);
1087 /*Ignoring charsets*/
1091 /*Ignoring charsets*/
1114 /*regular character */ default:
1115 vt102_do_pending_wrap (v);
1117 if (v->modes[VT102_MODE_INSERT])
1118 vt102_insert_into_line (v, v->pos);
1120 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1121 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1122 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1123 vt102_cursor_advance (v);
1127 v->crt.pos = v->pos;
1128 v->crt.hide_cursor =
1129 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1131 if (v->current_line.y != v->pos.y)
1133 vt102_log_line (v, v->current_line.y);
1134 v->current_line = v->pos;
1137 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1140 vt102_parse (VT102 * v, char *buf, int len,TTY *t)
1143 vt102_parse_char (v, *(buf++),t);
1148 vt102_parser_reset (VT102_parser * p)
1158 vt102_send (VT102 * v, uint8_t key,TTY *tty)
1162 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1164 if ((key > 31) && (key < 127))
1166 tty_write (tty, &key, 1);
1185 tty_write (tty, &key, 1);
1188 tty_write (tty, &key, 1);
1189 if (v->modes[VT102_MODE_NEWLINE_MODE])
1192 tty_write (tty, &c, 1);
1208 tty_write (tty, &key, 1);
1216 tty_write (tty, &key, 1);
1227 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1229 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1230 tty_write (tty, &buf, sizeof (buf));
1234 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1235 tty_write (tty, &buf, sizeof (buf));
1253 if (v->application_keypad_mode)
1255 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1256 tty_write (tty, &buf, sizeof (buf));
1260 static char kpoff[KEY_NUM] = {
1279 tty_write (tty, &kpoff[key], 1);
1283 if (v->application_keypad_mode)
1285 uint8_t buf[] = { 033, 'O', 'M' };
1286 tty_write (tty, &buf, sizeof (buf));
1291 tty_write (tty, &c, 1);
1292 if (v->modes[VT102_MODE_NEWLINE_MODE])
1295 tty_write (tty, &c, 1);
1304 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1305 tty_write (tty, &buf, sizeof (buf));
1313 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1314 tty_write (tty, &buf, sizeof (buf));
1322 vt102_reset (VT102 * v)
1324 VT102_parser *p = &v->parser;
1326 vt102_parser_reset (p);
1329 v->attr=CRT_ATTR_NORMAL;
1330 v->color=CRT_COLOR_NORMAL;
1332 v->application_keypad_mode = 0;
1334 v->current_line = v->pos;
1335 v->pending_wrap = 0;
1337 v->screen_start.x = 0;
1338 v->screen_start.y = 0;
1339 v->screen_end.x = VT102_COLS - 1;
1340 v->screen_end.y = VT102_ROWS - 1;
1342 v->top_margin = v->screen_start;
1343 v->bottom_margin = v->screen_end;
1345 memset (v->modes, 0, VT102_NMODES);
1346 memset (v->private_modes, 0, VT102_NMODES);
1348 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1349 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1350 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1351 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1353 vt102_cursor_home (v);
1354 vt102_reset_tabs (v);
1355 v->current_line = v->pos;
1357 vt102_save_state (v);
1359 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1364 vt102_dispatch (VT102 * v,TTY *tty)
1369 red = tty_read (tty, buf, sizeof (buf));
1377 vt102_parse (v, buf, red,tty);
1387 v = (VT102 *) malloc (sizeof (VT102));
1396 vt102_free (VT102 * v)