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