2 * Copyright 2018 Nexenta Systems, Inc.
3 * Copyright 2015 John Marino <draco@marino.st>
5 * This source code is derived from the illumos localedef command, and
6 * provided under BSD-style license terms by Nexenta Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * LC_COLLATE database generation routines for localedef.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
48 #include "localedef.h"
55 * It will be extremely helpful to the reader if they have access to
56 * the localedef and locale file format specifications available.
57 * Latest versions of these are available from www.opengroup.org.
59 * The design for the collation code is a bit complex. The goal is a
60 * single collation database as described in collate.h (in
61 * libc/port/locale). However, there are some other tidbits:
63 * a) The substitution entries are now a directly indexable array. A
64 * priority elsewhere in the table is taken as an index into the
65 * substitution table if it has a high bit (COLLATE_SUBST_PRIORITY)
66 * set. (The bit is cleared and the result is the index into the
69 * b) We eliminate duplicate entries into the substitution table.
70 * This saves a lot of space.
72 * c) The priorities for each level are "compressed", so that each
73 * sorting level has consecutively numbered priorities starting at 1.
74 * (O is reserved for the ignore priority.) This means sort levels
75 * which only have a few distinct priorities can represent the
76 * priority level in fewer bits, which makes the strxfrm output
79 * d) We record the total number of priorities so that strxfrm can
80 * figure out how many bytes to expand a numeric priority into.
82 * e) For the UNDEFINED pass (the last pass), we record the maximum
83 * number of bits needed to uniquely prioritize these entries, so that
84 * the last pass can also use smaller strxfrm output when possible.
86 * f) Priorities with the sign bit set are verboten. This works out
87 * because no active character set needs that bit to carry significant
88 * information once the character is in wide form.
90 * To process the entire data to make the database, we actually run
91 * multiple passes over the data.
93 * The first pass, which is done at parse time, identifies elements,
94 * substitutions, and such, and records them in priority order. As
95 * some priorities can refer to other priorities, using forward
96 * references, we use a table of references indicating whether the
97 * priority's value has been resolved, or whether it is still a
100 * The second pass walks over all the items in priority order, noting
101 * that they are used directly, and not just an indirect reference.
102 * This is done by creating a "weight" structure for the item. The
103 * weights are stashed in an RB tree sorted by relative "priority".
105 * The third pass walks over all the weight structures, in priority
106 * order, and assigns a new monotonically increasing (per sort level)
107 * weight value to them. These are the values that will actually be
108 * written to the file.
110 * The fourth pass just writes the data out.
114 * In order to resolve the priorities, we create a table of priorities.
115 * Entries in the table can be in one of three states.
117 * UNKNOWN is for newly allocated entries, and indicates that nothing
118 * is known about the priority. (For example, when new entries are created
119 * for collating-symbols, this is the value assigned for them until the
120 * collating symbol's order has been determined.
122 * RESOLVED is used for an entry where the priority indicates the final
125 * REFER is used for entries that reference other entries. Typically
126 * this is used for forward references. A collating-symbol can never
129 * The "pass" field is used during final resolution to aid in detection
130 * of referencing loops. (For example <A> depends on <B>, but <B> has its
131 * priority dependent on <A>.)
134 UNKNOWN, /* priority is totally unknown */
135 RESOLVED, /* priority value fully resolved */
136 REFER /* priority is a reference (index) */
139 typedef struct weight {
142 RB_ENTRY(weight) entry;
145 typedef struct priority {
152 #define NUM_WT collinfo.directive_count
155 * These are the abstract collating symbols, which are just a symbolic
156 * way to reference a priority.
161 RB_ENTRY(collsym) entry;
165 * These are also abstract collating symbols, but we allow them to have
166 * different priorities at different levels.
168 typedef struct collundef {
170 int32_t ref[COLL_WEIGHTS_MAX];
171 RB_ENTRY(collundef) entry;
175 * These are called "chains" in libc. This records the fact that two
176 * more characters should be treated as a single collating entity when
177 * they appear together. For example, in Spanish <C><h> gets collated
178 * as a character between <C> and <D>.
183 int32_t ref[COLL_WEIGHTS_MAX];
184 RB_ENTRY(collelem) rb_bysymbol;
185 RB_ENTRY(collelem) rb_byexpand;
189 * Individual characters have a sequence of weights as well.
191 typedef struct collchar {
193 int32_t ref[COLL_WEIGHTS_MAX];
194 RB_ENTRY(collchar) entry;
198 * Substitution entries. The key is itself a priority. Note that
199 * when we create one of these, we *automatically* wind up with a
200 * fully resolved priority for the key, because creation of
201 * substitutions creates a resolved priority at the same time.
203 typedef struct subst{
205 int32_t ref[COLLATE_STR_LEN];
206 RB_ENTRY(subst) entry;
207 RB_ENTRY(subst) entry_ref;
210 static RB_HEAD(collsyms, collsym) collsyms;
211 static RB_HEAD(collundefs, collundef) collundefs;
212 static RB_HEAD(elem_by_symbol, collelem) elem_by_symbol;
213 static RB_HEAD(elem_by_expand, collelem) elem_by_expand;
214 static RB_HEAD(collchars, collchar) collchars;
215 static RB_HEAD(substs, subst) substs[COLL_WEIGHTS_MAX];
216 static RB_HEAD(substs_ref, subst) substs_ref[COLL_WEIGHTS_MAX];
217 static RB_HEAD(weights, weight) weights[COLL_WEIGHTS_MAX];
218 static int32_t nweight[COLL_WEIGHTS_MAX];
221 * This is state tracking for the ellipsis token. Note that we start
222 * the initial values so that the ellipsis logic will think we got a
223 * magic starting value of NUL. It starts at minus one because the
224 * starting point is exclusive -- i.e. the starting point is not
225 * itself handled by the ellipsis code.
227 static int currorder = EOF;
228 static int lastorder = EOF;
229 static collelem_t *currelem;
230 static collchar_t *currchar;
231 static collundef_t *currundef;
232 static wchar_t ellipsis_start = 0;
233 static int32_t ellipsis_weights[COLL_WEIGHTS_MAX];
236 * We keep a running tally of weights.
238 static int nextpri = 1;
239 static int nextsubst[COLL_WEIGHTS_MAX] = { 0 };
242 * This array collects up the weights for each level.
244 static int32_t order_weights[COLL_WEIGHTS_MAX];
245 static int curr_weight = 0;
246 static int32_t subst_weights[COLLATE_STR_LEN];
247 static int curr_subst = 0;
250 * Some initial priority values.
252 static int32_t pri_undefined[COLL_WEIGHTS_MAX];
253 static int32_t pri_ignore;
255 static collate_info_t collinfo;
256 static int32_t subst_count[COLL_WEIGHTS_MAX];
257 static int32_t chain_count;
258 static int32_t large_count;
260 static collpri_t *prilist = NULL;
261 static int numpri = 0;
262 static int maxpri = 0;
264 static void start_order(int);
271 if (numpri >= maxpri) {
272 maxpri = maxpri ? maxpri * 2 : 1024;
273 prilist = realloc(prilist, sizeof (collpri_t) * maxpri);
274 if (prilist == NULL) {
275 fprintf(stderr,"out of memory");
278 for (i = numpri; i < maxpri; i++) {
279 prilist[i].res = UNKNOWN;
290 if ((ref < 0) || (ref > numpri)) {
294 return (&prilist[ref]);
298 set_pri(int32_t ref, int32_t v, res_t res)
304 if ((res == REFER) && ((v < 0) || (v >= numpri))) {
308 /* Resolve self references */
309 if ((res == REFER) && (ref == v)) {
314 if (pri->res != UNKNOWN) {
315 warn("repeated item in order list (first on %d)",
319 pri->lineno = lineno;
325 resolve_pri(int32_t ref)
328 static int32_t pass = 0;
332 while (pri->res == REFER) {
333 if (pri->pass == pass) {
334 /* report a line with the circular symbol */
335 lineno = pri->lineno;
336 fprintf(stderr,"circular reference in order list");
339 if ((pri->pri < 0) || (pri->pri >= numpri)) {
344 pri = &prilist[pri->pri];
347 if (pri->res == UNKNOWN) {
350 if (pri->res != RESOLVED)
357 weight_compare(const void *n1, const void *n2)
359 int32_t k1 = ((const weight_t *)n1)->pri;
360 int32_t k2 = ((const weight_t *)n2)->pri;
362 return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
365 RB_GENERATE_STATIC(weights, weight, entry, weight_compare);
368 collsym_compare(const void *n1, const void *n2)
370 const collsym_t *c1 = n1;
371 const collsym_t *c2 = n2;
374 rv = strcmp(c1->name, c2->name);
375 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
378 RB_GENERATE_STATIC(collsyms, collsym, entry, collsym_compare);
381 collundef_compare(const void *n1, const void *n2)
383 const collundef_t *c1 = n1;
384 const collundef_t *c2 = n2;
387 rv = strcmp(c1->name, c2->name);
388 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
391 RB_GENERATE_STATIC(collundefs, collundef, entry, collundef_compare);
394 element_compare_symbol(const void *n1, const void *n2)
396 const collelem_t *c1 = n1;
397 const collelem_t *c2 = n2;
400 rv = strcmp(c1->symbol, c2->symbol);
401 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
404 RB_GENERATE_STATIC(elem_by_symbol, collelem, rb_bysymbol, element_compare_symbol);
407 element_compare_expand(const void *n1, const void *n2)
409 const collelem_t *c1 = n1;
410 const collelem_t *c2 = n2;
413 rv = wcscmp(c1->expand, c2->expand);
414 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
417 RB_GENERATE_STATIC(elem_by_expand, collelem, rb_byexpand, element_compare_expand);
420 collchar_compare(const void *n1, const void *n2)
422 wchar_t k1 = ((const collchar_t *)n1)->wc;
423 wchar_t k2 = ((const collchar_t *)n2)->wc;
425 return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
428 RB_GENERATE_STATIC(collchars, collchar, entry, collchar_compare);
431 subst_compare(const void *n1, const void *n2)
433 int32_t k1 = ((const subst_t *)n1)->key;
434 int32_t k2 = ((const subst_t *)n2)->key;
436 return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
439 RB_GENERATE_STATIC(substs, subst, entry, subst_compare);
442 subst_compare_ref(const void *n1, const void *n2)
444 const wchar_t *c1 = ((const subst_t *)n1)->ref;
445 const wchar_t *c2 = ((const subst_t *)n2)->ref;
449 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
452 RB_GENERATE_STATIC(substs_ref, subst, entry_ref, subst_compare_ref);
461 RB_INIT(&collundefs);
463 RB_INIT(&elem_by_symbol);
465 RB_INIT(&elem_by_expand);
469 for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
471 RB_INIT(&substs_ref[i]);
472 RB_INIT(&weights[i]);
476 (void) memset(&collinfo, 0, sizeof (collinfo));
478 /* allocate some initial priorities */
479 pri_ignore = new_pri();
481 set_pri(pri_ignore, 0, RESOLVED);
483 for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
484 pri_undefined[i] = new_pri();
486 /* we will override this later */
487 set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN);
492 define_collsym(char *name)
496 if ((sym = calloc(1, sizeof(*sym))) == NULL) {
497 fprintf(stderr,"out of memory");
501 sym->ref = new_pri();
503 if (RB_FIND(collsyms, &collsyms, sym) != NULL) {
505 * This should never happen because we are only called
506 * for undefined symbols.
512 RB_INSERT(collsyms, &collsyms, sym);
516 lookup_collsym(char *name)
521 return (RB_FIND(collsyms, &collsyms, &srch));
525 lookup_collelem(char *symbol)
529 srch.symbol = symbol;
530 return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch));
534 get_collundef(char *name)
541 if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) {
542 if (((ud = calloc(1, sizeof(*ud))) == NULL) ||
543 ((ud->name = strdup(name)) == NULL)) {
544 fprintf(stderr,"out of memory");
548 for (i = 0; i < NUM_WT; i++) {
549 ud->ref[i] = new_pri();
551 RB_INSERT(collundefs, &collundefs, ud);
553 add_charmap_undefined(name);
558 get_collchar(wchar_t wc, int create)
565 cc = RB_FIND(collchars, &collchars, &srch);
566 if ((cc == NULL) && create) {
567 if ((cc = calloc(1, sizeof(*cc))) == NULL) {
568 fprintf(stderr, "out of memory");
571 for (i = 0; i < NUM_WT; i++) {
572 cc->ref[i] = new_pri();
575 RB_INSERT(collchars, &collchars, cc);
581 end_order_collsym(collsym_t *sym)
583 start_order(T_COLLSYM);
584 /* update the weight */
586 set_pri(sym->ref, nextpri, RESOLVED);
598 /* advance the priority/weight */
603 for (i = 0; i < NUM_WT; i++) {
604 if (((ref = order_weights[i]) < 0) ||
605 ((p = get_pri(ref)) == NULL) ||
607 /* unspecified weight is a self reference */
608 set_pri(currchar->ref[i], pri, RESOLVED);
610 set_pri(currchar->ref[i], ref, REFER);
612 order_weights[i] = -1;
615 /* leave a cookie trail in case next symbol is ellipsis */
616 ellipsis_start = currchar->wc + 1;
621 /* save off the weights were we can find them */
622 for (i = 0; i < NUM_WT; i++) {
623 ellipsis_weights[i] = order_weights[i];
624 order_weights[i] = -1;
629 if (currelem == NULL) {
632 for (i = 0; i < NUM_WT; i++) {
634 if (((ref = order_weights[i]) < 0) ||
635 ((p = get_pri(ref)) == NULL) ||
637 set_pri(currelem->ref[i], pri,
640 set_pri(currelem->ref[i], ref, REFER);
642 order_weights[i] = -1;
648 for (i = 0; i < NUM_WT; i++) {
649 if (((ref = order_weights[i]) < 0) ||
650 ((p = get_pri(ref)) == NULL) ||
652 set_pri(pri_undefined[i], -1, RESOLVED);
654 set_pri(pri_undefined[i], ref, REFER);
656 order_weights[i] = -1;
661 for (i = 0; i < NUM_WT; i++) {
662 if (((ref = order_weights[i]) < 0) ||
663 ((p = get_pri(ref)) == NULL) ||
665 set_pri(currundef->ref[i], pri, RESOLVED);
667 set_pri(currundef->ref[i], ref, REFER);
669 order_weights[i] = -1;
681 start_order(int type)
685 lastorder = currorder;
688 /* this is used to protect ELLIPSIS processing */
689 if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) {
690 fprintf(stderr, "character value expected");
693 for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
694 order_weights[i] = -1;
700 start_order_undefined(void)
702 start_order(T_UNDEFINED);
706 start_order_symbol(char *name)
708 currundef = get_collundef(name);
709 start_order(T_SYMBOL);
713 start_order_char(wchar_t wc)
721 * If we last saw an ellipsis, then we need to close the range.
722 * Handle that here. Note that we have to be careful because the
723 * items *inside* the range are treated exclusiveley to the items
724 * outside of the range. The ends of the range can have quite
725 * different weights than the range members.
727 if (lastorder == T_ELLIPSIS) {
730 if (wc < ellipsis_start) {
731 fprintf(stderr, "malformed range!");
734 while (ellipsis_start < wc) {
736 * pick all of the saved weights for the
737 * ellipsis. note that -1 encodes for the
738 * ellipsis itself, which means to take the
739 * current relative priority.
741 if ((cc = get_collchar(ellipsis_start, 1)) == NULL) {
745 for (i = 0; i < NUM_WT; i++) {
747 if (((ref = ellipsis_weights[i]) == -1) ||
748 ((p = get_pri(ref)) == NULL) ||
750 set_pri(cc->ref[i], nextpri, RESOLVED);
752 set_pri(cc->ref[i], ref, REFER);
754 ellipsis_weights[i] = 0;
761 currchar = get_collchar(wc, 1);
765 start_order_collelem(collelem_t *e)
767 start_order(T_COLLELEM);
772 start_order_ellipsis(void)
776 start_order(T_ELLIPSIS);
778 if (lastorder != T_CHAR) {
779 fprintf(stderr, "illegal starting point for range");
783 for (i = 0; i < NUM_WT; i++) {
784 ellipsis_weights[i] = order_weights[i];
789 define_collelem(char *name, wchar_t *wcs)
794 if (wcslen(wcs) >= COLLATE_STR_LEN) {
795 fprintf(stderr,"expanded collation element too long");
799 if ((e = calloc(1, sizeof(*e))) == NULL) {
800 fprintf(stderr, "out of memory");
807 * This is executed before the order statement, so we don't
808 * know how many priorities we *really* need. We allocate one
809 * for each possible weight. Not a big deal, as collating-elements
810 * prove to be quite rare.
812 for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
813 e->ref[i] = new_pri();
816 /* A character sequence can only reduce to one element. */
817 if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) ||
818 (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) {
819 fprintf(stderr, "duplicate collating element definition");
823 RB_INSERT(elem_by_symbol, &elem_by_symbol, e);
824 RB_INSERT(elem_by_expand, &elem_by_expand, e);
828 add_order_bit(int kw)
830 uint8_t bit = DIRECTIVE_UNDEF;
834 bit = DIRECTIVE_FORWARD;
837 bit = DIRECTIVE_BACKWARD;
840 bit = DIRECTIVE_POSITION;
846 collinfo.directive[collinfo.directive_count] |= bit;
850 add_order_directive(void)
852 if (collinfo.directive_count >= COLL_WEIGHTS_MAX) {
853 fprintf(stderr,"too many directives (max %d)", COLL_WEIGHTS_MAX);
855 collinfo.directive_count++;
859 add_order_pri(int32_t ref)
861 if (curr_weight >= NUM_WT) {
862 fprintf(stderr,"too many weights (max %d)", NUM_WT);
865 order_weights[curr_weight] = ref;
870 add_order_collsym(collsym_t *s)
872 add_order_pri(s->ref);
876 add_order_char(wchar_t wc)
880 if ((cc = get_collchar(wc, 1)) == NULL) {
885 add_order_pri(cc->ref[curr_weight]);
889 add_order_collelem(collelem_t *e)
891 add_order_pri(e->ref[curr_weight]);
895 add_order_ignore(void)
897 add_order_pri(pri_ignore);
901 add_order_symbol(char *sym)
904 if ((c = get_collundef(sym)) == NULL) {
908 add_order_pri(c->ref[curr_weight]);
912 add_order_ellipsis(void)
914 /* special NULL value indicates self reference */
919 add_order_subst(void)
925 (void) memset(&srch, 0, sizeof (srch));
926 for (i = 0; i < curr_subst; i++) {
927 srch.ref[i] = subst_weights[i];
928 subst_weights[i] = 0;
930 s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch);
933 if ((s = calloc(1, sizeof(*s))) == NULL) {
934 fprintf(stderr,"out of memory");
940 * We use a self reference for our key, but we set a
941 * high bit to indicate that this is a substitution
942 * reference. This will expedite table lookups later,
943 * and prevent table lookups for situations that don't
944 * require it. (In short, its a big win, because we
945 * can skip a lot of binary searching.)
948 (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY),
950 nextsubst[curr_weight] += 1;
952 for (i = 0; i < curr_subst; i++) {
953 s->ref[i] = srch.ref[i];
956 RB_INSERT(substs_ref, &substs_ref[curr_weight], s);
958 if (RB_FIND(substs, &substs[curr_weight], s) != NULL) {
962 RB_INSERT(substs, &substs[curr_weight], s);
968 * We are using the current (unique) priority as a search key
969 * in the substitution table.
971 add_order_pri(s->key);
975 add_subst_pri(int32_t ref)
977 if (curr_subst >= COLLATE_STR_LEN) {
978 fprintf(stderr,"substitution string is too long");
981 subst_weights[curr_subst] = ref;
986 add_subst_char(wchar_t wc)
991 if (((cc = get_collchar(wc, 1)) == NULL) ||
996 /* we take the weight for the character at that position */
997 add_subst_pri(cc->ref[curr_weight]);
1001 add_subst_collelem(collelem_t *e)
1003 add_subst_pri(e->ref[curr_weight]);
1007 add_subst_collsym(collsym_t *s)
1009 add_subst_pri(s->ref);
1013 add_subst_symbol(char *ptr)
1017 if ((cu = get_collundef(ptr)) != NULL) {
1018 add_subst_pri(cu->ref[curr_weight]);
1023 add_weight(int32_t ref, int pass)
1028 srch.pri = resolve_pri(ref);
1030 /* No translation of ignores */
1034 /* Substitution priorities are not weights */
1035 if (srch.pri & COLLATE_SUBST_PRIORITY)
1038 if (RB_FIND(weights, &weights[pass], &srch) != NULL)
1041 if ((w = calloc(1, sizeof(*w))) == NULL) {
1042 fprintf(stderr, "out of memory");
1046 RB_INSERT(weights, &weights[pass], w);
1050 add_weights(int32_t *refs)
1053 for (i = 0; i < NUM_WT; i++) {
1054 add_weight(refs[i], i);
1059 get_weight(int32_t ref, int pass)
1065 pri = resolve_pri(ref);
1066 if (pri & COLLATE_SUBST_PRIORITY) {
1073 if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) {
1081 wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
1086 while (--n > 0 && (*s1++ = htote(*s2++)) != 0)
1094 #define RB_COUNT(x, name, head, cnt) do { \
1096 RB_FOREACH(x, name, (head)) { \
1101 #define RB_NUMNODES(type, name, head, cnt) do { \
1104 RB_FOREACH(t, name, head) { \
1119 char vers[COLLATE_STR_LEN];
1120 collate_char_t chars[UCHAR_MAX + 1];
1121 collate_large_t *large;
1122 collate_subst_t *subst[COLL_WEIGHTS_MAX];
1123 collate_chain_t *chain;
1126 * We have to run through a preliminary pass to identify all the
1127 * weights that we use for each sorting level.
1129 for (i = 0; i < NUM_WT; i++) {
1130 add_weight(pri_ignore, i);
1132 for (i = 0; i < NUM_WT; i++) {
1133 RB_FOREACH(sb, substs, &substs[i]) {
1134 for (j = 0; sb->ref[j]; j++) {
1135 add_weight(sb->ref[j], i);
1139 RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1140 add_weights(ce->ref);
1142 RB_FOREACH(cc, collchars, &collchars) {
1143 add_weights(cc->ref);
1147 * Now we walk the entire set of weights, removing the gaps
1148 * in the weights. This gives us optimum usage. The walk
1149 * occurs in priority.
1151 for (i = 0; i < NUM_WT; i++) {
1153 RB_FOREACH(w, weights, &weights[i]) {
1154 w->opt = nweight[i];
1159 (void) memset(&chars, 0, sizeof (chars));
1160 (void) memset(vers, 0, COLLATE_STR_LEN);
1161 (void) strlcpy(vers, COLLATE_VERSION, sizeof (vers));
1164 * We need to make sure we arrange for the UNDEFINED field
1165 * to show up. Also, set the total weight counts.
1167 for (i = 0; i < NUM_WT; i++) {
1168 if (resolve_pri(pri_undefined[i]) == -1) {
1169 set_pri(pri_undefined[i], -1, RESOLVED);
1170 /* they collate at the end of everything else */
1171 collinfo.undef_pri[i] = htote(COLLATE_MAX_PRIORITY);
1173 collinfo.pri_count[i] = htote(nweight[i]);
1176 collinfo.pri_count[NUM_WT] = htote(max_wide());
1177 collinfo.undef_pri[NUM_WT] = htote(COLLATE_MAX_PRIORITY);
1178 collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
1181 * Ordinary character priorities
1183 for (i = 0; i <= UCHAR_MAX; i++) {
1184 if ((cc = get_collchar(i, 0)) != NULL) {
1185 for (j = 0; j < NUM_WT; j++) {
1187 htote(get_weight(cc->ref[j], j));
1190 for (j = 0; j < NUM_WT; j++) {
1192 htote(get_weight(pri_undefined[j], j));
1195 * Per POSIX, for undefined characters, we
1196 * also have to add a last item, which is the
1199 chars[i].pri[NUM_WT] = htote(i);
1204 * Substitution tables
1206 for (i = 0; i < NUM_WT; i++) {
1207 collate_subst_t *st = NULL;
1209 RB_COUNT(temp, substs, &substs[i], n);
1211 if ((st = calloc(n, sizeof(collate_subst_t))) == NULL) {
1212 fprintf(stderr, "out of memory");
1216 RB_FOREACH(sb, substs, &substs[i]) {
1217 if ((st[n].key = resolve_pri(sb->key)) < 0) {
1218 /* by definition these resolve! */
1221 if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
1224 st[n].key = htote(st[n].key);
1225 for (j = 0; sb->ref[j]; j++) {
1226 st[n].pri[j] = htote(get_weight(sb->ref[j],
1231 if (n != subst_count[i])
1238 * Chains, i.e. collating elements
1240 RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, chain_count);
1241 chain = calloc(chain_count, sizeof(collate_chain_t));
1242 if (chain == NULL) {
1243 fprintf(stderr, "out of memory");
1247 RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1248 (void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
1249 for (i = 0; i < NUM_WT; i++) {
1250 chain[n].pri[i] = htote(get_weight(ce->ref[i], i));
1254 if (n != chain_count)
1258 * Large (> UCHAR_MAX) character priorities
1260 RB_NUMNODES(collchar_t, collchars, &collchars, n);
1261 large = calloc(n, sizeof(collate_large_t));
1262 if (large == NULL) {
1263 fprintf(stderr, "out of memory");
1268 RB_FOREACH(cc, collchars, &collchars) {
1270 /* we already gathered those */
1271 if (cc->wc <= UCHAR_MAX)
1273 for (j = 0; j < NUM_WT; j++) {
1274 if ((pri = get_weight(cc->ref[j], j)) < 0) {
1277 if (undef && (pri >= 0)) {
1278 /* if undefined, then all priorities are */
1281 large[i].pri.pri[j] = htote(pri);
1285 large[i].val = htote(cc->wc);
1290 if ((f = open_category()) == NULL) {
1294 /* Time to write the entire data set out */
1296 for (i = 0; i < NUM_WT; i++)
1297 collinfo.subst_count[i] = htote(subst_count[i]);
1298 collinfo.chain_count = htote(chain_count);
1299 collinfo.large_count = htote(large_count);
1301 if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) ||
1302 (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
1303 (wr_category(&chars, sizeof (chars), f) < 0)) {
1307 for (i = 0; i < NUM_WT; i++) {
1308 sz = sizeof (collate_subst_t) * subst_count[i];
1309 if (wr_category(subst[i], sz, f) < 0) {
1313 sz = sizeof (collate_chain_t) * chain_count;
1314 if (wr_category(chain, sz, f) < 0) {
1317 sz = sizeof (collate_large_t) * large_count;
1318 if (wr_category(large, sz, f) < 0) {