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