* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Paul Borman at Krystal Technologies.
* %sccs.include.redist.c%
static char sccsid[] = "@(#)yacc.y 8.1 (Berkeley) %G%";
char *locale_file = "<stdout>";
rune_map maplower = { 0, };
rune_map mapupper = { 0, };
_RuneLocale new_locale = { 0, };
void set_map __P((rune_map *, rune_list *, u_long));
void set_digitmap __P((rune_map *, rune_list *));
void add_map __P((rune_map *, rune_list *, u_long));
{ strncpy(new_locale.encoding, $2, sizeof(new_locale.encoding)); }
{ new_locale.variable_len = strlen($1) + 1;
new_locale.variable = malloc(new_locale.variable_len);
strcpy((char *)new_locale.variable, $1);
{ new_locale.invalid_rune = $2; }
{ set_map(&types, $2, $1); }
{ set_map(&maplower, $2, 0); }
{ set_map(&mapupper, $2, 0); }
{ set_digitmap(&types, $2); }
$$ = (rune_list *)malloc(sizeof(rune_list));
$$ = (rune_list *)malloc(sizeof(rune_list));
$$ = (rune_list *)malloc(sizeof(rune_list));
$$ = (rune_list *)malloc(sizeof(rune_list));
map : LBRK RUNE RUNE RBRK
$$ = (rune_list *)malloc(sizeof(rune_list));
| map LBRK RUNE RUNE RBRK
$$ = (rune_list *)malloc(sizeof(rune_list));
| LBRK RUNE THRU RUNE ':' RUNE RBRK
$$ = (rune_list *)malloc(sizeof(rune_list));
| map LBRK RUNE THRU RUNE ':' RUNE RBRK
$$ = (rune_list *)malloc(sizeof(rune_list));
while ((x = getopt(ac, av, "do:")) != EOF) {
if ((fp = fopen(locale_file, "w")) == 0) {
fprintf(stderr, "Usage: mklocale [-d] [-o output] [source]\n");
if (freopen(av[optind], "r", stdin) == 0) {
for (x = 0; x < _CACHED_RUNES; ++x) {
new_locale.invalid_rune = _INVALID_RUNE;
memcpy(new_locale.magic, _RUNE_MAGIC_1, sizeof(new_locale.magic));
fprintf(stderr, "%s\n", s);
u_long *r = (u_long *)malloc(sz * sizeof(u_long));
u_long *r = (u_long *)realloc((char *)old, sz * sizeof(u_long));
rune_list *nlist = list->next;
add_map(map, list, flag);
rune_list *nlist = list->next;
for (i = list->min; i <= list->max; ++i) {
if (list->map + (i - list->min)) {
rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
add_map(map, tmp, list->map + (i - list->min));
while (list->min < _CACHED_RUNES && list->min <= list->max) {
map->map[list->min++] |= flag;
map->map[list->min++] = list->map++;
if (list->min > list->max) {
run = list->max - list->min + 1;
if (!(r = map->root) || (list->max < r->min - 1)
|| (!flag && list->max == r->min - 1)) {
list->types = xlalloc(run);
for (i = 0; i < run; ++i)
for (r = map->root; r && r->max + 1 < list->min; r = r->next)
list->types = xlalloc(run);
for (i = 0; i < run; ++i)
if (list->max < r->min - 1) {
* We come before this range and we do not intersect it.
* We are not before the root node, it was checked before the loop
list->types = xlalloc(run);
for (i = 0; i < run; ++i)
* At this point we have found that we at least intersect with
* the range pointed to by `r', we might intersect with one or
* more ranges beyond `r' as well.
if (!flag && list->map - list->min != r->map - r->min) {
* There are only two cases when we are doing case maps and
* our maps needn't have the same offset. When we are adjoining
if (list->max + 1 == r->min) {
if (list->min - 1 == r->max) {
fprintf(stderr, "Error: conflicting map entries\n");
if (list->min >= r->min && list->max <= r->max) {
for (i = list->min; i <= list->max; ++i)
r->types[i - r->min] |= flag;
if (list->min <= r->min && list->max >= r->max) {
* Superset case. Make him big enough to hold us.
* We might need to merge with the guy after him.
list->types = xlalloc(list->max - list->min + 1);
for (i = list->min; i <= list->max; ++i)
list->types[i - list->min] = flag;
for (i = r->min; i <= r->max; ++i)
list->types[i - list->min] |= r->types[i - r->min];
} else if (list->min < r->min) {
* Our tail intersects his head.
list->types = xlalloc(r->max - list->min + 1);
for (i = r->min; i <= r->max; ++i)
list->types[i - list->min] = r->types[i - r->min];
for (i = list->min; i < r->min; ++i)
list->types[i - list->min] = flag;
for (i = r->min; i <= list->max; ++i)
list->types[i - list->min] |= flag;
* Our head intersects his tail.
* We might need to merge with the guy after him.
r->types = xrelalloc(r->types, list->max - r->min + 1);
for (i = list->min; i <= r->max; ++i)
r->types[i - r->min] |= flag;
for (i = r->max+1; i <= list->max; ++i)
r->types[i - r->min] = flag;
* Okay, check to see if we grew into the next guy(s)
while ((lr = r->next) && r->max >= lr->min) {
* Good, we consumed all of him.
for (i = lr->min; i <= lr->max; ++i)
r->types[i - r->min] |= lr->types[i - lr->min];
* "append" him on to the end of us.
r->types = xrelalloc(r->types, lr->max - r->min + 1);
for (i = lr->min; i <= r->max; ++i)
r->types[i - r->min] |= lr->types[i - lr->min];
for (i = r->max+1; i <= lr->max; ++i)
r->types[i - r->min] = lr->types[i - lr->min];
* See if we can compress some of the istype arrays
for(list = types.root; list; list = list->next) {
list->map = list->types[0];
for (x = 1; x < list->max - list->min + 1; ++x) {
if (list->types[x] != list->map) {
new_locale.invalid_rune = htonl(new_locale.invalid_rune);
* Fill in our tables. Do this in network order so that
* diverse machines have a chance of sharing data.
* (Machines like Crays cannot share with little machines due to
* word size. Sigh. We tried.)
for (x = 0; x < _CACHED_RUNES; ++x) {
new_locale.runetype[x] = htonl(types.map[x]);
new_locale.maplower[x] = htonl(maplower.map[x]);
new_locale.mapupper[x] = htonl(mapupper.map[x]);
* Count up how many ranges we will need for each of the extents.
new_locale.runetype_ext.nranges++;
new_locale.runetype_ext.nranges = htonl(new_locale.runetype_ext.nranges);
new_locale.maplower_ext.nranges++;
new_locale.maplower_ext.nranges = htonl(new_locale.maplower_ext.nranges);
new_locale.mapupper_ext.nranges++;
new_locale.mapupper_ext.nranges = htonl(new_locale.mapupper_ext.nranges);
new_locale.variable_len = htonl(new_locale.variable_len);
* Okay, we are now ready to write the new locale file.
* PART 1: The _RuneLocale structure
if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
* PART 2: The runetype_ext structures (not the actual tables)
re.min = htonl(list->min);
re.max = htonl(list->max);
re.map = htonl(list->map);
if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
* PART 3: The maplower_ext structures
re.min = htonl(list->min);
re.max = htonl(list->max);
re.map = htonl(list->map);
if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
* PART 4: The mapupper_ext structures
re.min = htonl(list->min);
re.max = htonl(list->max);
re.map = htonl(list->map);
if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
* PART 5: The runetype_ext tables
for (x = 0; x < list->max - list->min + 1; ++x)
list->types[x] = htonl(list->types[x]);
if (fwrite((char *)&list->types,
(list->max - list->min + 1)*sizeof(u_long), 1, fp) != 1) {
* PART 5: And finally the variable data
if (fwrite((char *)new_locale.variable,
ntohl(new_locale.variable_len), 1, fp) != 1) {
if (new_locale.encoding[0])
fprintf(stderr, "ENCODING %s\n", new_locale.encoding);
fprintf(stderr, "VARIABLE %s\n", new_locale.variable);
fprintf(stderr, "\nMAPLOWER:\n\n");
for (x = 0; x < _CACHED_RUNES; ++x) {
if (isprint(maplower.map[x]))
fprintf(stderr, " '%c'", maplower.map[x]);
else if (maplower.map[x])
fprintf(stderr, "%04x", maplower.map[x]);
fprintf(stderr, "%4x", 0);
for (list = maplower.root; list; list = list->next)
fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
fprintf(stderr, "\nMAPUPPER:\n\n");
for (x = 0; x < _CACHED_RUNES; ++x) {
if (isprint(mapupper.map[x]))
fprintf(stderr, " '%c'", mapupper.map[x]);
else if (mapupper.map[x])
fprintf(stderr, "%04x", mapupper.map[x]);
fprintf(stderr, "%4x", 0);
for (list = mapupper.root; list; list = list->next)
fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
fprintf(stderr, "\nTYPES:\n\n");
for (x = 0; x < _CACHED_RUNES; ++x) {
fprintf(stderr, " '%c': %2d", x, r & 0xff);
fprintf(stderr, "%04x: %2d", x, r & 0xff);
fprintf(stderr, " %4s", (r & _A) ? "alph" : "");
fprintf(stderr, " %4s", (r & _C) ? "ctrl" : "");
fprintf(stderr, " %4s", (r & _D) ? "dig" : "");
fprintf(stderr, " %4s", (r & _G) ? "graf" : "");
fprintf(stderr, " %4s", (r & _L) ? "low" : "");
fprintf(stderr, " %4s", (r & _P) ? "punc" : "");
fprintf(stderr, " %4s", (r & _S) ? "spac" : "");
fprintf(stderr, " %4s", (r & _U) ? "upp" : "");
fprintf(stderr, " %4s", (r & _X) ? "xdig" : "");
fprintf(stderr, " %4s", (r & _B) ? "blnk" : "");
fprintf(stderr, " %4s", (r & _R) ? "prnt" : "");
fprintf(stderr, " %4s", (r & _I) ? "ideo" : "");
fprintf(stderr, " %4s", (r & _T) ? "spec" : "");
fprintf(stderr, " %4s", (r & _Q) ? "phon" : "");
for (list = types.root; list; list = list->next) {
if (list->map && list->min + 3 < list->max) {
fprintf(stderr, "%04x: %2d", list->min, r & 0xff);
fprintf(stderr, " %4s", (r & _A) ? "alph" : "");
fprintf(stderr, " %4s", (r & _C) ? "ctrl" : "");
fprintf(stderr, " %4s", (r & _D) ? "dig" : "");
fprintf(stderr, " %4s", (r & _G) ? "graf" : "");
fprintf(stderr, " %4s", (r & _L) ? "low" : "");
fprintf(stderr, " %4s", (r & _P) ? "punc" : "");
fprintf(stderr, " %4s", (r & _S) ? "spac" : "");
fprintf(stderr, " %4s", (r & _U) ? "upp" : "");
fprintf(stderr, " %4s", (r & _X) ? "xdig" : "");
fprintf(stderr, " %4s", (r & _B) ? "blnk" : "");
fprintf(stderr, " %4s", (r & _R) ? "prnt" : "");
fprintf(stderr, " %4s", (r & _I) ? "ideo" : "");
fprintf(stderr, " %4s", (r & _T) ? "spec" : "");
fprintf(stderr, " %4s", (r & _Q) ? "phon" : "");
fprintf(stderr, "\n...\n");
fprintf(stderr, "%04x: %2d", list->max, r & 0xff);
fprintf(stderr, " %4s", (r & _A) ? "alph" : "");
fprintf(stderr, " %4s", (r & _C) ? "ctrl" : "");
fprintf(stderr, " %4s", (r & _D) ? "dig" : "");
fprintf(stderr, " %4s", (r & _G) ? "graf" : "");
fprintf(stderr, " %4s", (r & _L) ? "low" : "");
fprintf(stderr, " %4s", (r & _P) ? "punc" : "");
fprintf(stderr, " %4s", (r & _S) ? "spac" : "");
fprintf(stderr, " %4s", (r & _U) ? "upp" : "");
fprintf(stderr, " %4s", (r & _X) ? "xdig" : "");
fprintf(stderr, " %4s", (r & _B) ? "blnk" : "");
fprintf(stderr, " %4s", (r & _R) ? "prnt" : "");
fprintf(stderr, " %4s", (r & _I) ? "ideo" : "");
fprintf(stderr, " %4s", (r & _T) ? "spec" : "");
fprintf(stderr, " %4s", (r & _Q) ? "phon" : "");
for (x = list->min; x <= list->max; ++x) {
u_long r = ntohl(list->types[x - list->min]);
fprintf(stderr, "%04x: %2d", x, r & 0xff);
fprintf(stderr, " %4s", (r & _A) ? "alph" : "");
fprintf(stderr, " %4s", (r & _C) ? "ctrl" : "");
fprintf(stderr, " %4s", (r & _D) ? "dig" : "");
fprintf(stderr, " %4s", (r & _G) ? "graf" : "");
fprintf(stderr, " %4s", (r & _L) ? "low" : "");
fprintf(stderr, " %4s", (r & _P) ? "punc" : "");
fprintf(stderr, " %4s", (r & _S) ? "spac" : "");
fprintf(stderr, " %4s", (r & _U) ? "upp" : "");
fprintf(stderr, " %4s", (r & _X) ? "xdig" : "");
fprintf(stderr, " %4s", (r & _B) ? "blnk" : "");
fprintf(stderr, " %4s", (r & _R) ? "prnt" : "");
fprintf(stderr, " %4s", (r & _I) ? "ideo" : "");
fprintf(stderr, " %4s", (r & _T) ? "spec" : "");
fprintf(stderr, " %4s", (r & _Q) ? "phon" : "");