colrm(1): Capsicumise
[freebsd/src.git] / usr.bin / localedef / charmap.c
bloba8d723163e7436b5ce8b8bceb8f60190d77b9299
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 <stddef.h>
43 #include <unistd.h>
44 #include "localedef.h"
45 #include "parser.h"
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;
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_GENERATE_STATIC(cmap_sym, charmap, rb_sym, cmap_compare_sym);
62 RB_GENERATE_STATIC(cmap_wc, charmap, rb_wc, cmap_compare_wc);
65 * Array of POSIX specific portable characters.
68 static const struct {
69 const char *name;
70 int ch;
71 } portable_chars[] = {
72 { "NUL", '\0' },
73 { "SOH", '\x01' },
74 { "STX", '\x02' },
75 { "ETX", '\x03' },
76 { "EOT", '\x04' },
77 { "ENQ", '\x05' },
78 { "ACK", '\x06' },
79 { "BEL", '\a' },
80 { "alert", '\a' },
81 { "BS", '\b' },
82 { "backspace", '\b' },
83 { "HT", '\t' },
84 { "tab", '\t' },
85 { "LF", '\n' },
86 { "newline", '\n' },
87 { "VT", '\v' },
88 { "vertical-tab", '\v' },
89 { "FF", '\f' },
90 { "form-feed", '\f' },
91 { "CR", '\r' },
92 { "carriage-return", '\r' },
93 { "SO", '\x0e' },
94 { "SI", '\x0f' },
95 { "DLE", '\x10' },
96 { "DC1", '\x11' },
97 { "DC2", '\x12' },
98 { "DC3", '\x13' },
99 { "DC4", '\x14' },
100 { "NAK", '\x15' },
101 { "SYN", '\x16' },
102 { "ETB", '\x17' },
103 { "CAN", '\x18' },
104 { "EM", '\x19' },
105 { "SUB", '\x1a' },
106 { "ESC", '\x1b' },
107 { "FS", '\x1c' },
108 { "IS4", '\x1c' },
109 { "GS", '\x1d' },
110 { "IS3", '\x1d' },
111 { "RS", '\x1e' },
112 { "IS2", '\x1e' },
113 { "US", '\x1f' },
114 { "IS1", '\x1f' },
115 { "DEL", '\x7f' },
116 { "space", ' ' },
117 { "exclamation-mark", '!' },
118 { "quotation-mark", '"' },
119 { "number-sign", '#' },
120 { "dollar-sign", '$' },
121 { "percent-sign", '%' },
122 { "ampersand", '&' },
123 { "apostrophe", '\'' },
124 { "left-parenthesis", '(' },
125 { "right-parenthesis", ')' },
126 { "asterisk", '*' },
127 { "plus-sign", '+' },
128 { "comma", ','},
129 { "hyphen-minus", '-' },
130 { "hyphen", '-' },
131 { "full-stop", '.' },
132 { "period", '.' },
133 { "slash", '/' },
134 { "solidus", '/' },
135 { "zero", '0' },
136 { "one", '1' },
137 { "two", '2' },
138 { "three", '3' },
139 { "four", '4' },
140 { "five", '5' },
141 { "six", '6' },
142 { "seven", '7' },
143 { "eight", '8' },
144 { "nine", '9' },
145 { "colon", ':' },
146 { "semicolon", ';' },
147 { "less-than-sign", '<' },
148 { "equals-sign", '=' },
149 { "greater-than-sign", '>' },
150 { "question-mark", '?' },
151 { "commercial-at", '@' },
152 { "left-square-bracket", '[' },
153 { "backslash", '\\' },
154 { "reverse-solidus", '\\' },
155 { "right-square-bracket", ']' },
156 { "circumflex", '^' },
157 { "circumflex-accent", '^' },
158 { "low-line", '_' },
159 { "underscore", '_' },
160 { "grave-accent", '`' },
161 { "left-brace", '{' },
162 { "left-curly-bracket", '{' },
163 { "vertical-line", '|' },
164 { "right-brace", '}' },
165 { "right-curly-bracket", '}' },
166 { "tilde", '~' },
167 { "A", 'A' },
168 { "B", 'B' },
169 { "C", 'C' },
170 { "D", 'D' },
171 { "E", 'E' },
172 { "F", 'F' },
173 { "G", 'G' },
174 { "H", 'H' },
175 { "I", 'I' },
176 { "J", 'J' },
177 { "K", 'K' },
178 { "L", 'L' },
179 { "M", 'M' },
180 { "N", 'N' },
181 { "O", 'O' },
182 { "P", 'P' },
183 { "Q", 'Q' },
184 { "R", 'R' },
185 { "S", 'S' },
186 { "T", 'T' },
187 { "U", 'U' },
188 { "V", 'V' },
189 { "W", 'W' },
190 { "X", 'X' },
191 { "Y", 'Y' },
192 { "Z", 'Z' },
193 { "a", 'a' },
194 { "b", 'b' },
195 { "c", 'c' },
196 { "d", 'd' },
197 { "e", 'e' },
198 { "f", 'f' },
199 { "g", 'g' },
200 { "h", 'h' },
201 { "i", 'i' },
202 { "j", 'j' },
203 { "k", 'k' },
204 { "l", 'l' },
205 { "m", 'm' },
206 { "n", 'n' },
207 { "o", 'o' },
208 { "p", 'p' },
209 { "q", 'q' },
210 { "r", 'r' },
211 { "s", 's' },
212 { "t", 't' },
213 { "u", 'u' },
214 { "v", 'v' },
215 { "w", 'w' },
216 { "x", 'x' },
217 { "y", 'y' },
218 { "z", 'z' },
219 { NULL, 0 }
222 static int
223 cmap_compare_sym(const void *n1, const void *n2)
225 const charmap_t *c1 = n1;
226 const charmap_t *c2 = n2;
227 int rv;
229 rv = strcmp(c1->name, c2->name);
230 return ((rv < 0) ? -1 : (rv > 0) ? 1 : 0);
233 static int
234 cmap_compare_wc(const void *n1, const void *n2)
236 const charmap_t *c1 = n1;
237 const charmap_t *c2 = n2;
239 return ((c1->wc < c2->wc) ? -1 : (c1->wc > c2->wc) ? 1 : 0);
242 void
243 init_charmap(void)
245 RB_INIT(&cmap_sym);
247 RB_INIT(&cmap_wc);
250 static void
251 add_charmap_impl(const char *sym, wchar_t wc, int nodups)
253 charmap_t srch;
254 charmap_t *n = NULL;
256 srch.wc = wc;
257 srch.name = sym;
260 * also possibly insert the wide mapping, although note that there
261 * can only be one of these per wide character code.
263 if ((wc != (wchar_t)-1) && ((RB_FIND(cmap_wc, &cmap_wc, &srch)) == NULL)) {
264 if ((n = calloc(1, sizeof (*n))) == NULL) {
265 errf("out of memory");
266 return;
268 n->wc = wc;
269 RB_INSERT(cmap_wc, &cmap_wc, n);
272 if (sym) {
273 if (RB_FIND(cmap_sym, &cmap_sym, &srch) != NULL) {
274 if (nodups) {
275 errf("duplicate character definition");
277 return;
279 if ((n == NULL) && ((n = calloc(1, sizeof (*n))) == NULL)) {
280 errf("out of memory");
281 return;
283 n->wc = wc;
284 n->name = sym;
286 RB_INSERT(cmap_sym, &cmap_sym, n);
290 void
291 add_charmap(const char *sym, int c)
293 add_charmap_impl(sym, c, 1);
296 void
297 add_charmap_undefined(char *sym)
299 charmap_t srch;
300 charmap_t *cm = NULL;
302 srch.name = sym;
303 cm = RB_FIND(cmap_sym, &cmap_sym, &srch);
305 if ((undefok == 0) && ((cm == NULL) || (cm->wc == (wchar_t)-1))) {
306 warn("undefined symbol <%s>", sym);
307 add_charmap_impl(sym, -1, 0);
308 } else {
309 free(sym);
313 void
314 add_charmap_range(char *s, char *e, int wc)
316 int ls, le;
317 int si;
318 int sn, en;
319 int i;
321 static const char *digits = "0123456789";
323 ls = strlen(s);
324 le = strlen(e);
326 if (((si = strcspn(s, digits)) == 0) || (si == ls) ||
327 (strncmp(s, e, si) != 0) ||
328 ((int)strspn(s + si, digits) != (ls - si)) ||
329 ((int)strspn(e + si, digits) != (le - si)) ||
330 ((sn = atoi(s + si)) > ((en = atoi(e + si))))) {
331 errf("malformed charmap range");
332 return;
335 s[si] = 0;
337 for (i = sn; i <= en; i++) {
338 char *nn;
339 (void) asprintf(&nn, "%s%0*u", s, ls - si, i);
340 if (nn == NULL) {
341 errf("out of memory");
342 return;
345 add_charmap_impl(nn, wc, 1);
346 wc++;
348 free(s);
349 free(e);
352 void
353 add_charmap_char(const char *name, int val)
355 add_charmap_impl(name, val, 0);
359 * POSIX insists that certain entries be present, even when not in the
360 * original charmap file.
362 void
363 add_charmap_posix(void)
365 int i;
367 for (i = 0; portable_chars[i].name; i++) {
368 add_charmap_char(portable_chars[i].name, portable_chars[i].ch);
373 lookup_charmap(const char *sym, wchar_t *wc)
375 charmap_t srch;
376 charmap_t *n;
378 srch.name = sym;
379 n = RB_FIND(cmap_sym, &cmap_sym, &srch);
380 if (n && n->wc != (wchar_t)-1) {
381 if (wc)
382 *wc = n->wc;
383 return (0);
385 return (-1);
389 check_charmap(wchar_t wc)
391 charmap_t srch;
393 srch.wc = wc;
394 return (RB_FIND(cmap_wc, &cmap_wc, &srch) ? 0 : -1);