4 * Copyright (c) 2008 James McKenzie <james@fishsoup.dhs.org>,
9 static char rcsid[] = "$Id$";
13 * Revision 1.20 2008/02/07 12:16:04 james
14 * *** empty log message ***
16 * Revision 1.19 2008/02/07 11:27:02 james
17 * *** empty log message ***
19 * Revision 1.18 2008/02/07 01:59:25 james
20 * *** empty log message ***
22 * Revision 1.17 2008/02/07 01:58:28 james
23 * *** empty log message ***
25 * Revision 1.16 2008/02/07 01:57:46 james
26 * *** empty log message ***
28 * Revision 1.15 2008/02/07 00:43:27 james
29 * *** empty log message ***
31 * Revision 1.14 2008/02/07 00:40:23 james
32 * *** empty log message ***
34 * Revision 1.13 2008/02/07 00:39:59 james
35 * *** empty log message ***
37 * Revision 1.12 2008/02/07 00:39:13 james
38 * *** empty log message ***
40 * Revision 1.11 2008/02/06 20:26:58 james
41 * *** empty log message ***
43 * Revision 1.10 2008/02/06 17:53:28 james
44 * *** empty log message ***
46 * Revision 1.9 2008/02/06 15:53:22 james
47 * *** empty log message ***
49 * Revision 1.8 2008/02/06 11:49:47 james
50 * *** empty log message ***
52 * Revision 1.7 2008/02/06 11:30:37 james
53 * *** empty log message ***
55 * Revision 1.6 2008/02/05 01:11:46 james
56 * *** empty log message ***
58 * Revision 1.5 2008/02/04 20:23:55 james
59 * *** empty log message ***
61 * Revision 1.4 2008/02/04 05:45:55 james
64 * Revision 1.3 2008/02/04 02:05:06 james
65 * *** empty log message ***
67 * Revision 1.2 2008/02/04 01:32:39 james
68 * *** empty log message ***
70 * Revision 1.1 2008/02/03 23:36:41 james
71 * *** empty log message ***
80 :al=\E[L:dc=\E[P:dl=\E[M:ei=\E[4l:im=\E[4h:tc=vt100:
82 vt100|vt100-am|dec vt100 (w/advanced video):\
84 :co#80:it#8:li#24:vt#3:\
85 :DO=\E[%dB:LE=\E[%dD:RA=\E[?7l:RI=\E[%dC:SA=\E[?7h:\
87 :ac=``aaffggjjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~:\
88 :ae=^O:as=^N:bl=^G:cb=\E[1K:cd=\E[J:ce=\E[K:cl=\E[H\E[J:\
89 :cm=\E[%i%d;%dH:cr=^M:cs=\E[%i%d;%dr:ct=\E[3g:do=^J:\
90 :eA=\E(B\E)0:ho=\E[H:kb=^H:kd=\EOB:ke=\E[?1l\E>:kl=\EOD:\
91 :kr=\EOC:ks=\E[?1h\E=:ku=\EOA:le=^H:mb=\E[5m:md=\E[1m:\
92 :me=\E[m\017:mr=\E[7m:nd=\E[C:rc=\E8:\
93 :rs=\E>\E[?3l\E[?4l\E[?5l\E[?7h\E[?8h:sc=\E7:se=\E[m:\
94 :sf=^J:so=\E[7m:sr=\EM:st=\EH:ta=^I:ue=\E[m:up=\E[A:\
95 :us=\E[4m:tc=vt100+fnkeys:
97 vt100+fnkeys|dec vt100 numeric keypad:\
98 :k0=\EOy:k5=\EOt:k6=\EOu:k7=\EOv:k8=\EOl:k9=\EOw:k;=\EOx:\
101 vt100+pfkeys|dec vt100 numeric keypad:\
102 :@8=\EOM:k1=\EOP:k2=\EOQ:k3=\EOR:k4=\EOS:tc=vt100+keypad:
104 vt100+keypad|dec vt100 numeric keypad no fkeys:\
105 :K1=\EOq:K2=\EOr:K3=\EOs:K4=\EOp:K5=\EOn:
110 so the parser needs to be able to at least do
178 if ((c >= 'a') && (c <= 'z'))
180 if ((c >= 'A') && (c <= 'Z'))
209 in_margins (VT102 * v, CRT_Pos p)
211 if (v->pos.x < v->top_margin.x)
213 if (v->pos.y < v->top_margin.y)
216 if (v->pos.x > v->bottom_margin.x)
218 if (v->pos.y > v->bottom_margin.y)
225 vt102_log_line (VT102 * v, int line)
228 CRT_Pos e = { VT102_COLS - 1, line };
229 CRT_Pos p = { 0, line };
232 log = fopen ("log", "a+");
238 for (; e.x > 0; --e.x)
240 if (v->crt.screen[CRT_ADDR_POS (&e)].chr != ' ')
244 for (; p.x <= e.x; ++p.x)
246 int c = v->crt.screen[CRT_ADDR_POS (&p)].chr;
257 vt102_clip_cursor (VT102 * v, CRT_Pos tl, CRT_Pos br)
272 vt102_cursor_normalize (VT102 * v)
274 CRT_Pos *top, *bottom;
276 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
278 vt102_clip_cursor (v, v->top_margin, v->bottom_margin);
282 vt102_clip_cursor (v, v->screen_start, v->screen_end);
288 vt102_cursor_carriage_return (VT102 * v)
290 /*FISH*/ v->pos.x = v->top_margin.x;
295 vt102_cursor_advance_line (VT102 * v)
297 int couldscroll = in_margins (v, v->pos);
299 /*have wraped off end of last line in scrolling region */
300 /* (|| not necessary, but shuts compiler up */
301 if (((v->pos.y == v->bottom_margin.y) || (v->pos.y == v->screen_end.y)) &&
304 vt102_log_line (v, v->pos.y);
305 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
315 vt102_cursor_advance (VT102 * v)
318 if (v->pos.x < v->bottom_margin.x)
330 vt102_do_pending_wrap (VT102 * v)
332 int couldscroll = in_margins (v, v->pos);
333 int autowrap = v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP];
335 if (!v->pending_wrap)
339 fprintf (stderr, "ca: (%d,%d) autowrap %d couldscroll %d\n", v->pos.x,
340 v->pos.y, autowrap, couldscroll);
343 /*End of line but no autowrap, nothing to do*/
347 /*End of screen and not allowed to scroll, nothing to do*/
348 if ((v->pos.y == v->screen_end.y) && (!couldscroll))
353 v->pos.x = v->top_margin.x;
360 vt102_cursor_advance_line (v);
365 vt102_cursor_retard (VT102 * v)
367 if (v->pos.x != v->top_margin.x)
374 vt102_reset_tabs (VT102 * v)
378 memset (v->tabs, 0, sizeof (v->tabs));
380 for (i = 0; i < VT102_COLS; i += 8)
386 vt102_cursor_advance_tab (VT102 * v)
388 if (v->pos.x == v->bottom_margin.x)
390 while (v->pos.x < v->bottom_margin.x)
393 if (v->tabs[v->pos.x])
399 vt102_cursor_home (VT102 * v)
401 v->pos = v->top_margin;
402 vt102_cursor_normalize (v);
407 vt102_cursor_absolute (VT102 * v, int x, int y)
409 if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
411 v->pos.x = x + v->top_margin.x;
412 v->pos.y = y + v->top_margin.x;
419 vt102_cursor_normalize (v);
423 vt102_cursor_relative (VT102 * v, int x, int y)
427 vt102_cursor_normalize (v);
434 vt102_delete_from_line (VT102 * v, CRT_Pos p)
436 int n = v->bottom_margin.x - p.x;
444 memmove (&v->crt.screen[CRT_ADDR_POS (&p)],
445 &v->crt.screen[CRT_ADDR_POS (&p) + 1], sizeof (CRT_CA) * n);
448 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
449 /*But not attr due to vt102 bug*/
453 vt102_insert_into_line (VT102 * v, CRT_Pos p)
455 int n = v->bottom_margin.x - p.x;
463 memmove (&v->crt.screen[CRT_ADDR_POS (&p) + 1],
464 &v->crt.screen[CRT_ADDR_POS (&p)], sizeof (CRT_CA) * n);
467 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].chr = ' ';
468 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].attr = CRT_ATTR_NORMAL;
469 v->crt.screen[CRT_ADDR (p.y, v->bottom_margin.x)].color = CRT_COLOR_NORMAL;
475 vt102_change_mode (VT102 * v, int private, char *ns, int set)
491 if (m >= VT102_NMODES)
496 v->private_modes[m] = set;
499 case VT102_PRIVATE_MODE_CURSOR_MODE:
500 if (v->application_keypad_mode)
501 v->private_modes[m] = 0;
503 fprintf (stderr, "APPLICATION CURSOR MODE %d wanted %d\n",
504 v->private_modes[m],set);
507 case VT102_PRIVATE_MODE_ORIGIN_MODE:
508 vt102_cursor_home (v);
517 fprintf (stderr, "mode set=%d private=%d num=%d\n", set, private, m);
522 vt102_parse_mode_string (VT102 * v, char *buf, int len)
525 char last = buf[len - 1];
529 memset (num, 0, sizeof (num));
530 o = sizeof (num) - 1;
548 vt102_change_mode (v, private, &num[o], last == 'h');
549 memset (num, 0, sizeof (num));
550 o = sizeof (num) - 1;
565 vt102_change_mode (v, private, &num[o], last == 'h');
571 vt102_change_attr (VT102 * v, char *na)
588 v->attr = CRT_ATTR_NORMAL;
589 v->color= CRT_COLOR_NORMAL;
592 v->attr |= CRT_ATTR_BOLD;
596 v->attr &= ~CRT_ATTR_BOLD;
599 v->attr |= CRT_ATTR_UNDERLINE;
602 v->attr &= ~CRT_ATTR_UNDERLINE;
605 v->attr |= CRT_ATTR_BLINK;
608 v->attr &= ~CRT_ATTR_BLINK;
611 v->attr |= CRT_ATTR_REVERSE;
614 v->attr &= ~CRT_ATTR_REVERSE;
624 v->color &= CRT_COLOR_FG_MASK;
625 v->color |= ((a-30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
635 v->color &= CRT_COLOR_FG_MASK;
636 v->color |= (((a-90)|CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
640 v->color &= CRT_COLOR_FG_MASK;
641 v->color |= (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
651 v->color &= CRT_COLOR_BG_MASK;
652 v->color |= ((a-40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
662 v->color &= CRT_COLOR_BG_MASK;
663 v->color |= (((a-100)|CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
667 v->color &= CRT_COLOR_BG_MASK;
668 v->color |= (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
674 fprintf (stderr, "unhandled SGR %d\n", a);
682 vt102_parse_attr_string (VT102 * v, char *buf, int len)
685 char last = buf[len - 1];
689 memset (num, 0, sizeof (num));
690 o = sizeof (num) - 1;
701 vt102_change_attr (v, &num[o]);
702 memset (num, 0, sizeof (num));
703 o = sizeof (num) - 1;
717 vt102_change_attr (v, &num[o]);
721 vt102_save_state (VT102 * v)
723 v->saved.pos = v->pos;
724 v->saved.attr = v->attr;
725 v->saved.color= v->color;
726 v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
730 vt102_restore_state (VT102 * v)
732 v->pos = v->saved.pos;
733 v->attr = v->saved.attr;
734 v->color= v->saved.color;
735 v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
736 vt102_cursor_normalize (v);
740 void vt102_scs(VT102 *c,int g,int s)
742 /*Ignoring charsets*/
746 vt102_parse_esc (VT102 * v, int c)
751 if (v->pos.y == v->bottom_margin.y)
753 vt102_log_line (v, v->pos.y);
754 crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1);
758 vt102_cursor_relative (v, 0, 1);
765 if (v->pos.y == v->top_margin.y)
767 crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1);
771 vt102_cursor_relative (v, 0, -1);
775 vt102_save_state (v);
778 vt102_restore_state (v);
781 v->application_keypad_mode = 1;
784 v->application_keypad_mode = 0;
788 fprintf (stderr, "unhandled ESC \\033 \\%03o (ESC %c)\n", c,
795 vt102_parse_csi (VT102 * v, char *buf, int len)
824 vt102_cursor_relative (v, 0, -narg);
827 vt102_cursor_relative (v, 0, narg);
830 vt102_cursor_relative (v, narg, 0);
833 vt102_cursor_relative (v, -narg, 0);
836 vt102_cursor_relative (v, 0, narg);
837 vt102_cursor_carriage_return (v);
840 vt102_cursor_relative (v, 0, -narg);
841 vt102_cursor_carriage_return (v);
844 vt102_cursor_absolute (v, narg - 1, v->pos.y);
853 ptr = index (arg, ';');
855 x = atoi (ptr + 1) - 1;
859 vt102_cursor_absolute (v, x, y);
866 crt_erase (&v->crt, v->pos, v->screen_end, 1);
869 crt_erase (&v->crt, v->screen_start, v->screen_end, 1);
875 CRT_Pos ls = { 0, v->pos.y };
876 CRT_Pos le = { VT102_COLS - 1, v->pos.y };
878 narg = 0; /*Different default */
883 crt_erase (&v->crt, v->pos, le, 1);
886 crt_erase (&v->crt, ls, v->pos, 1);
889 crt_erase (&v->crt, ls, le, 1);
897 vt102_delete_from_line (v, v->pos);
900 if ((v->pos.y >= v->top_margin.y)
901 && (v->pos.y <= v->bottom_margin.y))
904 crt_scroll_down (&v->crt, v->pos, v->bottom_margin, 1);
909 if ((v->pos.y >= v->top_margin.y)
910 && (v->pos.y <= v->bottom_margin.y))
913 crt_scroll_up (&v->crt, v->pos, v->bottom_margin, 0);
919 narg = 0; /*Different default */
924 v->tabs[v->pos.x] = 0;
927 memset (v->tabs, 0, sizeof (v->tabs));
934 vt102_parse_mode_string (v, &buf[1], len - 1);
938 vt102_parse_attr_string (v, &buf[1], len - 1);
941 v->top_margin = v->screen_start;
942 v->bottom_margin = v->screen_end;
944 if ((len > 2) && (ptr = index (arg, ';')))
947 v->top_margin.y = narg - 1;
948 v->bottom_margin.y = atoi (ptr) - 1;
951 if (v->top_margin.y < v->screen_start.y)
952 v->top_margin.y = v->screen_start.y;
953 if (v->top_margin.y > v->screen_end.y)
954 v->top_margin.y = v->screen_end.y;
955 if (v->bottom_margin.y < v->screen_start.y)
956 v->bottom_margin.y = v->screen_start.y;
957 if (v->bottom_margin.y > v->screen_end.y)
958 v->bottom_margin.y = v->screen_end.y;
960 vt102_cursor_home (v);
963 v->saved.pos = v->pos;
966 v->pos = v->saved.pos;
967 vt102_cursor_normalize (v);
973 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
980 fprintf (stderr, "unhandled CSI \\033%s\n", buf, buf[0]);
990 vt102_status_line (VT102 * v, char *str)
993 CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
997 ca->attr = CRT_ATTR_REVERSE;
998 ca->color=CRT_COLOR_NORMAL;
1009 vt102_parse_char (VT102 * v, int c,TTY *tty)
1011 VT102_parser *p = &v->parser;
1015 fprintf (stderr, "char %c pc %d %d %d %d %d\n", (c < 32) ? 32 : c, c,
1016 p->in_csi, p->in_escape, v->pos.x, v->pos.y);
1020 p->csi_buf[p->csi_ptr++] = c;
1021 if (csi_ender (c) || (p->csi_ptr == VT102_CSI_LEN))
1023 vt102_parse_csi (v, p->csi_buf, p->csi_ptr);
1027 else if (p->in_escape)
1029 if (csi_starter (c))
1032 p->csi_buf[p->csi_ptr++] = c;
1036 else if (scs_starter(c)) {
1043 vt102_parse_esc (v, c);
1046 else if (p->in_scs) {
1047 vt102_scs(v,p->in_scs,c);
1062 tty_write (tty, "vt102", 5);
1068 vt102_cursor_retard (v);
1071 vt102_cursor_advance_tab (v);
1076 vt102_cursor_advance_line (v);
1077 if (!v->modes[VT102_MODE_NEWLINE_MODE])
1080 vt102_cursor_carriage_return (v);
1084 /*Ignoring charsets*/
1088 /*Ignoring charsets*/
1111 /*regular character */ default:
1112 vt102_do_pending_wrap (v);
1114 if (v->modes[VT102_MODE_INSERT])
1115 vt102_insert_into_line (v, v->pos);
1117 v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = c;
1118 v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
1119 v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
1120 vt102_cursor_advance (v);
1124 v->crt.pos = v->pos;
1125 v->crt.hide_cursor =
1126 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] ? 0 : 1;
1128 if (v->current_line.y != v->pos.y)
1130 vt102_log_line (v, v->current_line.y);
1131 v->current_line = v->pos;
1134 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1137 vt102_parse (VT102 * v, char *buf, int len,TTY *t)
1140 vt102_parse_char (v, *(buf++),t);
1145 vt102_parser_reset (VT102_parser * p)
1155 vt102_send (VT102 * v, uint8_t key,TTY *tty)
1159 fprintf (stderr, "vts: %d(%c)\n", key, (key > 31) ? key : ' ');
1161 if ((key > 31) && (key < 127))
1163 tty_write (tty, &key, 1);
1182 tty_write (tty, &key, 1);
1185 tty_write (tty, &key, 1);
1186 if (v->modes[VT102_MODE_NEWLINE_MODE])
1189 tty_write (tty, &c, 1);
1205 tty_write (tty, &key, 1);
1213 tty_write (tty, &key, 1);
1224 if (v->private_modes[VT102_PRIVATE_MODE_CURSOR_MODE])
1226 uint8_t buf[] = { 033, 'O', 'A' + (key - KEY_UP) };
1227 tty_write (tty, &buf, sizeof (buf));
1231 uint8_t buf[] = { 033, '[', 'A' + (key - KEY_UP) };
1232 tty_write (tty, &buf, sizeof (buf));
1250 if (v->application_keypad_mode)
1252 uint8_t buf[] = { 033, 'O', 'a' + (key - KEY_154) };
1253 tty_write (tty, &buf, sizeof (buf));
1257 static char kpoff[KEY_NUM] = {
1276 tty_write (tty, &kpoff[key], 1);
1280 if (v->application_keypad_mode)
1282 uint8_t buf[] = { 033, 'O', 'M' };
1283 tty_write (tty, &buf, sizeof (buf));
1288 tty_write (tty, &c, 1);
1289 if (v->modes[VT102_MODE_NEWLINE_MODE])
1292 tty_write (tty, &c, 1);
1301 uint8_t buf[] = { 033, 'O', 'P' + (key - KEY_PF1) };
1302 tty_write (tty, &buf, sizeof (buf));
1310 uint8_t buf[] = { 033, '[', '0' + (key - KEY_180), '~' };
1311 tty_write (tty, &buf, sizeof (buf));
1319 vt102_reset (VT102 * v)
1321 VT102_parser *p = &v->parser;
1323 vt102_parser_reset (p);
1326 v->attr=CRT_ATTR_NORMAL;
1327 v->color=CRT_COLOR_NORMAL;
1329 v->application_keypad_mode = 0;
1331 v->current_line = v->pos;
1332 v->pending_wrap = 0;
1334 v->screen_start.x = 0;
1335 v->screen_start.y = 0;
1336 v->screen_end.x = VT102_COLS - 1;
1337 v->screen_end.y = VT102_ROWS - 1;
1339 v->top_margin = v->screen_start;
1340 v->bottom_margin = v->screen_end;
1342 memset (v->modes, 0, VT102_NMODES);
1343 memset (v->private_modes, 0, VT102_NMODES);
1345 v->private_modes[VT102_PRIVATE_MODE_AUTO_WRAP] = 1;
1346 v->private_modes[VT102_PRIVATE_MODE_AUTO_REPEAT] = 1;
1347 v->private_modes[VT102_PRIVATE_MODE_SHOW_CURSOR] = 1;
1348 v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
1350 vt102_cursor_home (v);
1351 vt102_reset_tabs (v);
1352 v->current_line = v->pos;
1354 vt102_save_state (v);
1356 vt102_status_line (v, "VT102 foo bar baz I'm the urban spaceman baby");
1361 vt102_dispatch (VT102 * v,TTY *tty)
1366 red = tty_read (tty, buf, sizeof (buf));
1374 vt102_parse (v, buf, red,tty);
1384 v = (VT102 *) malloc (sizeof (VT102));
1393 vt102_free (VT102 * v)