Add a script as a front-end for Flex, to work around various problems,
[obnox/wireshark/wip.git] / epan / dtd_preparse.l
1 /*
2  * We want to stop processing when we get to the end of the input.
3  */
4 %option noyywrap
5
6 /*
7  * We don't use unput, so don't generate code for it.
8  */
9 %option nounput
10
11 /*
12  * We don't read from the terminal.
13  */
14 %option never-interactive
15
16 /*
17  * The language we're scanning is case-insensitive.
18  */
19 %option caseless
20
21 /*
22  * Prefix scanner routines with "Dtd_PreParse_" rather than "yy", so this
23  * scanner can coexist with other scanners.
24  */
25 %option prefix="Dtd_PreParse_"
26
27 %option outfile="dtd_preparse.c"
28
29 %{
30         /*
31          * dtd_preparser.l
32          *
33          * an XML dissector for wireshark 
34          *
35          * DTD Preparser -  import a dtd file into a GString
36          *                                      including files, removing comments
37          *                  and resolving %entities;
38          * 
39          * Copyright 2004, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
40          *
41          * $Id$
42          *
43          * Wireshark - Network traffic analyzer
44          * By Gerald Combs <gerald@wireshark.org>
45          * Copyright 1998 Gerald Combs
46          *
47          * This program is free software; you can redistribute it and/or
48          * modify it under the terms of the GNU General Public License
49          * as published by the Free Software Foundation; either version 2
50          * of the License, or (at your option) any later version.
51          * 
52          * This program is distributed in the hope that it will be useful,
53          * but WITHOUT ANY WARRANTY; without even the implied warranty of
54          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
55          * GNU General Public License for more details.
56          * 
57          * You should have received a copy of the GNU General Public License
58          * along with this program; if not, write to the Free Software
59          * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
60          */
61                         
62 #include <glib.h>
63 #include <string.h>
64 #include <errno.h>
65 #include <stdio.h>
66 #include "dtd.h"
67 #include "dtd_preparse_lex.h"
68
69 #define ECHO g_string_append(current,yytext);
70
71 static GString* current;
72 static GString* output;
73 static GHashTable* entities;
74 static gchar* entity_name;
75 static GString* error;
76
77 static const gchar* dtd_dirname;
78 static const gchar* filename;
79 static guint linenum;
80
81 static gchar* replace_entity(gchar* s);
82 static const gchar* location(void);
83
84 %}
85 xmlpi_start "<?"
86 xmlpi_stop  "?>"
87 xmlpi_chars .
88
89 comment_start "<!--"
90 comment_stop "-->"
91 special_start "<!"
92 special_stop ">"
93
94 entity_start     "<!"[[:blank:]\n]*entity[[:blank:]\n]*"%"
95 system     SYSTEM
96 filename   [^"]+
97
98
99 name [A-Za-z][-:A-Za-z0-9_\.]*
100
101 quote "\""
102 percent [%]
103 escaped_quote "\\\""
104 non_quote [^"%]+
105
106 avoid_editor_bug ["]
107
108 entity        [%&][A-Za-z][-A-Za-z0-9_]*;
109
110 whitespace [[blank:]]+
111 newline    \n
112 %START OUTSIDE IN_COMMENT IN_ENTITY NAMED_ENTITY IN_QUOTE ENTITY_DONE XMLPI
113 %%
114
115
116 {entity}                                                if (current) g_string_sprintfa(current,"%s\n%s\n",replace_entity(yytext),location());
117
118 {whitespace}                                    if (current) g_string_append(current," ");
119
120 <OUTSIDE>{xmlpi_start}                  { g_string_append(current,yytext); BEGIN XMLPI; }
121 <XMLPI>{xmlpi_chars}                    { g_string_append(current,yytext); }
122 <XMLPI>{newline}                                { g_string_append(current,yytext); }
123 <XMLPI>{xmlpi_stop}                             { g_string_append(current,yytext); BEGIN OUTSIDE; }
124
125 <OUTSIDE>{comment_start}                { current = NULL; BEGIN IN_COMMENT; }
126 <IN_COMMENT>[^-]?                               |
127 <IN_COMMENT>[-]                                 ;
128 <IN_COMMENT>{comment_stop}              { current = output; BEGIN OUTSIDE; }
129         
130 {newline}                                               {
131         linenum++;
132         if (current) g_string_sprintfa(current,"%s\n",location());
133 }
134
135
136 <OUTSIDE>{entity_start}                 { BEGIN IN_ENTITY; }
137 <IN_ENTITY>{name}                               { entity_name = g_strdup_printf("%%%s;",yytext); BEGIN NAMED_ENTITY; }
138 <NAMED_ENTITY>{quote}                   { current = g_string_new(location()); BEGIN IN_QUOTE; }
139 <IN_QUOTE>{quote}                               { g_hash_table_insert(entities,entity_name,current);  BEGIN ENTITY_DONE; }
140 <IN_QUOTE>{percent}                             |
141 <IN_QUOTE>{non_quote}                   |
142 <IN_QUOTE>{escaped_quote}               g_string_append(current,yytext);
143 <NAMED_ENTITY>{system}                  {
144     g_string_sprintfa(error,"at %s:%u: file inclusion is not supported!", filename, linenum);
145     yyterminate();
146 }
147 <ENTITY_DONE>{special_stop}             { current = output; g_string_append(current,"\n"); BEGIN OUTSIDE; }
148
149 %%
150
151 static gchar* replace_entity(gchar* entity) {
152         GString* replacement;
153         
154         *entity = '%';
155         
156         replacement = g_hash_table_lookup(entities,entity);
157         
158         if (replacement) {
159                 return replacement->str;
160         } else {
161                 g_string_sprintfa(error,"dtd_preparse: in file '%s': entity %s does not exists\n", filename, entity);
162                 return "";
163         }
164         
165 }
166
167 static const gchar* location(void) {
168         static gchar* loc = NULL;
169     
170         if (loc) g_free(loc);
171     
172         loc = g_strdup_printf("<? wireshark:location %s:%u ?>", filename, linenum);
173         
174         return loc;
175 }
176
177 static gboolean free_gstring_hash_items(gpointer k,gpointer v,gpointer p _U_) {
178         g_free(k);
179         g_string_free(v,TRUE);
180         return TRUE;
181 }
182
183 extern GString* dtd_preparse(const gchar* dname,const  gchar* fname, GString* err) {
184         gchar* fullname = g_strdup_printf("%s%c%s",dname,G_DIR_SEPARATOR,fname);
185     
186         dtd_dirname = dname;
187         filename = fname;
188         linenum = 1;
189
190         yyin = fopen(fullname,"r");
191         
192         if (!yyin) {
193                 if (err)
194                         g_string_sprintfa(err, "Could not open file: '%s', error: %s",fullname,strerror(errno));
195                         
196                 return NULL;
197         }
198         
199         error = err;
200         
201         entities = g_hash_table_new(g_str_hash,g_str_equal);
202         current = output = g_string_new(location());
203         
204         BEGIN OUTSIDE;
205
206         yylex();
207         
208         fclose(yyin);
209         
210         yyrestart(NULL);
211
212         g_hash_table_foreach_remove(entities,free_gstring_hash_items,NULL);
213         g_hash_table_destroy(entities);
214
215     g_free(fullname);
216
217         return output;
218 }