2 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
3 * Copyright 2012 Garrett D'Amore <garrett@damore.org> All rights reserved.
4 * Copyright 2015 John Marino <draco@marino.st>
6 * This source code is derived from the illumos localedef command, and
7 * provided under BSD-style license terms by Nexenta Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * LC_CTYPE database generation routines for localedef.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
44 #include <sys/types.h>
49 #include "localedef.h"
54 /* Needed for bootstrapping, _CTYPE_N */
56 #define _CTYPE_N 0x00400000L
59 #define _ISUPPER _CTYPE_U
60 #define _ISLOWER _CTYPE_L
61 #define _ISDIGIT _CTYPE_D
62 #define _ISXDIGIT _CTYPE_X
63 #define _ISSPACE _CTYPE_S
64 #define _ISBLANK _CTYPE_B
65 #define _ISALPHA _CTYPE_A
66 #define _ISPUNCT _CTYPE_P
67 #define _ISGRAPH _CTYPE_G
68 #define _ISPRINT _CTYPE_R
69 #define _ISCNTRL _CTYPE_C
76 static wchar_t last_ctype;
77 static int ctype_compare(const void *n1, const void *n2);
79 typedef struct ctype_node {
84 RB_ENTRY(ctype_node) entry;
87 static RB_HEAD(ctypes, ctype_node) ctypes;
88 RB_GENERATE_STATIC(ctypes, ctype_node, entry, ctype_compare);
91 ctype_compare(const void *n1, const void *n2)
93 const ctype_node_t *c1 = n1;
94 const ctype_node_t *c2 = n2;
96 return (c1->wc < c2->wc ? -1 : c1->wc > c2->wc ? 1 : 0);
107 add_ctype_impl(ctype_node_t *ctn)
111 ctn->ctype |= (_ISUPPER | _ISALPHA | _ISGRAPH | _ISPRINT);
114 ctn->ctype |= (_ISLOWER | _ISALPHA | _ISGRAPH | _ISPRINT);
117 ctn->ctype |= (_ISALPHA | _ISGRAPH | _ISPRINT);
120 ctn->ctype |= (_ISDIGIT | _ISGRAPH | _ISPRINT | _ISXDIGIT | _E4);
124 * This can be troublesome as <form-feed>, <newline>,
125 * <carriage-return>, <tab>, and <vertical-tab> are defined both
126 * as space and cntrl, and POSIX doesn't allow cntrl/print
127 * combination. We will take care of this in dump_ctype().
129 ctn->ctype |= (_ISSPACE | _ISPRINT);
132 ctn->ctype |= _ISCNTRL;
135 ctn->ctype |= (_ISGRAPH | _ISPRINT);
138 ctn->ctype |= _ISPRINT;
141 ctn->ctype |= (_ISPUNCT | _ISGRAPH | _ISPRINT);
144 ctn->ctype |= (_ISXDIGIT | _ISPRINT);
147 ctn->ctype |= (_ISBLANK | _ISSPACE);
150 ctn->ctype |= (_E1 | _ISPRINT | _ISGRAPH);
153 ctn->ctype |= (_E2 | _ISPRINT | _ISGRAPH);
156 ctn->ctype |= (_E3 | _ISPRINT | _ISGRAPH);
159 ctn->ctype |= (_E4 | _ISPRINT | _ISGRAPH);
162 ctn->ctype |= (_E5 | _ISPRINT | _ISGRAPH);
166 * We can't do anything with this. The character
167 * should already be specified as a digit or alpha.
171 errf("not a valid character class");
175 static ctype_node_t *
176 get_ctype(wchar_t wc)
182 if ((ctn = RB_FIND(ctypes, &ctypes, &srch)) == NULL) {
183 if ((ctn = calloc(1, sizeof (*ctn))) == NULL) {
184 errf("out of memory");
189 RB_INSERT(ctypes, &ctypes, ctn);
199 if ((ctn = get_ctype(val)) == NULL) {
204 last_ctype = ctn->wc;
208 add_ctype_range(wchar_t end)
213 if (end < last_ctype) {
214 errf("malformed character range (%u ... %u))",
218 for (cur = last_ctype + 1; cur <= end; cur++) {
219 if ((ctn = get_ctype(cur)) == NULL) {
230 * A word about widths: if the width mask is specified, then libc
231 * unconditionally honors it. Otherwise, it assumes printable
232 * characters have width 1, and non-printable characters have width
233 * -1 (except for NULL which is special with with 0). Hence, we have
234 * no need to inject defaults here -- the "default" unset value of 0
235 * indicates that libc should use its own logic in wcwidth as described.
238 add_width(int wc, int width)
242 if ((ctn = get_ctype(wc)) == NULL) {
246 ctn->ctype &= ~(_CTYPE_SWM);
249 ctn->ctype |= _CTYPE_SW0;
252 ctn->ctype |= _CTYPE_SW1;
255 ctn->ctype |= _CTYPE_SW2;
258 ctn->ctype |= _CTYPE_SW3;
264 add_width_range(int start, int end, int width)
266 for (; start <= end; start++) {
267 add_width(start, width);
272 add_caseconv(int val, int wc)
276 ctn = get_ctype(val);
300 ctype_node_t *ctn, *last_ct, *last_lo, *last_up;
301 _FileRuneEntry *ct = NULL;
302 _FileRuneEntry *lo = NULL;
303 _FileRuneEntry *up = NULL;
306 (void) memset(&rl, 0, sizeof (rl));
311 if ((f = open_category()) == NULL)
314 (void) memcpy(rl.magic, _FILE_RUNE_MAGIC_1, 8);
315 (void) strlcpy(rl.encoding, get_wide_encoding(), sizeof (rl.encoding));
318 * Initialize the identity map.
320 for (wc = 0; (unsigned)wc < _CACHED_RUNES; wc++) {
321 rl.maplower[wc] = wc;
322 rl.mapupper[wc] = wc;
325 RB_FOREACH(ctn, ctypes, &ctypes) {
331 * POSIX requires certain portable characters have
332 * certain types. Add them if they are missing.
334 if ((wc >= 1) && (wc <= 127)) {
335 if ((wc >= 'A') && (wc <= 'Z'))
336 ctn->ctype |= _ISUPPER;
337 if ((wc >= 'a') && (wc <= 'z'))
338 ctn->ctype |= _ISLOWER;
339 if ((wc >= '0') && (wc <= '9'))
340 ctn->ctype |= _ISDIGIT;
342 ctn->ctype |= _ISPRINT;
343 if (strchr(" \f\n\r\t\v", (char)wc) != NULL)
344 ctn->ctype |= _ISSPACE;
345 if (strchr("0123456789ABCDEFabcdef", (char)wc) != NULL)
346 ctn->ctype |= _ISXDIGIT;
347 if (strchr(" \t", (char)wc))
348 ctn->ctype |= _ISBLANK;
351 * Technically these settings are only
352 * required for the C locale. However, it
353 * turns out that because of the historical
354 * version of isprint(), we need them for all
355 * locales as well. Note that these are not
356 * necessarily valid punctation characters in
357 * the current language, but ispunct() needs
358 * to return TRUE for them.
360 if (strchr("!\"'#$%&()*+,-./:;<=>?@[\\]^_`{|}~",
362 ctn->ctype |= _ISPUNCT;
366 * POSIX also requires that certain types imply
367 * others. Add any inferred types here.
369 if (ctn->ctype & (_ISUPPER |_ISLOWER))
370 ctn->ctype |= _ISALPHA;
371 if (ctn->ctype & _ISDIGIT)
372 ctn->ctype |= _ISXDIGIT;
373 if (ctn->ctype & _ISBLANK)
374 ctn->ctype |= _ISSPACE;
375 if (ctn->ctype & (_ISALPHA|_ISDIGIT|_ISXDIGIT))
376 ctn->ctype |= _ISGRAPH;
377 if (ctn->ctype & _ISGRAPH)
378 ctn->ctype |= _ISPRINT;
381 * POSIX requires that certain combinations are invalid.
382 * Try fixing the cases we know about (see add_ctype_impl()).
384 if ((ctn->ctype & (_ISSPACE|_ISCNTRL)) == (_ISSPACE|_ISCNTRL))
385 ctn->ctype &= ~_ISPRINT;
388 * Finally, don't flag remaining cases as a fatal error,
389 * and just warn about them.
391 if ((ctn->ctype & _ISALPHA) &&
392 (ctn->ctype & (_ISPUNCT|_ISDIGIT)))
394 if ((ctn->ctype & _ISPUNCT) &&
395 (ctn->ctype & (_ISDIGIT|_ISALPHA|_ISXDIGIT)))
397 if ((ctn->ctype & _ISSPACE) && (ctn->ctype & _ISGRAPH))
399 if ((ctn->ctype & _ISCNTRL) && (ctn->ctype & _ISPRINT))
401 if ((wc == ' ') && (ctn->ctype & (_ISPUNCT|_ISGRAPH)))
405 warn("conflicting classes for character 0x%x (%x)",
409 * Handle the lower 256 characters using the simple
410 * optimization. Note that if we have not defined the
411 * upper/lower case, then we identity map it.
413 if ((unsigned)wc < _CACHED_RUNES) {
414 rl.runetype[wc] = ctn->ctype;
416 rl.maplower[wc] = ctn->tolower;
418 rl.mapupper[wc] = ctn->toupper;
422 if ((last_ct != NULL) && (last_ct->ctype == ctn->ctype) &&
423 (last_ct->wc + 1 == wc)) {
424 ct[rl.runetype_ext_nranges-1].max = wc;
426 rl.runetype_ext_nranges++;
428 sizeof (*ct) * rl.runetype_ext_nranges);
429 ct[rl.runetype_ext_nranges - 1].min = wc;
430 ct[rl.runetype_ext_nranges - 1].max = wc;
431 ct[rl.runetype_ext_nranges - 1].map = ctn->ctype;
434 if (ctn->tolower == 0) {
436 } else if ((last_lo != NULL) &&
437 (last_lo->tolower + 1 == ctn->tolower)) {
438 lo[rl.maplower_ext_nranges-1].max = wc;
441 rl.maplower_ext_nranges++;
443 sizeof (*lo) * rl.maplower_ext_nranges);
444 lo[rl.maplower_ext_nranges - 1].min = wc;
445 lo[rl.maplower_ext_nranges - 1].max = wc;
446 lo[rl.maplower_ext_nranges - 1].map = ctn->tolower;
450 if (ctn->toupper == 0) {
452 } else if ((last_up != NULL) &&
453 (last_up->toupper + 1 == ctn->toupper)) {
454 up[rl.mapupper_ext_nranges-1].max = wc;
457 rl.mapupper_ext_nranges++;
459 sizeof (*up) * rl.mapupper_ext_nranges);
460 up[rl.mapupper_ext_nranges - 1].min = wc;
461 up[rl.mapupper_ext_nranges - 1].max = wc;
462 up[rl.mapupper_ext_nranges - 1].map = ctn->toupper;
467 if ((wr_category(&rl, sizeof (rl), f) < 0) ||
468 (wr_category(ct, sizeof (*ct) * rl.runetype_ext_nranges, f) < 0) ||
469 (wr_category(lo, sizeof (*lo) * rl.maplower_ext_nranges, f) < 0) ||
470 (wr_category(up, sizeof (*up) * rl.mapupper_ext_nranges, f) < 0)) {