3 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua>
4 * at Electronni Visti IA, Kiev, Ukraine.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
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 $
32 #include <sys/types.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
46 void yyerror(const char *fmt, ...) __printflike(1, 2);
49 static void usage(void);
50 static void collate_print_tables(void);
52 char map_name[FILENAME_MAX] = ".";
53 char curr_chain[STR_LEN];
55 char __collate_version[STR_LEN];
56 u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];
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;
65 int prim_pri = 1, sec_pri = 1;
70 const char *out_file = "LC_COLLATE";
76 %token SUBSTITUTE WITH ORDER RANGE
81 collate : statment_list
83 statment_list : statment
84 | statment_list '\n' statment
92 if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN)
93 yyerror("Charmap symbol name '%s' is too long", $1);
94 strcpy(charmap_table[$2], $1);
97 substitute : SUBSTITUTE CHAR WITH STRING {
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);
107 order : ORDER order_list {
109 int ch, substed, ordered;
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);
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;
130 if ((fp = fopen(out_file, "w")) == NULL)
131 err(EX_UNAVAILABLE, "can't open destination file %s",
134 strcpy(__collate_version, COLLATE_VERSION1_1);
135 if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
137 "IO error writting collate version to destination file %s",
139 u32 = htonl(chain_index);
140 if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
142 "IO error writting chains number to destination file %s",
144 if (fwrite(__collate_substitute_table,
145 sizeof(__collate_substitute_table), 1, fp) != 1)
147 "IO error writting substitute table to destination file %s",
149 if (fwrite(__collate_char_pri_table,
150 sizeof(__collate_char_pri_table), 1, fp) != 1)
152 "IO error writting char table to destination file %s",
154 if (fwrite(__collate_chain_pri_table,
155 sizeof(*__collate_chain_pri_table), chain_index, fp) !=
158 "IO error writting chain table to destination file %s",
161 err(EX_IOERR, "IO error closing destination file %s",
165 collate_print_tables();
171 | order_list ';' item
176 if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
177 yyerror("\\0 can't be chained");
178 curr_chain[2] = '\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);
192 if (__collate_char_pri_table[$1].prim)
193 yyerror("Char 0x%02x duplicated", $1);
194 __collate_char_pri_table[$1].prim = prim_pri++;
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;
211 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);
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++;
219 | '{' prim_order_list '}' {
222 | '(' sec_order_list ')' {
227 prim_order_list : prim_sub_item
228 | prim_order_list ',' prim_sub_item
230 sec_order_list : sec_sub_item
231 | sec_order_list ',' sec_sub_item
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;
242 yyerror("Illegal range 0x%02x -- 0x%02x",
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;
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;
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++;
273 yyerror("Illegal range 0x%02x -- 0x%02x",
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++;
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++;
297 main(int ac, char **av)
302 while((ch = getopt(ac, av, ":do:I:")) != EOF) {
304 while((ch = getopt(ac, av, ":o:I:")) != EOF) {
318 strlcpy(map_name, optarg, sizeof(map_name));
328 if ((yyin = fopen(*av, "r")) == NULL)
329 err(EX_UNAVAILABLE, "can't open source file %s", *av);
331 for (ch = 0; ch <= UCHAR_MAX; ch++)
332 __collate_substitute_table[ch][0] = ch;
340 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n");
345 yyerror(const char *fmt, ...)
351 vsnprintf(msg, sizeof(msg), fmt, ap);
353 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
358 collate_print_tables(void)
361 struct __collate_st_chain_pri *p2;
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);