summaryrefslogtreecommitdiffstats
path: root/usr.bin/colldef/parse.y
blob: e362bb027338c2e0be4d02339c523f7dc1d40a60 (plain)
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
%{
/*-
 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
 *		at Electronni Visti IA, Kiev, Ukraine.
 *			All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <arpa/inet.h>
#include <err.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sysexits.h>
#include "collate.h"
#include "common.h"

extern FILE *yyin;
void yyerror(const char *fmt, ...) __printflike(1, 2);
int yyparse(void);
int yylex(void);
static void usage(void);
static void collate_print_tables(void);

char map_name[FILENAME_MAX] = ".";
char curr_chain[STR_LEN];

char __collate_version[STR_LEN];
u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];

#undef __collate_substitute_table
u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
#undef __collate_char_pri_table
struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
struct __collate_st_chain_pri *__collate_chain_pri_table;

int chain_index;
int prim_pri = 1, sec_pri = 1;
#ifdef COLLATE_DEBUG
int debug;
#endif

const char *out_file = "LC_COLLATE";
%}
%union {
	u_char ch;
	u_char str[BUFSIZE];
}
%token SUBSTITUTE WITH ORDER RANGE
%token <str> STRING
%token <str> DEFN
%token <ch> CHAR
%%
collate : statment_list
;
statment_list : statment
	| statment_list '\n' statment
;
statment :
	| charmap
	| substitute
	| order
;
charmap : DEFN CHAR {
	if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN)
		yyerror("Charmap symbol name '%s' is too long", $1);
	strcpy(charmap_table[$2], $1);
}
;
substitute : SUBSTITUTE CHAR WITH STRING {
	if ($2 == '\0')
		yyerror("NUL character can't be substituted");
	if (strchr($4, $2) != NULL)
		yyerror("Char 0x%02x substitution is recursive", $2);
	if (strlen($4) + 1 > STR_LEN)
		yyerror("Char 0x%02x substitution is too long", $2);
	strcpy(__collate_substitute_table[$2], $4);
}
;
order : ORDER order_list {
	FILE *fp;
	int ch, substed, ordered;
	uint32_t u32;

	for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
		substed = (__collate_substitute_table[ch][0] != ch);
		ordered = !!__collate_char_pri_table[ch].prim;
		if (!ordered && !substed)
			yyerror("Char 0x%02x not found", ch);
		if (substed && ordered)
			yyerror("Char 0x%02x can't be ordered since substituted", ch);
	}

	if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
	     sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
		yyerror("can't grow chain table");
	(void)memset(__collate_chain_pri_table[chain_index].str, 0,
		     sizeof(__collate_chain_pri_table[0].str));
	__collate_chain_pri_table[chain_index].prim = 0;
	__collate_chain_pri_table[chain_index].sec = 0;
	chain_index++;

	if ((fp = fopen(out_file, "w")) == NULL)
		err(EX_UNAVAILABLE, "can't open destination file %s",
		    out_file);

	strcpy(__collate_version, COLLATE_VERSION1_1);
	if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
		err(EX_IOERR,
		"IO error writting collate version to destination file %s",
		    out_file);
	u32 = htonl(chain_index);
	if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
		err(EX_IOERR,
		"IO error writting chains number to destination file %s",
		    out_file);
	if (fwrite(__collate_substitute_table,
		   sizeof(__collate_substitute_table), 1, fp) != 1)
		err(EX_IOERR,
		"IO error writting substitute table to destination file %s",
		    out_file);
	if (fwrite(__collate_char_pri_table,
		   sizeof(__collate_char_pri_table), 1, fp) != 1)
		err(EX_IOERR,
		"IO error writting char table to destination file %s",
		    out_file);
	if (fwrite(__collate_chain_pri_table,
		   sizeof(*__collate_chain_pri_table), chain_index, fp) !=
		   (size_t)chain_index)
		err(EX_IOERR,
		"IO error writting chain table to destination file %s",
		    out_file);
	if (fclose(fp) != 0)
		err(EX_IOERR, "IO error closing destination file %s",
		    out_file);
#ifdef COLLATE_DEBUG
	if (debug)
		collate_print_tables();
#endif
	exit(EX_OK);
}
;
order_list : item
	| order_list ';' item
;
chain : CHAR CHAR {
	curr_chain[0] = $1;
	curr_chain[1] = $2;
	if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
		yyerror("\\0 can't be chained");
	curr_chain[2] = '\0';
}
	| chain CHAR {
	static char tb[2];

	tb[0] = $2;
	if (tb[0] == '\0')
		yyerror("\\0 can't be chained");
	if (strlen(curr_chain) + 2 > STR_LEN)
		yyerror("Chain '%s' grows too long", curr_chain);
	(void)strcat(curr_chain, tb);
}
;
item :  CHAR {
	if (__collate_char_pri_table[$1].prim)
		yyerror("Char 0x%02x duplicated", $1);
	__collate_char_pri_table[$1].prim = prim_pri++;
}
	| chain {
	if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
	     sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
		yyerror("can't grow chain table");
	(void)memset(__collate_chain_pri_table[chain_index].str, 0,
		     sizeof(__collate_chain_pri_table[0].str));
	(void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
	__collate_chain_pri_table[chain_index].prim = prim_pri++;
	__collate_chain_pri_table[chain_index].sec = 0;
	chain_index++;
}
	| CHAR RANGE CHAR {
	u_int i;

	if ($3 <= $1)
		yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);

	for (i = $1; i <= $3; i++) {
		if (__collate_char_pri_table[(u_char)i].prim)
			yyerror("Char 0x%02x duplicated", (u_char)i);
		__collate_char_pri_table[(u_char)i].prim = prim_pri++;
	}
}
	| '{' prim_order_list '}' {
	prim_pri++;
}
	| '(' sec_order_list ')' {
	prim_pri++;
	sec_pri = 1;
}
;
prim_order_list : prim_sub_item
	| prim_order_list ',' prim_sub_item 
;
sec_order_list : sec_sub_item
	| sec_order_list ',' sec_sub_item 
;
prim_sub_item : CHAR {
	if (__collate_char_pri_table[$1].prim)
		yyerror("Char 0x%02x duplicated", $1);
	__collate_char_pri_table[$1].prim = prim_pri;
}
	| CHAR RANGE CHAR {
	u_int i;

	if ($3 <= $1)
		yyerror("Illegal range 0x%02x -- 0x%02x",
			$1, $3);

	for (i = $1; i <= $3; i++) {
		if (__collate_char_pri_table[(u_char)i].prim)
			yyerror("Char 0x%02x duplicated", (u_char)i);
		__collate_char_pri_table[(u_char)i].prim = prim_pri;
	}
}
	| chain {
	if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
	     sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
		yyerror("can't grow chain table");
	(void)memset(__collate_chain_pri_table[chain_index].str, 0,
		     sizeof(__collate_chain_pri_table[0].str));
	(void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
	__collate_chain_pri_table[chain_index].prim = prim_pri;
	__collate_chain_pri_table[chain_index].sec = 0;
	chain_index++;
}
;
sec_sub_item : CHAR {
	if (__collate_char_pri_table[$1].prim)
		yyerror("Char 0x%02x duplicated", $1);
	__collate_char_pri_table[$1].prim = prim_pri;
	__collate_char_pri_table[$1].sec = sec_pri++;
}
	| CHAR RANGE CHAR {
	u_int i;

	if ($3 <= $1)
		yyerror("Illegal range 0x%02x -- 0x%02x",
			$1, $3);

	for (i = $1; i <= $3; i++) {
		if (__collate_char_pri_table[(u_char)i].prim)
			yyerror("Char 0x%02x duplicated", (u_char)i);
		__collate_char_pri_table[(u_char)i].prim = prim_pri;
		__collate_char_pri_table[(u_char)i].sec = sec_pri++;
	}
}
	| chain {
	if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
	     sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
		yyerror("can't grow chain table");
	(void)memset(__collate_chain_pri_table[chain_index].str, 0,
		     sizeof(__collate_chain_pri_table[0].str));
	(void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
	__collate_chain_pri_table[chain_index].prim = prim_pri;
	__collate_chain_pri_table[chain_index].sec = sec_pri++;
	chain_index++;
}
;
%%
int
main(int ac, char **av)
{
	int ch;

#ifdef COLLATE_DEBUG
	while((ch = getopt(ac, av, ":do:I:")) != EOF) {
#else
	while((ch = getopt(ac, av, ":o:I:")) != EOF) {
#endif
		switch (ch)
		{
#ifdef COLLATE_DEBUG
		  case 'd':
			debug++;
			break;
#endif
		  case 'o':
			out_file = optarg;
			break;

		  case 'I':
			strlcpy(map_name, optarg, sizeof(map_name));
			break;

		  default:
			usage();
		}
	}
	ac -= optind;
	av += optind;
	if (ac > 0) {
		if ((yyin = fopen(*av, "r")) == NULL)
			err(EX_UNAVAILABLE, "can't open source file %s", *av);
	}
	for (ch = 0; ch <= UCHAR_MAX; ch++)
		__collate_substitute_table[ch][0] = ch;
	yyparse();
	return 0;
}

static void
usage(void)
{
	fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n");
	exit(EX_USAGE);
}

void
yyerror(const char *fmt, ...)
{
	va_list ap;
	char msg[128];

	va_start(ap, fmt);
	vsnprintf(msg, sizeof(msg), fmt, ap);
	va_end(ap);
	errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
}

#ifdef COLLATE_DEBUG
static void
collate_print_tables(void)
{
	int i;
	struct __collate_st_chain_pri *p2;

	printf("Substitute table:\n");
	for (i = 0; i < UCHAR_MAX + 1; i++)
	    if (i != *__collate_substitute_table[i])
		printf("\t'%c' --> \"%s\"\n", i,
		       __collate_substitute_table[i]);
	printf("Chain priority table:\n");
	for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
		printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
	printf("Char priority table:\n");
	for (i = 0; i < UCHAR_MAX + 1; i++)
		printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
		       __collate_char_pri_table[i].sec);
}
#endif
OpenPOWER on IntegriCloud