Debian Codegraph @

WWW/Library/Implementation/dtd_util.c

raw
1 /*
2 * $LynxId: dtd_util.c,v 1.91 2025/07/22 00:09:42 tom Exp $
3 *
4 * Given a SGML_dtd structure, write a corresponding flat file, or "C" source.
5 * Given the flat-file, write the "C" source.
6 *
7 * TODO: use symbols for HTMLA_NORMAL, etc.
8 */
9
10 #include <HTUtils.h>
11 #include <HTMLDTD.h>
12 #include <string.h>
13 #include <stdarg.h>
14
15 /*
16 * Tweaks to build standalone.
17 */
18 #undef exit
19
20 BOOLEAN WWW_TraceFlag = FALSE;
21 FILE *TraceFP(void)
22 {
23 return stderr;
24 }
25
26 /*
27 * Begin the actual utility.
28 */
29 #define GETOPT "chl:o:tsx"
30
31 #define NOTE(message) fprintf(output, message "\n");
32 /* *INDENT-OFF* */
33 #ifdef USE_PRETTYSRC
34 # define N HTMLA_NORMAL
35 # define i HTMLA_ANAME
36 # define h HTMLA_HREF
37 # define c HTMLA_CLASS
38 # define x HTMLA_AUXCLASS
39 # define T(t) , t
40 #else
41 # define T(t) /*nothing */
42 #endif
43
44 #define ATTR_TYPE(name) { #name, name##_attr_list }
45
46 #define MY_LIMIT 1024
47 #define FMT_WS "%[ \t\n]"
48 #define FMT_NUM_ATTR_TYPES "%d attr_types"
49 #define FMT_NUM_ATTRS "%d attributes:"
50 #define FMT_ONE_ATTR "%d:%d:%s"
51 #define NUM_ONE_ATTR 4
52
53 static const char alias_codes[] = "!@#$%^&*";
54
55 #define DATA(name) { #name, Tgc_##name }
56 static const struct {
57 const char *name;
58 TagClass tagclass;
59 } class_list[] = {
60 DATA(APPLETlike),
61 DATA(Alike),
62 DATA(BODYlike),
63 DATA(BRlike),
64 DATA(DELlike),
65 DATA(DIVlike),
66 DATA(EMlike),
67 DATA(FONTlike),
68 DATA(FORMlike),
69 DATA(HEADstuff),
70 DATA(HRlike),
71 DATA(LIlike),
72 DATA(MAPlike),
73 DATA(MATHlike),
74 DATA(Plike),
75 DATA(SELECTlike),
76 DATA(TRlike),
77 DATA(ULlike),
78 DATA(formula),
79 DATA(outer),
80 DATA(same)
81 };
82
83 static const attr core_attr_list[] = {
84 { "CLASS" T(c) },
85 { "ID" T(i) },
86 { "STYLE" T(N) },
87 { "TITLE" T(N) },
88 { NULL T(N) } /* Terminate list */
89 };
90
91 static const attr i18n_attr_list[] = {
92 { "DIR" T(N) },
93 { "LANG" T(N) },
94 { NULL T(N) } /* Terminate list */
95 };
96
97 static const attr events_attr_list[] = {
98 { "ONCLICK" T(N) },
99 { "ONDBLCLICK" T(N) },
100 { "ONKEYDOWN" T(N) },
101 { "ONKEYPRESS" T(N) },
102 { "ONKEYUP" T(N) },
103 { "ONMOUSEDOWN" T(N) },
104 { "ONMOUSEMOVE" T(N) },
105 { "ONMOUSEOUT" T(N) },
106 { "ONMOUSEOVER" T(N) },
107 { "ONMOUSEUP" T(N) },
108 { NULL T(N) } /* Terminate list */
109 };
110
111 static const attr align_attr_list[] = {
112 { "ALIGN" T(N) },
113 { NULL T(N) } /* Terminate list */
114 };
115
116 static const attr cellalign_attr_list[] = {
117 { "ALIGN" T(N) },
118 { "CHAR" T(N) },
119 { "CHAROFF" T(N) },
120 { "VALIGN" T(N) },
121 { NULL T(N) } /* Terminate list */
122 };
123
124 static const attr bgcolor_attr_list[] = {
125 { "BGCOLOR" T(N) },
126 { NULL T(N) } /* Terminate list */
127 };
128
129 #undef T
130 /* *INDENT-ON* */
131
132 static const char *input_filename;
133 static int input_lineno;
134
135 static void failed(const char *s)
136 {
137 if (input_lineno) {
138 int save = errno;
139
140 fprintf(stderr, "%s:%d ", input_filename, input_lineno);
141 errno = save;
142 }
143 perror(s);
144 exit(EXIT_FAILURE);
145 }
146
147 static void warning(const char *, ...) GCC_PRINTFLIKE(1, 2);
148
149 static void warning(const char *fmt, ...)
150 {
151 va_list ap;
152
153 va_start(ap, fmt);
154 fprintf(stderr, "%s:%d: ", input_filename, input_lineno);
155 vfprintf(stderr, fmt, ap);
156 fputc('\n', stderr);
157 va_end(ap);
158 }
159
160 static void usage(void)
161 {
162 static const char *tbl[] =
163 {
164 "Usage: dtd_util [options]",
165 "",
166 "Options:",
167 " -c generate C-source"
168 " -h generate C-header"
169 " -l load",
170 " -o filename specify output (default: stdout)",
171 " -s strict (HTML DTD 0)",
172 " -t tagsoup (HTML DTD 1)",
173 " -x cross-check contains/contained data (repeat for more)"
174 };
175 unsigned n;
176
177 for (n = 0; n < TABLESIZE(tbl); ++n) {
178 fprintf(stderr, "%s\n", tbl[n]);
179 }
180 exit(EXIT_FAILURE);
181 }
182
183 static const char *SGMLContent2s(SGMLContent contents)
184 {
185 char *value = "?";
186
187 switch (contents) {
188 case SGML_EMPTY:
189 value = "SGML_EMPTY";
190 break;
191 case SGML_LITTERAL:
192 value = "SGML_LITTERAL";
193 break;
194 case SGML_CDATA:
195 value = "SGML_CDATA";
196 break;
197 case SGML_SCRIPT:
198 value = "SGML_SCRIPT";
199 break;
200 case SGML_RCDATA:
201 value = "SGML_RCDATA";
202 break;
203 case SGML_MIXED:
204 value = "SGML_MIXED";
205 break;
206 case SGML_ELEMENT:
207 value = "SGML_ELEMENT";
208 break;
209 case SGML_PCDATA:
210 value = "SGML_PCDATA";
211 break;
212 }
213 return value;
214 }
215
216 static SGMLContent s2SGMLContent(const char *value)
217 {
218 static SGMLContent table[] =
219 {
220 SGML_EMPTY,
221 SGML_LITTERAL,
222 SGML_CDATA,
223 SGML_SCRIPT,
224 SGML_RCDATA,
225 SGML_MIXED,
226 SGML_ELEMENT,
227 SGML_PCDATA
228 };
229 unsigned n;
230 SGMLContent result = SGML_EMPTY;
231
232 for (n = 0; n < TABLESIZE(table); ++n) {
233 if (!strcmp(SGMLContent2s(table[n]), value)) {
234 result = table[n];
235 break;
236 }
237 }
238 return result;
239 }
240
241 static void PrintF(FILE *, int, const char *, ...) GCC_PRINTFLIKE(3, 4);
242
243 static void PrintF(FILE *output, int width, const char *fmt, ...)
244 {
245 char buffer[BUFSIZ];
246 va_list ap;
247
248 va_start(ap, fmt);
249 vsprintf(buffer, fmt, ap);
250 va_end(ap);
251
252 fprintf(output, "%-*s", width, buffer);
253 }
254
255 static char *get_line(FILE *input)
256 {
257 char temp[MY_LIMIT];
258 char *result = 0;
259
260 if (fgets(temp, (int) sizeof(temp), input) != 0) {
261 result = strdup(temp);
262 ++input_lineno;
263 }
264 return result;
265 }
266
267 static int read_num_attr_types(FILE *input)
268 {
269 char *next = get_line(input);
270 int count;
271 int code;
272 char trail[MY_LIMIT];
273
274 if (next == 0
275 || (code = sscanf(next, FMT_NUM_ATTR_TYPES FMT_WS, &count, trail)) < 2
276 || !count) {
277 warning("expected attr_types");
278 }
279 return count;
280 }
281
282 static int same_AttrList(AttrList a, AttrList b)
283 {
284 int result = 1;
285
286 if (a && b) {
287 while (a->name && b->name) {
288 if (strcmp(a->name, b->name)) {
289 result = 0;
290 break;
291 }
292 ++a, ++b;
293 }
294 if (a->name || b->name)
295 result = 0;
296 } else {
297 result = 0;
298 }
299 return result;
300 }
301
302 static int first_attrs(const SGML_dtd * dtd, int which)
303 {
304 int check;
305 int result = TRUE;
306
307 for (check = 0; check < which; ++check) {
308 if (dtd->tags[check].attributes == dtd->tags[which].attributes) {
309 result = FALSE;
310 break;
311 } else if (same_AttrList(dtd->tags[check].attributes,
312 dtd->tags[which].attributes)) {
313 result = FALSE;
314 dtd->tags[which].attributes = dtd->tags[check].attributes;
315 break;
316 }
317 }
318 return result;
319 }
320
321 static char *no_dashes(char *target, const char *source)
322 {
323 int j;
324
325 for (j = 0; (target[j] = source[j]) != '\0'; ++j) {
326 if (!isalnum(target[j]))
327 target[j] = '_';
328 }
329 return target;
330 }
331
332 /* the second "OBJECT" is treated specially */
333 static int first_object(const SGML_dtd * dtd, int which)
334 {
335 int check;
336
337 for (check = 0; check <= which; ++check) {
338 if (!strcmp(dtd->tags[check].name, "OBJECT"))
339 break;
340 }
341 return (check == which);
342 }
343
344 static const char *NameOfAttrs(const SGML_dtd * dtd, int which)
345 {
346 int check;
347 const char *result = dtd->tags[which].name;
348
349 for (check = 0; check < which; ++check) {
350 if (dtd->tags[check].attributes == dtd->tags[which].attributes) {
351 result = dtd->tags[check].name;
352 break;
353 }
354 }
355 /* special cases to match existing headers */
356 if (!strcmp(result, "ABBR"))
357 result = "GEN";
358 else if (!strcmp(result, "ARTICLE"))
359 result = "GEN5";
360 else if (!strcmp(result, "BLOCKQUOTE"))
361 result = "BQ";
362 else if (!strcmp(result, "BASEFONT"))
363 result = "FONT";
364 else if (!strcmp(result, "CENTER"))
365 result = "DIV";
366 else if (!strcmp(result, "DIR"))
367 result = "UL";
368 else if (!strcmp(result, "H1"))
369 result = "H";
370 else if (!strcmp(result, "TBODY"))
371 result = "TR";
372 return result;
373 }
374
375 static const char *DEF_name(const SGML_dtd * dtd, int which, unsigned alias)
376 {
377 const char *result = dtd->tags[which].name;
378
379 if (!strcmp(result, "OBJECT") && !first_object(dtd, which)) {
380 result = "OBJECT_PCDATA";
381 } else if (alias) {
382 char buffer[MY_LIMIT];
383
384 sprintf(buffer, "%s_%d", result, alias + 1);
385 result = strdup(buffer);
386 }
387 return result;
388 }
389
390 static const char *EXT_name(HTTag * tag)
391 {
392 const char *result = tag->name;
393
394 if (tag->alias) {
395 char buffer[MY_LIMIT];
396
397 sprintf(buffer, "%s%c", result, alias_codes[tag->alias - 1]);
398 result = strdup(buffer);
399 }
400 return result;
401 }
402
403 typedef struct {
404 const char *name;
405 const attr *attrs;
406 int count;
407 int which;
408 } AttrInfo;
409
410 static int compare_attr_types(const void *a, const void *b)
411 {
412 const AttrType *p = (const AttrType *) a;
413 const AttrType *q = (const AttrType *) b;
414 int result = 0;
415
416 /* keep lowercase AttrType lists before uppercase, since latter are derived */
417 if (isupper(p->name[0]) ^ isupper(q->name[0])) {
418 if (isupper(p->name[0])) {
419 result = 1;
420 } else {
421 result = -1;
422 }
423 } else {
424 result = strcmp(p->name, q->name);
425 }
426 return result;
427 }
428
429 static int len_AttrTypes(const AttrType * data)
430 {
431 int result = 0;
432
433 for (result = 0; data[result].name != 0; ++result) {
434 ;
435 }
436 return result;
437 }
438
439 static AttrType *sorted_AttrTypes(const AttrType * source)
440 {
441 AttrType *result = 0;
442 unsigned number = len_AttrTypes(source);
443
444 if (number != 0) {
445 result = typecallocn(AttrType, number + 1);
446 if (result != 0) {
447 MemCpy(result, source, number * sizeof(*result));
448 qsort(result, number, sizeof(*result), compare_attr_types);
449 }
450 }
451
452 return result;
453 }
454
455 static int compare_attr(const void *a, const void *b)
456 {
457 const AttrInfo *p = (const AttrInfo *) a;
458 const AttrInfo *q = (const AttrInfo *) b;
459
460 return strcmp(p->name, q->name);
461 }
462
463 static int len_AttrList(AttrList data)
464 {
465 int result = 0;
466
467 for (result = 0; data[result].name != 0; ++result) {
468 ;
469 }
470 return result;
471 }
472
473 static void sort_uniq_AttrList(attr * data)
474 {
475 unsigned have = len_AttrList(data);
476 unsigned j, k;
477
478 qsort(data, have, sizeof(*data), compare_attr);
479 /*
480 * Eliminate duplicates
481 */
482 for (j = 0; j < have; ++j) {
483 for (k = j; data[k].name; ++k) {
484 if (data[k + 1].name == 0)
485 break;
486 if (strcmp(data[j].name, data[k + 1].name)) {
487 break;
488 }
489 }
490 data[j] = data[k];
491 }
492 memset(data + j, 0, sizeof(data[0]));
493 }
494
495 static attr *copy_AttrList(AttrList data)
496 {
497 unsigned need = len_AttrList(data);
498 unsigned n;
499
500 attr *result = (attr *) calloc(need + 1, sizeof(attr));
501
502 for (n = 0; n < need; ++n)
503 result[n] = data[n];
504 sort_uniq_AttrList(result);
505 return result;
506 }
507
508 static attr *merge_AttrLists(const AttrType * data)
509 {
510 const AttrType *at;
511 attr *result = 0;
512 unsigned need = 1;
513 unsigned have = 0;
514 unsigned j;
515
516 for (at = data; at->name; ++at) {
517 need += len_AttrList(at->list);
518 }
519 result = (attr *) calloc(need + 1, sizeof(attr));
520 for (at = data; at->name; ++at) {
521 if (!strcmp(at->name, "events")) {
522 ; /* lynx does not use events */
523 } else {
524 for (j = 0; at->list[j].name; ++j) {
525 result[have++] = at->list[j];
526 }
527 }
528 }
529 sort_uniq_AttrList(result);
530 return result;
531 }
532
533 static int clean_AttrList(attr * target, AttrList source)
534 {
535 int result = 0;
536 int j, k;
537
538 for (j = 0; target[j].name != 0; ++j) {
539 for (k = 0; source[k].name != 0; ++k) {
540 if (!strcmp(target[j].name, source[k].name)) {
541 k = j--;
542 for (;;) {
543 target[k] = target[k + 1];
544 if (target[k++].name == 0)
545 break;
546 }
547 ++result;
548 break;
549 }
550 }
551 }
552 return result;
553 }
554
555 /*
556 * Actually COUNT the number of attributes, to make it possible to edit a
557 * attribute-table in src0_HTMLDTD.h and have all of the files updated by
558 * just doing a "make sources".
559 */
560 static int AttrCount(HTTag * tag)
561 {
562 return len_AttrList(tag->attributes);
563 }
564
565 static AttrInfo *sorted_attrs(const SGML_dtd * dtd, unsigned *countp)
566 {
567 int j;
568
569 AttrInfo *data = (AttrInfo *) calloc(dtd->number_of_tags, sizeof(AttrInfo));
570 unsigned count = 0;
571
572 /* get the attribute-data */
573 for (j = 0; j < dtd->number_of_tags; ++j) {
574 if (first_attrs(dtd, j)) {
575 data[count].name = NameOfAttrs(dtd, j);
576 data[count].attrs = dtd->tags[j].attributes;
577 data[count].count = AttrCount(&(dtd->tags[j]));
578 data[count].which = j;
579 ++count;
580 }
581 }
582 /* sort the data by the name of their associated tag */
583 qsort(data, count, sizeof(*data), compare_attr);
584 *countp = count;
585 return data;
586 }
587
588 static void dump_src_HTTag_Defines(FILE *output, const SGML_dtd * dtd, int which)
589 {
590 HTTag *tag = &(dtd->tags[which]);
591
592 #define myFMT "0x%05X"
593 fprintf(output,
594 "#define T_%-13s "
595 myFMT "," myFMT "," myFMT "," myFMT "," myFMT "," myFMT
596 "," myFMT "\n",
597 DEF_name(dtd, which, tag->alias),
598 tag->tagclass,
599 tag->contains,
600 tag->icontains,
601 tag->contained,
602 tag->icontained,
603 tag->canclose,
604 tag->flags);
605 }
606
607 static void dump_AttrItem(FILE *output, const attr * data)
608 {
609 char buffer[BUFSIZ];
610 char pretty = 'N';
611
612 sprintf(buffer, "\"%s\"", data->name);
613 #ifdef USE_PRETTYSRC
614 switch (data->type) {
615 case HTMLA_NORMAL:
616 pretty = 'N';
617 break;
618 case HTMLA_ANAME:
619 pretty = 'i';
620 break;
621 case HTMLA_HREF:
622 pretty = 'h';
623 break;
624 case HTMLA_CLASS:
625 pretty = 'c';
626 break;
627 case HTMLA_AUXCLASS:
628 pretty = 'x';
629 break;
630 }
631 #endif
632 fprintf(output, "\t{ %-15s T(%c) },\n", buffer, pretty);
633 }
634
635 static void dump_AttrItem0(FILE *output)
636 {
637 fprintf(output, "\t{ NULL T(N) }\t/* Terminate list */\n");
638 }
639
640 static void dump_src_AttrType(FILE *output, const char *name, AttrList data, const char **from)
641 {
642 int n;
643
644 fprintf(output, "static const attr %s_attr_list[] = {\n", name);
645 if (data != 0) {
646 for (n = 0; data[n].name != 0; ++n) {
647 dump_AttrItem(output, data + n);
648 }
649 }
650 fprintf(output, "\t{ NULL T(N) } /* Terminate list */\n");
651 fprintf(output, "};\n");
652 NOTE("");
653 fprintf(output, "static const AttrType %s_attr_type[] = {\n", name);
654 if (from != 0) {
655 while (*from != 0) {
656 fprintf(output, "\t{ ATTR_TYPE(%s) },\n", *from);
657 ++from;
658 }
659 } else {
660 fprintf(output, "\t{ ATTR_TYPE(%s) },\n", name);
661 }
662 fprintf(output, "\t{ NULL, NULL },\n");
663 fprintf(output, "};\n");
664 NOTE("");
665 }
666
667 static void dump_src_HTTag_Attrs(FILE *output, const SGML_dtd * dtd, int which)
668 {
669 HTTag *tag = &(dtd->tags[which]);
670 attr *list = merge_AttrLists(tag->attr_types);
671 char buffer[BUFSIZ];
672 int n;
673 int limit = len_AttrList(list);
674
675 sprintf(buffer, "static const attr %s_attr[] = {", NameOfAttrs(dtd, which));
676 fprintf(output,
677 "%-40s/* %s attributes */\n", buffer, tag->name);
678 for (n = 0; n < limit; ++n) {
679 dump_AttrItem(output, list + n);
680 }
681 dump_AttrItem0(output);
682 fprintf(output, "};\n");
683 NOTE("");
684 free(list);
685 }
686
687 static void dump_src_HTTag(FILE *output, const SGML_dtd * dtd, int which)
688 {
689 HTTag *tag = &(dtd->tags[which]);
690 char *P_macro = "P";
691
692 #ifdef USE_JUSTIFY_ELTS
693 if (!tag->can_justify)
694 P_macro = "P0";
695 #endif
696 PrintF(output, 19, " { %s(%s),", P_macro, tag->name);
697 PrintF(output, 24, "ATTR_DATA(%s), ", NameOfAttrs(dtd, which));
698 PrintF(output, 14, "%s,", SGMLContent2s(tag->contents));
699 fprintf(output, "T_%s", DEF_name(dtd, which, tag->alias));
700 fprintf(output, ", %d", tag->alias);
701 fprintf(output, ", %d", tag->aliases);
702 fprintf(output, "},\n");
703 }
704
705 static void dump_source(FILE *output, const SGML_dtd * dtd, int dtd_version)
706 {
707 static AttrType generic_types[] =
708 {
709 ATTR_TYPE(core),
710 ATTR_TYPE(i18n),
711 ATTR_TYPE(events),
712 ATTR_TYPE(align),
713 ATTR_TYPE(cellalign),
714 ATTR_TYPE(bgcolor),
715 {NULL, NULL}
716 };
717 AttrType *gt;
718 TagAlias aliases;
719
720 const char *marker = "src_HTMLDTD_H";
721 int j;
722 int inalias;
723
724 unsigned count = 0;
725 AttrInfo *data = sorted_attrs(dtd, &count);
726
727 fprintf(output, "/* %cLynxId%c */\n", '$', '$');
728 fprintf(output, "#ifndef %s%d\n", marker, dtd_version);
729 fprintf(output, "#define %s%d 1\n\n", marker, dtd_version);
730
731 /*
732 * If we ifdef this for once, and make the table names distinct, we can
733 * #include the strict- and tagsoup-output directly in HTMLDTD.c
734 */
735 NOTE("#ifndef once_HTMLDTD");
736 NOTE("#define once_HTMLDTD 1");
737 NOTE("");
738 NOTE("#include <HTMLDTD.h>");
739 NOTE("");
740
741 /* construct TagClass-define's */
742 for (j = 0; j <= dtd->number_of_tags; ++j) {
743 dump_src_HTTag_Defines(output, dtd, j);
744 }
745 NOTE("#define T__UNREC_ 0x00000,0x00000,0x00000,0x00000,0x00000,0x00000,0x00000");
746
747 /* construct attribute-tables */
748 NOTE("#ifdef USE_PRETTYSRC");
749 NOTE("# define N HTMLA_NORMAL");
750 NOTE("# define i HTMLA_ANAME");
751 NOTE("# define h HTMLA_HREF");
752 NOTE("# define c HTMLA_CLASS");
753 NOTE("# define x HTMLA_AUXCLASS");
754 NOTE("# define T(t) , t");
755 NOTE("#else");
756 NOTE("# define T(t) /*nothing */");
757 NOTE("#endif");
758 NOTE("/* *INDENT-OFF* */");
759 NOTE("");
760 NOTE("#define ATTR_TYPE(name) #name, name##_attr_list");
761 NOTE("");
762 NOTE("/* generic attributes, used in different tags */");
763 for (gt = generic_types; gt->name != 0; ++gt) {
764 dump_src_AttrType(output, gt->name, gt->list, 0);
765 }
766 NOTE("");
767 NOTE("/* tables defining attributes per-tag in terms of generic attributes (editable) */");
768 for (j = 0; j < (int) count; ++j) {
769 int which = data[j].which;
770
771 if (first_attrs(dtd, which)) {
772 HTTag *tag = &(dtd->tags[which]);
773 const AttrType *types = tag->attr_types;
774 const char *name = NameOfAttrs(dtd, which);
775 attr *list = 0;
776 const char *from_attr[10];
777 int from_size = 0;
778
779 while (types->name != 0) {
780 from_attr[from_size++] = types->name;
781 if (!strcmp(types->name, name)) {
782 list = copy_AttrList(types->list);
783 for (gt = generic_types; gt->name != 0; ++gt) {
784 if (clean_AttrList(list, gt->list)) {
785 int k;
786 int found = 0;
787
788 for (k = 0; k < from_size; ++k) {
789 if (!strcmp(from_attr[k], gt->name)) {
790 found = 1;
791 break;
792 }
793 }
794 if (!found)
795 from_attr[from_size++] = gt->name;
796 break;
797 }
798 }
799 }
800 ++types;
801 }
802 from_attr[from_size] = 0;
803
804 if (list != 0) {
805 /* FIXME check if each remaining attribute is defined in XXX */
806
807 dump_src_AttrType(output, name, list, from_attr);
808 free(list);
809 }
810 }
811 }
812 NOTE("");
813 NOTE("/* attribute lists for the runtime (generated by dtd_util) */");
814 for (j = 0; j < (int) count; ++j) {
815 dump_src_HTTag_Attrs(output, dtd, data[j].which);
816 }
817 NOTE("/* *INDENT-ON* */");
818 NOTE("");
819 NOTE("/* justification-flags */");
820 NOTE("#undef N");
821 NOTE("#undef i");
822 NOTE("#undef h");
823 NOTE("#undef c");
824 NOTE("#undef x");
825 NOTE("");
826 NOTE("#undef T");
827 NOTE("");
828 NOTE("/* tag-names */");
829 /* these may be needed as parameters to macros */
830 for (j = 0; j <= dtd->number_of_tags; ++j) {
831 fprintf(output, "#undef %s\n", DEF_name(dtd, j, 0));
832 }
833 NOTE("");
834 NOTE("/* these definitions are used in the tags-tables */");
835 NOTE("#undef P");
836 NOTE("#undef P_");
837 NOTE("#ifdef USE_COLOR_STYLE");
838 NOTE("#define P_(x) #x, (sizeof #x) -1");
839 NOTE("#define NULL_HTTag_ NULL, 0");
840 NOTE("#else");
841 NOTE("#define P_(x) #x");
842 NOTE("#define NULL_HTTag_ NULL");
843 NOTE("#endif");
844 NOTE("");
845 NOTE("#ifdef USE_JUSTIFY_ELTS");
846 NOTE("#define P(x) P_(x), 1");
847 NOTE("#define P0(x) P_(x), 0");
848 NOTE("#define NULL_HTTag NULL_HTTag_,0");
849 NOTE("#else");
850 NOTE("#define P(x) P_(x)");
851 NOTE("#define P0(x) P_(x)");
852 NOTE("#define NULL_HTTag NULL_HTTag_");
853 NOTE("#endif");
854 NOTE("");
855 NOTE("#define ATTR_DATA(name) name##_attr, HTML_##name##_ATTRIBUTES, name##_attr_type");
856 NOTE("");
857 NOTE("#endif /* once_HTMLDTD */");
858 NOTE("/* *INDENT-OFF* */");
859
860 /* construct the tags table */
861 fprintf(output,
862 "static const HTTag tags_table%d[HTML_ALL_ELEMENTS] = {\n",
863 dtd_version);
864 aliases = 0;
865 inalias = -1;
866 for (j = dtd->number_of_tags - 1; j >= 0; --j) {
867 dtd->tags[j].aliases = aliases;
868 if (aliases != 0) {
869 if ((inalias - aliases) >= j) {
870 aliases = 0;
871 inalias = -1;
872 }
873 } else if ((aliases = dtd->tags[j].alias) != 0) {
874 inalias = j;
875 dtd->tags[j].aliases = aliases;
876 }
877 }
878 for (j = 0; j <= dtd->number_of_tags; ++j) {
879 if (j == dtd->number_of_tags) {
880 NOTE("/* additional (alternative variants), not counted in HTML_ELEMENTS: */");
881 NOTE("/* This one will be used as a temporary substitute within the parser when");
882 NOTE(" it has been signalled to parse OBJECT content as MIXED. - kw */");
883 }
884 dump_src_HTTag(output, dtd, j);
885 }
886 fprintf(output, "};\n");
887
888 NOTE("/* *INDENT-ON* */");
889 NOTE("");
890 fprintf(output, "#endif /* %s%d */\n", marker, dtd_version);
891
892 free(data);
893 }
894
895 static void dump_hdr_attr(FILE *output, AttrInfo * data)
896 {
897 int j;
898 char buffer[BUFSIZ];
899
900 for (j = 0; j < data->count; ++j) {
901 PrintF(output, 33, "#define HTML_%s_%s",
902 data->name,
903 no_dashes(buffer, data->attrs[j].name));
904 fprintf(output, "%2d\n", j);
905 }
906 PrintF(output, 33, "#define HTML_%s_ATTRIBUTES", data->name);
907 fprintf(output, "%2d\n", data->count);
908 fprintf(output, "\n");
909 }
910
911 static void dump_header(FILE *output, const SGML_dtd * dtd)
912 {
913 const char *marker = "hdr_HTMLDTD_H";
914 int j;
915
916 unsigned count = 0;
917 AttrInfo *data = sorted_attrs(dtd, &count);
918
919 fprintf(output, "/* %cLynxId%c */\n", '$', '$');
920 fprintf(output, "#ifndef %s\n", marker);
921 fprintf(output, "#define %s 1\n\n", marker);
922
923 NOTE("#ifdef __cplusplus");
924 NOTE("extern \"C\" {");
925 NOTE("#endif");
926
927 NOTE("/*");
928 NOTE("");
929 NOTE(" Element Numbers");
930 NOTE("");
931 NOTE(" Must Match all tables by element!");
932 NOTE(" These include tables in HTMLDTD.c");
933 NOTE(" and code in HTML.c.");
934 NOTE("");
935 NOTE(" */");
936
937 fprintf(output, " typedef enum {\n");
938 for (j = 0; j < dtd->number_of_tags; ++j) {
939 if (dtd->tags[j].alias) {
940 fprintf(output, "\tHTML_%s_%d,\n", dtd->tags[j].name,
941 dtd->tags[j].alias + 1);
942 } else {
943 fprintf(output, "\tHTML_%s,\n", dtd->tags[j].name);
944 }
945 }
946 NOTE("\tHTML_ALT_OBJECT");
947 NOTE(" } HTMLElement;\n");
948 NOTE("/* Notes: HTML.c uses a different extension of the");
949 NOTE(" HTML_ELEMENTS space privately, see");
950 NOTE(" HTNestedList.h.");
951 NOTE("");
952 NOTE(" Do NOT replace HTML_ELEMENTS with");
953 NOTE(" TABLESIZE(mumble_dtd.tags).");
954 NOTE("");
955 NOTE(" Keep the following defines in synch with");
956 NOTE(" the above enum!");
957 NOTE(" */");
958 NOTE("");
959 NOTE("/* # of elements generally visible to Lynx code */");
960 fprintf(output, "#define HTML_ELEMENTS %d\n", dtd->number_of_tags);
961 NOTE("");
962 NOTE("/* # of elements visible to SGML parser */");
963 fprintf(output, "#define HTML_ALL_ELEMENTS %d\n", dtd->number_of_tags + 1);
964 NOTE("");
965 NOTE("/*");
966 NOTE("");
967 NOTE(" Attribute numbers");
968 NOTE("");
969 NOTE(" Identifier is HTML_<element>_<attribute>.");
970 NOTE(" These must match the tables in HTML.c!");
971 NOTE("");
972 NOTE(" */");
973
974 /* output the sorted list */
975 for (j = 0; j < (int) count; ++j) {
976 dump_hdr_attr(output, data + j);
977 }
978 free(data);
979
980 NOTE("#ifdef __cplusplus");
981 NOTE("}");
982 NOTE("#endif");
983
984 fprintf(output, "#endif\t\t\t\t/* %s */\n", marker);
985 }
986
987 static void dump_flat_attrs(FILE *output,
988 const attr * attributes,
989 int number_of_attributes)
990 {
991 int n;
992
993 fprintf(output, "\t\t" FMT_NUM_ATTRS "\n", number_of_attributes);
994 for (n = 0; n < number_of_attributes; ++n) {
995 fprintf(output, "\t\t\t" FMT_ONE_ATTR "\n", n,
996 #ifdef USE_PRETTYSRC
997 attributes[n].type,
998 #else
999 0, /* need placeholder for source-compat */
1000 #endif
1001 attributes[n].name
1002 );
1003 }
1004 }
1005
1006 static void dump_flat_attr_types(FILE *output, const AttrType * attr_types)
1007 {
1008 const AttrType *p = sorted_AttrTypes(attr_types);
1009 int number = len_AttrTypes(attr_types);
1010
1011 fprintf(output, "\t\t" FMT_NUM_ATTR_TYPES "\n", number);
1012
1013 if (p != 0) {
1014 while (p->name != 0) {
1015 fprintf(output, "\t\t\t%s\n", p->name);
1016 ++p;
1017 }
1018 }
1019 }
1020
1021 static void dump_flat_SGMLContent(FILE *output, const char *name, SGMLContent contents)
1022 {
1023 fprintf(output, "\t\t%s: %s\n", name, SGMLContent2s(contents));
1024 }
1025
1026 #define DUMP(name) \
1027 if (theClass & Tgc_##name) {\
1028 fprintf(output, " " #name); \
1029 theClass &= ~(Tgc_##name); \
1030 }
1031
1032 static void dump_flat_TagClass(FILE *output, const char *name, TagClass theClass)
1033 {
1034 fprintf(output, "\t\t%s:", name);
1035 DUMP(FONTlike);
1036 DUMP(EMlike);
1037 DUMP(MATHlike);
1038 DUMP(Alike);
1039 DUMP(formula);
1040 DUMP(TRlike);
1041 DUMP(SELECTlike);
1042 DUMP(FORMlike);
1043 DUMP(Plike);
1044 DUMP(DIVlike);
1045 DUMP(LIlike);
1046 DUMP(ULlike);
1047 DUMP(BRlike);
1048 DUMP(APPLETlike);
1049 DUMP(HRlike);
1050 DUMP(MAPlike);
1051 DUMP(outer);
1052 DUMP(BODYlike);
1053 DUMP(HEADstuff);
1054 DUMP(same);
1055 DUMP(DELlike);
1056 if (theClass)
1057 fprintf(output, " OOPS:%#x", theClass);
1058 fprintf(output, "\n");
1059 }
1060
1061 #undef DUMP
1062
1063 #define DUMP(name) \
1064 if (theFlags & Tgf_##name) {\
1065 fprintf(output, " " #name); \
1066 theFlags &= ~(Tgf_##name); \
1067 }
1068
1069 static void dump_flat_TagFlags(FILE *output, const char *name, TagFlags theFlags)
1070 {
1071 fprintf(output, "\t\t%s:", name);
1072 DUMP(endO);
1073 DUMP(startO);
1074 DUMP(mafse);
1075 DUMP(strict);
1076 DUMP(nreie);
1077 DUMP(frecyc);
1078 DUMP(nolyspcl);
1079 if (theFlags)
1080 fprintf(output, " OOPS:%#x", theFlags);
1081 fprintf(output, "\n");
1082 }
1083
1084 #undef DUMP
1085
1086 static void dump_flat_HTTag(FILE *output, unsigned n, HTTag * tag)
1087 {
1088 fprintf(output, "\t%u:%s\n", n, EXT_name(tag));
1089 #ifdef USE_JUSTIFY_ELTS
1090 fprintf(output, "\t\t%s\n", tag->can_justify ? "justify" : "nojustify");
1091 #endif
1092 dump_flat_attrs(output, tag->attributes, AttrCount(tag));
1093 dump_flat_attr_types(output, tag->attr_types);
1094 dump_flat_SGMLContent(output, "contents", tag->contents);
1095 dump_flat_TagClass(output, "tagclass", tag->tagclass);
1096 dump_flat_TagClass(output, "contains", tag->contains);
1097 dump_flat_TagClass(output, "icontains", tag->icontains);
1098 dump_flat_TagClass(output, "contained", tag->contained);
1099 dump_flat_TagClass(output, "icontained", tag->icontained);
1100 dump_flat_TagClass(output, "canclose", tag->canclose);
1101 dump_flat_TagFlags(output, "flags", tag->flags);
1102 }
1103
1104 static int count_attr_types(AttrType * attr_types, HTTag * tag)
1105 {
1106 int count = 0;
1107 const AttrType *p;
1108 AttrType *q;
1109
1110 if ((p = tag->attr_types) != 0) {
1111 while (p->name != 0) {
1112 if ((q = attr_types) != 0) {
1113 while (q->name != 0) {
1114 if (!strcmp(q->name, p->name)) {
1115 --count;
1116 break;
1117 }
1118 ++q;
1119 }
1120 *q = *p;
1121 }
1122 ++count;
1123 ++p;
1124 }
1125 }
1126 return count;
1127 }
1128
1129 static void dump_flatfile(FILE *output, const SGML_dtd * dtd)
1130 {
1131 AttrType *attr_types = 0;
1132 int pass;
1133 unsigned count = 0;
1134 unsigned n;
1135
1136 /* merge all of the attr_types data */
1137 for (pass = 0; pass < 2; ++pass) {
1138 for (n = 0; (int) n < dtd->number_of_tags; ++n) {
1139 count += count_attr_types(attr_types, &(dtd->tags[n]));
1140 }
1141 if (pass == 0) {
1142 attr_types = typecallocn(AttrType, count + 1);
1143 count = 0;
1144 } else {
1145 count = len_AttrTypes(attr_types);
1146 qsort(attr_types, count, sizeof(*attr_types), compare_attr_types);
1147 fprintf(output, FMT_NUM_ATTR_TYPES "\n", count);
1148 for (n = 0; n < count; ++n) {
1149 fprintf(output, "\t%d:%s\n", n, attr_types[n].name);
1150 dump_flat_attrs(output, attr_types[n].list,
1151 len_AttrList(attr_types[n].list));
1152 }
1153 }
1154 }
1155
1156 fprintf(output, "%d tags\n", dtd->number_of_tags);
1157 for (n = 0; (int) n < dtd->number_of_tags; ++n) {
1158 dump_flat_HTTag(output, n, &(dtd->tags[n]));
1159 }
1160 #if 0
1161 fprintf(output, "%d entities\n", dtd->number_of_entities);
1162 for (n = 0; n < dtd->number_of_entities; ++n) {
1163 }
1164 #endif
1165 }
1166
1167 #define LOAD(name) \
1168 if (!strcmp(data, #name)) {\
1169 *theClass |= Tgc_##name; \
1170 continue; \
1171 }
1172
1173 static int load_flat_TagClass(FILE *input, const char *name, TagClass * theClass)
1174 {
1175 char prefix[80];
1176 char *next = get_line(input);
1177 char *data;
1178 int result = 0;
1179
1180 *theClass = 0;
1181 if (next != 0) {
1182 sprintf(prefix, "\t\t%s:", name);
1183 data = strtok(next, "\n ");
1184
1185 if (data != 0 && !strcmp(data, prefix)) {
1186 result = 1;
1187
1188 while ((data = strtok(NULL, "\n ")) != 0) {
1189
1190 LOAD(FONTlike);
1191 LOAD(EMlike);
1192 LOAD(MATHlike);
1193 LOAD(Alike);
1194 LOAD(formula);
1195 LOAD(TRlike);
1196 LOAD(SELECTlike);
1197 LOAD(FORMlike);
1198 LOAD(Plike);
1199 LOAD(DIVlike);
1200 LOAD(LIlike);
1201 LOAD(ULlike);
1202 LOAD(BRlike);
1203 LOAD(APPLETlike);
1204 LOAD(HRlike);
1205 LOAD(MAPlike);
1206 LOAD(outer);
1207 LOAD(BODYlike);
1208 LOAD(HEADstuff);
1209 LOAD(same);
1210 LOAD(DELlike);
1211
1212 warning("Unexpected TagClass '%s'", data);
1213 result = 0;
1214 break;
1215 }
1216 } else if (data) {
1217 warning("load_flat_TagClass: '%s' vs '%s'", data, prefix);
1218 }
1219 free(next);
1220 } else {
1221 warning("Did not find contents");
1222 }
1223 return result;
1224 }
1225
1226 #undef LOAD
1227
1228 #define LOAD(name) \
1229 if (!strcmp(data, #name)) {\
1230 *flags |= Tgf_##name; \
1231 continue; \
1232 }
1233
1234 static int load_flat_TagFlags(FILE *input, const char *name, TagFlags * flags)
1235 {
1236 char prefix[80];
1237 char *next = get_line(input);
1238 char *data;
1239 int result = 0;
1240
1241 *flags = 0;
1242 if (next != 0) {
1243 sprintf(prefix, "\t\t%s:", name);
1244 data = strtok(next, "\n ");
1245
1246 if (data != 0 && !strcmp(data, prefix)) {
1247 result = 1;
1248
1249 while ((data = strtok(NULL, "\n ")) != 0) {
1250
1251 LOAD(endO);
1252 LOAD(startO);
1253 LOAD(mafse);
1254 LOAD(strict);
1255 LOAD(nreie);
1256 LOAD(frecyc);
1257 LOAD(nolyspcl);
1258
1259 warning("Unexpected TagFlag '%s'", data);
1260 result = 0;
1261 break;
1262 }
1263 } else if (data) {
1264 warning("load_flat_TagFlags: '%s' vs '%s'", data, prefix);
1265 }
1266 free(next);
1267 }
1268 return result;
1269 }
1270
1271 #undef LOAD
1272
1273 static int load_flat_AttrList(FILE *input, AttrList * attrs, int *length)
1274 {
1275 attr *attributes;
1276 int j, jcmp, code;
1277 int result = 1;
1278 char name[MY_LIMIT];
1279 char *next;
1280 char trail[MY_LIMIT];
1281
1282 #ifdef USE_PRETTYSRC
1283 int atype;
1284 #endif
1285
1286 next = get_line(input);
1287 if (sscanf(next, FMT_NUM_ATTRS FMT_WS, length, trail) == 2
1288 && *length > 0
1289 && (attributes = typecallocn(attr, *length + 1)) != 0) {
1290 *attrs = attributes;
1291 for (j = 0; j < *length; ++j) {
1292 next = get_line(input);
1293 code = sscanf(next, FMT_ONE_ATTR FMT_WS, &jcmp, &atype, name, trail);
1294 if (code == NUM_ONE_ATTR) {
1295 if (j != jcmp)
1296 warning("resequence %s from %d to %d", name, jcmp, j);
1297 attributes[j].name = strdup(name);
1298 #ifdef USE_PRETTYSRC
1299 attributes[j].type = atype;
1300 #endif
1301 } else {
1302 warning("Did not find attributes");
1303 result = 0;
1304 break;
1305 }
1306 }
1307 if (*length > 1 && result)
1308 qsort(attributes, *length, sizeof(attributes[0]), compare_attr);
1309 } else {
1310 warning("expected attribute count:\n%s", next);
1311 }
1312 return result;
1313 }
1314
1315 /* find the given attribute in the list of attr_types */
1316 static int find_attribute(const char *attribute, const AttrType * attr_types)
1317 {
1318 int j, k;
1319 int found = -1;
1320
1321 for (j = 0; attr_types[j].name; ++j) {
1322 AttrList list = attr_types[j].list;
1323
1324 for (k = 0; list[k].name; ++k) {
1325 if (!strcmp(attribute, list[k].name)) {
1326 if (found >= 0)
1327 warning("attribute %s is in attr_types %s and %s",
1328 attribute,
1329 attr_types[found].name,
1330 attr_types[j].name);
1331 found = j;
1332 }
1333 }
1334 }
1335 return (found >= 0);
1336 }
1337
1338 static int load_flat_HTTag(FILE *input, HTTag * tag, AttrType * allTypes)
1339 {
1340 int result = 0;
1341 unsigned ncmp = 0;
1342 char name[MY_LIMIT];
1343 char trail[MY_LIMIT];
1344 char *next;
1345 int code;
1346 int j;
1347
1348 next = get_line(input);
1349 code = sscanf(next, "%d:%s" FMT_WS, &ncmp, name, trail);
1350 if (code == 3) {
1351 result = 1;
1352 tag->alias = 0;
1353 tag->aliases = 0;
1354 for (j = 0; name[j] != '\0'; ++j) {
1355 int ch = UCH(name[j]);
1356
1357 if (!isalnum(ch)) {
1358 if (strchr(alias_codes, ch) != NULL) {
1359 tag->alias = (ch - '!') + 1;
1360 name[j] = '\0';
1361 } else {
1362 result = 0;
1363 }
1364 break;
1365 }
1366 }
1367 tag->name = strdup(name);
1368 #ifdef USE_COLOR_STYLE
1369 tag->name_len = strlen(tag->name);
1370 #endif
1371 #ifdef USE_JUSTIFY_ELTS
1372 next = get_line(input);
1373 if (sscanf(next, "%s" FMT_WS, name, trail) == 2) {
1374 tag->can_justify = !strcmp(name, "justify");
1375 } else {
1376 warning("Did not find can_justify");
1377 result = 0;
1378 }
1379 #endif
1380 if (result) {
1381 result = load_flat_AttrList(input, &(tag->attributes), &(tag->number_of_attributes));
1382 }
1383 if (result) {
1384 AttrType *myTypes;
1385 int k, count;
1386
1387 if ((count = read_num_attr_types(input)) > 0
1388 && (myTypes = typecallocn(AttrType, count + 1)) != 0) {
1389 tag->attr_types = myTypes;
1390 for (k = 0; k < count; ++k) {
1391 next = get_line(input);
1392 if (next != 0
1393 && sscanf(next, "%s" FMT_WS, name, trail)) {
1394 int found = -1;
1395
1396 for (j = 0; allTypes[j].name != 0; ++j) {
1397 if (!strcmp(allTypes[j].name, name)) {
1398 myTypes[k].name = strdup(name);
1399 myTypes[k].list = allTypes[j].list;
1400 found = k;
1401 break;
1402 }
1403 }
1404 if (found < 0) {
1405 warning("not found in attr_types: %s", name);
1406 continue;
1407 }
1408 } else {
1409 result = 0;
1410 break;
1411 }
1412 }
1413 if (result) {
1414 if (count > 1)
1415 qsort(myTypes,
1416 count,
1417 sizeof(myTypes[0]),
1418 compare_attr_types);
1419 for (k = 0; k < tag->number_of_attributes; ++k) {
1420 if (!find_attribute(tag->attributes[k].name, tag->attr_types)) {
1421 warning("%s attribute %s not found in attr_types",
1422 tag->name, tag->attributes[k].name);
1423 }
1424 }
1425 }
1426 }
1427 }
1428 if (result) {
1429
1430 next = get_line(input);
1431 if (next != 0
1432 && sscanf(next, "\t\tcontents: %s" FMT_WS, name, trail)) {
1433 tag->contents = s2SGMLContent(name);
1434 free(next);
1435 } else {
1436 warning("Did not find contents");
1437 result = 0;
1438 }
1439 }
1440 if (result) {
1441 result = load_flat_TagClass(input, "tagclass", &(tag->tagclass));
1442 }
1443 if (result) {
1444 result = load_flat_TagClass(input, "contains", &(tag->contains));
1445 }
1446 if (result) {
1447 result = load_flat_TagClass(input, "icontains", &(tag->icontains));
1448 }
1449 if (result) {
1450 result = load_flat_TagClass(input, "contained", &(tag->contained));
1451 }
1452 if (result) {
1453 result = load_flat_TagClass(input, "icontained", &(tag->icontained));
1454 }
1455 if (result) {
1456 result = load_flat_TagClass(input, "canclose", &(tag->canclose));
1457 }
1458 if (result) {
1459 result = load_flat_TagFlags(input, "flags", &(tag->flags));
1460 }
1461 } else {
1462 warning("load_flat_HTTag error");
1463 }
1464 return result;
1465 }
1466
1467 static int load_flat_AttrType(FILE *input, AttrType * types, size_t ncmp)
1468 {
1469 int result = 0;
1470 int ntst;
1471 char name[MY_LIMIT];
1472 char trail[MY_LIMIT];
1473 char *next;
1474
1475 next = get_line(input);
1476 if (sscanf(next, "%d:%s" FMT_WS, &ntst, name, trail) == 3
1477 && (ntst == (int) ncmp)) {
1478 result = 1;
1479 types->name = strdup(name);
1480 if (!load_flat_AttrList(input, &(types->list), &ntst))
1481 result = 0;
1482 } else {
1483 warning("expected a tag:\n%s", next);
1484 }
1485 return result;
1486 }
1487
1488 static int compare_tags(const void *a, const void *b)
1489 {
1490 const HTTag *p = (const HTTag *) a;
1491 const HTTag *q = (const HTTag *) b;
1492 int result = 0;
1493
1494 if ((result = strcmp(p->name, q->name)) == 0) {
1495 result = p->alias - q->alias;
1496 }
1497 return result;
1498 }
1499
1500 static SGML_dtd *load_flatfile(FILE *input)
1501 {
1502 AttrType *attr_types = 0;
1503 SGML_dtd *result = 0;
1504 char *next;
1505 size_t n;
1506 int number_of_attrs = 0;
1507 int number_of_tags = 0;
1508 HTTag *tag;
1509 int code;
1510 char trail[MY_LIMIT];
1511
1512 if ((number_of_attrs = read_num_attr_types(input)) <= 0) {
1513 ;
1514 } else if ((attr_types = typecallocn(AttrType, number_of_attrs + 1)) == 0) {
1515 failed("calloc attr_types");
1516 }
1517
1518 for (n = 0; n < (size_t) number_of_attrs; ++n) {
1519 if (!load_flat_AttrType(input, attr_types + n, n)) {
1520 break;
1521 }
1522 }
1523
1524 next = get_line(input);
1525 code = sscanf(next, "%d tags" FMT_WS, &number_of_tags, trail);
1526 if (code == 2) {
1527 if ((result = typecalloc(SGML_dtd)) != 0
1528 && (result->tags = typecallocn(HTTag, number_of_tags + 2)) != 0) {
1529 for (n = 0; n < (size_t) number_of_tags; ++n) {
1530 if (load_flat_HTTag(input, &(result->tags[n]), attr_types)) {
1531 result->number_of_tags = (n + 1);
1532 } else {
1533 break;
1534 }
1535 }
1536 tag = 0;
1537 for (n = 0; n < (size_t) number_of_tags; ++n) {
1538 if (result->tags[n].name != 0
1539 && !strcmp(result->tags[n].name, "OBJECT")) {
1540 tag = result->tags + number_of_tags;
1541 *tag = result->tags[n];
1542 tag->contents = SGML_MIXED;
1543 tag->flags = Tgf_strict;
1544 break;
1545 }
1546 }
1547 if (tag == 0) {
1548 warning("Did not find OBJECT tag");
1549 result = 0;
1550 } else {
1551 qsort(result->tags, number_of_tags, sizeof(HTTag), compare_tags);
1552 }
1553 }
1554 } else {
1555 warning("expected tag count:\n%s", next);
1556 }
1557 return result;
1558 }
1559
1560 static void cross_check(FILE *output, const SGML_dtd * the_dtd, int level)
1561 {
1562 int ft;
1563
1564 fprintf(output, "Cross-check HTML DTD:\n");
1565 fprintf(output, "\n");
1566 /* make a sorted list of tags */
1567 /* for each tag in the list, find the classes it might contain */
1568 for (ft = 0; ft < the_dtd->number_of_tags; ++ft) {
1569 int xc;
1570 HTTag *ftag = &(the_dtd->tags[ft]);
1571
1572 /* for each contained-class, check if it says it can be contained */
1573 fprintf(output, "tag %s\n", EXT_name(ftag));
1574 for (xc = 0; class_list[xc].name != 0; ++xc) {
1575 int rt;
1576 int direct;
1577 int passes = 2;
1578
1579 /* most of the tags are (should be) symmetric */
1580 if (level <= 1) {
1581 BOOL same = TRUE;
1582
1583 if ((ftag->contains & class_list[xc].tagclass)
1584 != (ftag->icontains & class_list[xc].tagclass)) {
1585 same = FALSE;
1586 } else if ((ftag->contains & class_list[xc].tagclass) == 0) {
1587 continue;
1588 } else if (0) {
1589 HTTag *rtag;
1590
1591 for (rt = 0; rt < the_dtd->number_of_tags; ++rt) {
1592 rtag = &(the_dtd->tags[rt]);
1593 if (ftag == rtag)
1594 continue;
1595 if ((ftag->contains & rtag->tagclass) == 0)
1596 continue;
1597 if ((rtag->contained & ftag->tagclass)
1598 != (rtag->icontained & ftag->tagclass)) {
1599 same = FALSE;
1600 break;
1601 }
1602 }
1603 }
1604 if (same)
1605 passes = 1;
1606 }
1607
1608 for (direct = 0; direct < passes; ++direct) {
1609 TagClass check = (direct ? ftag->contains : ftag->icontains);
1610 BOOL first = TRUE;
1611
1612 if ((check &= class_list[xc].tagclass) == 0)
1613 continue;
1614
1615 for (rt = 0; rt < the_dtd->number_of_tags; ++rt) {
1616 HTTag *rtag = &(the_dtd->tags[rt]);
1617 TagClass check2 = (direct ? rtag->contained : rtag->icontained);
1618
1619 if (rt == ft)
1620 continue;
1621 if ((check & rtag->tagclass) == 0)
1622 continue;
1623 if ((check2 & ftag->tagclass) == 0)
1624 continue;
1625 if (first) {
1626 if (passes == 2) {
1627 fprintf(output, "\t%s (%s)\n",
1628 class_list[xc].name,
1629 direct ? "direct" : "indirect");
1630 } else {
1631 fprintf(output, "\t%s\n",
1632 class_list[xc].name);
1633 }
1634 }
1635 fprintf(output, "%s%s",
1636 first ? "\t\t" : " ",
1637 EXT_name(rtag));
1638 first = FALSE;
1639 }
1640 if (first) {
1641 if (level > 1) {
1642 if (passes == 2) {
1643 fprintf(output, "\t%s (%s)\n",
1644 class_list[xc].name,
1645 direct ? "direct" : "indirect");
1646 } else {
1647 fprintf(output, "\t%s\n",
1648 class_list[xc].name);
1649 }
1650 fprintf(output, "\t\t(missing)\n");
1651 }
1652 } else {
1653 fprintf(output, "\n");
1654 }
1655 }
1656 }
1657 }
1658 }
1659
1660 int main(int argc, char *argv[])
1661 {
1662 const SGML_dtd *the_dtd = &HTML_dtd;
1663 int ch;
1664 int dtd_version = 0;
1665 int c_option = FALSE;
1666 int h_option = FALSE;
1667 int l_option = FALSE;
1668 int x_option = 0;
1669 FILE *input = stdin;
1670 FILE *output = stdout;
1671
1672 input_filename = "<builtin>";
1673 input_lineno = 0;
1674
1675 while ((ch = getopt(argc, argv, GETOPT)) != -1) {
1676 switch (ch) {
1677 case 'c':
1678 c_option = TRUE;
1679 break;
1680 case 'h':
1681 h_option = TRUE;
1682 break;
1683 case 'l':
1684 l_option = TRUE;
1685 input = fopen((input_filename = optarg), "r");
1686 if (input == 0)
1687 failed(optarg);
1688 break;
1689 case 'o':
1690 output = fopen(optarg, "w");
1691 if (output == 0)
1692 failed(optarg);
1693 break;
1694 case 't':
1695 dtd_version = 1;
1696 break;
1697 case 's':
1698 dtd_version = 0;
1699 break;
1700 case 'x':
1701 ++x_option;
1702 break;
1703 default:
1704 usage();
1705 }
1706 }
1707
1708 HTSwitchDTD(dtd_version);
1709 if (l_option)
1710 the_dtd = load_flatfile(input);
1711
1712 if (the_dtd != 0) {
1713 if (c_option)
1714 dump_source(output, the_dtd, dtd_version);
1715 if (h_option)
1716 dump_header(output, the_dtd);
1717 if (x_option)
1718 cross_check(output, the_dtd, x_option);
1719 if (!c_option && !h_option && !x_option)
1720 dump_flatfile(output, the_dtd);
1721 }
1722
1723 return EXIT_SUCCESS;
1724 }