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: src/usr.bin/colldef/parse.y,v 1.31 2002/10/16 12:56:22 charnier Exp $");
32 #include <arpa/inet.h>
45 #define PRI_UNDEFINED (-1)
47 #define LINE_NONE (-1)
49 #define LINE_ELLIPSIS 1
50 #define LINE_UNDEFINED 2
51 /* If UNDEFINED is specified with ellipses, we reposition prim_pri to
52 * UNDEFINED_PRI, leaving gap for undefined characters. */
53 #define UNDEFINED_PRI (COLLATE_MAX_PRIORITY - (COLLATE_MAX_PRIORITY >> 2))
56 void yyerror(const char *fmt, ...) __printflike(1, 2);
59 static void usage(void);
60 static void collate_print_tables(void);
61 static struct __collate_st_char_pri *getpri(int32_t);
62 static struct __collate_st_char_pri *haspri(int32_t);
63 static struct __collate_st_chain_pri *getchain(const wchar_t *, int);
64 static struct symbol *getsymbolbychar(wchar_t);
65 static struct symbol *hassymbolbychar(wchar_t);
66 static void setsymbolbychar(struct symbol *);
67 struct symbol *getstring(const wchar_t *);
68 static void makeforwardref(int, const struct symbol *, const struct symbol *);
69 static int charpricompar(const void *, const void *);
70 static int substcompar(const void *, const void *);
71 static int chainpricompar(const void *, const void *);
72 static void putsubst(int32_t, int, const wchar_t *);
73 static int hassubst(int32_t, int);
74 static const wchar_t *__collate_wcsnchr(const wchar_t *, wchar_t, int);
75 static int __collate_wcsnlen(const wchar_t *, int);
76 char *showwcs(const wchar_t *, int);
77 static char *charname(wchar_t);
78 static char *charname2(wchar_t);
80 char map_name[FILENAME_MAX] = ".";
81 wchar_t curr_chain[STR_LEN + 1];
83 char __collate_version[STR_LEN];
85 static DB *charmapdb2;
86 static DB *largemapdb;
87 static int nlargemap = 0;
88 static DB *substdb[COLL_WEIGHTS_MAX];
89 static int nsubst[COLL_WEIGHTS_MAX];
91 static int nchain = 0;
93 static DB *forward_ref[COLL_WEIGHTS_MAX];
94 static struct symbol *prev_weight_table[COLL_WEIGHTS_MAX];
95 static struct symbol *prev2_weight_table[COLL_WEIGHTS_MAX];
96 static struct symbol *weight_table[COLL_WEIGHTS_MAX];
97 static int prev_line = LINE_NONE;
98 static struct symbol *prev_elem;
99 static int weight_index = 0;
100 static int allow_ellipsis = 0;
101 static struct symbol sym_ellipsis = {SYMBOL_ELLIPSIS, PRI_UNDEFINED};
102 static struct symbol sym_ignore = {SYMBOL_IGNORE, PRI_IGNORE};
103 static struct symbol sym_undefined = {SYMBOL_CHAR, PRI_UNDEFINED};
104 static int order_pass = 0;
106 #undef __collate_char_pri_table
107 struct __collate_st_char_pri __collate_char_pri_table[UCHAR_MAX + 1];
108 struct __collate_st_chain_pri *__collate_chain_pri_table;
109 struct __collate_st_subst *__collate_substitute_table[COLL_WEIGHTS_MAX];
110 struct __collate_st_large_char_pri *__collate_large_char_pri_table;
112 int prim_pri = 2, sec_pri = 2;
116 struct __collate_st_info info = {{DIRECTIVE_FORWARD, DIRECTIVE_FORWARD}, 0, 0, 0, {PRI_UNDEFINED, PRI_UNDEFINED}};
118 /* Some of the code expects COLL_WEIGHTS_MAX == 2 */
119 int directive_count = COLL_WEIGHTS_MAX;
121 const char *out_file = "LC_COLLATE";
125 wchar_t str[BUFSIZE];
127 %token SUBSTITUTE WITH
128 %token START_LC_COLLATE END_LC_COLLATE COLLATING_ELEMENT FROM COLLATING_SYMBOL
129 %token ELLIPSIS IGNORE UNDEFINED
130 %token ORDER RANGE ORDER_START ORDER_END ORDER_SECOND_PASS
135 %token <ch> ORDER_DIRECTIVE
139 int localedef = (stringdb != NULL);
144 struct __collate_st_chain_pri *t, *v;
148 if ((__collate_chain_pri_table = (struct __collate_st_chain_pri *)malloc(nchain * sizeof(struct __collate_st_chain_pri))) == NULL)
149 err(1, "chain malloc");
151 t = __collate_chain_pri_table;
152 for(i = 0; i < nchain; i++) {
153 if (chaindb->seq(chaindb, &key, &val, flags) != 0)
154 err(1, "Can't retrieve chaindb %d", i);
155 memcpy(&v, val.data, sizeof(struct __collate_st_chain_pri *));
157 if ((len = __collate_wcsnlen(v->str, STR_LEN)) > info.chain_max_len)
158 info.chain_max_len = len;
161 if (chaindb->seq(chaindb, &key, &val, flags) == 0)
162 err(1, "More in chaindb after retrieving %d", nchain);
163 qsort(__collate_chain_pri_table, nchain, sizeof(struct __collate_st_chain_pri), chainpricompar);
165 for(z = 0; z < directive_count; z++) {
168 struct __collate_st_subst *t;
173 if ((__collate_substitute_table[z] = (struct __collate_st_subst *)calloc(nsubst[z], sizeof(struct __collate_st_subst))) == NULL)
174 err(1, "__collate_substitute_table[%d] calloc", z);
176 t = __collate_substitute_table[z];
177 for(i = 0; i < nsubst[z]; i++) {
178 if (substdb[z]->seq(substdb[z], &key, &val, flags) != 0)
179 err(1, "Can't retrieve substdb[%d]", z);
180 memcpy(&cval, key.data, sizeof(int32_t));
181 /* we don't set the byte order of t->val, since we
182 * need it for sorting */
184 for(wp = (wchar_t *)val.data, tp = t->str, j = STR_LEN; *wp && j-- > 0;)
185 *tp++ = htonl(*wp++);
189 if (substdb[z]->seq(substdb[z], &key, &val, flags) == 0)
190 err(1, "More in substdb[%d] after retrieving %d", z, nsubst[z]);
191 qsort(__collate_substitute_table[z], nsubst[z], sizeof(struct __collate_st_subst), substcompar);
196 struct __collate_st_large_char_pri *t;
197 struct __collate_st_char_pri *p;
201 if ((__collate_large_char_pri_table = (struct __collate_st_large_char_pri *)malloc(nlargemap * sizeof(struct __collate_st_large_char_pri))) == NULL)
202 err(1, "nlargemap malloc");
204 t = __collate_large_char_pri_table;
205 for(i = 0; i < nlargemap; i++) {
206 if (largemapdb->seq(largemapdb, &key, &val, flags) != 0)
207 err(1, "Can't retrieve largemapdb %d", i);
208 memcpy(&cval, key.data, sizeof(int32_t));
209 memcpy(&p, val.data, sizeof(struct __collate_st_char_pri *));
210 /* we don't set the byte order of t->val, since we
211 * need it for sorting */
213 for(z = 0; z < directive_count; z++)
214 t->pri.pri[z] = htonl(p->pri[z]);
218 if (largemapdb->seq(largemapdb, &key, &val, flags) == 0)
219 err(1, "More in largemapdb after retrieving %d", nlargemap);
220 qsort(__collate_large_char_pri_table, nlargemap, sizeof(struct __collate_st_large_char_pri), charpricompar);
223 if (info.undef_pri[0] == PRI_UNDEFINED) {
225 info.undef_pri[0] = prim_pri;
226 for(i = 1; i < directive_count; i++)
227 info.undef_pri[i] = -prim_pri;
232 if (sym_undefined.val == PRI_UNDEFINED) {
236 while((ret = charmapdb->seq(charmapdb, &key, &val, flags)) == 0) {
237 memcpy(&v, val.data, sizeof(struct symbol *));
240 struct __collate_st_char_pri *p = haspri(v->u.wc);
241 if (!p || p->pri[0] == PRI_UNDEFINED)
242 warnx("<%s> was not defined", showwcs((const wchar_t *)key.data, key.size / sizeof(wchar_t)));
246 struct __collate_st_chain_pri *p = getchain(v->u.str, EXISTS);
247 if (p->pri[0] == PRI_UNDEFINED)
248 warnx("<%s> was not defined", showwcs((const wchar_t *)key.data, key.size / sizeof(wchar_t)));
255 err(1, "Error retrieving from charmapdb");
257 for (ch = 1; ch < UCHAR_MAX + 1; ch++) {
258 for(z = 0; z < directive_count; z++)
259 if (__collate_char_pri_table[ch].pri[z] == PRI_UNDEFINED)
260 __collate_char_pri_table[ch].pri[z] = (info.undef_pri[z] >= 0) ? info.undef_pri[z] : (ch - info.undef_pri[z]);
262 for (ch = 0; ch < nlargemap; ch++) {
263 for(z = 0; z < directive_count; z++)
264 if (__collate_large_char_pri_table[ch].pri.pri[z] == PRI_UNDEFINED)
265 __collate_large_char_pri_table[ch].pri.pri[z] = (info.undef_pri[z] >= 0) ? info.undef_pri[z] : (__collate_large_char_pri_table[ch].val - info.undef_pri[z]);
268 int ch, substed, ordered;
270 for (ch = 1; ch < UCHAR_MAX + 1; ch++) {
271 substed = hassubst(ch, 0);
272 ordered = (__collate_char_pri_table[ch].pri[0] != PRI_UNDEFINED);
273 if (!ordered && !substed) {
275 warnx("%s not found", charname(ch));
277 if (substed && ordered) {
279 warnx("%s can't be ordered since substituted", charname(ch));
286 /* COLLATE_SUBST_DUP depends on COLL_WEIGHTS_MAX == 2 */
288 if (nsubst[0] == nsubst[1] && (nsubst[0] == 0 ||
289 memcmp(__collate_substitute_table[0], __collate_substitute_table[1], nsubst[0] * sizeof(struct __collate_st_subst)) == 0)) {
290 info.flags |= COLLATE_SUBST_DUP;
294 info.flags |= COLLATE_SUBST_DUP;
298 for(z = 0; z < directive_count; z++)
299 info.subst_count[z] = nsubst[z];
301 info.directive_count = directive_count;
302 info.chain_count = nchain;
303 info.large_pri_count = nlargemap;
305 if ((fp = fopen(out_file, "w")) == NULL)
306 err(EX_UNAVAILABLE, "can't open destination file %s",
309 strcpy(__collate_version, COLLATE_VERSION1_1A);
310 if (fwrite(__collate_version, sizeof(__collate_version), 1, fp) != 1)
312 "IO error writting collate version to destination file %s",
314 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
315 for(z = 0; z < directive_count; z++) {
316 info.undef_pri[z] = htonl(info.undef_pri[z]);
317 info.subst_count[z] = htonl(info.subst_count[z]);
319 info.chain_count = htonl(info.chain_count);
320 info.large_pri_count = htonl(info.large_pri_count);
321 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
322 if (fwrite(&info, sizeof(info), 1, fp) != 1)
324 "IO error writting collate info to destination file %s",
326 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
329 struct __collate_st_char_pri *p = __collate_char_pri_table;
331 for(i = UCHAR_MAX + 1; i-- > 0; p++) {
332 for(z = 0; z < directive_count; z++)
333 p->pri[z] = htonl(p->pri[z]);
336 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
337 if (fwrite(__collate_char_pri_table,
338 sizeof(__collate_char_pri_table), 1, fp) != 1)
340 "IO error writting char table to destination file %s",
342 for(z = 0; z < directive_count; z++) {
344 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
345 struct __collate_st_subst *t = __collate_substitute_table[z];
347 for(i = nsubst[z]; i > 0; i--) {
348 t->val = htonl(t->val);
351 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
352 if (fwrite(__collate_substitute_table[z], sizeof(struct __collate_st_subst), nsubst[z], fp) != nsubst[z])
354 "IO error writting large substprim table %d to destination file %s",
359 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
361 struct __collate_st_chain_pri *p = __collate_chain_pri_table;
364 for(i = nchain; i-- > 0; p++) {
365 for(j = STR_LEN, w = p->str; *w && j-- > 0; w++)
367 for(z = 0; z < directive_count; z++)
368 p->pri[z] = htonl(p->pri[z]);
370 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
371 if (fwrite(__collate_chain_pri_table,
372 sizeof(*__collate_chain_pri_table), nchain, fp) !=
375 "IO error writting chain table to destination file %s",
380 #if __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN
381 struct __collate_st_large_char_pri *t = __collate_large_char_pri_table;
383 for(i = 0; i < nlargemap; i++) {
384 t->val = htonl(t->val);
387 #endif /* __DARWIN_BYTE_ORDER == __DARWIN_LITTLE_ENDIAN */
388 if (fwrite(__collate_large_char_pri_table, sizeof(struct __collate_st_large_char_pri), nlargemap, fp) != nlargemap)
390 "IO error writting large pri tables to destination file %s",
395 err(EX_IOERR, "IO error closing destination file %s",
400 collate_print_tables();
405 datafile : statment_list
406 | blank_lines start_localedef localedef_sections blank_lines end_localedef blank_lines
408 statment_list : statment
409 | statment_list '\n' statment
420 start_localedef : START_LC_COLLATE '\n' {
422 if ((stringdb = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
423 err(1, "dbopen stringdb");
425 for(i = 0; i < COLL_WEIGHTS_MAX; i++)
426 info.directive[i] = DIRECTIVE_UNDEF;
429 end_localedef : END_LC_COLLATE '\n'
431 localedef_sections : localedef_preface localedef_order
433 localedef_preface : localedef_statment '\n'
434 | localedef_preface localedef_statment '\n'
441 collating_element : COLLATING_ELEMENT ELEM FROM STRING {
444 if (wcslen($2) > CHARMAP_SYMBOL_LEN)
445 yyerror("collating-element symbol name '%s' is too long", showwcs($2, CHARMAP_SYMBOL_LEN));
446 if ((len = wcslen($4)) > STR_LEN)
447 yyerror("collating-element string '%s' is too long", showwcs($4, STR_LEN));
449 yyerror("collating-element string '%s' must be at least two characters", showwcs($4, STR_LEN));
450 s = getsymbol($2, NOTEXISTS);
451 s->val = PRI_UNDEFINED;
452 s->type = SYMBOL_CHAIN;
453 wcsncpy(s->u.str, $4, STR_LEN);
454 getchain($4, NOTEXISTS);
457 collating_symbol : COLLATING_SYMBOL ELEM {
459 if (wcslen($2) > CHARMAP_SYMBOL_LEN)
460 yyerror("collating-element symbol name '%s' is too long", showwcs($2, CHARMAP_SYMBOL_LEN));
461 s = getsymbol($2, NOTEXISTS);
462 s->val = PRI_UNDEFINED;
463 s->type = SYMBOL_SYMBOL;
466 localedef_order : order_start order_lines1 order_second_pass order_lines2 order_end
468 order_start: ORDER_START order_start_list '\n'
470 order_second_pass: ORDER_SECOND_PASS {
471 prev_line = LINE_NONE;
476 order_start_list : order_start_list_directives {
477 if (directive_count > 0)
478 yyerror("Multiple order_start lines not allowed");
479 if ((info.directive[0] & DIRECTIVE_DIRECTION_MASK) == 0)
480 info.directive[0] |= DIRECTIVE_FORWARD;
483 | order_start_list ';' order_start_list_directives {
484 if (directive_count >= COLL_WEIGHTS_MAX)
485 yyerror("only COLL_WEIGHTS_MAX weights allowed");
486 if ((info.directive[directive_count] & DIRECTIVE_DIRECTION_MASK) == 0)
487 info.directive[directive_count] |= DIRECTIVE_FORWARD;
491 order_start_list_directives : ORDER_DIRECTIVE {
492 info.directive[directive_count] = $1;
494 | order_start_list_directives ',' ORDER_DIRECTIVE {
495 int direction = ($3 & DIRECTIVE_DIRECTION_MASK);
496 int prev = (info.directive[directive_count] & DIRECTIVE_DIRECTION_MASK);
497 if (direction && prev && direction != prev)
498 yyerror("The forward and backward directives are mutually exclusive");
499 info.directive[directive_count] |= $3;
502 order_lines1 : order_line1 '\n'
503 | order_lines1 order_line1 '\n'
507 struct symbol *s = getsymbol($1, EXISTS);
508 if (s->val != PRI_UNDEFINED)
509 yyerror("<%s> redefined", showwcs($1, CHARMAP_SYMBOL_LEN));
510 if (prev_line == LINE_ELLIPSIS) {
516 yyerror("Chain <%s> can't be endpoints of ellipsis", showwcs($1, CHARMAP_SYMBOL_LEN));
518 yyerror("Collating symbol <%s> can't be endpoints of ellipsis", showwcs($1, CHARMAP_SYMBOL_LEN));
520 if (s->u.wc <= prev_elem->u.wc)
521 yyerror("<%s> is before starting point of ellipsis", showwcs($1, CHARMAP_SYMBOL_LEN));
522 for(i = prev_elem->u.wc + 1, v = prev_elem->val + 1; i < s->u.wc; i++, v++) {
523 m = getsymbolbychar(i);
524 if (m->val != PRI_UNDEFINED)
525 yyerror("<%s> was previously defined while filling ellipsis symbols", showwcs(m->name, CHARMAP_SYMBOL_LEN));
531 prim_pri = s->val + 1;
535 struct symbol *s = getsymbol($1, EXISTS);
536 if (s->type == SYMBOL_SYMBOL) {
537 if (weight_index != 0)
538 yyerror("Can't specify weights for collating symbol <%s>", showwcs($1, CHARMAP_SYMBOL_LEN));
539 } else if (weight_index == 0) {
540 for(i = 0; i < directive_count; i++)
542 } else if (weight_index != directive_count)
543 yyerror("Not enough weights specified");
544 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
545 prev_line = LINE_NORMAL;
548 | ELLIPSIS { weight_index = 0; allow_ellipsis = 1; } weights {
550 if (prev_line == LINE_ELLIPSIS)
551 yyerror("Illegal sequential ellipsis lines");
552 if (prev_line == LINE_UNDEFINED)
553 yyerror("Ellipsis line can not follow UNDEFINED line");
554 if (prev_line == LINE_NONE)
555 yyerror("Ellipsis line must follow a collating identifier lines");
556 if (weight_index == 0) {
557 for(i = 0; i < directive_count; i++)
558 weight_table[i] = &sym_ellipsis;
559 } else if (weight_index != directive_count)
560 yyerror("Not enough weights specified");
561 for(i = 0; i < directive_count; i++) {
562 if (weight_table[i]->type != SYMBOL_ELLIPSIS)
564 switch (prev_weight_table[i]->type) {
566 yyerror("Startpoint of ellipsis can't be a collating element");
568 yyerror("Startpoint of ellipsis can't be IGNORE");
570 yyerror("Startpoint of ellipsis can't be a collating symbol");
572 yyerror("Startpoint of ellipsis can't be a string");
575 memcpy(prev2_weight_table, prev_weight_table, sizeof(prev_weight_table));
576 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
577 prev_line = LINE_ELLIPSIS;
581 if (sym_undefined.val != PRI_UNDEFINED)
582 yyerror("Multiple UNDEFINED lines not allowed");
583 sym_undefined.val = prim_pri++;
588 if (weight_index == 0) {
589 weight_table[0] = &sym_undefined;
590 for(i = 1; i < directive_count; i++)
591 weight_table[i] = &sym_ellipsis;
592 } else if (weight_index != directive_count)
593 yyerror("Not enough weights specified");
594 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
595 prev_line = LINE_UNDEFINED;
598 order_lines2 : order_line2 '\n'
599 | order_lines2 order_line2 '\n'
602 | ELEM { weight_index = 0; } weights {
604 struct symbol *s = getsymbol($1, EXISTS);
605 if (s->val == PRI_UNDEFINED)
606 yyerror("<%s> undefined", showwcs($1, CHARMAP_SYMBOL_LEN));
607 if (s->type == SYMBOL_SYMBOL) {
608 if (weight_index != 0)
609 yyerror("Can't specify weights for collating symbol <%s>", showwcs($1, CHARMAP_SYMBOL_LEN));
610 } else if (weight_index == 0) {
611 for(i = 0; i < directive_count; i++)
613 } else if (weight_index != directive_count)
614 yyerror("Not enough weights specified");
615 if (prev_line == LINE_ELLIPSIS) {
617 for(i = 0; i < directive_count; i++) {
618 switch (prev_weight_table[i]->type) {
623 for (w = prev_elem->u.wc + 1; w < s->u.wc; w++) {
624 struct __collate_st_char_pri *p = getpri(w);
625 if (p->pri[i] != PRI_UNDEFINED)
626 yyerror("Char 0x02x previously defined", w);
627 p->pri[i] = prev_weight_table[i]->val;
630 case SYMBOL_ELLIPSIS:
632 switch (weight_table[i]->type) {
634 yyerror("Strings can't be endpoints of ellipsis");
636 yyerror("Chains can't be endpoints of ellipsis");
638 yyerror("IGNORE can't be endpoints of ellipsis");
640 yyerror("Collation symbols can't be endpoints of ellipsis");
642 if (s->val - prev_elem->val != weight_table[i]->val - prev2_weight_table[i]->val)
643 yyerror("Range mismatch in weight %d", i);
644 x = prev2_weight_table[i]->val + 1;
645 for (w = prev_elem->u.wc + 1; w < s->u.wc; w++) {
646 struct __collate_st_char_pri *p = getpri(w);
647 if (p->pri[i] != PRI_UNDEFINED)
648 yyerror("Char 0x02x previously defined", w);
653 for (w = prev_elem->u.wc + 1; w < s->u.wc; w++) {
654 struct __collate_st_char_pri *p = getpri(w);
655 if (p->pri[i] != PRI_UNDEFINED)
656 yyerror("Char 0x02x previously defined", w);
657 putsubst(w, i, prev_weight_table[i]->u.str);
658 p->pri[i] = prev_weight_table[i]->val;
666 struct __collate_st_char_pri *p = getpri(s->u.wc);
667 for(i = 0; i < directive_count; i++) {
668 switch (weight_table[i]->type) {
673 if (p->pri[i] != PRI_UNDEFINED)
674 yyerror("Char 0x02x previously defined", s->u.wc);
675 p->pri[i] = weight_table[i]->val;
678 if (p->pri[i] != PRI_UNDEFINED)
679 yyerror("Char 0x02x previously defined", s->u.wc);
680 putsubst(s->u.wc, i, weight_table[i]->u.str);
681 p->pri[i] = weight_table[i]->val;
688 struct __collate_st_chain_pri *p = getchain(s->u.str, EXISTS);
689 for(i = 0; i < directive_count; i++) {
690 switch (weight_table[i]->type) {
695 if (p->pri[i] != PRI_UNDEFINED)
696 yyerror("Chain %s previously defined", showwcs(s->u.str, STR_LEN));
697 p->pri[i] = weight_table[i]->val;
700 if (wcsncmp(s->u.str, weight_table[i]->u.str, STR_LEN) != 0)
701 yyerror("Chain/string mismatch");
702 if (p->pri[i] != PRI_UNDEFINED)
703 yyerror("Chain %s previously defined", showwcs(s->u.str, STR_LEN));
704 /* negative value mean don't substitute
705 * the chain, but it is in an
706 * equivalence class */
707 p->pri[i] = -weight_table[i]->val;
713 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
714 prev_line = LINE_NORMAL;
717 | ELLIPSIS { weight_index = 0; allow_ellipsis = 1; } weights {
719 if (prev_line == LINE_ELLIPSIS)
720 yyerror("Illegal sequential ellipsis lines");
721 if (prev_line == LINE_UNDEFINED)
722 yyerror("Ellipsis line can not follow UNDEFINED line");
723 if (prev_line == LINE_NONE)
724 yyerror("Ellipsis line must follow a collating identifier lines");
725 if (weight_index == 0) {
726 for(i = 0; i < directive_count; i++)
727 weight_table[i] = &sym_ellipsis;
728 } else if (weight_index != directive_count)
729 yyerror("Not enough weights specified");
730 for(i = 0; i < directive_count; i++) {
731 if (weight_table[i]->type != SYMBOL_ELLIPSIS)
733 switch (prev_weight_table[i]->type) {
735 yyerror("Startpoint of ellipsis can't be a collating element");
737 yyerror("Startpoint of ellipsis can't be IGNORE");
739 yyerror("Startpoint of ellipsis can't be a collating symbol");
741 yyerror("Startpoint of ellipsis can't be a string");
744 memcpy(prev2_weight_table, prev_weight_table, sizeof(prev_weight_table));
745 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
746 prev_line = LINE_ELLIPSIS;
749 | UNDEFINED { weight_index = 0; allow_ellipsis = 1; } weights {
752 if (weight_index == 0) {
753 weight_table[0] = &sym_undefined;
754 for(i = 1; i < directive_count; i++)
755 weight_table[i] = &sym_ellipsis;
756 } else if (weight_index != directive_count)
757 yyerror("Not enough weights specified");
758 for(i = 0; i < directive_count; i++) {
759 switch (weight_table[i]->type) {
764 info.undef_pri[i] = weight_table[i]->val;
766 case SYMBOL_ELLIPSIS :
767 /* Negative values mean that the priority is
768 * relative to the lexical value */
769 info.undef_pri[i] = -sym_undefined.val;
770 prim_pri = UNDEFINED_PRI;
773 yyerror("Strings can't be used with UNDEFINED");
776 memcpy(prev_weight_table, weight_table, sizeof(weight_table));
777 prev_line = LINE_UNDEFINED;
786 if (weight_index >= directive_count)
787 yyerror("More weights than specified by order_start");
788 s = getsymbol($1, EXISTS);
789 if (order_pass && s->val == PRI_UNDEFINED)
790 yyerror("<%s> is undefined", showwcs($1, CHARMAP_SYMBOL_LEN));
791 weight_table[weight_index++] = s;
794 if (weight_index >= directive_count)
795 yyerror("More weights than specified by order_start");
797 yyerror("Ellipsis weight not allowed");
798 weight_table[weight_index++] = &sym_ellipsis;
801 if (weight_index >= directive_count)
802 yyerror("More weights than specified by order_start");
803 weight_table[weight_index++] = &sym_ignore;
806 if (weight_index >= directive_count)
807 yyerror("More weights than specified by order_start");
808 if (wcslen($1) > STR_LEN)
809 yyerror("String '%s' is too long", showwcs($1, STR_LEN));
810 weight_table[weight_index++] = getstring($1);
813 order_end : ORDER_END '\n'
815 charmap : DEFN CHAR {
816 int len = wcslen($1);
818 if (len > CHARMAP_SYMBOL_LEN)
819 yyerror("Charmap symbol name '%s' is too long", showwcs($1, CHARMAP_SYMBOL_LEN));
820 s = getsymbol($1, NOTEXISTS);
821 s->type = SYMBOL_CHAR;
822 s->val = PRI_UNDEFINED;
827 substitute : SUBSTITUTE CHAR WITH STRING {
828 if (wcslen($4) + 1 > STR_LEN)
829 yyerror("%s substitution is too long", charname($2));
833 order : ORDER order_list
836 | order_list ';' item
841 if (curr_chain[0] == '\0' || curr_chain[1] == '\0')
842 yyerror("\\0 can't be chained");
843 curr_chain[2] = '\0';
846 static wchar_t tb[2];
849 yyerror("\\0 can't be chained");
850 if (wcslen(curr_chain) + 1 > STR_LEN)
851 yyerror("Chain '%s' grows too long", curr_chain);
852 (void)wcscat(curr_chain, tb);
856 struct __collate_st_char_pri *p = getpri($1);
858 yyerror("%s duplicated", charname($1));
859 p->pri[0] = p->pri[1] = prim_pri;
860 sec_pri = ++prim_pri;
863 struct __collate_st_chain_pri *c = getchain(curr_chain, NOTEXISTS);
864 c->pri[0] = c->pri[1] = prim_pri;
865 sec_pri = ++prim_pri;
869 struct __collate_st_char_pri *p;
872 yyerror("Illegal range %s -- %s", charname($1), charname2($3));
874 for (i = $1; i <= $3; i++) {
877 yyerror("%s duplicated", charname(i));
878 p->pri[0] = p->pri[1] = prim_pri++;
882 | '{' mixed_order_list '}' {
885 | '(' sec_order_list ')' {
889 mixed_order_list : mixed_sub_list {
892 | mixed_order_list ';' mixed_sub_list {
896 mixed_sub_list : mixed_sub_item
897 | mixed_sub_list ',' mixed_sub_item
899 sec_order_list : sec_sub_item
900 | sec_order_list ',' sec_sub_item
902 mixed_sub_item : CHAR {
903 struct __collate_st_char_pri *p = getpri($1);
905 yyerror("%s duplicated", charname($1));
906 p->pri[0] = prim_pri;
911 struct __collate_st_char_pri *p;
914 yyerror("Illegal range %s -- %s",
915 charname($1), charname2($3));
917 for (i = $1; i <= $3; i++) {
920 yyerror("%s duplicated", charname(i));
921 p->pri[0] = prim_pri;
926 struct __collate_st_chain_pri *c = getchain(curr_chain, NOTEXISTS);
927 c->pri[0] = prim_pri;
930 sec_sub_item : CHAR {
931 struct __collate_st_char_pri *p = getpri($1);
933 yyerror("%s duplicated", charname($1));
934 p->pri[0] = prim_pri;
935 p->pri[1] = sec_pri++;
939 struct __collate_st_char_pri *p;
942 yyerror("Illegal range %s -- %s",
943 charname($1), charname2($3));
945 for (i = $1; i <= $3; i++) {
948 yyerror("%s duplicated", charname(i));
949 p->pri[0] = prim_pri;
950 p->pri[1] = sec_pri++;
954 struct __collate_st_chain_pri *c = getchain(curr_chain, NOTEXISTS);
955 c->pri[0] = prim_pri;
956 c->pri[1] = sec_pri++;
961 main(int ac, char **av)
965 if ((charmapdb = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
966 err(1, "dbopen charmapdb");
967 if ((charmapdb2 = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
968 err(1, "dbopen charmapdb");
969 if ((largemapdb = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
970 err(1, "dbopen largemapdb");
971 if ((substdb[0] = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
972 err(1, "dbopen substdb[0]");
973 if ((chaindb = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
974 err(1, "dbopen chaindb");
975 /* -1 means an undefined priority, which we adjust after parsing */
976 for (ch = 0; ch <= UCHAR_MAX; ch++)
977 for(z = 0; z < COLL_WEIGHTS_MAX; z++)
978 __collate_char_pri_table[ch].pri[z] = PRI_UNDEFINED;
980 while((ch = getopt(ac, av, ":do:I:")) != -1) {
982 while((ch = getopt(ac, av, ":o:I:")) != -1) {
996 strlcpy(map_name, optarg, sizeof(map_name));
1006 if ((yyin = fopen(*av, "r")) == NULL)
1007 err(EX_UNAVAILABLE, "can't open source file %s", *av);
1013 static struct __collate_st_char_pri *
1017 struct __collate_st_char_pri *p;
1021 return &__collate_char_pri_table[c];
1023 key.size = sizeof(int32_t);
1024 if ((ret = largemapdb->get(largemapdb, &key, &val, 0)) < 0)
1025 err(1, "getpri: Error getting %s", charname(c));
1027 struct __collate_st_char_pri *pn;
1029 if ((pn = (struct __collate_st_char_pri *)malloc(sizeof(struct __collate_st_char_pri))) == NULL)
1030 err(1, "getpri: malloc");
1031 for(z = 0; z < COLL_WEIGHTS_MAX; z++)
1032 pn->pri[z] = PRI_UNDEFINED;
1034 val.size = sizeof(struct __collate_st_char_pri *);
1035 if (largemapdb->put(largemapdb, &key, &val, 0) < 0)
1036 err(1, "getpri: Error storing %s", charname(c));
1039 memcpy(&p, val.data, sizeof(struct __collate_st_char_pri *));
1043 static struct __collate_st_char_pri *
1047 struct __collate_st_char_pri *p;
1051 return &__collate_char_pri_table[c];
1053 key.size = sizeof(int32_t);
1054 if ((ret = largemapdb->get(largemapdb, &key, &val, 0)) < 0)
1055 err(1, "haspri: Error getting %s", charname(c));
1058 memcpy(&p, val.data, sizeof(struct __collate_st_char_pri *));
1062 static struct __collate_st_chain_pri *
1063 getchain(const wchar_t *wcs, int exists)
1066 struct __collate_st_chain_pri *p;
1069 key.data = (void *)wcs;
1070 key.size = __collate_wcsnlen(wcs, STR_LEN) * sizeof(wchar_t);
1071 if ((ret = chaindb->get(chaindb, &key, &val, 0)) < 0)
1072 err(1, "getchain: Error getting \"%s\"", showwcs(wcs, STR_LEN));
1074 struct __collate_st_chain_pri *pn;
1077 errx(1, "getchain: \"%s\" is not defined", showwcs(wcs, STR_LEN));
1078 if ((pn = (struct __collate_st_chain_pri *)malloc(sizeof(struct __collate_st_chain_pri))) == NULL)
1079 err(1, "getchain: malloc");
1080 for(z = 0; z < COLL_WEIGHTS_MAX; z++)
1081 pn->pri[z] = PRI_UNDEFINED;
1082 bzero(pn->str, sizeof(pn->str));
1083 wcsncpy(pn->str, wcs, STR_LEN);
1085 val.size = sizeof(struct __collate_st_chain_pri *);
1086 if (chaindb->put(chaindb, &key, &val, 0) < 0)
1087 err(1, "getchain: Error storing \"%s\"", showwcs(wcs, STR_LEN));
1089 } else if (exists == 0)
1090 errx(1, "getchain: \"%s\" already exists", showwcs(wcs, STR_LEN));
1091 memcpy(&p, val.data, sizeof(struct __collate_st_chain_pri *));
1096 getsymbol(const wchar_t *wcs, int exists)
1102 key.data = (void *)wcs;
1103 key.size = wcslen(wcs) * sizeof(wchar_t);
1104 if ((ret = charmapdb->get(charmapdb, &key, &val, 0)) < 0)
1105 err(1, "getsymbol: Error getting \"%s\"", showwcs(wcs, CHARMAP_SYMBOL_LEN));
1109 errx(1, "getsymbol: \"%s\" is not defined", showwcs(wcs, CHARMAP_SYMBOL_LEN));
1110 if ((pn = (struct symbol *)malloc(sizeof(struct symbol))) == NULL)
1111 err(1, "getsymbol: malloc");
1112 pn->val = PRI_UNDEFINED;
1113 wcsncpy(pn->name, wcs, CHARMAP_SYMBOL_LEN);
1115 val.size = sizeof(struct symbol *);
1116 if (charmapdb->put(charmapdb, &key, &val, 0) < 0)
1117 err(1, "getsymbol: Error storing \"%s\"", showwcs(wcs, CHARMAP_SYMBOL_LEN));
1118 } else if (exists == 0)
1119 errx(1, "getsymbol: \"%s\" already exists", showwcs(wcs, CHARMAP_SYMBOL_LEN));
1120 memcpy(&p, val.data, sizeof(struct symbol *));
1124 static struct symbol *
1125 getsymbolbychar(wchar_t wc)
1132 key.size = sizeof(wchar_t);
1133 if ((ret = charmapdb2->get(charmapdb2, &key, &val, 0)) < 0)
1134 err(1, "getsymbolbychar: Error getting Char 0x%02x", wc);
1136 errx(1, "getsymbolbychar: Char 0x%02x is not defined", wc);
1137 memcpy(&p, val.data, sizeof(struct symbol *));
1141 static struct symbol *
1142 hassymbolbychar(wchar_t wc)
1149 key.size = sizeof(wchar_t);
1150 if ((ret = charmapdb2->get(charmapdb2, &key, &val, 0)) < 0)
1151 err(1, "hassymbolbychar: Error getting Char 0x%02x", wc);
1154 memcpy(&p, val.data, sizeof(struct symbol *));
1159 setsymbolbychar(struct symbol *s)
1165 key.data = &s->u.wc;
1166 key.size = sizeof(wchar_t);
1168 val.size = sizeof(struct symbol *);
1169 if (charmapdb2->put(charmapdb2, &key, &val, 0) < 0)
1170 err(1, "setsymbolbychar: Error storing <%s>", showwcs(s->name, CHARMAP_SYMBOL_LEN));
1174 getstring(const wchar_t *wcs)
1180 key.data = (void *)wcs;
1181 key.size = wcslen(wcs) * sizeof(wchar_t);
1182 if ((ret = stringdb->get(stringdb, &key, &val, 0)) < 0)
1183 err(1, "getstring: Error getting \"%s\"", showwcs(wcs, STR_LEN));
1186 if ((pn = (struct symbol *)malloc(sizeof(struct symbol))) == NULL)
1187 err(1, "getstring: malloc");
1188 pn->type = SYMBOL_STRING;
1189 pn->val = prim_pri++;
1190 wcsncpy(pn->u.str, wcs, STR_LEN);
1192 val.size = sizeof(struct symbol *);
1193 if (stringdb->put(stringdb, &key, &val, 0) < 0)
1194 err(1, "getstring: Error storing \"%s\"", showwcs(wcs, STR_LEN));
1196 memcpy(&p, val.data, sizeof(struct symbol *));
1201 makeforwardref(int i, const struct symbol *from, const struct symbol * to)
1206 putsubst(int32_t c, int i, const wchar_t *str)
1210 wchar_t clean[STR_LEN];
1213 if ((substdb[i] = dbopen(NULL, O_CREAT | O_RDWR, 0600, DB_HASH, NULL)) == NULL)
1214 err(1, "dbopen substdb[%d]", i);
1216 key.size = sizeof(int32_t);
1217 bzero(clean, sizeof(clean));
1218 wcsncpy(clean, str, STR_LEN);
1220 val.size = sizeof(clean);
1221 if ((ret = substdb[i]->put(substdb[i], &key, &val, R_NOOVERWRITE)) < 0)
1222 err(1, "putsubst: Error on %s", charname(c));
1224 errx(1, "putsubst: Duplicate substitution of %s", charname(c));
1229 hassubst(int32_t c, int i)
1237 key.size = sizeof(int32_t);
1238 if ((ret = substdb[i]->get(substdb[i], &key, &val, 0)) < 0)
1239 err(1, "hassubst: Error getting %s", charname(c));
1244 chainpricompar(const void *a, const void *b)
1246 return wcsncmp(((struct __collate_st_chain_pri *)a)->str, ((struct __collate_st_chain_pri *)b)->str, STR_LEN);
1250 charpricompar(const void *a, const void *b)
1252 return ((struct __collate_st_large_char_pri *)a)->val - ((struct __collate_st_large_char_pri *)b)->val;
1256 substcompar(const void *a, const void *b)
1258 return ((struct __collate_st_subst *)a)->val - ((struct __collate_st_subst *)b)->val;
1261 static const wchar_t *
1262 __collate_wcsnchr(const wchar_t *s, wchar_t c, int len)
1264 while (*s && len > 0) {
1274 __collate_wcsnlen(const wchar_t *s, int len)
1277 while (*s && n < len) {
1287 fprintf(stderr, "usage: colldef [-o out_file] [-I map_dir] [filename]\n");
1292 yyerror(const char *fmt, ...)
1298 vsnprintf(msg, sizeof(msg), fmt, ap);
1300 errx(EX_UNAVAILABLE, "%s, near line %d", msg, line_no);
1304 showwcs(const wchar_t *t, int len)
1306 static char buf[8* CHARMAP_SYMBOL_LEN];
1309 for(; *t && len > 0; len--, t++) {
1310 if (*t >=32 && *t <= 126)
1313 sprintf(cp, "\\x{%02x}", *t);
1322 charname(wchar_t wc)
1324 static char buf[CHARMAP_SYMBOL_LEN + 1];
1325 struct symbol *s = hassymbolbychar(wc);
1328 strcpy(buf, showwcs(s->name, CHARMAP_SYMBOL_LEN));
1330 sprintf(buf, "Char 0x%02x", wc);
1335 charname2(wchar_t wc)
1337 static char buf[CHARMAP_SYMBOL_LEN + 1];
1338 struct symbol *s = hassymbolbychar(wc);
1341 strcpy(buf, showwcs(s->name, CHARMAP_SYMBOL_LEN));
1343 sprintf(buf, "Char 0x%02x", wc);
1347 #ifdef COLLATE_DEBUG
1353 if (c >=32 && c <= 126)
1354 sprintf(buf, "'%c' ", c);
1356 sprintf(buf, "\\x{%02x}", c);
1361 collate_print_tables(void)
1365 printf("Info: p=%d s=%d f=0x%02x m=%d dc=%d up=%d us=%d pc=%d sc=%d cc=%d lc=%d\n",
1366 info.directive[0], info.directive[1],
1367 info.flags, info.chain_max_len,
1368 info.directive_count,
1369 info.undef_pri[0], info.undef_pri[1],
1370 info.subst_count[0], info.subst_count[1],
1371 info.chain_count, info.large_pri_count);
1372 for(z = 0; z < info.directive_count; z++) {
1373 if (info.subst_count[z] > 0) {
1374 struct __collate_st_subst *p2 = __collate_substitute_table[z];
1375 if (z == 0 && (info.flags & COLLATE_SUBST_DUP))
1376 printf("Both substitute tables:\n");
1378 printf("Substitute table %d:\n", z);
1379 for (i = info.subst_count[z]; i-- > 0; p2++)
1380 printf("\t%s --> \"%s\"\n",
1382 showwcs(p2->str, STR_LEN));
1385 if (info.chain_count > 0) {
1386 printf("Chain priority table:\n");
1387 struct __collate_st_chain_pri *p2 = __collate_chain_pri_table;
1388 for (i = info.chain_count; i-- > 0; p2++) {
1389 printf("\t\"%s\" :", showwcs(p2->str, STR_LEN));
1390 for(z = 0; z < info.directive_count; z++)
1391 printf(" %d", p2->pri[z]);
1395 printf("Char priority table:\n");
1397 struct __collate_st_char_pri *p2 = __collate_char_pri_table;
1398 for (i = 0; i < UCHAR_MAX + 1; i++, p2++) {
1399 printf("\t%s :", show(i));
1400 for(z = 0; z < info.directive_count; z++)
1401 printf(" %d", p2->pri[z]);
1405 if (info.large_pri_count > 0) {
1406 struct __collate_st_large_char_pri *p2 = __collate_large_char_pri_table;
1407 printf("Large priority table:\n");
1408 for (i = info.large_pri_count; i-- > 0; p2++) {
1409 printf("\t%s :", show(p2->val));
1410 for(z = 0; z < info.directive_count; z++)
1411 printf(" %d", p2->pri.pri[z]);