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 #include <sys/types.h>
44 #include "localedef.h"
48 typedef struct charmap {
51 RB_ENTRY(charmap) rb_sym;
52 RB_ENTRY(charmap) rb_wc;
55 static int cmap_compare_sym(const void *n1, const void *n2);
56 static int cmap_compare_wc(const void *n1, const void *n2);
58 static RB_HEAD(cmap_sym, charmap) cmap_sym;
59 static RB_HEAD(cmap_wc, charmap) cmap_wc;
61 RB_GENERATE_STATIC(cmap_sym, charmap, rb_sym, cmap_compare_sym);
62 RB_GENERATE_STATIC(cmap_wc, charmap, rb_wc, cmap_compare_wc);
65 * Array of POSIX specific portable characters.
71 } portable_chars[] = {
74 { "backspace", '\b' },
76 { "carriage-return", '\r' },
78 { "vertical-tab", '\v' },
79 { "form-feed", '\f' },
81 { "exclamation-mark", '!' },
82 { "quotation-mark", '"' },
83 { "number-sign", '#' },
84 { "dollar-sign", '$' },
85 { "percent-sign", '%' },
87 { "apostrophe", '\'' },
88 { "left-parenthesis", '(' },
89 { "right-parenthesis", '(' },
93 { "hyphen-minus", '-' },
110 { "semicolon", ';' },
111 { "less-than-sign", '<' },
112 { "equals-sign", '=' },
113 { "greater-than-sign", '>' },
114 { "question-mark", '?' },
115 { "commercial-at", '@' },
116 { "left-square-bracket", '[' },
117 { "backslash", '\\' },
118 { "reverse-solidus", '\\' },
119 { "right-square-bracket", ']' },
120 { "circumflex", '^' },
121 { "circumflex-accent", '^' },
123 { "underscore", '_' },
124 { "grave-accent", '`' },
125 { "left-brace", '{' },
126 { "left-curly-bracket", '{' },
127 { "vertical-line", '|' },
128 { "right-brace", '}' },
129 { "right-curly-bracket", '}' },
187 cmap_compare_sym(const void *n1, const void *n2)
189 const charmap_t *c1 = n1;
190 const charmap_t *c2 = n2;
193 rv = strcmp(c1->name, c2->name);
194 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
198 cmap_compare_wc(const void *n1, const void *n2)
200 const charmap_t *c1 = n1;
201 const charmap_t *c2 = n2;
203 return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
215 add_charmap_impl(const char *sym, wchar_t wc, int nodups)
224 * also possibly insert the wide mapping, although note that there
225 * can only be one of these per wide character code.
227 if ((wc != (wchar_t)-1) && ((RB_FIND(cmap_wc, &cmap_wc, &srch)) == NULL)) {
228 if ((n = calloc(1, sizeof (*n))) == NULL) {
229 errf("out of memory");
233 RB_INSERT(cmap_wc, &cmap_wc, n);
237 if (RB_FIND(cmap_sym, &cmap_sym, &srch) != NULL) {
239 errf("duplicate character definition");
243 if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
244 errf("out of memory");
250 RB_INSERT(cmap_sym, &cmap_sym, n);
255 add_charmap(const char *sym, int c)
257 add_charmap_impl(sym, c, 1);
261 add_charmap_undefined(char *sym)
264 charmap_t *cm = NULL;
267 cm = RB_FIND(cmap_sym, &cmap_sym, &srch);
269 if ((undefok == 0) && ((cm == NULL) || (cm->wc == (wchar_t)-1))) {
270 warn("undefined symbol <%s>", sym);
271 add_charmap_impl(sym, -1, 0);
278 add_charmap_range(char *s, char *e, int wc)
285 static const char *digits = "0123456789";
290 if (((si = strcspn(s, digits)) == 0) || (si == ls) ||
291 (strncmp(s, e, si) != 0) ||
292 ((int)strspn(s + si, digits) != (ls - si)) ||
293 ((int)strspn(e + si, digits) != (le - si)) ||
294 ((sn = atoi(s + si)) > ((en = atoi(e + si))))) {
295 errf("malformed charmap range");
301 for (i = sn; i <= en; i++) {
303 (void) asprintf(&nn, "%s%0*u", s, ls - si, i);
305 errf("out of memory");
309 add_charmap_impl(nn, wc, 1);
317 add_charmap_char(const char *name, int val)
319 add_charmap_impl(name, val, 0);
323 * POSIX insists that certain entries be present, even when not in the
324 * original charmap file.
327 add_charmap_posix(void)
331 for (i = 0; portable_chars[i].name; i++) {
332 add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
337 lookup_charmap(const char *sym, wchar_t *wc)
343 n = RB_FIND(cmap_sym, &cmap_sym, &srch);
344 if (n && n->wc != (wchar_t)-1) {
353 check_charmap(wchar_t wc)
358 return (RB_FIND(cmap_wc, &cmap_wc, &srch) ? 0 : -1);