-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkchrtab.c
1675 lines (1483 loc) · 45.7 KB
/
mkchrtab.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/TADS2/mkchrtab.c,v 1.2 1999/05/17 02:52:14 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1998, 2002 Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
mkchrtab.c - TADS character table generator
Function
Notes
Modified
05/31/98 MJRoberts - Creation
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "os.h"
#include "cmap.h"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
/*
* Read a number. Returns zero on success, non-zero on error.
*/
static int read_number(unsigned int *result, char **p,
char *infile, int linenum, int is_char)
{
unsigned int base;
unsigned int acc;
int digcnt;
/* skip any leading spaces */
while (isspace(**p))
++(*p);
/*
* Check for a character value
*/
if (**p == '\'')
{
unsigned char c;
/* get the next character */
++(*p);
/* if it's a backslash, escape the next character */
if (**p == '\\')
{
/* skip the backslash */
++(*p);
/* the next character gives the value */
switch(**p)
{
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
case '\\':
c = '\\';
break;
case '\'':
c = '\'';
break;
default:
printf("%s: line %d: invalid backslash sequence '\\%c'\n",
infile, linenum, **p);
return 4;
}
}
else
{
/* use this value as the character code */
c = (unsigned char)**p;
}
/* skip the character, and make sure we have the closing quote */
++(*p);
if (**p != '\'')
{
printf("%s: line %d: missing close quote\n", infile, linenum);
return 5;
}
/* skip the close quote */
++(*p);
/* skip trailing spaces */
while (isspace(**p))
++(*p);
/* return the result */
*result = (unsigned int)c;
return 0;
}
/*
* determine the base - if there's a leading zero, it's hex or
* octal; otherwise, it's decimal
*/
if (**p == '0')
{
/* skip the leading zero */
++(*p);
/* if the next character is 'x', it's hex, otherwise it's octal */
if (**p == 'x' || **p == 'X')
{
/* skip the 'x' */
++(*p);
/* it's hex */
base = 16;
}
else
{
/* it's octal */
base = 8;
}
}
else
{
/* no leading zero - it's a decimal number */
base = 10;
}
/* read digits */
for (acc = 0, digcnt = 0 ;; ++(*p), ++digcnt)
{
/* see if we still have a digit */
if (isdigit(**p) || (base == 16 && isxdigit(**p)))
{
unsigned int dig;
/* get this digit's value */
dig = (unsigned int)(isdigit(**p)
? **p - '0'
: **p - (isupper(**p) ? 'A' : 'a') + 10);
/* make sure it's in range for our radix */
if (dig >= base)
{
printf("%s: line %d: invalid digit for radix\n",
infile, linenum);
return 3;
}
/* accumulate this digit */
acc *= base;
acc += dig;
}
else
{
/* that's the end of the number */
break;
}
}
/* if we didn't get any valid digits, it's an error */
if (digcnt == 0)
{
printf("%s: line %d: invalid number\n", infile, linenum);
return 1;
}
/* skip any trailing spaces */
while (isspace(**p))
++(*p);
/* make sure the result isn't too large */
if ((is_char && acc > (unsigned int)(unsigned char)0xff)
|| acc > (unsigned int)0xffff)
{
printf("%s: line %d: value out of range\n", infile, linenum);
return 2;
}
/* return the accumulated value */
*result = acc;
/* success */
return 0;
}
/*
* Check to see if an identifier matches a given string
*/
static int id_matches(const char *idp, size_t idlen, const char *str)
{
return (idlen == strlen(str) && memicmp(idp, str, idlen) == 0);
}
/*
* HTML Entity mapping structure
*/
struct entity_t
{
const char *ename;
unsigned int charval;
};
/*
* List of HTML TADS entity names and the corresponding character codes
*/
static const struct entity_t entities[] =
{
{ "nbsp", 160 },
{ "iexcl", 161 },
{ "cent", 162 },
{ "pound", 163 },
{ "curren", 164 },
{ "yen", 165 },
{ "brvbar", 166 },
{ "sect", 167 },
{ "uml", 168 },
{ "copy", 169 },
{ "ordf", 170 },
{ "laquo", 171 },
{ "not", 172 },
{ "shy", 173 },
{ "reg", 174 },
{ "macr", 175 },
{ "deg", 176 },
{ "plusmn", 177 },
{ "sup2", 178 },
{ "sup3", 179 },
{ "acute", 180 },
{ "micro", 181 },
{ "para", 182 },
{ "middot", 183 },
{ "cedil", 184 },
{ "sup1", 185 },
{ "ordm", 186 },
{ "raquo", 187 },
{ "frac14", 188 },
{ "frac12", 189 },
{ "frac34", 190 },
{ "iquest", 191 },
{ "times", 215 },
{ "divide", 247 },
{ "AElig", 198 },
{ "Aacute", 193 },
{ "Acirc", 194 },
{ "Agrave", 192 },
{ "Aring", 197 },
{ "Atilde", 195 },
{ "Auml", 196 },
{ "Ccedil", 199 },
{ "ETH", 208 },
{ "Eacute", 201 },
{ "Ecirc", 202 },
{ "Egrave", 200 },
{ "Euml", 203 },
{ "Iacute", 205 },
{ "Icirc", 206 },
{ "Igrave", 204 },
{ "Iuml", 207 },
{ "Ntilde", 209 },
{ "Oacute", 211 },
{ "Ocirc", 212 },
{ "Ograve", 210 },
{ "Oslash", 216 },
{ "Otilde", 213 },
{ "Ouml", 214 },
{ "THORN", 222 },
{ "Uacute", 218 },
{ "Ucirc", 219 },
{ "Ugrave", 217 },
{ "Uuml", 220 },
{ "Yacute", 221 },
{ "aacute", 225 },
{ "acirc", 226 },
{ "aelig", 230 },
{ "agrave", 224 },
{ "aring", 229 },
{ "atilde", 227 },
{ "auml", 228 },
{ "ccedil", 231 },
{ "eacute", 233 },
{ "ecirc", 234 },
{ "egrave", 232 },
{ "eth", 240 },
{ "euml", 235 },
{ "iacute", 237 },
{ "icirc", 238 },
{ "igrave", 236 },
{ "iuml", 239 },
{ "ntilde", 241 },
{ "oacute", 243 },
{ "ocirc", 244 },
{ "ograve", 242 },
{ "oslash", 248 },
{ "otilde", 245 },
{ "ouml", 246 },
{ "szlig", 223 },
{ "thorn", 254 },
{ "uacute", 250 },
{ "ucirc", 251 },
{ "ugrave", 249 },
{ "uuml", 252 },
{ "yacute", 253 },
{ "thorn", 254 },
{ "yuml", 255 },
/* TADS extensions to the standard characters */
{ "lsq", 8216 },
{ "rsq", 8217 },
{ "ldq", 8220 },
{ "rdq", 8221 },
{ "endash", 8211 },
{ "emdash", 8212 },
{ "trade", 8482 },
/* HTML 4.0 character extensions */
{ "ndash", 8211 },
{ "mdash", 8212 },
{ "lsquo", 8216 },
{ "rsquo", 8217 },
{ "ldquo", 8220 },
{ "rdquo", 8221 },
{ "sbquo", 8218 },
{ "bdquo", 8222 },
{ "lsaquo", 8249 },
{ "rsaquo", 8250 },
{ "dagger", 8224 },
{ "Dagger", 8225 },
{ "OElig", 338 },
{ "oelig", 339 },
{ "permil", 8240 },
{ "Yuml", 376 },
{ "scaron", 353 },
{ "Scaron", 352 },
{ "circ", 710 },
{ "tilde", 732 },
/* Greek letters */
{ "Alpha", 913 },
{ "Beta", 914 },
{ "Gamma", 915 },
{ "Delta", 916 },
{ "Epsilon", 917 },
{ "Zeta", 918 },
{ "Eta", 919 },
{ "Theta", 920 },
{ "Iota", 921 },
{ "Kappa", 922 },
{ "Lambda", 923 },
{ "Mu", 924 },
{ "Nu", 925 },
{ "Xi", 926 },
{ "Omicron", 927 },
{ "Pi", 928 },
{ "Rho", 929 },
{ "Sigma", 931 },
{ "Tau", 932 },
{ "Upsilon", 933 },
{ "Phi", 934 },
{ "Chi", 935 },
{ "Psi", 936 },
{ "Omega", 937 },
{ "alpha", 945 },
{ "beta", 946 },
{ "gamma", 947 },
{ "delta", 948 },
{ "epsilon", 949 },
{ "zeta", 950 },
{ "eta", 951 },
{ "theta", 952 },
{ "iota", 953 },
{ "kappa", 954 },
{ "lambda", 955 },
{ "mu", 956 },
{ "nu", 957 },
{ "xi", 958 },
{ "omicron", 959 },
{ "pi", 960 },
{ "rho", 961 },
{ "sigmaf", 962 },
{ "sigma", 963 },
{ "tau", 964 },
{ "upsilon", 965 },
{ "phi", 966 },
{ "chi", 967 },
{ "psi", 968 },
{ "omega", 969 },
{ "thetasym", 977 },
{ "upsih", 978 },
{ "piv", 982 },
/* general punctuation marks */
{ "bull", 8226 },
{ "hellip", 8230 },
{ "prime", 8242 },
{ "Prime", 8243 },
{ "oline", 8254 },
{ "frasl", 8260 },
/* letter-like symbols */
{ "weierp", 8472 },
{ "image", 8465 },
{ "real", 8476 },
{ "alefsym", 8501 },
/* arrows */
{ "larr", 8592 },
{ "uarr", 8593 },
{ "rarr", 8594 },
{ "darr", 8595 },
{ "harr", 8596 },
{ "crarr", 8629 },
{ "lArr", 8656 },
{ "uArr", 8657 },
{ "rArr", 8658 },
{ "dArr", 8659 },
{ "hArr", 8660 },
/* mathematical operators */
{ "forall", 8704 },
{ "part", 8706 },
{ "exist", 8707 },
{ "empty", 8709 },
{ "nabla", 8711 },
{ "isin", 8712 },
{ "notin", 8713 },
{ "ni", 8715 },
{ "prod", 8719 },
{ "sum", 8721 },
{ "minus", 8722 },
{ "lowast", 8727 },
{ "radic", 8730 },
{ "prop", 8733 },
{ "infin", 8734 },
{ "ang", 8736 },
{ "and", 8743 },
{ "or", 8744 },
{ "cap", 8745 },
{ "cup", 8746 },
{ "int", 8747 },
{ "there4", 8756 },
{ "sim", 8764 },
{ "cong", 8773 },
{ "asymp", 8776 },
{ "ne", 8800 },
{ "equiv", 8801 },
{ "le", 8804 },
{ "ge", 8805 },
{ "sub", 8834 },
{ "sup", 8835 },
{ "nsub", 8836 },
{ "sube", 8838 },
{ "supe", 8839 },
{ "oplus", 8853 },
{ "otimes", 8855 },
{ "perp", 8869 },
{ "sdot", 8901 },
{ "lceil", 8968 },
{ "rceil", 8969 },
{ "lfloor", 8970 },
{ "rfloor", 8971 },
{ "lang", 9001 },
{ "rang", 9002 },
/* geometric shapes */
{ "loz", 9674 },
/* miscellaneous symbols */
{ "spades", 9824 },
{ "clubs", 9827 },
{ "hearts", 9829 },
{ "diams", 9830 },
/* Latin-extended B */
{ "fnof", 402 },
/* Latin-2 characters */
{ "Aogon", 260 },
{ "breve", 728 },
{ "Lstrok", 321 },
{ "Lcaron", 317 },
{ "Sacute", 346 },
{ "Scedil", 350 },
{ "Tcaron", 356 },
{ "Zacute", 377 },
{ "Zcaron", 381 },
{ "Zdot", 379 },
{ "aogon", 261 },
{ "ogon", 731 },
{ "lstrok", 322 },
{ "lcaron", 318 },
{ "sacute", 347 },
{ "caron", 711 },
{ "scedil", 351 },
{ "tcaron", 357 },
{ "zacute", 378 },
{ "dblac", 733 },
{ "zcaron", 382 },
{ "zdot", 380 },
{ "Racute", 340 },
{ "Abreve", 258 },
{ "Lacute", 313 },
{ "Cacute", 262 },
{ "Ccaron", 268 },
{ "Eogon", 280 },
{ "Ecaron", 282 },
{ "Dcaron", 270 },
{ "Dstrok", 272 },
{ "Nacute", 323 },
{ "Ncaron", 327 },
{ "Odblac", 336 },
{ "Rcaron", 344 },
{ "Uring", 366 },
{ "Udblac", 368 },
{ "Tcedil", 354 },
{ "racute", 341 },
{ "abreve", 259 },
{ "lacute", 314 },
{ "cacute", 263 },
{ "ccaron", 269 },
{ "eogon", 281 },
{ "ecaron", 283 },
{ "dcaron", 271 },
{ "dstrok", 273 },
{ "nacute", 324 },
{ "ncaron", 328 },
{ "odblac", 337 },
{ "rcaron", 345 },
{ "uring", 367 },
{ "udblac", 369 },
{ "tcedil", 355 },
{ "dot", 729 },
{ 0, 0 }
};
/*
* Entity mapping list entry. We store each entity mapping we find in
* the file in one of these structures, and link the structures together
* into a list.
*/
typedef struct entity_map_t entity_map_t;
struct entity_map_t
{
/* next mapping entry in the list */
entity_map_t *nxt;
/* HTML entity character code (Unicode value) */
unsigned int html_char;
/* length of the expansion */
size_t exp_len;
/* local character set expansion */
unsigned char expansion[1];
};
/*
* Parse an entity name mapping
*/
static entity_map_t *parse_entity(char *p, char *infile, int linenum)
{
const char *start;
const struct entity_t *entp;
unsigned char buf[CMAP_MAX_ENTITY_EXPANSION];
unsigned char *dstp;
entity_map_t *mapp;
/* find the end of the entity name */
start = p;
for ( ; isalpha(*p) || isdigit(*p) ; ++p) ;
/* scan the list for the entity name */
for (entp = entities ; entp->ename != 0 ; ++entp)
{
/* see if this one is a match - note that case is significant */
if (strlen(entp->ename) == (size_t)(p - start)
&& memcmp(entp->ename, start, p - start) == 0)
break;
}
/* if we didn't find it, it's an error */
if (entp->ename == 0)
{
printf("%s: line %d: unknown entity name \"%.*s\"\n",
infile, linenum, p - start, start);
return 0;
}
/* skip any intervening whitespace and check for the '=' sign */
for ( ; isspace(*p) ; ++p) ;
if (*p != '=')
{
printf("%s: line %d: expected '=' after entity name\n",
infile, linenum);
return 0;
}
/* skip the '=' */
++p;
/* read the series of numbers */
for (dstp = buf ; *p != '\0' ; )
{
unsigned int intval;
unsigned char c;
/* skip any leading whitespace */
while (isspace(*p))
++p;
/* if it's the end of the line or a comment, stop */
if (*p == '\0' || *p == '#')
break;
/* scan the character code */
if (read_number(&intval, &p, infile, linenum, TRUE))
return 0;
c = (unsigned char)intval;
/* make sure we haven't overflowed our buffer */
if (dstp >= buf + sizeof(buf))
{
printf("%s: line %d: entity mapping is too long (maximum of %d "
"characters are allowed\n",
infile, linenum, sizeof(buf));
return 0;
}
/* add it to the output list */
*dstp++ = c;
}
/* if we didn't get any characters, it's an error */
if (dstp == buf)
{
printf("%s: line %d: no local character codes found after '='\n",
infile, linenum);
return 0;
}
/* create a new mapping structure and set it up */
mapp = (entity_map_t *)malloc(sizeof(entity_map_t) + dstp - buf);
mapp->nxt = 0;
mapp->html_char = entp->charval;
mapp->exp_len = dstp - buf;
memcpy(mapp->expansion, buf, dstp - buf);
/* return the new mapping structure */
return mapp;
}
/*
* Unicode mapping table entry
*/
typedef struct unicode_map_t unicode_map_t;
struct unicode_map_t
{
/* corresponding Unicode code point */
unsigned int unicode_val;
};
/*
* Generate a mapping table by associating two Unicode mapping tables
*/
static void gen_unicode_mapping(unicode_map_t map_from[256],
unicode_map_t map_to[256],
unsigned char result_fwd[256],
unsigned char result_fwd_set[256],
unsigned char result_rev[256],
unsigned char result_rev_set[256])
{
int i;
/*
* For each item in the 'from' map, find the entry in the 'to' map
* with the same Unicode value. This gives the value that we store
* in the result table at the 'from' character index location.
*/
for (i = 0 ; i < 256 ; ++i)
{
int j;
/* find this 'from' Unicode value in the 'to' table */
for (j = 0 ; j < 256 ; ++j)
{
/* if they match, map it */
if (map_to[j].unicode_val == map_from[i].unicode_val)
{
/* set the forward mapping, if it's not already set */
if (!result_fwd_set[i])
{
result_fwd[i] = j;
result_fwd_set[i] = TRUE;
}
/* set the reverse mapping, if it's not already set */
if (!result_rev_set[j])
{
result_rev[j] = i;
result_rev_set[j] = TRUE;
}
/* no need to look any further */
break;
}
}
}
}
/*
* Load a Unicode mapping file
*/
static void load_unicode_file(char *filename, unicode_map_t map[256],
char *infile, int linenum)
{
osfildef *fp;
int linenum_u;
/* open the unicode file */
fp = osfoprs(filename, OSFTTEXT);
if (fp == 0)
{
printf("%s: line %d: unable to open unicode mapping file \"%s\"\n",
infile, linenum, filename);
return;
}
/* read it */
for (linenum_u = 1 ;; ++linenum_u)
{
char buf[256];
char *p;
unsigned int n1;
unsigned int n2;
/* read the next line */
if (osfgets(buf, sizeof(buf), fp) == 0)
break;
/* skip leading spaces */
for (p = buf ; isspace(*p) ; ++p) ;
/* if it's blank or starts with a comment, ignore it */
if (*p == 0x1a || *p == '#' || *p == '\n' || *p == '\r' || *p == '\0')
continue;
/* read the first number - this is the native character value */
if (read_number(&n1, &p, filename, linenum_u, TRUE))
break;
/* read the second number */
if (read_number(&n2, &p, filename, linenum_u, FALSE))
break;
/* set the association */
map[n1].unicode_val = n2;
}
/* done with the file */
osfcls(fp);
}
/*
* Generate the HTML named entity mappings for the native character set
*/
static void gen_unicode_html_mapping(unicode_map_t map_native[256],
entity_map_t **entity_first,
entity_map_t **entity_last)
{
unsigned int i;
/* go through the native characters and find the named entity for each */
for (i = 0 ; i < 255 ; ++i)
{
const struct entity_t *entity;
/*
* scan the named entity table to find an entity with the same
* Unicode value as this native character's Unicode mapping
*/
for (entity = entities ; entity->ename != 0 ; ++entity)
{
/* if this one matches the Unicode value, map it */
if (entity->charval == map_native[i].unicode_val)
{
entity_map_t *mapp;
/* create a new mapping structure */
mapp = (entity_map_t *)malloc(sizeof(entity_map_t) + 1);
/* set it up */
mapp->nxt = 0;
mapp->html_char = map_native[i].unicode_val;
mapp->exp_len = 1;
mapp->expansion[0] = (unsigned char)i;
/*
* link it at the head of the list, so that any explicit
* mapping specified by the user for the same entity
* already or subsequently will override it (this will
* happen because the users's entries always go at the
* end of the list)
*/
mapp->nxt = *entity_first;
if (*entity_last == 0)
*entity_last = mapp;
*entity_first = mapp;
/* no need to look any further */
break;
}
}
}
}
/*
* Parse Unicode mapping files
*/
static void parse_unicode_files(char *val, size_t vallen,
char *infile, int linenum,
unsigned char input_map[256],
unsigned char input_map_set[256],
unsigned char output_map[256],
unsigned char output_map_set[256],
entity_map_t **entity_first,
entity_map_t **entity_last)
{
char fn_native[OSFNMAX];
char fn_internal[OSFNMAX];
char *dst;
unicode_map_t map_native[256];
unicode_map_t map_internal[256];
/* retrieve the filenames */
fn_native[0] = '\0';
fn_internal[0] = '\0';
while (vallen != 0)
{
char *id;
size_t idlen;
char qu;
/* if we're at a comment, we're done */
if (*val == '#')
break;
/* find the end of the current identifier */
for (id = val ; vallen > 0 && isalpha(*val) ; ++val, --vallen) ;
idlen = val - id;
/* see what we have */
if (id_matches(id, idlen, "native"))
dst = fn_native;
else if (id_matches(id, idlen, "internal"))
dst = fn_internal;
else
{
printf("%s: line %d: expected 'internal' or 'native', "
"found '%.*s'\n", infile, linenum, idlen, id);
return;
}
/* if the current name has already been matched, it's an error */
if (*dst != '\0')
{
printf("%s: line %d: '%.*s' specified more than once\n",
infile, linenum, idlen, id);
return;
}
/* scan ahead to the '=' */
while (vallen > 0 && isspace(*val))
++val, --vallen;
/* make sure we have the '=' */
if (vallen == 0 || *val != '=')
{
printf("%s: line %d: expected '=' after '%.*s'\n",
infile, linenum, idlen, id);
return;
}
/* skip intervening spaces */
++val, --vallen;
while (vallen > 0 && isspace(*val))
++val, --vallen;
/* make sure we have a filename */
if (vallen == 0)
{
printf("%s: line %d: expected filename after '='\n",
infile, linenum);
return;
}
/* check for a quote */
if (*val == '"' || *val == '\'')
{
/* remember the quote */
qu = *val;
/* skip it */
++val, --vallen;
}
else
{
/* we have no quote */
qu = '\0';
}
/* scan the filename */
for ( ; vallen > 0 ; ++val, --vallen)
{
/* check for a quote */
if (qu != '\0' && *val == qu)
{
/* skip the quote */
++val;
--vallen;
/*
* if it's stuttered, keep a single copy and keep going;
* otherwise, we're done
*/
if (vallen > 1 && *(val+1) == qu)
{
/* just skip the first quote, and keep the second */
}
else
{
/* that's the matching close quote - we're done */
break;
}
}
/* copy this character */
*dst++ = *val;
}
/* null-terminate the filename */
*dst = '\0';
/* skip trailing spaces */
while (vallen > 0 && isspace(*val))
++val, --vallen;
}
/* make sure we got both filenames */
if (fn_internal[0] == '\0' || fn_native[0] == '\0')
{
printf("%s: line %d: must specify both 'native' and 'internal'"
" with 'unicode'\n", infile, linenum);
return;
}
/* load the two files */
load_unicode_file(fn_internal, map_internal, infile, linenum);
load_unicode_file(fn_native, map_native, infile, linenum);
/* generate the forward and reverse mappings */
gen_unicode_mapping(map_native, map_internal, input_map, input_map_set,
output_map, output_map_set);
gen_unicode_mapping(map_internal, map_native, output_map, output_map_set,
input_map, input_map_set);
/* generate the HTML named entity mappings */
gen_unicode_html_mapping(map_native, entity_first, entity_last);
}
/*
* Main entrypoint
*/