emem -> wmem
[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 { error = ep_strdup_printf("%s:%d: %s",uat->filename,linenum,ep_strdup_printf fmtd); yyterminate(); } while(0)
85
86 #define SET_FIELD() \
87         { const gchar* errx; \
88         if (uat->fields[colnum].cb.chk) { \
89                 if ( ! uat->fields[colnum].cb.chk(record, ptrx, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data, &errx) ) { \
90                         error = ep_strdup_printf("%s:%d: %s",uat->filename,linenum,ep_strdup_printf("%s",errx)); \
91                         valid_record = FALSE; \
92                 }\
93         }\
94         uat->fields[colnum].cb.set(record, ptrx, len, uat->fields[colnum].cbdata.chk, uat->fields[colnum].fld_data);\
95         g_free(ptrx);\
96         colnum++; \
97         } while(0)
98
99 #ifdef DEBUG_UAT_LOAD
100 #define DUMP_FIELD(str) \
101                 { 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); }
102
103 #define DUMP(str) printf("%s\n",str)
104 #else
105 #define DUMP_FIELD(s)
106 #define DUMP(s)
107 #endif
108
109 /* Modified version of YY_INPUT generated by Flex 2.91 */
110 #define YY_INPUT(buf,result,max_size) \
111         if ( parse_str ) \
112                 { \
113                 int n = 0; \
114                 guint pslen = strlen(parse_str); \
115                 if (parse_str_pos < pslen) \
116                         { \
117                         n = pslen - parse_str_pos; \
118                         if (n > max_size) n = max_size; \
119                         memcpy(buf, parse_str + parse_str_pos, n); \
120                         parse_str_pos += n; \
121                         } \
122                 result = n; \
123                 } \
124         else \
125                 { \
126                 errno=0; \
127                 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
128                         { \
129                         if( errno != EINTR) \
130                                 { \
131                                 YY_FATAL_ERROR( "input in flex scanner failed" ); \
132                                 break; \
133                                 } \
134                         errno=0; \
135                         clearerr(yyin); \
136                         } \
137                 }
138
139                 /*
140                  * XXX
141                  * quoted_string below fails badly on "...\\"
142                  * workarround in uat_save(), using /x5c and /x22
143                  */
144 %}
145
146 quoted_string \042([^\042]|\134\042)*\042
147 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
148 separator [ \t]*,
149 newline [ \t]*[\r]?\n
150 ws [ \t]+
151 comment #[^\n]*\n
152
153 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
154 %%
155 <START_OF_LINE,NEXT_FIELD>{ws} ;
156 <START_OF_LINE>{newline} linenum++;
157 <START_OF_LINE>{comment} linenum++;
158
159 <START_OF_LINE,NEXT_FIELD>{separator} {
160         ptrx = g_strdup("");
161         len = 0;
162
163         DUMP_FIELD("empty->next");
164
165         SET_FIELD();
166
167         if ( colnum >= uat->ncols ) {
168                 ERROR(("more fields than required"));
169         }
170
171         BEGIN NEXT_FIELD;
172 }
173
174 <START_OF_LINE,NEXT_FIELD>{newline}   {
175         ptrx = g_strdup("");
176         len = 0;
177
178         BEGIN END_OF_RECORD;
179
180         yyless(yyleng);
181 }
182
183 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
184         ptrx = uat_undquote(yytext,yyleng,&len);
185
186
187         if (colnum < uat->ncols - 1) {
188                 DUMP("quoted_str->s");
189                 BEGIN SEPARATOR;
190         } else {
191                 DUMP("quoted_str->eor");
192                 BEGIN END_OF_RECORD;
193         }
194 }
195
196 <START_OF_LINE,NEXT_FIELD>{binstring} {
197         ptrx = uat_unbinstring(yytext,yyleng,&len);
198
199         if (!ptrx) {
200                 ERROR(("uneven hexstring for field %s",uat->fields[colnum].name));
201         }
202
203         if ( colnum < uat->ncols - 1 ) {
204                 DUMP("binstring->s");
205                 BEGIN SEPARATOR;
206         } else {
207                 DUMP("binstring->eor");
208                 BEGIN END_OF_RECORD;
209         }
210 }
211
212 <SEPARATOR>{separator} {
213
214         DUMP_FIELD("separator->next");
215
216         SET_FIELD();
217
218         if ( colnum >= uat->ncols ) {
219                 ERROR(("more fields than required"));
220         }
221
222         BEGIN NEXT_FIELD;
223 }
224
225 <SEPARATOR>{newline} {
226         linenum++;
227         ERROR(("expecting field %s in previous line",uat->fields[colnum].name));
228 }
229
230 <SEPARATOR>. {
231         ERROR(("unexpected char '%s' while looking for field %s",yytext,uat->fields[colnum].name));
232 }
233
234 <END_OF_RECORD>{separator} {
235         ERROR(("more fields than required"));
236 }
237
238 <END_OF_RECORD>{newline} {
239         void* rec;
240         const gchar* err = NULL;
241
242         linenum++;
243
244         DUMP_FIELD("newline->start");
245
246         SET_FIELD();
247
248         rec = uat_add_record(uat, record, valid_record);
249
250         if ((uat->update_cb) && (rec != NULL))
251                 uat->update_cb(rec,&err);
252
253         if (err) {
254                 ERROR(("%s",err));
255         }
256
257         valid_record = TRUE;
258         colnum = 0;
259         ptrx = NULL;
260         len = 0;
261
262         /* XXX is this necessary since we free it before reusing anyway? */
263         memset(record,0,uat->record_size);
264
265         BEGIN START_OF_LINE;
266  }
267
268 <END_OF_RECORD>. {
269         ERROR(("unexpected char while looking for end of line"));
270 }
271
272 <ERRORED>{newline} { linenum++; BEGIN START_OF_LINE; }
273 <ERRORED>. ;
274
275 {newline} { linenum++; ERROR(("incomplete record")); BEGIN START_OF_LINE; }
276 . { ERROR(("unexpected input")); }
277
278 %%
279
280
281
282
283 gboolean
284 uat_load(uat_t *uat_in, const char **errx)
285 {
286         gchar *fname = uat_get_actual_filename(uat_in, FALSE);
287
288         uat = uat_in;
289         parse_str = NULL;
290
291         if (!fname) {
292                 UAT_UPDATE(uat);
293
294                 if (uat->post_update_cb)
295                         uat->post_update_cb();
296
297                 return TRUE;
298         }
299
300
301         if (!(yyin = ws_fopen(fname,"r"))) {
302                 *errx = g_strerror(errno);
303                 g_free(fname);
304                 return FALSE;
305         }
306
307         error = NULL;
308         valid_record = TRUE;
309         colnum = 0;
310         g_free(record);
311         record = g_malloc0(uat->record_size);
312         linenum = 1;
313
314         BEGIN START_OF_LINE;
315         DUMP(fname);
316         g_free(fname);  /* we're done with the file name now */
317
318         yylex();
319         fclose(yyin);
320         yyrestart(NULL);
321
322         uat->changed = FALSE;
323         uat->loaded = TRUE;
324         UAT_UPDATE(uat);
325
326         if (error) {
327                 *errx = ep_strdup(error);
328                 return FALSE;
329         }
330
331         if (uat->post_update_cb)
332                 uat->post_update_cb();
333
334         *errx = NULL;
335         return TRUE;
336 }
337
338 gboolean
339 uat_load_str(uat_t *uat_in, char *entry, char **err)
340 {
341         uat = uat_in;
342         parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
343         parse_str_pos = 0;
344         yyin = NULL;
345
346         error = NULL;
347         valid_record = TRUE;
348         colnum = 0;
349         g_free(record);
350         record = g_malloc0(uat->record_size);
351         linenum = 1;
352
353         BEGIN START_OF_LINE;
354         DUMP(entry);
355
356         yylex();
357         yyrestart(NULL);
358         g_free(parse_str);
359         parse_str = NULL;
360
361         uat->changed = TRUE;
362         uat->loaded = TRUE;
363         UAT_UPDATE(uat);
364
365         if (error) {
366                 *err = ep_strdup(error);
367                 return FALSE;
368         }
369
370         if (uat->post_update_cb)
371                 uat->post_update_cb();
372
373         *err = NULL;
374         return TRUE;
375 }
376
377 /*
378  * We want to stop processing when we get to the end of the input.
379  * (%option noyywrap is not used because if used then
380  * some flex versions (eg: 2.5.35) generate code which causes
381  * warnings by the Windows VC compiler).
382  */
383
384 int
385 yywrap(void)
386 {
387     return 1;
388 }