Add back the fix from revision 54693.
[metze/wireshark/wip.git] / epan / uat_load.l
1 /*
2  * We don't use input, so don't generate code for it.
3  */
4 %option noinput
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  * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
18  * can coexist with other scanners.
19  */
20 %option prefix="uat_load_"
21
22 %{
23         /*
24          * uat_load.l
25          *
26          * $Id$
27          *
28          *  User Accessible Tables
29          *  Mantain an array of user accessible data strucures
30          *  One parser to fit them all
31          *
32          * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
33          *
34          * Wireshark - Network traffic analyzer
35          * By Gerald Combs <gerald@wireshark.org>
36          * Copyright 2001 Gerald Combs
37          *
38          * This program is free software; you can redistribute it and/or
39          * modify it under the terms of the GNU General Public License
40          * as published by the Free Software Foundation; either version 2
41          * of the License, or (at your option) any later version.
42          *
43          * This program is distributed in the hope that it will be useful,
44          * but WITHOUT ANY WARRANTY; without even the implied warranty of
45          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
46          * GNU General Public License for more details.
47          *
48          * You should have received a copy of the GNU General Public License
49          * along with this program; if not, write to the Free Software
50          * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
51          */
52 #include "config.h"
53
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <errno.h>
58 #include <ctype.h>
59
60 #include <glib.h>
61
62 #include <epan/emem.h>
63 #include "uat-int.h"
64 #include "uat_load_lex.h"
65 #include <wsutil/file_util.h>
66
67 #ifdef _WIN32
68 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated  */
69 /* with YY_INPUT code generated by flex versions such as 2.5.35.              */
70 #pragma warning (disable:4018)
71 #endif
72
73 static uat_t* uat;
74 static gboolean valid_record;
75 static guint colnum;
76 static gchar* ptrx;
77 static guint len;
78 static gchar* error;
79 static void* record;
80 static guint linenum;
81 static gchar *parse_str;
82 static guint parse_str_pos;
83
84 #define ERROR(fmtd) do { \
85     error = ep_strdup_printf("%s:%d: %s",uat->filename,linenum,ep_strdup_printf fmtd); \
86     yyterminate(); \
87 } while(0)
88
89 #define SET_FIELD() \
90         { const gchar* errx; \
91         if (uat->fields[colnum].cb.chk) { \
92                 if ( ! uat->fields[colnum].cb.chk(record, ptrx, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data, &errx) ) { \
93                         error = ep_strdup_printf("%s:%d: %s",uat->filename,linenum,ep_strdup_printf("%s",errx)); \
94                         valid_record = FALSE; \
95                 }\
96         }\
97         uat->fields[colnum].cb.set(record, ptrx, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data);\
98         g_free(ptrx);\
99         colnum++; \
100         } while(0)
101
102 #ifdef DEBUG_UAT_LOAD
103 #define DUMP_FIELD(str) \
104                 { guint i; printf("%s: %s='",str,uat->fields[colnum].name); for(i=0;i<len;i++) if (uat->fields[colnum].mode == PT_TXTMOD_HEXBYTES) { printf("%.2x ",((guint8*)ptrx)[i]); } else putc(ptrx[i],stdout); printf("'[%d]\n",len); }
105
106 #define DUMP(str) printf("%s\n",str)
107 #else
108 #define DUMP_FIELD(s)
109 #define DUMP(s)
110 #endif
111
112 /* Modified version of YY_INPUT generated by Flex 2.91 */
113 #define YY_INPUT(buf,result,max_size) \
114         if ( parse_str ) \
115                 { \
116                 size_t n = 0; \
117                 size_t pslen = strlen(parse_str); \
118                 if (parse_str_pos < pslen) \
119                         { \
120                         n = pslen - parse_str_pos; \
121                         if (n > max_size) n = max_size; \
122                         memcpy(buf, parse_str + parse_str_pos, n); \
123                         parse_str_pos += n; \
124                         } \
125                 result = n; \
126                 } \
127         else \
128                 { \
129                 errno=0; \
130                 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
131                         { \
132                         if( errno != EINTR) \
133                                 { \
134                                 YY_FATAL_ERROR( "input in flex scanner failed" ); \
135                                 break; \
136                                 } \
137                         errno=0; \
138                         clearerr(yyin); \
139                         } \
140                 }
141
142                 /*
143                  * XXX
144                  * quoted_string below fails badly on "...\\"
145                  * workarround in uat_save(), using /x5c and /x22
146                  */
147 %}
148
149 quoted_string \042([^\042]|\134\042)*\042
150 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
151 separator [ \t]*,
152 newline [ \t]*[\r]?\n
153 ws [ \t]+
154 comment #[^\n]*\n
155
156 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
157 %%
158 <START_OF_LINE,NEXT_FIELD>{ws} ;
159 <START_OF_LINE>{newline} linenum++;
160 <START_OF_LINE>{comment} linenum++;
161
162 <START_OF_LINE,NEXT_FIELD>{separator} {
163         ptrx = g_strdup("");
164         len = 0;
165
166         DUMP_FIELD("empty->next");
167
168         SET_FIELD();
169
170         if ( colnum >= uat->ncols ) {
171                 ERROR(("more fields than required"));
172         }
173
174         BEGIN NEXT_FIELD;
175 }
176
177 <START_OF_LINE,NEXT_FIELD>{newline}   {
178         ptrx = g_strdup("");
179         len = 0;
180
181         BEGIN END_OF_RECORD;
182
183         yyless((int) yyleng);
184 }
185
186 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
187         ptrx = uat_undquote(yytext, (guint) yyleng, &len);
188
189
190         if (colnum < uat->ncols - 1) {
191                 DUMP("quoted_str->s");
192                 BEGIN SEPARATOR;
193         } else {
194                 DUMP("quoted_str->eor");
195                 BEGIN END_OF_RECORD;
196         }
197 }
198
199 <START_OF_LINE,NEXT_FIELD>{binstring} {
200         ptrx = uat_unbinstring(yytext,  (guint) yyleng, &len);
201
202         if (!ptrx) {
203                 ERROR(("uneven hexstring for field %s",uat->fields[colnum].name));
204         }
205
206         if ( colnum < uat->ncols - 1 ) {
207                 DUMP("binstring->s");
208                 BEGIN SEPARATOR;
209         } else {
210                 DUMP("binstring->eor");
211                 BEGIN END_OF_RECORD;
212         }
213 }
214
215 <SEPARATOR>{separator} {
216
217         DUMP_FIELD("separator->next");
218
219         SET_FIELD();
220
221         if ( colnum >= uat->ncols ) {
222                 ERROR(("more fields than required"));
223         }
224
225         BEGIN NEXT_FIELD;
226 }
227
228 <SEPARATOR>{newline} {
229         linenum++;
230         ERROR(("expecting field %s in previous line",uat->fields[colnum].name));
231 }
232
233 <SEPARATOR>. {
234         ERROR(("unexpected char '%s' while looking for field %s",yytext,uat->fields[colnum].name));
235 }
236
237 <END_OF_RECORD>{separator} {
238         ERROR(("more fields than required"));
239 }
240
241 <END_OF_RECORD>{newline} {
242         void* rec;
243         const char* err = NULL;
244
245         linenum++;
246
247         DUMP_FIELD("newline->start");
248
249         SET_FIELD();
250
251         rec = uat_add_record(uat, record, valid_record);
252
253         if ((uat->update_cb) && (rec != NULL))
254                 uat->update_cb(rec,&err);
255
256         if (err) {
257                 char *tmp = ep_strdup(err);
258                 /* XXX bit of a hack to remove emem from dissectors, this can
259                  * be removed as proper use of glib memory is propogated
260                  * through the rest of the UAT code */
261                 g_free((char*)err);
262                 ERROR(("%s",tmp));
263         }
264
265         valid_record = TRUE;
266         colnum = 0;
267         ptrx = NULL;
268         len = 0;
269
270         /* XXX is this necessary since we free it before reusing anyway? */
271         memset(record,0,uat->record_size);
272
273         BEGIN START_OF_LINE;
274  }
275
276 <END_OF_RECORD>. {
277         ERROR(("unexpected char while looking for end of line"));
278 }
279
280 <ERRORED>{newline} { linenum++; BEGIN START_OF_LINE; }
281 <ERRORED>. ;
282
283 {newline} { linenum++; ERROR(("incomplete record")); BEGIN START_OF_LINE; }
284 . { ERROR(("unexpected input")); }
285
286 %%
287
288
289
290
291 gboolean
292 uat_load(uat_t *uat_in, const char **errx)
293 {
294         gchar *fname = uat_get_actual_filename(uat_in, FALSE);
295
296         uat = uat_in;
297         parse_str = NULL;
298
299         if (!fname) {
300                 UAT_UPDATE(uat);
301
302                 if (uat->post_update_cb)
303                         uat->post_update_cb();
304
305                 return TRUE;
306         }
307
308
309         if (!(yyin = ws_fopen(fname,"r"))) {
310                 *errx = g_strerror(errno);
311                 g_free(fname);
312                 return FALSE;
313         }
314
315         error = NULL;
316         valid_record = TRUE;
317         colnum = 0;
318         g_free(record);
319         record = g_malloc0(uat->record_size);
320         linenum = 1;
321
322         BEGIN START_OF_LINE;
323         DUMP(fname);
324         g_free(fname);  /* we're done with the file name now */
325
326         yylex();
327         fclose(yyin);
328         yyrestart(NULL);
329
330         uat->changed = FALSE;
331         uat->loaded = TRUE;
332         UAT_UPDATE(uat);
333
334         if (error) {
335                 *errx = ep_strdup(error);
336                 return FALSE;
337         }
338
339         if (uat->post_update_cb)
340                 uat->post_update_cb();
341
342         *errx = NULL;
343         return TRUE;
344 }
345
346 gboolean
347 uat_load_str(uat_t *uat_in, char *entry, char **err)
348 {
349         uat = uat_in;
350         parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
351         parse_str_pos = 0;
352         yyin = NULL;
353
354         error = NULL;
355         valid_record = TRUE;
356         colnum = 0;
357         g_free(record);
358         record = g_malloc0(uat->record_size);
359         linenum = 1;
360
361         BEGIN START_OF_LINE;
362         DUMP(entry);
363
364         yylex();
365         yyrestart(NULL);
366         g_free(parse_str);
367         parse_str = NULL;
368
369         uat->changed = TRUE;
370         uat->loaded = TRUE;
371         UAT_UPDATE(uat);
372
373         if (error) {
374                 *err = ep_strdup(error);
375                 return FALSE;
376         }
377
378         if (uat->post_update_cb)
379                 uat->post_update_cb();
380
381         *err = NULL;
382         return TRUE;
383 }
384
385 /*
386  * We want to stop processing when we get to the end of the input.
387  * (%option noyywrap is not used because if used then
388  * some flex versions (eg: 2.5.35) generate code which causes
389  * warnings by the Windows VC compiler).
390  */
391
392 int
393 yywrap(void)
394 {
395     return 1;
396 }