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