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
37 * @(#)yacc.y 8.1 (Berkeley) 6/6/93
38 * $FreeBSD: src/usr.bin/mklocale/yacc.y,v 1.6.2.3 2003/06/03 21:15:48 ache Exp $
39 * $DragonFly: src/usr.bin/mklocale/yacc.y,v 1.3 2004/07/22 14:30:07 hmp Exp $
42 #include <sys/types.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
58 static void *xmalloc(unsigned int sz);
59 static unsigned long *xlalloc(unsigned int sz);
60 void yyerror(const char *s);
61 static unsigned long *xrelalloc(unsigned long *old, unsigned int sz);
62 static void dump_tables(void);
63 static void cleanout(void);
65 const char *locale_file = "<stdout>";
67 rune_map maplower = { { 0 }, NULL };
68 rune_map mapupper = { { 0 }, NULL };
69 rune_map types = { { 0 }, NULL };
71 _RuneLocale new_locale = { "", "", NULL, NULL, 0, {}, {}, {},
72 {0, NULL}, {0, NULL}, {0, NULL}, NULL, 0 };
74 void set_map(rune_map *, rune_list *, unsigned long);
75 void set_digitmap(rune_map *, rune_list *);
76 void add_map(rune_map *, rune_list *, unsigned long);
115 entry : ENCODING STRING
116 { if (strcmp($2, "NONE") &&
117 strcmp($2, "UTF2") &&
118 strcmp($2, "UTF-8") &&
120 strcmp($2, "BIG5") &&
121 strcmp($2, "MSKanji"))
122 warnx("ENCODING %s is not supported by libc", $2);
123 strncpy(new_locale.encoding, $2, sizeof(new_locale.encoding)); }
125 { new_locale.variable_len = strlen($1) + 1;
126 new_locale.variable = malloc(new_locale.variable_len);
127 strcpy((char *)new_locale.variable, $1);
130 { warnx("the INVALID keyword is deprecated");
131 new_locale.invalid_rune = $2;
134 { set_map(&types, $2, $1); }
136 { set_map(&maplower, $2, 0); }
138 { set_map(&mapupper, $2, 0); }
140 { set_digitmap(&types, $2); }
145 $$ = (rune_list *)malloc(sizeof(rune_list));
152 $$ = (rune_list *)malloc(sizeof(rune_list));
159 $$ = (rune_list *)malloc(sizeof(rune_list));
164 | list RUNE THRU RUNE
166 $$ = (rune_list *)malloc(sizeof(rune_list));
173 map : LBRK RUNE RUNE RBRK
175 $$ = (rune_list *)malloc(sizeof(rune_list));
181 | map LBRK RUNE RUNE RBRK
183 $$ = (rune_list *)malloc(sizeof(rune_list));
189 | LBRK RUNE THRU RUNE ':' RUNE RBRK
191 $$ = (rune_list *)malloc(sizeof(rune_list));
197 | map LBRK RUNE THRU RUNE ':' RUNE RBRK
199 $$ = (rune_list *)malloc(sizeof(rune_list));
219 main(int ac, char *av[])
227 while ((x = getopt(ac, av, "do:")) != EOF) {
233 locale_file = optarg;
234 if ((fp = fopen(locale_file, "w")) == 0) {
242 fprintf(stderr, "usage: mklocale [-d] [-o output] [source]\n");
247 switch (ac - optind) {
251 if (freopen(av[optind], "r", stdin) == 0) {
259 for (x = 0; x < _CACHED_RUNES; ++x) {
263 new_locale.invalid_rune = _INVALID_RUNE;
264 memcpy(new_locale.magic, _RUNE_MAGIC_1, sizeof(new_locale.magic));
275 fprintf(stderr, "%s\n", s);
282 void *r = malloc(sz);
290 static unsigned long *
294 unsigned long *r = (unsigned long *)malloc(sz * sizeof(unsigned long));
302 static unsigned long *
307 unsigned long *r = (unsigned long *)realloc((char *)old,
308 sz * sizeof(unsigned long));
317 set_map(map, list, flag)
323 rune_list *nlist = list->next;
324 add_map(map, list, flag);
330 set_digitmap(map, list)
337 rune_list *nlist = list->next;
338 for (i = list->min; i <= list->max; ++i) {
339 if (list->map + (i - list->min)) {
340 rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
343 add_map(map, tmp, list->map + (i - list->min));
352 add_map(map, list, flag)
362 while (list->min < _CACHED_RUNES && list->min <= list->max) {
364 map->map[list->min++] |= flag;
366 map->map[list->min++] = list->map++;
369 if (list->min > list->max) {
374 run = list->max - list->min + 1;
376 if (!(r = map->root) || (list->max < r->min - 1)
377 || (!flag && list->max == r->min - 1)) {
379 list->types = xlalloc(run);
380 for (i = 0; i < run; ++i)
381 list->types[i] = flag;
383 list->next = map->root;
388 for (r = map->root; r && r->max + 1 < list->min; r = r->next)
393 * We are off the end.
396 list->types = xlalloc(run);
397 for (i = 0; i < run; ++i)
398 list->types[i] = flag;
405 if (list->max < r->min - 1) {
407 * We come before this range and we do not intersect it.
408 * We are not before the root node, it was checked before the loop
411 list->types = xlalloc(run);
412 for (i = 0; i < run; ++i)
413 list->types[i] = flag;
415 list->next = lr->next;
421 * At this point we have found that we at least intersect with
422 * the range pointed to by `r', we might intersect with one or
423 * more ranges beyond `r' as well.
426 if (!flag && list->map - list->min != r->map - r->min) {
428 * There are only two cases when we are doing case maps and
429 * our maps needn't have the same offset. When we are adjoining
430 * but not intersecting.
432 if (list->max + 1 == r->min) {
437 if (list->min - 1 == r->max) {
438 list->next = r->next;
442 fprintf(stderr, "Error: conflicting map entries\n");
446 if (list->min >= r->min && list->max <= r->max) {
452 for (i = list->min; i <= list->max; ++i)
453 r->types[i - r->min] |= flag;
458 if (list->min <= r->min && list->max >= r->max) {
460 * Superset case. Make him big enough to hold us.
461 * We might need to merge with the guy after him.
464 list->types = xlalloc(list->max - list->min + 1);
466 for (i = list->min; i <= list->max; ++i)
467 list->types[i - list->min] = flag;
469 for (i = r->min; i <= r->max; ++i)
470 list->types[i - list->min] |= r->types[i - r->min];
473 r->types = list->types;
480 } else if (list->min < r->min) {
482 * Our tail intersects his head.
485 list->types = xlalloc(r->max - list->min + 1);
487 for (i = r->min; i <= r->max; ++i)
488 list->types[i - list->min] = r->types[i - r->min];
490 for (i = list->min; i < r->min; ++i)
491 list->types[i - list->min] = flag;
493 for (i = r->min; i <= list->max; ++i)
494 list->types[i - list->min] |= flag;
497 r->types = list->types;
506 * Our head intersects his tail.
507 * We might need to merge with the guy after him.
510 r->types = xrelalloc(r->types, list->max - r->min + 1);
512 for (i = list->min; i <= r->max; ++i)
513 r->types[i - r->min] |= flag;
515 for (i = r->max+1; i <= list->max; ++i)
516 r->types[i - r->min] = flag;
523 * Okay, check to see if we grew into the next guy(s)
525 while ((lr = r->next) && r->max >= lr->min) {
527 if (r->max >= lr->max) {
529 * Good, we consumed all of him.
531 for (i = lr->min; i <= lr->max; ++i)
532 r->types[i - r->min] |= lr->types[i - lr->min];
535 * "append" him on to the end of us.
537 r->types = xrelalloc(r->types, lr->max - r->min + 1);
539 for (i = lr->min; i <= r->max; ++i)
540 r->types[i - r->min] |= lr->types[i - lr->min];
542 for (i = r->max+1; i <= lr->max; ++i)
543 r->types[i - r->min] = lr->types[i - lr->min];
548 if (lr->max > r->max)
563 int x, first_d, curr_d;
567 * See if we can compress some of the istype arrays
569 for(list = types.root; list; list = list->next) {
570 list->map = list->types[0];
571 for (x = 1; x < list->max - list->min + 1; ++x) {
572 if ((rune_t)list->types[x] != list->map) {
580 for (x = 0; x < _CACHED_RUNES; ++x) {
581 unsigned long r = types.map[x];
585 first_d = curr_d = x;
586 else if (x != curr_d + 1) {
587 fprintf(stderr, "Error: DIGIT range is not contiguous\n");
589 } else if (x - first_d > 9) {
590 fprintf(stderr, "Error: DIGIT range is too big\n");
594 if (!(r & _CTYPE_X)) {
595 fprintf(stderr, "Error: DIGIT range is not a subset of XDIGIT range\n");
601 fprintf(stderr, "Error: no DIGIT range defined in the single byte area\n");
603 } else if (curr_d - first_d < 9) {
604 fprintf(stderr, "Error: DIGIT range is too small in the single byte area\n");
608 new_locale.invalid_rune = htonl(new_locale.invalid_rune);
611 * Fill in our tables. Do this in network order so that
612 * diverse machines have a chance of sharing data.
613 * (Machines like Crays cannot share with little machines due to
614 * word size. Sigh. We tried.)
616 for (x = 0; x < _CACHED_RUNES; ++x) {
617 new_locale.runetype[x] = htonl(types.map[x]);
618 new_locale.maplower[x] = htonl(maplower.map[x]);
619 new_locale.mapupper[x] = htonl(mapupper.map[x]);
623 * Count up how many ranges we will need for each of the extents.
628 new_locale.runetype_ext.nranges++;
631 new_locale.runetype_ext.nranges = htonl(new_locale.runetype_ext.nranges);
633 list = maplower.root;
636 new_locale.maplower_ext.nranges++;
639 new_locale.maplower_ext.nranges = htonl(new_locale.maplower_ext.nranges);
641 list = mapupper.root;
644 new_locale.mapupper_ext.nranges++;
647 new_locale.mapupper_ext.nranges = htonl(new_locale.mapupper_ext.nranges);
649 new_locale.variable_len = htonl(new_locale.variable_len);
652 * Okay, we are now ready to write the new locale file.
656 * PART 1: The _RuneLocale structure
658 if (fwrite((char *)&new_locale, sizeof(new_locale), 1, fp) != 1) {
663 * PART 2: The runetype_ext structures (not the actual tables)
670 re.min = htonl(list->min);
671 re.max = htonl(list->max);
672 re.map = htonl(list->map);
674 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
682 * PART 3: The maplower_ext structures
684 list = maplower.root;
689 re.min = htonl(list->min);
690 re.max = htonl(list->max);
691 re.map = htonl(list->map);
693 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
701 * PART 4: The mapupper_ext structures
703 list = mapupper.root;
708 re.min = htonl(list->min);
709 re.max = htonl(list->max);
710 re.map = htonl(list->map);
712 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1) {
720 * PART 5: The runetype_ext tables
725 for (x = 0; x < list->max - list->min + 1; ++x)
726 list->types[x] = htonl(list->types[x]);
729 if (fwrite((char *)list->types,
730 (list->max - list->min + 1) * sizeof(unsigned long),
739 * PART 5: And finally the variable data
741 if (fwrite((char *)new_locale.variable,
742 ntohl(new_locale.variable_len), 1, fp) != 1) {
746 if (fclose(fp) != 0) {
755 if (new_locale.encoding[0])
756 fprintf(stderr, "ENCODING %s\n", new_locale.encoding);
757 if (new_locale.variable)
758 fprintf(stderr, "VARIABLE %s\n", (char *)new_locale.variable);
760 fprintf(stderr, "\nMAPLOWER:\n\n");
762 for (x = 0; x < _CACHED_RUNES; ++x) {
763 if (isprint(maplower.map[x]))
764 fprintf(stderr, " '%c'", (int)maplower.map[x]);
765 else if (maplower.map[x])
766 fprintf(stderr, "%04lx", maplower.map[x]);
768 fprintf(stderr, "%4x", 0);
769 if ((x & 0xf) == 0xf)
770 fprintf(stderr, "\n");
772 fprintf(stderr, " ");
774 fprintf(stderr, "\n");
776 for (list = maplower.root; list; list = list->next)
777 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
779 fprintf(stderr, "\nMAPUPPER:\n\n");
781 for (x = 0; x < _CACHED_RUNES; ++x) {
782 if (isprint(mapupper.map[x]))
783 fprintf(stderr, " '%c'", (int)mapupper.map[x]);
784 else if (mapupper.map[x])
785 fprintf(stderr, "%04lx", mapupper.map[x]);
787 fprintf(stderr, "%4x", 0);
788 if ((x & 0xf) == 0xf)
789 fprintf(stderr, "\n");
791 fprintf(stderr, " ");
793 fprintf(stderr, "\n");
795 for (list = mapupper.root; list; list = list->next)
796 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
799 fprintf(stderr, "\nTYPES:\n\n");
801 for (x = 0; x < _CACHED_RUNES; ++x) {
802 unsigned long r = types.map[x];
806 fprintf(stderr, " '%c': %2d", x, (int)(r & 0xff));
808 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
810 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
811 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
812 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
813 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
814 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
815 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
816 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
817 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
818 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
819 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
820 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
821 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
822 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
823 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
824 fprintf(stderr, "\n");
828 for (list = types.root; list; list = list->next) {
829 if (list->map && list->min + 3 < list->max) {
830 unsigned long r = list->map;
832 fprintf(stderr, "%04lx: %2d",
833 (unsigned long)list->min, (int)(r & 0xff));
835 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
836 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
837 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
838 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
839 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
840 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
841 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
842 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
843 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
844 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
845 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
846 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
847 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
848 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
849 fprintf(stderr, "\n...\n");
851 fprintf(stderr, "%04lx: %2d",
852 (unsigned long)list->max, (int)(r & 0xff));
854 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
855 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
856 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
857 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
858 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
859 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
860 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
861 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
862 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
863 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
864 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
865 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
866 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
867 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
868 fprintf(stderr, "\n");
870 for (x = list->min; x <= list->max; ++x) {
871 unsigned long r = ntohl(list->types[x - list->min]);
874 fprintf(stderr, "%04x: %2d", x, (int)(r & 0xff));
876 fprintf(stderr, " %4s", (r & _CTYPE_A) ? "alph" : "");
877 fprintf(stderr, " %4s", (r & _CTYPE_C) ? "ctrl" : "");
878 fprintf(stderr, " %4s", (r & _CTYPE_D) ? "dig" : "");
879 fprintf(stderr, " %4s", (r & _CTYPE_G) ? "graf" : "");
880 fprintf(stderr, " %4s", (r & _CTYPE_L) ? "low" : "");
881 fprintf(stderr, " %4s", (r & _CTYPE_P) ? "punc" : "");
882 fprintf(stderr, " %4s", (r & _CTYPE_S) ? "spac" : "");
883 fprintf(stderr, " %4s", (r & _CTYPE_U) ? "upp" : "");
884 fprintf(stderr, " %4s", (r & _CTYPE_X) ? "xdig" : "");
885 fprintf(stderr, " %4s", (r & _CTYPE_B) ? "blnk" : "");
886 fprintf(stderr, " %4s", (r & _CTYPE_R) ? "prnt" : "");
887 fprintf(stderr, " %4s", (r & _CTYPE_I) ? "ideo" : "");
888 fprintf(stderr, " %4s", (r & _CTYPE_T) ? "spec" : "");
889 fprintf(stderr, " %4s", (r & _CTYPE_Q) ? "phon" : "");
890 fprintf(stderr, "\n");