1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 #include <wsutil/wslog.h>
5 
6 #define WS_LOG_DOMAIN LOG_DOMAIN_UAT
7 
8 /* #define DEBUG_UAT_LOAD 1 */
9 }
10 
11 /*
12  * We want a reentrant scanner.
13  */
14 %option reentrant
15 
16 /*
17  * We don't use input, so don't generate code for it.
18  */
19 %option noinput
20 
21 /*
22  * We don't use unput, so don't generate code for it.
23  */
24 %option nounput
25 
26 /*
27  * We don't read interactively from the terminal.
28  */
29 %option never-interactive
30 
31 /*
32  * We want to stop processing when we get to the end of the input.
33  */
34 %option noyywrap
35 
36 /*
37  * The type for the state we keep for a scanner.
38  */
39 %option extra-type="uat_load_scanner_state_t *"
40 
41 /*
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).
45  *
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.
49  */
50 %option noyyalloc
51 %option noyyrealloc
52 %option noyyfree
53 
54 /*
55  * Prefix scanner routines with "uat_load_" rather than "yy", so this scanner
56  * can coexist with other scanners.
57  */
58 %option prefix="uat_load_"
59 
60 %{
61 	/*
62 	 * uat_load.l
63 	 *
64 	 *  User Accessible Tables
65 	 *  Maintain an array of user accessible data strucures
66 	 *  One parser to fit them all
67 	 *
68 	 * (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
69 	 *
70 	 * Wireshark - Network traffic analyzer
71 	 * By Gerald Combs <gerald@wireshark.org>
72 	 * Copyright 2001 Gerald Combs
73 	 *
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.
78 	 *
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.
83 	 *
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.
87 	 */
88 #include <stdlib.h>
89 #include <stdio.h>
90 #include <string.h>
91 #include <errno.h>
92 
93 #include <glib.h>
94 
95 #include "uat-int.h"
96 #include <wsutil/file_util.h>
97 
98 /*
99  * Disable diagnostics in the code generated by Flex.
100  */
101 DIAG_OFF_FLEX
102 
103 typedef struct {
104 	uat_t* uat;
105 	gchar *parse_str;
106 
107 	gchar* error;
108 	gboolean valid_record;
109 	guint colnum;
110 	gchar* ptrx;
111 	guint len;
112 	void* record;
113 	guint linenum;
114 	size_t parse_str_pos;
115 } uat_load_scanner_state_t;
116 
117 /*
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.
122  */
123 #define ERROR(fmtd) do { \
124 	char* fmt_str = g_strdup_printf fmtd; \
125 	g_free(yyextra->error); \
126 	yyextra->error = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,fmt_str); \
127 	g_free(fmt_str); \
128 	if (yyextra->uat->free_cb) { \
129 		yyextra->uat->free_cb(yyextra->record); \
130 	} \
131 	g_free(yyextra->ptrx); \
132 	yyterminate(); \
133 } while(0)
134 
135 /*
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.)
141  */
142 #define SET_FIELD() \
143 	{ gchar* errx; \
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 = g_strdup_printf("%s:%d: %s",yyextra->uat->filename,yyextra->linenum,errx); \
148 			g_free(errx); \
149 			yyextra->valid_record = FALSE; \
150 		}\
151 	}\
152 	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);\
153 	g_free(yyextra->ptrx);\
154 	yyextra->ptrx = NULL;\
155 	yyextra->colnum++; \
156 	} while(0)
157 
158 #ifdef DEBUG_UAT_LOAD
159 #define DUMP_FIELD(str) \
160 		{ 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); }
161 
162 #define DUMP(str) printf("%s\n",str)
163 #else
164 #define DUMP_FIELD(s)
165 #define DUMP(s)
166 #endif
167 
168 /* Modified version of YY_INPUT generated by Flex 2.91 */
169 #define YY_INPUT(buf,result,max_size) \
170 	if ( yyextra->parse_str ) \
171 		{ \
172 		size_t n = 0; \
173 		size_t pslen = strlen(yyextra->parse_str); \
174 		if (yyextra->parse_str_pos < pslen) \
175 			{ \
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; \
180 			} \
181 		result = n; \
182 		} \
183 	else \
184 		{ \
185 		errno=0; \
186 		while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
187 			{ \
188 			if( errno != EINTR) \
189 				{ \
190 				YY_FATAL_ERROR( "input in flex scanner failed" ); \
191 				break; \
192 				} \
193 			errno=0; \
194 			clearerr(yyin); \
195 			} \
196 		}
197 
198 		/*
199 		 * XXX
200 		 * quoted_string below fails badly on "...\\"
201 		 * workarround in uat_save(), using /x5c and /x22
202 		 */
203 
204 #define YY_USER_INIT BEGIN START_OF_LINE;
205 
206 /*
207  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
208  */
209 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
210 
211 /*
212  * Macros for the allocators, to discard the extra argument.
213  */
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)
217 
218 %}
219 
220 quoted_string \042([^\042]|\134\042)*\042
221 extra_records_string (\042.*\042)
222 binstring ([0-9a-zA-Z][0-9a-zA-Z])*
223 separator [ \t]*,
224 newline [ \t]*[\r]?\n
225 ws [ \t]+
226 comment #[^\n]*\n
227 
228 %x START_OF_LINE NEXT_FIELD SEPARATOR END_OF_RECORD ERRORED
229 %%
230 <START_OF_LINE,NEXT_FIELD>{ws} ;
231 <START_OF_LINE>{newline} yyextra->linenum++;
232 <START_OF_LINE>{comment} yyextra->linenum++;
233 
234 <START_OF_LINE,NEXT_FIELD>{separator} {
235 	yyextra->ptrx = g_strdup("");
236 	yyextra->len = 0;
237 
238 	DUMP_FIELD("empty->next");
239 
240 	SET_FIELD();
241 
242 	if ( yyextra->colnum >= yyextra->uat->ncols ) {
243 		ERROR(("more fields than required"));
244 	}
245 
246 	BEGIN NEXT_FIELD;
247 }
248 
249 <START_OF_LINE,NEXT_FIELD>{newline} {
250 	yyextra->ptrx = g_strdup("");
251 	yyextra->len = 0;
252 
253 	BEGIN END_OF_RECORD;
254 
255 	yyless((int) yyleng);
256 }
257 
258 <START_OF_LINE,NEXT_FIELD>{quoted_string} {
259 	yyextra->ptrx = uat_undquote(yytext, (guint) yyleng, &yyextra->len);
260 
261 
262 	if (yyextra->colnum < yyextra->uat->ncols - 1) {
263 		DUMP("quoted_str->s");
264 		BEGIN SEPARATOR;
265 	} else {
266 		DUMP("quoted_str->eor");
267 		BEGIN END_OF_RECORD;
268 	}
269 }
270 
271 <START_OF_LINE,NEXT_FIELD>{binstring} {
272 	yyextra->ptrx = uat_unbinstring(yytext,  (guint) yyleng, &yyextra->len);
273 
274 	if (!yyextra->ptrx) {
275 		ERROR(("uneven hexstring for field %s",yyextra->uat->fields[yyextra->colnum].name));
276 	}
277 
278 	if ( yyextra->colnum < yyextra->uat->ncols - 1 ) {
279 		DUMP("binstring->s");
280 		BEGIN SEPARATOR;
281 	} else {
282 		DUMP("binstring->eor");
283 		BEGIN END_OF_RECORD;
284 	}
285 }
286 
287 <SEPARATOR>{separator} {
288 
289 	DUMP_FIELD("separator->next");
290 
291 	SET_FIELD();
292 
293 	if ( yyextra->colnum >= yyextra->uat->ncols ) {
294 		ERROR(("more fields than required"));
295 	}
296 
297 	BEGIN NEXT_FIELD;
298 }
299 
300 <SEPARATOR>{newline} {
301 	DUMP("separator->newline");
302 
303 	/*
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}.
307 	 */
308 	guint save_colnum = yyextra->colnum;
309 	yyextra->colnum++;
310 	while (yyextra->colnum < yyextra->uat->ncols) {
311 		const char *default_value = yyextra->uat->default_values[yyextra->colnum];
312 		if (!default_value) {
313 			break;
314 		}
315 		yyextra->uat->fields[yyextra->colnum].cb.set(yyextra->record, default_value, (guint) strlen(default_value), yyextra->uat->fields[yyextra->colnum].cbdata.chk, yyextra->uat->fields[yyextra->colnum].fld_data);
316 		ws_log(WS_LOG_DOMAIN, LOG_LEVEL_INFO, "%s:%d: Set %s to %s.",
317 			yyextra->uat->filename, yyextra->linenum, yyextra->uat->fields[yyextra->colnum].name, default_value);
318 		yyextra->colnum++;
319 	}
320 
321 	if (yyextra->colnum < yyextra->uat->ncols) {
322 		ERROR(("expecting field %s", yyextra->uat->fields[yyextra->colnum].name));
323 	}
324 
325 	yyextra->colnum = save_colnum;
326 	yyextra->linenum++;
327 	BEGIN END_OF_RECORD;
328 	yyless(0);
329 }
330 
331 <SEPARATOR>. {
332 	ERROR(("unexpected char '%s' while looking for field %s",yytext,yyextra->uat->fields[yyextra->colnum].name));
333 }
334 
335 <END_OF_RECORD>{separator}{extra_records_string} {
336 	/* If we wanted to be really fancy we could retain the extra data. */
337 	ws_log(WS_LOG_DOMAIN, LOG_LEVEL_WARNING, "%s:%d: More fields than required. Discarding '%s'.",
338 		yyextra->uat->filename, yyextra->linenum, yytext);
339 }
340 
341 <END_OF_RECORD>{newline} {
342 	void* rec;
343 	char* err = NULL;
344 
345 	yyextra->linenum++;
346 
347 	DUMP_FIELD("newline->start");
348 
349 	SET_FIELD();
350 
351 	/* Last field was processed, try to store the full record in the UAT. */
352 	rec = uat_add_record(yyextra->uat, yyextra->record, yyextra->valid_record);
353 
354 	if ((yyextra->uat->update_cb) && (rec != NULL)) {
355 		if (!yyextra->uat->update_cb(rec,&err)) {
356 			g_free(yyextra->error);
357 			yyextra->error = err;
358 			yyterminate();
359 		}
360 	}
361 
362 	/* The record was duplicated to the UAT above, now free our fields. */
363 	if (yyextra->uat->free_cb) {
364 		yyextra->uat->free_cb(yyextra->record);
365 	}
366 	memset(yyextra->record, 0, yyextra->uat->record_size);
367 
368 	yyextra->valid_record = TRUE;
369 	yyextra->colnum = 0;
370 	yyextra->ptrx = NULL;
371 	yyextra->len = 0;
372 
373 	BEGIN START_OF_LINE;
374  }
375 
376 <END_OF_RECORD>. {
377 	ERROR(("unexpected char %s while looking for end of line", yytext));
378 }
379 
380 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
381 <ERRORED>. ;
382 
383 {newline} { yyextra->linenum++; ERROR(("incomplete record")); }
384 . { ERROR(("unexpected input")); }
385 
386 %%
387 
388 /*
389  * Turn diagnostics back on, so we check the code that we've written.
390  */
391 DIAG_ON_FLEX
392 
393 gboolean
394 uat_load(uat_t *uat, const gchar *filename, char **errx)
395 {
396 	gchar *fname;
397 	FILE *in;
398 	yyscan_t scanner;
399 	uat_load_scanner_state_t state;
400 
401 	if (filename) {
402 		fname = g_strdup(filename);
403 	} else {
404 		fname = uat_get_actual_filename(uat, FALSE);
405 	}
406 
407 	if (!fname) {
408 		UAT_UPDATE(uat);
409 
410 		if (uat->post_update_cb)
411 			uat->post_update_cb();
412 
413 		return TRUE;
414 	}
415 
416 
417 	if (!(in = ws_fopen(fname,"r"))) {
418 		*errx = g_strdup(g_strerror(errno));
419 		g_free(fname);
420 		return FALSE;
421 	}
422 
423 	if (uat_load_lex_init(&scanner) != 0) {
424 		*errx = g_strdup(g_strerror(errno));
425 		fclose(in);
426 		g_free(fname);
427 		return FALSE;
428 	}
429 
430 	uat_load_set_in(in, scanner);
431 
432 	state.uat = uat;
433 	state.parse_str = NULL;	/* we're reading from a file */
434 
435 	state.error = NULL;
436 	state.valid_record = TRUE;
437 	state.colnum = 0;
438 	state.ptrx = NULL;
439 	state.len = 0;
440 	state.record = g_malloc0(uat->record_size);
441 	state.linenum = 1;
442 	state.parse_str_pos = 0;
443 
444 	DUMP(fname);
445 	g_free(fname);	/* we're done with the file name now */
446 
447 	/* Associate the state with the scanner */
448 	uat_load_set_extra(&state, scanner);
449 
450 	uat_load_lex(scanner);
451 
452 	uat_load_lex_destroy(scanner);
453 	g_free(state.record);
454 	fclose(in);
455 
456 	uat->changed = FALSE;
457 	uat->loaded = TRUE;
458 	UAT_UPDATE(uat);
459 
460 	if (state.error) {
461 		*errx = state.error;
462 		return FALSE;
463 	}
464 
465 	if (uat->post_update_cb)
466 		uat->post_update_cb();
467 
468 	*errx = NULL;
469 	return TRUE;
470 }
471 
472 gboolean
uat_load_str(uat_t * uat,char * entry,char ** err)473 uat_load_str(uat_t *uat, char *entry, char **err)
474 {
475 	yyscan_t scanner;
476 	uat_load_scanner_state_t state;
477 
478 	state.uat = uat;
479 	state.parse_str = g_strdup_printf("%s\n", entry); /* Records must end with a newline */
480 
481 	state.error = NULL;
482 	state.valid_record = TRUE;
483 	state.colnum = 0;
484 	state.ptrx = NULL;
485 	state.len = 0;
486 	state.record = g_malloc0(uat->record_size);
487 	state.linenum = 1;
488 	state.parse_str_pos = 0;
489 
490 	if (uat_load_lex_init(&scanner) != 0) {
491 		*err = g_strdup(g_strerror(errno));
492 		g_free(state.parse_str);
493 		g_free(state.record);
494 		return FALSE;
495 	}
496 
497 	DUMP(entry);
498 
499 	/* Associate the state with the scanner */
500 	uat_load_set_extra(&state, scanner);
501 
502 	uat_load_lex(scanner);
503 
504 	uat_load_lex_destroy(scanner);
505 	g_free(state.record);
506 	g_free(state.parse_str);
507 
508 	uat->changed = TRUE;
509 	uat->loaded = TRUE;
510 	UAT_UPDATE(uat);
511 
512 	if (state.error) {
513 		*err = state.error;
514 		return FALSE;
515 	}
516 
517 	if (uat->post_update_cb)
518 		uat->post_update_cb();
519 
520 	*err = NULL;
521 	return TRUE;
522 }
523