OSDN Git Service

* add support for UTF-8-MAC
[nkf/nkf.git] / nkf.c
diff --git a/nkf.c b/nkf.c
index 09f0eb6..b36ebf3 100644 (file)
--- a/nkf.c
+++ b/nkf.c
 **        E-Mail: furukawa@tcp-ip.or.jp
 **    \e$B$^$G8fO"Mm$r$*4j$$$7$^$9!#\e(B
 ***********************************************************************/
-/* $Id: nkf.c,v 1.62 2005/02/27 01:22:02 rei_furukawa Exp $ */
-#define NKF_VERSION "2.0.4"
-#define NKF_RELEASE_DATE "2005-02-20"
+/* $Id: nkf.c,v 1.70 2005/07/05 12:39:00 naruse Exp $ */
+#define NKF_VERSION "2.0.5"
+#define NKF_RELEASE_DATE "2005-07-05"
 #include "config.h"
 
 static char *CopyRight =
-      "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2004 Kono, Furukawa";
+      "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2005 Kono, Furukawa, Naruse";
 
 
 /*
@@ -105,6 +105,11 @@ static char *CopyRight =
 #ifdef PERL_XS
 #undef OVERWRITE
 #endif
+#if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
+#define UNICODE_ENABLE
+#else
+#undef UNICODE_NORMALIZATION
+#endif
 
 #ifndef PERL_XS
 #include <stdio.h>
@@ -208,8 +213,8 @@ static char *CopyRight =
 
 #define                UTF8           12
 #define                UTF8_INPUT     13
-#define                UTF16LE_INPUT  14
-#define                UTF16BE_INPUT  15
+#define                UTF16BE_INPUT  14
+#define                UTF16LE_INPUT  15
 
 #define         WISH_TRUE      15
 
@@ -246,7 +251,7 @@ static char *CopyRight =
 #define         GETA2   0x2e
 
 
-#if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
+#ifdef UNICODE_ENABLE
 #define sizeof_euc_utf8 94
 #define sizeof_euc_to_utf8_1byte 94
 #define sizeof_euc_to_utf8_2bytes 94
@@ -389,17 +394,21 @@ static int             x0201_f = TRUE;         /* Assume JISX0201 kana */
 static int             x0201_f = NO_X0201;     /* Assume NO JISX0201 */
 #endif
 static int             iso2022jp_f = FALSE;    /* convert ISO-2022-JP */
+#ifdef UNICODE_ENABLE
+static int             internal_unicode_f = FALSE;   /* Internal Unicode Processing */
+#endif
 #ifdef UTF8_OUTPUT_ENABLE
 static int             unicode_bom_f= 0;   /* Output Unicode BOM */
 static int             w_oconv16_LE = 0;   /* utf-16 little endian */
 static int             ms_ucs_map_f = FALSE;   /* Microsoft UCS Mapping Compatible */
 #endif
 
-
-#ifdef NUMCHAR_OPTION
-
-#define CLASS_MASK  0x0f000000
-#define CLASS_UTF16 0x01000000
+#ifdef UNICODE_NORMALIZATION
+static int nfc_f = FALSE;
+static int (*i_nfc_getc)PROTO((FILE *)) = std_getc; /* input of ugetc */
+static int (*i_nfc_ungetc)PROTO((int c ,FILE *f)) = std_ungetc;
+STATIC int nfc_getc PROTO((FILE *f));
+STATIC int nfc_ungetc PROTO((int c,FILE *f));
 #endif
 
 #ifdef INPUT_OPTION
@@ -414,7 +423,11 @@ static int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
 static int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
 STATIC int url_getc PROTO((FILE *f));
 STATIC int url_ungetc PROTO((int c,FILE *f));
+#endif
 
+#ifdef NUMCHAR_OPTION
+#define CLASS_MASK  0x0f000000
+#define CLASS_UTF16 0x01000000
 static int numchar_f = FALSE;
 static int (*i_ngetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
 static int (*i_nungetc)PROTO((int c ,FILE *f)) = std_ungetc;
@@ -465,7 +478,7 @@ STATIC void s_status PROTO((struct input_code *, int));
 #ifdef UTF8_INPUT_ENABLE
 STATIC void w_status PROTO((struct input_code *, int));
 STATIC void w16_status PROTO((struct input_code *, int));
-static int             utf16_mode = UTF16LE_INPUT;
+static int             utf16_mode = UTF16BE_INPUT;
 #endif
 
 struct input_code input_code_list[] = {
@@ -743,6 +756,9 @@ main(argc, argv)
     } else {
       int nfiles = argc;
       while (argc--) {
+           is_inputcode_mixed = FALSE;
+           is_inputcode_set   = FALSE;
+           input_codename = "";
           if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
               perror(*--argv);
               return(-1);
@@ -923,6 +939,9 @@ struct {
 #ifdef X0212_ENABLE
     {"x0212", ""},
 #endif
+#ifdef UNICODE_ENABLE
+    {"internal-unicode", ""},
+#endif
 #ifdef UTF8_OUTPUT_ENABLE
     {"utf8", "w"},
     {"utf16", "w16"},
@@ -932,6 +951,9 @@ struct {
     {"utf8-input", "W"},
     {"utf16-input", "W16"},
 #endif
+#ifdef UNICODE_NORMALIZATION
+    {"utf8mac-input", ""},
+#endif
 #ifdef OVERWRITE
     {"overwrite", ""},
 #endif
@@ -967,13 +989,9 @@ options(cp)
 
     if (option_mode==1)
        return;
-    if (*cp++ != '-') 
-       return;
+    while(*cp && *cp++!='-');
     while (*cp) {
-       if (p && !*cp) {
-           cp = p;
-           p = 0;
-       }
+       p = 0;
         switch (*cp++) {
         case '-':  /* literal options */
            if (!*cp) {        /* ignore the rest of arguments */
@@ -983,8 +1001,8 @@ options(cp)
             for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
                int j;
                 p = (unsigned char *)long_option[i].name;
-                for (j=0;*p && (*p != '=') && *p == cp[j];p++, j++);
-               if (*p == cp[j]){
+                for (j=0;*p && *p != '=' && *p == cp[j];p++, j++);
+               if (*p == cp[j] || cp[j] == ' '){
                    p = &cp[j];
                    break;
                }
@@ -993,6 +1011,7 @@ options(cp)
            if (p == 0) return;
             cp = (unsigned char *)long_option[i].alias;
             if (!*cp){
+               cp = p;
 #ifdef OVERWRITE
                 if (strcmp(long_option[i].name, "overwrite") == 0){
                     file_out = TRUE;
@@ -1070,12 +1089,25 @@ options(cp)
                       return;
                   }
 #endif
+#ifdef UNICODE_ENABLE
+                if (strcmp(long_option[i].name, "internal-unicode") == 0){
+                    internal_unicode_f = TRUE;
+                    continue;
+                }
+#endif
 #ifdef UTF8_OUTPUT_ENABLE
                 if (strcmp(long_option[i].name, "ms-ucs-map") == 0){
                     ms_ucs_map_f = TRUE;
                     continue;
                 }
 #endif
+#ifdef UNICODE_NORMALIZATION
+               if (strcmp(long_option[i].name, "utf8mac-input") == 0){
+                   input_f = UTF8_INPUT;
+                   nfc_f = TRUE;
+                   continue;
+               }
+#endif
                 if (strcmp(long_option[i].name, "prefix=") == 0){
                     if (*p == '=' && ' ' < p[1] && p[1] < 128){
                         for (i = 2; ' ' < p[i] && p[i] < 128; i++){
@@ -1119,8 +1151,8 @@ options(cp)
             continue;
         case 'h':
             /*  
-                bit:1   hira -> kata
-                bit:2   kata -> hira
+                bit:1   katakana->hiragana
+                bit:2   hiragana->katakana
             */
             if ('9'>= *cp && *cp>='0') 
                 hira_f |= (*cp++ -'0');
@@ -1174,12 +1206,17 @@ options(cp)
 #ifdef UTF8_INPUT_ENABLE
         case 'W':           /* UTF-8 input */
             if ('1'== cp[0] && '6'==cp[1]) {
-               input_f = UTF16LE_INPUT;
+               input_f = UTF16BE_INPUT;
+               utf16_mode = UTF16BE_INPUT;
+               cp += 2;
                if (cp[0]=='L') {
                    cp++;
+                   input_f = UTF16LE_INPUT;
+                   utf16_mode = UTF16LE_INPUT;
                } else if (cp[0] == 'B') {
                    cp++;
                    input_f = UTF16BE_INPUT;
+                   utf16_mode = UTF16BE_INPUT;
                }
            } else if (cp[0] == '8') {
                cp++;
@@ -1246,7 +1283,7 @@ options(cp)
            }
             continue;
         case 'm':   /* MIME support */
-            mime_decode_f = TRUE;
+            /* mime_decode_f = TRUE; */ /* this has too large side effects... */
             if (*cp=='B'||*cp=='Q') {
                 mime_decode_mode = *cp++;
                 mimebuf_f = FIXED_MIME;
@@ -1255,6 +1292,7 @@ options(cp)
             } else if (*cp=='S') {
                 mime_f = STRICT_MIME; cp++;
             } else if (*cp=='0') {
+                mime_decode_f = FALSE;
                 mime_f = FALSE; cp++;
             }
             continue;
@@ -1311,8 +1349,7 @@ options(cp)
             continue;
         case ' ':    
         /* module muliple options in a string are allowed for Perl moudle  */
-           while(*cp && *cp!='-') cp++;
-            if(*cp=='-') cp++;
+           while(*cp && *cp++!='-');
             continue;
         default:
             /* bogus option but ignored */
@@ -1844,6 +1881,12 @@ module_connection()
         i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
     }
 #endif
+#ifdef UNICODE_NORMALIZATION
+    if (nfc_f && input_f == UTF8_INPUT){
+        i_nfc_getc = i_getc; i_getc = nfc_getc;
+        i_nfc_ungetc = i_ungetc; i_ungetc= nfc_ungetc;
+    }
+#endif
     if (mime_f && mimebuf_f==FIXED_MIME) {
        i_mgetc = i_getc; i_getc = mime_getc;
        i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
@@ -1859,6 +1902,8 @@ module_connection()
 #ifdef UTF8_INPUT_ENABLE
     } else if (input_f == UTF8_INPUT) {
         set_iconv(-TRUE, w_iconv);
+    } else if (input_f == UTF16BE_INPUT) {
+        set_iconv(-TRUE, w_iconv16);
     } else if (input_f == UTF16LE_INPUT) {
         set_iconv(-TRUE, w_iconv16);
 #endif
@@ -1884,6 +1929,7 @@ kanji_convert(f)
 {
     int    c1,
                     c2, c3;
+    int is_8bit = FALSE;
 
     module_connection();
     c2 = 0;
@@ -1948,6 +1994,7 @@ kanji_convert(f)
                 /* 8 bit code */
                 if (!estab_f && !iso8859_f) {
                     /* not established yet */
+                   if (!is_8bit) is_8bit = TRUE;
                     c2 = c1;
                     NEXT;
                 } else { /* estab_f==TRUE */
@@ -2197,6 +2244,18 @@ kanji_convert(f)
 
     /* epilogue */
     (*iconv)(EOF, 0, 0);
+    if (!is_inputcode_set)
+    {
+       if (is_8bit) {
+           struct input_code *p = input_code_list;
+           struct input_code *result = p;
+           while (p->name){
+               if (p->score < result->score) result = p;
+               ++p;
+           }
+           set_input_codename(result->name);
+       }
+    }
     return 1;
 }
 
@@ -2459,7 +2518,30 @@ w_iconv(c2, c1, c0)
     int    c2,
                     c1, c0;
 {
-    int ret = w2e_conv(c2, c1, c0, &c2, &c1);
+    int ret = 0;
+    unsigned short val = 0;
+    
+    if (c0 == 0){
+       if (c2 < 0x80 || (c2 & 0xc0) == 0xdf) /* 0x00-0x7f 0xc0-0xdf */
+           ; /* 1 or 2ytes */
+       else if ((c2 & 0xf0) == 0xe0) /* 0xe0-0xef */
+           return -1; /* 3bytes */
+       /*else if (0xf0 <= c2)
+           return 0; /* 4,5,6bytes */
+       else if ((c2 & 0xc0) == 0x80) /* 0x80-0xbf */
+           return 0; /* trail byte */
+       else return 0;
+    }
+    if (c2 == EOF);
+    else if (c2 == 0xef && c1 == 0xbb && c0 == 0xbf)
+       return 0; /* throw BOM */
+    else if (internal_unicode_f && (output_conv == w_oconv || output_conv == w_oconv16)){
+       val = ww16_conv(c2, c1, c0);
+       c2 = (val >> 8) & 0xff;
+       c1 = val & 0xff;
+    } else {
+       ret = w2e_conv(c2, c1, c0, &c2, &c1);
+    }
     if (ret == 0){
         (*oconv)(c2, c1);
     }
@@ -2545,16 +2627,16 @@ int
 w_iconv16(c2, c1, c0)
     int    c2, c1,c0;
 {
-    int ret;
+    int ret = 0;
 
     if (c2==0376 && c1==0377){
-       utf16_mode = UTF16LE_INPUT;
+       utf16_mode = UTF16BE_INPUT;
        return 0;    
     } else if (c2==0377 && c1==0376){
-       utf16_mode = UTF16BE_INPUT;
+       utf16_mode = UTF16LE_INPUT;
        return 0;    
     }
-    if (c2 != EOF && utf16_mode == UTF16BE_INPUT) {
+    if (c2 != EOF && utf16_mode == UTF16LE_INPUT) {
        int tmp;
        tmp=c1; c1=c2; c2=tmp;
     }
@@ -2562,7 +2644,8 @@ w_iconv16(c2, c1, c0)
        (*oconv)(c2, c1);
        return 0;
     }
-    ret = w16e_conv(((c2<<8)&0xff00) + c1, &c2, &c1);
+    if (internal_unicode_f && (output_conv == w_oconv || output_conv == w_oconv16));
+    else ret = w16e_conv(((c2<<8)&0xff00) + c1, &c2, &c1);
     if (ret) return ret;
     (*oconv)(c2, c1);
     return 0;
@@ -2647,6 +2730,7 @@ w_oconv(c2, c1)
                     c1;
 {
     int c0;
+    unsigned short val;
     if (c2 == EOF) {
         (*o_putc)(EOF);
         return;
@@ -2678,9 +2762,10 @@ w_oconv(c2, c1)
        output_mode = ISO8859_1;
         (*o_putc)(c1 | 0x080);
     } else {
-        unsigned short val;
         output_mode = UTF8;
-        val = e2w_conv(c2, c1);
+       if (internal_unicode_f && (iconv == w_iconv || iconv == w_iconv16))
+           val = ((c2<<8)&0xff00) + c1;
+       else val = e2w_conv(c2, c1);
         if (val){
             w16w_conv(val, &c2, &c1, &c0);
             (*o_putc)(c2);
@@ -2713,7 +2798,8 @@ w_oconv16(c2, c1)
        unicode_bom_f=1;
     }
 
-    if (c2 == ISO8859_1) {
+    if (internal_unicode_f && (iconv == w_iconv || iconv == w_iconv16)){
+    } else if (c2 == ISO8859_1) {
         c2 = 0;
         c1 |= 0x80;
 #ifdef NUMCHAR_OPTION
@@ -3841,6 +3927,57 @@ numchar_ungetc(c, f)
 }
 #endif
 
+#ifdef UNICODE_NORMALIZATION
+
+/* Normalization Form C */
+int
+nfc_getc(f)
+     FILE *f;
+{
+    int (*g)() = i_nfc_getc;
+    int (*u)() = i_nfc_ungetc;
+    int i=0, j, k=1, lower, upper;
+    int buf[9];
+    int *array = NULL;
+    extern struct normalization_pair normalization_table[];
+    
+    buf[i] = (*g)(f);
+    while (k > 0 && ((buf[i] & 0xc0) != 0x80)){
+       lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
+       while (upper >= lower) {
+           j = (lower+upper) / 2;
+           array = normalization_table[j].nfd;
+           for (k=0; k < NORMALIZATION_TABLE_NFD_LENGTH && array[k]; k++){
+               if (array[k] != buf[k]){
+                   array[k] < buf[k] ? (lower = j + 1) : (upper = j - 1);
+                   k = 0;
+                   break;
+               } else if (k >= i)
+                   buf[++i] = (*g)(f);
+           }
+           if (k > 0){
+               array = normalization_table[j].nfc;
+               for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
+                   buf[i] = array[i];
+               i--;
+               break;
+           }
+       }
+       while (i > 0)
+           (*u)(buf[i--], f);
+    }
+    return buf[0];
+}
+
+int
+nfc_ungetc(c, f)
+     int c;
+     FILE *f;
+{
+    return (*i_nfc_ungetc)(c, f);
+}
+#endif /* UNICODE_NORMALIZATION */
+
 
 int 
 mime_getc(f)
@@ -4576,11 +4713,17 @@ reinit()
      x0201_f = NO_X0201;
 #endif
     iso2022jp_f = FALSE;
+#ifdef UNICODE_ENABLE
+    internal_unicode_f = TRUE;
+#endif
 #ifdef UTF8_OUTPUT_ENABLE
     unicode_bom_f = 0;
     w_oconv16_LE = 0;
     ms_ucs_map_f = FALSE;
 #endif
+#ifdef UNICODE_NORMALIZATION
+    nfc_f = FALSE;
+#endif
 #ifdef INPUT_OPTION
     cap_f = FALSE;
     url_f = FALSE;
@@ -4607,7 +4750,7 @@ reinit()
         }
     }
 #ifdef UTF8_INPUT_ENABLE
-    utf16_mode = UTF16LE_INPUT;
+    utf16_mode = UTF16BE_INPUT;
 #endif
     mimeout_buf_count = 0;
     mimeout_mode = 0;
@@ -4707,7 +4850,7 @@ usage()
     fprintf(stderr,"t        no conversion\n");
     fprintf(stderr,"i_/o_    Output sequence to designate JIS-kanji/ASCII (DEFAULT B)\n");
     fprintf(stderr,"r        {de/en}crypt ROT13/47\n");
-    fprintf(stderr,"h        1 hirakana->katakana, 2 katakana->hirakana,3 both\n");
+    fprintf(stderr,"h        1 katakana->hiragana, 2 hiragana->katakana, 3 both\n");
     fprintf(stderr,"v        Show this usage. V: show version\n");
     fprintf(stderr,"m[BQN0]  MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
     fprintf(stderr,"M[BQ]    MIME encode [B:base64 Q:quoted]\n");
@@ -4730,6 +4873,7 @@ usage()
     fprintf(stderr," --hiragana, --katakana    Hiragana/Katakana Conversion\n");
     fprintf(stderr," --x0212                   Convert JISX0212\n");
     fprintf(stderr," --cp932, --no-cp932       CP932 compatibility\n");
+    fprintf(stderr," --prefix=    Insert escape before troublesome characters of Shift_JIS\n");
 #ifdef INPUT_OPTION
     fprintf(stderr," --cap-input, --url-input  Convert hex after ':' or '%%'\n");
 #endif