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.
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
[] = {
82 { "backspace", '\b' },
88 { "vertical-tab", '\v' },
90 { "form-feed", '\f' },
92 { "carriage-return", '\r' },
117 { "exclamation-mark", '!' },
118 { "quotation-mark", '"' },
119 { "number-sign", '#' },
120 { "dollar-sign", '$' },
121 { "percent-sign", '%' },
122 { "ampersand", '&' },
123 { "apostrophe", '\'' },
124 { "left-parenthesis", '(' },
125 { "right-parenthesis", ')' },
127 { "plus-sign", '+' },
129 { "hyphen-minus", '-' },
131 { "full-stop", '.' },
146 { "semicolon", ';' },
147 { "less-than-sign", '<' },
148 { "equals-sign", '=' },
149 { "greater-than-sign", '>' },
150 { "question-mark", '?' },
151 { "commercial-at", '@' },
152 { "left-square-bracket", '[' },
153 { "backslash", '\\' },
154 { "reverse-solidus", '\\' },
155 { "right-square-bracket", ']' },
156 { "circumflex", '^' },
157 { "circumflex-accent", '^' },
159 { "underscore", '_' },
160 { "grave-accent", '`' },
161 { "left-brace", '{' },
162 { "left-curly-bracket", '{' },
163 { "vertical-line", '|' },
164 { "right-brace", '}' },
165 { "right-curly-bracket", '}' },
223 cmap_compare_sym(const void *n1
, const void *n2
)
225 const charmap_t
*c1
= n1
;
226 const charmap_t
*c2
= n2
;
229 rv
= strcmp(c1
->name
, c2
->name
);
230 return ((rv
< 0) ? -1 : (rv
> 0) ? 1 : 0);
234 cmap_compare_wc(const void *n1
, const void *n2
)
236 const charmap_t
*c1
= n1
;
237 const charmap_t
*c2
= n2
;
239 return ((c1
->wc
< c2
->wc
) ? -1 : (c1
->wc
> c2
->wc
) ? 1 : 0);
251 add_charmap_impl(const char *sym
, wchar_t wc
, int nodups
)
260 * also possibly insert the wide mapping, although note that there
261 * can only be one of these per wide character code.
263 if ((wc
!= (wchar_t)-1) && ((RB_FIND(cmap_wc
, &cmap_wc
, &srch
)) == NULL
)) {
264 if ((n
= calloc(1, sizeof (*n
))) == NULL
) {
265 errf("out of memory");
269 RB_INSERT(cmap_wc
, &cmap_wc
, n
);
273 if (RB_FIND(cmap_sym
, &cmap_sym
, &srch
) != NULL
) {
275 errf("duplicate character definition");
279 if ((n
== NULL
) && ((n
= calloc(1, sizeof (*n
))) == NULL
)) {
280 errf("out of memory");
286 RB_INSERT(cmap_sym
, &cmap_sym
, n
);
291 add_charmap(const char *sym
, int c
)
293 add_charmap_impl(sym
, c
, 1);
297 add_charmap_undefined(char *sym
)
300 charmap_t
*cm
= NULL
;
303 cm
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
305 if ((undefok
== 0) && ((cm
== NULL
) || (cm
->wc
== (wchar_t)-1))) {
306 warn("undefined symbol <%s>", sym
);
307 add_charmap_impl(sym
, -1, 0);
314 add_charmap_range(char *s
, char *e
, int wc
)
321 static const char *digits
= "0123456789";
326 if (((si
= strcspn(s
, digits
)) == 0) || (si
== ls
) ||
327 (strncmp(s
, e
, si
) != 0) ||
328 ((int)strspn(s
+ si
, digits
) != (ls
- si
)) ||
329 ((int)strspn(e
+ si
, digits
) != (le
- si
)) ||
330 ((sn
= atoi(s
+ si
)) > ((en
= atoi(e
+ si
))))) {
331 errf("malformed charmap range");
337 for (i
= sn
; i
<= en
; i
++) {
339 (void) asprintf(&nn
, "%s%0*u", s
, ls
- si
, i
);
341 errf("out of memory");
345 add_charmap_impl(nn
, wc
, 1);
353 add_charmap_char(const char *name
, int val
)
355 add_charmap_impl(name
, val
, 0);
359 * POSIX insists that certain entries be present, even when not in the
360 * original charmap file.
363 add_charmap_posix(void)
367 for (i
= 0; portable_chars
[i
].name
; i
++) {
368 add_charmap_char(portable_chars
[i
].name
, portable_chars
[i
].ch
);
373 lookup_charmap(const char *sym
, wchar_t *wc
)
379 n
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
380 if (n
&& n
->wc
!= (wchar_t)-1) {
389 check_charmap(wchar_t wc
)
394 return (RB_FIND(cmap_wc
, &cmap_wc
, &srch
) ? 0 : -1);