xref: /titanic_44/usr/src/cmd/localedef/charmap.c (revision 5aec55eb0591d2fcdd38d7dd5408a6ff3456e596)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
14  */
15 
16 /*
17  * CHARMAP file handling for localedef.
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <limits.h>
24 #include <unistd.h>
25 #include <alloca.h>
26 #include <sys/avl.h>
27 #include <stddef.h>
28 #include <unistd.h>
29 #include "localedef.h"
30 #include "parser.tab.h"
31 
32 static avl_tree_t	cmap_sym;
33 static avl_tree_t	cmap_wc;
34 
35 typedef struct charmap {
36 	const char *name;
37 	wchar_t wc;
38 	avl_node_t avl_sym;
39 	avl_node_t avl_wc;
40 } charmap_t;
41 
42 /*
43  * Array of POSIX specific portable characters.
44  */
45 static const struct {
46 	char	*name;
47 	int	ch;
48 } portable_chars[] = {
49 	{ "NUL",		 '\0' },
50 	{ "alert",		'\a' },
51 	{ "backspace",		'\b' },
52 	{ "tab",		'\t' },
53 	{ "carriage-return",	'\r' },
54 	{ "newline",		'\n' },
55 	{ "vertical-tab",	'\v' },
56 	{ "form-feed",		'\f' },
57 	{ "space",		' ' },
58 	{ "exclamation-mark",	'!' },
59 	{ "quotation-mark",	'"' },
60 	{ "number-sign",	'#' },
61 	{ "dollar-sign",	'$' },
62 	{ "percent-sign",	'%' },
63 	{ "ampersand",		'&' },
64 	{ "apostrophe",		'\'' },
65 	{ "left-parenthesis",	'(' },
66 	{ "right-parenthesis",	'(' },
67 	{ "asterisk",		'*' },
68 	{ "plus-sign",		'+' },
69 	{ "comma",		 ','},
70 	{ "hyphen-minus",	'-' },
71 	{ "hyphen",		'-' },
72 	{ "full-stop",		'.' },
73 	{ "period",		'.' },
74 	{ "slash",		'/' },
75 	{ "solidus",		'/' },
76 	{ "zero",		'0' },
77 	{ "one",		'1' },
78 	{ "two",		'2' },
79 	{ "three",		'3' },
80 	{ "four",		'4' },
81 	{ "five",		'5' },
82 	{ "six",		'6' },
83 	{ "seven",		'7' },
84 	{ "eight",		'8' },
85 	{ "nine",		'9' },
86 	{ "colon",		':' },
87 	{ "semicolon",		';' },
88 	{ "less-than-sign",	'<' },
89 	{ "equals-sign",	'=' },
90 	{ "greater-than-sign",	'>' },
91 	{ "question-mark",	'?' },
92 	{ "commercial-at",	'@' },
93 	{ "left-square-bracket", '[' },
94 	{ "backslash",		'\\' },
95 	{ "reverse-solidus",	'\\' },
96 	{ "right-square-bracket", ']' },
97 	{ "circumflex",		'^' },
98 	{ "circumflex-accent",	'^' },
99 	{ "low-line",		'_' },
100 	{ "underscore",		'_' },
101 	{ "grave-accent",	'`' },
102 	{ "left-brace",		'{' },
103 	{ "left-curly-bracket",	'{' },
104 	{ "vertical-line",	'|' },
105 	{ "right-brace",	'}' },
106 	{ "right-curly-bracket", '}' },
107 	{ "tilde",		'~' },
108 	{ "A", 'A' },
109 	{ "B", 'B' },
110 	{ "C", 'C' },
111 	{ "D", 'D' },
112 	{ "E", 'E' },
113 	{ "F", 'F' },
114 	{ "G", 'G' },
115 	{ "H", 'H' },
116 	{ "I", 'I' },
117 	{ "J", 'J' },
118 	{ "K", 'K' },
119 	{ "L", 'L' },
120 	{ "M", 'M' },
121 	{ "N", 'N' },
122 	{ "O", 'O' },
123 	{ "P", 'P' },
124 	{ "Q", 'Q' },
125 	{ "R", 'R' },
126 	{ "S", 'S' },
127 	{ "T", 'T' },
128 	{ "U", 'U' },
129 	{ "V", 'V' },
130 	{ "W", 'W' },
131 	{ "X", 'X' },
132 	{ "Y", 'Y' },
133 	{ "Z", 'Z' },
134 	{ "a", 'a' },
135 	{ "b", 'b' },
136 	{ "c", 'c' },
137 	{ "d", 'd' },
138 	{ "e", 'e' },
139 	{ "f", 'f' },
140 	{ "g", 'g' },
141 	{ "h", 'h' },
142 	{ "i", 'i' },
143 	{ "j", 'j' },
144 	{ "k", 'k' },
145 	{ "l", 'l' },
146 	{ "m", 'm' },
147 	{ "n", 'n' },
148 	{ "o", 'o' },
149 	{ "p", 'p' },
150 	{ "q", 'q' },
151 	{ "r", 'r' },
152 	{ "s", 's' },
153 	{ "t", 't' },
154 	{ "u", 'u' },
155 	{ "v", 'v' },
156 	{ "w", 'w' },
157 	{ "x", 'x' },
158 	{ "y", 'y' },
159 	{ "z", 'z' },
160 	{ NULL, 0 }
161 };
162 
163 static int
cmap_compare_sym(const void * n1,const void * n2)164 cmap_compare_sym(const void *n1, const void *n2)
165 {
166 	const charmap_t *c1 = n1;
167 	const charmap_t *c2 = n2;
168 	int rv;
169 
170 	rv = strcmp(c1->name, c2->name);
171 	return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
172 }
173 
174 static int
cmap_compare_wc(const void * n1,const void * n2)175 cmap_compare_wc(const void *n1, const void *n2)
176 {
177 	const charmap_t *c1 = n1;
178 	const charmap_t *c2 = n2;
179 
180 	return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
181 }
182 
183 void
init_charmap(void)184 init_charmap(void)
185 {
186 	avl_create(&cmap_sym, cmap_compare_sym, sizeof (charmap_t),
187 	    offsetof(charmap_t, avl_sym));
188 
189 	avl_create(&cmap_wc, cmap_compare_wc, sizeof (charmap_t),
190 	    offsetof(charmap_t, avl_wc));
191 }
192 
193 static void
add_charmap_impl(char * sym,wchar_t wc,int nodups)194 add_charmap_impl(char *sym, wchar_t wc, int nodups)
195 {
196 	charmap_t	srch;
197 	charmap_t	*n = NULL;
198 	avl_index_t	where;
199 
200 	srch.wc = wc;
201 	srch.name = sym;
202 
203 	/*
204 	 * also possibly insert the wide mapping, although note that there
205 	 * can only be one of these per wide character code.
206 	 */
207 	if ((wc != -1) && ((avl_find(&cmap_wc, &srch, &where)) == NULL)) {
208 		if ((n = calloc(1, sizeof (*n))) == NULL) {
209 			errf(_("out of memory"));
210 			return;
211 		}
212 		n->wc = wc;
213 		avl_insert(&cmap_wc, n, where);
214 	}
215 
216 	if (sym) {
217 		if (avl_find(&cmap_sym, &srch, &where) != NULL) {
218 			if (nodups) {
219 				errf(_("duplicate character definition"));
220 			}
221 			return;
222 		}
223 		if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
224 			errf(_("out of memory"));
225 			return;
226 		}
227 		n->wc = wc;
228 		n->name = sym;
229 
230 		avl_insert(&cmap_sym, n, where);
231 	}
232 }
233 
234 void
add_charmap(char * sym,int c)235 add_charmap(char *sym, int c)
236 {
237 	add_charmap_impl(sym, c, 1);
238 }
239 
240 void
add_charmap_undefined(char * sym)241 add_charmap_undefined(char *sym)
242 {
243 	charmap_t srch;
244 	charmap_t *cm = NULL;
245 
246 	srch.name = sym;
247 	cm = avl_find(&cmap_sym, &srch, NULL);
248 
249 	if ((undefok == 0) && ((cm == NULL) || (cm->wc == -1))) {
250 		warn(_("undefined symbol <%s>"), sym);
251 		add_charmap_impl(sym, -1, 0);
252 	} else {
253 		free(sym);
254 	}
255 }
256 
257 void
add_charmap_range(char * s,char * e,int wc)258 add_charmap_range(char *s, char *e, int wc)
259 {
260 	int	ls, le;
261 	int	si;
262 	int	sn, en;
263 	int	i;
264 
265 	static const char *digits = "0123456789";
266 
267 	ls = strlen(s);
268 	le = strlen(e);
269 
270 	if (((si = strcspn(s, digits)) == 0) || (si == ls) ||
271 	    (strncmp(s, e, si) != 0) ||
272 	    (strspn(s + si, digits) != (ls - si)) ||
273 	    (strspn(e + si, digits) != (le - si)) ||
274 	    ((sn = atoi(s + si)) > ((en = atoi(e + si))))) {
275 		errf(_("malformed charmap range"));
276 		return;
277 	}
278 
279 	s[si] = 0;
280 
281 	for (i = sn; i <= en; i++) {
282 		char *nn;
283 		(void) asprintf(&nn, "%s%0*u", s, ls - si, i);
284 		if (nn == NULL) {
285 			errf(_("out of memory"));
286 			return;
287 		}
288 
289 		add_charmap_impl(nn, wc, 1);
290 		wc++;
291 	}
292 	free(s);
293 	free(e);
294 }
295 
296 void
add_charmap_char(char * name,int val)297 add_charmap_char(char *name, int val)
298 {
299 	add_charmap_impl(name, val, 0);
300 }
301 
302 /*
303  * POSIX insists that certain entries be present, even when not in the
304  * orginal charmap file.
305  */
306 void
add_charmap_posix(void)307 add_charmap_posix(void)
308 {
309 	char	i;
310 
311 	for (i = 0; portable_chars[i].name; i++) {
312 		add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
313 	}
314 }
315 
316 int
lookup_charmap(const char * sym,wchar_t * wc)317 lookup_charmap(const char *sym, wchar_t *wc)
318 {
319 	charmap_t	srch;
320 	charmap_t	*n;
321 
322 	srch.name = sym;
323 	n = avl_find(&cmap_sym, &srch, NULL);
324 	if (n && n->wc != -1) {
325 		if (wc)
326 			*wc = n->wc;
327 		return (0);
328 	}
329 	return (-1);
330 }
331 
332 int
check_charmap(wchar_t wc)333 check_charmap(wchar_t wc)
334 {
335 	charmap_t srch;
336 
337 	srch.wc = wc;
338 	return (avl_find(&cmap_wc, &srch, NULL) ? 0 : -1);
339 }
340