2 * We don't use input, so don't generate code for it.
7 * We don't use unput, so don't generate code for it.
12 * We don't read from the terminal.
14 %option never-interactive
17 * The language we're scanning is case-insensitive.
22 * We use start condition stacks.
27 * Prefix scanner routines with "WimaxasncpDict" rather than "yy", so this
28 * scanner can coexist with other scanners.
30 %option prefix="WimaxasncpDict"
32 %option outfile="wimaxasncp_dict.c"
37 ** WIMAXASNCP Dictionary Import Routines
41 ** (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
42 ** (c) 2007, Stephen Croll <stephen.d.croll@gmail.com>
44 ** This library is free software; you can redistribute it and/or
45 ** modify it under the terms of the GNU Library General Public
46 ** License as published by the Free Software Foundation; either
47 ** version 2 of the License, or (at your option) any later version.
49 ** This library is distributed in the hope that it will be useful,
50 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
51 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
52 ** Library General Public License for more details.
54 ** You should have received a copy of the GNU Library General Public
55 ** License along with this library; if not, write to the Free Software
56 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
57 ** Boston, MA 02110-1301, USA.
68 #include <epan/emem.h>
69 #include <epan/value_string.h>
70 #include <epan/packet.h> /* array_length */
71 #include <wsutil/file_util.h>
73 #include "wimaxasncp_dict.h"
74 #include "wimaxasncp_dict_lex.h"
76 typedef struct entity_t {
79 struct entity_t *next;
82 #define ATTR_UINT(cont) do { D(("attr_uint " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_UINT_ATTR); } while(0)
83 #define ATTR_UINT16(cont) do { D(("attr_uint16 " #cont "\t" )); attr_uint16 = &(cont); yy_push_state(GET_UINT16_ATTR); } while(0)
84 #define ATTR_STR(cont) do { D(("attr_str " #cont "\t" )); attr_str = &(cont); yy_push_state(GET_ATTR); } while(0)
85 #define ATTR_DECODER(cont) do { D(("attr_decoder " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_DECODER_ATTR); } while(0)
86 #define WIMAXASNCP_IGNORE() do { D(("ignore: %s\t",yytext)); yy_push_state(IGNORE_ATTR); } while(0)
88 #define D(args) wimaxasncp_dict_debug args
90 #define MAX_INCLUDE_DEPTH 10
91 #define YY_INPUT(buf,result,max_size) { result = current_yyinput(buf,max_size); }
93 #define APPEND(txt,len) append_to_buffer(txt,(int)len)
96 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
97 static int include_stack_ptr = 0;
98 static size_t (*current_yyinput)(gchar*,size_t);
99 static const gchar *sys_dir;
100 static wimaxasncp_dict_t *dict;
101 static wimaxasncp_dict_tlv_t *tlv;
102 static wimaxasncp_dict_enum_t *enumitem;
103 static wimaxasncp_dict_xmlpi_t *xmlpi;
105 static wimaxasncp_dict_tlv_t *last_tlv;
106 static wimaxasncp_dict_enum_t *last_enumitem;
107 static wimaxasncp_dict_xmlpi_t *last_xmlpi;
109 static gchar **attr_str;
110 static guint *attr_uint;
111 static gint16 *attr_uint16;
113 static guint wimaxasncp_bits(guint bits, char *n);
114 static gint wimaxasncp_decode_type(const gchar *name);
115 static void wimaxasncp_dict_debug(const gchar *fmt, ...);
116 static void append_to_buffer(const gchar *txt, int len);
117 static FILE *wimaxasncp_dict_open(const gchar*, const gchar*);
119 static GString *dict_error = NULL;
124 xmlpi_start [[:blank:] \r\n]*<\?[[:blank:] \r\n]*
125 xmlpi_end [[:blank:] \r\n]*\?>[[:blank:] \r\n]*
126 xmlpi_key_attr [[:blank:] \r\n]*key[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
127 xmlpi_value_attr [[:blank:] \r\n]*value[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
129 comment_start [[:blank:] \r\n]*<!--[[:blank:] \r\n]*
130 comment_end [[:blank:] \r\n]*-->[[:blank:] \r\n]*
131 open_tag [[:blank:] \r\n]*<[[:blank:] \r\n]*
132 end_tag [[:blank:] \r\n]*\/>[[:blank:] \r\n]*
133 close_tag [[:blank:] \r\n]*>[[:blank:] \r\n]*
134 open_closetag [[:blank:] \r\n]*<\/[[:blank:] \r\n]*
135 equals [[:blank:] \r\n]*=[[:blank:] \r\n]*
136 whitespace [[:blank:] \r\n]*
137 dquoted \042[^\042]*\042
139 doctype [[:blank:] \r\n]*<!DOCTYPE[^\[]*\[[[:blank:] \r\n]*
140 doctype_end [[:blank:] \r\n]*\][[:blank:] \r\n]*>[[:blank:] \r\n]*
142 start_entity [[:blank:] \r\n]*<\!ENTITY[[:blank:] \r\n]*
143 system [[:blank:] \r\n]*SYSTEM[[:blank:] \r\n]*\042
144 entityname [a-z0-9-]+
146 end_entity \042[[:blank:] \r\n]*>[[:blank:] \r\n]*
158 number [-]?[0-9]*|(0x)?[0-9a-fA-F]*
160 dictionary_start <dictionary>
161 dictionary_end <\/dictionary>
169 ignored_attr [a-z0-9-]+=
170 ignored_quoted \042[^\042]*\042
175 typename_attr type-name=\042
176 description_attr description=\042
177 decoder_attr decoder=\042
178 since_attr since=\042
181 %S LOADING LOADING_COMMENT LOADING_XMLPI ENTITY GET_SYSTEM GET_FILE END_ENTITY
182 %S GET_ATTR GET_UINT_ATTR GET_UINT16_ATTR
183 %S BIT32 BIT16 BIT8 GET_DECODER_ATTR END_ATTR
184 %S OUTSIDE IN_DICT IN_APPL IN_TLV IGNORE_ATTR
185 %S ENUM_ATTRS TLV_ATTRS
186 %S XMLPI_ATTRS XMLPI_GETKEY XMLPI_GETVAL XMLPI_ENDATTR
189 <LOADING>{doctype_end} ;
191 <LOADING>{comment_start} BEGIN LOADING_COMMENT;
193 <LOADING_COMMENT>{comment_end} BEGIN LOADING;
195 <LOADING>{xmlpi_start} BEGIN LOADING_XMLPI;
196 <LOADING_XMLPI>{whitespace} ;
197 <LOADING_XMLPI>{entityname} {
198 xmlpi = g_new(wimaxasncp_dict_xmlpi_t,1);
199 xmlpi->name = g_strdup(yytext);
204 if (!dict->xmlpis) last_xmlpi = dict->xmlpis = xmlpi;
205 else last_xmlpi = last_xmlpi->next = xmlpi;
210 <XMLPI_ATTRS>{xmlpi_key_attr} BEGIN XMLPI_GETKEY;
211 <XMLPI_GETKEY>{ndquot} { xmlpi->key = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
213 <XMLPI_ATTRS>{xmlpi_value_attr} BEGIN XMLPI_GETVAL;
214 <XMLPI_GETVAL>{ndquot} { xmlpi->value = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
217 <XMLPI_ATTRS>{xmlpi_end} BEGIN LOADING;
220 <LOADING>{start_entity} BEGIN ENTITY;
221 <ENTITY>{entityname} {
222 entity_t *e = g_new(entity_t,1);
223 D(("ENTITY: %s\n",yytext));
224 e->name = g_strdup(yytext);
229 <GET_SYSTEM>{system} BEGIN GET_FILE;
231 D(("GET_FILE: %s\n",yytext));
232 ents.next->file = g_strdup(yytext);
235 <END_ENTITY>{end_entity} BEGIN LOADING;
237 <LOADING>{open_tag} APPEND("<",1);
239 <LOADING>{close_tag} APPEND(">",1);
241 <LOADING>{end_tag} APPEND("/>",2);
243 <LOADING>{open_closetag} APPEND("</",2);
245 <LOADING>{whitespace} APPEND(" ",1);
247 <LOADING>{dquoted} APPEND(yytext,yyleng);
249 <LOADING>{equals} APPEND("=",1);
251 <LOADING>{any} APPEND(yytext,yyleng);
253 <LOADING,IN_DICT>{entity} {
254 gchar *p = ++yytext, *temp_str;
257 while(*p != ';') p++;
261 D(("looking for entity: %s\n",yytext));
263 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
264 dict_error = g_string_append(
265 dict_error, "included files nested too deeply\n");
269 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
272 for (e = ents.next; e; e = e->next) {
273 if (strcmp(e->name,yytext) == 0) {
274 yyin = wimaxasncp_dict_open(sys_dir,e->file);
275 D(("entity: %s filename: %s yyin: %p\n",e->name,e->file,yyin));
279 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
286 temp_str = g_strdup_printf(
287 "cannot find entity: '%s'\n", yytext);
288 dict_error = g_string_append(dict_error, temp_str);
296 if (!yyin) yyterminate();
299 D(("closing: %p %i\n",yyin,include_stack_ptr));
301 if ( --include_stack_ptr < 0 ) {
302 D(("DONE READING\n"));
306 yy_delete_buffer( YY_CURRENT_BUFFER );
307 yy_switch_to_buffer(include_stack[include_stack_ptr]);
314 *attr_str = g_strdup(yytext);
320 <GET_UINT_ATTR>{number} {
321 *attr_uint = (guint)strtoul(yytext,NULL,0);
327 <GET_UINT16_ATTR>{number} {
328 *attr_uint16 = (gint16) strtol(yytext,NULL,0);
334 <GET_UINT_ATTR>"WIMAXASNCP_BIT32"[ \t]*"(" { BEGIN BIT32; }
337 *attr_uint = wimaxasncp_bits(32, yytext);
338 D(("WIMAXASNCP_BIT32(%s)\n",yytext););
342 <GET_UINT_ATTR>"WIMAXASNCP_BIT16"[ \t]*"(" { BEGIN BIT16; }
345 *attr_uint = wimaxasncp_bits(16, yytext);
346 D(("WIMAXASNCP_BIT16(%s)\n",yytext););
350 <GET_UINT_ATTR>"WIMAXASNCP_BIT8"[ \t]*"(" { BEGIN BIT8; }
353 *attr_uint = wimaxasncp_bits(8, yytext);
354 D(("WIMAXASNCP_BIT8(%s)\n",yytext););
358 <BIT32,BIT16,BIT8>[ \t]*")" { BEGIN END_ATTR; }
360 <GET_DECODER_ATTR>{ndquot} {
361 *attr_uint = wimaxasncp_decode_type(yytext);
367 <END_ATTR>{dquot} { yy_pop_state(); }
374 <IGNORE_ATTR>{ignored_quoted} {
375 D(("=>%s<=\n",yytext));
379 <OUTSIDE>{dictionary_start} {
380 D(("dictionary_start\n"));
385 <IN_DICT>{tlv_start} {
388 tlv = g_new(wimaxasncp_dict_tlv_t,1);
391 tlv->description = NULL;
399 tlv->hf_protocol = -1;
400 tlv->hf_port_low = -1;
401 tlv->hf_port_high = -1;
402 tlv->hf_ipv4_mask = -1;
403 tlv->hf_ipv6_mask = -1;
404 tlv->hf_vendor_id = -1;
405 tlv->hf_vendor_rest_of_info = -1;
410 if (! dict->tlvs ) last_tlv = dict->tlvs = tlv;
411 else last_tlv = last_tlv->next = tlv;
416 <TLV_ATTRS>{name_attr} { ATTR_STR(tlv->name); }
417 <TLV_ATTRS>{description_attr} { ATTR_STR(tlv->description); }
418 <TLV_ATTRS>{type_attr} { ATTR_UINT16(tlv->type); }
419 <TLV_ATTRS>{decoder_attr} { ATTR_DECODER(tlv->decoder); }
420 <TLV_ATTRS>{since_attr} { ATTR_UINT(tlv->since); }
421 <TLV_ATTRS>{stop} { BEGIN IN_TLV; }
422 <TLV_ATTRS>{stop_end} { BEGIN IN_DICT; }
425 <IN_TLV>{enum_start} {
428 enumitem = g_new(wimaxasncp_dict_enum_t,1);
429 enumitem->name = NULL;
431 enumitem->next = NULL;
433 if (!tlv->enums) last_enumitem = tlv->enums = enumitem;
434 else last_enumitem = last_enumitem->next = enumitem;
440 <ENUM_ATTRS>{name_attr} { ATTR_STR(enumitem->name); }
441 <ENUM_ATTRS>{code_attr} { ATTR_UINT(enumitem->code); }
443 <ENUM_ATTRS>{stop} { BEGIN IN_TLV; }
444 <ENUM_ATTRS>{stop_end} { BEGIN IN_TLV; }
446 <IN_TLV>{tlv_end} { D(("tlv_end")); BEGIN IN_DICT; }
448 <IN_DICT>{dictionary_end} {
452 <TLV_ATTRS,ENUM_ATTRS>{ignored_attr} WIMAXASNCP_IGNORE();
463 static int debugging = 0;
465 static void wimaxasncp_dict_debug(const gchar *fmt, ...) {
469 if (debugging) vfprintf(stderr, fmt, ap);
475 static guint wimaxasncp_bits(guint bits, char *n)
477 return 1 << ((bits - 1) - (strtoul(n, NULL, 10)));
480 static const value_string wimaxasncp_decode_type_vals[] =
482 { WIMAXASNCP_TLV_TBD, "WIMAXASNCP_TLV_TBD"},
483 { WIMAXASNCP_TLV_COMPOUND, "WIMAXASNCP_TLV_COMPOUND"},
484 { WIMAXASNCP_TLV_BYTES, "WIMAXASNCP_TLV_BYTES"},
485 { WIMAXASNCP_TLV_ENUM8, "WIMAXASNCP_TLV_ENUM8"},
486 { WIMAXASNCP_TLV_ENUM16, "WIMAXASNCP_TLV_ENUM16"},
487 { WIMAXASNCP_TLV_ENUM32, "WIMAXASNCP_TLV_ENUM32"},
488 { WIMAXASNCP_TLV_ETHER, "WIMAXASNCP_TLV_ETHER"},
489 { WIMAXASNCP_TLV_ASCII_STRING, "WIMAXASNCP_TLV_ASCII_STRING"},
490 { WIMAXASNCP_TLV_FLAG0, "WIMAXASNCP_TLV_FLAG0"},
491 { WIMAXASNCP_TLV_BITFLAGS8, "WIMAXASNCP_TLV_BITFLAGS8"},
492 { WIMAXASNCP_TLV_BITFLAGS16, "WIMAXASNCP_TLV_BITFLAGS16"},
493 { WIMAXASNCP_TLV_BITFLAGS32, "WIMAXASNCP_TLV_BITFLAGS32"},
494 { WIMAXASNCP_TLV_ID, "WIMAXASNCP_TLV_ID"},
495 { WIMAXASNCP_TLV_HEX8, "WIMAXASNCP_TLV_HEX8"},
496 { WIMAXASNCP_TLV_HEX16, "WIMAXASNCP_TLV_HEX16"},
497 { WIMAXASNCP_TLV_HEX32, "WIMAXASNCP_TLV_HEX32"},
498 { WIMAXASNCP_TLV_DEC8, "WIMAXASNCP_TLV_DEC8"},
499 { WIMAXASNCP_TLV_DEC16, "WIMAXASNCP_TLV_DEC16"},
500 { WIMAXASNCP_TLV_DEC32, "WIMAXASNCP_TLV_DEC32"},
501 { WIMAXASNCP_TLV_IP_ADDRESS, "WIMAXASNCP_TLV_IP_ADDRESS"},
502 { WIMAXASNCP_TLV_IPV4_ADDRESS, "WIMAXASNCP_TLV_IPV4_ADDRESS"},
503 { WIMAXASNCP_TLV_PROTOCOL_LIST, "WIMAXASNCP_TLV_PROTOCOL_LIST"},
504 { WIMAXASNCP_TLV_PORT_RANGE_LIST, "WIMAXASNCP_TLV_PORT_RANGE_LIST"},
505 { WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST,"WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST"},
506 { WIMAXASNCP_TLV_EAP, "WIMAXASNCP_TLV_EAP"},
507 { WIMAXASNCP_TLV_VENDOR_SPECIFIC, "WIMAXASNCP_TLV_VENDOR_SPECIFIC"},
511 static gint wimaxasncp_decode_type(const gchar *name)
514 for (i = 0; i < array_length(wimaxasncp_decode_type_vals) - 1; ++i)
516 if (strcmp(name, wimaxasncp_decode_type_vals[i].strptr) == 0)
518 return wimaxasncp_decode_type_vals[i].value;
522 /* not found, emit some sort of error here? */
524 return WIMAXASNCP_TLV_TBD;
527 static gchar *strbuf = NULL;
528 static gchar *write_ptr = NULL;
529 static gchar *read_ptr = NULL;
531 static guint size_strbuf = 8192;
532 static guint len_strbuf = 0;
534 extern void wimaxasncp_dict_unused(void);
535 void wimaxasncp_dict_unused(void) {
539 static void append_to_buffer(const gchar *txt, int len) {
541 if (strbuf == NULL) {
542 read_ptr = write_ptr = strbuf = (gchar *)g_malloc(size_strbuf);
545 if ( (len_strbuf + len + 1) >= size_strbuf ) {
546 read_ptr = strbuf = (gchar *)g_realloc(strbuf,size_strbuf *= 2);
549 write_ptr = strbuf + len_strbuf;
550 strncpy(write_ptr,txt,len);
552 strbuf[len_strbuf] = '\0';
555 static size_t file_input(gchar *buf, size_t max) {
558 read_cnt = fread(buf,1,max,yyin);
560 if ( read_cnt == max ) {
562 } else if (read_cnt > 0) {
570 static size_t string_input(gchar *buf, size_t max) {
571 if (read_ptr >= write_ptr ) {
573 } else if ( read_ptr + max > write_ptr ) {
574 max = write_ptr - read_ptr;
577 memcpy(buf,read_ptr,max);
583 static FILE *wimaxasncp_dict_open(
584 const gchar *system_directory,
585 const gchar *filename)
589 if (system_directory)
591 fname = g_strdup_printf("%s%s%s",
592 system_directory, G_DIR_SEPARATOR_S,filename);
596 fname = g_strdup(filename);
599 fh = ws_fopen(fname,"r");
601 D(("fname: %s fh: %p\n",fname,fh));
609 wimaxasncp_dict_t *wimaxasncp_dict_scan(
610 const gchar *system_directory, const gchar *filename, int dbg,
615 dict_error = g_string_new("");
619 sys_dir = system_directory;
626 wimaxasncp_dict_free(dict);
629 dict = g_new(wimaxasncp_dict_t,1);
638 last_enumitem = NULL;
643 yyin = wimaxasncp_dict_open(sys_dir,filename);
647 current_yyinput = file_input;
651 D(("\n---------------\n%s\n------- %d -------\n",
652 strbuf, len_strbuf));
654 current_yyinput = string_input;
663 entity_t *next = e->next;
674 if (dict_error->len > 0)
676 *error = dict_error->str;
677 g_string_free(dict_error, FALSE);
682 g_string_free(dict_error, TRUE);
688 void wimaxasncp_dict_free(wimaxasncp_dict_t *d) {
689 wimaxasncp_dict_tlv_t *t, *tn;
691 #define FREE_NAMEANDOBJ(n) do { if(n->name) g_free(n->name); g_free(n); } while(0)
693 for (t = d->tlvs; t; t = tn) {
694 wimaxasncp_dict_enum_t *e, *en;
697 for (e = t->enums; e; e = en) {
702 if (!t->description) g_free(t->description);
709 void wimaxasncp_dict_print(FILE *fh, wimaxasncp_dict_t *d) {
710 wimaxasncp_dict_tlv_t *tlvp;
714 for (tlvp = d->tlvs; tlvp; tlvp = tlvp->next) {
715 wimaxasncp_dict_enum_t *e;
716 fprintf(fh,"TLV: %s[%u] %s[%d] %s (since %u)\n",
717 tlvp->name ? tlvp->name : "-",
719 val_to_str(tlvp->decoder,
720 wimaxasncp_decode_type_vals,
723 tlvp->description ? tlvp->description : "",
726 for (e = tlvp->enums; e; e = e->next) {
727 fprintf(fh,"\tEnum: %s[%u]\n",
728 e->name ? e->name : "-",
735 * We want to stop processing when we get to the end of the input.
736 * (%option noyywrap is not used because if used then
737 * some flex versions (eg: 2.5.35) generate code which causes
738 * warnings by the Windows VC compiler).
745 #ifdef TEST_WIMAXASNCP_DICT_STANDALONE
746 int main(int argc, char **argv) {
747 wimaxasncp_dict_t *d;
759 fprintf(stderr,"%s: usage [dictionary_dir] dictionary_filename\n",argv[0]);
763 d = wimaxasncp_dict_scan(dname,fname,1,&dict_error);
767 printf("wimaxasncp - %s", dict_error);
771 wimaxasncp_dict_print(stdout, d);