1 1.36 rin /* $NetBSD: yacc.y,v 1.36 2024/01/05 02:38:06 rin Exp $ */ 2 1.2 itojun 3 1.2 itojun %{ 4 1.2 itojun /*- 5 1.2 itojun * Copyright (c) 1993 6 1.2 itojun * The Regents of the University of California. All rights reserved. 7 1.2 itojun * 8 1.2 itojun * This code is derived from software contributed to Berkeley by 9 1.2 itojun * Paul Borman at Krystal Technologies. 10 1.2 itojun * 11 1.2 itojun * Redistribution and use in source and binary forms, with or without 12 1.2 itojun * modification, are permitted provided that the following conditions 13 1.2 itojun * are met: 14 1.2 itojun * 1. Redistributions of source code must retain the above copyright 15 1.2 itojun * notice, this list of conditions and the following disclaimer. 16 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright 17 1.2 itojun * notice, this list of conditions and the following disclaimer in the 18 1.2 itojun * documentation and/or other materials provided with the distribution. 19 1.22 agc * 3. Neither the name of the University nor the names of its contributors 20 1.2 itojun * may be used to endorse or promote products derived from this software 21 1.2 itojun * without specific prior written permission. 22 1.2 itojun * 23 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 1.2 itojun * SUCH DAMAGE. 34 1.2 itojun */ 35 1.2 itojun 36 1.23 lukem #if HAVE_NBTOOL_CONFIG_H 37 1.23 lukem #include "nbtool_config.h" 38 1.7 tv #endif 39 1.7 tv 40 1.2 itojun #include <sys/cdefs.h> 41 1.18 christos #ifndef lint 42 1.2 itojun #if 0 43 1.2 itojun static char sccsid[] = "@(#)yacc.y 8.1 (Berkeley) 6/6/93"; 44 1.2 itojun static char rcsid[] = "$FreeBSD$"; 45 1.2 itojun #else 46 1.36 rin __RCSID("$NetBSD: yacc.y,v 1.36 2024/01/05 02:38:06 rin Exp $"); 47 1.2 itojun #endif 48 1.18 christos #endif /* not lint */ 49 1.10 bjh21 50 1.10 bjh21 #include <sys/types.h> 51 1.10 bjh21 #include <netinet/in.h> /* Needed by <arpa/inet.h> on NetBSD 1.5. */ 52 1.10 bjh21 #include <arpa/inet.h> /* Needed for htonl on POSIX systems. */ 53 1.2 itojun 54 1.8 tv #include <err.h> 55 1.25 tnozaki #include <locale.h> 56 1.2 itojun #include <stddef.h> 57 1.2 itojun #include <stdio.h> 58 1.2 itojun #include <stdlib.h> 59 1.2 itojun #include <string.h> 60 1.2 itojun #include <unistd.h> 61 1.15 tshiozak #include <ctype.h> 62 1.2 itojun 63 1.31 tnozaki #include "runetype_file.h" 64 1.30 tnozaki 65 1.2 itojun #include "ldef.h" 66 1.7 tv 67 1.4 thorpej const char *locale_file = "<stdout>"; 68 1.2 itojun 69 1.2 itojun rune_map maplower = { { 0, }, }; 70 1.2 itojun rune_map mapupper = { { 0, }, }; 71 1.2 itojun rune_map types = { { 0, }, }; 72 1.2 itojun 73 1.30 tnozaki _FileRuneLocale new_locale = { { 0, }, }; 74 1.30 tnozaki 75 1.30 tnozaki size_t rl_variable_len = (size_t)0; 76 1.30 tnozaki void *rl_variable = NULL; 77 1.2 itojun 78 1.17 tshiozak __nbrune_t charsetbits = (__nbrune_t)0x00000000; 79 1.2 itojun #if 0 80 1.17 tshiozak __nbrune_t charsetmask = (__nbrune_t)0x0000007f; 81 1.2 itojun #endif 82 1.17 tshiozak __nbrune_t charsetmask = (__nbrune_t)0xffffffff; 83 1.2 itojun 84 1.32 sevan void set_map(rune_map *, rune_list *, u_int32_t); 85 1.32 sevan void set_digitmap(rune_map *, rune_list *); 86 1.32 sevan void add_map(rune_map *, rune_list *, u_int32_t); 87 1.32 sevan 88 1.33 dholland __dead void usage(void); 89 1.32 sevan int yyerror(const char *s); 90 1.32 sevan void *xmalloc(unsigned int sz); 91 1.32 sevan u_int32_t *xlalloc(unsigned int sz); 92 1.32 sevan u_int32_t *xrelalloc(u_int32_t *old, unsigned int sz); 93 1.32 sevan void dump_tables(void); 94 1.32 sevan int yyparse(void); 95 1.32 sevan extern int yylex(void); 96 1.25 tnozaki 97 1.25 tnozaki /* mklocaledb.c */ 98 1.32 sevan extern void mklocaledb(const char *, FILE *, FILE *); 99 1.25 tnozaki 100 1.2 itojun %} 101 1.2 itojun 102 1.2 itojun %union { 103 1.17 tshiozak __nbrune_t rune; 104 1.2 itojun int i; 105 1.2 itojun char *str; 106 1.2 itojun 107 1.2 itojun rune_list *list; 108 1.2 itojun } 109 1.2 itojun 110 1.2 itojun %token <rune> RUNE 111 1.2 itojun %token LBRK 112 1.2 itojun %token RBRK 113 1.2 itojun %token THRU 114 1.2 itojun %token MAPLOWER 115 1.2 itojun %token MAPUPPER 116 1.2 itojun %token DIGITMAP 117 1.2 itojun %token <i> LIST 118 1.2 itojun %token <str> VARIABLE 119 1.2 itojun %token CHARSET 120 1.2 itojun %token ENCODING 121 1.2 itojun %token INVALID 122 1.2 itojun %token <str> STRING 123 1.2 itojun 124 1.2 itojun %type <list> list 125 1.2 itojun %type <list> map 126 1.2 itojun 127 1.2 itojun 128 1.2 itojun %% 129 1.2 itojun 130 1.2 itojun locale : /* empty */ 131 1.2 itojun | table 132 1.2 itojun { dump_tables(); } 133 1.2 itojun ; 134 1.2 itojun 135 1.2 itojun table : entry 136 1.2 itojun | table entry 137 1.2 itojun ; 138 1.2 itojun 139 1.2 itojun entry : ENCODING STRING 140 1.34 christos { strlcpy(new_locale.frl_encoding, $2, sizeof(new_locale.frl_encoding)); } 141 1.2 itojun | VARIABLE 142 1.30 tnozaki { rl_variable_len = strlen($1) + 1; 143 1.30 tnozaki rl_variable = strdup($1); 144 1.30 tnozaki new_locale.frl_variable_len = htonl((u_int32_t)rl_variable_len); 145 1.2 itojun } 146 1.2 itojun | CHARSET RUNE 147 1.2 itojun { charsetbits = $2; charsetmask = 0x0000007f; } 148 1.2 itojun | CHARSET RUNE RUNE 149 1.2 itojun { charsetbits = $2; charsetmask = $3; } 150 1.2 itojun | CHARSET STRING 151 1.2 itojun { int final = $2[strlen($2) - 1] & 0x7f; 152 1.2 itojun charsetbits = final << 24; 153 1.2 itojun if ($2[0] == '$') { 154 1.2 itojun charsetmask = 0x00007f7f; 155 1.2 itojun if (strchr(",-./", $2[1])) 156 1.2 itojun charsetbits |= 0x80; 157 1.2 itojun if (0xd0 <= final && final <= 0xdf) 158 1.2 itojun charsetmask |= 0x007f0000; 159 1.2 itojun } else { 160 1.2 itojun charsetmask = 0x0000007f; 161 1.2 itojun if (strchr(",-./", $2[0])) 162 1.2 itojun charsetbits |= 0x80; 163 1.2 itojun if (strlen($2) == 2 && $2[0] == '!') 164 1.2 itojun charsetbits |= ((0x80 | $2[0]) << 16); 165 1.2 itojun } 166 1.2 itojun 167 1.2 itojun /* 168 1.2 itojun * special rules 169 1.2 itojun */ 170 1.2 itojun if (charsetbits == ('B' << 24) 171 1.2 itojun && charsetmask == 0x0000007f) { 172 1.2 itojun /*ASCII: 94B*/ 173 1.2 itojun charsetbits = 0; 174 1.2 itojun charsetmask = 0x0000007f; 175 1.2 itojun } else if (charsetbits == (('A' << 24) | 0x80) 176 1.2 itojun && charsetmask == 0x0000007f) { 177 1.2 itojun /*Latin1: 96A*/ 178 1.2 itojun charsetbits = 0x80; 179 1.2 itojun charsetmask = 0x0000007f; 180 1.2 itojun } 181 1.2 itojun } 182 1.2 itojun | INVALID RUNE 183 1.30 tnozaki { new_locale.frl_invalid_rune = htonl((u_int32_t)$2); } 184 1.2 itojun | LIST list 185 1.2 itojun { set_map(&types, $2, $1); } 186 1.2 itojun | MAPLOWER map 187 1.2 itojun { set_map(&maplower, $2, 0); } 188 1.2 itojun | MAPUPPER map 189 1.2 itojun { set_map(&mapupper, $2, 0); } 190 1.36 rin | DIGITMAP map 191 1.36 rin { set_digitmap(&types, $2); } 192 1.2 itojun ; 193 1.2 itojun 194 1.2 itojun list : RUNE 195 1.2 itojun { 196 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 197 1.2 itojun $$->min = ($1 & charsetmask) | charsetbits; 198 1.2 itojun $$->max = ($1 & charsetmask) | charsetbits; 199 1.2 itojun $$->next = 0; 200 1.2 itojun } 201 1.2 itojun | RUNE THRU RUNE 202 1.2 itojun { 203 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 204 1.2 itojun $$->min = ($1 & charsetmask) | charsetbits; 205 1.2 itojun $$->max = ($3 & charsetmask) | charsetbits; 206 1.2 itojun $$->next = 0; 207 1.2 itojun } 208 1.2 itojun | list RUNE 209 1.2 itojun { 210 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 211 1.2 itojun $$->min = ($2 & charsetmask) | charsetbits; 212 1.2 itojun $$->max = ($2 & charsetmask) | charsetbits; 213 1.2 itojun $$->next = $1; 214 1.2 itojun } 215 1.2 itojun | list RUNE THRU RUNE 216 1.2 itojun { 217 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 218 1.2 itojun $$->min = ($2 & charsetmask) | charsetbits; 219 1.2 itojun $$->max = ($4 & charsetmask) | charsetbits; 220 1.2 itojun $$->next = $1; 221 1.2 itojun } 222 1.2 itojun ; 223 1.2 itojun 224 1.2 itojun map : LBRK RUNE RUNE RBRK 225 1.2 itojun { 226 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 227 1.2 itojun $$->min = ($2 & charsetmask) | charsetbits; 228 1.2 itojun $$->max = ($2 & charsetmask) | charsetbits; 229 1.2 itojun $$->map = $3; 230 1.2 itojun $$->next = 0; 231 1.2 itojun } 232 1.2 itojun | map LBRK RUNE RUNE RBRK 233 1.2 itojun { 234 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 235 1.2 itojun $$->min = ($3 & charsetmask) | charsetbits; 236 1.2 itojun $$->max = ($3 & charsetmask) | charsetbits; 237 1.2 itojun $$->map = $4; 238 1.2 itojun $$->next = $1; 239 1.2 itojun } 240 1.2 itojun | LBRK RUNE THRU RUNE ':' RUNE RBRK 241 1.2 itojun { 242 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 243 1.2 itojun $$->min = ($2 & charsetmask) | charsetbits; 244 1.2 itojun $$->max = ($4 & charsetmask) | charsetbits; 245 1.2 itojun $$->map = $6; 246 1.2 itojun $$->next = 0; 247 1.2 itojun } 248 1.2 itojun | map LBRK RUNE THRU RUNE ':' RUNE RBRK 249 1.2 itojun { 250 1.2 itojun $$ = (rune_list *)malloc(sizeof(rune_list)); 251 1.2 itojun $$->min = ($3 & charsetmask) | charsetbits; 252 1.2 itojun $$->max = ($5 & charsetmask) | charsetbits; 253 1.2 itojun $$->map = $7; 254 1.2 itojun $$->next = $1; 255 1.2 itojun } 256 1.2 itojun ; 257 1.2 itojun %% 258 1.2 itojun 259 1.2 itojun int debug = 0; 260 1.8 tv FILE *ofile; 261 1.2 itojun 262 1.2 itojun int 263 1.32 sevan main(int ac, char *av[]) 264 1.2 itojun { 265 1.2 itojun int x; 266 1.25 tnozaki const char *locale_type; 267 1.2 itojun 268 1.2 itojun extern char *optarg; 269 1.2 itojun extern int optind; 270 1.2 itojun 271 1.25 tnozaki locale_type = NULL; 272 1.25 tnozaki while ((x = getopt(ac, av, "do:t:")) != EOF) { 273 1.2 itojun switch(x) { 274 1.2 itojun case 'd': 275 1.2 itojun debug = 1; 276 1.2 itojun break; 277 1.2 itojun case 'o': 278 1.2 itojun locale_file = optarg; 279 1.8 tv if ((ofile = fopen(locale_file, "w")) == 0) 280 1.5 thorpej err(1, "unable to open output file %s", locale_file); 281 1.2 itojun break; 282 1.25 tnozaki case 't': 283 1.25 tnozaki locale_type = optarg; 284 1.25 tnozaki break; 285 1.2 itojun default: 286 1.25 tnozaki usage(); 287 1.2 itojun } 288 1.2 itojun } 289 1.2 itojun 290 1.2 itojun switch (ac - optind) { 291 1.2 itojun case 0: 292 1.2 itojun break; 293 1.2 itojun case 1: 294 1.5 thorpej if (freopen(av[optind], "r", stdin) == 0) 295 1.5 thorpej err(1, "unable to open input file %s", av[optind]); 296 1.2 itojun break; 297 1.2 itojun default: 298 1.25 tnozaki usage(); 299 1.25 tnozaki } 300 1.25 tnozaki 301 1.25 tnozaki if (ofile == NULL) 302 1.25 tnozaki ofile = stdout; 303 1.25 tnozaki if (locale_type != NULL && strcasecmp(locale_type, "CTYPE")) { 304 1.25 tnozaki mklocaledb(locale_type, stdin, ofile); 305 1.25 tnozaki return 0; 306 1.2 itojun } 307 1.25 tnozaki 308 1.29 tnozaki for (x = 0; x < _CTYPE_CACHE_SIZE; ++x) { 309 1.2 itojun mapupper.map[x] = x; 310 1.2 itojun maplower.map[x] = x; 311 1.2 itojun } 312 1.25 tnozaki 313 1.31 tnozaki new_locale.frl_invalid_rune = htonl((u_int32_t)_DEFAULT_INVALID_RUNE); 314 1.31 tnozaki memcpy(new_locale.frl_magic, _RUNECT10_MAGIC, sizeof(new_locale.frl_magic)); 315 1.2 itojun 316 1.2 itojun yyparse(); 317 1.2 itojun 318 1.2 itojun return 0; 319 1.25 tnozaki 320 1.25 tnozaki } 321 1.25 tnozaki 322 1.25 tnozaki void 323 1.33 dholland usage(void) 324 1.25 tnozaki { 325 1.25 tnozaki fprintf(stderr, 326 1.25 tnozaki "usage: mklocale [-d] [-o output] [-t type] [source]\n"); 327 1.25 tnozaki 328 1.25 tnozaki exit(1); 329 1.2 itojun } 330 1.2 itojun 331 1.2 itojun int 332 1.33 dholland yyerror(const char *s) 333 1.2 itojun { 334 1.2 itojun fprintf(stderr, "%s\n", s); 335 1.2 itojun 336 1.2 itojun return 0; 337 1.2 itojun } 338 1.2 itojun 339 1.2 itojun void * 340 1.33 dholland xmalloc(unsigned int sz) 341 1.2 itojun { 342 1.2 itojun void *r = malloc(sz); 343 1.2 itojun if (!r) { 344 1.2 itojun perror("xmalloc"); 345 1.2 itojun abort(); 346 1.2 itojun } 347 1.2 itojun return(r); 348 1.2 itojun } 349 1.2 itojun 350 1.2 itojun u_int32_t * 351 1.33 dholland xlalloc(unsigned int sz) 352 1.2 itojun { 353 1.2 itojun u_int32_t *r = (u_int32_t *)malloc(sz * sizeof(u_int32_t)); 354 1.2 itojun if (!r) { 355 1.2 itojun perror("xlalloc"); 356 1.2 itojun abort(); 357 1.2 itojun } 358 1.2 itojun return(r); 359 1.2 itojun } 360 1.2 itojun 361 1.2 itojun u_int32_t * 362 1.33 dholland xrelalloc(u_int32_t *old, unsigned int sz) 363 1.2 itojun { 364 1.2 itojun u_int32_t *r = (u_int32_t *)realloc((char *)old, 365 1.2 itojun sz * sizeof(u_int32_t)); 366 1.2 itojun if (!r) { 367 1.2 itojun perror("xrelalloc"); 368 1.2 itojun abort(); 369 1.2 itojun } 370 1.2 itojun return(r); 371 1.2 itojun } 372 1.2 itojun 373 1.2 itojun void 374 1.33 dholland set_map(rune_map *map, rune_list *list, u_int32_t flag) 375 1.2 itojun { 376 1.2 itojun list->map &= charsetmask; 377 1.2 itojun list->map |= charsetbits; 378 1.2 itojun while (list) { 379 1.2 itojun rune_list *nlist = list->next; 380 1.2 itojun add_map(map, list, flag); 381 1.2 itojun list = nlist; 382 1.2 itojun } 383 1.2 itojun } 384 1.2 itojun 385 1.2 itojun void 386 1.33 dholland set_digitmap(rune_map *map, rune_list *list) 387 1.2 itojun { 388 1.17 tshiozak __nbrune_t i; 389 1.2 itojun 390 1.2 itojun while (list) { 391 1.2 itojun rune_list *nlist = list->next; 392 1.2 itojun for (i = list->min; i <= list->max; ++i) { 393 1.36 rin /* 394 1.36 rin * XXX PR lib/57798 395 1.36 rin * Currently, we support mapping up to 255. Attempts to map 396 1.36 rin * 256 (== _RUNETYPE_A) and above are silently ignored. 397 1.36 rin */ 398 1.36 rin _RuneType digit = list->map + (i - list->min); 399 1.36 rin if (digit > 0 && digit <= 0xff) { 400 1.2 itojun rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list)); 401 1.36 rin memset(tmp, 0, sizeof(*tmp)); 402 1.2 itojun tmp->min = i; 403 1.2 itojun tmp->max = i; 404 1.36 rin add_map(map, tmp, digit); 405 1.2 itojun } 406 1.2 itojun } 407 1.2 itojun free(list); 408 1.2 itojun list = nlist; 409 1.2 itojun } 410 1.2 itojun } 411 1.2 itojun 412 1.2 itojun void 413 1.33 dholland add_map(rune_map *map, rune_list *list, u_int32_t flag) 414 1.2 itojun { 415 1.17 tshiozak __nbrune_t i; 416 1.2 itojun rune_list *lr = 0; 417 1.2 itojun rune_list *r; 418 1.17 tshiozak __nbrune_t run; 419 1.2 itojun 420 1.29 tnozaki while (list->min < _CTYPE_CACHE_SIZE && list->min <= list->max) { 421 1.2 itojun if (flag) 422 1.2 itojun map->map[list->min++] |= flag; 423 1.2 itojun else 424 1.2 itojun map->map[list->min++] = list->map++; 425 1.2 itojun } 426 1.2 itojun 427 1.2 itojun if (list->min > list->max) { 428 1.2 itojun free(list); 429 1.2 itojun return; 430 1.2 itojun } 431 1.2 itojun 432 1.2 itojun run = list->max - list->min + 1; 433 1.2 itojun 434 1.2 itojun if (!(r = map->root) || (list->max < r->min - 1) 435 1.2 itojun || (!flag && list->max == r->min - 1)) { 436 1.2 itojun if (flag) { 437 1.2 itojun list->types = xlalloc(run); 438 1.2 itojun for (i = 0; i < run; ++i) 439 1.2 itojun list->types[i] = flag; 440 1.2 itojun } 441 1.2 itojun list->next = map->root; 442 1.2 itojun map->root = list; 443 1.2 itojun return; 444 1.2 itojun } 445 1.2 itojun 446 1.2 itojun for (r = map->root; r && r->max + 1 < list->min; r = r->next) 447 1.2 itojun lr = r; 448 1.2 itojun 449 1.2 itojun if (!r) { 450 1.2 itojun /* 451 1.2 itojun * We are off the end. 452 1.2 itojun */ 453 1.2 itojun if (flag) { 454 1.2 itojun list->types = xlalloc(run); 455 1.2 itojun for (i = 0; i < run; ++i) 456 1.2 itojun list->types[i] = flag; 457 1.2 itojun } 458 1.2 itojun list->next = 0; 459 1.2 itojun lr->next = list; 460 1.2 itojun return; 461 1.2 itojun } 462 1.2 itojun 463 1.2 itojun if (list->max < r->min - 1) { 464 1.2 itojun /* 465 1.2 itojun * We come before this range and we do not intersect it. 466 1.2 itojun * We are not before the root node, it was checked before the loop 467 1.2 itojun */ 468 1.2 itojun if (flag) { 469 1.2 itojun list->types = xlalloc(run); 470 1.2 itojun for (i = 0; i < run; ++i) 471 1.2 itojun list->types[i] = flag; 472 1.2 itojun } 473 1.2 itojun list->next = lr->next; 474 1.2 itojun lr->next = list; 475 1.2 itojun return; 476 1.2 itojun } 477 1.2 itojun 478 1.2 itojun /* 479 1.2 itojun * At this point we have found that we at least intersect with 480 1.2 itojun * the range pointed to by `r', we might intersect with one or 481 1.2 itojun * more ranges beyond `r' as well. 482 1.2 itojun */ 483 1.2 itojun 484 1.2 itojun if (!flag && list->map - list->min != r->map - r->min) { 485 1.2 itojun /* 486 1.2 itojun * There are only two cases when we are doing case maps and 487 1.2 itojun * our maps needn't have the same offset. When we are adjoining 488 1.2 itojun * but not intersecting. 489 1.2 itojun */ 490 1.2 itojun if (list->max + 1 == r->min) { 491 1.2 itojun lr->next = list; 492 1.2 itojun list->next = r; 493 1.2 itojun return; 494 1.2 itojun } 495 1.2 itojun if (list->min - 1 == r->max) { 496 1.2 itojun list->next = r->next; 497 1.2 itojun r->next = list; 498 1.2 itojun return; 499 1.2 itojun } 500 1.2 itojun fprintf(stderr, "Error: conflicting map entries\n"); 501 1.2 itojun exit(1); 502 1.2 itojun } 503 1.2 itojun 504 1.2 itojun if (list->min >= r->min && list->max <= r->max) { 505 1.2 itojun /* 506 1.2 itojun * Subset case. 507 1.2 itojun */ 508 1.2 itojun 509 1.2 itojun if (flag) { 510 1.2 itojun for (i = list->min; i <= list->max; ++i) 511 1.2 itojun r->types[i - r->min] |= flag; 512 1.2 itojun } 513 1.2 itojun free(list); 514 1.2 itojun return; 515 1.2 itojun } 516 1.2 itojun if (list->min <= r->min && list->max >= r->max) { 517 1.2 itojun /* 518 1.2 itojun * Superset case. Make him big enough to hold us. 519 1.2 itojun * We might need to merge with the guy after him. 520 1.2 itojun */ 521 1.2 itojun if (flag) { 522 1.2 itojun list->types = xlalloc(list->max - list->min + 1); 523 1.2 itojun 524 1.2 itojun for (i = list->min; i <= list->max; ++i) 525 1.2 itojun list->types[i - list->min] = flag; 526 1.2 itojun 527 1.2 itojun for (i = r->min; i <= r->max; ++i) 528 1.2 itojun list->types[i - list->min] |= r->types[i - r->min]; 529 1.2 itojun 530 1.2 itojun free(r->types); 531 1.2 itojun r->types = list->types; 532 1.2 itojun } else { 533 1.2 itojun r->map = list->map; 534 1.2 itojun } 535 1.2 itojun r->min = list->min; 536 1.2 itojun r->max = list->max; 537 1.2 itojun free(list); 538 1.2 itojun } else if (list->min < r->min) { 539 1.2 itojun /* 540 1.2 itojun * Our tail intersects his head. 541 1.2 itojun */ 542 1.2 itojun if (flag) { 543 1.2 itojun list->types = xlalloc(r->max - list->min + 1); 544 1.2 itojun 545 1.2 itojun for (i = r->min; i <= r->max; ++i) 546 1.2 itojun list->types[i - list->min] = r->types[i - r->min]; 547 1.2 itojun 548 1.2 itojun for (i = list->min; i < r->min; ++i) 549 1.2 itojun list->types[i - list->min] = flag; 550 1.2 itojun 551 1.2 itojun for (i = r->min; i <= list->max; ++i) 552 1.2 itojun list->types[i - list->min] |= flag; 553 1.2 itojun 554 1.2 itojun free(r->types); 555 1.2 itojun r->types = list->types; 556 1.2 itojun } else { 557 1.2 itojun r->map = list->map; 558 1.2 itojun } 559 1.2 itojun r->min = list->min; 560 1.2 itojun free(list); 561 1.2 itojun return; 562 1.2 itojun } else { 563 1.2 itojun /* 564 1.2 itojun * Our head intersects his tail. 565 1.2 itojun * We might need to merge with the guy after him. 566 1.2 itojun */ 567 1.2 itojun if (flag) { 568 1.2 itojun r->types = xrelalloc(r->types, list->max - r->min + 1); 569 1.2 itojun 570 1.2 itojun for (i = list->min; i <= r->max; ++i) 571 1.2 itojun r->types[i - r->min] |= flag; 572 1.2 itojun 573 1.2 itojun for (i = r->max+1; i <= list->max; ++i) 574 1.2 itojun r->types[i - r->min] = flag; 575 1.2 itojun } 576 1.2 itojun r->max = list->max; 577 1.2 itojun free(list); 578 1.2 itojun } 579 1.2 itojun 580 1.2 itojun /* 581 1.2 itojun * Okay, check to see if we grew into the next guy(s) 582 1.2 itojun */ 583 1.2 itojun while ((lr = r->next) && r->max >= lr->min) { 584 1.2 itojun if (flag) { 585 1.2 itojun if (r->max >= lr->max) { 586 1.2 itojun /* 587 1.2 itojun * Good, we consumed all of him. 588 1.2 itojun */ 589 1.2 itojun for (i = lr->min; i <= lr->max; ++i) 590 1.2 itojun r->types[i - r->min] |= lr->types[i - lr->min]; 591 1.2 itojun } else { 592 1.2 itojun /* 593 1.2 itojun * "append" him on to the end of us. 594 1.2 itojun */ 595 1.2 itojun r->types = xrelalloc(r->types, lr->max - r->min + 1); 596 1.2 itojun 597 1.2 itojun for (i = lr->min; i <= r->max; ++i) 598 1.2 itojun r->types[i - r->min] |= lr->types[i - lr->min]; 599 1.2 itojun 600 1.2 itojun for (i = r->max+1; i <= lr->max; ++i) 601 1.2 itojun r->types[i - r->min] = lr->types[i - lr->min]; 602 1.2 itojun 603 1.2 itojun r->max = lr->max; 604 1.2 itojun } 605 1.2 itojun } else { 606 1.2 itojun if (lr->max > r->max) 607 1.2 itojun r->max = lr->max; 608 1.2 itojun } 609 1.2 itojun 610 1.2 itojun r->next = lr->next; 611 1.2 itojun 612 1.2 itojun if (flag) 613 1.2 itojun free(lr->types); 614 1.2 itojun free(lr); 615 1.2 itojun } 616 1.2 itojun } 617 1.2 itojun 618 1.2 itojun void 619 1.33 dholland dump_tables(void) 620 1.2 itojun { 621 1.5 thorpej int x, n; 622 1.2 itojun rune_list *list; 623 1.25 tnozaki FILE *fp = ofile; 624 1.30 tnozaki u_int32_t nranges; 625 1.2 itojun 626 1.2 itojun /* 627 1.2 itojun * See if we can compress some of the istype arrays 628 1.2 itojun */ 629 1.2 itojun for(list = types.root; list; list = list->next) { 630 1.2 itojun list->map = list->types[0]; 631 1.2 itojun for (x = 1; x < list->max - list->min + 1; ++x) { 632 1.2 itojun if (list->types[x] != list->map) { 633 1.2 itojun list->map = 0; 634 1.2 itojun break; 635 1.2 itojun } 636 1.2 itojun } 637 1.2 itojun } 638 1.2 itojun 639 1.2 itojun /* 640 1.2 itojun * Fill in our tables. Do this in network order so that 641 1.2 itojun * diverse machines have a chance of sharing data. 642 1.2 itojun * (Machines like Crays cannot share with little machines due to 643 1.2 itojun * word size. Sigh. We tried.) 644 1.2 itojun */ 645 1.29 tnozaki for (x = 0; x < _CTYPE_CACHE_SIZE; ++x) { 646 1.30 tnozaki new_locale.frl_runetype[x] = htonl(types.map[x]); 647 1.30 tnozaki new_locale.frl_maplower[x] = htonl(maplower.map[x]); 648 1.30 tnozaki new_locale.frl_mapupper[x] = htonl(mapupper.map[x]); 649 1.2 itojun } 650 1.2 itojun 651 1.2 itojun /* 652 1.2 itojun * Count up how many ranges we will need for each of the extents. 653 1.2 itojun */ 654 1.2 itojun list = types.root; 655 1.2 itojun 656 1.30 tnozaki nranges = (u_int32_t)0; 657 1.2 itojun while (list) { 658 1.30 tnozaki ++nranges; 659 1.2 itojun list = list->next; 660 1.2 itojun } 661 1.30 tnozaki new_locale.frl_runetype_ext.frr_nranges = 662 1.30 tnozaki htonl(nranges); 663 1.2 itojun 664 1.2 itojun list = maplower.root; 665 1.2 itojun 666 1.30 tnozaki nranges = (u_int32_t)0; 667 1.2 itojun while (list) { 668 1.30 tnozaki ++nranges; 669 1.2 itojun list = list->next; 670 1.2 itojun } 671 1.30 tnozaki new_locale.frl_maplower_ext.frr_nranges = 672 1.30 tnozaki htonl(nranges); 673 1.2 itojun 674 1.2 itojun list = mapupper.root; 675 1.2 itojun 676 1.30 tnozaki nranges = (u_int32_t)0; 677 1.2 itojun while (list) { 678 1.30 tnozaki ++nranges; 679 1.2 itojun list = list->next; 680 1.2 itojun } 681 1.30 tnozaki new_locale.frl_mapupper_ext.frr_nranges = 682 1.30 tnozaki htonl(nranges); 683 1.2 itojun 684 1.2 itojun /* 685 1.2 itojun * Okay, we are now ready to write the new locale file. 686 1.2 itojun */ 687 1.2 itojun 688 1.2 itojun /* 689 1.2 itojun * PART 1: The _RuneLocale structure 690 1.2 itojun */ 691 1.30 tnozaki if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) 692 1.30 tnozaki err(1, "writing _FileRuneLocale to %s", locale_file); 693 1.2 itojun /* 694 1.2 itojun * PART 2: The runetype_ext structures (not the actual tables) 695 1.2 itojun */ 696 1.5 thorpej for (list = types.root, n = 0; list != NULL; list = list->next, n++) { 697 1.2 itojun _FileRuneEntry re; 698 1.2 itojun 699 1.20 tshiozak memset(&re, 0, sizeof(re)); 700 1.9 tshiozak re.fre_min = htonl(list->min); 701 1.9 tshiozak re.fre_max = htonl(list->max); 702 1.9 tshiozak re.fre_map = htonl(list->map); 703 1.2 itojun 704 1.5 thorpej if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) 705 1.5 thorpej err(1, "writing runetype_ext #%d to %s", n, locale_file); 706 1.2 itojun } 707 1.2 itojun /* 708 1.2 itojun * PART 3: The maplower_ext structures 709 1.2 itojun */ 710 1.5 thorpej for (list = maplower.root, n = 0; list != NULL; list = list->next, n++) { 711 1.2 itojun _FileRuneEntry re; 712 1.2 itojun 713 1.20 tshiozak memset(&re, 0, sizeof(re)); 714 1.9 tshiozak re.fre_min = htonl(list->min); 715 1.9 tshiozak re.fre_max = htonl(list->max); 716 1.9 tshiozak re.fre_map = htonl(list->map); 717 1.2 itojun 718 1.5 thorpej if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) 719 1.5 thorpej err(1, "writing maplower_ext #%d to %s", n, locale_file); 720 1.2 itojun } 721 1.2 itojun /* 722 1.2 itojun * PART 4: The mapupper_ext structures 723 1.2 itojun */ 724 1.5 thorpej for (list = mapupper.root, n = 0; list != NULL; list = list->next, n++) { 725 1.2 itojun _FileRuneEntry re; 726 1.2 itojun 727 1.20 tshiozak memset(&re, 0, sizeof(re)); 728 1.9 tshiozak re.fre_min = htonl(list->min); 729 1.9 tshiozak re.fre_max = htonl(list->max); 730 1.9 tshiozak re.fre_map = htonl(list->map); 731 1.2 itojun 732 1.5 thorpej if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) 733 1.5 thorpej err(1, "writing mapupper_ext #%d to %s", n, locale_file); 734 1.2 itojun } 735 1.2 itojun /* 736 1.2 itojun * PART 5: The runetype_ext tables 737 1.2 itojun */ 738 1.5 thorpej for (list = types.root, n = 0; list != NULL; list = list->next, n++) { 739 1.2 itojun for (x = 0; x < list->max - list->min + 1; ++x) 740 1.2 itojun list->types[x] = htonl(list->types[x]); 741 1.2 itojun 742 1.2 itojun if (!list->map) { 743 1.2 itojun if (fwrite((char *)list->types, 744 1.2 itojun (list->max - list->min + 1) * sizeof(u_int32_t), 745 1.5 thorpej 1, fp) != 1) 746 1.5 thorpej err(1, "writing runetype_ext table #%d to %s", n, locale_file); 747 1.2 itojun } 748 1.2 itojun } 749 1.2 itojun /* 750 1.2 itojun * PART 5: And finally the variable data 751 1.2 itojun */ 752 1.30 tnozaki if (rl_variable_len != 0 && 753 1.30 tnozaki fwrite((char *)rl_variable, rl_variable_len, 1, fp) != 1) 754 1.5 thorpej err(1, "writing variable data to %s", locale_file); 755 1.2 itojun fclose(fp); 756 1.2 itojun 757 1.2 itojun if (!debug) 758 1.2 itojun return; 759 1.2 itojun 760 1.30 tnozaki if (new_locale.frl_encoding[0]) 761 1.30 tnozaki fprintf(stderr, "ENCODING %.*s\n", 762 1.30 tnozaki (int)sizeof(new_locale.frl_encoding), new_locale.frl_encoding); 763 1.30 tnozaki if (rl_variable) 764 1.30 tnozaki fprintf(stderr, "VARIABLE %.*s\n", 765 1.30 tnozaki (int)rl_variable_len, (char *)rl_variable); 766 1.2 itojun 767 1.2 itojun fprintf(stderr, "\nMAPLOWER:\n\n"); 768 1.2 itojun 769 1.29 tnozaki for (x = 0; x < _CTYPE_CACHE_SIZE; ++x) { 770 1.2 itojun if (isprint(maplower.map[x])) 771 1.2 itojun fprintf(stderr, " '%c'", (int)maplower.map[x]); 772 1.2 itojun else if (maplower.map[x]) 773 1.2 itojun fprintf(stderr, "%04x", maplower.map[x]); 774 1.2 itojun else 775 1.2 itojun fprintf(stderr, "%4x", 0); 776 1.2 itojun if ((x & 0xf) == 0xf) 777 1.2 itojun fprintf(stderr, "\n"); 778 1.2 itojun else 779 1.2 itojun fprintf(stderr, " "); 780 1.2 itojun } 781 1.2 itojun fprintf(stderr, "\n"); 782 1.2 itojun 783 1.2 itojun for (list = maplower.root; list; list = list->next) 784 1.2 itojun fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map); 785 1.2 itojun 786 1.2 itojun fprintf(stderr, "\nMAPUPPER:\n\n"); 787 1.2 itojun 788 1.29 tnozaki for (x = 0; x < _CTYPE_CACHE_SIZE; ++x) { 789 1.2 itojun if (isprint(mapupper.map[x])) 790 1.2 itojun fprintf(stderr, " '%c'", (int)mapupper.map[x]); 791 1.2 itojun else if (mapupper.map[x]) 792 1.2 itojun fprintf(stderr, "%04x", mapupper.map[x]); 793 1.2 itojun else 794 1.2 itojun fprintf(stderr, "%4x", 0); 795 1.2 itojun if ((x & 0xf) == 0xf) 796 1.2 itojun fprintf(stderr, "\n"); 797 1.2 itojun else 798 1.2 itojun fprintf(stderr, " "); 799 1.2 itojun } 800 1.2 itojun fprintf(stderr, "\n"); 801 1.2 itojun 802 1.2 itojun for (list = mapupper.root; list; list = list->next) 803 1.2 itojun fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map); 804 1.2 itojun 805 1.2 itojun 806 1.2 itojun fprintf(stderr, "\nTYPES:\n\n"); 807 1.2 itojun 808 1.29 tnozaki for (x = 0; x < _CTYPE_CACHE_SIZE; ++x) { 809 1.2 itojun u_int32_t r = types.map[x]; 810 1.2 itojun 811 1.2 itojun if (r) { 812 1.2 itojun if (isprint(x)) 813 1.2 itojun fprintf(stderr, " '%c':%2d", x, (int)(r & 0xff)); 814 1.2 itojun else 815 1.2 itojun fprintf(stderr, "%04x:%2d", x, (int)(r & 0xff)); 816 1.2 itojun 817 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : ""); 818 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : ""); 819 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : ""); 820 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : ""); 821 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : ""); 822 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : ""); 823 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : ""); 824 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : ""); 825 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : ""); 826 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : ""); 827 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : ""); 828 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : ""); 829 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : ""); 830 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : ""); 831 1.2 itojun fprintf(stderr, "\n"); 832 1.2 itojun } 833 1.2 itojun } 834 1.2 itojun 835 1.2 itojun for (list = types.root; list; list = list->next) { 836 1.2 itojun if (list->map && list->min + 3 < list->max) { 837 1.2 itojun u_int32_t r = list->map; 838 1.2 itojun 839 1.2 itojun fprintf(stderr, "%04x:%2d", list->min, r & 0xff); 840 1.2 itojun 841 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : ""); 842 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : ""); 843 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : ""); 844 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : ""); 845 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : ""); 846 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : ""); 847 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : ""); 848 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : ""); 849 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : ""); 850 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : ""); 851 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : ""); 852 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : ""); 853 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : ""); 854 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : ""); 855 1.2 itojun fprintf(stderr, "\n...\n"); 856 1.2 itojun 857 1.2 itojun fprintf(stderr, "%04x:%2d", list->max, r & 0xff); 858 1.2 itojun 859 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : ""); 860 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : ""); 861 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : ""); 862 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : ""); 863 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : ""); 864 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : ""); 865 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : ""); 866 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : ""); 867 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : ""); 868 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : ""); 869 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : ""); 870 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : ""); 871 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : ""); 872 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : ""); 873 1.16 tshiozak fprintf(stderr, " %1u", (unsigned)((r & _RUNETYPE_SWM)>>_RUNETYPE_SWS)); 874 1.2 itojun fprintf(stderr, "\n"); 875 1.2 itojun } else 876 1.2 itojun for (x = list->min; x <= list->max; ++x) { 877 1.2 itojun u_int32_t r = ntohl(list->types[x - list->min]); 878 1.2 itojun 879 1.2 itojun if (r) { 880 1.2 itojun fprintf(stderr, "%04x:%2d", x, (int)(r & 0xff)); 881 1.2 itojun 882 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : ""); 883 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : ""); 884 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : ""); 885 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : ""); 886 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : ""); 887 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : ""); 888 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : ""); 889 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : ""); 890 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : ""); 891 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : ""); 892 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : ""); 893 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : ""); 894 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : ""); 895 1.16 tshiozak fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : ""); 896 1.16 tshiozak fprintf(stderr, " %1u", (unsigned)((r & _RUNETYPE_SWM)>>_RUNETYPE_SWS)); 897 1.2 itojun fprintf(stderr, "\n"); 898 1.2 itojun } 899 1.2 itojun } 900 1.2 itojun } 901 1.2 itojun } 902