xref: /freebsd/usr.bin/localedef/collate.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
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  */
34*5e3934b1SWarner Losh 
35057ca2d4SBaptiste Daroussin #include <sys/types.h>
363a444dbdSBaptiste Daroussin #include <sys/tree.h>
37057ca2d4SBaptiste Daroussin 
38057ca2d4SBaptiste Daroussin #include <stdio.h>
39057ca2d4SBaptiste Daroussin #include <stddef.h>
40057ca2d4SBaptiste Daroussin #include <stdlib.h>
41057ca2d4SBaptiste Daroussin #include <errno.h>
42057ca2d4SBaptiste Daroussin #include <string.h>
43057ca2d4SBaptiste Daroussin #include <unistd.h>
44057ca2d4SBaptiste Daroussin #include <wchar.h>
45057ca2d4SBaptiste Daroussin #include <limits.h>
46057ca2d4SBaptiste Daroussin #include "localedef.h"
47057ca2d4SBaptiste Daroussin #include "parser.h"
48057ca2d4SBaptiste Daroussin #include "collate.h"
49057ca2d4SBaptiste Daroussin 
5000c61a3bSAlex Richardson _Static_assert(COLL_WEIGHTS_MAX == 10, "This code assumes a value of 10");
5100c61a3bSAlex Richardson 
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;
2564644f9beSYuri Pankov static int32_t subst_count[COLL_WEIGHTS_MAX];
2574644f9beSYuri Pankov static int32_t chain_count;
2584644f9beSYuri Pankov static int32_t large_count;
259057ca2d4SBaptiste Daroussin 
260057ca2d4SBaptiste Daroussin static collpri_t	*prilist = NULL;
261057ca2d4SBaptiste Daroussin static int		numpri = 0;
262057ca2d4SBaptiste Daroussin static int		maxpri = 0;
263057ca2d4SBaptiste Daroussin 
264057ca2d4SBaptiste Daroussin static void start_order(int);
265057ca2d4SBaptiste Daroussin 
266057ca2d4SBaptiste Daroussin static int32_t
new_pri(void)267057ca2d4SBaptiste Daroussin new_pri(void)
268057ca2d4SBaptiste Daroussin {
269057ca2d4SBaptiste Daroussin 	int i;
270057ca2d4SBaptiste Daroussin 
271057ca2d4SBaptiste Daroussin 	if (numpri >= maxpri) {
272057ca2d4SBaptiste Daroussin 		maxpri = maxpri ? maxpri * 2 : 1024;
273057ca2d4SBaptiste Daroussin 		prilist = realloc(prilist, sizeof (collpri_t) * maxpri);
274057ca2d4SBaptiste Daroussin 		if (prilist == NULL) {
2753141e51dSKyle Evans 			fprintf(stderr,"out of memory\n");
276057ca2d4SBaptiste Daroussin 			return (-1);
277057ca2d4SBaptiste Daroussin 		}
278057ca2d4SBaptiste Daroussin 		for (i = numpri; i < maxpri; i++) {
279057ca2d4SBaptiste Daroussin 			prilist[i].res = UNKNOWN;
280057ca2d4SBaptiste Daroussin 			prilist[i].pri = 0;
281057ca2d4SBaptiste Daroussin 			prilist[i].pass = 0;
282057ca2d4SBaptiste Daroussin 		}
283057ca2d4SBaptiste Daroussin 	}
284057ca2d4SBaptiste Daroussin 	return (numpri++);
285057ca2d4SBaptiste Daroussin }
286057ca2d4SBaptiste Daroussin 
287057ca2d4SBaptiste Daroussin static collpri_t *
get_pri(int32_t ref)288057ca2d4SBaptiste Daroussin get_pri(int32_t ref)
289057ca2d4SBaptiste Daroussin {
290057ca2d4SBaptiste Daroussin 	if ((ref < 0) || (ref > numpri)) {
291057ca2d4SBaptiste Daroussin 		INTERR;
292057ca2d4SBaptiste Daroussin 		return (NULL);
293057ca2d4SBaptiste Daroussin 	}
294057ca2d4SBaptiste Daroussin 	return (&prilist[ref]);
295057ca2d4SBaptiste Daroussin }
296057ca2d4SBaptiste Daroussin 
297057ca2d4SBaptiste Daroussin static void
set_pri(int32_t ref,int32_t v,res_t res)298057ca2d4SBaptiste Daroussin set_pri(int32_t ref, int32_t v, res_t res)
299057ca2d4SBaptiste Daroussin {
300057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
301057ca2d4SBaptiste Daroussin 
302057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
303057ca2d4SBaptiste Daroussin 
304057ca2d4SBaptiste Daroussin 	if ((res == REFER) && ((v < 0) || (v >= numpri))) {
305057ca2d4SBaptiste Daroussin 		INTERR;
306057ca2d4SBaptiste Daroussin 	}
307057ca2d4SBaptiste Daroussin 
308057ca2d4SBaptiste Daroussin 	/* Resolve self references */
309057ca2d4SBaptiste Daroussin 	if ((res == REFER) && (ref == v)) {
310057ca2d4SBaptiste Daroussin 		v = nextpri;
311057ca2d4SBaptiste Daroussin 		res = RESOLVED;
312057ca2d4SBaptiste Daroussin 	}
313057ca2d4SBaptiste Daroussin 
314057ca2d4SBaptiste Daroussin 	if (pri->res != UNKNOWN) {
315057ca2d4SBaptiste Daroussin 		warn("repeated item in order list (first on %d)",
316057ca2d4SBaptiste Daroussin 		    pri->lineno);
317057ca2d4SBaptiste Daroussin 		return;
318057ca2d4SBaptiste Daroussin 	}
319057ca2d4SBaptiste Daroussin 	pri->lineno = lineno;
320057ca2d4SBaptiste Daroussin 	pri->pri = v;
321057ca2d4SBaptiste Daroussin 	pri->res = res;
322057ca2d4SBaptiste Daroussin }
323057ca2d4SBaptiste Daroussin 
324057ca2d4SBaptiste Daroussin static int32_t
resolve_pri(int32_t ref)325057ca2d4SBaptiste Daroussin resolve_pri(int32_t ref)
326057ca2d4SBaptiste Daroussin {
327057ca2d4SBaptiste Daroussin 	collpri_t	*pri;
328057ca2d4SBaptiste Daroussin 	static int32_t	pass = 0;
329057ca2d4SBaptiste Daroussin 
330057ca2d4SBaptiste Daroussin 	pri = get_pri(ref);
331057ca2d4SBaptiste Daroussin 	pass++;
332057ca2d4SBaptiste Daroussin 	while (pri->res == REFER) {
333057ca2d4SBaptiste Daroussin 		if (pri->pass == pass) {
334057ca2d4SBaptiste Daroussin 			/* report a line with the circular symbol */
335057ca2d4SBaptiste Daroussin 			lineno = pri->lineno;
3363141e51dSKyle Evans 			fprintf(stderr,"circular reference in order list\n");
337057ca2d4SBaptiste Daroussin 			return (-1);
338057ca2d4SBaptiste Daroussin 		}
339057ca2d4SBaptiste Daroussin 		if ((pri->pri < 0) || (pri->pri >= numpri)) {
340057ca2d4SBaptiste Daroussin 			INTERR;
341057ca2d4SBaptiste Daroussin 			return (-1);
342057ca2d4SBaptiste Daroussin 		}
343057ca2d4SBaptiste Daroussin 		pri->pass = pass;
344057ca2d4SBaptiste Daroussin 		pri = &prilist[pri->pri];
345057ca2d4SBaptiste Daroussin 	}
346057ca2d4SBaptiste Daroussin 
347057ca2d4SBaptiste Daroussin 	if (pri->res == UNKNOWN) {
348057ca2d4SBaptiste Daroussin 		return (-1);
349057ca2d4SBaptiste Daroussin 	}
350057ca2d4SBaptiste Daroussin 	if (pri->res != RESOLVED)
351057ca2d4SBaptiste Daroussin 		INTERR;
352057ca2d4SBaptiste Daroussin 
353057ca2d4SBaptiste Daroussin 	return (pri->pri);
354057ca2d4SBaptiste Daroussin }
355057ca2d4SBaptiste Daroussin 
356057ca2d4SBaptiste Daroussin static int
weight_compare(const void * n1,const void * n2)357057ca2d4SBaptiste Daroussin weight_compare(const void *n1, const void *n2)
358057ca2d4SBaptiste Daroussin {
359057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const weight_t *)n1)->pri;
360057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const weight_t *)n2)->pri;
361057ca2d4SBaptiste Daroussin 
362057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
363057ca2d4SBaptiste Daroussin }
364057ca2d4SBaptiste Daroussin 
3653a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(weights, weight, entry, weight_compare);
3663a444dbdSBaptiste Daroussin 
367057ca2d4SBaptiste Daroussin static int
collsym_compare(const void * n1,const void * n2)368057ca2d4SBaptiste Daroussin collsym_compare(const void *n1, const void *n2)
369057ca2d4SBaptiste Daroussin {
370057ca2d4SBaptiste Daroussin 	const collsym_t *c1 = n1;
371057ca2d4SBaptiste Daroussin 	const collsym_t *c2 = n2;
372057ca2d4SBaptiste Daroussin 	int rv;
373057ca2d4SBaptiste Daroussin 
374057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
375057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
376057ca2d4SBaptiste Daroussin }
377057ca2d4SBaptiste Daroussin 
3783a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collsyms, collsym, entry, collsym_compare);
3793a444dbdSBaptiste Daroussin 
380057ca2d4SBaptiste Daroussin static int
collundef_compare(const void * n1,const void * n2)381057ca2d4SBaptiste Daroussin collundef_compare(const void *n1, const void *n2)
382057ca2d4SBaptiste Daroussin {
383057ca2d4SBaptiste Daroussin 	const collundef_t *c1 = n1;
384057ca2d4SBaptiste Daroussin 	const collundef_t *c2 = n2;
385057ca2d4SBaptiste Daroussin 	int rv;
386057ca2d4SBaptiste Daroussin 
387057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->name, c2->name);
388057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
389057ca2d4SBaptiste Daroussin }
390057ca2d4SBaptiste Daroussin 
3913a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collundefs, collundef, entry, collundef_compare);
3923a444dbdSBaptiste Daroussin 
393057ca2d4SBaptiste Daroussin static int
element_compare_symbol(const void * n1,const void * n2)394057ca2d4SBaptiste Daroussin element_compare_symbol(const void *n1, const void *n2)
395057ca2d4SBaptiste Daroussin {
396057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
397057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
398057ca2d4SBaptiste Daroussin 	int rv;
399057ca2d4SBaptiste Daroussin 
400057ca2d4SBaptiste Daroussin 	rv = strcmp(c1->symbol, c2->symbol);
401057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
402057ca2d4SBaptiste Daroussin }
403057ca2d4SBaptiste Daroussin 
4043a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_symbol, collelem, rb_bysymbol, element_compare_symbol);
4053a444dbdSBaptiste Daroussin 
406057ca2d4SBaptiste Daroussin static int
element_compare_expand(const void * n1,const void * n2)407057ca2d4SBaptiste Daroussin element_compare_expand(const void *n1, const void *n2)
408057ca2d4SBaptiste Daroussin {
409057ca2d4SBaptiste Daroussin 	const collelem_t *c1 = n1;
410057ca2d4SBaptiste Daroussin 	const collelem_t *c2 = n2;
411057ca2d4SBaptiste Daroussin 	int rv;
412057ca2d4SBaptiste Daroussin 
413057ca2d4SBaptiste Daroussin 	rv = wcscmp(c1->expand, c2->expand);
414057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
415057ca2d4SBaptiste Daroussin }
416057ca2d4SBaptiste Daroussin 
4173a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(elem_by_expand, collelem, rb_byexpand, element_compare_expand);
4183a444dbdSBaptiste Daroussin 
419057ca2d4SBaptiste Daroussin static int
collchar_compare(const void * n1,const void * n2)420057ca2d4SBaptiste Daroussin collchar_compare(const void *n1, const void *n2)
421057ca2d4SBaptiste Daroussin {
422057ca2d4SBaptiste Daroussin 	wchar_t	k1 = ((const collchar_t *)n1)->wc;
423057ca2d4SBaptiste Daroussin 	wchar_t	k2 = ((const collchar_t *)n2)->wc;
424057ca2d4SBaptiste Daroussin 
425057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
426057ca2d4SBaptiste Daroussin }
427057ca2d4SBaptiste Daroussin 
4283a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(collchars, collchar, entry, collchar_compare);
4293a444dbdSBaptiste Daroussin 
430057ca2d4SBaptiste Daroussin static int
subst_compare(const void * n1,const void * n2)431057ca2d4SBaptiste Daroussin subst_compare(const void *n1, const void *n2)
432057ca2d4SBaptiste Daroussin {
433057ca2d4SBaptiste Daroussin 	int32_t	k1 = ((const subst_t *)n1)->key;
434057ca2d4SBaptiste Daroussin 	int32_t	k2 = ((const subst_t *)n2)->key;
435057ca2d4SBaptiste Daroussin 
436057ca2d4SBaptiste Daroussin 	return (k1 < k2 ? -1 : k1 > k2 ? 1 : 0);
437057ca2d4SBaptiste Daroussin }
438057ca2d4SBaptiste Daroussin 
4393a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs, subst, entry, subst_compare);
4403a444dbdSBaptiste Daroussin 
441057ca2d4SBaptiste Daroussin static int
subst_compare_ref(const void * n1,const void * n2)442057ca2d4SBaptiste Daroussin subst_compare_ref(const void *n1, const void *n2)
443057ca2d4SBaptiste Daroussin {
44455b270e6SBaptiste Daroussin 	const wchar_t *c1 = ((const subst_t *)n1)->ref;
44555b270e6SBaptiste Daroussin 	const wchar_t *c2 = ((const subst_t *)n2)->ref;
446057ca2d4SBaptiste Daroussin 	int rv;
447057ca2d4SBaptiste Daroussin 
44855b270e6SBaptiste Daroussin 	rv = wcscmp(c1, c2);
449057ca2d4SBaptiste Daroussin 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
450057ca2d4SBaptiste Daroussin }
451057ca2d4SBaptiste Daroussin 
4523a444dbdSBaptiste Daroussin RB_GENERATE_STATIC(substs_ref, subst, entry_ref, subst_compare_ref);
4533a444dbdSBaptiste Daroussin 
454057ca2d4SBaptiste Daroussin void
init_collate(void)455057ca2d4SBaptiste Daroussin init_collate(void)
456057ca2d4SBaptiste Daroussin {
457057ca2d4SBaptiste Daroussin 	int i;
458057ca2d4SBaptiste Daroussin 
4593a444dbdSBaptiste Daroussin 	RB_INIT(&collsyms);
460057ca2d4SBaptiste Daroussin 
4613a444dbdSBaptiste Daroussin 	RB_INIT(&collundefs);
462057ca2d4SBaptiste Daroussin 
4633a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_symbol);
464057ca2d4SBaptiste Daroussin 
4653a444dbdSBaptiste Daroussin 	RB_INIT(&elem_by_expand);
4663a444dbdSBaptiste Daroussin 
4673a444dbdSBaptiste Daroussin 	RB_INIT(&collchars);
468057ca2d4SBaptiste Daroussin 
469057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
4703a444dbdSBaptiste Daroussin 		RB_INIT(&substs[i]);
4713a444dbdSBaptiste Daroussin 		RB_INIT(&substs_ref[i]);
4723a444dbdSBaptiste Daroussin 		RB_INIT(&weights[i]);
473057ca2d4SBaptiste Daroussin 		nweight[i] = 1;
474057ca2d4SBaptiste Daroussin 	}
475057ca2d4SBaptiste Daroussin 
476057ca2d4SBaptiste Daroussin 	(void) memset(&collinfo, 0, sizeof (collinfo));
477057ca2d4SBaptiste Daroussin 
478057ca2d4SBaptiste Daroussin 	/* allocate some initial priorities */
479057ca2d4SBaptiste Daroussin 	pri_ignore = new_pri();
480057ca2d4SBaptiste Daroussin 
481057ca2d4SBaptiste Daroussin 	set_pri(pri_ignore, 0, RESOLVED);
482057ca2d4SBaptiste Daroussin 
483057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
484057ca2d4SBaptiste Daroussin 		pri_undefined[i] = new_pri();
485057ca2d4SBaptiste Daroussin 
486057ca2d4SBaptiste Daroussin 		/* we will override this later */
487057ca2d4SBaptiste Daroussin 		set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN);
488057ca2d4SBaptiste Daroussin 	}
489057ca2d4SBaptiste Daroussin }
490057ca2d4SBaptiste Daroussin 
491057ca2d4SBaptiste Daroussin void
define_collsym(char * name)492057ca2d4SBaptiste Daroussin define_collsym(char *name)
493057ca2d4SBaptiste Daroussin {
494057ca2d4SBaptiste Daroussin 	collsym_t	*sym;
495057ca2d4SBaptiste Daroussin 
4964c22fda9SMarcelo Araujo 	if ((sym = calloc(1, sizeof(*sym))) == NULL) {
4973141e51dSKyle Evans 		fprintf(stderr,"out of memory\n");
498057ca2d4SBaptiste Daroussin 		return;
499057ca2d4SBaptiste Daroussin 	}
500057ca2d4SBaptiste Daroussin 	sym->name = name;
501057ca2d4SBaptiste Daroussin 	sym->ref = new_pri();
502057ca2d4SBaptiste Daroussin 
5033a444dbdSBaptiste Daroussin 	if (RB_FIND(collsyms, &collsyms, sym) != NULL) {
504057ca2d4SBaptiste Daroussin 		/*
505057ca2d4SBaptiste Daroussin 		 * This should never happen because we are only called
506057ca2d4SBaptiste Daroussin 		 * for undefined symbols.
507057ca2d4SBaptiste Daroussin 		 */
508e6d8c0e2SBaptiste Daroussin 		free(sym);
509057ca2d4SBaptiste Daroussin 		INTERR;
510057ca2d4SBaptiste Daroussin 		return;
511057ca2d4SBaptiste Daroussin 	}
5123a444dbdSBaptiste Daroussin 	RB_INSERT(collsyms, &collsyms, sym);
513057ca2d4SBaptiste Daroussin }
514057ca2d4SBaptiste Daroussin 
515057ca2d4SBaptiste Daroussin collsym_t *
lookup_collsym(char * name)516057ca2d4SBaptiste Daroussin lookup_collsym(char *name)
517057ca2d4SBaptiste Daroussin {
518057ca2d4SBaptiste Daroussin 	collsym_t	srch;
519057ca2d4SBaptiste Daroussin 
520057ca2d4SBaptiste Daroussin 	srch.name = name;
5213a444dbdSBaptiste Daroussin 	return (RB_FIND(collsyms, &collsyms, &srch));
522057ca2d4SBaptiste Daroussin }
523057ca2d4SBaptiste Daroussin 
524057ca2d4SBaptiste Daroussin collelem_t *
lookup_collelem(char * symbol)525057ca2d4SBaptiste Daroussin lookup_collelem(char *symbol)
526057ca2d4SBaptiste Daroussin {
527057ca2d4SBaptiste Daroussin 	collelem_t	srch;
528057ca2d4SBaptiste Daroussin 
529057ca2d4SBaptiste Daroussin 	srch.symbol = symbol;
5303a444dbdSBaptiste Daroussin 	return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch));
531057ca2d4SBaptiste Daroussin }
532057ca2d4SBaptiste Daroussin 
533057ca2d4SBaptiste Daroussin static collundef_t *
get_collundef(char * name)534057ca2d4SBaptiste Daroussin get_collundef(char *name)
535057ca2d4SBaptiste Daroussin {
536057ca2d4SBaptiste Daroussin 	collundef_t	srch;
537057ca2d4SBaptiste Daroussin 	collundef_t	*ud;
538057ca2d4SBaptiste Daroussin 	int		i;
539057ca2d4SBaptiste Daroussin 
540057ca2d4SBaptiste Daroussin 	srch.name = name;
5413a444dbdSBaptiste Daroussin 	if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) {
5424c22fda9SMarcelo Araujo 		if (((ud = calloc(1, sizeof(*ud))) == NULL) ||
543057ca2d4SBaptiste Daroussin 		    ((ud->name = strdup(name)) == NULL)) {
5443141e51dSKyle Evans 			fprintf(stderr,"out of memory\n");
545e6d8c0e2SBaptiste Daroussin 			free(ud);
546057ca2d4SBaptiste Daroussin 			return (NULL);
547057ca2d4SBaptiste Daroussin 		}
548057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
549057ca2d4SBaptiste Daroussin 			ud->ref[i] = new_pri();
550057ca2d4SBaptiste Daroussin 		}
5513a444dbdSBaptiste Daroussin 		RB_INSERT(collundefs, &collundefs, ud);
552057ca2d4SBaptiste Daroussin 	}
553057ca2d4SBaptiste Daroussin 	add_charmap_undefined(name);
554057ca2d4SBaptiste Daroussin 	return (ud);
555057ca2d4SBaptiste Daroussin }
556057ca2d4SBaptiste Daroussin 
557057ca2d4SBaptiste Daroussin static collchar_t *
get_collchar(wchar_t wc,int create)558057ca2d4SBaptiste Daroussin get_collchar(wchar_t wc, int create)
559057ca2d4SBaptiste Daroussin {
560057ca2d4SBaptiste Daroussin 	collchar_t	srch;
561057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
562057ca2d4SBaptiste Daroussin 	int		i;
563057ca2d4SBaptiste Daroussin 
564057ca2d4SBaptiste Daroussin 	srch.wc = wc;
5653a444dbdSBaptiste Daroussin 	cc = RB_FIND(collchars, &collchars, &srch);
566057ca2d4SBaptiste Daroussin 	if ((cc == NULL) && create) {
5674c22fda9SMarcelo Araujo 		if ((cc = calloc(1, sizeof(*cc))) == NULL) {
5683141e51dSKyle Evans 			fprintf(stderr, "out of memory\n");
569057ca2d4SBaptiste Daroussin 			return (NULL);
570057ca2d4SBaptiste Daroussin 		}
571057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
572057ca2d4SBaptiste Daroussin 			cc->ref[i] = new_pri();
573057ca2d4SBaptiste Daroussin 		}
574057ca2d4SBaptiste Daroussin 		cc->wc = wc;
5753a444dbdSBaptiste Daroussin 		RB_INSERT(collchars, &collchars, cc);
576057ca2d4SBaptiste Daroussin 	}
577057ca2d4SBaptiste Daroussin 	return (cc);
578057ca2d4SBaptiste Daroussin }
579057ca2d4SBaptiste Daroussin 
580057ca2d4SBaptiste Daroussin void
end_order_collsym(collsym_t * sym)581057ca2d4SBaptiste Daroussin end_order_collsym(collsym_t *sym)
582057ca2d4SBaptiste Daroussin {
583057ca2d4SBaptiste Daroussin 	start_order(T_COLLSYM);
584057ca2d4SBaptiste Daroussin 	/* update the weight */
585057ca2d4SBaptiste Daroussin 
586057ca2d4SBaptiste Daroussin 	set_pri(sym->ref, nextpri, RESOLVED);
587057ca2d4SBaptiste Daroussin 	nextpri++;
588057ca2d4SBaptiste Daroussin }
589057ca2d4SBaptiste Daroussin 
590057ca2d4SBaptiste Daroussin void
end_order(void)591057ca2d4SBaptiste Daroussin end_order(void)
592057ca2d4SBaptiste Daroussin {
593057ca2d4SBaptiste Daroussin 	int		i;
594057ca2d4SBaptiste Daroussin 	int32_t		pri;
595057ca2d4SBaptiste Daroussin 	int32_t		ref;
596057ca2d4SBaptiste Daroussin 	collpri_t	*p;
597057ca2d4SBaptiste Daroussin 
598057ca2d4SBaptiste Daroussin 	/* advance the priority/weight */
599057ca2d4SBaptiste Daroussin 	pri = nextpri;
600057ca2d4SBaptiste Daroussin 
601057ca2d4SBaptiste Daroussin 	switch (currorder) {
602057ca2d4SBaptiste Daroussin 	case T_CHAR:
603057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
604057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
605057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
606057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
607057ca2d4SBaptiste Daroussin 				/* unspecified weight is a self reference */
608057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], pri, RESOLVED);
609057ca2d4SBaptiste Daroussin 			} else {
610057ca2d4SBaptiste Daroussin 				set_pri(currchar->ref[i], ref, REFER);
611057ca2d4SBaptiste Daroussin 			}
612057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
613057ca2d4SBaptiste Daroussin 		}
614057ca2d4SBaptiste Daroussin 
615057ca2d4SBaptiste Daroussin 		/* leave a cookie trail in case next symbol is ellipsis */
616057ca2d4SBaptiste Daroussin 		ellipsis_start = currchar->wc + 1;
617057ca2d4SBaptiste Daroussin 		currchar = NULL;
618057ca2d4SBaptiste Daroussin 		break;
619057ca2d4SBaptiste Daroussin 
620057ca2d4SBaptiste Daroussin 	case T_ELLIPSIS:
621057ca2d4SBaptiste Daroussin 		/* save off the weights were we can find them */
622057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
623057ca2d4SBaptiste Daroussin 			ellipsis_weights[i] = order_weights[i];
624057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
625057ca2d4SBaptiste Daroussin 		}
626057ca2d4SBaptiste Daroussin 		break;
627057ca2d4SBaptiste Daroussin 
628057ca2d4SBaptiste Daroussin 	case T_COLLELEM:
629057ca2d4SBaptiste Daroussin 		if (currelem == NULL) {
630057ca2d4SBaptiste Daroussin 			INTERR;
631057ca2d4SBaptiste Daroussin 		} else {
632057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
633057ca2d4SBaptiste Daroussin 
634057ca2d4SBaptiste Daroussin 				if (((ref = order_weights[i]) < 0) ||
635057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
636057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
637057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], pri,
638057ca2d4SBaptiste Daroussin 					    RESOLVED);
639057ca2d4SBaptiste Daroussin 				} else {
640057ca2d4SBaptiste Daroussin 					set_pri(currelem->ref[i], ref, REFER);
641057ca2d4SBaptiste Daroussin 				}
642057ca2d4SBaptiste Daroussin 				order_weights[i] = -1;
643057ca2d4SBaptiste Daroussin 			}
644057ca2d4SBaptiste Daroussin 		}
645057ca2d4SBaptiste Daroussin 		break;
646057ca2d4SBaptiste Daroussin 
647057ca2d4SBaptiste Daroussin 	case T_UNDEFINED:
648057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
649057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
650057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
651057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
652057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], -1, RESOLVED);
653057ca2d4SBaptiste Daroussin 			} else {
654057ca2d4SBaptiste Daroussin 				set_pri(pri_undefined[i], ref, REFER);
655057ca2d4SBaptiste Daroussin 			}
656057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
657057ca2d4SBaptiste Daroussin 		}
658057ca2d4SBaptiste Daroussin 		break;
659057ca2d4SBaptiste Daroussin 
660057ca2d4SBaptiste Daroussin 	case T_SYMBOL:
661057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
662057ca2d4SBaptiste Daroussin 			if (((ref = order_weights[i]) < 0) ||
663057ca2d4SBaptiste Daroussin 			    ((p = get_pri(ref)) == NULL) ||
664057ca2d4SBaptiste Daroussin 			    (p->pri == -1)) {
665057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], pri, RESOLVED);
666057ca2d4SBaptiste Daroussin 			} else {
667057ca2d4SBaptiste Daroussin 				set_pri(currundef->ref[i], ref, REFER);
668057ca2d4SBaptiste Daroussin 			}
669057ca2d4SBaptiste Daroussin 			order_weights[i] = -1;
670057ca2d4SBaptiste Daroussin 		}
671057ca2d4SBaptiste Daroussin 		break;
672057ca2d4SBaptiste Daroussin 
673057ca2d4SBaptiste Daroussin 	default:
674057ca2d4SBaptiste Daroussin 		INTERR;
675057ca2d4SBaptiste Daroussin 	}
676057ca2d4SBaptiste Daroussin 
677057ca2d4SBaptiste Daroussin 	nextpri++;
678057ca2d4SBaptiste Daroussin }
679057ca2d4SBaptiste Daroussin 
680057ca2d4SBaptiste Daroussin static void
start_order(int type)681057ca2d4SBaptiste Daroussin start_order(int type)
682057ca2d4SBaptiste Daroussin {
683057ca2d4SBaptiste Daroussin 	int	i;
684057ca2d4SBaptiste Daroussin 
685057ca2d4SBaptiste Daroussin 	lastorder = currorder;
686057ca2d4SBaptiste Daroussin 	currorder = type;
687057ca2d4SBaptiste Daroussin 
688057ca2d4SBaptiste Daroussin 	/* this is used to protect ELLIPSIS processing */
689057ca2d4SBaptiste Daroussin 	if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) {
6903141e51dSKyle Evans 		fprintf(stderr, "character value expected\n");
691057ca2d4SBaptiste Daroussin 	}
692057ca2d4SBaptiste Daroussin 
693057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
694057ca2d4SBaptiste Daroussin 		order_weights[i] = -1;
695057ca2d4SBaptiste Daroussin 	}
696057ca2d4SBaptiste Daroussin 	curr_weight = 0;
697057ca2d4SBaptiste Daroussin }
698057ca2d4SBaptiste Daroussin 
699057ca2d4SBaptiste Daroussin void
start_order_undefined(void)700057ca2d4SBaptiste Daroussin start_order_undefined(void)
701057ca2d4SBaptiste Daroussin {
702057ca2d4SBaptiste Daroussin 	start_order(T_UNDEFINED);
703057ca2d4SBaptiste Daroussin }
704057ca2d4SBaptiste Daroussin 
705057ca2d4SBaptiste Daroussin void
start_order_symbol(char * name)706057ca2d4SBaptiste Daroussin start_order_symbol(char *name)
707057ca2d4SBaptiste Daroussin {
708057ca2d4SBaptiste Daroussin 	currundef = get_collundef(name);
709057ca2d4SBaptiste Daroussin 	start_order(T_SYMBOL);
710057ca2d4SBaptiste Daroussin }
711057ca2d4SBaptiste Daroussin 
712057ca2d4SBaptiste Daroussin void
start_order_char(wchar_t wc)713057ca2d4SBaptiste Daroussin start_order_char(wchar_t wc)
714057ca2d4SBaptiste Daroussin {
715057ca2d4SBaptiste Daroussin 	collchar_t	*cc;
716057ca2d4SBaptiste Daroussin 	int32_t		ref;
717057ca2d4SBaptiste Daroussin 
718057ca2d4SBaptiste Daroussin 	start_order(T_CHAR);
719057ca2d4SBaptiste Daroussin 
720057ca2d4SBaptiste Daroussin 	/*
721057ca2d4SBaptiste Daroussin 	 * If we last saw an ellipsis, then we need to close the range.
722057ca2d4SBaptiste Daroussin 	 * Handle that here.  Note that we have to be careful because the
723057ca2d4SBaptiste Daroussin 	 * items *inside* the range are treated exclusiveley to the items
724057ca2d4SBaptiste Daroussin 	 * outside of the range.  The ends of the range can have quite
725057ca2d4SBaptiste Daroussin 	 * different weights than the range members.
726057ca2d4SBaptiste Daroussin 	 */
727057ca2d4SBaptiste Daroussin 	if (lastorder == T_ELLIPSIS) {
728057ca2d4SBaptiste Daroussin 		int		i;
729057ca2d4SBaptiste Daroussin 
730057ca2d4SBaptiste Daroussin 		if (wc < ellipsis_start) {
7313141e51dSKyle Evans 			fprintf(stderr, "malformed range!\n");
732057ca2d4SBaptiste Daroussin 			return;
733057ca2d4SBaptiste Daroussin 		}
734057ca2d4SBaptiste Daroussin 		while (ellipsis_start < wc) {
735057ca2d4SBaptiste Daroussin 			/*
736057ca2d4SBaptiste Daroussin 			 * pick all of the saved weights for the
737057ca2d4SBaptiste Daroussin 			 * ellipsis.  note that -1 encodes for the
738057ca2d4SBaptiste Daroussin 			 * ellipsis itself, which means to take the
739057ca2d4SBaptiste Daroussin 			 * current relative priority.
740057ca2d4SBaptiste Daroussin 			 */
741057ca2d4SBaptiste Daroussin 			if ((cc = get_collchar(ellipsis_start, 1)) == NULL) {
742057ca2d4SBaptiste Daroussin 				INTERR;
743057ca2d4SBaptiste Daroussin 				return;
744057ca2d4SBaptiste Daroussin 			}
745057ca2d4SBaptiste Daroussin 			for (i = 0; i < NUM_WT; i++) {
746057ca2d4SBaptiste Daroussin 				collpri_t *p;
747057ca2d4SBaptiste Daroussin 				if (((ref = ellipsis_weights[i]) == -1) ||
748057ca2d4SBaptiste Daroussin 				    ((p = get_pri(ref)) == NULL) ||
749057ca2d4SBaptiste Daroussin 				    (p->pri == -1)) {
750057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], nextpri, RESOLVED);
751057ca2d4SBaptiste Daroussin 				} else {
752057ca2d4SBaptiste Daroussin 					set_pri(cc->ref[i], ref, REFER);
753057ca2d4SBaptiste Daroussin 				}
754057ca2d4SBaptiste Daroussin 				ellipsis_weights[i] = 0;
755057ca2d4SBaptiste Daroussin 			}
756057ca2d4SBaptiste Daroussin 			ellipsis_start++;
757057ca2d4SBaptiste Daroussin 			nextpri++;
758057ca2d4SBaptiste Daroussin 		}
759057ca2d4SBaptiste Daroussin 	}
760057ca2d4SBaptiste Daroussin 
761057ca2d4SBaptiste Daroussin 	currchar = get_collchar(wc, 1);
762057ca2d4SBaptiste Daroussin }
763057ca2d4SBaptiste Daroussin 
764057ca2d4SBaptiste Daroussin void
start_order_collelem(collelem_t * e)765057ca2d4SBaptiste Daroussin start_order_collelem(collelem_t *e)
766057ca2d4SBaptiste Daroussin {
767057ca2d4SBaptiste Daroussin 	start_order(T_COLLELEM);
768057ca2d4SBaptiste Daroussin 	currelem = e;
769057ca2d4SBaptiste Daroussin }
770057ca2d4SBaptiste Daroussin 
771057ca2d4SBaptiste Daroussin void
start_order_ellipsis(void)772057ca2d4SBaptiste Daroussin start_order_ellipsis(void)
773057ca2d4SBaptiste Daroussin {
774057ca2d4SBaptiste Daroussin 	int	i;
775057ca2d4SBaptiste Daroussin 
776057ca2d4SBaptiste Daroussin 	start_order(T_ELLIPSIS);
777057ca2d4SBaptiste Daroussin 
778057ca2d4SBaptiste Daroussin 	if (lastorder != T_CHAR) {
7793141e51dSKyle Evans 		fprintf(stderr, "illegal starting point for range\n");
780057ca2d4SBaptiste Daroussin 		return;
781057ca2d4SBaptiste Daroussin 	}
782057ca2d4SBaptiste Daroussin 
783057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
784057ca2d4SBaptiste Daroussin 		ellipsis_weights[i] = order_weights[i];
785057ca2d4SBaptiste Daroussin 	}
786057ca2d4SBaptiste Daroussin }
787057ca2d4SBaptiste Daroussin 
788057ca2d4SBaptiste Daroussin void
define_collelem(char * name,wchar_t * wcs)789057ca2d4SBaptiste Daroussin define_collelem(char *name, wchar_t *wcs)
790057ca2d4SBaptiste Daroussin {
791057ca2d4SBaptiste Daroussin 	collelem_t	*e;
792057ca2d4SBaptiste Daroussin 	int		i;
793057ca2d4SBaptiste Daroussin 
794057ca2d4SBaptiste Daroussin 	if (wcslen(wcs) >= COLLATE_STR_LEN) {
7953141e51dSKyle Evans 		fprintf(stderr,"expanded collation element too long\n");
796057ca2d4SBaptiste Daroussin 		return;
797057ca2d4SBaptiste Daroussin 	}
798057ca2d4SBaptiste Daroussin 
7994c22fda9SMarcelo Araujo 	if ((e = calloc(1, sizeof(*e))) == NULL) {
8003141e51dSKyle Evans 		fprintf(stderr, "out of memory\n");
801057ca2d4SBaptiste Daroussin 		return;
802057ca2d4SBaptiste Daroussin 	}
803057ca2d4SBaptiste Daroussin 	e->expand = wcs;
804057ca2d4SBaptiste Daroussin 	e->symbol = name;
805057ca2d4SBaptiste Daroussin 
806057ca2d4SBaptiste Daroussin 	/*
807057ca2d4SBaptiste Daroussin 	 * This is executed before the order statement, so we don't
808057ca2d4SBaptiste Daroussin 	 * know how many priorities we *really* need.  We allocate one
809057ca2d4SBaptiste Daroussin 	 * for each possible weight.  Not a big deal, as collating-elements
810057ca2d4SBaptiste Daroussin 	 * prove to be quite rare.
811057ca2d4SBaptiste Daroussin 	 */
812057ca2d4SBaptiste Daroussin 	for (i = 0; i < COLL_WEIGHTS_MAX; i++) {
813057ca2d4SBaptiste Daroussin 		e->ref[i] = new_pri();
814057ca2d4SBaptiste Daroussin 	}
815057ca2d4SBaptiste Daroussin 
816057ca2d4SBaptiste Daroussin 	/* A character sequence can only reduce to one element. */
8173a444dbdSBaptiste Daroussin 	if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) ||
8183a444dbdSBaptiste Daroussin 	    (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) {
8193141e51dSKyle Evans 		fprintf(stderr, "duplicate collating element definition\n");
820e6d8c0e2SBaptiste Daroussin 		free(e);
821057ca2d4SBaptiste Daroussin 		return;
822057ca2d4SBaptiste Daroussin 	}
8233a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_symbol, &elem_by_symbol, e);
8243a444dbdSBaptiste Daroussin 	RB_INSERT(elem_by_expand, &elem_by_expand, e);
825057ca2d4SBaptiste Daroussin }
826057ca2d4SBaptiste Daroussin 
827057ca2d4SBaptiste Daroussin void
add_order_bit(int kw)828057ca2d4SBaptiste Daroussin add_order_bit(int kw)
829057ca2d4SBaptiste Daroussin {
830057ca2d4SBaptiste Daroussin 	uint8_t bit = DIRECTIVE_UNDEF;
831057ca2d4SBaptiste Daroussin 
832057ca2d4SBaptiste Daroussin 	switch (kw) {
833057ca2d4SBaptiste Daroussin 	case T_FORWARD:
834057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_FORWARD;
835057ca2d4SBaptiste Daroussin 		break;
836057ca2d4SBaptiste Daroussin 	case T_BACKWARD:
837057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_BACKWARD;
838057ca2d4SBaptiste Daroussin 		break;
839057ca2d4SBaptiste Daroussin 	case T_POSITION:
840057ca2d4SBaptiste Daroussin 		bit = DIRECTIVE_POSITION;
841057ca2d4SBaptiste Daroussin 		break;
842057ca2d4SBaptiste Daroussin 	default:
843057ca2d4SBaptiste Daroussin 		INTERR;
844057ca2d4SBaptiste Daroussin 		break;
845057ca2d4SBaptiste Daroussin 	}
846057ca2d4SBaptiste Daroussin 	collinfo.directive[collinfo.directive_count] |= bit;
847057ca2d4SBaptiste Daroussin }
848057ca2d4SBaptiste Daroussin 
849057ca2d4SBaptiste Daroussin void
add_order_directive(void)850057ca2d4SBaptiste Daroussin add_order_directive(void)
851057ca2d4SBaptiste Daroussin {
852057ca2d4SBaptiste Daroussin 	if (collinfo.directive_count >= COLL_WEIGHTS_MAX) {
8532becc9efSAlex Richardson 		fprintf(stderr, "too many directives (max %d)\n", COLL_WEIGHTS_MAX);
8542becc9efSAlex Richardson 		return;
855057ca2d4SBaptiste Daroussin 	}
856057ca2d4SBaptiste Daroussin 	collinfo.directive_count++;
857057ca2d4SBaptiste Daroussin }
858057ca2d4SBaptiste Daroussin 
859057ca2d4SBaptiste Daroussin static void
add_order_pri(int32_t ref)860057ca2d4SBaptiste Daroussin add_order_pri(int32_t ref)
861057ca2d4SBaptiste Daroussin {
862057ca2d4SBaptiste Daroussin 	if (curr_weight >= NUM_WT) {
8632becc9efSAlex Richardson 		fprintf(stderr, "too many weights (max %d)\n", NUM_WT);
864057ca2d4SBaptiste Daroussin 		return;
865057ca2d4SBaptiste Daroussin 	}
866057ca2d4SBaptiste Daroussin 	order_weights[curr_weight] = ref;
867057ca2d4SBaptiste Daroussin 	curr_weight++;
868057ca2d4SBaptiste Daroussin }
869057ca2d4SBaptiste Daroussin 
870057ca2d4SBaptiste Daroussin void
add_order_collsym(collsym_t * s)871057ca2d4SBaptiste Daroussin add_order_collsym(collsym_t *s)
872057ca2d4SBaptiste Daroussin {
873057ca2d4SBaptiste Daroussin 	add_order_pri(s->ref);
874057ca2d4SBaptiste Daroussin }
875057ca2d4SBaptiste Daroussin 
876057ca2d4SBaptiste Daroussin void
add_order_char(wchar_t wc)877057ca2d4SBaptiste Daroussin add_order_char(wchar_t wc)
878057ca2d4SBaptiste Daroussin {
879057ca2d4SBaptiste Daroussin 	collchar_t *cc;
880057ca2d4SBaptiste Daroussin 
881057ca2d4SBaptiste Daroussin 	if ((cc = get_collchar(wc, 1)) == NULL) {
882057ca2d4SBaptiste Daroussin 		INTERR;
883057ca2d4SBaptiste Daroussin 		return;
884057ca2d4SBaptiste Daroussin 	}
885057ca2d4SBaptiste Daroussin 
886057ca2d4SBaptiste Daroussin 	add_order_pri(cc->ref[curr_weight]);
887057ca2d4SBaptiste Daroussin }
888057ca2d4SBaptiste Daroussin 
889057ca2d4SBaptiste Daroussin void
add_order_collelem(collelem_t * e)890057ca2d4SBaptiste Daroussin add_order_collelem(collelem_t *e)
891057ca2d4SBaptiste Daroussin {
892057ca2d4SBaptiste Daroussin 	add_order_pri(e->ref[curr_weight]);
893057ca2d4SBaptiste Daroussin }
894057ca2d4SBaptiste Daroussin 
895057ca2d4SBaptiste Daroussin void
add_order_ignore(void)896057ca2d4SBaptiste Daroussin add_order_ignore(void)
897057ca2d4SBaptiste Daroussin {
898057ca2d4SBaptiste Daroussin 	add_order_pri(pri_ignore);
899057ca2d4SBaptiste Daroussin }
900057ca2d4SBaptiste Daroussin 
901057ca2d4SBaptiste Daroussin void
add_order_symbol(char * sym)902057ca2d4SBaptiste Daroussin add_order_symbol(char *sym)
903057ca2d4SBaptiste Daroussin {
904057ca2d4SBaptiste Daroussin 	collundef_t *c;
905057ca2d4SBaptiste Daroussin 	if ((c = get_collundef(sym)) == NULL) {
906057ca2d4SBaptiste Daroussin 		INTERR;
907057ca2d4SBaptiste Daroussin 		return;
908057ca2d4SBaptiste Daroussin 	}
909057ca2d4SBaptiste Daroussin 	add_order_pri(c->ref[curr_weight]);
910057ca2d4SBaptiste Daroussin }
911057ca2d4SBaptiste Daroussin 
912057ca2d4SBaptiste Daroussin void
add_order_ellipsis(void)913057ca2d4SBaptiste Daroussin add_order_ellipsis(void)
914057ca2d4SBaptiste Daroussin {
915057ca2d4SBaptiste Daroussin 	/* special NULL value indicates self reference */
916057ca2d4SBaptiste Daroussin 	add_order_pri(0);
917057ca2d4SBaptiste Daroussin }
918057ca2d4SBaptiste Daroussin 
919057ca2d4SBaptiste Daroussin void
add_order_subst(void)920057ca2d4SBaptiste Daroussin add_order_subst(void)
921057ca2d4SBaptiste Daroussin {
922057ca2d4SBaptiste Daroussin 	subst_t srch;
923057ca2d4SBaptiste Daroussin 	subst_t	*s;
924057ca2d4SBaptiste Daroussin 	int i;
925057ca2d4SBaptiste Daroussin 
926057ca2d4SBaptiste Daroussin 	(void) memset(&srch, 0, sizeof (srch));
927057ca2d4SBaptiste Daroussin 	for (i = 0; i < curr_subst; i++) {
928057ca2d4SBaptiste Daroussin 		srch.ref[i] = subst_weights[i];
929057ca2d4SBaptiste Daroussin 		subst_weights[i] = 0;
930057ca2d4SBaptiste Daroussin 	}
9313a444dbdSBaptiste Daroussin 	s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch);
932057ca2d4SBaptiste Daroussin 
933057ca2d4SBaptiste Daroussin 	if (s == NULL) {
9344c22fda9SMarcelo Araujo 		if ((s = calloc(1, sizeof(*s))) == NULL) {
9353141e51dSKyle Evans 			fprintf(stderr,"out of memory\n");
936057ca2d4SBaptiste Daroussin 			return;
937057ca2d4SBaptiste Daroussin 		}
938057ca2d4SBaptiste Daroussin 		s->key = new_pri();
939057ca2d4SBaptiste Daroussin 
940057ca2d4SBaptiste Daroussin 		/*
941057ca2d4SBaptiste Daroussin 		 * We use a self reference for our key, but we set a
942057ca2d4SBaptiste Daroussin 		 * high bit to indicate that this is a substitution
943057ca2d4SBaptiste Daroussin 		 * reference.  This will expedite table lookups later,
944057ca2d4SBaptiste Daroussin 		 * and prevent table lookups for situations that don't
945057ca2d4SBaptiste Daroussin 		 * require it.  (In short, its a big win, because we
946057ca2d4SBaptiste Daroussin 		 * can skip a lot of binary searching.)
947057ca2d4SBaptiste Daroussin 		 */
948057ca2d4SBaptiste Daroussin 		set_pri(s->key,
949057ca2d4SBaptiste Daroussin 		    (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY),
950057ca2d4SBaptiste Daroussin 		    RESOLVED);
951057ca2d4SBaptiste Daroussin 		nextsubst[curr_weight] += 1;
952057ca2d4SBaptiste Daroussin 
953057ca2d4SBaptiste Daroussin 		for (i = 0; i < curr_subst; i++) {
954057ca2d4SBaptiste Daroussin 			s->ref[i] = srch.ref[i];
955057ca2d4SBaptiste Daroussin 		}
956057ca2d4SBaptiste Daroussin 
9573a444dbdSBaptiste Daroussin 		RB_INSERT(substs_ref, &substs_ref[curr_weight], s);
958057ca2d4SBaptiste Daroussin 
9593a444dbdSBaptiste Daroussin 		if (RB_FIND(substs, &substs[curr_weight], s) != NULL) {
960057ca2d4SBaptiste Daroussin 			INTERR;
961057ca2d4SBaptiste Daroussin 			return;
962057ca2d4SBaptiste Daroussin 		}
9633a444dbdSBaptiste Daroussin 		RB_INSERT(substs, &substs[curr_weight], s);
964057ca2d4SBaptiste Daroussin 	}
965057ca2d4SBaptiste Daroussin 	curr_subst = 0;
966057ca2d4SBaptiste Daroussin 
967057ca2d4SBaptiste Daroussin 
968057ca2d4SBaptiste Daroussin 	/*
969057ca2d4SBaptiste Daroussin 	 * We are using the current (unique) priority as a search key
970057ca2d4SBaptiste Daroussin 	 * in the substitution table.
971057ca2d4SBaptiste Daroussin 	 */
972057ca2d4SBaptiste Daroussin 	add_order_pri(s->key);
973057ca2d4SBaptiste Daroussin }
974057ca2d4SBaptiste Daroussin 
975057ca2d4SBaptiste Daroussin static void
add_subst_pri(int32_t ref)976057ca2d4SBaptiste Daroussin add_subst_pri(int32_t ref)
977057ca2d4SBaptiste Daroussin {
978057ca2d4SBaptiste Daroussin 	if (curr_subst >= COLLATE_STR_LEN) {
9793141e51dSKyle Evans 		fprintf(stderr,"substitution string is too long\n");
980057ca2d4SBaptiste Daroussin 		return;
981057ca2d4SBaptiste Daroussin 	}
982057ca2d4SBaptiste Daroussin 	subst_weights[curr_subst] = ref;
983057ca2d4SBaptiste Daroussin 	curr_subst++;
984057ca2d4SBaptiste Daroussin }
985057ca2d4SBaptiste Daroussin 
986057ca2d4SBaptiste Daroussin void
add_subst_char(wchar_t wc)987057ca2d4SBaptiste Daroussin add_subst_char(wchar_t wc)
988057ca2d4SBaptiste Daroussin {
989057ca2d4SBaptiste Daroussin 	collchar_t *cc;
990057ca2d4SBaptiste Daroussin 
991057ca2d4SBaptiste Daroussin 
992057ca2d4SBaptiste Daroussin 	if (((cc = get_collchar(wc, 1)) == NULL) ||
993057ca2d4SBaptiste Daroussin 	    (cc->wc != wc)) {
994057ca2d4SBaptiste Daroussin 		INTERR;
995057ca2d4SBaptiste Daroussin 		return;
996057ca2d4SBaptiste Daroussin 	}
997057ca2d4SBaptiste Daroussin 	/* we take the weight for the character at that position */
998057ca2d4SBaptiste Daroussin 	add_subst_pri(cc->ref[curr_weight]);
999057ca2d4SBaptiste Daroussin }
1000057ca2d4SBaptiste Daroussin 
1001057ca2d4SBaptiste Daroussin void
add_subst_collelem(collelem_t * e)1002057ca2d4SBaptiste Daroussin add_subst_collelem(collelem_t *e)
1003057ca2d4SBaptiste Daroussin {
1004057ca2d4SBaptiste Daroussin 	add_subst_pri(e->ref[curr_weight]);
1005057ca2d4SBaptiste Daroussin }
1006057ca2d4SBaptiste Daroussin 
1007057ca2d4SBaptiste Daroussin void
add_subst_collsym(collsym_t * s)1008057ca2d4SBaptiste Daroussin add_subst_collsym(collsym_t *s)
1009057ca2d4SBaptiste Daroussin {
1010057ca2d4SBaptiste Daroussin 	add_subst_pri(s->ref);
1011057ca2d4SBaptiste Daroussin }
1012057ca2d4SBaptiste Daroussin 
1013057ca2d4SBaptiste Daroussin void
add_subst_symbol(char * ptr)1014057ca2d4SBaptiste Daroussin add_subst_symbol(char *ptr)
1015057ca2d4SBaptiste Daroussin {
1016057ca2d4SBaptiste Daroussin 	collundef_t *cu;
1017057ca2d4SBaptiste Daroussin 
1018057ca2d4SBaptiste Daroussin 	if ((cu = get_collundef(ptr)) != NULL) {
1019057ca2d4SBaptiste Daroussin 		add_subst_pri(cu->ref[curr_weight]);
1020057ca2d4SBaptiste Daroussin 	}
1021057ca2d4SBaptiste Daroussin }
1022057ca2d4SBaptiste Daroussin 
1023057ca2d4SBaptiste Daroussin void
add_weight(int32_t ref,int pass)1024057ca2d4SBaptiste Daroussin add_weight(int32_t ref, int pass)
1025057ca2d4SBaptiste Daroussin {
1026057ca2d4SBaptiste Daroussin 	weight_t srch;
1027057ca2d4SBaptiste Daroussin 	weight_t *w;
1028057ca2d4SBaptiste Daroussin 
1029057ca2d4SBaptiste Daroussin 	srch.pri = resolve_pri(ref);
1030057ca2d4SBaptiste Daroussin 
1031057ca2d4SBaptiste Daroussin 	/* No translation of ignores */
1032057ca2d4SBaptiste Daroussin 	if (srch.pri == 0)
1033057ca2d4SBaptiste Daroussin 		return;
1034057ca2d4SBaptiste Daroussin 
1035057ca2d4SBaptiste Daroussin 	/* Substitution priorities are not weights */
1036057ca2d4SBaptiste Daroussin 	if (srch.pri & COLLATE_SUBST_PRIORITY)
1037057ca2d4SBaptiste Daroussin 		return;
1038057ca2d4SBaptiste Daroussin 
10393a444dbdSBaptiste Daroussin 	if (RB_FIND(weights, &weights[pass], &srch) != NULL)
1040057ca2d4SBaptiste Daroussin 		return;
1041057ca2d4SBaptiste Daroussin 
10424c22fda9SMarcelo Araujo 	if ((w = calloc(1, sizeof(*w))) == NULL) {
10433141e51dSKyle Evans 		fprintf(stderr, "out of memory\n");
1044057ca2d4SBaptiste Daroussin 		return;
1045057ca2d4SBaptiste Daroussin 	}
1046057ca2d4SBaptiste Daroussin 	w->pri = srch.pri;
10473a444dbdSBaptiste Daroussin 	RB_INSERT(weights, &weights[pass], w);
1048057ca2d4SBaptiste Daroussin }
1049057ca2d4SBaptiste Daroussin 
1050057ca2d4SBaptiste Daroussin void
add_weights(int32_t * refs)1051057ca2d4SBaptiste Daroussin add_weights(int32_t *refs)
1052057ca2d4SBaptiste Daroussin {
1053057ca2d4SBaptiste Daroussin 	int i;
1054057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1055057ca2d4SBaptiste Daroussin 		add_weight(refs[i], i);
1056057ca2d4SBaptiste Daroussin 	}
1057057ca2d4SBaptiste Daroussin }
1058057ca2d4SBaptiste Daroussin 
1059057ca2d4SBaptiste Daroussin int32_t
get_weight(int32_t ref,int pass)1060057ca2d4SBaptiste Daroussin get_weight(int32_t ref, int pass)
1061057ca2d4SBaptiste Daroussin {
1062057ca2d4SBaptiste Daroussin 	weight_t	srch;
1063057ca2d4SBaptiste Daroussin 	weight_t	*w;
1064057ca2d4SBaptiste Daroussin 	int32_t		pri;
1065057ca2d4SBaptiste Daroussin 
1066057ca2d4SBaptiste Daroussin 	pri = resolve_pri(ref);
1067057ca2d4SBaptiste Daroussin 	if (pri & COLLATE_SUBST_PRIORITY) {
1068057ca2d4SBaptiste Daroussin 		return (pri);
1069057ca2d4SBaptiste Daroussin 	}
1070057ca2d4SBaptiste Daroussin 	if (pri <= 0) {
1071057ca2d4SBaptiste Daroussin 		return (pri);
1072057ca2d4SBaptiste Daroussin 	}
1073057ca2d4SBaptiste Daroussin 	srch.pri = pri;
10743a444dbdSBaptiste Daroussin 	if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) {
1075057ca2d4SBaptiste Daroussin 		INTERR;
1076057ca2d4SBaptiste Daroussin 		return (-1);
1077057ca2d4SBaptiste Daroussin 	}
1078057ca2d4SBaptiste Daroussin 	return (w->opt);
1079057ca2d4SBaptiste Daroussin }
1080057ca2d4SBaptiste Daroussin 
1081057ca2d4SBaptiste Daroussin wchar_t *
wsncpy(wchar_t * s1,const wchar_t * s2,size_t n)1082057ca2d4SBaptiste Daroussin wsncpy(wchar_t *s1, const wchar_t *s2, size_t n)
1083057ca2d4SBaptiste Daroussin {
1084057ca2d4SBaptiste Daroussin 	wchar_t *os1 = s1;
1085057ca2d4SBaptiste Daroussin 
1086057ca2d4SBaptiste Daroussin 	n++;
10874644f9beSYuri Pankov 	while (--n > 0 && (*s1++ = htote(*s2++)) != 0)
1088057ca2d4SBaptiste Daroussin 		continue;
1089057ca2d4SBaptiste Daroussin 	if (n > 0)
1090057ca2d4SBaptiste Daroussin 		while (--n > 0)
1091057ca2d4SBaptiste Daroussin 			*s1++ = 0;
1092057ca2d4SBaptiste Daroussin 	return (os1);
1093057ca2d4SBaptiste Daroussin }
1094057ca2d4SBaptiste Daroussin 
109523a32822SBaptiste Daroussin #define RB_COUNT(x, name, head, cnt) do { \
109623a32822SBaptiste Daroussin 	(cnt) = 0; \
109723a32822SBaptiste Daroussin 	RB_FOREACH(x, name, (head)) { \
109823a32822SBaptiste Daroussin 		(cnt)++; \
109923a32822SBaptiste Daroussin 	} \
110023a32822SBaptiste Daroussin } while (0)
110123a32822SBaptiste Daroussin 
11023a444dbdSBaptiste Daroussin #define RB_NUMNODES(type, name, head, cnt) do { \
11033a444dbdSBaptiste Daroussin 	type *t; \
11043a444dbdSBaptiste Daroussin 	cnt = 0; \
11053a444dbdSBaptiste Daroussin 	RB_FOREACH(t, name, head) { \
11063a444dbdSBaptiste Daroussin 		cnt++; \
11073a444dbdSBaptiste Daroussin 	} \
110823a32822SBaptiste Daroussin } while (0)
11093a444dbdSBaptiste Daroussin 
1110057ca2d4SBaptiste Daroussin void
dump_collate(void)1111057ca2d4SBaptiste Daroussin dump_collate(void)
1112057ca2d4SBaptiste Daroussin {
1113057ca2d4SBaptiste Daroussin 	FILE			*f;
1114057ca2d4SBaptiste Daroussin 	int			i, j, n;
1115057ca2d4SBaptiste Daroussin 	size_t			sz;
1116057ca2d4SBaptiste Daroussin 	int32_t			pri;
1117057ca2d4SBaptiste Daroussin 	collelem_t		*ce;
1118057ca2d4SBaptiste Daroussin 	collchar_t		*cc;
1119057ca2d4SBaptiste Daroussin 	subst_t			*sb;
1120cc7edd25SThomas Munro 	char			fmt_version[COLLATE_FMT_VERSION_LEN];
1121cc7edd25SThomas Munro 	char			def_version[XLOCALE_DEF_VERSION_LEN];
1122057ca2d4SBaptiste Daroussin 	collate_char_t		chars[UCHAR_MAX + 1];
1123057ca2d4SBaptiste Daroussin 	collate_large_t		*large;
1124057ca2d4SBaptiste Daroussin 	collate_subst_t		*subst[COLL_WEIGHTS_MAX];
1125057ca2d4SBaptiste Daroussin 	collate_chain_t		*chain;
1126057ca2d4SBaptiste Daroussin 
1127057ca2d4SBaptiste Daroussin 	/*
1128fcc7baa1SPedro F. Giffuni 	 * We have to run through a preliminary pass to identify all the
1129057ca2d4SBaptiste Daroussin 	 * weights that we use for each sorting level.
1130057ca2d4SBaptiste Daroussin 	 */
1131057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1132057ca2d4SBaptiste Daroussin 		add_weight(pri_ignore, i);
1133057ca2d4SBaptiste Daroussin 	}
1134057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
11353a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1136057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
1137057ca2d4SBaptiste Daroussin 				add_weight(sb->ref[j], i);
1138057ca2d4SBaptiste Daroussin 			}
1139057ca2d4SBaptiste Daroussin 		}
1140057ca2d4SBaptiste Daroussin 	}
11413a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1142057ca2d4SBaptiste Daroussin 		add_weights(ce->ref);
1143057ca2d4SBaptiste Daroussin 	}
11443a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1145057ca2d4SBaptiste Daroussin 		add_weights(cc->ref);
1146057ca2d4SBaptiste Daroussin 	}
1147057ca2d4SBaptiste Daroussin 
1148057ca2d4SBaptiste Daroussin 	/*
1149057ca2d4SBaptiste Daroussin 	 * Now we walk the entire set of weights, removing the gaps
1150057ca2d4SBaptiste Daroussin 	 * in the weights.  This gives us optimum usage.  The walk
1151057ca2d4SBaptiste Daroussin 	 * occurs in priority.
1152057ca2d4SBaptiste Daroussin 	 */
1153057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1154057ca2d4SBaptiste Daroussin 		weight_t *w;
11553a444dbdSBaptiste Daroussin 		RB_FOREACH(w, weights, &weights[i]) {
1156057ca2d4SBaptiste Daroussin 			w->opt = nweight[i];
1157057ca2d4SBaptiste Daroussin 			nweight[i] += 1;
1158057ca2d4SBaptiste Daroussin 		}
1159057ca2d4SBaptiste Daroussin 	}
1160057ca2d4SBaptiste Daroussin 
1161057ca2d4SBaptiste Daroussin 	(void) memset(&chars, 0, sizeof (chars));
1162cc7edd25SThomas Munro 	(void) memset(fmt_version, 0, COLLATE_FMT_VERSION_LEN);
1163cc7edd25SThomas Munro 	(void) strlcpy(fmt_version, COLLATE_FMT_VERSION, sizeof (fmt_version));
1164cc7edd25SThomas Munro 	(void) memset(def_version, 0, XLOCALE_DEF_VERSION_LEN);
1165cc7edd25SThomas Munro 	if (version)
1166cc7edd25SThomas Munro 		(void) strlcpy(def_version, version, sizeof (def_version));
1167057ca2d4SBaptiste Daroussin 
1168057ca2d4SBaptiste Daroussin 	/*
1169057ca2d4SBaptiste Daroussin 	 * We need to make sure we arrange for the UNDEFINED field
1170057ca2d4SBaptiste Daroussin 	 * to show up.  Also, set the total weight counts.
1171057ca2d4SBaptiste Daroussin 	 */
1172057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1173057ca2d4SBaptiste Daroussin 		if (resolve_pri(pri_undefined[i]) == -1) {
1174057ca2d4SBaptiste Daroussin 			set_pri(pri_undefined[i], -1, RESOLVED);
1175057ca2d4SBaptiste Daroussin 			/* they collate at the end of everything else */
11764644f9beSYuri Pankov 			collinfo.undef_pri[i] = htote(COLLATE_MAX_PRIORITY);
1177057ca2d4SBaptiste Daroussin 		}
11784644f9beSYuri Pankov 		collinfo.pri_count[i] = htote(nweight[i]);
1179057ca2d4SBaptiste Daroussin 	}
1180057ca2d4SBaptiste Daroussin 
11814644f9beSYuri Pankov 	collinfo.pri_count[NUM_WT] = htote(max_wide());
11824644f9beSYuri Pankov 	collinfo.undef_pri[NUM_WT] = htote(COLLATE_MAX_PRIORITY);
1183057ca2d4SBaptiste Daroussin 	collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED;
1184057ca2d4SBaptiste Daroussin 
1185057ca2d4SBaptiste Daroussin 	/*
1186057ca2d4SBaptiste Daroussin 	 * Ordinary character priorities
1187057ca2d4SBaptiste Daroussin 	 */
1188057ca2d4SBaptiste Daroussin 	for (i = 0; i <= UCHAR_MAX; i++) {
1189057ca2d4SBaptiste Daroussin 		if ((cc = get_collchar(i, 0)) != NULL) {
1190057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
11914644f9beSYuri Pankov 				chars[i].pri[j] =
11924644f9beSYuri Pankov 				    htote(get_weight(cc->ref[j], j));
1193057ca2d4SBaptiste Daroussin 			}
1194057ca2d4SBaptiste Daroussin 		} else {
1195057ca2d4SBaptiste Daroussin 			for (j = 0; j < NUM_WT; j++) {
1196057ca2d4SBaptiste Daroussin 				chars[i].pri[j] =
11974644f9beSYuri Pankov 				    htote(get_weight(pri_undefined[j], j));
1198057ca2d4SBaptiste Daroussin 			}
1199057ca2d4SBaptiste Daroussin 			/*
1200057ca2d4SBaptiste Daroussin 			 * Per POSIX, for undefined characters, we
1201057ca2d4SBaptiste Daroussin 			 * also have to add a last item, which is the
1202057ca2d4SBaptiste Daroussin 			 * character code.
1203057ca2d4SBaptiste Daroussin 			 */
12044644f9beSYuri Pankov 			chars[i].pri[NUM_WT] = htote(i);
1205057ca2d4SBaptiste Daroussin 		}
1206057ca2d4SBaptiste Daroussin 	}
1207057ca2d4SBaptiste Daroussin 
1208057ca2d4SBaptiste Daroussin 	/*
1209057ca2d4SBaptiste Daroussin 	 * Substitution tables
1210057ca2d4SBaptiste Daroussin 	 */
1211057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
1212057ca2d4SBaptiste Daroussin 		collate_subst_t *st = NULL;
121323a32822SBaptiste Daroussin 		subst_t *temp;
121423a32822SBaptiste Daroussin 		RB_COUNT(temp, substs, &substs[i], n);
12154644f9beSYuri Pankov 		subst_count[i] = n;
1216be4391a2SPedro F. Giffuni 		if ((st = calloc(n, sizeof(collate_subst_t))) == NULL) {
12173141e51dSKyle Evans 			fprintf(stderr, "out of memory\n");
1218057ca2d4SBaptiste Daroussin 			return;
1219057ca2d4SBaptiste Daroussin 		}
1220057ca2d4SBaptiste Daroussin 		n = 0;
12213a444dbdSBaptiste Daroussin 		RB_FOREACH(sb, substs, &substs[i]) {
1222057ca2d4SBaptiste Daroussin 			if ((st[n].key = resolve_pri(sb->key)) < 0) {
1223057ca2d4SBaptiste Daroussin 				/* by definition these resolve! */
1224057ca2d4SBaptiste Daroussin 				INTERR;
1225057ca2d4SBaptiste Daroussin 			}
1226057ca2d4SBaptiste Daroussin 			if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) {
1227057ca2d4SBaptiste Daroussin 				INTERR;
1228057ca2d4SBaptiste Daroussin 			}
12294644f9beSYuri Pankov 			st[n].key = htote(st[n].key);
1230057ca2d4SBaptiste Daroussin 			for (j = 0; sb->ref[j]; j++) {
12314644f9beSYuri Pankov 				st[n].pri[j] = htote(get_weight(sb->ref[j],
12324644f9beSYuri Pankov 				    i));
1233057ca2d4SBaptiste Daroussin 			}
1234057ca2d4SBaptiste Daroussin 			n++;
1235057ca2d4SBaptiste Daroussin 		}
12364644f9beSYuri Pankov 		if (n != subst_count[i])
1237057ca2d4SBaptiste Daroussin 			INTERR;
1238057ca2d4SBaptiste Daroussin 		subst[i] = st;
1239057ca2d4SBaptiste Daroussin 	}
1240057ca2d4SBaptiste Daroussin 
1241057ca2d4SBaptiste Daroussin 
1242057ca2d4SBaptiste Daroussin 	/*
1243057ca2d4SBaptiste Daroussin 	 * Chains, i.e. collating elements
1244057ca2d4SBaptiste Daroussin 	 */
12454644f9beSYuri Pankov 	RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, chain_count);
12464644f9beSYuri Pankov 	chain = calloc(chain_count, sizeof(collate_chain_t));
1247057ca2d4SBaptiste Daroussin 	if (chain == NULL) {
12483141e51dSKyle Evans 		fprintf(stderr, "out of memory\n");
1249057ca2d4SBaptiste Daroussin 		return;
1250057ca2d4SBaptiste Daroussin 	}
12513a444dbdSBaptiste Daroussin 	n = 0;
12523a444dbdSBaptiste Daroussin 	RB_FOREACH(ce, elem_by_expand, &elem_by_expand) {
1253057ca2d4SBaptiste Daroussin 		(void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN);
1254057ca2d4SBaptiste Daroussin 		for (i = 0; i < NUM_WT; i++) {
12554644f9beSYuri Pankov 			chain[n].pri[i] = htote(get_weight(ce->ref[i], i));
1256057ca2d4SBaptiste Daroussin 		}
12575d21db09SBaptiste Daroussin 		n++;
1258057ca2d4SBaptiste Daroussin 	}
12594644f9beSYuri Pankov 	if (n != chain_count)
1260057ca2d4SBaptiste Daroussin 		INTERR;
1261057ca2d4SBaptiste Daroussin 
1262057ca2d4SBaptiste Daroussin 	/*
1263057ca2d4SBaptiste Daroussin 	 * Large (> UCHAR_MAX) character priorities
1264057ca2d4SBaptiste Daroussin 	 */
12653a444dbdSBaptiste Daroussin 	RB_NUMNODES(collchar_t, collchars, &collchars, n);
12665d21db09SBaptiste Daroussin 	large = calloc(n, sizeof(collate_large_t));
1267057ca2d4SBaptiste Daroussin 	if (large == NULL) {
12683141e51dSKyle Evans 		fprintf(stderr, "out of memory\n");
1269057ca2d4SBaptiste Daroussin 		return;
1270057ca2d4SBaptiste Daroussin 	}
1271057ca2d4SBaptiste Daroussin 
1272057ca2d4SBaptiste Daroussin 	i = 0;
12733a444dbdSBaptiste Daroussin 	RB_FOREACH(cc, collchars, &collchars) {
1274057ca2d4SBaptiste Daroussin 		int	undef = 0;
1275057ca2d4SBaptiste Daroussin 		/* we already gathered those */
1276057ca2d4SBaptiste Daroussin 		if (cc->wc <= UCHAR_MAX)
1277057ca2d4SBaptiste Daroussin 			continue;
1278057ca2d4SBaptiste Daroussin 		for (j = 0; j < NUM_WT; j++) {
1279057ca2d4SBaptiste Daroussin 			if ((pri = get_weight(cc->ref[j], j)) < 0) {
1280057ca2d4SBaptiste Daroussin 				undef = 1;
1281057ca2d4SBaptiste Daroussin 			}
1282057ca2d4SBaptiste Daroussin 			if (undef && (pri >= 0)) {
1283057ca2d4SBaptiste Daroussin 				/* if undefined, then all priorities are */
1284057ca2d4SBaptiste Daroussin 				INTERR;
1285057ca2d4SBaptiste Daroussin 			} else {
12864644f9beSYuri Pankov 				large[i].pri.pri[j] = htote(pri);
1287057ca2d4SBaptiste Daroussin 			}
1288057ca2d4SBaptiste Daroussin 		}
1289057ca2d4SBaptiste Daroussin 		if (!undef) {
12904644f9beSYuri Pankov 			large[i].val = htote(cc->wc);
12914644f9beSYuri Pankov 			large_count = i++;
1292057ca2d4SBaptiste Daroussin 		}
1293057ca2d4SBaptiste Daroussin 	}
1294057ca2d4SBaptiste Daroussin 
1295057ca2d4SBaptiste Daroussin 	if ((f = open_category()) == NULL) {
1296057ca2d4SBaptiste Daroussin 		return;
1297057ca2d4SBaptiste Daroussin 	}
1298057ca2d4SBaptiste Daroussin 
1299057ca2d4SBaptiste Daroussin 	/* Time to write the entire data set out */
1300057ca2d4SBaptiste Daroussin 
13014644f9beSYuri Pankov 	for (i = 0; i < NUM_WT; i++)
13024644f9beSYuri Pankov 		collinfo.subst_count[i] = htote(subst_count[i]);
13034644f9beSYuri Pankov 	collinfo.chain_count = htote(chain_count);
13044644f9beSYuri Pankov 	collinfo.large_count = htote(large_count);
13054644f9beSYuri Pankov 
1306cc7edd25SThomas Munro 	if ((wr_category(fmt_version, COLLATE_FMT_VERSION_LEN, f) < 0) ||
1307cc7edd25SThomas Munro 	    (wr_category(def_version, XLOCALE_DEF_VERSION_LEN, f) < 0) ||
1308057ca2d4SBaptiste Daroussin 	    (wr_category(&collinfo, sizeof (collinfo), f) < 0) ||
1309057ca2d4SBaptiste Daroussin 	    (wr_category(&chars, sizeof (chars), f) < 0)) {
1310057ca2d4SBaptiste Daroussin 		return;
1311057ca2d4SBaptiste Daroussin 	}
1312057ca2d4SBaptiste Daroussin 
1313057ca2d4SBaptiste Daroussin 	for (i = 0; i < NUM_WT; i++) {
13144644f9beSYuri Pankov 		sz = sizeof (collate_subst_t) * subst_count[i];
1315057ca2d4SBaptiste Daroussin 		if (wr_category(subst[i], sz, f) < 0) {
1316057ca2d4SBaptiste Daroussin 			return;
1317057ca2d4SBaptiste Daroussin 		}
1318057ca2d4SBaptiste Daroussin 	}
13194644f9beSYuri Pankov 	sz = sizeof (collate_chain_t) * chain_count;
1320057ca2d4SBaptiste Daroussin 	if (wr_category(chain, sz, f) < 0) {
1321057ca2d4SBaptiste Daroussin 		return;
1322057ca2d4SBaptiste Daroussin 	}
13234644f9beSYuri Pankov 	sz = sizeof (collate_large_t) * large_count;
1324057ca2d4SBaptiste Daroussin 	if (wr_category(large, sz, f) < 0) {
1325057ca2d4SBaptiste Daroussin 		return;
1326057ca2d4SBaptiste Daroussin 	}
1327057ca2d4SBaptiste Daroussin 
1328057ca2d4SBaptiste Daroussin 	close_category(f);
1329057ca2d4SBaptiste Daroussin }
1330