Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / uat_load.l
blobe83444a18384f86a2edfb987984b1dc5f34eb5a3
1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 #define WS_LOG_DOMAIN LOG_DOMAIN_UAT
6 #include <wireshark.h>
8 /* #define DEBUG_UAT_LOAD 1 */
12  * We want a reentrant scanner.
13  */
14 %option reentrant
17  * We don't use input, so don't generate code for it.
18  */
19 %option noinput
22  * We don't use unput, so don't generate code for it.
23  */
24 %option nounput
27  * We don't read interactively from the terminal.
28  */
29 %option never-interactive
32  * We want to stop processing when we get to the end of the input.
33  */
34 %option noyywrap
37  * The type for the state we keep for a scanner.
38  */
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).
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
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_"
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>
93 #include <glib.h>
95 #include "uat-int.h"
96 #include <wsutil/file_util.h>
99  * Disable diagnostics in the code generated by Flex.
100  */
101 DIAG_OFF_FLEX()
103 typedef struct {
104         uat_t* uat;
105         char *parse_str;
107         char* error;
108         bool valid_record;
109         unsigned colnum;
110         char* ptrx;
111         unsigned len;
112         void* record;
113         unsigned linenum;
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.
122  */
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); \
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)
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         { char* 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 = ws_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.set, yyextra->uat->fields[yyextra->colnum].fld_data);\
153         g_free(yyextra->ptrx);\
154         yyextra->ptrx = NULL;\
155         yyextra->colnum++; \
156         } while(0)
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)
163 #else
164 #define DUMP_FIELD(s)
165 #define DUMP(s)
166 #endif
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                 }
198                 /*
199                  * XXX
200                  * quoted_string below fails badly on "...\\"
201                  * workarround in uat_save(), using /x5c and /x22
202                  */
204 #define YY_USER_INIT BEGIN START_OF_LINE;
207  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
208  */
209 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
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)
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
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("");
236         yyextra->len = 0;
238         DUMP_FIELD("empty->next");
240         SET_FIELD();
242         if ( yyextra->colnum >= yyextra->uat->ncols ) {
243                 ERROR(("more fields than required"));
244         }
246         BEGIN NEXT_FIELD;
249 <START_OF_LINE,NEXT_FIELD>{newline} {
250         yyextra->ptrx = g_strdup("");
251         yyextra->len = 0;
253         BEGIN END_OF_RECORD;
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");
264                 BEGIN SEPARATOR;
265         } else {
266                 DUMP("quoted_str->eor");
267                 BEGIN END_OF_RECORD;
268         }
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));
276         }
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         }
287 <SEPARATOR>{separator} {
289         DUMP_FIELD("separator->next");
291         SET_FIELD();
293         if ( yyextra->colnum >= yyextra->uat->ncols ) {
294                 ERROR(("more fields than required"));
295         }
297         BEGIN NEXT_FIELD;
300 <SEPARATOR>{newline} {
301         DUMP("separator->newline");
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         unsigned save_colnum = yyextra->colnum;
309         yyextra->colnum++;
310         while (yyextra->colnum < yyextra->uat->ncols) {
311                 if (!yyextra->uat->default_values) {
312                         break;
313                 }
314                 const char *default_value = yyextra->uat->default_values[yyextra->colnum];
315                 if (!default_value) {
316                         break;
317                 }
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);
321                 yyextra->colnum++;
322         }
324         if (yyextra->colnum < yyextra->uat->ncols) {
325                 ERROR(("expecting field %s", yyextra->uat->fields[yyextra->colnum].name));
326         }
328         yyextra->colnum = save_colnum;
329         yyextra->linenum++;
330         BEGIN END_OF_RECORD;
331         yyless(0);
334 <SEPARATOR>. {
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} {
345         void* rec;
346         char* err = NULL;
348         yyextra->linenum++;
350         DUMP_FIELD("newline->start");
352         SET_FIELD();
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;
361                         yyterminate();
362                 }
363         }
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);
368         }
369         memset(yyextra->record, 0, yyextra->uat->record_size);
371         yyextra->valid_record = true;
372         yyextra->colnum = 0;
373         yyextra->ptrx = NULL;
374         yyextra->len = 0;
376         BEGIN START_OF_LINE;
379 <END_OF_RECORD>. {
380         ERROR(("unexpected char %s while looking for end of line", yytext));
383 <ERRORED>{newline} { yyextra->linenum++; BEGIN START_OF_LINE; }
384 <ERRORED>. ;
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.
393  */
394 DIAG_ON_FLEX()
396 bool
397 uat_load(uat_t *uat, const char *filename, char **errx)
399         char *fname;
400         FILE *in;
401         yyscan_t scanner;
402         uat_load_scanner_state_t state;
404         if (filename) {
405                 fname = g_strdup(filename);
406         } else {
407                 fname = uat_get_actual_filename(uat, false);
408         }
410         if (!fname) {
411                 UAT_UPDATE(uat);
413                 if (uat->post_update_cb)
414                         uat->post_update_cb();
416                 return true;
417         }
420         if (!(in = ws_fopen(fname,"r"))) {
421                 *errx = g_strdup(g_strerror(errno));
422                 g_free(fname);
423                 return false;
424         }
426         if (uat_load_lex_init(&scanner) != 0) {
427                 *errx = g_strdup(g_strerror(errno));
428                 fclose(in);
429                 g_free(fname);
430                 return false;
431         }
433         uat_load_set_in(in, scanner);
435         state.uat = uat;
436         state.parse_str = NULL; /* we're reading from a file */
438         state.error = NULL;
439         state.valid_record = true;
440         state.colnum = 0;
441         state.ptrx = NULL;
442         state.len = 0;
443         state.record = g_malloc0(uat->record_size);
444         state.linenum = 1;
445         state.parse_str_pos = 0;
447         DUMP(fname);
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);
457         fclose(in);
459         uat->changed = false;
460         uat->loaded = true;
461         UAT_UPDATE(uat);
463         if (state.error) {
464                 *errx = state.error;
465                 return false;
466         }
468         if (uat->post_update_cb)
469                 uat->post_update_cb();
471         *errx = NULL;
472         return true;
475 bool
476 uat_load_str(uat_t *uat, const char *entry, char **err)
478         yyscan_t scanner;
479         uat_load_scanner_state_t state;
481         state.uat = uat;
482         state.parse_str = ws_strdup_printf("%s\n", entry); /* Records must end with a newline */
484         state.error = NULL;
485         state.valid_record = true;
486         state.colnum = 0;
487         state.ptrx = NULL;
488         state.len = 0;
489         state.record = g_malloc0(uat->record_size);
490         state.linenum = 1;
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);
497                 return false;
498         }
500         DUMP(entry);
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);
511         uat->changed = true;
512         uat->loaded = true;
513         UAT_UPDATE(uat);
515         if (state.error) {
516                 *err = state.error;
517                 return false;
518         }
520         if (uat->post_update_cb)
521                 uat->post_update_cb();
523         *err = NULL;
524         return true;