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