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