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 <sys/types.h>
33 #include <arpa/inet.h>
45 void yyerror(const char *fmt, ...) __printflike(1, 2);
48 static void usage(void);
49 static void collate_print_tables(void);
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"
60 struct __collate_st_char_pri {
64 struct __collate_st_chain_pri {
69 char map_name[FILENAME_MAX] = ".";
70 char curr_chain[STR_LEN];
72 char __collate_version[STR_LEN];
73 u_char charmap_table[UCHAR_MAX + 1][CHARMAP_SYMBOL_LEN];
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;
82 int prim_pri = 1, sec_pri = 1;
87 const char *out_file = "LC_COLLATE";
93 %token SUBSTITUTE WITH ORDER RANGE
98 collate : statment_list
100 statment_list : statment
101 | statment_list '\n' statment
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);
114 substitute : SUBSTITUTE CHAR WITH STRING {
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);
124 order : ORDER order_list {
126 int ch, substed, ordered;
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);
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]));
147 collate_print_tables();
149 if ((fp = fopen(out_file, "w")) == NULL)
150 err(EX_UNAVAILABLE, "can't open destination file %s",
153 strcpy(__collate_version, COLLATE_VERSION1_2);
154 if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
156 "I/O error writing collate version to destination file %s",
158 u32 = htonl(chain_index);
159 if (fwrite(&u32, sizeof(u32), 1, fp) != 1)
161 "I/O error writing chains number to destination file %s",
163 if (fwrite(__collate_substitute_table,
164 sizeof(__collate_substitute_table), 1, fp) != 1)
166 "I/O error writing substitution table to destination file %s",
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);
174 if (fwrite(__collate_char_pri_table,
175 sizeof(__collate_char_pri_table), 1, fp) != 1)
177 "I/O error writing char table to destination file %s",
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);
185 if (fwrite(__collate_chain_pri_table,
186 sizeof(*__collate_chain_pri_table), chain_index, fp) !=
189 "I/O error writing chain table to destination file %s",
192 err(EX_IOERR, "I/O error closing destination file %s",
198 | order_list ';' item
203 if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
204 yyerror("\\0 can't be chained");
205 curr_chain[2] = '\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);
219 if (__collate_char_pri_table[$1].prim)
220 yyerror("Char 0x%02x duplicated", $1);
221 __collate_char_pri_table[$1].prim = prim_pri++;
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++;
237 yyerror("Illegal range 0x%02x -- 0x%02x", $1, $3);
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++;
245 | '{' prim_order_list '}' {
248 | '(' sec_order_list ')' {
253 prim_order_list : prim_sub_item
254 | prim_order_list ',' prim_sub_item
256 sec_order_list : sec_sub_item
257 | sec_order_list ',' sec_sub_item
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;
268 yyerror("Illegal range 0x%02x -- 0x%02x",
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;
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;
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++;
298 yyerror("Illegal range 0x%02x -- 0x%02x",
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++;
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++;
322 main(int ac, char **av)
327 while((ch = getopt(ac, av, ":do:I:")) != -1) {
329 while((ch = getopt(ac, av, ":o:I:")) != -1) {
343 strlcpy(map_name, optarg, sizeof(map_name));
353 if ((yyin = fopen(*av, "r")) == NULL)
354 err(EX_UNAVAILABLE, "can't open source file %s", *av);
356 for (ch = 0; ch <= UCHAR_MAX; ch++)
357 __collate_substitute_table[ch][0] = ch;
365 fprintf(stderr, "usage: colldef [-I map_dir] [-o out_file] [filename]\n");
370 yyerror(const char *fmt, ...)
376 vsnprintf(msg, sizeof(msg), fmt, ap);
378 errx(EX_UNAVAILABLE, "%s near line %d", msg, line_no);
383 collate_print_tables(void)
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);