📄 nkf.c
字号:
return '\n';/* add one new line before this character */ } return 1; /* default no fold in ASCII */ } else { if(c2=='!') { if(c1=='"') return 1; /* $B!"(B */ if(c1=='#') return 1; /* $B!#(B */ if(c1=='$') return 1; /* $B!$(B */ if(c1=='%') return 1; /* $B!%(B */ if(c1=='\'') return 1; /* $B!\(B */ if(c1=='(') return 1; /* $B!((B */ if(c1==')') return 1; /* $B!)(B */ if(c1=='*') return 1; /* $B!*(B */ if(c1=='+') return 1; /* $B!+(B */ if(c1==',') return 1; /* $B!,(B */ } line = 2; return '\n'; /* add one new line before this character */ }}intpre_convert(c1,c2)int c1,c2;{ if(c2) c1 &= 0x7f; c1_return = c1; if(c2==EOF) return c2; c2 &= 0x7f; if(rot_f) { if(c2) { c1 = rot47(c1); c2 = rot47(c2); } else { if(!(c1 & 0x80)) c1 = rot13(c1); } c1_return = c1; } /* JISX0208 Alphabet */ if(alpha_f && c2 == 0x23 ) return 0; /* JISX0208 Kigou */ if(alpha_f && c2 == 0x21 ) { if(0x21==c1) { if(alpha_f&0x2) { c1_return = ' '; return 0; } else if(alpha_f&0x4) { c1_return = ' '; return DOUBLE_SPACE; } else { return c2; } } else if(0x20<c1 && c1<0x7f && fv[c1-0x20]) { c1_return = fv[c1-0x20]; return 0; } } return c2;}#ifdef STRICT_MIME/* This converts =?ISO-2022-JP?B?HOGE HOGE */unsigned char *mime_pattern[] = { (unsigned char *)"\075?ISO-8859-1?Q?", (unsigned char *)"\075?ISO-2022-JP?B?", (unsigned char *)"\075?ISO-2022-JP?Q?", (unsigned char *)"\075?JAPANESE_EUC?B?", (unsigned char *)"\075?SHIFT_JIS?B?", NULL};int mime_encode[] = { 'Q', 'B', 'Q', 0};#endif#define MAXRECOVER 20int iso8859_f_save;#ifdef STRICT_MIME#define nkf_toupper(c) (('a'<=c && c<='z')?(c-('a'-'A')):c)/* I don't trust portablity of toupper */intmime_begin(f)FILE *f;{ int c1; int i,j,k; unsigned char *p,*q; int r[MAXRECOVER]; /* recovery buffer, max mime pattern lenght */ mime_mode = FALSE; /* =? has been checked */ j = 0; p = mime_pattern[j]; r[0]='='; r[1]='?'; for(i=2;p[i]>' ';i++) { /* start at =? */ if( ((r[i] = c1 = getc(f))==EOF) || nkf_toupper(c1) != p[i] ) { /* pattern fails, try next one */ q = p; while (p = mime_pattern[++j]) { for(k=2;k<i;k++) /* assume length(p) > i */ if(p[k]!=q[k]) break; if(k==i && nkf_toupper(c1)==p[k]) break; } if(p) continue; /* found next one, continue */ /* all fails, output from recovery buffer */ ungetc(c1,f); for(j=0;j<i;j++) { (*oconv)(0,r[j]); } return c1; } } mime_mode = mime_encode[j]; iso8859_f_save = iso8859_f; if(j==0) { iso8859_f = TRUE; } if(mime_mode=='B') { mimebuf_f = unbuf_f; if(!unbuf_f) { /* do MIME integrity check */ return mime_integrity(f,mime_pattern[j]); } } mimebuf_f = TRUE; return c1;}#define mime_getc0(f) (mimebuf_f?getc(f):Fifo(mime_input++))#define mime_ungetc0(c,f) (mimebuf_f?ungetc(c,f):mime_input--)#elseintmime_begin(f)FILE *f;{ int c1; int i,j,k; int r[MAXRECOVER]; /* recovery buffer, max mime pattern lenght */ mime_mode = FALSE; /* =? has been checked */ j = 0; r[0]='='; r[1]='?'; for(i=2;i<MAXRECOVER;i++) { /* start at =? */ /* We accept any charcter type even if it is breaked by new lines */ if( (r[i] = c1 = getc(f))==EOF) break; if(c1=='=') break; if(c1<' '&& c1!='\r' && c1!='\n') break; if(!(('A'<=c1&&c1<='Z')||('a'<=c1&&c1<='z')||('0'<=c1&&c1<='9') ||c1=='_'||c1=='-'||c1=='?')) break; if(c1=='?') { i++; if(!(i<MAXRECOVER) || (r[i] = c1 = getc(f))==EOF) break; if(c1=='b'||c1=='B') { mime_mode = 'B'; } else if(c1=='q'||c1=='Q') { mime_mode = 'Q'; } else { break; } i++; if(!(i<MAXRECOVER) || (r[i] = c1 = getc(f))==EOF) break; if(c1=='?') { break; } else { mime_mode = FALSE; } break; } } if(!mime_mode || c1==EOF || i==MAXRECOVER) { ungetc(c1,f); for(j=0;j<i;j++) { (*oconv)(0,r[j]); } return c1; } iso8859_f_save = iso8859_f; /* do no MIME integrity check */ return c1; /* used only for checking EOF */}#define mime_getc0(f) getc(f)#define mime_ungetc0(c,f) ungetc(c,f)#endifint mime_getc(f)FILE *f;{ int c1, c2, c3, c4, cc; int t1, t2, t3, t4, mode, exit_mode; if(mime_top != mime_last) { /* Something is in FIFO */ return Fifo(mime_top++); } if(mimebuf_f == FIXED_MIME) exit_mode = mime_mode; else exit_mode = FALSE; if(mime_mode == 'Q') { if((c1 = mime_getc0(f)) == EOF) return (EOF); if(c1=='_') return ' '; if(c1!='=' && c1!='?') return c1; mime_mode = exit_mode; /* prepare for quit */ if(c1<=' ') return c1; if((c2 = mime_getc0(f)) == EOF) return (EOF); if(c2<=' ') return c2; if(c1=='?'&&c2=='=') { /* end Q encoding */ input_mode = exit_mode; iso8859_f = iso8859_f_save; return getc(f); } if(c1=='?') { mime_mode = 'Q'; /* still in MIME */ mime_ungetc0(c2,f); return c1; } if((c3 = mime_getc0(f)) == EOF) return (EOF); if(c2<=' ') return c2; mime_mode = 'Q'; /* still in MIME */#define hex(c) (('0'<=c&&c<='9')?(c-'0'):\ ('A'<=c&&c<='F')?(c-'A'+10):('a'<=c&&c<='f')?(c-'a'+10):0) return ((hex(c2)<<4) + hex(c3)); } if(mime_mode != 'B') { mime_mode = FALSE; return getc(f); } /* Base64 encoding */ /* MIME allows line break in the middle of Base64, but we are very pessimistic in decoding in unbuf mode because MIME encoded code may broken by less or editor's control sequence (such as ESC-[-K in unbuffered mode. ignore incomplete MIME. */ mode = mime_mode; mime_mode = exit_mode; /* prepare for quit */ while ((c1 = mime_getc0(f))<=' ') { if(c1==EOF) return (EOF); } if((c2 = mime_getc0(f))<=' ') { if(c2==EOF) return (EOF); if(mimebuf_f!=FIXED_MIME) input_mode = ASCII; return c2; } if((c1 == '?') && (c2 == '=')) { input_mode = ASCII; while((c1 = getc(f))==' ' /* || c1=='\n' || c1=='\r' */); return c1; } if((c3 = mime_getc0(f))<=' ') { if(c3==EOF) return (EOF); if(mimebuf_f!=FIXED_MIME) input_mode = ASCII; return c3; } if((c4 = mime_getc0(f))<=' ') { if(c4==EOF) return (EOF); if(mimebuf_f!=FIXED_MIME) input_mode = ASCII; return c4; } mime_mode = mode; /* still in MIME sigh... */ /* BASE 64 decoding */ t1 = 0x3f & base64decode(c1); t2 = 0x3f & base64decode(c2); t3 = 0x3f & base64decode(c3); t4 = 0x3f & base64decode(c4); cc = ((t1 << 2) & 0x0fc) | ((t2 >> 4) & 0x03); if(c2 != '=') { Fifo(mime_last++) = cc; cc = ((t2 << 4) & 0x0f0) | ((t3 >> 2) & 0x0f); if(c3 != '=') { Fifo(mime_last++) = cc; cc = ((t3 << 6) & 0x0c0) | (t4 & 0x3f); if(c4 != '=') Fifo(mime_last++) = cc; } } else { return c1; } return Fifo(mime_top++);}intmime_ungetc(c) unsigned int c;{ Fifo(mime_last++) = c; return c;}#ifdef STRICT_MIMEintmime_integrity(f,p)FILE *f;unsigned char *p;{ int c,d; unsigned int q; /* In buffered mode, read until =? or NL or buffer fffull */ mime_input = mime_top; mime_last = mime_top; while(*p) Fifo(mime_input++) = *p++; d = 0; q = mime_input; while((c=getc(f))!=EOF) { if(((mime_input-mime_top)&MIME_BUF_MASK)==0) break; if(c=='=' && d=='?') { /* checked. skip header, start decode */ Fifo(mime_input++) = c; mime_input = q; return 1; } if(!( (c=='+'||c=='/'|| c=='=' || c=='?' || ('a'<=c && c<='z')||('A'<= c && c<='Z')||('0'<=c && c<='9')))) break; /* Should we check length mod 4? */ Fifo(mime_input++) = c; d=c; } /* In case of Incomplete MIME, no MIME decode */ Fifo(mime_input++) = c; mime_last = mime_input; /* point undecoded buffer */ mime_mode = 1; /* no decode on Fifo last in mime_getc */ return 1;}#endifintbase64decode(c) int c;{ int i; if(c > '@') if(c < '[') i = c - 'A'; /* A..Z 0-25 */ else i = c - 'G' /* - 'a' + 26 */ ; /* a..z 26-51 */ else if(c > '/') i = c - '0' + '4' /* - '0' + 52 */ ; /* 0..9 52-61 */ else if(c == '+') i = '>' /* 62 */ ; /* + 62 */ else i = '?' /* 63 */ ; /* / 63 */ return (i);}void reinit(){ unbuf_f = FALSE; estab_f = FALSE; nop_f = FALSE; binmode_f = TRUE; rot_f = FALSE; input_f = FALSE; alpha_f = FALSE; mime_f = TRUE; mimebuf_f = FALSE; broken_f = FALSE; iso8859_f = FALSE; x0201_f = TRUE; x0201_f = NO_X0201; fold_f = FALSE; kanji_intro = DEFAULT_J; ascii_intro = DEFAULT_R; oconv = DEFAULT_CONV; output_mode = ASCII; input_mode = ASCII; shift_mode = FALSE; mime_mode = FALSE; file_out = FALSE; add_cr = FALSE; del_cr = FALSE;}#ifndef PERL_XSint usage() { fprintf(stderr,"USAGE: nkf(nkf32,wnkf,nkf2) -[flags] [in file] .. [out file for -O flag]\n"); fprintf(stderr,"Flags:\n"); fprintf(stderr,"b,u Output is bufferred (DEFAULT),Output is unbufferred\n");#ifdef DEFAULT_CODE_SJIS fprintf(stderr,"j,s,e Outout code is JIS 7 bit, Shift JIS (DEFAULT), AT&T JIS (EUC)\n");#endif#ifdef DEFAULT_CODE_JIS fprintf(stderr,"j,s,e Outout code is JIS 7 bit (DEFAULT), Shift JIS, AT&T JIS (EUC)\n");#endif#ifdef DEFAULT_CODE_EUC fprintf(stderr,"j,s,e Outout code is JIS 7 bit, Shift JIS, AT&T JIS (EUC) (DEFAULT)\n");#endif fprintf(stderr,"J,S,E Input assumption is JIS 7 bit , Shift JIS, AT&T JIS (EUC)\n"); fprintf(stderr,"t no conversion\n"); fprintf(stderr,"i_ Output sequence to designate JIS-kanji (DEFAULT B)\n"); fprintf(stderr,"o_ Output sequence to designate ASCII (DEFAULT B)\n"); fprintf(stderr,"r {de/en}crypt ROT13/47\n"); fprintf(stderr,"v Show this usage\n"); fprintf(stderr,"m[BQ0] MIME decode [B:base64,Q:quoted,0:no decode]\n"); fprintf(stderr,"l ISO8859-1 (Latin-1) support\n"); fprintf(stderr,"f Folding: -f60 or -f\n"); fprintf(stderr,"Z[0-2] Convert X0208 alphabet to ASCII 1: Kankaku to space,2: 2 spaces\n"); fprintf(stderr,"X,x Assume X0201 kana in MS-Kanji, -x preserves X0201\n"); fprintf(stderr,"B[0-2] Broken input 0: missing ESC,1: any X on ESC-[($]-X,2: ASCII on NL\n");#ifdef MSDOS fprintf(stderr,"T Text mode output\n");#endif fprintf(stderr,"O Output to File (DEFAULT 'nkf.out')\n"); fprintf(stderr,"d,c Delete \\r in line feed, Add \\r in line feed\n"); fprintf(stderr,"Network Kanji Filter Version %s (%s) "#if defined(MSDOS) && !defined(_Windows) "for DOS"#endif#if !defined(__WIN32__) && defined(_Windows) "for Win16"#endif#if defined(__WIN32__) && defined(_Windows) "for Win32"#endif#ifdef __OS2__ "for OS/2"#endif ,Version,Patchlevel); fprintf(stderr,"\n%s\n",CopyRight); return 0;}#endif/** ** $B%Q%C%A@):n<T(B ** void@merope.pleiades.or.jp (Kusakabe Youichi) ** NIDE Naoyuki <nide@ics.nara-wu.ac.jp> ** ohta@src.ricoh.co.jp (Junn Ohta) ** inouet@strl.nhk.or.jp (Tomoyuki Inoue) ** kiri@pulser.win.or.jp (Tetsuaki Kiriyama) ** Kimihiko Sato <sato@sail.t.u-tokyo.ac.jp> ** a_kuroe@kuroe.aoba.yokohama.jp (Akihiko Kuroe) ** kono@ie.u-ryukyu.ac.jp (Shinji Kono) ** GHG00637@nifty-serve.or.jp (COW) ** ** $B:G=*99?7F|(B ** 1998.11.16 **//* end */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -