2 /* Include this before everything else, for various large-file definitions */
4 #define WS_LOG_DOMAIN LOG_DOMAIN_UAT
8 /* #define DEBUG_UAT_LOAD 1 */
12 * We want a reentrant scanner.
17 * We don't use input, so don't generate code for it.
22 * We don't use unput, so don't generate code for it.
27 * We don't read interactively from the terminal.
29 %option never-interactive
32 * We want to stop processing when we get to the end of the input.
37 * The type for the state we keep for a scanner.
39 %option extra-type="uat_load_scanner_state_t *"
42 * We have to override the memory allocators so that we don't get
43 * "unused argument" warnings from the yyscanner argument (which
44 * we don't use, as we have a global memory allocator).
46 * We provide, as macros, our own versions of the routines generated by Flex,
47 * which just call malloc()/realloc()/free() (as the Flex versions do),
48 * discarding the extra argument.
55 * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
56 * can coexist with other scanners.
58 %option prefix="uat_load_"
64 * User Accessible Tables
65 * Maintain an array of user accessible data strucures
66 * One parser to fit them all
68 * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
70 * Wireshark - Network traffic analyzer
71 * By Gerald Combs <gerald@wireshark.org>
72 * Copyright 2001 Gerald Combs
74 * This program is free software; you can redistribute it and/or
75 * modify it under the terms of the GNU General Public License
76 * as published by the Free Software Foundation; either version 2
77 * of the License, or (at your option) any later version.
79 * This program is distributed in the hope that it will be useful,
80 * but WITHOUT ANY WARRANTY; without even the implied warranty of
81 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82 * GNU General Public License for more details.
84 * You should have received a copy of the GNU General Public License
85 * along with this program; if not, write to the Free Software
86 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
96 #include <wsutil/file_util.h>
99 * Disable diagnostics in the code generated by Flex.
114 size_t parse_str_pos;
115 } uat_load_scanner_state_t;
118 * Signal a fatal error and stops parsing.
119 * Since the record is internal to the parsing process, its contents must also
120 * be cleared before terminating. Any values that are not handled yet (ptrx)
121 * must also be freed.
123 #define ERROR(fmtd) do { \
124 char* fmt_str = ws_strdup_printf fmtd; \
125 g_free(yyextra->error); \
126 yyextra->error = ws_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
128 if (yyextra->uat->free_cb) { \
129 yyextra->uat->free_cb(yyextra->record); \
131 g_free(yyextra->ptrx); \
136 * Sets the field of the current (scanner-internal) record, using the parsed
137 * value. If the field validation function exists and returns an error, then
138 * the record is marked as invalid and an error message is stored such that it
139 * can be shown after parsing. (If other errors occur after this issue, then
140 * this message will be overwritten though.)
142 #define SET_FIELD() \
144 if (yyextra->uat->fields[yyextra->colnum].cb.chk) { \
145 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) ) { \
146 g_free(yyextra->error); \
147 yyextra->error = ws_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
149 yyextra->valid_record = false; \
152 yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, yyextra->ptrx, yyextra->len, yyextra->uat->fields[yyextra->colnum].cbdata.set, yyextra->uat->fields[yyextra->colnum].fld_data);\
153 g_free(yyextra->ptrx);\
154 yyextra->ptrx = NULL;\
158 #ifdef DEBUG_UAT_LOAD
159 #define DUMP_FIELD(str) \
160 { unsigned 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 ",((uint8_t*)yyextra->ptrx)[i]); } else putc(yyextra->ptrx[i],stdout); printf("'[%d]\n",yyextra->len); }
162 #define DUMP(str) printf("%s\n",str)
164 #define DUMP_FIELD(s)
168 /* Modified version of YY_INPUT generated by Flex 2.91 */
169 #define YY_INPUT(buf,result,max_size) \
170 if ( yyextra->parse_str ) \
173 size_t pslen = strlen(yyextra->parse_str); \
174 if (yyextra->parse_str_pos < pslen) \
176 n = pslen - yyextra->parse_str_pos; \
177 if (n > max_size) n = max_size; \
178 memcpy(buf, yyextra->parse_str + yyextra->parse_str_pos, n); \
179 yyextra->parse_str_pos += n; \
186 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
188 if( errno != EINTR) \
190 YY_FATAL_ERROR( "input in flex scanner failed" ); \
200 * quoted_string below fails badly on "...\\"
201 * workarround in uat_save(), using /x5c and /x22
204 #define YY_USER_INIT BEGIN START_OF_LINE;
207 * Sleazy hack to suppress compiler warnings in yy_fatal_error().
209 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
212 * Macros for the allocators, to discard the extra argument.
214 #define uat_load_alloc(size, yyscanner) (void *)malloc(size)
215 #define uat_load_realloc(ptr, size, yyscanner) (void *)realloc((char *)(ptr), (size))
216 #define uat_load_free(ptr, yyscanner) free((char *)ptr)
220 quoted_string \042([^\042]|\134\042)*\042
221 extra_records_string (\042.*\042)
222 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
224 newline [ \t]*[\r]?\n
228 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
230 <START_OF_LINE,NEXT_FIELD>{ws} ;
231 <START_OF_LINE>{newline} yyextra->linenum++;
232 <START_OF_LINE>{comment} yyextra->linenum++;
234 <START_OF_LINE,NEXT_FIELD>{separator} {
235 yyextra->ptrx = g_strdup("");
238 DUMP_FIELD("empty->next");
242 if ( yyextra->colnum >= yyextra->uat->ncols ) {
243 ERROR(("more fields than required"));
249 <START_OF_LINE,NEXT_FIELD>{newline} {
250 yyextra->ptrx = g_strdup("");
255 yyless((int) yyleng);
258 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
259 yyextra->ptrx = uat_undquote(yytext, (unsigned) yyleng, &yyextra->len);
262 if (yyextra->colnum < yyextra->uat->ncols - 1) {
263 DUMP("quoted_str->s");
266 DUMP("quoted_str->eor");
271 <START_OF_LINE,NEXT_FIELD>{binstring} {
272 yyextra->ptrx = uat_unbinstring(yytext, (unsigned) yyleng, &yyextra->len);
274 if (!yyextra->ptrx) {
275 ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
278 if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
279 DUMP("binstring->s");
282 DUMP("binstring->eor");
287 <SEPARATOR>{separator} {
289 DUMP_FIELD("separator->next");
293 if ( yyextra->colnum >= yyextra->uat->ncols ) {
294 ERROR(("more fields than required"));
300 <SEPARATOR>{newline} {
301 DUMP("separator->newline");
304 * We've run out of fields. Check to see if we have any default
305 * values that we can fill in. The field for the current column
306 * will be filled in below in <END_OF_RECORD>{newline}.
308 unsigned save_colnum = yyextra->colnum;
310 while (yyextra->colnum < yyextra->uat->ncols) {
311 if (!yyextra->uat->default_values) {
314 const char *default_value = yyextra->uat->default_values[yyextra->colnum];
315 if (!default_value) {
318 yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, default_value, (unsigned) strlen(default_value), yyextra->uat->fields[yyextra->colnum].cbdata.set, yyextra->uat->fields[yyextra->colnum].fld_data);
319 ws_log(WS_LOG_DOMAIN, LOG_LEVEL_INFO, "%s:%d: Set %s to %s.",
320 yyextra->uat->filename, yyextra->linenum, yyextra->uat->fields[yyextra->colnum].name, default_value);
324 if (yyextra->colnum < yyextra->uat->ncols) {
325 ERROR(("expecting field %s", yyextra->uat->fields[yyextra->colnum].name));
328 yyextra->colnum = save_colnum;
335 ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
338 <END_OF_RECORD>{separator}{extra_records_string} {
339 /* If we wanted to be really fancy we could retain the extra data. */
340 ws_log(WS_LOG_DOMAIN, LOG_LEVEL_WARNING, "%s:%d: More fields than required. Discarding '%s'.",
341 yyextra->uat->filename, yyextra->linenum, yytext);
344 <END_OF_RECORD>{newline} {
350 DUMP_FIELD("newline->start");
354 /* Last field was processed, try to store the full record in the UAT. */
355 rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
357 if ((yyextra->uat->update_cb) && (rec != NULL)) {
358 if (!yyextra->uat->update_cb(rec,&err)) {
359 g_free(yyextra->error);
360 yyextra->error = err;
365 /* The record was duplicated to the UAT above, now free our fields. */
366 if (yyextra->uat->free_cb) {
367 yyextra->uat->free_cb(yyextra->record);
369 memset(yyextra->record, 0, yyextra->uat->record_size);
371 yyextra->valid_record = true;
373 yyextra->ptrx = NULL;
380 ERROR(("unexpected char %s while looking for end of line", yytext));
383 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
386 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
387 . { ERROR(("unexpected input")); }
392 * Turn diagnostics back on, so we check the code that we've written.
397 uat_load(uat_t *uat, const char *filename, char **errx)
402 uat_load_scanner_state_t state;
405 fname = g_strdup(filename);
407 fname = uat_get_actual_filename(uat, false);
413 if (uat->post_update_cb)
414 uat->post_update_cb();
420 if (!(in = ws_fopen(fname,"r"))) {
421 *errx = g_strdup(g_strerror(errno));
426 if (uat_load_lex_init(&scanner) != 0) {
427 *errx = g_strdup(g_strerror(errno));
433 uat_load_set_in(in, scanner);
436 state.parse_str = NULL; /* we're reading from a file */
439 state.valid_record = true;
443 state.record = g_malloc0(uat->record_size);
445 state.parse_str_pos = 0;
448 g_free(fname); /* we're done with the file name now */
450 /* Associate the state with the scanner */
451 uat_load_set_extra(&state, scanner);
453 uat_load_lex(scanner);
455 uat_load_lex_destroy(scanner);
456 g_free(state.record);
459 uat->changed = false;
468 if (uat->post_update_cb)
469 uat->post_update_cb();
476 uat_load_str(uat_t *uat, const char *entry, char **err)
479 uat_load_scanner_state_t state;
482 state.parse_str = ws_strdup_printf("%s\n", entry); /* Records must end with a newline */
485 state.valid_record = true;
489 state.record = g_malloc0(uat->record_size);
491 state.parse_str_pos = 0;
493 if (uat_load_lex_init(&scanner) != 0) {
494 *err = g_strdup(g_strerror(errno));
495 g_free(state.parse_str);
496 g_free(state.record);
502 /* Associate the state with the scanner */
503 uat_load_set_extra(&state, scanner);
505 uat_load_lex(scanner);
507 uat_load_lex_destroy(scanner);
508 g_free(state.record);
509 g_free(state.parse_str);
520 if (uat->post_update_cb)
521 uat->post_update_cb();