Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / plugins / epan / wimaxasncp / wimaxasncp_dict.l
blob0259aec180d9f0d21d1767abaec9cb3f5dc69272
1 %top {
2 /* Include this before everything else, for various large-file definitions */
3 #include "config.h"
4 #include <wireshark.h>
7 /*
8  * We want a reentrant scanner.
9  */
10 %option reentrant
13  * We don't use input, so don't generate code for it.
14  */
15 %option noinput
18  * We don't use unput, so don't generate code for it.
19  */
20 %option nounput
23  * We don't read interactively from the terminal.
24  */
25 %option never-interactive
28  * We want to stop processing when we get to the end of the input.
29  */
30 %option noyywrap
33  * The type for the state we keep for a scanner.
34  */
35 %option extra-type="WimaxasncpDict_scanner_state_t *"
38  * We have to override the memory allocators so that we don't get
39  * "unused argument" warnings from the yyscanner argument (which
40  * we don't use, as we have a global memory allocator).
41  *
42  * We provide, as macros, our own versions of the routines generated by Flex,
43  * which just call malloc()/realloc()/free() (as the Flex versions do),
44  * discarding the extra argument.
45  */
46 %option noyyalloc
47 %option noyyrealloc
48 %option noyyfree
51  * The language we're scanning is case-insensitive.
52  */
53 %option caseless
56  * We use start condition stacks.
57  */
58 %option stack
61  * Prefix scanner routines with "WimaxasncpDict_" rather than "yy", so this
62  * scanner can coexist with other scanners.
63  */
64 %option prefix="WimaxasncpDict_"
67         /*
68          ** wimaxasncp_dict.h
69          ** WIMAXASNCP Dictionary Import Routines
70          **
71          ** (c) 2007, Luis E. Garcia Ontanon <luis@ontanon.org>
72          ** (c) 2007, Stephen Croll <stephen.d.croll@gmail.com>
73          **
74          ** This library is free software; you can redistribute it and/or
75          ** modify it under the terms of the GNU Library General Public
76          ** License as published by the Free Software Foundation; either
77          ** version 2 of the License, or (at your option) any later version.
78          **
79          ** This library 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 GNU
82          ** Library General Public License for more details.
83          **
84          ** You should have received a copy of the GNU Library General Public
85          ** License along with this library; if not, write to the Free Software
86          ** Foundation, Inc., 51 Franklin Street, Fifth Floor,
87          ** Boston, MA  02110-1301, USA.
88          */
90 #include <glib.h>
91 #include <stdio.h>
92 #include <string.h>
93 #include <errno.h>
94 #include <stdlib.h>
95 #include <stdarg.h>
96 #include <epan/value_string.h>
97 #include <epan/packet.h>        /* array_length */
98 #include <wsutil/file_util.h>
100 #include "wimaxasncp_dict.h"
103  * Disable diagnostics in the code generated by Flex.
104  */
105 DIAG_OFF_FLEX()
107 typedef struct entity_t {
108         char *name;
109         char *file;
110         struct entity_t *next;
111 } entity_t;
113 #define ATTR_UINT(cont) do { D(("attr_uint " #cont "\t" )); yyextra->attr_uint = &(cont); yy_push_state(GET_UINT_ATTR, yyscanner); } while(0)
114 #define ATTR_UINT16(cont) do { D(("attr_uint16 " #cont "\t" )); yyextra->attr_uint16 = &(cont); yy_push_state(GET_UINT16_ATTR, yyscanner); } while(0)
115 #define ATTR_STR(cont) do { D(("attr_str " #cont "\t" )); yyextra->attr_str = &(cont); yy_push_state(GET_ATTR, yyscanner); } while(0)
116 #define ATTR_DECODER(cont) do { D(("attr_decoder " #cont "\t" )); yyextra->attr_uint = &(cont); yy_push_state(GET_DECODER_ATTR, yyscanner); } while(0)
117 #define WIMAXASNCP_IGNORE() do { D(("ignore: %s\t",yytext)); yy_push_state(IGNORE_ATTR, yyscanner); } while(0)
119 #define D(args) wimaxasncp_dict_debug args
121 #define MAX_INCLUDE_DEPTH 10
122 #define YY_INPUT(buf,result,max_size) { result = yyextra->current_yyinput(buf,max_size,yyscanner); }
123 #define YY_USER_INIT { \
124         WimaxasncpDict_scanner_state_t *scanner_state = WimaxasncpDict_get_extra(yyscanner); \
125         BEGIN(scanner_state->start_state); \
127 #define ECHO
128 #define APPEND(txt,len) append_to_buffer(txt,(int)len,yyextra)
130 typedef struct {
131         GString *dict_error;
133         const char *sys_dir;
135         char *strbuf;
136         unsigned size_strbuf;
137         unsigned len_strbuf;
139         char *write_ptr;
140         char *read_ptr;
142         wimaxasncp_dict_t *dict;
143         wimaxasncp_dict_tlv_t *tlv;
144         wimaxasncp_dict_enum_t *enumitem;
145         wimaxasncp_dict_xmlpi_t *xmlpi;
147         wimaxasncp_dict_tlv_t *last_tlv;
148         wimaxasncp_dict_enum_t *last_enumitem;
149         wimaxasncp_dict_xmlpi_t *last_xmlpi;
151         entity_t *ents;
153         YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH];
154         int include_stack_ptr;
155         size_t (*current_yyinput)(char*,size_t,yyscan_t);
157         char **attr_str;
158         unsigned *attr_uint;
159         int16_t *attr_uint16;
161         int start_state;
162 } WimaxasncpDict_scanner_state_t;
164 static unsigned wimaxasncp_bits(unsigned bits, char *n);
165 static int wimaxasncp_decode_type(const char *name);
166 static void wimaxasncp_dict_debug(const char *fmt, ...) G_GNUC_PRINTF(1, 2);
167 static void append_to_buffer(const char *txt, int len, WimaxasncpDict_scanner_state_t *state);
168 static FILE *wimaxasncp_dict_open(const char*, const char*);
171  * Sleazy hack to suppress compiler warnings in yy_fatal_error().
172  */
173 #define YY_EXIT_FAILURE ((void)yyscanner, 2)
176  * Macros for the allocators, to discard the extra argument.
177  */
178 #define WimaxasncpDict_alloc(size, yyscanner)           (void *)malloc(size)
179 #define WimaxasncpDict_realloc(ptr, size, yyscanner)    (void *)realloc((char *)(ptr), (size))
180 #define WimaxasncpDict_free(ptr, yyscanner)             free((char *)ptr)
185 xmlpi_start [[:blank:] \r\n]*<\?[[:blank:] \r\n]*
186 xmlpi_end [[:blank:] \r\n]*\?>[[:blank:] \r\n]*
187 xmlpi_key_attr [[:blank:] \r\n]*key[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
188 xmlpi_value_attr [[:blank:] \r\n]*value[[:blank:] \r\n]*=[[:blank:] \r\n]*\042
190 comment_start [[:blank:] \r\n]*<!--[[:blank:] \r\n]*
191 comment_end [[:blank:] \r\n]*-->[[:blank:] \r\n]*
192 open_tag [[:blank:] \r\n]*<[[:blank:] \r\n]*
193 end_tag [[:blank:] \r\n]*\/>[[:blank:] \r\n]*
194 close_tag [[:blank:] \r\n]*>[[:blank:] \r\n]*
195 open_closetag [[:blank:] \r\n]*<\/[[:blank:] \r\n]*
196 equals [[:blank:] \r\n]*=[[:blank:] \r\n]*
197 whitespace [[:blank:] \r\n]*
198 dquoted \042[^\042]*\042
200 doctype [[:blank:] \r\n]*<!DOCTYPE[^\[]*\[[[:blank:] \r\n]*
201 doctype_end [[:blank:] \r\n]*\][[:blank:] \r\n]*>[[:blank:] \r\n]*
203 start_entity [[:blank:] \r\n]*<\!ENTITY[[:blank:] \r\n]*
204 system [[:blank:] \r\n]*SYSTEM[[:blank:] \r\n]*\042
205 entityname [a-z0-9-]+
206 ndquot [^\042]+
207 end_entity \042[[:blank:] \r\n]*>[[:blank:] \r\n]*
209 entity \&[a-z0-9-]+;
211 any .
216 stop >
217 stop_end \/>
218 dquot \042
219 number [-]?[0-9]*|(0x)?[0-9a-fA-F]*
221 dictionary_start <dictionary>
222 dictionary_end <\/dictionary>
224 tlv_start <tlv
225 tlv_end <\/tlv>
227 type_start <type
228 enum_start <enum
230 ignored_attr [a-z0-9-]+=
231 ignored_quoted \042[^\042]*\042
233 name_attr name=\042
234 type_attr type=\042
235 code_attr code=\042
236 typename_attr type-name=\042
237 description_attr description=\042
238 decoder_attr decoder=\042
239 since_attr since=\042
242 %S LOADING LOADING_COMMENT LOADING_XMLPI ENTITY GET_SYSTEM GET_FILE END_ENTITY
243 %S GET_ATTR GET_UINT_ATTR GET_UINT16_ATTR
244 %S BIT32 BIT16 BIT8 GET_DECODER_ATTR END_ATTR
245 %S OUTSIDE IN_DICT IN_APPL IN_TLV IGNORE_ATTR
246 %S ENUM_ATTRS TLV_ATTRS
247 %S XMLPI_ATTRS XMLPI_GETKEY XMLPI_GETVAL XMLPI_ENDATTR
249 <LOADING>{doctype} ;
250 <LOADING>{doctype_end} ;
252 <LOADING>{comment_start} BEGIN LOADING_COMMENT;
253 <LOADING_COMMENT>. ;
254 <LOADING_COMMENT>{comment_end} BEGIN LOADING;
256 <LOADING>{xmlpi_start} BEGIN LOADING_XMLPI;
257 <LOADING_XMLPI>{whitespace} ;
258 <LOADING_XMLPI>{entityname} {
259         yyextra->xmlpi = g_new(wimaxasncp_dict_xmlpi_t,1);
260         yyextra->xmlpi->name = g_strdup(yytext);
261         yyextra->xmlpi->key = NULL;
262         yyextra->xmlpi->value = NULL;
263         yyextra->xmlpi->next = NULL;
265         if (!yyextra->dict->xmlpis) yyextra->last_xmlpi = yyextra->dict->xmlpis = yyextra->xmlpi;
266         else yyextra->last_xmlpi = yyextra->last_xmlpi->next = yyextra->xmlpi;
268         BEGIN XMLPI_ATTRS;
271 <XMLPI_ATTRS>{xmlpi_key_attr} BEGIN XMLPI_GETKEY;
272 <XMLPI_GETKEY>{ndquot} { yyextra->xmlpi->key = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
274 <XMLPI_ATTRS>{xmlpi_value_attr} BEGIN XMLPI_GETVAL;
275 <XMLPI_GETVAL>{ndquot} { yyextra->xmlpi->value = g_strdup(yytext); BEGIN XMLPI_ATTRS; }
277 <XMLPI_ATTRS>.
278 <XMLPI_ATTRS>{xmlpi_end} BEGIN LOADING;
281 <LOADING>{start_entity} BEGIN ENTITY;
282 <ENTITY>{entityname} {
283         entity_t *e = g_new(entity_t,1);
284         D(("ENTITY: %s\n",yytext));
285         e->name = g_strdup(yytext);
286         e->next = yyextra->ents;
287         yyextra->ents = e;
288         BEGIN GET_SYSTEM;
290 <GET_SYSTEM>{system} BEGIN GET_FILE;
291 <GET_FILE>{ndquot} {
292         D(("GET_FILE: %s\n",yytext));
293         yyextra->ents->file = g_strdup(yytext);
294         BEGIN END_ENTITY;
296 <END_ENTITY>{end_entity} BEGIN LOADING;
298 <LOADING>{open_tag} APPEND("<",1);
300 <LOADING>{close_tag} APPEND(">",1);
302 <LOADING>{end_tag} APPEND("/>",2);
304 <LOADING>{open_closetag} APPEND("</",2);
306 <LOADING>{whitespace} APPEND(" ",1);
308 <LOADING>{dquoted} APPEND(yytext,yyleng);
310 <LOADING>{equals} APPEND("=",1);
312 <LOADING>{any} APPEND(yytext,yyleng);
314 <LOADING,IN_DICT>{entity} {
315         char *p = ++yytext, *temp_str;
316         entity_t* e;
318         while(*p != ';') p++;
320         *p = '\0';
322         D(("looking for entity: %s\n",yytext));
324         if ( yyextra->include_stack_ptr >= MAX_INCLUDE_DEPTH ) {
325                 yyextra->dict_error = g_string_append(
326                         yyextra->dict_error, "included files nested too deeply\n");
327                 yyterminate();
328         }
330         yyextra->include_stack[yyextra->include_stack_ptr++] = YY_CURRENT_BUFFER;
333         for (e = yyextra->ents; e; e = e->next) {
334                 if (strcmp(e->name,yytext) == 0) {
335                         yyin = wimaxasncp_dict_open(yyextra->sys_dir,e->file);
336                         D(("entity: %s filename: %s yyin: %p\n",e->name,e->file,(void*)yyin));
337                         if (!yyin) {
338                                 yyterminate();
339                         } else {
340                                 WimaxasncpDict__switch_to_buffer(WimaxasncpDict__create_buffer(yyin, YY_BUF_SIZE, yyscanner), yyscanner);
341                         }
342                         break;
343                 }
344         }
346         if (!e) {
347                 temp_str = ws_strdup_printf(
348                         "cannot find entity: '%s'\n", yytext);
349                 yyextra->dict_error = g_string_append(yyextra->dict_error, temp_str);
350                 g_free(temp_str);
351                 yyterminate();
352         }
356 <<EOF>> {
357         if (!yyin) yyterminate();
359         fclose(yyin);
360         D(("closing: %p %i\n",(void*)yyin,yyextra->include_stack_ptr));
362         if ( --yyextra->include_stack_ptr < 0 ) {
363                 D(("DONE READING\n"));
364                 yyin = NULL;
365                 yyterminate();
366         } else {
367                 WimaxasncpDict__delete_buffer(YY_CURRENT_BUFFER, yyscanner);
368                 WimaxasncpDict__switch_to_buffer(yyextra->include_stack[yyextra->include_stack_ptr], yyscanner);
369                 BEGIN LOADING;
370         }
374 <GET_ATTR>{ndquot} {
375         *yyextra->attr_str = wmem_strdup(wmem_epan_scope(), yytext);
376         D(("%s\n",yytext));
377         yyextra->attr_str = NULL;
378         BEGIN END_ATTR;
381 <GET_UINT_ATTR>{number} {
382         *yyextra->attr_uint = (unsigned)strtoul(yytext,NULL,0);
383         D(("%s\n",yytext););
384         yyextra->attr_uint = NULL;
385         BEGIN END_ATTR;
388 <GET_UINT16_ATTR>{number} {
389         *yyextra->attr_uint16 = (int16_t) strtol(yytext,NULL,0);
390         D(("%s\n",yytext););
391         yyextra->attr_uint16 = NULL;
392         BEGIN END_ATTR;
395 <GET_UINT_ATTR>"WIMAXASNCP_BIT32"[ \t]*"("    { BEGIN BIT32; }
397 <BIT32>[0-9]+ {
398         *yyextra->attr_uint = wimaxasncp_bits(32, yytext);
399         D(("WIMAXASNCP_BIT32(%s)\n",yytext););
400         yyextra->attr_uint = NULL;
403 <GET_UINT_ATTR>"WIMAXASNCP_BIT16"[ \t]*"("    { BEGIN BIT16; }
405 <BIT16>[0-9]+ {
406         *yyextra->attr_uint = wimaxasncp_bits(16, yytext);
407         D(("WIMAXASNCP_BIT16(%s)\n",yytext););
408         yyextra->attr_uint = NULL;
411 <GET_UINT_ATTR>"WIMAXASNCP_BIT8"[ \t]*"("     { BEGIN BIT8; }
413 <BIT8>[0-9]+ {
414         *yyextra->attr_uint = wimaxasncp_bits(8, yytext);
415         D(("WIMAXASNCP_BIT8(%s)\n",yytext););
416         yyextra->attr_uint = NULL;
419 <BIT32,BIT16,BIT8>[ \t]*")" { BEGIN END_ATTR; }
421 <GET_DECODER_ATTR>{ndquot} {
422         *yyextra->attr_uint = wimaxasncp_decode_type(yytext);
423         D(("%s\n",yytext));
424         yyextra->attr_uint = NULL;
425         BEGIN END_ATTR;
428 <END_ATTR>{dquot} {     yy_pop_state(yyscanner); }
430 <IGNORE_ATTR>. {
431         /* XXX: should go?*/
432         D(("{%s}",yytext));
435 <IGNORE_ATTR>{ignored_quoted} {
436         D(("=>%s<=\n",yytext));
437         yy_pop_state(yyscanner);
440 <OUTSIDE>{dictionary_start} {
441         D(("dictionary_start\n"));
443         BEGIN IN_DICT;
446 <IN_DICT>{tlv_start}    {
447         D(("tlv_start\n"));
449         yyextra->tlv = wmem_new0(wmem_epan_scope(), wimaxasncp_dict_tlv_t);
451         if (! yyextra->dict->tlvs )
452                 yyextra->last_tlv = yyextra->dict->tlvs = yyextra->tlv;
453         else
454                 yyextra->last_tlv = yyextra->last_tlv->next = yyextra->tlv;
456         BEGIN TLV_ATTRS;
459 <TLV_ATTRS>{name_attr}                  { ATTR_STR(yyextra->tlv->name); }
460 <TLV_ATTRS>{description_attr}           { ATTR_STR(yyextra->tlv->description); }
461 <TLV_ATTRS>{type_attr}                  { ATTR_UINT16(yyextra->tlv->type); }
462 <TLV_ATTRS>{decoder_attr}               { ATTR_DECODER(yyextra->tlv->decoder); }
463 <TLV_ATTRS>{since_attr}                 { ATTR_UINT(yyextra->tlv->since); }
464 <TLV_ATTRS>{stop}                       { BEGIN IN_TLV;  }
465 <TLV_ATTRS>{stop_end}                   { BEGIN IN_DICT; }
468 <IN_TLV>{enum_start} {
469         D(("enum_start\n"));
471         yyextra->enumitem = wmem_new(wmem_epan_scope(), wimaxasncp_dict_enum_t);
472         yyextra->enumitem->name = NULL;
473         yyextra->enumitem->code = 0;
474         yyextra->enumitem->next = NULL;
476         if (!yyextra->tlv->enums)
477                 yyextra->last_enumitem = yyextra->tlv->enums = yyextra->enumitem;
478         else
479                 yyextra->last_enumitem = yyextra->last_enumitem->next = yyextra->enumitem;
481         BEGIN ENUM_ATTRS;
485 <ENUM_ATTRS>{name_attr}                 { ATTR_STR(yyextra->enumitem->name); }
486 <ENUM_ATTRS>{code_attr}                 { ATTR_UINT(yyextra->enumitem->code); }
488 <ENUM_ATTRS>{stop}           { BEGIN IN_TLV; }
489 <ENUM_ATTRS>{stop_end}       { BEGIN IN_TLV; }
491 <IN_TLV>{tlv_end} { D(("tlv_end")); BEGIN IN_DICT; }
493 <IN_DICT>{dictionary_end} {
494         yyterminate();
497 <TLV_ATTRS,ENUM_ATTRS>{ignored_attr} WIMAXASNCP_IGNORE();
499 <OUTSIDE>. ;
504  * Turn diagnostics back on, so we check the code that we've written.
505  */
506 DIAG_ON_FLEX()
508 static int debugging  = 0;
510 static void wimaxasncp_dict_debug(const char *fmt, ...) {
511         va_list ap;
513         va_start(ap, fmt);
514         if (debugging) vfprintf(stderr, fmt, ap);
515         va_end(ap);
517         fflush(stderr);
520 static unsigned wimaxasncp_bits(unsigned bits, char *n)
522         return 1u << ((bits - 1) - (strtoul(n, NULL, 10)));
525 static const value_string wimaxasncp_decode_type_vals[] =
527   { WIMAXASNCP_TLV_TBD,                 "WIMAXASNCP_TLV_TBD"},
528   { WIMAXASNCP_TLV_COMPOUND,            "WIMAXASNCP_TLV_COMPOUND"},
529   { WIMAXASNCP_TLV_BYTES,               "WIMAXASNCP_TLV_BYTES"},
530   { WIMAXASNCP_TLV_ENUM8,               "WIMAXASNCP_TLV_ENUM8"},
531   { WIMAXASNCP_TLV_ENUM16,              "WIMAXASNCP_TLV_ENUM16"},
532   { WIMAXASNCP_TLV_ENUM32,              "WIMAXASNCP_TLV_ENUM32"},
533   { WIMAXASNCP_TLV_ETHER,               "WIMAXASNCP_TLV_ETHER"},
534   { WIMAXASNCP_TLV_ASCII_STRING,        "WIMAXASNCP_TLV_ASCII_STRING"},
535   { WIMAXASNCP_TLV_FLAG0,               "WIMAXASNCP_TLV_FLAG0"},
536   { WIMAXASNCP_TLV_BITFLAGS8,           "WIMAXASNCP_TLV_BITFLAGS8"},
537   { WIMAXASNCP_TLV_BITFLAGS16,          "WIMAXASNCP_TLV_BITFLAGS16"},
538   { WIMAXASNCP_TLV_BITFLAGS32,          "WIMAXASNCP_TLV_BITFLAGS32"},
539   { WIMAXASNCP_TLV_ID,                  "WIMAXASNCP_TLV_ID"},
540   { WIMAXASNCP_TLV_HEX8,                "WIMAXASNCP_TLV_HEX8"},
541   { WIMAXASNCP_TLV_HEX16,               "WIMAXASNCP_TLV_HEX16"},
542   { WIMAXASNCP_TLV_HEX32,               "WIMAXASNCP_TLV_HEX32"},
543   { WIMAXASNCP_TLV_DEC8,                "WIMAXASNCP_TLV_DEC8"},
544   { WIMAXASNCP_TLV_DEC16,               "WIMAXASNCP_TLV_DEC16"},
545   { WIMAXASNCP_TLV_DEC32,               "WIMAXASNCP_TLV_DEC32"},
546   { WIMAXASNCP_TLV_IP_ADDRESS,          "WIMAXASNCP_TLV_IP_ADDRESS"},
547   { WIMAXASNCP_TLV_IPV4_ADDRESS,        "WIMAXASNCP_TLV_IPV4_ADDRESS"},
548   { WIMAXASNCP_TLV_PROTOCOL_LIST,       "WIMAXASNCP_TLV_PROTOCOL_LIST"},
549   { WIMAXASNCP_TLV_PORT_RANGE_LIST,     "WIMAXASNCP_TLV_PORT_RANGE_LIST"},
550   { WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST,"WIMAXASNCP_TLV_IP_ADDRESS_MASK_LIST"},
551   { WIMAXASNCP_TLV_EAP,                 "WIMAXASNCP_TLV_EAP"},
552   { WIMAXASNCP_TLV_VENDOR_SPECIFIC,     "WIMAXASNCP_TLV_VENDOR_SPECIFIC"},
553   { 0, NULL}
556 static int wimaxasncp_decode_type(const char *name)
558         size_t i;
559         for (i = 0; i < array_length(wimaxasncp_decode_type_vals) - 1; ++i)
560         {
561                 if (strcmp(name, wimaxasncp_decode_type_vals[i].strptr) == 0)
562                 {
563                         return wimaxasncp_decode_type_vals[i].value;
564                 }
565         }
567         /* not found, emit some sort of error here? */
569         return WIMAXASNCP_TLV_TBD;
572 extern void wimaxasncp_dict_unused(yyscan_t yyscanner);
573 void wimaxasncp_dict_unused(yyscan_t yyscanner) {
574         yy_top_state(yyscanner);
577 static void append_to_buffer(const char *txt, int len, WimaxasncpDict_scanner_state_t *state) {
579         if (state->strbuf == NULL) {
580                 state->read_ptr = state->write_ptr = state->strbuf = (char *)g_malloc(state->size_strbuf);
581         }
583         if (state->len_strbuf + len >= state->size_strbuf) {
584                 state->read_ptr = state->strbuf = (char *)g_realloc(state->strbuf,state->size_strbuf *= 2);
585         }
587         state->write_ptr = state->strbuf + state->len_strbuf;
588         memcpy(state->write_ptr, txt, len + 1);
589         state->len_strbuf += len;
592 static size_t file_input(char *buf, size_t max, yyscan_t scanner) {
593         FILE *in = yyget_in(scanner);
594         size_t read_cnt;
596         read_cnt = fread(buf,1,max,in);
598         if ( read_cnt == max ) {
599                 return max;
600         } else if (read_cnt > 0) {
601                 return read_cnt;
602         } else {
603                 return YY_NULL;
604         }
608 static size_t string_input(char *buf, size_t max, yyscan_t scanner) {
609         WimaxasncpDict_scanner_state_t *statep = yyget_extra(scanner);
611         if (statep->read_ptr >= statep->write_ptr ) {
612                 return YY_NULL;
613         } else if ( statep->read_ptr + max > statep->write_ptr ) {
614                 max = statep->write_ptr - statep->read_ptr;
615         }
617         memcpy(buf,statep->read_ptr,max);
618         statep->read_ptr += max;
620         return max;
623 static FILE *wimaxasncp_dict_open(
624         const char *system_directory,
625         const char *filename)
627         FILE *fh;
628         char *fname;
629         if (system_directory)
630         {
631                 fname = ws_strdup_printf("%s%s%s",
632                            system_directory, G_DIR_SEPARATOR_S,filename);
633         }
634         else
635         {
636                 fname = g_strdup(filename);
637         }
639         fh = ws_fopen(fname,"r");
641         D(("fname: %s fh: %p\n",fname,(void*)fh));
643         g_free(fname);
646         return fh;
649 wimaxasncp_dict_t *wimaxasncp_dict_scan(
650         const char *system_directory, const char *filename, int dbg,
651         char **error) {
653         WimaxasncpDict_scanner_state_t state;
654         FILE *in;
655         yyscan_t scanner;
656         entity_t *e;
658         debugging = dbg;
660         state.dict_error = g_string_new("");
662         state.sys_dir = system_directory;
664         state.dict = g_new(wimaxasncp_dict_t,1);
665         state.dict->tlvs = NULL;
666         state.dict->xmlpis = NULL;
668         state.strbuf = NULL;
669         state.size_strbuf = 8192;
670         state.len_strbuf = 0;
672         state.write_ptr = NULL;
673         state.read_ptr = NULL;
675         state.tlv = NULL;
676         state.enumitem = NULL;
677         state.xmlpi = NULL;
679         state.last_tlv = NULL;
680         state.last_enumitem = NULL;
681         state.last_xmlpi = NULL;
683         state.ents = NULL;
685         /*
686          * Pass 1.
687          *
688          * Reads the file, does some work, and stores a modified version
689          * of the file contents in memory.
690          */
691         state.current_yyinput = file_input;
692         state.include_stack_ptr = 0;
694         in = wimaxasncp_dict_open(system_directory,filename);
696         if (in == NULL) {
697                 /*
698                  * Couldn't open the dictionary.
699                  *
700                  * Treat all failures other then ENOENT as errors?
701                  */
702                 *error = NULL;
703                 return state.dict;
704         }
706         if (WimaxasncpDict_lex_init(&scanner) != 0) {
707                 D(("Can't initialize scanner: %s\n", strerror(errno)));
708                 fclose(in);
709                 g_free(state.dict);
710                 return NULL;
711         }
713         WimaxasncpDict_set_in(in, scanner);
715         /* Associate the state with the scanner */
716         WimaxasncpDict_set_extra(&state, scanner);
718         state.start_state = LOADING;
719         WimaxasncpDict_lex(scanner);
721         WimaxasncpDict_lex_destroy(scanner);
722         /*
723          * XXX - can the lexical analyzer terminate without closing
724          * all open input files?
725          */
727         D(("\n---------------\n%s\n------- %d -------\n",
728            state.strbuf, state.len_strbuf));
730         /*
731          * Pass 2.
732          *
733          * Reads the modified version of the file contents and does the
734          * rest of the work.
735          */
736         state.current_yyinput = string_input;
738         if (WimaxasncpDict_lex_init(&scanner) != 0) {
739                 D(("Can't initialize scanner: %s\n", strerror(errno)));
740                 fclose(in);
741                 g_free(state.dict);
742                 g_free(state.strbuf);
743                 return NULL;
744         }
746         /* Associate the state with the scanner */
747         WimaxasncpDict_set_extra(&state, scanner);
749         state.start_state = OUTSIDE;
750         WimaxasncpDict_lex(scanner);
752         WimaxasncpDict_lex_destroy(scanner);
753         g_free(state.strbuf);
755         e = state.ents;
756         while (e)
757         {
758                 entity_t *next = e->next;
759                 g_free(e->name);
760                 g_free(e->file);
761                 g_free(e);
762                 e = next;
763         }
765         if (state.dict_error->len > 0)
766         {
767                 *error = g_string_free(state.dict_error, FALSE);
768         }
769         else
770         {
771                 *error = NULL;
772                 g_string_free(state.dict_error, TRUE);
773         }
774         return state.dict;
777 void wimaxasncp_dict_free(wimaxasncp_dict_t *d) {
778         wimaxasncp_dict_tlv_t *t, *tn;
780 #define FREE_NAMEANDOBJ(n) do { g_free(n->name); g_free(n); } while(0)
782         for (t = d->tlvs; t; t = tn) {
783                 wimaxasncp_dict_enum_t *e, *en;
784                 tn = t->next;
786                 for (e = t->enums; e; e = en) {
787                         en = e->next;
788                         FREE_NAMEANDOBJ(e);
789                 }
791                 g_free(t->description);
792                 FREE_NAMEANDOBJ(t);
793         }
795         g_free(d);
798 void wimaxasncp_dict_print(FILE *fh, wimaxasncp_dict_t *d) {
799         wimaxasncp_dict_tlv_t *tlvp;
801         fprintf(fh,"\n");
803         for (tlvp = d->tlvs; tlvp; tlvp = tlvp->next) {
804                 wimaxasncp_dict_enum_t *e;
805                 fprintf(fh,"TLV: %s[%u] %s[%d] %s (since %u)\n",
806                                 tlvp->name ? tlvp->name : "-",
807                                 tlvp->type,
808                                 val_to_str(tlvp->decoder,
809                                            wimaxasncp_decode_type_vals,
810                                            "Unknown"),
811                                 tlvp->decoder,
812                                 tlvp->description ? tlvp->description : "",
813                                 tlvp->since);
815                 for (e = tlvp->enums; e; e = e->next) {
816                         fprintf(fh,"\tEnum: %s[%u]\n",
817                                         e->name ? e->name : "-",
818                                         e->code);
819                 }
820         }
823 #ifdef TEST_WIMAXASNCP_DICT_STANDALONE
824 int main(int argc, char **argv) {
825         wimaxasncp_dict_t *d;
826         char *dname = NULL;
827         char *fname;
828         int i = 1;
830         switch (argc) {
831                 case 3:
832                         dname = argv[i++];
833                 case 2:
834                         fname = argv[i];
835                         break;
836                 default:
837                         fprintf(stderr,"%s: usage [dictionary_dir] dictionary_filename\n",argv[0]);
838                         return 1;
839         }
841         d = wimaxasncp_dict_scan(dname,fname,1,&dict_error);
843         if (dict_error)
844         {
845                 printf("wimaxasncp - %s", dict_error);
846                 g_free(dict_error);
847         }
849         wimaxasncp_dict_print(stdout, d);
851         return 0;
853 #endif