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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
|
/*
* $LynxId: dtd_util.c,v 1.76 2010/09/25 00:30:23 tom Exp $
*
* Given a SGML_dtd structure, write a corresponding flat file, or "C" source.
* Given the flat-file, write the "C" source.
*
* TODO: use symbols for HTMLA_NORMAL, etc.
*/
#include <HTUtils.h>
#include <HTMLDTD.h>
#include <string.h>
/*
* Tweaks to build standalone.
*/
#undef exit
BOOLEAN WWW_TraceFlag = FALSE;
FILE *TraceFP(void)
{
return stderr;
}
/*
* Begin the actual utility.
*/
#define GETOPT "chl:o:ts"
#define NOTE(message) fprintf(output, message "\n");
/* *INDENT-OFF* */
#ifdef USE_PRETTYSRC
# define N HTMLA_NORMAL
# define i HTMLA_ANAME
# define h HTMLA_HREF
# define c HTMLA_CLASS
# define x HTMLA_AUXCLASS
# define T(t) , t
#else
# define T(t) /*nothing */
#endif
#define ATTR_TYPE(name) { #name, name##_attr_list }
static const attr core_attr_list[] = {
{ "CLASS" T(c) },
{ "ID" T(i) },
{ "STYLE" T(N) },
{ "TITLE" T(N) },
{ 0 T(N) } /* Terminate list */
};
static const attr i18n_attr_list[] = {
{ "DIR" T(N) },
{ "LANG" T(N) },
{ 0 T(N) } /* Terminate list */
};
static const attr events_attr_list[] = {
{ "ONCLICK" T(N) },
{ "ONDBLCLICK" T(N) },
{ "ONKEYDOWN" T(N) },
{ "ONKEYPRESS" T(N) },
{ "ONKEYUP" T(N) },
{ "ONMOUSEDOWN" T(N) },
{ "ONMOUSEMOVE" T(N) },
{ "ONMOUSEOUT" T(N) },
{ "ONMOUSEOVER" T(N) },
{ "ONMOUSEUP" T(N) },
{ 0 T(N) } /* Terminate list */
};
static const attr align_attr_list[] = {
{ "ALIGN" T(N) },
{ 0 T(N) } /* Terminate list */
};
static const attr cellalign_attr_list[] = {
{ "ALIGN" T(N) },
{ "CHAR" T(N) },
{ "CHAROFF" T(N) },
{ "VALIGN" T(N) },
{ 0 T(N) } /* Terminate list */
};
static const attr bgcolor_attr_list[] = {
{ "BGCOLOR" T(N) },
{ 0 T(N) } /* Terminate list */
};
#undef T
/* *INDENT-ON* */
static void failed(const char *s)
{
perror(s);
exit(EXIT_FAILURE);
}
static void usage(void)
{
static const char *tbl[] =
{
"Usage: dtd_util [options]",
"",
"Options:",
" -c generate C-source"
" -h generate C-header"
" -l load",
" -o filename specify output (default: stdout)",
" -s strict (HTML DTD 0)",
" -t tagsoup (HTML DTD 1)",
};
unsigned n;
for (n = 0; n < TABLESIZE(tbl); ++n) {
fprintf(stderr, "%s\n", tbl[n]);
}
exit(EXIT_FAILURE);
}
static const char *SGMLContent2s(SGMLContent contents)
{
char *value = "?";
switch (contents) {
case SGML_EMPTY:
value = "SGML_EMPTY";
break;
case SGML_LITTERAL:
value = "SGML_LITTERAL";
break;
case SGML_CDATA:
value = "SGML_CDATA";
break;
case SGML_SCRIPT:
value = "SGML_SCRIPT";
break;
case SGML_RCDATA:
value = "SGML_RCDATA";
break;
case SGML_MIXED:
value = "SGML_MIXED";
break;
case SGML_ELEMENT:
value = "SGML_ELEMENT";
break;
case SGML_PCDATA:
value = "SGML_PCDATA";
break;
}
return value;
}
static SGMLContent s2SGMLContent(const char *value)
{
static SGMLContent table[] =
{
SGML_EMPTY,
SGML_LITTERAL,
SGML_CDATA,
SGML_SCRIPT,
SGML_RCDATA,
SGML_MIXED,
SGML_ELEMENT,
SGML_PCDATA
};
unsigned n;
SGMLContent result = SGML_EMPTY;
for (n = 0; n < TABLESIZE(table); ++n) {
if (!strcmp(SGMLContent2s(table[n]), value)) {
result = table[n];
break;
}
}
return result;
}
static void PrintF(FILE *, int, const char *,...) GCC_PRINTFLIKE(3, 4);
static void PrintF(FILE *output, int width, const char *fmt,...)
{
char buffer[BUFSIZ];
va_list ap;
va_start(ap, fmt);
vsprintf(buffer, fmt, ap);
va_end(ap);
fprintf(output, "%-*s", width, buffer);
}
static int same_AttrList(AttrList a, AttrList b)
{
int result = 1;
if (a && b) {
while (a->name && b->name) {
if (strcmp(a->name, b->name)) {
result = 0;
break;
}
++a, ++b;
}
if (a->name || b->name)
result = 0;
} else {
result = 0;
}
return result;
}
static int first_attrs(const SGML_dtd * dtd, int which)
{
int check;
int result = TRUE;
for (check = 0; check < which; ++check) {
if (dtd->tags[check].attributes == dtd->tags[which].attributes) {
result = FALSE;
break;
} else if (same_AttrList(dtd->tags[check].attributes,
dtd->tags[which].attributes)) {
result = FALSE;
dtd->tags[which].attributes = dtd->tags[check].attributes;
break;
}
}
return result;
}
static char *no_dashes(char *target, const char *source)
{
int j;
for (j = 0; (target[j] = source[j]) != '\0'; ++j) {
if (!isalnum(target[j]))
target[j] = '_';
}
return target;
}
/* the second "OBJECT" is treated specially */
static int first_object(const SGML_dtd * dtd, int which)
{
int check;
for (check = 0; check <= which; ++check) {
if (!strcmp(dtd->tags[check].name, "OBJECT"))
break;
}
return (check == which);
}
static const char *NameOfAttrs(const SGML_dtd * dtd, int which)
{
int check;
const char *result = dtd->tags[which].name;
for (check = 0; check < which; ++check) {
if (dtd->tags[check].attributes == dtd->tags[which].attributes) {
result = dtd->tags[check].name;
break;
}
}
/* special cases to match existing headers */
if (!strcmp(result, "ABBR"))
result = "GEN";
else if (!strcmp(result, "BLOCKQUOTE"))
result = "BQ";
else if (!strcmp(result, "BASEFONT"))
result = "FONT";
else if (!strcmp(result, "CENTER"))
result = "DIV";
else if (!strcmp(result, "DIR"))
result = "UL";
else if (!strcmp(result, "H1"))
result = "H";
else if (!strcmp(result, "TBODY"))
result = "TR";
return result;
}
static const char *DEF_name(const SGML_dtd * dtd, int which)
{
const char *result = dtd->tags[which].name;
if (!strcmp(result, "OBJECT") && !first_object(dtd, which))
result = "OBJECT_PCDATA";
return result;
}
typedef struct {
const char *name;
const attr *attrs;
int count;
int which;
} AttrInfo;
static int compare_attr_types(const void *a, const void *b)
{
const AttrType *p = (const AttrType *) a;
const AttrType *q = (const AttrType *) b;
int result = 0;
/* keep lowercase AttrType lists before uppercase, since latter are derived */
if (isupper(p->name[0]) ^ isupper(q->name[0])) {
if (isupper(p->name[0])) {
result = 1;
} else {
result = -1;
}
} else {
result = strcmp(p->name, q->name);
}
return result;
}
static int len_AttrTypes(const AttrType * data)
{
int result = 0;
for (result = 0; data[result].name != 0; ++result) {
;
}
return result;
}
static AttrType *sorted_AttrTypes(const AttrType * source)
{
AttrType *result = 0;
unsigned number = len_AttrTypes(source);
if (number != 0) {
result = typecallocn(AttrType, number + 1);
if (result != 0) {
MemCpy(result, source, number * sizeof(*result));
qsort(result, number, sizeof(*result), compare_attr_types);
}
}
return result;
}
static int compare_attr(const void *a, const void *b)
{
const AttrInfo *p = (const AttrInfo *) a;
const AttrInfo *q = (const AttrInfo *) b;
return strcmp(p->name, q->name);
}
static int len_AttrList(AttrList data)
{
int result = 0;
for (result = 0; data[result].name != 0; ++result) {
;
}
return result;
}
static void sort_uniq_AttrList(attr * data)
{
unsigned have = len_AttrList(data);
unsigned j, k;
qsort(data, have, sizeof(*data), compare_attr);
/*
* Eliminate duplicates
*/
for (j = 0; j < have; ++j) {
for (k = j; data[k].name; ++k) {
if (data[k + 1].name == 0)
break;
if (strcmp(data[j].name, data[k + 1].name)) {
break;
}
}
data[j] = data[k];
}
memset(data + j, 0, sizeof(data[0]));
}
static attr *copy_AttrList(AttrList data)
{
unsigned need = len_AttrList(data);
unsigned n;
attr *result = (attr *) calloc(need + 1, sizeof(attr));
for (n = 0; n < need; ++n)
result[n] = data[n];
sort_uniq_AttrList(result);
return result;
}
static attr *merge_AttrLists(const AttrType * data)
{
const AttrType *at;
attr *result = 0;
unsigned need = 1;
unsigned have = 0;
unsigned j;
for (at = data; at->name; ++at) {
need += len_AttrList(at->list);
}
result = (attr *) calloc(need + 1, sizeof(attr));
for (at = data; at->name; ++at) {
if (!strcmp(at->name, "events")) {
; /* lynx does not use events */
} else {
for (j = 0; at->list[j].name; ++j) {
result[have++] = at->list[j];
}
}
}
sort_uniq_AttrList(result);
return result;
}
static int clean_AttrList(attr * target, AttrList source)
{
int result = 0;
int j, k;
for (j = 0; target[j].name != 0; ++j) {
for (k = 0; source[k].name != 0; ++k) {
if (!strcmp(target[j].name, source[k].name)) {
k = j--;
for (;;) {
target[k] = target[k + 1];
if (target[k++].name == 0)
break;
}
++result;
break;
}
}
}
return result;
}
/*
* Actually COUNT the number of attributes, to make it possible to edit a
* attribute-table in src0_HTMLDTD.h and have all of the files updated by
* just doing a "make sources".
*/
static int AttrCount(HTTag * tag)
{
return len_AttrList(tag->attributes);
}
static AttrInfo *sorted_attrs(const SGML_dtd * dtd, unsigned *countp)
{
int j;
AttrInfo *data = (AttrInfo *) calloc(dtd->number_of_tags, sizeof(AttrInfo));
unsigned count = 0;
/* get the attribute-data */
for (j = 0; j < dtd->number_of_tags; ++j) {
if (first_attrs(dtd, j)) {
data[count].name = NameOfAttrs(dtd, j);
data[count].attrs = dtd->tags[j].attributes;
data[count].count = AttrCount(&(dtd->tags[j]));
data[count].which = j;
++count;
}
}
/* sort the data by the name of their associated tag */
qsort(data, count, sizeof(*data), compare_attr);
*countp = count;
return data;
}
static void dump_src_HTTag_Defines(FILE *output, const SGML_dtd * dtd, int which)
{
HTTag *tag = &(dtd->tags[which]);
#define myFMT "0x%05X"
fprintf(output,
"#define T_%-13s "
myFMT "," myFMT "," myFMT "," myFMT "," myFMT "," myFMT
"," myFMT "\n",
DEF_name(dtd, which),
tag->tagclass,
tag->contains,
tag->icontains,
tag->contained,
tag->icontained,
tag->canclose,
tag->flags);
}
static void dump_AttrItem(FILE *output, const attr * data)
{
char buffer[BUFSIZ];
char pretty = 'N';
sprintf(buffer, "\"%s\"", data->name);
#ifdef USE_PRETTYSRC
switch (data->type) {
case HTMLA_NORMAL:
pretty = 'N';
break;
case HTMLA_ANAME:
pretty = 'i';
break;
case HTMLA_HREF:
pretty = 'h';
break;
case HTMLA_CLASS:
pretty = 'c';
break;
case HTMLA_AUXCLASS:
pretty = 'x';
break;
}
#endif
fprintf(output, "\t{ %-15s T(%c) },\n", buffer, pretty);
}
static void dump_AttrItem0(FILE *output)
{
fprintf(output, "\t{ 0 T(N) }\t/* Terminate list */\n");
}
static void dump_src_AttrType(FILE *output, const char *name, AttrList data, const char **from)
{
int n;
fprintf(output, "static const attr %s_attr_list[] = {\n", name);
if (data != 0) {
for (n = 0; data[n].name != 0; ++n) {
dump_AttrItem(output, data + n);
}
}
fprintf(output, "\t{ 0 T(N) } /* Terminate list */\n");
fprintf(output, "};\n");
NOTE("");
fprintf(output, "static const AttrType %s_attr_type[] = {\n", name);
if (from != 0) {
while (*from != 0) {
fprintf(output, "\t{ ATTR_TYPE(%s) },\n", *from);
++from;
}
} else {
fprintf(output, "\t{ ATTR_TYPE(%s) },\n", name);
}
fprintf(output, "\t{ 0, 0 },\n");
fprintf(output, "};\n");
NOTE("");
}
static void dump_src_HTTag_Attrs(FILE *output, const SGML_dtd * dtd, int which)
{
HTTag *tag = &(dtd->tags[which]);
attr *list = merge_AttrLists(tag->attr_types);
char buffer[BUFSIZ];
int n;
int limit = len_AttrList(list);
sprintf(buffer, "static const attr %s_attr[] = {", NameOfAttrs(dtd, which));
fprintf(output,
"%-40s/* %s attributes */\n", buffer, tag->name);
for (n = 0; n < limit; ++n) {
dump_AttrItem(output, list + n);
}
dump_AttrItem0(output);
fprintf(output, "};\n");
NOTE("");
free(list);
}
static void dump_src_HTTag(FILE *output, const SGML_dtd * dtd, int which)
{
HTTag *tag = &(dtd->tags[which]);
char *P_macro = "P";
#ifdef USE_JUSTIFY_ELTS
if (!tag->can_justify)
P_macro = "P0";
#endif
PrintF(output, 19, " { %s(%s),", P_macro, tag->name);
PrintF(output, 24, "ATTR_DATA(%s), ", NameOfAttrs(dtd, which));
PrintF(output, 14, "%s,", SGMLContent2s(tag->contents));
fprintf(output, "T_%s", DEF_name(dtd, which));
fprintf(output, "},\n");
}
static void dump_source(FILE *output, const SGML_dtd * dtd, int dtd_version)
{
static AttrType generic_types[] =
{
ATTR_TYPE(core),
ATTR_TYPE(i18n),
ATTR_TYPE(events),
ATTR_TYPE(align),
ATTR_TYPE(cellalign),
ATTR_TYPE(bgcolor),
{0, 0}
};
AttrType *gt;
const char *marker = "src_HTMLDTD_H";
int j;
unsigned count = 0;
AttrInfo *data = sorted_attrs(dtd, &count);
fprintf(output, "/* %cLynxId%c */\n", '$', '$');
fprintf(output, "#ifndef %s%d\n", marker, dtd_version);
fprintf(output, "#define %s%d 1\n\n", marker, dtd_version);
/*
* If we ifdef this for once, and make the table names distinct, we can
* #include the strict- and tagsoup-output directly in HTMLDTD.c
*/
NOTE("#ifndef once_HTMLDTD");
NOTE("#define once_HTMLDTD 1");
NOTE("");
/* construct TagClass-define's */
for (j = 0; j <= dtd->number_of_tags; ++j) {
dump_src_HTTag_Defines(output, dtd, j);
}
NOTE("#define T__UNREC_ 0x00000,0x00000,0x00000,0x00000,0x00000,0x00000,0x00000");
/* construct attribute-tables */
NOTE("#ifdef USE_PRETTYSRC");
NOTE("# define N HTMLA_NORMAL");
NOTE("# define i HTMLA_ANAME");
NOTE("# define h HTMLA_HREF");
NOTE("# define c HTMLA_CLASS");
NOTE("# define x HTMLA_AUXCLASS");
NOTE("# define T(t) , t");
NOTE("#else");
NOTE("# define T(t) /*nothing */");
NOTE("#endif");
NOTE("/* *INDENT-OFF* */");
NOTE("");
NOTE("#define ATTR_TYPE(name) #name, name##_attr_list");
NOTE("");
NOTE("/* generic attributes, used in different tags */");
for (gt = generic_types; gt->name != 0; ++gt) {
dump_src_AttrType(output, gt->name, gt->list, 0);
}
NOTE("");
NOTE("/* tables defining attributes per-tag in terms of generic attributes (editable) */");
for (j = 0; j < (int) count; ++j) {
int which = data[j].which;
if (first_attrs(dtd, which)) {
HTTag *tag = &(dtd->tags[which]);
const AttrType *types = tag->attr_types;
const char *name = NameOfAttrs(dtd, which);
attr *list = 0;
const char *from_attr[10];
int from_size = 0;
while (types->name != 0) {
from_attr[from_size++] = types->name;
if (!strcmp(types->name, name)) {
list = copy_AttrList(types->list);
for (gt = generic_types; gt->name != 0; ++gt) {
if (clean_AttrList(list, gt->list)) {
int k;
int found = 0;
for (k = 0; k < from_size; ++k) {
if (!strcmp(from_attr[k], gt->name)) {
found = 1;
break;
}
}
if (!found)
from_attr[from_size++] = gt->name;
break;
}
}
}
++types;
}
from_attr[from_size] = 0;
if (list != 0) {
dump_src_AttrType(output, name, list, from_attr);
free(list);
}
}
}
NOTE("");
NOTE("/* attribute lists for the runtime (generated by dtd_util) */");
for (j = 0; j < (int) count; ++j) {
dump_src_HTTag_Attrs(output, dtd, data[j].which);
}
NOTE("/* *INDENT-ON* */");
NOTE("");
NOTE("/* justification-flags */");
NOTE("#undef N");
NOTE("#undef i");
NOTE("#undef h");
NOTE("#undef c");
NOTE("#undef x");
NOTE("");
NOTE("#undef T");
NOTE("");
NOTE("/* tag-names */");
for (j = 0; j <= dtd->number_of_tags; ++j) {
fprintf(output, "#undef %s\n", DEF_name(dtd, j));
}
NOTE("");
NOTE("/* these definitions are used in the tags-tables */");
NOTE("#undef P");
NOTE("#undef P_");
NOTE("#ifdef USE_COLOR_STYLE");
NOTE("#define P_(x) #x, (sizeof #x) -1");
NOTE("#define NULL_HTTag_ NULL, 0");
NOTE("#else");
NOTE("#define P_(x) #x");
NOTE("#define NULL_HTTag_ NULL");
NOTE("#endif");
NOTE("");
NOTE("#ifdef USE_JUSTIFY_ELTS");
NOTE("#define P(x) P_(x), 1");
NOTE("#define P0(x) P_(x), 0");
NOTE("#define NULL_HTTag NULL_HTTag_,0");
NOTE("#else");
NOTE("#define P(x) P_(x)");
NOTE("#define P0(x) P_(x)");
NOTE("#define NULL_HTTag NULL_HTTag_");
NOTE("#endif");
NOTE("");
NOTE("#define ATTR_DATA(name) name##_attr, HTML_##name##_ATTRIBUTES, name##_attr_type");
NOTE("");
NOTE("#endif /* once_HTMLDTD */");
NOTE("/* *INDENT-OFF* */");
/* construct the tags table */
fprintf(output,
"static const HTTag tags_table%d[HTML_ALL_ELEMENTS] = {\n",
dtd_version);
for (j = 0; j <= dtd->number_of_tags; ++j) {
if (j == dtd->number_of_tags) {
NOTE("/* additional (alternative variants), not counted in HTML_ELEMENTS: */");
NOTE("/* This one will be used as a temporary substitute within the parser when");
NOTE(" it has been signalled to parse OBJECT content as MIXED. - kw */");
}
dump_src_HTTag(output, dtd, j);
}
fprintf(output, "};\n");
NOTE("/* *INDENT-ON* */");
NOTE("");
fprintf(output, "#endif /* %s%d */\n", marker, dtd_version);
free(data);
}
static void dump_hdr_attr(FILE *output, AttrInfo * data)
{
int j;
char buffer[BUFSIZ];
for (j = 0; j < data->count; ++j) {
PrintF(output, 33, "#define HTML_%s_%s",
data->name,
no_dashes(buffer, data->attrs[j].name));
fprintf(output, "%2d\n", j);
}
PrintF(output, 33, "#define HTML_%s_ATTRIBUTES", data->name);
fprintf(output, "%2d\n", data->count);
fprintf(output, "\n");
}
static void dump_header(FILE *output, const SGML_dtd * dtd)
{
const char *marker = "hdr_HTMLDTD_H";
int j;
unsigned count = 0;
AttrInfo *data = sorted_attrs(dtd, &count);
fprintf(output, "/* %cLynxId%c */\n", '$', '$');
fprintf(output, "#ifndef %s\n", marker);
fprintf(output, "#define %s 1\n\n", marker);
NOTE("#ifdef __cplusplus");
NOTE("extern \"C\" {");
NOTE("#endif");
NOTE("/*");
NOTE("");
NOTE(" Element Numbers");
NOTE("");
NOTE(" Must Match all tables by element!");
NOTE(" These include tables in HTMLDTD.c");
NOTE(" and code in HTML.c.");
NOTE("");
NOTE(" */");
fprintf(output, " typedef enum {\n");
for (j = 0; j < dtd->number_of_tags; ++j) {
fprintf(output, "\tHTML_%s,\n", dtd->tags[j].name);
}
NOTE("\tHTML_ALT_OBJECT");
NOTE(" } HTMLElement;\n");
NOTE("/* Notes: HTML.c uses a different extension of the");
NOTE(" HTML_ELEMENTS space privately, see");
NOTE(" HTNestedList.h.");
NOTE("");
NOTE(" Do NOT replace HTML_ELEMENTS with");
NOTE(" TABLESIZE(mumble_dtd.tags).");
NOTE("");
NOTE(" Keep the following defines in synch with");
NOTE(" the above enum!");
NOTE(" */");
NOTE("");
NOTE("/* # of elements generally visible to Lynx code */");
fprintf(output, "#define HTML_ELEMENTS %d\n", dtd->number_of_tags);
NOTE("");
NOTE("/* # of elements visible to SGML parser */");
fprintf(output, "#define HTML_ALL_ELEMENTS %d\n", dtd->number_of_tags + 1);
NOTE("");
NOTE("/*");
NOTE("");
NOTE(" Attribute numbers");
NOTE("");
NOTE(" Identifier is HTML_<element>_<attribute>.");
NOTE(" These must match the tables in HTML.c!");
NOTE("");
NOTE(" */");
/* output the sorted list */
for (j = 0; j < (int) count; ++j) {
dump_hdr_attr(output, data + j);
}
free(data);
NOTE("#ifdef __cplusplus");
NOTE("}");
NOTE("#endif");
fprintf(output, "#endif\t\t\t\t/* %s */\n", marker);
}
#define FMT_NUM_ATTRS "%d attributes:\n"
#define FMT_ONE_ATTR "%d:%d:%s\n"
#define NUM_ONE_ATTR 3
static void dump_flat_attrs(FILE *output,
const attr * attributes,
int number_of_attributes)
{
int n;
fprintf(output, "\t\t" FMT_NUM_ATTRS, number_of_attributes);
for (n = 0; n < number_of_attributes; ++n) {
fprintf(output, "\t\t\t" FMT_ONE_ATTR, n,
#ifdef USE_PRETTYSRC
attributes[n].type,
#else
0, /* need placeholder for source-compat */
#endif
attributes[n].name
);
}
}
static void dump_flat_attr_types(FILE *output, const AttrType * attr_types)
{
const AttrType *p = sorted_AttrTypes(attr_types);
int number = len_AttrTypes(attr_types);
fprintf(output, "\t\t%d attr_types\n", number);
if (p != 0) {
while (p->name != 0) {
fprintf(output, "\t\t\t%s\n", p->name);
++p;
}
}
}
static void dump_flat_SGMLContent(FILE *output, const char *name, SGMLContent contents)
{
fprintf(output, "\t\t%s: %s\n", name, SGMLContent2s(contents));
}
#define DUMP(name) \
if (theClass & Tgc_##name) {\
fprintf(output, " " #name); \
theClass &= ~(Tgc_##name); \
}
static void dump_flat_TagClass(FILE *output, const char *name, TagClass theClass)
{
fprintf(output, "\t\t%s:", name);
DUMP(FONTlike);
DUMP(EMlike);
DUMP(MATHlike);
DUMP(Alike);
DUMP(formula);
DUMP(TRlike);
DUMP(SELECTlike);
DUMP(FORMlike);
DUMP(Plike);
DUMP(DIVlike);
DUMP(LIlike);
DUMP(ULlike);
DUMP(BRlike);
DUMP(APPLETlike);
DUMP(HRlike);
DUMP(MAPlike);
DUMP(outer);
DUMP(BODYlike);
DUMP(HEADstuff);
DUMP(same);
if (theClass)
fprintf(output, " OOPS:%#x", theClass);
fprintf(output, "\n");
}
#undef DUMP
#define DUMP(name) \
if (theFlags & Tgf_##name) {\
fprintf(output, " " #name); \
theFlags &= ~(Tgf_##name); \
}
static void dump_flat_TagFlags(FILE *output, const char *name, TagFlags theFlags)
{
fprintf(output, "\t\t%s:", name);
DUMP(endO);
DUMP(startO);
DUMP(mafse);
DUMP(strict);
DUMP(nreie);
DUMP(frecyc);
DUMP(nolyspcl);
if (theFlags)
fprintf(output, " OOPS:%#x", theFlags);
fprintf(output, "\n");
}
#undef DUMP
static void dump_flat_HTTag(FILE *output, unsigned n, HTTag * tag)
{
fprintf(output, "\t%u:%s\n", n, tag->name);
#ifdef USE_JUSTIFY_ELTS
fprintf(output, "\t\t%s\n", tag->can_justify ? "justify" : "nojustify");
#endif
dump_flat_attrs(output, tag->attributes, AttrCount(tag));
dump_flat_attr_types(output, tag->attr_types);
dump_flat_SGMLContent(output, "contents", tag->contents);
dump_flat_TagClass(output, "tagclass", tag->tagclass);
dump_flat_TagClass(output, "contains", tag->contains);
dump_flat_TagClass(output, "icontains", tag->icontains);
dump_flat_TagClass(output, "contained", tag->contained);
dump_flat_TagClass(output, "icontained", tag->icontained);
dump_flat_TagClass(output, "canclose", tag->canclose);
dump_flat_TagFlags(output, "flags", tag->flags);
}
static int count_attr_types(AttrType * attr_types, HTTag * tag)
{
int count = 0;
const AttrType *p;
AttrType *q;
if ((p = tag->attr_types) != 0) {
while (p->name != 0) {
if ((q = attr_types) != 0) {
while (q->name != 0) {
if (!strcmp(q->name, p->name)) {
--count;
break;
}
++q;
}
*q = *p;
}
++count;
++p;
}
}
return count;
}
static void dump_flatfile(FILE *output, const SGML_dtd * dtd)
{
AttrType *attr_types = 0;
int pass;
unsigned count = 0;
unsigned n;
/* merge all of the attr_types data */
for (pass = 0; pass < 2; ++pass) {
for (n = 0; (int) n < dtd->number_of_tags; ++n) {
count += count_attr_types(attr_types, &(dtd->tags[n]));
}
if (pass == 0) {
attr_types = typecallocn(AttrType, count + 1);
count = 0;
} else {
count = len_AttrTypes(attr_types);
qsort(attr_types, count, sizeof(*attr_types), compare_attr_types);
fprintf(output, "%d attr_types\n", count);
for (n = 0; n < count; ++n) {
fprintf(output, "\t%d:%s\n", n, attr_types[n].name);
dump_flat_attrs(output, attr_types[n].list,
len_AttrList(attr_types[n].list));
}
}
}
fprintf(output, "%d tags\n", dtd->number_of_tags);
for (n = 0; (int) n < dtd->number_of_tags; ++n) {
dump_flat_HTTag(output, n, &(dtd->tags[n]));
}
#if 0
fprintf(output, "%d entities\n", dtd->number_of_entities);
for (n = 0; n < dtd->number_of_entities; ++n) {
}
#endif
}
static char *get_line(FILE *input)
{
char temp[1024];
char *result = 0;
if (fgets(temp, (int) sizeof(temp), input) != 0) {
result = strdup(temp);
}
return result;
}
#define LOAD(name) \
if (!strcmp(data, #name)) {\
*theClass |= Tgc_##name; \
continue; \
}
static int load_flat_TagClass(FILE *input, const char *name, TagClass * theClass)
{
char prefix[80];
char *next = get_line(input);
char *data;
int result = 0;
*theClass = 0;
if (next != 0) {
sprintf(prefix, "\t\t%s:", name);
data = strtok(next, "\n ");
if (data != 0 && !strcmp(data, prefix)) {
result = 1;
while ((data = strtok(NULL, "\n ")) != 0) {
LOAD(FONTlike);
LOAD(EMlike);
LOAD(MATHlike);
LOAD(Alike);
LOAD(formula);
LOAD(TRlike);
LOAD(SELECTlike);
LOAD(FORMlike);
LOAD(Plike);
LOAD(DIVlike);
LOAD(LIlike);
LOAD(ULlike);
LOAD(BRlike);
LOAD(APPLETlike);
LOAD(HRlike);
LOAD(MAPlike);
LOAD(outer);
LOAD(BODYlike);
LOAD(HEADstuff);
LOAD(same);
fprintf(stderr, "Unexpected TagClass '%s'\n", data);
result = 0;
break;
}
} else if (data) {
fprintf(stderr, "load_flat_TagClass: '%s' vs '%s'\n", data, prefix);
}
free(next);
} else {
fprintf(stderr, "Did not find contents\n");
}
return result;
}
#undef LOAD
#define LOAD(name) \
if (!strcmp(data, #name)) {\
*flags |= Tgf_##name; \
continue; \
}
static int load_flat_TagFlags(FILE *input, const char *name, TagFlags * flags)
{
char prefix[80];
char *next = get_line(input);
char *data;
int result = 0;
*flags = 0;
if (next != 0) {
sprintf(prefix, "\t\t%s:", name);
data = strtok(next, "\n ");
if (data != 0 && !strcmp(data, prefix)) {
result = 1;
while ((data = strtok(NULL, "\n ")) != 0) {
LOAD(endO);
LOAD(startO);
LOAD(mafse);
LOAD(strict);
LOAD(nreie);
LOAD(frecyc);
LOAD(nolyspcl);
fprintf(stderr, "Unexpected TagFlag '%s'\n", data);
result = 0;
break;
}
} else if (data) {
fprintf(stderr, "load_flat_TagFlags: '%s' vs '%s'\n", data, prefix);
}
free(next);
}
return result;
}
#undef LOAD
static int load_flat_AttrList(FILE *input, AttrList * attrs, int *length)
{
attr *attributes;
int j, jcmp, code;
int result = 1;
char name[1024];
#ifdef USE_PRETTYSRC
int atype;
#endif
if (fscanf(input, FMT_NUM_ATTRS, length) == 1
&& *length > 0
&& (attributes = typecallocn(attr, (size_t) (*length + 1))) != 0) {
*attrs = attributes;
for (j = 0; j < *length; ++j) {
code = fscanf(input, FMT_ONE_ATTR,
&jcmp,
&atype,
name
);
if (code == NUM_ONE_ATTR && (j == jcmp)) {
attributes[j].name = strdup(name);
#ifdef USE_PRETTYSRC
attributes[j].type = atype;
#endif
} else {
fprintf(stderr, "Did not find attributes\n");
result = 0;
break;
}
}
if (*length > 1)
qsort(attributes, *length, sizeof(attributes[0]), compare_attr);
}
return result;
}
static int load_flat_HTTag(FILE *input, unsigned nref, HTTag * tag, AttrType * allTypes)
{
int result = 0;
unsigned ncmp = 0;
char name[1024];
int code;
int j;
code = fscanf(input, "%d:%s\n", &ncmp, name);
if (code == 2 && (nref == ncmp)) {
result = 1;
tag->name = strdup(name);
#ifdef USE_COLOR_STYLE
tag->name_len = strlen(tag->name);
#endif
#ifdef USE_JUSTIFY_ELTS
if (fscanf(input, "%s\n", name) == 1) {
tag->can_justify = !strcmp(name, "justify");
} else {
fprintf(stderr, "Did not find can_justify\n");
result = 0;
}
#endif
if (result) {
result = load_flat_AttrList(input, &(tag->attributes), &(tag->number_of_attributes));
}
if (result) {
AttrType *myTypes;
int k, count;
char *next = get_line(input);
if (next != 0
&& sscanf(next, "%d attr_types\n", &count)
&& (myTypes = typecallocn(AttrType, (size_t) (count + 1)))
!= 0) {
tag->attr_types = myTypes;
for (k = 0; k < count; ++k) {
next = get_line(input);
if (next != 0
&& sscanf(next, "%s\n", name)) {
for (j = 0; allTypes[j].name != 0; ++j) {
if (!strcmp(allTypes[j].name, name)) {
myTypes[k].name = strdup(name);
myTypes[k].list = allTypes[j].list;
break;
}
}
} else {
result = 0;
break;
}
}
if (result && count > 1)
qsort(myTypes, count, sizeof(myTypes[0]), compare_attr_types);
}
}
if (result) {
char *next = get_line(input);
if (next != 0
&& sscanf(next, "\t\tcontents: %s\n", name)) {
tag->contents = s2SGMLContent(name);
free(next);
} else {
fprintf(stderr, "Did not find contents\n");
result = 0;
}
}
if (result) {
result = load_flat_TagClass(input, "tagclass", &(tag->tagclass));
}
if (result) {
result = load_flat_TagClass(input, "contains", &(tag->contains));
}
if (result) {
result = load_flat_TagClass(input, "icontains", &(tag->icontains));
}
if (result) {
result = load_flat_TagClass(input, "contained", &(tag->contained));
}
if (result) {
result = load_flat_TagClass(input, "icontained", &(tag->icontained));
}
if (result) {
result = load_flat_TagClass(input, "canclose", &(tag->canclose));
}
if (result) {
result = load_flat_TagFlags(input, "flags", &(tag->flags));
}
} else {
fprintf(stderr, "load_flat_HTTag error\n");
}
return result;
}
static int load_flat_AttrType(FILE *input, AttrType * types, size_t ncmp)
{
int result = 0;
int ntst;
char name[1024];
if (fscanf(input, "%d:%s\n", &ntst, name) == 2
&& (ntst == (int) ncmp)) {
result = 1;
types->name = strdup(name);
if (!load_flat_AttrList(input, &(types->list), &ntst))
result = 0;
}
return result;
}
static SGML_dtd *load_flatfile(FILE *input)
{
AttrType *attr_types = 0;
SGML_dtd *result = 0;
size_t n;
size_t number_of_attrs = 0;
size_t number_of_tags = 0;
HTTag *tag;
int code;
code = fscanf(input, "%d attr_types\n", &number_of_attrs);
if (code
&& number_of_attrs
&& (attr_types = typecallocn(AttrType, number_of_attrs + 1)) != 0) {
for (n = 0; n < number_of_attrs; ++n) {
if (!load_flat_AttrType(input, attr_types + n, n)) {
break;
}
}
}
code = fscanf(input, "%d tags\n", &number_of_tags);
if (code == 1) {
if ((result = typecalloc(SGML_dtd)) != 0
&& (result->tags = typecallocn(HTTag, (number_of_tags + 2))) != 0) {
for (n = 0; n < number_of_tags; ++n) {
if (load_flat_HTTag(input, n, &(result->tags[n]), attr_types)) {
result->number_of_tags = (n + 1);
} else {
break;
}
}
tag = 0;
for (n = 0; n < number_of_tags; ++n) {
if (result->tags[n].name != 0
&& !strcmp(result->tags[n].name, "OBJECT")) {
tag = result->tags + number_of_tags;
*tag = result->tags[n];
tag->contents = SGML_MIXED;
tag->flags = Tgf_strict;
break;
}
}
if (tag == 0) {
fprintf(stderr, "Did not find OBJECT tag\n");
result = 0;
}
}
}
return result;
}
int main(int argc, char *argv[])
{
const SGML_dtd *the_dtd = &HTML_dtd;
int ch;
int dtd_version = 0;
int c_option = FALSE;
int h_option = FALSE;
int l_option = FALSE;
FILE *input = stdin;
FILE *output = stdout;
while ((ch = getopt(argc, argv, GETOPT)) != -1) {
switch (ch) {
case 'c':
c_option = TRUE;
break;
case 'h':
h_option = TRUE;
break;
case 'l':
l_option = TRUE;
input = fopen(optarg, "r");
if (input == 0)
failed(optarg);
break;
case 'o':
output = fopen(optarg, "w");
if (output == 0)
failed(optarg);
break;
case 't':
dtd_version = 1;
break;
case 's':
dtd_version = 0;
break;
default:
usage();
}
}
HTSwitchDTD(dtd_version);
if (l_option)
the_dtd = load_flatfile(input);
if (the_dtd != 0) {
if (c_option)
dump_source(output, the_dtd, dtd_version);
if (h_option)
dump_header(output, the_dtd);
if (!c_option && !h_option)
dump_flatfile(output, the_dtd);
}
return EXIT_SUCCESS;
}
|