2 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
3 * Copyright 2015 John Marino <draco@marino.st>
5 * This source code is derived from the illumos localedef command, and
6 * provided under BSD-style license terms by Nexenta Systems, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 * CHARMAP file handling for localedef.
42 #include "localedef.h"
46 static avl_tree_t cmap_sym;
47 static avl_tree_t cmap_wc;
49 typedef struct charmap {
58 * Array of POSIX specific portable characters.
61 #pragma GCC diagnostic push
62 #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
67 } portable_chars[] = {
70 { "backspace", '\b' },
72 { "carriage-return", '\r' },
74 { "vertical-tab", '\v' },
75 { "form-feed", '\f' },
77 { "exclamation-mark", '!' },
78 { "quotation-mark", '"' },
79 { "number-sign", '#' },
80 { "dollar-sign", '$' },
81 { "percent-sign", '%' },
83 { "apostrophe", '\'' },
84 { "left-parenthesis", '(' },
85 { "right-parenthesis", '(' },
89 { "hyphen-minus", '-' },
106 { "semicolon", ';' },
107 { "less-than-sign", '<' },
108 { "equals-sign", '=' },
109 { "greater-than-sign", '>' },
110 { "question-mark", '?' },
111 { "commercial-at", '@' },
112 { "left-square-bracket", '[' },
113 { "backslash", '\\' },
114 { "reverse-solidus", '\\' },
115 { "right-square-bracket", ']' },
116 { "circumflex", '^' },
117 { "circumflex-accent", '^' },
119 { "underscore", '_' },
120 { "grave-accent", '`' },
121 { "left-brace", '{' },
122 { "left-curly-bracket", '{' },
123 { "vertical-line", '|' },
124 { "right-brace", '}' },
125 { "right-curly-bracket", '}' },
182 #pragma GCC diagnostic pop
185 cmap_compare_sym(const void *n1, const void *n2)
187 const charmap_t *c1 = n1;
188 const charmap_t *c2 = n2;
191 rv = strcmp(c1->name, c2->name);
192 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
196 cmap_compare_wc(const void *n1, const void *n2)
198 const charmap_t *c1 = n1;
199 const charmap_t *c2 = n2;
201 return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
207 avl_create(&cmap_sym, cmap_compare_sym, sizeof (charmap_t),
208 offsetof(charmap_t, avl_sym));
210 avl_create(&cmap_wc, cmap_compare_wc, sizeof (charmap_t),
211 offsetof(charmap_t, avl_wc));
215 add_charmap_impl(char *sym, wchar_t wc, int nodups)
225 * also possibly insert the wide mapping, although note that there
226 * can only be one of these per wide character code.
228 if ((wc != -1) && ((avl_find(&cmap_wc, &srch, &where)) == NULL)) {
229 if ((n = calloc(1, sizeof (*n))) == NULL) {
230 errf("out of memory");
234 avl_insert(&cmap_wc, n, where);
238 if (avl_find(&cmap_sym, &srch, &where) != NULL) {
240 errf("duplicate character definition");
244 if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
245 errf("out of memory");
251 avl_insert(&cmap_sym, n, where);
256 add_charmap(char *sym, int c)
258 add_charmap_impl(sym, c, 1);
262 add_charmap_undefined(char *sym)
265 charmap_t *cm = NULL;
268 cm = avl_find(&cmap_sym, &srch, NULL);
270 if ((undefok == 0) && ((cm == NULL) || (cm->wc == -1))) {
271 warn("undefined symbol <%s>", sym);
272 add_charmap_impl(sym, -1, 0);
279 add_charmap_range(char *s, char *e, int wc)
286 static const char *digits = "0123456789";
291 if (((si = strcspn(s, digits)) == 0) || (si == ls) ||
292 (strncmp(s, e, si) != 0) ||
293 ((int)strspn(s + si, digits) != (ls - si)) ||
294 ((int)strspn(e + si, digits) != (le - si)) ||
295 ((sn = atoi(s + si)) > ((en = atoi(e + si))))) {
296 errf("malformed charmap range");
302 for (i = sn; i <= en; i++) {
304 (void) asprintf(&nn, "%s%0*u", s, ls - si, i);
306 errf("out of memory");
310 add_charmap_impl(nn, wc, 1);
318 add_charmap_char(char *name, int val)
320 add_charmap_impl(name, val, 0);
324 * POSIX insists that certain entries be present, even when not in the
325 * orginal charmap file.
328 add_charmap_posix(void)
332 for (i = 0; portable_chars[i].name; i++) {
333 add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
338 lookup_charmap(const char *sym, wchar_t *wc)
344 n = avl_find(&cmap_sym, &srch, NULL);
345 if (n && n->wc != -1) {
354 check_charmap(wchar_t wc)
359 return (avl_find(&cmap_wc, &srch, NULL) ? 0 : -1);