/* * nkc.c (version 2) * by N.Maruyama and M.Katsurada (mk@math.meiji.ac.jp) * based on nkf.c by I.Ichikawa */ /** Network Kanji Checker. (NKF modifiee) ** Written by I.Ichikawa ** Fujitsu Labs., JH2FKN/1 ** **/ #include #define FALSE 0 #define TRUE 1 #define ESC 0x01b #define SP 0x020 #define AT 0x040 #define DEL 0x07f #define SSP 0x0a0 #define E0 0x0e0 #define FF 0x0ff #define NON_KANJI 0 #define JIS 1 #define AMBIGUOUS 2 #define MS_KANJI 4 #define EUC 8 #define GETC(x) if (((x) = getc(fp)) == EOF) goto L_0 check1(fp) FILE *fp; { register int c1, c2; register int k_shift = FALSE; int code = NON_KANJI; L_1: GETC(c1); /* first byte */ if (c1 > DEL) { /* 8 bit code */ c2 = c1; /* goto 2nd byte */ goto L_2; } else { if ((c1 > SP) && (c1 != DEL)) { /* in case of Roman characters */ if (k_shift) { /* in case of Kanji shifted */ c2 = c1; GETC(c1); if ((c1 <= DEL) || (c1 > SP)) code |= JIS; goto L_0; /* I ignored wrong code */ } /* goto next */ } else { if (c1 == ESC) { /* in case of ESC */ GETC(c1); if (c1 == '$') { GETC(c1); /* * in case of Kanji in ESC sequence */ k_shift = TRUE; } else if (c1 == '(') { GETC(c1); /* * in case of Kanji out ESC sequence */ k_shift = FALSE; } else { /* not kanji ESC */ /* goto next */ } } } /* goto next */ goto L_1; } L_2: /* second byte */ GETC(c1); if ((c2 > SSP) && (c1 > SSP)) if (c2 < E0) { /* it seems to be EUC */ code |= EUC; } else { /* it seems to be ambiguous */ /* goto next */ code |= AMBIGUOUS; goto L_1; } else /* then it is assumed as MS KANJI */ code |= MS_KANJI; L_0: /* epilogue */ printf("NKF will assume that this input "); printf((code & JIS) ? "is JIS kanji" : (code & MS_KANJI) ? "is MS kanji" : (code & EUC) ? "EUC" : "doesn't have any KANJI"); if (code & AMBIGUOUS) printf(" code\twith some ambiguous code\n"); else printf(" code\n"); return (code); } #include #include int main(int argc, char **argv) { int i, error = 0; struct stat sb; FILE *fp; if (argc == 1) check1(stdin); else for (i = 1; i < argc; i++) { if (stat(argv[i], &sb) == 0 && (sb.st_mode & S_IFMT) == S_IFREG) { if ((fp = fopen(argv[i], "r")) == NULL) { error++; break; } printf("%s: ", argv[i]); check1(fp); fclose(fp); } } return error; }