yacc.y revision 81606
11590Srgrimes%{
21590Srgrimes/*-
31590Srgrimes * Copyright (c) 1993
41590Srgrimes *	The Regents of the University of California.  All rights reserved.
51590Srgrimes *
61590Srgrimes * This code is derived from software contributed to Berkeley by
71590Srgrimes * Paul Borman at Krystal Technologies.
81590Srgrimes *
91590Srgrimes * Redistribution and use in source and binary forms, with or without
101590Srgrimes * modification, are permitted provided that the following conditions
111590Srgrimes * are met:
121590Srgrimes * 1. Redistributions of source code must retain the above copyright
131590Srgrimes *    notice, this list of conditions and the following disclaimer.
141590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151590Srgrimes *    notice, this list of conditions and the following disclaimer in the
161590Srgrimes *    documentation and/or other materials provided with the distribution.
171590Srgrimes * 3. All advertising materials mentioning features or use of this software
181590Srgrimes *    must display the following acknowledgement:
191590Srgrimes *	This product includes software developed by the University of
201590Srgrimes *	California, Berkeley and its contributors.
211590Srgrimes * 4. Neither the name of the University nor the names of its contributors
221590Srgrimes *    may be used to endorse or promote products derived from this software
231590Srgrimes *    without specific prior written permission.
241590Srgrimes *
251590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
261590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
271590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
281590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
291590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
301590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
311590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
321590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
331590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
341590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
351590Srgrimes * SUCH DAMAGE.
361590Srgrimes */
371590Srgrimes
381590Srgrimes#ifndef lint
391590Srgrimesstatic char sccsid[] = "@(#)yacc.y	8.1 (Berkeley) 6/6/93";
4057035Sobrienstatic char rcsid[] = "$FreeBSD: head/usr.bin/mklocale/yacc.y 81606 2001-08-13 21:57:12Z peter $";
411590Srgrimes#endif /* not lint */
421590Srgrimes
431590Srgrimes#include <ctype.h>
441590Srgrimes#include <rune.h>
451590Srgrimes#include <stddef.h>
461590Srgrimes#include <stdio.h>
471590Srgrimes#include <stdlib.h>
4833648Sjb#include <string.h>
491590Srgrimes
501590Srgrimes#include "ldef.h"
511590Srgrimes
521590Srgrimeschar	*locale_file = "<stdout>";
531590Srgrimes
541590Srgrimesrune_map	maplower = { 0, };
551590Srgrimesrune_map	mapupper = { 0, };
561590Srgrimesrune_map	types = { 0, };
571590Srgrimes
581590Srgrimes_RuneLocale	new_locale = { 0, };
591590Srgrimes
602509Sbdevoid set_map __P((rune_map *, rune_list *, unsigned long));
611590Srgrimesvoid set_digitmap __P((rune_map *, rune_list *));
622509Sbdevoid add_map __P((rune_map *, rune_list *, unsigned long));
631590Srgrimes%}
641590Srgrimes
651590Srgrimes%union	{
661590Srgrimes    rune_t	rune;
671590Srgrimes    int		i;
681590Srgrimes    char	*str;
691590Srgrimes
701590Srgrimes    rune_list	*list;
711590Srgrimes}
721590Srgrimes
731590Srgrimes%token	<rune>	RUNE
741590Srgrimes%token		LBRK
751590Srgrimes%token		RBRK
761590Srgrimes%token		THRU
771590Srgrimes%token		MAPLOWER
781590Srgrimes%token		MAPUPPER
791590Srgrimes%token		DIGITMAP
801590Srgrimes%token	<i>	LIST
811590Srgrimes%token	<str>	VARIABLE
821590Srgrimes%token		ENCODING
831590Srgrimes%token		INVALID
841590Srgrimes%token	<str>	STRING
851590Srgrimes
861590Srgrimes%type	<list>	list
871590Srgrimes%type	<list>	map
881590Srgrimes
891590Srgrimes
901590Srgrimes%%
911590Srgrimes
921590Srgrimeslocale	:	/* empty */
931590Srgrimes	|	table
941590Srgrimes	    	{ dump_tables(); }
951590Srgrimes	;
961590Srgrimes
971590Srgrimestable	:	entry
981590Srgrimes	|	table entry
991590Srgrimes	;
1001590Srgrimes
1011590Srgrimesentry	:	ENCODING STRING
1021590Srgrimes		{ strncpy(new_locale.encoding, $2, sizeof(new_locale.encoding)); }
1031590Srgrimes	|	VARIABLE
1041590Srgrimes		{ new_locale.variable_len = strlen($1) + 1;
1051590Srgrimes		  new_locale.variable = malloc(new_locale.variable_len);
1061590Srgrimes		  strcpy((char *)new_locale.variable, $1);
1071590Srgrimes		}
1081590Srgrimes	|	INVALID RUNE
1091590Srgrimes		{ new_locale.invalid_rune = $2; }
1101590Srgrimes	|	LIST list
1111590Srgrimes		{ set_map(&types, $2, $1); }
1121590Srgrimes	|	MAPLOWER map
1131590Srgrimes		{ set_map(&maplower, $2, 0); }
1141590Srgrimes	|	MAPUPPER map
1151590Srgrimes		{ set_map(&mapupper, $2, 0); }
1161590Srgrimes	|	DIGITMAP map
1171590Srgrimes		{ set_digitmap(&types, $2); }
1181590Srgrimes	;
1191590Srgrimes
1201590Srgrimeslist	:	RUNE
1211590Srgrimes		{
1221590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1231590Srgrimes		    $$->min = $1;
1241590Srgrimes		    $$->max = $1;
1251590Srgrimes		    $$->next = 0;
1261590Srgrimes		}
1271590Srgrimes	|	RUNE THRU RUNE
1281590Srgrimes		{
1291590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1301590Srgrimes		    $$->min = $1;
1311590Srgrimes		    $$->max = $3;
1321590Srgrimes		    $$->next = 0;
1331590Srgrimes		}
1341590Srgrimes	|	list RUNE
1351590Srgrimes		{
1361590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1371590Srgrimes		    $$->min = $2;
1381590Srgrimes		    $$->max = $2;
1391590Srgrimes		    $$->next = $1;
1401590Srgrimes		}
1411590Srgrimes	|	list RUNE THRU RUNE
1421590Srgrimes		{
1431590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1441590Srgrimes		    $$->min = $2;
1451590Srgrimes		    $$->max = $4;
1461590Srgrimes		    $$->next = $1;
1471590Srgrimes		}
1481590Srgrimes	;
1491590Srgrimes
1501590Srgrimesmap	:	LBRK RUNE RUNE RBRK
1511590Srgrimes		{
1521590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1531590Srgrimes		    $$->min = $2;
1541590Srgrimes		    $$->max = $2;
1551590Srgrimes		    $$->map = $3;
1561590Srgrimes		    $$->next = 0;
1571590Srgrimes		}
1581590Srgrimes	|	map LBRK RUNE RUNE RBRK
1591590Srgrimes		{
1601590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1611590Srgrimes		    $$->min = $3;
1621590Srgrimes		    $$->max = $3;
1631590Srgrimes		    $$->map = $4;
1641590Srgrimes		    $$->next = $1;
1651590Srgrimes		}
1661590Srgrimes	|	LBRK RUNE THRU RUNE ':' RUNE RBRK
1671590Srgrimes		{
1681590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1691590Srgrimes		    $$->min = $2;
1701590Srgrimes		    $$->max = $4;
1711590Srgrimes		    $$->map = $6;
1721590Srgrimes		    $$->next = 0;
1731590Srgrimes		}
1741590Srgrimes	|	map LBRK RUNE THRU RUNE ':' RUNE RBRK
1751590Srgrimes		{
1761590Srgrimes		    $$ = (rune_list *)malloc(sizeof(rune_list));
1771590Srgrimes		    $$->min = $3;
1781590Srgrimes		    $$->max = $5;
1791590Srgrimes		    $$->map = $7;
1801590Srgrimes		    $$->next = $1;
1811590Srgrimes		}
1821590Srgrimes	;
1831590Srgrimes%%
1841590Srgrimes
1851590Srgrimesint debug = 0;
18681606SpeterFILE *fp;
1871590Srgrimes
1881590Srgrimesmain(ac, av)
1891590Srgrimes	int ac;
1901590Srgrimes	char *av[];
1911590Srgrimes{
1921590Srgrimes    int x;
1931590Srgrimes
1941590Srgrimes    extern char *optarg;
1951590Srgrimes    extern int optind;
19681606Speter    fp = stdout;
1971590Srgrimes
1981590Srgrimes    while ((x = getopt(ac, av, "do:")) != EOF) {
1991590Srgrimes	switch(x) {
2001590Srgrimes	case 'd':
2011590Srgrimes	    debug = 1;
2021590Srgrimes	    break;
2031590Srgrimes	case 'o':
2041590Srgrimes	    locale_file = optarg;
2051590Srgrimes	    if ((fp = fopen(locale_file, "w")) == 0) {
2061590Srgrimes		perror(locale_file);
2071590Srgrimes		exit(1);
2081590Srgrimes	    }
2091590Srgrimes	    break;
2101590Srgrimes	default:
2111590Srgrimes	usage:
2121590Srgrimes	    fprintf(stderr, "Usage: mklocale [-d] [-o output] [source]\n");
2131590Srgrimes	    exit(1);
2141590Srgrimes	}
2151590Srgrimes    }
2161590Srgrimes
2171590Srgrimes    switch (ac - optind) {
2181590Srgrimes    case 0:
2191590Srgrimes	break;
2201590Srgrimes    case 1:
2211590Srgrimes	if (freopen(av[optind], "r", stdin) == 0) {
2221590Srgrimes	    perror(av[optind]);
2231590Srgrimes	    exit(1);
2241590Srgrimes	}
2251590Srgrimes	break;
2261590Srgrimes    default:
2271590Srgrimes	goto usage;
2281590Srgrimes    }
2291590Srgrimes    for (x = 0; x < _CACHED_RUNES; ++x) {
2301590Srgrimes	mapupper.map[x] = x;
2311590Srgrimes	maplower.map[x] = x;
2321590Srgrimes    }
2331590Srgrimes    new_locale.invalid_rune = _INVALID_RUNE;
2341590Srgrimes    memcpy(new_locale.magic, _RUNE_MAGIC_1, sizeof(new_locale.magic));
2351590Srgrimes
2361590Srgrimes    yyparse();
2371590Srgrimes}
2381590Srgrimes
2391590Srgrimesyyerror(s)
2401590Srgrimes	char *s;
2411590Srgrimes{
2421590Srgrimes    fprintf(stderr, "%s\n", s);
2431590Srgrimes}
2441590Srgrimes
2451590Srgrimesvoid *
2461590Srgrimesxmalloc(sz)
2471590Srgrimes	unsigned int sz;
2481590Srgrimes{
2491590Srgrimes    void *r = malloc(sz);
2501590Srgrimes    if (!r) {
2511590Srgrimes	perror("xmalloc");
2521590Srgrimes	abort();
2531590Srgrimes    }
2541590Srgrimes    return(r);
2551590Srgrimes}
2561590Srgrimes
2572509Sbdeunsigned long *
2581590Srgrimesxlalloc(sz)
2591590Srgrimes	unsigned int sz;
2601590Srgrimes{
2612509Sbde    unsigned long *r = (unsigned long *)malloc(sz * sizeof(unsigned long));
2621590Srgrimes    if (!r) {
2631590Srgrimes	perror("xlalloc");
2641590Srgrimes	abort();
2651590Srgrimes    }
2661590Srgrimes    return(r);
2671590Srgrimes}
2681590Srgrimes
2692509Sbdeunsigned long *
2701590Srgrimesxrelalloc(old, sz)
2712509Sbde	unsigned long *old;
2721590Srgrimes	unsigned int sz;
2731590Srgrimes{
2742509Sbde    unsigned long *r = (unsigned long *)realloc((char *)old,
2752509Sbde						sz * sizeof(unsigned long));
2761590Srgrimes    if (!r) {
2771590Srgrimes	perror("xrelalloc");
2781590Srgrimes	abort();
2791590Srgrimes    }
2801590Srgrimes    return(r);
2811590Srgrimes}
2821590Srgrimes
2831590Srgrimesvoid
2841590Srgrimesset_map(map, list, flag)
2851590Srgrimes	rune_map *map;
2861590Srgrimes	rune_list *list;
2872509Sbde	unsigned long flag;
2881590Srgrimes{
2891590Srgrimes    while (list) {
2901590Srgrimes	rune_list *nlist = list->next;
2911590Srgrimes	add_map(map, list, flag);
2921590Srgrimes	list = nlist;
2931590Srgrimes    }
2941590Srgrimes}
2951590Srgrimes
2961590Srgrimesvoid
2971590Srgrimesset_digitmap(map, list)
2981590Srgrimes	rune_map *map;
2991590Srgrimes	rune_list *list;
3001590Srgrimes{
3011590Srgrimes    rune_t i;
3021590Srgrimes
3031590Srgrimes    while (list) {
3041590Srgrimes	rune_list *nlist = list->next;
3051590Srgrimes	for (i = list->min; i <= list->max; ++i) {
3061590Srgrimes	    if (list->map + (i - list->min)) {
3071590Srgrimes		rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
3081590Srgrimes		tmp->min = i;
3091590Srgrimes		tmp->max = i;
3101590Srgrimes		add_map(map, tmp, list->map + (i - list->min));
3111590Srgrimes	    }
3121590Srgrimes	}
3131590Srgrimes	free(list);
3141590Srgrimes	list = nlist;
3151590Srgrimes    }
3161590Srgrimes}
3171590Srgrimes
3181590Srgrimesvoid
3191590Srgrimesadd_map(map, list, flag)
3201590Srgrimes	rune_map *map;
3211590Srgrimes	rune_list *list;
3222509Sbde	unsigned long flag;
3231590Srgrimes{
3241590Srgrimes    rune_t i;
3251590Srgrimes    rune_list *lr = 0;
3261590Srgrimes    rune_list *r;
3271590Srgrimes    rune_t run;
3281590Srgrimes
3291590Srgrimes    while (list->min < _CACHED_RUNES && list->min <= list->max) {
3301590Srgrimes	if (flag)
3311590Srgrimes	    map->map[list->min++] |= flag;
3321590Srgrimes	else
3331590Srgrimes	    map->map[list->min++] = list->map++;
3341590Srgrimes    }
3351590Srgrimes
3361590Srgrimes    if (list->min > list->max) {
3371590Srgrimes	free(list);
3381590Srgrimes	return;
3391590Srgrimes    }
3401590Srgrimes
3411590Srgrimes    run = list->max - list->min + 1;
3421590Srgrimes
3431590Srgrimes    if (!(r = map->root) || (list->max < r->min - 1)
3441590Srgrimes			 || (!flag && list->max == r->min - 1)) {
3451590Srgrimes	if (flag) {
3461590Srgrimes	    list->types = xlalloc(run);
3471590Srgrimes	    for (i = 0; i < run; ++i)
3481590Srgrimes		list->types[i] = flag;
3491590Srgrimes	}
3501590Srgrimes	list->next = map->root;
3511590Srgrimes	map->root = list;
3521590Srgrimes	return;
3531590Srgrimes    }
3541590Srgrimes
3551590Srgrimes    for (r = map->root; r && r->max + 1 < list->min; r = r->next)
3561590Srgrimes	lr = r;
3571590Srgrimes
3581590Srgrimes    if (!r) {
3591590Srgrimes	/*
3601590Srgrimes	 * We are off the end.
3611590Srgrimes	 */
3621590Srgrimes	if (flag) {
3631590Srgrimes	    list->types = xlalloc(run);
3641590Srgrimes	    for (i = 0; i < run; ++i)
3651590Srgrimes		list->types[i] = flag;
3661590Srgrimes	}
3671590Srgrimes	list->next = 0;
3681590Srgrimes	lr->next = list;
3691590Srgrimes	return;
3701590Srgrimes    }
3711590Srgrimes
3721590Srgrimes    if (list->max < r->min - 1) {
3731590Srgrimes	/*
3741590Srgrimes	 * We come before this range and we do not intersect it.
3751590Srgrimes	 * We are not before the root node, it was checked before the loop
3761590Srgrimes	 */
3771590Srgrimes	if (flag) {
3781590Srgrimes	    list->types = xlalloc(run);
3791590Srgrimes	    for (i = 0; i < run; ++i)
3801590Srgrimes		list->types[i] = flag;
3811590Srgrimes	}
3821590Srgrimes	list->next = lr->next;
3831590Srgrimes	lr->next = list;
3841590Srgrimes	return;
3851590Srgrimes    }
3861590Srgrimes
3871590Srgrimes    /*
3881590Srgrimes     * At this point we have found that we at least intersect with
3891590Srgrimes     * the range pointed to by `r', we might intersect with one or
3901590Srgrimes     * more ranges beyond `r' as well.
3911590Srgrimes     */
3921590Srgrimes
3931590Srgrimes    if (!flag && list->map - list->min != r->map - r->min) {
3941590Srgrimes	/*
3951590Srgrimes	 * There are only two cases when we are doing case maps and
3961590Srgrimes	 * our maps needn't have the same offset.  When we are adjoining
3971590Srgrimes	 * but not intersecting.
3981590Srgrimes	 */
3991590Srgrimes	if (list->max + 1 == r->min) {
4001590Srgrimes	    lr->next = list;
4011590Srgrimes	    list->next = r;
4021590Srgrimes	    return;
4031590Srgrimes	}
4041590Srgrimes	if (list->min - 1 == r->max) {
4051590Srgrimes	    list->next = r->next;
4061590Srgrimes	    r->next = list;
4071590Srgrimes	    return;
4081590Srgrimes	}
4091590Srgrimes	fprintf(stderr, "Error: conflicting map entries\n");
4101590Srgrimes	exit(1);
4111590Srgrimes    }
4121590Srgrimes
4131590Srgrimes    if (list->min >= r->min && list->max <= r->max) {
4141590Srgrimes	/*
4151590Srgrimes	 * Subset case.
4161590Srgrimes	 */
4171590Srgrimes
4181590Srgrimes	if (flag) {
4191590Srgrimes	    for (i = list->min; i <= list->max; ++i)
4201590Srgrimes		r->types[i - r->min] |= flag;
4211590Srgrimes	}
4221590Srgrimes	free(list);
4231590Srgrimes	return;
4241590Srgrimes    }
4251590Srgrimes    if (list->min <= r->min && list->max >= r->max) {
4261590Srgrimes	/*
4271590Srgrimes	 * Superset case.  Make him big enough to hold us.
4281590Srgrimes	 * We might need to merge with the guy after him.
4291590Srgrimes	 */
4301590Srgrimes	if (flag) {
4311590Srgrimes	    list->types = xlalloc(list->max - list->min + 1);
4321590Srgrimes
4331590Srgrimes	    for (i = list->min; i <= list->max; ++i)
4341590Srgrimes		list->types[i - list->min] = flag;
4351590Srgrimes
4361590Srgrimes	    for (i = r->min; i <= r->max; ++i)
4371590Srgrimes		list->types[i - list->min] |= r->types[i - r->min];
4381590Srgrimes
4391590Srgrimes	    free(r->types);
4401590Srgrimes	    r->types = list->types;
4411590Srgrimes	} else {
4421590Srgrimes	    r->map = list->map;
4431590Srgrimes	}
4441590Srgrimes	r->min = list->min;
4451590Srgrimes	r->max = list->max;
4461590Srgrimes	free(list);
4471590Srgrimes    } else if (list->min < r->min) {
4481590Srgrimes	/*
4491590Srgrimes	 * Our tail intersects his head.
4501590Srgrimes	 */
4511590Srgrimes	if (flag) {
4521590Srgrimes	    list->types = xlalloc(r->max - list->min + 1);
4531590Srgrimes
4541590Srgrimes	    for (i = r->min; i <= r->max; ++i)
4551590Srgrimes		list->types[i - list->min] = r->types[i - r->min];
4561590Srgrimes
4571590Srgrimes	    for (i = list->min; i < r->min; ++i)
4581590Srgrimes		list->types[i - list->min] = flag;
4591590Srgrimes
4601590Srgrimes	    for (i = r->min; i <= list->max; ++i)
4611590Srgrimes		list->types[i - list->min] |= flag;
4621590Srgrimes
4631590Srgrimes	    free(r->types);
4641590Srgrimes	    r->types = list->types;
4651590Srgrimes	} else {
4661590Srgrimes	    r->map = list->map;
4671590Srgrimes	}
4681590Srgrimes	r->min = list->min;
4691590Srgrimes	free(list);
4701590Srgrimes	return;
4711590Srgrimes    } else {
4721590Srgrimes	/*
4731590Srgrimes	 * Our head intersects his tail.
4741590Srgrimes	 * We might need to merge with the guy after him.
4751590Srgrimes	 */
4761590Srgrimes	if (flag) {
4771590Srgrimes	    r->types = xrelalloc(r->types, list->max - r->min + 1);
4781590Srgrimes
4791590Srgrimes	    for (i = list->min; i <= r->max; ++i)
4801590Srgrimes		r->types[i - r->min] |= flag;
4811590Srgrimes
4821590Srgrimes	    for (i = r->max+1; i <= list->max; ++i)
4831590Srgrimes		r->types[i - r->min] = flag;
4841590Srgrimes	}
48521393Sache	r->max = list->max;
4861590Srgrimes	free(list);
4871590Srgrimes    }
4881590Srgrimes
4891590Srgrimes    /*
4901590Srgrimes     * Okay, check to see if we grew into the next guy(s)
4911590Srgrimes     */
4921590Srgrimes    while ((lr = r->next) && r->max >= lr->min) {
4931590Srgrimes	if (flag) {
4941590Srgrimes	    if (r->max >= lr->max) {
4951590Srgrimes		/*
4961590Srgrimes		 * Good, we consumed all of him.
4971590Srgrimes		 */
4981590Srgrimes		for (i = lr->min; i <= lr->max; ++i)
4991590Srgrimes		    r->types[i - r->min] |= lr->types[i - lr->min];
5001590Srgrimes	    } else {
5011590Srgrimes		/*
5021590Srgrimes		 * "append" him on to the end of us.
5031590Srgrimes		 */
5041590Srgrimes		r->types = xrelalloc(r->types, lr->max - r->min + 1);
5051590Srgrimes
5061590Srgrimes		for (i = lr->min; i <= r->max; ++i)
5071590Srgrimes		    r->types[i - r->min] |= lr->types[i - lr->min];
5081590Srgrimes
5091590Srgrimes		for (i = r->max+1; i <= lr->max; ++i)
5101590Srgrimes		    r->types[i - r->min] = lr->types[i - lr->min];
5111590Srgrimes
5121590Srgrimes		r->max = lr->max;
5131590Srgrimes	    }
5141590Srgrimes	} else {
5151590Srgrimes	    if (lr->max > r->max)
5161590Srgrimes		r->max = lr->max;
5171590Srgrimes	}
5181590Srgrimes
5191590Srgrimes	r->next = lr->next;
5201590Srgrimes
5211590Srgrimes	if (flag)
5221590Srgrimes	    free(lr->types);
5231590Srgrimes	free(lr);
5241590Srgrimes    }
5251590Srgrimes}
5261590Srgrimes
5271590Srgrimesvoid
5281590Srgrimesdump_tables()
5291590Srgrimes{
5301590Srgrimes    int x;
5311590Srgrimes    rune_list *list;
5321590Srgrimes
5331590Srgrimes    /*
5341590Srgrimes     * See if we can compress some of the istype arrays
5351590Srgrimes     */
5361590Srgrimes    for(list = types.root; list; list = list->next) {
5371590Srgrimes	list->map = list->types[0];
5381590Srgrimes	for (x = 1; x < list->max - list->min + 1; ++x) {
5391590Srgrimes	    if (list->types[x] != list->map) {
5401590Srgrimes		list->map = 0;
5411590Srgrimes		break;
5421590Srgrimes	    }
5431590Srgrimes	}
5441590Srgrimes    }
5451590Srgrimes
5461590Srgrimes    new_locale.invalid_rune = htonl(new_locale.invalid_rune);
5471590Srgrimes
5481590Srgrimes    /*
5491590Srgrimes     * Fill in our tables.  Do this in network order so that
5501590Srgrimes     * diverse machines have a chance of sharing data.
5511590Srgrimes     * (Machines like Crays cannot share with little machines due to
5521590Srgrimes     *  word size.  Sigh.  We tried.)
5531590Srgrimes     */
5541590Srgrimes    for (x = 0; x < _CACHED_RUNES; ++x) {
5551590Srgrimes	new_locale.runetype[x] = htonl(types.map[x]);
5561590Srgrimes	new_locale.maplower[x] = htonl(maplower.map[x]);
5571590Srgrimes	new_locale.mapupper[x] = htonl(mapupper.map[x]);
5581590Srgrimes    }
5591590Srgrimes
5601590Srgrimes    /*
5611590Srgrimes     * Count up how many ranges we will need for each of the extents.
5621590Srgrimes     */
5631590Srgrimes    list = types.root;
5641590Srgrimes
5651590Srgrimes    while (list) {
5661590Srgrimes	new_locale.runetype_ext.nranges++;
5671590Srgrimes	list = list->next;
5681590Srgrimes    }
5691590Srgrimes    new_locale.runetype_ext.nranges = htonl(new_locale.runetype_ext.nranges);
5701590Srgrimes
5711590Srgrimes    list = maplower.root;
5721590Srgrimes
5731590Srgrimes    while (list) {
5741590Srgrimes	new_locale.maplower_ext.nranges++;
5751590Srgrimes	list = list->next;
5761590Srgrimes    }
5771590Srgrimes    new_locale.maplower_ext.nranges = htonl(new_locale.maplower_ext.nranges);
5781590Srgrimes
5791590Srgrimes    list = mapupper.root;
5801590Srgrimes
5811590Srgrimes    while (list) {
5821590Srgrimes	new_locale.mapupper_ext.nranges++;
5831590Srgrimes	list = list->next;
5841590Srgrimes    }
5851590Srgrimes    new_locale.mapupper_ext.nranges = htonl(new_locale.mapupper_ext.nranges);
5861590Srgrimes
5871590Srgrimes    new_locale.variable_len = htonl(new_locale.variable_len);
5881590Srgrimes
5891590Srgrimes    /*
5901590Srgrimes     * Okay, we are now ready to write the new locale file.
5911590Srgrimes     */
5921590Srgrimes
5931590Srgrimes    /*
5941590Srgrimes     * PART 1: The _RuneLocale structure
5951590Srgrimes     */
5961590Srgrimes    if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
5971590Srgrimes	perror(locale_file);
5981590Srgrimes	exit(1);
5991590Srgrimes    }
6001590Srgrimes    /*
6011590Srgrimes     * PART 2: The runetype_ext structures (not the actual tables)
6021590Srgrimes     */
6031590Srgrimes    list = types.root;
6041590Srgrimes
6051590Srgrimes    while (list) {
6061590Srgrimes	_RuneEntry re;
6071590Srgrimes
6081590Srgrimes	re.min = htonl(list->min);
6091590Srgrimes	re.max = htonl(list->max);
6101590Srgrimes	re.map = htonl(list->map);
6111590Srgrimes
6121590Srgrimes	if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
6131590Srgrimes	    perror(locale_file);
6141590Srgrimes	    exit(1);
6151590Srgrimes	}
6161590Srgrimes
6171590Srgrimes        list = list->next;
6181590Srgrimes    }
6191590Srgrimes    /*
6201590Srgrimes     * PART 3: The maplower_ext structures
6211590Srgrimes     */
6221590Srgrimes    list = maplower.root;
6231590Srgrimes
6241590Srgrimes    while (list) {
6251590Srgrimes	_RuneEntry re;
6261590Srgrimes
6271590Srgrimes	re.min = htonl(list->min);
6281590Srgrimes	re.max = htonl(list->max);
6291590Srgrimes	re.map = htonl(list->map);
6301590Srgrimes
6311590Srgrimes	if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
6321590Srgrimes	    perror(locale_file);
6331590Srgrimes	    exit(1);
6341590Srgrimes	}
6351590Srgrimes
6361590Srgrimes        list = list->next;
6371590Srgrimes    }
6381590Srgrimes    /*
6391590Srgrimes     * PART 4: The mapupper_ext structures
6401590Srgrimes     */
6411590Srgrimes    list = mapupper.root;
6421590Srgrimes
6431590Srgrimes    while (list) {
6441590Srgrimes	_RuneEntry re;
6451590Srgrimes
6461590Srgrimes	re.min = htonl(list->min);
6471590Srgrimes	re.max = htonl(list->max);
6481590Srgrimes	re.map = htonl(list->map);
6491590Srgrimes
6501590Srgrimes	if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
6511590Srgrimes	    perror(locale_file);
6521590Srgrimes	    exit(1);
6531590Srgrimes	}
6541590Srgrimes
6551590Srgrimes        list = list->next;
6561590Srgrimes    }
6571590Srgrimes    /*
6581590Srgrimes     * PART 5: The runetype_ext tables
6591590Srgrimes     */
6601590Srgrimes    list = types.root;
6611590Srgrimes
6621590Srgrimes    while (list) {
6631590Srgrimes	for (x = 0; x < list->max - list->min + 1; ++x)
6641590Srgrimes	    list->types[x] = htonl(list->types[x]);
6651590Srgrimes
6661590Srgrimes	if (!list->map) {
66721393Sache	    if (fwrite((char *)list->types,
6682509Sbde		       (list->max - list->min + 1) * sizeof(unsigned long),
6692509Sbde		       1, fp) != 1) {
6701590Srgrimes		perror(locale_file);
6711590Srgrimes		exit(1);
6721590Srgrimes	    }
6731590Srgrimes	}
6741590Srgrimes        list = list->next;
6751590Srgrimes    }
6761590Srgrimes    /*
6771590Srgrimes     * PART 5: And finally the variable data
6781590Srgrimes     */
6791590Srgrimes    if (fwrite((char *)new_locale.variable,
6801590Srgrimes	       ntohl(new_locale.variable_len), 1, fp) != 1) {
6811590Srgrimes	perror(locale_file);
6821590Srgrimes	exit(1);
6831590Srgrimes    }
6841590Srgrimes    fclose(fp);
6851590Srgrimes
6861590Srgrimes    if (!debug)
6871590Srgrimes	return;
6881590Srgrimes
6891590Srgrimes    if (new_locale.encoding[0])
6901590Srgrimes	fprintf(stderr, "ENCODING	%s\n", new_locale.encoding);
6911590Srgrimes    if (new_locale.variable)
69238022Sbde	fprintf(stderr, "VARIABLE	%s\n", (char *)new_locale.variable);
6931590Srgrimes
6941590Srgrimes    fprintf(stderr, "\nMAPLOWER:\n\n");
6951590Srgrimes
6961590Srgrimes    for (x = 0; x < _CACHED_RUNES; ++x) {
6971590Srgrimes	if (isprint(maplower.map[x]))
69838022Sbde	    fprintf(stderr, " '%c'", (int)maplower.map[x]);
6991590Srgrimes	else if (maplower.map[x])
70038022Sbde	    fprintf(stderr, "%04lx", maplower.map[x]);
7011590Srgrimes	else
7021590Srgrimes	    fprintf(stderr, "%4x", 0);
7031590Srgrimes	if ((x & 0xf) == 0xf)
7041590Srgrimes	    fprintf(stderr, "\n");
7051590Srgrimes	else
7061590Srgrimes	    fprintf(stderr, " ");
7071590Srgrimes    }
7081590Srgrimes    fprintf(stderr, "\n");
7091590Srgrimes
7101590Srgrimes    for (list = maplower.root; list; list = list->next)
7111590Srgrimes	fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
7121590Srgrimes
7131590Srgrimes    fprintf(stderr, "\nMAPUPPER:\n\n");
7141590Srgrimes
7151590Srgrimes    for (x = 0; x < _CACHED_RUNES; ++x) {
7161590Srgrimes	if (isprint(mapupper.map[x]))
71738022Sbde	    fprintf(stderr, " '%c'", (int)mapupper.map[x]);
7181590Srgrimes	else if (mapupper.map[x])
71938022Sbde	    fprintf(stderr, "%04lx", mapupper.map[x]);
7201590Srgrimes	else
7211590Srgrimes	    fprintf(stderr, "%4x", 0);
7221590Srgrimes	if ((x & 0xf) == 0xf)
7231590Srgrimes	    fprintf(stderr, "\n");
7241590Srgrimes	else
7251590Srgrimes	    fprintf(stderr, " ");
7261590Srgrimes    }
7271590Srgrimes    fprintf(stderr, "\n");
7281590Srgrimes
7291590Srgrimes    for (list = mapupper.root; list; list = list->next)
7301590Srgrimes	fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
7311590Srgrimes
7321590Srgrimes
7331590Srgrimes    fprintf(stderr, "\nTYPES:\n\n");
7341590Srgrimes
7351590Srgrimes    for (x = 0; x < _CACHED_RUNES; ++x) {
7362509Sbde	unsigned long r = types.map[x];
7371590Srgrimes
7381590Srgrimes	if (r) {
7391590Srgrimes	    if (isprint(x))
74038022Sbde		fprintf(stderr, " '%c': %2d", x, (int)(r & 0xff));
7411590Srgrimes	    else
74238022Sbde		fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
7431590Srgrimes
74457035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
74557035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
74657035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
74757035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
74857035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
74957035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
75057035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
75157035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
75257035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
75357035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
75457035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
75557035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
75657035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
75757035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
7581590Srgrimes	    fprintf(stderr, "\n");
7591590Srgrimes	}
7601590Srgrimes    }
7611590Srgrimes
7621590Srgrimes    for (list = types.root; list; list = list->next) {
7631590Srgrimes	if (list->map && list->min + 3 < list->max) {
7642509Sbde	    unsigned long r = list->map;
7651590Srgrimes
76638022Sbde	    fprintf(stderr, "%04lx: %2d",
76738022Sbde		(unsigned long)list->min, (int)(r & 0xff));
7681590Srgrimes
76957035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
77057035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
77157035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
77257035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
77357035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
77457035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
77557035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
77657035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
77757035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
77857035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
77957035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
78057035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
78157035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
78257035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
7831590Srgrimes	    fprintf(stderr, "\n...\n");
7841590Srgrimes
78538022Sbde	    fprintf(stderr, "%04lx: %2d",
78638022Sbde		(unsigned long)list->max, (int)(r & 0xff));
7871590Srgrimes
78857035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
78957035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
79057035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
79157035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
79257035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
79357035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
79457035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
79557035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
79657035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
79757035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
79857035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
79957035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
80057035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
80157035Sobrien	    fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
8021590Srgrimes	    fprintf(stderr, "\n");
8031590Srgrimes	} else
8041590Srgrimes	for (x = list->min; x <= list->max; ++x) {
8052509Sbde	    unsigned long r = ntohl(list->types[x - list->min]);
8061590Srgrimes
8071590Srgrimes	    if (r) {
80838022Sbde		fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
8091590Srgrimes
81057035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
81157035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
81257035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
81357035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
81457035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
81557035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
81657035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
81757035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
81857035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
81957035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
82057035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
82157035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
82257035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
82357035Sobrien		fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
8241590Srgrimes		fprintf(stderr, "\n");
8251590Srgrimes	    }
8261590Srgrimes	}
8271590Srgrimes    }
8281590Srgrimes}
829