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