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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <arpa/inet.h>
43 void yyerror(const char *fmt, ...) __printflike(1, 2);
46 static void usage(void);
47 static void collate_print_tables(void);
49 char map_name[FILENAME_MAX] = ".";
50 char curr_chain[STR_LEN];
52 char __collate_version[STR_LEN];
53 u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];
55 #undef __collate_substitute_table
56 u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN];
57 #undef __collate_char_pri_table
58 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
59 struct __collate_st_chain_pri *__collate_chain_pri_table;
62 int prim_pri = 1, sec_pri = 1;
67 const char *out_file = "LC_COLLATE";
73 %token SUBSTITUTE WITH ORDER RANGE
78 collate : statment_list
80 statment_list : statment
81 | statment_list '\n' statment
89 if (strlen($1) + 1 > CHARMAP_SYMBOL_LEN)
90 yyerror("Charmap symbol name '%s' is too long", $1);
91 strcpy(charmap_table[$2], $1);
94 substitute : SUBSTITUTE CHAR WITH STRING {
96 yyerror("NUL character can't be substituted");
97 if (strchr($4, $2) != NULL)
98 yyerror("Char 0x%02x substitution is recursive", $2);
99 if (strlen($4) + 1 > STR_LEN)
100 yyerror("Char 0x%02x substitution is too long", $2);
101 strcpy(__collate_substitute_table[$2], $4);
104 order : ORDER order_list {
106 int ch, substed, ordered;
109 for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
110 substed = (__collate_substitute_table[ch][0] != ch);
111 ordered = !!__collate_char_pri_table[ch].prim;
112 if (!ordered && !substed)
113 yyerror("Char 0x%02x not found", ch);
114 if (substed && ordered)
115 yyerror("Char 0x%02x can't be ordered since substituted", ch);
118 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
119 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
120 yyerror("can't grow chain table");
121 (void)memset(&__collate_chain_pri_table[chain_index], 0,
122 sizeof(__collate_chain_pri_table[0]));
127 collate_print_tables();
129 if ((fp = fopen(out_file, "w")) == NULL)
130 err(EX_UNAVAILABLE, "can't open destination file %s",
133 strcpy(__collate_version, COLLATE_VERSION1_2);
134 if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
136 "IO error writting collate version to destination file %s",
138 u32 = htonl(chain_index);
139 if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
141 "IO error writting chains number to destination file %s",
143 if (fwrite(__collate_substitute_table,
144 sizeof(__collate_substitute_table), 1, fp) != 1)
146 "IO error writting substitute table to destination file %s",
148 for (ch = 0; ch < UCHAR_MAX + 1; ch++) {
149 __collate_char_pri_table[ch].prim =
150 htonl(__collate_char_pri_table[ch].prim);
151 __collate_char_pri_table[ch].sec =
152 htonl(__collate_char_pri_table[ch].sec);
154 if (fwrite(__collate_char_pri_table,
155 sizeof(__collate_char_pri_table), 1, fp) != 1)
157 "IO error writting char table to destination file %s",
159 for (ch = 0; ch < chain_index; ch++) {
160 __collate_chain_pri_table[ch].prim =
161 htonl(__collate_chain_pri_table[ch].prim);
162 __collate_chain_pri_table[ch].sec =
163 htonl(__collate_chain_pri_table[ch].sec);
165 if (fwrite(__collate_chain_pri_table,
166 sizeof(*__collate_chain_pri_table), chain_index, fp) !=
169 "IO error writting chain table to destination file %s",
172 err(EX_IOERR, "IO error closing destination file %s",
178 | order_list ';' item
183 if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
184 yyerror("\\0 can't be chained");
185 curr_chain[2] = '\0';
192 yyerror("\\0 can't be chained");
193 if (strlen(curr_chain) + 2 > STR_LEN)
194 yyerror("Chain '%s' grows too long", curr_chain);
195 (void)strcat(curr_chain, tb);
199 if (__collate_char_pri_table[$1].prim)
200 yyerror("Char 0x%02x duplicated", $1);
201 __collate_char_pri_table[$1].prim = prim_pri++;
204 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
205 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
206 yyerror("can't grow chain table");
207 (void)memset(&__collate_chain_pri_table[chain_index], 0,
208 sizeof(__collate_chain_pri_table[0]));
209 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
210 __collate_chain_pri_table[chain_index].prim = prim_pri++;
217 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);
219 for (i = $1; i <= $3; i++) {
220 if (__collate_char_pri_table[(u_char)i].prim)
221 yyerror("Char 0x%02x duplicated", (u_char)i);
222 __collate_char_pri_table[(u_char)i].prim = prim_pri++;
225 | '{' prim_order_list '}' {
228 | '(' sec_order_list ')' {
233 prim_order_list : prim_sub_item
234 | prim_order_list ',' prim_sub_item
236 sec_order_list : sec_sub_item
237 | sec_order_list ',' sec_sub_item
239 prim_sub_item : CHAR {
240 if (__collate_char_pri_table[$1].prim)
241 yyerror("Char 0x%02x duplicated", $1);
242 __collate_char_pri_table[$1].prim = prim_pri;
248 yyerror("Illegal range 0x%02x -- 0x%02x",
251 for (i = $1; i <= $3; i++) {
252 if (__collate_char_pri_table[(u_char)i].prim)
253 yyerror("Char 0x%02x duplicated", (u_char)i);
254 __collate_char_pri_table[(u_char)i].prim = prim_pri;
258 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
259 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
260 yyerror("can't grow chain table");
261 (void)memset(&__collate_chain_pri_table[chain_index], 0,
262 sizeof(__collate_chain_pri_table[0]));
263 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
264 __collate_chain_pri_table[chain_index].prim = prim_pri;
268 sec_sub_item : CHAR {
269 if (__collate_char_pri_table[$1].prim)
270 yyerror("Char 0x%02x duplicated", $1);
271 __collate_char_pri_table[$1].prim = prim_pri;
272 __collate_char_pri_table[$1].sec = sec_pri++;
278 yyerror("Illegal range 0x%02x -- 0x%02x",
281 for (i = $1; i <= $3; i++) {
282 if (__collate_char_pri_table[(u_char)i].prim)
283 yyerror("Char 0x%02x duplicated", (u_char)i);
284 __collate_char_pri_table[(u_char)i].prim = prim_pri;
285 __collate_char_pri_table[(u_char)i].sec = sec_pri++;
289 if ((__collate_chain_pri_table = realloc(__collate_chain_pri_table,
290 sizeof(*__collate_chain_pri_table) * (chain_index + 1))) == NULL)
291 yyerror("can't grow chain table");
292 (void)memset(&__collate_chain_pri_table[chain_index], 0,
293 sizeof(__collate_chain_pri_table[0]));
294 (void)strcpy(__collate_chain_pri_table[chain_index].str, curr_chain);
295 __collate_chain_pri_table[chain_index].prim = prim_pri;
296 __collate_chain_pri_table[chain_index].sec = sec_pri++;
302 main(int ac, char **av)
307 while((ch = getopt(ac, av, ":do:I:")) != -1) {
309 while((ch = getopt(ac, av, ":o:I:")) != -1) {
323 strlcpy(map_name, optarg, sizeof(map_name));
333 if ((yyin = fopen(*av, "r")) == NULL)
334 err(EX_UNAVAILABLE, "can't open source file %s", *av);
336 for (ch = 0; ch <= UCHAR_MAX; ch++)
337 __collate_substitute_table[ch][0] = ch;
345 fprintf(stderr, "usage: colldef [-I map_dir] [-o out_file] [filename]\n");
350 yyerror(const char *fmt, ...)
356 vsnprintf(msg, sizeof(msg), fmt, ap);
358 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
363 collate_print_tables(void)
367 printf("Substitute table:\n");
368 for (i = 0; i < UCHAR_MAX + 1; i++)
369 if (i != *__collate_substitute_table[i])
370 printf("\t'%c' --> \"%s\"\n", i,
371 __collate_substitute_table[i]);
372 printf("Chain priority table:\n");
373 for (i = 0; i < chain_index - 1; i++)
374 printf("\t\"%s\" : %d %d\n",
375 __collate_chain_pri_table[i].str,
376 __collate_chain_pri_table[i].prim,
377 __collate_chain_pri_table[i].sec);
378 printf("Char priority table:\n");
379 for (i = 0; i < UCHAR_MAX + 1; i++)
380 printf("\t'%c' : %d %d\n", i, __collate_char_pri_table[i].prim,
381 __collate_char_pri_table[i].sec);