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 diagnostics in the code generated by Flex.
103 gboolean valid_record;
109 size_t parse_str_pos;
110 } uat_load_scanner_state_t;
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.
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); \
123 if (yyextra->uat->free_cb) { \
124 yyextra->uat->free_cb(yyextra->record); \
126 g_free(yyextra->ptrx); \
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.)
137 #define SET_FIELD() \
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); \
144 yyextra->valid_record = FALSE; \
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;\
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); }
157 #define DUMP(str) printf("%s\n",str)
159 #define DUMP_FIELD(s)
163 /* Modified version of YY_INPUT generated by Flex 2.91 */
164 #define YY_INPUT(buf,result,max_size) \
165 if ( yyextra->parse_str ) \
168 size_t pslen = strlen(yyextra->parse_str); \
169 if (yyextra->parse_str_pos < pslen) \
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; \
181 while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
183 if( errno != EINTR) \
185 YY_FATAL_ERROR( "input in flex scanner failed" ); \
195 * quoted_string below fails badly on "...\\"
196 * workarround in uat_save(), using /x5c and /x22
199 #define YY_USER_INIT BEGIN START_OF_LINE;
202 * Sleazy hack to suppress compiler warnings in yy_fatal_error().
204 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
207 * Macros for the allocators, to discard the extra argument.
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)
215 quoted_string \042([^\042]|\134\042)*\042
216 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
218 newline [ \t]*[\r]?\n
222 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
224 <START_OF_LINE,NEXT_FIELD>{ws} ;
225 <START_OF_LINE>{newline} yyextra->linenum++;
226 <START_OF_LINE>{comment} yyextra->linenum++;
228 <START_OF_LINE,NEXT_FIELD>{separator} {
229 yyextra->ptrx = g_strdup("");
232 DUMP_FIELD("empty->next");
236 if ( yyextra->colnum >= yyextra->uat->ncols ) {
237 ERROR(("more fields than required"));
243 <START_OF_LINE,NEXT_FIELD>{newline} {
244 yyextra->ptrx = g_strdup("");
249 yyless((int) yyleng);
252 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
253 yyextra->ptrx = uat_undquote(yytext, (guint) yyleng, &yyextra->len);
256 if (yyextra->colnum < yyextra->uat->ncols - 1) {
257 DUMP("quoted_str->s");
260 DUMP("quoted_str->eor");
265 <START_OF_LINE,NEXT_FIELD>{binstring} {
266 yyextra->ptrx = uat_unbinstring(yytext, (guint) yyleng, &yyextra->len);
268 if (!yyextra->ptrx) {
269 ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
272 if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
273 DUMP("binstring->s");
276 DUMP("binstring->eor");
281 <SEPARATOR>{separator} {
283 DUMP_FIELD("separator->next");
287 if ( yyextra->colnum >= yyextra->uat->ncols ) {
288 ERROR(("more fields than required"));
294 <SEPARATOR>{newline} {
296 ERROR(("expecting field %s in previous line",yyextra->uat->fields[yyextra->colnum].name));
300 ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
303 <END_OF_RECORD>{separator} {
304 ERROR(("more fields than required"));
307 <END_OF_RECORD>{newline} {
313 DUMP_FIELD("newline->start");
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);
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;
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);
332 memset(yyextra->record, 0, yyextra->uat->record_size);
334 yyextra->valid_record = TRUE;
336 yyextra->ptrx = NULL;
343 ERROR(("unexpected char while looking for end of line"));
346 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
349 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
350 . { ERROR(("unexpected input")); }
355 * Turn diagnostics back on, so we check the code that we've written.
360 uat_load(uat_t *uat, char **errx)
362 gchar *fname = uat_get_actual_filename(uat, FALSE);
365 uat_load_scanner_state_t state;
370 if (uat->post_update_cb)
371 uat->post_update_cb();
377 if (!(in = ws_fopen(fname,"r"))) {
378 *errx = g_strdup(g_strerror(errno));
383 if (uat_load_lex_init(&scanner) != 0) {
384 *errx = g_strdup(g_strerror(errno));
390 uat_load_set_in(in, scanner);
393 state.parse_str = NULL; /* we're reading from a file */
396 state.valid_record = TRUE;
400 state.record = g_malloc0(uat->record_size);
402 state.parse_str_pos = 0;
405 g_free(fname); /* we're done with the file name now */
407 /* Associate the state with the scanner */
408 uat_load_set_extra(&state, scanner);
410 uat_load_lex(scanner);
412 uat_load_lex_destroy(scanner);
413 g_free(state.record);
416 uat->changed = FALSE;
425 if (uat->post_update_cb)
426 uat->post_update_cb();
433 uat_load_str(uat_t *uat, char *entry, char **err)
436 uat_load_scanner_state_t state;
439 state.parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
442 state.valid_record = TRUE;
446 state.record = g_malloc0(uat->record_size);
448 state.parse_str_pos = 0;
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);
459 /* Associate the state with the scanner */
460 uat_load_set_extra(&state, scanner);
462 uat_load_lex(scanner);
464 uat_load_lex_destroy(scanner);
465 g_free(state.record);
466 g_free(state.parse_str);
477 if (uat->post_update_cb)
478 uat->post_update_cb();