newsyslog(8): Further reduce differences with FreeBSD.
[dragonfly.git] / usr.bin / localedef / charmap.c
blob8f0727ddd93e63dcbf35e2d4a3a0c5e7f174def6
1 /*
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
10 * are met:
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>
36 #include <sys/tree.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <unistd.h>
43 #include <stddef.h>
44 #include <unistd.h>
45 #include "localedef.h"
46 #include "parser.h"
49 typedef struct charmap {
50 const char *name;
51 wchar_t wc;
52 RB_ENTRY(charmap) rb_sym;
53 RB_ENTRY(charmap) rb_wc;
54 } charmap_t;
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.
72 static const struct {
73 const char *name;
74 int ch;
75 } portable_chars[] = {
76 { "NUL", '\0' },
77 { "alert", '\a' },
78 { "backspace", '\b' },
79 { "tab", '\t' },
80 { "carriage-return", '\r' },
81 { "newline", '\n' },
82 { "vertical-tab", '\v' },
83 { "form-feed", '\f' },
84 { "space", ' ' },
85 { "exclamation-mark", '!' },
86 { "quotation-mark", '"' },
87 { "number-sign", '#' },
88 { "dollar-sign", '$' },
89 { "percent-sign", '%' },
90 { "ampersand", '&' },
91 { "apostrophe", '\'' },
92 { "left-parenthesis", '(' },
93 { "right-parenthesis", '(' },
94 { "asterisk", '*' },
95 { "plus-sign", '+' },
96 { "comma", ','},
97 { "hyphen-minus", '-' },
98 { "hyphen", '-' },
99 { "full-stop", '.' },
100 { "period", '.' },
101 { "slash", '/' },
102 { "solidus", '/' },
103 { "zero", '0' },
104 { "one", '1' },
105 { "two", '2' },
106 { "three", '3' },
107 { "four", '4' },
108 { "five", '5' },
109 { "six", '6' },
110 { "seven", '7' },
111 { "eight", '8' },
112 { "nine", '9' },
113 { "colon", ':' },
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", '^' },
126 { "low-line", '_' },
127 { "underscore", '_' },
128 { "grave-accent", '`' },
129 { "left-brace", '{' },
130 { "left-curly-bracket", '{' },
131 { "vertical-line", '|' },
132 { "right-brace", '}' },
133 { "right-curly-bracket", '}' },
134 { "tilde", '~' },
135 { "A", 'A' },
136 { "B", 'B' },
137 { "C", 'C' },
138 { "D", 'D' },
139 { "E", 'E' },
140 { "F", 'F' },
141 { "G", 'G' },
142 { "H", 'H' },
143 { "I", 'I' },
144 { "J", 'J' },
145 { "K", 'K' },
146 { "L", 'L' },
147 { "M", 'M' },
148 { "N", 'N' },
149 { "O", 'O' },
150 { "P", 'P' },
151 { "Q", 'Q' },
152 { "R", 'R' },
153 { "S", 'S' },
154 { "T", 'T' },
155 { "U", 'U' },
156 { "V", 'V' },
157 { "W", 'W' },
158 { "X", 'X' },
159 { "Y", 'Y' },
160 { "Z", 'Z' },
161 { "a", 'a' },
162 { "b", 'b' },
163 { "c", 'c' },
164 { "d", 'd' },
165 { "e", 'e' },
166 { "f", 'f' },
167 { "g", 'g' },
168 { "h", 'h' },
169 { "i", 'i' },
170 { "j", 'j' },
171 { "k", 'k' },
172 { "l", 'l' },
173 { "m", 'm' },
174 { "n", 'n' },
175 { "o", 'o' },
176 { "p", 'p' },
177 { "q", 'q' },
178 { "r", 'r' },
179 { "s", 's' },
180 { "t", 't' },
181 { "u", 'u' },
182 { "v", 'v' },
183 { "w", 'w' },
184 { "x", 'x' },
185 { "y", 'y' },
186 { "z", 'z' },
187 { NULL, 0 }
190 static int
191 cmap_compare_sym(const void *n1, const void *n2)
193 const charmap_t *c1 = n1;
194 const charmap_t *c2 = n2;
195 int rv;
197 rv = strcmp(c1->name, c2->name);
198 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
201 static int
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);
210 void
211 init_charmap(void)
213 RB_INIT(&cmap_sym);
215 RB_INIT(&cmap_wc);
218 static void
219 add_charmap_impl(const char *sym, wchar_t wc, int nodups)
221 charmap_t srch;
222 charmap_t *n = NULL;
224 srch.wc = wc;
225 srch.name = sym;
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");
234 return;
236 n->wc = wc;
237 RB_INSERT(cmap_wc, &cmap_wc, n);
240 if (sym) {
241 if (RB_FIND(cmap_sym, &cmap_sym, &srch) != NULL) {
242 if (nodups) {
243 errf("duplicate character definition");
245 return;
247 if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
248 errf("out of memory");
249 return;
251 n->wc = wc;
252 n->name = sym;
254 RB_INSERT(cmap_sym, &cmap_sym, n);
258 void
259 add_charmap(const char *sym, int c)
261 add_charmap_impl(sym, c, 1);
264 void
265 add_charmap_undefined(char *sym)
267 charmap_t srch;
268 charmap_t *cm = NULL;
270 srch.name = sym;
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);
276 } else {
277 free(sym);
281 void
282 add_charmap_range(char *s, char *e, int wc)
284 int ls, le;
285 int si;
286 int sn, en;
287 int i;
289 static const char *digits = "0123456789";
291 ls = strlen(s);
292 le = strlen(e);
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");
300 return;
303 s[si] = 0;
305 for (i = sn; i <= en; i++) {
306 char *nn;
307 (void) asprintf(&nn, "%s%0*u", s, ls - si, i);
308 if (nn == NULL) {
309 errf("out of memory");
310 return;
313 add_charmap_impl(nn, wc, 1);
314 wc++;
316 free(s);
317 free(e);
320 void
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.
330 void
331 add_charmap_posix(void)
333 int i;
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)
343 charmap_t srch;
344 charmap_t *n;
346 srch.name = sym;
347 n = RB_FIND(cmap_sym, &cmap_sym, &srch);
348 if (n && n->wc != (wchar_t)-1) {
349 if (wc)
350 *wc = n->wc;
351 return (0);
353 return (-1);
357 check_charmap(wchar_t wc)
359 charmap_t srch;
361 srch.wc = wc;
362 return (RB_FIND(cmap_wc, &cmap_wc, &srch) ? 0 : -1);