File: mkcat.c

package info (click to toggle)
fdclone 3.00k-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,120 kB
  • sloc: ansic: 99,013; makefile: 4,257; sh: 1,432; sed: 196
file content (459 lines) | stat: -rw-r--r-- 9,619 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
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
/*
 *	mkcat.c
 *
 *	message catalog generator
 */

#define	K_EXTERN
#include "headers.h"
#include "kctype.h"
#include "typesize.h"
#include "version.h"

#define	MAXLINEBUF		255
#define	MAXVERBUF		8
#define	MAXCATALOG		2
#define	CF_KANJI		0001
#define	CF_NOSUM		0100
#define	DEFINESTR		"define"
#define	MESCONVSTR		"mesconv2"
#define	MESLISTSTR		"meslist"
#define	CATSUMSTR		"CAT_SUM"
#define	VERSIONSTR		"#version\t"
#define	SUMSTR			"#sum\t"
#define	BEGINSTR		"#begin\t"
#define	ENDSTR			"#end"

static VOID_P NEAR Xrealloc __P_((VOID_P, ALLOC_T));
static char *NEAR fgets2 __P_((FILE *));
static char *NEAR getversion __P_((VOID_A));
static CONST char *NEAR getnum __P_((CONST char *, u_int *));
static int NEAR geteol __P_((CONST char *));
static VOID NEAR putstr __P_((CONST char *, int, FILE *));
static int NEAR mkcat __P_((int, FILE *, FILE *));
static int NEAR addbuf __P_((u_int, CONST char *));
static VOID NEAR cleanbuf __P_((VOID_A));
static int NEAR fputbyte __P_((int, FILE *));
static int NEAR fputword __P_((u_int, FILE *));
static int NEAR cnvcat __P_((FILE *, FILE *));
int main __P_((int, char *CONST []));

static char **meslist = NULL;
static u_int maxmes = (u_int)0;
static CONST char escapechar[] = "abefnrtv";
static CONST char escapevalue[] = {
	0x07, 0x08, 0x1b, 0x0c, 0x0a, 0x0d, 0x09, 0x0b,
};


static VOID_P NEAR Xrealloc(ptr, size)
VOID_P ptr;
ALLOC_T size;
{
	if (!size) return(NULL);
	return((ptr) ? realloc(ptr, size) : malloc(size));
}

static char *NEAR fgets2(fp)
FILE *fp;
{
	char *cp, *tmp, buf[MAXLINEBUF + 1];
	ALLOC_T i, len, size;
	int cont;

	cp = NULL;
	size = (ALLOC_T)0;
	for (;;) {
		if (!fgets(buf, sizeof(buf), fp)) return(cp);
		len = strlen(buf);
		if (!len || buf[len - 1] != '\n') cont = 1;
		else {
			cont = 0;
			len--;
		}

		if (!(tmp = (char *)Xrealloc(cp, size + len + 1))) {
			free(cp);
			return(NULL);
		}
		cp = tmp;
		memcpy(&(cp[size]), buf, len);
		cp[size += len] = '\0';

		if (cont) continue;
		if (!len) break;
		for (i = 0; i < len - 1; i++) if (iskanji1(buf, i)) i++;
		if (i >= len || buf[i] != '\\') break;
		size--;
	}

	return(cp);
}

static char *NEAR getversion(VOID_A)
{
	static char buf[MAXVERBUF];
	char *cp;
	int n;

	if (!(cp = strchr(version, ' '))) cp = version;
	else while (*cp == ' ') cp++;
	memset(buf, '\0', sizeof(buf));
	for (n = 0; cp[n]; n++) {
		if (n >= sizeof(buf) - 1 || cp[n] == ' ') break;
		buf[n] = cp[n];
	}

	return(buf);
}

static CONST char *NEAR getnum(cp, wp)
CONST char *cp;
u_int *wp;
{
	long n;
	char *tmp;

	if (!Xisdigit(*cp)) return(NULL);
	n = strtol(cp, &tmp, 10);
	if (n < 0L || n >= MAXUTYPE(u_int) || !tmp || tmp <= cp) return(NULL);
	if (wp) *wp = (u_int)n;

	return(tmp);
}

static int NEAR geteol(s)
CONST char *s;
{
	int i;

	for (i = 0; s[i]; i++) {
		if (s[i] == '\\') {
			if (!s[++i]) return(-1);
		}
		else if (iskanji1(s, i)) i++;
		else if (s[i] == '"') break;
	}
	if (!s[i]) return(-1);

	return(i);
}

static VOID NEAR putstr(s, len, fp)
CONST char *s;
int len;
FILE *fp;
{
	int i, c, n;

	for (i = 0; i < len; i++) {
		if (iskanji1(s, i)) {
			fputc(s[i], fp);
			fputc(s[++i], fp);
		}
		else if (s[i] != '\\') fputc(s[i], fp);
		else {
			i++;
			c = 0;
			for (n = 0; n < 3; n++) {
				if (s[i + n] < '0' || s[i + n] > '7') break;
				c = c * 8 + s[i + n] - '0';
			}
			if (n > 0) i += n - 1;
			else {
				c = s[i];
				for (n = 0; escapechar[n]; n++)
					if (c == escapechar[n]) break;
				if (escapechar[n]) c = escapevalue[n];
			}
			fputc(c, fp);
		}
	}

	fputc('\n', fp);
}

static int NEAR mkcat(col, fpin, fpout)
int col;
FILE *fpin, *fpout;
{
	CONST char *cp, *tmp, *ptr[MAXCATALOG];
	char *buf;
	u_int w;
	int i, c, len[MAXCATALOG];

	fprintf(fpout, "%s%s\n", VERSIONSTR, getversion());
	for (; (buf = fgets2(fpin)); free(buf)) {
		cp = buf;
		if (*(cp++) != '#') continue;
		while (Xisblank(*cp)) cp++;
		if (strncmp(cp, DEFINESTR, strsize(DEFINESTR))) continue;
		cp += strsize(DEFINESTR);
		if (!Xisblank(*cp)) continue;
		for (cp++; Xisblank(*cp); cp++) /*EMPTY*/;
		tmp = cp;
		if (!Xisalpha(*(cp++))) continue;
		while (Xisalnum(*cp) || *cp == '_') cp++;
		if (!Xisblank(*cp)) continue;
		i = cp - tmp;
		for (cp++; Xisblank(*cp); cp++) /*EMPTY*/;
		if (i == strsize(CATSUMSTR) && !strncmp(tmp, CATSUMSTR, i)) {
			if (!(cp = getnum(cp, &w)) || *cp) continue;
			fprintf(fpout, "%s%u\n", SUMSTR, w);
			continue;
		}

		if (!strncmp(cp, MESCONVSTR, strsize(MESCONVSTR)))
			cp += strsize(MESCONVSTR);
		else if (!strncmp(cp, MESLISTSTR, strsize(MESLISTSTR)))
			cp += strsize(MESLISTSTR);
		else continue;

		while (Xisblank(*cp)) cp++;
		if (*(cp++) != '(') continue;
		while (Xisblank(*cp)) cp++;
		if (!(cp = getnum(cp, &w)) || *(cp++) != ',') continue;

		for (i = 0; i < MAXCATALOG; i++) {
			while (Xisblank(*cp)) cp++;
			if (*(cp++) != '"') break;
			if ((len[i] = geteol(cp)) < 0) break;
			ptr[i] = cp;
			cp += len[i] + 1;
			while (Xisblank(*cp)) cp++;
			c = (i < MAXCATALOG - 1) ? ',' : ')';
			if (*(cp++) != c) break;
		}
		if (i < MAXCATALOG) continue;

		fprintf(fpout, "%s%u\n", BEGINSTR, w);
		putstr(ptr[col], len[col], fpout);
		fprintf(fpout, "%s\n", ENDSTR);
	}

	return(0);
}

static int NEAR addbuf(n, s)
u_int n;
CONST char *s;
{
	char *cp, **tmp;
	ALLOC_T len, size;

	if (n >= maxmes) {
		tmp = (char **)Xrealloc(meslist, (n + 1) * sizeof(char *));
		if (!tmp) return(-1);
		meslist = tmp;
		while (maxmes <= n) meslist[maxmes++] = NULL;
	}

	len = strlen(s);
	if (!meslist[n]) {
		size = (ALLOC_T)0;
		cp = (char *)malloc(len + 1);
		if (!cp) return(-1);
	}
	else {
		size = strlen(meslist[n]);
		cp = (char *)Xrealloc(meslist[n], size + 1 + len + 1);
		if (!cp) return(-1);
		cp[size++] = '\n';
	}
	memcpy(&(cp[size]), s, len + 1);
	meslist[n] = cp;

	return(0);
}

static VOID NEAR cleanbuf(VOID_A)
{
	u_int n;

	if (meslist) {
		for (n = 0; n < maxmes; n++)
			if (meslist[n]) free(meslist[n]);
		free(meslist);
	}
	maxmes = (u_int)0;
	meslist = NULL;
}

static int NEAR fputbyte(c, fp)
int c;
FILE *fp;
{
	return((fputc(c, fp) == EOF && ferror(fp)) ? -1 : 0);
}

static int NEAR fputword(w, fp)
u_int w;
FILE *fp;
{
	if (fputbyte((int)(w & 0xff), fp) < 0
	|| fputbyte((int)((w >> 8) & 0xff), fp) < 0)
		return(-1);

	return(0);
}

static int NEAR cnvcat(fpin, fpout)
FILE *fpin, *fpout;
{
	CONST char *cp;
	char *buf, *ver;
	long n;
	u_int i, w, sum, flags;

	ver = NULL;
	w = sum = (u_int)0;
	flags = CF_NOSUM;
	for (; (buf = fgets2(fpin)); free(buf)) {
		cp = buf;
		if (w) {
			if (!strcmp(buf, ENDSTR)) w = (u_int)0;
			else if (addbuf(w, buf) < 0) {
				fprintf(stderr, "cannot allocate memory.\n");
				cleanbuf();
				return(-1);
			}
			else if (!(flags & CF_KANJI)) {
				for (i = 0; buf[i]; i++)
					if (ismsb(buf[i])) break;
				if (buf[i]) flags |= CF_KANJI;
			}
		}
		else if (!strncmp(buf, VERSIONSTR, strsize(VERSIONSTR))) {
			cp += strsize(VERSIONSTR);
			ver = getversion();
			if (strcmp(cp, ver)) {
				fprintf(stderr, "%s: illegal version.\n", cp);
				cleanbuf();
				return(-1);
			}
		}
		else if (!strncmp(buf, SUMSTR, strsize(SUMSTR))) {
			cp += strsize(SUMSTR);
			if (!(cp = getnum(cp, &sum)) || *cp || !sum) {
				fprintf(stderr,
					"%s: illegal check sum.\n", cp);
				cleanbuf();
				return(-1);
			}
			flags &= ~CF_NOSUM;
		}
		else if (!strncmp(buf, BEGINSTR, strsize(BEGINSTR))) {
			cp += strsize(BEGINSTR);
			if (!(cp = getnum(cp, &w)) || *cp || !w) {
				fprintf(stderr, "%s: illegal ID.\n", cp);
				cleanbuf();
				return(-1);
			}
		}
	}

	if (!ver) {
		fprintf(stderr, "no version exists.\n");
		cleanbuf();
		return(-1);
	}
	if (flags & CF_NOSUM) {
		fprintf(stderr, "no check sum exists.\n");
		cleanbuf();
		return(-1);
	}

	if (!buf) {
		if (fwrite(ver, MAXVERBUF, 1, fpout) != 1
		|| fputword(flags, fpout) < 0 || fputword(sum, fpout) < 0
		|| fputword(maxmes, fpout) < 0) {
			fprintf(stderr, "cannot write.\n");
			cleanbuf();
			return(-1);
		}
		n = 0L;
		for (i = 0; i < maxmes; i++) {
			n += (meslist[i]) ? strlen(meslist[i]) : 0;
			n++;
			if (n >= MAXUTYPE(u_short)) {
				fprintf(stderr, "too mary messages.\n");
				cleanbuf();
				return(-1);
			}
			if (fputword(n, fpout) < 0) {
				fprintf(stderr, "cannot write.\n");
				cleanbuf();
				return(-1);
			}
		}
		for (i = 0; i < maxmes; i++) {
			cp = (meslist[i]) ? meslist[i] : "";
			n = strlen(cp);
			if (fwrite(cp, n + 1, 1, fpout) != 1) {
				fprintf(stderr, "cannot write.\n");
				cleanbuf();
				return(-1);
			}
		}
	}

	cleanbuf();
	if (buf) return(-1);
	return(0);
}

int main(argc, argv)
int argc;
char *CONST argv[];
{
	FILE *fpin, *fpout;
	CONST char *cp;
	int n, col;

	col = 0;
	for (n = 1; n < argc; n++) {
		if (argv[n][0] != '-' || !argv[n][1]) break;
		if (argv[n][1] == '-' && !argv[n][2]) {
			n++;
			break;
		}
		if (argv[n][1] == 'c') {
			cp = &(argv[n][2]);
			if (!*cp) cp = argv[++n];
			if (!cp || (col = atoi(cp)) <= 0) {
				n = argc;
				break;
			}
			if (col > MAXCATALOG) {
				fprintf(stderr,
					"%d: column too large.\n", col);
				return(1);
			}
		}
	}

	if (n >= argc || n + 2 < argc) {
		fprintf(stderr,
			"Usage: mkcat [-c <column>] <infile> [<outfile>]\n");
		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], "wb"))) {
		fprintf(stderr, "%s: cannot open.\n", argv[n + 1]);
		VOID_C fclose(fpin);
		return(1);
	}

	if (col) n = mkcat(col - 1, fpin, fpout);
	else n = cnvcat(fpin, fpout);
	VOID_C fclose(fpin);
	VOID_C fclose(fpout);
	if (n < 0) return(1);

	return(0);
}