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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/types.h>
46 #include "localedef.h"
50 typedef struct charmap {
53 RB_ENTRY(charmap) rb_sym;
54 RB_ENTRY(charmap) rb_wc;
57 static int cmap_compare_sym(const void *n1, const void *n2);
58 static int cmap_compare_wc(const void *n1, const void *n2);
60 static RB_HEAD(cmap_sym, charmap) cmap_sym;
61 static RB_HEAD(cmap_wc, charmap) cmap_wc;
63 RB_GENERATE_STATIC(cmap_sym, charmap, rb_sym, cmap_compare_sym);
64 RB_GENERATE_STATIC(cmap_wc, charmap, rb_wc, cmap_compare_wc);
67 * Array of POSIX specific portable characters.
73 } portable_chars[] = {
76 { "backspace", '\b' },
78 { "carriage-return", '\r' },
80 { "vertical-tab", '\v' },
81 { "form-feed", '\f' },
83 { "exclamation-mark", '!' },
84 { "quotation-mark", '"' },
85 { "number-sign", '#' },
86 { "dollar-sign", '$' },
87 { "percent-sign", '%' },
89 { "apostrophe", '\'' },
90 { "left-parenthesis", '(' },
91 { "right-parenthesis", '(' },
95 { "hyphen-minus", '-' },
112 { "semicolon", ';' },
113 { "less-than-sign", '<' },
114 { "equals-sign", '=' },
115 { "greater-than-sign", '>' },
116 { "question-mark", '?' },
117 { "commercial-at", '@' },
118 { "left-square-bracket", '[' },
119 { "backslash", '\\' },
120 { "reverse-solidus", '\\' },
121 { "right-square-bracket", ']' },
122 { "circumflex", '^' },
123 { "circumflex-accent", '^' },
125 { "underscore", '_' },
126 { "grave-accent", '`' },
127 { "left-brace", '{' },
128 { "left-curly-bracket", '{' },
129 { "vertical-line", '|' },
130 { "right-brace", '}' },
131 { "right-curly-bracket", '}' },
189 cmap_compare_sym(const void *n1, const void *n2)
191 const charmap_t *c1 = n1;
192 const charmap_t *c2 = n2;
195 rv = strcmp(c1->name, c2->name);
196 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
200 cmap_compare_wc(const void *n1, const void *n2)
202 const charmap_t *c1 = n1;
203 const charmap_t *c2 = n2;
205 return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
217 add_charmap_impl(const char *sym, wchar_t wc, int nodups)
226 * also possibly insert the wide mapping, although note that there
227 * can only be one of these per wide character code.
229 if ((wc != (wchar_t)-1) && ((RB_FIND(cmap_wc, &cmap_wc, &srch)) == NULL)) {
230 if ((n = calloc(1, sizeof (*n))) == NULL) {
231 errf("out of memory");
235 RB_INSERT(cmap_wc, &cmap_wc, n);
239 if (RB_FIND(cmap_sym, &cmap_sym, &srch) != NULL) {
241 errf("duplicate character definition");
245 if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
246 errf("out of memory");
252 RB_INSERT(cmap_sym, &cmap_sym, n);
257 add_charmap(const char *sym, int c)
259 add_charmap_impl(sym, c, 1);
263 add_charmap_undefined(char *sym)
266 charmap_t *cm = NULL;
269 cm = RB_FIND(cmap_sym, &cmap_sym, &srch);
271 if ((undefok == 0) && ((cm == NULL) || (cm->wc == (wchar_t)-1))) {
272 warn("undefined symbol <%s>", sym);
273 add_charmap_impl(sym, -1, 0);
280 add_charmap_range(char *s, char *e, int wc)
287 static const char *digits = "0123456789";
292 if (((si = strcspn(s, digits)) == 0) || (si == ls) ||
293 (strncmp(s, e, si) != 0) ||
294 ((int)strspn(s + si, digits) != (ls - si)) ||
295 ((int)strspn(e + si, digits) != (le - si)) ||
296 ((sn = atoi(s + si)) > ((en = atoi(e + si))))) {
297 errf("malformed charmap range");
303 for (i = sn; i <= en; i++) {
305 (void) asprintf(&nn, "%s%0*u", s, ls - si, i);
307 errf("out of memory");
311 add_charmap_impl(nn, wc, 1);
319 add_charmap_char(const char *name, int val)
321 add_charmap_impl(name, val, 0);
325 * POSIX insists that certain entries be present, even when not in the
326 * original charmap file.
329 add_charmap_posix(void)
333 for (i = 0; portable_chars[i].name; i++) {
334 add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
339 lookup_charmap(const char *sym, wchar_t *wc)
345 n = RB_FIND(cmap_sym, &cmap_sym, &srch);
346 if (n && n->wc != (wchar_t)-1) {
355 check_charmap(wchar_t wc)
360 return (RB_FIND(cmap_wc, &cmap_wc, &srch) ? 0 : -1);