File: kanjicnv.c

package info (click to toggle)
fdclone 3.01b-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 4,772 kB
  • ctags: 8,674
  • sloc: ansic: 100,552; makefile: 4,497; sh: 1,480; sed: 224
file content (358 lines) | stat: -rw-r--r-- 7,042 bytes parent folder | download
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
/*
 *	kanjicnv.c
 *
 *	tiny Kanji code converter
 */

#include "headers.h"
#include "evalopt.h"

#ifdef	WITHUTF8
#define	K_EXTERN
#include "kctype.h"
#endif

#define	ASCII			000
#define	KANA			001
#define	KANJI			002
#define	JKANA			004

#define	K_SJIS			010
#define	K_EUC			020
#define	K_UTF8			040

#define	U2_UDEF			0x3013	/* GETA */
#define	MINUNICODE		0x00a7
#define	MAXUNICODE		0xffe5
#define	MINKANJI		0x8140
#define	MAXKANJI		0xfc4b
#define	getword(s, n)		(((u_short)((s)[(n) + 1]) << 8) | (s)[n])

#ifdef	WITHUTF8
extern CONST u_char unitblbuf[];
extern u_int unitblent;
extern int nftblnum;
extern int nflen;
extern CONST u_char *nftblbuf[];
extern u_int nftblent[];
#endif

static VOID NEAR Xfputc __P_((int, FILE *));
#ifdef	WITHUTF8
static u_int NEAR cnvunicode __P_((u_int));
static VOID NEAR fputucs2 __P_((u_int, FILE *));
static VOID NEAR ucs2normalization __P_((u_int, int, FILE *));
static VOID NEAR convertutf8 __P_((u_int, FILE *));
#endif
static VOID NEAR convert __P_((int, int, FILE *));
static VOID NEAR output __P_((FILE *, int, int));
int main __P_((int, char *CONST []));

static int msboff = 0;
static int prefix = 0;
static int removebs = 0;
static int kanjicode = K_SJIS;
static CONST opt_t optlist[] = {
	{'7', &msboff, 1, NULL},
	{'b', &removebs, 1, NULL},
	{'c', &prefix, 1, NULL},
	{'e', &kanjicode, K_EUC, NULL},
	{'s', &kanjicode, K_SJIS, NULL},
#ifdef	WITHUTF8
	{'u', &kanjicode, K_UTF8, NULL},
#endif
	{'\0', NULL, 0, NULL},
};


static VOID NEAR Xfputc(c, fp)
int c;
FILE *fp;
{
	if (msboff && (c & 0x80)) fprintf(fp, "\\%03o", c);
	else fputc(c, fp);
}

#ifdef	WITHUTF8
static u_int NEAR cnvunicode(wc)
u_int wc;
{
	CONST u_char *cp;
	u_int w, ofs, min, max;

	if (wc < 0x0080) return(wc);
	if (wc >= 0x00a1 && wc <= 0x00df)
		return(0xff00 | (wc - 0x00a1 + 0x61));
	if (wc >= 0x8260 && wc <= 0x8279)
		return(0xff00 | (wc - 0x8260 + 0x21));
	if (wc >= 0x8281 && wc <= 0x829a)
		return(0xff00 | (wc - 0x8281 + 0x41));
	if (wc < MINKANJI || wc > MAXKANJI) return(U2_UDEF);

# if	0
	wc = unifysjis(wc, 0);
# endif

	ofs = min = max = 0;
	cp = unitblbuf;
	for (ofs = 0; ofs < unitblent; ofs++) {
		w = getword(cp, 2);
		if (wc == w) return(getword(cp, 0));
		cp += 4;
	}

	return(U2_UDEF);
}

static VOID NEAR fputucs2(wc, fp)
u_int wc;
FILE *fp;
{
	if (wc < 0x80) Xfputc(wc, fp);
	else if (wc < 0x800) {
		Xfputc(0xc0 | (wc >> 6), fp);
		Xfputc(0x80 | (wc & 0x3f), fp);
	}
	else {
		Xfputc(0xe0 | (wc >> 12), fp);
		Xfputc(0x80 | ((wc >> 6) & 0x3f), fp);
		Xfputc(0x80 | (wc & 0x3f), fp);
	}
}

static VOID NEAR ucs2normalization(wc, nf, fp)
u_int wc;
int nf;
FILE *fp;
{
	CONST u_char *cp;
	u_int w, ofs, ent;
	int n;

	if (nf <= 0 || nf > nftblnum || wc < MINUNICODE || wc > MAXUNICODE) {
		cp = NULL;
		ofs = ent = (u_int)0;
	}
	else {
		n = 2 + nflen * 2;
		cp = nftblbuf[nf - 1];
		ent = nftblent[nf - 1];
		for (ofs = 0; ofs < ent; ofs++) {
			w = getword(cp, 0);
			if (wc == w) break;
			cp += n;
		}
	}

	if (ofs >= ent) fputucs2(wc, fp);
	else for (n = 0; n < nflen; n++) {
		cp += 2;
		w = getword(cp, 0);
		if (!w) break;
		fputucs2(w, fp);;
	}
}

static VOID NEAR convertutf8(wc, fp)
u_int wc;
FILE *fp;
{
	ucs2normalization(cnvunicode(wc), WITHUTF8, fp);
}
#endif	/* WITHUTF8 */

static VOID NEAR convert(j1, j2, fp)
int j1, j2;
FILE *fp;
{
	int c1, c2;

	c1 = c2 = '\0';
	if (kanjicode == K_EUC) {
		if (j1) {
			c1 = (j1 | 0x80);
			c2 = (j2 | 0x80);
		}
		else if (j2 <= 0x20 || j2 >= 0x60) c2 = j2;
		else {
			c1 = 0x8e;
			c2 = (j2 | 0x80);
		}
	}
	else {
		if (j1) {
			c1 = ((j1 - 1) >> 1) + ((j1 < 0x5f) ? 0x71 : 0xb1);
			c2 = j2 + ((j1 & 1)
				? ((j2 < 0x60) ? 0x1f : 0x20) : 0x7e);
		}
		else if (j2 <= 0x20 || j2 >= 0x60) c2 = j2;
		else c2 = (j2 | 0x80);
#ifdef	WITHUTF8
		if (kanjicode == K_UTF8) {
			convertutf8(((u_int)c1 << 8) | c2, fp);
			c1 = c2 = '\0';
		}
#endif
	}

	if (c1) Xfputc(c1, fp);
	if (c2) Xfputc(c2, fp);
	if (c2 == '\\' && (prefix || msboff)) Xfputc('\\', fp);
}

static VOID NEAR output(fp, c, mode)
FILE *fp;
int c, mode;
{
	static u_char buf[2];
	static int kanji1 = 0;
	static int bufp = 0;

	if (!fp) {
		bufp = kanji1 = 0;
		return;
	}

	if (!bufp) /*EMPTY*/;
	else if (bufp > 1) {
		convert(buf[0], buf[1], fp);
		bufp = 0;
	}
	else if (kanji1 & (KANA | JKANA)) {
		convert(0, buf[0], fp);
		bufp = 0;
	}
	else if (!(kanji1 & KANJI)) {
		Xfputc(buf[0], fp);
		bufp = 0;
	}
	kanji1 = mode;

	if (c != EOF) buf[bufp++] = c;
}

int main(argc, argv)
int argc;
char *CONST argv[];
{
	FILE *fpin, *fpout;
	int n, c, mode, esc, kanji;

	initopt(optlist);
	n = evalopt(argc, argv, optlist);
	if (n >= argc || n + 2 < argc) {
		optusage(argv[0], "<infile> [<outfile>]", optlist);
		return(1);
	}

	if (!strcmp(argv[n], "-")) fpin = stdin;
	else if (!(fpin = fopen(argv[n], "r"))) {
		fprintf(stderr, "%s: cannot open.\n", argv[n]);
		return(1);
	}
	if (n + 1 >= argc) fpout = stdout;
	else if (!(fpout = fopen(argv[n + 1], "w"))) {
		fprintf(stderr, "%s: cannot open.\n", argv[n + 1]);
		VOID_C fclose(fpin);
		return(1);
	}

	mode = ASCII;
	esc = kanji = 0;

	while ((c = fgetc(fpin)) != EOF) {
		switch (c) {
			case '\033':	/* ESC */
				if (esc) output(fpout, '\033', mode);
				else if (kanji > 0) {
					output(fpout, '\033', mode);
					output(fpout, '$', mode);
				}
				else if (kanji < 0) {
					output(fpout, '\033', mode);
					output(fpout, '(', mode);
				}
				esc = 1;
				kanji = 0;
				break;
			case '$':
				if (!esc) {
					if (!kanji) output(fpout, c, mode);
				}
				else {
					mode &= ~JKANA;
					esc = 0;
					kanji = 1;
				}
				break;
			case '(':
				if (!esc) {
					if (!kanji) output(fpout, c, mode);
				}
				else {
					mode &= ~JKANA;
					esc = 0;
					kanji = -1;
				}
				break;
			case '\016':	/* SO */
				if (esc) output(fpout, '\033', mode);
				else if (kanji > 0) {
					output(fpout, '\033', mode);
					output(fpout, '$', mode);
				}
				else if (kanji < 0) {
					output(fpout, '\033', mode);
					output(fpout, '(', mode);
				}
				mode |= KANA;
				esc = kanji = 0;
				break;
			case '\017':	/* SI */
				if (esc) output(fpout, '\033', mode);
				else if (kanji > 0) {
					output(fpout, '\033', mode);
					output(fpout, '$', mode);
				}
				else if (kanji < 0) {
					output(fpout, '\033', mode);
					output(fpout, '(', mode);
				}
				mode &= ~KANA;
				esc = kanji = 0;
				break;
			case '\b':
				if (removebs) {
					if (esc) output(fpout, '\033', mode);
					else if (kanji > 0) {
						output(fpout, '\033', mode);
						output(fpout, '$', mode);
					}
					else if (kanji < 0) {
						output(fpout, '\033', mode);
						output(fpout, '(', mode);
					}
					output(NULL, EOF, mode);
					esc = kanji = 0;
					break;
				}
/*FALLTHRU*/
			default:
				if (esc) output(fpout, '\033', mode);
				else if (kanji > 0) mode |= KANJI;
				else if (kanji < 0) {
					if (c == 'I') mode |= JKANA;
					else mode &= ~KANJI;
				}
				else output(fpout, c, mode);
				esc = kanji = 0;
				break;
		}
	}
	output(fpout, EOF, mode);

	VOID_C fclose(fpout);
	VOID_C fclose(fpin);

	return(0);
}