+ case 0:
+ v->attr = CRT_ATTR_NORMAL;
+ v->color = CRT_COLOR_NORMAL;
+ break;
+ case 1:
+ v->attr |= CRT_ATTR_BOLD;
+ break;
+ case 21:
+ case 22:
+ v->attr &= ~CRT_ATTR_BOLD;
+ break;
+ case 4:
+ v->attr |= CRT_ATTR_UNDERLINE;
+ break;
+ case 24:
+ v->attr &= ~CRT_ATTR_UNDERLINE;
+ break;
+ case 5:
+ v->attr |= CRT_ATTR_BLINK;
+ break;
+ case 25:
+ v->attr &= ~CRT_ATTR_BLINK;
+ break;
+ case 7:
+ v->attr |= CRT_ATTR_REVERSE;
+ break;
+ case 27:
+ v->attr &= ~CRT_ATTR_REVERSE;
+ break;
+ case 30:
+ case 31:
+ case 32:
+ case 33:
+ case 34:
+ case 35:
+ case 36:
+ case 37:
+ v->color &= ~CRT_COLOR_FG_MASK;
+ v->color |= ((a - 30) << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
+ break;
+ case 90:
+ case 91:
+ case 92:
+ case 93:
+ case 94:
+ case 95:
+ case 96:
+ case 97:
+ v->color &= ~CRT_COLOR_FG_MASK;
+ v->color |=
+ (((a -
+ 90) | CRT_COLOR_INTENSITY) << CRT_COLOR_FG_SHIFT) &
+ CRT_COLOR_FG_MASK;
+ break;
+ case 39:
+ case 99:
+ v->color &= ~CRT_COLOR_FG_MASK;
+ v->color |=
+ (CRT_FGCOLOR_NORMAL << CRT_COLOR_FG_SHIFT) & CRT_COLOR_FG_MASK;
+ break;
+ case 40:
+ case 41:
+ case 42:
+ case 43:
+ case 44:
+ case 45:
+ case 46:
+ case 47:
+ v->color &= ~CRT_COLOR_BG_MASK;
+ v->color |= ((a - 40) << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
+ break;
+ case 100:
+ case 101:
+ case 102:
+ case 103:
+ case 104:
+ case 105:
+ case 106:
+ case 107:
+ v->color &= ~CRT_COLOR_BG_MASK;
+ v->color |=
+ (((a -
+ 100) | CRT_COLOR_INTENSITY) << CRT_COLOR_BG_SHIFT) &
+ CRT_COLOR_BG_MASK;
+ break;
+ case 49:
+ case 109:
+ v->color &= ~CRT_COLOR_BG_MASK;
+ v->color |=
+ (CRT_BGCOLOR_NORMAL << CRT_COLOR_BG_SHIFT) & CRT_COLOR_BG_MASK;
+ break;
+
+ default:
+ ;
+#if 0
+ fprintf (stderr, "unhandled SGR %d\n", a);
+#endif
+ }
+
+}
+
+
+void
+vt102_parse_attr_string (VT102 * v, char *buf, int len)
+{
+ int private = 0;
+ char last = buf[len - 1];
+ char num[4];
+ int o;
+
+ memset (num, 0, sizeof (num));
+ o = sizeof (num) - 1;
+
+ len--;
+
+ if (len < 0)
+ return;
+
+ while (len--)
+ {
+ if (*buf == ';')
+ {
+ vt102_change_attr (v, &num[o]);
+ memset (num, 0, sizeof (num));
+ o = sizeof (num) - 1;
+ buf++;
+ continue;
+ }
+
+ num[0] = num[1];
+ num[1] = num[2];
+ num[2] = *buf;
+
+ if (o)
+ o--;
+
+ buf++;
+ }
+ vt102_change_attr (v, &num[o]);
+}
+
+void
+vt102_save_state (VT102 * v)
+{
+ v->saved.pos = v->pos;
+ v->saved.attr = v->attr;
+ v->saved.color = v->color;
+ v->saved.origin_mode = v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE];
+}
+
+void
+vt102_restore_state (VT102 * v)
+{
+ v->pos = v->saved.pos;
+ v->attr = v->saved.attr;
+ v->color = v->saved.color;
+ v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE] = v->saved.origin_mode;
+ vt102_cursor_normalize (v);
+ v->pending_wrap = 0;
+}
+
+void
+vt102_regular_char (Context * c, VT102 * v, int ch)
+{
+
+ vt102_do_pending_wrap (c);
+
+ if (v->modes[VT102_MODE_INSERT])
+ vt102_insert_into_line (v, v->pos);
+
+ v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
+ v->crt.screen[CRT_ADDR_POS (&v->pos)].attr = v->attr;
+ v->crt.screen[CRT_ADDR_POS (&v->pos)].color = v->color;
+ vt102_cursor_advance (c);
+
+ v->last_reg_char = ch;
+}
+
+void
+vt102_scs (Context * c, int g, int s)
+{
+/*Ignoring charsets*/
+}
+
+void
+vt102_parse_esc (Context * c)
+{
+ VT102 *v = c->v;
+ VT102_parser *p=&v->parser;
+
+/*If you edit this switch don't forget to edit the length and termination tables*/
+switch(p->cmd_buf[0]) {
+ case 'A':
+ vt102_cursor_relative (v, 0, -1);
+ break;
+ case 'B':
+ vt102_cursor_relative (v, 0, 1);
+ break;
+ case 'C':
+ vt102_cursor_relative (v, 1, 0);
+ break;
+ case 'D':
+ vt102_cursor_relative (v, -1, 0);
+ break;
+ case 'E':
+ if (v->pos.y == v->bottom_margin.y)
+ {
+ vt102_log_line (c, v->pos.y);
+ vt102_history (c, v->top_margin, v->bottom_margin);
+ crt_scroll_up (&v->crt, v->top_margin, v->bottom_margin, 1,
+ v->color);
+ }
+ else
+ {
+ vt102_cursor_relative (v, 0, 1);
+ }
+ break;
+ case 'H':
+ v->tabs[v->pos.x]++;
+ break;
+ case 'I':
+ vt102_cursor_retreat_line (c);
+ break;
+ case 'J':
+ crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
+ break;
+ case 'K':
+ {
+ CRT_Pos le = { VT102_COLS - 1, v->pos.y };
+ crt_erase (&v->crt, v->pos, le, 1, v->color);
+ }
+ break;
+ case 'M':
+ if (v->pos.y == v->top_margin.y)
+ {
+ crt_scroll_down (&v->crt, v->top_margin, v->bottom_margin, 1,
+ v->color);
+ }
+ else
+ {
+ vt102_cursor_relative (v, 0, -1);
+ }
+ break;
+
+ case 'Y':
+ vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
+ break;
+ case 'Z':
+ if (c->t)
+ {
+ c->t->xmit (c->t, terminal_reply, sizeof (terminal_reply));
+ }
+ break;
+ case '#':
+ {
+ switch(p->cmd_buf[1]) {
+ case '8':/*DECALN*/
+ {
+ int i;
+ crt_erase (&v->crt, v->screen_start, v->screen_end, 1, CRT_NORMAL_COLOR);
+ for (i=0;i<CRT_ADDR_POS(&v->screen_end);++i)
+ v->crt.screen[i].chr='E';
+ }
+ default:
+
+ log_f (c->l, "<%s:%d unhandled ESC: \\033 \\043 \\%03o (ESC # %c)>", __FILE__,
+ __LINE__, p->cmd_buf[1], safe_ch (p->cmd_buf[1]));
+
+ }
+ break;
+ case '<':
+ /*Set ansi mode - ignored */
+ break;
+
+ case '7':
+ vt102_save_state (v);
+ break;
+ case '8':
+ vt102_restore_state (v);
+ break;
+ case '=':
+ v->application_keypad_mode = 1;
+ break;
+ case '>':
+ v->application_keypad_mode = 0;
+ break;
+ case ']': /*Set various titles*/
+ //FIXME:
+ break;
+ case '[':
+ vt102_parse_csi (c, p->cmd_buf, p->cmd_ptr);
+ break;
+ case '(': /*Charsets*/
+ case ')':
+ case '+':
+ case '*':
+ case '%':
+ vt102_scs (c, p->cmd_buf[0], p->cmd_buf[1]);
+ break;
+ default:
+ log_f (c->l, "<%s:%d unhandled ESC: \\033 \\%03o (ESC %c)>", __FILE__,
+ __LINE__, p->cmd_buf[0], safe_ch (p->cmd_buf[0]));
+
+ ;
+ }
+}
+void
+vt102_parse_csi (Context * c, char *buf, int len)
+{
+ char last;
+ char *ptr;
+ char *arg = buf + 1;
+ int narg;
+
+ VT102 *v = c->v;
+
+ buf[len] = 0;
+
+ last = buf[len - 1];
+#if 0
+ buf[len - 1] = 0;
+#endif
+
+ if (len > 2)
+ {
+ narg = atoi (arg);
+ }
+ else
+ {
+ narg = 1;
+ }
+
+ switch (buf[0])
+ {
+ case '[':
+ switch (last)
+ {
+ case '@':
+ while (narg--)
+ vt102_insert_into_line (v, v->pos);
+ break;
+ case 'A':
+ vt102_cursor_relative (v, 0, -narg);
+ break;
+ case 'e':
+ case 'B':
+ vt102_cursor_relative (v, 0, narg);
+ break;
+ case 'a':
+ case 'C':
+ vt102_cursor_relative (v, narg, 0);
+ break;
+ case 'D':
+ vt102_cursor_relative (v, -narg, 0);
+ break;
+ case 'E':
+ vt102_cursor_relative (v, 0, narg);
+ vt102_cursor_carriage_return (v);
+ break;
+ case 'F':
+ vt102_cursor_relative (v, 0, -narg);
+ vt102_cursor_carriage_return (v);
+ break;
+ case 'G':
+ vt102_cursor_absolute (v, narg - 1, v->pos.y);
+ break;
+ case 'H':
+ case 'f':
+ {
+ int x, y;
+
+ y = narg - 1;
+
+ ptr = index (arg, ';');
+ if (ptr)
+ x = atoi (ptr + 1) - 1;
+ else
+ x = 0;
+
+ vt102_cursor_absolute (v, x, y);
+ }
+ break;
+ case 'I':
+ while (narg--)
+ vt102_cursor_advance_tab (c->v);
+ break;
+ case 'J':
+ if (len == 2)
+ narg = 0; /*Different default */
+ switch (narg)
+ {
+ case 0:
+ crt_erase (&v->crt, v->pos, v->screen_end, 1, v->color);
+ break;
+ case 1:
+ crt_erase (&v->crt, v->screen_start, v->pos, 1, v->color);
+ break;
+ case 2:
+ crt_erase (&v->crt, v->screen_start, v->screen_end, 1,
+ v->color);