4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Paul Borman at Krystal Technologies.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 static char sccsid[] = "@(#)yacc.y 8.1 (Berkeley) 6/6/93";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <arpa/inet.h>
61 static void *xmalloc(unsigned int sz);
62 static uint32_t *xlalloc(unsigned int sz);
63 void yyerror(const char *s);
64 static uint32_t *xrelalloc(uint32_t *old, unsigned int sz);
65 static void dump_tables(void);
66 static void cleanout(void);
68 const char *locale_file = "<stdout>";
70 rune_map maplower = { { 0 }, NULL };
71 rune_map mapupper = { { 0 }, NULL };
72 rune_map types = { { 0 }, NULL };
74 _FileRuneLocale new_locale = { "", "", {}, {}, {}, 0, 0, 0, 0 };
75 char *variable = NULL;
77 void set_map(rune_map *, rune_list *, uint32_t);
78 void set_digitmap(rune_map *, rune_list *);
79 void add_map(rune_map *, rune_list *, uint32_t);
80 static void usage(void);
119 entry : ENCODING STRING
120 { if (strcmp($2, "NONE") &&
121 strcmp($2, "UTF-8") &&
124 strcmp($2, "GB18030") &&
125 strcmp($2, "GB2312") &&
126 strcmp($2, "BIG5") &&
127 strcmp($2, "MSKanji"))
128 warnx("ENCODING %s is not supported by libc", $2);
129 strncpy(new_locale.encoding, $2,
130 sizeof(new_locale.encoding)); }
132 { new_locale.variable_len = strlen($1) + 1;
133 variable = xmalloc(new_locale.variable_len);
134 strcpy(variable, $1);
137 { warnx("the INVALID keyword is deprecated"); }
139 { set_map(&types, $2, $1); }
141 { set_map(&maplower, $2, 0); }
143 { set_map(&mapupper, $2, 0); }
145 { set_digitmap(&types, $2); }
150 $$ = (rune_list *)xmalloc(sizeof(rune_list));
157 $$ = (rune_list *)xmalloc(sizeof(rune_list));
164 $$ = (rune_list *)xmalloc(sizeof(rune_list));
169 | list RUNE THRU RUNE
171 $$ = (rune_list *)xmalloc(sizeof(rune_list));
178 map : LBRK RUNE RUNE RBRK
180 $$ = (rune_list *)xmalloc(sizeof(rune_list));
186 | map LBRK RUNE RUNE RBRK
188 $$ = (rune_list *)xmalloc(sizeof(rune_list));
194 | LBRK RUNE THRU RUNE ':' RUNE RBRK
196 $$ = (rune_list *)xmalloc(sizeof(rune_list));
202 | map LBRK RUNE THRU RUNE ':' RUNE RBRK
204 $$ = (rune_list *)xmalloc(sizeof(rune_list));
224 main(int ac, char *av[])
230 while ((x = getopt(ac, av, "do:")) != -1) {
236 locale_file = optarg;
237 if ((fp = fopen(locale_file, "w")) == NULL)
238 err(1, "%s", locale_file);
246 switch (ac - optind) {
250 if (freopen(av[optind], "r", stdin) == 0)
251 err(1, "%s", av[optind]);
256 for (x = 0; x < _CACHED_RUNES; ++x) {
260 memcpy(new_locale.magic, _FILE_RUNE_MAGIC_1, sizeof(new_locale.magic));
270 fprintf(stderr, "usage: mklocale [-d] [-o output] [source]\n");
278 fprintf(stderr, "%s\n", s);
285 void *r = malloc(sz);
295 uint32_t *r = (uint32_t *)malloc(sz * sizeof(uint32_t));
306 uint32_t *r = (uint32_t *)realloc((char *)old,
307 sz * sizeof(uint32_t));
309 errx(1, "xrelalloc");
314 set_map(map, list, flag)
320 rune_list *nlist = list->next;
321 add_map(map, list, flag);
327 set_digitmap(map, list)
334 rune_list *nlist = list->next;
335 for (i = list->min; i <= list->max; ++i) {
336 if (list->map + (i - list->min)) {
337 rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
340 add_map(map, tmp, list->map + (i - list->min));
349 add_map(map, list, flag)
359 while (list->min < _CACHED_RUNES && list->min <= list->max) {
361 map->map[list->min++] |= flag;
363 map->map[list->min++] = list->map++;
366 if (list->min > list->max) {
371 run = list->max - list->min + 1;
373 if (!(r = map->root) || (list->max < r->min - 1)
374 || (!flag && list->max == r->min - 1)) {
376 list->types = xlalloc(run);
377 for (i = 0; i < run; ++i)
378 list->types[i] = flag;
380 list->next = map->root;
385 for (r = map->root; r && r->max + 1 < list->min; r = r->next)
390 * We are off the end.
393 list->types = xlalloc(run);
394 for (i = 0; i < run; ++i)
395 list->types[i] = flag;
402 if (list->max < r->min - 1) {
404 * We come before this range and we do not intersect it.
405 * We are not before the root node, it was checked before the loop
408 list->types = xlalloc(run);
409 for (i = 0; i < run; ++i)
410 list->types[i] = flag;
412 list->next = lr->next;
418 * At this point we have found that we at least intersect with
419 * the range pointed to by `r', we might intersect with one or
420 * more ranges beyond `r' as well.
423 if (!flag && list->map - list->min != r->map - r->min) {
425 * There are only two cases when we are doing case maps and
426 * our maps needn't have the same offset. When we are adjoining
427 * but not intersecting.
429 if (list->max + 1 == r->min) {
434 if (list->min - 1 == r->max) {
435 list->next = r->next;
439 errx(1, "error: conflicting map entries");
442 if (list->min >= r->min && list->max <= r->max) {
448 for (i = list->min; i <= list->max; ++i)
449 r->types[i - r->min] |= flag;
454 if (list->min <= r->min && list->max >= r->max) {
456 * Superset case. Make him big enough to hold us.
457 * We might need to merge with the guy after him.
460 list->types = xlalloc(list->max - list->min + 1);
462 for (i = list->min; i <= list->max; ++i)
463 list->types[i - list->min] = flag;
465 for (i = r->min; i <= r->max; ++i)
466 list->types[i - list->min] |= r->types[i - r->min];
469 r->types = list->types;
476 } else if (list->min < r->min) {
478 * Our tail intersects his head.
481 list->types = xlalloc(r->max - list->min + 1);
483 for (i = r->min; i <= r->max; ++i)
484 list->types[i - list->min] = r->types[i - r->min];
486 for (i = list->min; i < r->min; ++i)
487 list->types[i - list->min] = flag;
489 for (i = r->min; i <= list->max; ++i)
490 list->types[i - list->min] |= flag;
493 r->types = list->types;
502 * Our head intersects his tail.
503 * We might need to merge with the guy after him.
506 r->types = xrelalloc(r->types, list->max - r->min + 1);
508 for (i = list->min; i <= r->max; ++i)
509 r->types[i - r->min] |= flag;
511 for (i = r->max+1; i <= list->max; ++i)
512 r->types[i - r->min] = flag;
519 * Okay, check to see if we grew into the next guy(s)
521 while ((lr = r->next) && r->max >= lr->min) {
523 if (r->max >= lr->max) {
525 * Good, we consumed all of him.
527 for (i = lr->min; i <= lr->max; ++i)
528 r->types[i - r->min] |= lr->types[i - lr->min];
531 * "append" him on to the end of us.
533 r->types = xrelalloc(r->types, lr->max - r->min + 1);
535 for (i = lr->min; i <= r->max; ++i)
536 r->types[i - r->min] |= lr->types[i - lr->min];
538 for (i = r->max+1; i <= lr->max; ++i)
539 r->types[i - r->min] = lr->types[i - lr->min];
544 if (lr->max > r->max)
559 int x, first_d, curr_d;
563 * See if we can compress some of the istype arrays
565 for(list = types.root; list; list = list->next) {
566 list->map = list->types[0];
567 for (x = 1; x < list->max - list->min + 1; ++x) {
568 if ((int32_t)list->types[x] != list->map) {
575 first_d = curr_d = -1;
576 for (x = 0; x < _CACHED_RUNES; ++x) {
577 uint32_t r = types.map[x];
581 first_d = curr_d = x;
582 else if (x != curr_d + 1)
583 errx(1, "error: DIGIT range is not contiguous");
584 else if (x - first_d > 9)
585 errx(1, "error: DIGIT range is too big");
590 "error: DIGIT range is not a subset of XDIGIT range");
594 errx(1, "error: no DIGIT range defined in the single byte area");
595 else if (curr_d - first_d < 9)
596 errx(1, "error: DIGIT range is too small in the single byte area");
599 * Fill in our tables. Do this in network order so that
600 * diverse machines have a chance of sharing data.
601 * (Machines like Crays cannot share with little machines due to
602 * word size. Sigh. We tried.)
604 for (x = 0; x < _CACHED_RUNES; ++x) {
605 new_locale.runetype[x] = htonl(types.map[x]);
606 new_locale.maplower[x] = htonl(maplower.map[x]);
607 new_locale.mapupper[x] = htonl(mapupper.map[x]);
611 * Count up how many ranges we will need for each of the extents.
616 new_locale.runetype_ext_nranges++;
619 new_locale.runetype_ext_nranges =
620 htonl(new_locale.runetype_ext_nranges);
622 list = maplower.root;
625 new_locale.maplower_ext_nranges++;
628 new_locale.maplower_ext_nranges =
629 htonl(new_locale.maplower_ext_nranges);
631 list = mapupper.root;
634 new_locale.mapupper_ext_nranges++;
637 new_locale.mapupper_ext_nranges =
638 htonl(new_locale.mapupper_ext_nranges);
640 new_locale.variable_len = htonl(new_locale.variable_len);
643 * Okay, we are now ready to write the new locale file.
647 * PART 1: The _FileRuneLocale structure
649 if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
654 * PART 2: The runetype_ext structures (not the actual tables)
661 re.min = htonl(list->min);
662 re.max = htonl(list->max);
663 re.map = htonl(list->map);
665 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
673 * PART 3: The maplower_ext structures
675 list = maplower.root;
680 re.min = htonl(list->min);
681 re.max = htonl(list->max);
682 re.map = htonl(list->map);
684 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
692 * PART 4: The mapupper_ext structures
694 list = mapupper.root;
699 re.min = htonl(list->min);
700 re.max = htonl(list->max);
701 re.map = htonl(list->map);
703 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
711 * PART 5: The runetype_ext tables
716 for (x = 0; x < list->max - list->min + 1; ++x)
717 list->types[x] = htonl(list->types[x]);
720 if (fwrite((char *)list->types,
721 (list->max - list->min + 1) * sizeof(uint32_t),
730 * PART 6: And finally the variable data
733 ntohl(new_locale.variable_len), 1, fp) != 1) {
737 if (fclose(fp) != 0) {
746 if (new_locale.encoding[0])
747 fprintf(stderr, "ENCODING %s\n", new_locale.encoding);
749 fprintf(stderr, "VARIABLE %s\n", variable);
751 fprintf(stderr, "\nMAPLOWER:\n\n");
753 for (x = 0; x < _CACHED_RUNES; ++x) {
754 if (isprint(maplower.map[x]))
755 fprintf(stderr, " '%c'", (int)maplower.map[x]);
756 else if (maplower.map[x])
757 fprintf(stderr, "%04x", maplower.map[x]);
759 fprintf(stderr, "%4x", 0);
760 if ((x & 0xf) == 0xf)
761 fprintf(stderr, "\n");
763 fprintf(stderr, " ");
765 fprintf(stderr, "\n");
767 for (list = maplower.root; list; list = list->next)
768 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
770 fprintf(stderr, "\nMAPUPPER:\n\n");
772 for (x = 0; x < _CACHED_RUNES; ++x) {
773 if (isprint(mapupper.map[x]))
774 fprintf(stderr, " '%c'", (int)mapupper.map[x]);
775 else if (mapupper.map[x])
776 fprintf(stderr, "%04x", mapupper.map[x]);
778 fprintf(stderr, "%4x", 0);
779 if ((x & 0xf) == 0xf)
780 fprintf(stderr, "\n");
782 fprintf(stderr, " ");
784 fprintf(stderr, "\n");
786 for (list = mapupper.root; list; list = list->next)
787 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
790 fprintf(stderr, "\nTYPES:\n\n");
792 for (x = 0; x < _CACHED_RUNES; ++x) {
793 uint32_t r = types.map[x];
797 fprintf(stderr, " '%c': %2d", x, (int)(r & 0xff));
799 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
801 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
802 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
803 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
804 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
805 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
806 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
807 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
808 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
809 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
810 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
811 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
812 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
813 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
814 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
815 fprintf(stderr, "\n");
819 for (list = types.root; list; list = list->next) {
820 if (list->map && list->min + 3 < list->max) {
821 uint32_t r = list->map;
823 fprintf(stderr, "%04x: %2d",
824 (uint32_t)list->min, (int)(r & 0xff));
826 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
827 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
828 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
829 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
830 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
831 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
832 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
833 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
834 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
835 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
836 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
837 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
838 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
839 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
840 fprintf(stderr, "\n...\n");
842 fprintf(stderr, "%04x: %2d",
843 (uint32_t)list->max, (int)(r & 0xff));
845 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
846 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
847 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
848 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
849 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
850 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
851 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
852 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
853 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
854 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
855 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
856 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
857 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
858 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
859 fprintf(stderr, "\n");
861 for (x = list->min; x <= list->max; ++x) {
862 uint32_t r = ntohl(list->types[x - list->min]);
865 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
867 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
868 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
869 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
870 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
871 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
872 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
873 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
874 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
875 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
876 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
877 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
878 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
879 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
880 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
881 fprintf(stderr, "\n");