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>
45 #include "localedef.h"
49 typedef struct charmap
{
52 RB_ENTRY(charmap
) rb_sym
;
53 RB_ENTRY(charmap
) rb_wc
;
56 static int cmap_compare_sym(const void *n1
, const void *n2
);
57 static int cmap_compare_wc(const void *n1
, const void *n2
);
59 static RB_HEAD(cmap_sym
, charmap
) cmap_sym
;
60 static RB_HEAD(cmap_wc
, charmap
) cmap_wc
;
62 RB_PROTOTYPE_STATIC(cmap_sym
, charmap
, rb_sym
, cmap_compare_sym
);
63 RB_PROTOTYPE_STATIC(cmap_wc
, charmap
, rb_wc
, cmap_compare_wc
);
65 RB_GENERATE(cmap_sym
, charmap
, rb_sym
, cmap_compare_sym
);
66 RB_GENERATE(cmap_wc
, charmap
, rb_wc
, cmap_compare_wc
);
69 * Array of POSIX specific portable characters.
75 } portable_chars
[] = {
78 { "backspace", '\b' },
80 { "carriage-return", '\r' },
82 { "vertical-tab", '\v' },
83 { "form-feed", '\f' },
85 { "exclamation-mark", '!' },
86 { "quotation-mark", '"' },
87 { "number-sign", '#' },
88 { "dollar-sign", '$' },
89 { "percent-sign", '%' },
91 { "apostrophe", '\'' },
92 { "left-parenthesis", '(' },
93 { "right-parenthesis", '(' },
97 { "hyphen-minus", '-' },
114 { "semicolon", ';' },
115 { "less-than-sign", '<' },
116 { "equals-sign", '=' },
117 { "greater-than-sign", '>' },
118 { "question-mark", '?' },
119 { "commercial-at", '@' },
120 { "left-square-bracket", '[' },
121 { "backslash", '\\' },
122 { "reverse-solidus", '\\' },
123 { "right-square-bracket", ']' },
124 { "circumflex", '^' },
125 { "circumflex-accent", '^' },
127 { "underscore", '_' },
128 { "grave-accent", '`' },
129 { "left-brace", '{' },
130 { "left-curly-bracket", '{' },
131 { "vertical-line", '|' },
132 { "right-brace", '}' },
133 { "right-curly-bracket", '}' },
191 cmap_compare_sym(const void *n1
, const void *n2
)
193 const charmap_t
*c1
= n1
;
194 const charmap_t
*c2
= n2
;
197 rv
= strcmp(c1
->name
, c2
->name
);
198 return ((rv
< 0) ? -1 : (rv
> 0) ? 1 : 0);
202 cmap_compare_wc(const void *n1
, const void *n2
)
204 const charmap_t
*c1
= n1
;
205 const charmap_t
*c2
= n2
;
207 return ((c1
->wc
< c2
->wc
) ? -1 : (c1
->wc
> c2
->wc
) ? 1 : 0);
219 add_charmap_impl(const char *sym
, wchar_t wc
, int nodups
)
228 * also possibly insert the wide mapping, although note that there
229 * can only be one of these per wide character code.
231 if ((wc
!= (wchar_t)-1) && ((RB_FIND(cmap_wc
, &cmap_wc
, &srch
)) == NULL
)) {
232 if ((n
= calloc(1, sizeof (*n
))) == NULL
) {
233 errf("out of memory");
237 RB_INSERT(cmap_wc
, &cmap_wc
, n
);
241 if (RB_FIND(cmap_sym
, &cmap_sym
, &srch
) != NULL
) {
243 errf("duplicate character definition");
247 if ((n
== NULL
) && ((n
= calloc(1, sizeof (*n
))) == NULL
)) {
248 errf("out of memory");
254 RB_INSERT(cmap_sym
, &cmap_sym
, n
);
259 add_charmap(const char *sym
, int c
)
261 add_charmap_impl(sym
, c
, 1);
265 add_charmap_undefined(char *sym
)
268 charmap_t
*cm
= NULL
;
271 cm
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
273 if ((undefok
== 0) && ((cm
== NULL
) || (cm
->wc
== (wchar_t)-1))) {
274 warn("undefined symbol <%s>", sym
);
275 add_charmap_impl(sym
, -1, 0);
282 add_charmap_range(char *s
, char *e
, int wc
)
289 static const char *digits
= "0123456789";
294 if (((si
= strcspn(s
, digits
)) == 0) || (si
== ls
) ||
295 (strncmp(s
, e
, si
) != 0) ||
296 ((int)strspn(s
+ si
, digits
) != (ls
- si
)) ||
297 ((int)strspn(e
+ si
, digits
) != (le
- si
)) ||
298 ((sn
= atoi(s
+ si
)) > ((en
= atoi(e
+ si
))))) {
299 errf("malformed charmap range");
305 for (i
= sn
; i
<= en
; i
++) {
307 (void) asprintf(&nn
, "%s%0*u", s
, ls
- si
, i
);
309 errf("out of memory");
313 add_charmap_impl(nn
, wc
, 1);
321 add_charmap_char(const char *name
, int val
)
323 add_charmap_impl(name
, val
, 0);
327 * POSIX insists that certain entries be present, even when not in the
328 * orginal charmap file.
331 add_charmap_posix(void)
335 for (i
= 0; portable_chars
[i
].name
; i
++) {
336 add_charmap_char(portable_chars
[i
].name
, portable_chars
[i
].ch
);
341 lookup_charmap(const char *sym
, wchar_t *wc
)
347 n
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
348 if (n
&& n
->wc
!= (wchar_t)-1) {
357 check_charmap(wchar_t wc
)
362 return (RB_FIND(cmap_wc
, &cmap_wc
, &srch
) ? 0 : -1);