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 #pragma GCC diagnostic push 439057ca2d4SBaptiste Daroussin #pragma GCC diagnostic ignored "-Wcast-qual" 440057ca2d4SBaptiste Daroussin 441057ca2d4SBaptiste Daroussin static int 442057ca2d4SBaptiste Daroussin subst_compare_ref(const void *n1, const void *n2) 443057ca2d4SBaptiste Daroussin { 444057ca2d4SBaptiste Daroussin int32_t *c1 = ((subst_t *)n1)->ref; 445057ca2d4SBaptiste Daroussin int32_t *c2 = ((subst_t *)n2)->ref; 446057ca2d4SBaptiste Daroussin int rv; 447057ca2d4SBaptiste Daroussin 448057ca2d4SBaptiste Daroussin rv = wcscmp((wchar_t *)c1, (wchar_t *)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 #pragma GCC diagnostic pop 455057ca2d4SBaptiste Daroussin 456057ca2d4SBaptiste Daroussin void 457057ca2d4SBaptiste Daroussin init_collate(void) 458057ca2d4SBaptiste Daroussin { 459057ca2d4SBaptiste Daroussin int i; 460057ca2d4SBaptiste Daroussin 4613a444dbdSBaptiste Daroussin RB_INIT(&collsyms); 462057ca2d4SBaptiste Daroussin 4633a444dbdSBaptiste Daroussin RB_INIT(&collundefs); 464057ca2d4SBaptiste Daroussin 4653a444dbdSBaptiste Daroussin RB_INIT(&elem_by_symbol); 466057ca2d4SBaptiste Daroussin 4673a444dbdSBaptiste Daroussin RB_INIT(&elem_by_expand); 4683a444dbdSBaptiste Daroussin 4693a444dbdSBaptiste Daroussin RB_INIT(&collchars); 470057ca2d4SBaptiste Daroussin 471057ca2d4SBaptiste Daroussin for (i = 0; i < COLL_WEIGHTS_MAX; i++) { 4723a444dbdSBaptiste Daroussin RB_INIT(&substs[i]); 4733a444dbdSBaptiste Daroussin RB_INIT(&substs_ref[i]); 4743a444dbdSBaptiste Daroussin RB_INIT(&weights[i]); 475057ca2d4SBaptiste Daroussin nweight[i] = 1; 476057ca2d4SBaptiste Daroussin } 477057ca2d4SBaptiste Daroussin 478057ca2d4SBaptiste Daroussin (void) memset(&collinfo, 0, sizeof (collinfo)); 479057ca2d4SBaptiste Daroussin 480057ca2d4SBaptiste Daroussin /* allocate some initial priorities */ 481057ca2d4SBaptiste Daroussin pri_ignore = new_pri(); 482057ca2d4SBaptiste Daroussin 483057ca2d4SBaptiste Daroussin set_pri(pri_ignore, 0, RESOLVED); 484057ca2d4SBaptiste Daroussin 485057ca2d4SBaptiste Daroussin for (i = 0; i < COLL_WEIGHTS_MAX; i++) { 486057ca2d4SBaptiste Daroussin pri_undefined[i] = new_pri(); 487057ca2d4SBaptiste Daroussin 488057ca2d4SBaptiste Daroussin /* we will override this later */ 489057ca2d4SBaptiste Daroussin set_pri(pri_undefined[i], COLLATE_MAX_PRIORITY, UNKNOWN); 490057ca2d4SBaptiste Daroussin } 491057ca2d4SBaptiste Daroussin } 492057ca2d4SBaptiste Daroussin 493057ca2d4SBaptiste Daroussin void 494057ca2d4SBaptiste Daroussin define_collsym(char *name) 495057ca2d4SBaptiste Daroussin { 496057ca2d4SBaptiste Daroussin collsym_t *sym; 497057ca2d4SBaptiste Daroussin 498057ca2d4SBaptiste Daroussin if ((sym = calloc(sizeof (*sym), 1)) == NULL) { 499057ca2d4SBaptiste Daroussin fprintf(stderr,"out of memory"); 500057ca2d4SBaptiste Daroussin return; 501057ca2d4SBaptiste Daroussin } 502057ca2d4SBaptiste Daroussin sym->name = name; 503057ca2d4SBaptiste Daroussin sym->ref = new_pri(); 504057ca2d4SBaptiste Daroussin 5053a444dbdSBaptiste Daroussin if (RB_FIND(collsyms, &collsyms, sym) != NULL) { 506057ca2d4SBaptiste Daroussin /* 507057ca2d4SBaptiste Daroussin * This should never happen because we are only called 508057ca2d4SBaptiste Daroussin * for undefined symbols. 509057ca2d4SBaptiste Daroussin */ 510057ca2d4SBaptiste Daroussin INTERR; 511057ca2d4SBaptiste Daroussin return; 512057ca2d4SBaptiste Daroussin } 5133a444dbdSBaptiste Daroussin RB_INSERT(collsyms, &collsyms, sym); 514057ca2d4SBaptiste Daroussin } 515057ca2d4SBaptiste Daroussin 516057ca2d4SBaptiste Daroussin collsym_t * 517057ca2d4SBaptiste Daroussin lookup_collsym(char *name) 518057ca2d4SBaptiste Daroussin { 519057ca2d4SBaptiste Daroussin collsym_t srch; 520057ca2d4SBaptiste Daroussin 521057ca2d4SBaptiste Daroussin srch.name = name; 5223a444dbdSBaptiste Daroussin return (RB_FIND(collsyms, &collsyms, &srch)); 523057ca2d4SBaptiste Daroussin } 524057ca2d4SBaptiste Daroussin 525057ca2d4SBaptiste Daroussin collelem_t * 526057ca2d4SBaptiste Daroussin lookup_collelem(char *symbol) 527057ca2d4SBaptiste Daroussin { 528057ca2d4SBaptiste Daroussin collelem_t srch; 529057ca2d4SBaptiste Daroussin 530057ca2d4SBaptiste Daroussin srch.symbol = symbol; 5313a444dbdSBaptiste Daroussin return (RB_FIND(elem_by_symbol, &elem_by_symbol, &srch)); 532057ca2d4SBaptiste Daroussin } 533057ca2d4SBaptiste Daroussin 534057ca2d4SBaptiste Daroussin static collundef_t * 535057ca2d4SBaptiste Daroussin get_collundef(char *name) 536057ca2d4SBaptiste Daroussin { 537057ca2d4SBaptiste Daroussin collundef_t srch; 538057ca2d4SBaptiste Daroussin collundef_t *ud; 539057ca2d4SBaptiste Daroussin int i; 540057ca2d4SBaptiste Daroussin 541057ca2d4SBaptiste Daroussin srch.name = name; 5423a444dbdSBaptiste Daroussin if ((ud = RB_FIND(collundefs, &collundefs, &srch)) == NULL) { 543057ca2d4SBaptiste Daroussin if (((ud = calloc(sizeof (*ud), 1)) == NULL) || 544057ca2d4SBaptiste Daroussin ((ud->name = strdup(name)) == NULL)) { 545057ca2d4SBaptiste Daroussin fprintf(stderr,"out of memory"); 546057ca2d4SBaptiste Daroussin return (NULL); 547057ca2d4SBaptiste Daroussin } 548057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 549057ca2d4SBaptiste Daroussin ud->ref[i] = new_pri(); 550057ca2d4SBaptiste Daroussin } 5513a444dbdSBaptiste Daroussin RB_INSERT(collundefs, &collundefs, ud); 552057ca2d4SBaptiste Daroussin } 553057ca2d4SBaptiste Daroussin add_charmap_undefined(name); 554057ca2d4SBaptiste Daroussin return (ud); 555057ca2d4SBaptiste Daroussin } 556057ca2d4SBaptiste Daroussin 557057ca2d4SBaptiste Daroussin static collchar_t * 558057ca2d4SBaptiste Daroussin get_collchar(wchar_t wc, int create) 559057ca2d4SBaptiste Daroussin { 560057ca2d4SBaptiste Daroussin collchar_t srch; 561057ca2d4SBaptiste Daroussin collchar_t *cc; 562057ca2d4SBaptiste Daroussin int i; 563057ca2d4SBaptiste Daroussin 564057ca2d4SBaptiste Daroussin srch.wc = wc; 5653a444dbdSBaptiste Daroussin cc = RB_FIND(collchars, &collchars, &srch); 566057ca2d4SBaptiste Daroussin if ((cc == NULL) && create) { 567057ca2d4SBaptiste Daroussin if ((cc = calloc(sizeof (*cc), 1)) == NULL) { 568057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 569057ca2d4SBaptiste Daroussin return (NULL); 570057ca2d4SBaptiste Daroussin } 571057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 572057ca2d4SBaptiste Daroussin cc->ref[i] = new_pri(); 573057ca2d4SBaptiste Daroussin } 574057ca2d4SBaptiste Daroussin cc->wc = wc; 5753a444dbdSBaptiste Daroussin RB_INSERT(collchars, &collchars, cc); 576057ca2d4SBaptiste Daroussin } 577057ca2d4SBaptiste Daroussin return (cc); 578057ca2d4SBaptiste Daroussin } 579057ca2d4SBaptiste Daroussin 580057ca2d4SBaptiste Daroussin void 581057ca2d4SBaptiste Daroussin end_order_collsym(collsym_t *sym) 582057ca2d4SBaptiste Daroussin { 583057ca2d4SBaptiste Daroussin start_order(T_COLLSYM); 584057ca2d4SBaptiste Daroussin /* update the weight */ 585057ca2d4SBaptiste Daroussin 586057ca2d4SBaptiste Daroussin set_pri(sym->ref, nextpri, RESOLVED); 587057ca2d4SBaptiste Daroussin nextpri++; 588057ca2d4SBaptiste Daroussin } 589057ca2d4SBaptiste Daroussin 590057ca2d4SBaptiste Daroussin void 591057ca2d4SBaptiste Daroussin end_order(void) 592057ca2d4SBaptiste Daroussin { 593057ca2d4SBaptiste Daroussin int i; 594057ca2d4SBaptiste Daroussin int32_t pri; 595057ca2d4SBaptiste Daroussin int32_t ref; 596057ca2d4SBaptiste Daroussin collpri_t *p; 597057ca2d4SBaptiste Daroussin 598057ca2d4SBaptiste Daroussin /* advance the priority/weight */ 599057ca2d4SBaptiste Daroussin pri = nextpri; 600057ca2d4SBaptiste Daroussin 601057ca2d4SBaptiste Daroussin switch (currorder) { 602057ca2d4SBaptiste Daroussin case T_CHAR: 603057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 604057ca2d4SBaptiste Daroussin if (((ref = order_weights[i]) < 0) || 605057ca2d4SBaptiste Daroussin ((p = get_pri(ref)) == NULL) || 606057ca2d4SBaptiste Daroussin (p->pri == -1)) { 607057ca2d4SBaptiste Daroussin /* unspecified weight is a self reference */ 608057ca2d4SBaptiste Daroussin set_pri(currchar->ref[i], pri, RESOLVED); 609057ca2d4SBaptiste Daroussin } else { 610057ca2d4SBaptiste Daroussin set_pri(currchar->ref[i], ref, REFER); 611057ca2d4SBaptiste Daroussin } 612057ca2d4SBaptiste Daroussin order_weights[i] = -1; 613057ca2d4SBaptiste Daroussin } 614057ca2d4SBaptiste Daroussin 615057ca2d4SBaptiste Daroussin /* leave a cookie trail in case next symbol is ellipsis */ 616057ca2d4SBaptiste Daroussin ellipsis_start = currchar->wc + 1; 617057ca2d4SBaptiste Daroussin currchar = NULL; 618057ca2d4SBaptiste Daroussin break; 619057ca2d4SBaptiste Daroussin 620057ca2d4SBaptiste Daroussin case T_ELLIPSIS: 621057ca2d4SBaptiste Daroussin /* save off the weights were we can find them */ 622057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 623057ca2d4SBaptiste Daroussin ellipsis_weights[i] = order_weights[i]; 624057ca2d4SBaptiste Daroussin order_weights[i] = -1; 625057ca2d4SBaptiste Daroussin } 626057ca2d4SBaptiste Daroussin break; 627057ca2d4SBaptiste Daroussin 628057ca2d4SBaptiste Daroussin case T_COLLELEM: 629057ca2d4SBaptiste Daroussin if (currelem == NULL) { 630057ca2d4SBaptiste Daroussin INTERR; 631057ca2d4SBaptiste Daroussin } else { 632057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 633057ca2d4SBaptiste Daroussin 634057ca2d4SBaptiste Daroussin if (((ref = order_weights[i]) < 0) || 635057ca2d4SBaptiste Daroussin ((p = get_pri(ref)) == NULL) || 636057ca2d4SBaptiste Daroussin (p->pri == -1)) { 637057ca2d4SBaptiste Daroussin set_pri(currelem->ref[i], pri, 638057ca2d4SBaptiste Daroussin RESOLVED); 639057ca2d4SBaptiste Daroussin } else { 640057ca2d4SBaptiste Daroussin set_pri(currelem->ref[i], ref, REFER); 641057ca2d4SBaptiste Daroussin } 642057ca2d4SBaptiste Daroussin order_weights[i] = -1; 643057ca2d4SBaptiste Daroussin } 644057ca2d4SBaptiste Daroussin } 645057ca2d4SBaptiste Daroussin break; 646057ca2d4SBaptiste Daroussin 647057ca2d4SBaptiste Daroussin case T_UNDEFINED: 648057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 649057ca2d4SBaptiste Daroussin if (((ref = order_weights[i]) < 0) || 650057ca2d4SBaptiste Daroussin ((p = get_pri(ref)) == NULL) || 651057ca2d4SBaptiste Daroussin (p->pri == -1)) { 652057ca2d4SBaptiste Daroussin set_pri(pri_undefined[i], -1, RESOLVED); 653057ca2d4SBaptiste Daroussin } else { 654057ca2d4SBaptiste Daroussin set_pri(pri_undefined[i], ref, REFER); 655057ca2d4SBaptiste Daroussin } 656057ca2d4SBaptiste Daroussin order_weights[i] = -1; 657057ca2d4SBaptiste Daroussin } 658057ca2d4SBaptiste Daroussin break; 659057ca2d4SBaptiste Daroussin 660057ca2d4SBaptiste Daroussin case T_SYMBOL: 661057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 662057ca2d4SBaptiste Daroussin if (((ref = order_weights[i]) < 0) || 663057ca2d4SBaptiste Daroussin ((p = get_pri(ref)) == NULL) || 664057ca2d4SBaptiste Daroussin (p->pri == -1)) { 665057ca2d4SBaptiste Daroussin set_pri(currundef->ref[i], pri, RESOLVED); 666057ca2d4SBaptiste Daroussin } else { 667057ca2d4SBaptiste Daroussin set_pri(currundef->ref[i], ref, REFER); 668057ca2d4SBaptiste Daroussin } 669057ca2d4SBaptiste Daroussin order_weights[i] = -1; 670057ca2d4SBaptiste Daroussin } 671057ca2d4SBaptiste Daroussin break; 672057ca2d4SBaptiste Daroussin 673057ca2d4SBaptiste Daroussin default: 674057ca2d4SBaptiste Daroussin INTERR; 675057ca2d4SBaptiste Daroussin } 676057ca2d4SBaptiste Daroussin 677057ca2d4SBaptiste Daroussin nextpri++; 678057ca2d4SBaptiste Daroussin } 679057ca2d4SBaptiste Daroussin 680057ca2d4SBaptiste Daroussin static void 681057ca2d4SBaptiste Daroussin start_order(int type) 682057ca2d4SBaptiste Daroussin { 683057ca2d4SBaptiste Daroussin int i; 684057ca2d4SBaptiste Daroussin 685057ca2d4SBaptiste Daroussin lastorder = currorder; 686057ca2d4SBaptiste Daroussin currorder = type; 687057ca2d4SBaptiste Daroussin 688057ca2d4SBaptiste Daroussin /* this is used to protect ELLIPSIS processing */ 689057ca2d4SBaptiste Daroussin if ((lastorder == T_ELLIPSIS) && (type != T_CHAR)) { 690057ca2d4SBaptiste Daroussin fprintf(stderr, "character value expected"); 691057ca2d4SBaptiste Daroussin } 692057ca2d4SBaptiste Daroussin 693057ca2d4SBaptiste Daroussin for (i = 0; i < COLL_WEIGHTS_MAX; i++) { 694057ca2d4SBaptiste Daroussin order_weights[i] = -1; 695057ca2d4SBaptiste Daroussin } 696057ca2d4SBaptiste Daroussin curr_weight = 0; 697057ca2d4SBaptiste Daroussin } 698057ca2d4SBaptiste Daroussin 699057ca2d4SBaptiste Daroussin void 700057ca2d4SBaptiste Daroussin start_order_undefined(void) 701057ca2d4SBaptiste Daroussin { 702057ca2d4SBaptiste Daroussin start_order(T_UNDEFINED); 703057ca2d4SBaptiste Daroussin } 704057ca2d4SBaptiste Daroussin 705057ca2d4SBaptiste Daroussin void 706057ca2d4SBaptiste Daroussin start_order_symbol(char *name) 707057ca2d4SBaptiste Daroussin { 708057ca2d4SBaptiste Daroussin currundef = get_collundef(name); 709057ca2d4SBaptiste Daroussin start_order(T_SYMBOL); 710057ca2d4SBaptiste Daroussin } 711057ca2d4SBaptiste Daroussin 712057ca2d4SBaptiste Daroussin void 713057ca2d4SBaptiste Daroussin start_order_char(wchar_t wc) 714057ca2d4SBaptiste Daroussin { 715057ca2d4SBaptiste Daroussin collchar_t *cc; 716057ca2d4SBaptiste Daroussin int32_t ref; 717057ca2d4SBaptiste Daroussin 718057ca2d4SBaptiste Daroussin start_order(T_CHAR); 719057ca2d4SBaptiste Daroussin 720057ca2d4SBaptiste Daroussin /* 721057ca2d4SBaptiste Daroussin * If we last saw an ellipsis, then we need to close the range. 722057ca2d4SBaptiste Daroussin * Handle that here. Note that we have to be careful because the 723057ca2d4SBaptiste Daroussin * items *inside* the range are treated exclusiveley to the items 724057ca2d4SBaptiste Daroussin * outside of the range. The ends of the range can have quite 725057ca2d4SBaptiste Daroussin * different weights than the range members. 726057ca2d4SBaptiste Daroussin */ 727057ca2d4SBaptiste Daroussin if (lastorder == T_ELLIPSIS) { 728057ca2d4SBaptiste Daroussin int i; 729057ca2d4SBaptiste Daroussin 730057ca2d4SBaptiste Daroussin if (wc < ellipsis_start) { 731057ca2d4SBaptiste Daroussin fprintf(stderr, "malformed range!"); 732057ca2d4SBaptiste Daroussin return; 733057ca2d4SBaptiste Daroussin } 734057ca2d4SBaptiste Daroussin while (ellipsis_start < wc) { 735057ca2d4SBaptiste Daroussin /* 736057ca2d4SBaptiste Daroussin * pick all of the saved weights for the 737057ca2d4SBaptiste Daroussin * ellipsis. note that -1 encodes for the 738057ca2d4SBaptiste Daroussin * ellipsis itself, which means to take the 739057ca2d4SBaptiste Daroussin * current relative priority. 740057ca2d4SBaptiste Daroussin */ 741057ca2d4SBaptiste Daroussin if ((cc = get_collchar(ellipsis_start, 1)) == NULL) { 742057ca2d4SBaptiste Daroussin INTERR; 743057ca2d4SBaptiste Daroussin return; 744057ca2d4SBaptiste Daroussin } 745057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 746057ca2d4SBaptiste Daroussin collpri_t *p; 747057ca2d4SBaptiste Daroussin if (((ref = ellipsis_weights[i]) == -1) || 748057ca2d4SBaptiste Daroussin ((p = get_pri(ref)) == NULL) || 749057ca2d4SBaptiste Daroussin (p->pri == -1)) { 750057ca2d4SBaptiste Daroussin set_pri(cc->ref[i], nextpri, RESOLVED); 751057ca2d4SBaptiste Daroussin } else { 752057ca2d4SBaptiste Daroussin set_pri(cc->ref[i], ref, REFER); 753057ca2d4SBaptiste Daroussin } 754057ca2d4SBaptiste Daroussin ellipsis_weights[i] = 0; 755057ca2d4SBaptiste Daroussin } 756057ca2d4SBaptiste Daroussin ellipsis_start++; 757057ca2d4SBaptiste Daroussin nextpri++; 758057ca2d4SBaptiste Daroussin } 759057ca2d4SBaptiste Daroussin } 760057ca2d4SBaptiste Daroussin 761057ca2d4SBaptiste Daroussin currchar = get_collchar(wc, 1); 762057ca2d4SBaptiste Daroussin } 763057ca2d4SBaptiste Daroussin 764057ca2d4SBaptiste Daroussin void 765057ca2d4SBaptiste Daroussin start_order_collelem(collelem_t *e) 766057ca2d4SBaptiste Daroussin { 767057ca2d4SBaptiste Daroussin start_order(T_COLLELEM); 768057ca2d4SBaptiste Daroussin currelem = e; 769057ca2d4SBaptiste Daroussin } 770057ca2d4SBaptiste Daroussin 771057ca2d4SBaptiste Daroussin void 772057ca2d4SBaptiste Daroussin start_order_ellipsis(void) 773057ca2d4SBaptiste Daroussin { 774057ca2d4SBaptiste Daroussin int i; 775057ca2d4SBaptiste Daroussin 776057ca2d4SBaptiste Daroussin start_order(T_ELLIPSIS); 777057ca2d4SBaptiste Daroussin 778057ca2d4SBaptiste Daroussin if (lastorder != T_CHAR) { 779057ca2d4SBaptiste Daroussin fprintf(stderr, "illegal starting point for range"); 780057ca2d4SBaptiste Daroussin return; 781057ca2d4SBaptiste Daroussin } 782057ca2d4SBaptiste Daroussin 783057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 784057ca2d4SBaptiste Daroussin ellipsis_weights[i] = order_weights[i]; 785057ca2d4SBaptiste Daroussin } 786057ca2d4SBaptiste Daroussin } 787057ca2d4SBaptiste Daroussin 788057ca2d4SBaptiste Daroussin void 789057ca2d4SBaptiste Daroussin define_collelem(char *name, wchar_t *wcs) 790057ca2d4SBaptiste Daroussin { 791057ca2d4SBaptiste Daroussin collelem_t *e; 792057ca2d4SBaptiste Daroussin int i; 793057ca2d4SBaptiste Daroussin 794057ca2d4SBaptiste Daroussin if (wcslen(wcs) >= COLLATE_STR_LEN) { 795057ca2d4SBaptiste Daroussin fprintf(stderr,"expanded collation element too long"); 796057ca2d4SBaptiste Daroussin return; 797057ca2d4SBaptiste Daroussin } 798057ca2d4SBaptiste Daroussin 799057ca2d4SBaptiste Daroussin if ((e = calloc(sizeof (*e), 1)) == NULL) { 800057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 801057ca2d4SBaptiste Daroussin return; 802057ca2d4SBaptiste Daroussin } 803057ca2d4SBaptiste Daroussin e->expand = wcs; 804057ca2d4SBaptiste Daroussin e->symbol = name; 805057ca2d4SBaptiste Daroussin 806057ca2d4SBaptiste Daroussin /* 807057ca2d4SBaptiste Daroussin * This is executed before the order statement, so we don't 808057ca2d4SBaptiste Daroussin * know how many priorities we *really* need. We allocate one 809057ca2d4SBaptiste Daroussin * for each possible weight. Not a big deal, as collating-elements 810057ca2d4SBaptiste Daroussin * prove to be quite rare. 811057ca2d4SBaptiste Daroussin */ 812057ca2d4SBaptiste Daroussin for (i = 0; i < COLL_WEIGHTS_MAX; i++) { 813057ca2d4SBaptiste Daroussin e->ref[i] = new_pri(); 814057ca2d4SBaptiste Daroussin } 815057ca2d4SBaptiste Daroussin 816057ca2d4SBaptiste Daroussin /* A character sequence can only reduce to one element. */ 8173a444dbdSBaptiste Daroussin if ((RB_FIND(elem_by_symbol, &elem_by_symbol, e) != NULL) || 8183a444dbdSBaptiste Daroussin (RB_FIND(elem_by_expand, &elem_by_expand, e) != NULL)) { 819057ca2d4SBaptiste Daroussin fprintf(stderr, "duplicate collating element definition"); 820057ca2d4SBaptiste Daroussin return; 821057ca2d4SBaptiste Daroussin } 8223a444dbdSBaptiste Daroussin RB_INSERT(elem_by_symbol, &elem_by_symbol, e); 8233a444dbdSBaptiste Daroussin RB_INSERT(elem_by_expand, &elem_by_expand, e); 824057ca2d4SBaptiste Daroussin } 825057ca2d4SBaptiste Daroussin 826057ca2d4SBaptiste Daroussin void 827057ca2d4SBaptiste Daroussin add_order_bit(int kw) 828057ca2d4SBaptiste Daroussin { 829057ca2d4SBaptiste Daroussin uint8_t bit = DIRECTIVE_UNDEF; 830057ca2d4SBaptiste Daroussin 831057ca2d4SBaptiste Daroussin switch (kw) { 832057ca2d4SBaptiste Daroussin case T_FORWARD: 833057ca2d4SBaptiste Daroussin bit = DIRECTIVE_FORWARD; 834057ca2d4SBaptiste Daroussin break; 835057ca2d4SBaptiste Daroussin case T_BACKWARD: 836057ca2d4SBaptiste Daroussin bit = DIRECTIVE_BACKWARD; 837057ca2d4SBaptiste Daroussin break; 838057ca2d4SBaptiste Daroussin case T_POSITION: 839057ca2d4SBaptiste Daroussin bit = DIRECTIVE_POSITION; 840057ca2d4SBaptiste Daroussin break; 841057ca2d4SBaptiste Daroussin default: 842057ca2d4SBaptiste Daroussin INTERR; 843057ca2d4SBaptiste Daroussin break; 844057ca2d4SBaptiste Daroussin } 845057ca2d4SBaptiste Daroussin collinfo.directive[collinfo.directive_count] |= bit; 846057ca2d4SBaptiste Daroussin } 847057ca2d4SBaptiste Daroussin 848057ca2d4SBaptiste Daroussin void 849057ca2d4SBaptiste Daroussin add_order_directive(void) 850057ca2d4SBaptiste Daroussin { 851057ca2d4SBaptiste Daroussin if (collinfo.directive_count >= COLL_WEIGHTS_MAX) { 852057ca2d4SBaptiste Daroussin fprintf(stderr,"too many directives (max %d)", COLL_WEIGHTS_MAX); 853057ca2d4SBaptiste Daroussin } 854057ca2d4SBaptiste Daroussin collinfo.directive_count++; 855057ca2d4SBaptiste Daroussin } 856057ca2d4SBaptiste Daroussin 857057ca2d4SBaptiste Daroussin static void 858057ca2d4SBaptiste Daroussin add_order_pri(int32_t ref) 859057ca2d4SBaptiste Daroussin { 860057ca2d4SBaptiste Daroussin if (curr_weight >= NUM_WT) { 861057ca2d4SBaptiste Daroussin fprintf(stderr,"too many weights (max %d)", NUM_WT); 862057ca2d4SBaptiste Daroussin return; 863057ca2d4SBaptiste Daroussin } 864057ca2d4SBaptiste Daroussin order_weights[curr_weight] = ref; 865057ca2d4SBaptiste Daroussin curr_weight++; 866057ca2d4SBaptiste Daroussin } 867057ca2d4SBaptiste Daroussin 868057ca2d4SBaptiste Daroussin void 869057ca2d4SBaptiste Daroussin add_order_collsym(collsym_t *s) 870057ca2d4SBaptiste Daroussin { 871057ca2d4SBaptiste Daroussin add_order_pri(s->ref); 872057ca2d4SBaptiste Daroussin } 873057ca2d4SBaptiste Daroussin 874057ca2d4SBaptiste Daroussin void 875057ca2d4SBaptiste Daroussin add_order_char(wchar_t wc) 876057ca2d4SBaptiste Daroussin { 877057ca2d4SBaptiste Daroussin collchar_t *cc; 878057ca2d4SBaptiste Daroussin 879057ca2d4SBaptiste Daroussin if ((cc = get_collchar(wc, 1)) == NULL) { 880057ca2d4SBaptiste Daroussin INTERR; 881057ca2d4SBaptiste Daroussin return; 882057ca2d4SBaptiste Daroussin } 883057ca2d4SBaptiste Daroussin 884057ca2d4SBaptiste Daroussin add_order_pri(cc->ref[curr_weight]); 885057ca2d4SBaptiste Daroussin } 886057ca2d4SBaptiste Daroussin 887057ca2d4SBaptiste Daroussin void 888057ca2d4SBaptiste Daroussin add_order_collelem(collelem_t *e) 889057ca2d4SBaptiste Daroussin { 890057ca2d4SBaptiste Daroussin add_order_pri(e->ref[curr_weight]); 891057ca2d4SBaptiste Daroussin } 892057ca2d4SBaptiste Daroussin 893057ca2d4SBaptiste Daroussin void 894057ca2d4SBaptiste Daroussin add_order_ignore(void) 895057ca2d4SBaptiste Daroussin { 896057ca2d4SBaptiste Daroussin add_order_pri(pri_ignore); 897057ca2d4SBaptiste Daroussin } 898057ca2d4SBaptiste Daroussin 899057ca2d4SBaptiste Daroussin void 900057ca2d4SBaptiste Daroussin add_order_symbol(char *sym) 901057ca2d4SBaptiste Daroussin { 902057ca2d4SBaptiste Daroussin collundef_t *c; 903057ca2d4SBaptiste Daroussin if ((c = get_collundef(sym)) == NULL) { 904057ca2d4SBaptiste Daroussin INTERR; 905057ca2d4SBaptiste Daroussin return; 906057ca2d4SBaptiste Daroussin } 907057ca2d4SBaptiste Daroussin add_order_pri(c->ref[curr_weight]); 908057ca2d4SBaptiste Daroussin } 909057ca2d4SBaptiste Daroussin 910057ca2d4SBaptiste Daroussin void 911057ca2d4SBaptiste Daroussin add_order_ellipsis(void) 912057ca2d4SBaptiste Daroussin { 913057ca2d4SBaptiste Daroussin /* special NULL value indicates self reference */ 914057ca2d4SBaptiste Daroussin add_order_pri(0); 915057ca2d4SBaptiste Daroussin } 916057ca2d4SBaptiste Daroussin 917057ca2d4SBaptiste Daroussin void 918057ca2d4SBaptiste Daroussin add_order_subst(void) 919057ca2d4SBaptiste Daroussin { 920057ca2d4SBaptiste Daroussin subst_t srch; 921057ca2d4SBaptiste Daroussin subst_t *s; 922057ca2d4SBaptiste Daroussin int i; 923057ca2d4SBaptiste Daroussin 924057ca2d4SBaptiste Daroussin (void) memset(&srch, 0, sizeof (srch)); 925057ca2d4SBaptiste Daroussin for (i = 0; i < curr_subst; i++) { 926057ca2d4SBaptiste Daroussin srch.ref[i] = subst_weights[i]; 927057ca2d4SBaptiste Daroussin subst_weights[i] = 0; 928057ca2d4SBaptiste Daroussin } 9293a444dbdSBaptiste Daroussin s = RB_FIND(substs_ref, &substs_ref[curr_weight], &srch); 930057ca2d4SBaptiste Daroussin 931057ca2d4SBaptiste Daroussin if (s == NULL) { 932057ca2d4SBaptiste Daroussin if ((s = calloc(sizeof (*s), 1)) == NULL) { 933057ca2d4SBaptiste Daroussin fprintf(stderr,"out of memory"); 934057ca2d4SBaptiste Daroussin return; 935057ca2d4SBaptiste Daroussin } 936057ca2d4SBaptiste Daroussin s->key = new_pri(); 937057ca2d4SBaptiste Daroussin 938057ca2d4SBaptiste Daroussin /* 939057ca2d4SBaptiste Daroussin * We use a self reference for our key, but we set a 940057ca2d4SBaptiste Daroussin * high bit to indicate that this is a substitution 941057ca2d4SBaptiste Daroussin * reference. This will expedite table lookups later, 942057ca2d4SBaptiste Daroussin * and prevent table lookups for situations that don't 943057ca2d4SBaptiste Daroussin * require it. (In short, its a big win, because we 944057ca2d4SBaptiste Daroussin * can skip a lot of binary searching.) 945057ca2d4SBaptiste Daroussin */ 946057ca2d4SBaptiste Daroussin set_pri(s->key, 947057ca2d4SBaptiste Daroussin (nextsubst[curr_weight] | COLLATE_SUBST_PRIORITY), 948057ca2d4SBaptiste Daroussin RESOLVED); 949057ca2d4SBaptiste Daroussin nextsubst[curr_weight] += 1; 950057ca2d4SBaptiste Daroussin 951057ca2d4SBaptiste Daroussin for (i = 0; i < curr_subst; i++) { 952057ca2d4SBaptiste Daroussin s->ref[i] = srch.ref[i]; 953057ca2d4SBaptiste Daroussin } 954057ca2d4SBaptiste Daroussin 9553a444dbdSBaptiste Daroussin RB_INSERT(substs_ref, &substs_ref[curr_weight], s); 956057ca2d4SBaptiste Daroussin 9573a444dbdSBaptiste Daroussin if (RB_FIND(substs, &substs[curr_weight], s) != NULL) { 958057ca2d4SBaptiste Daroussin INTERR; 959057ca2d4SBaptiste Daroussin return; 960057ca2d4SBaptiste Daroussin } 9613a444dbdSBaptiste Daroussin RB_INSERT(substs, &substs[curr_weight], s); 962057ca2d4SBaptiste Daroussin } 963057ca2d4SBaptiste Daroussin curr_subst = 0; 964057ca2d4SBaptiste Daroussin 965057ca2d4SBaptiste Daroussin 966057ca2d4SBaptiste Daroussin /* 967057ca2d4SBaptiste Daroussin * We are using the current (unique) priority as a search key 968057ca2d4SBaptiste Daroussin * in the substitution table. 969057ca2d4SBaptiste Daroussin */ 970057ca2d4SBaptiste Daroussin add_order_pri(s->key); 971057ca2d4SBaptiste Daroussin } 972057ca2d4SBaptiste Daroussin 973057ca2d4SBaptiste Daroussin static void 974057ca2d4SBaptiste Daroussin add_subst_pri(int32_t ref) 975057ca2d4SBaptiste Daroussin { 976057ca2d4SBaptiste Daroussin if (curr_subst >= COLLATE_STR_LEN) { 977057ca2d4SBaptiste Daroussin fprintf(stderr,"substitution string is too long"); 978057ca2d4SBaptiste Daroussin return; 979057ca2d4SBaptiste Daroussin } 980057ca2d4SBaptiste Daroussin subst_weights[curr_subst] = ref; 981057ca2d4SBaptiste Daroussin curr_subst++; 982057ca2d4SBaptiste Daroussin } 983057ca2d4SBaptiste Daroussin 984057ca2d4SBaptiste Daroussin void 985057ca2d4SBaptiste Daroussin add_subst_char(wchar_t wc) 986057ca2d4SBaptiste Daroussin { 987057ca2d4SBaptiste Daroussin collchar_t *cc; 988057ca2d4SBaptiste Daroussin 989057ca2d4SBaptiste Daroussin 990057ca2d4SBaptiste Daroussin if (((cc = get_collchar(wc, 1)) == NULL) || 991057ca2d4SBaptiste Daroussin (cc->wc != wc)) { 992057ca2d4SBaptiste Daroussin INTERR; 993057ca2d4SBaptiste Daroussin return; 994057ca2d4SBaptiste Daroussin } 995057ca2d4SBaptiste Daroussin /* we take the weight for the character at that position */ 996057ca2d4SBaptiste Daroussin add_subst_pri(cc->ref[curr_weight]); 997057ca2d4SBaptiste Daroussin } 998057ca2d4SBaptiste Daroussin 999057ca2d4SBaptiste Daroussin void 1000057ca2d4SBaptiste Daroussin add_subst_collelem(collelem_t *e) 1001057ca2d4SBaptiste Daroussin { 1002057ca2d4SBaptiste Daroussin add_subst_pri(e->ref[curr_weight]); 1003057ca2d4SBaptiste Daroussin } 1004057ca2d4SBaptiste Daroussin 1005057ca2d4SBaptiste Daroussin void 1006057ca2d4SBaptiste Daroussin add_subst_collsym(collsym_t *s) 1007057ca2d4SBaptiste Daroussin { 1008057ca2d4SBaptiste Daroussin add_subst_pri(s->ref); 1009057ca2d4SBaptiste Daroussin } 1010057ca2d4SBaptiste Daroussin 1011057ca2d4SBaptiste Daroussin void 1012057ca2d4SBaptiste Daroussin add_subst_symbol(char *ptr) 1013057ca2d4SBaptiste Daroussin { 1014057ca2d4SBaptiste Daroussin collundef_t *cu; 1015057ca2d4SBaptiste Daroussin 1016057ca2d4SBaptiste Daroussin if ((cu = get_collundef(ptr)) != NULL) { 1017057ca2d4SBaptiste Daroussin add_subst_pri(cu->ref[curr_weight]); 1018057ca2d4SBaptiste Daroussin } 1019057ca2d4SBaptiste Daroussin } 1020057ca2d4SBaptiste Daroussin 1021057ca2d4SBaptiste Daroussin void 1022057ca2d4SBaptiste Daroussin add_weight(int32_t ref, int pass) 1023057ca2d4SBaptiste Daroussin { 1024057ca2d4SBaptiste Daroussin weight_t srch; 1025057ca2d4SBaptiste Daroussin weight_t *w; 1026057ca2d4SBaptiste Daroussin 1027057ca2d4SBaptiste Daroussin srch.pri = resolve_pri(ref); 1028057ca2d4SBaptiste Daroussin 1029057ca2d4SBaptiste Daroussin /* No translation of ignores */ 1030057ca2d4SBaptiste Daroussin if (srch.pri == 0) 1031057ca2d4SBaptiste Daroussin return; 1032057ca2d4SBaptiste Daroussin 1033057ca2d4SBaptiste Daroussin /* Substitution priorities are not weights */ 1034057ca2d4SBaptiste Daroussin if (srch.pri & COLLATE_SUBST_PRIORITY) 1035057ca2d4SBaptiste Daroussin return; 1036057ca2d4SBaptiste Daroussin 10373a444dbdSBaptiste Daroussin if (RB_FIND(weights, &weights[pass], &srch) != NULL) 1038057ca2d4SBaptiste Daroussin return; 1039057ca2d4SBaptiste Daroussin 1040057ca2d4SBaptiste Daroussin if ((w = calloc(sizeof (*w), 1)) == NULL) { 1041057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 1042057ca2d4SBaptiste Daroussin return; 1043057ca2d4SBaptiste Daroussin } 1044057ca2d4SBaptiste Daroussin w->pri = srch.pri; 10453a444dbdSBaptiste Daroussin RB_INSERT(weights, &weights[pass], w); 1046057ca2d4SBaptiste Daroussin } 1047057ca2d4SBaptiste Daroussin 1048057ca2d4SBaptiste Daroussin void 1049057ca2d4SBaptiste Daroussin add_weights(int32_t *refs) 1050057ca2d4SBaptiste Daroussin { 1051057ca2d4SBaptiste Daroussin int i; 1052057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1053057ca2d4SBaptiste Daroussin add_weight(refs[i], i); 1054057ca2d4SBaptiste Daroussin } 1055057ca2d4SBaptiste Daroussin } 1056057ca2d4SBaptiste Daroussin 1057057ca2d4SBaptiste Daroussin int32_t 1058057ca2d4SBaptiste Daroussin get_weight(int32_t ref, int pass) 1059057ca2d4SBaptiste Daroussin { 1060057ca2d4SBaptiste Daroussin weight_t srch; 1061057ca2d4SBaptiste Daroussin weight_t *w; 1062057ca2d4SBaptiste Daroussin int32_t pri; 1063057ca2d4SBaptiste Daroussin 1064057ca2d4SBaptiste Daroussin pri = resolve_pri(ref); 1065057ca2d4SBaptiste Daroussin if (pri & COLLATE_SUBST_PRIORITY) { 1066057ca2d4SBaptiste Daroussin return (pri); 1067057ca2d4SBaptiste Daroussin } 1068057ca2d4SBaptiste Daroussin if (pri <= 0) { 1069057ca2d4SBaptiste Daroussin return (pri); 1070057ca2d4SBaptiste Daroussin } 1071057ca2d4SBaptiste Daroussin srch.pri = pri; 10723a444dbdSBaptiste Daroussin if ((w = RB_FIND(weights, &weights[pass], &srch)) == NULL) { 1073057ca2d4SBaptiste Daroussin INTERR; 1074057ca2d4SBaptiste Daroussin return (-1); 1075057ca2d4SBaptiste Daroussin } 1076057ca2d4SBaptiste Daroussin return (w->opt); 1077057ca2d4SBaptiste Daroussin } 1078057ca2d4SBaptiste Daroussin 1079057ca2d4SBaptiste Daroussin wchar_t * 1080057ca2d4SBaptiste Daroussin wsncpy(wchar_t *s1, const wchar_t *s2, size_t n) 1081057ca2d4SBaptiste Daroussin { 1082057ca2d4SBaptiste Daroussin wchar_t *os1 = s1; 1083057ca2d4SBaptiste Daroussin 1084057ca2d4SBaptiste Daroussin n++; 1085057ca2d4SBaptiste Daroussin while (--n > 0 && (*s1++ = *s2++) != 0) 1086057ca2d4SBaptiste Daroussin continue; 1087057ca2d4SBaptiste Daroussin if (n > 0) 1088057ca2d4SBaptiste Daroussin while (--n > 0) 1089057ca2d4SBaptiste Daroussin *s1++ = 0; 1090057ca2d4SBaptiste Daroussin return (os1); 1091057ca2d4SBaptiste Daroussin } 1092057ca2d4SBaptiste Daroussin 109323a32822SBaptiste Daroussin #define RB_COUNT(x, name, head, cnt) do { \ 109423a32822SBaptiste Daroussin (cnt) = 0; \ 109523a32822SBaptiste Daroussin RB_FOREACH(x, name, (head)) { \ 109623a32822SBaptiste Daroussin (cnt)++; \ 109723a32822SBaptiste Daroussin } \ 109823a32822SBaptiste Daroussin } while (0) 109923a32822SBaptiste Daroussin 11003a444dbdSBaptiste Daroussin #define RB_NUMNODES(type, name, head, cnt) do { \ 11013a444dbdSBaptiste Daroussin type *t; \ 11023a444dbdSBaptiste Daroussin cnt = 0; \ 11033a444dbdSBaptiste Daroussin RB_FOREACH(t, name, head) { \ 11043a444dbdSBaptiste Daroussin cnt++; \ 11053a444dbdSBaptiste Daroussin } \ 110623a32822SBaptiste Daroussin } while (0) 11073a444dbdSBaptiste Daroussin 1108057ca2d4SBaptiste Daroussin void 1109057ca2d4SBaptiste Daroussin dump_collate(void) 1110057ca2d4SBaptiste Daroussin { 1111057ca2d4SBaptiste Daroussin FILE *f; 1112057ca2d4SBaptiste Daroussin int i, j, n; 1113057ca2d4SBaptiste Daroussin size_t sz; 1114057ca2d4SBaptiste Daroussin int32_t pri; 1115057ca2d4SBaptiste Daroussin collelem_t *ce; 1116057ca2d4SBaptiste Daroussin collchar_t *cc; 1117057ca2d4SBaptiste Daroussin subst_t *sb; 1118057ca2d4SBaptiste Daroussin char vers[COLLATE_STR_LEN]; 1119057ca2d4SBaptiste Daroussin collate_char_t chars[UCHAR_MAX + 1]; 1120057ca2d4SBaptiste Daroussin collate_large_t *large; 1121057ca2d4SBaptiste Daroussin collate_subst_t *subst[COLL_WEIGHTS_MAX]; 1122057ca2d4SBaptiste Daroussin collate_chain_t *chain; 1123057ca2d4SBaptiste Daroussin 1124057ca2d4SBaptiste Daroussin /* 1125057ca2d4SBaptiste Daroussin * We have to run throught a preliminary pass to identify all the 1126057ca2d4SBaptiste Daroussin * weights that we use for each sorting level. 1127057ca2d4SBaptiste Daroussin */ 1128057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1129057ca2d4SBaptiste Daroussin add_weight(pri_ignore, i); 1130057ca2d4SBaptiste Daroussin } 1131057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 11323a444dbdSBaptiste Daroussin RB_FOREACH(sb, substs, &substs[i]) { 1133057ca2d4SBaptiste Daroussin for (j = 0; sb->ref[j]; j++) { 1134057ca2d4SBaptiste Daroussin add_weight(sb->ref[j], i); 1135057ca2d4SBaptiste Daroussin } 1136057ca2d4SBaptiste Daroussin } 1137057ca2d4SBaptiste Daroussin } 11383a444dbdSBaptiste Daroussin RB_FOREACH(ce, elem_by_expand, &elem_by_expand) { 1139057ca2d4SBaptiste Daroussin add_weights(ce->ref); 1140057ca2d4SBaptiste Daroussin } 11413a444dbdSBaptiste Daroussin RB_FOREACH(cc, collchars, &collchars) { 1142057ca2d4SBaptiste Daroussin add_weights(cc->ref); 1143057ca2d4SBaptiste Daroussin } 1144057ca2d4SBaptiste Daroussin 1145057ca2d4SBaptiste Daroussin /* 1146057ca2d4SBaptiste Daroussin * Now we walk the entire set of weights, removing the gaps 1147057ca2d4SBaptiste Daroussin * in the weights. This gives us optimum usage. The walk 1148057ca2d4SBaptiste Daroussin * occurs in priority. 1149057ca2d4SBaptiste Daroussin */ 1150057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1151057ca2d4SBaptiste Daroussin weight_t *w; 11523a444dbdSBaptiste Daroussin RB_FOREACH(w, weights, &weights[i]) { 1153057ca2d4SBaptiste Daroussin w->opt = nweight[i]; 1154057ca2d4SBaptiste Daroussin nweight[i] += 1; 1155057ca2d4SBaptiste Daroussin } 1156057ca2d4SBaptiste Daroussin } 1157057ca2d4SBaptiste Daroussin 1158057ca2d4SBaptiste Daroussin (void) memset(&chars, 0, sizeof (chars)); 1159057ca2d4SBaptiste Daroussin (void) memset(vers, 0, COLLATE_STR_LEN); 1160057ca2d4SBaptiste Daroussin (void) strlcpy(vers, COLLATE_VERSION, sizeof (vers)); 1161057ca2d4SBaptiste Daroussin 1162057ca2d4SBaptiste Daroussin /* 1163057ca2d4SBaptiste Daroussin * We need to make sure we arrange for the UNDEFINED field 1164057ca2d4SBaptiste Daroussin * to show up. Also, set the total weight counts. 1165057ca2d4SBaptiste Daroussin */ 1166057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1167057ca2d4SBaptiste Daroussin if (resolve_pri(pri_undefined[i]) == -1) { 1168057ca2d4SBaptiste Daroussin set_pri(pri_undefined[i], -1, RESOLVED); 1169057ca2d4SBaptiste Daroussin /* they collate at the end of everything else */ 1170057ca2d4SBaptiste Daroussin collinfo.undef_pri[i] = COLLATE_MAX_PRIORITY; 1171057ca2d4SBaptiste Daroussin } 1172057ca2d4SBaptiste Daroussin collinfo.pri_count[i] = nweight[i]; 1173057ca2d4SBaptiste Daroussin } 1174057ca2d4SBaptiste Daroussin 1175057ca2d4SBaptiste Daroussin collinfo.pri_count[NUM_WT] = max_wide(); 1176057ca2d4SBaptiste Daroussin collinfo.undef_pri[NUM_WT] = COLLATE_MAX_PRIORITY; 1177057ca2d4SBaptiste Daroussin collinfo.directive[NUM_WT] = DIRECTIVE_UNDEFINED; 1178057ca2d4SBaptiste Daroussin 1179057ca2d4SBaptiste Daroussin /* 1180057ca2d4SBaptiste Daroussin * Ordinary character priorities 1181057ca2d4SBaptiste Daroussin */ 1182057ca2d4SBaptiste Daroussin for (i = 0; i <= UCHAR_MAX; i++) { 1183057ca2d4SBaptiste Daroussin if ((cc = get_collchar(i, 0)) != NULL) { 1184057ca2d4SBaptiste Daroussin for (j = 0; j < NUM_WT; j++) { 1185057ca2d4SBaptiste Daroussin chars[i].pri[j] = get_weight(cc->ref[j], j); 1186057ca2d4SBaptiste Daroussin } 1187057ca2d4SBaptiste Daroussin } else { 1188057ca2d4SBaptiste Daroussin for (j = 0; j < NUM_WT; j++) { 1189057ca2d4SBaptiste Daroussin chars[i].pri[j] = 1190057ca2d4SBaptiste Daroussin get_weight(pri_undefined[j], j); 1191057ca2d4SBaptiste Daroussin } 1192057ca2d4SBaptiste Daroussin /* 1193057ca2d4SBaptiste Daroussin * Per POSIX, for undefined characters, we 1194057ca2d4SBaptiste Daroussin * also have to add a last item, which is the 1195057ca2d4SBaptiste Daroussin * character code. 1196057ca2d4SBaptiste Daroussin */ 1197057ca2d4SBaptiste Daroussin chars[i].pri[NUM_WT] = i; 1198057ca2d4SBaptiste Daroussin } 1199057ca2d4SBaptiste Daroussin } 1200057ca2d4SBaptiste Daroussin 1201057ca2d4SBaptiste Daroussin /* 1202057ca2d4SBaptiste Daroussin * Substitution tables 1203057ca2d4SBaptiste Daroussin */ 1204057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1205057ca2d4SBaptiste Daroussin collate_subst_t *st = NULL; 120623a32822SBaptiste Daroussin subst_t *temp; 120723a32822SBaptiste Daroussin RB_COUNT(temp, substs, &substs[i], n); 12083a444dbdSBaptiste Daroussin collinfo.subst_count[i] = n; 1209057ca2d4SBaptiste Daroussin if ((st = calloc(sizeof (collate_subst_t) * n, 1)) == NULL) { 1210057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 1211057ca2d4SBaptiste Daroussin return; 1212057ca2d4SBaptiste Daroussin } 1213057ca2d4SBaptiste Daroussin n = 0; 12143a444dbdSBaptiste Daroussin RB_FOREACH(sb, substs, &substs[i]) { 1215057ca2d4SBaptiste Daroussin if ((st[n].key = resolve_pri(sb->key)) < 0) { 1216057ca2d4SBaptiste Daroussin /* by definition these resolve! */ 1217057ca2d4SBaptiste Daroussin INTERR; 1218057ca2d4SBaptiste Daroussin } 1219057ca2d4SBaptiste Daroussin if (st[n].key != (n | COLLATE_SUBST_PRIORITY)) { 1220057ca2d4SBaptiste Daroussin INTERR; 1221057ca2d4SBaptiste Daroussin } 1222057ca2d4SBaptiste Daroussin for (j = 0; sb->ref[j]; j++) { 1223057ca2d4SBaptiste Daroussin st[n].pri[j] = get_weight(sb->ref[j], i); 1224057ca2d4SBaptiste Daroussin } 1225057ca2d4SBaptiste Daroussin n++; 1226057ca2d4SBaptiste Daroussin } 1227057ca2d4SBaptiste Daroussin if (n != collinfo.subst_count[i]) 1228057ca2d4SBaptiste Daroussin INTERR; 1229057ca2d4SBaptiste Daroussin subst[i] = st; 1230057ca2d4SBaptiste Daroussin } 1231057ca2d4SBaptiste Daroussin 1232057ca2d4SBaptiste Daroussin 1233057ca2d4SBaptiste Daroussin /* 1234057ca2d4SBaptiste Daroussin * Chains, i.e. collating elements 1235057ca2d4SBaptiste Daroussin */ 12363a444dbdSBaptiste Daroussin RB_NUMNODES(collelem_t, elem_by_expand, &elem_by_expand, 12373a444dbdSBaptiste Daroussin collinfo.chain_count); 1238057ca2d4SBaptiste Daroussin chain = calloc(sizeof (collate_chain_t), collinfo.chain_count); 1239057ca2d4SBaptiste Daroussin if (chain == NULL) { 1240057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 1241057ca2d4SBaptiste Daroussin return; 1242057ca2d4SBaptiste Daroussin } 12433a444dbdSBaptiste Daroussin n = 0; 12443a444dbdSBaptiste Daroussin RB_FOREACH(ce, elem_by_expand, &elem_by_expand) { 1245057ca2d4SBaptiste Daroussin (void) wsncpy(chain[n].str, ce->expand, COLLATE_STR_LEN); 1246057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1247057ca2d4SBaptiste Daroussin chain[n].pri[i] = get_weight(ce->ref[i], i); 1248057ca2d4SBaptiste Daroussin } 1249*5d21db09SBaptiste Daroussin n++; 1250057ca2d4SBaptiste Daroussin } 1251057ca2d4SBaptiste Daroussin if (n != collinfo.chain_count) 1252057ca2d4SBaptiste Daroussin INTERR; 1253057ca2d4SBaptiste Daroussin 1254057ca2d4SBaptiste Daroussin /* 1255057ca2d4SBaptiste Daroussin * Large (> UCHAR_MAX) character priorities 1256057ca2d4SBaptiste Daroussin */ 12573a444dbdSBaptiste Daroussin RB_NUMNODES(collchar_t, collchars, &collchars, n); 1258*5d21db09SBaptiste Daroussin large = calloc(n, sizeof (collate_large_t)); 1259057ca2d4SBaptiste Daroussin if (large == NULL) { 1260057ca2d4SBaptiste Daroussin fprintf(stderr, "out of memory"); 1261057ca2d4SBaptiste Daroussin return; 1262057ca2d4SBaptiste Daroussin } 1263057ca2d4SBaptiste Daroussin 1264057ca2d4SBaptiste Daroussin i = 0; 12653a444dbdSBaptiste Daroussin RB_FOREACH(cc, collchars, &collchars) { 1266057ca2d4SBaptiste Daroussin int undef = 0; 1267057ca2d4SBaptiste Daroussin /* we already gathered those */ 1268057ca2d4SBaptiste Daroussin if (cc->wc <= UCHAR_MAX) 1269057ca2d4SBaptiste Daroussin continue; 1270057ca2d4SBaptiste Daroussin for (j = 0; j < NUM_WT; j++) { 1271057ca2d4SBaptiste Daroussin if ((pri = get_weight(cc->ref[j], j)) < 0) { 1272057ca2d4SBaptiste Daroussin undef = 1; 1273057ca2d4SBaptiste Daroussin } 1274057ca2d4SBaptiste Daroussin if (undef && (pri >= 0)) { 1275057ca2d4SBaptiste Daroussin /* if undefined, then all priorities are */ 1276057ca2d4SBaptiste Daroussin INTERR; 1277057ca2d4SBaptiste Daroussin } else { 1278057ca2d4SBaptiste Daroussin large[i].pri.pri[j] = pri; 1279057ca2d4SBaptiste Daroussin } 1280057ca2d4SBaptiste Daroussin } 1281057ca2d4SBaptiste Daroussin if (!undef) { 1282057ca2d4SBaptiste Daroussin large[i].val = cc->wc; 1283057ca2d4SBaptiste Daroussin collinfo.large_count = i++; 1284057ca2d4SBaptiste Daroussin } 1285057ca2d4SBaptiste Daroussin } 1286057ca2d4SBaptiste Daroussin 1287057ca2d4SBaptiste Daroussin if ((f = open_category()) == NULL) { 1288057ca2d4SBaptiste Daroussin return; 1289057ca2d4SBaptiste Daroussin } 1290057ca2d4SBaptiste Daroussin 1291057ca2d4SBaptiste Daroussin /* Time to write the entire data set out */ 1292057ca2d4SBaptiste Daroussin 1293057ca2d4SBaptiste Daroussin if ((wr_category(vers, COLLATE_STR_LEN, f) < 0) || 1294057ca2d4SBaptiste Daroussin (wr_category(&collinfo, sizeof (collinfo), f) < 0) || 1295057ca2d4SBaptiste Daroussin (wr_category(&chars, sizeof (chars), f) < 0)) { 1296057ca2d4SBaptiste Daroussin return; 1297057ca2d4SBaptiste Daroussin } 1298057ca2d4SBaptiste Daroussin 1299057ca2d4SBaptiste Daroussin for (i = 0; i < NUM_WT; i++) { 1300057ca2d4SBaptiste Daroussin sz = sizeof (collate_subst_t) * collinfo.subst_count[i]; 1301057ca2d4SBaptiste Daroussin if (wr_category(subst[i], sz, f) < 0) { 1302057ca2d4SBaptiste Daroussin return; 1303057ca2d4SBaptiste Daroussin } 1304057ca2d4SBaptiste Daroussin } 1305057ca2d4SBaptiste Daroussin sz = sizeof (collate_chain_t) * collinfo.chain_count; 1306057ca2d4SBaptiste Daroussin if (wr_category(chain, sz, f) < 0) { 1307057ca2d4SBaptiste Daroussin return; 1308057ca2d4SBaptiste Daroussin } 1309057ca2d4SBaptiste Daroussin sz = sizeof (collate_large_t) * collinfo.large_count; 1310057ca2d4SBaptiste Daroussin if (wr_category(large, sz, f) < 0) { 1311057ca2d4SBaptiste Daroussin return; 1312057ca2d4SBaptiste Daroussin } 1313057ca2d4SBaptiste Daroussin 1314057ca2d4SBaptiste Daroussin close_category(f); 1315057ca2d4SBaptiste Daroussin } 1316