xref: /freebsd/usr.bin/localedef/collate.c (revision 4644f9bef6bcfa8d1ead9db38a548567cda8eac6)
1e12a957fSPedro F. Giffuni /*-
2*4644f9beSYuri Pankov  * Copyright 2018 Nexenta Systems, Inc.
3057ca2d4SBaptiste Daroussin  * Copyright 2015 John Marino <draco@marino.st>
4057ca2d4SBaptiste Daroussin  *
5057ca2d4SBaptiste Daroussin  * This source code is derived from the illumos localedef command, and
6057ca2d4SBaptiste Daroussin  * provided under BSD-style license terms by Nexenta Systems, Inc.
7057ca2d4SBaptiste Daroussin  *
8057ca2d4SBaptiste Daroussin  * Redistribution and use in source and binary forms, with or without
9057ca2d4SBaptiste Daroussin  * modification, are permitted provided that the following conditions
10057ca2d4SBaptiste Daroussin  * are met:
11057ca2d4SBaptiste Daroussin  *
12057ca2d4SBaptiste Daroussin  * 1. Redistributions of source code must retain the above copyright
13057ca2d4SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer.
14057ca2d4SBaptiste Daroussin  * 2. Redistributions in binary form must reproduce the above copyright
15057ca2d4SBaptiste Daroussin  *    notice, this list of conditions and the following disclaimer in the
16057ca2d4SBaptiste Daroussin  *    documentation and/or other materials provided with the distribution.
17057ca2d4SBaptiste Daroussin  *
18057ca2d4SBaptiste Daroussin  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19057ca2d4SBaptiste Daroussin  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20057ca2d4SBaptiste Daroussin  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21057ca2d4SBaptiste Daroussin  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22057ca2d4SBaptiste Daroussin  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23057ca2d4SBaptiste Daroussin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24057ca2d4SBaptiste Daroussin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25057ca2d4SBaptiste Daroussin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26057ca2d4SBaptiste Daroussin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27057ca2d4SBaptiste Daroussin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28057ca2d4SBaptiste Daroussin  * POSSIBILITY OF SUCH DAMAGE.
29057ca2d4SBaptiste Daroussin  */
30057ca2d4SBaptiste Daroussin 
31057ca2d4SBaptiste Daroussin /*
32057ca2d4SBaptiste Daroussin  * LC_COLLATE database generation routines for localedef.
33057ca2d4SBaptiste Daroussin  */
34057ca2d4SBaptiste Daroussin #include <sys/cdefs.h>
35057ca2d4SBaptiste Daroussin __FBSDID("$FreeBSD$");
36057ca2d4SBaptiste Daroussin 
37057ca2d4SBaptiste Daroussin #include <sys/types.h>
383a444dbdSBaptiste Daroussin #include <sys/tree.h>
39057ca2d4SBaptiste Daroussin 
40057ca2d4SBaptiste Daroussin #include <stdio.h>
41057ca2d4SBaptiste Daroussin #include <stddef.h>
42057ca2d4SBaptiste Daroussin #include <stdlib.h>
43057ca2d4SBaptiste Daroussin #include <errno.h>
44057ca2d4SBaptiste Daroussin #include <string.h>
45057ca2d4SBaptiste Daroussin #include <unistd.h>
46057ca2d4SBaptiste Daroussin #include <wchar.h>
47057ca2d4SBaptiste Daroussin #include <limits.h>
48057ca2d4SBaptiste Daroussin #include "localedef.h"
49057ca2d4SBaptiste Daroussin #include "parser.h"
50057ca2d4SBaptiste Daroussin #include "collate.h"
51057ca2d4SBaptiste Daroussin 
52057ca2d4SBaptiste Daroussin /*
53057ca2d4SBaptiste Daroussin  * Design notes.
54057ca2d4SBaptiste Daroussin  *
55057ca2d4SBaptiste Daroussin  * It will be extremely helpful to the reader if they have access to
56057ca2d4SBaptiste Daroussin  * the localedef and locale file format specifications available.
57057ca2d4SBaptiste Daroussin  * Latest versions of these are available from www.opengroup.org.
58057ca2d4SBaptiste Daroussin  *
59057ca2d4SBaptiste Daroussin  * The design for the collation code is a bit complex.  The goal is a
60057ca2d4SBaptiste Daroussin  * single collation database as described in collate.h (in
61057ca2d4SBaptiste Daroussin  * libc/port/locale).  However, there are some other tidbits:
62057ca2d4SBaptiste Daroussin  *
63057ca2d4SBaptiste Daroussin  * a) The substitution entries are now a directly indexable array.  A
64057ca2d4SBaptiste Daroussin  * priority elsewhere in the table is taken as an index into the
65057ca2d4SBaptiste Daroussin  * substitution table if it has a high bit (COLLATE_SUBST_PRIORITY)
66057ca2d4SBaptiste Daroussin  * set.  (The bit is cleared and the result is the index into the
67057ca2d4SBaptiste Daroussin  * table.
68057ca2d4SBaptiste Daroussin  *
69057ca2d4SBaptiste Daroussin  * b) We eliminate duplicate entries into the substitution table.
70057ca2d4SBaptiste Daroussin  * This saves a lot of space.
71057ca2d4SBaptiste Daroussin  *
72057ca2d4SBaptiste Daroussin  * c) The priorities for each level are "compressed", so that each
73057ca2d4SBaptiste Daroussin  * sorting level has consecutively numbered priorities starting at 1.
74057ca2d4SBaptiste Daroussin  * (O is reserved for the ignore priority.)  This means sort levels
75057ca2d4SBaptiste Daroussin  * which only have a few distinct priorities can represent the
76057ca2d4SBaptiste Daroussin  * priority level in fewer bits, which makes the strxfrm output
77057ca2d4SBaptiste Daroussin  * smaller.
78057ca2d4SBaptiste Daroussin  *
79057ca2d4SBaptiste Daroussin  * d) We record the total number of priorities so that strxfrm can
80057ca2d4SBaptiste Daroussin  * figure out how many bytes to expand a numeric priority into.
81057ca2d4SBaptiste Daroussin  *
82057ca2d4SBaptiste Daroussin  * e) For the UNDEFINED pass (the last pass), we record the maximum
83057ca2d4SBaptiste Daroussin  * number of bits needed to uniquely prioritize these entries, so that
84057ca2d4SBaptiste Daroussin  * the last pass can also use smaller strxfrm output when possible.
85057ca2d4SBaptiste Daroussin  *
86057ca2d4SBaptiste Daroussin  * f) Priorities with the sign bit set are verboten.  This works out
87057ca2d4SBaptiste Daroussin  * because no active character set needs that bit to carry significant
88057ca2d4SBaptiste Daroussin  * information once the character is in wide form.
89057ca2d4SBaptiste Daroussin  *
90057ca2d4SBaptiste Daroussin  * To process the entire data to make the database, we actually run
91057ca2d4SBaptiste Daroussin  * multiple passes over the data.
92057ca2d4SBaptiste Daroussin  *
93057ca2d4SBaptiste Daroussin  * The first pass, which is done at parse time, identifies elements,
94057ca2d4SBaptiste Daroussin  * substitutions, and such, and records them in priority order.  As
95057ca2d4SBaptiste Daroussin  * some priorities can refer to other priorities, using forward
96057ca2d4SBaptiste Daroussin  * references, we use a table of references indicating whether the
97057ca2d4SBaptiste Daroussin  * priority's value has been resolved, or whether it is still a
98057ca2d4SBaptiste Daroussin  * reference.
99057ca2d4SBaptiste Daroussin  *
100057ca2d4SBaptiste Daroussin  * The second pass walks over all the items in priority order, noting
101057ca2d4SBaptiste Daroussin  * that they are used directly, and not just an indirect reference.
102057ca2d4SBaptiste Daroussin  * This is done by creating a "weight" structure for the item.  The
1033a444dbdSBaptiste Daroussin  * weights are stashed in an RB tree sorted by relative "priority".
104057ca2d4SBaptiste Daroussin  *
105057ca2d4SBaptiste Daroussin  * The third pass walks over all the weight structures, in priority
106057ca2d4SBaptiste Daroussin  * order, and assigns a new monotonically increasing (per sort level)
107057ca2d4SBaptiste Daroussin  * weight value to them.  These are the values that will actually be
108057ca2d4SBaptiste Daroussin  * written to the file.
109057ca2d4SBaptiste Daroussin  *
110057ca2d4SBaptiste Daroussin  * The fourth pass just writes the data out.
111057ca2d4SBaptiste Daroussin  */
112057ca2d4SBaptiste Daroussin 
113057ca2d4SBaptiste Daroussin /*
114057ca2d4SBaptiste Daroussin  * In order to resolve the priorities, we create a table of priorities.
115057ca2d4SBaptiste Daroussin  * Entries in the table can be in one of three states.
116057ca2d4SBaptiste Daroussin  *
117057ca2d4SBaptiste Daroussin  * UNKNOWN is for newly allocated entries, and indicates that nothing
118057ca2d4SBaptiste Daroussin  * is known about the priority.  (For example, when new entries are created
119057ca2d4SBaptiste Daroussin  * for collating-symbols, this is the value assigned for them until the
120057ca2d4SBaptiste Daroussin  * collating symbol's order has been determined.
121057ca2d4SBaptiste Daroussin  *
122057ca2d4SBaptiste Daroussin  * RESOLVED is used for an entry where the priority indicates the final
123057ca2d4SBaptiste Daroussin  * numeric weight.
124057ca2d4SBaptiste Daroussin  *
125057ca2d4SBaptiste Daroussin  * REFER is used for entries that reference other entries.  Typically
126057ca2d4SBaptiste Daroussin  * this is used for forward references.  A collating-symbol can never
127057ca2d4SBaptiste Daroussin  * have this value.
128057ca2d4SBaptiste Daroussin  *
129057ca2d4SBaptiste Daroussin  * The "pass" field is used during final resolution to aid in detection
130057ca2d4SBaptiste Daroussin  * of referencing loops.  (For example <A> depends on <B>, but <B> has its
131057ca2d4SBaptiste Daroussin  * priority dependent on <A>.)
132057ca2d4SBaptiste Daroussin  */
133057ca2d4SBaptiste Daroussin typedef enum {
134057ca2d4SBaptiste Daroussin 	UNKNOWN,	/* priority is totally unknown */
135057ca2d4SBaptiste Daroussin 	RESOLVED,	/* priority value fully resolved */
136057ca2d4SBaptiste Daroussin 	REFER		/* priority is a reference (index) */
137057ca2d4SBaptiste Daroussin } res_t;
138057ca2d4SBaptiste Daroussin 
139057ca2d4SBaptiste Daroussin typedef struct weight {
140057ca2d4SBaptiste Daroussin 	int32_t		pri;
141057ca2d4SBaptiste Daroussin 	int		opt;
1423a444dbdSBaptiste Daroussin 	RB_ENTRY(weight) entry;
143057ca2d4SBaptiste Daroussin } weight_t;
144057ca2d4SBaptiste Daroussin 
145057ca2d4SBaptiste Daroussin typedef struct priority {
146057ca2d4SBaptiste Daroussin 	res_t		res;
147057ca2d4SBaptiste Daroussin 	int32_t		pri;
148057ca2d4SBaptiste Daroussin 	int		pass;
149057ca2d4SBaptiste Daroussin 	int		lineno;
150057ca2d4SBaptiste Daroussin } collpri_t;
151057ca2d4SBaptiste Daroussin 
152057ca2d4SBaptiste Daroussin #define	NUM_WT	collinfo.directive_count
153057ca2d4SBaptiste Daroussin 
154057ca2d4SBaptiste Daroussin /*
155057ca2d4SBaptiste Daroussin  * These are the abstract collating symbols, which are just a symbolic
156057ca2d4SBaptiste Daroussin  * way to reference a priority.
157057ca2d4SBaptiste Daroussin  */
158057ca2d4SBaptiste Daroussin struct collsym {
159057ca2d4SBaptiste Daroussin 	char		*name;
160057ca2d4SBaptiste Daroussin 	int32_t		ref;
1613a444dbdSBaptiste Daroussin 	RB_ENTRY(collsym) entry;
162057ca2d4SBaptiste Daroussin };
163057ca2d4SBaptiste Daroussin 
164057ca2d4SBaptiste Daroussin /*
165057ca2d4SBaptiste Daroussin  * These are also abstract collating symbols, but we allow them to have
166057ca2d4SBaptiste Daroussin  * different priorities at different levels.
167057ca2d4SBaptiste Daroussin  */
168057ca2d4SBaptiste Daroussin typedef struct collundef {
169057ca2d4SBaptiste Daroussin 	char		*name;
170057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1713a444dbdSBaptiste Daroussin 	RB_ENTRY(collundef) entry;
172057ca2d4SBaptiste Daroussin } collundef_t;
173057ca2d4SBaptiste Daroussin 
174057ca2d4SBaptiste Daroussin /*
175057ca2d4SBaptiste Daroussin  * These are called "chains" in libc.  This records the fact that two
176057ca2d4SBaptiste Daroussin  * more characters should be treated as a single collating entity when
177057ca2d4SBaptiste Daroussin  * they appear together.  For example, in Spanish <C><h> gets collated
178057ca2d4SBaptiste Daroussin  * as a character between <C> and <D>.
179057ca2d4SBaptiste Daroussin  */
180057ca2d4SBaptiste Daroussin struct collelem {
181057ca2d4SBaptiste Daroussin 	char		*symbol;
182057ca2d4SBaptiste Daroussin 	wchar_t		*expand;
183057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1843a444dbdSBaptiste Daroussin 	RB_ENTRY(collelem) rb_bysymbol;
1853a444dbdSBaptiste Daroussin 	RB_ENTRY(collelem) rb_byexpand;
186057ca2d4SBaptiste Daroussin };
187057ca2d4SBaptiste Daroussin 
188057ca2d4SBaptiste Daroussin /*
189057ca2d4SBaptiste Daroussin  * Individual characters have a sequence of weights as well.
190057ca2d4SBaptiste Daroussin  */
191057ca2d4SBaptiste Daroussin typedef struct collchar {
192057ca2d4SBaptiste Daroussin 	wchar_t		wc;
193057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1943a444dbdSBaptiste Daroussin 	RB_ENTRY(collchar) entry;
195057ca2d4SBaptiste Daroussin } collchar_t;
196057ca2d4SBaptiste Daroussin 
197057ca2d4SBaptiste Daroussin /*
198057ca2d4SBaptiste Daroussin  * Substitution entries.  The key is itself a priority.  Note that
199057ca2d4SBaptiste Daroussin  * when we create one of these, we *automatically* wind up with a
200057ca2d4SBaptiste Daroussin  * fully resolved priority for the key, because creation of
201057ca2d4SBaptiste Daroussin  * substitutions creates a resolved priority at the same time.
202057ca2d4SBaptiste Daroussin  */
2033a444dbdSBaptiste Daroussin typedef struct subst{
204057ca2d4SBaptiste Daroussin 	int32_t		key;
205057ca2d4SBaptiste Daroussin 	int32_t		ref[COLLATE_STR_LEN];
2063a444dbdSBaptiste Daroussin 	RB_ENTRY(subst)	entry;
2073a444dbdSBaptiste Daroussin 	RB_ENTRY(subst)	entry_ref;
208057ca2d4SBaptiste Daroussin } subst_t;
209057ca2d4SBaptiste Daroussin 
2103a444dbdSBaptiste Daroussin static RB_HEAD(collsyms, collsym) collsyms;
2113a444dbdSBaptiste Daroussin static RB_HEAD(collundefs, collundef) collundefs;
2123a444dbdSBaptiste Daroussin static RB_HEAD(elem_by_symbol, collelem) elem_by_symbol;
2133a444dbdSBaptiste Daroussin static RB_HEAD(elem_by_expand, collelem) elem_by_expand;
2143a444dbdSBaptiste Daroussin static RB_HEAD(collchars, collchar) collchars;
2153a444dbdSBaptiste Daroussin static RB_HEAD(substs, subst) substs[COLL_WEIGHTS_MAX];
2163a444dbdSBaptiste Daroussin static RB_HEAD(substs_ref, subst) substs_ref[COLL_WEIGHTS_MAX];
2173a444dbdSBaptiste Daroussin static RB_HEAD(weights, weight) weights[COLL_WEIGHTS_MAX];
218057ca2d4SBaptiste Daroussin static int32_t		nweight[COLL_WEIGHTS_MAX];
219057ca2d4SBaptiste Daroussin 
220057ca2d4SBaptiste Daroussin /*
221057ca2d4SBaptiste Daroussin  * This is state tracking for the ellipsis token.  Note that we start
222057ca2d4SBaptiste Daroussin  * the initial values so that the ellipsis logic will think we got a
223057ca2d4SBaptiste Daroussin  * magic starting value of NUL.  It starts at minus one because the
224057ca2d4SBaptiste Daroussin  * starting point is exclusive -- i.e. the starting point is not
225057ca2d4SBaptiste Daroussin  * itself handled by the ellipsis code.
226057ca2d4SBaptiste Daroussin  */
227057ca2d4SBaptiste Daroussin static int currorder = EOF;
228057ca2d4SBaptiste Daroussin static int lastorder = EOF;
229057ca2d4SBaptiste Daroussin static collelem_t *currelem;
230057ca2d4SBaptiste Daroussin static collchar_t *currchar;
231057ca2d4SBaptiste Daroussin static collundef_t *currundef;
232057ca2d4SBaptiste Daroussin static wchar_t ellipsis_start = 0;
233057ca2d4SBaptiste Daroussin static int32_t ellipsis_weights[COLL_WEIGHTS_MAX];
234057ca2d4SBaptiste Daroussin 
235057ca2d4SBaptiste Daroussin /*
236057ca2d4SBaptiste Daroussin  * We keep a running tally of weights.
237057ca2d4SBaptiste Daroussin  */
238057ca2d4SBaptiste Daroussin static int nextpri = 1;
239057ca2d4SBaptiste Daroussin static int nextsubst[COLL_WEIGHTS_MAX] = { 0 };
240057ca2d4SBaptiste Daroussin 
241057ca2d4SBaptiste Daroussin /*
242057ca2d4SBaptiste Daroussin  * This array collects up the weights for each level.
243057ca2d4SBaptiste Daroussin  */
244057ca2d4SBaptiste Daroussin static int32_t order_weights[COLL_WEIGHTS_MAX];
245057ca2d4SBaptiste Daroussin static int curr_weight = 0;
246057ca2d4SBaptiste Daroussin static int32_t subst_weights[COLLATE_STR_LEN];
247057ca2d4SBaptiste Daroussin static int curr_subst = 0;
248057ca2d4SBaptiste Daroussin 
249057ca2d4SBaptiste Daroussin /*
250057ca2d4SBaptiste Daroussin  * Some initial priority values.
251057ca2d4SBaptiste Daroussin  */
252057ca2d4SBaptiste Daroussin static int32_t pri_undefined[COLL_WEIGHTS_MAX];
253057ca2d4SBaptiste Daroussin static int32_t pri_ignore;
254057ca2d4SBaptiste Daroussin 
255057ca2d4SBaptiste Daroussin static collate_info_t collinfo;
256*4644f9beSYuri Pankov static int32_t subst_count[COLL_WEIGHTS_MAX];
257*4644f9beSYuri Pankov static int32_t chain_count;
258*4644f9beSYuri Pankov static int32_t large_count;
259057ca2d4SBaptiste Daroussin 
260057ca2d4SBaptiste Daroussin static collpri_t	*prilist = NULL;
261057ca2d4SBaptiste Daroussin static int		numpri = 0;
262057ca2d4SBaptiste Daroussin static int		maxpri = 0;
263057ca2d4SBaptiste Daroussin 
264057ca2d4SBaptiste Daroussin static void start_order(int);
265057ca2d4SBaptiste Daroussin 
266057ca2d4SBaptiste Daroussin static int32_t
267057ca2d4SBaptiste Daroussin new_pri(void)
268057ca2d4SBaptiste Daroussin {
269057ca2d4SBaptiste Daroussin 	int i;
270057ca2d4SBaptiste Daroussin 
271057ca2d4SBaptiste Daroussin 	if (numpri >= maxpri) {
272057ca2d4SBaptiste Daroussin 		maxpri = maxpri ? maxpri * 2 : 1024;
273057ca2d4SBaptiste Daroussin 		prilist = realloc(prilist, sizeof (collpri_t) * maxpri);
274057ca2d4SBaptiste Daroussin 		if (prilist == NULL) {
275057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
276057ca2d4SBaptiste Daroussin 			return (-1);
277057ca2d4SBaptiste Daroussin 		}
278057ca2d4SBaptiste Daroussin 		for (i = numpri; i < maxpri; i++) {
279057ca2d4SBaptiste Daroussin 			prilist[i].res = UNKNOWN;
280057ca2d4SBaptiste Daroussin 			prilist[i].pri = 0;
281057ca2d4SBaptiste Daroussin 			prilist[i].pass = 0;
282057ca2d4SBaptiste Daroussin 		}
283057ca2d4SBaptiste Daroussin 	}
284057ca2d4SBaptiste Daroussin 	return (numpri++);
285057ca2d4SBaptiste Daroussin }
286057ca2d4SBaptiste Daroussin 
287057ca2d4SBaptiste Daroussin static collpri_t *
288057ca2d4SBaptiste Daroussin get_pri(int32_t ref)
289057ca2d4SBaptiste Daroussin {
290057ca2d4SBaptiste Daroussin 	if ((ref < 0) || (ref > numpri)) {
291057ca2d4SBaptiste Daroussin 		INTERR;
292057ca2d4SBaptiste Daroussin 		return (NULL);
293057ca2d4SBaptiste Daroussin 	}
294057ca2d4SBaptiste Daroussin 	return (&prilist[ref]);
295057ca2d4SBaptiste Daroussin }
296057ca2d4SBaptiste Daroussin 
297057ca2d4SBaptiste Daroussin static void
298057ca2d4SBaptiste Daroussin set_pri(int32_t ref, int32_t v, res_t res)
299057ca2d4SBaptiste Daroussin {
300057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
301057ca2d4SBaptiste Daroussin 
302057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
303057ca2d4SBaptiste Daroussin 
304057ca2d4SBaptiste Daroussin 	if ((res == REFER) && ((v < 0) || (v >= numpri))) {
305057ca2d4SBaptiste Daroussin 		INTERR;
306057ca2d4SBaptiste Daroussin 	}
307057ca2d4SBaptiste Daroussin 
308057ca2d4SBaptiste Daroussin 	/* Resolve self references */
309057ca2d4SBaptiste Daroussin 	if ((res == REFER) && (ref == v)) {
310057ca2d4SBaptiste Daroussin 		v = nextpri;
311057ca2d4SBaptiste Daroussin 		res = RESOLVED;
312057ca2d4SBaptiste Daroussin 	}
313057ca2d4SBaptiste Daroussin 
314057ca2d4SBaptiste Daroussin 	if (pri->res != UNKNOWN) {
315057ca2d4SBaptiste Daroussin 		warn("repeated item in order list (first on %d)",
316057ca2d4SBaptiste Daroussin 		    pri->lineno);
317057ca2d4SBaptiste Daroussin 		return;
318057ca2d4SBaptiste Daroussin 	}
319057ca2d4SBaptiste Daroussin 	pri->lineno = lineno;
320057ca2d4SBaptiste Daroussin 	pri->pri = v;
321057ca2d4SBaptiste Daroussin 	pri->res = res;
322057ca2d4SBaptiste Daroussin }
323057ca2d4SBaptiste Daroussin 
324057ca2d4SBaptiste Daroussin static int32_t
325057ca2d4SBaptiste Daroussin resolve_pri(int32_t ref)
326057ca2d4SBaptiste Daroussin {
327057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
328057ca2d4SBaptiste Daroussin 	static int32_t	pass = 0;
329057ca2d4SBaptiste Daroussin 
330057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
331057ca2d4SBaptiste Daroussin 	pass++;
332057ca2d4SBaptiste Daroussin 	while (pri->res == REFER) {
333057ca2d4SBaptiste Daroussin 		if (pri->pass == pass) {
334057ca2d4SBaptiste Daroussin 			/* report a line with the circular symbol */
335057ca2d4SBaptiste Daroussin 			lineno = pri->lineno;
336057ca2d4SBaptiste Daroussin 			fprintf(stderr,"circular reference in order list");
337057ca2d4SBaptiste Daroussin 			return (-1);
338057ca2d4SBaptiste Daroussin 		}
339057ca2d4SBaptiste Daroussin 		if ((pri->pri < 0) || (pri->pri >= numpri)) {
340057ca2d4SBaptiste Daroussin 			INTERR;
341057ca2d4SBaptiste Daroussin 			return (-1);
342057ca2d4SBaptiste Daroussin 		}
343057ca2d4SBaptiste Daroussin 		pri->pass = pass;
344057ca2d4SBaptiste Daroussin 		pri = &prilist[pri->pri];
345057ca2d4SBaptiste Daroussin 	}
346057ca2d4SBaptiste Daroussin 
347057ca2d4SBaptiste Daroussin 	if (pri->res == UNKNOWN) {
348057ca2d4SBaptiste Daroussin 		return (-1);
349057ca2d4SBaptiste Daroussin 	}
350057ca2d4SBaptiste Daroussin 	if (pri->res != RESOLVED)
351057ca2d4SBaptiste Daroussin 		INTERR;
352057ca2d4SBaptiste Daroussin 
353057ca2d4SBaptiste Daroussin 	return (pri->pri);
354057ca2d4SBaptiste Daroussin }
355057ca2d4SBaptiste Daroussin 
356057ca2d4SBaptiste Daroussin static int
357057ca2d4SBaptiste Daroussin weight_compare(const void *n1, const void *n2)
358057ca2d4SBaptiste Daroussin {
359057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const weight_t *)n1)->pri;
360057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const weight_t *)n2)->pri;
361057ca2d4SBaptiste Daroussin 
362057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
363057ca2d4SBaptiste Daroussin }
364057ca2d4SBaptiste Daroussin 
3653a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(weights, weight, entry, weight_compare);
3663a444dbdSBaptiste Daroussin 
367057ca2d4SBaptiste Daroussin static int
368057ca2d4SBaptiste Daroussin collsym_compare(const void *n1, const void *n2)
369057ca2d4SBaptiste Daroussin {
370057ca2d4SBaptiste Daroussin 	const collsym_t *c1 = n1;
371057ca2d4SBaptiste Daroussin 	const collsym_t *c2 = n2;
372057ca2d4SBaptiste Daroussin 	int rv;
373057ca2d4SBaptiste Daroussin 
374057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
375057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
376057ca2d4SBaptiste Daroussin }
377057ca2d4SBaptiste Daroussin 
3783a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collsyms, collsym, entry, collsym_compare);
3793a444dbdSBaptiste Daroussin 
380057ca2d4SBaptiste Daroussin static int
381057ca2d4SBaptiste Daroussin collundef_compare(const void *n1, const void *n2)
382057ca2d4SBaptiste Daroussin {
383057ca2d4SBaptiste Daroussin 	const collundef_t *c1 = n1;
384057ca2d4SBaptiste Daroussin 	const collundef_t *c2 = n2;
385057ca2d4SBaptiste Daroussin 	int rv;
386057ca2d4SBaptiste Daroussin 
387057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
388057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
389057ca2d4SBaptiste Daroussin }
390057ca2d4SBaptiste Daroussin 
3913a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collundefs, collundef, entry, collundef_compare);
3923a444dbdSBaptiste Daroussin 
393057ca2d4SBaptiste Daroussin static int
394057ca2d4SBaptiste Daroussin element_compare_symbol(const void *n1, const void *n2)
395057ca2d4SBaptiste Daroussin {
396057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
397057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
398057ca2d4SBaptiste Daroussin 	int rv;
399057ca2d4SBaptiste Daroussin 
400057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->symbol, c2->symbol);
401057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
402057ca2d4SBaptiste Daroussin }
403057ca2d4SBaptiste Daroussin 
4043a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_symbol, collelem, rb_bysymbol, element_compare_symbol);
4053a444dbdSBaptiste Daroussin 
406057ca2d4SBaptiste Daroussin static int
407057ca2d4SBaptiste Daroussin element_compare_expand(const void *n1, const void *n2)
408057ca2d4SBaptiste Daroussin {
409057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
410057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
411057ca2d4SBaptiste Daroussin 	int rv;
412057ca2d4SBaptiste Daroussin 
413057ca2d4SBaptiste Daroussin 	rv = wcscmp(c1->expand, c2->expand);
414057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
415057ca2d4SBaptiste Daroussin }
416057ca2d4SBaptiste Daroussin 
4173a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_expand, collelem, rb_byexpand, element_compare_expand);
4183a444dbdSBaptiste Daroussin 
419057ca2d4SBaptiste Daroussin static int
420057ca2d4SBaptiste Daroussin collchar_compare(const void *n1, const void *n2)
421057ca2d4SBaptiste Daroussin {
422057ca2d4SBaptiste Daroussin 	wchar_t	k1 = ((const collchar_t *)n1)->wc;
423057ca2d4SBaptiste Daroussin 	wchar_t	k2 = ((const collchar_t *)n2)->wc;
424057ca2d4SBaptiste Daroussin 
425057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
426057ca2d4SBaptiste Daroussin }
427057ca2d4SBaptiste Daroussin 
4283a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collchars, collchar, entry, collchar_compare);
4293a444dbdSBaptiste Daroussin 
430057ca2d4SBaptiste Daroussin static int
431057ca2d4SBaptiste Daroussin subst_compare(const void *n1, const void *n2)
432057ca2d4SBaptiste Daroussin {
433057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const subst_t *)n1)->key;
434057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const subst_t *)n2)->key;
435057ca2d4SBaptiste Daroussin 
436057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
437057ca2d4SBaptiste Daroussin }
438057ca2d4SBaptiste Daroussin 
4393a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs, subst, entry, subst_compare);
4403a444dbdSBaptiste Daroussin 
441057ca2d4SBaptiste Daroussin static int
442057ca2d4SBaptiste Daroussin subst_compare_ref(const void *n1, const void *n2)
443057ca2d4SBaptiste Daroussin {
44455b270e6SBaptiste Daroussin 	const wchar_t *c1 = ((const subst_t *)n1)->ref;
44555b270e6SBaptiste Daroussin 	const wchar_t *c2 = ((const subst_t *)n2)->ref;
446057ca2d4SBaptiste Daroussin 	int rv;
447057ca2d4SBaptiste Daroussin 
44855b270e6SBaptiste Daroussin 	rv = wcscmp(c1, c2);
449057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
450057ca2d4SBaptiste Daroussin }
451057ca2d4SBaptiste Daroussin 
4523a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs_ref, subst, entry_ref, subst_compare_ref);
4533a444dbdSBaptiste Daroussin 
454057ca2d4SBaptiste Daroussin void
455057ca2d4SBaptiste Daroussin init_collate(void)
456057ca2d4SBaptiste Daroussin {
457057ca2d4SBaptiste Daroussin 	int i;
458057ca2d4SBaptiste Daroussin 
4593a444dbdSBaptiste Daroussin 	RB_INIT(&collsyms);
460057ca2d4SBaptiste Daroussin 
4613a444dbdSBaptiste Daroussin 	RB_INIT(&collundefs);
462057ca2d4SBaptiste Daroussin 
4633a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_symbol);
464057ca2d4SBaptiste Daroussin 
4653a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_expand);
4663a444dbdSBaptiste Daroussin 
4673a444dbdSBaptiste Daroussin 	RB_INIT(&collchars);
468057ca2d4SBaptiste Daroussin 
469057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
4703a444dbdSBaptiste Daroussin 		RB_INIT(&substs[i]);
4713a444dbdSBaptiste Daroussin 		RB_INIT(&substs_ref[i]);
4723a444dbdSBaptiste Daroussin 		RB_INIT(&weights[i]);
473057ca2d4SBaptiste Daroussin 		nweight[i] = 1;
474057ca2d4SBaptiste Daroussin 	}
475057ca2d4SBaptiste Daroussin 
476057ca2d4SBaptiste Daroussin 	(void) memset(&collinfo, 0, sizeof (collinfo));
477057ca2d4SBaptiste Daroussin 
478057ca2d4SBaptiste Daroussin 	/* allocate some initial priorities */
479057ca2d4SBaptiste Daroussin 	pri_ignore = new_pri();
480057ca2d4SBaptiste Daroussin 
481057ca2d4SBaptiste Daroussin 	set_pri(pri_ignore, 0, RESOLVED);
482057ca2d4SBaptiste Daroussin 
483057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
484057ca2d4SBaptiste Daroussin 		pri_undefined[i] = new_pri();
485057ca2d4SBaptiste Daroussin 
486057ca2d4SBaptiste Daroussin 		/* we will override this later */
487057ca2d4SBaptiste Daroussin 		set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN);
488057ca2d4SBaptiste Daroussin 	}
489057ca2d4SBaptiste Daroussin }
490057ca2d4SBaptiste Daroussin 
491057ca2d4SBaptiste Daroussin void
492057ca2d4SBaptiste Daroussin define_collsym(char *name)
493057ca2d4SBaptiste Daroussin {
494057ca2d4SBaptiste Daroussin 	collsym_t	*sym;
495057ca2d4SBaptiste Daroussin 
4964c22fda9SMarcelo Araujo 	if ((sym = calloc(1, sizeof(*sym))) == NULL) {
497057ca2d4SBaptiste Daroussin 		fprintf(stderr,"out of memory");
498057ca2d4SBaptiste Daroussin 		return;
499057ca2d4SBaptiste Daroussin 	}
500057ca2d4SBaptiste Daroussin 	sym->name = name;
501057ca2d4SBaptiste Daroussin 	sym->ref = new_pri();
502057ca2d4SBaptiste Daroussin 
5033a444dbdSBaptiste Daroussin 	if (RB_FIND(collsyms, &collsyms, sym) != NULL) {
504057ca2d4SBaptiste Daroussin 		/*
505057ca2d4SBaptiste Daroussin 		 * This should never happen because we are only called
506057ca2d4SBaptiste Daroussin 		 * for undefined symbols.
507057ca2d4SBaptiste Daroussin 		 */
508e6d8c0e2SBaptiste Daroussin 		free(sym);
509057ca2d4SBaptiste Daroussin 		INTERR;
510057ca2d4SBaptiste Daroussin 		return;
511057ca2d4SBaptiste Daroussin 	}
5123a444dbdSBaptiste Daroussin 	RB_INSERT(collsyms, &collsyms, sym);
513057ca2d4SBaptiste Daroussin }
514057ca2d4SBaptiste Daroussin 
515057ca2d4SBaptiste Daroussin collsym_t *
516057ca2d4SBaptiste Daroussin lookup_collsym(char *name)
517057ca2d4SBaptiste Daroussin {
518057ca2d4SBaptiste Daroussin 	collsym_t	srch;
519057ca2d4SBaptiste Daroussin 
520057ca2d4SBaptiste Daroussin 	srch.name = name;
5213a444dbdSBaptiste Daroussin 	return (RB_FIND(collsyms, &collsyms, &srch));
522057ca2d4SBaptiste Daroussin }
523057ca2d4SBaptiste Daroussin 
524057ca2d4SBaptiste Daroussin collelem_t *
525057ca2d4SBaptiste Daroussin lookup_collelem(char *symbol)
526057ca2d4SBaptiste Daroussin {
527057ca2d4SBaptiste Daroussin 	collelem_t	srch;
528057ca2d4SBaptiste Daroussin 
529057ca2d4SBaptiste Daroussin 	srch.symbol = symbol;
5303a444dbdSBaptiste Daroussin 	return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch));
531057ca2d4SBaptiste Daroussin }
532057ca2d4SBaptiste Daroussin 
533057ca2d4SBaptiste Daroussin static collundef_t *
534057ca2d4SBaptiste Daroussin get_collundef(char *name)
535057ca2d4SBaptiste Daroussin {
536057ca2d4SBaptiste Daroussin 	collundef_t	srch;
537057ca2d4SBaptiste Daroussin 	collundef_t	*ud;
538057ca2d4SBaptiste Daroussin 	int		i;
539057ca2d4SBaptiste Daroussin 
540057ca2d4SBaptiste Daroussin 	srch.name = name;
5413a444dbdSBaptiste Daroussin 	if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) {
5424c22fda9SMarcelo Araujo 		if (((ud = calloc(1, sizeof(*ud))) == NULL) ||
543057ca2d4SBaptiste Daroussin 		    ((ud->name = strdup(name)) == NULL)) {
544057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
545e6d8c0e2SBaptiste Daroussin 			free(ud);
546057ca2d4SBaptiste Daroussin 			return (NULL);
547057ca2d4SBaptiste Daroussin 		}
548057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
549057ca2d4SBaptiste Daroussin 			ud->ref[i] = new_pri();
550057ca2d4SBaptiste Daroussin 		}
5513a444dbdSBaptiste Daroussin 		RB_INSERT(collundefs, &collundefs, ud);
552057ca2d4SBaptiste Daroussin 	}
553057ca2d4SBaptiste Daroussin 	add_charmap_undefined(name);
554057ca2d4SBaptiste Daroussin 	return (ud);
555057ca2d4SBaptiste Daroussin }
556057ca2d4SBaptiste Daroussin 
557057ca2d4SBaptiste Daroussin static collchar_t *
558057ca2d4SBaptiste Daroussin get_collchar(wchar_t wc, int create)
559057ca2d4SBaptiste Daroussin {
560057ca2d4SBaptiste Daroussin 	collchar_t	srch;
561057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
562057ca2d4SBaptiste Daroussin 	int		i;
563057ca2d4SBaptiste Daroussin 
564057ca2d4SBaptiste Daroussin 	srch.wc = wc;
5653a444dbdSBaptiste Daroussin 	cc = RB_FIND(collchars, &collchars, &srch);
566057ca2d4SBaptiste Daroussin 	if ((cc == NULL) && create) {
5674c22fda9SMarcelo Araujo 		if ((cc = calloc(1, sizeof(*cc))) == NULL) {
568057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
569057ca2d4SBaptiste Daroussin 			return (NULL);
570057ca2d4SBaptiste Daroussin 		}
571057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
572057ca2d4SBaptiste Daroussin 			cc->ref[i] = new_pri();
573057ca2d4SBaptiste Daroussin 		}
574057ca2d4SBaptiste Daroussin 		cc->wc = wc;
5753a444dbdSBaptiste Daroussin 		RB_INSERT(collchars, &collchars, cc);
576057ca2d4SBaptiste Daroussin 	}
577057ca2d4SBaptiste Daroussin 	return (cc);
578057ca2d4SBaptiste Daroussin }
579057ca2d4SBaptiste Daroussin 
580057ca2d4SBaptiste Daroussin void
581057ca2d4SBaptiste Daroussin end_order_collsym(collsym_t *sym)
582057ca2d4SBaptiste Daroussin {
583057ca2d4SBaptiste Daroussin 	start_order(T_COLLSYM);
584057ca2d4SBaptiste Daroussin 	/* update the weight */
585057ca2d4SBaptiste Daroussin 
586057ca2d4SBaptiste Daroussin 	set_pri(sym->ref, nextpri, RESOLVED);
587057ca2d4SBaptiste Daroussin 	nextpri++;
588057ca2d4SBaptiste Daroussin }
589057ca2d4SBaptiste Daroussin 
590057ca2d4SBaptiste Daroussin void
591057ca2d4SBaptiste Daroussin end_order(void)
592057ca2d4SBaptiste Daroussin {
593057ca2d4SBaptiste Daroussin 	int		i;
594057ca2d4SBaptiste Daroussin 	int32_t		pri;
595057ca2d4SBaptiste Daroussin 	int32_t		ref;
596057ca2d4SBaptiste Daroussin 	collpri_t	*p;
597057ca2d4SBaptiste Daroussin 
598057ca2d4SBaptiste Daroussin 	/* advance the priority/weight */
599057ca2d4SBaptiste Daroussin 	pri = nextpri;
600057ca2d4SBaptiste Daroussin 
601057ca2d4SBaptiste Daroussin 	switch (currorder) {
602057ca2d4SBaptiste Daroussin 	case T_CHAR:
603057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
604057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
605057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
606057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
607057ca2d4SBaptiste Daroussin 				/* unspecified weight is a self reference */
608057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], pri, RESOLVED);
609057ca2d4SBaptiste Daroussin 			} else {
610057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], ref, REFER);
611057ca2d4SBaptiste Daroussin 			}
612057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
613057ca2d4SBaptiste Daroussin 		}
614057ca2d4SBaptiste Daroussin 
615057ca2d4SBaptiste Daroussin 		/* leave a cookie trail in case next symbol is ellipsis */
616057ca2d4SBaptiste Daroussin 		ellipsis_start = currchar->wc + 1;
617057ca2d4SBaptiste Daroussin 		currchar = NULL;
618057ca2d4SBaptiste Daroussin 		break;
619057ca2d4SBaptiste Daroussin 
620057ca2d4SBaptiste Daroussin 	case T_ELLIPSIS:
621057ca2d4SBaptiste Daroussin 		/* save off the weights were we can find them */
622057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
623057ca2d4SBaptiste Daroussin 			ellipsis_weights[i] = order_weights[i];
624057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
625057ca2d4SBaptiste Daroussin 		}
626057ca2d4SBaptiste Daroussin 		break;
627057ca2d4SBaptiste Daroussin 
628057ca2d4SBaptiste Daroussin 	case T_COLLELEM:
629057ca2d4SBaptiste Daroussin 		if (currelem == NULL) {
630057ca2d4SBaptiste Daroussin 			INTERR;
631057ca2d4SBaptiste Daroussin 		} else {
632057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
633057ca2d4SBaptiste Daroussin 
634057ca2d4SBaptiste Daroussin 				if (((ref = order_weights[i]) < 0) ||
635057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
636057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
637057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], pri,
638057ca2d4SBaptiste Daroussin 					    RESOLVED);
639057ca2d4SBaptiste Daroussin 				} else {
640057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], ref, REFER);
641057ca2d4SBaptiste Daroussin 				}
642057ca2d4SBaptiste Daroussin 				order_weights[i] = -1;
643057ca2d4SBaptiste Daroussin 			}
644057ca2d4SBaptiste Daroussin 		}
645057ca2d4SBaptiste Daroussin 		break;
646057ca2d4SBaptiste Daroussin 
647057ca2d4SBaptiste Daroussin 	case T_UNDEFINED:
648057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
649057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
650057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
651057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
652057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], -1, RESOLVED);
653057ca2d4SBaptiste Daroussin 			} else {
654057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], ref, REFER);
655057ca2d4SBaptiste Daroussin 			}
656057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
657057ca2d4SBaptiste Daroussin 		}
658057ca2d4SBaptiste Daroussin 		break;
659057ca2d4SBaptiste Daroussin 
660057ca2d4SBaptiste Daroussin 	case T_SYMBOL:
661057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
662057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
663057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
664057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
665057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], pri, RESOLVED);
666057ca2d4SBaptiste Daroussin 			} else {
667057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], ref, REFER);
668057ca2d4SBaptiste Daroussin 			}
669057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
670057ca2d4SBaptiste Daroussin 		}
671057ca2d4SBaptiste Daroussin 		break;
672057ca2d4SBaptiste Daroussin 
673057ca2d4SBaptiste Daroussin 	default:
674057ca2d4SBaptiste Daroussin 		INTERR;
675057ca2d4SBaptiste Daroussin 	}
676057ca2d4SBaptiste Daroussin 
677057ca2d4SBaptiste Daroussin 	nextpri++;
678057ca2d4SBaptiste Daroussin }
679057ca2d4SBaptiste Daroussin 
680057ca2d4SBaptiste Daroussin static void
681057ca2d4SBaptiste Daroussin start_order(int type)
682057ca2d4SBaptiste Daroussin {
683057ca2d4SBaptiste Daroussin 	int	i;
684057ca2d4SBaptiste Daroussin 
685057ca2d4SBaptiste Daroussin 	lastorder = currorder;
686057ca2d4SBaptiste Daroussin 	currorder = type;
687057ca2d4SBaptiste Daroussin 
688057ca2d4SBaptiste Daroussin 	/* this is used to protect ELLIPSIS processing */
689057ca2d4SBaptiste Daroussin 	if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) {
690057ca2d4SBaptiste Daroussin 		fprintf(stderr, "character value expected");
691057ca2d4SBaptiste Daroussin 	}
692057ca2d4SBaptiste Daroussin 
693057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
694057ca2d4SBaptiste Daroussin 		order_weights[i] = -1;
695057ca2d4SBaptiste Daroussin 	}
696057ca2d4SBaptiste Daroussin 	curr_weight = 0;
697057ca2d4SBaptiste Daroussin }
698057ca2d4SBaptiste Daroussin 
699057ca2d4SBaptiste Daroussin void
700057ca2d4SBaptiste Daroussin start_order_undefined(void)
701057ca2d4SBaptiste Daroussin {
702057ca2d4SBaptiste Daroussin 	start_order(T_UNDEFINED);
703057ca2d4SBaptiste Daroussin }
704057ca2d4SBaptiste Daroussin 
705057ca2d4SBaptiste Daroussin void
706057ca2d4SBaptiste Daroussin start_order_symbol(char *name)
707057ca2d4SBaptiste Daroussin {
708057ca2d4SBaptiste Daroussin 	currundef = get_collundef(name);
709057ca2d4SBaptiste Daroussin 	start_order(T_SYMBOL);
710057ca2d4SBaptiste Daroussin }
711057ca2d4SBaptiste Daroussin 
712057ca2d4SBaptiste Daroussin void
713057ca2d4SBaptiste Daroussin start_order_char(wchar_t wc)
714057ca2d4SBaptiste Daroussin {
715057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
716057ca2d4SBaptiste Daroussin 	int32_t		ref;
717057ca2d4SBaptiste Daroussin 
718057ca2d4SBaptiste Daroussin 	start_order(T_CHAR);
719057ca2d4SBaptiste Daroussin 
720057ca2d4SBaptiste Daroussin 	/*
721057ca2d4SBaptiste Daroussin 	 * If we last saw an ellipsis, then we need to close the range.
722057ca2d4SBaptiste Daroussin 	 * Handle that here.  Note that we have to be careful because the
723057ca2d4SBaptiste Daroussin 	 * items *inside* the range are treated exclusiveley to the items
724057ca2d4SBaptiste Daroussin 	 * outside of the range.  The ends of the range can have quite
725057ca2d4SBaptiste Daroussin 	 * different weights than the range members.
726057ca2d4SBaptiste Daroussin 	 */
727057ca2d4SBaptiste Daroussin 	if (lastorder == T_ELLIPSIS) {
728057ca2d4SBaptiste Daroussin 		int		i;
729057ca2d4SBaptiste Daroussin 
730057ca2d4SBaptiste Daroussin 		if (wc < ellipsis_start) {
731057ca2d4SBaptiste Daroussin 			fprintf(stderr, "malformed range!");
732057ca2d4SBaptiste Daroussin 			return;
733057ca2d4SBaptiste Daroussin 		}
734057ca2d4SBaptiste Daroussin 		while (ellipsis_start < wc) {
735057ca2d4SBaptiste Daroussin 			/*
736057ca2d4SBaptiste Daroussin 			 * pick all of the saved weights for the
737057ca2d4SBaptiste Daroussin 			 * ellipsis.  note that -1 encodes for the
738057ca2d4SBaptiste Daroussin 			 * ellipsis itself, which means to take the
739057ca2d4SBaptiste Daroussin 			 * current relative priority.
740057ca2d4SBaptiste Daroussin 			 */
741057ca2d4SBaptiste Daroussin 			if ((cc = get_collchar(ellipsis_start, 1)) == NULL) {
742057ca2d4SBaptiste Daroussin 				INTERR;
743057ca2d4SBaptiste Daroussin 				return;
744057ca2d4SBaptiste Daroussin 			}
745057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
746057ca2d4SBaptiste Daroussin 				collpri_t *p;
747057ca2d4SBaptiste Daroussin 				if (((ref = ellipsis_weights[i]) == -1) ||
748057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
749057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
750057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], nextpri, RESOLVED);
751057ca2d4SBaptiste Daroussin 				} else {
752057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], ref, REFER);
753057ca2d4SBaptiste Daroussin 				}
754057ca2d4SBaptiste Daroussin 				ellipsis_weights[i] = 0;
755057ca2d4SBaptiste Daroussin 			}
756057ca2d4SBaptiste Daroussin 			ellipsis_start++;
757057ca2d4SBaptiste Daroussin 			nextpri++;
758057ca2d4SBaptiste Daroussin 		}
759057ca2d4SBaptiste Daroussin 	}
760057ca2d4SBaptiste Daroussin 
761057ca2d4SBaptiste Daroussin 	currchar = get_collchar(wc, 1);
762057ca2d4SBaptiste Daroussin }
763057ca2d4SBaptiste Daroussin 
764057ca2d4SBaptiste Daroussin void
765057ca2d4SBaptiste Daroussin start_order_collelem(collelem_t *e)
766057ca2d4SBaptiste Daroussin {
767057ca2d4SBaptiste Daroussin 	start_order(T_COLLELEM);
768057ca2d4SBaptiste Daroussin 	currelem = e;
769057ca2d4SBaptiste Daroussin }
770057ca2d4SBaptiste Daroussin 
771057ca2d4SBaptiste Daroussin void
772057ca2d4SBaptiste Daroussin start_order_ellipsis(void)
773057ca2d4SBaptiste Daroussin {
774057ca2d4SBaptiste Daroussin 	int	i;
775057ca2d4SBaptiste Daroussin 
776057ca2d4SBaptiste Daroussin 	start_order(T_ELLIPSIS);
777057ca2d4SBaptiste Daroussin 
778057ca2d4SBaptiste Daroussin 	if (lastorder != T_CHAR) {
779057ca2d4SBaptiste Daroussin 		fprintf(stderr, "illegal starting point for range");
780057ca2d4SBaptiste Daroussin 		return;
781057ca2d4SBaptiste Daroussin 	}
782057ca2d4SBaptiste Daroussin 
783057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
784057ca2d4SBaptiste Daroussin 		ellipsis_weights[i] = order_weights[i];
785057ca2d4SBaptiste Daroussin 	}
786057ca2d4SBaptiste Daroussin }
787057ca2d4SBaptiste Daroussin 
788057ca2d4SBaptiste Daroussin void
789057ca2d4SBaptiste Daroussin define_collelem(char *name, wchar_t *wcs)
790057ca2d4SBaptiste Daroussin {
791057ca2d4SBaptiste Daroussin 	collelem_t	*e;
792057ca2d4SBaptiste Daroussin 	int		i;
793057ca2d4SBaptiste Daroussin 
794057ca2d4SBaptiste Daroussin 	if (wcslen(wcs) >= COLLATE_STR_LEN) {
795057ca2d4SBaptiste Daroussin 		fprintf(stderr,"expanded collation element too long");
796057ca2d4SBaptiste Daroussin 		return;
797057ca2d4SBaptiste Daroussin 	}
798057ca2d4SBaptiste Daroussin 
7994c22fda9SMarcelo Araujo 	if ((e = calloc(1, sizeof(*e))) == NULL) {
800057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
801057ca2d4SBaptiste Daroussin 		return;
802057ca2d4SBaptiste Daroussin 	}
803057ca2d4SBaptiste Daroussin 	e->expand = wcs;
804057ca2d4SBaptiste Daroussin 	e->symbol = name;
805057ca2d4SBaptiste Daroussin 
806057ca2d4SBaptiste Daroussin 	/*
807057ca2d4SBaptiste Daroussin 	 * This is executed before the order statement, so we don't
808057ca2d4SBaptiste Daroussin 	 * know how many priorities we *really* need.  We allocate one
809057ca2d4SBaptiste Daroussin 	 * for each possible weight.  Not a big deal, as collating-elements
810057ca2d4SBaptiste Daroussin 	 * prove to be quite rare.
811057ca2d4SBaptiste Daroussin 	 */
812057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
813057ca2d4SBaptiste Daroussin 		e->ref[i] = new_pri();
814057ca2d4SBaptiste Daroussin 	}
815057ca2d4SBaptiste Daroussin 
816057ca2d4SBaptiste Daroussin 	/* A character sequence can only reduce to one element. */
8173a444dbdSBaptiste Daroussin 	if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) ||
8183a444dbdSBaptiste Daroussin 	    (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) {
819057ca2d4SBaptiste Daroussin 		fprintf(stderr, "duplicate collating element definition");
820e6d8c0e2SBaptiste Daroussin 		free(e);
821057ca2d4SBaptiste Daroussin 		return;
822057ca2d4SBaptiste Daroussin 	}
8233a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_symbol, &elem_by_symbol, e);
8243a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_expand, &elem_by_expand, e);
825057ca2d4SBaptiste Daroussin }
826057ca2d4SBaptiste Daroussin 
827057ca2d4SBaptiste Daroussin void
828057ca2d4SBaptiste Daroussin add_order_bit(int kw)
829057ca2d4SBaptiste Daroussin {
830057ca2d4SBaptiste Daroussin 	uint8_t bit = DIRECTIVE_UNDEF;
831057ca2d4SBaptiste Daroussin 
832057ca2d4SBaptiste Daroussin 	switch (kw) {
833057ca2d4SBaptiste Daroussin 	case T_FORWARD:
834057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_FORWARD;
835057ca2d4SBaptiste Daroussin 		break;
836057ca2d4SBaptiste Daroussin 	case T_BACKWARD:
837057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_BACKWARD;
838057ca2d4SBaptiste Daroussin 		break;
839057ca2d4SBaptiste Daroussin 	case T_POSITION:
840057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_POSITION;
841057ca2d4SBaptiste Daroussin 		break;
842057ca2d4SBaptiste Daroussin 	default:
843057ca2d4SBaptiste Daroussin 		INTERR;
844057ca2d4SBaptiste Daroussin 		break;
845057ca2d4SBaptiste Daroussin 	}
846057ca2d4SBaptiste Daroussin 	collinfo.directive[collinfo.directive_count] |= bit;
847057ca2d4SBaptiste Daroussin }
848057ca2d4SBaptiste Daroussin 
849057ca2d4SBaptiste Daroussin void
850057ca2d4SBaptiste Daroussin add_order_directive(void)
851057ca2d4SBaptiste Daroussin {
852057ca2d4SBaptiste Daroussin 	if (collinfo.directive_count >= COLL_WEIGHTS_MAX) {
853057ca2d4SBaptiste Daroussin 		fprintf(stderr,"too many directives (max %d)", COLL_WEIGHTS_MAX);
854057ca2d4SBaptiste Daroussin 	}
855057ca2d4SBaptiste Daroussin 	collinfo.directive_count++;
856057ca2d4SBaptiste Daroussin }
857057ca2d4SBaptiste Daroussin 
858057ca2d4SBaptiste Daroussin static void
859057ca2d4SBaptiste Daroussin add_order_pri(int32_t ref)
860057ca2d4SBaptiste Daroussin {
861057ca2d4SBaptiste Daroussin 	if (curr_weight >= NUM_WT) {
862057ca2d4SBaptiste Daroussin 		fprintf(stderr,"too many weights (max %d)", NUM_WT);
863057ca2d4SBaptiste Daroussin 		return;
864057ca2d4SBaptiste Daroussin 	}
865057ca2d4SBaptiste Daroussin 	order_weights[curr_weight] = ref;
866057ca2d4SBaptiste Daroussin 	curr_weight++;
867057ca2d4SBaptiste Daroussin }
868057ca2d4SBaptiste Daroussin 
869057ca2d4SBaptiste Daroussin void
870057ca2d4SBaptiste Daroussin add_order_collsym(collsym_t *s)
871057ca2d4SBaptiste Daroussin {
872057ca2d4SBaptiste Daroussin 	add_order_pri(s->ref);
873057ca2d4SBaptiste Daroussin }
874057ca2d4SBaptiste Daroussin 
875057ca2d4SBaptiste Daroussin void
876057ca2d4SBaptiste Daroussin add_order_char(wchar_t wc)
877057ca2d4SBaptiste Daroussin {
878057ca2d4SBaptiste Daroussin 	collchar_t *cc;
879057ca2d4SBaptiste Daroussin 
880057ca2d4SBaptiste Daroussin 	if ((cc = get_collchar(wc, 1)) == NULL) {
881057ca2d4SBaptiste Daroussin 		INTERR;
882057ca2d4SBaptiste Daroussin 		return;
883057ca2d4SBaptiste Daroussin 	}
884057ca2d4SBaptiste Daroussin 
885057ca2d4SBaptiste Daroussin 	add_order_pri(cc->ref[curr_weight]);
886057ca2d4SBaptiste Daroussin }
887057ca2d4SBaptiste Daroussin 
888057ca2d4SBaptiste Daroussin void
889057ca2d4SBaptiste Daroussin add_order_collelem(collelem_t *e)
890057ca2d4SBaptiste Daroussin {
891057ca2d4SBaptiste Daroussin 	add_order_pri(e->ref[curr_weight]);
892057ca2d4SBaptiste Daroussin }
893057ca2d4SBaptiste Daroussin 
894057ca2d4SBaptiste Daroussin void
895057ca2d4SBaptiste Daroussin add_order_ignore(void)
896057ca2d4SBaptiste Daroussin {
897057ca2d4SBaptiste Daroussin 	add_order_pri(pri_ignore);
898057ca2d4SBaptiste Daroussin }
899057ca2d4SBaptiste Daroussin 
900057ca2d4SBaptiste Daroussin void
901057ca2d4SBaptiste Daroussin add_order_symbol(char *sym)
902057ca2d4SBaptiste Daroussin {
903057ca2d4SBaptiste Daroussin 	collundef_t *c;
904057ca2d4SBaptiste Daroussin 	if ((c = get_collundef(sym)) == NULL) {
905057ca2d4SBaptiste Daroussin 		INTERR;
906057ca2d4SBaptiste Daroussin 		return;
907057ca2d4SBaptiste Daroussin 	}
908057ca2d4SBaptiste Daroussin 	add_order_pri(c->ref[curr_weight]);
909057ca2d4SBaptiste Daroussin }
910057ca2d4SBaptiste Daroussin 
911057ca2d4SBaptiste Daroussin void
912057ca2d4SBaptiste Daroussin add_order_ellipsis(void)
913057ca2d4SBaptiste Daroussin {
914057ca2d4SBaptiste Daroussin 	/* special NULL value indicates self reference */
915057ca2d4SBaptiste Daroussin 	add_order_pri(0);
916057ca2d4SBaptiste Daroussin }
917057ca2d4SBaptiste Daroussin 
918057ca2d4SBaptiste Daroussin void
919057ca2d4SBaptiste Daroussin add_order_subst(void)
920057ca2d4SBaptiste Daroussin {
921057ca2d4SBaptiste Daroussin 	subst_t srch;
922057ca2d4SBaptiste Daroussin 	subst_t	*s;
923057ca2d4SBaptiste Daroussin 	int i;
924057ca2d4SBaptiste Daroussin 
925057ca2d4SBaptiste Daroussin 	(void) memset(&srch, 0, sizeof (srch));
926057ca2d4SBaptiste Daroussin 	for (i = 0; i < curr_subst; i++) {
927057ca2d4SBaptiste Daroussin 		srch.ref[i] = subst_weights[i];
928057ca2d4SBaptiste Daroussin 		subst_weights[i] = 0;
929057ca2d4SBaptiste Daroussin 	}
9303a444dbdSBaptiste Daroussin 	s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch);
931057ca2d4SBaptiste Daroussin 
932057ca2d4SBaptiste Daroussin 	if (s == NULL) {
9334c22fda9SMarcelo Araujo 		if ((s = calloc(1, sizeof(*s))) == NULL) {
934057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
935057ca2d4SBaptiste Daroussin 			return;
936057ca2d4SBaptiste Daroussin 		}
937057ca2d4SBaptiste Daroussin 		s->key = new_pri();
938057ca2d4SBaptiste Daroussin 
939057ca2d4SBaptiste Daroussin 		/*
940057ca2d4SBaptiste Daroussin 		 * We use a self reference for our key, but we set a
941057ca2d4SBaptiste Daroussin 		 * high bit to indicate that this is a substitution
942057ca2d4SBaptiste Daroussin 		 * reference.  This will expedite table lookups later,
943057ca2d4SBaptiste Daroussin 		 * and prevent table lookups for situations that don't
944057ca2d4SBaptiste Daroussin 		 * require it.  (In short, its a big win, because we
945057ca2d4SBaptiste Daroussin 		 * can skip a lot of binary searching.)
946057ca2d4SBaptiste Daroussin 		 */
947057ca2d4SBaptiste Daroussin 		set_pri(s->key,
948057ca2d4SBaptiste Daroussin 		    (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY),
949057ca2d4SBaptiste Daroussin 		    RESOLVED);
950057ca2d4SBaptiste Daroussin 		nextsubst[curr_weight] += 1;
951057ca2d4SBaptiste Daroussin 
952057ca2d4SBaptiste Daroussin 		for (i = 0; i < curr_subst; i++) {
953057ca2d4SBaptiste Daroussin 			s->ref[i] = srch.ref[i];
954057ca2d4SBaptiste Daroussin 		}
955057ca2d4SBaptiste Daroussin 
9563a444dbdSBaptiste Daroussin 		RB_INSERT(substs_ref, &substs_ref[curr_weight], s);
957057ca2d4SBaptiste Daroussin 
9583a444dbdSBaptiste Daroussin 		if (RB_FIND(substs, &substs[curr_weight], s) != NULL) {
959057ca2d4SBaptiste Daroussin 			INTERR;
960057ca2d4SBaptiste Daroussin 			return;
961057ca2d4SBaptiste Daroussin 		}
9623a444dbdSBaptiste Daroussin 		RB_INSERT(substs, &substs[curr_weight], s);
963057ca2d4SBaptiste Daroussin 	}
964057ca2d4SBaptiste Daroussin 	curr_subst = 0;
965057ca2d4SBaptiste Daroussin 
966057ca2d4SBaptiste Daroussin 
967057ca2d4SBaptiste Daroussin 	/*
968057ca2d4SBaptiste Daroussin 	 * We are using the current (unique) priority as a search key
969057ca2d4SBaptiste Daroussin 	 * in the substitution table.
970057ca2d4SBaptiste Daroussin 	 */
971057ca2d4SBaptiste Daroussin 	add_order_pri(s->key);
972057ca2d4SBaptiste Daroussin }
973057ca2d4SBaptiste Daroussin 
974057ca2d4SBaptiste Daroussin static void
975057ca2d4SBaptiste Daroussin add_subst_pri(int32_t ref)
976057ca2d4SBaptiste Daroussin {
977057ca2d4SBaptiste Daroussin 	if (curr_subst >= COLLATE_STR_LEN) {
978057ca2d4SBaptiste Daroussin 		fprintf(stderr,"substitution string is too long");
979057ca2d4SBaptiste Daroussin 		return;
980057ca2d4SBaptiste Daroussin 	}
981057ca2d4SBaptiste Daroussin 	subst_weights[curr_subst] = ref;
982057ca2d4SBaptiste Daroussin 	curr_subst++;
983057ca2d4SBaptiste Daroussin }
984057ca2d4SBaptiste Daroussin 
985057ca2d4SBaptiste Daroussin void
986057ca2d4SBaptiste Daroussin add_subst_char(wchar_t wc)
987057ca2d4SBaptiste Daroussin {
988057ca2d4SBaptiste Daroussin 	collchar_t *cc;
989057ca2d4SBaptiste Daroussin 
990057ca2d4SBaptiste Daroussin 
991057ca2d4SBaptiste Daroussin 	if (((cc = get_collchar(wc, 1)) == NULL) ||
992057ca2d4SBaptiste Daroussin 	    (cc->wc != wc)) {
993057ca2d4SBaptiste Daroussin 		INTERR;
994057ca2d4SBaptiste Daroussin 		return;
995057ca2d4SBaptiste Daroussin 	}
996057ca2d4SBaptiste Daroussin 	/* we take the weight for the character at that position */
997057ca2d4SBaptiste Daroussin 	add_subst_pri(cc->ref[curr_weight]);
998057ca2d4SBaptiste Daroussin }
999057ca2d4SBaptiste Daroussin 
1000057ca2d4SBaptiste Daroussin void
1001057ca2d4SBaptiste Daroussin add_subst_collelem(collelem_t *e)
1002057ca2d4SBaptiste Daroussin {
1003057ca2d4SBaptiste Daroussin 	add_subst_pri(e->ref[curr_weight]);
1004057ca2d4SBaptiste Daroussin }
1005057ca2d4SBaptiste Daroussin 
1006057ca2d4SBaptiste Daroussin void
1007057ca2d4SBaptiste Daroussin add_subst_collsym(collsym_t *s)
1008057ca2d4SBaptiste Daroussin {
1009057ca2d4SBaptiste Daroussin 	add_subst_pri(s->ref);
1010057ca2d4SBaptiste Daroussin }
1011057ca2d4SBaptiste Daroussin 
1012057ca2d4SBaptiste Daroussin void
1013057ca2d4SBaptiste Daroussin add_subst_symbol(char *ptr)
1014057ca2d4SBaptiste Daroussin {
1015057ca2d4SBaptiste Daroussin 	collundef_t *cu;
1016057ca2d4SBaptiste Daroussin 
1017057ca2d4SBaptiste Daroussin 	if ((cu = get_collundef(ptr)) != NULL) {
1018057ca2d4SBaptiste Daroussin 		add_subst_pri(cu->ref[curr_weight]);
1019057ca2d4SBaptiste Daroussin 	}
1020057ca2d4SBaptiste Daroussin }
1021057ca2d4SBaptiste Daroussin 
1022057ca2d4SBaptiste Daroussin void
1023057ca2d4SBaptiste Daroussin add_weight(int32_t ref, int pass)
1024057ca2d4SBaptiste Daroussin {
1025057ca2d4SBaptiste Daroussin 	weight_t srch;
1026057ca2d4SBaptiste Daroussin 	weight_t *w;
1027057ca2d4SBaptiste Daroussin 
1028057ca2d4SBaptiste Daroussin 	srch.pri = resolve_pri(ref);
1029057ca2d4SBaptiste Daroussin 
1030057ca2d4SBaptiste Daroussin 	/* No translation of ignores */
1031057ca2d4SBaptiste Daroussin 	if (srch.pri == 0)
1032057ca2d4SBaptiste Daroussin 		return;
1033057ca2d4SBaptiste Daroussin 
1034057ca2d4SBaptiste Daroussin 	/* Substitution priorities are not weights */
1035057ca2d4SBaptiste Daroussin 	if (srch.pri & COLLATE_SUBST_PRIORITY)
1036057ca2d4SBaptiste Daroussin 		return;
1037057ca2d4SBaptiste Daroussin 
10383a444dbdSBaptiste Daroussin 	if (RB_FIND(weights, &weights[pass], &srch) != NULL)
1039057ca2d4SBaptiste Daroussin 		return;
1040057ca2d4SBaptiste Daroussin 
10414c22fda9SMarcelo Araujo 	if ((w = calloc(1, sizeof(*w))) == NULL) {
1042057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1043057ca2d4SBaptiste Daroussin 		return;
1044057ca2d4SBaptiste Daroussin 	}
1045057ca2d4SBaptiste Daroussin 	w->pri = srch.pri;
10463a444dbdSBaptiste Daroussin 	RB_INSERT(weights, &weights[pass], w);
1047057ca2d4SBaptiste Daroussin }
1048057ca2d4SBaptiste Daroussin 
1049057ca2d4SBaptiste Daroussin void
1050057ca2d4SBaptiste Daroussin add_weights(int32_t *refs)
1051057ca2d4SBaptiste Daroussin {
1052057ca2d4SBaptiste Daroussin 	int i;
1053057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1054057ca2d4SBaptiste Daroussin 		add_weight(refs[i], i);
1055057ca2d4SBaptiste Daroussin 	}
1056057ca2d4SBaptiste Daroussin }
1057057ca2d4SBaptiste Daroussin 
1058057ca2d4SBaptiste Daroussin int32_t
1059057ca2d4SBaptiste Daroussin get_weight(int32_t ref, int pass)
1060057ca2d4SBaptiste Daroussin {
1061057ca2d4SBaptiste Daroussin 	weight_t	srch;
1062057ca2d4SBaptiste Daroussin 	weight_t	*w;
1063057ca2d4SBaptiste Daroussin 	int32_t		pri;
1064057ca2d4SBaptiste Daroussin 
1065057ca2d4SBaptiste Daroussin 	pri = resolve_pri(ref);
1066057ca2d4SBaptiste Daroussin 	if (pri & COLLATE_SUBST_PRIORITY) {
1067057ca2d4SBaptiste Daroussin 		return (pri);
1068057ca2d4SBaptiste Daroussin 	}
1069057ca2d4SBaptiste Daroussin 	if (pri <= 0) {
1070057ca2d4SBaptiste Daroussin 		return (pri);
1071057ca2d4SBaptiste Daroussin 	}
1072057ca2d4SBaptiste Daroussin 	srch.pri = pri;
10733a444dbdSBaptiste Daroussin 	if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) {
1074057ca2d4SBaptiste Daroussin 		INTERR;
1075057ca2d4SBaptiste Daroussin 		return (-1);
1076057ca2d4SBaptiste Daroussin 	}
1077057ca2d4SBaptiste Daroussin 	return (w->opt);
1078057ca2d4SBaptiste Daroussin }
1079057ca2d4SBaptiste Daroussin 
1080057ca2d4SBaptiste Daroussin wchar_t *
1081057ca2d4SBaptiste Daroussin wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
1082057ca2d4SBaptiste Daroussin {
1083057ca2d4SBaptiste Daroussin 	wchar_t *os1 = s1;
1084057ca2d4SBaptiste Daroussin 
1085057ca2d4SBaptiste Daroussin 	n++;
1086*4644f9beSYuri Pankov 	while (--n > 0 && (*s1++ = htote(*s2++)) != 0)
1087057ca2d4SBaptiste Daroussin 		continue;
1088057ca2d4SBaptiste Daroussin 	if (n > 0)
1089057ca2d4SBaptiste Daroussin 		while (--n > 0)
1090057ca2d4SBaptiste Daroussin 			*s1++ = 0;
1091057ca2d4SBaptiste Daroussin 	return (os1);
1092057ca2d4SBaptiste Daroussin }
1093057ca2d4SBaptiste Daroussin 
109423a32822SBaptiste Daroussin #define RB_COUNT(x, name, head, cnt) do { \
109523a32822SBaptiste Daroussin 	(cnt) = 0; \
109623a32822SBaptiste Daroussin 	RB_FOREACH(x, name, (head)) { \
109723a32822SBaptiste Daroussin 		(cnt)++; \
109823a32822SBaptiste Daroussin 	} \
109923a32822SBaptiste Daroussin } while (0)
110023a32822SBaptiste Daroussin 
11013a444dbdSBaptiste Daroussin #define RB_NUMNODES(type, name, head, cnt) do { \
11023a444dbdSBaptiste Daroussin 	type *t; \
11033a444dbdSBaptiste Daroussin 	cnt = 0; \
11043a444dbdSBaptiste Daroussin 	RB_FOREACH(t, name, head) { \
11053a444dbdSBaptiste Daroussin 		cnt++; \
11063a444dbdSBaptiste Daroussin 	} \
110723a32822SBaptiste Daroussin } while (0)
11083a444dbdSBaptiste Daroussin 
1109057ca2d4SBaptiste Daroussin void
1110057ca2d4SBaptiste Daroussin dump_collate(void)
1111057ca2d4SBaptiste Daroussin {
1112057ca2d4SBaptiste Daroussin 	FILE			*f;
1113057ca2d4SBaptiste Daroussin 	int			i, j, n;
1114057ca2d4SBaptiste Daroussin 	size_t			sz;
1115057ca2d4SBaptiste Daroussin 	int32_t			pri;
1116057ca2d4SBaptiste Daroussin 	collelem_t		*ce;
1117057ca2d4SBaptiste Daroussin 	collchar_t		*cc;
1118057ca2d4SBaptiste Daroussin 	subst_t			*sb;
1119057ca2d4SBaptiste Daroussin 	char			vers[COLLATE_STR_LEN];
1120057ca2d4SBaptiste Daroussin 	collate_char_t		chars[UCHAR_MAX + 1];
1121057ca2d4SBaptiste Daroussin 	collate_large_t		*large;
1122057ca2d4SBaptiste Daroussin 	collate_subst_t		*subst[COLL_WEIGHTS_MAX];
1123057ca2d4SBaptiste Daroussin 	collate_chain_t		*chain;
1124057ca2d4SBaptiste Daroussin 
1125057ca2d4SBaptiste Daroussin 	/*
1126fcc7baa1SPedro F. Giffuni 	 * We have to run through a preliminary pass to identify all the
1127057ca2d4SBaptiste Daroussin 	 * weights that we use for each sorting level.
1128057ca2d4SBaptiste Daroussin 	 */
1129057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1130057ca2d4SBaptiste Daroussin 		add_weight(pri_ignore, i);
1131057ca2d4SBaptiste Daroussin 	}
1132057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
11333a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1134057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1135057ca2d4SBaptiste Daroussin 				add_weight(sb->ref[j], i);
1136057ca2d4SBaptiste Daroussin 			}
1137057ca2d4SBaptiste Daroussin 		}
1138057ca2d4SBaptiste Daroussin 	}
11393a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1140057ca2d4SBaptiste Daroussin 		add_weights(ce->ref);
1141057ca2d4SBaptiste Daroussin 	}
11423a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1143057ca2d4SBaptiste Daroussin 		add_weights(cc->ref);
1144057ca2d4SBaptiste Daroussin 	}
1145057ca2d4SBaptiste Daroussin 
1146057ca2d4SBaptiste Daroussin 	/*
1147057ca2d4SBaptiste Daroussin 	 * Now we walk the entire set of weights, removing the gaps
1148057ca2d4SBaptiste Daroussin 	 * in the weights.  This gives us optimum usage.  The walk
1149057ca2d4SBaptiste Daroussin 	 * occurs in priority.
1150057ca2d4SBaptiste Daroussin 	 */
1151057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1152057ca2d4SBaptiste Daroussin 		weight_t *w;
11533a444dbdSBaptiste Daroussin 		RB_FOREACH(w, weights, &weights[i]) {
1154057ca2d4SBaptiste Daroussin 			w->opt = nweight[i];
1155057ca2d4SBaptiste Daroussin 			nweight[i] += 1;
1156057ca2d4SBaptiste Daroussin 		}
1157057ca2d4SBaptiste Daroussin 	}
1158057ca2d4SBaptiste Daroussin 
1159057ca2d4SBaptiste Daroussin 	(void) memset(&chars, 0, sizeof (chars));
1160057ca2d4SBaptiste Daroussin 	(void) memset(vers, 0, COLLATE_STR_LEN);
1161057ca2d4SBaptiste Daroussin 	(void) strlcpy(vers, COLLATE_VERSION, sizeof (vers));
1162057ca2d4SBaptiste Daroussin 
1163057ca2d4SBaptiste Daroussin 	/*
1164057ca2d4SBaptiste Daroussin 	 * We need to make sure we arrange for the UNDEFINED field
1165057ca2d4SBaptiste Daroussin 	 * to show up.  Also, set the total weight counts.
1166057ca2d4SBaptiste Daroussin 	 */
1167057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1168057ca2d4SBaptiste Daroussin 		if (resolve_pri(pri_undefined[i]) == -1) {
1169057ca2d4SBaptiste Daroussin 			set_pri(pri_undefined[i], -1, RESOLVED);
1170057ca2d4SBaptiste Daroussin 			/* they collate at the end of everything else */
1171*4644f9beSYuri Pankov 			collinfo.undef_pri[i] = htote(COLLATE_MAX_PRIORITY);
1172057ca2d4SBaptiste Daroussin 		}
1173*4644f9beSYuri Pankov 		collinfo.pri_count[i] = htote(nweight[i]);
1174057ca2d4SBaptiste Daroussin 	}
1175057ca2d4SBaptiste Daroussin 
1176*4644f9beSYuri Pankov 	collinfo.pri_count[NUM_WT] = htote(max_wide());
1177*4644f9beSYuri Pankov 	collinfo.undef_pri[NUM_WT] = htote(COLLATE_MAX_PRIORITY);
1178057ca2d4SBaptiste Daroussin 	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
1179057ca2d4SBaptiste Daroussin 
1180057ca2d4SBaptiste Daroussin 	/*
1181057ca2d4SBaptiste Daroussin 	 * Ordinary character priorities
1182057ca2d4SBaptiste Daroussin 	 */
1183057ca2d4SBaptiste Daroussin 	for (i = 0; i <= UCHAR_MAX; i++) {
1184057ca2d4SBaptiste Daroussin 		if ((cc = get_collchar(i, 0)) != NULL) {
1185057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1186*4644f9beSYuri Pankov 				chars[i].pri[j] =
1187*4644f9beSYuri Pankov 				    htote(get_weight(cc->ref[j], j));
1188057ca2d4SBaptiste Daroussin 			}
1189057ca2d4SBaptiste Daroussin 		} else {
1190057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1191057ca2d4SBaptiste Daroussin 				chars[i].pri[j] =
1192*4644f9beSYuri Pankov 				    htote(get_weight(pri_undefined[j], j));
1193057ca2d4SBaptiste Daroussin 			}
1194057ca2d4SBaptiste Daroussin 			/*
1195057ca2d4SBaptiste Daroussin 			 * Per POSIX, for undefined characters, we
1196057ca2d4SBaptiste Daroussin 			 * also have to add a last item, which is the
1197057ca2d4SBaptiste Daroussin 			 * character code.
1198057ca2d4SBaptiste Daroussin 			 */
1199*4644f9beSYuri Pankov 			chars[i].pri[NUM_WT] = htote(i);
1200057ca2d4SBaptiste Daroussin 		}
1201057ca2d4SBaptiste Daroussin 	}
1202057ca2d4SBaptiste Daroussin 
1203057ca2d4SBaptiste Daroussin 	/*
1204057ca2d4SBaptiste Daroussin 	 * Substitution tables
1205057ca2d4SBaptiste Daroussin 	 */
1206057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1207057ca2d4SBaptiste Daroussin 		collate_subst_t *st = NULL;
120823a32822SBaptiste Daroussin 		subst_t *temp;
120923a32822SBaptiste Daroussin 		RB_COUNT(temp, substs, &substs[i], n);
1210*4644f9beSYuri Pankov 		subst_count[i] = n;
1211be4391a2SPedro F. Giffuni 		if ((st = calloc(n, sizeof(collate_subst_t))) == NULL) {
1212057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
1213057ca2d4SBaptiste Daroussin 			return;
1214057ca2d4SBaptiste Daroussin 		}
1215057ca2d4SBaptiste Daroussin 		n = 0;
12163a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1217057ca2d4SBaptiste Daroussin 			if ((st[n].key = resolve_pri(sb->key)) < 0) {
1218057ca2d4SBaptiste Daroussin 				/* by definition these resolve! */
1219057ca2d4SBaptiste Daroussin 				INTERR;
1220057ca2d4SBaptiste Daroussin 			}
1221057ca2d4SBaptiste Daroussin 			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
1222057ca2d4SBaptiste Daroussin 				INTERR;
1223057ca2d4SBaptiste Daroussin 			}
1224*4644f9beSYuri Pankov 			st[n].key = htote(st[n].key);
1225057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1226*4644f9beSYuri Pankov 				st[n].pri[j] = htote(get_weight(sb->ref[j],
1227*4644f9beSYuri Pankov 				    i));
1228057ca2d4SBaptiste Daroussin 			}
1229057ca2d4SBaptiste Daroussin 			n++;
1230057ca2d4SBaptiste Daroussin 		}
1231*4644f9beSYuri Pankov 		if (n != subst_count[i])
1232057ca2d4SBaptiste Daroussin 			INTERR;
1233057ca2d4SBaptiste Daroussin 		subst[i] = st;
1234057ca2d4SBaptiste Daroussin 	}
1235057ca2d4SBaptiste Daroussin 
1236057ca2d4SBaptiste Daroussin 
1237057ca2d4SBaptiste Daroussin 	/*
1238057ca2d4SBaptiste Daroussin 	 * Chains, i.e. collating elements
1239057ca2d4SBaptiste Daroussin 	 */
1240*4644f9beSYuri Pankov 	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, chain_count);
1241*4644f9beSYuri Pankov 	chain = calloc(chain_count, sizeof(collate_chain_t));
1242057ca2d4SBaptiste Daroussin 	if (chain == NULL) {
1243057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1244057ca2d4SBaptiste Daroussin 		return;
1245057ca2d4SBaptiste Daroussin 	}
12463a444dbdSBaptiste Daroussin 	n = 0;
12473a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1248057ca2d4SBaptiste Daroussin 		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
1249057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
1250*4644f9beSYuri Pankov 			chain[n].pri[i] = htote(get_weight(ce->ref[i], i));
1251057ca2d4SBaptiste Daroussin 		}
12525d21db09SBaptiste Daroussin 		n++;
1253057ca2d4SBaptiste Daroussin 	}
1254*4644f9beSYuri Pankov 	if (n != chain_count)
1255057ca2d4SBaptiste Daroussin 		INTERR;
1256057ca2d4SBaptiste Daroussin 
1257057ca2d4SBaptiste Daroussin 	/*
1258057ca2d4SBaptiste Daroussin 	 * Large (> UCHAR_MAX) character priorities
1259057ca2d4SBaptiste Daroussin 	 */
12603a444dbdSBaptiste Daroussin 	RB_NUMNODES(collchar_t, collchars, &collchars, n);
12615d21db09SBaptiste Daroussin 	large = calloc(n, sizeof(collate_large_t));
1262057ca2d4SBaptiste Daroussin 	if (large == NULL) {
1263057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1264057ca2d4SBaptiste Daroussin 		return;
1265057ca2d4SBaptiste Daroussin 	}
1266057ca2d4SBaptiste Daroussin 
1267057ca2d4SBaptiste Daroussin 	i = 0;
12683a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1269057ca2d4SBaptiste Daroussin 		int	undef = 0;
1270057ca2d4SBaptiste Daroussin 		/* we already gathered those */
1271057ca2d4SBaptiste Daroussin 		if (cc->wc <= UCHAR_MAX)
1272057ca2d4SBaptiste Daroussin 			continue;
1273057ca2d4SBaptiste Daroussin 		for (j = 0; j < NUM_WT; j++) {
1274057ca2d4SBaptiste Daroussin 			if ((pri = get_weight(cc->ref[j], j)) < 0) {
1275057ca2d4SBaptiste Daroussin 				undef = 1;
1276057ca2d4SBaptiste Daroussin 			}
1277057ca2d4SBaptiste Daroussin 			if (undef && (pri >= 0)) {
1278057ca2d4SBaptiste Daroussin 				/* if undefined, then all priorities are */
1279057ca2d4SBaptiste Daroussin 				INTERR;
1280057ca2d4SBaptiste Daroussin 			} else {
1281*4644f9beSYuri Pankov 				large[i].pri.pri[j] = htote(pri);
1282057ca2d4SBaptiste Daroussin 			}
1283057ca2d4SBaptiste Daroussin 		}
1284057ca2d4SBaptiste Daroussin 		if (!undef) {
1285*4644f9beSYuri Pankov 			large[i].val = htote(cc->wc);
1286*4644f9beSYuri Pankov 			large_count = i++;
1287057ca2d4SBaptiste Daroussin 		}
1288057ca2d4SBaptiste Daroussin 	}
1289057ca2d4SBaptiste Daroussin 
1290057ca2d4SBaptiste Daroussin 	if ((f = open_category()) == NULL) {
1291057ca2d4SBaptiste Daroussin 		return;
1292057ca2d4SBaptiste Daroussin 	}
1293057ca2d4SBaptiste Daroussin 
1294057ca2d4SBaptiste Daroussin 	/* Time to write the entire data set out */
1295057ca2d4SBaptiste Daroussin 
1296*4644f9beSYuri Pankov 	for (i = 0; i < NUM_WT; i++)
1297*4644f9beSYuri Pankov 		collinfo.subst_count[i] = htote(subst_count[i]);
1298*4644f9beSYuri Pankov 	collinfo.chain_count = htote(chain_count);
1299*4644f9beSYuri Pankov 	collinfo.large_count = htote(large_count);
1300*4644f9beSYuri Pankov 
1301057ca2d4SBaptiste Daroussin 	if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) ||
1302057ca2d4SBaptiste Daroussin 	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
1303057ca2d4SBaptiste Daroussin 	    (wr_category(&chars, sizeof (chars), f) < 0)) {
1304057ca2d4SBaptiste Daroussin 		return;
1305057ca2d4SBaptiste Daroussin 	}
1306057ca2d4SBaptiste Daroussin 
1307057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1308*4644f9beSYuri Pankov 		sz = sizeof (collate_subst_t) * subst_count[i];
1309057ca2d4SBaptiste Daroussin 		if (wr_category(subst[i], sz, f) < 0) {
1310057ca2d4SBaptiste Daroussin 			return;
1311057ca2d4SBaptiste Daroussin 		}
1312057ca2d4SBaptiste Daroussin 	}
1313*4644f9beSYuri Pankov 	sz = sizeof (collate_chain_t) * chain_count;
1314057ca2d4SBaptiste Daroussin 	if (wr_category(chain, sz, f) < 0) {
1315057ca2d4SBaptiste Daroussin 		return;
1316057ca2d4SBaptiste Daroussin 	}
1317*4644f9beSYuri Pankov 	sz = sizeof (collate_large_t) * large_count;
1318057ca2d4SBaptiste Daroussin 	if (wr_category(large, sz, f) < 0) {
1319057ca2d4SBaptiste Daroussin 		return;
1320057ca2d4SBaptiste Daroussin 	}
1321057ca2d4SBaptiste Daroussin 
1322057ca2d4SBaptiste Daroussin 	close_category(f);
1323057ca2d4SBaptiste Daroussin }
1324