-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathnkf.c
3958 lines (3654 loc) · 104 KB
/
nkf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** Network Kanji Filter. (PDS Version)
************************************************************************
** Copyright (C) 1987, Fujitsu LTD. (Itaru ICHIKAWA)
** $BO"Mm@h!'(B $B!J3t!KIY;NDL8&5f=j!!%=%U%H#38&!!;T@n!!;j(B
** $B!J(BE-Mail Address: [email protected]$B!K(B
** Copyright (C) 1996,1998
** Copyright (C) 2002
** $BO"Mm@h!'(B $BN05eBg3X>pJs9)3X2J(B $B2OLn(B $B??<#(B mime/X0208 support
** $B!J(BE-Mail Address: [email protected]$B!K(B
** $BO"Mm@h!'(B COW for DOS & Win16 & Win32 & OS/2
** $B!J(BE-Mail Address: [email protected]$B!K(B
**
** $B$3$N%=!<%9$N$$$+$J$kJ#<L!$2~JQ!$=$@5$b5vBz$7$^$9!#$?$@$7!"(B
** $B$=$N:]$K$O!"C/$,9W8%$7$?$r<($9$3$NItJ,$r;D$9$3$H!#(B
** $B:FG[I[$d;(;o$NIUO?$J$I$NLd$$9g$o$;$bI,MW$"$j$^$;$s!#(B
** $B1DMxMxMQ$b>e5-$KH?$7$J$$HO0O$G5v2D$7$^$9!#(B
** $B%P%$%J%j$NG[I[$N:]$K$O(Bversion message$B$rJ]B8$9$k$3$H$r>r7o$H$7$^$9!#(B
** $B$3$N%W%m%0%i%`$K$D$$$F$OFC$K2?$NJ]>Z$b$7$J$$!"0-$7$+$i$:!#(B
**
** Everyone is permitted to do anything on this program
** including copying, modifying, improving,
** as long as you don't try to pretend that you wrote it.
** i.e., the above copyright notice has to appear in all copies.
** Binary distribution requires original version messages.
** You don't have to ask before copying, redistribution or publishing.
** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
***********************************************************************/
/***********************************************************************
** UTF-8 $B%5%]!<%H$K$D$$$F(B
** $B=>Mh$N(B nkf $B$HF~$l$+$($F$=$N$^$^;H$($k$h$&$K$J$C$F$$$^$9(B
** nkf -e $B$J$I$H$7$F5/F0$9$k$H!"<+F0H=JL$G(B UTF-8 $B$HH=Dj$5$l$l$P!"(B
** $B$=$N$^$^(B euc-jp $B$KJQ49$5$l$^$9(B
**
** $B$^$@%P%0$,$"$k2DG=@-$,9b$$$G$9!#(B
** ($BFC$K<+F0H=JL!"%3!<%I:.:_!"%(%i!<=hM}7O(B)
**
** $B2?$+LdBj$r8+$D$1$?$i!"(B
** E-Mail: [email protected]
** $B$^$G8fO"Mm$r$*4j$$$7$^$9!#(B
***********************************************************************/
#include "config.h"
static char *CopyRight =
"Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),2000 S. Kono, COW, 2002-2004 Kono, Furukawa";
static char *Version =
"2.0";
static char *Patchlevel =
"4/0401/Shinji Kono";
/*
**
**
**
** USAGE: nkf [flags] [file]
**
** Flags:
** b Output is bufferred (DEFAULT)
** u Output is unbufferred
**
** t no operation
**
** j Outout code is JIS 7 bit (DEFAULT SELECT)
** s Output code is MS Kanji (DEFAULT SELECT)
** e Output code is AT&T JIS (DEFAULT SELECT)
** w Output code is AT&T JIS (DEFAULT SELECT)
** l Output code is JIS 7bit and ISO8859-1 Latin-1
**
** m MIME conversion for ISO-2022-JP
** I Convert non ISO-2022-JP charactor to GETA by Pekoe <[email protected]>
** i_ Output sequence to designate JIS-kanji (DEFAULT_J)
** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)
** M MIME output conversion
**
** r {de/en}crypt ROT13/47
**
** v display Version
**
** T Text mode output (for MS-DOS)
**
** x Do not convert X0201 kana into X0208
** Z Convert X0208 alphabet to ASCII
**
** f60 fold option
**
** m MIME decode
** B try to fix broken JIS, missing Escape
** B[1-9] broken level
**
** O Output to 'nkf.out' file or last file name
** d Delete \r in line feed
** c Add \r in line feed
** -- other long option
** -- ignore following option (don't use with -O )
**
**/
#if (defined(__TURBOC__) || defined(_MSC_VER) || defined(LSI_C) || defined(__MINGW32__)) && !defined(MSDOS)
#define MSDOS
#if (defined(__Win32__) || defined(_WIN32)) && !defined(__WIN32__)
#define __WIN32__
#endif
#endif
#ifdef PERL_XS
#undef OVERWRITE
#endif
#ifndef PERL_XS
#include <stdio.h>
#endif
#if defined(MSDOS) || defined(__OS2__)
#include <fcntl.h>
#include <io.h>
#endif
#ifdef MSDOS
#ifdef LSI_C
#define setbinmode(fp) fsetbin(fp)
#else /* Microsoft C, Turbo C */
#define setbinmode(fp) setmode(fileno(fp), O_BINARY)
#endif
#else /* UNIX,OS/2 */
#define setbinmode(fp)
#endif
#ifdef _IOFBF /* SysV and MSDOS, Windows */
#define setvbuffer(fp, buf, size) setvbuf(fp, buf, _IOFBF, size)
#else /* BSD */
#define setvbuffer(fp, buf, size) setbuffer(fp, buf, size)
#endif
/*Borland C++ 4.5 EasyWin*/
#if defined(__TURBOC__) && defined(_Windows) && !defined(__WIN32__) /*Easy Win */
#define EASYWIN
#ifndef __WIN16__
#define __WIN16__
#endif
#include <windows.h>
#endif
#ifdef OVERWRITE
/* added by [email protected] */
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifndef MSDOS /* UNIX, OS/2 */
#include <unistd.h>
#include <utime.h>
#else
#if defined(_MSC_VER) || defined(__MINGW32__) /* VC++, MinGW */
#include <sys/utime.h>
#elif defined(__TURBOC__) /* BCC */
#include <utime.h>
#elif defined(LSI_C) /* LSI C */
#endif
#endif
#endif
#ifdef INT_IS_SHORT
#define int long
#endif
#define FALSE 0
#define TRUE 1
/* state of output_mode and input_mode
c2 0 means ASCII
X0201
ISO8859_1
X0208
EOF all termination
c1 32bit data
*/
#define ASCII 0
#define X0208 1
#define X0201 2
#define ISO8859_1 8
#define NO_X0201 3
/* Input Assumption */
#define JIS_INPUT 4
#define SJIS_INPUT 5
#define LATIN1_INPUT 6
#define FIXED_MIME 7
#define STRICT_MIME 8
/* MIME ENCODE */
#define ISO2022JP 9
#define JAPANESE_EUC 10
#define SHIFT_JIS 11
#define UTF8 12
#define UTF8_INPUT 13
#define UTF16_INPUT 14
#define UTF16BE_INPUT 15
#define WISH_TRUE 15
/* ASCII CODE */
#define BS 0x08
#define NL 0x0a
#define CR 0x0d
#define ESC 0x1b
#define SPACE 0x20
#define AT 0x40
#define SSP 0xa0
#define DEL 0x7f
#define SI 0x0f
#define SO 0x0e
#define SSO 0x8e
#define is_alnum(c) \
(('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9'))
#define HOLD_SIZE 1024
#define IOBUF_SIZE 16384
#define DEFAULT_J 'B'
#define DEFAULT_R 'B'
#define SJ0162 0x00e1 /* 01 - 62 ku offset */
#define SJ6394 0x0161 /* 63 - 94 ku offset */
#define RANGE_NUM_MAX 18
#define GETA1 0x22
#define GETA2 0x2e
#if defined( UTF8_OUTPUT_ENABLE ) || defined( UTF8_INPUT_ENABLE )
#define sizeof_euc_utf8 94
#define sizeof_euc_to_utf8_1byte 94
#define sizeof_euc_to_utf8_2bytes 94
#define sizeof_utf8_to_euc_C2 64
#define sizeof_utf8_to_euc_E5B8 64
#define sizeof_utf8_to_euc_2bytes 112
#define sizeof_utf8_to_euc_3bytes 112
#endif
/* MIME preprocessor */
#ifdef EASYWIN /*Easy Win */
extern POINT _BufferSize;
#endif
/* function prototype */
#ifdef ANSI_C_PROTOTYPE
#define PROTO(x) x
#define STATIC static
#else
#define PROTO(x) ()
#define STATIC
#endif
struct input_code{
char *name;
int stat;
int score;
int index;
int buf[3];
void (*status_func)PROTO((struct input_code *, int));
int (*iconv_func)PROTO((int c2, int c1, int c0));
int _file_stat;
};
STATIC char *input_codename = "";
STATIC int noconvert PROTO((FILE *f));
STATIC int kanji_convert PROTO((FILE *f));
STATIC int h_conv PROTO((FILE *f,int c2,int c1));
STATIC int push_hold_buf PROTO((int c2));
STATIC void set_iconv PROTO((int f, int (*iconv_func)()));
STATIC int s_iconv PROTO((int c2,int c1,int c0));
STATIC int s2e_conv PROTO((int c2, int c1, int *p2, int *p1));
STATIC int e_iconv PROTO((int c2,int c1,int c0));
#ifdef UTF8_INPUT_ENABLE
STATIC int w2e_conv PROTO((int c2,int c1,int c0,int *p2,int *p1));
STATIC int w_iconv PROTO((int c2,int c1,int c0));
STATIC int w_iconv16 PROTO((int c2,int c1,int c0));
STATIC int w_iconv_common PROTO((int c1,int c0,unsigned short **pp,int psize,int *p2,int *p1));
STATIC int ww16_conv PROTO((int c2, int c1, int c0));
#endif
#ifdef UTF8_OUTPUT_ENABLE
STATIC int e2w_conv PROTO((int c2,int c1));
STATIC void w_oconv PROTO((int c2,int c1));
STATIC void w_oconv16 PROTO((int c2,int c1));
#endif
STATIC void e_oconv PROTO((int c2,int c1));
STATIC void e2s_conv PROTO((int c2, int c1, int *p2, int *p1));
STATIC void s_oconv PROTO((int c2,int c1));
STATIC void j_oconv PROTO((int c2,int c1));
STATIC void fold_conv PROTO((int c2,int c1));
STATIC void cr_conv PROTO((int c2,int c1));
STATIC void z_conv PROTO((int c2,int c1));
STATIC void rot_conv PROTO((int c2,int c1));
STATIC void hira_conv PROTO((int c2,int c1));
STATIC void base64_conv PROTO((int c2,int c1));
STATIC void iso2022jp_check_conv PROTO((int c2,int c1));
STATIC void no_connection PROTO((int c2,int c1));
STATIC int no_connection2 PROTO((int c2,int c1,int c0));
STATIC void code_score PROTO((struct input_code *ptr));
STATIC void code_status PROTO((int c));
STATIC void std_putc PROTO((int c));
STATIC int std_getc PROTO((FILE *f));
STATIC int std_ungetc PROTO((int c,FILE *f));
STATIC int broken_getc PROTO((FILE *f));
STATIC int broken_ungetc PROTO((int c,FILE *f));
STATIC int mime_begin PROTO((FILE *f));
STATIC int mime_getc PROTO((FILE *f));
STATIC int mime_ungetc PROTO((int c,FILE *f));
STATIC int mime_begin_strict PROTO((FILE *f));
STATIC int mime_getc_buf PROTO((FILE *f));
STATIC int mime_ungetc_buf PROTO((int c,FILE *f));
STATIC int mime_integrity PROTO((FILE *f,unsigned char *p));
STATIC int base64decode PROTO((int c));
STATIC void mime_putc PROTO((int c));
STATIC void open_mime PROTO((int c));
STATIC void close_mime PROTO(());
STATIC void usage PROTO(());
STATIC void version PROTO(());
STATIC void options PROTO((unsigned char *c));
#ifdef PERL_XS
STATIC void reinit PROTO(());
#endif
/* buffers */
static unsigned char stdibuf[IOBUF_SIZE];
static unsigned char stdobuf[IOBUF_SIZE];
static unsigned char hold_buf[HOLD_SIZE*2];
static int hold_count;
/* MIME preprocessor fifo */
#define MIME_BUF_SIZE (1024) /* 2^n ring buffer */
#define MIME_BUF_MASK (MIME_BUF_SIZE-1)
#define Fifo(n) mime_buf[(n)&MIME_BUF_MASK]
static unsigned char mime_buf[MIME_BUF_SIZE];
static unsigned int mime_top = 0;
static unsigned int mime_last = 0; /* decoded */
static unsigned int mime_input = 0; /* undecoded */
/* flags */
static int unbuf_f = FALSE;
static int estab_f = FALSE;
static int nop_f = FALSE;
static int binmode_f = TRUE; /* binary mode */
static int rot_f = FALSE; /* rot14/43 mode */
static int hira_f = FALSE; /* hira/kata henkan */
static int input_f = FALSE; /* non fixed input code */
static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */
static int mime_f = STRICT_MIME; /* convert MIME B base64 or Q */
static int mimebuf_f = FALSE; /* MIME buffered input */
static int broken_f = FALSE; /* convert ESC-less broken JIS */
static int iso8859_f = FALSE; /* ISO8859 through */
static int mimeout_f = FALSE; /* base64 mode */
#if defined(MSDOS) || defined(__OS2__)
static int x0201_f = TRUE; /* Assume JISX0201 kana */
#else
static int x0201_f = NO_X0201; /* Assume NO JISX0201 */
#endif
static int iso2022jp_f = FALSE; /* convert ISO-2022-JP */
#ifdef UTF8_OUTPUT_ENABLE
static int w_oconv16_begin_f= 0; /* utf-16 header */
static int w_oconv16_LE = 0; /* utf-16 little endian */
#endif
#ifdef NUMCHAR_OPTION
#define CLASS_MASK 0x0f000000
#define CLASS_UTF16 0x01000000
#endif
#ifdef INPUT_OPTION
static int cap_f = FALSE;
static int (*i_cgetc)PROTO((FILE *)) = std_getc; /* input of cgetc */
static int (*i_cungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int cap_getc PROTO((FILE *f));
STATIC int cap_ungetc PROTO((int c,FILE *f));
static int url_f = FALSE;
static int (*i_ugetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
static int (*i_uungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int url_getc PROTO((FILE *f));
STATIC int url_ungetc PROTO((int c,FILE *f));
static int numchar_f = FALSE;
static int (*i_ngetc)PROTO((FILE *)) = std_getc; /* input of ugetc */
static int (*i_nungetc)PROTO((int c ,FILE *f)) = std_ungetc;
STATIC int numchar_getc PROTO((FILE *f));
STATIC int numchar_ungetc PROTO((int c,FILE *f));
#endif
#ifdef CHECK_OPTION
static int noout_f = FALSE;
STATIC void no_putc PROTO((int c));
static int debug_f = FALSE;
STATIC void debug PROTO((char *str));
#endif
#ifdef EXEC_IO
static int exec_f = 0;
#endif
#ifdef SHIFTJIS_CP932
STATIC int cp932_f = TRUE;
#define CP932_TABLE_BEGIN (0xfa)
#define CP932_TABLE_END (0xfc)
#endif /* SHIFTJIS_CP932 */
STATIC void e_status PROTO((struct input_code *, int));
STATIC void s_status PROTO((struct input_code *, int));
#ifdef UTF8_INPUT_ENABLE
STATIC void w_status PROTO((struct input_code *, int));
STATIC void w16_status PROTO((struct input_code *, int));
static int utf16_mode = UTF16_INPUT;
#endif
struct input_code input_code_list[] = {
{"EUC-JP", 0, 0, 0, {0, 0, 0}, e_status, e_iconv, 0},
{"Shift_JIS", 0, 0, 0, {0, 0, 0}, s_status, s_iconv, 0},
{"UTF-8", 0, 0, 0, {0, 0, 0}, w_status, w_iconv, 0},
{"UTF-16", 0, 0, 0, {0, 0, 0}, w16_status, w_iconv16, 0},
{0}
};
static int mimeout_mode = 0;
static int base64_count = 0;
/* X0208 -> ASCII converter */
/* fold parameter */
static int f_line = 0; /* chars in line */
static int f_prev = 0;
static int fold_preserve_f = FALSE; /* preserve new lines */
static int fold_f = FALSE;
static int fold_len = 0;
/* options */
static unsigned char kanji_intro = DEFAULT_J,
ascii_intro = DEFAULT_R;
/* Folding */
#define FOLD_MARGIN 10
#define DEFAULT_FOLD 60
static int fold_margin = FOLD_MARGIN;
/* converters */
#ifdef DEFAULT_CODE_JIS
# define DEFAULT_CONV j_oconv
#endif
#ifdef DEFAULT_CODE_SJIS
# define DEFAULT_CONV s_oconv
#endif
#ifdef DEFAULT_CODE_EUC
# define DEFAULT_CONV e_oconv
#endif
#ifdef DEFAULT_CODE_UTF8
# define DEFAULT_CONV w_oconv
#endif
/* process default */
static void (*output_conv)PROTO((int c2,int c1)) = DEFAULT_CONV;
static void (*oconv)PROTO((int c2,int c1)) = no_connection;
/* s_iconv or oconv */
static int (*iconv)PROTO((int c2,int c1,int c0)) = no_connection2;
static void (*o_zconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_fconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_crconv)PROTO((int c2,int c1)) = no_connection;
static void (*o_rot_conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_hira_conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_base64conv)PROTO((int c2,int c1)) = no_connection;
static void (*o_iso2022jp_check_conv)PROTO((int c2,int c1)) = no_connection;
/* static redirections */
static void (*o_putc)PROTO((int c)) = std_putc;
static int (*i_getc)PROTO((FILE *f)) = std_getc; /* general input */
static int (*i_ungetc)PROTO((int c,FILE *f)) =std_ungetc;
static int (*i_bgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
static int (*i_bungetc)PROTO((int c ,FILE *f)) = std_ungetc;
static void (*o_mputc)PROTO((int c)) = std_putc ; /* output of mputc */
static int (*i_mgetc)PROTO((FILE *)) = std_getc; /* input of mgetc */
static int (*i_mungetc)PROTO((int c ,FILE *f)) = std_ungetc;
/* for strict mime */
static int (*i_mgetc_buf)PROTO((FILE *)) = std_getc; /* input of mgetc_buf */
static int (*i_mungetc_buf)PROTO((int c,FILE *f)) = std_ungetc;
/* Global states */
static int output_mode = ASCII, /* output kanji mode */
input_mode = ASCII, /* input kanji mode */
shift_mode = FALSE; /* TRUE shift out, or X0201 */
static int mime_decode_mode = FALSE; /* MIME mode B base64, Q hex */
/* X0201 / X0208 conversion tables */
/* X0201 kana conversion table */
/* 90-9F A0-DF */
static
unsigned char cv[]= {
0x21,0x21,0x21,0x23,0x21,0x56,0x21,0x57,
0x21,0x22,0x21,0x26,0x25,0x72,0x25,0x21,
0x25,0x23,0x25,0x25,0x25,0x27,0x25,0x29,
0x25,0x63,0x25,0x65,0x25,0x67,0x25,0x43,
0x21,0x3c,0x25,0x22,0x25,0x24,0x25,0x26,
0x25,0x28,0x25,0x2a,0x25,0x2b,0x25,0x2d,
0x25,0x2f,0x25,0x31,0x25,0x33,0x25,0x35,
0x25,0x37,0x25,0x39,0x25,0x3b,0x25,0x3d,
0x25,0x3f,0x25,0x41,0x25,0x44,0x25,0x46,
0x25,0x48,0x25,0x4a,0x25,0x4b,0x25,0x4c,
0x25,0x4d,0x25,0x4e,0x25,0x4f,0x25,0x52,
0x25,0x55,0x25,0x58,0x25,0x5b,0x25,0x5e,
0x25,0x5f,0x25,0x60,0x25,0x61,0x25,0x62,
0x25,0x64,0x25,0x66,0x25,0x68,0x25,0x69,
0x25,0x6a,0x25,0x6b,0x25,0x6c,0x25,0x6d,
0x25,0x6f,0x25,0x73,0x21,0x2b,0x21,0x2c,
0x00,0x00};
/* X0201 kana conversion table for daguten */
/* 90-9F A0-DF */
static
unsigned char dv[]= {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x25,0x74,
0x00,0x00,0x00,0x00,0x25,0x2c,0x25,0x2e,
0x25,0x30,0x25,0x32,0x25,0x34,0x25,0x36,
0x25,0x38,0x25,0x3a,0x25,0x3c,0x25,0x3e,
0x25,0x40,0x25,0x42,0x25,0x45,0x25,0x47,
0x25,0x49,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x25,0x50,0x25,0x53,
0x25,0x56,0x25,0x59,0x25,0x5c,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};
/* X0201 kana conversion table for han-daguten */
/* 90-9F A0-DF */
static
unsigned char ev[]= {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x25,0x51,0x25,0x54,
0x25,0x57,0x25,0x5a,0x25,0x5d,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00};
/* X0208 kigou conversion table */
/* 0x8140 - 0x819e */
static
unsigned char fv[] = {
0x00,0x00,0x00,0x00,0x2c,0x2e,0x00,0x3a,
0x3b,0x3f,0x21,0x00,0x00,0x27,0x60,0x00,
0x5e,0x00,0x5f,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x2d,0x00,0x2f,
0x5c,0x00,0x00,0x7c,0x00,0x00,0x60,0x27,
0x22,0x22,0x28,0x29,0x00,0x00,0x5b,0x5d,
0x7b,0x7d,0x3c,0x3e,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x2b,0x2d,0x00,0x00,
0x00,0x3d,0x00,0x3c,0x3e,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x24,0x00,0x00,0x25,0x23,0x26,0x2a,0x40,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
} ;
#define CRLF 1
static int file_out = FALSE;
#ifdef OVERWRITE
static int overwrite = FALSE;
#endif
static int crmode_f = 0; /* CR, NL, CRLF */
#ifdef EASYWIN /*Easy Win */
static int end_check;
#endif /*Easy Win */
#ifndef PERL_XS
int
main(argc, argv)
int argc;
char **argv;
{
FILE *fin;
unsigned char *cp;
char *outfname;
char *origfname;
#ifdef EASYWIN /*Easy Win */
_BufferSize.y = 400;/*Set Scroll Buffer Size*/
#endif
for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) {
cp = (unsigned char *)*argv;
options(cp);
#ifdef EXEC_IO
if (exec_f){
int fds[2], pid;
if (pipe(fds) < 0 || (pid = fork()) < 0){
abort();
}
if (pid == 0){
if (exec_f > 0){
close(fds[0]);
dup2(fds[1], 1);
}else{
close(fds[1]);
dup2(fds[0], 0);
}
execvp(argv[1], &argv[1]);
}
if (exec_f > 0){
close(fds[1]);
dup2(fds[0], 0);
}else{
close(fds[0]);
dup2(fds[1], 1);
}
argc = 0;
break;
}
#endif
}
if(x0201_f == WISH_TRUE)
x0201_f = ((!iso2022jp_f)? TRUE : NO_X0201);
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","wb",stdout) == NULL)
return (-1);
#else
setbinmode(stdout);
#endif
if (unbuf_f)
setbuf(stdout, (char *) NULL);
else
setvbuffer(stdout, stdobuf, IOBUF_SIZE);
if (argc == 0) {
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","rb",stdin) == NULL) return (-1);
#else
setbinmode(stdin);
#endif
setvbuffer(stdin, stdibuf, IOBUF_SIZE);
if (nop_f)
noconvert(stdin);
else
kanji_convert(stdin);
} else {
while (argc--) {
if ((fin = fopen((origfname = *argv++), "r")) == NULL) {
perror(*--argv);
return(-1);
} else {
#ifdef OVERWRITE
int fd;
int fd_backup;
#endif
/* reopen file for stdout */
if (file_out == TRUE) {
#ifdef OVERWRITE
if (overwrite){
outfname = malloc(strlen(origfname)
+ strlen(".nkftmpXXXXXX")
+ 1);
if (!outfname){
perror(origfname);
return -1;
}
strcpy(outfname, origfname);
#ifdef MSDOS
{
int i;
for (i = strlen(outfname); i; --i){
if (outfname[i - 1] == '/'
|| outfname[i - 1] == '\\'){
break;
}
}
outfname[i] = '\0';
}
strcat(outfname, "ntXXXXXX");
mktemp(outfname);
fd = open(outfname, O_WRONLY | O_CREAT | O_TRUNC,
S_IREAD | S_IWRITE);
#else
strcat(outfname, ".nkftmpXXXXXX");
fd = mkstemp(outfname);
#endif
if (fd < 0
|| (fd_backup = dup(fileno(stdout))) < 0
|| dup2(fd, fileno(stdout)) < 0
){
perror(origfname);
return -1;
}
}else
#endif
if(argc == 1 ) {
outfname = *argv++;
argc--;
} else {
outfname = "nkf.out";
}
if(freopen(outfname, "w", stdout) == NULL) {
perror (outfname);
return (-1);
}
if (binmode_f == TRUE) {
#ifdef __OS2__
if (freopen("","wb",stdout) == NULL)
return (-1);
#else
setbinmode(stdout);
#endif
}
}
if (binmode_f == TRUE)
#ifdef __OS2__
if (freopen("","rb",fin) == NULL)
return (-1);
#else
setbinmode(fin);
#endif
setvbuffer(fin, stdibuf, IOBUF_SIZE);
if (nop_f)
noconvert(fin);
else
kanji_convert(fin);
fclose(fin);
#ifdef OVERWRITE
if (overwrite) {
struct stat sb;
#if defined(MSDOS) && !defined(__MINGW32__)
time_t tb[2];
#else
struct utimbuf tb;
#endif
fflush(stdout);
close(fd);
if (dup2(fd_backup, fileno(stdout)) < 0){
perror("dup2");
}
if (stat(origfname, &sb)) {
fprintf(stderr, "Can't stat %s\n", origfname);
}
/* $B%Q!<%_%C%7%g%s$rI|85(B */
if (chmod(outfname, sb.st_mode)) {
fprintf(stderr, "Can't set permission %s\n", outfname);
}
/* $B%?%$%`%9%?%s%W$rI|85(B */
#if defined(MSDOS) && !defined(__MINGW32__)
tb[0] = tb[1] = sb.st_mtime;
if (utime(outfname, tb)) {
fprintf(stderr, "Can't set timestamp %s\n", outfname);
}
#else
tb.actime = sb.st_atime;
tb.modtime = sb.st_mtime;
if (utime(outfname, &tb)) {
fprintf(stderr, "Can't set timestamp %s\n", outfname);
}
#endif
#ifdef MSDOS
if (unlink(origfname)){
perror(origfname);
}
#endif
if (rename(outfname, origfname)) {
perror(origfname);
fprintf(stderr, "Can't rename %s to %s\n",
outfname, origfname);
}
free(outfname);
}
#endif
}
}
}
#ifdef EASYWIN /*Easy Win */
if (file_out == FALSE)
scanf("%d",&end_check);
else
fclose(stdout);
#else /* for Other OS */
if (file_out == TRUE)
fclose(stdout);
#endif
return (0);
}
#endif
static
struct {
char *name;
char *alias;
} long_option[] = {
{"base64","jMB"},
{"euc","e"},
{"euc-input","E"},
{"fj","jm"},
{"help","v"},
{"jis","j"},
{"jis-input","J"},
{"mac","sLm"},
{"mime","jM"},
{"mime-input","m"},
{"msdos","sLw"},
{"sjis","s"},
{"sjis-input","S"},
{"unix","eLu"},
{"version","V"},
{"windows","sLw"},
{"hiragana","h1"},
{"katakana","h2"},
{"katakana-hiragana","h3"},
#ifdef UTF8_OUTPUT_ENABLE
{"utf8", "w"},
{"utf16", "w16"},
#endif
#ifdef UTF8_INPUT_ENABLE
{"utf8-input", "W"},
{"utf16-input", "W16"},
#endif
#ifdef OVERWRITE
{"overwrite", ""},
#endif
#ifdef INPUT_OPTION
{"cap-input", ""},
{"url-input", ""},
#endif
#ifdef NUMCHAR_OPTION
{"numchar-input", ""},
#endif
#ifdef CHECK_OPTION
{"no-output", ""},
{"debug", ""},
#endif
#ifdef SHIFTJIS_CP932
{"no-cp932", ""},
#endif
#ifdef EXEC_IO
{"exec-in", ""},
{"exec-out", ""},
#endif
};
static int option_mode;
void
options(cp)
unsigned char *cp;
{
int i;
unsigned char *p;
if (option_mode==1)
return;
if (*cp++ != '-')
return;
while (*cp) {
switch (*cp++) {
case '-': /* literal options */
if (!*cp) { /* ignore the rest of arguments */
option_mode = 1;
return;
}
for (i=0;i<sizeof(long_option)/sizeof(long_option[0]);i++) {
int j;
p = (unsigned char *)long_option[i].name;
for (j=0;*p && *p++ == cp[j];j++);
if (! *p && !cp[j]) break;
}
if (*p) return;
cp = (unsigned char *)long_option[i].alias;
if (!*cp){
#ifdef OVERWRITE
if (strcmp(long_option[i].name, "overwrite") == 0){
file_out = TRUE;
overwrite = TRUE;
continue;
}
#endif
#ifdef INPUT_OPTION
if (strcmp(long_option[i].name, "cap-input") == 0){
cap_f = TRUE;
continue;
}
if (strcmp(long_option[i].name, "url-input") == 0){
url_f = TRUE;
continue;
}
#endif
#ifdef NUMCHAR_OPTION
if (strcmp(long_option[i].name, "numchar-input") == 0){
numchar_f = TRUE;
continue;
}
#endif
#ifdef CHECK_OPTION
if (strcmp(long_option[i].name, "no-output") == 0){
noout_f = TRUE;
continue;
}
if (strcmp(long_option[i].name, "debug") == 0){
debug_f = TRUE;
continue;
}
#endif
#ifdef SHIFTJIS_CP932
if (strcmp(long_option[i].name, "no-cp932") == 0){
cp932_f = FALSE;
continue;
}
#endif
#ifdef EXEC_IO
if (strcmp(long_option[i].name, "exec-in") == 0){
exec_f = 1;
return;
}
if (strcmp(long_option[i].name, "exec-out") == 0){
exec_f = -1;
return;
}
#endif
}
continue;
case 'b': /* buffered mode */
unbuf_f = FALSE;
continue;
case 'u': /* non bufferd mode */
unbuf_f = TRUE;
continue;
case 't': /* transparent mode */
nop_f = TRUE;
continue;
case 'j': /* JIS output */
case 'n':
output_conv = j_oconv;
continue;
case 'e': /* AT&T EUC output */
output_conv = e_oconv;
continue;
case 's': /* SJIS output */
output_conv = s_oconv;