xref: /freebsd/usr.bin/localedef/charmap.c (revision 2ad756a6)
1 /*-
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright 2015 John Marino <draco@marino.st>
4  *
5  * This source code is derived from the illumos localedef command, and
6  * provided under BSD-style license terms by Nexenta Systems, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
17  *
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.
29  */
30 
31 /*
32  * CHARMAP file handling for localedef.
33  */
34 #include <sys/cdefs.h>
35 #include <sys/types.h>
36 #include <sys/tree.h>
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <limits.h>
42 #include <stddef.h>
43 #include <unistd.h>
44 #include "localedef.h"
45 #include "parser.h"
46 
47 
48 typedef struct charmap {
49 	const char *name;
50 	wchar_t wc;
51 	RB_ENTRY(charmap) rb_sym;
52 	RB_ENTRY(charmap) rb_wc;
53 } charmap_t;
54 
55 static int cmap_compare_sym(const void *n1, const void *n2);
56 static int cmap_compare_wc(const void *n1, const void *n2);
57 
58 static RB_HEAD(cmap_sym, charmap) cmap_sym;
59 static RB_HEAD(cmap_wc, charmap) cmap_wc;
60 
61 RB_GENERATE_STATIC(cmap_sym, charmap, rb_sym, cmap_compare_sym);
62 RB_GENERATE_STATIC(cmap_wc, charmap, rb_wc, cmap_compare_wc);
63 
64 /*
65  * Array of POSIX specific portable characters.
66  */
67 
68 static const struct {
69 	const char *name;
70 	int	ch;
71 } portable_chars[] = {
72 	{ "NUL",		'\0' },
73 	{ "alert",		'\a' },
74 	{ "backspace",		'\b' },
75 	{ "tab",		'\t' },
76 	{ "carriage-return",	'\r' },
77 	{ "newline",		'\n' },
78 	{ "vertical-tab",	'\v' },
79 	{ "form-feed",		'\f' },
80 	{ "space",		' ' },
81 	{ "exclamation-mark",	'!' },
82 	{ "quotation-mark",	'"' },
83 	{ "number-sign",	'#' },
84 	{ "dollar-sign",	'$' },
85 	{ "percent-sign",	'%' },
86 	{ "ampersand",		'&' },
87 	{ "apostrophe",		'\'' },
88 	{ "left-parenthesis",	'(' },
89 	{ "right-parenthesis",	')' },
90 	{ "asterisk",		'*' },
91 	{ "plus-sign",		'+' },
92 	{ "comma",		 ','},
93 	{ "hyphen-minus",	'-' },
94 	{ "hyphen",		'-' },
95 	{ "full-stop",		'.' },
96 	{ "period",		'.' },
97 	{ "slash",		'/' },
98 	{ "solidus",		'/' },
99 	{ "zero",		'0' },
100 	{ "one",		'1' },
101 	{ "two",		'2' },
102 	{ "three",		'3' },
103 	{ "four",		'4' },
104 	{ "five",		'5' },
105 	{ "six",		'6' },
106 	{ "seven",		'7' },
107 	{ "eight",		'8' },
108 	{ "nine",		'9' },
109 	{ "colon",		':' },
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",	'^' },
122 	{ "low-line",		'_' },
123 	{ "underscore",		'_' },
124 	{ "grave-accent",	'`' },
125 	{ "left-brace",		'{' },
126 	{ "left-curly-bracket",	'{' },
127 	{ "vertical-line",	'|' },
128 	{ "right-brace",	'}' },
129 	{ "right-curly-bracket", '}' },
130 	{ "tilde",		'~' },
131 	{ "A", 'A' },
132 	{ "B", 'B' },
133 	{ "C", 'C' },
134 	{ "D", 'D' },
135 	{ "E", 'E' },
136 	{ "F", 'F' },
137 	{ "G", 'G' },
138 	{ "H", 'H' },
139 	{ "I", 'I' },
140 	{ "J", 'J' },
141 	{ "K", 'K' },
142 	{ "L", 'L' },
143 	{ "M", 'M' },
144 	{ "N", 'N' },
145 	{ "O", 'O' },
146 	{ "P", 'P' },
147 	{ "Q", 'Q' },
148 	{ "R", 'R' },
149 	{ "S", 'S' },
150 	{ "T", 'T' },
151 	{ "U", 'U' },
152 	{ "V", 'V' },
153 	{ "W", 'W' },
154 	{ "X", 'X' },
155 	{ "Y", 'Y' },
156 	{ "Z", 'Z' },
157 	{ "a", 'a' },
158 	{ "b", 'b' },
159 	{ "c", 'c' },
160 	{ "d", 'd' },
161 	{ "e", 'e' },
162 	{ "f", 'f' },
163 	{ "g", 'g' },
164 	{ "h", 'h' },
165 	{ "i", 'i' },
166 	{ "j", 'j' },
167 	{ "k", 'k' },
168 	{ "l", 'l' },
169 	{ "m", 'm' },
170 	{ "n", 'n' },
171 	{ "o", 'o' },
172 	{ "p", 'p' },
173 	{ "q", 'q' },
174 	{ "r", 'r' },
175 	{ "s", 's' },
176 	{ "t", 't' },
177 	{ "u", 'u' },
178 	{ "v", 'v' },
179 	{ "w", 'w' },
180 	{ "x", 'x' },
181 	{ "y", 'y' },
182 	{ "z", 'z' },
183 	{ NULL, 0 }
184 };
185 
186 static int
187 cmap_compare_sym(const void *n1, const void *n2)
188 {
189 	const charmap_t *c1 = n1;
190 	const charmap_t *c2 = n2;
191 	int rv;
192 
193 	rv = strcmp(c1->name, c2->name);
194 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
195 }
196 
197 static int
198 cmap_compare_wc(const void *n1, const void *n2)
199 {
200 	const charmap_t *c1 = n1;
201 	const charmap_t *c2 = n2;
202 
203 	return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
204 }
205 
206 void
207 init_charmap(void)
208 {
209 	RB_INIT(&cmap_sym);
210 
211 	RB_INIT(&cmap_wc);
212 }
213 
214 static void
215 add_charmap_impl(const char *sym, wchar_t wc, int nodups)
216 {
217 	charmap_t	srch;
218 	charmap_t	*n = NULL;
219 
220 	srch.wc = wc;
221 	srch.name = sym;
222 
223 	/*
224 	 * also possibly insert the wide mapping, although note that there
225 	 * can only be one of these per wide character code.
226 	 */
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");
230 			return;
231 		}
232 		n->wc = wc;
233 		RB_INSERT(cmap_wc, &cmap_wc, n);
234 	}
235 
236 	if (sym) {
237 		if (RB_FIND(cmap_sym, &cmap_sym, &srch) != NULL) {
238 			if (nodups) {
239 				errf("duplicate character definition");
240 			}
241 			return;
242 		}
243 		if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
244 			errf("out of memory");
245 			return;
246 		}
247 		n->wc = wc;
248 		n->name = sym;
249 
250 		RB_INSERT(cmap_sym, &cmap_sym, n);
251 	}
252 }
253 
254 void
255 add_charmap(const char *sym, int c)
256 {
257 	add_charmap_impl(sym, c, 1);
258 }
259 
260 void
261 add_charmap_undefined(char *sym)
262 {
263 	charmap_t srch;
264 	charmap_t *cm = NULL;
265 
266 	srch.name = sym;
267 	cm = RB_FIND(cmap_sym, &cmap_sym, &srch);
268 
269 	if ((undefok == 0) && ((cm == NULL) || (cm->wc == (wchar_t)-1))) {
270 		warn("undefined symbol <%s>", sym);
271 		add_charmap_impl(sym, -1, 0);
272 	} else {
273 		free(sym);
274 	}
275 }
276 
277 void
278 add_charmap_range(char *s, char *e, int wc)
279 {
280 	int	ls, le;
281 	int	si;
282 	int	sn, en;
283 	int	i;
284 
285 	static const char *digits = "0123456789";
286 
287 	ls = strlen(s);
288 	le = strlen(e);
289 
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");
296 		return;
297 	}
298 
299 	s[si] = 0;
300 
301 	for (i = sn; i <= en; i++) {
302 		char *nn;
303 		(void) asprintf(&nn, "%s%0*u", s, ls - si, i);
304 		if (nn == NULL) {
305 			errf("out of memory");
306 			return;
307 		}
308 
309 		add_charmap_impl(nn, wc, 1);
310 		wc++;
311 	}
312 	free(s);
313 	free(e);
314 }
315 
316 void
317 add_charmap_char(const char *name, int val)
318 {
319 	add_charmap_impl(name, val, 0);
320 }
321 
322 /*
323  * POSIX insists that certain entries be present, even when not in the
324  * original charmap file.
325  */
326 void
327 add_charmap_posix(void)
328 {
329 	int	i;
330 
331 	for (i = 0; portable_chars[i].name; i++) {
332 		add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
333 	}
334 }
335 
336 int
337 lookup_charmap(const char *sym, wchar_t *wc)
338 {
339 	charmap_t	srch;
340 	charmap_t	*n;
341 
342 	srch.name = sym;
343 	n = RB_FIND(cmap_sym, &cmap_sym, &srch);
344 	if (n && n->wc != (wchar_t)-1) {
345 		if (wc)
346 			*wc = n->wc;
347 		return (0);
348 	}
349 	return (-1);
350 }
351 
352 int
353 check_charmap(wchar_t wc)
354 {
355 	charmap_t srch;
356 
357 	srch.wc = wc;
358 	return (RB_FIND(cmap_wc, &cmap_wc, &srch) ? 0 : -1);
359 }
360