Get rid of TestBigEndian and AC_C_BIGENDIAN.
[metze/wireshark/wip.git] / epan / uat_load.l
1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 }
5
6 /*
7  * We want a reentrant scanner.
8  */
9 %option reentrant
10
11 /*
12  * We don't use input, so don't generate code for it.
13  */
14 %option noinput
15
16 /*
17  * We don't use unput, so don't generate code for it.
18  */
19 %option nounput
20
21 /*
22  * We don't read interactively from the terminal.
23  */
24 %option never-interactive
25
26 /*
27  * We want to stop processing when we get to the end of the input.
28  */
29 %option noyywrap
30
31 /*
32  * The type for the state we keep for a scanner.
33  */
34 %option extra-type="uat_load_scanner_state_t *"
35
36 /*
37  * We have to override the memory allocators so that we don't get
38  * "unused argument" warnings from the yyscanner argument (which
39  * we don't use, as we have a global memory allocator).
40  *
41  * We provide, as macros, our own versions of the routines generated by Flex,
42  * which just call malloc()/realloc()/free() (as the Flex versions do),
43  * discarding the extra argument.
44  */
45 %option noyyalloc
46 %option noyyrealloc
47 %option noyyfree
48
49 /*
50  * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
51  * can coexist with other scanners.
52  */
53 %option prefix="uat_load_"
54
55 %{
56         /*
57          * uat_load.l
58          *
59          *  User Accessible Tables
60          *  Maintain an array of user accessible data strucures
61          *  One parser to fit them all
62          *
63          * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
64          *
65          * Wireshark - Network traffic analyzer
66          * By Gerald Combs <gerald@wireshark.org>
67          * Copyright 2001 Gerald Combs
68          *
69          * This program is free software; you can redistribute it and/or
70          * modify it under the terms of the GNU General Public License
71          * as published by the Free Software Foundation; either version 2
72          * of the License, or (at your option) any later version.
73          *
74          * This program is distributed in the hope that it will be useful,
75          * but WITHOUT ANY WARRANTY; without even the implied warranty of
76          * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
77          * GNU General Public License for more details.
78          *
79          * You should have received a copy of the GNU General Public License
80          * along with this program; if not, write to the Free Software
81          * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
82          */
83 #include <stdlib.h>
84 #include <stdio.h>
85 #include <string.h>
86 #include <errno.h>
87
88 #include <glib.h>
89
90 #include "uat-int.h"
91 #include <wsutil/file_util.h>
92
93 /*
94  * Disable diagnostics in the code generated by Flex.
95  */
96 DIAG_OFF_FLEX
97
98 typedef struct {
99         uat_t* uat;
100         gchar *parse_str;
101
102         gchar* error;
103         gboolean valid_record;
104         guint colnum;
105         gchar* ptrx;
106         guint len;
107         void* record;
108         guint linenum;
109         size_t parse_str_pos;
110 } uat_load_scanner_state_t;
111
112 /*
113  * Signal a fatal error and stops parsing.
114  * Since the record is internal to the parsing process, its contents must also
115  * be cleared before terminating. Any values that are not handled yet (ptrx)
116  * must also be freed.
117  */
118 #define ERROR(fmtd) do { \
119         char* fmt_str = g_strdup_printf fmtd; \
120         g_free(yyextra->error); \
121         yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
122         g_free(fmt_str); \
123         if (yyextra->uat->free_cb) { \
124                 yyextra->uat->free_cb(yyextra->record); \
125         } \
126         g_free(yyextra->ptrx); \
127         yyterminate(); \
128 } while(0)
129
130 /*
131  * Sets the field of the current (scanner-internal) record, using the parsed
132  * value. If the field validation function exists and returns an error, then
133  * the record is marked as invalid and an error message is stored such that it
134  * can be shown after parsing. (If other errors occur after this issue, then
135  * this message will be overwritten though.)
136  */
137 #define SET_FIELD() \
138         { gchar* errx; \
139         if (yyextra->uat->fields[yyextra->colnum].cb.chk) { \
140                 if ( ! yyextra->uat->fields[yyextra->colnum].cb.chk(yyextra->record, yyextra->ptrx, yyextra->len, yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data, &errx) ) { \
141                         g_free(yyextra->error); \
142                         yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
143                         g_free(errx); \
144                         yyextra->valid_record = FALSE; \
145                 }\
146         }\
147         yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, yyextra->ptrx, yyextra->len, yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data);\
148         g_free(yyextra->ptrx);\
149         yyextra->ptrx = NULL;\
150         yyextra->colnum++; \
151         } while(0)
152
153 #ifdef DEBUG_UAT_LOAD
154 #define DUMP_FIELD(str) \
155                 { guint i; printf("%s: %s='",str,yyextra->uat->fields[yyextra->colnum].name); for(i=0;i<yyextra->len;i++) if (yyextra->uat->fields[yyextra->colnum].mode == PT_TXTMOD_HEXBYTES) { printf("%.2x ",((guint8*)yyextra->ptrx)[i]); } else putc(yyextra->ptrx[i],stdout); printf("'[%d]\n",yyextra->len); }
156
157 #define DUMP(str) printf("%s\n",str)
158 #else
159 #define DUMP_FIELD(s)
160 #define DUMP(s)
161 #endif
162
163 /* Modified version of YY_INPUT generated by Flex 2.91 */
164 #define YY_INPUT(buf,result,max_size) \
165         if ( yyextra->parse_str ) \
166                 { \
167                 size_t n = 0; \
168                 size_t pslen = strlen(yyextra->parse_str); \
169                 if (yyextra->parse_str_pos < pslen) \
170                         { \
171                         n = pslen - yyextra->parse_str_pos; \
172                         if (n > max_size) n = max_size; \
173                         memcpy(buf, yyextra->parse_str + yyextra->parse_str_pos, n); \
174                         yyextra->parse_str_pos += n; \
175                         } \
176                 result = n; \
177                 } \
178         else \
179                 { \
180                 errno=0; \
181                 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
182                         { \
183                         if( errno != EINTR) \
184                                 { \
185                                 YY_FATAL_ERROR( "input in flex scanner failed" ); \
186                                 break; \
187                                 } \
188                         errno=0; \
189                         clearerr(yyin); \
190                         } \
191                 }
192
193                 /*
194                  * XXX
195                  * quoted_string below fails badly on "...\\"
196                  * workarround in uat_save(), using /x5c and /x22
197                  */
198
199 #define YY_USER_INIT BEGIN START_OF_LINE;
200
201 /*
202  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
203  */
204 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
205
206 /*
207  * Macros for the allocators, to discard the extra argument.
208  */
209 #define uat_load_alloc(size, yyscanner)         (void *)malloc(size)
210 #define uat_load_realloc(ptr, size, yyscanner)  (void *)realloc((char *)(ptr), (size))
211 #define uat_load_free(ptr, yyscanner)           free((char *)ptr)
212
213 %}
214
215 quoted_string \042([^\042]|\134\042)*\042
216 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
217 separator [ \t]*,
218 newline [ \t]*[\r]?\n
219 ws [ \t]+
220 comment #[^\n]*\n
221
222 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
223 %%
224 <START_OF_LINE,NEXT_FIELD>{ws} ;
225 <START_OF_LINE>{newline} yyextra->linenum++;
226 <START_OF_LINE>{comment} yyextra->linenum++;
227
228 <START_OF_LINE,NEXT_FIELD>{separator} {
229         yyextra->ptrx = g_strdup("");
230         yyextra->len = 0;
231
232         DUMP_FIELD("empty->next");
233
234         SET_FIELD();
235
236         if ( yyextra->colnum >= yyextra->uat->ncols ) {
237                 ERROR(("more fields than required"));
238         }
239
240         BEGIN NEXT_FIELD;
241 }
242
243 <START_OF_LINE,NEXT_FIELD>{newline}   {
244         yyextra->ptrx = g_strdup("");
245         yyextra->len = 0;
246
247         BEGIN END_OF_RECORD;
248
249         yyless((int) yyleng);
250 }
251
252 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
253         yyextra->ptrx = uat_undquote(yytext, (guint) yyleng, &yyextra->len);
254
255
256         if (yyextra->colnum < yyextra->uat->ncols - 1) {
257                 DUMP("quoted_str->s");
258                 BEGIN SEPARATOR;
259         } else {
260                 DUMP("quoted_str->eor");
261                 BEGIN END_OF_RECORD;
262         }
263 }
264
265 <START_OF_LINE,NEXT_FIELD>{binstring} {
266         yyextra->ptrx = uat_unbinstring(yytext,  (guint) yyleng, &yyextra->len);
267
268         if (!yyextra->ptrx) {
269                 ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
270         }
271
272         if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
273                 DUMP("binstring->s");
274                 BEGIN SEPARATOR;
275         } else {
276                 DUMP("binstring->eor");
277                 BEGIN END_OF_RECORD;
278         }
279 }
280
281 <SEPARATOR>{separator} {
282
283         DUMP_FIELD("separator->next");
284
285         SET_FIELD();
286
287         if ( yyextra->colnum >= yyextra->uat->ncols ) {
288                 ERROR(("more fields than required"));
289         }
290
291         BEGIN NEXT_FIELD;
292 }
293
294 <SEPARATOR>{newline} {
295         yyextra->linenum++;
296         ERROR(("expecting field %s in previous line",yyextra->uat->fields[yyextra->colnum].name));
297 }
298
299 <SEPARATOR>. {
300         ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
301 }
302
303 <END_OF_RECORD>{separator} {
304         ERROR(("more fields than required"));
305 }
306
307 <END_OF_RECORD>{newline} {
308         void* rec;
309         char* err = NULL;
310
311         yyextra->linenum++;
312
313         DUMP_FIELD("newline->start");
314
315         SET_FIELD();
316
317         /* Last field was processed, try to store the full record in the UAT. */
318         rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
319
320         if ((yyextra->uat->update_cb) && (rec != NULL)) {
321                 if (!yyextra->uat->update_cb(rec,&err)) {
322                         g_free(yyextra->error);
323                         yyextra->error = err;
324                         yyterminate();
325                 }
326         }
327
328         /* The record was duplicated to the UAT above, now free our fields. */
329         if (yyextra->uat->free_cb) {
330                 yyextra->uat->free_cb(yyextra->record);
331         }
332         memset(yyextra->record, 0, yyextra->uat->record_size);
333
334         yyextra->valid_record = TRUE;
335         yyextra->colnum = 0;
336         yyextra->ptrx = NULL;
337         yyextra->len = 0;
338
339         BEGIN START_OF_LINE;
340  }
341
342 <END_OF_RECORD>. {
343         ERROR(("unexpected char while looking for end of line"));
344 }
345
346 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
347 <ERRORED>. ;
348
349 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
350 . { ERROR(("unexpected input")); }
351
352 %%
353
354 /*
355  * Turn diagnostics back on, so we check the code that we've written.
356  */
357 DIAG_ON_FLEX
358
359 gboolean
360 uat_load(uat_t *uat, char **errx)
361 {
362         gchar *fname = uat_get_actual_filename(uat, FALSE);
363         FILE *in;
364         yyscan_t scanner;
365         uat_load_scanner_state_t state;
366
367         if (!fname) {
368                 UAT_UPDATE(uat);
369
370                 if (uat->post_update_cb)
371                         uat->post_update_cb();
372
373                 return TRUE;
374         }
375
376
377         if (!(in = ws_fopen(fname,"r"))) {
378                 *errx = g_strdup(g_strerror(errno));
379                 g_free(fname);
380                 return FALSE;
381         }
382
383         if (uat_load_lex_init(&scanner) != 0) {
384                 *errx = g_strdup(g_strerror(errno));
385                 fclose(in);
386                 g_free(fname);
387                 return FALSE;
388         }
389
390         uat_load_set_in(in, scanner);
391
392         state.uat = uat;
393         state.parse_str = NULL; /* we're reading from a file */
394
395         state.error = NULL;
396         state.valid_record = TRUE;
397         state.colnum = 0;
398         state.ptrx = NULL;
399         state.len = 0;
400         state.record = g_malloc0(uat->record_size);
401         state.linenum = 1;
402         state.parse_str_pos = 0;
403
404         DUMP(fname);
405         g_free(fname);  /* we're done with the file name now */
406
407         /* Associate the state with the scanner */
408         uat_load_set_extra(&state, scanner);
409
410         uat_load_lex(scanner);
411
412         uat_load_lex_destroy(scanner);
413         g_free(state.record);
414         fclose(in);
415
416         uat->changed = FALSE;
417         uat->loaded = TRUE;
418         UAT_UPDATE(uat);
419
420         if (state.error) {
421                 *errx = state.error;
422                 return FALSE;
423         }
424
425         if (uat->post_update_cb)
426                 uat->post_update_cb();
427
428         *errx = NULL;
429         return TRUE;
430 }
431
432 gboolean
433 uat_load_str(uat_t *uat, char *entry, char **err)
434 {
435         yyscan_t scanner;
436         uat_load_scanner_state_t state;
437
438         state.uat = uat;
439         state.parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
440
441         state.error = NULL;
442         state.valid_record = TRUE;
443         state.colnum = 0;
444         state.ptrx = NULL;
445         state.len = 0;
446         state.record = g_malloc0(uat->record_size);
447         state.linenum = 1;
448         state.parse_str_pos = 0;
449
450         if (uat_load_lex_init(&scanner) != 0) {
451                 *err = g_strdup(g_strerror(errno));
452                 g_free(state.parse_str);
453                 g_free(state.record);
454                 return FALSE;
455         }
456
457         DUMP(entry);
458
459         /* Associate the state with the scanner */
460         uat_load_set_extra(&state, scanner);
461
462         uat_load_lex(scanner);
463
464         uat_load_lex_destroy(scanner);
465         g_free(state.record);
466         g_free(state.parse_str);
467
468         uat->changed = TRUE;
469         uat->loaded = TRUE;
470         UAT_UPDATE(uat);
471
472         if (state.error) {
473                 *err = state.error;
474                 return FALSE;
475         }
476
477         if (uat->post_update_cb)
478                 uat->post_update_cb();
479
480         *err = NULL;
481         return TRUE;
482 }