OSDN Git Service

* new long option: --ic=<input codeset> and --oc=<output codeset>
[nkf/nkf.git] / nkf.c
1 /** Network Kanji Filter. (PDS Version)
2 ************************************************************************
3 ** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
4 ** \e$BO"Mm@h!'\e(B \e$B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j\e(B 
5 ** \e$B!J\e(BE-Mail Address: ichikawa@flab.fujitsu.co.jp\e$B!K\e(B
6 ** Copyright (C) 1996,1998
7 ** Copyright (C) 2002
8 ** \e$BO"Mm@h!'\e(B \e$BN05eBg3X>pJs9)3X2J\e(B \e$B2OLn\e(B \e$B??<#\e(B  mime/X0208 support
9 ** \e$B!J\e(BE-Mail Address: kono@ie.u-ryukyu.ac.jp\e$B!K\e(B
10 ** \e$BO"Mm@h!'\e(B COW for DOS & Win16 & Win32 & OS/2
11 ** \e$B!J\e(BE-Mail Address: GHG00637@niftyserve.or.p\e$B!K\e(B
12 **
13 **    \e$B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"\e(B
14 **    \e$B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#\e(B
15 **    \e$B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#\e(B
16 **    \e$B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#\e(B
17 **    \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
18 **    \e$B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#\e(B
19 **
20 **    Everyone is permitted to do anything on this program 
21 **    including copying, modifying, improving,
22 **    as long as you don't try to pretend that you wrote it.
23 **    i.e., the above copyright notice has to appear in all copies.  
24 **    Binary distribution requires original version messages.
25 **    You don't have to ask before copying, redistribution or publishing.
26 **    THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
27 ***********************************************************************/
28
29 /***********************************************************************
30 ** UTF-8 \e$B%5%]!<%H$K$D$$$F\e(B
31 **    \e$B=>Mh$N\e(B nkf \e$B$HF~$l$+$($F$=$N$^$^;H$($k$h$&$K$J$C$F$$$^$9\e(B
32 **    nkf -e \e$B$J$I$H$7$F5/F0$9$k$H!"<+F0H=JL$G\e(B UTF-8 \e$B$HH=Dj$5$l$l$P!"\e(B
33 **    \e$B$=$N$^$^\e(B euc-jp \e$B$KJQ49$5$l$^$9\e(B
34 **
35 **    \e$B$^$@%P%0$,$"$k2DG=@-$,9b$$$G$9!#\e(B
36 **    (\e$BFC$K<+F0H=JL!"%3!<%I:.:_!"%(%i!<=hM}7O\e(B)
37 **
38 **    \e$B2?$+LdBj$r8+$D$1$?$i!"\e(B
39 **        E-Mail: furukawa@tcp-ip.or.jp
40 **    \e$B$^$G8fO"Mm$r$*4j$$$7$^$9!#\e(B
41 ***********************************************************************/
42 /* $Id: nkf.c,v 1.81 2005/11/06 20:17:01 naruse Exp $ */
43 #define NKF_VERSION "2.0.5"
44 #define NKF_RELEASE_DATE "2005-11-07"
45 #include "config.h"
46
47 #define COPY_RIGHT \
48     "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2005 Kono, Furukawa, Naruse"
49
50
51 /*
52 **
53 **
54 **
55 ** USAGE:       nkf [flags] [file] 
56 **
57 ** Flags:
58 ** b    Output is buffered             (DEFAULT)
59 ** u    Output is unbuffered
60 **
61 ** t    no operation
62 **
63 ** j    Outout code is JIS 7 bit        (DEFAULT SELECT) 
64 ** s    Output code is MS Kanji         (DEFAULT SELECT) 
65 ** e    Output code is AT&T JIS         (DEFAULT SELECT) 
66 ** w    Output code is AT&T JIS         (DEFAULT SELECT) 
67 ** l    Output code is JIS 7bit and ISO8859-1 Latin-1
68 **
69 ** m    MIME conversion for ISO-2022-JP
70 ** I    Convert non ISO-2022-JP charactor to GETA by Pekoe <pekoe@lair.net>
71 ** i_ Output sequence to designate JIS-kanji (DEFAULT_J)
72 ** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)
73 ** M    MIME output conversion 
74 **
75 ** r  {de/en}crypt ROT13/47
76 **
77 ** v  display Version
78 **
79 ** T  Text mode output        (for MS-DOS)
80 **
81 ** x    Do not convert X0201 kana into X0208
82 ** Z    Convert X0208 alphabet to ASCII
83 **
84 ** f60  fold option
85 **
86 ** m    MIME decode
87 ** B    try to fix broken JIS, missing Escape
88 ** B[1-9]  broken level
89 **
90 ** O   Output to 'nkf.out' file or last file name
91 ** d   Delete \r in line feed 
92 ** c   Add \r in line feed 
93 ** -- other long option
94 ** -- ignore following option (don't use with -O )
95 **
96 **/
97
98 #if (defined(__TURBOC__) || defined(_MSC_VER) || defined(LSI_C) || defined(__MINGW32__)) && !defined(MSDOS)
99 #define MSDOS
100 #if (defined(__Win32__) || defined(_WIN32)) && !defined(__WIN32__)
101 #define __WIN32__
102 #endif
103 #endif
104
105 #ifdef PERL_XS
106 #undef OVERWRITE
107 #endif
108
109 #ifndef PERL_XS
110 #include <stdio.h>
111 #endif
112
113 #include <stdlib.h>
114 #include <string.h>
115
116 #if defined(MSDOS) || defined(__OS2__) 
117 #include <fcntl.h>
118 #include <io.h>
119 #endif
120
121 #ifdef MSDOS
122 #ifdef LSI_C
123 #define setbinmode(fp) fsetbin(fp)
124 #else /* Microsoft C, Turbo C */
125 #define setbinmode(fp) setmode(fileno(fp), O_BINARY)
126 #endif
127 #else /* UNIX,OS/2 */
128 #define setbinmode(fp)
129 #endif
130
131 #ifdef _IOFBF /* SysV and MSDOS, Windows */
132 #define       setvbuffer(fp, buf, size)       setvbuf(fp, buf, _IOFBF, size)
133 #else /* BSD */
134 #define       setvbuffer(fp, buf, size)       setbuffer(fp, buf, size)
135 #endif
136
137 /*Borland C++ 4.5 EasyWin*/
138 #if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
139 #define         EASYWIN
140 #ifndef __WIN16__
141 #define __WIN16__
142 #endif
143 #include <windows.h>
144 #endif
145
146 #ifdef OVERWRITE
147 /* added by satoru@isoternet.org */
148 #include <sys/stat.h>
149 #ifndef MSDOS /* UNIX, OS/2 */
150 #include <unistd.h>
151 #include <utime.h>
152 #else /* defined(MSDOS) */
153 #ifdef __WIN32__
154 #ifdef __BORLANDC__ /* BCC32 */
155 #include <utime.h>
156 #else /* !defined(__BORLANDC__) */
157 #include <sys/utime.h>
158 #endif /* (__BORLANDC__) */
159 #else /* !defined(__WIN32__) */
160 #if defined(_MSC_VER) || defined(__MINGW32__) /* VC++, MinGW */
161 #include <sys/utime.h>
162 #elif defined(__TURBOC__) /* BCC */
163 #include <utime.h>
164 #elif defined(LSI_C) /* LSI C */
165 #endif /* (__WIN32__) */
166 #endif
167 #endif
168 #endif 
169
170 #ifdef INT_IS_SHORT
171 #define int long
172 #endif
173
174 #define         FALSE   0
175 #define         TRUE    1
176
177 /* state of output_mode and input_mode  
178
179    c2           0 means ASCII
180                 X0201
181                 ISO8859_1
182                 X0208
183                 EOF      all termination
184    c1           32bit data
185
186  */
187
188 #define         ASCII           0
189 #define         X0208           1
190 #define         X0201           2
191 #define         ISO8859_1       8
192 #define         NO_X0201        3
193 #define         X0212           16
194
195 /* Input Assumption */
196
197 #define         JIS_INPUT       4
198 #define         SJIS_INPUT      5
199 #define         LATIN1_INPUT    6
200 #define         FIXED_MIME      7
201 #define         STRICT_MIME     8
202
203 /* MIME ENCODE */
204
205 #define         ISO2022JP       9
206 #define         JAPANESE_EUC   10
207 #define         SHIFT_JIS      11
208
209 #define         UTF8           12
210 #define         UTF8_INPUT     13
211 #define         UTF16BE_INPUT  14
212 #define         UTF16LE_INPUT  15
213
214 #define         WISH_TRUE      15
215
216 /* ASCII CODE */
217
218 #define         BS      0x08
219 #define         TAB     0x09
220 #define         NL      0x0a
221 #define         CR      0x0d
222 #define         ESC     0x1b
223 #define         SPACE   0x20
224 #define         AT      0x40
225 #define         SSP     0xa0
226 #define         DEL     0x7f
227 #define         SI      0x0f
228 #define         SO      0x0e
229 #define         SSO     0x8e
230 #define         SS3     0x8f
231
232 #define         is_alnum(c)  \
233             (('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
234
235 /* I don't trust portablity of toupper */
236 #define nkf_toupper(c)  (('a'<=c && c<='z')?(c-('a'-'A')):c)
237 #define nkf_isoctal(c)  ('0'<=c && c<='7')
238 #define nkf_isdigit(c)  ('0'<=c && c<='9')
239 #define nkf_isxdigit(c)  (nkf_isdigit(c) || ('a'<=c && c<='f') || ('A'<=c && c <= 'F'))
240 #define nkf_isblank(c) (c == SPACE || c == TAB)
241 #define nkf_isspace(c) (nkf_isblank(c) || c == CR || c == NL)
242 #define nkf_isalpha(c) (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))
243 #define nkf_isalnum(c) (nkf_isdigit(c) || nkf_isalpha(c))
244 #define hex2bin(x)     ( nkf_isdigit(x) ? x - '0' : nkf_toupper(x) - 'A' + 10)
245
246 #define         HOLD_SIZE       1024
247 #define         IOBUF_SIZE      16384
248
249 #define         DEFAULT_J       'B'
250 #define         DEFAULT_R       'B'
251
252 #define         SJ0162  0x00e1          /* 01 - 62 ku offset */
253 #define         SJ6394  0x0161          /* 63 - 94 ku offset */
254
255 #define         RANGE_NUM_MAX   18
256 #define         GETA1   0x22
257 #define         GETA2   0x2e
258
259
260 #if defined(UTF8_OUTPUT_ENABLE) || defined(UTF8_INPUT_ENABLE)
261 #define sizeof_euc_utf8 94
262 #define sizeof_euc_to_utf8_1byte 94
263 #define sizeof_euc_to_utf8_2bytes 94
264 #define sizeof_utf8_to_euc_C2 64
265 #define sizeof_utf8_to_euc_E5B8 64
266 #define sizeof_utf8_to_euc_2bytes 112
267 #define sizeof_utf8_to_euc_3bytes 112
268 #endif
269
270 /* MIME preprocessor */
271
272 #ifdef EASYWIN /*Easy Win */
273 extern POINT _BufferSize;
274 #endif
275
276 /*      function prototype  */
277
278 #ifdef ANSI_C_PROTOTYPE
279 #define PROTO(x)  x 
280 #define STATIC static
281 #define CONST const
282 #else
283 #define PROTO(x)  ()
284 #define STATIC
285 #define CONST
286 #endif
287
288 struct input_code{
289     char *name;
290     int stat;
291     int score;
292     int index;
293     int buf[3];
294     void (*status_func)PROTO((struct input_code *, int));
295     int (*iconv_func)PROTO((int c2, int c1, int c0));
296     int _file_stat;
297 };
298
299 STATIC char *input_codename = "";
300
301 #ifndef PERL_XS
302 STATIC const char *CopyRight = COPY_RIGHT;
303 #endif
304 #if !defined(PERL_XS) && !defined(WIN32DLL)
305 STATIC  int     noconvert PROTO((FILE *f));
306 #endif
307 STATIC  int     kanji_convert PROTO((FILE *f));
308 STATIC  int     h_conv PROTO((FILE *f,int c2,int c1));
309 STATIC  int     push_hold_buf PROTO((int c2));
310 STATIC  void    set_iconv PROTO((int f, int (*iconv_func)(int c2,int c1,int c0)));
311 STATIC  int     s_iconv PROTO((int c2,int c1,int c0));
312 STATIC  int     s2e_conv PROTO((int c2, int c1, int *p2, int *p1));
313 STATIC  int     e_iconv PROTO((int c2,int c1,int c0));
314 #ifdef UTF8_INPUT_ENABLE
315 STATIC  void    encode_fallback_html PROTO((int c));
316 STATIC  void    encode_fallback_xml PROTO((int c));
317 STATIC  void    encode_fallback_java PROTO((int c));
318 STATIC  void    encode_fallback_perl PROTO((int c));
319 STATIC  void    encode_fallback_subchar PROTO((int c));
320 STATIC  void    (*encode_fallback)PROTO((int c)) = NULL;
321 STATIC  int     w2e_conv PROTO((int c2,int c1,int c0,int *p2,int *p1));
322 STATIC  int     w_iconv PROTO((int c2,int c1,int c0));
323 STATIC  int     w_iconv16 PROTO((int c2,int c1,int c0));
324 STATIC  int     w_iconv_common PROTO((int c1,int c0,const unsigned short *const *pp,int psize,int *p2,int *p1));
325 STATIC  int     ww16_conv PROTO((int c2, int c1, int c0));
326 STATIC  int     w16e_conv PROTO((unsigned short val,int *p2,int *p1));
327 #endif
328 #ifdef UTF8_OUTPUT_ENABLE
329 STATIC  int     e2w_conv PROTO((int c2,int c1));
330 STATIC  void    w_oconv PROTO((int c2,int c1));
331 STATIC  void    w_oconv16 PROTO((int c2,int c1));
332 #endif
333 STATIC  void    e_oconv PROTO((int c2,int c1));
334 STATIC  int     e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
335 STATIC  void    s_oconv PROTO((int c2,int c1));
336 STATIC  void    j_oconv PROTO((int c2,int c1));
337 STATIC  void    fold_conv PROTO((int c2,int c1));
338 STATIC  void    cr_conv PROTO((int c2,int c1));
339 STATIC  void    z_conv PROTO((int c2,int c1));
340 STATIC  void    rot_conv PROTO((int c2,int c1));
341 STATIC  void    hira_conv PROTO((int c2,int c1));
342 STATIC  void    base64_conv PROTO((int c2,int c1));
343 STATIC  void    iso2022jp_check_conv PROTO((int c2,int c1));
344 STATIC  void    no_connection PROTO((int c2,int c1));
345 STATIC  int     no_connection2 PROTO((int c2,int c1,int c0));
346
347 STATIC  void    code_score PROTO((struct input_code *ptr));
348 STATIC  void    code_status PROTO((int c));
349
350 STATIC  void    std_putc PROTO((int c));
351 STATIC  int     std_getc PROTO((FILE *f));
352 STATIC  int     std_ungetc PROTO((int c,FILE *f));
353
354 STATIC  int     broken_getc PROTO((FILE *f));
355 STATIC  int     broken_ungetc PROTO((int c,FILE *f));
356
357 STATIC  int     mime_begin PROTO((FILE *f));
358 STATIC  int     mime_getc PROTO((FILE *f));
359 STATIC  int     mime_ungetc PROTO((int c,FILE *f));
360
361 STATIC  int     mime_begin_strict PROTO((FILE *f));
362 STATIC  int     mime_getc_buf PROTO((FILE *f));
363 STATIC  int     mime_ungetc_buf  PROTO((int c,FILE *f));
364 STATIC  int     mime_integrity PROTO((FILE *f,const unsigned char *p));
365
366 STATIC  int     base64decode PROTO((int c));
367 STATIC  void    mime_prechar PROTO((int c2, int c1));
368 STATIC  void    mime_putc PROTO((int c));
369 STATIC  void    open_mime PROTO((int c));
370 STATIC  void    close_mime PROTO(());
371 #ifndef PERL_XS
372 STATIC  void    usage PROTO(());
373 STATIC  void    version PROTO(());
374 #endif
375 STATIC  void    options PROTO((unsigned char *c));
376 #if defined(PERL_XS) || defined(WIN32DLL)
377 STATIC  void    reinit PROTO(());
378 #endif
379
380 /* buffers */
381
382 #if !defined(PERL_XS) && !defined(WIN32DLL)
383 STATIC unsigned char   stdibuf[IOBUF_SIZE];
384 STATIC unsigned char   stdobuf[IOBUF_SIZE];
385 #endif
386 STATIC unsigned char   hold_buf[HOLD_SIZE*2];
387 STATIC int             hold_count;
388
389 /* MIME preprocessor fifo */
390
391 #define MIME_BUF_SIZE   (1024)    /* 2^n ring buffer */
392 #define MIME_BUF_MASK   (MIME_BUF_SIZE-1)   
393 #define Fifo(n)         mime_buf[(n)&MIME_BUF_MASK]
394 STATIC unsigned char           mime_buf[MIME_BUF_SIZE];
395 STATIC unsigned int            mime_top = 0;
396 STATIC unsigned int            mime_last = 0;  /* decoded */
397 STATIC unsigned int            mime_input = 0; /* undecoded */
398 STATIC int (*mime_iconv_back)PROTO((int c2,int c1,int c0)) = NULL;
399
400 /* flags */
401 STATIC int             unbuf_f = FALSE;
402 STATIC int             estab_f = FALSE;
403 STATIC int             nop_f = FALSE;
404 STATIC int             binmode_f = TRUE;       /* binary mode */
405 STATIC int             rot_f = FALSE;          /* rot14/43 mode */
406 STATIC int             hira_f = FALSE;          /* hira/kata henkan */
407 STATIC int             input_f = FALSE;        /* non fixed input code  */
408 STATIC int             alpha_f = FALSE;        /* convert JIx0208 alphbet to ASCII */
409 STATIC int             mime_f = STRICT_MIME;   /* convert MIME B base64 or Q */
410 STATIC int             mime_decode_f = FALSE;  /* mime decode is explicitly on */
411 STATIC int             mimebuf_f = FALSE;      /* MIME buffered input */
412 STATIC int             broken_f = FALSE;       /* convert ESC-less broken JIS */
413 STATIC int             iso8859_f = FALSE;      /* ISO8859 through */
414 STATIC int             mimeout_f = FALSE;       /* base64 mode */
415 #if defined(MSDOS) || defined(__OS2__) 
416 STATIC int             x0201_f = TRUE;         /* Assume JISX0201 kana */
417 #else
418 STATIC int             x0201_f = NO_X0201;     /* Assume NO JISX0201 */
419 #endif
420 STATIC int             iso2022jp_f = FALSE;    /* convert ISO-2022-JP */
421 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
422 STATIC int             internal_unicode_f = FALSE;   /* Internal Unicode Processing */
423 #endif
424 #ifdef UTF8_OUTPUT_ENABLE
425 STATIC int             unicode_bom_f= 0;   /* Output Unicode BOM */
426 STATIC int             w_oconv16_LE = 0;   /* utf-16 little endian */
427 STATIC int             ms_ucs_map_f = FALSE;   /* Microsoft UCS Mapping Compatible */
428 STATIC int             unicode_subchar = '?'; /* the regular substitution character */
429 #endif
430
431 #ifdef UNICODE_NORMALIZATION
432 STATIC int nfc_f = FALSE;
433 STATIC int (*i_nfc_getc)PROTO((FILE *)) = std_getc; /* input of ugetc */
434 STATIC int (*i_nfc_ungetc)PROTO((int c ,FILE *f)) = std_ungetc;
435 STATIC int nfc_getc PROTO((FILE *f));
436 STATIC int nfc_ungetc PROTO((int c,FILE *f));
437 #endif
438
439 #ifdef INPUT_OPTION
440 STATIC int cap_f = FALSE;
441 STATIC int (*i_cgetc)PROTO((FILE *)) = std_getc; /* input of cgetc */
442 STATIC int (*i_cungetc)PROTO((int c ,FILE *f)) = std_ungetc;
443 STATIC int cap_getc PROTO((FILE *f));
444 STATIC int cap_ungetc PROTO((int c,FILE *f));
445
446 STATIC int url_f = FALSE;
447 STATIC int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
448 STATIC int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
449 STATIC int url_getc PROTO((FILE *f));
450 STATIC int url_ungetc PROTO((int c,FILE *f));
451 #endif
452
453 #ifdef NUMCHAR_OPTION
454 #define CLASS_MASK  0x0f000000
455 #define CLASS_UTF16 0x01000000
456 STATIC int numchar_f = FALSE;
457 STATIC int (*i_ngetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
458 STATIC int (*i_nungetc)PROTO((int c ,FILE *f)) = std_ungetc;
459 STATIC int numchar_getc PROTO((FILE *f));
460 STATIC int numchar_ungetc PROTO((int c,FILE *f));
461 #endif
462
463 #ifdef CHECK_OPTION
464 STATIC int noout_f = FALSE;
465 STATIC void no_putc PROTO((int c));
466 STATIC int debug_f = FALSE;
467 STATIC void debug PROTO((const char *str));
468 STATIC int (*iconv_for_check)() = 0;
469 #endif
470
471 STATIC int guess_f = FALSE;
472 #if !defined PERL_XS
473 STATIC  void    print_guessed_code PROTO((char *filename));
474 #endif
475 STATIC  void    set_input_codename PROTO((char *codename));
476 STATIC int is_inputcode_mixed = FALSE;
477 STATIC int is_inputcode_set   = FALSE;
478
479 #ifdef EXEC_IO
480 STATIC int exec_f = 0;
481 #endif
482
483 #ifdef SHIFTJIS_CP932
484 /* invert IBM extended characters to others and controls some UCS mapping */
485 STATIC int cp51932_f = TRUE;
486 #define CP932_TABLE_BEGIN (0xfa)
487 #define CP932_TABLE_END   (0xfc)
488
489 /* invert NEC-selected IBM extended characters to IBM extended characters */
490 STATIC int cp932inv_f = TRUE;
491 #define CP932INV_TABLE_BEGIN (0xed)
492 #define CP932INV_TABLE_END   (0xee)
493
494 /* STATIC int cp932_conv PROTO((int c2, int c1)); */
495 #endif /* SHIFTJIS_CP932 */
496
497 #ifdef X0212_ENABLE
498 STATIC int x0212_f = FALSE;
499 STATIC int x0212_shift PROTO((int c));
500 STATIC int x0212_unshift PROTO((int c));
501 #endif
502
503 STATIC unsigned char prefix_table[256];
504
505 STATIC void e_status PROTO((struct input_code *, int));
506 STATIC void s_status PROTO((struct input_code *, int));
507
508 #ifdef UTF8_INPUT_ENABLE
509 STATIC void w_status PROTO((struct input_code *, int));
510 STATIC void w16_status PROTO((struct input_code *, int));
511 STATIC int             utf16_mode = UTF16BE_INPUT;
512 #endif
513
514 struct input_code input_code_list[] = {
515     {"EUC-JP",    0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
516     {"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
517 #ifdef UTF8_INPUT_ENABLE
518     {"UTF-8",     0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
519     {"UTF-16",    0, 0, 0, {0, 0, 0}, w16_status, w_iconv16, 0},
520 #endif
521     {0}
522 };
523
524 STATIC int              mimeout_mode = 0;
525 STATIC int              base64_count = 0;
526
527 /* X0208 -> ASCII converter */
528
529 /* fold parameter */
530 STATIC int             f_line = 0;    /* chars in line */
531 STATIC int             f_prev = 0;
532 STATIC int             fold_preserve_f = FALSE; /* preserve new lines */
533 STATIC int             fold_f  = FALSE;
534 STATIC int             fold_len  = 0;
535
536 /* options */
537 STATIC unsigned char   kanji_intro = DEFAULT_J;
538 STATIC unsigned char   ascii_intro = DEFAULT_R;
539
540 /* Folding */
541
542 #define FOLD_MARGIN  10
543 #define DEFAULT_FOLD 60
544
545 STATIC int             fold_margin  = FOLD_MARGIN;
546
547 /* converters */
548
549 #ifdef DEFAULT_CODE_JIS
550 #   define  DEFAULT_CONV j_oconv
551 #endif
552 #ifdef DEFAULT_CODE_SJIS
553 #   define  DEFAULT_CONV s_oconv
554 #endif
555 #ifdef DEFAULT_CODE_EUC
556 #   define  DEFAULT_CONV e_oconv
557 #endif
558 #ifdef DEFAULT_CODE_UTF8
559 #   define  DEFAULT_CONV w_oconv
560 #endif
561
562 /* process default */
563 STATIC void (*output_conv)PROTO((int c2,int c1)) = DEFAULT_CONV;   
564
565 STATIC void (*oconv)PROTO((int c2,int c1)) = no_connection; 
566 /* s_iconv or oconv */
567 STATIC int (*iconv)PROTO((int c2,int c1,int c0)) = no_connection2;   
568
569 STATIC void (*o_zconv)PROTO((int c2,int c1)) = no_connection; 
570 STATIC void (*o_fconv)PROTO((int c2,int c1)) = no_connection; 
571 STATIC void (*o_crconv)PROTO((int c2,int c1)) = no_connection; 
572 STATIC void (*o_rot_conv)PROTO((int c2,int c1)) = no_connection; 
573 STATIC void (*o_hira_conv)PROTO((int c2,int c1)) = no_connection; 
574 STATIC void (*o_base64conv)PROTO((int c2,int c1)) = no_connection;
575 STATIC void (*o_iso2022jp_check_conv)PROTO((int c2,int c1)) = no_connection;
576
577 /* STATIC redirections */
578
579 STATIC  void   (*o_putc)PROTO((int c)) = std_putc;
580
581 STATIC  int    (*i_getc)PROTO((FILE *f)) = std_getc; /* general input */
582 STATIC  int    (*i_ungetc)PROTO((int c,FILE *f)) =std_ungetc;
583
584 STATIC  int    (*i_bgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
585 STATIC  int    (*i_bungetc)PROTO((int c ,FILE *f)) = std_ungetc;
586
587 STATIC  void   (*o_mputc)PROTO((int c)) = std_putc ; /* output of mputc */
588
589 STATIC  int    (*i_mgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
590 STATIC  int    (*i_mungetc)PROTO((int c ,FILE *f)) = std_ungetc;
591
592 /* for strict mime */
593 STATIC  int    (*i_mgetc_buf)PROTO((FILE *)) = std_getc; /* input of mgetc_buf */
594 STATIC  int    (*i_mungetc_buf)PROTO((int c,FILE *f)) = std_ungetc;
595
596 /* Global states */
597 STATIC int output_mode = ASCII,    /* output kanji mode */
598            input_mode =  ASCII,    /* input kanji mode */
599            shift_mode =  FALSE;    /* TRUE shift out, or X0201  */
600 STATIC int mime_decode_mode =   FALSE;    /* MIME mode B base64, Q hex */
601
602 /* X0201 / X0208 conversion tables */
603
604 /* X0201 kana conversion table */
605 /* 90-9F A0-DF */
606 STATIC const
607 unsigned char cv[]= {
608     0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
609     0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
610     0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
611     0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
612     0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
613     0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
614     0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
615     0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
616     0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
617     0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
618     0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
619     0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
620     0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
621     0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
622     0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
623     0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
624     0x00,0x00};
625
626
627 /* X0201 kana conversion table for daguten */
628 /* 90-9F A0-DF */
629 STATIC const
630 unsigned char dv[]= { 
631     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
632     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
633     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
634     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
635     0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
636     0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
637     0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
638     0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
639     0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
640     0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
641     0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
642     0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
643     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
644     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
645     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
646     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
647     0x00,0x00};
648
649 /* X0201 kana conversion table for han-daguten */
650 /* 90-9F A0-DF */
651 STATIC const
652 unsigned char ev[]= { 
653     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
654     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
655     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
656     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
657     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
658     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
659     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
660     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
661     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
662     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
663     0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
664     0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
665     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
666     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
667     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
668     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
669     0x00,0x00};
670
671
672 /* X0208 kigou conversion table */
673 /* 0x8140 - 0x819e */
674 STATIC const
675 unsigned char fv[] = {
676
677     0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
678     0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
679     0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
680     0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
681     0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
682     0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
683     0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
684     0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
685     0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
686     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
687     0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
688     0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
689 } ;
690
691
692 #define    CRLF      1
693
694 STATIC int             file_out = FALSE;
695 #ifdef OVERWRITE
696 STATIC int             overwrite = FALSE;
697 #endif
698
699 STATIC int             crmode_f = 0;   /* CR, NL, CRLF */
700 #ifdef EASYWIN /*Easy Win */
701 STATIC int             end_check;
702 #endif /*Easy Win */
703
704 #define STD_GC_BUFSIZE (256)
705 int std_gc_buf[STD_GC_BUFSIZE];
706 int std_gc_ndx;
707
708 #ifdef WIN32DLL
709 #include "nkf32dll.c"
710 #elif defined(PERL_XS)
711 #else /* WIN32DLL */
712 int
713 main(argc, argv)
714     int             argc;
715     char          **argv;
716 {
717     FILE  *fin;
718     unsigned char  *cp;
719
720     char *outfname = NULL;
721     char *origfname;
722
723 #ifdef EASYWIN /*Easy Win */
724     _BufferSize.y = 400;/*Set Scroll Buffer Size*/
725 #endif
726
727     for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
728         cp = (unsigned char *)*argv;
729         options(cp);
730 #ifdef EXEC_IO
731         if (exec_f){
732             int fds[2], pid;
733             if (pipe(fds) < 0 || (pid = fork()) < 0){
734                 abort();
735             }
736             if (pid == 0){
737                 if (exec_f > 0){
738                     close(fds[0]);
739                     dup2(fds[1], 1);
740                 }else{
741                     close(fds[1]);
742                     dup2(fds[0], 0);
743                 }
744                 execvp(argv[1], &argv[1]);
745             }
746             if (exec_f > 0){
747                 close(fds[1]);
748                 dup2(fds[0], 0);
749             }else{
750                 close(fds[0]);
751                 dup2(fds[1], 1);
752             }
753             argc = 0;
754             break;
755         }
756 #endif
757     }
758     if(x0201_f == WISH_TRUE)
759          x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
760
761     if (binmode_f == TRUE)
762 #ifdef __OS2__
763     if (freopen("","wb",stdout) == NULL) 
764         return (-1);
765 #else
766     setbinmode(stdout);
767 #endif
768
769     if (unbuf_f)
770       setbuf(stdout, (char *) NULL);
771     else
772       setvbuffer(stdout, stdobuf, IOBUF_SIZE);
773
774     if (argc == 0) {
775       if (binmode_f == TRUE)
776 #ifdef __OS2__
777       if (freopen("","rb",stdin) == NULL) return (-1);
778 #else
779       setbinmode(stdin);
780 #endif
781       setvbuffer(stdin, stdibuf, IOBUF_SIZE);
782       if (nop_f)
783           noconvert(stdin);
784       else {
785           kanji_convert(stdin);
786           if (guess_f) print_guessed_code(NULL);
787       }
788     } else {
789       int nfiles = argc;
790       while (argc--) {
791             is_inputcode_mixed = FALSE;
792             is_inputcode_set   = FALSE;
793             input_codename = "";
794 #ifdef CHECK_OPTION
795             iconv_for_check = 0;
796 #endif
797           if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
798               perror(*--argv);
799               return(-1);
800           } else {
801 #ifdef OVERWRITE
802               int fd = 0;
803               int fd_backup = 0;
804 #endif
805
806 /* reopen file for stdout */
807               if (file_out == TRUE) {
808 #ifdef OVERWRITE
809                   if (overwrite){
810                       outfname = malloc(strlen(origfname)
811                                         + strlen(".nkftmpXXXXXX")
812                                         + 1);
813                       if (!outfname){
814                           perror(origfname);
815                           return -1;
816                       }
817                       strcpy(outfname, origfname);
818 #ifdef MSDOS
819                       {
820                           int i;
821                           for (i = strlen(outfname); i; --i){
822                               if (outfname[i - 1] == '/'
823                                   || outfname[i - 1] == '\\'){
824                                   break;
825                               }
826                           }
827                           outfname[i] = '\0';
828                       }
829                       strcat(outfname, "ntXXXXXX");
830                       mktemp(outfname);
831                       fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC,
832                                 S_IREAD | S_IWRITE);
833 #else
834                       strcat(outfname, ".nkftmpXXXXXX");
835                       fd = mkstemp(outfname);
836 #endif
837                       if (fd < 0
838                           || (fd_backup = dup(fileno(stdout))) < 0
839                           || dup2(fd, fileno(stdout)) < 0
840                           ){
841                           perror(origfname);
842                           return -1;
843                       }
844                   }else
845 #endif
846                   if(argc == 1 ) {
847                       outfname = *argv++;
848                       argc--;
849                   } else {
850                       outfname = "nkf.out";
851                   }
852
853                   if(freopen(outfname, "w", stdout) == NULL) {
854                       perror (outfname);
855                       return (-1);
856                   }
857                   if (binmode_f == TRUE) {
858 #ifdef __OS2__
859                       if (freopen("","wb",stdout) == NULL) 
860                            return (-1);
861 #else
862                       setbinmode(stdout);
863 #endif
864                   }
865               }
866               if (binmode_f == TRUE)
867 #ifdef __OS2__
868                  if (freopen("","rb",fin) == NULL) 
869                     return (-1);
870 #else
871                  setbinmode(fin);
872 #endif 
873               setvbuffer(fin, stdibuf, IOBUF_SIZE);
874               if (nop_f)
875                   noconvert(fin);
876               else {
877                   char *filename = NULL;
878                   kanji_convert(fin);
879                   if (nfiles > 1) filename = origfname;
880                   if (guess_f) print_guessed_code(filename);
881               }
882               fclose(fin);
883 #ifdef OVERWRITE
884               if (overwrite) {
885                   struct stat     sb;
886 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__)
887                   time_t tb[2];
888 #else
889                   struct utimbuf  tb;
890 #endif
891
892                   fflush(stdout);
893                   close(fd);
894                   if (dup2(fd_backup, fileno(stdout)) < 0){
895                       perror("dup2");
896                   }
897                   if (stat(origfname, &sb)) {
898                       fprintf(stderr, "Can't stat %s\n", origfname);
899                   }
900                   /* \e$B%Q!<%_%C%7%g%s$rI|85\e(B */
901                   if (chmod(outfname, sb.st_mode)) {
902                       fprintf(stderr, "Can't set permission %s\n", outfname);
903                   }
904
905                   /* \e$B%?%$%`%9%?%s%W$rI|85\e(B */
906 #if defined(MSDOS) && !defined(__MINGW32__) && !defined(__WIN32__)
907                   tb[0] = tb[1] = sb.st_mtime;
908                   if (utime(outfname, tb)) {
909                       fprintf(stderr, "Can't set timestamp %s\n", outfname);
910                   }
911 #else
912                   tb.actime  = sb.st_atime;
913                   tb.modtime = sb.st_mtime;
914                   if (utime(outfname, &tb)) {
915                       fprintf(stderr, "Can't set timestamp %s\n", outfname);
916                   }
917 #endif
918 #ifdef MSDOS
919                   if (unlink(origfname)){
920                       perror(origfname);
921                   }
922 #endif
923                   if (rename(outfname, origfname)) {
924                       perror(origfname);
925                       fprintf(stderr, "Can't rename %s to %s\n",
926                               outfname, origfname);
927                   }
928                   free(outfname);
929               }
930 #endif
931           }
932       }
933     }
934 #ifdef EASYWIN /*Easy Win */
935     if (file_out == FALSE) 
936         scanf("%d",&end_check);
937     else 
938         fclose(stdout);
939 #else /* for Other OS */
940     if (file_out == TRUE) 
941         fclose(stdout);
942 #endif /*Easy Win */
943     return (0);
944 }
945 #endif /* WIN32DLL */
946
947 STATIC const
948 struct {
949     const char *name;
950     const char *alias;
951 } long_option[] = {
952     {"ic=", ""},
953     {"oc=", ""},
954     {"base64","jMB"},
955     {"euc","e"},
956     {"euc-input","E"},
957     {"fj","jm"},
958     {"help","v"},
959     {"jis","j"},
960     {"jis-input","J"},
961     {"mac","sLm"},
962     {"mime","jM"},
963     {"mime-input","m"},
964     {"msdos","sLw"},
965     {"sjis","s"},
966     {"sjis-input","S"},
967     {"unix","eLu"},
968     {"version","V"},
969     {"windows","sLw"},
970     {"hiragana","h1"},
971     {"katakana","h2"},
972     {"katakana-hiragana","h3"},
973     {"guess", "g"},
974     {"cp932", ""},
975     {"no-cp932", ""},
976 #ifdef X0212_ENABLE
977     {"x0212", ""},
978 #endif
979 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
980     {"internal-unicode", ""},
981 #endif
982 #ifdef UTF8_OUTPUT_ENABLE
983     {"utf8", "w"},
984     {"utf16", "w16"},
985     {"ms-ucs-map", ""},
986     {"fb-skip", ""},
987     {"fb-html", ""},
988     {"fb-xml", ""},
989     {"fb-perl", ""},
990     {"fb-java", ""},
991     {"fb-subchar", ""},
992     {"fb-subchar=", ""},
993 #endif
994 #ifdef UTF8_INPUT_ENABLE
995     {"utf8-input", "W"},
996     {"utf16-input", "W16"},
997 #endif
998 #ifdef UNICODE_NORMALIZATION
999     {"utf8mac-input", ""},
1000 #endif
1001 #ifdef OVERWRITE
1002     {"overwrite", ""},
1003 #endif
1004 #ifdef INPUT_OPTION
1005     {"cap-input", ""},
1006     {"url-input", ""},
1007 #endif
1008 #ifdef NUMCHAR_OPTION
1009     {"numchar-input", ""},
1010 #endif
1011 #ifdef CHECK_OPTION
1012     {"no-output", ""},
1013     {"debug", ""},
1014 #endif
1015 #ifdef SHIFTJIS_CP932
1016     {"cp932inv", ""},
1017 #endif
1018 #ifdef EXEC_IO
1019     {"exec-in", ""},
1020     {"exec-out", ""},
1021 #endif
1022     {"prefix=", ""},
1023 };
1024
1025 STATIC int option_mode = 0;
1026
1027 void
1028 options(cp) 
1029      unsigned char *cp;
1030 {
1031     int i, j;
1032     unsigned char *p = NULL;
1033     unsigned char *cp_back = NULL;
1034     unsigned char codeset[32];
1035
1036     if (option_mode==1)
1037         return;
1038     while(*cp && *cp++!='-');
1039     while (*cp || cp_back) {
1040         if(!*cp){
1041             cp = cp_back;
1042             cp_back = NULL;
1043             continue;
1044         }
1045         p = 0;
1046         switch (*cp++) {
1047         case '-':  /* literal options */
1048             if (!*cp || *cp == SPACE) {        /* ignore the rest of arguments */
1049                 option_mode = 1;
1050                 return;
1051             }
1052             for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
1053                 p = (unsigned char *)long_option[i].name;
1054                 for (j=0;*p && *p != '=' && *p == cp[j];p++, j++);
1055                 if (*p == cp[j] || cp[j] == ' '){
1056                     p = &cp[j] + 1;
1057                     break;
1058                 }
1059                 p = 0;
1060             }
1061             if (p == 0) return;
1062             while(*cp && *cp != SPACE && cp++);
1063             if (long_option[i].alias[0]){
1064                 cp_back = cp;
1065                 cp = (unsigned char *)long_option[i].alias;
1066             }else{
1067                 if (strcmp(long_option[i].name, "ic=") == 0){
1068                     for (i=0; i < 16 && SPACE < p[i] && p[i] < DEL; i++){
1069                         codeset[i] = nkf_toupper(p[i]);
1070                     }
1071                     codeset[i] = 0;
1072                     if(strcmp(codeset, "ISO-2022-JP") == 0){
1073                         input_f = JIS_INPUT;
1074                     }else if(strcmp(codeset, "SHIFT_JIS") == 0){
1075                         input_f = SJIS_INPUT;
1076                         if (x0201_f==NO_X0201) x0201_f=TRUE;
1077                     }else if(strcmp(codeset, "CP932") == 0){
1078                         input_f = SJIS_INPUT;
1079                         x0201_f = FALSE;
1080 #ifdef SHIFTJIS_CP932
1081                     cp51932_f = TRUE;
1082                     cp932inv_f = TRUE;
1083 #endif
1084 #ifdef UTF8_OUTPUT_ENABLE
1085                     ms_ucs_map_f = TRUE;
1086 #endif
1087                     }else if(strcmp(codeset, "EUCJP") == 0 ||
1088                              strcmp(codeset, "EUC-JP") == 0 ||
1089                              strcmp(codeset, "CP51932") == 0){
1090                         input_f = JIS_INPUT;
1091                         x0201_f = FALSE;
1092 #ifdef SHIFTJIS_CP932
1093                     cp51932_f = TRUE;
1094                     cp932inv_f = TRUE;
1095 #endif
1096 #ifdef UTF8_OUTPUT_ENABLE
1097                     ms_ucs_map_f = TRUE;
1098 #endif
1099                     }else if(strcmp(codeset, "EUC-JP-MS") == 0 ||
1100                              strcmp(codeset, "EUCJP-MS") == 0){
1101                         input_f = JIS_INPUT;
1102                         x0201_f = FALSE;
1103 #ifdef SHIFTJIS_CP932
1104                     cp51932_f = FALSE;
1105                     cp932inv_f = TRUE;
1106 #endif
1107 #ifdef UTF8_OUTPUT_ENABLE
1108                     ms_ucs_map_f = TRUE;
1109 #endif
1110 #ifdef UTF8_INPUT_ENABLE
1111                     }else if(strcmp(codeset, "UTF-8") == 0 ||
1112                              strcmp(codeset, "UTF-8N") == 0 ||
1113                              strcmp(codeset, "UTF-8-BOM") == 0){
1114                         input_f = UTF8_INPUT;
1115 #ifdef UNICODE_NORMALIZATION
1116                     }else if(strcmp(codeset, "UTF8-MAC") == 0){
1117                         input_f = UTF8_INPUT;
1118                         nfc_f = TRUE;
1119 #endif
1120                     }else if(strcmp(codeset, "UTF-16") == 0){
1121                         input_f = UTF16BE_INPUT;
1122                         utf16_mode = UTF16BE_INPUT;
1123                     }else if(strcmp(codeset, "UTF-16BE") == 0 ||
1124                              strcmp(codeset, "UTF-16BE-BOM") == 0){
1125                         input_f = UTF16BE_INPUT;
1126                         utf16_mode = UTF16BE_INPUT;
1127                     }else if(strcmp(codeset, "UTF-16LE") == 0 ||
1128                              strcmp(codeset, "UTF-16LE-BOM") == 0){
1129                         input_f = UTF16LE_INPUT;
1130                         utf16_mode = UTF16LE_INPUT;
1131 #endif
1132                     }
1133                     continue;
1134                 }
1135                 if (strcmp(long_option[i].name, "oc=") == 0){
1136                     for (i=0; i < 16 && SPACE < p[i] && p[i] < DEL; i++){
1137                         codeset[i] = nkf_toupper(p[i]);
1138                     }
1139                     codeset[i] = 0;
1140                     if(strcmp(codeset, "ISO-2022-JP") == 0){
1141                         output_conv = j_oconv;
1142                     }else if(strcmp(codeset, "SHIFT_JIS") == 0){
1143                         output_conv = s_oconv;
1144                     }else if(strcmp(codeset, "CP932") == 0){
1145                         output_conv = s_oconv;
1146                         x0201_f = FALSE;
1147 #ifdef SHIFTJIS_CP932
1148                     cp51932_f = TRUE;
1149                     cp932inv_f = TRUE;
1150 #endif
1151 #ifdef UTF8_OUTPUT_ENABLE
1152                     ms_ucs_map_f = TRUE;
1153 #endif
1154                     }else if(strcmp(codeset, "EUCJP") == 0 ||
1155                              strcmp(codeset, "EUC-JP") == 0 ||
1156                              strcmp(codeset, "CP51932") == 0){
1157                         output_conv = e_oconv;
1158                         x0201_f = FALSE;
1159 #ifdef SHIFTJIS_CP932
1160                     cp51932_f = TRUE;
1161                     cp932inv_f = TRUE;
1162 #endif
1163 #ifdef UTF8_OUTPUT_ENABLE
1164                     ms_ucs_map_f = TRUE;
1165 #endif
1166                     }else if(strcmp(codeset, "EUC-JP-MS") == 0 ||
1167                              strcmp(codeset, "EUCJP-MS") == 0){
1168                         output_conv = e_oconv;
1169                         x0201_f = FALSE;
1170                         x0212_f = TRUE;
1171 #ifdef SHIFTJIS_CP932
1172                     cp51932_f = FALSE;
1173 #endif
1174 #ifdef UTF8_OUTPUT_ENABLE
1175                     ms_ucs_map_f = TRUE;
1176 #endif
1177 #ifdef UTF8_OUTPUT_ENABLE
1178                     }else if(strcmp(codeset, "UTF-8") == 0){
1179                         output_conv = w_oconv;
1180                     }else if(strcmp(codeset, "UTF-8N") == 0){
1181                         output_conv = w_oconv;
1182                         unicode_bom_f=1;
1183                     }else if(strcmp(codeset, "UTF-8-BOM") == 0){
1184                         output_conv = w_oconv;
1185                         unicode_bom_f=2;
1186                     }else if(strcmp(codeset, "UTF-16") == 0){
1187                         output_conv = w_oconv16; 
1188                     }else if(strcmp(codeset, "UTF-16BE") == 0){
1189                         output_conv = w_oconv16; 
1190                         unicode_bom_f=1;
1191                     }else if(strcmp(codeset, "UTF-16BE-BOM") == 0){
1192                         output_conv = w_oconv16; 
1193                         unicode_bom_f=2;
1194                     }else if(strcmp(codeset, "UTF-16LE") == 0){
1195                         output_conv = w_oconv16; 
1196                         w_oconv16_LE = 1;
1197                         unicode_bom_f=1;
1198                     }else if(strcmp(codeset, "UTF-16LE-BOM") == 0){
1199                         output_conv = w_oconv16; 
1200                         w_oconv16_LE = 1;
1201                         unicode_bom_f=2;
1202 #endif
1203                     }
1204                     continue;
1205                 }
1206 #ifdef OVERWRITE
1207                 if (strcmp(long_option[i].name, "overwrite") == 0){
1208                     file_out = TRUE;
1209                     overwrite = TRUE;
1210                     continue;
1211                 }
1212 #endif
1213 #ifdef INPUT_OPTION
1214                 if (strcmp(long_option[i].name, "cap-input") == 0){
1215                     cap_f = TRUE;
1216                     continue;
1217                 }
1218                 if (strcmp(long_option[i].name, "url-input") == 0){
1219                     url_f = TRUE;
1220                     continue;
1221                 }
1222 #endif
1223 #ifdef NUMCHAR_OPTION
1224                 if (strcmp(long_option[i].name, "numchar-input") == 0){
1225                     numchar_f = TRUE;
1226                     continue;
1227                 }
1228 #endif
1229 #ifdef CHECK_OPTION
1230                 if (strcmp(long_option[i].name, "no-output") == 0){
1231                     noout_f = TRUE;
1232                     continue;
1233                 }
1234                 if (strcmp(long_option[i].name, "debug") == 0){
1235                     debug_f = TRUE;
1236                     continue;
1237                 }
1238 #endif
1239                 if (strcmp(long_option[i].name, "cp932") == 0){
1240 #ifdef SHIFTJIS_CP932
1241                     cp51932_f = TRUE;
1242                     cp932inv_f = TRUE;
1243 #endif
1244 #ifdef UTF8_OUTPUT_ENABLE
1245                     ms_ucs_map_f = TRUE;
1246 #endif
1247                     continue;
1248                 }
1249                 if (strcmp(long_option[i].name, "no-cp932") == 0){
1250 #ifdef SHIFTJIS_CP932
1251                     cp51932_f = FALSE;
1252                     cp932inv_f = FALSE;
1253 #endif
1254 #ifdef UTF8_OUTPUT_ENABLE
1255                     ms_ucs_map_f = FALSE;
1256 #endif
1257                     continue;
1258                 }
1259 #ifdef SHIFTJIS_CP932
1260                 if (strcmp(long_option[i].name, "cp932inv") == 0){
1261                     cp932inv_f = TRUE;
1262                     continue;
1263                 }
1264 #endif
1265
1266 #ifdef X0212_ENABLE
1267                 if (strcmp(long_option[i].name, "x0212") == 0){
1268                     x0212_f = TRUE;
1269                     continue;
1270                 }
1271 #endif
1272
1273 #ifdef EXEC_IO
1274                   if (strcmp(long_option[i].name, "exec-in") == 0){
1275                       exec_f = 1;
1276                       return;
1277                   }
1278                   if (strcmp(long_option[i].name, "exec-out") == 0){
1279                       exec_f = -1;
1280                       return;
1281                   }
1282 #endif
1283 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
1284                 if (strcmp(long_option[i].name, "internal-unicode") == 0){
1285                     internal_unicode_f = TRUE;
1286                     continue;
1287                 }
1288                 if (strcmp(long_option[i].name, "fb-skip") == 0){
1289                     encode_fallback = NULL;
1290                     continue;
1291                 }
1292                 if (strcmp(long_option[i].name, "fb-html") == 0){
1293                     encode_fallback = encode_fallback_html;
1294                     continue;
1295                 }
1296                 if (strcmp(long_option[i].name, "fb-xml" ) == 0){
1297                     encode_fallback = encode_fallback_xml;
1298                     continue;
1299                 }
1300                 if (strcmp(long_option[i].name, "fb-java") == 0){
1301                     encode_fallback = encode_fallback_java;
1302                     continue;
1303                 }
1304                 if (strcmp(long_option[i].name, "fb-perl") == 0){
1305                     encode_fallback = encode_fallback_perl;
1306                     continue;
1307                 }
1308                 if (strcmp(long_option[i].name, "fb-subchar") == 0){
1309                     encode_fallback = encode_fallback_subchar;
1310                     continue;
1311                 }
1312                 if (strcmp(long_option[i].name, "fb-subchar=") == 0){
1313                     encode_fallback = encode_fallback_subchar;
1314                     unicode_subchar = 0;
1315                     if (p[0] != '0'){
1316                         /* decimal number */
1317                         for (i = 0; i < 7 && nkf_isdigit(p[i]); i++){
1318                             unicode_subchar *= 10;
1319                             unicode_subchar += hex2bin(p[i]);
1320                         }
1321                     }else if(p[1] == 'x' || p[1] == 'X'){
1322                         /* hexadecimal number */
1323                         for (i = 2; i < 8 && nkf_isxdigit(p[i]); i++){
1324                             unicode_subchar <<= 4;
1325                             unicode_subchar |= hex2bin(p[i]);
1326                         }
1327                     }else{
1328                         /* octal number */
1329                         for (i = 1; i < 8 && nkf_isoctal(p[i]); i++){
1330                             unicode_subchar *= 8;
1331                             unicode_subchar += hex2bin(p[i]);
1332                         }
1333                     }
1334                     w16e_conv(unicode_subchar, &i, &j);
1335                     unicode_subchar = i<<8 | j;
1336                     continue;
1337                 }
1338 #endif
1339 #ifdef UTF8_OUTPUT_ENABLE
1340                 if (strcmp(long_option[i].name, "ms-ucs-map") == 0){
1341                     ms_ucs_map_f = TRUE;
1342                     continue;
1343                 }
1344 #endif
1345 #ifdef UNICODE_NORMALIZATION
1346                 if (strcmp(long_option[i].name, "utf8mac-input") == 0){
1347                     input_f = UTF8_INPUT;
1348                     nfc_f = TRUE;
1349                     continue;
1350                 }
1351 #endif
1352                 if (strcmp(long_option[i].name, "prefix=") == 0){
1353                     if (' ' < p[0] && p[0] < 128){
1354                         for (i = 1; ' ' < p[i] && p[i] < 128; i++){
1355                             prefix_table[p[i]] = p[0];
1356                         }
1357                     }
1358                     continue;
1359                 }
1360             }
1361             continue;
1362         case 'b':           /* buffered mode */
1363             unbuf_f = FALSE;
1364             continue;
1365         case 'u':           /* non bufferd mode */
1366             unbuf_f = TRUE;
1367             continue;
1368         case 't':           /* transparent mode */
1369             nop_f = TRUE;
1370             continue;
1371         case 'j':           /* JIS output */
1372         case 'n':
1373             output_conv = j_oconv;
1374             continue;
1375         case 'e':           /* AT&T EUC output */
1376             output_conv = e_oconv;
1377             continue;
1378         case 's':           /* SJIS output */
1379             output_conv = s_oconv;
1380             continue;
1381         case 'l':           /* ISO8859 Latin-1 support, no conversion */
1382             iso8859_f = TRUE;  /* Only compatible with ISO-2022-JP */
1383             input_f = LATIN1_INPUT;
1384             continue;
1385         case 'i':           /* Kanji IN ESC-$-@/B */
1386             if (*cp=='@'||*cp=='B') 
1387                 kanji_intro = *cp++;
1388             continue;
1389         case 'o':           /* ASCII IN ESC-(-J/B */
1390             if (*cp=='J'||*cp=='B'||*cp=='H') 
1391                 ascii_intro = *cp++;
1392             continue;
1393         case 'h':
1394             /*  
1395                 bit:1   katakana->hiragana
1396                 bit:2   hiragana->katakana
1397             */
1398             if ('9'>= *cp && *cp>='0') 
1399                 hira_f |= (*cp++ -'0');
1400             else 
1401                 hira_f |= 1;
1402             continue;
1403         case 'r':
1404             rot_f = TRUE;
1405             continue;
1406 #if defined(MSDOS) || defined(__OS2__) 
1407         case 'T':
1408             binmode_f = FALSE;
1409             continue;
1410 #endif
1411 #ifndef PERL_XS
1412         case 'V':
1413             version();
1414             exit(1);
1415             break;
1416         case 'v':
1417             usage();
1418             exit(1);
1419             break;
1420 #endif
1421 #ifdef UTF8_OUTPUT_ENABLE
1422         case 'w':           /* UTF-8 output */
1423             if ('1'== cp[0] && '6'==cp[1]) {
1424                 output_conv = w_oconv16; cp+=2;
1425                 if (cp[0]=='L') {
1426                     unicode_bom_f=2; cp++;
1427                     w_oconv16_LE = 1;
1428                     if (cp[0] == '0'){
1429                         unicode_bom_f=1; cp++;
1430                     }
1431                 } else if (cp[0] == 'B') {
1432                     unicode_bom_f=2; cp++;
1433                     if (cp[0] == '0'){
1434                         unicode_bom_f=1; cp++;
1435                     }
1436                 } 
1437             } else if (cp[0] == '8') {
1438                 output_conv = w_oconv; cp++;
1439                 unicode_bom_f=2;
1440                 if (cp[0] == '0'){
1441                     unicode_bom_f=1; cp++;
1442                 }
1443             } else
1444                 output_conv = w_oconv;
1445             continue;
1446 #endif
1447 #ifdef UTF8_INPUT_ENABLE
1448         case 'W':           /* UTF-8 input */
1449             if ('1'== cp[0] && '6'==cp[1]) {
1450                 input_f = UTF16BE_INPUT;
1451                 utf16_mode = UTF16BE_INPUT;
1452                 cp += 2;
1453                 if (cp[0]=='L') {
1454                     cp++;
1455                     input_f = UTF16LE_INPUT;
1456                     utf16_mode = UTF16LE_INPUT;
1457                 } else if (cp[0] == 'B') {
1458                     cp++;
1459                     input_f = UTF16BE_INPUT;
1460                     utf16_mode = UTF16BE_INPUT;
1461                 }
1462             } else if (cp[0] == '8') {
1463                 cp++;
1464                 input_f = UTF8_INPUT;
1465             } else
1466                 input_f = UTF8_INPUT;
1467             continue;
1468 #endif
1469         /* Input code assumption */
1470         case 'J':   /* JIS input */
1471         case 'E':   /* AT&T EUC input */
1472             input_f = JIS_INPUT;
1473             continue;
1474         case 'S':   /* MS Kanji input */
1475             input_f = SJIS_INPUT;
1476             if (x0201_f==NO_X0201) x0201_f=TRUE;
1477             continue;
1478         case 'Z':   /* Convert X0208 alphabet to asii */
1479             /*  bit:0   Convert X0208
1480                 bit:1   Convert Kankaku to one space
1481                 bit:2   Convert Kankaku to two spaces
1482                 bit:3   Convert HTML Entity
1483             */
1484             if ('9'>= *cp && *cp>='0') 
1485                 alpha_f |= 1<<(*cp++ -'0');
1486             else 
1487                 alpha_f |= TRUE;
1488             continue;
1489         case 'x':   /* Convert X0201 kana to X0208 or X0201 Conversion */
1490             x0201_f = FALSE;    /* No X0201->X0208 conversion */
1491             /* accept  X0201
1492                     ESC-(-I     in JIS, EUC, MS Kanji
1493                     SI/SO       in JIS, EUC, MS Kanji
1494                     SSO         in EUC, JIS, not in MS Kanji
1495                     MS Kanji (0xa0-0xdf) 
1496                output  X0201
1497                     ESC-(-I     in JIS (0x20-0x5f)
1498                     SSO         in EUC (0xa0-0xdf)
1499                     0xa0-0xd    in MS Kanji (0xa0-0xdf) 
1500             */
1501             continue;
1502         case 'X':   /* Assume X0201 kana */
1503             /* Default value is NO_X0201 for EUC/MS-Kanji mix */
1504             x0201_f = TRUE;
1505             continue;
1506         case 'F':   /* prserve new lines */
1507             fold_preserve_f = TRUE;
1508         case 'f':   /* folding -f60 or -f */
1509             fold_f = TRUE;
1510             fold_len = 0;
1511             while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1512                 fold_len *= 10;
1513                 fold_len += *cp++ - '0';
1514             }
1515             if (!(0<fold_len && fold_len<BUFSIZ)) 
1516                 fold_len = DEFAULT_FOLD;
1517             if (*cp=='-') {
1518                 fold_margin = 0;
1519                 cp++;
1520                 while('0'<= *cp && *cp <='9') { /* we don't use atoi here */
1521                     fold_margin *= 10;
1522                     fold_margin += *cp++ - '0';
1523                 }
1524             }
1525             continue;
1526         case 'm':   /* MIME support */
1527             /* mime_decode_f = TRUE; */ /* this has too large side effects... */
1528             if (*cp=='B'||*cp=='Q') {
1529                 mime_decode_mode = *cp++;
1530                 mimebuf_f = FIXED_MIME;
1531             } else if (*cp=='N') {
1532                 mime_f = TRUE; cp++;
1533             } else if (*cp=='S') {
1534                 mime_f = STRICT_MIME; cp++;
1535             } else if (*cp=='0') {
1536                 mime_decode_f = FALSE;
1537                 mime_f = FALSE; cp++;
1538             }
1539             continue;
1540         case 'M':   /* MIME output */
1541             if (*cp=='B') {
1542                 mimeout_mode = 'B';
1543                 mimeout_f = FIXED_MIME; cp++;
1544             } else if (*cp=='Q') {
1545                 mimeout_mode = 'Q';
1546                 mimeout_f = FIXED_MIME; cp++;
1547             } else {
1548                 mimeout_f = TRUE;
1549             }
1550             continue;
1551         case 'B':   /* Broken JIS support */
1552             /*  bit:0   no ESC JIS
1553                 bit:1   allow any x on ESC-(-x or ESC-$-x
1554                 bit:2   reset to ascii on NL
1555             */
1556             if ('9'>= *cp && *cp>='0') 
1557                 broken_f |= 1<<(*cp++ -'0');
1558             else 
1559                 broken_f |= TRUE;
1560             continue;
1561 #ifndef PERL_XS
1562         case 'O':/* for Output file */
1563             file_out = TRUE;
1564             continue;
1565 #endif
1566         case 'c':/* add cr code */
1567             crmode_f = CRLF;
1568             continue;
1569         case 'd':/* delete cr code */
1570             crmode_f = NL;
1571             continue;
1572         case 'I':   /* ISO-2022-JP output */
1573             iso2022jp_f = TRUE;
1574             continue;
1575         case 'L':  /* line mode */
1576             if (*cp=='u') {         /* unix */
1577                 crmode_f = NL; cp++;
1578             } else if (*cp=='m') { /* mac */
1579                 crmode_f = CR; cp++;
1580             } else if (*cp=='w') { /* windows */
1581                 crmode_f = CRLF; cp++;
1582             } else if (*cp=='0') { /* no conversion  */
1583                 crmode_f = 0; cp++;
1584             }
1585             continue;
1586         case 'g':
1587 #ifndef PERL_XS
1588             guess_f = TRUE;
1589 #endif
1590             continue;
1591         case ' ':    
1592         /* module muliple options in a string are allowed for Perl moudle  */
1593             while(*cp && *cp++!='-');
1594             continue;
1595         default:
1596             /* bogus option but ignored */
1597             continue;
1598         }
1599     }
1600 }
1601
1602 #ifdef ANSI_C_PROTOTYPE
1603 struct input_code * find_inputcode_byfunc(int (*iconv_func)(int c2,int c1,int c0))
1604 #else
1605 struct input_code * find_inputcode_byfunc(iconv_func)
1606      int (*iconv_func)();
1607 #endif
1608 {
1609     if (iconv_func){
1610         struct input_code *p = input_code_list;
1611         while (p->name){
1612             if (iconv_func == p->iconv_func){
1613                 return p;
1614             }
1615             p++;
1616         }
1617     }
1618     return 0;
1619 }
1620
1621 #ifdef ANSI_C_PROTOTYPE
1622 void set_iconv(int f, int (*iconv_func)(int c2,int c1,int c0))
1623 #else
1624 void set_iconv(f, iconv_func)
1625      int f;
1626      int (*iconv_func)();
1627 #endif
1628 {
1629 #ifdef INPUT_CODE_FIX
1630     if (f || !input_f)
1631 #endif
1632         if (estab_f != f){
1633             estab_f = f;
1634         }
1635
1636     if (iconv_func
1637 #ifdef INPUT_CODE_FIX
1638         && (f == -TRUE || !input_f) /* -TRUE means "FORCE" */
1639 #endif
1640         ){
1641         iconv = iconv_func;
1642     }
1643 #ifdef CHECK_OPTION
1644     if (estab_f && iconv_for_check != iconv){
1645         struct input_code *p = find_inputcode_byfunc(iconv);
1646         if (p){
1647             set_input_codename(p->name);
1648             debug(input_codename);
1649         }
1650         iconv_for_check = iconv;
1651     }
1652 #endif
1653 }
1654
1655 #define SCORE_L2       (1)                   /* \e$BBh\e(B2\e$B?e=`4A;z\e(B */
1656 #define SCORE_KANA     (SCORE_L2 << 1)       /* \e$B$$$o$f$kH>3Q%+%J\e(B */
1657 #define SCORE_DEPEND   (SCORE_KANA << 1)     /* \e$B5!<o0MB8J8;z\e(B */
1658 #ifdef SHIFTJIS_CP932
1659 #define SCORE_CP932    (SCORE_DEPEND << 1)   /* CP932 \e$B$K$h$kFI$_49$(\e(B */
1660 #define SCORE_NO_EXIST (SCORE_CP932 << 1)    /* \e$BB8:_$7$J$$J8;z\e(B */
1661 #else
1662 #define SCORE_NO_EXIST (SCORE_DEPEND << 1)   /* \e$BB8:_$7$J$$J8;z\e(B */
1663 #endif
1664 #define SCORE_iMIME    (SCORE_NO_EXIST << 1) /* MIME \e$B$K$h$k;XDj\e(B */
1665 #define SCORE_ERROR    (SCORE_iMIME << 1) /* \e$B%(%i!<\e(B */
1666
1667 #define SCORE_INIT (SCORE_iMIME)
1668
1669 const int score_table_A0[] = {
1670     0, 0, 0, 0,
1671     0, 0, 0, 0,
1672     0, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1673     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_NO_EXIST,
1674 };
1675
1676 const int score_table_F0[] = {
1677     SCORE_L2, SCORE_L2, SCORE_L2, SCORE_L2,
1678     SCORE_L2, SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST,
1679     SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND, SCORE_DEPEND,
1680     SCORE_DEPEND, SCORE_NO_EXIST, SCORE_NO_EXIST, SCORE_ERROR,
1681 };
1682
1683 void set_code_score(ptr, score)
1684      struct input_code *ptr;
1685      int score;
1686 {
1687     if (ptr){
1688         ptr->score |= score;
1689     }
1690 }
1691
1692 void clr_code_score(ptr, score)
1693      struct input_code *ptr;
1694      int score;
1695 {
1696     if (ptr){
1697         ptr->score &= ~score;
1698     }
1699 }
1700
1701 void code_score(ptr)
1702      struct input_code *ptr;
1703 {
1704     int c2 = ptr->buf[0];
1705 #ifdef UTF8_OUTPUT_ENABLE
1706     int c1 = ptr->buf[1];
1707 #endif
1708     if (c2 < 0){
1709         set_code_score(ptr, SCORE_ERROR);
1710     }else if (c2 == SSO){
1711         set_code_score(ptr, SCORE_KANA);
1712 #ifdef UTF8_OUTPUT_ENABLE
1713     }else if (!e2w_conv(c2, c1)){
1714         set_code_score(ptr, SCORE_NO_EXIST);
1715 #endif
1716     }else if ((c2 & 0x70) == 0x20){
1717         set_code_score(ptr, score_table_A0[c2 & 0x0f]);
1718     }else if ((c2 & 0x70) == 0x70){
1719         set_code_score(ptr, score_table_F0[c2 & 0x0f]);
1720     }else if ((c2 & 0x70) >= 0x50){
1721         set_code_score(ptr, SCORE_L2);
1722     }
1723 }
1724
1725 void status_disable(ptr)
1726 struct input_code *ptr;
1727 {
1728     ptr->stat = -1;
1729     ptr->buf[0] = -1;
1730     code_score(ptr);
1731     if (iconv == ptr->iconv_func) set_iconv(FALSE, 0);
1732 }
1733
1734 void status_push_ch(ptr, c)
1735      struct input_code *ptr;
1736      int c;
1737 {
1738     ptr->buf[ptr->index++] = c;
1739 }
1740
1741 void status_clear(ptr)
1742      struct input_code *ptr;
1743 {
1744     ptr->stat = 0;
1745     ptr->index = 0;
1746 }
1747
1748 void status_reset(ptr)
1749      struct input_code *ptr;
1750 {
1751     status_clear(ptr);
1752     ptr->score = SCORE_INIT;
1753 }
1754
1755 void status_reinit(ptr)
1756      struct input_code *ptr;
1757 {
1758     status_reset(ptr);
1759     ptr->_file_stat = 0;
1760 }
1761
1762 void status_check(ptr, c)
1763      struct input_code *ptr;
1764      int c;
1765 {
1766     if (c <= DEL && estab_f){
1767         status_reset(ptr);
1768     }
1769 }
1770
1771 void s_status(ptr, c)
1772      struct input_code *ptr;
1773      int c;
1774 {
1775     switch(ptr->stat){
1776       case -1:
1777           status_check(ptr, c);
1778           break;
1779       case 0:
1780           if (c <= DEL){
1781               break;
1782 #ifdef NUMCHAR_OPTION
1783           }else if ((c & CLASS_MASK) == CLASS_UTF16){
1784               break;
1785 #endif
1786           }else if (0xa1 <= c && c <= 0xdf){
1787               status_push_ch(ptr, SSO);
1788               status_push_ch(ptr, c);
1789               code_score(ptr);
1790               status_clear(ptr);
1791           }else if ((0x81 <= c && c < 0xa0) || (0xe0 <= c && c <= 0xef)){
1792               ptr->stat = 1;
1793               status_push_ch(ptr, c);
1794 #ifdef SHIFTJIS_CP932
1795           }else if (cp51932_f
1796                     && CP932_TABLE_BEGIN <= c && c <= CP932_TABLE_END){
1797               ptr->stat = 2;
1798               status_push_ch(ptr, c);
1799 #endif /* SHIFTJIS_CP932 */
1800 #ifdef X0212_ENABLE
1801           }else if (x0212_f && 0xf0 <= c && c <= 0xfc){
1802               ptr->stat = 1;
1803               status_push_ch(ptr, c);
1804 #endif /* X0212_ENABLE */
1805           }else{
1806               status_disable(ptr);
1807           }
1808           break;
1809       case 1:
1810           if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
1811               status_push_ch(ptr, c);
1812               s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]);
1813               code_score(ptr);
1814               status_clear(ptr);
1815           }else{
1816               status_disable(ptr);
1817           }
1818           break;
1819       case 2:
1820 #ifdef SHIFTJIS_CP932
1821           if ((0x40 <= c && c <= 0x7e) || (0x80 <= c && c <= 0xfc)){
1822               status_push_ch(ptr, c);
1823               if (s2e_conv(ptr->buf[0], ptr->buf[1], &ptr->buf[0], &ptr->buf[1]) == 0){
1824                   set_code_score(ptr, SCORE_CP932);
1825                   status_clear(ptr);
1826                   break;
1827               }
1828           }
1829 #endif /* SHIFTJIS_CP932 */
1830 #ifndef X0212_ENABLE
1831           status_disable(ptr);
1832           break;
1833 #endif
1834     }
1835 }
1836
1837 void e_status(ptr, c)
1838      struct input_code *ptr;
1839      int c;
1840 {
1841     switch (ptr->stat){
1842       case -1:
1843           status_check(ptr, c);
1844           break;
1845       case 0:
1846           if (c <= DEL){
1847               break;
1848 #ifdef NUMCHAR_OPTION
1849           }else if ((c & CLASS_MASK) == CLASS_UTF16){
1850               break;
1851 #endif
1852           }else if (SSO == c || (0xa1 <= c && c <= 0xfe)){
1853               ptr->stat = 1;
1854               status_push_ch(ptr, c);
1855 #ifdef X0212_ENABLE
1856           }else if (0x8f == c){
1857               ptr->stat = 2;
1858               status_push_ch(ptr, c);
1859 #endif /* X0212_ENABLE */
1860           }else{
1861               status_disable(ptr);
1862           }
1863           break;
1864       case 1:
1865           if (0xa1 <= c && c <= 0xfe){
1866               status_push_ch(ptr, c);
1867               code_score(ptr);
1868               status_clear(ptr);
1869           }else{
1870               status_disable(ptr);
1871           }
1872           break;
1873 #ifdef X0212_ENABLE
1874       case 2:
1875           if (0xa1 <= c && c <= 0xfe){
1876               ptr->stat = 1;
1877               status_push_ch(ptr, c);
1878           }else{
1879               status_disable(ptr);
1880           }
1881 #endif /* X0212_ENABLE */
1882     }
1883 }
1884
1885 #ifdef UTF8_INPUT_ENABLE
1886 void w16_status(ptr, c)
1887      struct input_code *ptr;
1888      int c;
1889 {
1890     switch (ptr->stat){
1891       case -1:
1892           break;
1893       case 0:
1894           if (ptr->_file_stat == 0){
1895               if (c == 0xfe || c == 0xff){
1896                   ptr->stat = c;
1897                   status_push_ch(ptr, c);
1898                   ptr->_file_stat = 1;
1899               }else{
1900                   status_disable(ptr);
1901                   ptr->_file_stat = -1;
1902               }
1903           }else if (ptr->_file_stat > 0){
1904               ptr->stat = 1;
1905               status_push_ch(ptr, c);
1906           }else if (ptr->_file_stat < 0){
1907               status_disable(ptr);
1908           }
1909           break;
1910
1911       case 1:
1912           if (c == EOF){
1913               status_disable(ptr);
1914               ptr->_file_stat = -1;
1915           }else{
1916               status_push_ch(ptr, c);
1917               status_clear(ptr);
1918           }
1919           break;
1920
1921       case 0xfe:
1922       case 0xff:
1923           if (ptr->stat != c && (c == 0xfe || c == 0xff)){
1924               status_push_ch(ptr, c);
1925               status_clear(ptr);
1926           }else{
1927               status_disable(ptr);
1928               ptr->_file_stat = -1;
1929           }
1930           break;
1931     }
1932 }
1933
1934 void w_status(ptr, c)
1935      struct input_code *ptr;
1936      int c;
1937 {
1938     switch (ptr->stat){
1939       case -1:
1940           status_check(ptr, c);
1941           break;
1942       case 0:
1943           if (c <= DEL){
1944               break;
1945 #ifdef NUMCHAR_OPTION
1946           }else if ((c & CLASS_MASK) == CLASS_UTF16){
1947               break;
1948 #endif
1949           }else if (0xc0 <= c && c <= 0xdf){
1950               ptr->stat = 1;
1951               status_push_ch(ptr, c);
1952           }else if (0xe0 <= c && c <= 0xef){
1953               ptr->stat = 2;
1954               status_push_ch(ptr, c);
1955           }else{
1956               status_disable(ptr);
1957           }
1958           break;
1959       case 1:
1960       case 2:
1961           if (0x80 <= c && c <= 0xbf){
1962               status_push_ch(ptr, c);
1963               if (ptr->index > ptr->stat){
1964                   int bom = (ptr->buf[0] == 0xef && ptr->buf[1] == 0xbb
1965                              && ptr->buf[2] == 0xbf);
1966                   w2e_conv(ptr->buf[0], ptr->buf[1], ptr->buf[2],
1967                            &ptr->buf[0], &ptr->buf[1]);
1968                   if (!bom){
1969                       code_score(ptr);
1970                   }
1971                   status_clear(ptr);
1972               }
1973           }else{
1974               status_disable(ptr);
1975           }
1976           break;
1977     }
1978 }
1979 #endif
1980
1981 void
1982 code_status(c)
1983      int c;
1984 {
1985     int action_flag = 1;
1986     struct input_code *result = 0;
1987     struct input_code *p = input_code_list;
1988     while (p->name){
1989         (p->status_func)(p, c);
1990         if (p->stat > 0){
1991             action_flag = 0;
1992         }else if(p->stat == 0){
1993             if (result){
1994                 action_flag = 0;
1995             }else{
1996                 result = p;
1997             }
1998         }
1999         ++p;
2000     }
2001
2002     if (action_flag){
2003         if (result && !estab_f){
2004             set_iconv(TRUE, result->iconv_func);
2005         }else if (c <= DEL){
2006             struct input_code *ptr = input_code_list;
2007             while (ptr->name){
2008                 status_reset(ptr);
2009                 ++ptr;
2010             }
2011         }
2012     }
2013 }
2014
2015 #ifndef WIN32DLL
2016 int 
2017 std_getc(f)
2018 FILE *f;
2019 {
2020     if (std_gc_ndx){
2021         return std_gc_buf[--std_gc_ndx];
2022     }
2023     return getc(f);
2024 }
2025 #endif /*WIN32DLL*/
2026
2027 int 
2028 std_ungetc(c,f)
2029 int c;
2030 FILE *f;
2031 {
2032     if (std_gc_ndx == STD_GC_BUFSIZE){
2033         return EOF;
2034     }
2035     std_gc_buf[std_gc_ndx++] = c;
2036     return c;
2037 }
2038
2039 #ifndef WIN32DLL
2040 void 
2041 std_putc(c)
2042 int c;
2043 {
2044     if(c!=EOF)
2045       putchar(c);
2046 }
2047 #endif /*WIN32DLL*/
2048
2049 #if !defined(PERL_XS) && !defined(WIN32DLL)
2050 int
2051 noconvert(f)
2052     FILE  *f;
2053 {
2054     int    c;
2055
2056     while ((c = (*i_getc)(f)) != EOF)
2057       (*o_putc)(c);
2058     return 1;
2059 }
2060 #endif
2061
2062 void
2063 module_connection()
2064 {
2065     oconv = output_conv; 
2066     o_putc = std_putc;
2067
2068     /* replace continucation module, from output side */
2069
2070     /* output redicrection */
2071 #ifdef CHECK_OPTION
2072     if (noout_f || guess_f){
2073         o_putc = no_putc;
2074     }
2075 #endif
2076     if (mimeout_f) {
2077         o_mputc = o_putc;
2078         o_putc = mime_putc;
2079         if (mimeout_f == TRUE) {
2080             o_base64conv = oconv; oconv = base64_conv;
2081         }
2082         /* base64_count = 0; */
2083     }
2084
2085     if (crmode_f) {
2086         o_crconv = oconv; oconv = cr_conv;
2087     }
2088     if (rot_f) {
2089         o_rot_conv = oconv; oconv = rot_conv;
2090     }
2091     if (iso2022jp_f) {
2092         o_iso2022jp_check_conv = oconv; oconv = iso2022jp_check_conv;
2093     }
2094     if (hira_f) {
2095         o_hira_conv = oconv; oconv = hira_conv;
2096     }
2097     if (fold_f) {
2098         o_fconv = oconv; oconv = fold_conv;
2099         f_line = 0;
2100     }
2101     if (alpha_f || x0201_f) {
2102         o_zconv = oconv; oconv = z_conv;
2103     }
2104
2105     i_getc = std_getc;
2106     i_ungetc = std_ungetc;
2107     /* input redicrection */
2108 #ifdef INPUT_OPTION
2109     if (cap_f){
2110         i_cgetc = i_getc; i_getc = cap_getc;
2111         i_cungetc = i_ungetc; i_ungetc= cap_ungetc;
2112     }
2113     if (url_f){
2114         i_ugetc = i_getc; i_getc = url_getc;
2115         i_uungetc = i_ungetc; i_ungetc= url_ungetc;
2116     }
2117 #endif
2118 #ifdef NUMCHAR_OPTION
2119     if (numchar_f){
2120         i_ngetc = i_getc; i_getc = numchar_getc;
2121         i_nungetc = i_ungetc; i_ungetc= numchar_ungetc;
2122     }
2123 #endif
2124 #ifdef UNICODE_NORMALIZATION
2125     if (nfc_f && input_f == UTF8_INPUT){
2126         i_nfc_getc = i_getc; i_getc = nfc_getc;
2127         i_nfc_ungetc = i_ungetc; i_ungetc= nfc_ungetc;
2128     }
2129 #endif
2130     if (mime_f && mimebuf_f==FIXED_MIME) {
2131         i_mgetc = i_getc; i_getc = mime_getc;
2132         i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
2133     }
2134     if (broken_f & 1) {
2135         i_bgetc = i_getc; i_getc = broken_getc;
2136         i_bungetc = i_ungetc; i_ungetc = broken_ungetc;
2137     }
2138     if (input_f == JIS_INPUT || input_f == LATIN1_INPUT) {
2139         set_iconv(-TRUE, e_iconv);
2140     } else if (input_f == SJIS_INPUT) {
2141         set_iconv(-TRUE, s_iconv);
2142 #ifdef UTF8_INPUT_ENABLE
2143     } else if (input_f == UTF8_INPUT) {
2144         set_iconv(-TRUE, w_iconv);
2145     } else if (input_f == UTF16BE_INPUT) {
2146         set_iconv(-TRUE, w_iconv16);
2147     } else if (input_f == UTF16LE_INPUT) {
2148         set_iconv(-TRUE, w_iconv16);
2149 #endif
2150     } else {
2151         set_iconv(FALSE, e_iconv);
2152     }
2153
2154     {
2155         struct input_code *p = input_code_list;
2156         while (p->name){
2157             status_reinit(p++);
2158         }
2159     }
2160 }
2161
2162 /*
2163    Conversion main loop. Code detection only. 
2164  */
2165
2166 int
2167 kanji_convert(f)
2168     FILE  *f;
2169 {
2170     int    c1,
2171                     c2, c3;
2172     int is_8bit = FALSE;
2173
2174     module_connection();
2175     c2 = 0;
2176
2177
2178     input_mode = ASCII;
2179     output_mode = ASCII;
2180     shift_mode = FALSE;
2181
2182 #define NEXT continue      /* no output, get next */
2183 #define SEND ;             /* output c1 and c2, get next */
2184 #define LAST break         /* end of loop, go closing  */
2185
2186     while ((c1 = (*i_getc)(f)) != EOF) {
2187         code_status(c1);
2188         if (c2) {
2189             /* second byte */
2190             if (c2 > DEL) {
2191                 /* in case of 8th bit is on */
2192                 if (!estab_f&&!mime_decode_mode) {
2193                     /* in case of not established yet */
2194                     /* It is still ambiguious */
2195                     if (h_conv(f, c2, c1)==EOF) 
2196                         LAST;
2197                     else 
2198                         c2 = 0;
2199                     NEXT;
2200                 } else
2201                     /* in case of already established */
2202                     if (c1 < AT) {
2203                         /* ignore bogus code */
2204                         c2 = 0;
2205                         NEXT;
2206                     } else
2207                         SEND;
2208             } else
2209                 /* second byte, 7 bit code */
2210                 /* it might be kanji shitfted */
2211                 if ((c1 == DEL) || (c1 <= SPACE)) {
2212                     /* ignore bogus first code */
2213                     c2 = 0;
2214                     NEXT;
2215                 } else
2216                     SEND;
2217         } else {
2218             /* first byte */
2219             if (
2220 #ifdef UTF8_INPUT_ENABLE
2221                 iconv == w_iconv16
2222 #else
2223                 0
2224 #endif
2225                 ) {
2226                 c2 = c1;
2227                 c1 = (*i_getc)(f);
2228                 SEND;
2229 #ifdef NUMCHAR_OPTION
2230             } else if ((c1 & CLASS_MASK) == CLASS_UTF16){
2231                 SEND;
2232 #endif
2233             } else if (c1 > DEL) {
2234                 /* 8 bit code */
2235                 if (!estab_f && !iso8859_f) {
2236                     /* not established yet */
2237                     if (!is_8bit) is_8bit = TRUE;
2238                     c2 = c1;
2239                     NEXT;
2240                 } else { /* estab_f==TRUE */
2241                     if (iso8859_f) {
2242                         c2 = ISO8859_1;
2243                         c1 &= 0x7f;
2244                         SEND;
2245                     } else if (SSP<=c1 && c1<0xe0 && iconv == s_iconv) {
2246                         /* SJIS X0201 Case... */
2247                         if(iso2022jp_f && x0201_f==NO_X0201) {
2248                             (*oconv)(GETA1, GETA2);
2249                             NEXT;
2250                         } else {
2251                             c2 = X0201;
2252                             c1 &= 0x7f;
2253                             SEND;
2254                         }
2255                     } else if (c1==SSO && iconv != s_iconv) {
2256                         /* EUC X0201 Case */
2257                         c1 = (*i_getc)(f);  /* skip SSO */
2258                         code_status(c1);
2259                         if (SSP<=c1 && c1<0xe0) {
2260                             if(iso2022jp_f &&  x0201_f==NO_X0201) {
2261                                 (*oconv)(GETA1, GETA2);
2262                                 NEXT;
2263                             } else {
2264                                 c2 = X0201;
2265                                 c1 &= 0x7f;
2266                                 SEND;
2267                             }
2268                         } else  { /* bogus code, skip SSO and one byte */
2269                             NEXT;
2270                         }
2271                     } else {
2272                        /* already established */
2273                        c2 = c1;
2274                        NEXT;
2275                     }
2276                 }
2277             } else if ((c1 > SPACE) && (c1 != DEL)) {
2278                 /* in case of Roman characters */
2279                 if (shift_mode) { 
2280                     /* output 1 shifted byte */
2281                     if (iso8859_f) {
2282                         c2 = ISO8859_1;
2283                         SEND;
2284                     } else if (SPACE<=c1 && c1<(0xe0&0x7f) ){
2285                       /* output 1 shifted byte */
2286                         if(iso2022jp_f && x0201_f==NO_X0201) {
2287                             (*oconv)(GETA1, GETA2);
2288                             NEXT;
2289                         } else {
2290                             c2 = X0201;
2291                             SEND;
2292                         }
2293                     } else {
2294                         /* look like bogus code */
2295                         NEXT;
2296                     }
2297                 } else if (input_mode == X0208) {
2298                     /* in case of Kanji shifted */
2299                     c2 = c1;
2300                     NEXT;
2301                 } else if (c1 == '=' && mime_f && !mime_decode_mode ) {
2302                     /* Check MIME code */
2303                     if ((c1 = (*i_getc)(f)) == EOF) {
2304                         (*oconv)(0, '=');
2305                         LAST;
2306                     } else if (c1 == '?') {
2307                         /* =? is mime conversion start sequence */
2308                         if(mime_f == STRICT_MIME) {
2309                             /* check in real detail */
2310                             if (mime_begin_strict(f) == EOF) 
2311                                 LAST;
2312                             else
2313                                 NEXT;
2314                         } else if (mime_begin(f) == EOF) 
2315                             LAST;
2316                         else
2317                             NEXT;
2318                     } else {
2319                         (*oconv)(0, '=');
2320                         (*i_ungetc)(c1,f);
2321                         NEXT;
2322                     }
2323                 } else {
2324                     /* normal ASCII code */ 
2325                     SEND;
2326                 }
2327             } else if (!is_8bit && c1 == SI) {
2328                 shift_mode = FALSE; 
2329                 NEXT;
2330             } else if (!is_8bit && c1 == SO) {
2331                 shift_mode = TRUE; 
2332                 NEXT;
2333             } else if (!is_8bit && c1 == ESC ) {
2334                 if ((c1 = (*i_getc)(f)) == EOF) {
2335                     /*  (*oconv)(0, ESC); don't send bogus code */
2336                     LAST;
2337                 } else if (c1 == '$') {
2338                     if ((c1 = (*i_getc)(f)) == EOF) {
2339                         /*
2340                         (*oconv)(0, ESC); don't send bogus code 
2341                         (*oconv)(0, '$'); */
2342                         LAST;
2343                     } else if (c1 == '@'|| c1 == 'B') {
2344                         /* This is kanji introduction */
2345                         input_mode = X0208;
2346                         shift_mode = FALSE;
2347                         set_input_codename("ISO-2022-JP");
2348 #ifdef CHECK_OPTION
2349                         debug(input_codename);
2350 #endif
2351                         NEXT;
2352                     } else if (c1 == '(') {
2353                         if ((c1 = (*i_getc)(f)) == EOF) {
2354                             /* don't send bogus code 
2355                             (*oconv)(0, ESC);
2356                             (*oconv)(0, '$');
2357                             (*oconv)(0, '(');
2358                                 */
2359                             LAST;
2360                         } else if (c1 == '@'|| c1 == 'B') {
2361                             /* This is kanji introduction */
2362                             input_mode = X0208;
2363                             shift_mode = FALSE;
2364                             NEXT;
2365 #ifdef X0212_ENABLE
2366                         } else if (c1 == 'D'){
2367                             input_mode = X0212;
2368                             shift_mode = FALSE;
2369                             NEXT;
2370 #endif /* X0212_ENABLE */
2371                         } else {
2372                             /* could be some special code */
2373                             (*oconv)(0, ESC);
2374                             (*oconv)(0, '$');
2375                             (*oconv)(0, '(');
2376                             (*oconv)(0, c1);
2377                             NEXT;
2378                         }
2379                     } else if (broken_f&0x2) {
2380                         /* accept any ESC-(-x as broken code ... */
2381                         input_mode = X0208;
2382                         shift_mode = FALSE;
2383                         NEXT;
2384                     } else {
2385                         (*oconv)(0, ESC);
2386                         (*oconv)(0, '$');
2387                         (*oconv)(0, c1);
2388                         NEXT;
2389                     }
2390                 } else if (c1 == '(') {
2391                     if ((c1 = (*i_getc)(f)) == EOF) {
2392                         /* don't send bogus code 
2393                         (*oconv)(0, ESC);
2394                         (*oconv)(0, '('); */
2395                         LAST;
2396                     } else {
2397                         if (c1 == 'I') {
2398                             /* This is X0201 kana introduction */
2399                             input_mode = X0201; shift_mode = X0201;
2400                             NEXT;
2401                         } else if (c1 == 'B' || c1 == 'J' || c1 == 'H') {
2402                             /* This is X0208 kanji introduction */
2403                             input_mode = ASCII; shift_mode = FALSE;
2404                             NEXT;
2405                         } else if (broken_f&0x2) {
2406                             input_mode = ASCII; shift_mode = FALSE;
2407                             NEXT;
2408                         } else {
2409                             (*oconv)(0, ESC);
2410                             (*oconv)(0, '(');
2411                             /* maintain various input_mode here */
2412                             SEND;
2413                         }
2414                     }
2415                } else if ( c1 == 'N' || c1 == 'n' ){
2416                    /* SS2 */
2417                    c3 = (*i_getc)(f);  /* skip SS2 */
2418                    if ( (SPACE<=c3 && c3 < 0x60) || (0xa0<=c3 && c3 < 0xe0)){
2419                        c1 = c3;
2420                        c2 = X0201;
2421                        SEND;
2422                    }else{
2423                        (*i_ungetc)(c3, f);
2424                        /* lonely ESC  */
2425                        (*oconv)(0, ESC);
2426                        SEND;
2427                    }
2428                 } else {
2429                     /* lonely ESC  */
2430                     (*oconv)(0, ESC);
2431                     SEND;
2432                 }
2433             } else if ((c1 == NL || c1 == CR) && broken_f&4) {
2434                 input_mode = ASCII; set_iconv(FALSE, 0);
2435                 SEND;
2436             } else if (c1 == NL && mime_decode_f && !mime_decode_mode ) {
2437                 if ((c1=(*i_getc)(f))!=EOF && c1 == SPACE) {
2438                     i_ungetc(SPACE,f);
2439                     continue;
2440                 } else {
2441                     i_ungetc(c1,f);
2442                 }
2443                 c1 = NL;
2444                 SEND;
2445             } else if (c1 == CR && mime_decode_f && !mime_decode_mode ) {
2446                 if ((c1=(*i_getc)(f))!=EOF) {
2447                     if (c1==SPACE) {
2448                         i_ungetc(SPACE,f);
2449                         continue;
2450                     } else if (c1 == NL && (c1=(*i_getc)(f))!=EOF && c1 == SPACE) {
2451                         i_ungetc(SPACE,f);
2452                         continue;
2453                     } else {
2454                         i_ungetc(c1,f);
2455                     }
2456                     i_ungetc(NL,f);
2457                 } else {
2458                     i_ungetc(c1,f);
2459                 }
2460                 c1 = CR;
2461                 SEND;
2462             } else 
2463                 SEND;
2464         }
2465         /* send: */
2466         if (input_mode == X0208) 
2467             (*oconv)(c2, c1);  /* this is JIS, not SJIS/EUC case */
2468 #ifdef X0212_ENABLE
2469         else if (input_mode == X0212) 
2470             (*oconv)((0x8f << 8) | c2, c1);
2471 #endif /* X0212_ENABLE */
2472         else if (input_mode) 
2473             (*oconv)(input_mode, c1);  /* other special case */
2474         else if ((*iconv)(c2, c1, 0) < 0){  /* can be EUC/SJIS */
2475             int c0 = (*i_getc)(f);
2476             if (c0 != EOF){
2477                 code_status(c0);
2478                 (*iconv)(c2, c1, c0);
2479             }
2480         }
2481
2482         c2 = 0;
2483         continue;
2484         /* goto next_word */
2485     }
2486
2487     /* epilogue */
2488     (*iconv)(EOF, 0, 0);
2489     if (!is_inputcode_set)
2490     {
2491         if (is_8bit) {
2492             struct input_code *p = input_code_list;
2493             struct input_code *result = p;
2494             while (p->name){
2495                 if (p->score < result->score) result = p;
2496                 ++p;
2497             }
2498             set_input_codename(result->name);
2499         }
2500     }
2501     return 1;
2502 }
2503
2504 int
2505 h_conv(f, c2, c1)
2506     FILE  *f;
2507     int    c1,
2508                     c2;
2509 {
2510     int    wc,c3;
2511
2512
2513     /** it must NOT be in the kanji shifte sequence      */
2514     /** it must NOT be written in JIS7                   */
2515     /** and it must be after 2 byte 8bit code            */
2516
2517     hold_count = 0;
2518     push_hold_buf(c2);
2519     push_hold_buf(c1);
2520     c2 = 0;
2521
2522     while ((c1 = (*i_getc)(f)) != EOF) {
2523         if (c1 == ESC){
2524             (*i_ungetc)(c1,f);
2525             break;
2526         }
2527         code_status(c1);
2528         if (push_hold_buf(c1) == EOF || estab_f){
2529             break;
2530         }
2531     }
2532
2533     if (!estab_f){
2534         struct input_code *p = input_code_list;
2535         struct input_code *result = p;
2536         if (c1 == EOF){
2537             code_status(c1);
2538         }
2539         while (p->name){
2540             if (p->score < result->score){
2541                 result = p;
2542             }
2543             ++p;
2544         }
2545         set_iconv(FALSE, result->iconv_func);
2546     }
2547
2548
2549     /** now,
2550      ** 1) EOF is detected, or
2551      ** 2) Code is established, or
2552      ** 3) Buffer is FULL (but last word is pushed)
2553      **
2554      ** in 1) and 3) cases, we continue to use
2555      ** Kanji codes by oconv and leave estab_f unchanged.
2556      **/
2557
2558     c3=c1;
2559     wc = 0;
2560     while (wc < hold_count){
2561         c2 = hold_buf[wc++];
2562         if (c2 <= DEL
2563 #ifdef NUMCHAR_OPTION
2564             || (c2 & CLASS_MASK) == CLASS_UTF16
2565 #endif
2566             ){
2567             (*iconv)(0, c2, 0);
2568             continue;
2569         }else if (iconv == s_iconv && 0xa1 <= c2 && c2 <= 0xdf){
2570             (*iconv)(X0201, c2, 0);
2571             continue;
2572         }
2573         if (wc < hold_count){
2574             c1 = hold_buf[wc++];
2575         }else{
2576             c1 = (*i_getc)(f);
2577             if (c1 == EOF){
2578                 c3 = EOF;
2579                 break;
2580             }
2581             code_status(c1);
2582         }
2583         if ((*iconv)(c2, c1, 0) < 0){
2584             int c0;
2585             if (wc < hold_count){
2586                 c0 = hold_buf[wc++];
2587             }else{
2588                 c0 = (*i_getc)(f);
2589                 if (c0 == EOF){
2590                     c3 = EOF;
2591                     break;
2592                 }
2593                 code_status(c0);
2594             }
2595             (*iconv)(c2, c1, c0);
2596             c1 = c0;
2597         }
2598     }
2599     return c3;
2600 }
2601
2602
2603
2604 int
2605 push_hold_buf(c2)
2606      int             c2;
2607 {
2608     if (hold_count >= HOLD_SIZE*2)
2609         return (EOF);
2610     hold_buf[hold_count++] = c2;
2611     return ((hold_count >= HOLD_SIZE*2) ? EOF : hold_count);
2612 }
2613
2614 int s2e_conv(c2, c1, p2, p1)
2615      int c2, c1;
2616      int *p2, *p1;
2617 {
2618 #if defined(SHIFTJIS_CP932) || defined(X0212_ENABLE)
2619     int val;
2620 #endif
2621 #ifdef SHIFTJIS_CP932
2622     if (cp51932_f && CP932_TABLE_BEGIN <= c2 && c2 <= CP932_TABLE_END){
2623         extern const unsigned short shiftjis_cp932[3][189];
2624         val = shiftjis_cp932[c2 - CP932_TABLE_BEGIN][c1 - 0x40];
2625         if (val){
2626             c2 = val >> 8;
2627             c1 = val & 0xff;
2628         }
2629     }
2630 #endif /* SHIFTJIS_CP932 */
2631 #ifdef X0212_ENABLE
2632     if (x0212_f && 0xfa <= c2 && c2 <= 0xfc){
2633         extern const unsigned short shiftjis_x0212[3][189];
2634         val = shiftjis_x0212[c2 - 0xfa][c1 - 0x40];
2635         if (val){
2636             if (val & 0x8000){
2637                 c2 = (0x8f << 8) | (val >> 8);
2638                 c1 = val & 0xff;
2639             }else{
2640                 c2 = val >> 8;
2641                 c1 = val & 0xff;
2642             }
2643             if (p2) *p2 = c2;
2644             if (p1) *p1 = c1;
2645             return 0;
2646         }
2647     }
2648 #endif
2649     if(c2 >= 0x80){
2650         c2 = c2 + c2 - ((c2 <= 0x9f) ? SJ0162 : SJ6394);
2651         if (c1 < 0x9f)
2652             c1 = c1 - ((c1 > DEL) ? SPACE : 0x1f);
2653         else {
2654             c1 = c1 - 0x7e;
2655             c2++;
2656         }
2657     }
2658
2659 #ifdef X0212_ENABLE
2660     if (x0212_f){
2661         c2 = x0212_unshift(c2);
2662     }
2663 #endif
2664     if (p2) *p2 = c2;
2665     if (p1) *p1 = c1;
2666     return 0;
2667 }
2668
2669 int
2670 s_iconv(c2, c1, c0)
2671     int    c2,
2672                     c1, c0;
2673 {
2674     if (c2 == X0201) {
2675         c1 &= 0x7f;
2676     } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
2677         /* NOP */
2678     } else {
2679         int ret = s2e_conv(c2, c1, &c2, &c1);
2680         if (ret) return ret;
2681     }
2682     (*oconv)(c2, c1);
2683     return 0;
2684 }
2685
2686 int
2687 e_iconv(c2, c1, c0)
2688     int    c2,
2689                     c1, c0;
2690 {
2691     if (c2 == X0201) {
2692         c1 &= 0x7f;
2693 #ifdef X0212_ENABLE
2694     }else if (c2 == 0x8f){
2695         if (c0 == 0){
2696             return -1;
2697         }
2698         c2 = (c2 << 8) | (c1 & 0x7f);
2699         c1 = c0 & 0x7f;
2700 #ifdef SHIFTJIS_CP932
2701         if (cp51932_f){
2702             int s2, s1;
2703             if (e2s_conv(c2, c1, &s2, &s1) == 0){
2704                 s2e_conv(s2, s1, &c2, &c1);
2705                 if ((c2 & 0xff00) == 0){
2706                     c1 &= 0x7f;
2707                     c2 &= 0x7f;
2708                 }
2709             }
2710         }
2711 #endif /* SHIFTJIS_CP932 */
2712 #endif /* X0212_ENABLE */
2713     } else if (c2 == SSO){
2714         c2 = X0201;
2715         c1 &= 0x7f;
2716     } else if ((c2 == EOF) || (c2 == 0) || c2 < SPACE) {
2717         /* NOP */
2718     } else {
2719         c1 &= 0x7f;
2720         c2 &= 0x7f;
2721     }
2722     (*oconv)(c2, c1);
2723     return 0;
2724 }
2725
2726 #ifdef UTF8_INPUT_ENABLE
2727 int
2728 w2e_conv(c2, c1, c0, p2, p1)
2729     int    c2, c1, c0;
2730     int *p2, *p1;
2731 {
2732     extern const unsigned short *const utf8_to_euc_2bytes[];
2733     extern const unsigned short *const *const utf8_to_euc_3bytes[];
2734     int ret = 0;
2735
2736     if (0xc0 <= c2 && c2 <= 0xef) {
2737         const unsigned short *const *pp;
2738
2739         if (0xe0 <= c2) {
2740             if (c0 == 0) return -1;
2741             pp = utf8_to_euc_3bytes[c2 - 0x80];
2742             ret = w_iconv_common(c1, c0, pp, sizeof_utf8_to_euc_C2, p2, p1);
2743         } else {
2744             ret =  w_iconv_common(c2, c1, utf8_to_euc_2bytes, sizeof_utf8_to_euc_2bytes, p2, p1);
2745         }
2746 #ifdef NUMCHAR_OPTION
2747         if (ret){
2748             if (p2) *p2 = 0;
2749             if (p1) *p1 = CLASS_UTF16 | ww16_conv(c2, c1, c0);
2750             ret = 0;
2751         }
2752 #endif
2753         return ret;
2754     } else if (c2 == X0201) {
2755         c1 &= 0x7f;
2756     }
2757     if (p2) *p2 = c2;
2758     if (p1) *p1 = c1;
2759     return ret;
2760 }
2761
2762 int
2763 w_iconv(c2, c1, c0)
2764     int    c2,
2765                     c1, c0;
2766 {
2767     int ret = 0;
2768     
2769     if (c0 == 0){
2770         if (c2 == 0) /* 0x00-0x7f */
2771             ; /* 1byte */
2772         else if ((c2 & 0xe0) == 0xc0) /* 0xc0-0xdf */
2773             ; /* 2ytes */
2774         else if ((c2 & 0xf0) == 0xe0) /* 0xe0-0xef */
2775             return -1; /* 3bytes */
2776 #ifdef __COMMENT__
2777         else if (0xf0 <= c2)
2778             return 0; /* 4,5,6bytes */
2779         else if ((c2 & 0xc0) == 0x80) /* 0x80-0xbf */
2780             return 0; /* trail byte */
2781 #endif
2782         else return 0;
2783     }
2784     if (c2 == EOF);
2785     else if (c2 == 0xef && c1 == 0xbb && c0 == 0xbf) {
2786         return 0; /* throw BOM */
2787 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
2788     } else if (internal_unicode_f && (output_conv == w_oconv || output_conv == w_oconv16)){
2789         unsigned short val = 0;
2790         if(c2 == 0){
2791             c2 = c1;
2792             c1 = 0;
2793         }
2794         val = ww16_conv(c2, c1, c0);
2795         c2 = (val >> 8) & 0xff;
2796         c1 = val & 0xff;
2797 #endif
2798     } else {
2799         ret = w2e_conv(c2, c1, c0, &c2, &c1);
2800     }
2801     if (ret == 0){
2802         (*oconv)(c2, c1);
2803     }
2804     return ret;
2805 }
2806
2807 void
2808 w16w_conv(val, p2, p1, p0)
2809      unsigned short val;
2810      int *p2, *p1, *p0;
2811 {
2812     if (val < 0x80){
2813         *p2 = val;
2814         *p1 = 0;
2815         *p0 = 0;
2816     }else if (val < 0x800){
2817         *p2 = 0xc0 | (val >> 6);
2818         *p1 = 0x80 | (val & 0x3f);
2819         *p0 = 0;
2820     }else{
2821         *p2 = 0xe0 | (val >> 12);
2822         *p1 = 0x80 | ((val >> 6) & 0x3f);
2823         *p0 = 0x80 | (val        & 0x3f);
2824     }
2825 }
2826
2827 int
2828 ww16_conv(c2, c1, c0)
2829      int c2, c1, c0;
2830 {
2831     unsigned short val;
2832     if (c2 >= 0xe0){
2833         val = (c2 & 0x0f) << 12;
2834         val |= (c1 & 0x3f) << 6;
2835         val |= (c0 & 0x3f);
2836     }else if (c2 >= 0xc0){
2837         val = (c2 & 0x1f) << 6;
2838         val |= (c1 & 0x3f);
2839     }else{
2840         val = c2;
2841     }
2842     return val;
2843 }
2844
2845 int
2846 w16e_conv(val, p2, p1)
2847      unsigned short val;
2848      int *p2, *p1;
2849 {
2850     extern const unsigned short *const utf8_to_euc_2bytes[];
2851     extern const unsigned short *const *const utf8_to_euc_3bytes[];
2852     int c2, c1, c0;
2853     const unsigned short *const *pp;
2854     int psize;
2855     int ret = 0;
2856
2857     w16w_conv(val, &c2, &c1, &c0);
2858     if (c1){
2859         if (c0){
2860             pp = utf8_to_euc_3bytes[c2 - 0x80];
2861             psize = sizeof_utf8_to_euc_C2;
2862             ret =  w_iconv_common(c1, c0, pp, psize, p2, p1);
2863         }else{
2864             pp = utf8_to_euc_2bytes;
2865             psize = sizeof_utf8_to_euc_2bytes;
2866             ret =  w_iconv_common(c2, c1, pp, psize, p2, p1);
2867         }
2868 #ifdef NUMCHAR_OPTION
2869         if (ret){
2870             *p2 = 0;
2871             *p1 = CLASS_UTF16 | val;
2872             ret = 0;
2873         }
2874 #endif
2875     }else{
2876         *p2 = 0;
2877         *p1 = c2;
2878     }
2879     return ret;
2880 }
2881
2882 int
2883 w_iconv16(c2, c1, c0)
2884     int    c2, c1,c0;
2885 {
2886     int ret = 0;
2887
2888     if (c2==0376 && c1==0377){
2889         utf16_mode = UTF16BE_INPUT;
2890         return 0;    
2891     } else if (c2==0377 && c1==0376){
2892         utf16_mode = UTF16LE_INPUT;
2893         return 0;    
2894     }
2895     if (c2 != EOF && utf16_mode == UTF16LE_INPUT) {
2896         int tmp;
2897         tmp=c1; c1=c2; c2=tmp;
2898     }
2899     if ((c2==0 && c1 < 0x80) || c2==EOF) {
2900         (*oconv)(c2, c1);
2901         return 0;
2902     }
2903 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
2904     if (internal_unicode_f && (output_conv == w_oconv || output_conv == w_oconv16));
2905 #endif
2906     else ret = w16e_conv(((c2<<8)&0xff00) + c1, &c2, &c1);
2907     if (ret) return ret;
2908     (*oconv)(c2, c1);
2909     return 0;
2910 }
2911
2912 int
2913 w_iconv_common(c1, c0, pp, psize, p2, p1)
2914     int    c1,c0;
2915     const unsigned short *const *pp;
2916     int psize;
2917     int *p2, *p1;
2918 {
2919     int c2;
2920     const unsigned short *p;
2921     unsigned short val;
2922
2923     /* CP932/CP51932: U+00A6 (BROKEN BAR) -> not 0x8fa2c3, but 0x7c */
2924     if (cp51932_f && c1 == 0xC2 && c0 == 0xA6){
2925         if (p2) *p2 = 0;
2926         if (p1) *p1 = 0x7C;
2927         return 0;
2928     }
2929
2930     if (pp == 0) return 1;
2931
2932     c1 -= 0x80;
2933     if (c1 < 0 || psize <= c1) return 1;
2934     p = pp[c1];
2935     if (p == 0)  return 1;
2936
2937     c0 -= 0x80;
2938     if (c0 < 0 || sizeof_utf8_to_euc_E5B8 <= c0) return 1;
2939     val = p[c0];
2940     if (val == 0) return 1;
2941
2942     c2 = val >> 8;
2943     if (val & 0x8000){
2944         c2 &= 0x7f;
2945         c2 |= 0x8f00;
2946     }
2947     if (c2 == SO) c2 = X0201;
2948     c1 = val & 0x7f;
2949     if (p2) *p2 = c2;
2950     if (p1) *p1 = c1;
2951     return 0;
2952 }
2953
2954 #endif
2955
2956 #ifdef UTF8_OUTPUT_ENABLE
2957 void
2958 nkf_each_char_to_hex(f, c)
2959     void (*f)PROTO((int c2,int c1));
2960     int c;
2961 {
2962     const char *hex = "0123456789ABCDEF";
2963     c &= 0x00FFFFFF;
2964     int shift = 20;
2965     while(shift >= 0){
2966         if(c >= 1<<shift){
2967             while(shift >= 0){
2968                 (*f)(0, hex[(c>>shift)&0xF]);
2969                 shift -= 4;
2970             }
2971         }else{
2972             shift -= 4;
2973         }
2974     }
2975     return;
2976 }
2977
2978 void
2979 encode_fallback_html(c)
2980     int c;
2981 {
2982     (*oconv)(0, '&');
2983     (*oconv)(0, '#');
2984     c &= 0x00FFFFFF;
2985     if(c >= 1000000)
2986         (*oconv)(0, 0x30+(c/1000000)%10);
2987     if(c >= 100000)
2988         (*oconv)(0, 0x30+(c/100000 )%10);
2989     if(c >= 10000)
2990         (*oconv)(0, 0x30+(c/10000  )%10);
2991     if(c >= 1000)
2992         (*oconv)(0, 0x30+(c/1000   )%10);
2993     if(c >= 100)
2994         (*oconv)(0, 0x30+(c/100    )%10);
2995     if(c >= 10)
2996         (*oconv)(0, 0x30+(c/10     )%10);
2997     if(c >= 0)
2998         (*oconv)(0, 0x30+ c         %10);
2999     (*oconv)(0, ';');
3000     return;
3001 }
3002
3003 void
3004 encode_fallback_xml(c)
3005     int c;
3006 {
3007     (*oconv)(0, '&');
3008     (*oconv)(0, '#');
3009     (*oconv)(0, 'x');
3010     nkf_each_char_to_hex(oconv, c);
3011     (*oconv)(0, ';');
3012     return;
3013 }
3014
3015 void
3016 encode_fallback_java(c)
3017     int c;
3018 {
3019     const char *hex = "0123456789ABCDEF";
3020     (*oconv)(0, '\\');
3021     if((c&0x00FFFFFF) > 0xFFFF){
3022         (*oconv)(0, 'U');
3023         (*oconv)(0, '0');
3024         (*oconv)(0, '0');
3025         (*oconv)(0, hex[(c>>20)&0xF]);
3026         (*oconv)(0, hex[(c>>16)&0xF]);
3027     }else{
3028         (*oconv)(0, 'u');
3029     }
3030     (*oconv)(0, hex[(c>>12)&0xF]);
3031     (*oconv)(0, hex[(c>> 8)&0xF]);
3032     (*oconv)(0, hex[(c>> 4)&0xF]);
3033     (*oconv)(0, hex[ c     &0xF]);
3034     return;
3035 }
3036
3037 void
3038 encode_fallback_perl(c)
3039     int c;
3040 {
3041     (*oconv)(0, '\\');
3042     (*oconv)(0, 'x');
3043     (*oconv)(0, '{');
3044     nkf_each_char_to_hex(oconv, c);
3045     (*oconv)(0, '}');
3046     return;
3047 }
3048
3049 void
3050 encode_fallback_subchar(c)
3051     int c;
3052 {
3053     c = unicode_subchar;
3054     (*oconv)((c>>8)&0xFF, c&0xFF);
3055     return;
3056     int shift = 16;
3057     while(shift >= 0){
3058         if(c >= 1<<shift){
3059             while(shift >= 0){
3060                 (*oconv)(0, (c>>shift)&0xFF);
3061                 shift -= 8;
3062             }
3063         }else{
3064             shift -= 8;
3065         }
3066     }
3067     return;
3068 }
3069
3070 int
3071 e2w_conv(c2, c1)
3072     int    c2, c1;
3073 {
3074     extern const unsigned short euc_to_utf8_1byte[];
3075     extern const unsigned short *const euc_to_utf8_2bytes[];
3076     extern const unsigned short *const euc_to_utf8_2bytes_ms[];
3077     const unsigned short *p;
3078
3079     if (c2 == X0201) {
3080         p = euc_to_utf8_1byte;
3081 #ifdef X0212_ENABLE
3082     } else if (c2 >> 8 == 0x8f){
3083         extern const unsigned short *const x0212_to_utf8_2bytes[];
3084         c2 = (c2&0x7f) - 0x21;
3085         if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
3086             p = x0212_to_utf8_2bytes[c2];
3087         else
3088             return 0;
3089 #endif
3090     } else {
3091         c2 &= 0x7f;
3092         c2 = (c2&0x7f) - 0x21;
3093         if (0<=c2 && c2<sizeof_euc_to_utf8_2bytes)
3094             p = ms_ucs_map_f ? euc_to_utf8_2bytes_ms[c2] : euc_to_utf8_2bytes[c2];
3095         else
3096             return 0;
3097     }
3098     if (!p) return 0;
3099     c1 = (c1 & 0x7f) - 0x21;
3100     if (0<=c1 && c1<sizeof_euc_to_utf8_1byte)
3101         return p[c1];
3102     return 0;
3103 }
3104
3105 void
3106 w_oconv(c2, c1)
3107     int    c2,
3108                     c1;
3109 {
3110     int c0;
3111     unsigned short val;
3112     if (c2 == EOF) {
3113         (*o_putc)(EOF);
3114         return;
3115     }
3116
3117     if (unicode_bom_f==2) {
3118         (*o_putc)('\357');
3119         (*o_putc)('\273');
3120         (*o_putc)('\277');
3121         unicode_bom_f=1;
3122     }
3123
3124 #ifdef NUMCHAR_OPTION
3125     if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3126         w16w_conv(c1, &c2, &c1, &c0);
3127         (*o_putc)(c2);
3128         if (c1){
3129             (*o_putc)(c1);
3130             if (c0) (*o_putc)(c0);
3131         }
3132         return;
3133     }
3134 #endif
3135
3136     if (c2 == 0) { 
3137         output_mode = ASCII;
3138         (*o_putc)(c1);
3139     } else if (c2 == ISO8859_1) {
3140         output_mode = ISO8859_1;
3141         (*o_putc)(c1 | 0x080);
3142     } else {
3143         output_mode = UTF8;
3144         if (internal_unicode_f && (iconv == w_iconv || iconv == w_iconv16))
3145             val = ((c2<<8)&0xff00) + c1;
3146         else val = e2w_conv(c2, c1);
3147         if (val){
3148             w16w_conv(val, &c2, &c1, &c0);
3149             (*o_putc)(c2);
3150             if (c1){
3151                 (*o_putc)(c1);
3152                 if (c0) (*o_putc)(c0);
3153             }
3154         }
3155     }
3156 }
3157
3158 void
3159 w_oconv16(c2, c1)
3160     int    c2,
3161                     c1;
3162 {
3163     if (c2 == EOF) {
3164         (*o_putc)(EOF);
3165         return;
3166     }    
3167
3168     if (unicode_bom_f==2) {
3169         if (w_oconv16_LE){
3170             (*o_putc)((unsigned char)'\377');
3171             (*o_putc)('\376');
3172         }else{
3173             (*o_putc)('\376');
3174             (*o_putc)((unsigned char)'\377');
3175         }
3176         unicode_bom_f=1;
3177     }
3178
3179     if (internal_unicode_f && (iconv == w_iconv || iconv == w_iconv16)){
3180     } else if (c2 == ISO8859_1) {
3181         c2 = 0;
3182         c1 |= 0x80;
3183 #ifdef NUMCHAR_OPTION
3184     } else if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16) {
3185         c2 = (c1 >> 8) & 0xff;
3186         c1 &= 0xff;
3187 #endif
3188     } else if (c2) {
3189         unsigned short val = e2w_conv(c2, c1);
3190         c2 = (val >> 8) & 0xff;
3191         c1 = val & 0xff;
3192     }
3193     if (w_oconv16_LE){
3194         (*o_putc)(c1);
3195         (*o_putc)(c2);
3196     }else{
3197         (*o_putc)(c2);
3198         (*o_putc)(c1);
3199     }
3200 }
3201
3202 #endif
3203
3204 void
3205 e_oconv(c2, c1)
3206     int    c2,
3207                     c1;
3208 {
3209 #ifdef NUMCHAR_OPTION
3210     if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3211         w16e_conv(c1, &c2, &c1);
3212         if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3213             if(encode_fallback)(*encode_fallback)(c1);
3214             return;
3215         }
3216     }
3217 #endif
3218     if (c2 == EOF) {
3219         (*o_putc)(EOF);
3220         return;
3221     } else if (c2 == 0) { 
3222         output_mode = ASCII;
3223         (*o_putc)(c1);
3224     } else if (c2 == X0201) {
3225         output_mode = JAPANESE_EUC;
3226         (*o_putc)(SSO); (*o_putc)(c1|0x80);
3227     } else if (c2 == ISO8859_1) {
3228         output_mode = ISO8859_1;
3229         (*o_putc)(c1 | 0x080);
3230 #ifdef X0212_ENABLE
3231     } else if ((c2 & 0xff00) >> 8 == 0x8f){
3232         output_mode = JAPANESE_EUC;
3233 #ifdef SHIFTJIS_CP932
3234         if (cp51932_f){
3235             int s2, s1;
3236             if (e2s_conv(c2, c1, &s2, &s1) == 0){
3237                 s2e_conv(s2, s1, &c2, &c1);
3238             }
3239         }
3240 #endif
3241         if (c2 == 0) {
3242             output_mode = ASCII;
3243             (*o_putc)(c1);
3244         }else if ((c2 & 0xff00) >> 8 == 0x8f){
3245             if (x0212_f){
3246                 (*o_putc)(0x8f);
3247                 (*o_putc)((c2 & 0x7f) | 0x080);
3248                 (*o_putc)(c1 | 0x080);
3249             }
3250         }else{
3251             (*o_putc)((c2 & 0x7f) | 0x080);
3252             (*o_putc)(c1 | 0x080);
3253         }
3254 #endif
3255     } else {
3256         if ((c1<0x21 || 0x7e<c1) ||
3257            (c2<0x21 || 0x7e<c2)) {
3258             set_iconv(FALSE, 0);
3259             return; /* too late to rescue this char */
3260         }
3261         output_mode = JAPANESE_EUC;
3262         (*o_putc)(c2 | 0x080);
3263         (*o_putc)(c1 | 0x080);
3264     }
3265 }
3266
3267 #ifdef X0212_ENABLE
3268 int x0212_shift(c)
3269      int c;
3270 {
3271     int ret = c;
3272     c &= 0x7f;
3273     if ((ret & 0xff00) == 0x8f00){
3274         if (0x75 <= c && c <= 0x7f){
3275             ret = c + (0x109 - 0x75);
3276         }
3277     }else{
3278         if (0x75 <= c && c <= 0x7f){
3279             ret = c + (0x113 - 0x75);
3280         }
3281     }
3282     return ret;
3283 }
3284
3285
3286 int x0212_unshift(c)
3287      int c;
3288 {
3289     int ret = c;
3290     if (0x7f <= c && c <= 0x88){
3291         ret = c + (0x75 - 0x7f);
3292     }else if (0x89 <= c && c <= 0x92){
3293         ret = (0x8f << 8) | 0x80 | (c + (0x75 - 0x89));
3294     }
3295     return ret;
3296 }
3297 #endif /* X0212_ENABLE */
3298
3299 int
3300 e2s_conv(c2, c1, p2, p1)
3301      int c2, c1, *p2, *p1;
3302 {
3303 #ifdef X0212_ENABLE
3304     int val = 0;
3305     const unsigned short *ptr;
3306     int ndx;
3307     extern const unsigned short *const x0212_shiftjis[];
3308     if ((c2 & 0xff00) == 0x8f00){
3309         ndx = c2 & 0x7f;
3310         if (0x21 <= ndx && ndx <= 0x7e){
3311             ptr = x0212_shiftjis[ndx - 0x21];
3312             if (ptr){
3313                 val = ptr[(c1 & 0x7f) - 0x21];
3314             }
3315             if (val){
3316                 c2 = val >> 8;
3317                 c1 = val & 0xff;
3318                 if (p2) *p2 = c2;
3319                 if (p1) *p1 = c1;
3320                 return 0;
3321             }
3322         }
3323         c2 = x0212_shift(c2);
3324     }
3325 #endif /* X0212_ENABLE */
3326     if ((c2 & 0xff00) == 0x8f00){
3327         return 1;
3328     }
3329     if (p2) *p2 = ((c2 - 1) >> 1) + ((c2 <= 0x5e) ? 0x71 : 0xb1);
3330     if (p1) *p1 = c1 + ((c2 & 1) ? ((c1 < 0x60) ? 0x1f : 0x20) : 0x7e);
3331     return 0;
3332 }
3333
3334 void
3335 s_oconv(c2, c1)
3336     int    c2,
3337                     c1;
3338 {
3339 #ifdef NUMCHAR_OPTION
3340     if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3341         w16e_conv(c1, &c2, &c1);
3342         if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3343             if(encode_fallback)(*encode_fallback)(c1);
3344             return;
3345         }
3346     }
3347 #endif
3348     if (c2 == EOF) {
3349         (*o_putc)(EOF);
3350         return;
3351     } else if (c2 == 0) {
3352         output_mode = ASCII;
3353         (*o_putc)(c1);
3354     } else if (c2 == X0201) {
3355         output_mode = SHIFT_JIS;
3356         (*o_putc)(c1|0x80);
3357     } else if (c2 == ISO8859_1) {
3358         output_mode = ISO8859_1;
3359         (*o_putc)(c1 | 0x080);
3360 #ifdef X0212_ENABLE
3361     } else if ((c2 & 0xff00) >> 8 == 0x8f){
3362         output_mode = SHIFT_JIS;
3363         if (e2s_conv(c2, c1, &c2, &c1) == 0){
3364             (*o_putc)(c2);
3365             (*o_putc)(c1);
3366         }
3367 #endif
3368     } else {
3369         if ((c1<0x20 || 0x7e<c1) ||
3370            (c2<0x20 || 0x7e<c2)) {
3371             set_iconv(FALSE, 0);
3372             return; /* too late to rescue this char */
3373         }
3374         output_mode = SHIFT_JIS;
3375         e2s_conv(c2, c1, &c2, &c1);
3376
3377 #ifdef SHIFTJIS_CP932
3378         if (cp932inv_f
3379             && CP932INV_TABLE_BEGIN <= c2 && c2 <= CP932INV_TABLE_END){
3380             extern const unsigned short cp932inv[2][189];
3381             int c = cp932inv[c2 - CP932INV_TABLE_BEGIN][c1 - 0x40];
3382             if (c){
3383                 c2 = c >> 8;
3384                 c1 = c & 0xff;
3385             }
3386         }
3387 #endif /* SHIFTJIS_CP932 */
3388
3389         (*o_putc)(c2);
3390         if (prefix_table[(unsigned char)c1]){
3391             (*o_putc)(prefix_table[(unsigned char)c1]);
3392         }
3393         (*o_putc)(c1);
3394     }
3395 }
3396
3397 void
3398 j_oconv(c2, c1)
3399     int    c2,
3400                     c1;
3401 {
3402 #ifdef NUMCHAR_OPTION
3403     if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3404         w16e_conv(c1, &c2, &c1);
3405         if (c2 == 0 && (c1 & CLASS_MASK) == CLASS_UTF16){
3406             if(encode_fallback)(*encode_fallback)(c1);
3407             return;
3408         }
3409     }
3410 #endif
3411     if (c2 == EOF) {
3412         if (output_mode !=ASCII && output_mode!=ISO8859_1) {
3413             (*o_putc)(ESC);
3414             (*o_putc)('(');
3415             (*o_putc)(ascii_intro);
3416             output_mode = ASCII;
3417         }
3418         (*o_putc)(EOF);
3419 #ifdef X0212_ENABLE
3420     } else if ((c2 & 0xff00) >> 8 == 0x8f){
3421         if (output_mode!=X0212) {
3422             output_mode = X0212;
3423             (*o_putc)(ESC);
3424             (*o_putc)('$');
3425             (*o_putc)('(');
3426             (*o_putc)('D');
3427         }
3428         (*o_putc)(c2 & 0x7f);
3429         (*o_putc)(c1);
3430 #endif
3431     } else if (c2==X0201) {
3432         if (output_mode!=X0201) {
3433             output_mode = X0201;
3434             (*o_putc)(ESC);
3435             (*o_putc)('(');
3436             (*o_putc)('I');
3437         }
3438         (*o_putc)(c1);
3439     } else if (c2==ISO8859_1) {
3440             /* iso8859 introduction, or 8th bit on */
3441             /* Can we convert in 7bit form using ESC-'-'-A ? 
3442                Is this popular? */
3443         output_mode = ISO8859_1;
3444         (*o_putc)(c1|0x80);
3445     } else if (c2 == 0) {
3446         if (output_mode !=ASCII && output_mode!=ISO8859_1) {
3447             (*o_putc)(ESC);
3448             (*o_putc)('(');
3449             (*o_putc)(ascii_intro);
3450             output_mode = ASCII;
3451         }
3452         (*o_putc)(c1);
3453     } else {
3454         if (output_mode != X0208) {
3455             output_mode = X0208;
3456             (*o_putc)(ESC);
3457             (*o_putc)('$');
3458             (*o_putc)(kanji_intro);
3459         }
3460         if (c1<0x20 || 0x7e<c1) 
3461             return;
3462         if (c2<0x20 || 0x7e<c2) 
3463             return;
3464         (*o_putc)(c2);
3465         (*o_putc)(c1);
3466     }
3467 }
3468
3469 void
3470 base64_conv(c2, c1)
3471     int    c2,
3472                     c1;
3473 {
3474     mime_prechar(c2, c1);
3475     (*o_base64conv)(c2,c1);
3476 }
3477
3478
3479 STATIC int broken_buf[3];
3480 STATIC int broken_counter = 0;
3481 STATIC int broken_last = 0;
3482 int
3483 broken_getc(f)
3484 FILE *f;
3485 {
3486     int c,c1;
3487
3488     if (broken_counter>0) {
3489         return broken_buf[--broken_counter];
3490     }
3491     c= (*i_bgetc)(f);
3492     if (c=='$' && broken_last != ESC 
3493             && (input_mode==ASCII || input_mode==X0201)) {
3494         c1= (*i_bgetc)(f);
3495         broken_last = 0;
3496         if (c1=='@'|| c1=='B') {
3497             broken_buf[0]=c1; broken_buf[1]=c; 
3498             broken_counter=2;
3499             return ESC;
3500         } else {
3501             (*i_bungetc)(c1,f);
3502             return c;
3503         }
3504     } else if (c=='(' && broken_last != ESC 
3505             && (input_mode==X0208 || input_mode==X0201)) { /* ) */
3506         c1= (*i_bgetc)(f);
3507         broken_last = 0;
3508         if (c1=='J'|| c1=='B') {
3509             broken_buf[0]=c1; broken_buf[1]=c;
3510             broken_counter=2;
3511             return ESC;
3512         } else {
3513             (*i_bungetc)(c1,f);
3514             return c;
3515         }
3516     } else {
3517         broken_last = c;
3518         return c;
3519     }
3520 }
3521
3522 int
3523 broken_ungetc(c,f)
3524 int c;
3525 FILE *f;
3526 {
3527     if (broken_counter<2)
3528         broken_buf[broken_counter++]=c;
3529     return c;
3530 }
3531
3532 STATIC int prev_cr = 0;
3533
3534 void
3535 cr_conv(c2,c1) 
3536 int c2,c1;
3537 {
3538     if (prev_cr) {
3539         prev_cr = 0;
3540         if (! (c2==0&&c1==NL) ) {
3541             cr_conv(0,'\n');
3542         }
3543     }
3544     if (c2) {
3545         (*o_crconv)(c2,c1);
3546     } else if (c1=='\r') {
3547         prev_cr = c1;
3548     } else if (c1=='\n') {
3549         if (crmode_f==CRLF) {
3550             (*o_crconv)(0,'\r');
3551         } else if (crmode_f==CR) {
3552             (*o_crconv)(0,'\r');
3553             return;
3554         } 
3555         (*o_crconv)(0,NL);
3556     } else if (c1!='\032' || crmode_f!=NL){
3557         (*o_crconv)(c2,c1);
3558     }
3559 }
3560
3561 /* 
3562   Return value of fold_conv()
3563
3564        \n  add newline  and output char
3565        \r  add newline  and output nothing
3566        ' ' space
3567        0   skip  
3568        1   (or else) normal output 
3569
3570   fold state in prev (previous character)
3571
3572       >0x80 Japanese (X0208/X0201)
3573       <0x80 ASCII
3574       \n    new line 
3575       ' '   space
3576
3577   This fold algorthm does not preserve heading space in a line.
3578   This is the main difference from fmt.
3579 */
3580
3581 #define char_size(c2,c1) (c2?2:1)
3582
3583 void
3584 fold_conv(c2,c1) 
3585 int c2,c1;
3586
3587     int prev0;
3588     int fold_state=0;
3589
3590     if (c1== '\r' && !fold_preserve_f) {
3591         fold_state=0;  /* ignore cr */
3592     }else if (c1== '\n'&&f_prev=='\r' && fold_preserve_f) {
3593         f_prev = '\n';
3594         fold_state=0;  /* ignore cr */
3595     } else if (c1== BS) {
3596         if (f_line>0) f_line--;
3597         fold_state =  1;
3598     } else if (c2==EOF && f_line != 0) {    /* close open last line */
3599             fold_state = '\n';
3600     } else if ((c1=='\n' && !fold_preserve_f)
3601                || ((c1=='\r'||(c1=='\n'&&f_prev!='\r'))
3602                    && fold_preserve_f)) {
3603         /* new line */
3604         if (fold_preserve_f) { 
3605             f_prev = c1;
3606             f_line = 0;
3607             fold_state =  '\r';
3608         } else if ((f_prev == c1 && !fold_preserve_f)
3609                    || (f_prev == '\n' && fold_preserve_f)
3610                    ) {        /* duplicate newline */
3611             if (f_line) {
3612                 f_line = 0;
3613                 fold_state =  '\n';    /* output two newline */
3614             } else {
3615                 f_line = 0;
3616                 fold_state =  1;
3617             }
3618         } else  {
3619             if (f_prev&0x80) {     /* Japanese? */
3620                 f_prev = c1;
3621                 fold_state =  0;       /* ignore given single newline */
3622             } else if (f_prev==' ') {
3623                 fold_state =  0;
3624             } else {
3625                 f_prev = c1;
3626                 if (++f_line<=fold_len) 
3627                     fold_state =  ' ';
3628                 else {
3629                     f_line = 0;
3630                     fold_state =  '\r';        /* fold and output nothing */
3631                 }
3632             }
3633         }
3634     } else if (c1=='\f') {
3635         f_prev = '\n';
3636         if (f_line==0)
3637             fold_state =  1;
3638         f_line = 0;
3639         fold_state =  '\n';            /* output newline and clear */
3640     } else if ( (c2==0  && c1==' ')||
3641                (c2==0  && c1=='\t')||
3642                (c2=='!'&& c1=='!')) {
3643         /* X0208 kankaku or ascii space */
3644             if (f_prev == ' ') {
3645                 fold_state = 0;         /* remove duplicate spaces */
3646             } else {
3647                 f_prev = ' ';    
3648                 if (++f_line<=fold_len) 
3649                     fold_state = ' ';         /* output ASCII space only */
3650                 else {
3651                     f_prev = ' '; f_line = 0;
3652                     fold_state = '\r';        /* fold and output nothing */
3653                 }
3654             }
3655     } else {
3656         prev0 = f_prev; /* we still need this one... , but almost done */
3657         f_prev = c1;
3658         if (c2 || c2==X0201) 
3659             f_prev |= 0x80;  /* this is Japanese */
3660         f_line += char_size(c2,c1);
3661         if (f_line<=fold_len) {   /* normal case */
3662             fold_state = 1;
3663         } else {
3664             if (f_line>fold_len+fold_margin) { /* too many kinsoku suspension */
3665                 f_line = char_size(c2,c1);
3666                 fold_state =  '\n';       /* We can't wait, do fold now */
3667             } else if (c2==X0201) {
3668             /* simple kinsoku rules  return 1 means no folding  */
3669                 if (c1==(0xde&0x7f)) fold_state = 1; /* \e$B!+\e(B*/
3670                 else if (c1==(0xdf&0x7f)) fold_state = 1; /* \e$B!,\e(B*/
3671                 else if (c1==(0xa4&0x7f)) fold_state = 1; /* \e$B!#\e(B*/
3672                 else if (c1==(0xa3&0x7f)) fold_state = 1; /* \e$B!$\e(B*/
3673                 else if (c1==(0xa1&0x7f)) fold_state = 1; /* \e$B!W\e(B*/
3674                 else if (c1==(0xb0&0x7f)) fold_state = 1; /* - */
3675                 else if (SPACE<=c1 && c1<=(0xdf&0x7f)) {      /* X0201 */
3676                     f_line = 1;
3677                     fold_state = '\n';/* add one new f_line before this character */
3678                 } else {
3679                     f_line = 1;
3680                     fold_state = '\n';/* add one new f_line before this character */
3681                 }
3682             } else if (c2==0) {
3683                 /* kinsoku point in ASCII */ 
3684                 if (  c1==')'||    /* { [ ( */
3685                      c1==']'||
3686                      c1=='}'||
3687                      c1=='.'||
3688                      c1==','||
3689                      c1=='!'||
3690                      c1=='?'||
3691                      c1=='/'||
3692                      c1==':'||
3693                      c1==';' ) {
3694                     fold_state = 1;
3695                 /* just after special */
3696                 } else if (!is_alnum(prev0)) {
3697                     f_line = char_size(c2,c1);
3698                     fold_state = '\n';
3699                 } else if ((prev0==' ') ||   /* ignored new f_line */
3700                       (prev0=='\n')||        /* ignored new f_line */
3701                       (prev0&0x80)) {        /* X0208 - ASCII */
3702                     f_line = char_size(c2,c1);
3703                     fold_state = '\n';/* add one new f_line before this character */
3704                 } else {
3705                     fold_state = 1;  /* default no fold in ASCII */
3706                 }
3707             } else {
3708                 if (c2=='!') {
3709                     if (c1=='"')  fold_state = 1; /* \e$B!"\e(B */
3710                     else if (c1=='#')  fold_state = 1; /* \e$B!#\e(B */
3711                     else if (c1=='W')  fold_state = 1; /* \e$B!W\e(B */
3712                     else if (c1=='K')  fold_state = 1; /* \e$B!K\e(B */
3713                     else if (c1=='$')  fold_state = 1; /* \e$B!$\e(B */
3714                     else if (c1=='%')  fold_state = 1; /* \e$B!%\e(B */
3715                     else if (c1=='\'') fold_state = 1; /* \e$B!\\e(B */
3716                     else if (c1=='(')  fold_state = 1; /* \e$B!(\e(B */
3717                     else if (c1==')')  fold_state = 1; /* \e$B!)\e(B */
3718                     else if (c1=='*')  fold_state = 1; /* \e$B!*\e(B */
3719                     else if (c1=='+')  fold_state = 1; /* \e$B!+\e(B */
3720                     else if (c1==',')  fold_state = 1; /* \e$B!,\e(B */
3721                          /* default no fold in kinsoku */
3722                     else { 
3723                         fold_state = '\n';
3724                         f_line = char_size(c2,c1);
3725                         /* add one new f_line before this character */
3726                     }
3727                 } else {
3728                     f_line = char_size(c2,c1);
3729                     fold_state = '\n'; 
3730                     /* add one new f_line before this character */
3731                 }
3732             }
3733         }
3734     }
3735     /* terminator process */
3736     switch(fold_state) {
3737         case '\n': 
3738             (*o_fconv)(0,'\n');
3739             (*o_fconv)(c2,c1);
3740             break;
3741         case 0:    
3742             return;
3743         case '\r': 
3744             (*o_fconv)(0,'\n');
3745             break;
3746         case '\t': 
3747         case ' ': 
3748             (*o_fconv)(0,' ');
3749             break;
3750         default:
3751             (*o_fconv)(c2,c1);
3752     }
3753 }
3754
3755 int z_prev2=0,z_prev1=0;
3756
3757 void
3758 z_conv(c2,c1)
3759 int c2,c1;
3760 {
3761
3762     /* if (c2) c1 &= 0x7f; assertion */
3763
3764     if (x0201_f && z_prev2==X0201) {  /* X0201 */
3765         if (c1==(0xde&0x7f)) { /* \e$BByE@\e(B */
3766             z_prev2=0;
3767             (*o_zconv)(dv[(z_prev1-SPACE)*2],dv[(z_prev1-SPACE)*2+1]);
3768             return;
3769         } else if (c1==(0xdf&0x7f)&&ev[(z_prev1-SPACE)*2]) {  /* \e$BH>ByE@\e(B */
3770             z_prev2=0;
3771             (*o_zconv)(ev[(z_prev1-SPACE)*2],ev[(z_prev1-SPACE)*2+1]);
3772             return;
3773         } else {
3774             z_prev2=0;
3775             (*o_zconv)(cv[(z_prev1-SPACE)*2],cv[(z_prev1-SPACE)*2+1]);
3776         }
3777     }
3778
3779     if (c2==EOF) {
3780         (*o_zconv)(c2,c1);
3781         return;
3782     }
3783
3784     if (x0201_f && c2==X0201) {
3785         if (dv[(c1-SPACE)*2]||ev[(c1-SPACE)*2]) {
3786             /* wait for \e$BByE@\e(B or \e$BH>ByE@\e(B */
3787             z_prev1 = c1; z_prev2 = c2;
3788             return;
3789         } else {
3790             (*o_zconv)(cv[(c1-SPACE)*2],cv[(c1-SPACE)*2+1]);
3791             return;
3792         }
3793     }
3794
3795     /* JISX0208 Alphabet */
3796     if (alpha_f && c2 == 0x23 ) {
3797         c2 = 0;
3798     } else if (alpha_f && c2 == 0x21 ) { 
3799     /* JISX0208 Kigou */
3800        if (0x21==c1) {
3801            if (alpha_f&0x2) {
3802                c1 = ' ';
3803                c2 = 0;
3804            } else if (alpha_f&0x4) {
3805                 (*o_zconv)(0,' ');
3806                 (*o_zconv)(0,' ');
3807                 return;
3808            } 
3809        } else if (0x20<c1 && c1<0x7f && fv[c1-0x20]) {
3810            c1 = fv[c1-0x20];
3811            c2 =  0;
3812            if (alpha_f&0x8) {
3813                char *entity = 0;
3814                switch (c1){
3815                  case '>': entity = "&gt;"; break;
3816                  case '<': entity = "&lt;"; break;
3817                  case '\"': entity = "&quot;"; break;
3818                  case '&': entity = "&amp;"; break;
3819                }
3820                if (entity){
3821                    while (*entity) (*o_zconv)(0, *entity++);
3822                    return;
3823                }
3824            }
3825        } 
3826     }
3827     (*o_zconv)(c2,c1);
3828 }
3829
3830
3831 #define rot13(c)  ( \
3832       ( c < 'A' ) ? c: \
3833       (c <= 'M')  ? (c + 13): \
3834       (c <= 'Z')  ? (c - 13): \
3835       (c < 'a')   ? (c): \
3836       (c <= 'm')  ? (c + 13): \
3837       (c <= 'z')  ? (c - 13): \
3838       (c) \
3839 )
3840
3841 #define  rot47(c) ( \
3842       ( c < '!' ) ? c: \
3843       ( c <= 'O' ) ? (c + 47) : \
3844       ( c <= '~' ) ?  (c - 47) : \
3845       c \
3846 )
3847
3848 void
3849 rot_conv(c2,c1)
3850 int c2,c1;
3851 {
3852     if (c2==0 || c2==X0201 || c2==ISO8859_1) {
3853         c1 = rot13(c1);
3854     } else if (c2) {
3855         c1 = rot47(c1);
3856         c2 = rot47(c2);
3857     }
3858     (*o_rot_conv)(c2,c1);
3859 }
3860
3861 void
3862 hira_conv(c2,c1)
3863 int c2,c1;
3864 {
3865     if ((hira_f & 1) && c2==0x25 && 0x20<c1 && c1<0x74) {
3866         c2 = 0x24;
3867     } else if ((hira_f & 2) && c2==0x24 && 0x20<c1 && c1<0x74) {
3868         c2 = 0x25;
3869     } 
3870     (*o_hira_conv)(c2,c1);
3871 }
3872
3873
3874 void
3875 iso2022jp_check_conv(c2,c1)
3876 int    c2, c1;
3877 {
3878     STATIC const int range[RANGE_NUM_MAX][2] = {
3879         {0x222f, 0x2239,},
3880         {0x2242, 0x2249,},
3881         {0x2251, 0x225b,},
3882         {0x226b, 0x2271,},
3883         {0x227a, 0x227d,},
3884         {0x2321, 0x232f,},
3885         {0x233a, 0x2340,},
3886         {0x235b, 0x2360,},
3887         {0x237b, 0x237e,},
3888         {0x2474, 0x247e,},
3889         {0x2577, 0x257e,},
3890         {0x2639, 0x2640,},
3891         {0x2659, 0x267e,},
3892         {0x2742, 0x2750,},
3893         {0x2772, 0x277e,},
3894         {0x2841, 0x287e,},
3895         {0x4f54, 0x4f7e,},
3896         {0x7425, 0x747e},
3897     };
3898     int i;
3899     int start, end, c;
3900
3901     if(c2 >= 0x00 && c2 <= 0x20 && c1 >= 0x7f && c1 <= 0xff) {
3902         c2 = GETA1;
3903         c1 = GETA2;
3904     }
3905     if((c2 >= 0x29 && c2 <= 0x2f) || (c2 >= 0x75 && c2 <= 0x7e)) {
3906         c2 = GETA1;
3907         c1 = GETA2;
3908     }
3909
3910     for (i = 0; i < RANGE_NUM_MAX; i++) {
3911         start = range[i][0];
3912         end   = range[i][1];
3913         c     = (c2 << 8) + c1;
3914         if (c >= start && c <= end) {
3915             c2 = GETA1;
3916             c1 = GETA2;
3917         }
3918     }
3919     (*o_iso2022jp_check_conv)(c2,c1);
3920 }
3921
3922
3923 /* This converts  =?ISO-2022-JP?B?HOGE HOGE?= */
3924
3925 const unsigned char *mime_pattern[] = {
3926     (const unsigned char *)"\075?EUC-JP?B?",
3927     (const unsigned char *)"\075?SHIFT_JIS?B?",
3928     (const unsigned char *)"\075?ISO-8859-1?Q?",
3929     (const unsigned char *)"\075?ISO-8859-1?B?",
3930     (const unsigned char *)"\075?ISO-2022-JP?B?",
3931     (const unsigned char *)"\075?ISO-2022-JP?Q?",
3932 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3933     (const unsigned char *)"\075?UTF-8?B?",
3934     (const unsigned char *)"\075?UTF-8?Q?",
3935 #endif
3936     (const unsigned char *)"\075?US-ASCII?Q?",
3937     NULL
3938 };
3939
3940
3941 /* \e$B3:Ev$9$k%3!<%I$NM%@hEY$r>e$2$k$?$a$NL\0u\e(B */
3942 int (*mime_priority_func[])PROTO((int c2, int c1, int c0)) = {
3943     e_iconv, s_iconv, 0, 0, 0, 0,
3944 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3945     w_iconv, w_iconv,
3946 #endif
3947     0,
3948 };
3949
3950 const int mime_encode[] = {
3951     JAPANESE_EUC, SHIFT_JIS,ISO8859_1, ISO8859_1, X0208, X0201,
3952 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3953     UTF8, UTF8,
3954 #endif
3955     ASCII,
3956     0
3957 };
3958
3959 const int mime_encode_method[] = {
3960     'B', 'B','Q', 'B', 'B', 'Q',
3961 #if defined(UTF8_INPUT_ENABLE) || defined(UTF8_OUTPUT_ENABLE)
3962     'B', 'Q',
3963 #endif
3964     'Q',
3965     0
3966 };
3967
3968
3969 #define MAXRECOVER 20
3970
3971 void
3972 switch_mime_getc()
3973 {
3974     if (i_getc!=mime_getc) {
3975         i_mgetc = i_getc; i_getc = mime_getc;
3976         i_mungetc = i_ungetc; i_ungetc = mime_ungetc;
3977         if(mime_f==STRICT_MIME) {
3978             i_mgetc_buf = i_mgetc; i_mgetc = mime_getc_buf;
3979             i_mungetc_buf = i_mungetc; i_mungetc = mime_ungetc_buf;
3980         }
3981     }
3982 }
3983
3984 void
3985 unswitch_mime_getc()
3986 {
3987     if(mime_f==STRICT_MIME) {
3988         i_mgetc = i_mgetc_buf;
3989         i_mungetc = i_mungetc_buf;
3990     }
3991     i_getc = i_mgetc;
3992     i_ungetc = i_mungetc;
3993     if(mime_iconv_back)set_iconv(FALSE, mime_iconv_back);
3994     mime_iconv_back = NULL;
3995 }
3996
3997 int
3998 mime_begin_strict(f)
3999 FILE *f;
4000 {
4001     int c1 = 0;
4002     int i,j,k;
4003     const unsigned char *p,*q;
4004     int r[MAXRECOVER];    /* recovery buffer, max mime pattern lenght */
4005
4006     mime_decode_mode = FALSE;
4007     /* =? has been checked */
4008     j = 0;
4009     p = mime_pattern[j];
4010     r[0]='='; r[1]='?';
4011
4012     for(i=2;p[i]>' ';i++) {                   /* start at =? */
4013         if ( ((r[i] = c1 = (*i_getc)(f))==EOF) || nkf_toupper(c1) != p[i] ) {
4014             /* pattern fails, try next one */
4015             q = p;
4016             while ((p = mime_pattern[++j])) {
4017                 for(k=2;k<i;k++)              /* assume length(p) > i */
4018                     if (p[k]!=q[k]) break;
4019                 if (k==i && nkf_toupper(c1)==p[k]) break;
4020             }
4021             if (p) continue;  /* found next one, continue */
4022             /* all fails, output from recovery buffer */
4023             (*i_ungetc)(c1,f);
4024             for(j=0;j<i;j++) {
4025                 (*oconv)(0,r[j]);
4026             }
4027             return c1;
4028         }
4029     }
4030     mime_decode_mode = p[i-2];
4031
4032     mime_iconv_back = iconv;
4033     set_iconv(FALSE, mime_priority_func[j]);
4034     clr_code_score(find_inputcode_byfunc(mime_priority_func[j]), SCORE_iMIME);
4035
4036     if (mime_decode_mode=='B') {
4037         mimebuf_f = unbuf_f;
4038         if (!unbuf_f) {
4039             /* do MIME integrity check */
4040             return mime_integrity(f,mime_pattern[j]);
4041         } 
4042     }
4043     switch_mime_getc();
4044     mimebuf_f = TRUE;
4045     return c1;
4046 }
4047
4048 int
4049 mime_getc_buf(f) 
4050 FILE *f;
4051 {
4052     /* we don't keep eof of Fifo, becase it contains ?= as
4053        a terminator. It was checked in mime_integrity. */
4054     return ((mimebuf_f)?
4055         (*i_mgetc_buf)(f):Fifo(mime_input++));
4056 }
4057
4058 int
4059 mime_ungetc_buf(c,f) 
4060 FILE *f;
4061 int c;
4062 {
4063     if (mimebuf_f)
4064         (*i_mungetc_buf)(c,f);
4065     else 
4066         Fifo(--mime_input)=c;
4067     return c;
4068 }
4069
4070 int
4071 mime_begin(f)
4072 FILE *f;
4073 {
4074     int c1;
4075     int i,k;
4076
4077     /* In NONSTRICT mode, only =? is checked. In case of failure, we  */
4078     /* re-read and convert again from mime_buffer.  */
4079
4080     /* =? has been checked */
4081     k = mime_last;
4082     Fifo(mime_last++)='='; Fifo(mime_last++)='?';
4083     for(i=2;i<MAXRECOVER;i++) {                   /* start at =? */
4084         /* We accept any character type even if it is breaked by new lines */
4085         c1 = (*i_getc)(f); Fifo(mime_last++)= c1 ;
4086         if (c1=='\n'||c1==' '||c1=='\r'||
4087                 c1=='-'||c1=='_'||is_alnum(c1) ) continue;
4088         if (c1=='=') {
4089             /* Failed. But this could be another MIME preemble */
4090             (*i_ungetc)(c1,f);
4091             mime_last--;
4092             break;
4093         }
4094         if (c1!='?') break;
4095         else {
4096             /* c1=='?' */
4097             c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
4098             if (!(++i<MAXRECOVER) || c1==EOF) break;
4099             if (c1=='b'||c1=='B') {
4100                 mime_decode_mode = 'B';
4101             } else if (c1=='q'||c1=='Q') {
4102                 mime_decode_mode = 'Q';
4103             } else {
4104                 break;
4105             }
4106             c1 = (*i_getc)(f); Fifo(mime_last++) = c1;
4107             if (!(++i<MAXRECOVER) || c1==EOF) break;
4108             if (c1!='?') {
4109                 mime_decode_mode = FALSE;
4110             }
4111             break;
4112         }
4113     }
4114     switch_mime_getc();
4115     if (!mime_decode_mode) {
4116         /* false MIME premble, restart from mime_buffer */
4117         mime_decode_mode = 1;  /* no decode, but read from the mime_buffer */
4118         /* Since we are in MIME mode until buffer becomes empty,    */
4119         /* we never go into mime_begin again for a while.           */
4120         return c1;
4121     }
4122     /* discard mime preemble, and goto MIME mode */
4123     mime_last = k;
4124     /* do no MIME integrity check */
4125     return c1;   /* used only for checking EOF */
4126 }
4127
4128 #ifdef CHECK_OPTION
4129 void
4130 no_putc(c)
4131      int c;
4132 {
4133     ;
4134 }
4135
4136 void debug(str)
4137     const char *str;
4138 {
4139     if (debug_f){
4140         fprintf(stderr, "%s\n", str);
4141     }
4142 }
4143 #endif
4144
4145 void
4146 set_input_codename (codename)
4147     char *codename;
4148 {
4149     if (guess_f && 
4150         is_inputcode_set &&
4151         strcmp(codename, "") != 0 && 
4152         strcmp(codename, input_codename) != 0)
4153     {
4154         is_inputcode_mixed = TRUE;
4155     }
4156     input_codename = codename;
4157     is_inputcode_set = TRUE;
4158 }
4159
4160 #if !defined(PERL_XS) && !defined(WIN32DLL)
4161 void
4162 print_guessed_code (filename)
4163     char *filename;
4164 {
4165     char *codename = "BINARY";
4166     if (!is_inputcode_mixed) {
4167         if (strcmp(input_codename, "") == 0) {
4168             codename = "ASCII";
4169         } else {
4170             codename = input_codename;
4171         }
4172     }
4173     if (filename != NULL) printf("%s:", filename);
4174     printf("%s\n", codename);
4175 }
4176 #endif /*WIN32DLL*/
4177
4178 #ifdef INPUT_OPTION 
4179
4180 #ifdef ANSI_C_PROTOTYPE
4181 int hex_getc(int ch, FILE *f, int (*g)(FILE *f), int (*u)(int c, FILE *f))
4182 #else
4183 int
4184 hex_getc(ch, f, g, u)
4185      int ch;
4186      FILE *f;
4187      int (*g)();
4188      int (*u)();
4189 #endif
4190 {
4191     int c1, c2, c3;
4192     c1 = (*g)(f);
4193     if (c1 != ch){
4194         return c1;
4195     }
4196     c2 = (*g)(f);
4197     if (!nkf_isxdigit(c2)){
4198         (*u)(c2, f);
4199         return c1;
4200     }
4201     c3 = (*g)(f);
4202     if (!nkf_isxdigit(c3)){
4203         (*u)(c2, f);
4204         (*u)(c3, f);
4205         return c1;
4206     }
4207     return (hex2bin(c2) << 4) | hex2bin(c3);
4208 }
4209
4210 int
4211 cap_getc(f)
4212      FILE *f;
4213 {
4214     return hex_getc(':', f, i_cgetc, i_cungetc);
4215 }
4216
4217 int
4218 cap_ungetc(c, f)
4219      int c;
4220      FILE *f;
4221 {
4222     return (*i_cungetc)(c, f);
4223 }
4224
4225 int
4226 url_getc(f)
4227      FILE *f;
4228 {
4229     return hex_getc('%', f, i_ugetc, i_uungetc);
4230 }
4231
4232 int
4233 url_ungetc(c, f)
4234      int c;
4235      FILE *f;
4236 {
4237     return (*i_uungetc)(c, f);
4238 }
4239 #endif
4240
4241 #ifdef NUMCHAR_OPTION
4242 int
4243 numchar_getc(f)
4244      FILE *f;
4245 {
4246     int (*g)() = i_ngetc;
4247     int (*u)() = i_nungetc;
4248     int i = 0, j;
4249     int buf[8];
4250     long c = -1;
4251
4252     buf[i] = (*g)(f);
4253     if (buf[i] == '&'){
4254         buf[++i] = (*g)(f);
4255         if (buf[i] == '#'){
4256             c = 0;
4257             buf[++i] = (*g)(f);
4258             if (buf[i] == 'x' || buf[i] == 'X'){
4259                 for (j = 0; j < 5; j++){
4260                     buf[++i] = (*g)(f);
4261                     if (!nkf_isxdigit(buf[i])){
4262                         if (buf[i] != ';'){
4263                             c = -1;
4264                         }
4265                         break;
4266                     }
4267                     c <<= 4;
4268                     c |= hex2bin(buf[i]);
4269                 }
4270             }else{
4271                 for (j = 0; j < 6; j++){
4272                     if (j){
4273                         buf[++i] = (*g)(f);
4274                     }
4275                     if (!nkf_isdigit(buf[i])){
4276                         if (buf[i] != ';'){
4277                             c = -1;
4278                         }
4279                         break;
4280                     }
4281                     c *= 10;
4282                     c += hex2bin(buf[i]);
4283                 }
4284             }
4285         }
4286     }
4287     if (c != -1){
4288         return CLASS_UTF16 | c;
4289     }
4290     while (i > 0){
4291         (*u)(buf[i], f);
4292         --i;
4293     }
4294     return buf[0];
4295 }
4296
4297 int
4298 numchar_ungetc(c, f)
4299      int c;
4300      FILE *f;
4301 {
4302     return (*i_nungetc)(c, f);
4303 }
4304 #endif
4305
4306 #ifdef UNICODE_NORMALIZATION
4307
4308 /* Normalization Form C */
4309 int
4310 nfc_getc(f)
4311      FILE *f;
4312 {
4313     int (*g)() = i_nfc_getc;
4314     int (*u)() = i_nfc_ungetc;
4315     int i=0, j, k=1, lower, upper;
4316     int buf[9];
4317     const int *array = NULL;
4318     extern const struct normalization_pair normalization_table[];
4319     
4320     buf[i] = (*g)(f);
4321     while (k > 0 && ((buf[i] & 0xc0) != 0x80)){
4322         lower=0, upper=NORMALIZATION_TABLE_LENGTH-1;
4323         while (upper >= lower) {
4324             j = (lower+upper) / 2;
4325             array = normalization_table[j].nfd;
4326             for (k=0; k < NORMALIZATION_TABLE_NFD_LENGTH && array[k]; k++){
4327                 if (array[k] != buf[k]){
4328                     array[k] < buf[k] ? (lower = j + 1) : (upper = j - 1);
4329                     k = 0;
4330                     break;
4331                 } else if (k >= i)
4332                     buf[++i] = (*g)(f);
4333             }
4334             if (k > 0){
4335                 array = normalization_table[j].nfc;
4336                 for (i=0; i < NORMALIZATION_TABLE_NFC_LENGTH && array[i]; i++)
4337                     buf[i] = array[i];
4338                 i--;
4339                 break;
4340             }
4341         }
4342         while (i > 0)
4343             (*u)(buf[i--], f);
4344     }
4345     return buf[0];
4346 }
4347
4348 int
4349 nfc_ungetc(c, f)
4350      int c;
4351      FILE *f;
4352 {
4353     return (*i_nfc_ungetc)(c, f);
4354 }
4355 #endif /* UNICODE_NORMALIZATION */
4356
4357
4358 int 
4359 mime_getc(f)
4360 FILE *f;
4361 {
4362     int c1, c2, c3, c4, cc;
4363     int t1, t2, t3, t4, mode, exit_mode;
4364     int lwsp_count;
4365     char *lwsp_buf;
4366     char *lwsp_buf_new;
4367     int lwsp_size = 128;
4368
4369     if (mime_top != mime_last) {  /* Something is in FIFO */
4370         return  Fifo(mime_top++);
4371     }
4372     if (mime_decode_mode==1 ||mime_decode_mode==FALSE) {
4373         mime_decode_mode=FALSE;
4374         unswitch_mime_getc();
4375         return (*i_getc)(f);
4376     }
4377
4378     if (mimebuf_f == FIXED_MIME)
4379         exit_mode = mime_decode_mode;
4380     else
4381         exit_mode = FALSE;
4382     if (mime_decode_mode == 'Q') {
4383         if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
4384 restart_mime_q:
4385         if (c1=='_') return ' ';
4386         if (c1<=' ' || DEL<=c1) {
4387             mime_decode_mode = exit_mode; /* prepare for quit */
4388             return c1;
4389         }
4390         if (c1!='=' && c1!='?') {
4391             return c1;
4392         }
4393                 
4394         mime_decode_mode = exit_mode; /* prepare for quit */
4395         if ((c2 = (*i_mgetc)(f)) == EOF) return (EOF);
4396         if (c1=='?'&&c2=='=' && mimebuf_f != FIXED_MIME) {
4397             /* end Q encoding */
4398             input_mode = exit_mode;
4399             lwsp_count = 0;
4400             lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
4401             if (lwsp_buf==NULL) {
4402                 perror("can't malloc");
4403                 return -1;
4404             }
4405             while ((c1=(*i_getc)(f))!=EOF) {
4406                 switch (c1) {
4407                 case NL:
4408                 case CR:
4409                     if (c1==NL) {
4410                         if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
4411                             i_ungetc(SPACE,f);
4412                             continue;
4413                         } else {
4414                             i_ungetc(c1,f);
4415                         }
4416                         c1 = NL;
4417                     } else {
4418                         if ((c1=(*i_getc)(f))!=EOF && c1 == NL) {
4419                             if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
4420                                 i_ungetc(SPACE,f);
4421                                 continue;
4422                             } else {
4423                                 i_ungetc(c1,f);
4424                             }
4425                             i_ungetc(NL,f);
4426                         } else {
4427                             i_ungetc(c1,f);
4428                         }
4429                         c1 = CR;
4430                     }
4431                     break;
4432                 case SPACE:
4433                 case TAB:
4434                     lwsp_buf[lwsp_count] = c1;
4435                     if (lwsp_count++>lwsp_size){
4436                         lwsp_size *= 2;
4437                         lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
4438                         if (lwsp_buf_new==NULL) {
4439                             free(lwsp_buf);
4440                             lwsp_buf = NULL;
4441                             perror("can't realloc");
4442                             return -1;
4443                         }
4444                         lwsp_buf = lwsp_buf_new;
4445                     }
4446                     continue;
4447                 }
4448                 break;
4449             }
4450             if (lwsp_count > 0) {
4451                 if (c1=='=' && (lwsp_buf[lwsp_count-1]==SPACE||lwsp_buf[lwsp_count-1]==TAB)) {
4452                     lwsp_count = 0;
4453                 } else {
4454                     i_ungetc(c1,f);
4455                     for(lwsp_count--;lwsp_count>0;lwsp_count--)
4456                         i_ungetc(lwsp_buf[lwsp_count],f);
4457                     c1 = lwsp_buf[0];
4458                 }
4459             }
4460             free(lwsp_buf);
4461             lwsp_buf = NULL;
4462             return c1;
4463         }
4464         if (c1=='='&&c2<' ') { /* this is soft wrap */
4465             while((c1 =  (*i_mgetc)(f)) <=' ') {
4466                 if ((c1 = (*i_mgetc)(f)) == EOF) return (EOF);
4467             }
4468             mime_decode_mode = 'Q'; /* still in MIME */
4469             goto restart_mime_q;
4470         }
4471         if (c1=='?') {
4472             mime_decode_mode = 'Q'; /* still in MIME */
4473             (*i_mungetc)(c2,f);
4474             return c1;
4475         }
4476         if ((c3 = (*i_mgetc)(f)) == EOF) return (EOF);
4477         if (c2<=' ') return c2;
4478         mime_decode_mode = 'Q'; /* still in MIME */
4479 #define hex(c)   (('0'<=c&&c<='9')?(c-'0'):\
4480      ('A'<=c&&c<='F')?(c-'A'+10):('a'<=c&&c<='f')?(c-'a'+10):0)
4481         return ((hex(c2)<<4) + hex(c3));
4482     }
4483
4484     if (mime_decode_mode != 'B') {
4485         mime_decode_mode = FALSE;
4486         return (*i_mgetc)(f);
4487     }
4488
4489
4490     /* Base64 encoding */
4491     /* 
4492         MIME allows line break in the middle of 
4493         Base64, but we are very pessimistic in decoding
4494         in unbuf mode because MIME encoded code may broken by 
4495         less or editor's control sequence (such as ESC-[-K in unbuffered
4496         mode. ignore incomplete MIME.
4497     */
4498     mode = mime_decode_mode;
4499     mime_decode_mode = exit_mode;  /* prepare for quit */
4500
4501     while ((c1 = (*i_mgetc)(f))<=' ') {
4502         if (c1==EOF)
4503             return (EOF);
4504     }
4505 mime_c2_retry:
4506     if ((c2 = (*i_mgetc)(f))<=' ') {
4507         if (c2==EOF)
4508             return (EOF);
4509         if (mime_f != STRICT_MIME) goto mime_c2_retry;
4510         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
4511         return c2;
4512     }
4513     if ((c1 == '?') && (c2 == '=')) {
4514         input_mode = ASCII;
4515         lwsp_count = 0;
4516         lwsp_buf = malloc((lwsp_size+5)*sizeof(char));
4517         if (lwsp_buf==NULL) {
4518             perror("can't malloc");
4519             return -1;
4520         }
4521         while ((c1=(*i_getc)(f))!=EOF) {
4522             switch (c1) {
4523             case NL:
4524             case CR:
4525                 if (c1==NL) {
4526                     if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
4527                         i_ungetc(SPACE,f);
4528                         continue;
4529                     } else {
4530                         i_ungetc(c1,f);
4531                     }
4532                     c1 = NL;
4533                 } else {
4534                     if ((c1=(*i_getc)(f))!=EOF) {
4535                         if (c1==SPACE) {
4536                             i_ungetc(SPACE,f);
4537                             continue;
4538                         } else if ((c1=(*i_getc)(f))!=EOF && (c1==SPACE||c1==TAB)) {
4539                             i_ungetc(SPACE,f);
4540                             continue;
4541                         } else {
4542                             i_ungetc(c1,f);
4543                         }
4544                         i_ungetc(NL,f);
4545                     } else {
4546                         i_ungetc(c1,f);
4547                     }
4548                     c1 = CR;
4549                 }
4550                 break;
4551             case SPACE:
4552             case TAB:
4553                 lwsp_buf[lwsp_count] = c1;
4554                 if (lwsp_count++>lwsp_size){
4555                     lwsp_size *= 2;
4556                     lwsp_buf_new = realloc(lwsp_buf, (lwsp_size+5)*sizeof(char));
4557                     if (lwsp_buf_new==NULL) {
4558                         free(lwsp_buf);
4559                         lwsp_buf = NULL;
4560                         perror("can't realloc");
4561                         return -1;
4562                     }
4563                     lwsp_buf = lwsp_buf_new;
4564                 }
4565                 continue;
4566             }
4567             break;
4568         }
4569         if (lwsp_count > 0) {
4570             if (c1=='=' && (lwsp_buf[lwsp_count-1]==SPACE||lwsp_buf[lwsp_count-1]==TAB)) {
4571                 lwsp_count = 0;
4572             } else {
4573                 i_ungetc(c1,f);
4574                 for(lwsp_count--;lwsp_count>0;lwsp_count--)
4575                     i_ungetc(lwsp_buf[lwsp_count],f);
4576                 c1 = lwsp_buf[0];
4577             }
4578         }
4579         free(lwsp_buf);
4580         lwsp_buf = NULL;
4581         return c1;
4582     }
4583 mime_c3_retry:
4584     if ((c3 = (*i_mgetc)(f))<=' ') {
4585         if (c3==EOF)
4586             return (EOF);
4587         if (mime_f != STRICT_MIME) goto mime_c3_retry;
4588         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
4589         return c3;
4590     }
4591 mime_c4_retry:
4592     if ((c4 = (*i_mgetc)(f))<=' ') {
4593         if (c4==EOF)
4594             return (EOF);
4595         if (mime_f != STRICT_MIME) goto mime_c4_retry;
4596         if (mimebuf_f!=FIXED_MIME) input_mode = ASCII;  
4597         return c4;
4598     }
4599
4600     mime_decode_mode = mode; /* still in MIME sigh... */
4601
4602     /* BASE 64 decoding */
4603
4604     t1 = 0x3f & base64decode(c1);
4605     t2 = 0x3f & base64decode(c2);
4606     t3 = 0x3f & base64decode(c3);
4607     t4 = 0x3f & base64decode(c4);
4608     cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03);
4609     if (c2 != '=') {
4610         Fifo(mime_last++) = cc;
4611         cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f);
4612         if (c3 != '=') {
4613             Fifo(mime_last++) = cc;
4614             cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f);
4615             if (c4 != '=') 
4616                 Fifo(mime_last++) = cc;
4617         }
4618     } else {
4619         return c1;
4620     }
4621     return  Fifo(mime_top++);
4622 }
4623
4624 int
4625 mime_ungetc(c,f) 
4626 int   c;
4627 FILE  *f;
4628 {
4629     Fifo(--mime_top) = c;
4630     return c;
4631 }
4632
4633 int
4634 mime_integrity(f,p)
4635     FILE *f;
4636     const unsigned char *p;
4637 {
4638     int c,d;
4639     unsigned int q;
4640     /* In buffered mode, read until =? or NL or buffer full
4641      */
4642     mime_input = mime_top;
4643     mime_last = mime_top;
4644     
4645     while(*p) Fifo(mime_input++) = *p++;
4646     d = 0;
4647     q = mime_input;
4648     while((c=(*i_getc)(f))!=EOF) {
4649         if (((mime_input-mime_top)&MIME_BUF_MASK)==0) {
4650             break;   /* buffer full */
4651         }
4652         if (c=='=' && d=='?') {
4653             /* checked. skip header, start decode */
4654             Fifo(mime_input++) = c;
4655             /* mime_last_input = mime_input; */
4656             mime_input = q; 
4657             switch_mime_getc();
4658             return 1;
4659         }
4660         if (!( (c=='+'||c=='/'|| c=='=' || c=='?' || is_alnum(c))))
4661             break;
4662         /* Should we check length mod 4? */
4663         Fifo(mime_input++) = c;
4664         d=c;
4665     }
4666     /* In case of Incomplete MIME, no MIME decode  */
4667     Fifo(mime_input++) = c;
4668     mime_last = mime_input;     /* point undecoded buffer */
4669     mime_decode_mode = 1;              /* no decode on Fifo last in mime_getc */
4670     switch_mime_getc();         /* anyway we need buffered getc */
4671     return 1;
4672 }
4673
4674 int
4675 base64decode(c)
4676     int            c;
4677 {
4678     int             i;
4679     if (c > '@') {
4680         if (c < '[') {
4681             i = c - 'A';                        /* A..Z 0-25 */
4682         } else {
4683             i = c - 'G'     /* - 'a' + 26 */ ;  /* a..z 26-51 */
4684         }
4685     } else if (c > '/') {
4686         i = c - '0' + '4'   /* - '0' + 52 */ ;  /* 0..9 52-61 */
4687     } else if (c == '+') {
4688         i = '>'             /* 62 */ ;          /* +  62 */
4689     } else {
4690         i = '?'             /* 63 */ ;          /* / 63 */
4691     }
4692     return (i);
4693 }
4694
4695 STATIC const char basis_64[] =
4696    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
4697
4698 STATIC int b64c;
4699 #define MIMEOUT_BUF_LENGTH (60)
4700 char mimeout_buf[MIMEOUT_BUF_LENGTH+1];
4701 int mimeout_buf_count = 0;
4702 int mimeout_preserve_space = 0;
4703 #define itoh4(c)   (c>=10?c+'A'-10:c+'0')
4704
4705 void
4706 open_mime(mode)
4707 int mode;
4708 {
4709     const unsigned char *p;
4710     int i;
4711     int j;
4712     p  = mime_pattern[0];
4713     for(i=0;mime_encode[i];i++) {
4714         if (mode == mime_encode[i]) {
4715             p = mime_pattern[i];
4716             break;
4717         }
4718     }
4719     mimeout_mode = mime_encode_method[i];
4720     
4721     i = 0;
4722     if (base64_count>45) {
4723         if (mimeout_buf_count>0 && nkf_isblank(mimeout_buf[i])){
4724             (*o_mputc)(mimeout_buf[i]);
4725             i++;
4726         }
4727         (*o_mputc)(NL);
4728         (*o_mputc)(SPACE);
4729         base64_count = 1;
4730         if (!mimeout_preserve_space && mimeout_buf_count>0
4731             && (mimeout_buf[i]==SPACE || mimeout_buf[i]==TAB
4732                 || mimeout_buf[i]==CR || mimeout_buf[i]==NL )) {
4733             i++;
4734         }
4735     }
4736     if (!mimeout_preserve_space) {
4737         for (;i<mimeout_buf_count;i++) {
4738             if (mimeout_buf[i]==SPACE || mimeout_buf[i]==TAB
4739                 || mimeout_buf[i]==CR || mimeout_buf[i]==NL ) {
4740                 (*o_mputc)(mimeout_buf[i]);
4741                 base64_count ++;
4742             } else {
4743                 break;
4744             }
4745         }
4746     }
4747     mimeout_preserve_space = FALSE;
4748     
4749     while(*p) {
4750         (*o_mputc)(*p++);
4751         base64_count ++;
4752     }
4753     j = mimeout_buf_count;
4754     mimeout_buf_count = 0;
4755     for (;i<j;i++) {
4756         mime_putc(mimeout_buf[i]);
4757     }
4758 }
4759
4760 void
4761 close_mime()
4762 {
4763     (*o_mputc)('?');
4764     (*o_mputc)('=');
4765     base64_count += 2;
4766     mimeout_mode = 0;
4767 }
4768
4769 void
4770 eof_mime()
4771 {
4772     switch(mimeout_mode) {
4773     case 'Q':
4774     case 'B':
4775         break;
4776     case 2:
4777         (*o_mputc)(basis_64[((b64c & 0x3)<< 4)]);
4778         (*o_mputc)('=');
4779         (*o_mputc)('=');
4780         base64_count += 3;
4781         break;
4782     case 1:
4783         (*o_mputc)(basis_64[((b64c & 0xF) << 2)]);
4784         (*o_mputc)('=');
4785         base64_count += 2;
4786         break;
4787     }
4788     if (mimeout_mode) {
4789         if (mimeout_f!=FIXED_MIME) {
4790             close_mime(); 
4791         } else if (mimeout_mode != 'Q')
4792             mimeout_mode = 'B';
4793     }
4794 }
4795
4796 void
4797 mimeout_addchar(c)
4798     int            c;
4799 {
4800     switch(mimeout_mode) {
4801     case 'Q':
4802         if(c==SPACE){
4803             (*o_mputc)('_');
4804             base64_count++;
4805         } else if (c==CR||c==NL) {
4806             (*o_mputc)(c);
4807             base64_count = 0;
4808         } else if(c<SPACE||c=='='||c=='?'||c=='_'||DEL<=c) {
4809             (*o_mputc)('=');
4810             (*o_mputc)(itoh4(((c>>4)&0xf)));
4811             (*o_mputc)(itoh4((c&0xf)));
4812             base64_count += 3;
4813         } else {
4814             (*o_mputc)(c);
4815             base64_count++;
4816         }
4817         break;
4818     case 'B':
4819         b64c=c;
4820         (*o_mputc)(basis_64[c>>2]);
4821         mimeout_mode=2;
4822         base64_count ++;
4823         break;
4824     case 2:
4825         (*o_mputc)(basis_64[((b64c & 0x3)<< 4) | ((c & 0xF0) >> 4)]);
4826         b64c=c;
4827         mimeout_mode=1;
4828         base64_count ++;
4829         break;
4830     case 1:
4831         (*o_mputc)(basis_64[((b64c & 0xF) << 2) | ((c & 0xC0) >>6)]);
4832         (*o_mputc)(basis_64[c & 0x3F]);
4833         mimeout_mode='B';
4834         base64_count += 2;
4835         break;
4836     default:
4837         (*o_mputc)(c);
4838         base64_count++;
4839         break;
4840     }
4841 }
4842
4843 int mime_lastchar2, mime_lastchar1;
4844
4845 void mime_prechar(c2, c1)
4846      int c2, c1;
4847 {
4848     if (mimeout_mode){
4849         if (c2){
4850             if (base64_count + mimeout_buf_count/3*4> 66){
4851                 (*o_base64conv)(EOF,0);
4852                 (*o_base64conv)(0,NL);
4853                 (*o_base64conv)(0,SPACE);
4854             }
4855         }/*else if (mime_lastchar2){
4856             if (c1 <=DEL && !nkf_isspace(c1)){
4857                 (*o_base64conv)(0,SPACE);
4858             }
4859         }*/
4860     }/*else{
4861         if (c2 && mime_lastchar2 == 0
4862             && mime_lastchar1 && !nkf_isspace(mime_lastchar1)){
4863             (*o_base64conv)(0,SPACE);
4864         }
4865     }*/
4866     mime_lastchar2 = c2;
4867     mime_lastchar1 = c1;
4868 }
4869
4870 void
4871 mime_putc(c)
4872     int            c;
4873 {
4874     int i = 0;
4875     int j = 0;
4876     int lastchar;
4877
4878     if (mimeout_f == FIXED_MIME){
4879         if (mimeout_mode == 'Q'){
4880             if (base64_count > 71){
4881                 if (c!=CR && c!=NL) {
4882                     (*o_mputc)('=');
4883                     (*o_mputc)(NL);
4884                 }
4885                 base64_count = 0;
4886             }
4887         }else{
4888             if (base64_count > 71){
4889                 eof_mime();
4890                 (*o_mputc)(NL);
4891                 base64_count = 0;
4892             }
4893             if (c == EOF) { /* c==EOF */
4894                 eof_mime();
4895             }
4896         }
4897         if (c != EOF) { /* c==EOF */
4898             mimeout_addchar(c);
4899         }
4900         return;
4901     }
4902     
4903     /* mimeout_f != FIXED_MIME */
4904
4905     if (c == EOF) { /* c==EOF */
4906         j = mimeout_buf_count;
4907         mimeout_buf_count = 0;
4908         i = 0;
4909         for (;i<j;i++) {
4910             /*if (nkf_isspace(mimeout_buf[i])){
4911                 break;
4912             }*/
4913             mimeout_addchar(mimeout_buf[i]);
4914         }
4915         eof_mime();
4916         for (;i<j;i++) {
4917             (*o_mputc)(mimeout_buf[i]);
4918             base64_count++;
4919         }
4920         return;
4921     }
4922
4923     if (mimeout_mode=='Q') {
4924         if (c <= DEL && (output_mode==ASCII ||output_mode == ISO8859_1 ) ) {
4925             if (c <= SPACE) {
4926                 close_mime();
4927                 (*o_mputc)(SPACE);
4928                 base64_count++;
4929             }
4930             (*o_mputc)(c);
4931             base64_count++;
4932         }
4933         return;
4934     }
4935
4936     if (mimeout_buf_count > 0){
4937         lastchar = mimeout_buf[mimeout_buf_count - 1];
4938     }else{
4939         lastchar = -1;
4940     }
4941
4942     if (!mimeout_mode) {
4943         if (c <= DEL && (output_mode==ASCII ||output_mode == ISO8859_1)) {
4944             if (nkf_isspace(c)) {
4945                 if (c==CR || c==NL) {
4946                     base64_count=0;
4947                 }
4948                 for (i=0;i<mimeout_buf_count;i++) {
4949                     (*o_mputc)(mimeout_buf[i]);
4950                     if (mimeout_buf[i] == CR || mimeout_buf[i] == NL){
4951                         base64_count = 0;
4952                     }else{
4953                         base64_count++;
4954                     }
4955                 }
4956                 mimeout_buf[0] = c;
4957                 mimeout_buf_count = 1;
4958             }else{
4959                 if (base64_count > 1
4960                     && base64_count + mimeout_buf_count > 76){
4961                     (*o_mputc)(NL);
4962                     base64_count = 0;
4963                     if (!nkf_isspace(mimeout_buf[0])){
4964                         (*o_mputc)(SPACE);
4965                         base64_count++;
4966                     }
4967                 }
4968                 mimeout_buf[mimeout_buf_count++] = c;
4969                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
4970                     open_mime(output_mode);
4971                 }
4972             }
4973             return;
4974         }else{
4975             if (lastchar==CR || lastchar == NL){
4976                 for (i=0;i<mimeout_buf_count;i++) {
4977                     (*o_mputc)(mimeout_buf[i]);
4978                 }
4979                 base64_count = 0;
4980                 mimeout_buf_count = 0;
4981             }
4982             if (lastchar==SPACE) {
4983                 for (i=0;i<mimeout_buf_count-1;i++) {
4984                     (*o_mputc)(mimeout_buf[i]);
4985                     base64_count++;
4986                 }
4987                 mimeout_buf[0] = SPACE;
4988                 mimeout_buf_count = 1;
4989             }
4990             open_mime(output_mode);
4991         }
4992     }else{
4993         /* mimeout_mode == 'B', 1, 2 */
4994         if ( c<=DEL && (output_mode==ASCII ||output_mode == ISO8859_1 ) ) {
4995             if (lastchar == CR || lastchar == NL){
4996                 if (nkf_isblank(c)) {
4997                     for (i=0;i<mimeout_buf_count;i++) {
4998                         mimeout_addchar(mimeout_buf[i]);
4999                     }
5000                     mimeout_buf_count = 0;
5001                 } else if (SPACE<c && c<DEL) {
5002                     eof_mime();
5003                     for (i=0;i<mimeout_buf_count;i++) {
5004                         (*o_mputc)(mimeout_buf[i]);
5005                     }
5006                     base64_count = 0;
5007                     mimeout_buf_count = 0;
5008                 }
5009             }
5010             if (c==SPACE || c==TAB || c==CR || c==NL) {
5011                 for (i=0;i<mimeout_buf_count;i++) {
5012                     if (SPACE<mimeout_buf[i] && mimeout_buf[i]<DEL) {
5013                         eof_mime();
5014                         for (i=0;i<mimeout_buf_count;i++) {
5015                             (*o_mputc)(mimeout_buf[i]);
5016                             base64_count++;
5017                         }
5018                         mimeout_buf_count = 0;
5019                     }
5020                 }
5021                 mimeout_buf[mimeout_buf_count++] = c;
5022                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
5023                     eof_mime();
5024                     for (i=0;i<mimeout_buf_count;i++) {
5025                         (*o_mputc)(mimeout_buf[i]);
5026                         base64_count++;
5027                     }
5028                     mimeout_buf_count = 0;
5029                 }
5030                 return;
5031             }
5032             if (mimeout_buf_count>0 && SPACE<c && c!='=') {
5033                 mimeout_buf[mimeout_buf_count++] = c;
5034                 if (mimeout_buf_count>MIMEOUT_BUF_LENGTH) {
5035                     j = mimeout_buf_count;
5036                     mimeout_buf_count = 0;
5037                     for (i=0;i<j;i++) {
5038                         mimeout_addchar(mimeout_buf[i]);
5039                     }
5040                 }
5041                 return;
5042             }
5043         }
5044     }
5045     if (mimeout_buf_count>0) {
5046         j = mimeout_buf_count;
5047         mimeout_buf_count = 0;
5048         for (i=0;i<j;i++) {
5049             if (mimeout_buf[i]==CR || mimeout_buf[i]==NL)
5050                 break;
5051             mimeout_addchar(mimeout_buf[i]);
5052         }
5053         if (i<j) {
5054             eof_mime();
5055             base64_count=0;
5056             for (;i<j;i++) {
5057                 (*o_mputc)(mimeout_buf[i]);
5058             }
5059             open_mime(output_mode);
5060         }
5061     }
5062     mimeout_addchar(c);
5063 }
5064
5065
5066 #if defined(PERL_XS) || defined(WIN32DLL)
5067 void 
5068 reinit()
5069 {
5070     {
5071         struct input_code *p = input_code_list;
5072         while (p->name){
5073             status_reinit(p++);
5074         }
5075     }
5076     unbuf_f = FALSE;
5077     estab_f = FALSE;
5078     nop_f = FALSE;
5079     binmode_f = TRUE;
5080     rot_f = FALSE;
5081     hira_f = FALSE;
5082     input_f = FALSE;
5083     alpha_f = FALSE;
5084     mime_f = STRICT_MIME;
5085     mime_decode_f = FALSE;
5086     mimebuf_f = FALSE;
5087     broken_f = FALSE;
5088     iso8859_f = FALSE;
5089     mimeout_f = FALSE;
5090 #if defined(MSDOS) || defined(__OS2__)
5091      x0201_f = TRUE;
5092 #else
5093      x0201_f = NO_X0201;
5094 #endif
5095     iso2022jp_f = FALSE;
5096 #if defined(UTF8_OUTPUT_ENABLE) && defined(UTF8_INPUT_ENABLE)
5097     internal_unicode_f = FALSE;
5098 #endif
5099 #ifdef UTF8_OUTPUT_ENABLE
5100     unicode_bom_f = 0;
5101     w_oconv16_LE = 0;
5102     ms_ucs_map_f = FALSE;
5103     encode_fallback = NULL;
5104     unicode_subchar  = '?';
5105 #endif
5106 #ifdef UNICODE_NORMALIZATION
5107     nfc_f = FALSE;
5108 #endif
5109 #ifdef INPUT_OPTION
5110     cap_f = FALSE;
5111     url_f = FALSE;
5112     numchar_f = FALSE;
5113 #endif
5114 #ifdef CHECK_OPTION
5115     noout_f = FALSE;
5116     debug_f = FALSE;
5117 #endif
5118     guess_f = FALSE;
5119     is_inputcode_mixed = FALSE;
5120     is_inputcode_set   = FALSE;
5121 #ifdef EXEC_IO
5122     exec_f = 0;
5123 #endif
5124 #ifdef SHIFTJIS_CP932
5125     cp51932_f = TRUE;
5126     cp932inv_f = TRUE;
5127 #endif
5128 #ifdef X0212_ENABLE
5129     x0212_f = FALSE;
5130 #endif
5131     {
5132         int i;
5133         for (i = 0; i < 256; i++){
5134             prefix_table[i] = 0;
5135         }
5136     }
5137 #ifdef UTF8_INPUT_ENABLE
5138     utf16_mode = UTF16BE_INPUT;
5139 #endif
5140     mimeout_buf_count = 0;
5141     mimeout_mode = 0;
5142     base64_count = 0;
5143     f_line = 0;
5144     f_prev = 0;
5145     fold_preserve_f = FALSE;
5146     fold_f = FALSE;
5147     fold_len = 0;
5148     kanji_intro = DEFAULT_J;
5149     ascii_intro = DEFAULT_R;
5150     fold_margin  = FOLD_MARGIN;
5151     output_conv = DEFAULT_CONV;
5152     oconv = DEFAULT_CONV;
5153     o_zconv = no_connection;
5154     o_fconv = no_connection;
5155     o_crconv = no_connection;
5156     o_rot_conv = no_connection;
5157     o_hira_conv = no_connection;
5158     o_base64conv = no_connection;
5159     o_iso2022jp_check_conv = no_connection;
5160     o_putc = std_putc;
5161     i_getc = std_getc;
5162     i_ungetc = std_ungetc;
5163     i_bgetc = std_getc;
5164     i_bungetc = std_ungetc;
5165     o_mputc = std_putc;
5166     i_mgetc = std_getc;
5167     i_mungetc  = std_ungetc;
5168     i_mgetc_buf = std_getc;
5169     i_mungetc_buf = std_ungetc;
5170     output_mode = ASCII;
5171     input_mode =  ASCII;
5172     shift_mode =  FALSE;
5173     mime_decode_mode = FALSE;
5174     file_out = FALSE;
5175     crmode_f = 0;
5176     option_mode = 0;
5177     broken_counter = 0;
5178     broken_last = 0;
5179     z_prev2=0,z_prev1=0;
5180 #ifdef CHECK_OPTION
5181     iconv_for_check = 0;
5182 #endif
5183     input_codename = "";
5184 #ifdef WIN32DLL
5185     reinitdll();
5186 #endif /*WIN32DLL*/
5187 }
5188 #endif
5189
5190 void 
5191 no_connection(c2,c1) 
5192 int c2,c1;
5193 {
5194     no_connection2(c2,c1,0);
5195 }
5196
5197 int
5198 no_connection2(c2,c1,c0) 
5199 int c2,c1,c0;
5200 {
5201     fprintf(stderr,"nkf internal module connection failure.\n");
5202     exit(1);
5203     return 0; /* LINT */
5204 }
5205
5206 #ifndef PERL_XS
5207 #ifdef WIN32DLL
5208 #define fprintf dllprintf
5209 #endif
5210 void 
5211 usage()   
5212 {
5213     fprintf(stderr,"USAGE:  nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n");
5214     fprintf(stderr,"Flags:\n");
5215     fprintf(stderr,"b,u      Output is buffered (DEFAULT),Output is unbuffered\n");
5216 #ifdef DEFAULT_CODE_SJIS
5217     fprintf(stderr,"j,s,e,w  Outout code is JIS 7 bit, Shift JIS (DEFAULT), AT&T JIS (EUC), UTF-8N\n");
5218 #endif
5219 #ifdef DEFAULT_CODE_JIS
5220     fprintf(stderr,"j,s,e,w  Outout code is JIS 7 bit (DEFAULT), Shift JIS, AT&T JIS (EUC), UTF-8N\n");
5221 #endif
5222 #ifdef DEFAULT_CODE_EUC
5223     fprintf(stderr,"j,s,e,w  Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC) (DEFAULT), UTF-8N\n");
5224 #endif
5225 #ifdef DEFAULT_CODE_UTF8
5226     fprintf(stderr,"j,s,e,w  Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC), UTF-8N (DEFAULT)\n");
5227 #endif
5228 #ifdef UTF8_OUTPUT_ENABLE
5229     fprintf(stderr,"         After 'w' you can add more options. (80?|16((B|L)0?)?) \n");
5230 #endif
5231     fprintf(stderr,"J,S,E,W  Input assumption is JIS 7 bit , Shift JIS, AT&T JIS (EUC), UTF-8\n");
5232 #ifdef UTF8_INPUT_ENABLE
5233     fprintf(stderr,"         After 'W' you can add more options. (8|16(B|L)?) \n");
5234 #endif
5235     fprintf(stderr,"t        no conversion\n");
5236     fprintf(stderr,"i_/o_    Output sequence to designate JIS-kanji/ASCII (DEFAULT B)\n");
5237     fprintf(stderr,"r        {de/en}crypt ROT13/47\n");
5238     fprintf(stderr,"h        1 katakana->hiragana, 2 hiragana->katakana, 3 both\n");
5239     fprintf(stderr,"v        Show this usage. V: show version\n");
5240     fprintf(stderr,"m[BQN0]  MIME decode [B:base64,Q:quoted,N:non-strict,0:no decode]\n");
5241     fprintf(stderr,"M[BQ]    MIME encode [B:base64 Q:quoted]\n");
5242     fprintf(stderr,"l        ISO8859-1 (Latin-1) support\n");
5243     fprintf(stderr,"f/F      Folding: -f60 or -f or -f60-10 (fold margin 10) F preserve nl\n");
5244     fprintf(stderr,"Z[0-3]   Convert X0208 alphabet to ASCII  1: Kankaku to space,2: 2 spaces,\n");
5245     fprintf(stderr,"                                          3: Convert HTML Entity\n");
5246     fprintf(stderr,"X,x      Assume X0201 kana in MS-Kanji, -x preserves X0201\n");
5247     fprintf(stderr,"B[0-2]   Broken input  0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");
5248 #ifdef MSDOS
5249     fprintf(stderr,"T        Text mode output\n");
5250 #endif
5251     fprintf(stderr,"O        Output to File (DEFAULT 'nkf.out')\n");
5252     fprintf(stderr,"d,c      Delete \\r in line feed and \\032, Add \\r in line feed\n");
5253     fprintf(stderr,"I        Convert non ISO-2022-JP charactor to GETA\n");
5254     fprintf(stderr,"-L[uwm]  line mode u:LF w:CRLF m:CR (DEFAULT noconversion)\n");
5255     fprintf(stderr,"long name options\n");
5256     fprintf(stderr," --fj,--unix,--mac,--windows                        convert for the system\n");
5257     fprintf(stderr," --jis,--euc,--sjis,--utf8,--utf16,--mime,--base64  convert for the code\n");
5258     fprintf(stderr," --hiragana, --katakana    Hiragana/Katakana Conversion\n");
5259     fprintf(stderr," --x0212                   Convert JISX0212\n");
5260     fprintf(stderr," --cp932, --no-cp932       CP932 compatibility\n");
5261     fprintf(stderr," --prefix=    Insert escape before troublesome characters of Shift_JIS\n");
5262 #ifdef INPUT_OPTION
5263     fprintf(stderr," --cap-input, --url-input  Convert hex after ':' or '%%'\n");
5264 #endif
5265 #ifdef NUMCHAR_OPTION
5266     fprintf(stderr," --numchar-input   Convert Unicode Character Reference\n");
5267 #endif
5268 #ifdef UNICODE_NORMALIZATION
5269     fprintf(stderr," --utf8mac-input   UTF-8-MAC input\n");
5270 #endif
5271 #ifdef UTF8_INPUT_ENABLE
5272     fprintf(stderr," --fb-{skip, html, xml, perl, java, subchar}\n");
5273     fprintf(stderr,"                   set the way nkf handles unassigned characters\n");
5274 #endif
5275 #ifdef UTF8_OUTPUT_ENABLE
5276     fprintf(stderr," --ms-ucs-map      Microsoft UCS Mapping Compatible\n");
5277 #endif
5278 #ifdef OVERWRITE
5279     fprintf(stderr," --overwrite       Overwrite original listed files by filtered result\n");
5280 #endif
5281     fprintf(stderr," -g, --guess       Guess the input code\n");
5282     fprintf(stderr," --help,--version\n");
5283     version();
5284 }
5285
5286 void
5287 version()
5288 {
5289     fprintf(stderr,"Network Kanji Filter Version %s (%s) "
5290 #if defined(MSDOS) && !defined(__WIN32__) && !defined(__WIN16__)
5291                   "for DOS"
5292 #endif
5293 #if defined(MSDOS) && defined(__WIN16__)
5294                   "for Win16"
5295 #endif
5296 #if defined(MSDOS) && defined(__WIN32__)
5297                   "for Win32"
5298 #endif
5299 #ifdef __OS2__
5300                   "for OS/2"
5301 #endif
5302                   ,NKF_VERSION,NKF_RELEASE_DATE);
5303     fprintf(stderr,"\n%s\n",CopyRight);
5304 }
5305 #endif /*PERL_XS*/
5306
5307 /**
5308  ** \e$B%Q%C%A@):n<T\e(B
5309  **  void@merope.pleiades.or.jp (Kusakabe Youichi)
5310  **  NIDE Naoyuki <nide@ics.nara-wu.ac.jp>
5311  **  ohta@src.ricoh.co.jp (Junn Ohta)
5312  **  inouet@strl.nhk.or.jp (Tomoyuki Inoue)
5313  **  kiri@pulser.win.or.jp (Tetsuaki Kiriyama)
5314  **  Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp>
5315  **  a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe)
5316  **  kono@ie.u-ryukyu.ac.jp (Shinji Kono)
5317  **  GHG00637@nifty-serve.or.jp (COW)
5318  **
5319  **/
5320
5321 /* end */