xref: /freebsd/usr.bin/localedef/collate.c (revision cc7edd258c2564fe9e3c4a0dc839acc4a71caff9)
1e12a957fSPedro F. Giffuni /*-
24644f9beSYuri 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 
5200c61a3bSAlex Richardson _Static_assert(COLL_WEIGHTS_MAX == 10, "This code assumes a value of 10");
5300c61a3bSAlex Richardson 
54057ca2d4SBaptiste Daroussin /*
55057ca2d4SBaptiste Daroussin  * Design notes.
56057ca2d4SBaptiste Daroussin  *
57057ca2d4SBaptiste Daroussin  * It will be extremely helpful to the reader if they have access to
58057ca2d4SBaptiste Daroussin  * the localedef and locale file format specifications available.
59057ca2d4SBaptiste Daroussin  * Latest versions of these are available from www.opengroup.org.
60057ca2d4SBaptiste Daroussin  *
61057ca2d4SBaptiste Daroussin  * The design for the collation code is a bit complex.  The goal is a
62057ca2d4SBaptiste Daroussin  * single collation database as described in collate.h (in
63057ca2d4SBaptiste Daroussin  * libc/port/locale).  However, there are some other tidbits:
64057ca2d4SBaptiste Daroussin  *
65057ca2d4SBaptiste Daroussin  * a) The substitution entries are now a directly indexable array.  A
66057ca2d4SBaptiste Daroussin  * priority elsewhere in the table is taken as an index into the
67057ca2d4SBaptiste Daroussin  * substitution table if it has a high bit (COLLATE_SUBST_PRIORITY)
68057ca2d4SBaptiste Daroussin  * set.  (The bit is cleared and the result is the index into the
69057ca2d4SBaptiste Daroussin  * table.
70057ca2d4SBaptiste Daroussin  *
71057ca2d4SBaptiste Daroussin  * b) We eliminate duplicate entries into the substitution table.
72057ca2d4SBaptiste Daroussin  * This saves a lot of space.
73057ca2d4SBaptiste Daroussin  *
74057ca2d4SBaptiste Daroussin  * c) The priorities for each level are "compressed", so that each
75057ca2d4SBaptiste Daroussin  * sorting level has consecutively numbered priorities starting at 1.
76057ca2d4SBaptiste Daroussin  * (O is reserved for the ignore priority.)  This means sort levels
77057ca2d4SBaptiste Daroussin  * which only have a few distinct priorities can represent the
78057ca2d4SBaptiste Daroussin  * priority level in fewer bits, which makes the strxfrm output
79057ca2d4SBaptiste Daroussin  * smaller.
80057ca2d4SBaptiste Daroussin  *
81057ca2d4SBaptiste Daroussin  * d) We record the total number of priorities so that strxfrm can
82057ca2d4SBaptiste Daroussin  * figure out how many bytes to expand a numeric priority into.
83057ca2d4SBaptiste Daroussin  *
84057ca2d4SBaptiste Daroussin  * e) For the UNDEFINED pass (the last pass), we record the maximum
85057ca2d4SBaptiste Daroussin  * number of bits needed to uniquely prioritize these entries, so that
86057ca2d4SBaptiste Daroussin  * the last pass can also use smaller strxfrm output when possible.
87057ca2d4SBaptiste Daroussin  *
88057ca2d4SBaptiste Daroussin  * f) Priorities with the sign bit set are verboten.  This works out
89057ca2d4SBaptiste Daroussin  * because no active character set needs that bit to carry significant
90057ca2d4SBaptiste Daroussin  * information once the character is in wide form.
91057ca2d4SBaptiste Daroussin  *
92057ca2d4SBaptiste Daroussin  * To process the entire data to make the database, we actually run
93057ca2d4SBaptiste Daroussin  * multiple passes over the data.
94057ca2d4SBaptiste Daroussin  *
95057ca2d4SBaptiste Daroussin  * The first pass, which is done at parse time, identifies elements,
96057ca2d4SBaptiste Daroussin  * substitutions, and such, and records them in priority order.  As
97057ca2d4SBaptiste Daroussin  * some priorities can refer to other priorities, using forward
98057ca2d4SBaptiste Daroussin  * references, we use a table of references indicating whether the
99057ca2d4SBaptiste Daroussin  * priority's value has been resolved, or whether it is still a
100057ca2d4SBaptiste Daroussin  * reference.
101057ca2d4SBaptiste Daroussin  *
102057ca2d4SBaptiste Daroussin  * The second pass walks over all the items in priority order, noting
103057ca2d4SBaptiste Daroussin  * that they are used directly, and not just an indirect reference.
104057ca2d4SBaptiste Daroussin  * This is done by creating a "weight" structure for the item.  The
1053a444dbdSBaptiste Daroussin  * weights are stashed in an RB tree sorted by relative "priority".
106057ca2d4SBaptiste Daroussin  *
107057ca2d4SBaptiste Daroussin  * The third pass walks over all the weight structures, in priority
108057ca2d4SBaptiste Daroussin  * order, and assigns a new monotonically increasing (per sort level)
109057ca2d4SBaptiste Daroussin  * weight value to them.  These are the values that will actually be
110057ca2d4SBaptiste Daroussin  * written to the file.
111057ca2d4SBaptiste Daroussin  *
112057ca2d4SBaptiste Daroussin  * The fourth pass just writes the data out.
113057ca2d4SBaptiste Daroussin  */
114057ca2d4SBaptiste Daroussin 
115057ca2d4SBaptiste Daroussin /*
116057ca2d4SBaptiste Daroussin  * In order to resolve the priorities, we create a table of priorities.
117057ca2d4SBaptiste Daroussin  * Entries in the table can be in one of three states.
118057ca2d4SBaptiste Daroussin  *
119057ca2d4SBaptiste Daroussin  * UNKNOWN is for newly allocated entries, and indicates that nothing
120057ca2d4SBaptiste Daroussin  * is known about the priority.  (For example, when new entries are created
121057ca2d4SBaptiste Daroussin  * for collating-symbols, this is the value assigned for them until the
122057ca2d4SBaptiste Daroussin  * collating symbol's order has been determined.
123057ca2d4SBaptiste Daroussin  *
124057ca2d4SBaptiste Daroussin  * RESOLVED is used for an entry where the priority indicates the final
125057ca2d4SBaptiste Daroussin  * numeric weight.
126057ca2d4SBaptiste Daroussin  *
127057ca2d4SBaptiste Daroussin  * REFER is used for entries that reference other entries.  Typically
128057ca2d4SBaptiste Daroussin  * this is used for forward references.  A collating-symbol can never
129057ca2d4SBaptiste Daroussin  * have this value.
130057ca2d4SBaptiste Daroussin  *
131057ca2d4SBaptiste Daroussin  * The "pass" field is used during final resolution to aid in detection
132057ca2d4SBaptiste Daroussin  * of referencing loops.  (For example <A> depends on <B>, but <B> has its
133057ca2d4SBaptiste Daroussin  * priority dependent on <A>.)
134057ca2d4SBaptiste Daroussin  */
135057ca2d4SBaptiste Daroussin typedef enum {
136057ca2d4SBaptiste Daroussin 	UNKNOWN,	/* priority is totally unknown */
137057ca2d4SBaptiste Daroussin 	RESOLVED,	/* priority value fully resolved */
138057ca2d4SBaptiste Daroussin 	REFER		/* priority is a reference (index) */
139057ca2d4SBaptiste Daroussin } res_t;
140057ca2d4SBaptiste Daroussin 
141057ca2d4SBaptiste Daroussin typedef struct weight {
142057ca2d4SBaptiste Daroussin 	int32_t		pri;
143057ca2d4SBaptiste Daroussin 	int		opt;
1443a444dbdSBaptiste Daroussin 	RB_ENTRY(weight) entry;
145057ca2d4SBaptiste Daroussin } weight_t;
146057ca2d4SBaptiste Daroussin 
147057ca2d4SBaptiste Daroussin typedef struct priority {
148057ca2d4SBaptiste Daroussin 	res_t		res;
149057ca2d4SBaptiste Daroussin 	int32_t		pri;
150057ca2d4SBaptiste Daroussin 	int		pass;
151057ca2d4SBaptiste Daroussin 	int		lineno;
152057ca2d4SBaptiste Daroussin } collpri_t;
153057ca2d4SBaptiste Daroussin 
154057ca2d4SBaptiste Daroussin #define	NUM_WT	collinfo.directive_count
155057ca2d4SBaptiste Daroussin 
156057ca2d4SBaptiste Daroussin /*
157057ca2d4SBaptiste Daroussin  * These are the abstract collating symbols, which are just a symbolic
158057ca2d4SBaptiste Daroussin  * way to reference a priority.
159057ca2d4SBaptiste Daroussin  */
160057ca2d4SBaptiste Daroussin struct collsym {
161057ca2d4SBaptiste Daroussin 	char		*name;
162057ca2d4SBaptiste Daroussin 	int32_t		ref;
1633a444dbdSBaptiste Daroussin 	RB_ENTRY(collsym) entry;
164057ca2d4SBaptiste Daroussin };
165057ca2d4SBaptiste Daroussin 
166057ca2d4SBaptiste Daroussin /*
167057ca2d4SBaptiste Daroussin  * These are also abstract collating symbols, but we allow them to have
168057ca2d4SBaptiste Daroussin  * different priorities at different levels.
169057ca2d4SBaptiste Daroussin  */
170057ca2d4SBaptiste Daroussin typedef struct collundef {
171057ca2d4SBaptiste Daroussin 	char		*name;
172057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1733a444dbdSBaptiste Daroussin 	RB_ENTRY(collundef) entry;
174057ca2d4SBaptiste Daroussin } collundef_t;
175057ca2d4SBaptiste Daroussin 
176057ca2d4SBaptiste Daroussin /*
177057ca2d4SBaptiste Daroussin  * These are called "chains" in libc.  This records the fact that two
178057ca2d4SBaptiste Daroussin  * more characters should be treated as a single collating entity when
179057ca2d4SBaptiste Daroussin  * they appear together.  For example, in Spanish <C><h> gets collated
180057ca2d4SBaptiste Daroussin  * as a character between <C> and <D>.
181057ca2d4SBaptiste Daroussin  */
182057ca2d4SBaptiste Daroussin struct collelem {
183057ca2d4SBaptiste Daroussin 	char		*symbol;
184057ca2d4SBaptiste Daroussin 	wchar_t		*expand;
185057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1863a444dbdSBaptiste Daroussin 	RB_ENTRY(collelem) rb_bysymbol;
1873a444dbdSBaptiste Daroussin 	RB_ENTRY(collelem) rb_byexpand;
188057ca2d4SBaptiste Daroussin };
189057ca2d4SBaptiste Daroussin 
190057ca2d4SBaptiste Daroussin /*
191057ca2d4SBaptiste Daroussin  * Individual characters have a sequence of weights as well.
192057ca2d4SBaptiste Daroussin  */
193057ca2d4SBaptiste Daroussin typedef struct collchar {
194057ca2d4SBaptiste Daroussin 	wchar_t		wc;
195057ca2d4SBaptiste Daroussin 	int32_t		ref[COLL_WEIGHTS_MAX];
1963a444dbdSBaptiste Daroussin 	RB_ENTRY(collchar) entry;
197057ca2d4SBaptiste Daroussin } collchar_t;
198057ca2d4SBaptiste Daroussin 
199057ca2d4SBaptiste Daroussin /*
200057ca2d4SBaptiste Daroussin  * Substitution entries.  The key is itself a priority.  Note that
201057ca2d4SBaptiste Daroussin  * when we create one of these, we *automatically* wind up with a
202057ca2d4SBaptiste Daroussin  * fully resolved priority for the key, because creation of
203057ca2d4SBaptiste Daroussin  * substitutions creates a resolved priority at the same time.
204057ca2d4SBaptiste Daroussin  */
2053a444dbdSBaptiste Daroussin typedef struct subst{
206057ca2d4SBaptiste Daroussin 	int32_t		key;
207057ca2d4SBaptiste Daroussin 	int32_t		ref[COLLATE_STR_LEN];
2083a444dbdSBaptiste Daroussin 	RB_ENTRY(subst)	entry;
2093a444dbdSBaptiste Daroussin 	RB_ENTRY(subst)	entry_ref;
210057ca2d4SBaptiste Daroussin } subst_t;
211057ca2d4SBaptiste Daroussin 
2123a444dbdSBaptiste Daroussin static RB_HEAD(collsyms, collsym) collsyms;
2133a444dbdSBaptiste Daroussin static RB_HEAD(collundefs, collundef) collundefs;
2143a444dbdSBaptiste Daroussin static RB_HEAD(elem_by_symbol, collelem) elem_by_symbol;
2153a444dbdSBaptiste Daroussin static RB_HEAD(elem_by_expand, collelem) elem_by_expand;
2163a444dbdSBaptiste Daroussin static RB_HEAD(collchars, collchar) collchars;
2173a444dbdSBaptiste Daroussin static RB_HEAD(substs, subst) substs[COLL_WEIGHTS_MAX];
2183a444dbdSBaptiste Daroussin static RB_HEAD(substs_ref, subst) substs_ref[COLL_WEIGHTS_MAX];
2193a444dbdSBaptiste Daroussin static RB_HEAD(weights, weight) weights[COLL_WEIGHTS_MAX];
220057ca2d4SBaptiste Daroussin static int32_t		nweight[COLL_WEIGHTS_MAX];
221057ca2d4SBaptiste Daroussin 
222057ca2d4SBaptiste Daroussin /*
223057ca2d4SBaptiste Daroussin  * This is state tracking for the ellipsis token.  Note that we start
224057ca2d4SBaptiste Daroussin  * the initial values so that the ellipsis logic will think we got a
225057ca2d4SBaptiste Daroussin  * magic starting value of NUL.  It starts at minus one because the
226057ca2d4SBaptiste Daroussin  * starting point is exclusive -- i.e. the starting point is not
227057ca2d4SBaptiste Daroussin  * itself handled by the ellipsis code.
228057ca2d4SBaptiste Daroussin  */
229057ca2d4SBaptiste Daroussin static int currorder = EOF;
230057ca2d4SBaptiste Daroussin static int lastorder = EOF;
231057ca2d4SBaptiste Daroussin static collelem_t *currelem;
232057ca2d4SBaptiste Daroussin static collchar_t *currchar;
233057ca2d4SBaptiste Daroussin static collundef_t *currundef;
234057ca2d4SBaptiste Daroussin static wchar_t ellipsis_start = 0;
235057ca2d4SBaptiste Daroussin static int32_t ellipsis_weights[COLL_WEIGHTS_MAX];
236057ca2d4SBaptiste Daroussin 
237057ca2d4SBaptiste Daroussin /*
238057ca2d4SBaptiste Daroussin  * We keep a running tally of weights.
239057ca2d4SBaptiste Daroussin  */
240057ca2d4SBaptiste Daroussin static int nextpri = 1;
241057ca2d4SBaptiste Daroussin static int nextsubst[COLL_WEIGHTS_MAX] = { 0 };
242057ca2d4SBaptiste Daroussin 
243057ca2d4SBaptiste Daroussin /*
244057ca2d4SBaptiste Daroussin  * This array collects up the weights for each level.
245057ca2d4SBaptiste Daroussin  */
246057ca2d4SBaptiste Daroussin static int32_t order_weights[COLL_WEIGHTS_MAX];
247057ca2d4SBaptiste Daroussin static int curr_weight = 0;
248057ca2d4SBaptiste Daroussin static int32_t subst_weights[COLLATE_STR_LEN];
249057ca2d4SBaptiste Daroussin static int curr_subst = 0;
250057ca2d4SBaptiste Daroussin 
251057ca2d4SBaptiste Daroussin /*
252057ca2d4SBaptiste Daroussin  * Some initial priority values.
253057ca2d4SBaptiste Daroussin  */
254057ca2d4SBaptiste Daroussin static int32_t pri_undefined[COLL_WEIGHTS_MAX];
255057ca2d4SBaptiste Daroussin static int32_t pri_ignore;
256057ca2d4SBaptiste Daroussin 
257057ca2d4SBaptiste Daroussin static collate_info_t collinfo;
2584644f9beSYuri Pankov static int32_t subst_count[COLL_WEIGHTS_MAX];
2594644f9beSYuri Pankov static int32_t chain_count;
2604644f9beSYuri Pankov static int32_t large_count;
261057ca2d4SBaptiste Daroussin 
262057ca2d4SBaptiste Daroussin static collpri_t	*prilist = NULL;
263057ca2d4SBaptiste Daroussin static int		numpri = 0;
264057ca2d4SBaptiste Daroussin static int		maxpri = 0;
265057ca2d4SBaptiste Daroussin 
266057ca2d4SBaptiste Daroussin static void start_order(int);
267057ca2d4SBaptiste Daroussin 
268057ca2d4SBaptiste Daroussin static int32_t
269057ca2d4SBaptiste Daroussin new_pri(void)
270057ca2d4SBaptiste Daroussin {
271057ca2d4SBaptiste Daroussin 	int i;
272057ca2d4SBaptiste Daroussin 
273057ca2d4SBaptiste Daroussin 	if (numpri >= maxpri) {
274057ca2d4SBaptiste Daroussin 		maxpri = maxpri ? maxpri * 2 : 1024;
275057ca2d4SBaptiste Daroussin 		prilist = realloc(prilist, sizeof (collpri_t) * maxpri);
276057ca2d4SBaptiste Daroussin 		if (prilist == NULL) {
277057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
278057ca2d4SBaptiste Daroussin 			return (-1);
279057ca2d4SBaptiste Daroussin 		}
280057ca2d4SBaptiste Daroussin 		for (i = numpri; i < maxpri; i++) {
281057ca2d4SBaptiste Daroussin 			prilist[i].res = UNKNOWN;
282057ca2d4SBaptiste Daroussin 			prilist[i].pri = 0;
283057ca2d4SBaptiste Daroussin 			prilist[i].pass = 0;
284057ca2d4SBaptiste Daroussin 		}
285057ca2d4SBaptiste Daroussin 	}
286057ca2d4SBaptiste Daroussin 	return (numpri++);
287057ca2d4SBaptiste Daroussin }
288057ca2d4SBaptiste Daroussin 
289057ca2d4SBaptiste Daroussin static collpri_t *
290057ca2d4SBaptiste Daroussin get_pri(int32_t ref)
291057ca2d4SBaptiste Daroussin {
292057ca2d4SBaptiste Daroussin 	if ((ref < 0) || (ref > numpri)) {
293057ca2d4SBaptiste Daroussin 		INTERR;
294057ca2d4SBaptiste Daroussin 		return (NULL);
295057ca2d4SBaptiste Daroussin 	}
296057ca2d4SBaptiste Daroussin 	return (&prilist[ref]);
297057ca2d4SBaptiste Daroussin }
298057ca2d4SBaptiste Daroussin 
299057ca2d4SBaptiste Daroussin static void
300057ca2d4SBaptiste Daroussin set_pri(int32_t ref, int32_t v, res_t res)
301057ca2d4SBaptiste Daroussin {
302057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
303057ca2d4SBaptiste Daroussin 
304057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
305057ca2d4SBaptiste Daroussin 
306057ca2d4SBaptiste Daroussin 	if ((res == REFER) && ((v < 0) || (v >= numpri))) {
307057ca2d4SBaptiste Daroussin 		INTERR;
308057ca2d4SBaptiste Daroussin 	}
309057ca2d4SBaptiste Daroussin 
310057ca2d4SBaptiste Daroussin 	/* Resolve self references */
311057ca2d4SBaptiste Daroussin 	if ((res == REFER) && (ref == v)) {
312057ca2d4SBaptiste Daroussin 		v = nextpri;
313057ca2d4SBaptiste Daroussin 		res = RESOLVED;
314057ca2d4SBaptiste Daroussin 	}
315057ca2d4SBaptiste Daroussin 
316057ca2d4SBaptiste Daroussin 	if (pri->res != UNKNOWN) {
317057ca2d4SBaptiste Daroussin 		warn("repeated item in order list (first on %d)",
318057ca2d4SBaptiste Daroussin 		    pri->lineno);
319057ca2d4SBaptiste Daroussin 		return;
320057ca2d4SBaptiste Daroussin 	}
321057ca2d4SBaptiste Daroussin 	pri->lineno = lineno;
322057ca2d4SBaptiste Daroussin 	pri->pri = v;
323057ca2d4SBaptiste Daroussin 	pri->res = res;
324057ca2d4SBaptiste Daroussin }
325057ca2d4SBaptiste Daroussin 
326057ca2d4SBaptiste Daroussin static int32_t
327057ca2d4SBaptiste Daroussin resolve_pri(int32_t ref)
328057ca2d4SBaptiste Daroussin {
329057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
330057ca2d4SBaptiste Daroussin 	static int32_t	pass = 0;
331057ca2d4SBaptiste Daroussin 
332057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
333057ca2d4SBaptiste Daroussin 	pass++;
334057ca2d4SBaptiste Daroussin 	while (pri->res == REFER) {
335057ca2d4SBaptiste Daroussin 		if (pri->pass == pass) {
336057ca2d4SBaptiste Daroussin 			/* report a line with the circular symbol */
337057ca2d4SBaptiste Daroussin 			lineno = pri->lineno;
338057ca2d4SBaptiste Daroussin 			fprintf(stderr,"circular reference in order list");
339057ca2d4SBaptiste Daroussin 			return (-1);
340057ca2d4SBaptiste Daroussin 		}
341057ca2d4SBaptiste Daroussin 		if ((pri->pri < 0) || (pri->pri >= numpri)) {
342057ca2d4SBaptiste Daroussin 			INTERR;
343057ca2d4SBaptiste Daroussin 			return (-1);
344057ca2d4SBaptiste Daroussin 		}
345057ca2d4SBaptiste Daroussin 		pri->pass = pass;
346057ca2d4SBaptiste Daroussin 		pri = &prilist[pri->pri];
347057ca2d4SBaptiste Daroussin 	}
348057ca2d4SBaptiste Daroussin 
349057ca2d4SBaptiste Daroussin 	if (pri->res == UNKNOWN) {
350057ca2d4SBaptiste Daroussin 		return (-1);
351057ca2d4SBaptiste Daroussin 	}
352057ca2d4SBaptiste Daroussin 	if (pri->res != RESOLVED)
353057ca2d4SBaptiste Daroussin 		INTERR;
354057ca2d4SBaptiste Daroussin 
355057ca2d4SBaptiste Daroussin 	return (pri->pri);
356057ca2d4SBaptiste Daroussin }
357057ca2d4SBaptiste Daroussin 
358057ca2d4SBaptiste Daroussin static int
359057ca2d4SBaptiste Daroussin weight_compare(const void *n1, const void *n2)
360057ca2d4SBaptiste Daroussin {
361057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const weight_t *)n1)->pri;
362057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const weight_t *)n2)->pri;
363057ca2d4SBaptiste Daroussin 
364057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
365057ca2d4SBaptiste Daroussin }
366057ca2d4SBaptiste Daroussin 
3673a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(weights, weight, entry, weight_compare);
3683a444dbdSBaptiste Daroussin 
369057ca2d4SBaptiste Daroussin static int
370057ca2d4SBaptiste Daroussin collsym_compare(const void *n1, const void *n2)
371057ca2d4SBaptiste Daroussin {
372057ca2d4SBaptiste Daroussin 	const collsym_t *c1 = n1;
373057ca2d4SBaptiste Daroussin 	const collsym_t *c2 = n2;
374057ca2d4SBaptiste Daroussin 	int rv;
375057ca2d4SBaptiste Daroussin 
376057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
377057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
378057ca2d4SBaptiste Daroussin }
379057ca2d4SBaptiste Daroussin 
3803a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collsyms, collsym, entry, collsym_compare);
3813a444dbdSBaptiste Daroussin 
382057ca2d4SBaptiste Daroussin static int
383057ca2d4SBaptiste Daroussin collundef_compare(const void *n1, const void *n2)
384057ca2d4SBaptiste Daroussin {
385057ca2d4SBaptiste Daroussin 	const collundef_t *c1 = n1;
386057ca2d4SBaptiste Daroussin 	const collundef_t *c2 = n2;
387057ca2d4SBaptiste Daroussin 	int rv;
388057ca2d4SBaptiste Daroussin 
389057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
390057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
391057ca2d4SBaptiste Daroussin }
392057ca2d4SBaptiste Daroussin 
3933a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collundefs, collundef, entry, collundef_compare);
3943a444dbdSBaptiste Daroussin 
395057ca2d4SBaptiste Daroussin static int
396057ca2d4SBaptiste Daroussin element_compare_symbol(const void *n1, const void *n2)
397057ca2d4SBaptiste Daroussin {
398057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
399057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
400057ca2d4SBaptiste Daroussin 	int rv;
401057ca2d4SBaptiste Daroussin 
402057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->symbol, c2->symbol);
403057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
404057ca2d4SBaptiste Daroussin }
405057ca2d4SBaptiste Daroussin 
4063a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_symbol, collelem, rb_bysymbol, element_compare_symbol);
4073a444dbdSBaptiste Daroussin 
408057ca2d4SBaptiste Daroussin static int
409057ca2d4SBaptiste Daroussin element_compare_expand(const void *n1, const void *n2)
410057ca2d4SBaptiste Daroussin {
411057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
412057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
413057ca2d4SBaptiste Daroussin 	int rv;
414057ca2d4SBaptiste Daroussin 
415057ca2d4SBaptiste Daroussin 	rv = wcscmp(c1->expand, c2->expand);
416057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
417057ca2d4SBaptiste Daroussin }
418057ca2d4SBaptiste Daroussin 
4193a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_expand, collelem, rb_byexpand, element_compare_expand);
4203a444dbdSBaptiste Daroussin 
421057ca2d4SBaptiste Daroussin static int
422057ca2d4SBaptiste Daroussin collchar_compare(const void *n1, const void *n2)
423057ca2d4SBaptiste Daroussin {
424057ca2d4SBaptiste Daroussin 	wchar_t	k1 = ((const collchar_t *)n1)->wc;
425057ca2d4SBaptiste Daroussin 	wchar_t	k2 = ((const collchar_t *)n2)->wc;
426057ca2d4SBaptiste Daroussin 
427057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
428057ca2d4SBaptiste Daroussin }
429057ca2d4SBaptiste Daroussin 
4303a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collchars, collchar, entry, collchar_compare);
4313a444dbdSBaptiste Daroussin 
432057ca2d4SBaptiste Daroussin static int
433057ca2d4SBaptiste Daroussin subst_compare(const void *n1, const void *n2)
434057ca2d4SBaptiste Daroussin {
435057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const subst_t *)n1)->key;
436057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const subst_t *)n2)->key;
437057ca2d4SBaptiste Daroussin 
438057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
439057ca2d4SBaptiste Daroussin }
440057ca2d4SBaptiste Daroussin 
4413a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs, subst, entry, subst_compare);
4423a444dbdSBaptiste Daroussin 
443057ca2d4SBaptiste Daroussin static int
444057ca2d4SBaptiste Daroussin subst_compare_ref(const void *n1, const void *n2)
445057ca2d4SBaptiste Daroussin {
44655b270e6SBaptiste Daroussin 	const wchar_t *c1 = ((const subst_t *)n1)->ref;
44755b270e6SBaptiste Daroussin 	const wchar_t *c2 = ((const subst_t *)n2)->ref;
448057ca2d4SBaptiste Daroussin 	int rv;
449057ca2d4SBaptiste Daroussin 
45055b270e6SBaptiste Daroussin 	rv = wcscmp(c1, c2);
451057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
452057ca2d4SBaptiste Daroussin }
453057ca2d4SBaptiste Daroussin 
4543a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs_ref, subst, entry_ref, subst_compare_ref);
4553a444dbdSBaptiste Daroussin 
456057ca2d4SBaptiste Daroussin void
457057ca2d4SBaptiste Daroussin init_collate(void)
458057ca2d4SBaptiste Daroussin {
459057ca2d4SBaptiste Daroussin 	int i;
460057ca2d4SBaptiste Daroussin 
4613a444dbdSBaptiste Daroussin 	RB_INIT(&collsyms);
462057ca2d4SBaptiste Daroussin 
4633a444dbdSBaptiste Daroussin 	RB_INIT(&collundefs);
464057ca2d4SBaptiste Daroussin 
4653a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_symbol);
466057ca2d4SBaptiste Daroussin 
4673a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_expand);
4683a444dbdSBaptiste Daroussin 
4693a444dbdSBaptiste Daroussin 	RB_INIT(&collchars);
470057ca2d4SBaptiste Daroussin 
471057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
4723a444dbdSBaptiste Daroussin 		RB_INIT(&substs[i]);
4733a444dbdSBaptiste Daroussin 		RB_INIT(&substs_ref[i]);
4743a444dbdSBaptiste Daroussin 		RB_INIT(&weights[i]);
475057ca2d4SBaptiste Daroussin 		nweight[i] = 1;
476057ca2d4SBaptiste Daroussin 	}
477057ca2d4SBaptiste Daroussin 
478057ca2d4SBaptiste Daroussin 	(void) memset(&collinfo, 0, sizeof (collinfo));
479057ca2d4SBaptiste Daroussin 
480057ca2d4SBaptiste Daroussin 	/* allocate some initial priorities */
481057ca2d4SBaptiste Daroussin 	pri_ignore = new_pri();
482057ca2d4SBaptiste Daroussin 
483057ca2d4SBaptiste Daroussin 	set_pri(pri_ignore, 0, RESOLVED);
484057ca2d4SBaptiste Daroussin 
485057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
486057ca2d4SBaptiste Daroussin 		pri_undefined[i] = new_pri();
487057ca2d4SBaptiste Daroussin 
488057ca2d4SBaptiste Daroussin 		/* we will override this later */
489057ca2d4SBaptiste Daroussin 		set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN);
490057ca2d4SBaptiste Daroussin 	}
491057ca2d4SBaptiste Daroussin }
492057ca2d4SBaptiste Daroussin 
493057ca2d4SBaptiste Daroussin void
494057ca2d4SBaptiste Daroussin define_collsym(char *name)
495057ca2d4SBaptiste Daroussin {
496057ca2d4SBaptiste Daroussin 	collsym_t	*sym;
497057ca2d4SBaptiste Daroussin 
4984c22fda9SMarcelo Araujo 	if ((sym = calloc(1, sizeof(*sym))) == NULL) {
499057ca2d4SBaptiste Daroussin 		fprintf(stderr,"out of memory");
500057ca2d4SBaptiste Daroussin 		return;
501057ca2d4SBaptiste Daroussin 	}
502057ca2d4SBaptiste Daroussin 	sym->name = name;
503057ca2d4SBaptiste Daroussin 	sym->ref = new_pri();
504057ca2d4SBaptiste Daroussin 
5053a444dbdSBaptiste Daroussin 	if (RB_FIND(collsyms, &collsyms, sym) != NULL) {
506057ca2d4SBaptiste Daroussin 		/*
507057ca2d4SBaptiste Daroussin 		 * This should never happen because we are only called
508057ca2d4SBaptiste Daroussin 		 * for undefined symbols.
509057ca2d4SBaptiste Daroussin 		 */
510e6d8c0e2SBaptiste Daroussin 		free(sym);
511057ca2d4SBaptiste Daroussin 		INTERR;
512057ca2d4SBaptiste Daroussin 		return;
513057ca2d4SBaptiste Daroussin 	}
5143a444dbdSBaptiste Daroussin 	RB_INSERT(collsyms, &collsyms, sym);
515057ca2d4SBaptiste Daroussin }
516057ca2d4SBaptiste Daroussin 
517057ca2d4SBaptiste Daroussin collsym_t *
518057ca2d4SBaptiste Daroussin lookup_collsym(char *name)
519057ca2d4SBaptiste Daroussin {
520057ca2d4SBaptiste Daroussin 	collsym_t	srch;
521057ca2d4SBaptiste Daroussin 
522057ca2d4SBaptiste Daroussin 	srch.name = name;
5233a444dbdSBaptiste Daroussin 	return (RB_FIND(collsyms, &collsyms, &srch));
524057ca2d4SBaptiste Daroussin }
525057ca2d4SBaptiste Daroussin 
526057ca2d4SBaptiste Daroussin collelem_t *
527057ca2d4SBaptiste Daroussin lookup_collelem(char *symbol)
528057ca2d4SBaptiste Daroussin {
529057ca2d4SBaptiste Daroussin 	collelem_t	srch;
530057ca2d4SBaptiste Daroussin 
531057ca2d4SBaptiste Daroussin 	srch.symbol = symbol;
5323a444dbdSBaptiste Daroussin 	return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch));
533057ca2d4SBaptiste Daroussin }
534057ca2d4SBaptiste Daroussin 
535057ca2d4SBaptiste Daroussin static collundef_t *
536057ca2d4SBaptiste Daroussin get_collundef(char *name)
537057ca2d4SBaptiste Daroussin {
538057ca2d4SBaptiste Daroussin 	collundef_t	srch;
539057ca2d4SBaptiste Daroussin 	collundef_t	*ud;
540057ca2d4SBaptiste Daroussin 	int		i;
541057ca2d4SBaptiste Daroussin 
542057ca2d4SBaptiste Daroussin 	srch.name = name;
5433a444dbdSBaptiste Daroussin 	if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) {
5444c22fda9SMarcelo Araujo 		if (((ud = calloc(1, sizeof(*ud))) == NULL) ||
545057ca2d4SBaptiste Daroussin 		    ((ud->name = strdup(name)) == NULL)) {
546057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
547e6d8c0e2SBaptiste Daroussin 			free(ud);
548057ca2d4SBaptiste Daroussin 			return (NULL);
549057ca2d4SBaptiste Daroussin 		}
550057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
551057ca2d4SBaptiste Daroussin 			ud->ref[i] = new_pri();
552057ca2d4SBaptiste Daroussin 		}
5533a444dbdSBaptiste Daroussin 		RB_INSERT(collundefs, &collundefs, ud);
554057ca2d4SBaptiste Daroussin 	}
555057ca2d4SBaptiste Daroussin 	add_charmap_undefined(name);
556057ca2d4SBaptiste Daroussin 	return (ud);
557057ca2d4SBaptiste Daroussin }
558057ca2d4SBaptiste Daroussin 
559057ca2d4SBaptiste Daroussin static collchar_t *
560057ca2d4SBaptiste Daroussin get_collchar(wchar_t wc, int create)
561057ca2d4SBaptiste Daroussin {
562057ca2d4SBaptiste Daroussin 	collchar_t	srch;
563057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
564057ca2d4SBaptiste Daroussin 	int		i;
565057ca2d4SBaptiste Daroussin 
566057ca2d4SBaptiste Daroussin 	srch.wc = wc;
5673a444dbdSBaptiste Daroussin 	cc = RB_FIND(collchars, &collchars, &srch);
568057ca2d4SBaptiste Daroussin 	if ((cc == NULL) && create) {
5694c22fda9SMarcelo Araujo 		if ((cc = calloc(1, sizeof(*cc))) == NULL) {
570057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
571057ca2d4SBaptiste Daroussin 			return (NULL);
572057ca2d4SBaptiste Daroussin 		}
573057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
574057ca2d4SBaptiste Daroussin 			cc->ref[i] = new_pri();
575057ca2d4SBaptiste Daroussin 		}
576057ca2d4SBaptiste Daroussin 		cc->wc = wc;
5773a444dbdSBaptiste Daroussin 		RB_INSERT(collchars, &collchars, cc);
578057ca2d4SBaptiste Daroussin 	}
579057ca2d4SBaptiste Daroussin 	return (cc);
580057ca2d4SBaptiste Daroussin }
581057ca2d4SBaptiste Daroussin 
582057ca2d4SBaptiste Daroussin void
583057ca2d4SBaptiste Daroussin end_order_collsym(collsym_t *sym)
584057ca2d4SBaptiste Daroussin {
585057ca2d4SBaptiste Daroussin 	start_order(T_COLLSYM);
586057ca2d4SBaptiste Daroussin 	/* update the weight */
587057ca2d4SBaptiste Daroussin 
588057ca2d4SBaptiste Daroussin 	set_pri(sym->ref, nextpri, RESOLVED);
589057ca2d4SBaptiste Daroussin 	nextpri++;
590057ca2d4SBaptiste Daroussin }
591057ca2d4SBaptiste Daroussin 
592057ca2d4SBaptiste Daroussin void
593057ca2d4SBaptiste Daroussin end_order(void)
594057ca2d4SBaptiste Daroussin {
595057ca2d4SBaptiste Daroussin 	int		i;
596057ca2d4SBaptiste Daroussin 	int32_t		pri;
597057ca2d4SBaptiste Daroussin 	int32_t		ref;
598057ca2d4SBaptiste Daroussin 	collpri_t	*p;
599057ca2d4SBaptiste Daroussin 
600057ca2d4SBaptiste Daroussin 	/* advance the priority/weight */
601057ca2d4SBaptiste Daroussin 	pri = nextpri;
602057ca2d4SBaptiste Daroussin 
603057ca2d4SBaptiste Daroussin 	switch (currorder) {
604057ca2d4SBaptiste Daroussin 	case T_CHAR:
605057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
606057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
607057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
608057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
609057ca2d4SBaptiste Daroussin 				/* unspecified weight is a self reference */
610057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], pri, RESOLVED);
611057ca2d4SBaptiste Daroussin 			} else {
612057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], ref, REFER);
613057ca2d4SBaptiste Daroussin 			}
614057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
615057ca2d4SBaptiste Daroussin 		}
616057ca2d4SBaptiste Daroussin 
617057ca2d4SBaptiste Daroussin 		/* leave a cookie trail in case next symbol is ellipsis */
618057ca2d4SBaptiste Daroussin 		ellipsis_start = currchar->wc + 1;
619057ca2d4SBaptiste Daroussin 		currchar = NULL;
620057ca2d4SBaptiste Daroussin 		break;
621057ca2d4SBaptiste Daroussin 
622057ca2d4SBaptiste Daroussin 	case T_ELLIPSIS:
623057ca2d4SBaptiste Daroussin 		/* save off the weights were we can find them */
624057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
625057ca2d4SBaptiste Daroussin 			ellipsis_weights[i] = order_weights[i];
626057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
627057ca2d4SBaptiste Daroussin 		}
628057ca2d4SBaptiste Daroussin 		break;
629057ca2d4SBaptiste Daroussin 
630057ca2d4SBaptiste Daroussin 	case T_COLLELEM:
631057ca2d4SBaptiste Daroussin 		if (currelem == NULL) {
632057ca2d4SBaptiste Daroussin 			INTERR;
633057ca2d4SBaptiste Daroussin 		} else {
634057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
635057ca2d4SBaptiste Daroussin 
636057ca2d4SBaptiste Daroussin 				if (((ref = order_weights[i]) < 0) ||
637057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
638057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
639057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], pri,
640057ca2d4SBaptiste Daroussin 					    RESOLVED);
641057ca2d4SBaptiste Daroussin 				} else {
642057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], ref, REFER);
643057ca2d4SBaptiste Daroussin 				}
644057ca2d4SBaptiste Daroussin 				order_weights[i] = -1;
645057ca2d4SBaptiste Daroussin 			}
646057ca2d4SBaptiste Daroussin 		}
647057ca2d4SBaptiste Daroussin 		break;
648057ca2d4SBaptiste Daroussin 
649057ca2d4SBaptiste Daroussin 	case T_UNDEFINED:
650057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
651057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
652057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
653057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
654057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], -1, RESOLVED);
655057ca2d4SBaptiste Daroussin 			} else {
656057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], ref, REFER);
657057ca2d4SBaptiste Daroussin 			}
658057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
659057ca2d4SBaptiste Daroussin 		}
660057ca2d4SBaptiste Daroussin 		break;
661057ca2d4SBaptiste Daroussin 
662057ca2d4SBaptiste Daroussin 	case T_SYMBOL:
663057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
664057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
665057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
666057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
667057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], pri, RESOLVED);
668057ca2d4SBaptiste Daroussin 			} else {
669057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], ref, REFER);
670057ca2d4SBaptiste Daroussin 			}
671057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
672057ca2d4SBaptiste Daroussin 		}
673057ca2d4SBaptiste Daroussin 		break;
674057ca2d4SBaptiste Daroussin 
675057ca2d4SBaptiste Daroussin 	default:
676057ca2d4SBaptiste Daroussin 		INTERR;
677057ca2d4SBaptiste Daroussin 	}
678057ca2d4SBaptiste Daroussin 
679057ca2d4SBaptiste Daroussin 	nextpri++;
680057ca2d4SBaptiste Daroussin }
681057ca2d4SBaptiste Daroussin 
682057ca2d4SBaptiste Daroussin static void
683057ca2d4SBaptiste Daroussin start_order(int type)
684057ca2d4SBaptiste Daroussin {
685057ca2d4SBaptiste Daroussin 	int	i;
686057ca2d4SBaptiste Daroussin 
687057ca2d4SBaptiste Daroussin 	lastorder = currorder;
688057ca2d4SBaptiste Daroussin 	currorder = type;
689057ca2d4SBaptiste Daroussin 
690057ca2d4SBaptiste Daroussin 	/* this is used to protect ELLIPSIS processing */
691057ca2d4SBaptiste Daroussin 	if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) {
692057ca2d4SBaptiste Daroussin 		fprintf(stderr, "character value expected");
693057ca2d4SBaptiste Daroussin 	}
694057ca2d4SBaptiste Daroussin 
695057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
696057ca2d4SBaptiste Daroussin 		order_weights[i] = -1;
697057ca2d4SBaptiste Daroussin 	}
698057ca2d4SBaptiste Daroussin 	curr_weight = 0;
699057ca2d4SBaptiste Daroussin }
700057ca2d4SBaptiste Daroussin 
701057ca2d4SBaptiste Daroussin void
702057ca2d4SBaptiste Daroussin start_order_undefined(void)
703057ca2d4SBaptiste Daroussin {
704057ca2d4SBaptiste Daroussin 	start_order(T_UNDEFINED);
705057ca2d4SBaptiste Daroussin }
706057ca2d4SBaptiste Daroussin 
707057ca2d4SBaptiste Daroussin void
708057ca2d4SBaptiste Daroussin start_order_symbol(char *name)
709057ca2d4SBaptiste Daroussin {
710057ca2d4SBaptiste Daroussin 	currundef = get_collundef(name);
711057ca2d4SBaptiste Daroussin 	start_order(T_SYMBOL);
712057ca2d4SBaptiste Daroussin }
713057ca2d4SBaptiste Daroussin 
714057ca2d4SBaptiste Daroussin void
715057ca2d4SBaptiste Daroussin start_order_char(wchar_t wc)
716057ca2d4SBaptiste Daroussin {
717057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
718057ca2d4SBaptiste Daroussin 	int32_t		ref;
719057ca2d4SBaptiste Daroussin 
720057ca2d4SBaptiste Daroussin 	start_order(T_CHAR);
721057ca2d4SBaptiste Daroussin 
722057ca2d4SBaptiste Daroussin 	/*
723057ca2d4SBaptiste Daroussin 	 * If we last saw an ellipsis, then we need to close the range.
724057ca2d4SBaptiste Daroussin 	 * Handle that here.  Note that we have to be careful because the
725057ca2d4SBaptiste Daroussin 	 * items *inside* the range are treated exclusiveley to the items
726057ca2d4SBaptiste Daroussin 	 * outside of the range.  The ends of the range can have quite
727057ca2d4SBaptiste Daroussin 	 * different weights than the range members.
728057ca2d4SBaptiste Daroussin 	 */
729057ca2d4SBaptiste Daroussin 	if (lastorder == T_ELLIPSIS) {
730057ca2d4SBaptiste Daroussin 		int		i;
731057ca2d4SBaptiste Daroussin 
732057ca2d4SBaptiste Daroussin 		if (wc < ellipsis_start) {
733057ca2d4SBaptiste Daroussin 			fprintf(stderr, "malformed range!");
734057ca2d4SBaptiste Daroussin 			return;
735057ca2d4SBaptiste Daroussin 		}
736057ca2d4SBaptiste Daroussin 		while (ellipsis_start < wc) {
737057ca2d4SBaptiste Daroussin 			/*
738057ca2d4SBaptiste Daroussin 			 * pick all of the saved weights for the
739057ca2d4SBaptiste Daroussin 			 * ellipsis.  note that -1 encodes for the
740057ca2d4SBaptiste Daroussin 			 * ellipsis itself, which means to take the
741057ca2d4SBaptiste Daroussin 			 * current relative priority.
742057ca2d4SBaptiste Daroussin 			 */
743057ca2d4SBaptiste Daroussin 			if ((cc = get_collchar(ellipsis_start, 1)) == NULL) {
744057ca2d4SBaptiste Daroussin 				INTERR;
745057ca2d4SBaptiste Daroussin 				return;
746057ca2d4SBaptiste Daroussin 			}
747057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
748057ca2d4SBaptiste Daroussin 				collpri_t *p;
749057ca2d4SBaptiste Daroussin 				if (((ref = ellipsis_weights[i]) == -1) ||
750057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
751057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
752057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], nextpri, RESOLVED);
753057ca2d4SBaptiste Daroussin 				} else {
754057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], ref, REFER);
755057ca2d4SBaptiste Daroussin 				}
756057ca2d4SBaptiste Daroussin 				ellipsis_weights[i] = 0;
757057ca2d4SBaptiste Daroussin 			}
758057ca2d4SBaptiste Daroussin 			ellipsis_start++;
759057ca2d4SBaptiste Daroussin 			nextpri++;
760057ca2d4SBaptiste Daroussin 		}
761057ca2d4SBaptiste Daroussin 	}
762057ca2d4SBaptiste Daroussin 
763057ca2d4SBaptiste Daroussin 	currchar = get_collchar(wc, 1);
764057ca2d4SBaptiste Daroussin }
765057ca2d4SBaptiste Daroussin 
766057ca2d4SBaptiste Daroussin void
767057ca2d4SBaptiste Daroussin start_order_collelem(collelem_t *e)
768057ca2d4SBaptiste Daroussin {
769057ca2d4SBaptiste Daroussin 	start_order(T_COLLELEM);
770057ca2d4SBaptiste Daroussin 	currelem = e;
771057ca2d4SBaptiste Daroussin }
772057ca2d4SBaptiste Daroussin 
773057ca2d4SBaptiste Daroussin void
774057ca2d4SBaptiste Daroussin start_order_ellipsis(void)
775057ca2d4SBaptiste Daroussin {
776057ca2d4SBaptiste Daroussin 	int	i;
777057ca2d4SBaptiste Daroussin 
778057ca2d4SBaptiste Daroussin 	start_order(T_ELLIPSIS);
779057ca2d4SBaptiste Daroussin 
780057ca2d4SBaptiste Daroussin 	if (lastorder != T_CHAR) {
781057ca2d4SBaptiste Daroussin 		fprintf(stderr, "illegal starting point for range");
782057ca2d4SBaptiste Daroussin 		return;
783057ca2d4SBaptiste Daroussin 	}
784057ca2d4SBaptiste Daroussin 
785057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
786057ca2d4SBaptiste Daroussin 		ellipsis_weights[i] = order_weights[i];
787057ca2d4SBaptiste Daroussin 	}
788057ca2d4SBaptiste Daroussin }
789057ca2d4SBaptiste Daroussin 
790057ca2d4SBaptiste Daroussin void
791057ca2d4SBaptiste Daroussin define_collelem(char *name, wchar_t *wcs)
792057ca2d4SBaptiste Daroussin {
793057ca2d4SBaptiste Daroussin 	collelem_t	*e;
794057ca2d4SBaptiste Daroussin 	int		i;
795057ca2d4SBaptiste Daroussin 
796057ca2d4SBaptiste Daroussin 	if (wcslen(wcs) >= COLLATE_STR_LEN) {
797057ca2d4SBaptiste Daroussin 		fprintf(stderr,"expanded collation element too long");
798057ca2d4SBaptiste Daroussin 		return;
799057ca2d4SBaptiste Daroussin 	}
800057ca2d4SBaptiste Daroussin 
8014c22fda9SMarcelo Araujo 	if ((e = calloc(1, sizeof(*e))) == NULL) {
802057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
803057ca2d4SBaptiste Daroussin 		return;
804057ca2d4SBaptiste Daroussin 	}
805057ca2d4SBaptiste Daroussin 	e->expand = wcs;
806057ca2d4SBaptiste Daroussin 	e->symbol = name;
807057ca2d4SBaptiste Daroussin 
808057ca2d4SBaptiste Daroussin 	/*
809057ca2d4SBaptiste Daroussin 	 * This is executed before the order statement, so we don't
810057ca2d4SBaptiste Daroussin 	 * know how many priorities we *really* need.  We allocate one
811057ca2d4SBaptiste Daroussin 	 * for each possible weight.  Not a big deal, as collating-elements
812057ca2d4SBaptiste Daroussin 	 * prove to be quite rare.
813057ca2d4SBaptiste Daroussin 	 */
814057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
815057ca2d4SBaptiste Daroussin 		e->ref[i] = new_pri();
816057ca2d4SBaptiste Daroussin 	}
817057ca2d4SBaptiste Daroussin 
818057ca2d4SBaptiste Daroussin 	/* A character sequence can only reduce to one element. */
8193a444dbdSBaptiste Daroussin 	if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) ||
8203a444dbdSBaptiste Daroussin 	    (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) {
821057ca2d4SBaptiste Daroussin 		fprintf(stderr, "duplicate collating element definition");
822e6d8c0e2SBaptiste Daroussin 		free(e);
823057ca2d4SBaptiste Daroussin 		return;
824057ca2d4SBaptiste Daroussin 	}
8253a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_symbol, &elem_by_symbol, e);
8263a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_expand, &elem_by_expand, e);
827057ca2d4SBaptiste Daroussin }
828057ca2d4SBaptiste Daroussin 
829057ca2d4SBaptiste Daroussin void
830057ca2d4SBaptiste Daroussin add_order_bit(int kw)
831057ca2d4SBaptiste Daroussin {
832057ca2d4SBaptiste Daroussin 	uint8_t bit = DIRECTIVE_UNDEF;
833057ca2d4SBaptiste Daroussin 
834057ca2d4SBaptiste Daroussin 	switch (kw) {
835057ca2d4SBaptiste Daroussin 	case T_FORWARD:
836057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_FORWARD;
837057ca2d4SBaptiste Daroussin 		break;
838057ca2d4SBaptiste Daroussin 	case T_BACKWARD:
839057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_BACKWARD;
840057ca2d4SBaptiste Daroussin 		break;
841057ca2d4SBaptiste Daroussin 	case T_POSITION:
842057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_POSITION;
843057ca2d4SBaptiste Daroussin 		break;
844057ca2d4SBaptiste Daroussin 	default:
845057ca2d4SBaptiste Daroussin 		INTERR;
846057ca2d4SBaptiste Daroussin 		break;
847057ca2d4SBaptiste Daroussin 	}
848057ca2d4SBaptiste Daroussin 	collinfo.directive[collinfo.directive_count] |= bit;
849057ca2d4SBaptiste Daroussin }
850057ca2d4SBaptiste Daroussin 
851057ca2d4SBaptiste Daroussin void
852057ca2d4SBaptiste Daroussin add_order_directive(void)
853057ca2d4SBaptiste Daroussin {
854057ca2d4SBaptiste Daroussin 	if (collinfo.directive_count >= COLL_WEIGHTS_MAX) {
8552becc9efSAlex Richardson 		fprintf(stderr, "too many directives (max %d)\n", COLL_WEIGHTS_MAX);
8562becc9efSAlex Richardson 		return;
857057ca2d4SBaptiste Daroussin 	}
858057ca2d4SBaptiste Daroussin 	collinfo.directive_count++;
859057ca2d4SBaptiste Daroussin }
860057ca2d4SBaptiste Daroussin 
861057ca2d4SBaptiste Daroussin static void
862057ca2d4SBaptiste Daroussin add_order_pri(int32_t ref)
863057ca2d4SBaptiste Daroussin {
864057ca2d4SBaptiste Daroussin 	if (curr_weight >= NUM_WT) {
8652becc9efSAlex Richardson 		fprintf(stderr, "too many weights (max %d)\n", NUM_WT);
866057ca2d4SBaptiste Daroussin 		return;
867057ca2d4SBaptiste Daroussin 	}
868057ca2d4SBaptiste Daroussin 	order_weights[curr_weight] = ref;
869057ca2d4SBaptiste Daroussin 	curr_weight++;
870057ca2d4SBaptiste Daroussin }
871057ca2d4SBaptiste Daroussin 
872057ca2d4SBaptiste Daroussin void
873057ca2d4SBaptiste Daroussin add_order_collsym(collsym_t *s)
874057ca2d4SBaptiste Daroussin {
875057ca2d4SBaptiste Daroussin 	add_order_pri(s->ref);
876057ca2d4SBaptiste Daroussin }
877057ca2d4SBaptiste Daroussin 
878057ca2d4SBaptiste Daroussin void
879057ca2d4SBaptiste Daroussin add_order_char(wchar_t wc)
880057ca2d4SBaptiste Daroussin {
881057ca2d4SBaptiste Daroussin 	collchar_t *cc;
882057ca2d4SBaptiste Daroussin 
883057ca2d4SBaptiste Daroussin 	if ((cc = get_collchar(wc, 1)) == NULL) {
884057ca2d4SBaptiste Daroussin 		INTERR;
885057ca2d4SBaptiste Daroussin 		return;
886057ca2d4SBaptiste Daroussin 	}
887057ca2d4SBaptiste Daroussin 
888057ca2d4SBaptiste Daroussin 	add_order_pri(cc->ref[curr_weight]);
889057ca2d4SBaptiste Daroussin }
890057ca2d4SBaptiste Daroussin 
891057ca2d4SBaptiste Daroussin void
892057ca2d4SBaptiste Daroussin add_order_collelem(collelem_t *e)
893057ca2d4SBaptiste Daroussin {
894057ca2d4SBaptiste Daroussin 	add_order_pri(e->ref[curr_weight]);
895057ca2d4SBaptiste Daroussin }
896057ca2d4SBaptiste Daroussin 
897057ca2d4SBaptiste Daroussin void
898057ca2d4SBaptiste Daroussin add_order_ignore(void)
899057ca2d4SBaptiste Daroussin {
900057ca2d4SBaptiste Daroussin 	add_order_pri(pri_ignore);
901057ca2d4SBaptiste Daroussin }
902057ca2d4SBaptiste Daroussin 
903057ca2d4SBaptiste Daroussin void
904057ca2d4SBaptiste Daroussin add_order_symbol(char *sym)
905057ca2d4SBaptiste Daroussin {
906057ca2d4SBaptiste Daroussin 	collundef_t *c;
907057ca2d4SBaptiste Daroussin 	if ((c = get_collundef(sym)) == NULL) {
908057ca2d4SBaptiste Daroussin 		INTERR;
909057ca2d4SBaptiste Daroussin 		return;
910057ca2d4SBaptiste Daroussin 	}
911057ca2d4SBaptiste Daroussin 	add_order_pri(c->ref[curr_weight]);
912057ca2d4SBaptiste Daroussin }
913057ca2d4SBaptiste Daroussin 
914057ca2d4SBaptiste Daroussin void
915057ca2d4SBaptiste Daroussin add_order_ellipsis(void)
916057ca2d4SBaptiste Daroussin {
917057ca2d4SBaptiste Daroussin 	/* special NULL value indicates self reference */
918057ca2d4SBaptiste Daroussin 	add_order_pri(0);
919057ca2d4SBaptiste Daroussin }
920057ca2d4SBaptiste Daroussin 
921057ca2d4SBaptiste Daroussin void
922057ca2d4SBaptiste Daroussin add_order_subst(void)
923057ca2d4SBaptiste Daroussin {
924057ca2d4SBaptiste Daroussin 	subst_t srch;
925057ca2d4SBaptiste Daroussin 	subst_t	*s;
926057ca2d4SBaptiste Daroussin 	int i;
927057ca2d4SBaptiste Daroussin 
928057ca2d4SBaptiste Daroussin 	(void) memset(&srch, 0, sizeof (srch));
929057ca2d4SBaptiste Daroussin 	for (i = 0; i < curr_subst; i++) {
930057ca2d4SBaptiste Daroussin 		srch.ref[i] = subst_weights[i];
931057ca2d4SBaptiste Daroussin 		subst_weights[i] = 0;
932057ca2d4SBaptiste Daroussin 	}
9333a444dbdSBaptiste Daroussin 	s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch);
934057ca2d4SBaptiste Daroussin 
935057ca2d4SBaptiste Daroussin 	if (s == NULL) {
9364c22fda9SMarcelo Araujo 		if ((s = calloc(1, sizeof(*s))) == NULL) {
937057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
938057ca2d4SBaptiste Daroussin 			return;
939057ca2d4SBaptiste Daroussin 		}
940057ca2d4SBaptiste Daroussin 		s->key = new_pri();
941057ca2d4SBaptiste Daroussin 
942057ca2d4SBaptiste Daroussin 		/*
943057ca2d4SBaptiste Daroussin 		 * We use a self reference for our key, but we set a
944057ca2d4SBaptiste Daroussin 		 * high bit to indicate that this is a substitution
945057ca2d4SBaptiste Daroussin 		 * reference.  This will expedite table lookups later,
946057ca2d4SBaptiste Daroussin 		 * and prevent table lookups for situations that don't
947057ca2d4SBaptiste Daroussin 		 * require it.  (In short, its a big win, because we
948057ca2d4SBaptiste Daroussin 		 * can skip a lot of binary searching.)
949057ca2d4SBaptiste Daroussin 		 */
950057ca2d4SBaptiste Daroussin 		set_pri(s->key,
951057ca2d4SBaptiste Daroussin 		    (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY),
952057ca2d4SBaptiste Daroussin 		    RESOLVED);
953057ca2d4SBaptiste Daroussin 		nextsubst[curr_weight] += 1;
954057ca2d4SBaptiste Daroussin 
955057ca2d4SBaptiste Daroussin 		for (i = 0; i < curr_subst; i++) {
956057ca2d4SBaptiste Daroussin 			s->ref[i] = srch.ref[i];
957057ca2d4SBaptiste Daroussin 		}
958057ca2d4SBaptiste Daroussin 
9593a444dbdSBaptiste Daroussin 		RB_INSERT(substs_ref, &substs_ref[curr_weight], s);
960057ca2d4SBaptiste Daroussin 
9613a444dbdSBaptiste Daroussin 		if (RB_FIND(substs, &substs[curr_weight], s) != NULL) {
962057ca2d4SBaptiste Daroussin 			INTERR;
963057ca2d4SBaptiste Daroussin 			return;
964057ca2d4SBaptiste Daroussin 		}
9653a444dbdSBaptiste Daroussin 		RB_INSERT(substs, &substs[curr_weight], s);
966057ca2d4SBaptiste Daroussin 	}
967057ca2d4SBaptiste Daroussin 	curr_subst = 0;
968057ca2d4SBaptiste Daroussin 
969057ca2d4SBaptiste Daroussin 
970057ca2d4SBaptiste Daroussin 	/*
971057ca2d4SBaptiste Daroussin 	 * We are using the current (unique) priority as a search key
972057ca2d4SBaptiste Daroussin 	 * in the substitution table.
973057ca2d4SBaptiste Daroussin 	 */
974057ca2d4SBaptiste Daroussin 	add_order_pri(s->key);
975057ca2d4SBaptiste Daroussin }
976057ca2d4SBaptiste Daroussin 
977057ca2d4SBaptiste Daroussin static void
978057ca2d4SBaptiste Daroussin add_subst_pri(int32_t ref)
979057ca2d4SBaptiste Daroussin {
980057ca2d4SBaptiste Daroussin 	if (curr_subst >= COLLATE_STR_LEN) {
981057ca2d4SBaptiste Daroussin 		fprintf(stderr,"substitution string is too long");
982057ca2d4SBaptiste Daroussin 		return;
983057ca2d4SBaptiste Daroussin 	}
984057ca2d4SBaptiste Daroussin 	subst_weights[curr_subst] = ref;
985057ca2d4SBaptiste Daroussin 	curr_subst++;
986057ca2d4SBaptiste Daroussin }
987057ca2d4SBaptiste Daroussin 
988057ca2d4SBaptiste Daroussin void
989057ca2d4SBaptiste Daroussin add_subst_char(wchar_t wc)
990057ca2d4SBaptiste Daroussin {
991057ca2d4SBaptiste Daroussin 	collchar_t *cc;
992057ca2d4SBaptiste Daroussin 
993057ca2d4SBaptiste Daroussin 
994057ca2d4SBaptiste Daroussin 	if (((cc = get_collchar(wc, 1)) == NULL) ||
995057ca2d4SBaptiste Daroussin 	    (cc->wc != wc)) {
996057ca2d4SBaptiste Daroussin 		INTERR;
997057ca2d4SBaptiste Daroussin 		return;
998057ca2d4SBaptiste Daroussin 	}
999057ca2d4SBaptiste Daroussin 	/* we take the weight for the character at that position */
1000057ca2d4SBaptiste Daroussin 	add_subst_pri(cc->ref[curr_weight]);
1001057ca2d4SBaptiste Daroussin }
1002057ca2d4SBaptiste Daroussin 
1003057ca2d4SBaptiste Daroussin void
1004057ca2d4SBaptiste Daroussin add_subst_collelem(collelem_t *e)
1005057ca2d4SBaptiste Daroussin {
1006057ca2d4SBaptiste Daroussin 	add_subst_pri(e->ref[curr_weight]);
1007057ca2d4SBaptiste Daroussin }
1008057ca2d4SBaptiste Daroussin 
1009057ca2d4SBaptiste Daroussin void
1010057ca2d4SBaptiste Daroussin add_subst_collsym(collsym_t *s)
1011057ca2d4SBaptiste Daroussin {
1012057ca2d4SBaptiste Daroussin 	add_subst_pri(s->ref);
1013057ca2d4SBaptiste Daroussin }
1014057ca2d4SBaptiste Daroussin 
1015057ca2d4SBaptiste Daroussin void
1016057ca2d4SBaptiste Daroussin add_subst_symbol(char *ptr)
1017057ca2d4SBaptiste Daroussin {
1018057ca2d4SBaptiste Daroussin 	collundef_t *cu;
1019057ca2d4SBaptiste Daroussin 
1020057ca2d4SBaptiste Daroussin 	if ((cu = get_collundef(ptr)) != NULL) {
1021057ca2d4SBaptiste Daroussin 		add_subst_pri(cu->ref[curr_weight]);
1022057ca2d4SBaptiste Daroussin 	}
1023057ca2d4SBaptiste Daroussin }
1024057ca2d4SBaptiste Daroussin 
1025057ca2d4SBaptiste Daroussin void
1026057ca2d4SBaptiste Daroussin add_weight(int32_t ref, int pass)
1027057ca2d4SBaptiste Daroussin {
1028057ca2d4SBaptiste Daroussin 	weight_t srch;
1029057ca2d4SBaptiste Daroussin 	weight_t *w;
1030057ca2d4SBaptiste Daroussin 
1031057ca2d4SBaptiste Daroussin 	srch.pri = resolve_pri(ref);
1032057ca2d4SBaptiste Daroussin 
1033057ca2d4SBaptiste Daroussin 	/* No translation of ignores */
1034057ca2d4SBaptiste Daroussin 	if (srch.pri == 0)
1035057ca2d4SBaptiste Daroussin 		return;
1036057ca2d4SBaptiste Daroussin 
1037057ca2d4SBaptiste Daroussin 	/* Substitution priorities are not weights */
1038057ca2d4SBaptiste Daroussin 	if (srch.pri & COLLATE_SUBST_PRIORITY)
1039057ca2d4SBaptiste Daroussin 		return;
1040057ca2d4SBaptiste Daroussin 
10413a444dbdSBaptiste Daroussin 	if (RB_FIND(weights, &weights[pass], &srch) != NULL)
1042057ca2d4SBaptiste Daroussin 		return;
1043057ca2d4SBaptiste Daroussin 
10444c22fda9SMarcelo Araujo 	if ((w = calloc(1, sizeof(*w))) == NULL) {
1045057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1046057ca2d4SBaptiste Daroussin 		return;
1047057ca2d4SBaptiste Daroussin 	}
1048057ca2d4SBaptiste Daroussin 	w->pri = srch.pri;
10493a444dbdSBaptiste Daroussin 	RB_INSERT(weights, &weights[pass], w);
1050057ca2d4SBaptiste Daroussin }
1051057ca2d4SBaptiste Daroussin 
1052057ca2d4SBaptiste Daroussin void
1053057ca2d4SBaptiste Daroussin add_weights(int32_t *refs)
1054057ca2d4SBaptiste Daroussin {
1055057ca2d4SBaptiste Daroussin 	int i;
1056057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1057057ca2d4SBaptiste Daroussin 		add_weight(refs[i], i);
1058057ca2d4SBaptiste Daroussin 	}
1059057ca2d4SBaptiste Daroussin }
1060057ca2d4SBaptiste Daroussin 
1061057ca2d4SBaptiste Daroussin int32_t
1062057ca2d4SBaptiste Daroussin get_weight(int32_t ref, int pass)
1063057ca2d4SBaptiste Daroussin {
1064057ca2d4SBaptiste Daroussin 	weight_t	srch;
1065057ca2d4SBaptiste Daroussin 	weight_t	*w;
1066057ca2d4SBaptiste Daroussin 	int32_t		pri;
1067057ca2d4SBaptiste Daroussin 
1068057ca2d4SBaptiste Daroussin 	pri = resolve_pri(ref);
1069057ca2d4SBaptiste Daroussin 	if (pri & COLLATE_SUBST_PRIORITY) {
1070057ca2d4SBaptiste Daroussin 		return (pri);
1071057ca2d4SBaptiste Daroussin 	}
1072057ca2d4SBaptiste Daroussin 	if (pri <= 0) {
1073057ca2d4SBaptiste Daroussin 		return (pri);
1074057ca2d4SBaptiste Daroussin 	}
1075057ca2d4SBaptiste Daroussin 	srch.pri = pri;
10763a444dbdSBaptiste Daroussin 	if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) {
1077057ca2d4SBaptiste Daroussin 		INTERR;
1078057ca2d4SBaptiste Daroussin 		return (-1);
1079057ca2d4SBaptiste Daroussin 	}
1080057ca2d4SBaptiste Daroussin 	return (w->opt);
1081057ca2d4SBaptiste Daroussin }
1082057ca2d4SBaptiste Daroussin 
1083057ca2d4SBaptiste Daroussin wchar_t *
1084057ca2d4SBaptiste Daroussin wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
1085057ca2d4SBaptiste Daroussin {
1086057ca2d4SBaptiste Daroussin 	wchar_t *os1 = s1;
1087057ca2d4SBaptiste Daroussin 
1088057ca2d4SBaptiste Daroussin 	n++;
10894644f9beSYuri Pankov 	while (--n > 0 && (*s1++ = htote(*s2++)) != 0)
1090057ca2d4SBaptiste Daroussin 		continue;
1091057ca2d4SBaptiste Daroussin 	if (n > 0)
1092057ca2d4SBaptiste Daroussin 		while (--n > 0)
1093057ca2d4SBaptiste Daroussin 			*s1++ = 0;
1094057ca2d4SBaptiste Daroussin 	return (os1);
1095057ca2d4SBaptiste Daroussin }
1096057ca2d4SBaptiste Daroussin 
109723a32822SBaptiste Daroussin #define RB_COUNT(x, name, head, cnt) do { \
109823a32822SBaptiste Daroussin 	(cnt) = 0; \
109923a32822SBaptiste Daroussin 	RB_FOREACH(x, name, (head)) { \
110023a32822SBaptiste Daroussin 		(cnt)++; \
110123a32822SBaptiste Daroussin 	} \
110223a32822SBaptiste Daroussin } while (0)
110323a32822SBaptiste Daroussin 
11043a444dbdSBaptiste Daroussin #define RB_NUMNODES(type, name, head, cnt) do { \
11053a444dbdSBaptiste Daroussin 	type *t; \
11063a444dbdSBaptiste Daroussin 	cnt = 0; \
11073a444dbdSBaptiste Daroussin 	RB_FOREACH(t, name, head) { \
11083a444dbdSBaptiste Daroussin 		cnt++; \
11093a444dbdSBaptiste Daroussin 	} \
111023a32822SBaptiste Daroussin } while (0)
11113a444dbdSBaptiste Daroussin 
1112057ca2d4SBaptiste Daroussin void
1113057ca2d4SBaptiste Daroussin dump_collate(void)
1114057ca2d4SBaptiste Daroussin {
1115057ca2d4SBaptiste Daroussin 	FILE			*f;
1116057ca2d4SBaptiste Daroussin 	int			i, j, n;
1117057ca2d4SBaptiste Daroussin 	size_t			sz;
1118057ca2d4SBaptiste Daroussin 	int32_t			pri;
1119057ca2d4SBaptiste Daroussin 	collelem_t		*ce;
1120057ca2d4SBaptiste Daroussin 	collchar_t		*cc;
1121057ca2d4SBaptiste Daroussin 	subst_t			*sb;
1122*cc7edd25SThomas Munro 	char			fmt_version[COLLATE_FMT_VERSION_LEN];
1123*cc7edd25SThomas Munro 	char			def_version[XLOCALE_DEF_VERSION_LEN];
1124057ca2d4SBaptiste Daroussin 	collate_char_t		chars[UCHAR_MAX + 1];
1125057ca2d4SBaptiste Daroussin 	collate_large_t		*large;
1126057ca2d4SBaptiste Daroussin 	collate_subst_t		*subst[COLL_WEIGHTS_MAX];
1127057ca2d4SBaptiste Daroussin 	collate_chain_t		*chain;
1128057ca2d4SBaptiste Daroussin 
1129057ca2d4SBaptiste Daroussin 	/*
1130fcc7baa1SPedro F. Giffuni 	 * We have to run through a preliminary pass to identify all the
1131057ca2d4SBaptiste Daroussin 	 * weights that we use for each sorting level.
1132057ca2d4SBaptiste Daroussin 	 */
1133057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1134057ca2d4SBaptiste Daroussin 		add_weight(pri_ignore, i);
1135057ca2d4SBaptiste Daroussin 	}
1136057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
11373a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1138057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1139057ca2d4SBaptiste Daroussin 				add_weight(sb->ref[j], i);
1140057ca2d4SBaptiste Daroussin 			}
1141057ca2d4SBaptiste Daroussin 		}
1142057ca2d4SBaptiste Daroussin 	}
11433a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1144057ca2d4SBaptiste Daroussin 		add_weights(ce->ref);
1145057ca2d4SBaptiste Daroussin 	}
11463a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1147057ca2d4SBaptiste Daroussin 		add_weights(cc->ref);
1148057ca2d4SBaptiste Daroussin 	}
1149057ca2d4SBaptiste Daroussin 
1150057ca2d4SBaptiste Daroussin 	/*
1151057ca2d4SBaptiste Daroussin 	 * Now we walk the entire set of weights, removing the gaps
1152057ca2d4SBaptiste Daroussin 	 * in the weights.  This gives us optimum usage.  The walk
1153057ca2d4SBaptiste Daroussin 	 * occurs in priority.
1154057ca2d4SBaptiste Daroussin 	 */
1155057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1156057ca2d4SBaptiste Daroussin 		weight_t *w;
11573a444dbdSBaptiste Daroussin 		RB_FOREACH(w, weights, &weights[i]) {
1158057ca2d4SBaptiste Daroussin 			w->opt = nweight[i];
1159057ca2d4SBaptiste Daroussin 			nweight[i] += 1;
1160057ca2d4SBaptiste Daroussin 		}
1161057ca2d4SBaptiste Daroussin 	}
1162057ca2d4SBaptiste Daroussin 
1163057ca2d4SBaptiste Daroussin 	(void) memset(&chars, 0, sizeof (chars));
1164*cc7edd25SThomas Munro 	(void) memset(fmt_version, 0, COLLATE_FMT_VERSION_LEN);
1165*cc7edd25SThomas Munro 	(void) strlcpy(fmt_version, COLLATE_FMT_VERSION, sizeof (fmt_version));
1166*cc7edd25SThomas Munro 	(void) memset(def_version, 0, XLOCALE_DEF_VERSION_LEN);
1167*cc7edd25SThomas Munro 	if (version)
1168*cc7edd25SThomas Munro 		(void) strlcpy(def_version, version, sizeof (def_version));
1169057ca2d4SBaptiste Daroussin 
1170057ca2d4SBaptiste Daroussin 	/*
1171057ca2d4SBaptiste Daroussin 	 * We need to make sure we arrange for the UNDEFINED field
1172057ca2d4SBaptiste Daroussin 	 * to show up.  Also, set the total weight counts.
1173057ca2d4SBaptiste Daroussin 	 */
1174057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1175057ca2d4SBaptiste Daroussin 		if (resolve_pri(pri_undefined[i]) == -1) {
1176057ca2d4SBaptiste Daroussin 			set_pri(pri_undefined[i], -1, RESOLVED);
1177057ca2d4SBaptiste Daroussin 			/* they collate at the end of everything else */
11784644f9beSYuri Pankov 			collinfo.undef_pri[i] = htote(COLLATE_MAX_PRIORITY);
1179057ca2d4SBaptiste Daroussin 		}
11804644f9beSYuri Pankov 		collinfo.pri_count[i] = htote(nweight[i]);
1181057ca2d4SBaptiste Daroussin 	}
1182057ca2d4SBaptiste Daroussin 
11834644f9beSYuri Pankov 	collinfo.pri_count[NUM_WT] = htote(max_wide());
11844644f9beSYuri Pankov 	collinfo.undef_pri[NUM_WT] = htote(COLLATE_MAX_PRIORITY);
1185057ca2d4SBaptiste Daroussin 	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
1186057ca2d4SBaptiste Daroussin 
1187057ca2d4SBaptiste Daroussin 	/*
1188057ca2d4SBaptiste Daroussin 	 * Ordinary character priorities
1189057ca2d4SBaptiste Daroussin 	 */
1190057ca2d4SBaptiste Daroussin 	for (i = 0; i <= UCHAR_MAX; i++) {
1191057ca2d4SBaptiste Daroussin 		if ((cc = get_collchar(i, 0)) != NULL) {
1192057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
11934644f9beSYuri Pankov 				chars[i].pri[j] =
11944644f9beSYuri Pankov 				    htote(get_weight(cc->ref[j], j));
1195057ca2d4SBaptiste Daroussin 			}
1196057ca2d4SBaptiste Daroussin 		} else {
1197057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1198057ca2d4SBaptiste Daroussin 				chars[i].pri[j] =
11994644f9beSYuri Pankov 				    htote(get_weight(pri_undefined[j], j));
1200057ca2d4SBaptiste Daroussin 			}
1201057ca2d4SBaptiste Daroussin 			/*
1202057ca2d4SBaptiste Daroussin 			 * Per POSIX, for undefined characters, we
1203057ca2d4SBaptiste Daroussin 			 * also have to add a last item, which is the
1204057ca2d4SBaptiste Daroussin 			 * character code.
1205057ca2d4SBaptiste Daroussin 			 */
12064644f9beSYuri Pankov 			chars[i].pri[NUM_WT] = htote(i);
1207057ca2d4SBaptiste Daroussin 		}
1208057ca2d4SBaptiste Daroussin 	}
1209057ca2d4SBaptiste Daroussin 
1210057ca2d4SBaptiste Daroussin 	/*
1211057ca2d4SBaptiste Daroussin 	 * Substitution tables
1212057ca2d4SBaptiste Daroussin 	 */
1213057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1214057ca2d4SBaptiste Daroussin 		collate_subst_t *st = NULL;
121523a32822SBaptiste Daroussin 		subst_t *temp;
121623a32822SBaptiste Daroussin 		RB_COUNT(temp, substs, &substs[i], n);
12174644f9beSYuri Pankov 		subst_count[i] = n;
1218be4391a2SPedro F. Giffuni 		if ((st = calloc(n, sizeof(collate_subst_t))) == NULL) {
1219057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
1220057ca2d4SBaptiste Daroussin 			return;
1221057ca2d4SBaptiste Daroussin 		}
1222057ca2d4SBaptiste Daroussin 		n = 0;
12233a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1224057ca2d4SBaptiste Daroussin 			if ((st[n].key = resolve_pri(sb->key)) < 0) {
1225057ca2d4SBaptiste Daroussin 				/* by definition these resolve! */
1226057ca2d4SBaptiste Daroussin 				INTERR;
1227057ca2d4SBaptiste Daroussin 			}
1228057ca2d4SBaptiste Daroussin 			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
1229057ca2d4SBaptiste Daroussin 				INTERR;
1230057ca2d4SBaptiste Daroussin 			}
12314644f9beSYuri Pankov 			st[n].key = htote(st[n].key);
1232057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
12334644f9beSYuri Pankov 				st[n].pri[j] = htote(get_weight(sb->ref[j],
12344644f9beSYuri Pankov 				    i));
1235057ca2d4SBaptiste Daroussin 			}
1236057ca2d4SBaptiste Daroussin 			n++;
1237057ca2d4SBaptiste Daroussin 		}
12384644f9beSYuri Pankov 		if (n != subst_count[i])
1239057ca2d4SBaptiste Daroussin 			INTERR;
1240057ca2d4SBaptiste Daroussin 		subst[i] = st;
1241057ca2d4SBaptiste Daroussin 	}
1242057ca2d4SBaptiste Daroussin 
1243057ca2d4SBaptiste Daroussin 
1244057ca2d4SBaptiste Daroussin 	/*
1245057ca2d4SBaptiste Daroussin 	 * Chains, i.e. collating elements
1246057ca2d4SBaptiste Daroussin 	 */
12474644f9beSYuri Pankov 	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, chain_count);
12484644f9beSYuri Pankov 	chain = calloc(chain_count, sizeof(collate_chain_t));
1249057ca2d4SBaptiste Daroussin 	if (chain == NULL) {
1250057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1251057ca2d4SBaptiste Daroussin 		return;
1252057ca2d4SBaptiste Daroussin 	}
12533a444dbdSBaptiste Daroussin 	n = 0;
12543a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1255057ca2d4SBaptiste Daroussin 		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
1256057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
12574644f9beSYuri Pankov 			chain[n].pri[i] = htote(get_weight(ce->ref[i], i));
1258057ca2d4SBaptiste Daroussin 		}
12595d21db09SBaptiste Daroussin 		n++;
1260057ca2d4SBaptiste Daroussin 	}
12614644f9beSYuri Pankov 	if (n != chain_count)
1262057ca2d4SBaptiste Daroussin 		INTERR;
1263057ca2d4SBaptiste Daroussin 
1264057ca2d4SBaptiste Daroussin 	/*
1265057ca2d4SBaptiste Daroussin 	 * Large (> UCHAR_MAX) character priorities
1266057ca2d4SBaptiste Daroussin 	 */
12673a444dbdSBaptiste Daroussin 	RB_NUMNODES(collchar_t, collchars, &collchars, n);
12685d21db09SBaptiste Daroussin 	large = calloc(n, sizeof(collate_large_t));
1269057ca2d4SBaptiste Daroussin 	if (large == NULL) {
1270057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1271057ca2d4SBaptiste Daroussin 		return;
1272057ca2d4SBaptiste Daroussin 	}
1273057ca2d4SBaptiste Daroussin 
1274057ca2d4SBaptiste Daroussin 	i = 0;
12753a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1276057ca2d4SBaptiste Daroussin 		int	undef = 0;
1277057ca2d4SBaptiste Daroussin 		/* we already gathered those */
1278057ca2d4SBaptiste Daroussin 		if (cc->wc <= UCHAR_MAX)
1279057ca2d4SBaptiste Daroussin 			continue;
1280057ca2d4SBaptiste Daroussin 		for (j = 0; j < NUM_WT; j++) {
1281057ca2d4SBaptiste Daroussin 			if ((pri = get_weight(cc->ref[j], j)) < 0) {
1282057ca2d4SBaptiste Daroussin 				undef = 1;
1283057ca2d4SBaptiste Daroussin 			}
1284057ca2d4SBaptiste Daroussin 			if (undef && (pri >= 0)) {
1285057ca2d4SBaptiste Daroussin 				/* if undefined, then all priorities are */
1286057ca2d4SBaptiste Daroussin 				INTERR;
1287057ca2d4SBaptiste Daroussin 			} else {
12884644f9beSYuri Pankov 				large[i].pri.pri[j] = htote(pri);
1289057ca2d4SBaptiste Daroussin 			}
1290057ca2d4SBaptiste Daroussin 		}
1291057ca2d4SBaptiste Daroussin 		if (!undef) {
12924644f9beSYuri Pankov 			large[i].val = htote(cc->wc);
12934644f9beSYuri Pankov 			large_count = i++;
1294057ca2d4SBaptiste Daroussin 		}
1295057ca2d4SBaptiste Daroussin 	}
1296057ca2d4SBaptiste Daroussin 
1297057ca2d4SBaptiste Daroussin 	if ((f = open_category()) == NULL) {
1298057ca2d4SBaptiste Daroussin 		return;
1299057ca2d4SBaptiste Daroussin 	}
1300057ca2d4SBaptiste Daroussin 
1301057ca2d4SBaptiste Daroussin 	/* Time to write the entire data set out */
1302057ca2d4SBaptiste Daroussin 
13034644f9beSYuri Pankov 	for (i = 0; i < NUM_WT; i++)
13044644f9beSYuri Pankov 		collinfo.subst_count[i] = htote(subst_count[i]);
13054644f9beSYuri Pankov 	collinfo.chain_count = htote(chain_count);
13064644f9beSYuri Pankov 	collinfo.large_count = htote(large_count);
13074644f9beSYuri Pankov 
1308*cc7edd25SThomas Munro 	if ((wr_category(fmt_version, COLLATE_FMT_VERSION_LEN, f) < 0) ||
1309*cc7edd25SThomas Munro 	    (wr_category(def_version, XLOCALE_DEF_VERSION_LEN, f) < 0) ||
1310057ca2d4SBaptiste Daroussin 	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
1311057ca2d4SBaptiste Daroussin 	    (wr_category(&chars, sizeof (chars), f) < 0)) {
1312057ca2d4SBaptiste Daroussin 		return;
1313057ca2d4SBaptiste Daroussin 	}
1314057ca2d4SBaptiste Daroussin 
1315057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
13164644f9beSYuri Pankov 		sz = sizeof (collate_subst_t) * subst_count[i];
1317057ca2d4SBaptiste Daroussin 		if (wr_category(subst[i], sz, f) < 0) {
1318057ca2d4SBaptiste Daroussin 			return;
1319057ca2d4SBaptiste Daroussin 		}
1320057ca2d4SBaptiste Daroussin 	}
13214644f9beSYuri Pankov 	sz = sizeof (collate_chain_t) * chain_count;
1322057ca2d4SBaptiste Daroussin 	if (wr_category(chain, sz, f) < 0) {
1323057ca2d4SBaptiste Daroussin 		return;
1324057ca2d4SBaptiste Daroussin 	}
13254644f9beSYuri Pankov 	sz = sizeof (collate_large_t) * large_count;
1326057ca2d4SBaptiste Daroussin 	if (wr_category(large, sz, f) < 0) {
1327057ca2d4SBaptiste Daroussin 		return;
1328057ca2d4SBaptiste Daroussin 	}
1329057ca2d4SBaptiste Daroussin 
1330057ca2d4SBaptiste Daroussin 	close_category(f);
1331057ca2d4SBaptiste Daroussin }
1332