📄 nkf.c
字号:
/** 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: ichikawa@flab.fujitsu.co.jp$B!K(B** Copyright (C) 1996,1998** $BO"Mm@h!'(B $BN05eBg3X>pJs9)3X2J(B $B2OLn(B $B??<#(B mine/X0208 support** $B!J(BE-Mail Address: kono@ie.u-ryukyu.ac.jp$B!K(B** $BO"Mm@h!'(B COW for DOS & Win16 & Win32 & OS/2** $B!J(BE-Mail Address: GHG00637@niftyserve.or.p$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** $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.** You don't have to ask before copying or publishing.** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.***********************************************************************/static char *CopyRight = "Copyright (C) 1987, FUJITSU LTD. (I.Ichikawa),1998 S. Kono, COW";static char *Version = "1.7";static char *Patchlevel = "1/9811/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) ** l Output code is JIS 7bit and ISO8859-1 Latin-1**** m MIME conversion for ISO-2022-JP** i_ Output sequence to designate JIS-kanji (DEFAULT_J)** o_ Output sequence to designate single-byte roman characters (DEFAULT_R)**** 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 ** d Delete \r in line feed ** c Add \r in line feed **//******************************//* $B%G%U%)%k%H$N=PNO%3!<%IA*Br(B *//* Select DEFAULT_CODE */#define DEFAULT_CODE_JIS/* #define DEFAULT_CODE_SJIS *//* #define DEFAULT_CODE_EUC *//******************************/#if (defined(__TURBOC__) || defined(LSI_C)) && !defined(MSDOS)#define MSDOS#endif#ifndef PERL_XS#include <stdio.h>#endif#if defined(MSDOS) || defined(__OS2__) #include <stdlib.h>#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 */#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#include <windows.h>#endif#define FALSE 0#define TRUE 1/* state of output_mode and input_mode */#define ASCII 0#define X0208 1#define X0201 2#define NO_X0201 3#define JIS_INPUT 4#define SJIS_INPUT 5#define LATIN1_INPUT 6#define FIXED_MIME 7#define DOUBLE_SPACE -2#define NL 0x0a#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 HOLD_SIZE 32#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 *//* MIME preprocessor */#define STRICT_MIME /* do stupid strict mime integrity check */#define GETC(p) ((!mime_mode)?getc(p):mime_getc(p))#define UNGETC(c,p) ((!mime_mode)?ungetc(c,p):mime_ungetc(c))#ifdef EASYWIN /*Easy Win */extern POINT _BufferSize;#endif/* function prototype */static int noconvert(FILE *f);static int kanji_convert(FILE *f);static int h_conv(FILE *f,int c2,int c1);static int push_hold_buf(int c2,int c1);static int s_iconv(int c2,int c1);static int e_oconv(int c2,int c1);static int s_oconv(int c2,int c1);static int j_oconv(int c2,int c1);static int line_fold(int c2,int c1);static int pre_convert(int c1,int c2);static int mime_begin(FILE *f);static int mime_getc(FILE *f);static int mime_ungetc(unsigned int c);static int mime_integrity(FILE *f,unsigned char *p);static int base64decode(int c);static int usage(void);static void arguments(char *c);static void reinit();/* buffers */static char stdibuf[IOBUF_SIZE];static 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 input_f = FALSE; /* non fixed input code */static int alpha_f = FALSE; /* convert JIx0208 alphbet to ASCII */static int mime_f = TRUE; /* 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 */#if defined(MSDOS) || defined(__OS2__) static int x0201_f = TRUE; /* Assume JISX0201 kana */#elsestatic int x0201_f = NO_X0201; /* Assume NO JISX0201 */#endif/* X0208 -> ASCII converter */static int c1_return;/* fold parameter */static int line = 0; /* chars in line */static int prev = 0;static int fold_f = FALSE;static int fold_len = 0;/* options */static char kanji_intro = DEFAULT_J, ascii_intro = DEFAULT_R;/* Folding */int line_fold();#define FOLD_MARGIN 10#define DEFAULT_FOLD 60/* 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#endifstatic int (*iconv)(int c2,int c1); /* s_iconv or oconv */static int (*oconv)(int c2,int c1) = DEFAULT_CONV; /* [ejs]_oconv *//* 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_mode = FALSE; /* MIME mode B base64, Q hex *//* X0201 / X0208 conversion tables *//* X0201 kana conversion table *//* 90-9F A0-DF */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 */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,0x00,0x00,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 */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 */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} ;static int file_out = FALSE;static int add_cr = FALSE;static int del_cr = FALSE;static int end_check;#ifndef PERL_XSintmain(argc, argv) int argc; char **argv;{ FILE *fin; char *cp;#ifdef EASYWIN /*Easy Win */ _BufferSize.y = 400;/*Set Scroll Buffer Size*/#endif for (argc--,argv++; (argc > 0) && **argv == '-'; argc--, argv++) { cp = *argv; arguments(cp); } if(iso8859_f && (oconv != j_oconv || !x0201_f )) { fprintf(stderr,"Mixed ISO8859/JISX0201/SJIS/EUC output is not allowed.\n"); exit(1); } 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(*argv++, "r")) == NULL) { perror(*--argv); return(-1); } else {/* reopen file for stdout */ if(file_out == TRUE){ if(argc == 1 ) { if(freopen(*argv++, "w", stdout) == NULL) { perror(*--argv); return (-1); } argc--; } else { if(freopen("nkf.out", "w", stdout) == NULL) { perror(*--argv); 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 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);}#endifvoidarguments(char *cp) { while (*cp) { switch (*cp++) { 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': oconv = j_oconv; continue; case 'e': /* AT&T EUC output */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -