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