(USE_XIM): New define.
[bpt/emacs.git] / src / ccl.c
index fb9fbf6..4f0dfbf 100644 (file)
--- a/src/ccl.c
+++ b/src/ccl.c
@@ -19,16 +19,14 @@ along with GNU Emacs; see the file COPYING.  If not, write to
 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
-#include <stdio.h>
-
 #ifdef emacs
-
 #include <config.h>
-
-#ifdef STDC_HEADERS
-#include <stdlib.h>
 #endif
 
+#include <stdio.h>
+
+#ifdef emacs
+
 #include "lisp.h"
 #include "charset.h"
 #include "ccl.h"
@@ -668,21 +666,31 @@ static tr_stack *mapping_stack_pointer;
 
 /* Encode one character CH to multibyte form and write to the current
    output buffer.  If CH is less than 256, CH is written as is.  */
-#define CCL_WRITE_CHAR(ch)                             \
-  do {                                                 \
-    if (!dst)                                          \
-      CCL_INVALID_CMD;                                 \
-    else                                               \
-      {                                                        \
-       unsigned char work[4], *str;                    \
-       int len = CHAR_STRING (ch, work, str);          \
-       if (dst + len <= (dst_bytes ? dst_end : src))   \
-         {                                             \
-           while (len--) *dst++ = *str++;              \
-         }                                             \
-       else                                            \
-         CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);        \
-      }                                                        \
+#define CCL_WRITE_CHAR(ch)                                     \
+  do {                                                         \
+    int bytes = SINGLE_BYTE_CHAR_P (ch) ? 1: CHAR_BYTES (ch);  \
+    if (ch == '\n' && ccl->eol_type == CODING_EOL_CRLF)                \
+      bytes++;                                                 \
+    if (!dst)                                                  \
+      CCL_INVALID_CMD;                                         \
+    else if (dst + bytes <= (dst_bytes ? dst_end : src))       \
+      {                                                                \
+       if (ch == '\n')                                         \
+         {                                                     \
+           if (ccl->eol_type == CODING_EOL_CRLF)               \
+             *dst++ = '\r', *dst++ = '\n';                     \
+           else if (ccl->eol_type == CODING_EOL_CR)            \
+             *dst++ = '\r';                                    \
+           else                                                \
+             *dst++ = '\n';                                    \
+         }                                                     \
+       else if (bytes == 1)                                    \
+         *dst++ = (ch);                                        \
+       else                                                    \
+         dst += CHAR_STRING (ch, dst);                         \
+      }                                                                \
+    else                                                       \
+      CCL_SUSPEND (CCL_STAT_SUSPEND_BY_DST);                   \
   } while (0)
 
 /* Write a string at ccl_prog[IC] of length LEN to the current output
@@ -716,6 +724,31 @@ static tr_stack *mapping_stack_pointer;
   } while (0)
 
 
+/* Set C to the character code made from CHARSET and CODE.  This is
+   like MAKE_CHAR but check the validity of CHARSET and CODE.  If they
+   are not valid, set C to (CODE & 0xFF) because that is usually the
+   case that CCL_ReadMultibyteChar2 read an invalid code and it set
+   CODE to that invalid byte.  */
+
+#define CCL_MAKE_CHAR(charset, code, c)                                \
+  do {                                                         \
+    if (charset == CHARSET_ASCII)                              \
+      c = code & 0xFF;                                         \
+    else if (CHARSET_DEFINED_P (charset)                       \
+            && (code & 0x7F) >= 32                             \
+            && (code < 256 || ((code >> 7) & 0x7F) >= 32))     \
+      {                                                                \
+       int c1 = code & 0x7F, c2 = 0;                           \
+                                                               \
+       if (code >= 256)                                        \
+         c2 = c1, c1 = (code >> 7) & 0x7F;                     \
+       c = MAKE_CHAR (charset, c1, c2);                        \
+      }                                                                \
+    else                                                       \
+      c = code & 0xFF;                                         \
+  } while (0)
+
+
 /* Execute CCL code on SRC_BYTES length text at SOURCE.  The resulting
    text goes to a place pointed by DESTINATION, the length of which
    should not exceed DST_BYTES.  The bytes actually processed is
@@ -911,7 +944,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
          i = reg[RRR];
          j = XINT (ccl_prog[ic]);
          op = field1 >> 6;
-         ic++;
+         jump_address = ic + 1;
          goto ccl_set_expr;
 
        case CCL_WriteRegister: /* CCCCCCCCCCCCCCCCCCCrrrXXXXX */
@@ -931,6 +964,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
          i = reg[RRR];
          j = reg[Rrr];
          op = field1 >> 6;
+         jump_address = ic;
          goto ccl_set_expr;
 
        case CCL_Call:          /* 1:CCCCCCCCCCCCCCCCCCCCFFFXXXXX */
@@ -992,8 +1026,9 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
          break;
 
        case CCL_End:           /* 0000000000000000000000XXXXX */
-         if (stack_idx-- > 0)
+         if (stack_idx > 0)
            {
+             stack_idx--;
              ccl_prog = ccl_prog_stack_struct[stack_idx].ccl_prog;
              ic = ccl_prog_stack_struct[stack_idx].ic;
              break;
@@ -1107,6 +1142,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
            {
              i = reg[rrr];
              CCL_WRITE_CHAR (i);
+             ic = jump_address;
            }
          else if (!reg[rrr])
            ic = jump_address;
@@ -1127,46 +1163,6 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
                  }
              
                i = *src++;
-               if (i == LEADING_CODE_COMPOSITION)
-                 {
-                   if (src >= src_end)
-                     goto ccl_read_multibyte_character_suspend;
-                   if (*src == 0xFF)
-                     {
-                       ccl->private_state = COMPOSING_WITH_RULE_HEAD;
-                       src++;
-                     }
-                   else
-                     ccl->private_state = COMPOSING_NO_RULE_HEAD;
-
-                   continue;
-                 }
-               if (ccl->private_state != COMPOSING_NO)
-                 {
-                   /* composite character */
-                   if (i < 0xA0)
-                     ccl->private_state = COMPOSING_NO;
-                   else
-                     {
-                       if (COMPOSING_WITH_RULE_RULE == ccl->private_state)
-                         {
-                           ccl->private_state = COMPOSING_WITH_RULE_HEAD;
-                           continue;
-                         }
-                       else if (COMPOSING_WITH_RULE_HEAD == ccl->private_state)
-                         ccl->private_state = COMPOSING_WITH_RULE_RULE;
-
-                       if (i == 0xA0)
-                         {
-                           if (src >= src_end)
-                             goto ccl_read_multibyte_character_suspend;
-                           i = *src++ & 0x7F;
-                         }
-                       else
-                         i -= 0x20;
-                     }
-                 }
-
                if (i < 0x80)
                  {
                    /* ASCII */
@@ -1207,6 +1203,18 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
                    reg[rrr] = ((i << 7) | (*src & 0x7F));
                    src++;
                  }
+               else if (i == LEADING_CODE_8_BIT_CONTROL)
+                 {
+                   if (src >= src_end)
+                     goto ccl_read_multibyte_character_suspend;
+                   reg[RRR] = CHARSET_8_BIT_CONTROL;
+                   reg[rrr] = (*src++ - 0x20);
+                 }
+               else if (i >= 0xA0)
+                 {
+                   reg[RRR] = CHARSET_8_BIT_GRAPHIC;
+                   reg[rrr] = i;
+                 }
                else
                  {
                    /* INVALID CODE.  Return a single byte character.  */
@@ -1231,10 +1239,10 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
 
            case CCL_WriteMultibyteChar2:
              i = reg[RRR]; /* charset */
-             if (i == CHARSET_ASCII)
+             if (i == CHARSET_ASCII
+                 || i == CHARSET_8_BIT_CONTROL
+                 || i == CHARSET_8_BIT_GRAPHIC)
                i = reg[rrr] & 0xFF;
-             else if (i == CHARSET_COMPOSITION)
-               i = MAKE_COMPOSITE_CHAR (reg[rrr]);
              else if (CHARSET_DIMENSION (i) == 1)
                i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
              else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
@@ -1247,21 +1255,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
              break;
 
            case CCL_TranslateCharacter:
-             i = reg[RRR]; /* charset */
-             if (i == CHARSET_ASCII)
-               i = reg[rrr];
-             else if (i == CHARSET_COMPOSITION)
-               {
-                 reg[RRR] = -1;
-                 break;
-               }
-             else if (CHARSET_DIMENSION (i) == 1)
-               i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
-             else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
-               i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
-             else
-               i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
-
+             CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
              op = translate_char (GET_TRANSLATION_TABLE (reg[Rrr]),
                                   i, -1, 0, 0);
              SPLIT_CHAR (op, reg[RRR], i, j);
@@ -1274,21 +1268,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
            case CCL_TranslateCharacterConstTbl:
              op = XINT (ccl_prog[ic]); /* table */
              ic++;
-             i = reg[RRR]; /* charset */
-             if (i == CHARSET_ASCII)
-               i = reg[rrr];
-             else if (i == CHARSET_COMPOSITION)
-               {
-                 reg[RRR] = -1;
-                 break;
-               }
-             else if (CHARSET_DIMENSION (i) == 1)
-               i = ((i - 0x70) << 7) | (reg[rrr] & 0x7F);
-             else if (i < MIN_CHARSET_PRIVATE_DIMENSION2)
-               i = ((i - 0x8F) << 14) | (reg[rrr] & 0x3FFF);
-             else
-               i = ((i - 0xE0) << 14) | (reg[rrr] & 0x3FFF);
-
+             CCL_MAKE_CHAR (reg[RRR], reg[rrr], i);
              op = translate_char (GET_TRANSLATION_TABLE (op), i, -1, 0, 0);
              SPLIT_CHAR (op, reg[RRR], i, j);
              if (j != -1)
@@ -1495,6 +1475,7 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
                      }
                    else if (EQ (content, Qlambda))
                      {
+                       reg[RRR] = i;
                        break;
                      }
                    else
@@ -1537,13 +1518,13 @@ ccl_driver (ccl, source, destination, src_bytes, dst_bytes, consumed)
                  reg[RRR] = -1;
                else
                  {
+                   reg[RRR] = 0;
                    content = XVECTOR (map)->contents[point];
                    if (NILP (content))
                      reg[RRR] = -1;
                    else if (NUMBERP (content))
                      reg[rrr] = XINT (content);
-                   else if (EQ (content, Qt))
-                     reg[RRR] = i;
+                   else if (EQ (content, Qt));
                    else if (CONSP (content))
                      {
                        attrib = XCAR (content);
@@ -1786,6 +1767,7 @@ setup_ccl_program (ccl, ccl_prog)
   ccl->private_state = 0;
   ccl->status = 0;
   ccl->stack_idx = 0;
+  ccl->eol_type = CODING_EOL_LF;
   return 0;
 }