2 /* Include this before everything else, for various large-file definitions */
7 * We want a reentrant scanner.
12 * We don't use input, so don't generate code for it.
17 * We don't use unput, so don't generate code for it.
22 * We don't read interactively from the terminal.
24 %option never-interactive
27 * We want to stop processing when we get to the end of the input.
32 * The type for the state we keep for a scanner.
34 %option extra-type="uat_load_scanner_state_t *"
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).
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.
50 * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
51 * can coexist with other scanners.
53 %option prefix="uat_load_"
59 * User Accessible Tables
60 * Maintain an array of user accessible data strucures
61 * One parser to fit them all
63 * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
65 * Wireshark - Network traffic analyzer
66 * By Gerald Combs <gerald@wireshark.org>
67 * Copyright 2001 Gerald Combs
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.
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.
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.
91 #include <wsutil/file_util.h>
94 /* disable Windows VC compiler warning "signed/unsigned mismatch" associated */
95 /* with YY_INPUT code generated by flex versions such as 2.5.35. */
96 #pragma warning (disable:4018)
104 gboolean valid_record;
110 size_t parse_str_pos;
111 } uat_load_scanner_state_t;
114 * Signal a fatal error and stops parsing.
115 * Since the record is internal to the parsing process, its contents must also
116 * be cleared before terminating. Any values that are not handled yet (ptrx)
117 * must also be freed.
119 #define ERROR(fmtd) do { \
120 char* fmt_str = g_strdup_printf fmtd; \
121 g_free(yyextra->error); \
122 yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
124 if (yyextra->uat->free_cb) { \
125 yyextra->uat->free_cb(yyextra->record); \
127 g_free(yyextra->ptrx); \
132 * Sets the field of the current (scanner-internal) record, using the parsed
133 * value. If the field validation function exists and returns an error, then
134 * the record is marked as invalid and an error message is stored such that it
135 * can be shown after parsing. (If other errors occur after this issue, then
136 * this message will be overwritten though.)
138 #define SET_FIELD() \
140 if (yyextra->uat->fields[yyextra->colnum].cb.chk) { \
141 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) ) { \
142 g_free(yyextra->error); \
143 yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
145 yyextra->valid_record = FALSE; \
148 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);\
149 g_free(yyextra->ptrx);\
150 yyextra->ptrx = NULL;\
154 #ifdef DEBUG_UAT_LOAD
155 #define DUMP_FIELD(str) \
156 { 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); }
158 #define DUMP(str) printf("%s\n",str)
160 #define DUMP_FIELD(s)
164 /* Modified version of YY_INPUT generated by Flex 2.91 */
165 #define YY_INPUT(buf,result,max_size) \
166 if ( yyextra->parse_str ) \
169 size_t pslen = strlen(yyextra->parse_str); \
170 if (yyextra->parse_str_pos < pslen) \
172 n = pslen - yyextra->parse_str_pos; \
173 if (n > max_size) n = max_size; \
174 memcpy(buf, yyextra->parse_str + yyextra->parse_str_pos, n); \
175 yyextra->parse_str_pos += n; \
182 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
184 if( errno != EINTR) \
186 YY_FATAL_ERROR( "input in flex scanner failed" ); \
196 * quoted_string below fails badly on "...\\"
197 * workarround in uat_save(), using /x5c and /x22
200 #define YY_USER_INIT BEGIN START_OF_LINE;
203 * Sleazy hack to suppress compiler warnings in yy_fatal_error().
205 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
208 * Macros for the allocators, to discard the extra argument.
210 #define uat_load_alloc(size, yyscanner) (void *)malloc(size)
211 #define uat_load_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size))
212 #define uat_load_free(ptr, yyscanner) free((char *)ptr)
216 quoted_string \042([^\042]|\134\042)*\042
217 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
219 newline [ \t]*[\r]?\n
223 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
225 <START_OF_LINE,NEXT_FIELD>{ws} ;
226 <START_OF_LINE>{newline} yyextra->linenum++;
227 <START_OF_LINE>{comment} yyextra->linenum++;
229 <START_OF_LINE,NEXT_FIELD>{separator} {
230 yyextra->ptrx = g_strdup("");
233 DUMP_FIELD("empty->next");
237 if ( yyextra->colnum >= yyextra->uat->ncols ) {
238 ERROR(("more fields than required"));
244 <START_OF_LINE,NEXT_FIELD>{newline} {
245 yyextra->ptrx = g_strdup("");
250 yyless((int) yyleng);
253 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
254 yyextra->ptrx = uat_undquote(yytext, (guint) yyleng, &yyextra->len);
257 if (yyextra->colnum < yyextra->uat->ncols - 1) {
258 DUMP("quoted_str->s");
261 DUMP("quoted_str->eor");
266 <START_OF_LINE,NEXT_FIELD>{binstring} {
267 yyextra->ptrx = uat_unbinstring(yytext, (guint) yyleng, &yyextra->len);
269 if (!yyextra->ptrx) {
270 ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
273 if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
274 DUMP("binstring->s");
277 DUMP("binstring->eor");
282 <SEPARATOR>{separator} {
284 DUMP_FIELD("separator->next");
288 if ( yyextra->colnum >= yyextra->uat->ncols ) {
289 ERROR(("more fields than required"));
295 <SEPARATOR>{newline} {
297 ERROR(("expecting field %s in previous line",yyextra->uat->fields[yyextra->colnum].name));
301 ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
304 <END_OF_RECORD>{separator} {
305 ERROR(("more fields than required"));
308 <END_OF_RECORD>{newline} {
314 DUMP_FIELD("newline->start");
318 /* Last field was processed, try to store the full record in the UAT. */
319 rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
321 if ((yyextra->uat->update_cb) && (rec != NULL)) {
322 if (!yyextra->uat->update_cb(rec,&err)) {
323 g_free(yyextra->error);
324 yyextra->error = err;
329 /* The record was duplicated to the UAT above, now free our fields. */
330 if (yyextra->uat->free_cb) {
331 yyextra->uat->free_cb(yyextra->record);
333 memset(yyextra->record, 0, yyextra->uat->record_size);
335 yyextra->valid_record = TRUE;
337 yyextra->ptrx = NULL;
344 ERROR(("unexpected char while looking for end of line"));
347 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
350 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
351 . { ERROR(("unexpected input")); }
359 uat_load(uat_t *uat, char **errx)
361 gchar *fname = uat_get_actual_filename(uat, FALSE);
364 uat_load_scanner_state_t state;
369 if (uat->post_update_cb)
370 uat->post_update_cb();
376 if (!(in = ws_fopen(fname,"r"))) {
377 *errx = g_strdup(g_strerror(errno));
382 if (uat_load_lex_init(&scanner) != 0) {
383 *errx = g_strdup(g_strerror(errno));
389 uat_load_set_in(in, scanner);
392 state.parse_str = NULL; /* we're reading from a file */
395 state.valid_record = TRUE;
399 state.record = g_malloc0(uat->record_size);
401 state.parse_str_pos = 0;
404 g_free(fname); /* we're done with the file name now */
406 /* Associate the state with the scanner */
407 uat_load_set_extra(&state, scanner);
409 uat_load_lex(scanner);
411 uat_load_lex_destroy(scanner);
412 g_free(state.record);
415 uat->changed = FALSE;
424 if (uat->post_update_cb)
425 uat->post_update_cb();
432 uat_load_str(uat_t *uat, char *entry, char **err)
435 uat_load_scanner_state_t state;
438 state.parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
441 state.valid_record = TRUE;
445 state.record = g_malloc0(uat->record_size);
447 state.parse_str_pos = 0;
449 if (uat_load_lex_init(&scanner) != 0) {
450 *err = g_strdup(g_strerror(errno));
451 g_free(state.parse_str);
452 g_free(state.record);
458 /* Associate the state with the scanner */
459 uat_load_set_extra(&state, scanner);
461 uat_load_lex(scanner);
463 uat_load_lex_destroy(scanner);
464 g_free(state.record);
465 g_free(state.parse_str);
476 if (uat->post_update_cb)
477 uat->post_update_cb();