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
|
/*
* $LynxId: dtd_util.c,v 1.28 2008/08/18 23:42:12 tom Exp $
*
* Given a SGML_dtd structure, write a corresponding flat file, or "C" source.
* Given the flat-file, write the "C" source.
*
* TODO: read flat-file
*/
#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 "chlo:ts"
#define NOTE(message) fprintf(output, message "\n");
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 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 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;
}
}
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 *XXX_attr(const SGML_dtd * dtd, int which)
{
static char result[80];
int i;
strcpy(result, NameOfAttrs(dtd, which));
for (i = 0; result[i]; ++i)
result[i] = tolower(result[i]);
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(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 AttrInfo *sorted_attrs(const SGML_dtd * dtd, unsigned *countp, int lower)
{
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)) {
if (lower)
data[count].name = strdup(XXX_attr(dtd, j));
else
data[count].name = NameOfAttrs(dtd, j);
data[count].attrs = dtd->tags[j].attributes;
data[count].count = dtd->tags[j].number_of_attributes;
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_src_HTTag_Attrs(FILE *output, const SGML_dtd * dtd, int which)
{
HTTag *tag = &(dtd->tags[which]);
char buffer[BUFSIZ];
char pretty = 'N';
int n;
sprintf(buffer, "static const attr %s_attr[] = {", XXX_attr(dtd, which));
fprintf(output,
"%-40s/* %s attributes */\n", buffer, tag->name);
for (n = 0; n < tag->number_of_attributes; ++n) {
sprintf(buffer, "\"%s\"", tag->attributes[n].name);
#ifdef USE_PRETTYSRC
switch (tag->attributes[n].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);
}
fprintf(output, "\t{ 0 T(N) }\t/* Terminate list */\n");
fprintf(output, "};\n");
NOTE("");
}
static void dump_src_HTTag(FILE *output, const SGML_dtd * dtd, int which)
{
HTTag *tag = &(dtd->tags[which]);
char *P_macro = "P";
#ifdef EXP_JUSTIFY_ELTS
if (!tag->can_justify)
P_macro = "P0";
#endif
PrintF(output, 19, " { %s(\"%s\"),", P_macro, tag->name);
PrintF(output, 16, "%s_attr,", XXX_attr(dtd, which));
PrintF(output, 28, "HTML_%s_ATTRIBUTES,", 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)
{
const char *marker = "src_HTMLDTD_H";
int j;
unsigned count = 0;
AttrInfo *data = sorted_attrs(dtd, &count, 1);
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("");
for (j = 0; j < (int) count; ++j) {
dump_src_HTTag_Attrs(output, dtd, data[j].which);
}
NOTE("/* *INDENT-ON* */");
NOTE("");
NOTE("#undef N");
NOTE("#undef i");
NOTE("#undef h");
NOTE("#undef c");
NOTE("#undef x");
NOTE("");
NOTE("#undef T");
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 EXP_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("#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, 0);
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);
}
static void dump_flat_attrs(FILE *output, const char *name, const attr * attributes, int number_of_attributes)
{
int n;
fprintf(output, "\t\t%d %s:\n", number_of_attributes, name);
for (n = 0; n < number_of_attributes; ++n) {
fprintf(output, "\t\t\t%d:%s\n", n, attributes[n].name);
}
}
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 EXP_JUSTIFY_ELTS
fprintf(output, "\t\t%s\n", tag->can_justify ? "justify" : "nojustify");
#endif
dump_flat_attrs(output, "attributes", tag->attributes, tag->number_of_attributes);
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 void dump_flatfile(FILE *output, const SGML_dtd * dtd)
{
unsigned n;
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 void load_flatfile(FILE *input, const SGML_dtd * dtd)
{
(void) input;
(void) dtd;
}
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;
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)
load_flatfile(input, the_dtd);
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;
}
|