3637128555136125a2e6a267226c1141e400a109
[obnox/wireshark/wip.git] / plugins / wimaxasncp / wimaxasncp_dict.l
1 /*
2  * We don't use unput, so don't generate code for it.
3  */
4 %option nounput
5
6 /*
7  * We don't read from the terminal.
8  */
9 %option never-interactive
10
11 /*
12  * The language we're scanning is case-insensitive.
13  */
14 %option caseless
15
16 /*
17  * We use start condition stacks.
18  */
19 %option stack
20
21 /*
22  * Prefix scanner routines with "WimaxasncpDict" rather than "yy", so this
23  * scanner can coexist with other scanners.
24  */
25 %option prefix="WimaxasncpDict"
26
27 %option outfile="wimaxasncp_dict.c"
28
29 %{
30         /*
31          ** wimaxasncp_dict.h
32          ** WIMAXASNCP Dictionary Import Routines
33          **
34          ** $Id$
35          **
36          ** (c) 2007, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
37          ** (c) 2007, Stephen Croll <stephen.d.croll@gmail.com>
38          **
39          ** This library is free software; you can redistribute it and/or
40          ** modify it under the terms of the GNU Library General Public
41          ** License as published by the Free Software Foundation; either
42          ** version 2 of the License, or (at your option) any later version.
43          **
44          ** This library is distributed in the hope that it will be useful,
45          ** but WITHOUT ANY WARRANTY; without even the implied warranty of
46          ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
47          ** Library General Public License for more details.
48          **
49          ** You should have received a copy of the GNU Library General Public
50          ** License along with this library; if not, write to the Free Software
51          ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
52          ** Boston, MA  02110-1301, USA.
53          */
54
55 #ifdef HAVE_CONFIG_H
56 #include "config.h"
57 #endif
58
59 #include <glib.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <errno.h>
63 #include <stdlib.h>
64 #include <stdarg.h>
65 #include <epan/emem.h>
66 #include <epan/value_string.h>
67 #include <epan/packet.h>        /* array_length */
68 #include <wsutil/file_util.h>
69
70 #include "wimaxasncp_dict.h"
71 #include "wimaxasncp_dict_lex.h"
72
73 typedef struct entity_t {
74         gchar *name;
75         gchar *file;
76         struct entity_t *next;
77 } entity_t;
78
79 #define ATTR_UINT(cont) do { D(("attr_uint " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_UINT_ATTR); } while(0)
80 #define ATTR_UINT16(cont) do { D(("attr_uint16 " #cont "\t" )); attr_uint16 = &(cont); yy_push_state(GET_UINT16_ATTR); } while(0)
81 #define ATTR_STR(cont) do { D(("attr_str " #cont "\t" )); attr_str = &(cont); yy_push_state(GET_ATTR); } while(0)
82 #define ATTR_DECODER(cont) do { D(("attr_decoder " #cont "\t" )); attr_uint = &(cont); yy_push_state(GET_DECODER_ATTR); } while(0)
83 #define WIMAXASNCP_IGNORE() do { D(("ignore: %s\t",yytext)); yy_push_state(IGNORE_ATTR); } while(0)
84
85 #define D(args) wimaxasncp_dict_debug args
86
87 #define MAX_INCLUDE_DEPTH 10
88 #define YY_INPUT(buf,result,max_size) { result = current_yyinput(buf,max_size); }
89 #define ECHO
90 #define APPEND(txt,len) append_to_buffer(txt,len)
91
92 static entity_t ents;
93 static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
94 static int include_stack_ptr = 0;
95 static size_t (*current_yyinput)(gchar*,size_t);
96 static const gchar *sys_dir;
97 static wimaxasncp_dict_t *dict;
98 static wimaxasncp_dict_tlv_t *tlv;
99 static wimaxasncp_dict_enum_t *enumitem;
100 static wimaxasncp_dict_xmlpi_t *xmlpi;
101
102 static wimaxasncp_dict_tlv_t *last_tlv;
103 static wimaxasncp_dict_enum_t *last_enumitem;
104 static wimaxasncp_dict_xmlpi_t *last_xmlpi;
105
106 static gchar **attr_str;
107 static guint *attr_uint;
108 static gint16 *attr_uint16;
109
110 static guint wimaxasncp_bits(guint bits, char *n);
111 static gint wimaxasncp_decode_type(const gchar *name);
112 static void wimaxasncp_dict_debug(const gchar *fmt, ...);
113 static void append_to_buffer(gchar *txt, int len);
114 static FILE *wimaxasncp_dict_open(const gchar*, const gchar*);
115
116 static GString *dict_error = NULL;
117
118 %}
119
120
121 xmlpi_start [[:blank:] \r\n]*<\?[[:blank:] \r\n]*
122 xmlpi_end [[:blank:] \r\n]*\?>[[:blank:] \r\n]*
123 xmlpi_key_attr [[:blank:] \r\n]*key[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
124 xmlpi_value_attr [[:blank:] \r\n]*value[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
125
126 comment_start [[:blank:] \r\n]*<!--[[:blank:] \r\n]*
127 comment_end [[:blank:] \r\n]*-->[[:blank:] \r\n]*
128 open_tag [[:blank:] \r\n]*<[[:blank:] \r\n]*
129 end_tag [[:blank:] \r\n]*\/>[[:blank:] \r\n]*
130 close_tag [[:blank:] \r\n]*>[[:blank:] \r\n]*
131 open_closetag [[:blank:] \r\n]*<\/[[:blank:] \r\n]*
132 equals [[:blank:] \r\n]*=[[:blank:] \r\n]*
133 whitespace [[:blank:] \r\n]*
134 dquoted \042[^\042]*\042
135
136 doctype [[:blank:] \r\n]*<!DOCTYPE[^\[]*\[[[:blank:] \r\n]*
137 doctype_end [[:blank:] \r\n]*\][[:blank:] \r\n]*>[[:blank:] \r\n]*
138
139 start_entity [[:blank:] \r\n]*<\!ENTITY[[:blank:] \r\n]*
140 system [[:blank:] \r\n]*SYSTEM[[:blank:] \r\n]*\042
141 entityname [a-z0-9-]+
142 ndquot [^\042]+
143 end_entity \042[[:blank:] \r\n]*>[[:blank:] \r\n]*
144
145 entity \&[a-z0-9-]+;
146
147 any .
148
149
150
151
152 stop >
153 stop_end \/>
154 dquot \042
155 number [-]?[0-9]*|(0x)?[0-9a-fA-F]*
156
157 dictionary_start <dictionary>
158 dictionary_end <\/dictionary>
159
160 tlv_start <tlv
161 tlv_end <\/tlv>
162
163 type_start <type
164 enum_start <enum
165
166 ignored_attr [a-z0-9-]+=
167 ignored_quoted \042[^\042]*\042
168
169 name_attr name=\042
170 type_attr type=\042
171 code_attr code=\042
172 typename_attr type-name=\042
173 description_attr description=\042
174 decoder_attr decoder=\042
175
176
177 %S LOADING LOADING_COMMENT LOADING_XMLPI ENTITY GET_SYSTEM GET_FILE END_ENTITY
178 %S GET_ATTR GET_UINT_ATTR GET_UINT16_ATTR
179 %S BIT32 BIT16 BIT8 GET_DECODER_ATTR END_ATTR
180 %S OUTSIDE IN_DICT IN_APPL IN_TLV IGNORE_ATTR
181 %S ENUM_ATTRS TLV_ATTRS
182 %S XMLPI_ATTRS XMLPI_GETKEY XMLPI_GETVAL XMLPI_ENDATTR
183 %%
184 <LOADING>{doctype} ;
185 <LOADING>{doctype_end} ;
186
187 <LOADING>{comment_start} BEGIN LOADING_COMMENT;
188 <LOADING_COMMENT>. ;
189 <LOADING_COMMENT>{comment_end} BEGIN LOADING;
190
191 <LOADING>{xmlpi_start} BEGIN LOADING_XMLPI;
192 <LOADING_XMLPI>{whitespace} ;
193 <LOADING_XMLPI>{entityname} {
194         xmlpi = g_malloc(sizeof(wimaxasncp_dict_xmlpi_t));
195         xmlpi->name = g_strdup(yytext);
196         xmlpi->key = NULL;
197         xmlpi->value = NULL;
198         xmlpi->next = NULL;
199
200         if (!dict->xmlpis) last_xmlpi = dict->xmlpis = xmlpi;
201         else last_xmlpi = last_xmlpi->next = xmlpi;
202
203         BEGIN XMLPI_ATTRS;
204 }
205
206 <XMLPI_ATTRS>{xmlpi_key_attr} BEGIN XMLPI_GETKEY;
207 <XMLPI_GETKEY>{ndquot} { xmlpi->key = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
208
209 <XMLPI_ATTRS>{xmlpi_value_attr} BEGIN XMLPI_GETVAL;
210 <XMLPI_GETVAL>{ndquot} { xmlpi->value = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
211
212 <XMLPI_ATTRS>.
213 <XMLPI_ATTRS>{xmlpi_end} BEGIN LOADING;
214
215
216 <LOADING>{start_entity} BEGIN ENTITY;
217 <ENTITY>{entityname} {
218         entity_t *e = g_malloc(sizeof(entity_t));
219         D(("ENTITY: %s\n",yytext));
220         e->name = g_strdup(yytext);
221         e->next = ents.next;
222         ents.next = e;
223         BEGIN GET_SYSTEM;
224 };
225 <GET_SYSTEM>{system} BEGIN GET_FILE;
226 <GET_FILE>{ndquot} {
227         D(("GET_FILE: %s\n",yytext));
228         ents.next->file = g_strdup(yytext);
229         BEGIN END_ENTITY;
230 }
231 <END_ENTITY>{end_entity} BEGIN LOADING;
232
233 <LOADING>{open_tag} APPEND("<",1);
234
235 <LOADING>{close_tag} APPEND(">",1);
236
237 <LOADING>{end_tag} APPEND("/>",2);
238
239 <LOADING>{open_closetag} APPEND("</",2);
240
241 <LOADING>{whitespace} APPEND(" ",1);
242
243 <LOADING>{dquoted} APPEND(yytext,yyleng);
244
245 <LOADING>{equals} APPEND("=",1);
246
247 <LOADING>{any} APPEND(yytext,yyleng);
248
249 <LOADING,IN_DICT>{entity} {
250         gchar *p = ++yytext, *temp_str;
251         entity_t* e;
252
253         while(*p != ';') p++;
254
255         *p = '\0';
256
257         D(("looking for entity: %s\n",yytext));
258
259         if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
260                 dict_error = g_string_append(
261                         dict_error, "included files nested too deeply\n");
262                 yyterminate();
263         }
264
265         include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER;
266
267
268         for (e = ents.next; e; e = e->next) {
269                 if (strcmp(e->name,yytext) == 0) {
270                         yyin = wimaxasncp_dict_open(sys_dir,e->file);
271                         D(("entity: %s filename: %s yyin: %p\n",e->name,e->file,yyin));
272                         if (!yyin) {
273                                 yyterminate();
274                         } else {
275                                 yy_switch_to_buffer(yy_create_buffer( yyin, YY_BUF_SIZE ) );
276                         }
277                         break;
278                 }
279         }
280
281         if (!e) {
282                 temp_str = g_strdup_printf(
283                         "cannot find entity: '%s'\n", e->name);
284                 dict_error = g_string_append(dict_error, temp_str);
285                 g_free(temp_str);
286                 yyterminate();
287         }
288
289 }
290
291 <<EOF>> {
292         if (!yyin) yyterminate();
293
294         fclose(yyin);
295         D(("closing: %p %i\n",yyin,include_stack_ptr));
296
297         if ( --include_stack_ptr < 0 ) {
298                 D(("DONE READING\n"));
299                 yyin = NULL;
300                 yyterminate();
301         } else {
302                 yy_delete_buffer( YY_CURRENT_BUFFER );
303                 yy_switch_to_buffer(include_stack[include_stack_ptr]);
304                 BEGIN LOADING;
305         }
306 }
307
308
309 <GET_ATTR>{ndquot} {
310         *attr_str = g_strdup(yytext);
311         D(("%s\n",yytext));
312         attr_str = NULL;
313         BEGIN END_ATTR;
314 }
315
316 <GET_UINT_ATTR>{number} {
317         *attr_uint = strtoul(yytext,NULL,0);
318         D(("%s\n",yytext););
319         attr_uint = NULL;
320         BEGIN END_ATTR;
321 }
322
323 <GET_UINT16_ATTR>{number} {
324         *attr_uint16 = (gint16) strtol(yytext,NULL,0);
325         D(("%s\n",yytext););
326         attr_uint16 = NULL;
327         BEGIN END_ATTR;
328 }
329
330 <GET_UINT_ATTR>"WIMAXASNCP_BIT32"[ \t]*"("    { BEGIN BIT32; }
331
332 <BIT32>[0-9]+ {
333         *attr_uint = wimaxasncp_bits(32, yytext);
334         D(("WIMAXASNCP_BIT32(%s)\n",yytext););
335         attr_uint = NULL;
336 }
337
338 <GET_UINT_ATTR>"WIMAXASNCP_BIT16"[ \t]*"("    { BEGIN BIT16; }
339
340 <BIT16>[0-9]+ {
341         *attr_uint = wimaxasncp_bits(16, yytext);
342         D(("WIMAXASNCP_BIT16(%s)\n",yytext););
343         attr_uint = NULL;
344 }
345
346 <GET_UINT_ATTR>"WIMAXASNCP_BIT8"[ \t]*"("     { BEGIN BIT8; }
347
348 <BIT8>[0-9]+ {
349         *attr_uint = wimaxasncp_bits(8, yytext);
350         D(("WIMAXASNCP_BIT8(%s)\n",yytext););
351         attr_uint = NULL;
352 }
353
354 <BIT32,BIT16,BIT8>[ \t]*")" { BEGIN END_ATTR; }
355
356 <GET_DECODER_ATTR>{ndquot} {
357         *attr_uint = wimaxasncp_decode_type(yytext);
358         D(("%s\n",yytext));
359         attr_uint = NULL;
360         BEGIN END_ATTR;
361 }
362
363 <END_ATTR>{dquot} {     yy_pop_state(); }
364
365 <IGNORE_ATTR>. {
366         /* XXX: should go?*/
367         D(("{%s}",yytext));
368 }
369
370 <IGNORE_ATTR>{ignored_quoted} {
371         D(("=>%s<=\n",yytext));
372         yy_pop_state();
373 }
374
375 <OUTSIDE>{dictionary_start} {
376         D(("dictionary_start\n"));
377
378         BEGIN IN_DICT;
379 }
380
381 <IN_DICT>{tlv_start}    {
382         D(("tlv_start\n"));
383
384         tlv = g_malloc(sizeof(wimaxasncp_dict_tlv_t));
385         tlv->type = 0;
386         tlv->name = NULL;
387         tlv->description = NULL;
388         tlv->decoder = 0;
389         tlv->hf_root = -1;
390         tlv->hf_value = -1;
391         tlv->hf_ipv4 = -1;
392         tlv->hf_ipv6 = -1;
393         tlv->hf_bsid = -1;
394         tlv->hf_protocol = -1;
395         tlv->hf_port_low = -1;
396         tlv->hf_port_high = -1;
397         tlv->hf_ipv4_mask = -1;
398         tlv->hf_ipv6_mask = -1;
399         tlv->hf_vendor_id = -1;
400         tlv->hf_vendor_rest_of_info = -1;
401         tlv->enum_vs = NULL;
402         tlv->enums = NULL;
403         tlv->next = NULL;
404
405         if (! dict->tlvs ) last_tlv = dict->tlvs = tlv;
406         else last_tlv = last_tlv->next = tlv;
407
408         BEGIN TLV_ATTRS;
409 }
410
411 <TLV_ATTRS>{name_attr}                  { ATTR_STR(tlv->name); }
412 <TLV_ATTRS>{description_attr}           { ATTR_STR(tlv->description); }
413 <TLV_ATTRS>{type_attr}                  { ATTR_UINT16(tlv->type); }
414 <TLV_ATTRS>{decoder_attr}               { ATTR_DECODER(tlv->decoder); }
415 <TLV_ATTRS>{stop}                       { BEGIN IN_TLV;  }
416 <TLV_ATTRS>{stop_end}                   { BEGIN IN_DICT; }
417
418
419 <IN_TLV>{enum_start} {
420         D(("enum_start\n"));
421
422         enumitem = g_malloc(sizeof(wimaxasncp_dict_enum_t));
423         enumitem->name = NULL;
424         enumitem->code = 0;
425         enumitem->next = NULL;
426
427         if (!tlv->enums) last_enumitem = tlv->enums = enumitem;
428         else last_enumitem = last_enumitem->next = enumitem;
429
430         BEGIN ENUM_ATTRS;
431 }
432
433
434 <ENUM_ATTRS>{name_attr}                 { ATTR_STR(enumitem->name); }
435 <ENUM_ATTRS>{code_attr}                 { ATTR_UINT(enumitem->code); }
436
437 <ENUM_ATTRS>{stop}           { BEGIN IN_TLV; }
438 <ENUM_ATTRS>{stop_end}       { BEGIN IN_TLV; }
439
440 <IN_TLV>{tlv_end} { D(("tlv_end")); BEGIN IN_DICT; }
441
442 <IN_DICT>{dictionary_end} {
443         yyterminate();
444 }
445
446 <TLV_ATTRS,ENUM_ATTRS>{ignored_attr} WIMAXASNCP_IGNORE();
447
448 <OUTSIDE>. ;
449
450
451
452
453
454
455 %%
456
457 static int debugging  = 0;
458
459 static void wimaxasncp_dict_debug(const gchar *fmt, ...) {
460         va_list ap;
461
462         va_start(ap, fmt);
463         if (debugging) vfprintf(stderr, fmt, ap);
464         va_end(ap);
465
466         fflush(stderr);
467 }
468
469 static guint wimaxasncp_bits(guint bits, char *n)
470 {
471         return 1 << ((bits - 1) - (strtoul(n, NULL, 10)));
472 }
473
474 static const value_string wimaxasncp_decode_type_vals[] =
475 {
476   { WIMAXASNCP_TLV_TBD,                 "WIMAXASNCP_TLV_TBD"},
477   { WIMAXASNCP_TLV_COMPOUND,            "WIMAXASNCP_TLV_COMPOUND"},
478   { WIMAXASNCP_TLV_BYTES,               "WIMAXASNCP_TLV_BYTES"},
479   { WIMAXASNCP_TLV_ENUM8,               "WIMAXASNCP_TLV_ENUM8"},
480   { WIMAXASNCP_TLV_ENUM16,              "WIMAXASNCP_TLV_ENUM16"},
481   { WIMAXASNCP_TLV_ENUM32,              "WIMAXASNCP_TLV_ENUM32"},
482   { WIMAXASNCP_TLV_ETHER,               "WIMAXASNCP_TLV_ETHER"},
483   { WIMAXASNCP_TLV_ASCII_STRING,        "WIMAXASNCP_TLV_ASCII_STRING"},
484   { WIMAXASNCP_TLV_FLAG0,               "WIMAXASNCP_TLV_FLAG0"},
485   { WIMAXASNCP_TLV_BITFLAGS16,          "WIMAXASNCP_TLV_BITFLAGS16"},
486   { WIMAXASNCP_TLV_BITFLAGS32,          "WIMAXASNCP_TLV_BITFLAGS32"},
487   { WIMAXASNCP_TLV_ID,                  "WIMAXASNCP_TLV_ID"},
488   { WIMAXASNCP_TLV_HEX8,                "WIMAXASNCP_TLV_HEX8"},
489   { WIMAXASNCP_TLV_HEX16,               "WIMAXASNCP_TLV_HEX16"},
490   { WIMAXASNCP_TLV_HEX32,               "WIMAXASNCP_TLV_HEX32"},
491   { WIMAXASNCP_TLV_DEC8,                "WIMAXASNCP_TLV_DEC8"},
492   { WIMAXASNCP_TLV_DEC16,               "WIMAXASNCP_TLV_DEC16"},
493   { WIMAXASNCP_TLV_DEC32,               "WIMAXASNCP_TLV_DEC32"},
494   { WIMAXASNCP_TLV_IP_ADDRESS,          "WIMAXASNCP_TLV_IP_ADDRESS"},
495   { WIMAXASNCP_TLV_IPV4_ADDRESS,        "WIMAXASNCP_TLV_IPV4_ADDRESS"},
496   { WIMAXASNCP_TLV_PROTOCOL_LIST,       "WIMAXASNCP_TLV_PROTOCOL_LIST"},
497   { WIMAXASNCP_TLV_PORT_RANGE_LIST,     "WIMAXASNCP_TLV_PORT_RANGE_LIST"},
498   { WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST,"WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST"},
499   { WIMAXASNCP_TLV_EAP,                 "WIMAXASNCP_TLV_EAP"},
500   { WIMAXASNCP_TLV_VENDOR_SPECIFIC,     "WIMAXASNCP_TLV_VENDOR_SPECIFIC"},
501   { 0, NULL}
502 };
503
504 static gint wimaxasncp_decode_type(const gchar *name)
505 {
506         gsize i;
507         for (i = 0; i < array_length(wimaxasncp_decode_type_vals) - 1; ++i)
508         {
509                 if (strcmp(name, wimaxasncp_decode_type_vals[i].strptr) == 0)
510                 {
511                         return wimaxasncp_decode_type_vals[i].value;
512                 }
513         }
514
515         /* not found, emit some sort of error here? */
516
517         return WIMAXASNCP_TLV_TBD;
518 }
519
520 static gchar *strbuf = NULL;
521 static gchar *write_ptr = NULL;
522 static gchar *read_ptr = NULL;
523
524 static guint size_strbuf = 8192;
525 static guint len_strbuf = 0;
526
527 extern void wimaxasncp_dict_unused(void);
528 void wimaxasncp_dict_unused(void) {
529         yy_top_state();
530 }
531
532 static void append_to_buffer(gchar *txt, int len) {
533
534         if (strbuf == NULL) {
535                 read_ptr = write_ptr = strbuf = g_malloc(size_strbuf);
536         }
537
538         if ( (len_strbuf + len + 1) >= size_strbuf ) {
539                 read_ptr = strbuf = g_realloc(strbuf,size_strbuf *= 2);
540         }
541
542         write_ptr = strbuf + len_strbuf;
543         strncpy(write_ptr,txt,len);
544         len_strbuf += len;
545         strbuf[len_strbuf] = '\0';
546 }
547
548 static size_t file_input(gchar *buf, size_t max) {
549         size_t read;
550
551         read = fread(buf,1,max,yyin);
552
553         if ( read == max ) {
554                 return max;
555         } else if (read > 0) {
556                 return read;
557         } else {
558                 return YY_NULL;
559         }
560 }
561
562
563 static size_t string_input(gchar *buf, size_t max) {
564         if (read_ptr >= write_ptr ) {
565                 return YY_NULL;
566         } else if ( read_ptr + max > write_ptr ) {
567                 max = write_ptr - read_ptr;
568         }
569
570         memcpy(buf,read_ptr,max);
571         read_ptr += max;
572
573         return max;
574 }
575
576 static FILE *wimaxasncp_dict_open(
577         const gchar *system_directory,
578         const gchar *filename)
579 {
580         FILE *fh;
581         gchar *fname;
582         if (system_directory)
583         {
584                 int len =  strlen(system_directory) + strlen(filename)
585                         + strlen(G_DIR_SEPARATOR_S) + 1;
586                 fname = g_malloc(len);
587                 g_snprintf(fname, len, "%s%s%s",
588                            system_directory, G_DIR_SEPARATOR_S,filename);
589         }
590         else
591         {
592                 fname = g_strdup(filename);
593         }
594
595         fh = ws_fopen(fname,"r");
596
597         D(("fname: %s fh: %p\n",fname,fh));
598
599         g_free(fname);
600
601
602         return fh;
603 }
604
605 wimaxasncp_dict_t *wimaxasncp_dict_scan(
606         const gchar *system_directory, const gchar *filename, int dbg,
607         gchar **error) {
608
609         entity_t *e;
610
611         dict_error = g_string_new("");
612
613         debugging = dbg;
614
615         sys_dir = system_directory;
616
617         write_ptr = NULL;
618         read_ptr = NULL;
619
620         if (dict)
621         {
622                 wimaxasncp_dict_free(dict);
623         }
624
625         dict = g_malloc(sizeof(wimaxasncp_dict_t));
626         dict->tlvs = NULL;
627         dict->xmlpis = NULL;
628
629         tlv = NULL;
630         enumitem = NULL;
631         xmlpi = NULL;
632
633         last_tlv = NULL;
634         last_enumitem = NULL;
635         last_xmlpi = NULL;
636
637         ents.next = NULL;
638
639         yyin = wimaxasncp_dict_open(sys_dir,filename);
640
641         if (yyin)
642         {
643                 current_yyinput = file_input;
644                 BEGIN LOADING;
645                 yylex();
646
647                 D(("\n---------------\n%s\n------- %d -------\n",
648                    strbuf, len_strbuf));
649
650                 current_yyinput = string_input;
651
652                 BEGIN OUTSIDE;
653                 yylex();
654         }
655
656         e = ents.next;
657         while (e)
658         {
659                 entity_t *next = e->next;
660                 g_free(e->name);
661                 g_free(e->file);
662                 g_free(e);
663                 e = next;
664         }
665
666         g_free(strbuf);
667         strbuf = NULL;
668         size_strbuf = 8192;
669
670         if (dict_error->len > 0)
671         {
672                 *error = dict_error->str;
673                 g_string_free(dict_error, FALSE);
674         }
675         else
676         {
677                 *error = NULL;
678                 g_string_free(dict_error, TRUE);
679         }
680
681         return dict;
682 }
683
684 void wimaxasncp_dict_free(wimaxasncp_dict_t *d) {
685         wimaxasncp_dict_tlv_t *t, *tn;
686
687 #define FREE_NAMEANDOBJ(n) do { if(n->name) g_free(n->name); g_free(n); } while(0)
688
689         for (t = d->tlvs; t; t = tn) {
690                 wimaxasncp_dict_enum_t *e, *en;
691                 tn = t->next;
692
693                 for (e = t->enums; e; e = en) {
694                         en = e->next;
695                         FREE_NAMEANDOBJ(e);
696                 }
697
698                 if (!t->description)  g_free(t->description);
699                 FREE_NAMEANDOBJ(t);
700         }
701
702         g_free(d);
703 }
704
705 void wimaxasncp_dict_print(FILE *fh, wimaxasncp_dict_t *d) {
706         wimaxasncp_dict_tlv_t *tlv;
707
708         fprintf(fh,"\n");
709
710         for (tlv = d->tlvs; tlv; tlv = tlv->next) {
711                 wimaxasncp_dict_enum_t *e;
712                 fprintf(fh,"TLV: %s[%u] %s[%d] %s\n",
713                                 tlv->name ? tlv->name : "-",
714                                 tlv->type,
715                                 val_to_str(tlv->decoder,
716                                            wimaxasncp_decode_type_vals,
717                                            "Unknown"),
718                                 tlv->decoder,
719                                 tlv->description ? tlv->description : "");
720
721                 for (e = tlv->enums; e; e = e->next) {
722                         fprintf(fh,"\tEnum: %s[%u]\n",
723                                         e->name ? e->name : "-",
724                                         e->code);
725                 }
726         }
727 }
728
729 /*
730  * We want to stop processing when we get to the end of the input.
731  * (%option noyywrap is not used because if used then
732  * some flex versions (eg: 2.5.35) generate code which causes
733  * warnings by the Windows VC compiler).
734  */
735
736 int yywrap(void) {
737     return 1;
738 }
739
740 #ifdef TEST_WIMAXASNCP_DICT_STANDALONE
741 int main(int argc, char **argv) {
742         wimaxasncp_dict_t *d;
743         gchar *dname = NULL;
744         gchar *fname;
745         int i = 1;
746
747         switch (argc) {
748                 case 3:
749                         dname = argv[i++];
750                 case 2:
751                         fname = argv[i];
752                         break;
753                 default:
754                         fprintf(stderr,"%s: usage [dictionary_dir] dictionary_filename\n",argv[0]);
755                         return 1;
756         }
757
758         d = wimaxasncp_dict_scan(dname,fname,1);
759
760         if (dict_error)
761         {
762                 printf("wimaxasncp - %s", dict_error);
763                 g_free(dict_error);
764         }
765
766         wimaxasncp_dict_print(stdout, d);
767
768         return 0;
769 }
770 #endif