rename exp() to expx() to avoid conflict with gcc-3.4 built-in.
[dragonfly.git] / usr.bin / colldef / parse.y
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.
27  *
28  * $FreeBSD: src/usr.bin/colldef/parse.y,v 1.15.2.5 2002/10/11 10:43:45 ache Exp $
29  * $DragonFly: src/usr.bin/colldef/parse.y,v 1.3 2004/07/22 14:05:07 hmp Exp $
30  */
31
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <err.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sysexits.h>
42 #include "collate.h"
43 #include "common.h"
44
45 extern FILE *yyin;
46 void yyerror(const char *fmt, ...) __printflike(1, 2);
47 int yyparse(void);
48 int yylex(void);
49 static void usage(void);
50 static void collate_print_tables(void);
51
52 char map_name[FILENAME_MAX] = ".";
53 char curr_chain[STR_LEN];
54
55 char __collate_version[STR_LEN];
56 u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];
57
58 #undef __collate_substitute_table
59 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
60 #undef __collate_char_pri_table
61 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
62 struct __collate_st_chain_pri *__collate_chain_pri_table;
63
64 int chain_index;
65 int prim_pri = 1, sec_pri = 1;
66 #ifdef COLLATE_DEBUG
67 int debug;
68 #endif
69
70 const char *out_file = "LC_COLLATE";
71 %}
72 %union {
73         u_char ch;
74         u_char str[BUFSIZE];
75 }
76 %token SUBSTITUTE WITH ORDER RANGE
77 %token <str> STRING
78 %token <str> DEFN
79 %token <ch> CHAR
80 %%
81 collate : statment_list
82 ;
83 statment_list : statment
84         | statment_list '\n' statment
85 ;
86 statment :
87         | charmap
88         | substitute
89         | order
90 ;
91 charmap : DEFN CHAR {
92         if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN)
93                 yyerror("Charmap symbol name '%s' is too long", $1);
94         strcpy(charmap_table[$2], $1);
95 }
96 ;
97 substitute : SUBSTITUTE CHAR WITH STRING {
98         if ($2 == '\0')
99                 yyerror("NUL character can't be substituted");
100         if (strchr($4, $2) != NULL)
101                 yyerror("Char 0x%02x substitution is recursive", $2);
102         if (strlen($4) + 1 > STR_LEN)
103                 yyerror("Char 0x%02x substitution is too long", $2);
104         strcpy(__collate_substitute_table[$2], $4);
105 }
106 ;
107 order : ORDER order_list {
108         FILE *fp;
109         int ch, substed, ordered;
110         uint32_t u32;
111
112         for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
113                 substed = (__collate_substitute_table[ch][0] != ch);
114                 ordered = !!__collate_char_pri_table[ch].prim;
115                 if (!ordered && !substed)
116                         yyerror("Char 0x%02x not found", ch);
117                 if (substed && ordered)
118                         yyerror("Char 0x%02x can't be ordered since substituted", ch);
119         }
120
121         if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
122              sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
123                 yyerror("can't grow chain table");
124         (void)memset(__collate_chain_pri_table[chain_index].str, 0,
125                      sizeof(__collate_chain_pri_table[0].str));
126         __collate_chain_pri_table[chain_index].prim = 0;
127         __collate_chain_pri_table[chain_index].sec = 0;
128         chain_index++;
129
130         if ((fp = fopen(out_file, "w")) == NULL)
131                 err(EX_UNAVAILABLE, "can't open destination file %s",
132                     out_file);
133
134         strcpy(__collate_version, COLLATE_VERSION1_1);
135         if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
136                 err(EX_IOERR,
137                 "IO error writting collate version to destination file %s",
138                     out_file);
139         u32 = htonl(chain_index);
140         if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
141                 err(EX_IOERR,
142                 "IO error writting chains number to destination file %s",
143                     out_file);
144         if (fwrite(__collate_substitute_table,
145                    sizeof(__collate_substitute_table), 1, fp) != 1)
146                 err(EX_IOERR,
147                 "IO error writting substitute table to destination file %s",
148                     out_file);
149         if (fwrite(__collate_char_pri_table,
150                    sizeof(__collate_char_pri_table), 1, fp) != 1)
151                 err(EX_IOERR,
152                 "IO error writting char table to destination file %s",
153                     out_file);
154         if (fwrite(__collate_chain_pri_table,
155                    sizeof(*__collate_chain_pri_table), chain_index, fp) !=
156                    (size_t)chain_index)
157                 err(EX_IOERR,
158                 "IO error writting chain table to destination file %s",
159                     out_file);
160         if (fclose(fp) != 0)
161                 err(EX_IOERR, "IO error closing destination file %s",
162                     out_file);
163 #ifdef COLLATE_DEBUG
164         if (debug)
165                 collate_print_tables();
166 #endif
167         exit(EX_OK);
168 }
169 ;
170 order_list : item
171         | order_list ';' item
172 ;
173 chain : CHAR CHAR {
174         curr_chain[0] = $1;
175         curr_chain[1] = $2;
176         if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
177                 yyerror("\\0 can't be chained");
178         curr_chain[2] = '\0';
179 }
180         | chain CHAR {
181         static char tb[2];
182
183         tb[0] = $2;
184         if (tb[0] == '\0')
185                 yyerror("\\0 can't be chained");
186         if (strlen(curr_chain) + 2 > STR_LEN)
187                 yyerror("Chain '%s' grows too long", curr_chain);
188         (void)strcat(curr_chain, tb);
189 }
190 ;
191 item :  CHAR {
192         if (__collate_char_pri_table[$1].prim)
193                 yyerror("Char 0x%02x duplicated", $1);
194         __collate_char_pri_table[$1].prim = prim_pri++;
195 }
196         | chain {
197         if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
198              sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
199                 yyerror("can't grow chain table");
200         (void)memset(__collate_chain_pri_table[chain_index].str, 0,
201                      sizeof(__collate_chain_pri_table[0].str));
202         (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
203         __collate_chain_pri_table[chain_index].prim = prim_pri++;
204         __collate_chain_pri_table[chain_index].sec = 0;
205         chain_index++;
206 }
207         | CHAR RANGE CHAR {
208         u_int i;
209
210         if ($3 <= $1)
211                 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);
212
213         for (i = $1; i <= $3; i++) {
214                 if (__collate_char_pri_table[(u_char)i].prim)
215                         yyerror("Char 0x%02x duplicated", (u_char)i);
216                 __collate_char_pri_table[(u_char)i].prim = prim_pri++;
217         }
218 }
219         | '{' prim_order_list '}' {
220         prim_pri++;
221 }
222         | '(' sec_order_list ')' {
223         prim_pri++;
224         sec_pri = 1;
225 }
226 ;
227 prim_order_list : prim_sub_item
228         | prim_order_list ',' prim_sub_item 
229 ;
230 sec_order_list : sec_sub_item
231         | sec_order_list ',' sec_sub_item 
232 ;
233 prim_sub_item : CHAR {
234         if (__collate_char_pri_table[$1].prim)
235                 yyerror("Char 0x%02x duplicated", $1);
236         __collate_char_pri_table[$1].prim = prim_pri;
237 }
238         | CHAR RANGE CHAR {
239         u_int i;
240
241         if ($3 <= $1)
242                 yyerror("Illegal range 0x%02x -- 0x%02x",
243                         $1, $3);
244
245         for (i = $1; i <= $3; i++) {
246                 if (__collate_char_pri_table[(u_char)i].prim)
247                         yyerror("Char 0x%02x duplicated", (u_char)i);
248                 __collate_char_pri_table[(u_char)i].prim = prim_pri;
249         }
250 }
251         | chain {
252         if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
253              sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
254                 yyerror("can't grow chain table");
255         (void)memset(__collate_chain_pri_table[chain_index].str, 0,
256                      sizeof(__collate_chain_pri_table[0].str));
257         (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
258         __collate_chain_pri_table[chain_index].prim = prim_pri;
259         __collate_chain_pri_table[chain_index].sec = 0;
260         chain_index++;
261 }
262 ;
263 sec_sub_item : CHAR {
264         if (__collate_char_pri_table[$1].prim)
265                 yyerror("Char 0x%02x duplicated", $1);
266         __collate_char_pri_table[$1].prim = prim_pri;
267         __collate_char_pri_table[$1].sec = sec_pri++;
268 }
269         | CHAR RANGE CHAR {
270         u_int i;
271
272         if ($3 <= $1)
273                 yyerror("Illegal range 0x%02x -- 0x%02x",
274                         $1, $3);
275
276         for (i = $1; i <= $3; i++) {
277                 if (__collate_char_pri_table[(u_char)i].prim)
278                         yyerror("Char 0x%02x duplicated", (u_char)i);
279                 __collate_char_pri_table[(u_char)i].prim = prim_pri;
280                 __collate_char_pri_table[(u_char)i].sec = sec_pri++;
281         }
282 }
283         | chain {
284         if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
285              sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
286                 yyerror("can't grow chain table");
287         (void)memset(__collate_chain_pri_table[chain_index].str, 0,
288                      sizeof(__collate_chain_pri_table[0].str));
289         (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
290         __collate_chain_pri_table[chain_index].prim = prim_pri;
291         __collate_chain_pri_table[chain_index].sec = sec_pri++;
292         chain_index++;
293 }
294 ;
295 %%
296 int
297 main(int ac, char **av)
298 {
299         int ch;
300
301 #ifdef COLLATE_DEBUG
302         while((ch = getopt(ac, av, ":do:I:")) != EOF) {
303 #else
304         while((ch = getopt(ac, av, ":o:I:")) != EOF) {
305 #endif
306                 switch (ch)
307                 {
308 #ifdef COLLATE_DEBUG
309                   case 'd':
310                         debug++;
311                         break;
312 #endif
313                   case 'o':
314                         out_file = optarg;
315                         break;
316
317                   case 'I':
318                         strlcpy(map_name, optarg, sizeof(map_name));
319                         break;
320
321                   default:
322                         usage();
323                 }
324         }
325         ac -= optind;
326         av += optind;
327         if (ac > 0) {
328                 if ((yyin = fopen(*av, "r")) == NULL)
329                         err(EX_UNAVAILABLE, "can't open source file %s", *av);
330         }
331         for (ch = 0; ch <= UCHAR_MAX; ch++)
332                 __collate_substitute_table[ch][0] = ch;
333         yyparse();
334         return 0;
335 }
336
337 static void
338 usage(void)
339 {
340         fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n");
341         exit(EX_USAGE);
342 }
343
344 void
345 yyerror(const char *fmt, ...)
346 {
347         va_list ap;
348         char msg[128];
349
350         va_start(ap, fmt);
351         vsnprintf(msg, sizeof(msg), fmt, ap);
352         va_end(ap);
353         errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
354 }
355
356 #ifdef COLLATE_DEBUG
357 static void
358 collate_print_tables(void)
359 {
360         int i;
361         struct __collate_st_chain_pri *p2;
362
363         printf("Substitute table:\n");
364         for (i = 0; i < UCHAR_MAX + 1; i++)
365             if (i != *__collate_substitute_table[i])
366                 printf("\t'%c' --> \"%s\"\n", i,
367                        __collate_substitute_table[i]);
368         printf("Chain priority table:\n");
369         for (p2 = __collate_chain_pri_table; p2->str[0] != '\0'; p2++)
370                 printf("\t\"%s\" : %d %d\n", p2->str, p2->prim, p2->sec);
371         printf("Char priority table:\n");
372         for (i = 0; i < UCHAR_MAX + 1; i++)
373                 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
374                        __collate_char_pri_table[i].sec);
375 }
376 #endif