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_PROTOTYPE_STATIC(cmap_sym
, charmap
, rb_sym
, cmap_compare_sym
);
62 RB_PROTOTYPE_STATIC(cmap_wc
, charmap
, rb_wc
, cmap_compare_wc
);
64 RB_GENERATE(cmap_sym
, charmap
, rb_sym
, cmap_compare_sym
);
65 RB_GENERATE(cmap_wc
, charmap
, rb_wc
, cmap_compare_wc
);
68 * Array of POSIX specific portable characters.
74 } portable_chars
[] = {
77 { "backspace", '\b' },
79 { "carriage-return", '\r' },
81 { "vertical-tab", '\v' },
82 { "form-feed", '\f' },
84 { "exclamation-mark", '!' },
85 { "quotation-mark", '"' },
86 { "number-sign", '#' },
87 { "dollar-sign", '$' },
88 { "percent-sign", '%' },
90 { "apostrophe", '\'' },
91 { "left-parenthesis", '(' },
92 { "right-parenthesis", '(' },
96 { "hyphen-minus", '-' },
113 { "semicolon", ';' },
114 { "less-than-sign", '<' },
115 { "equals-sign", '=' },
116 { "greater-than-sign", '>' },
117 { "question-mark", '?' },
118 { "commercial-at", '@' },
119 { "left-square-bracket", '[' },
120 { "backslash", '\\' },
121 { "reverse-solidus", '\\' },
122 { "right-square-bracket", ']' },
123 { "circumflex", '^' },
124 { "circumflex-accent", '^' },
126 { "underscore", '_' },
127 { "grave-accent", '`' },
128 { "left-brace", '{' },
129 { "left-curly-bracket", '{' },
130 { "vertical-line", '|' },
131 { "right-brace", '}' },
132 { "right-curly-bracket", '}' },
190 cmap_compare_sym(const void *n1
, const void *n2
)
192 const charmap_t
*c1
= n1
;
193 const charmap_t
*c2
= n2
;
196 rv
= strcmp(c1
->name
, c2
->name
);
197 return ((rv
< 0) ? -1 : (rv
> 0) ? 1 : 0);
201 cmap_compare_wc(const void *n1
, const void *n2
)
203 const charmap_t
*c1
= n1
;
204 const charmap_t
*c2
= n2
;
206 return ((c1
->wc
< c2
->wc
) ? -1 : (c1
->wc
> c2
->wc
) ? 1 : 0);
218 add_charmap_impl(const char *sym
, wchar_t wc
, int nodups
)
227 * also possibly insert the wide mapping, although note that there
228 * can only be one of these per wide character code.
230 if ((wc
!= (wchar_t)-1) && ((RB_FIND(cmap_wc
, &cmap_wc
, &srch
)) == NULL
)) {
231 if ((n
= calloc(1, sizeof (*n
))) == NULL
) {
232 errf("out of memory");
236 RB_INSERT(cmap_wc
, &cmap_wc
, n
);
240 if (RB_FIND(cmap_sym
, &cmap_sym
, &srch
) != NULL
) {
242 errf("duplicate character definition");
246 if ((n
== NULL
) && ((n
= calloc(1, sizeof (*n
))) == NULL
)) {
247 errf("out of memory");
253 RB_INSERT(cmap_sym
, &cmap_sym
, n
);
258 add_charmap(const char *sym
, int c
)
260 add_charmap_impl(sym
, c
, 1);
264 add_charmap_undefined(char *sym
)
267 charmap_t
*cm
= NULL
;
270 cm
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
272 if ((undefok
== 0) && ((cm
== NULL
) || (cm
->wc
== (wchar_t)-1))) {
273 warn("undefined symbol <%s>", sym
);
274 add_charmap_impl(sym
, -1, 0);
281 add_charmap_range(char *s
, char *e
, int wc
)
288 static const char *digits
= "0123456789";
293 if (((si
= strcspn(s
, digits
)) == 0) || (si
== ls
) ||
294 (strncmp(s
, e
, si
) != 0) ||
295 ((int)strspn(s
+ si
, digits
) != (ls
- si
)) ||
296 ((int)strspn(e
+ si
, digits
) != (le
- si
)) ||
297 ((sn
= atoi(s
+ si
)) > ((en
= atoi(e
+ si
))))) {
298 errf("malformed charmap range");
304 for (i
= sn
; i
<= en
; i
++) {
306 (void) asprintf(&nn
, "%s%0*u", s
, ls
- si
, i
);
308 errf("out of memory");
312 add_charmap_impl(nn
, wc
, 1);
320 add_charmap_char(const char *name
, int val
)
322 add_charmap_impl(name
, val
, 0);
326 * POSIX insists that certain entries be present, even when not in the
327 * orginal charmap file.
330 add_charmap_posix(void)
334 for (i
= 0; portable_chars
[i
].name
; i
++) {
335 add_charmap_char(portable_chars
[i
].name
, portable_chars
[i
].ch
);
340 lookup_charmap(const char *sym
, wchar_t *wc
)
346 n
= RB_FIND(cmap_sym
, &cmap_sym
, &srch
);
347 if (n
&& n
->wc
!= (wchar_t)-1) {
356 check_charmap(wchar_t wc
)
361 return (RB_FIND(cmap_wc
, &cmap_wc
, &srch
) ? 0 : -1);