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 "DiamDict" rather than "yy", so this scanner
28 * can coexist with other scanners.
30 %option prefix="DiamDict"
32 %option outfile="diam_dict.c"
37 ** Diameter Dictionary Import Routines
41 ** (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
43 ** This library is free software; you can redistribute it and/or
44 ** modify it under the terms of the GNU Library General Public
45 ** License as published by the Free Software Foundation; either
46 ** version 2 of the License, or (at your option) any later version.
48 ** This library is distributed in the hope that it will be useful,
49 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
50 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51 ** Library General Public License for more details.
53 ** You should have received a copy of the GNU Library General Public
54 ** License along with this library; if not, write to the Free Software
55 ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
56 ** Boston, MA 02110-1301, USA.
67 #include "diam_dict.h"
68 #include "diam_dict_lex.h"
69 #include <epan/to_str.h>
70 #include <wsutil/file_util.h>
72 typedef struct entity_t {
75 struct entity_t* next;
78 #define ATTR_UINT(cont) do { D(("attr_uint " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_UINT_ATTR); } while(0)
79 #define ATTR_STR(cont) do { D(("attr_str " #cont "\t" )); attr_str = &(cont); yy_push_state(GET_ATTR); } while(0)
80 #define IGNORE() do { D(("ignore: %s\t",yytext)); yy_push_state(IGNORE_ATTR); } while(0)
82 #define D(args) ddict_debug args
84 #define MAX_INCLUDE_DEPTH 10
85 #define YY_INPUT(buf,result,max_size) { result = current_yyinput(buf,max_size); }
87 #define APPEND(txt,len) append_to_buffer(txt,len)
90 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
91 static int include_stack_ptr = 0;
92 static size_t (*current_yyinput)(char*,size_t);
93 static const char* sys_dir;
95 static ddict_application_t* appl;
96 static ddict_avp_t* avp;
97 static ddict_enum_t* enumitem;
98 static ddict_gavp_t* gavp;
99 static ddict_typedefn_t* typedefn;
100 static ddict_cmd_t* cmd;
101 static ddict_vendor_t* vnd;
102 static ddict_xmlpi_t* xmlpi;
104 static ddict_application_t* last_appl;
105 static ddict_avp_t* last_avp;
106 static ddict_enum_t* last_enumitem;
107 static ddict_gavp_t* last_gavp;
108 static ddict_typedefn_t* last_typedefn;
109 static ddict_cmd_t* last_cmd;
110 static ddict_vendor_t* last_vnd;
111 static ddict_xmlpi_t* last_xmlpi;
113 static char** attr_str;
114 static unsigned* attr_uint;
116 static void ddict_debug(const char* fmt, ...);
117 static void append_to_buffer(const char* txt, int len);
118 static FILE* ddict_open(const char*, const char*);
123 xmlpi_start [[:blank:] \r\n]*<\?[[:blank:] \r\n]*
124 xmlpi_end [[:blank:] \r\n]*\?>[[:blank:] \r\n]*
125 xmlpi_key_attr [[:blank:] \r\n]*key[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
126 xmlpi_value_attr [[:blank:] \r\n]*value[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
128 comment_start [[:blank:] \r\n]*<!--[[:blank:] \r\n]*
129 comment_end [[:blank:] \r\n]*-->[[:blank:] \r\n]*
130 open_tag [[:blank:] \r\n]*<[[:blank:] \r\n]*
131 end_tag [[:blank:] \r\n]*\/>[[:blank:] \r\n]*
132 close_tag [[:blank:] \r\n]*>[[:blank:] \r\n]*
133 open_closetag [[:blank:] \r\n]*<\/[[:blank:] \r\n]*
134 equals [[:blank:] \r\n]*=[[:blank:] \r\n]*
135 whitespace [[:blank:] \r\n]*
136 dquoted \042[^\042]*\042
138 doctype [[:blank:] \r\n]*<!DOCTYPE[^\[]*\[[[:blank:] \r\n]*
139 doctype_end [[:blank:] \r\n]*\][[:blank:] \r\n]*>[[:blank:] \r\n]*
141 start_entity [[:blank:] \r\n]*<\!ENTITY[[:blank:] \r\n]*
142 system [[:blank:] \r\n]*SYSTEM[[:blank:] \r\n]*\042
143 entityname [a-z0-9-]+
145 end_entity \042[[:blank:] \r\n]*>[[:blank:] \r\n]*
159 dictionary_start <dictionary>
160 dictionary_end <\/dictionary>
162 base_start <base[^>*]*>
165 application_start <application
166 application_end<\/application>
168 command_start <command
169 command_end<\/command>
171 typedefn_start <typedefn
179 grouped_start <grouped>
180 grouped_end <\/grouped>
185 ignored_attr [a-z0-9-]+=
186 ignored_quoted \042[^\042]*\042
191 vendor_attr vendor-id=\042
192 typename_attr type-name=\042
193 typeparent_attr type-parent=\042
194 description_attr description=\042
198 %S LOADING LOADING_COMMENT LOADING_XMLPI ENTITY GET_SYSTEM GET_FILE END_ENTITY
199 %S GET_ATTR GET_UINT_ATTR END_ATTR OUTSIDE IN_DICT IN_APPL IN_AVP APPL_ATTRS IGNORE_ATTR
200 %S TYPE_ATTRS GAVP_ATTRS ENUM_ATTRS AVP_ATTRS VENDOR_ATTRS COMMAND_ATTRS TYPEDEFN_ATTRS
201 %S XMLPI_ATTRS XMLPI_GETKEY XMLPI_GETVAL XMLPI_ENDATTR
204 <LOADING>{doctype_end} ;
206 <LOADING>{comment_start} BEGIN LOADING_COMMENT;
208 <LOADING_COMMENT>{comment_end} BEGIN LOADING;
210 <LOADING>{xmlpi_start} BEGIN LOADING_XMLPI;
211 <LOADING_XMLPI>{whitespace} ;
212 <LOADING_XMLPI>{entityname} {
213 xmlpi = g_new(ddict_xmlpi_t,1);
214 xmlpi->name = g_strdup(yytext);
219 if (!dict->xmlpis) last_xmlpi = dict->xmlpis = xmlpi;
220 else last_xmlpi = last_xmlpi->next = xmlpi;
225 <XMLPI_ATTRS>{xmlpi_key_attr} BEGIN XMLPI_GETKEY;
226 <XMLPI_GETKEY>{ndquot} { xmlpi->key = strdup(yytext); BEGIN XMLPI_ATTRS; }
228 <XMLPI_ATTRS>{xmlpi_value_attr} BEGIN XMLPI_GETVAL;
229 <XMLPI_GETVAL>{ndquot} { xmlpi->value = strdup(yytext); BEGIN XMLPI_ATTRS; }
232 <XMLPI_ATTRS>{xmlpi_end} BEGIN LOADING;
235 <LOADING>{start_entity} BEGIN ENTITY;
236 <ENTITY>{entityname} {
237 entity_t* e = g_new(entity_t,1);
238 e->name = strdup(yytext);
243 <GET_SYSTEM>{system} BEGIN GET_FILE;
245 ents.next->file = strdup(yytext);
248 <END_ENTITY>{end_entity} BEGIN LOADING;
250 <LOADING>{open_tag} APPEND("<",1);
252 <LOADING>{close_tag} APPEND(">",1);
254 <LOADING>{end_tag} APPEND("/>",2);
256 <LOADING>{open_closetag} APPEND("</",2);
258 <LOADING>{whitespace} APPEND(" ",1);
260 <LOADING>{dquoted} APPEND(yytext,yyleng);
262 <LOADING>{equals} APPEND("=",1);
264 <LOADING>{any} APPEND(yytext,yyleng);
270 while(*p != ';') p++;
274 D(("looking for entity: %s\n",yytext));
276 if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
277 fprintf(stderr, "included files nested to deeply\n");
281 for (e = ents.next; e; e = e->next) {
282 if (strcmp(e->name,yytext) == 0) {
283 yyin = ddict_open(sys_dir,e->file);
284 D(("entity: %s filename: %s yyin: %p\n",e->name,e->file,yyin));
287 fprintf(stderr, "Could not open file: '%s', error: %s\n", e->file, g_strerror(errno) );
289 fprintf(stderr, "Could not open file: '%s', error unknown (errno == 0)\n", e->file );
292 include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
293 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
301 fprintf(stderr, "Could not find entity: '%s'\n", yytext );
308 if (!yyin) yyterminate();
311 D(("closing: %p %i\n",yyin,include_stack_ptr));
313 if ( --include_stack_ptr < 0 ) {
314 D(("DONE READING\n"));
318 yy_delete_buffer( YY_CURRENT_BUFFER );
319 yy_switch_to_buffer(include_stack[include_stack_ptr]);
326 *attr_str = strdup(yytext);
332 <GET_UINT_ATTR>{number} {
333 *attr_uint = strtoul(yytext,NULL,10);
339 <END_ATTR>{dquot} { yy_pop_state(); }
346 <IGNORE_ATTR>{ignored_quoted} {
347 D(("=>%s<=\n",yytext));
351 <OUTSIDE>{dictionary_start} {
352 D(("dictionary_start\n"));
357 <IN_DICT>{base_start} {
362 <IN_DICT>{application_start} {
363 D(("application_start\n"));
365 appl = g_new(ddict_application_t,1);
370 if (!dict->applications) last_appl = dict->applications = appl;
371 else last_appl = last_appl->next = appl;
376 <APPL_ATTRS>{name_attr} { ATTR_STR(appl->name); }
377 <APPL_ATTRS>{id_attr} { ATTR_UINT(appl->code); }
379 <APPL_ATTRS>{stop} BEGIN IN_APPL;
380 <APPL_ATTRS>{stop_end} BEGIN IN_DICT;
382 <IN_APPL>{command_end} ;
384 <IN_APPL>{command_start} {
385 D(("command_start\n"));
387 cmd = g_new(ddict_cmd_t,1);
393 if (!dict->cmds) last_cmd = dict->cmds = cmd;
394 else last_cmd = last_cmd->next = cmd;
399 <COMMAND_ATTRS>{name_attr} { ATTR_STR(cmd->name); }
400 <COMMAND_ATTRS>{vendor_attr} { ATTR_STR(cmd->vendor); }
401 <COMMAND_ATTRS>{code_attr} { ATTR_UINT(cmd->code); }
402 <COMMAND_ATTRS>{stop} |
403 <COMMAND_ATTRS>{stop_end} { BEGIN IN_APPL; }
405 <IN_APPL>{vendor_start} {
406 D(("vendor_start\n"));
408 vnd = g_new(ddict_vendor_t,1);
413 if (!dict->vendors) last_vnd = dict->vendors = vnd;
414 else last_vnd = last_vnd->next = vnd;
419 <VENDOR_ATTRS>{name_attr} { ATTR_STR(vnd->desc); }
420 <VENDOR_ATTRS>{vendor_attr} { ATTR_STR(vnd->name); }
421 <VENDOR_ATTRS>{code_attr} { ATTR_UINT(vnd->code); }
422 <VENDOR_ATTRS>{stop} |
423 <VENDOR_ATTRS>{stop_end} { BEGIN IN_APPL; }
425 <IN_APPL>{typedefn_start} {
426 D(("typedefn_start\n"));
428 typedefn = g_new(ddict_typedefn_t,1);
429 typedefn->name = NULL;
430 typedefn->parent = NULL;
431 typedefn->next = NULL;
433 if (!dict->typedefns) last_typedefn = dict->typedefns = typedefn;
434 else last_typedefn = last_typedefn->next = typedefn;
436 BEGIN TYPEDEFN_ATTRS;
439 <TYPEDEFN_ATTRS>{typename_attr} { ATTR_STR(typedefn->name); }
440 <TYPEDEFN_ATTRS>{typeparent_attr} { ATTR_STR(typedefn->parent); }
441 <TYPEDEFN_ATTRS>{stop} |
442 <TYPEDEFN_ATTRS>{stop_end} { BEGIN IN_APPL; }
445 <IN_APPL>{avp_start} {
448 avp = g_new(ddict_avp_t,1);
450 avp->description = NULL;
458 if (! dict->avps ) last_avp = dict->avps = avp;
459 else last_avp = last_avp->next = avp;
464 <AVP_ATTRS>{name_attr} { ATTR_STR(avp->name); }
465 <AVP_ATTRS>{description_attr} { ATTR_STR(avp->description); }
466 <AVP_ATTRS>{vendor_attr} { ATTR_STR(avp->vendor); }
467 <AVP_ATTRS>{code_attr} { ATTR_UINT(avp->code); }
468 <AVP_ATTRS>{stop} { BEGIN IN_AVP; }
469 <AVP_ATTRS>{stop_end} { BEGIN IN_APPL; }
472 <IN_AVP>{grouped_start} { avp->type = strdup("Grouped"); };
473 <IN_AVP>{grouped_end} ;
475 <IN_AVP>{type_start} { BEGIN TYPE_ATTRS; }
476 <TYPE_ATTRS>{typename_attr} { ATTR_STR(avp->type); }
478 <IN_AVP>{gavp_start} {
481 gavp = g_new(ddict_gavp_t,1);
486 if (!avp->gavps) last_gavp = avp->gavps = gavp;
487 else last_gavp = last_gavp->next = gavp;
493 <GAVP_ATTRS>{name_attr} { ATTR_STR(gavp->name); }
496 <IN_AVP>{enum_start} {
499 enumitem = g_new(ddict_enum_t,1);
500 enumitem->name = NULL;
502 enumitem->next = NULL;
504 if (!avp->enums) last_enumitem = avp->enums = enumitem;
505 else last_enumitem = last_enumitem->next = enumitem;
511 <ENUM_ATTRS>{name_attr} { ATTR_STR(enumitem->name); }
512 <ENUM_ATTRS>{code_attr} { ATTR_UINT(enumitem->code); }
514 <TYPE_ATTRS,GAVP_ATTRS,ENUM_ATTRS>{stop} { BEGIN IN_AVP; }
515 <TYPE_ATTRS,GAVP_ATTRS,ENUM_ATTRS>{stop_end} { BEGIN IN_AVP; }
517 <IN_AVP>{avp_end} { D(("avp_end")); BEGIN IN_APPL; }
519 <IN_APPL>{application_end} |
520 <IN_APPL>{base_end} {
524 <IN_DICT>{dictionary_end} {
528 <AVP_ATTRS,ENUM_ATTRS,GAVP_ATTRS,TYPE_ATTRS,TYPEDEFN_ATTRS,VENDOR_ATTRS,APPL_ATTRS,COMMAND_ATTRS>{ignored_attr} IGNORE();
539 static int debugging = 0;
541 static void ddict_debug(const char* fmt, ...) {
545 if (debugging) vfprintf(stderr, fmt, ap);
552 static char* strbuf = NULL;
553 static char* write_ptr = NULL;
554 static char* read_ptr = NULL;
556 static unsigned size_strbuf = 8192;
557 static unsigned len_strbuf = 0;
559 extern void ddict_unused(void);
560 void ddict_unused(void) {
564 static void append_to_buffer(const char* txt, int len) {
566 if (strbuf == NULL) {
567 read_ptr = write_ptr = strbuf = (char*)g_malloc(size_strbuf);
570 if ( (len_strbuf + len) >= size_strbuf ) {
571 read_ptr = strbuf = (char*)g_realloc(strbuf,size_strbuf *= 2);
574 write_ptr = strbuf + len_strbuf;
575 strncpy(write_ptr,txt,len);
580 static size_t file_input(char* buf, size_t max) {
583 read_cnt = fread(buf,1,max,yyin);
585 if ( read_cnt == max ) {
587 } else if (read_cnt > 0) {
595 static size_t string_input(char* buf, size_t max) {
596 if (read_ptr >= write_ptr ) {
598 } else if ( read_ptr + max > write_ptr ) {
599 max = write_ptr - read_ptr;
602 memcpy(buf,read_ptr,max);
608 static FILE* ddict_open(const char* system_directory, const char* filename) {
611 if (system_directory) {
612 fname = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
613 system_directory,filename);
615 fname = g_strdup(filename);
618 fh = ws_fopen(fname,"r");
620 D(("fname: %s fh: %p\n",fname,fh));
628 ddict_t* ddict_scan(const char* system_directory, const char* filename, int dbg) {
632 sys_dir = system_directory;
634 yyin = ddict_open(sys_dir,filename);
637 D(("unable to open %s\n", filename));
644 dict = g_new(ddict_t,1);
645 dict->applications = NULL;
647 dict->vendors = NULL;
648 dict->typedefns = NULL;
663 last_enumitem = NULL;
665 last_typedefn = NULL;
671 current_yyinput = file_input;
675 D(("\n---------------\n%s\n------- %d -------\n",strbuf,len_strbuf));
677 current_yyinput = string_input;
689 void ddict_free(ddict_t* d) {
690 ddict_application_t *p, *pn;
691 ddict_vendor_t *v, *vn;
693 ddict_typedefn_t *t, *tn;
696 #define FREE_NAMEANDOBJ(n) do { if(n->name) g_free(n->name); g_free(n); } while(0)
698 for (p = d->applications; p; p = pn ) {
703 for (v = d->vendors; v; v = vn) {
705 if (!v->desc) g_free(v->desc);
709 for (c = d->cmds; c; c = cn ) {
714 for (t = d->typedefns; t; t = tn) {
716 if (!t->parent) g_free(t->parent);
720 for (a = d->avps; a; a = an) {
721 ddict_gavp_t* g, *gn;
722 ddict_enum_t* e, *en;
725 for (g = a->gavps; g; g = gn) {
730 for (e = a->enums; e; e = en) {
735 if (!a->vendor) g_free(a->vendor);
736 if (!a->type) g_free(a->type);
737 if (!a->description) g_free(a->description);
744 void ddict_print(FILE* fh, ddict_t* d) {
745 ddict_application_t* p;
752 for (p = d->applications; p; p = p->next) {
753 fprintf(fh,"Application: %s[%u]:\n",
754 p->name ? p->name : "-",
758 for (v = d->vendors; v; v = v->next) {
759 fprintf(fh,"Vendor: %s[%u]:\n",
760 v->name ? v->name : "-",
764 for (c = d->cmds; c; c = c->next) {
765 fprintf(fh,"Command: %s[%u] \n",
766 c->name ? c->name : "-",
770 for (t = d->typedefns; t; t = t->next) {
771 fprintf(fh,"Type: %s -> %s \n",
772 t->name ? t->name : "-",
773 t->parent ? t->parent : "" );
776 for (a = d->avps; a; a = a->next) {
779 fprintf(fh,"AVP: %s[%u:%s] %s %s\n",
780 a->name ? a->name : "-",
782 a->vendor ? a->vendor : "None",
783 a->type ? a->type : "-",
784 a->description ? a->description : "");
786 for (g = a->gavps; g; g = g->next) {
787 fprintf(fh,"\tGAVP: %s\n",
788 g->name ? g->name : "-" );
791 for (e = a->enums; e; e = e->next) {
792 fprintf(fh,"\tEnum: %s[%u]\n",
793 e->name ? e->name : "-",
800 * We want to stop processing when we get to the end of the input.
801 * (%option noyywrap is not used because if used then
802 * some flex versions (eg: 2.5.35) generate code which causes
803 * warnings by the Windows VC compiler).
810 #ifdef TEST_DIAM_DICT_STANDALONE
811 int main(int argc, char** argv) {
824 fprintf(stderr,"%s: usage [dictionary_dir] dictionary_filename\n",argv[0]);
828 d = ddict_scan(dname,fname,1);
830 fprintf(stderr, "Can't open dictionary\n");
834 ddict_print(stdout, d);