style: compare return value of getopt() against -1 rather than EOF
[games.git] / usr.bin / mklocale / yacc.y
CommitLineData
32a7b49a 1/* $NetBSD: src/usr.bin/mklocale/yacc.y,v 1.24 2004/01/05 23:23:36 jmmv Exp $ */
9b0ec895 2/* $DragonFly: src/usr.bin/mklocale/yacc.y,v 1.8 2008/07/10 18:29:52 swildner Exp $ */
32a7b49a 3
984263bc
MD
4%{
5/*-
6 * Copyright (c) 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Paul Borman at Krystal Technologies.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
32a7b49a 20 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
2a4d0709 37#include <sys/types.h>
32a7b49a 38#include <netinet/in.h> /* Needed for htonl on POSIX systems. */
984263bc 39
32a7b49a 40#include <ctype.h>
984263bc 41#include <err.h>
984263bc
MD
42#include <stddef.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
32a7b49a 48#include "locale/runetype.h"
1e4f9932 49
984263bc 50#include "ldef.h"
984263bc
MD
51
52const char *locale_file = "<stdout>";
53
32a7b49a
JS
54rune_map maplower = { { 0, }, NULL };
55rune_map mapupper = { { 0, }, NULL };
56rune_map types = { { 0, }, NULL };
57
58_NBRuneLocale new_locale;
59
60__nbrune_t charsetbits = (__nbrune_t)0x00000000;
61#if 0
62__nbrune_t charsetmask = (__nbrune_t)0x0000007f;
63#endif
64__nbrune_t charsetmask = (__nbrune_t)0xffffffff;
65
66void set_map(rune_map *, rune_list *, uint32_t);
67void set_digitmap(rune_map *, rune_list *);
68void add_map(rune_map *, rune_list *, uint32_t);
69
70int main(int, char *[]);
71int yyerror(const char *s);
72void *xmalloc(unsigned int sz);
73uint32_t *xlalloc(unsigned int sz);
74uint32_t *xrelalloc(uint32_t *old, unsigned int sz);
75void dump_tables(void);
76int yyparse(void);
77extern int yylex(void);
984263bc
MD
78%}
79
80%union {
32a7b49a 81 __nbrune_t rune;
984263bc
MD
82 int i;
83 char *str;
84
85 rune_list *list;
86}
87
88%token <rune> RUNE
89%token LBRK
90%token RBRK
91%token THRU
92%token MAPLOWER
93%token MAPUPPER
94%token DIGITMAP
95%token <i> LIST
96%token <str> VARIABLE
32a7b49a 97%token CHARSET
984263bc 98%token ENCODING
32a7b49a 99%token INVALID
984263bc
MD
100%token <str> STRING
101
102%type <list> list
103%type <list> map
104
105
106%%
107
108locale : /* empty */
109 | table
110 { dump_tables(); }
111 ;
112
113table : entry
114 | table entry
115 ;
116
117entry : ENCODING STRING
32a7b49a 118 { strncpy(new_locale.rl_encoding, $2, sizeof(new_locale.rl_encoding)); }
984263bc 119 | VARIABLE
32a7b49a
JS
120 { new_locale.rl_variable_len = strlen($1) + 1;
121 new_locale.rl_variable = strdup($1);
122 }
123 | CHARSET RUNE
124 { charsetbits = $2; charsetmask = 0x0000007f; }
125 | CHARSET RUNE RUNE
126 { charsetbits = $2; charsetmask = $3; }
127 | CHARSET STRING
128 { int final = $2[strlen($2) - 1] & 0x7f;
129 charsetbits = final << 24;
130 if ($2[0] == '$') {
131 charsetmask = 0x00007f7f;
132 if (strchr(",-./", $2[1]))
133 charsetbits |= 0x80;
134 if (0xd0 <= final && final <= 0xdf)
135 charsetmask |= 0x007f0000;
136 } else {
137 charsetmask = 0x0000007f;
138 if (strchr(",-./", $2[0]))
139 charsetbits |= 0x80;
140 if (strlen($2) == 2 && $2[0] == '!')
141 charsetbits |= ((0x80 | $2[0]) << 16);
142 }
143
144 /*
145 * special rules
146 */
147 if (charsetbits == ('B' << 24)
148 && charsetmask == 0x0000007f) {
149 /*ASCII: 94B*/
150 charsetbits = 0;
151 charsetmask = 0x0000007f;
152 } else if (charsetbits == (('A' << 24) | 0x80)
153 && charsetmask == 0x0000007f) {
154 /*Latin1: 96A*/
155 charsetbits = 0x80;
156 charsetmask = 0x0000007f;
157 }
984263bc 158 }
32a7b49a
JS
159 | INVALID RUNE
160 { new_locale.rl_invalid_rune = $2; }
984263bc
MD
161 | LIST list
162 { set_map(&types, $2, $1); }
163 | MAPLOWER map
164 { set_map(&maplower, $2, 0); }
165 | MAPUPPER map
166 { set_map(&mapupper, $2, 0); }
167 | DIGITMAP map
168 { set_digitmap(&types, $2); }
169 ;
170
171list : RUNE
172 {
173 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
174 $$->min = ($1 & charsetmask) | charsetbits;
175 $$->max = ($1 & charsetmask) | charsetbits;
984263bc
MD
176 $$->next = 0;
177 }
178 | RUNE THRU RUNE
179 {
180 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
181 $$->min = ($1 & charsetmask) | charsetbits;
182 $$->max = ($3 & charsetmask) | charsetbits;
984263bc
MD
183 $$->next = 0;
184 }
185 | list RUNE
186 {
187 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
188 $$->min = ($2 & charsetmask) | charsetbits;
189 $$->max = ($2 & charsetmask) | charsetbits;
984263bc
MD
190 $$->next = $1;
191 }
192 | list RUNE THRU RUNE
193 {
194 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
195 $$->min = ($2 & charsetmask) | charsetbits;
196 $$->max = ($4 & charsetmask) | charsetbits;
984263bc
MD
197 $$->next = $1;
198 }
199 ;
200
201map : LBRK RUNE RUNE RBRK
202 {
203 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
204 $$->min = ($2 & charsetmask) | charsetbits;
205 $$->max = ($2 & charsetmask) | charsetbits;
984263bc
MD
206 $$->map = $3;
207 $$->next = 0;
208 }
209 | map LBRK RUNE RUNE RBRK
210 {
211 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
212 $$->min = ($3 & charsetmask) | charsetbits;
213 $$->max = ($3 & charsetmask) | charsetbits;
984263bc
MD
214 $$->map = $4;
215 $$->next = $1;
216 }
217 | LBRK RUNE THRU RUNE ':' RUNE RBRK
218 {
219 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
220 $$->min = ($2 & charsetmask) | charsetbits;
221 $$->max = ($4 & charsetmask) | charsetbits;
984263bc
MD
222 $$->map = $6;
223 $$->next = 0;
224 }
225 | map LBRK RUNE THRU RUNE ':' RUNE RBRK
226 {
227 $$ = (rune_list *)malloc(sizeof(rune_list));
32a7b49a
JS
228 $$->min = ($3 & charsetmask) | charsetbits;
229 $$->max = ($5 & charsetmask) | charsetbits;
984263bc
MD
230 $$->map = $7;
231 $$->next = $1;
232 }
233 ;
234%%
235
32a7b49a
JS
236int debug = 0;
237FILE *ofile;
984263bc
MD
238
239int
240main(int ac, char *av[])
241{
242 int x;
243
9b0ec895 244 while ((x = getopt(ac, av, "do:")) != -1) {
984263bc
MD
245 switch(x) {
246 case 'd':
247 debug = 1;
248 break;
249 case 'o':
250 locale_file = optarg;
32a7b49a
JS
251 if ((ofile = fopen(locale_file, "w")) == 0)
252 err(1, "unable to open output file %s", locale_file);
984263bc
MD
253 break;
254 default:
255 usage:
256 fprintf(stderr, "usage: mklocale [-d] [-o output] [source]\n");
257 exit(1);
258 }
259 }
260
261 switch (ac - optind) {
262 case 0:
263 break;
264 case 1:
32a7b49a
JS
265 if (freopen(av[optind], "r", stdin) == 0)
266 err(1, "unable to open input file %s", av[optind]);
984263bc
MD
267 break;
268 default:
269 goto usage;
270 }
32a7b49a 271 for (x = 0; x < _NB_CACHED_RUNES; ++x) {
984263bc
MD
272 mapupper.map[x] = x;
273 maplower.map[x] = x;
274 }
32a7b49a
JS
275 new_locale.rl_invalid_rune = _NB_DEFAULT_INVALID_RUNE;
276 memcpy(new_locale.rl_magic, _NB_RUNE_MAGIC_1, sizeof(new_locale.rl_magic));
984263bc
MD
277
278 yyparse();
279
32a7b49a 280 return 0;
984263bc
MD
281}
282
32a7b49a
JS
283int
284yyerror(const char *s)
984263bc
MD
285{
286 fprintf(stderr, "%s\n", s);
32a7b49a
JS
287
288 return 0;
984263bc
MD
289}
290
32a7b49a
JS
291void *
292xmalloc(unsigned int sz)
984263bc
MD
293{
294 void *r = malloc(sz);
295 if (!r) {
296 perror("xmalloc");
32a7b49a 297 abort();
984263bc
MD
298 }
299 return(r);
300}
301
32a7b49a
JS
302uint32_t *
303xlalloc(unsigned int sz)
984263bc 304{
32a7b49a 305 uint32_t *r = (uint32_t *)malloc(sz * sizeof(uint32_t));
984263bc
MD
306 if (!r) {
307 perror("xlalloc");
32a7b49a 308 abort();
984263bc
MD
309 }
310 return(r);
311}
312
32a7b49a
JS
313uint32_t *
314xrelalloc(uint32_t *old, unsigned int sz)
984263bc 315{
32a7b49a 316 uint32_t *r = (uint32_t *)realloc((char *)old, sz * sizeof(uint32_t));
984263bc
MD
317 if (!r) {
318 perror("xrelalloc");
32a7b49a 319 abort();
984263bc
MD
320 }
321 return(r);
322}
323
324void
32a7b49a 325set_map(rune_map *map, rune_list *list, uint32_t flag)
984263bc 326{
32a7b49a
JS
327 list->map &= charsetmask;
328 list->map |= charsetbits;
984263bc
MD
329 while (list) {
330 rune_list *nlist = list->next;
331 add_map(map, list, flag);
332 list = nlist;
333 }
334}
335
336void
32a7b49a 337set_digitmap(rune_map *map, rune_list *list)
984263bc 338{
32a7b49a 339 __nbrune_t i;
984263bc
MD
340
341 while (list) {
342 rune_list *nlist = list->next;
343 for (i = list->min; i <= list->max; ++i) {
344 if (list->map + (i - list->min)) {
345 rune_list *tmp = (rune_list *)xmalloc(sizeof(rune_list));
346 tmp->min = i;
347 tmp->max = i;
348 add_map(map, tmp, list->map + (i - list->min));
349 }
350 }
351 free(list);
352 list = nlist;
353 }
354}
355
356void
32a7b49a 357add_map(rune_map *map, rune_list *list, uint32_t flag)
984263bc 358{
32a7b49a 359 __nbrune_t i;
984263bc
MD
360 rune_list *lr = 0;
361 rune_list *r;
32a7b49a 362 __nbrune_t run;
984263bc 363
32a7b49a 364 while (list->min < _NB_CACHED_RUNES && list->min <= list->max) {
984263bc
MD
365 if (flag)
366 map->map[list->min++] |= flag;
367 else
368 map->map[list->min++] = list->map++;
369 }
370
371 if (list->min > list->max) {
372 free(list);
373 return;
374 }
375
376 run = list->max - list->min + 1;
377
378 if (!(r = map->root) || (list->max < r->min - 1)
379 || (!flag && list->max == r->min - 1)) {
380 if (flag) {
381 list->types = xlalloc(run);
382 for (i = 0; i < run; ++i)
383 list->types[i] = flag;
384 }
385 list->next = map->root;
386 map->root = list;
387 return;
388 }
389
390 for (r = map->root; r && r->max + 1 < list->min; r = r->next)
391 lr = r;
392
393 if (!r) {
394 /*
395 * We are off the end.
396 */
397 if (flag) {
398 list->types = xlalloc(run);
399 for (i = 0; i < run; ++i)
400 list->types[i] = flag;
401 }
402 list->next = 0;
403 lr->next = list;
404 return;
405 }
406
407 if (list->max < r->min - 1) {
408 /*
409 * We come before this range and we do not intersect it.
410 * We are not before the root node, it was checked before the loop
411 */
412 if (flag) {
413 list->types = xlalloc(run);
414 for (i = 0; i < run; ++i)
415 list->types[i] = flag;
416 }
417 list->next = lr->next;
418 lr->next = list;
419 return;
420 }
421
422 /*
423 * At this point we have found that we at least intersect with
424 * the range pointed to by `r', we might intersect with one or
425 * more ranges beyond `r' as well.
426 */
427
428 if (!flag && list->map - list->min != r->map - r->min) {
429 /*
430 * There are only two cases when we are doing case maps and
431 * our maps needn't have the same offset. When we are adjoining
432 * but not intersecting.
433 */
434 if (list->max + 1 == r->min) {
435 lr->next = list;
436 list->next = r;
437 return;
438 }
439 if (list->min - 1 == r->max) {
440 list->next = r->next;
441 r->next = list;
442 return;
443 }
444 fprintf(stderr, "Error: conflicting map entries\n");
445 exit(1);
446 }
447
448 if (list->min >= r->min && list->max <= r->max) {
449 /*
450 * Subset case.
451 */
452
453 if (flag) {
454 for (i = list->min; i <= list->max; ++i)
455 r->types[i - r->min] |= flag;
456 }
457 free(list);
458 return;
459 }
460 if (list->min <= r->min && list->max >= r->max) {
461 /*
462 * Superset case. Make him big enough to hold us.
463 * We might need to merge with the guy after him.
464 */
465 if (flag) {
466 list->types = xlalloc(list->max - list->min + 1);
467
468 for (i = list->min; i <= list->max; ++i)
469 list->types[i - list->min] = flag;
470
471 for (i = r->min; i <= r->max; ++i)
472 list->types[i - list->min] |= r->types[i - r->min];
473
474 free(r->types);
475 r->types = list->types;
476 } else {
477 r->map = list->map;
478 }
479 r->min = list->min;
480 r->max = list->max;
481 free(list);
482 } else if (list->min < r->min) {
483 /*
484 * Our tail intersects his head.
485 */
486 if (flag) {
487 list->types = xlalloc(r->max - list->min + 1);
488
489 for (i = r->min; i <= r->max; ++i)
490 list->types[i - list->min] = r->types[i - r->min];
491
492 for (i = list->min; i < r->min; ++i)
493 list->types[i - list->min] = flag;
494
495 for (i = r->min; i <= list->max; ++i)
496 list->types[i - list->min] |= flag;
497
498 free(r->types);
499 r->types = list->types;
500 } else {
501 r->map = list->map;
502 }
503 r->min = list->min;
504 free(list);
505 return;
506 } else {
507 /*
508 * Our head intersects his tail.
509 * We might need to merge with the guy after him.
510 */
511 if (flag) {
512 r->types = xrelalloc(r->types, list->max - r->min + 1);
513
514 for (i = list->min; i <= r->max; ++i)
515 r->types[i - r->min] |= flag;
516
517 for (i = r->max+1; i <= list->max; ++i)
518 r->types[i - r->min] = flag;
519 }
520 r->max = list->max;
521 free(list);
522 }
523
524 /*
525 * Okay, check to see if we grew into the next guy(s)
526 */
527 while ((lr = r->next) && r->max >= lr->min) {
528 if (flag) {
529 if (r->max >= lr->max) {
530 /*
531 * Good, we consumed all of him.
532 */
533 for (i = lr->min; i <= lr->max; ++i)
534 r->types[i - r->min] |= lr->types[i - lr->min];
535 } else {
536 /*
537 * "append" him on to the end of us.
538 */
539 r->types = xrelalloc(r->types, lr->max - r->min + 1);
540
541 for (i = lr->min; i <= r->max; ++i)
542 r->types[i - r->min] |= lr->types[i - lr->min];
543
544 for (i = r->max+1; i <= lr->max; ++i)
545 r->types[i - r->min] = lr->types[i - lr->min];
546
547 r->max = lr->max;
548 }
549 } else {
550 if (lr->max > r->max)
551 r->max = lr->max;
552 }
553
554 r->next = lr->next;
555
556 if (flag)
557 free(lr->types);
558 free(lr);
559 }
560}
561
32a7b49a
JS
562void
563dump_tables(void)
984263bc 564{
32a7b49a
JS
565 int n;
566 __nbrune_t x;
984263bc 567 rune_list *list;
32a7b49a
JS
568 _FileRuneLocale file_new_locale;
569 FILE *fp = (ofile ? ofile : stdout);
570
571 memset(&file_new_locale, 0, sizeof(file_new_locale));
984263bc
MD
572
573 /*
574 * See if we can compress some of the istype arrays
575 */
576 for(list = types.root; list; list = list->next) {
577 list->map = list->types[0];
578 for (x = 1; x < list->max - list->min + 1; ++x) {
32a7b49a 579 if (list->types[x] != list->map) {
984263bc
MD
580 list->map = 0;
581 break;
582 }
583 }
584 }
585
32a7b49a
JS
586 memcpy(&file_new_locale.frl_magic, new_locale.rl_magic,
587 sizeof(file_new_locale.frl_magic));
588 memcpy(&file_new_locale.frl_encoding, new_locale.rl_encoding,
589 sizeof(file_new_locale.frl_encoding));
590
591 file_new_locale.frl_invalid_rune = htonl(new_locale.rl_invalid_rune);
984263bc 592
984263bc
MD
593 /*
594 * Fill in our tables. Do this in network order so that
595 * diverse machines have a chance of sharing data.
596 * (Machines like Crays cannot share with little machines due to
597 * word size. Sigh. We tried.)
598 */
32a7b49a
JS
599 for (x = 0; x < _NB_CACHED_RUNES; ++x) {
600 file_new_locale.frl_runetype[x] = htonl(types.map[x]);
601 file_new_locale.frl_maplower[x] = htonl(maplower.map[x]);
602 file_new_locale.frl_mapupper[x] = htonl(mapupper.map[x]);
984263bc
MD
603 }
604
605 /*
606 * Count up how many ranges we will need for each of the extents.
607 */
608 list = types.root;
609
610 while (list) {
32a7b49a 611 new_locale.rl_runetype_ext.rr_nranges++;
984263bc
MD
612 list = list->next;
613 }
32a7b49a
JS
614 file_new_locale.frl_runetype_ext.frr_nranges =
615 htonl(new_locale.rl_runetype_ext.rr_nranges);
984263bc
MD
616
617 list = maplower.root;
618
619 while (list) {
32a7b49a 620 new_locale.rl_maplower_ext.rr_nranges++;
984263bc
MD
621 list = list->next;
622 }
32a7b49a
JS
623 file_new_locale.frl_maplower_ext.frr_nranges =
624 htonl(new_locale.rl_maplower_ext.rr_nranges);
984263bc
MD
625
626 list = mapupper.root;
627
628 while (list) {
32a7b49a 629 new_locale.rl_mapupper_ext.rr_nranges++;
984263bc
MD
630 list = list->next;
631 }
32a7b49a
JS
632 file_new_locale.frl_mapupper_ext.frr_nranges =
633 htonl(new_locale.rl_mapupper_ext.rr_nranges);
984263bc 634
32a7b49a 635 file_new_locale.frl_variable_len = htonl(new_locale.rl_variable_len);
984263bc
MD
636
637 /*
638 * Okay, we are now ready to write the new locale file.
639 */
640
641 /*
642 * PART 1: The _RuneLocale structure
643 */
32a7b49a
JS
644 if (fwrite((char *)&file_new_locale, sizeof(file_new_locale), 1, fp) != 1)
645 err(1, "writing _RuneLocale to %s", locale_file);
984263bc
MD
646 /*
647 * PART 2: The runetype_ext structures (not the actual tables)
648 */
32a7b49a
JS
649 for (list = types.root, n = 0; list != NULL; list = list->next, n++) {
650 _FileRuneEntry re;
984263bc 651
32a7b49a
JS
652 memset(&re, 0, sizeof(re));
653 re.fre_min = htonl(list->min);
654 re.fre_max = htonl(list->max);
655 re.fre_map = htonl(list->map);
984263bc 656
32a7b49a
JS
657 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1)
658 err(1, "writing runetype_ext #%d to %s", n, locale_file);
984263bc
MD
659 }
660 /*
661 * PART 3: The maplower_ext structures
662 */
32a7b49a
JS
663 for (list = maplower.root, n = 0; list != NULL; list = list->next, n++) {
664 _FileRuneEntry re;
984263bc 665
32a7b49a
JS
666 memset(&re, 0, sizeof(re));
667 re.fre_min = htonl(list->min);
668 re.fre_max = htonl(list->max);
669 re.fre_map = htonl(list->map);
984263bc 670
32a7b49a
JS
671 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1)
672 err(1, "writing maplower_ext #%d to %s", n, locale_file);
984263bc
MD
673 }
674 /*
675 * PART 4: The mapupper_ext structures
676 */
32a7b49a
JS
677 for (list = mapupper.root, n = 0; list != NULL; list = list->next, n++) {
678 _FileRuneEntry re;
984263bc 679
32a7b49a
JS
680 memset(&re, 0, sizeof(re));
681 re.fre_min = htonl(list->min);
682 re.fre_max = htonl(list->max);
683 re.fre_map = htonl(list->map);
984263bc 684
32a7b49a
JS
685 if (fwrite((char *)&re, sizeof(re), 1, fp) != 1)
686 err(1, "writing mapupper_ext #%d to %s", n, locale_file);
984263bc
MD
687 }
688 /*
689 * PART 5: The runetype_ext tables
690 */
32a7b49a 691 for (list = types.root, n = 0; list != NULL; list = list->next, n++) {
984263bc
MD
692 for (x = 0; x < list->max - list->min + 1; ++x)
693 list->types[x] = htonl(list->types[x]);
694
695 if (!list->map) {
696 if (fwrite((char *)list->types,
32a7b49a
JS
697 (list->max - list->min + 1) * sizeof(uint32_t),
698 1, fp) != 1)
699 err(1, "writing runetype_ext table #%d to %s", n, locale_file);
984263bc 700 }
984263bc
MD
701 }
702 /*
703 * PART 5: And finally the variable data
704 */
32a7b49a
JS
705 if (new_locale.rl_variable_len != 0 &&
706 fwrite((char *)new_locale.rl_variable,
707 new_locale.rl_variable_len, 1, fp) != 1)
708 err(1, "writing variable data to %s", locale_file);
709 fclose(fp);
984263bc
MD
710
711 if (!debug)
712 return;
713
32a7b49a
JS
714 if (new_locale.rl_encoding[0])
715 fprintf(stderr, "ENCODING %s\n", new_locale.rl_encoding);
716 if (new_locale.rl_variable)
717 fprintf(stderr, "VARIABLE %s\n",
718 (char *)new_locale.rl_variable);
984263bc
MD
719
720 fprintf(stderr, "\nMAPLOWER:\n\n");
721
32a7b49a 722 for (x = 0; x < _NB_CACHED_RUNES; ++x) {
984263bc
MD
723 if (isprint(maplower.map[x]))
724 fprintf(stderr, " '%c'", (int)maplower.map[x]);
725 else if (maplower.map[x])
32a7b49a 726 fprintf(stderr, "%04x", maplower.map[x]);
984263bc
MD
727 else
728 fprintf(stderr, "%4x", 0);
729 if ((x & 0xf) == 0xf)
730 fprintf(stderr, "\n");
731 else
732 fprintf(stderr, " ");
733 }
734 fprintf(stderr, "\n");
735
736 for (list = maplower.root; list; list = list->next)
737 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
738
739 fprintf(stderr, "\nMAPUPPER:\n\n");
740
32a7b49a 741 for (x = 0; x < _NB_CACHED_RUNES; ++x) {
984263bc
MD
742 if (isprint(mapupper.map[x]))
743 fprintf(stderr, " '%c'", (int)mapupper.map[x]);
744 else if (mapupper.map[x])
32a7b49a 745 fprintf(stderr, "%04x", mapupper.map[x]);
984263bc
MD
746 else
747 fprintf(stderr, "%4x", 0);
748 if ((x & 0xf) == 0xf)
749 fprintf(stderr, "\n");
750 else
751 fprintf(stderr, " ");
752 }
753 fprintf(stderr, "\n");
754
755 for (list = mapupper.root; list; list = list->next)
756 fprintf(stderr, "\t%04x - %04x : %04x\n", list->min, list->max, list->map);
757
758
759 fprintf(stderr, "\nTYPES:\n\n");
760
32a7b49a
JS
761 for (x = 0; x < _NB_CACHED_RUNES; ++x) {
762 uint32_t r = types.map[x];
984263bc
MD
763
764 if (r) {
765 if (isprint(x))
32a7b49a 766 fprintf(stderr, " '%c':%2d", x, (int)(r & 0xff));
984263bc 767 else
32a7b49a
JS
768 fprintf(stderr, "%04x:%2d", x, (int)(r & 0xff));
769
770 fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : "");
771 fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : "");
772 fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : "");
773 fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : "");
774 fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : "");
775 fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : "");
776 fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : "");
777 fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : "");
778 fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : "");
779 fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : "");
780 fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : "");
781 fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : "");
782 fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : "");
783 fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : "");
984263bc
MD
784 fprintf(stderr, "\n");
785 }
786 }
787
788 for (list = types.root; list; list = list->next) {
789 if (list->map && list->min + 3 < list->max) {
32a7b49a
JS
790 uint32_t r = list->map;
791
792 fprintf(stderr, "%04x:%2d", list->min, r & 0xff);
793
794 fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : "");
795 fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : "");
796 fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : "");
797 fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : "");
798 fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : "");
799 fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : "");
800 fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : "");
801 fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : "");
802 fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : "");
803 fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : "");
804 fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : "");
805 fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : "");
806 fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : "");
807 fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : "");
984263bc
MD
808 fprintf(stderr, "\n...\n");
809
32a7b49a
JS
810 fprintf(stderr, "%04x:%2d", list->max, r & 0xff);
811
812 fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : "");
813 fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : "");
814 fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : "");
815 fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : "");
816 fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : "");
817 fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : "");
818 fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : "");
819 fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : "");
820 fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : "");
821 fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : "");
822 fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : "");
823 fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : "");
824 fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : "");
825 fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : "");
826 fprintf(stderr, " %1u", (unsigned)((r & _RUNETYPE_SWM)>>_RUNETYPE_SWS));
984263bc
MD
827 fprintf(stderr, "\n");
828 } else
829 for (x = list->min; x <= list->max; ++x) {
32a7b49a 830 uint32_t r = ntohl(list->types[x - list->min]);
984263bc
MD
831
832 if (r) {
32a7b49a
JS
833 fprintf(stderr, "%04x:%2d", x, (int)(r & 0xff));
834
835 fprintf(stderr, " %4s", (r & _RUNETYPE_A) ? "alph" : "");
836 fprintf(stderr, " %4s", (r & _RUNETYPE_C) ? "ctrl" : "");
837 fprintf(stderr, " %4s", (r & _RUNETYPE_D) ? "dig" : "");
838 fprintf(stderr, " %4s", (r & _RUNETYPE_G) ? "graf" : "");
839 fprintf(stderr, " %4s", (r & _RUNETYPE_L) ? "low" : "");
840 fprintf(stderr, " %4s", (r & _RUNETYPE_P) ? "punc" : "");
841 fprintf(stderr, " %4s", (r & _RUNETYPE_S) ? "spac" : "");
842 fprintf(stderr, " %4s", (r & _RUNETYPE_U) ? "upp" : "");
843 fprintf(stderr, " %4s", (r & _RUNETYPE_X) ? "xdig" : "");
844 fprintf(stderr, " %4s", (r & _RUNETYPE_B) ? "blnk" : "");
845 fprintf(stderr, " %4s", (r & _RUNETYPE_R) ? "prnt" : "");
846 fprintf(stderr, " %4s", (r & _RUNETYPE_I) ? "ideo" : "");
847 fprintf(stderr, " %4s", (r & _RUNETYPE_T) ? "spec" : "");
848 fprintf(stderr, " %4s", (r & _RUNETYPE_Q) ? "phon" : "");
849 fprintf(stderr, " %1u", (unsigned)((r & _RUNETYPE_SWM)>>_RUNETYPE_SWS));
984263bc
MD
850 fprintf(stderr, "\n");
851 }
852 }
853 }
854}