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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)yacc.y 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: head/usr.bin/mklocale/yacc.y 216370 2010-12-11 08:32:16Z joel $
37 #include <arpa/inet.h>
49 static void *xmalloc(unsigned int sz);
50 static uint32_t *xlalloc(unsigned int sz);
51 void yyerror(const char *s);
52 static uint32_t *xrelalloc(uint32_t *old, unsigned int sz);
53 static void dump_tables(void);
54 static void cleanout(void);
56 const char *locale_file = "<stdout>";
58 rune_map maplower = { { 0 }, NULL };
59 rune_map mapupper = { { 0 }, NULL };
60 rune_map types = { { 0 }, NULL };
62 _FileRuneLocale new_locale = { "", "", {}, {}, {}, 0, 0, 0, 0 };
63 char *variable = NULL;
65 void set_map(rune_map *, rune_list *, uint32_t);
66 void set_digitmap(rune_map *, rune_list *);
67 void add_map(rune_map *, rune_list *, uint32_t);
68 static void usage(void);
107 entry : ENCODING STRING
108 { if (strcmp($2, "NONE") &&
109 strcmp($2, "ASCII") &&
110 strcmp($2, "UTF-8") &&
113 strcmp($2, "GB18030") &&
114 strcmp($2, "GB2312") &&
115 strcmp($2, "BIG5") &&
116 strcmp($2, "MSKanji"))
117 warnx("ENCODING %s is not supported by libc", $2);
118 strncpy(new_locale.encoding, $2,
119 sizeof(new_locale.encoding)); }
121 { new_locale.variable_len = strlen($1) + 1;
122 variable = xmalloc(new_locale.variable_len);
123 strcpy(variable, $1);
126 { warnx("the INVALID keyword is deprecated"); }
128 { set_map(&types, $2, $1); }
130 { set_map(&maplower, $2, 0); }
132 { set_map(&mapupper, $2, 0); }
134 { set_digitmap(&types, $2); }
139 $$ = (rune_list *)xmalloc(sizeof(rune_list));
146 $$ = (rune_list *)xmalloc(sizeof(rune_list));
153 $$ = (rune_list *)xmalloc(sizeof(rune_list));
158 | list RUNE THRU RUNE
160 $$ = (rune_list *)xmalloc(sizeof(rune_list));
167 map : LBRK RUNE RUNE RBRK
169 $$ = (rune_list *)xmalloc(sizeof(rune_list));
175 | map LBRK RUNE RUNE RBRK
177 $$ = (rune_list *)xmalloc(sizeof(rune_list));
183 | LBRK RUNE THRU RUNE ':' RUNE RBRK
185 $$ = (rune_list *)xmalloc(sizeof(rune_list));
191 | map LBRK RUNE THRU RUNE ':' RUNE RBRK
193 $$ = (rune_list *)xmalloc(sizeof(rune_list));
213 main(int ac, char *av[])
219 while ((x = getopt(ac, av, "do:")) != -1) {
225 locale_file = optarg;
226 if ((fp = fopen(locale_file, "w")) == NULL)
227 err(1, "%s", locale_file);
235 switch (ac - optind) {
239 if (freopen(av[optind], "r", stdin) == 0)
240 err(1, "%s", av[optind]);
245 for (x = 0; x < _CACHED_RUNES; ++x) {
249 memcpy(new_locale.magic, _FILE_RUNE_MAGIC_1, sizeof(new_locale.magic));
259 fprintf(stderr, "usage: mklocale [-d] [-o output] [source]\n");
264 yyerror(const char *s)
266 fprintf(stderr, "%s\n", s);
270 xmalloc(unsigned int sz)
272 void *r = malloc(sz);
279 xlalloc(unsigned int sz)
281 uint32_t *r = (uint32_t *)malloc(sz * sizeof(uint32_t));
288 xrelalloc(uint32_t *old, unsigned int sz)
290 uint32_t *r = (uint32_t *)realloc((char *)old,
291 sz * sizeof(uint32_t));
293 errx(1, "xrelalloc");
298 set_map(rune_map *map, rune_list *list, uint32_t flag)
301 rune_list *nlist = list->next;
302 add_map(map, list, flag);
308 set_digitmap(rune_map *map, rune_list *list)
313 rune_list *nlist = list->next;
314 for (i = list->min; i <= list->max; ++i) {
315 if (list->map + (i - list->min)) {
316 rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
319 add_map(map, tmp, list->map + (i - list->min));
328 add_map(rune_map *map, rune_list *list, uint32_t flag)
335 while (list->min < _CACHED_RUNES && list->min <= list->max) {
337 map->map[list->min++] |= flag;
339 map->map[list->min++] = list->map++;
342 if (list->min > list->max) {
347 run = list->max - list->min + 1;
349 if (!(r = map->root) || (list->max < r->min - 1)
350 || (!flag && list->max == r->min - 1)) {
352 list->types = xlalloc(run);
353 for (i = 0; i < run; ++i)
354 list->types[i] = flag;
356 list->next = map->root;
361 for (r = map->root; r && r->max + 1 < list->min; r = r->next)
366 * We are off the end.
369 list->types = xlalloc(run);
370 for (i = 0; i < run; ++i)
371 list->types[i] = flag;
378 if (list->max < r->min - 1) {
380 * We come before this range and we do not intersect it.
381 * We are not before the root node, it was checked before the loop
384 list->types = xlalloc(run);
385 for (i = 0; i < run; ++i)
386 list->types[i] = flag;
388 list->next = lr->next;
394 * At this point we have found that we at least intersect with
395 * the range pointed to by `r', we might intersect with one or
396 * more ranges beyond `r' as well.
399 if (!flag && list->map - list->min != r->map - r->min) {
401 * There are only two cases when we are doing case maps and
402 * our maps needn't have the same offset. When we are adjoining
403 * but not intersecting.
405 if (list->max + 1 == r->min) {
410 if (list->min - 1 == r->max) {
411 list->next = r->next;
415 errx(1, "error: conflicting map entries");
418 if (list->min >= r->min && list->max <= r->max) {
424 for (i = list->min; i <= list->max; ++i)
425 r->types[i - r->min] |= flag;
430 if (list->min <= r->min && list->max >= r->max) {
432 * Superset case. Make him big enough to hold us.
433 * We might need to merge with the guy after him.
436 list->types = xlalloc(list->max - list->min + 1);
438 for (i = list->min; i <= list->max; ++i)
439 list->types[i - list->min] = flag;
441 for (i = r->min; i <= r->max; ++i)
442 list->types[i - list->min] |= r->types[i - r->min];
445 r->types = list->types;
452 } else if (list->min < r->min) {
454 * Our tail intersects his head.
457 list->types = xlalloc(r->max - list->min + 1);
459 for (i = r->min; i <= r->max; ++i)
460 list->types[i - list->min] = r->types[i - r->min];
462 for (i = list->min; i < r->min; ++i)
463 list->types[i - list->min] = flag;
465 for (i = r->min; i <= list->max; ++i)
466 list->types[i - list->min] |= flag;
469 r->types = list->types;
478 * Our head intersects his tail.
479 * We might need to merge with the guy after him.
482 r->types = xrelalloc(r->types, list->max - r->min + 1);
484 for (i = list->min; i <= r->max; ++i)
485 r->types[i - r->min] |= flag;
487 for (i = r->max+1; i <= list->max; ++i)
488 r->types[i - r->min] = flag;
495 * Okay, check to see if we grew into the next guy(s)
497 while ((lr = r->next) && r->max >= lr->min) {
499 if (r->max >= lr->max) {
501 * Good, we consumed all of him.
503 for (i = lr->min; i <= lr->max; ++i)
504 r->types[i - r->min] |= lr->types[i - lr->min];
507 * "append" him on to the end of us.
509 r->types = xrelalloc(r->types, lr->max - r->min + 1);
511 for (i = lr->min; i <= r->max; ++i)
512 r->types[i - r->min] |= lr->types[i - lr->min];
514 for (i = r->max+1; i <= lr->max; ++i)
515 r->types[i - r->min] = lr->types[i - lr->min];
520 if (lr->max > r->max)
535 int x, first_d, curr_d;
539 * See if we can compress some of the istype arrays
541 for(list = types.root; list; list = list->next) {
542 list->map = list->types[0];
543 for (x = 1; x < list->max - list->min + 1; ++x) {
544 if ((int32_t)list->types[x] != list->map) {
551 first_d = curr_d = -1;
552 for (x = 0; x < _CACHED_RUNES; ++x) {
553 uint32_t r = types.map[x];
557 first_d = curr_d = x;
558 else if (x != curr_d + 1)
559 errx(1, "error: DIGIT range is not contiguous");
560 else if (x - first_d > 9)
561 errx(1, "error: DIGIT range is too big");
566 "error: DIGIT range is not a subset of XDIGIT range");
570 errx(1, "error: no DIGIT range defined in the single byte area");
571 else if (curr_d - first_d < 9)
572 errx(1, "error: DIGIT range is too small in the single byte area");
575 * Fill in our tables. Do this in network order so that
576 * diverse machines have a chance of sharing data.
577 * (Machines like Crays cannot share with little machines due to
578 * word size. Sigh. We tried.)
580 for (x = 0; x < _CACHED_RUNES; ++x) {
581 new_locale.runetype[x] = htonl(types.map[x]);
582 new_locale.maplower[x] = htonl(maplower.map[x]);
583 new_locale.mapupper[x] = htonl(mapupper.map[x]);
587 * Count up how many ranges we will need for each of the extents.
592 new_locale.runetype_ext_nranges++;
595 new_locale.runetype_ext_nranges =
596 htonl(new_locale.runetype_ext_nranges);
598 list = maplower.root;
601 new_locale.maplower_ext_nranges++;
604 new_locale.maplower_ext_nranges =
605 htonl(new_locale.maplower_ext_nranges);
607 list = mapupper.root;
610 new_locale.mapupper_ext_nranges++;
613 new_locale.mapupper_ext_nranges =
614 htonl(new_locale.mapupper_ext_nranges);
616 new_locale.variable_len = htonl(new_locale.variable_len);
619 * Okay, we are now ready to write the new locale file.
623 * PART 1: The _FileRuneLocale structure
625 if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
630 * PART 2: The runetype_ext structures (not the actual tables)
637 re.min = htonl(list->min);
638 re.max = htonl(list->max);
639 re.map = htonl(list->map);
641 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
649 * PART 3: The maplower_ext structures
651 list = maplower.root;
656 re.min = htonl(list->min);
657 re.max = htonl(list->max);
658 re.map = htonl(list->map);
660 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
668 * PART 4: The mapupper_ext structures
670 list = mapupper.root;
675 re.min = htonl(list->min);
676 re.max = htonl(list->max);
677 re.map = htonl(list->map);
679 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
687 * PART 5: The runetype_ext tables
692 for (x = 0; x < list->max - list->min + 1; ++x)
693 list->types[x] = htonl(list->types[x]);
696 if (fwrite((char *)list->types,
697 (list->max - list->min + 1) * sizeof(uint32_t),
706 * PART 6: And finally the variable data
708 if (new_locale.variable_len != 0 &&
709 fwrite(variable, ntohl(new_locale.variable_len), 1, fp) != 1) {
713 if (fclose(fp) != 0) {
722 if (new_locale.encoding[0])
723 fprintf(stderr, "ENCODING %s\n", new_locale.encoding);
725 fprintf(stderr, "VARIABLE %s\n", variable);
727 fprintf(stderr, "\nMAPLOWER:\n\n");
729 for (x = 0; x < _CACHED_RUNES; ++x) {
730 if (isprint(maplower.map[x]))
731 fprintf(stderr, " '%c'", (int)maplower.map[x]);
732 else if (maplower.map[x])
733 fprintf(stderr, "%04x", maplower.map[x]);
735 fprintf(stderr, "%4x", 0);
736 if ((x & 0xf) == 0xf)
737 fprintf(stderr, "\n");
739 fprintf(stderr, " ");
741 fprintf(stderr, "\n");
743 for (list = maplower.root; list; list = list->next)
744 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
746 fprintf(stderr, "\nMAPUPPER:\n\n");
748 for (x = 0; x < _CACHED_RUNES; ++x) {
749 if (isprint(mapupper.map[x]))
750 fprintf(stderr, " '%c'", (int)mapupper.map[x]);
751 else if (mapupper.map[x])
752 fprintf(stderr, "%04x", mapupper.map[x]);
754 fprintf(stderr, "%4x", 0);
755 if ((x & 0xf) == 0xf)
756 fprintf(stderr, "\n");
758 fprintf(stderr, " ");
760 fprintf(stderr, "\n");
762 for (list = mapupper.root; list; list = list->next)
763 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
766 fprintf(stderr, "\nTYPES:\n\n");
768 for (x = 0; x < _CACHED_RUNES; ++x) {
769 uint32_t r = types.map[x];
773 fprintf(stderr, " '%c': %2d", x, (int)(r & 0xff));
775 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
777 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
778 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
779 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
780 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
781 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
782 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
783 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
784 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
785 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
786 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
787 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
788 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
789 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
790 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
791 fprintf(stderr, "\n");
795 for (list = types.root; list; list = list->next) {
796 if (list->map && list->min + 3 < list->max) {
797 uint32_t r = list->map;
799 fprintf(stderr, "%04x: %2d",
800 (uint32_t)list->min, (int)(r & 0xff));
802 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
803 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
804 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
805 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
806 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
807 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
808 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
809 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
810 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
811 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
812 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
813 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
814 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
815 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
816 fprintf(stderr, "\n...\n");
818 fprintf(stderr, "%04x: %2d",
819 (uint32_t)list->max, (int)(r & 0xff));
821 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
822 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
823 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
824 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
825 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
826 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
827 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
828 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
829 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
830 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
831 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
832 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
833 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
834 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
835 fprintf(stderr, "\n");
837 for (x = list->min; x <= list->max; ++x) {
838 uint32_t r = ntohl(list->types[x - list->min]);
841 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
843 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
844 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
845 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
846 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
847 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
848 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
849 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
850 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
851 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
852 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
853 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
854 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
855 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
856 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
857 fprintf(stderr, "\n");