3 * SPDX-License-Identifier: BSD-3-Clause
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Paul Borman at Krystal Technologies.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
38 static char sccsid[] = "@(#)yacc.y 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
45 #include <arpa/inet.h>
59 static void *xmalloc(unsigned int sz);
60 static uint32_t *xlalloc(unsigned int sz);
61 void yyerror(const char *s);
62 static uint32_t *xrelalloc(uint32_t *old, unsigned int sz);
63 static void dump_tables(void);
64 static void cleanout(void);
66 const char *locale_file = "<stdout>";
68 rune_map maplower = { { 0 }, NULL };
69 rune_map mapupper = { { 0 }, NULL };
70 rune_map types = { { 0 }, NULL };
72 _FileRuneLocale new_locale = { "", "", {}, {}, {}, 0, 0, 0, 0 };
73 char *variable = NULL;
75 void set_map(rune_map *, rune_list *, uint32_t);
76 void set_digitmap(rune_map *, rune_list *);
77 void add_map(rune_map *, rune_list *, uint32_t);
78 static void usage(void);
117 entry : ENCODING STRING
118 { if (strcmp($2, "NONE") &&
119 strcmp($2, "ASCII") &&
120 strcmp($2, "UTF-8") &&
123 strcmp($2, "GB18030") &&
124 strcmp($2, "GB2312") &&
125 strcmp($2, "BIG5") &&
126 strcmp($2, "MSKanji"))
127 warnx("ENCODING %s is not supported by libc", $2);
128 strlcpy(new_locale.encoding, $2,
129 sizeof(new_locale.encoding)); }
131 { new_locale.variable_len = strlen($1) + 1;
132 variable = xmalloc(new_locale.variable_len);
133 strcpy(variable, $1);
136 { warnx("the INVALID keyword is deprecated"); }
138 { set_map(&types, $2, $1); }
140 { set_map(&maplower, $2, 0); }
142 { set_map(&mapupper, $2, 0); }
144 { set_digitmap(&types, $2); }
149 $$ = (rune_list *)xmalloc(sizeof(rune_list));
156 $$ = (rune_list *)xmalloc(sizeof(rune_list));
163 $$ = (rune_list *)xmalloc(sizeof(rune_list));
168 | list RUNE THRU RUNE
170 $$ = (rune_list *)xmalloc(sizeof(rune_list));
177 map : LBRK RUNE RUNE RBRK
179 $$ = (rune_list *)xmalloc(sizeof(rune_list));
185 | map LBRK RUNE RUNE RBRK
187 $$ = (rune_list *)xmalloc(sizeof(rune_list));
193 | LBRK RUNE THRU RUNE ':' RUNE RBRK
195 $$ = (rune_list *)xmalloc(sizeof(rune_list));
201 | map LBRK RUNE THRU RUNE ':' RUNE RBRK
203 $$ = (rune_list *)xmalloc(sizeof(rune_list));
223 main(int ac, char *av[])
229 while ((x = getopt(ac, av, "do:")) != -1) {
235 locale_file = optarg;
236 if ((fp = fopen(locale_file, "w")) == NULL)
237 err(1, "%s", locale_file);
245 switch (ac - optind) {
249 if (freopen(av[optind], "r", stdin) == 0)
250 err(1, "%s", av[optind]);
255 for (x = 0; x < _CACHED_RUNES; ++x) {
259 memcpy(new_locale.magic, _FILE_RUNE_MAGIC_1, sizeof(new_locale.magic));
269 fprintf(stderr, "usage: mklocale [-d] [-o output] [source]\n");
274 yyerror(const char *s)
276 fprintf(stderr, "%s\n", s);
280 xmalloc(unsigned int sz)
282 void *r = malloc(sz);
289 xlalloc(unsigned int sz)
291 uint32_t *r = (uint32_t *)malloc(sz * sizeof(uint32_t));
298 xrelalloc(uint32_t *old, unsigned int sz)
300 uint32_t *r = (uint32_t *)realloc((char *)old,
301 sz * sizeof(uint32_t));
303 errx(1, "xrelalloc");
308 set_map(rune_map *map, rune_list *list, uint32_t flag)
311 rune_list *nlist = list->next;
312 add_map(map, list, flag);
318 set_digitmap(rune_map *map, rune_list *list)
323 rune_list *nlist = list->next;
324 for (i = list->min; i <= list->max; ++i) {
325 if (list->map + (i - list->min)) {
326 rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
329 add_map(map, tmp, list->map + (i - list->min));
338 add_map(rune_map *map, rune_list *list, uint32_t flag)
345 while (list->min < _CACHED_RUNES && list->min <= list->max) {
347 map->map[list->min++] |= flag;
349 map->map[list->min++] = list->map++;
352 if (list->min > list->max) {
357 run = list->max - list->min + 1;
359 if (!(r = map->root) || (list->max < r->min - 1)
360 || (!flag && list->max == r->min - 1)) {
362 list->types = xlalloc(run);
363 for (i = 0; i < run; ++i)
364 list->types[i] = flag;
366 list->next = map->root;
371 for (r = map->root; r && r->max + 1 < list->min; r = r->next)
376 * We are off the end.
379 list->types = xlalloc(run);
380 for (i = 0; i < run; ++i)
381 list->types[i] = flag;
388 if (list->max < r->min - 1) {
390 * We come before this range and we do not intersect it.
391 * We are not before the root node, it was checked before the loop
394 list->types = xlalloc(run);
395 for (i = 0; i < run; ++i)
396 list->types[i] = flag;
398 list->next = lr->next;
404 * At this point we have found that we at least intersect with
405 * the range pointed to by `r', we might intersect with one or
406 * more ranges beyond `r' as well.
409 if (!flag && list->map - list->min != r->map - r->min) {
411 * There are only two cases when we are doing case maps and
412 * our maps needn't have the same offset. When we are adjoining
413 * but not intersecting.
415 if (list->max + 1 == r->min) {
420 if (list->min - 1 == r->max) {
421 list->next = r->next;
425 errx(1, "error: conflicting map entries");
428 if (list->min >= r->min && list->max <= r->max) {
434 for (i = list->min; i <= list->max; ++i)
435 r->types[i - r->min] |= flag;
440 if (list->min <= r->min && list->max >= r->max) {
442 * Superset case. Make him big enough to hold us.
443 * We might need to merge with the guy after him.
446 list->types = xlalloc(list->max - list->min + 1);
448 for (i = list->min; i <= list->max; ++i)
449 list->types[i - list->min] = flag;
451 for (i = r->min; i <= r->max; ++i)
452 list->types[i - list->min] |= r->types[i - r->min];
455 r->types = list->types;
462 } else if (list->min < r->min) {
464 * Our tail intersects his head.
467 list->types = xlalloc(r->max - list->min + 1);
469 for (i = r->min; i <= r->max; ++i)
470 list->types[i - list->min] = r->types[i - r->min];
472 for (i = list->min; i < r->min; ++i)
473 list->types[i - list->min] = flag;
475 for (i = r->min; i <= list->max; ++i)
476 list->types[i - list->min] |= flag;
479 r->types = list->types;
488 * Our head intersects his tail.
489 * We might need to merge with the guy after him.
492 r->types = xrelalloc(r->types, list->max - r->min + 1);
494 for (i = list->min; i <= r->max; ++i)
495 r->types[i - r->min] |= flag;
497 for (i = r->max+1; i <= list->max; ++i)
498 r->types[i - r->min] = flag;
505 * Okay, check to see if we grew into the next guy(s)
507 while ((lr = r->next) && r->max >= lr->min) {
509 if (r->max >= lr->max) {
511 * Good, we consumed all of him.
513 for (i = lr->min; i <= lr->max; ++i)
514 r->types[i - r->min] |= lr->types[i - lr->min];
517 * "append" him on to the end of us.
519 r->types = xrelalloc(r->types, lr->max - r->min + 1);
521 for (i = lr->min; i <= r->max; ++i)
522 r->types[i - r->min] |= lr->types[i - lr->min];
524 for (i = r->max+1; i <= lr->max; ++i)
525 r->types[i - r->min] = lr->types[i - lr->min];
530 if (lr->max > r->max)
545 int x, first_d, curr_d;
549 * See if we can compress some of the istype arrays
551 for(list = types.root; list; list = list->next) {
552 list->map = list->types[0];
553 for (x = 1; x < list->max - list->min + 1; ++x) {
554 if ((int32_t)list->types[x] != list->map) {
561 first_d = curr_d = -1;
562 for (x = 0; x < _CACHED_RUNES; ++x) {
563 uint32_t r = types.map[x];
567 first_d = curr_d = x;
568 else if (x != curr_d + 1)
569 errx(1, "error: DIGIT range is not contiguous");
570 else if (x - first_d > 9)
571 errx(1, "error: DIGIT range is too big");
576 "error: DIGIT range is not a subset of XDIGIT range");
580 errx(1, "error: no DIGIT range defined in the single byte area");
581 else if (curr_d - first_d < 9)
582 errx(1, "error: DIGIT range is too small in the single byte area");
585 * Fill in our tables. Do this in network order so that
586 * diverse machines have a chance of sharing data.
587 * (Machines like Crays cannot share with little machines due to
588 * word size. Sigh. We tried.)
590 for (x = 0; x < _CACHED_RUNES; ++x) {
591 new_locale.runetype[x] = htonl(types.map[x]);
592 new_locale.maplower[x] = htonl(maplower.map[x]);
593 new_locale.mapupper[x] = htonl(mapupper.map[x]);
597 * Count up how many ranges we will need for each of the extents.
602 new_locale.runetype_ext_nranges++;
605 new_locale.runetype_ext_nranges =
606 htonl(new_locale.runetype_ext_nranges);
608 list = maplower.root;
611 new_locale.maplower_ext_nranges++;
614 new_locale.maplower_ext_nranges =
615 htonl(new_locale.maplower_ext_nranges);
617 list = mapupper.root;
620 new_locale.mapupper_ext_nranges++;
623 new_locale.mapupper_ext_nranges =
624 htonl(new_locale.mapupper_ext_nranges);
626 new_locale.variable_len = htonl(new_locale.variable_len);
629 * Okay, we are now ready to write the new locale file.
633 * PART 1: The _FileRuneLocale structure
635 if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
640 * PART 2: The runetype_ext structures (not the actual tables)
647 re.min = htonl(list->min);
648 re.max = htonl(list->max);
649 re.map = htonl(list->map);
651 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
659 * PART 3: The maplower_ext structures
661 list = maplower.root;
666 re.min = htonl(list->min);
667 re.max = htonl(list->max);
668 re.map = htonl(list->map);
670 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
678 * PART 4: The mapupper_ext structures
680 list = mapupper.root;
685 re.min = htonl(list->min);
686 re.max = htonl(list->max);
687 re.map = htonl(list->map);
689 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
697 * PART 5: The runetype_ext tables
702 for (x = 0; x < list->max - list->min + 1; ++x)
703 list->types[x] = htonl(list->types[x]);
706 if (fwrite((char *)list->types,
707 (list->max - list->min + 1) * sizeof(uint32_t),
716 * PART 6: And finally the variable data
718 if (new_locale.variable_len != 0 &&
719 fwrite(variable, ntohl(new_locale.variable_len), 1, fp) != 1) {
723 if (fclose(fp) != 0) {
732 if (new_locale.encoding[0])
733 fprintf(stderr, "ENCODING %s\n", new_locale.encoding);
735 fprintf(stderr, "VARIABLE %s\n", variable);
737 fprintf(stderr, "\nMAPLOWER:\n\n");
739 for (x = 0; x < _CACHED_RUNES; ++x) {
740 if (isprint(maplower.map[x]))
741 fprintf(stderr, " '%c'", (int)maplower.map[x]);
742 else if (maplower.map[x])
743 fprintf(stderr, "%04x", maplower.map[x]);
745 fprintf(stderr, "%4x", 0);
746 if ((x & 0xf) == 0xf)
747 fprintf(stderr, "\n");
749 fprintf(stderr, " ");
751 fprintf(stderr, "\n");
753 for (list = maplower.root; list; list = list->next)
754 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
756 fprintf(stderr, "\nMAPUPPER:\n\n");
758 for (x = 0; x < _CACHED_RUNES; ++x) {
759 if (isprint(mapupper.map[x]))
760 fprintf(stderr, " '%c'", (int)mapupper.map[x]);
761 else if (mapupper.map[x])
762 fprintf(stderr, "%04x", mapupper.map[x]);
764 fprintf(stderr, "%4x", 0);
765 if ((x & 0xf) == 0xf)
766 fprintf(stderr, "\n");
768 fprintf(stderr, " ");
770 fprintf(stderr, "\n");
772 for (list = mapupper.root; list; list = list->next)
773 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
776 fprintf(stderr, "\nTYPES:\n\n");
778 for (x = 0; x < _CACHED_RUNES; ++x) {
779 uint32_t r = types.map[x];
783 fprintf(stderr, " '%c': %2d", x, (int)(r & 0xff));
785 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
787 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
788 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
789 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
790 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
791 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
792 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
793 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
794 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
795 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
796 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
797 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
798 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
799 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
800 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
801 fprintf(stderr, "\n");
805 for (list = types.root; list; list = list->next) {
806 if (list->map && list->min + 3 < list->max) {
807 uint32_t r = list->map;
809 fprintf(stderr, "%04x: %2d",
810 (uint32_t)list->min, (int)(r & 0xff));
812 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
813 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
814 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
815 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
816 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
817 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
818 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
819 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
820 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
821 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
822 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
823 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
824 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
825 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
826 fprintf(stderr, "\n...\n");
828 fprintf(stderr, "%04x: %2d",
829 (uint32_t)list->max, (int)(r & 0xff));
831 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
832 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
833 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
834 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
835 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
836 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
837 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
838 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
839 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
840 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
841 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
842 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
843 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
844 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
845 fprintf(stderr, "\n");
847 for (x = list->min; x <= list->max; ++x) {
848 uint32_t r = ntohl(list->types[x - list->min]);
851 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
853 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
854 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
855 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
856 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
857 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
858 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
859 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
860 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
861 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
862 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
863 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
864 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
865 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
866 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
867 fprintf(stderr, "\n");