style: compare return value of getopt() against -1 rather than EOF
[games.git] / usr.bin / colldef / parse.y
CommitLineData
984263bc
MD
1%{
2/*-
3 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
4 * at Electronni Visti IA, Kiev, Ukraine.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
1de703da
MD
27 *
28 * $FreeBSD: src/usr.bin/colldef/parse.y,v 1.15.2.5 2002/10/11 10:43:45 ache Exp $
9b0ec895 29 * $DragonFly: src/usr.bin/colldef/parse.y,v 1.6 2008/07/10 18:29:51 swildner Exp $
984263bc
MD
30 */
31
b340e503
HP
32#include <sys/types.h>
33#include <netinet/in.h>
984263bc
MD
34#include <err.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <string.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <sysexits.h>
41#include "collate.h"
42#include "common.h"
43
44extern FILE *yyin;
45void yyerror(const char *fmt, ...) __printflike(1, 2);
46int yyparse(void);
47int yylex(void);
48static void usage(void);
49static void collate_print_tables(void);
50
51char map_name[FILENAME_MAX] = ".";
52char curr_chain[STR_LEN];
53
54char __collate_version[STR_LEN];
55u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];
56
57#undef __collate_substitute_table
58u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
59#undef __collate_char_pri_table
60struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
61struct __collate_st_chain_pri *__collate_chain_pri_table;
62
63int chain_index;
64int prim_pri = 1, sec_pri = 1;
65#ifdef COLLATE_DEBUG
66int debug;
67#endif
68
69const char *out_file = "LC_COLLATE";
70%}
71%union {
72 u_char ch;
73 u_char str[BUFSIZE];
74}
75%token SUBSTITUTE WITH ORDER RANGE
76%token <str> STRING
77%token <str> DEFN
78%token <ch> CHAR
79%%
80collate : statment_list
81;
82statment_list : statment
83 | statment_list '\n' statment
84;
85statment :
86 | charmap
87 | substitute
88 | order
89;
90charmap : DEFN CHAR {
91 if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN)
92 yyerror("Charmap symbol name '%s' is too long", $1);
93 strcpy(charmap_table[$2], $1);
94}
95;
96substitute : SUBSTITUTE CHAR WITH STRING {
97 if ($2 == '\0')
98 yyerror("NUL character can't be substituted");
99 if (strchr($4, $2) != NULL)
100 yyerror("Char 0x%02x substitution is recursive", $2);
101 if (strlen($4) + 1 > STR_LEN)
102 yyerror("Char 0x%02x substitution is too long", $2);
103 strcpy(__collate_substitute_table[$2], $4);
104}
105;
106order : ORDER order_list {
107 FILE *fp;
108 int ch, substed, ordered;
109 uint32_t u32;
110
111 for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
112 substed = (__collate_substitute_table[ch][0] != ch);
113 ordered = !!__collate_char_pri_table[ch].prim;
114 if (!ordered && !substed)
115 yyerror("Char 0x%02x not found", ch);
116 if (substed && ordered)
117 yyerror("Char 0x%02x can't be ordered since substituted", ch);
118 }
119
120 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
121 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
122 yyerror("can't grow chain table");
123 (void)memset(__collate_chain_pri_table[chain_index].str, 0,
124 sizeof(__collate_chain_pri_table[0].str));
125 __collate_chain_pri_table[chain_index].prim = 0;
126 __collate_chain_pri_table[chain_index].sec = 0;
127 chain_index++;
128
129 if ((fp = fopen(out_file, "w")) == NULL)
130 err(EX_UNAVAILABLE, "can't open destination file %s",
131 out_file);
132
133 strcpy(__collate_version, COLLATE_VERSION1_1);
134 if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
135 err(EX_IOERR,
84982e90 136 "IO error writing collate version to destination file %s",
984263bc
MD
137 out_file);
138 u32 = htonl(chain_index);
139 if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
140 err(EX_IOERR,
84982e90 141 "IO error writing chains number to destination file %s",
984263bc
MD
142 out_file);
143 if (fwrite(__collate_substitute_table,
144 sizeof(__collate_substitute_table), 1, fp) != 1)
145 err(EX_IOERR,
84982e90 146 "IO error writing substitute table to destination file %s",
984263bc
MD
147 out_file);
148 if (fwrite(__collate_char_pri_table,
149 sizeof(__collate_char_pri_table), 1, fp) != 1)
150 err(EX_IOERR,
84982e90 151 "IO error writing char table to destination file %s",
984263bc
MD
152 out_file);
153 if (fwrite(__collate_chain_pri_table,
154 sizeof(*__collate_chain_pri_table), chain_index, fp) !=
155 (size_t)chain_index)
156 err(EX_IOERR,
84982e90 157 "IO error writing chain table to destination file %s",
984263bc
MD
158 out_file);
159 if (fclose(fp) != 0)
160 err(EX_IOERR, "IO error closing destination file %s",
161 out_file);
162#ifdef COLLATE_DEBUG
163 if (debug)
164 collate_print_tables();
165#endif
166 exit(EX_OK);
167}
168;
169order_list : item
170 | order_list ';' item
171;
172chain : CHAR CHAR {
173 curr_chain[0] = $1;
174 curr_chain[1] = $2;
175 if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
176 yyerror("\\0 can't be chained");
177 curr_chain[2] = '\0';
178}
179 | chain CHAR {
180 static char tb[2];
181
182 tb[0] = $2;
183 if (tb[0] == '\0')
184 yyerror("\\0 can't be chained");
185 if (strlen(curr_chain) + 2 > STR_LEN)
186 yyerror("Chain '%s' grows too long", curr_chain);
187 (void)strcat(curr_chain, tb);
188}
189;
190item : CHAR {
191 if (__collate_char_pri_table[$1].prim)
192 yyerror("Char 0x%02x duplicated", $1);
193 __collate_char_pri_table[$1].prim = prim_pri++;
194}
195 | chain {
196 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
197 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
198 yyerror("can't grow chain table");
199 (void)memset(__collate_chain_pri_table[chain_index].str, 0,
200 sizeof(__collate_chain_pri_table[0].str));
201 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
202 __collate_chain_pri_table[chain_index].prim = prim_pri++;
203 __collate_chain_pri_table[chain_index].sec = 0;
204 chain_index++;
205}
206 | CHAR RANGE CHAR {
207 u_int i;
208
209 if ($3 <= $1)
210 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);
211
212 for (i = $1; i <= $3; i++) {
213 if (__collate_char_pri_table[(u_char)i].prim)
214 yyerror("Char 0x%02x duplicated", (u_char)i);
215 __collate_char_pri_table[(u_char)i].prim = prim_pri++;
216 }
217}
218 | '{' prim_order_list '}' {
219 prim_pri++;
220}
221 | '(' sec_order_list ')' {
222 prim_pri++;
223 sec_pri = 1;
224}
225;
226prim_order_list : prim_sub_item
227 | prim_order_list ',' prim_sub_item
228;
229sec_order_list : sec_sub_item
230 | sec_order_list ',' sec_sub_item
231;
232prim_sub_item : CHAR {
233 if (__collate_char_pri_table[$1].prim)
234 yyerror("Char 0x%02x duplicated", $1);
235 __collate_char_pri_table[$1].prim = prim_pri;
236}
237 | CHAR RANGE CHAR {
238 u_int i;
239
240 if ($3 <= $1)
241 yyerror("Illegal range 0x%02x -- 0x%02x",
242 $1, $3);
243
244 for (i = $1; i <= $3; i++) {
245 if (__collate_char_pri_table[(u_char)i].prim)
246 yyerror("Char 0x%02x duplicated", (u_char)i);
247 __collate_char_pri_table[(u_char)i].prim = prim_pri;
248 }
249}
250 | chain {
251 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
252 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
253 yyerror("can't grow chain table");
254 (void)memset(__collate_chain_pri_table[chain_index].str, 0,
255 sizeof(__collate_chain_pri_table[0].str));
256 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
257 __collate_chain_pri_table[chain_index].prim = prim_pri;
258 __collate_chain_pri_table[chain_index].sec = 0;
259 chain_index++;
260}
261;
262sec_sub_item : CHAR {
263 if (__collate_char_pri_table[$1].prim)
264 yyerror("Char 0x%02x duplicated", $1);
265 __collate_char_pri_table[$1].prim = prim_pri;
266 __collate_char_pri_table[$1].sec = sec_pri++;
267}
268 | CHAR RANGE CHAR {
269 u_int i;
270
271 if ($3 <= $1)
272 yyerror("Illegal range 0x%02x -- 0x%02x",
273 $1, $3);
274
275 for (i = $1; i <= $3; i++) {
276 if (__collate_char_pri_table[(u_char)i].prim)
277 yyerror("Char 0x%02x duplicated", (u_char)i);
278 __collate_char_pri_table[(u_char)i].prim = prim_pri;
279 __collate_char_pri_table[(u_char)i].sec = sec_pri++;
280 }
281}
282 | chain {
283 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
284 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
285 yyerror("can't grow chain table");
286 (void)memset(__collate_chain_pri_table[chain_index].str, 0,
287 sizeof(__collate_chain_pri_table[0].str));
288 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
289 __collate_chain_pri_table[chain_index].prim = prim_pri;
290 __collate_chain_pri_table[chain_index].sec = sec_pri++;
291 chain_index++;
292}
293;
294%%
295int
296main(int ac, char **av)
297{
298 int ch;
299
300#ifdef COLLATE_DEBUG
9b0ec895 301 while((ch = getopt(ac, av, ":do:I:")) != -1) {
984263bc 302#else
9b0ec895 303 while((ch = getopt(ac, av, ":o:I:")) != -1) {
984263bc
MD
304#endif
305 switch (ch)
306 {
307#ifdef COLLATE_DEBUG
308 case 'd':
309 debug++;
310 break;
311#endif
312 case 'o':
313 out_file = optarg;
314 break;
315
316 case 'I':
317 strlcpy(map_name, optarg, sizeof(map_name));
318 break;
319
320 default:
321 usage();
322 }
323 }
324 ac -= optind;
325 av += optind;
326 if (ac > 0) {
327 if ((yyin = fopen(*av, "r")) == NULL)
328 err(EX_UNAVAILABLE, "can't open source file %s", *av);
329 }
330 for (ch = 0; ch <= UCHAR_MAX; ch++)
331 __collate_substitute_table[ch][0] = ch;
332 yyparse();
333 return 0;
334}
335
336static void
337usage(void)
338{
339 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n");
340 exit(EX_USAGE);
341}
342
343void
344yyerror(const char *fmt, ...)
345{
346 va_list ap;
347 char msg[128];
348
349 va_start(ap, fmt);
350 vsnprintf(msg, sizeof(msg), fmt, ap);
351 va_end(ap);
352 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
353}
354
355#ifdef COLLATE_DEBUG
356static void
357collate_print_tables(void)
358{
359 int i;
360 struct __collate_st_chain_pri *p2;
361
362 printf("Substitute table:\n");
363 for (i = 0; i < UCHAR_MAX + 1; i++)
364 if (i != *__collate_substitute_table[i])
365 printf("\t'%c' --> \"%s\"\n", i,
366 __collate_substitute_table[i]);
367 printf("Chain priority table:\n");
368 for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
369 printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
370 printf("Char priority table:\n");
371 for (i = 0; i < UCHAR_MAX + 1; i++)
372 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
373 __collate_char_pri_table[i].sec);
374}
375#endif