1 /*
2  * Clean up an HTML file:
3  * Insert missing tags.
4  *
5  * Copyright © 1994-2000 World Wide Web Consortium
6  * See http://www.w3.org/Consortium/Legal/copyright-software
7  *
8  * 16 September 1997
9  * Bert Bos
10  * $Id: hxclean.c,v 1.4 2017/11/24 09:50:25 bbos Exp $
11  */
12 #include "config.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "export.h"
16 #include "types.e"
17 #include "tree.e"
18 #include "html.e"
19 #include "scan.e"
20 
21 static Tree tree;
22 
23 /* handle_error -- called when a parse error occurred */
handle_error(void * clientdata,const string s,int lineno)24 void handle_error(void *clientdata, const string s, int lineno)
25 {
26   fprintf(stderr, "%d: %s\n", lineno, s);
27 }
28 
29 /* start -- called before the first event is reported */
start(void)30 void* start(void)
31 {
32   tree = create();
33   return NULL;
34 }
35 
36 /* end -- called after the last even is reported */
end(void * clientdata)37 void end(void *clientdata)
38 {
39   /* skip */
40 }
41 
42 /* handle_comment -- called after a comment is parsed */
handle_comment(void * clientdata,string commenttext)43 void handle_comment(void *clientdata, string commenttext)
44 {
45   tree = append_comment(tree, commenttext);
46 }
47 
48 /* handle_text -- called after a tex chunk is parsed */
handle_text(void * clientdata,string text)49 void handle_text(void *clientdata, string text)
50 {
51   tree = append_text(tree, text);
52 }
53 
54 /* handle_decl -- called after a declaration is parsed */
handle_decl(void * clientdata,string gi,string fpi,string url)55 void handle_decl(void *clientdata, string gi,
56 		 string fpi, string url)
57 {
58   tree = append_declaration(tree, gi, fpi, url);
59 }
60 
61 /* handle_pi -- called after a PI is parsed */
handle_pi(void * clientdata,string pi_text)62 void handle_pi(void *clientdata, string pi_text)
63 {
64   tree = append_procins(tree, pi_text);
65 }
66 
67 /* handle_starttag -- called after a start tag is parsed */
handle_starttag(void * clientdata,string name,pairlist attribs)68 void handle_starttag(void *clientdata, string name, pairlist attribs)
69 {
70   tree = html_push(tree, name, attribs);
71 }
72 
73 /* handle_emptytag -- called after an empty tag is parsed */
handle_emptytag(void * clientdata,string name,pairlist attribs)74 void handle_emptytag(void *clientdata, string name, pairlist attribs)
75 {
76   tree = html_push(tree, name, attribs);
77 }
78 
79 /* handle_pop -- called after an endtag is parsed (name may be "") */
handle_endtag(void * clientdata,string name)80 void handle_endtag(void *clientdata, string name)
81 {
82   tree = html_pop(tree, name);
83 }
84 
85 
main(int argc,char * argv[])86 int main(int argc, char *argv[])
87 {
88   /* Bind the parser callback routines to our handlers */
89   set_error_handler(handle_error);
90   set_start_handler(start);
91   set_end_handler(end);
92   set_comment_handler(handle_comment);
93   set_text_handler(handle_text);
94   set_decl_handler(handle_decl);
95   set_pi_handler(handle_pi);
96   set_starttag_handler(handle_starttag);
97   set_emptytag_handler(handle_emptytag);
98   set_endtag_handler(handle_endtag);
99 
100   if (argc == 1) {
101     yyin = stdin;
102   } else if (argc == 2) {
103     yyin = fopen(argv[1], "r");
104     if (yyin == NULL) {
105       perror(argv[1]);
106       exit(2);
107     }
108   } else {
109     fprintf(stderr, "Version %s\n", VERSION);
110     fprintf(stderr, "Usage: %s [html-file]\n", argv[0]);
111     exit(1);
112   }
113   if (yyparse() != 0) {
114     exit(3);
115   }
116   tree = get_root(tree);
117   dumptree(tree, stdout);
118   return 0;
119 
120 }
121