OSDN Git Service

Change to MIT License.
[nkf/nkf.git] / nkf.c
diff --git a/nkf.c b/nkf.c
index dff9241..d587aac 100644 (file)
--- a/nkf.c
+++ b/nkf.c
@@ -1,41 +1,30 @@
-/** Network Kanji Filter. (PDS Version)
- ** -*- coding: ISO-2022-JP -*-
- ************************************************************************
- ** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
- ** \e$BO"Mm@h!'\e(B \e$B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j\e(B
- ** \e$B!J\e(BE-Mail Address: ichikawa@flab.fujitsu.co.jp\e$B!K\e(B
- ** Copyright (C) 1996,1998
- ** Copyright (C) 2002
- ** \e$BO"Mm@h!'\e(B \e$BN05eBg3X>pJs9)3X2J\e(B \e$B2OLn\e(B \e$B??<#\e(B  mime/X0208 support
- ** \e$B!J\e(BE-Mail Address: kono@ie.u-ryukyu.ac.jp\e$B!K\e(B
- ** \e$BO"Mm@h!'\e(B COW for DOS & Win16 & Win32 & OS/2
- ** \e$B!J\e(BE-Mail Address: GHG00637@niftyserve.or.p\e$B!K\e(B
- **
- **    \e$B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"\e(B
- **    \e$B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#\e(B
- **    \e$B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#\e(B
- **    \e$B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#\e(B
- **    \e$B%P%$%J%j$NG[I[$N:]$K$O\e(Bversion message\e$B$rJ]B8$9$k$3$H$r>r7o$H$7$^$9!#\e(B
- **    \e$B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#\e(B
- **
- **    Everyone is permitted to do anything on this program
- **    including copying, modifying, improving,
- **    as long as you don't try to pretend that you wrote it.
- **    i.e., the above copyright notice has to appear in all copies.
- **    Binary distribution requires original version messages.
- **    You don't have to ask before copying, redistribution or publishing.
- **    THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
- ***********************************************************************/
-
-/***********************************************************************
- * \e$B8=:_!"\e(Bnkf \e$B$O\e(B SorceForge \e$B$K$F%a%s%F%J%s%9$,B3$1$i$l$F$$$^$9!#\e(B
- * http://sourceforge.jp/projects/nkf/
- ***********************************************************************/
+/*
+ * Copyright (c) 1987, Fujitsu LTD. (Itaru ICHIKAWA).
+ * Copyright (c) 1996-2009, The nkf Project.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
 #define NKF_VERSION "2.0.8"
-#define NKF_RELEASE_DATE "2009-01-04"
+#define NKF_RELEASE_DATE "2009-01-05"
 #define COPY_RIGHT \
-    "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW\n" \
-    "Copyright (C) 2002-2009 Kono, Furukawa, Naruse, mastodon"
+    "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa).\n" \
+    "Copyright (C) 1996-2009, The nkf Project."
 
 #include "config.h"
 #include "nkf.h"
@@ -140,7 +129,7 @@ enum nkf_encodings {
     JIS_X_0212        = 0x1159, /* D */
     /* JIS_X_0213_2000_1 = 0x1228, */ /* O */
     JIS_X_0213_2 = 0x1229, /* P */
-    JIS_X_0213_1 = 0x1233, /* Q */
+    JIS_X_0213_1 = 0x1233 /* Q */
 };
 
 static nkf_char s_iconv(nkf_char c2, nkf_char c1, nkf_char c0);
@@ -814,6 +803,60 @@ nkf_default_encoding()
     return enc;
 }
 
+typedef struct {
+    long capa;
+    long len;
+    unsigned char *ptr;
+} nkf_buf_t;
+
+static nkf_buf_t *
+nkf_buf_new(int length)
+{
+    nkf_buf_t *buf = nkf_xmalloc(sizeof(nkf_buf_t));
+    buf->ptr = nkf_xmalloc(length);
+    buf->capa = length;
+    buf->len = 0;
+    return buf;
+} 
+
+static void
+nkf_buf_dispose(nkf_buf_t *buf)
+{
+    nkf_xfree(buf->ptr);
+    nkf_xfree(buf);
+}
+
+#define nkf_buf_length(buf) ((buf)->len)
+#define nkf_buf_empty_p(buf) ((buf)->len == 0)
+
+static unsigned char
+nkf_buf_at(nkf_buf_t *buf, int index)
+{
+    assert(index <= buf->len);
+    return buf->ptr[index];
+}
+
+static void
+nkf_buf_clear(nkf_buf_t *buf)
+{
+    buf->ptr = 0;
+}
+
+static void
+nkf_buf_push(nkf_buf_t *buf, unsigned char c)
+{
+    assert(buf->capa > buf->len);
+    buf->ptr[buf->len++] = c;
+}
+
+static unsigned char
+nkf_buf_pop(nkf_buf_t *buf)
+{
+    assert(!nkf_buf_empty_p(buf));
+    return buf->ptr[--buf->len];
+}
+
+/* Normalization Form C */
 #ifndef PERL_XS
 #ifdef WIN32DLL
 #define fprintf dllprintf
@@ -841,6 +884,8 @@ usage(void)
            "         After 'W' you can add more options. -W[ 8, 16 [BL] ] \n"
 #endif
            "t        no conversion\n"
+           );
+    fprintf(HELP_OUTPUT,
            "i[@B]    Specify the Esc Seq for JIS X 0208-1978/83 (DEFAULT B)\n"
            "o[BJH]   Specify the Esc Seq for ASCII/Roman        (DEFAULT B)\n"
            "r        {de/en}crypt ROT13/47\n"
@@ -849,11 +894,15 @@ usage(void)
            "M[BQ]    MIME encode [B:base64 Q:quoted]\n"
            "l        ISO8859-1 (Latin-1) support\n"
            "f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n"
+           );
+    fprintf(HELP_OUTPUT,
            "Z[0-4]   Default/0: Convert JISX0208 Alphabet to ASCII\n"
            "         1: Kankaku to one space  2: to two spaces  3: HTML Entity\n"
            "         4: JISX0208 Katakana to JISX0201 Katakana\n"
            "X,x      Assume X0201 kana in MS-Kanji, -x preserves X0201\n"
            "B[0-2]   Broken input  0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n"
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef MSDOS
            "T        Text mode output\n"
 #endif
@@ -862,7 +911,8 @@ usage(void)
            "d,c      Convert line breaks  -d: LF  -c: CRLF\n"
            "-L[uwm]  line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n"
            "v, V     Show this usage. V: show configuration\n"
-           "\n"
+           "\n");
+    fprintf(HELP_OUTPUT,
            "Long name options\n"
            " --ic=<input codeset>  --oc=<output codeset>\n"
            "                   Specify the input or output codeset\n"
@@ -872,6 +922,8 @@ usage(void)
            " --hiragana  --katakana  --katakana-hiragana\n"
            "                   To Hiragana/Katakana Conversion\n"
            " --prefix=         Insert escape before troublesome characters of Shift_JIS\n"
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef INPUT_OPTION
            " --cap-input, --url-input  Convert hex after ':' or '%%'\n"
 #endif
@@ -882,6 +934,8 @@ usage(void)
            " --fb-{skip, html, xml, perl, java, subchar}\n"
            "                   Specify how nkf handles unassigned characters\n"
 #endif
+           );
+    fprintf(HELP_OUTPUT,
 #ifdef OVERWRITE
            " --in-place[=SUFFIX]  --overwrite[=SUFFIX]\n"
            "                   Overwrite original listed files by filtered result\n"
@@ -2616,25 +2670,25 @@ w_oconv32(nkf_char c2, nkf_char c1)
 }
 #endif
 
-#define SCORE_L2       (1)                   /* \e$BBh\e(B2\e$B?e=`4A;z\e(B */
-#define SCORE_KANA     (SCORE_L2 << 1)       /* \e$B$$$o$f$kH>3Q%+%J\e(B */
-#define SCORE_DEPEND   (SCORE_KANA << 1)     /* \e$B5!<o0MB8J8;z\e(B */
-#define SCORE_CP932    (SCORE_DEPEND << 1)   /* CP932 \e$B$K$h$kFI$_49$(\e(B (IBM extended characters) */
+#define SCORE_L2       (1)                   /* Kanji Level 2 */
+#define SCORE_KANA     (SCORE_L2 << 1)       /* Halfwidth Katakana */
+#define SCORE_DEPEND   (SCORE_KANA << 1)     /* MD Characters */
+#define SCORE_CP932    (SCORE_DEPEND << 1)   /* IBM extended characters */
 #define SCORE_X0212    (SCORE_CP932 << 1)    /* JIS X 0212 */
-#define SCORE_NO_EXIST (SCORE_X0212 << 1)    /* \e$BB8:_$7$J$$J8;z\e(B */
-#define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME \e$B$K$h$k;XDj\e(B */
-#define SCORE_ERROR    (SCORE_iMIME << 1) /* \e$B%(%i!<\e(B */
+#define SCORE_NO_EXIST (SCORE_X0212 << 1)    /* Undefined Characters */
+#define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME selected */
+#define SCORE_ERROR    (SCORE_iMIME << 1) /* Error */
 
 #define SCORE_INIT (SCORE_iMIME)
 
-static const char score_table_A0[] = {
+static const nkf_char score_table_A0[] = {
     0, 0, 0, 0,
     0, 0, 0, 0,
     0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
 };
 
-static const char score_table_F0[] = {
+static const nkf_char score_table_F0[] = {
     SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
     SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
     SCORE_DEPEND, SCORE_DEPEND, SCORE_CP932, SCORE_CP932,
@@ -3576,7 +3630,7 @@ z_conv(nkf_char c2, nkf_char c1)
     if (alpha_f & 16) {
        /* JIS X 0208 Katakana to JIS X 0201 Katakana */
        if (c2 == 0x21) {
-           char c = 0;
+           nkf_char c = 0;
            switch (c1) {
            case 0x23:
                /* U+3002 (0x8142) Ideographic Full Stop -> U+FF61 (0xA1) Halfwidth Ideographic Full Stop */
@@ -4216,91 +4270,36 @@ numchar_ungetc(nkf_char c, FILE *f)
 
 #ifdef UNICODE_NORMALIZATION
 
-typedef struct {
-    unsigned char *ary;
-    int max_length;
-    int count;
-} nkf_ary;
-
-static nkf_ary *
-nkf_ary_new(int length)
-{
-    nkf_ary *ary = nkf_xmalloc(sizeof(nkf_ary));
-    ary->ary = nkf_xmalloc(length);
-    ary->max_length = length;
-    ary->count = 0;
-    return ary;
-} 
-
-static void
-nkf_ary_dispose(nkf_ary *ary)
-{
-    nkf_xfree(ary->ary);
-    nkf_xfree(ary);
-}
-
-#define nkf_ary_length(ary) ((ary)->count)
-#define nkf_ary_empty_p(ary) ((ary)->count == 0)
-
-static unsigned char
-nkf_ary_at(nkf_ary *ary, int index)
-{
-    assert(index <= ary->count);
-    return ary->ary[index];
-}
-
-static void
-nkf_ary_clear(nkf_ary *ary)
-{
-    ary->count = 0;
-}
-
-static unsigned char
-nkf_ary_push(nkf_ary *ary, nkf_char c)
-{
-    assert(ary->max_length > ary->count);
-    ary->ary[ary->count++] = c;
-    return ary->count;
-}
-
-static unsigned char
-nkf_ary_pop(nkf_ary *ary)
-{
-    assert(0 < ary->count);
-    return ary->ary[--ary->count];
-}
-
-/* Normalization Form C */
 static nkf_char
 nfc_getc(FILE *f)
 {
     nkf_char (*g)(FILE *f) = i_nfc_getc;
     nkf_char (*u)(nkf_char c ,FILE *f) = i_nfc_ungetc;
-    nkf_ary *buf = nkf_ary_new(9);
+    nkf_buf_t *buf = nkf_buf_new(9);
     const unsigned char *array;
     int lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
     nkf_char c = (*g)(f);
 
     if (c == EOF || c > 0xFF || (c & 0xc0) == 0x80) return c;
 
-    nkf_ary_push(buf, (unsigned char)c);
+    nkf_buf_push(buf, (unsigned char)c);
     do {
        while (lower <= upper) {
            int mid = (lower+upper) / 2;
            int len;
            array = normalization_table[mid].nfd;
            for (len=0; len < NORMALIZATION_TABLE_NFD_LENGTH && array[len]; len++) {
-               if (len >= nkf_ary_length(buf)) {
+               if (len >= nkf_buf_length(buf)) {
                    c = (*g)(f);
                    if (c == EOF) {
                        len = 0;
                        lower = 1, upper = 0;
                        break;
                    }
-                   nkf_ary_push(buf, c);
+                   nkf_buf_push(buf, c);
                }
-               if (array[len] != nkf_ary_at(buf, len)) {
-                   if (array[len] < nkf_ary_at(buf, len)) lower = mid + 1;
+               if (array[len] != nkf_buf_at(buf, len)) {
+                   if (array[len] < nkf_buf_at(buf, len)) lower = mid + 1;
                    else  upper = mid - 1;
                    len = 0;
                    break;
@@ -4309,17 +4308,17 @@ nfc_getc(FILE *f)
            if (len > 0) {
                int i;
                array = normalization_table[mid].nfc;
-               nkf_ary_clear(buf);
+               nkf_buf_clear(buf);
                for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
-                   nkf_ary_push(buf, array[i]);
+                   nkf_buf_push(buf, array[i]);
                break;
            }
        }
     } while (lower <= upper);
 
-    while (nkf_ary_length(buf) > 1) (*u)(nkf_ary_pop(buf), f);
-    c = nkf_ary_pop(buf);
-    nkf_ary_dispose(buf);
+    while (nkf_buf_length(buf) > 1) (*u)(nkf_buf_pop(buf), f);
+    c = nkf_buf_pop(buf);
+    nkf_buf_dispose(buf);
 
     return c;
 }