xref: /freebsd/usr.bin/localedef/collate.c (revision fcc7baa1ae504e353c4a87292fd851192705ad72)
1057ca2d4SBaptiste Daroussin /*
2057ca2d4SBaptiste Daroussin  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
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;
256057ca2d4SBaptiste Daroussin 
257057ca2d4SBaptiste Daroussin static collpri_t	*prilist = NULL;
258057ca2d4SBaptiste Daroussin static int		numpri = 0;
259057ca2d4SBaptiste Daroussin static int		maxpri = 0;
260057ca2d4SBaptiste Daroussin 
261057ca2d4SBaptiste Daroussin static void start_order(int);
262057ca2d4SBaptiste Daroussin 
263057ca2d4SBaptiste Daroussin static int32_t
264057ca2d4SBaptiste Daroussin new_pri(void)
265057ca2d4SBaptiste Daroussin {
266057ca2d4SBaptiste Daroussin 	int i;
267057ca2d4SBaptiste Daroussin 
268057ca2d4SBaptiste Daroussin 	if (numpri >= maxpri) {
269057ca2d4SBaptiste Daroussin 		maxpri = maxpri ? maxpri * 2 : 1024;
270057ca2d4SBaptiste Daroussin 		prilist = realloc(prilist, sizeof (collpri_t) * maxpri);
271057ca2d4SBaptiste Daroussin 		if (prilist == NULL) {
272057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
273057ca2d4SBaptiste Daroussin 			return (-1);
274057ca2d4SBaptiste Daroussin 		}
275057ca2d4SBaptiste Daroussin 		for (i = numpri; i < maxpri; i++) {
276057ca2d4SBaptiste Daroussin 			prilist[i].res = UNKNOWN;
277057ca2d4SBaptiste Daroussin 			prilist[i].pri = 0;
278057ca2d4SBaptiste Daroussin 			prilist[i].pass = 0;
279057ca2d4SBaptiste Daroussin 		}
280057ca2d4SBaptiste Daroussin 	}
281057ca2d4SBaptiste Daroussin 	return (numpri++);
282057ca2d4SBaptiste Daroussin }
283057ca2d4SBaptiste Daroussin 
284057ca2d4SBaptiste Daroussin static collpri_t *
285057ca2d4SBaptiste Daroussin get_pri(int32_t ref)
286057ca2d4SBaptiste Daroussin {
287057ca2d4SBaptiste Daroussin 	if ((ref < 0) || (ref > numpri)) {
288057ca2d4SBaptiste Daroussin 		INTERR;
289057ca2d4SBaptiste Daroussin 		return (NULL);
290057ca2d4SBaptiste Daroussin 	}
291057ca2d4SBaptiste Daroussin 	return (&prilist[ref]);
292057ca2d4SBaptiste Daroussin }
293057ca2d4SBaptiste Daroussin 
294057ca2d4SBaptiste Daroussin static void
295057ca2d4SBaptiste Daroussin set_pri(int32_t ref, int32_t v, res_t res)
296057ca2d4SBaptiste Daroussin {
297057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
298057ca2d4SBaptiste Daroussin 
299057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
300057ca2d4SBaptiste Daroussin 
301057ca2d4SBaptiste Daroussin 	if ((res == REFER) && ((v < 0) || (v >= numpri))) {
302057ca2d4SBaptiste Daroussin 		INTERR;
303057ca2d4SBaptiste Daroussin 	}
304057ca2d4SBaptiste Daroussin 
305057ca2d4SBaptiste Daroussin 	/* Resolve self references */
306057ca2d4SBaptiste Daroussin 	if ((res == REFER) && (ref == v)) {
307057ca2d4SBaptiste Daroussin 		v = nextpri;
308057ca2d4SBaptiste Daroussin 		res = RESOLVED;
309057ca2d4SBaptiste Daroussin 	}
310057ca2d4SBaptiste Daroussin 
311057ca2d4SBaptiste Daroussin 	if (pri->res != UNKNOWN) {
312057ca2d4SBaptiste Daroussin 		warn("repeated item in order list (first on %d)",
313057ca2d4SBaptiste Daroussin 		    pri->lineno);
314057ca2d4SBaptiste Daroussin 		return;
315057ca2d4SBaptiste Daroussin 	}
316057ca2d4SBaptiste Daroussin 	pri->lineno = lineno;
317057ca2d4SBaptiste Daroussin 	pri->pri = v;
318057ca2d4SBaptiste Daroussin 	pri->res = res;
319057ca2d4SBaptiste Daroussin }
320057ca2d4SBaptiste Daroussin 
321057ca2d4SBaptiste Daroussin static int32_t
322057ca2d4SBaptiste Daroussin resolve_pri(int32_t ref)
323057ca2d4SBaptiste Daroussin {
324057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
325057ca2d4SBaptiste Daroussin 	static int32_t	pass = 0;
326057ca2d4SBaptiste Daroussin 
327057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
328057ca2d4SBaptiste Daroussin 	pass++;
329057ca2d4SBaptiste Daroussin 	while (pri->res == REFER) {
330057ca2d4SBaptiste Daroussin 		if (pri->pass == pass) {
331057ca2d4SBaptiste Daroussin 			/* report a line with the circular symbol */
332057ca2d4SBaptiste Daroussin 			lineno = pri->lineno;
333057ca2d4SBaptiste Daroussin 			fprintf(stderr,"circular reference in order list");
334057ca2d4SBaptiste Daroussin 			return (-1);
335057ca2d4SBaptiste Daroussin 		}
336057ca2d4SBaptiste Daroussin 		if ((pri->pri < 0) || (pri->pri >= numpri)) {
337057ca2d4SBaptiste Daroussin 			INTERR;
338057ca2d4SBaptiste Daroussin 			return (-1);
339057ca2d4SBaptiste Daroussin 		}
340057ca2d4SBaptiste Daroussin 		pri->pass = pass;
341057ca2d4SBaptiste Daroussin 		pri = &prilist[pri->pri];
342057ca2d4SBaptiste Daroussin 	}
343057ca2d4SBaptiste Daroussin 
344057ca2d4SBaptiste Daroussin 	if (pri->res == UNKNOWN) {
345057ca2d4SBaptiste Daroussin 		return (-1);
346057ca2d4SBaptiste Daroussin 	}
347057ca2d4SBaptiste Daroussin 	if (pri->res != RESOLVED)
348057ca2d4SBaptiste Daroussin 		INTERR;
349057ca2d4SBaptiste Daroussin 
350057ca2d4SBaptiste Daroussin 	return (pri->pri);
351057ca2d4SBaptiste Daroussin }
352057ca2d4SBaptiste Daroussin 
353057ca2d4SBaptiste Daroussin static int
354057ca2d4SBaptiste Daroussin weight_compare(const void *n1, const void *n2)
355057ca2d4SBaptiste Daroussin {
356057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const weight_t *)n1)->pri;
357057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const weight_t *)n2)->pri;
358057ca2d4SBaptiste Daroussin 
359057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
360057ca2d4SBaptiste Daroussin }
361057ca2d4SBaptiste Daroussin 
3623a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(weights, weight, entry, weight_compare);
3633a444dbdSBaptiste Daroussin 
364057ca2d4SBaptiste Daroussin static int
365057ca2d4SBaptiste Daroussin collsym_compare(const void *n1, const void *n2)
366057ca2d4SBaptiste Daroussin {
367057ca2d4SBaptiste Daroussin 	const collsym_t *c1 = n1;
368057ca2d4SBaptiste Daroussin 	const collsym_t *c2 = n2;
369057ca2d4SBaptiste Daroussin 	int rv;
370057ca2d4SBaptiste Daroussin 
371057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
372057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
373057ca2d4SBaptiste Daroussin }
374057ca2d4SBaptiste Daroussin 
3753a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collsyms, collsym, entry, collsym_compare);
3763a444dbdSBaptiste Daroussin 
377057ca2d4SBaptiste Daroussin static int
378057ca2d4SBaptiste Daroussin collundef_compare(const void *n1, const void *n2)
379057ca2d4SBaptiste Daroussin {
380057ca2d4SBaptiste Daroussin 	const collundef_t *c1 = n1;
381057ca2d4SBaptiste Daroussin 	const collundef_t *c2 = n2;
382057ca2d4SBaptiste Daroussin 	int rv;
383057ca2d4SBaptiste Daroussin 
384057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
385057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
386057ca2d4SBaptiste Daroussin }
387057ca2d4SBaptiste Daroussin 
3883a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collundefs, collundef, entry, collundef_compare);
3893a444dbdSBaptiste Daroussin 
390057ca2d4SBaptiste Daroussin static int
391057ca2d4SBaptiste Daroussin element_compare_symbol(const void *n1, const void *n2)
392057ca2d4SBaptiste Daroussin {
393057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
394057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
395057ca2d4SBaptiste Daroussin 	int rv;
396057ca2d4SBaptiste Daroussin 
397057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->symbol, c2->symbol);
398057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
399057ca2d4SBaptiste Daroussin }
400057ca2d4SBaptiste Daroussin 
4013a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_symbol, collelem, rb_bysymbol, element_compare_symbol);
4023a444dbdSBaptiste Daroussin 
403057ca2d4SBaptiste Daroussin static int
404057ca2d4SBaptiste Daroussin element_compare_expand(const void *n1, const void *n2)
405057ca2d4SBaptiste Daroussin {
406057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
407057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
408057ca2d4SBaptiste Daroussin 	int rv;
409057ca2d4SBaptiste Daroussin 
410057ca2d4SBaptiste Daroussin 	rv = wcscmp(c1->expand, c2->expand);
411057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
412057ca2d4SBaptiste Daroussin }
413057ca2d4SBaptiste Daroussin 
4143a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_expand, collelem, rb_byexpand, element_compare_expand);
4153a444dbdSBaptiste Daroussin 
416057ca2d4SBaptiste Daroussin static int
417057ca2d4SBaptiste Daroussin collchar_compare(const void *n1, const void *n2)
418057ca2d4SBaptiste Daroussin {
419057ca2d4SBaptiste Daroussin 	wchar_t	k1 = ((const collchar_t *)n1)->wc;
420057ca2d4SBaptiste Daroussin 	wchar_t	k2 = ((const collchar_t *)n2)->wc;
421057ca2d4SBaptiste Daroussin 
422057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
423057ca2d4SBaptiste Daroussin }
424057ca2d4SBaptiste Daroussin 
4253a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collchars, collchar, entry, collchar_compare);
4263a444dbdSBaptiste Daroussin 
427057ca2d4SBaptiste Daroussin static int
428057ca2d4SBaptiste Daroussin subst_compare(const void *n1, const void *n2)
429057ca2d4SBaptiste Daroussin {
430057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const subst_t *)n1)->key;
431057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const subst_t *)n2)->key;
432057ca2d4SBaptiste Daroussin 
433057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
434057ca2d4SBaptiste Daroussin }
435057ca2d4SBaptiste Daroussin 
4363a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs, subst, entry, subst_compare);
4373a444dbdSBaptiste Daroussin 
438057ca2d4SBaptiste Daroussin static int
439057ca2d4SBaptiste Daroussin subst_compare_ref(const void *n1, const void *n2)
440057ca2d4SBaptiste Daroussin {
44155b270e6SBaptiste Daroussin 	const wchar_t *c1 = ((const subst_t *)n1)->ref;
44255b270e6SBaptiste Daroussin 	const wchar_t *c2 = ((const subst_t *)n2)->ref;
443057ca2d4SBaptiste Daroussin 	int rv;
444057ca2d4SBaptiste Daroussin 
44555b270e6SBaptiste Daroussin 	rv = wcscmp(c1, c2);
446057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
447057ca2d4SBaptiste Daroussin }
448057ca2d4SBaptiste Daroussin 
4493a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs_ref, subst, entry_ref, subst_compare_ref);
4503a444dbdSBaptiste Daroussin 
451057ca2d4SBaptiste Daroussin void
452057ca2d4SBaptiste Daroussin init_collate(void)
453057ca2d4SBaptiste Daroussin {
454057ca2d4SBaptiste Daroussin 	int i;
455057ca2d4SBaptiste Daroussin 
4563a444dbdSBaptiste Daroussin 	RB_INIT(&collsyms);
457057ca2d4SBaptiste Daroussin 
4583a444dbdSBaptiste Daroussin 	RB_INIT(&collundefs);
459057ca2d4SBaptiste Daroussin 
4603a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_symbol);
461057ca2d4SBaptiste Daroussin 
4623a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_expand);
4633a444dbdSBaptiste Daroussin 
4643a444dbdSBaptiste Daroussin 	RB_INIT(&collchars);
465057ca2d4SBaptiste Daroussin 
466057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
4673a444dbdSBaptiste Daroussin 		RB_INIT(&substs[i]);
4683a444dbdSBaptiste Daroussin 		RB_INIT(&substs_ref[i]);
4693a444dbdSBaptiste Daroussin 		RB_INIT(&weights[i]);
470057ca2d4SBaptiste Daroussin 		nweight[i] = 1;
471057ca2d4SBaptiste Daroussin 	}
472057ca2d4SBaptiste Daroussin 
473057ca2d4SBaptiste Daroussin 	(void) memset(&collinfo, 0, sizeof (collinfo));
474057ca2d4SBaptiste Daroussin 
475057ca2d4SBaptiste Daroussin 	/* allocate some initial priorities */
476057ca2d4SBaptiste Daroussin 	pri_ignore = new_pri();
477057ca2d4SBaptiste Daroussin 
478057ca2d4SBaptiste Daroussin 	set_pri(pri_ignore, 0, RESOLVED);
479057ca2d4SBaptiste Daroussin 
480057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
481057ca2d4SBaptiste Daroussin 		pri_undefined[i] = new_pri();
482057ca2d4SBaptiste Daroussin 
483057ca2d4SBaptiste Daroussin 		/* we will override this later */
484057ca2d4SBaptiste Daroussin 		set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN);
485057ca2d4SBaptiste Daroussin 	}
486057ca2d4SBaptiste Daroussin }
487057ca2d4SBaptiste Daroussin 
488057ca2d4SBaptiste Daroussin void
489057ca2d4SBaptiste Daroussin define_collsym(char *name)
490057ca2d4SBaptiste Daroussin {
491057ca2d4SBaptiste Daroussin 	collsym_t	*sym;
492057ca2d4SBaptiste Daroussin 
493057ca2d4SBaptiste Daroussin 	if ((sym = calloc(sizeof (*sym), 1)) == NULL) {
494057ca2d4SBaptiste Daroussin 		fprintf(stderr,"out of memory");
495057ca2d4SBaptiste Daroussin 		return;
496057ca2d4SBaptiste Daroussin 	}
497057ca2d4SBaptiste Daroussin 	sym->name = name;
498057ca2d4SBaptiste Daroussin 	sym->ref = new_pri();
499057ca2d4SBaptiste Daroussin 
5003a444dbdSBaptiste Daroussin 	if (RB_FIND(collsyms, &collsyms, sym) != NULL) {
501057ca2d4SBaptiste Daroussin 		/*
502057ca2d4SBaptiste Daroussin 		 * This should never happen because we are only called
503057ca2d4SBaptiste Daroussin 		 * for undefined symbols.
504057ca2d4SBaptiste Daroussin 		 */
505e6d8c0e2SBaptiste Daroussin 		free(sym);
506057ca2d4SBaptiste Daroussin 		INTERR;
507057ca2d4SBaptiste Daroussin 		return;
508057ca2d4SBaptiste Daroussin 	}
5093a444dbdSBaptiste Daroussin 	RB_INSERT(collsyms, &collsyms, sym);
510057ca2d4SBaptiste Daroussin }
511057ca2d4SBaptiste Daroussin 
512057ca2d4SBaptiste Daroussin collsym_t *
513057ca2d4SBaptiste Daroussin lookup_collsym(char *name)
514057ca2d4SBaptiste Daroussin {
515057ca2d4SBaptiste Daroussin 	collsym_t	srch;
516057ca2d4SBaptiste Daroussin 
517057ca2d4SBaptiste Daroussin 	srch.name = name;
5183a444dbdSBaptiste Daroussin 	return (RB_FIND(collsyms, &collsyms, &srch));
519057ca2d4SBaptiste Daroussin }
520057ca2d4SBaptiste Daroussin 
521057ca2d4SBaptiste Daroussin collelem_t *
522057ca2d4SBaptiste Daroussin lookup_collelem(char *symbol)
523057ca2d4SBaptiste Daroussin {
524057ca2d4SBaptiste Daroussin 	collelem_t	srch;
525057ca2d4SBaptiste Daroussin 
526057ca2d4SBaptiste Daroussin 	srch.symbol = symbol;
5273a444dbdSBaptiste Daroussin 	return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch));
528057ca2d4SBaptiste Daroussin }
529057ca2d4SBaptiste Daroussin 
530057ca2d4SBaptiste Daroussin static collundef_t *
531057ca2d4SBaptiste Daroussin get_collundef(char *name)
532057ca2d4SBaptiste Daroussin {
533057ca2d4SBaptiste Daroussin 	collundef_t	srch;
534057ca2d4SBaptiste Daroussin 	collundef_t	*ud;
535057ca2d4SBaptiste Daroussin 	int		i;
536057ca2d4SBaptiste Daroussin 
537057ca2d4SBaptiste Daroussin 	srch.name = name;
5383a444dbdSBaptiste Daroussin 	if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) {
539057ca2d4SBaptiste Daroussin 		if (((ud = calloc(sizeof (*ud), 1)) == NULL) ||
540057ca2d4SBaptiste Daroussin 		    ((ud->name = strdup(name)) == NULL)) {
541057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
542e6d8c0e2SBaptiste Daroussin 			free(ud);
543057ca2d4SBaptiste Daroussin 			return (NULL);
544057ca2d4SBaptiste Daroussin 		}
545057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
546057ca2d4SBaptiste Daroussin 			ud->ref[i] = new_pri();
547057ca2d4SBaptiste Daroussin 		}
5483a444dbdSBaptiste Daroussin 		RB_INSERT(collundefs, &collundefs, ud);
549057ca2d4SBaptiste Daroussin 	}
550057ca2d4SBaptiste Daroussin 	add_charmap_undefined(name);
551057ca2d4SBaptiste Daroussin 	return (ud);
552057ca2d4SBaptiste Daroussin }
553057ca2d4SBaptiste Daroussin 
554057ca2d4SBaptiste Daroussin static collchar_t *
555057ca2d4SBaptiste Daroussin get_collchar(wchar_t wc, int create)
556057ca2d4SBaptiste Daroussin {
557057ca2d4SBaptiste Daroussin 	collchar_t	srch;
558057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
559057ca2d4SBaptiste Daroussin 	int		i;
560057ca2d4SBaptiste Daroussin 
561057ca2d4SBaptiste Daroussin 	srch.wc = wc;
5623a444dbdSBaptiste Daroussin 	cc = RB_FIND(collchars, &collchars, &srch);
563057ca2d4SBaptiste Daroussin 	if ((cc == NULL) && create) {
564057ca2d4SBaptiste Daroussin 		if ((cc = calloc(sizeof (*cc), 1)) == NULL) {
565057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
566057ca2d4SBaptiste Daroussin 			return (NULL);
567057ca2d4SBaptiste Daroussin 		}
568057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
569057ca2d4SBaptiste Daroussin 			cc->ref[i] = new_pri();
570057ca2d4SBaptiste Daroussin 		}
571057ca2d4SBaptiste Daroussin 		cc->wc = wc;
5723a444dbdSBaptiste Daroussin 		RB_INSERT(collchars, &collchars, cc);
573057ca2d4SBaptiste Daroussin 	}
574057ca2d4SBaptiste Daroussin 	return (cc);
575057ca2d4SBaptiste Daroussin }
576057ca2d4SBaptiste Daroussin 
577057ca2d4SBaptiste Daroussin void
578057ca2d4SBaptiste Daroussin end_order_collsym(collsym_t *sym)
579057ca2d4SBaptiste Daroussin {
580057ca2d4SBaptiste Daroussin 	start_order(T_COLLSYM);
581057ca2d4SBaptiste Daroussin 	/* update the weight */
582057ca2d4SBaptiste Daroussin 
583057ca2d4SBaptiste Daroussin 	set_pri(sym->ref, nextpri, RESOLVED);
584057ca2d4SBaptiste Daroussin 	nextpri++;
585057ca2d4SBaptiste Daroussin }
586057ca2d4SBaptiste Daroussin 
587057ca2d4SBaptiste Daroussin void
588057ca2d4SBaptiste Daroussin end_order(void)
589057ca2d4SBaptiste Daroussin {
590057ca2d4SBaptiste Daroussin 	int		i;
591057ca2d4SBaptiste Daroussin 	int32_t		pri;
592057ca2d4SBaptiste Daroussin 	int32_t		ref;
593057ca2d4SBaptiste Daroussin 	collpri_t	*p;
594057ca2d4SBaptiste Daroussin 
595057ca2d4SBaptiste Daroussin 	/* advance the priority/weight */
596057ca2d4SBaptiste Daroussin 	pri = nextpri;
597057ca2d4SBaptiste Daroussin 
598057ca2d4SBaptiste Daroussin 	switch (currorder) {
599057ca2d4SBaptiste Daroussin 	case T_CHAR:
600057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
601057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
602057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
603057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
604057ca2d4SBaptiste Daroussin 				/* unspecified weight is a self reference */
605057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], pri, RESOLVED);
606057ca2d4SBaptiste Daroussin 			} else {
607057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], ref, REFER);
608057ca2d4SBaptiste Daroussin 			}
609057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
610057ca2d4SBaptiste Daroussin 		}
611057ca2d4SBaptiste Daroussin 
612057ca2d4SBaptiste Daroussin 		/* leave a cookie trail in case next symbol is ellipsis */
613057ca2d4SBaptiste Daroussin 		ellipsis_start = currchar->wc + 1;
614057ca2d4SBaptiste Daroussin 		currchar = NULL;
615057ca2d4SBaptiste Daroussin 		break;
616057ca2d4SBaptiste Daroussin 
617057ca2d4SBaptiste Daroussin 	case T_ELLIPSIS:
618057ca2d4SBaptiste Daroussin 		/* save off the weights were we can find them */
619057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
620057ca2d4SBaptiste Daroussin 			ellipsis_weights[i] = order_weights[i];
621057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
622057ca2d4SBaptiste Daroussin 		}
623057ca2d4SBaptiste Daroussin 		break;
624057ca2d4SBaptiste Daroussin 
625057ca2d4SBaptiste Daroussin 	case T_COLLELEM:
626057ca2d4SBaptiste Daroussin 		if (currelem == NULL) {
627057ca2d4SBaptiste Daroussin 			INTERR;
628057ca2d4SBaptiste Daroussin 		} else {
629057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
630057ca2d4SBaptiste Daroussin 
631057ca2d4SBaptiste Daroussin 				if (((ref = order_weights[i]) < 0) ||
632057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
633057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
634057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], pri,
635057ca2d4SBaptiste Daroussin 					    RESOLVED);
636057ca2d4SBaptiste Daroussin 				} else {
637057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], ref, REFER);
638057ca2d4SBaptiste Daroussin 				}
639057ca2d4SBaptiste Daroussin 				order_weights[i] = -1;
640057ca2d4SBaptiste Daroussin 			}
641057ca2d4SBaptiste Daroussin 		}
642057ca2d4SBaptiste Daroussin 		break;
643057ca2d4SBaptiste Daroussin 
644057ca2d4SBaptiste Daroussin 	case T_UNDEFINED:
645057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
646057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
647057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
648057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
649057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], -1, RESOLVED);
650057ca2d4SBaptiste Daroussin 			} else {
651057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], ref, REFER);
652057ca2d4SBaptiste Daroussin 			}
653057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
654057ca2d4SBaptiste Daroussin 		}
655057ca2d4SBaptiste Daroussin 		break;
656057ca2d4SBaptiste Daroussin 
657057ca2d4SBaptiste Daroussin 	case T_SYMBOL:
658057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
659057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
660057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
661057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
662057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], pri, RESOLVED);
663057ca2d4SBaptiste Daroussin 			} else {
664057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], ref, REFER);
665057ca2d4SBaptiste Daroussin 			}
666057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
667057ca2d4SBaptiste Daroussin 		}
668057ca2d4SBaptiste Daroussin 		break;
669057ca2d4SBaptiste Daroussin 
670057ca2d4SBaptiste Daroussin 	default:
671057ca2d4SBaptiste Daroussin 		INTERR;
672057ca2d4SBaptiste Daroussin 	}
673057ca2d4SBaptiste Daroussin 
674057ca2d4SBaptiste Daroussin 	nextpri++;
675057ca2d4SBaptiste Daroussin }
676057ca2d4SBaptiste Daroussin 
677057ca2d4SBaptiste Daroussin static void
678057ca2d4SBaptiste Daroussin start_order(int type)
679057ca2d4SBaptiste Daroussin {
680057ca2d4SBaptiste Daroussin 	int	i;
681057ca2d4SBaptiste Daroussin 
682057ca2d4SBaptiste Daroussin 	lastorder = currorder;
683057ca2d4SBaptiste Daroussin 	currorder = type;
684057ca2d4SBaptiste Daroussin 
685057ca2d4SBaptiste Daroussin 	/* this is used to protect ELLIPSIS processing */
686057ca2d4SBaptiste Daroussin 	if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) {
687057ca2d4SBaptiste Daroussin 		fprintf(stderr, "character value expected");
688057ca2d4SBaptiste Daroussin 	}
689057ca2d4SBaptiste Daroussin 
690057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
691057ca2d4SBaptiste Daroussin 		order_weights[i] = -1;
692057ca2d4SBaptiste Daroussin 	}
693057ca2d4SBaptiste Daroussin 	curr_weight = 0;
694057ca2d4SBaptiste Daroussin }
695057ca2d4SBaptiste Daroussin 
696057ca2d4SBaptiste Daroussin void
697057ca2d4SBaptiste Daroussin start_order_undefined(void)
698057ca2d4SBaptiste Daroussin {
699057ca2d4SBaptiste Daroussin 	start_order(T_UNDEFINED);
700057ca2d4SBaptiste Daroussin }
701057ca2d4SBaptiste Daroussin 
702057ca2d4SBaptiste Daroussin void
703057ca2d4SBaptiste Daroussin start_order_symbol(char *name)
704057ca2d4SBaptiste Daroussin {
705057ca2d4SBaptiste Daroussin 	currundef = get_collundef(name);
706057ca2d4SBaptiste Daroussin 	start_order(T_SYMBOL);
707057ca2d4SBaptiste Daroussin }
708057ca2d4SBaptiste Daroussin 
709057ca2d4SBaptiste Daroussin void
710057ca2d4SBaptiste Daroussin start_order_char(wchar_t wc)
711057ca2d4SBaptiste Daroussin {
712057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
713057ca2d4SBaptiste Daroussin 	int32_t		ref;
714057ca2d4SBaptiste Daroussin 
715057ca2d4SBaptiste Daroussin 	start_order(T_CHAR);
716057ca2d4SBaptiste Daroussin 
717057ca2d4SBaptiste Daroussin 	/*
718057ca2d4SBaptiste Daroussin 	 * If we last saw an ellipsis, then we need to close the range.
719057ca2d4SBaptiste Daroussin 	 * Handle that here.  Note that we have to be careful because the
720057ca2d4SBaptiste Daroussin 	 * items *inside* the range are treated exclusiveley to the items
721057ca2d4SBaptiste Daroussin 	 * outside of the range.  The ends of the range can have quite
722057ca2d4SBaptiste Daroussin 	 * different weights than the range members.
723057ca2d4SBaptiste Daroussin 	 */
724057ca2d4SBaptiste Daroussin 	if (lastorder == T_ELLIPSIS) {
725057ca2d4SBaptiste Daroussin 		int		i;
726057ca2d4SBaptiste Daroussin 
727057ca2d4SBaptiste Daroussin 		if (wc < ellipsis_start) {
728057ca2d4SBaptiste Daroussin 			fprintf(stderr, "malformed range!");
729057ca2d4SBaptiste Daroussin 			return;
730057ca2d4SBaptiste Daroussin 		}
731057ca2d4SBaptiste Daroussin 		while (ellipsis_start < wc) {
732057ca2d4SBaptiste Daroussin 			/*
733057ca2d4SBaptiste Daroussin 			 * pick all of the saved weights for the
734057ca2d4SBaptiste Daroussin 			 * ellipsis.  note that -1 encodes for the
735057ca2d4SBaptiste Daroussin 			 * ellipsis itself, which means to take the
736057ca2d4SBaptiste Daroussin 			 * current relative priority.
737057ca2d4SBaptiste Daroussin 			 */
738057ca2d4SBaptiste Daroussin 			if ((cc = get_collchar(ellipsis_start, 1)) == NULL) {
739057ca2d4SBaptiste Daroussin 				INTERR;
740057ca2d4SBaptiste Daroussin 				return;
741057ca2d4SBaptiste Daroussin 			}
742057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
743057ca2d4SBaptiste Daroussin 				collpri_t *p;
744057ca2d4SBaptiste Daroussin 				if (((ref = ellipsis_weights[i]) == -1) ||
745057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
746057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
747057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], nextpri, RESOLVED);
748057ca2d4SBaptiste Daroussin 				} else {
749057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], ref, REFER);
750057ca2d4SBaptiste Daroussin 				}
751057ca2d4SBaptiste Daroussin 				ellipsis_weights[i] = 0;
752057ca2d4SBaptiste Daroussin 			}
753057ca2d4SBaptiste Daroussin 			ellipsis_start++;
754057ca2d4SBaptiste Daroussin 			nextpri++;
755057ca2d4SBaptiste Daroussin 		}
756057ca2d4SBaptiste Daroussin 	}
757057ca2d4SBaptiste Daroussin 
758057ca2d4SBaptiste Daroussin 	currchar = get_collchar(wc, 1);
759057ca2d4SBaptiste Daroussin }
760057ca2d4SBaptiste Daroussin 
761057ca2d4SBaptiste Daroussin void
762057ca2d4SBaptiste Daroussin start_order_collelem(collelem_t *e)
763057ca2d4SBaptiste Daroussin {
764057ca2d4SBaptiste Daroussin 	start_order(T_COLLELEM);
765057ca2d4SBaptiste Daroussin 	currelem = e;
766057ca2d4SBaptiste Daroussin }
767057ca2d4SBaptiste Daroussin 
768057ca2d4SBaptiste Daroussin void
769057ca2d4SBaptiste Daroussin start_order_ellipsis(void)
770057ca2d4SBaptiste Daroussin {
771057ca2d4SBaptiste Daroussin 	int	i;
772057ca2d4SBaptiste Daroussin 
773057ca2d4SBaptiste Daroussin 	start_order(T_ELLIPSIS);
774057ca2d4SBaptiste Daroussin 
775057ca2d4SBaptiste Daroussin 	if (lastorder != T_CHAR) {
776057ca2d4SBaptiste Daroussin 		fprintf(stderr, "illegal starting point for range");
777057ca2d4SBaptiste Daroussin 		return;
778057ca2d4SBaptiste Daroussin 	}
779057ca2d4SBaptiste Daroussin 
780057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
781057ca2d4SBaptiste Daroussin 		ellipsis_weights[i] = order_weights[i];
782057ca2d4SBaptiste Daroussin 	}
783057ca2d4SBaptiste Daroussin }
784057ca2d4SBaptiste Daroussin 
785057ca2d4SBaptiste Daroussin void
786057ca2d4SBaptiste Daroussin define_collelem(char *name, wchar_t *wcs)
787057ca2d4SBaptiste Daroussin {
788057ca2d4SBaptiste Daroussin 	collelem_t	*e;
789057ca2d4SBaptiste Daroussin 	int		i;
790057ca2d4SBaptiste Daroussin 
791057ca2d4SBaptiste Daroussin 	if (wcslen(wcs) >= COLLATE_STR_LEN) {
792057ca2d4SBaptiste Daroussin 		fprintf(stderr,"expanded collation element too long");
793057ca2d4SBaptiste Daroussin 		return;
794057ca2d4SBaptiste Daroussin 	}
795057ca2d4SBaptiste Daroussin 
796057ca2d4SBaptiste Daroussin 	if ((e = calloc(sizeof (*e), 1)) == NULL) {
797057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
798057ca2d4SBaptiste Daroussin 		return;
799057ca2d4SBaptiste Daroussin 	}
800057ca2d4SBaptiste Daroussin 	e->expand = wcs;
801057ca2d4SBaptiste Daroussin 	e->symbol = name;
802057ca2d4SBaptiste Daroussin 
803057ca2d4SBaptiste Daroussin 	/*
804057ca2d4SBaptiste Daroussin 	 * This is executed before the order statement, so we don't
805057ca2d4SBaptiste Daroussin 	 * know how many priorities we *really* need.  We allocate one
806057ca2d4SBaptiste Daroussin 	 * for each possible weight.  Not a big deal, as collating-elements
807057ca2d4SBaptiste Daroussin 	 * prove to be quite rare.
808057ca2d4SBaptiste Daroussin 	 */
809057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
810057ca2d4SBaptiste Daroussin 		e->ref[i] = new_pri();
811057ca2d4SBaptiste Daroussin 	}
812057ca2d4SBaptiste Daroussin 
813057ca2d4SBaptiste Daroussin 	/* A character sequence can only reduce to one element. */
8143a444dbdSBaptiste Daroussin 	if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) ||
8153a444dbdSBaptiste Daroussin 	    (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) {
816057ca2d4SBaptiste Daroussin 		fprintf(stderr, "duplicate collating element definition");
817e6d8c0e2SBaptiste Daroussin 		free(e);
818057ca2d4SBaptiste Daroussin 		return;
819057ca2d4SBaptiste Daroussin 	}
8203a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_symbol, &elem_by_symbol, e);
8213a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_expand, &elem_by_expand, e);
822057ca2d4SBaptiste Daroussin }
823057ca2d4SBaptiste Daroussin 
824057ca2d4SBaptiste Daroussin void
825057ca2d4SBaptiste Daroussin add_order_bit(int kw)
826057ca2d4SBaptiste Daroussin {
827057ca2d4SBaptiste Daroussin 	uint8_t bit = DIRECTIVE_UNDEF;
828057ca2d4SBaptiste Daroussin 
829057ca2d4SBaptiste Daroussin 	switch (kw) {
830057ca2d4SBaptiste Daroussin 	case T_FORWARD:
831057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_FORWARD;
832057ca2d4SBaptiste Daroussin 		break;
833057ca2d4SBaptiste Daroussin 	case T_BACKWARD:
834057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_BACKWARD;
835057ca2d4SBaptiste Daroussin 		break;
836057ca2d4SBaptiste Daroussin 	case T_POSITION:
837057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_POSITION;
838057ca2d4SBaptiste Daroussin 		break;
839057ca2d4SBaptiste Daroussin 	default:
840057ca2d4SBaptiste Daroussin 		INTERR;
841057ca2d4SBaptiste Daroussin 		break;
842057ca2d4SBaptiste Daroussin 	}
843057ca2d4SBaptiste Daroussin 	collinfo.directive[collinfo.directive_count] |= bit;
844057ca2d4SBaptiste Daroussin }
845057ca2d4SBaptiste Daroussin 
846057ca2d4SBaptiste Daroussin void
847057ca2d4SBaptiste Daroussin add_order_directive(void)
848057ca2d4SBaptiste Daroussin {
849057ca2d4SBaptiste Daroussin 	if (collinfo.directive_count >= COLL_WEIGHTS_MAX) {
850057ca2d4SBaptiste Daroussin 		fprintf(stderr,"too many directives (max %d)", COLL_WEIGHTS_MAX);
851057ca2d4SBaptiste Daroussin 	}
852057ca2d4SBaptiste Daroussin 	collinfo.directive_count++;
853057ca2d4SBaptiste Daroussin }
854057ca2d4SBaptiste Daroussin 
855057ca2d4SBaptiste Daroussin static void
856057ca2d4SBaptiste Daroussin add_order_pri(int32_t ref)
857057ca2d4SBaptiste Daroussin {
858057ca2d4SBaptiste Daroussin 	if (curr_weight >= NUM_WT) {
859057ca2d4SBaptiste Daroussin 		fprintf(stderr,"too many weights (max %d)", NUM_WT);
860057ca2d4SBaptiste Daroussin 		return;
861057ca2d4SBaptiste Daroussin 	}
862057ca2d4SBaptiste Daroussin 	order_weights[curr_weight] = ref;
863057ca2d4SBaptiste Daroussin 	curr_weight++;
864057ca2d4SBaptiste Daroussin }
865057ca2d4SBaptiste Daroussin 
866057ca2d4SBaptiste Daroussin void
867057ca2d4SBaptiste Daroussin add_order_collsym(collsym_t *s)
868057ca2d4SBaptiste Daroussin {
869057ca2d4SBaptiste Daroussin 	add_order_pri(s->ref);
870057ca2d4SBaptiste Daroussin }
871057ca2d4SBaptiste Daroussin 
872057ca2d4SBaptiste Daroussin void
873057ca2d4SBaptiste Daroussin add_order_char(wchar_t wc)
874057ca2d4SBaptiste Daroussin {
875057ca2d4SBaptiste Daroussin 	collchar_t *cc;
876057ca2d4SBaptiste Daroussin 
877057ca2d4SBaptiste Daroussin 	if ((cc = get_collchar(wc, 1)) == NULL) {
878057ca2d4SBaptiste Daroussin 		INTERR;
879057ca2d4SBaptiste Daroussin 		return;
880057ca2d4SBaptiste Daroussin 	}
881057ca2d4SBaptiste Daroussin 
882057ca2d4SBaptiste Daroussin 	add_order_pri(cc->ref[curr_weight]);
883057ca2d4SBaptiste Daroussin }
884057ca2d4SBaptiste Daroussin 
885057ca2d4SBaptiste Daroussin void
886057ca2d4SBaptiste Daroussin add_order_collelem(collelem_t *e)
887057ca2d4SBaptiste Daroussin {
888057ca2d4SBaptiste Daroussin 	add_order_pri(e->ref[curr_weight]);
889057ca2d4SBaptiste Daroussin }
890057ca2d4SBaptiste Daroussin 
891057ca2d4SBaptiste Daroussin void
892057ca2d4SBaptiste Daroussin add_order_ignore(void)
893057ca2d4SBaptiste Daroussin {
894057ca2d4SBaptiste Daroussin 	add_order_pri(pri_ignore);
895057ca2d4SBaptiste Daroussin }
896057ca2d4SBaptiste Daroussin 
897057ca2d4SBaptiste Daroussin void
898057ca2d4SBaptiste Daroussin add_order_symbol(char *sym)
899057ca2d4SBaptiste Daroussin {
900057ca2d4SBaptiste Daroussin 	collundef_t *c;
901057ca2d4SBaptiste Daroussin 	if ((c = get_collundef(sym)) == NULL) {
902057ca2d4SBaptiste Daroussin 		INTERR;
903057ca2d4SBaptiste Daroussin 		return;
904057ca2d4SBaptiste Daroussin 	}
905057ca2d4SBaptiste Daroussin 	add_order_pri(c->ref[curr_weight]);
906057ca2d4SBaptiste Daroussin }
907057ca2d4SBaptiste Daroussin 
908057ca2d4SBaptiste Daroussin void
909057ca2d4SBaptiste Daroussin add_order_ellipsis(void)
910057ca2d4SBaptiste Daroussin {
911057ca2d4SBaptiste Daroussin 	/* special NULL value indicates self reference */
912057ca2d4SBaptiste Daroussin 	add_order_pri(0);
913057ca2d4SBaptiste Daroussin }
914057ca2d4SBaptiste Daroussin 
915057ca2d4SBaptiste Daroussin void
916057ca2d4SBaptiste Daroussin add_order_subst(void)
917057ca2d4SBaptiste Daroussin {
918057ca2d4SBaptiste Daroussin 	subst_t srch;
919057ca2d4SBaptiste Daroussin 	subst_t	*s;
920057ca2d4SBaptiste Daroussin 	int i;
921057ca2d4SBaptiste Daroussin 
922057ca2d4SBaptiste Daroussin 	(void) memset(&srch, 0, sizeof (srch));
923057ca2d4SBaptiste Daroussin 	for (i = 0; i < curr_subst; i++) {
924057ca2d4SBaptiste Daroussin 		srch.ref[i] = subst_weights[i];
925057ca2d4SBaptiste Daroussin 		subst_weights[i] = 0;
926057ca2d4SBaptiste Daroussin 	}
9273a444dbdSBaptiste Daroussin 	s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch);
928057ca2d4SBaptiste Daroussin 
929057ca2d4SBaptiste Daroussin 	if (s == NULL) {
930057ca2d4SBaptiste Daroussin 		if ((s = calloc(sizeof (*s), 1)) == NULL) {
931057ca2d4SBaptiste Daroussin 			fprintf(stderr,"out of memory");
932057ca2d4SBaptiste Daroussin 			return;
933057ca2d4SBaptiste Daroussin 		}
934057ca2d4SBaptiste Daroussin 		s->key = new_pri();
935057ca2d4SBaptiste Daroussin 
936057ca2d4SBaptiste Daroussin 		/*
937057ca2d4SBaptiste Daroussin 		 * We use a self reference for our key, but we set a
938057ca2d4SBaptiste Daroussin 		 * high bit to indicate that this is a substitution
939057ca2d4SBaptiste Daroussin 		 * reference.  This will expedite table lookups later,
940057ca2d4SBaptiste Daroussin 		 * and prevent table lookups for situations that don't
941057ca2d4SBaptiste Daroussin 		 * require it.  (In short, its a big win, because we
942057ca2d4SBaptiste Daroussin 		 * can skip a lot of binary searching.)
943057ca2d4SBaptiste Daroussin 		 */
944057ca2d4SBaptiste Daroussin 		set_pri(s->key,
945057ca2d4SBaptiste Daroussin 		    (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY),
946057ca2d4SBaptiste Daroussin 		    RESOLVED);
947057ca2d4SBaptiste Daroussin 		nextsubst[curr_weight] += 1;
948057ca2d4SBaptiste Daroussin 
949057ca2d4SBaptiste Daroussin 		for (i = 0; i < curr_subst; i++) {
950057ca2d4SBaptiste Daroussin 			s->ref[i] = srch.ref[i];
951057ca2d4SBaptiste Daroussin 		}
952057ca2d4SBaptiste Daroussin 
9533a444dbdSBaptiste Daroussin 		RB_INSERT(substs_ref, &substs_ref[curr_weight], s);
954057ca2d4SBaptiste Daroussin 
9553a444dbdSBaptiste Daroussin 		if (RB_FIND(substs, &substs[curr_weight], s) != NULL) {
956057ca2d4SBaptiste Daroussin 			INTERR;
957057ca2d4SBaptiste Daroussin 			return;
958057ca2d4SBaptiste Daroussin 		}
9593a444dbdSBaptiste Daroussin 		RB_INSERT(substs, &substs[curr_weight], s);
960057ca2d4SBaptiste Daroussin 	}
961057ca2d4SBaptiste Daroussin 	curr_subst = 0;
962057ca2d4SBaptiste Daroussin 
963057ca2d4SBaptiste Daroussin 
964057ca2d4SBaptiste Daroussin 	/*
965057ca2d4SBaptiste Daroussin 	 * We are using the current (unique) priority as a search key
966057ca2d4SBaptiste Daroussin 	 * in the substitution table.
967057ca2d4SBaptiste Daroussin 	 */
968057ca2d4SBaptiste Daroussin 	add_order_pri(s->key);
969057ca2d4SBaptiste Daroussin }
970057ca2d4SBaptiste Daroussin 
971057ca2d4SBaptiste Daroussin static void
972057ca2d4SBaptiste Daroussin add_subst_pri(int32_t ref)
973057ca2d4SBaptiste Daroussin {
974057ca2d4SBaptiste Daroussin 	if (curr_subst >= COLLATE_STR_LEN) {
975057ca2d4SBaptiste Daroussin 		fprintf(stderr,"substitution string is too long");
976057ca2d4SBaptiste Daroussin 		return;
977057ca2d4SBaptiste Daroussin 	}
978057ca2d4SBaptiste Daroussin 	subst_weights[curr_subst] = ref;
979057ca2d4SBaptiste Daroussin 	curr_subst++;
980057ca2d4SBaptiste Daroussin }
981057ca2d4SBaptiste Daroussin 
982057ca2d4SBaptiste Daroussin void
983057ca2d4SBaptiste Daroussin add_subst_char(wchar_t wc)
984057ca2d4SBaptiste Daroussin {
985057ca2d4SBaptiste Daroussin 	collchar_t *cc;
986057ca2d4SBaptiste Daroussin 
987057ca2d4SBaptiste Daroussin 
988057ca2d4SBaptiste Daroussin 	if (((cc = get_collchar(wc, 1)) == NULL) ||
989057ca2d4SBaptiste Daroussin 	    (cc->wc != wc)) {
990057ca2d4SBaptiste Daroussin 		INTERR;
991057ca2d4SBaptiste Daroussin 		return;
992057ca2d4SBaptiste Daroussin 	}
993057ca2d4SBaptiste Daroussin 	/* we take the weight for the character at that position */
994057ca2d4SBaptiste Daroussin 	add_subst_pri(cc->ref[curr_weight]);
995057ca2d4SBaptiste Daroussin }
996057ca2d4SBaptiste Daroussin 
997057ca2d4SBaptiste Daroussin void
998057ca2d4SBaptiste Daroussin add_subst_collelem(collelem_t *e)
999057ca2d4SBaptiste Daroussin {
1000057ca2d4SBaptiste Daroussin 	add_subst_pri(e->ref[curr_weight]);
1001057ca2d4SBaptiste Daroussin }
1002057ca2d4SBaptiste Daroussin 
1003057ca2d4SBaptiste Daroussin void
1004057ca2d4SBaptiste Daroussin add_subst_collsym(collsym_t *s)
1005057ca2d4SBaptiste Daroussin {
1006057ca2d4SBaptiste Daroussin 	add_subst_pri(s->ref);
1007057ca2d4SBaptiste Daroussin }
1008057ca2d4SBaptiste Daroussin 
1009057ca2d4SBaptiste Daroussin void
1010057ca2d4SBaptiste Daroussin add_subst_symbol(char *ptr)
1011057ca2d4SBaptiste Daroussin {
1012057ca2d4SBaptiste Daroussin 	collundef_t *cu;
1013057ca2d4SBaptiste Daroussin 
1014057ca2d4SBaptiste Daroussin 	if ((cu = get_collundef(ptr)) != NULL) {
1015057ca2d4SBaptiste Daroussin 		add_subst_pri(cu->ref[curr_weight]);
1016057ca2d4SBaptiste Daroussin 	}
1017057ca2d4SBaptiste Daroussin }
1018057ca2d4SBaptiste Daroussin 
1019057ca2d4SBaptiste Daroussin void
1020057ca2d4SBaptiste Daroussin add_weight(int32_t ref, int pass)
1021057ca2d4SBaptiste Daroussin {
1022057ca2d4SBaptiste Daroussin 	weight_t srch;
1023057ca2d4SBaptiste Daroussin 	weight_t *w;
1024057ca2d4SBaptiste Daroussin 
1025057ca2d4SBaptiste Daroussin 	srch.pri = resolve_pri(ref);
1026057ca2d4SBaptiste Daroussin 
1027057ca2d4SBaptiste Daroussin 	/* No translation of ignores */
1028057ca2d4SBaptiste Daroussin 	if (srch.pri == 0)
1029057ca2d4SBaptiste Daroussin 		return;
1030057ca2d4SBaptiste Daroussin 
1031057ca2d4SBaptiste Daroussin 	/* Substitution priorities are not weights */
1032057ca2d4SBaptiste Daroussin 	if (srch.pri & COLLATE_SUBST_PRIORITY)
1033057ca2d4SBaptiste Daroussin 		return;
1034057ca2d4SBaptiste Daroussin 
10353a444dbdSBaptiste Daroussin 	if (RB_FIND(weights, &weights[pass], &srch) != NULL)
1036057ca2d4SBaptiste Daroussin 		return;
1037057ca2d4SBaptiste Daroussin 
1038057ca2d4SBaptiste Daroussin 	if ((w = calloc(sizeof (*w), 1)) == NULL) {
1039057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1040057ca2d4SBaptiste Daroussin 		return;
1041057ca2d4SBaptiste Daroussin 	}
1042057ca2d4SBaptiste Daroussin 	w->pri = srch.pri;
10433a444dbdSBaptiste Daroussin 	RB_INSERT(weights, &weights[pass], w);
1044057ca2d4SBaptiste Daroussin }
1045057ca2d4SBaptiste Daroussin 
1046057ca2d4SBaptiste Daroussin void
1047057ca2d4SBaptiste Daroussin add_weights(int32_t *refs)
1048057ca2d4SBaptiste Daroussin {
1049057ca2d4SBaptiste Daroussin 	int i;
1050057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1051057ca2d4SBaptiste Daroussin 		add_weight(refs[i], i);
1052057ca2d4SBaptiste Daroussin 	}
1053057ca2d4SBaptiste Daroussin }
1054057ca2d4SBaptiste Daroussin 
1055057ca2d4SBaptiste Daroussin int32_t
1056057ca2d4SBaptiste Daroussin get_weight(int32_t ref, int pass)
1057057ca2d4SBaptiste Daroussin {
1058057ca2d4SBaptiste Daroussin 	weight_t	srch;
1059057ca2d4SBaptiste Daroussin 	weight_t	*w;
1060057ca2d4SBaptiste Daroussin 	int32_t		pri;
1061057ca2d4SBaptiste Daroussin 
1062057ca2d4SBaptiste Daroussin 	pri = resolve_pri(ref);
1063057ca2d4SBaptiste Daroussin 	if (pri & COLLATE_SUBST_PRIORITY) {
1064057ca2d4SBaptiste Daroussin 		return (pri);
1065057ca2d4SBaptiste Daroussin 	}
1066057ca2d4SBaptiste Daroussin 	if (pri <= 0) {
1067057ca2d4SBaptiste Daroussin 		return (pri);
1068057ca2d4SBaptiste Daroussin 	}
1069057ca2d4SBaptiste Daroussin 	srch.pri = pri;
10703a444dbdSBaptiste Daroussin 	if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) {
1071057ca2d4SBaptiste Daroussin 		INTERR;
1072057ca2d4SBaptiste Daroussin 		return (-1);
1073057ca2d4SBaptiste Daroussin 	}
1074057ca2d4SBaptiste Daroussin 	return (w->opt);
1075057ca2d4SBaptiste Daroussin }
1076057ca2d4SBaptiste Daroussin 
1077057ca2d4SBaptiste Daroussin wchar_t *
1078057ca2d4SBaptiste Daroussin wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
1079057ca2d4SBaptiste Daroussin {
1080057ca2d4SBaptiste Daroussin 	wchar_t *os1 = s1;
1081057ca2d4SBaptiste Daroussin 
1082057ca2d4SBaptiste Daroussin 	n++;
1083057ca2d4SBaptiste Daroussin 	while (--n > 0 && (*s1++ = *s2++) != 0)
1084057ca2d4SBaptiste Daroussin 		continue;
1085057ca2d4SBaptiste Daroussin 	if (n > 0)
1086057ca2d4SBaptiste Daroussin 		while (--n > 0)
1087057ca2d4SBaptiste Daroussin 			*s1++ = 0;
1088057ca2d4SBaptiste Daroussin 	return (os1);
1089057ca2d4SBaptiste Daroussin }
1090057ca2d4SBaptiste Daroussin 
109123a32822SBaptiste Daroussin #define RB_COUNT(x, name, head, cnt) do { \
109223a32822SBaptiste Daroussin 	(cnt) = 0; \
109323a32822SBaptiste Daroussin 	RB_FOREACH(x, name, (head)) { \
109423a32822SBaptiste Daroussin 		(cnt)++; \
109523a32822SBaptiste Daroussin 	} \
109623a32822SBaptiste Daroussin } while (0)
109723a32822SBaptiste Daroussin 
10983a444dbdSBaptiste Daroussin #define RB_NUMNODES(type, name, head, cnt) do { \
10993a444dbdSBaptiste Daroussin 	type *t; \
11003a444dbdSBaptiste Daroussin 	cnt = 0; \
11013a444dbdSBaptiste Daroussin 	RB_FOREACH(t, name, head) { \
11023a444dbdSBaptiste Daroussin 		cnt++; \
11033a444dbdSBaptiste Daroussin 	} \
110423a32822SBaptiste Daroussin } while (0)
11053a444dbdSBaptiste Daroussin 
1106057ca2d4SBaptiste Daroussin void
1107057ca2d4SBaptiste Daroussin dump_collate(void)
1108057ca2d4SBaptiste Daroussin {
1109057ca2d4SBaptiste Daroussin 	FILE			*f;
1110057ca2d4SBaptiste Daroussin 	int			i, j, n;
1111057ca2d4SBaptiste Daroussin 	size_t			sz;
1112057ca2d4SBaptiste Daroussin 	int32_t			pri;
1113057ca2d4SBaptiste Daroussin 	collelem_t		*ce;
1114057ca2d4SBaptiste Daroussin 	collchar_t		*cc;
1115057ca2d4SBaptiste Daroussin 	subst_t			*sb;
1116057ca2d4SBaptiste Daroussin 	char			vers[COLLATE_STR_LEN];
1117057ca2d4SBaptiste Daroussin 	collate_char_t		chars[UCHAR_MAX + 1];
1118057ca2d4SBaptiste Daroussin 	collate_large_t		*large;
1119057ca2d4SBaptiste Daroussin 	collate_subst_t		*subst[COLL_WEIGHTS_MAX];
1120057ca2d4SBaptiste Daroussin 	collate_chain_t		*chain;
1121057ca2d4SBaptiste Daroussin 
1122057ca2d4SBaptiste Daroussin 	/*
1123*fcc7baa1SPedro F. Giffuni 	 * We have to run through a preliminary pass to identify all the
1124057ca2d4SBaptiste Daroussin 	 * weights that we use for each sorting level.
1125057ca2d4SBaptiste Daroussin 	 */
1126057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1127057ca2d4SBaptiste Daroussin 		add_weight(pri_ignore, i);
1128057ca2d4SBaptiste Daroussin 	}
1129057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
11303a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1131057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1132057ca2d4SBaptiste Daroussin 				add_weight(sb->ref[j], i);
1133057ca2d4SBaptiste Daroussin 			}
1134057ca2d4SBaptiste Daroussin 		}
1135057ca2d4SBaptiste Daroussin 	}
11363a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1137057ca2d4SBaptiste Daroussin 		add_weights(ce->ref);
1138057ca2d4SBaptiste Daroussin 	}
11393a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1140057ca2d4SBaptiste Daroussin 		add_weights(cc->ref);
1141057ca2d4SBaptiste Daroussin 	}
1142057ca2d4SBaptiste Daroussin 
1143057ca2d4SBaptiste Daroussin 	/*
1144057ca2d4SBaptiste Daroussin 	 * Now we walk the entire set of weights, removing the gaps
1145057ca2d4SBaptiste Daroussin 	 * in the weights.  This gives us optimum usage.  The walk
1146057ca2d4SBaptiste Daroussin 	 * occurs in priority.
1147057ca2d4SBaptiste Daroussin 	 */
1148057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1149057ca2d4SBaptiste Daroussin 		weight_t *w;
11503a444dbdSBaptiste Daroussin 		RB_FOREACH(w, weights, &weights[i]) {
1151057ca2d4SBaptiste Daroussin 			w->opt = nweight[i];
1152057ca2d4SBaptiste Daroussin 			nweight[i] += 1;
1153057ca2d4SBaptiste Daroussin 		}
1154057ca2d4SBaptiste Daroussin 	}
1155057ca2d4SBaptiste Daroussin 
1156057ca2d4SBaptiste Daroussin 	(void) memset(&chars, 0, sizeof (chars));
1157057ca2d4SBaptiste Daroussin 	(void) memset(vers, 0, COLLATE_STR_LEN);
1158057ca2d4SBaptiste Daroussin 	(void) strlcpy(vers, COLLATE_VERSION, sizeof (vers));
1159057ca2d4SBaptiste Daroussin 
1160057ca2d4SBaptiste Daroussin 	/*
1161057ca2d4SBaptiste Daroussin 	 * We need to make sure we arrange for the UNDEFINED field
1162057ca2d4SBaptiste Daroussin 	 * to show up.  Also, set the total weight counts.
1163057ca2d4SBaptiste Daroussin 	 */
1164057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1165057ca2d4SBaptiste Daroussin 		if (resolve_pri(pri_undefined[i]) == -1) {
1166057ca2d4SBaptiste Daroussin 			set_pri(pri_undefined[i], -1, RESOLVED);
1167057ca2d4SBaptiste Daroussin 			/* they collate at the end of everything else */
1168057ca2d4SBaptiste Daroussin 			collinfo.undef_pri[i] = COLLATE_MAX_PRIORITY;
1169057ca2d4SBaptiste Daroussin 		}
1170057ca2d4SBaptiste Daroussin 		collinfo.pri_count[i] = nweight[i];
1171057ca2d4SBaptiste Daroussin 	}
1172057ca2d4SBaptiste Daroussin 
1173057ca2d4SBaptiste Daroussin 	collinfo.pri_count[NUM_WT] = max_wide();
1174057ca2d4SBaptiste Daroussin 	collinfo.undef_pri[NUM_WT] = COLLATE_MAX_PRIORITY;
1175057ca2d4SBaptiste Daroussin 	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
1176057ca2d4SBaptiste Daroussin 
1177057ca2d4SBaptiste Daroussin 	/*
1178057ca2d4SBaptiste Daroussin 	 * Ordinary character priorities
1179057ca2d4SBaptiste Daroussin 	 */
1180057ca2d4SBaptiste Daroussin 	for (i = 0; i <= UCHAR_MAX; i++) {
1181057ca2d4SBaptiste Daroussin 		if ((cc = get_collchar(i, 0)) != NULL) {
1182057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1183057ca2d4SBaptiste Daroussin 				chars[i].pri[j] = get_weight(cc->ref[j], j);
1184057ca2d4SBaptiste Daroussin 			}
1185057ca2d4SBaptiste Daroussin 		} else {
1186057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1187057ca2d4SBaptiste Daroussin 				chars[i].pri[j] =
1188057ca2d4SBaptiste Daroussin 				    get_weight(pri_undefined[j], j);
1189057ca2d4SBaptiste Daroussin 			}
1190057ca2d4SBaptiste Daroussin 			/*
1191057ca2d4SBaptiste Daroussin 			 * Per POSIX, for undefined characters, we
1192057ca2d4SBaptiste Daroussin 			 * also have to add a last item, which is the
1193057ca2d4SBaptiste Daroussin 			 * character code.
1194057ca2d4SBaptiste Daroussin 			 */
1195057ca2d4SBaptiste Daroussin 			chars[i].pri[NUM_WT] = i;
1196057ca2d4SBaptiste Daroussin 		}
1197057ca2d4SBaptiste Daroussin 	}
1198057ca2d4SBaptiste Daroussin 
1199057ca2d4SBaptiste Daroussin 	/*
1200057ca2d4SBaptiste Daroussin 	 * Substitution tables
1201057ca2d4SBaptiste Daroussin 	 */
1202057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1203057ca2d4SBaptiste Daroussin 		collate_subst_t *st = NULL;
120423a32822SBaptiste Daroussin 		subst_t *temp;
120523a32822SBaptiste Daroussin 		RB_COUNT(temp, substs, &substs[i], n);
12063a444dbdSBaptiste Daroussin 		collinfo.subst_count[i] = n;
1207057ca2d4SBaptiste Daroussin 		if ((st = calloc(sizeof (collate_subst_t) * n, 1)) == NULL) {
1208057ca2d4SBaptiste Daroussin 			fprintf(stderr, "out of memory");
1209057ca2d4SBaptiste Daroussin 			return;
1210057ca2d4SBaptiste Daroussin 		}
1211057ca2d4SBaptiste Daroussin 		n = 0;
12123a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1213057ca2d4SBaptiste Daroussin 			if ((st[n].key = resolve_pri(sb->key)) < 0) {
1214057ca2d4SBaptiste Daroussin 				/* by definition these resolve! */
1215057ca2d4SBaptiste Daroussin 				INTERR;
1216057ca2d4SBaptiste Daroussin 			}
1217057ca2d4SBaptiste Daroussin 			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
1218057ca2d4SBaptiste Daroussin 				INTERR;
1219057ca2d4SBaptiste Daroussin 			}
1220057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1221057ca2d4SBaptiste Daroussin 				st[n].pri[j] = get_weight(sb->ref[j], i);
1222057ca2d4SBaptiste Daroussin 			}
1223057ca2d4SBaptiste Daroussin 			n++;
1224057ca2d4SBaptiste Daroussin 		}
1225057ca2d4SBaptiste Daroussin 		if (n != collinfo.subst_count[i])
1226057ca2d4SBaptiste Daroussin 			INTERR;
1227057ca2d4SBaptiste Daroussin 		subst[i] = st;
1228057ca2d4SBaptiste Daroussin 	}
1229057ca2d4SBaptiste Daroussin 
1230057ca2d4SBaptiste Daroussin 
1231057ca2d4SBaptiste Daroussin 	/*
1232057ca2d4SBaptiste Daroussin 	 * Chains, i.e. collating elements
1233057ca2d4SBaptiste Daroussin 	 */
12343a444dbdSBaptiste Daroussin 	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand,
12353a444dbdSBaptiste Daroussin 	    collinfo.chain_count);
1236057ca2d4SBaptiste Daroussin 	chain = calloc(sizeof (collate_chain_t), collinfo.chain_count);
1237057ca2d4SBaptiste Daroussin 	if (chain == NULL) {
1238057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1239057ca2d4SBaptiste Daroussin 		return;
1240057ca2d4SBaptiste Daroussin 	}
12413a444dbdSBaptiste Daroussin 	n = 0;
12423a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1243057ca2d4SBaptiste Daroussin 		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
1244057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
1245057ca2d4SBaptiste Daroussin 			chain[n].pri[i] = get_weight(ce->ref[i], i);
1246057ca2d4SBaptiste Daroussin 		}
12475d21db09SBaptiste Daroussin 		n++;
1248057ca2d4SBaptiste Daroussin 	}
1249057ca2d4SBaptiste Daroussin 	if (n != collinfo.chain_count)
1250057ca2d4SBaptiste Daroussin 		INTERR;
1251057ca2d4SBaptiste Daroussin 
1252057ca2d4SBaptiste Daroussin 	/*
1253057ca2d4SBaptiste Daroussin 	 * Large (> UCHAR_MAX) character priorities
1254057ca2d4SBaptiste Daroussin 	 */
12553a444dbdSBaptiste Daroussin 	RB_NUMNODES(collchar_t, collchars, &collchars, n);
12565d21db09SBaptiste Daroussin 	large = calloc(n, sizeof (collate_large_t));
1257057ca2d4SBaptiste Daroussin 	if (large == NULL) {
1258057ca2d4SBaptiste Daroussin 		fprintf(stderr, "out of memory");
1259057ca2d4SBaptiste Daroussin 		return;
1260057ca2d4SBaptiste Daroussin 	}
1261057ca2d4SBaptiste Daroussin 
1262057ca2d4SBaptiste Daroussin 	i = 0;
12633a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1264057ca2d4SBaptiste Daroussin 		int	undef = 0;
1265057ca2d4SBaptiste Daroussin 		/* we already gathered those */
1266057ca2d4SBaptiste Daroussin 		if (cc->wc <= UCHAR_MAX)
1267057ca2d4SBaptiste Daroussin 			continue;
1268057ca2d4SBaptiste Daroussin 		for (j = 0; j < NUM_WT; j++) {
1269057ca2d4SBaptiste Daroussin 			if ((pri = get_weight(cc->ref[j], j)) < 0) {
1270057ca2d4SBaptiste Daroussin 				undef = 1;
1271057ca2d4SBaptiste Daroussin 			}
1272057ca2d4SBaptiste Daroussin 			if (undef && (pri >= 0)) {
1273057ca2d4SBaptiste Daroussin 				/* if undefined, then all priorities are */
1274057ca2d4SBaptiste Daroussin 				INTERR;
1275057ca2d4SBaptiste Daroussin 			} else {
1276057ca2d4SBaptiste Daroussin 				large[i].pri.pri[j] = pri;
1277057ca2d4SBaptiste Daroussin 			}
1278057ca2d4SBaptiste Daroussin 		}
1279057ca2d4SBaptiste Daroussin 		if (!undef) {
1280057ca2d4SBaptiste Daroussin 			large[i].val = cc->wc;
1281057ca2d4SBaptiste Daroussin 			collinfo.large_count = i++;
1282057ca2d4SBaptiste Daroussin 		}
1283057ca2d4SBaptiste Daroussin 	}
1284057ca2d4SBaptiste Daroussin 
1285057ca2d4SBaptiste Daroussin 	if ((f = open_category()) == NULL) {
1286057ca2d4SBaptiste Daroussin 		return;
1287057ca2d4SBaptiste Daroussin 	}
1288057ca2d4SBaptiste Daroussin 
1289057ca2d4SBaptiste Daroussin 	/* Time to write the entire data set out */
1290057ca2d4SBaptiste Daroussin 
1291057ca2d4SBaptiste Daroussin 	if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) ||
1292057ca2d4SBaptiste Daroussin 	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
1293057ca2d4SBaptiste Daroussin 	    (wr_category(&chars, sizeof (chars), f) < 0)) {
1294057ca2d4SBaptiste Daroussin 		return;
1295057ca2d4SBaptiste Daroussin 	}
1296057ca2d4SBaptiste Daroussin 
1297057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1298057ca2d4SBaptiste Daroussin 		sz =  sizeof (collate_subst_t) * collinfo.subst_count[i];
1299057ca2d4SBaptiste Daroussin 		if (wr_category(subst[i], sz, f) < 0) {
1300057ca2d4SBaptiste Daroussin 			return;
1301057ca2d4SBaptiste Daroussin 		}
1302057ca2d4SBaptiste Daroussin 	}
1303057ca2d4SBaptiste Daroussin 	sz = sizeof (collate_chain_t) * collinfo.chain_count;
1304057ca2d4SBaptiste Daroussin 	if (wr_category(chain, sz, f) < 0) {
1305057ca2d4SBaptiste Daroussin 		return;
1306057ca2d4SBaptiste Daroussin 	}
1307057ca2d4SBaptiste Daroussin 	sz = sizeof (collate_large_t) * collinfo.large_count;
1308057ca2d4SBaptiste Daroussin 	if (wr_category(large, sz, f) < 0) {
1309057ca2d4SBaptiste Daroussin 		return;
1310057ca2d4SBaptiste Daroussin 	}
1311057ca2d4SBaptiste Daroussin 
1312057ca2d4SBaptiste Daroussin 	close_category(f);
1313057ca2d4SBaptiste Daroussin }
1314