chiark / gitweb /
*** empty log message ***
[sympathy.git] / src / vt102.c
index b4fe38191ab41a67ca3ac3adf64d656b441176a4..721886bd6f32e6fdf1b1b5aae6c6365c3badd5f1 100644 (file)
@@ -10,6 +10,9 @@ static char rcsid[] = "$Id$";
 
 /*
  * $Log$
+ * Revision 1.41  2008/02/26 23:23:17  james
+ * *** empty log message ***
+ *
  * Revision 1.40  2008/02/26 19:00:59  james
  * *** empty log message ***
  *
@@ -295,7 +298,7 @@ ESC+B
 
 #define TABLE_LENGTH 128
 
-static char terminal_id[]="vt102";
+static char terminal_id[] = "vt102";
 
 /* number of aditional chars after \033 ... to complete the escape if it's fixed length*/
 int vt102_cmd_length[TABLE_LENGTH] = {
@@ -341,14 +344,15 @@ csi_ender (int c)
   return 0;
 }
 
-
-
-/*Stay or enter command acquision and exit after n more chars*/
-void
-redispatch_with_more (VT102_parser * p, int n)
+static inline int ctrl_chr(int ch,int term)
 {
+if ((term>0) && (ch==term)) return 0;
+if (ch==033) return 0;
+if ((ch>0) && (ch<040)) return 1;
+return 0;
 }
 
+
 static inline int
 in_margins (VT102 * v, CRT_Pos p)
 {
@@ -368,9 +372,9 @@ in_margins (VT102 * v, CRT_Pos p)
 void
 vt102_log_line (Context * c, int line)
 {
-  CRT_Pos e = { VT102_COLS - 1, line };
+  CRT_Pos e = { c->v->current_width - 1, line };
   CRT_Pos p = { 0, line };
-  char logbuf[VT102_COLS + 1];
+  char logbuf[VT102_MAX_COLS + 1];
 
   if (!c->l)
     return;
@@ -551,8 +555,9 @@ vt102_cursor_advance (Context * c)
 void
 vt102_cursor_retreat (VT102 * v)
 {
-  if (v->pos.x != v->top_margin.x)
+  if (v->pos.x != v->top_margin.x) {
     v->pos.x--;
+  }
 
   v->pending_wrap = 0;
 }
@@ -564,7 +569,7 @@ vt102_reset_tabs (VT102 * v)
 
   memset (v->tabs, 0, sizeof (v->tabs));
 
-  for (i = 0; i < VT102_COLS; i += 8)
+  for (i = 0; i < VT102_MAX_COLS; i += 8)
     {
       v->tabs[i]++;
     }
@@ -610,7 +615,7 @@ vt102_cursor_absolute (VT102 * v, int x, int y)
   if (v->private_modes[VT102_PRIVATE_MODE_ORIGIN_MODE])
     {
       v->pos.x = x + v->top_margin.x;
-      v->pos.y = y + v->top_margin.x;
+      v->pos.y = y + v->top_margin.y;
     }
   else
     {
@@ -708,15 +713,20 @@ vt102_change_mode (VT102 * v, int private, char *ns, int set)
         case VT102_PRIVATE_MODE_ORIGIN_MODE:
           vt102_cursor_home (v);
           break;
-       case VT102_PRIVATE_MODE_132COLS:
-  /* We don't implement 132 col mode - yet*/
-  v->top_margin = v->screen_start;
-  v->bottom_margin = v->screen_end;
-  vt102_cursor_home(v);
-  crt_cls (&v->crt);
-       
+        case VT102_PRIVATE_MODE_132COLS:
+          /* We don't implement 132 col mode - yet */
+
+         v->current_width=v->private_modes[VT102_PRIVATE_MODE_132COLS] ? VT102_COLS_132:VT102_COLS_80;
 
-         break;
+         v->crt.width=v->current_width;
+         v->screen_end.x=v->current_width-1;
+          v->top_margin = v->screen_start;
+          v->bottom_margin = v->screen_end;
+          vt102_cursor_home (v);
+          crt_cls (&v->crt);
+
+
+          break;
         }
 
     }
@@ -956,7 +966,7 @@ vt102_restore_state (VT102 * v)
 }
 
 void
-vt102_regular_char (Context * c, VT102 * v, int ch)
+vt102_regular_char (Context * c, VT102 * v, uint32_t ch)
 {
 
   vt102_do_pending_wrap (c);
@@ -964,7 +974,12 @@ vt102_regular_char (Context * c, VT102 * v, int ch)
   if (v->modes[VT102_MODE_INSERT])
     vt102_insert_into_line (v, v->pos);
 
-  v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = ch;
+  if (ch < VT102_CHARSET_SIZE ){ 
+       int cs=charset_from_csid[v->g[v->cs]][ch];
+       v->crt.screen[CRT_ADDR_POS (&v->pos)].chr = cs ?cs:ch;
+  }  else {
+       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);
@@ -972,20 +987,46 @@ vt102_regular_char (Context * c, VT102 * v, int ch)
   v->last_reg_char = ch;
 }
 
-vt102_send_id(Context *c,char *buf)
+vt102_send_id (Context * c, char *buf)
 {
-       
-      if (c->t)
-        {
-       int l=strlen(buf);
-          c->t->xmit (c->t, buf,l);
-       }
+
+  if (c->t)
+    {
+      int l = strlen (buf);
+      c->t->xmit (c->t, buf, l);
+    }
 }
 
 void
 vt102_scs (Context * c, int g, int s)
 {
-/*Ignoring charsets*/
+VT102 *v=c->v;
+int cs=VT102_CSID_US;
+
+
+switch (s) {
+case 'A':
+       cs=VT102_CSID_UK;
+       break;
+case '1':
+case '2':
+case 'B':
+       cs=VT102_CSID_US;
+       break;
+case '0':
+       cs=VT102_CSID_GL;
+       break;
+}
+
+switch(g) {
+case '(':
+       v->g[0]=cs;
+       break;
+case ')':
+       v->g[1]=cs;
+       break;
+}
+
 }
 
 void
@@ -1088,7 +1129,7 @@ vt102_parse_csi (Context * c, char *buf, int len)
         case 'K':
           {
             CRT_Pos ls = { 0, v->pos.y };
-            CRT_Pos le = { VT102_COLS - 1, v->pos.y };
+            CRT_Pos le = { v->current_width -1, v->pos.y };
             if (len == 2)
               narg = 0;         /*Different default */
 
@@ -1174,7 +1215,7 @@ vt102_parse_csi (Context * c, char *buf, int len)
             vt102_regular_char (c, v, v->last_reg_char);
           break;
         case 'c':
-           vt102_send_id(c,terminal_id);
+          vt102_send_id (c, terminal_id);
           break;
         case 'd':
           vt102_cursor_absolute (v, v->pos.x, narg - 1);
@@ -1195,7 +1236,7 @@ vt102_parse_csi (Context * c, char *buf, int len)
             }
           break;
 
-        case 'i':      //Printer commands
+        case 'i':              //Printer commands
           //FIXME
           break;
         case 'h':
@@ -1207,13 +1248,13 @@ vt102_parse_csi (Context * c, char *buf, int len)
         case 'm':
           vt102_parse_attr_string (v, &buf[1], len - 1);
           break;
-        case 'n':       //Device status report
-       //5 requestion status
-       // 6 request cursor position FIXME
-       //?15n printer status
+        case 'n':              //Device status report
+          //5 requestion status
+          // 6 request cursor position FIXME
+          //?15n printer status
+          break;
+        case 'q':              //Load LED on off 
           break;
-       case 'q': //Load LED on off 
-       break;
         case 'r':
           v->top_margin = v->screen_start;
           v->bottom_margin = v->screen_end;
@@ -1244,8 +1285,8 @@ vt102_parse_csi (Context * c, char *buf, int len)
           vt102_cursor_normalize (v);
           v->pending_wrap = 0;
           break;
-       case 'y': //Invoke confidence test
-       break;
+        case 'y':              //Invoke confidence test
+          break;
 
         default:
           log_f (c->l, "<%s:%d unhandled CSI: \\033%s>", __FILE__, __LINE__,
@@ -1271,11 +1312,14 @@ vt102_parse_esc (Context * c)
   VT102_parser *p = &v->parser;
 
 #if 1
-p->cmd_buf[p->cmd_ptr]=0;
-log_f (c->l, "<cmd in  x=%d y=%d <ESC>%s >",v->pos.x,v->pos.y,p->cmd_buf);
+  p->cmd_buf[p->cmd_ptr] = 0;
+  log_f (c->l, "<cmd in  x=%3d y=%2d aw=%d <ESC>%s >", v->pos.x, v->pos.y,v->pending_wrap,
+         p->cmd_buf);
 #endif
 
-#if 0 //VT52
+if (v->private_modes[VT102_PRIVATE_MODE_VT52]) {
+int ate=1;
+
   switch (p->cmd_buf[0])
     {
     case 'A':
@@ -1291,14 +1335,16 @@ log_f (c->l, "<cmd in  x=%d y=%d <ESC>%s >",v->pos.x,v->pos.y,p->cmd_buf);
       vt102_cursor_relative (v, -1, 0);
       break;
     case 'F':
-       //Set graphics mode
-       break;
-       case 'G':
-       //Quit graphics mode
-       break;
+      v->cs=1;
+      v->g[1]=VT102_CSID_VT52;
+      break;
+    case 'G':
+      v->cs=0;
+      v->g[0]=VT102_CSID_US;
+      break;
     case 'H':
-       vt102_cursor_absolute (v, 0, 0);
-       break;
+      vt102_cursor_absolute (v, 0, 0);
+      break;
     case 'I':
       vt102_cursor_retreat_line (c);
       break;
@@ -1307,69 +1353,72 @@ log_f (c->l, "<cmd in  x=%d y=%d <ESC>%s >",v->pos.x,v->pos.y,p->cmd_buf);
       break;
     case 'K':
       {
-        CRT_Pos le = { VT102_COLS - 1, v->pos.y };
+        CRT_Pos le = { v->current_width - 1, v->pos.y };
         crt_erase (&v->crt, v->pos, le, 1, v->color);
       }
       break;
     case 'Y':
       vt102_cursor_absolute (v, p->cmd_buf[2] - 040, p->cmd_buf[1] - 040);
       break;
-case 'V': //Print current line
-       break;
-case 'W': //Printer on
-       break;
-case 'X': //printer off
-       break;
-case ']'://print screen
-       break;
-case 'Z'://ID
-       vt102_send_id(c,"\033/Z");
-       break;
-       }
-case '^': //Autoprint on
-       break;
-case '_'://Autoprint off
-       break;
-    case '=':
-      v->application_keypad_mode = 1;
+    case 'V':                  //Print current line
       break;
-    case '>':
-      v->application_keypad_mode = 0;
+    case 'W':                  //Printer on
+      break;
+    case 'X':                  //printer off
+      break;
+    case ']':                  //print screen
+      break;
+    case 'Z':                  //ID
+      vt102_send_id (c, "\033/Z");
       break;
+case '^':                      //Autoprint on
+  break;
+case '_':                      //Autoprint off
+  break;
+case '=':
+  v->application_keypad_mode = 1;
+  break;
+case '>':
+  v->application_keypad_mode = 0;
+  break;
+default:
+  ate=0;
+}
+if(ate) return;
+}
 
-#endif
 
 /*If you edit this switch don't forget to edit the length and termination tables*/
   switch (p->cmd_buf[0])
     {
-       case 'D':
-       vt102_cursor_advance_line(c);
-       break;
-       
+    case 'D':
+      vt102_cursor_advance_line (c);
+      break;
+
     case 'E':
-       vt102_cursor_advance_line(c);
-       v->pos.x=v->top_margin.x;
-       vt102_cursor_normalize (v);
-       v->pending_wrap = 0;
+      vt102_cursor_advance_line (c);
+      v->pos.x = v->top_margin.x;
+      vt102_cursor_normalize (v);
+      v->pending_wrap = 0;
       break;
     case 'H':
       v->tabs[v->pos.x]++;
       break;
-       case 'M':
+    case 'M':
       vt102_cursor_retreat_line (c);
       break;
-       case 'N': //select G2 for one char
-       break;
-       case 'O': //select G3 for one char
-       break;
-       case 'Z':
-           vt102_send_id(c,terminal_id);
-       break;
-       case 'c':
-      vt102_reset(v);
+    case 'N':                  //select G2 for one char
+      break;
+    case 'O':                  //select G3 for one char
+      break;
+    case 'Z':
+      vt102_send_id (c, terminal_id);
+      break;
+    case 'c':
+      vt102_reset (v);
 
 
-       break;
+      break;
     case '=':
       v->application_keypad_mode = 1;
       break;
@@ -1380,11 +1429,11 @@ case '_'://Autoprint off
     case '#':
       switch (p->cmd_buf[1])
         {
-       case '3': //top of double height line
-       case '4': //bottom of double height line
-       case '5': //single width line
-       case '6': //double width line
-       break;
+        case '3':              //top of double height line
+        case '4':              //bottom of double height line
+        case '5':              //single width line
+        case '6':              //double width line
+          break;
         case '8':
            /*DECALN*/
           {
@@ -1394,7 +1443,7 @@ case '_'://Autoprint off
             for (i = 0; i < CRT_ADDR_POS (&v->screen_end); ++i)
               v->crt.screen[i].chr = 'E';
           }
-       break;
+          break;
         default:
 
           log_f (c->l,
@@ -1433,25 +1482,32 @@ case '_'://Autoprint off
       ;
     }
 
-p->cmd_buf[p->cmd_ptr]=0;
-log_f (c->l, "<cmd out x=%d y=%d>",v->pos.x,v->pos.y);
+  p->cmd_buf[p->cmd_ptr] = 0;
+  log_f (c->l, "<cmd out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,v->pending_wrap);
 }
 
 void
 vt102_status_line (VT102 * v, char *str)
 {
-  int i = VT102_COLS;
+  int i = v->current_width -1;
   CRT_CA *ca = &v->crt.screen[CRT_ADDR (VT102_STATUS_ROW, 0)];
 
-  while (i--)
+  for (i=0;i<v->current_width;++i)
     {
       ca->attr = CRT_ATTR_REVERSE;
       ca->color = CRT_COLOR_NORMAL;
-      ca->chr = *str;
+      ca->chr = *str ? *str:' ';
       if (*str)
         str++;
       ca++;
     }
+  
+  for (;i<VT102_MAX_COLS;++i) {
+      ca->attr = CRT_ATTR_NORMAL;
+      ca->color = CRT_COLOR_NORMAL;
+       ca->chr=' ';
+      ca++;
+  }
 
 }
 
@@ -1476,6 +1532,10 @@ vt102_reset_state (VT102 * v)
 
   v->application_keypad_mode = 0;
 
+  v->current_width=VT102_COLS_80;
+  v->crt.width=v->current_width;
+  v->screen_end.x = v->current_width -1;
+
   v->top_margin = v->screen_start;
   v->bottom_margin = v->screen_end;
 
@@ -1488,6 +1548,9 @@ vt102_reset_state (VT102 * v)
   v->modes[VT102_MODE_LOCAL_ECHO_OFF] = 1;
 
   vt102_reset_tabs (v);
+
+  v->g[0]=v->g[1]=VT102_CSID_US;
+  v->cs=0;
 }
 
 static void
@@ -1548,16 +1611,19 @@ vt102_parse_char (Context * c, int ch)
     {
       vt102_reset_state (v);
     }
-  else if (p->in_cmd)
+  else if (p->in_cmd && !ctrl_chr(ch,p->cmd_termination))
     {
       p->cmd_buf[p->cmd_ptr++] = ch;
-      if (p->cmd_more_bytes) {
-        p->cmd_more_bytes--;
+       if (p->cmd_ptr==VT102_CMD_LEN)
+               p->in_cmd=0;
+      if (p->cmd_more_bytes)
+        {
+          p->cmd_more_bytes--;
 
 
-      if (!p->cmd_more_bytes == 1)
-        p->in_cmd = 0;
-       }
+          if (!p->cmd_more_bytes == 1)
+            p->in_cmd = 0;
+        }
 
       switch (p->cmd_termination)
         {
@@ -1580,9 +1646,8 @@ vt102_parse_char (Context * c, int ch)
           p->cmd_termination = 0;
         }
     }
-  else if (p->in_escape)
+  else if (p->in_escape && !ctrl_chr(ch,0))
     {
-
       p->cmd_ptr = 0;
       p->cmd_buf[p->cmd_ptr++] = ch;
       p->in_escape = 0;
@@ -1601,6 +1666,13 @@ vt102_parse_char (Context * c, int ch)
     }
   else
     {
+#if 1
+if (ch!=27)
+          log_f (c->l,
+                "<chr in  x=%3d y=%2d aw=%d  \\%03o %3d %c", 
+               v->pos.x, v->pos.y, v->pending_wrap,ch,ch,safe_ch(ch));
+
+#endif
 
       switch (ch)
         {
@@ -1611,7 +1683,7 @@ vt102_parse_char (Context * c, int ch)
          /*EOT*/ case 4:
           break;
          /*ENQ*/ case 5:
-           vt102_send_id(c,terminal_id);
+          vt102_send_id (c, terminal_id);
           break;
          /*ACK*/ case 6:
          /*BEL*/ case 7:
@@ -1633,11 +1705,11 @@ vt102_parse_char (Context * c, int ch)
           break;
          /*SO*/ case 14:
           /*select G1 */
-          /*Ignoring charsets */
+       v->cs=1;
           break;
          /*SI*/ case 15:
           /*select G0 */
-          /*Ignoring charsets */
+       v->cs=0;
           break;
          /*DLE*/ case 16:
         /*DC1 */ case 17:
@@ -1661,13 +1733,13 @@ vt102_parse_char (Context * c, int ch)
          /*DEL*/ case 127:
           break;
         /*regular character */ default:
-#if 1
-  log_f (c->l, "char %3d %c   ie=%d ic=%d cmb=%d ct=%3d    %2d %2d %d", ch,
-         safe_ch (ch), p->in_escape, p->in_cmd, p->cmd_more_bytes,
-         p->cmd_termination, v->pos.x, v->pos.y, v->pending_wrap);
-#endif
           vt102_regular_char (c, v, ch);
         }
+
+#if 1
+       if (ch!=27)
+       log_f (c->l, "<chr out x=%3d y=%2d aw=%d>", v->pos.x, v->pos.y,v->pending_wrap);
+#endif
     }
 
   v->crt.pos = v->pos;
@@ -1864,7 +1936,9 @@ vt102_reset (VT102 * v)
 
   v->screen_start.x = 0;
   v->screen_start.y = 0;
-  v->screen_end.x = VT102_COLS - 1;
+  v->current_width=VT102_COLS_80;
+  v->crt.width=v->current_width;
+  v->screen_end.x = v->current_width -1;
   v->screen_end.y = VT102_ROWS - 1;
 
   vt102_cursor_home (v);