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