Convert some deprecated tvb_length calls
[metze/wireshark/wip.git] / extcap_parser.c
1 /* extcap_parser.c
2  *
3  * Routines for extcap external capture
4  * Copyright 2013, Mike Ryan <mikeryan@lacklustre.net>
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24
25 #include <config.h>
26
27 #include <stdio.h>
28 #include <glib.h>
29 #include <string.h>
30
31 #include "extcap_parser.h"
32
33 void extcap_printf_complex(extcap_complex *comp) {
34         gchar *ret = extcap_get_complex_as_string(comp);
35         printf("%s", ret);
36         g_free(ret);
37 }
38
39 gchar *extcap_get_complex_as_string(extcap_complex *comp) {
40         /* Pick an arbitrary size that should be big enough */
41         gchar *ret = g_new(gchar, 32);
42
43         if (comp == NULL) {
44                 g_snprintf(ret, 32, "(null)");
45                 return ret;
46         }
47
48         switch (comp->complex_type) {
49         case EXTCAP_ARG_INTEGER:
50                 g_snprintf(ret, 32, "%d", comp->complex_value.int_value);
51                 break;
52         case EXTCAP_ARG_UNSIGNED:
53                 g_snprintf(ret, 32, "%u", comp->complex_value.uint_value);
54                 break;
55         case EXTCAP_ARG_LONG:
56                 g_snprintf(ret, 32, "%ld", comp->complex_value.long_value);
57                 break;
58         case EXTCAP_ARG_DOUBLE:
59                 g_snprintf(ret, 32, "%f", comp->complex_value.double_value);
60                 break;
61         case EXTCAP_ARG_BOOLEAN:
62                 g_snprintf(ret, 32, "%s",
63                                 comp->complex_value.bool_value ? "TRUE" : "FALSE");
64                 break;
65         case EXTCAP_ARG_STRING:
66         case EXTCAP_ARG_FILESELECT:
67                 g_free(ret);
68                 ret = g_strdup(comp->complex_value.string_value);
69                 break;
70         default:
71                 /* Nulling out the return string */
72                 g_snprintf(ret, 32, " ");
73                 break;
74         }
75
76         return ret;
77 }
78
79 extcap_complex *extcap_parse_complex(extcap_arg_type complex_type,
80                 const gchar *data) {
81         extcap_complex *rc = g_new(extcap_complex, 1);
82         gboolean success = FALSE;
83         long double exp_f;
84
85         switch (complex_type) {
86         case EXTCAP_ARG_INTEGER:
87                 if (sscanf(data, "%Lf", &exp_f) == 1) {
88                         rc->complex_value.int_value = (int) exp_f;
89                         success = TRUE;
90                         break;
91                 }
92                 break;
93         case EXTCAP_ARG_UNSIGNED:
94                 if (sscanf(data, "%Lf", &exp_f) == 1) {
95                         rc->complex_value.uint_value = (unsigned int) exp_f;
96                         success = TRUE;
97                         break;
98                 }
99                 break;
100         case EXTCAP_ARG_LONG:
101                 if (sscanf(data, "%Lf", &exp_f) == 1) {
102                         rc->complex_value.long_value = (long) exp_f;
103                         success = TRUE;
104                         break;
105                 }
106                 break;
107         case EXTCAP_ARG_DOUBLE:
108                 if (sscanf(data, "%Lf", &exp_f) == 1) {
109                         rc->complex_value.double_value = (double) exp_f;
110                         success = TRUE;
111                         break;
112                 }
113                 break;
114         case EXTCAP_ARG_BOOLEAN:
115         case EXTCAP_ARG_BOOLFLAG:
116                 if (data[0] == 't' || data[0] == 'T' || data[0] == '1') {
117                         rc->complex_value.bool_value = 1;
118                 } else {
119                         rc->complex_value.bool_value = 0;
120                 }
121                 success = TRUE;
122                 break;
123         case EXTCAP_ARG_STRING:
124         case EXTCAP_ARG_FILESELECT:
125                 rc->complex_value.string_value = g_strdup(data);
126                 success = TRUE;
127                 break;
128         default:
129                 break;
130         }
131
132         if (!success) {
133                 g_free(rc);
134                 return NULL ;
135         }
136
137         rc->complex_type = complex_type;
138         rc->value_filled = TRUE;
139
140         return rc;
141 }
142
143 gboolean extcap_compare_is_default(extcap_arg *element, extcap_complex *test) {
144         gboolean result = FALSE;
145
146         if (element->default_complex == NULL)
147                 return result;
148
149         switch (element->arg_type) {
150         case EXTCAP_ARG_INTEGER:
151                 if (extcap_complex_get_int(test)
152                                 == extcap_complex_get_int(element->default_complex))
153                         result = TRUE;
154                 break;
155         case EXTCAP_ARG_UNSIGNED:
156                 if (extcap_complex_get_uint(test)
157                                 == extcap_complex_get_uint(element->default_complex))
158                         result = TRUE;
159                 break;
160         case EXTCAP_ARG_LONG:
161                 if (extcap_complex_get_long(test)
162                                 == extcap_complex_get_long(element->default_complex))
163                         result = TRUE;
164                 break;
165         case EXTCAP_ARG_DOUBLE:
166                 if (extcap_complex_get_double(test)
167                                 == extcap_complex_get_double(element->default_complex))
168                         result = TRUE;
169                 break;
170         case EXTCAP_ARG_BOOLEAN:
171         case EXTCAP_ARG_BOOLFLAG:
172                 if (extcap_complex_get_bool(test)
173                                 == extcap_complex_get_bool(element->default_complex))
174                         result = TRUE;
175                 break;
176         case EXTCAP_ARG_STRING:
177                 if (strcmp(extcap_complex_get_string(test),
178                                 extcap_complex_get_string(element->default_complex)) == 0)
179                         result = TRUE;
180                 break;
181
182         default:
183                 break;
184         }
185
186         return result;
187 }
188
189 void extcap_free_complex(extcap_complex *comp) {
190         if (comp->complex_type == EXTCAP_ARG_STRING
191                         || comp->complex_type == EXTCAP_ARG_FILESELECT)
192                 g_free(comp->complex_value.string_value);
193
194         g_free(comp);
195 }
196
197 int extcap_complex_get_int(extcap_complex *comp) {
198         if ( comp == NULL )
199                 return (int)0;
200         return comp->complex_value.int_value;
201 }
202
203 unsigned int extcap_complex_get_uint(extcap_complex *comp) {
204         if ( comp == NULL )
205                 return (unsigned int)0;
206         return comp->complex_value.uint_value;
207 }
208
209 long extcap_complex_get_long(extcap_complex *comp) {
210         if ( comp == NULL )
211                 return (long)0;
212         return comp->complex_value.long_value;
213 }
214
215 double extcap_complex_get_double(extcap_complex *comp) {
216         if ( comp == NULL )
217                 return (double)0;
218         return comp->complex_value.double_value;
219 }
220
221 gboolean extcap_complex_get_bool(extcap_complex *comp) {
222         if ( comp == NULL )
223                 return FALSE;
224         return comp->complex_value.bool_value;
225 }
226
227 gchar *extcap_complex_get_string(extcap_complex *comp) {
228         return comp->complex_value.string_value;
229 }
230
231 void extcap_free_tokenized_param(extcap_token_param *v) {
232         if (v == NULL)
233                 return;
234
235         if (v->arg != NULL)
236                 g_free(v->arg);
237
238         if (v->value != NULL)
239                 g_free(v->value);
240
241         g_free(v);
242 }
243
244 void extcap_free_tokenized_sentence(extcap_token_sentence *s) {
245         extcap_token_param *tv;
246
247         if (s == NULL)
248                 return;
249
250         if (s->sentence != NULL)
251                 g_free(s->sentence);
252
253         while (s->param_list != NULL ) {
254                 tv = s->param_list;
255                 s->param_list = tv->next_token;
256
257                 extcap_free_tokenized_param(tv);
258         }
259 }
260
261 void extcap_free_tokenized_sentence_list(extcap_token_sentence *f) {
262         extcap_token_sentence *t;
263
264         while (f != NULL ) {
265                 t = f->next_sentence;
266                 extcap_free_tokenized_sentence(f);
267                 f = t;
268         }
269 }
270
271 extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
272         gchar *b, *e, *eq;
273
274         extcap_token_param *tv = NULL;
275
276         extcap_token_sentence *rs = g_new(extcap_token_sentence, 1);
277
278         rs->sentence = NULL;
279         rs->next_sentence = NULL;
280         rs->param_list = NULL;
281
282         if ((b = g_strstr_len(s, -1, " ")) == NULL) {
283                 extcap_free_tokenized_sentence(rs);
284                 return NULL ;
285         }
286
287         rs->sentence = g_strndup(s, b - s);
288
289         if ((b = g_strstr_len(s, -1, "{")) == NULL) {
290                 /* printf("debug - tokenizer - sentence with no values\n"); */
291                 extcap_free_tokenized_sentence(rs);
292                 return NULL ;
293         }
294
295         while (b != NULL ) {
296                 if ((e = g_strstr_len(b, -1, "}")) == NULL) {
297                         /* printf("debug - tokenizer - invalid, missing }\n"); */
298                         extcap_free_tokenized_sentence(rs);
299                         return NULL ;
300                 }
301
302                 if ((eq = g_strstr_len(b, -1, "=")) == NULL) {
303                         /* printf("debug - tokenizer - invalid, missing =\n"); */
304                         extcap_free_tokenized_sentence(rs);
305                         return NULL ;
306                 }
307
308                 b++;
309                 e--;
310
311                 if (b >= eq || e <= eq) {
312                         /* printf("debug - tokenizer - invalid, missing arg or value in {}\n"); */
313                         extcap_free_tokenized_sentence(rs);
314                         return NULL ;
315                 }
316
317                 tv = g_new(extcap_token_param, 1);
318                 tv->arg = g_strndup(b, eq - b);
319                 tv->value = g_strndup(eq + 1, e - eq);
320
321                 if (g_ascii_strcasecmp(tv->arg, "number") == 0) {
322                         tv->param_type = EXTCAP_PARAM_ARGNUM;
323                 } else if (g_ascii_strcasecmp(tv->arg, "call") == 0) {
324                         tv->param_type = EXTCAP_PARAM_CALL;
325                 } else if (g_ascii_strcasecmp(tv->arg, "display") == 0) {
326                         tv->param_type = EXTCAP_PARAM_DISPLAY;
327                 } else if (g_ascii_strcasecmp(tv->arg, "type") == 0) {
328                         tv->param_type = EXTCAP_PARAM_TYPE;
329                 } else if (g_ascii_strcasecmp(tv->arg, "arg") == 0) {
330                         tv->param_type = EXTCAP_PARAM_ARG;
331                 } else if (g_ascii_strcasecmp(tv->arg, "default") == 0) {
332                         tv->param_type = EXTCAP_PARAM_DEFAULT;
333                 } else if (g_ascii_strcasecmp(tv->arg, "value") == 0) {
334                         tv->param_type = EXTCAP_PARAM_VALUE;
335                 } else if (g_ascii_strcasecmp(tv->arg, "range") == 0) {
336                         tv->param_type = EXTCAP_PARAM_RANGE;
337                 } else if (g_ascii_strcasecmp(tv->arg, "tooltip") == 0) {
338                         tv->param_type = EXTCAP_PARAM_TOOLTIP;
339                 } else if (g_ascii_strcasecmp(tv->arg, "mustexist") == 0) {
340                         tv->param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
341                 } else if (g_ascii_strcasecmp(tv->arg, "name") == 0) {
342                         tv->param_type = EXTCAP_PARAM_NAME;
343                 } else if (g_ascii_strcasecmp(tv->arg, "enabled") == 0) {
344                         tv->param_type = EXTCAP_PARAM_ENABLED;
345                 } else {
346                         tv->param_type = EXTCAP_PARAM_UNKNOWN;
347                 }
348
349                 tv->next_token = rs->param_list;
350                 rs->param_list = tv;
351
352                 /* printf("debug - tokenizer - got '%s' = '%s'\n", tv->arg, tv->value); */
353
354                 b = e + 1;
355                 if ((size_t) (b - s) > strlen(s))
356                         break;
357
358                 b = g_strstr_len(b, -1, "{");
359         }
360
361         return rs;
362 }
363
364 extcap_token_sentence *extcap_tokenize_sentences(const gchar *s) {
365         extcap_token_sentence *first = NULL, *cur = NULL, *last = NULL;
366
367         gchar **list, **list_iter;
368
369         list_iter = list = g_strsplit(s, "\n", 0);
370
371         while (*list_iter != NULL ) {
372                 cur = extcap_tokenize_sentence(*list_iter);
373
374                 if (cur != NULL) {
375                         if (first == NULL) {
376                                 first = cur;
377                                 last = cur;
378                         } else {
379                                 last->next_sentence = cur;
380                                 last = cur;
381                         }
382                 }
383
384                 list_iter++;
385         }
386
387         g_strfreev(list);
388
389         return first;
390 }
391
392 extcap_token_param *extcap_find_param_by_type(extcap_token_param *first,
393                 extcap_param_type t) {
394         while (first != NULL ) {
395                 if (first->param_type == t) {
396                         return first;
397                 }
398
399                 first = first->next_token;
400         }
401
402         return NULL ;
403 }
404
405 void extcap_free_value(extcap_value *v) {
406         if (v == NULL)
407                 return;
408
409         if (v->call != NULL)
410                 g_free(v->call);
411
412         if (v->display != NULL)
413                 g_free(v->display);
414
415         g_free(v);
416 }
417
418 extcap_interface *extcap_new_interface(void) {
419         extcap_interface *r = g_new(extcap_interface, 1);
420
421         r->call = r->display = NULL;
422         r->next_interface = NULL;
423
424         return r;
425 }
426
427 void extcap_free_interface(extcap_interface *i) {
428         if (i == NULL)
429                 return;
430
431         if (i->call != NULL)
432                 g_free(i->call);
433
434         if (i->display != NULL)
435                 g_free(i->display);
436 }
437
438 extcap_dlt *extcap_new_dlt(void) {
439         extcap_dlt *r = g_new(extcap_dlt, 1);
440
441         r->number = -1;
442         r->name = r->display = NULL;
443         r->next_dlt = NULL;
444
445         return r;
446 }
447
448 void extcap_free_dlt(extcap_dlt *d) {
449         if (d == NULL)
450                 return;
451
452         if (d->name != NULL)
453                 g_free(d->name);
454
455         if (d->display != NULL)
456                 g_free(d->display);
457 }
458
459 extcap_arg *extcap_new_arg(void) {
460         extcap_arg *r = g_new(extcap_arg, 1);
461
462         r->call = NULL;
463         r->display = NULL;
464         r->tooltip = NULL;
465         r->arg_type = EXTCAP_ARG_UNKNOWN;
466         r->range_start = NULL;
467         r->range_end = NULL;
468         r->default_complex = NULL;
469         r->fileexists = FALSE;
470
471         r->values = NULL;
472         /*r->next_arg = NULL; */
473
474         return r;
475 }
476
477 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
478         extcap_free_value((extcap_value *) data);
479 }
480
481 void extcap_free_arg(extcap_arg *a) {
482
483         if (a == NULL)
484                 return;
485
486         if (a->call != NULL)
487                 g_free(a->call);
488
489         if (a->display != NULL)
490                 g_free(a->display);
491
492         if (a->tooltip != NULL)
493                 g_free(a->tooltip);
494
495         if (a->range_start != NULL)
496                 extcap_free_complex(a->range_start);
497
498         if (a->range_end != NULL)
499                 extcap_free_complex(a->range_end);
500
501         if (a->default_complex != NULL)
502                 extcap_free_complex(a->default_complex);
503
504         g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
505 }
506
507 static void extcap_free_arg_list_cb(gpointer listentry, gpointer data _U_) {
508         if (listentry != NULL)
509                 extcap_free_arg((extcap_arg *) listentry);
510 }
511
512 void extcap_free_arg_list(GList *a) {
513         g_list_foreach(a, extcap_free_arg_list_cb, NULL);
514         g_list_free(a);
515 }
516
517 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
518         if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
519                 return 0;
520         return 1;
521 }
522
523 extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
524         extcap_token_param *v = NULL;
525         extcap_arg *target_arg = NULL;
526         extcap_value *value = NULL;
527         GList * entry = NULL;
528         int tint;
529         extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
530
531         if (s == NULL)
532                 return target_arg;
533
534         if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
535                 sent = EXTCAP_SENTENCE_ARG;
536                 /* printf("ARG sentence\n"); */
537         } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
538                 sent = EXTCAP_SENTENCE_VALUE;
539                 /* printf("VALUE sentence\n"); */
540         }
541
542         if (sent == EXTCAP_SENTENCE_ARG) {
543                 target_arg = extcap_new_arg();
544
545                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
546                                 == NULL) {
547                         extcap_free_arg(target_arg);
548                         return NULL ;
549                 }
550
551                 if (sscanf(v->value, "%d", &(target_arg->arg_num)) != 1) {
552                         extcap_free_arg(target_arg);
553                         return NULL ;
554                 }
555
556                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_CALL))
557                                 == NULL) {
558                         extcap_free_arg(target_arg);
559                         return NULL ;
560                 }
561                 target_arg->call = g_strdup(v->value);
562
563                 /* No value only parameters allowed */
564                 if (strlen(target_arg->call) == 0) {
565                         extcap_free_arg(target_arg);
566                         return NULL ;
567                 }
568
569                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
570                                 == NULL) {
571                         extcap_free_arg(target_arg);
572                         return NULL ;
573                 }
574                 target_arg->display = g_strdup(v->value);
575
576                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TOOLTIP))
577                                 != NULL) {
578                         target_arg->tooltip = g_strdup(v->value);
579                 }
580
581                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_MUSTEXIST))
582                                 != NULL) {
583                         target_arg->fileexists = (v->value[0] == 't' || v->value[0] == 'T');
584                 }
585
586                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TYPE))
587                                 == NULL) {
588                         /* printf("no type in ARG sentence\n"); */
589                         extcap_free_arg(target_arg);
590                         return NULL ;
591                 }
592
593                 if (g_ascii_strcasecmp(v->value, "integer") == 0) {
594                         target_arg->arg_type = EXTCAP_ARG_INTEGER;
595                 } else if (g_ascii_strcasecmp(v->value, "unsigned") == 0) {
596                         target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
597                 } else if (g_ascii_strcasecmp(v->value, "long") == 0) {
598                         target_arg->arg_type = EXTCAP_ARG_LONG;
599                 } else if (g_ascii_strcasecmp(v->value, "double") == 0) {
600                         target_arg->arg_type = EXTCAP_ARG_DOUBLE;
601                 } else if (g_ascii_strcasecmp(v->value, "boolean") == 0) {
602                         target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
603                 } else if (g_ascii_strcasecmp(v->value, "boolflag") == 0) {
604                         target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
605                 } else if (g_ascii_strcasecmp(v->value, "menu") == 0) {
606                         target_arg->arg_type = EXTCAP_ARG_MENU;
607                 } else if (g_ascii_strcasecmp(v->value, "selector") == 0) {
608                         target_arg->arg_type = EXTCAP_ARG_SELECTOR;
609                 } else if (g_ascii_strcasecmp(v->value, "radio") == 0) {
610                         target_arg->arg_type = EXTCAP_ARG_RADIO;
611                 } else if (g_ascii_strcasecmp(v->value, "string") == 0) {
612                         target_arg->arg_type = EXTCAP_ARG_STRING;
613                 } else if (g_ascii_strcasecmp(v->value, "fileselect") == 0) {
614                         target_arg->arg_type = EXTCAP_ARG_FILESELECT;
615                 } else if (g_ascii_strcasecmp(v->value, "multicheck") == 0) {
616                         target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
617                 } else {
618                         printf("invalid type %s in ARG sentence\n", v->value);
619                         extcap_free_arg(target_arg);
620                         return NULL ;
621                 }
622
623                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_RANGE))
624                                 != NULL) {
625                         gchar *cp = g_strstr_len(v->value, -1, ",");
626
627                         if (cp == NULL) {
628                                 printf("invalid range, expected value,value got %s\n",
629                                                 v->value);
630                                 extcap_free_arg(target_arg);
631                                 return NULL ;
632                         }
633
634                         if ((target_arg->range_start = extcap_parse_complex(
635                                         target_arg->arg_type, v->value)) == NULL) {
636                                 printf("invalid range, expected value,value got %s\n",
637                                                 v->value);
638                                 extcap_free_arg(target_arg);
639                                 return NULL ;
640                         }
641
642                         if ((target_arg->range_end = extcap_parse_complex(
643                                         target_arg->arg_type, cp + 1)) == NULL) {
644                                 printf("invalid range, expected value,value got %s\n",
645                                                 v->value);
646                                 extcap_free_arg(target_arg);
647                                 return NULL ;
648                         }
649                 }
650
651                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
652                                 != NULL) {
653                         if ((target_arg->default_complex = extcap_parse_complex(
654                                         target_arg->arg_type, v->value)) == NULL) {
655                                 printf("invalid default, couldn't parse %s\n", v->value);
656                         }
657                 }
658
659         } else if (sent == EXTCAP_SENTENCE_VALUE) {
660                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARG))
661                                 == NULL) {
662                         printf("no arg in VALUE sentence\n");
663                         return NULL ;
664                 }
665
666                 if (sscanf(v->value, "%d", &tint) != 1) {
667                         printf("invalid arg in VALUE sentence\n");
668                         return NULL ;
669                 }
670
671                 ;
672                 if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
673                                 == NULL) {
674                         printf("couldn't find arg %d in list for VALUE sentence\n", tint);
675                         return NULL ;
676                 }
677
678                 value = g_new(extcap_value, 1);
679                 value->display = NULL;
680                 value->call = NULL;
681                 value->enabled = FALSE;
682                 value->is_default = FALSE;
683                 value->arg_num = tint;
684
685                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
686                                 == NULL) {
687                         /* printf("no value in VALUE sentence\n"); */
688                         extcap_free_value(value);
689                         return NULL ;
690                 }
691                 value->call = g_strdup(v->value);
692
693                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
694                                 == NULL) {
695                         /* printf("no display in VALUE sentence\n"); */
696                         extcap_free_value(value);
697                         return NULL ;
698                 }
699                 value->display = g_strdup(v->value);
700
701                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
702                                 != NULL) {
703                         /* printf("found default value\n"); */
704                         value->is_default = (v->value[0] == 't' || v->value[0] == 'T');
705                 }
706
707                 if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ENABLED))
708                                 != NULL) {
709                         value->enabled = (v->value[0] == 't' || v->value[0] == 'T');
710                 }
711
712                 ((extcap_arg*) entry->data)->values = g_list_append(
713                                 ((extcap_arg*) entry->data)->values, value);
714
715                 return NULL ;
716         }
717
718         return target_arg;
719 }
720
721 GList * extcap_parse_args(extcap_token_sentence *first_s) {
722         GList * args = NULL;
723
724         while (first_s) {
725                 extcap_arg *ra = NULL;
726
727                 if ((ra = extcap_parse_arg_sentence(args, first_s)) != NULL)
728                         args = g_list_append(args, (gpointer) ra);
729
730                 first_s = first_s->next_sentence;
731         }
732
733         return args;
734 }
735
736 int extcap_parse_interface_sentence(extcap_token_sentence *s,
737                 extcap_interface **ri) {
738         extcap_token_param *v = NULL;
739         extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
740
741         *ri = NULL;
742
743         if (s == NULL)
744                 return -1;
745
746         if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
747                 sent = EXTCAP_SENTENCE_INTERFACE;
748                 /* printf("INTERFACE sentence\n"); */
749         }
750
751         if (sent == EXTCAP_SENTENCE_UNKNOWN)
752                 return -1;
753
754         *ri = extcap_new_interface();
755
756         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
757                         == NULL) {
758                 printf("No value in INTERFACE sentence\n");
759                 extcap_free_interface(*ri);
760                 return -1;
761         }
762         (*ri)->call = g_strdup(v->value);
763
764         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
765                         == NULL) {
766                 printf("No display in INTERFACE sentence\n");
767                 extcap_free_interface(*ri);
768                 return -1;
769         }
770         (*ri)->display = g_strdup(v->value);
771
772         return 1;
773 }
774
775 int extcap_parse_interfaces(extcap_token_sentence *first_s,
776                 extcap_interface **first_int) {
777         extcap_interface *first_i = NULL, *last_i = NULL;
778
779         while (first_s) {
780                 extcap_interface *ri;
781
782                 if (extcap_parse_interface_sentence(first_s, &ri) >= 0 && ri != NULL) {
783                         if (first_i == NULL) {
784                                 first_i = last_i = ri;
785                         } else {
786                                 last_i->next_interface = ri;
787                                 last_i = ri;
788                         }
789                 }
790
791                 first_s = first_s->next_sentence;
792         }
793
794         *first_int = first_i;
795
796         return 1;
797 }
798
799 int extcap_parse_dlt_sentence(extcap_token_sentence *s, extcap_dlt **rd) {
800         extcap_token_param *v = NULL;
801         extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
802
803         *rd = NULL;
804
805         if (s == NULL)
806                 return -1;
807
808         if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
809                 sent = EXTCAP_SENTENCE_DLT;
810         }
811
812         if (sent == EXTCAP_SENTENCE_UNKNOWN)
813                 return -1;
814
815         *rd = extcap_new_dlt();
816
817         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
818                         == NULL) {
819                 printf("No number in DLT sentence\n");
820                 extcap_free_dlt(*rd);
821                 return -1;
822         }
823         if (sscanf(v->value, "%d", &((*rd)->number)) != 1) {
824                 printf("Invalid number in DLT sentence\n");
825                 extcap_free_dlt(*rd);
826                 return -1;
827         }
828
829         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_NAME))
830                         == NULL) {
831                 printf("No name in DLT sentence\n");
832                 extcap_free_dlt(*rd);
833                 return -1;
834         }
835         (*rd)->name = g_strdup(v->value);
836
837         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
838                         == NULL) {
839                 printf("No display in DLT sentence\n");
840                 extcap_free_dlt(*rd);
841                 return -1;
842         }
843         (*rd)->display = g_strdup(v->value);
844
845         return 1;
846 }
847
848 int extcap_parse_dlts(extcap_token_sentence *first_s, extcap_dlt **first_dlt) {
849         extcap_dlt *first_d = NULL, *last_d = NULL;
850
851         while (first_s) {
852                 extcap_dlt *rd;
853
854                 if (extcap_parse_dlt_sentence(first_s, &rd) >= 0 && rd != NULL) {
855                         if (first_d == NULL) {
856                                 first_d = last_d = rd;
857                         } else {
858                                 last_d->next_dlt = rd;
859                                 last_d = rd;
860                         }
861                 }
862
863                 first_s = first_s->next_sentence;
864         }
865
866         *first_dlt = first_d;
867
868         return 1;
869 }
870
871 /*
872  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
873  *
874  * Local variables:
875  * c-basic-offset: 4
876  * tab-width: 4
877  * indent-tabs-mode: t
878  * End:
879  *
880  * vi: set shiftwidth=4 tabstop=4 noexpandtab:
881  * :indentSize=4:tabSize=4:noTabs=false:
882  */