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