3fe0817465f02c0eaadfd750e4c9acf4fa848a7b
[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.h"
32 #include "extcap_parser.h"
33
34 void extcap_printf_complex(extcap_complex *comp) {
35     gchar *ret = extcap_get_complex_as_string(comp);
36     printf("%s", ret);
37     g_free(ret);
38 }
39
40 gchar *extcap_get_complex_as_string(extcap_complex *comp) {
41     return (comp ? g_strdup(comp->_val) : NULL);
42 }
43
44 extcap_complex *extcap_parse_complex(extcap_arg_type complex_type,
45         const gchar *data) {
46
47     extcap_complex *rc = g_new0(extcap_complex, 1);
48
49     rc->_val = g_strdup(data);
50     rc->complex_type = complex_type;
51
52     return rc;
53 }
54
55 gboolean extcap_compare_is_default(extcap_arg *element, extcap_complex *test) {
56     if ( element == NULL || element->default_complex == NULL || test == NULL )
57         return FALSE;
58
59     if ( g_strcmp0(element->default_complex->_val, test->_val) == 0 )
60         return TRUE;
61
62     return FALSE;
63 }
64
65 void extcap_free_complex(extcap_complex *comp) {
66     if ( comp )
67         g_free(comp->_val);
68     g_free(comp);
69 }
70
71 gint extcap_complex_get_int(extcap_complex *comp) {
72     if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_INTEGER )
73         return (gint)0;
74
75     return (gint) g_ascii_strtoll(comp->_val, NULL, 10);
76 }
77
78 guint extcap_complex_get_uint(extcap_complex *comp) {
79     if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_UNSIGNED )
80         return (guint)0;
81     return (guint) g_ascii_strtoull(comp->_val, NULL, 10);
82 }
83
84 gint64 extcap_complex_get_long(extcap_complex *comp) {
85     if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_LONG )
86         return (gint64)0;
87     return g_ascii_strtoll( comp->_val, NULL, 10 );
88 }
89
90 gdouble extcap_complex_get_double(extcap_complex *comp) {
91     if ( comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_DOUBLE )
92         return (gdouble)0;
93     return g_strtod( comp->_val, NULL );
94 }
95
96 gboolean extcap_complex_get_bool(extcap_complex *comp) {
97     if ( comp == NULL || comp->_val == NULL  )
98         return FALSE;
99
100     if ( comp->complex_type != EXTCAP_ARG_BOOLEAN && comp->complex_type != EXTCAP_ARG_BOOLFLAG )
101         return FALSE;
102
103     return g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, comp->_val, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
104 }
105
106 gchar *extcap_complex_get_string(extcap_complex *comp) {
107     /* Not checking for argument type, to use this method as fallback if only strings are needed */
108     return comp != NULL ? comp->_val : NULL;
109 }
110
111 void extcap_free_tokenized_param(extcap_token_param *v) {
112     if (v != NULL)
113     {
114         g_free(v->arg);
115         g_free(v->value);
116     }
117
118     g_free(v);
119 }
120
121 void extcap_free_tokenized_sentence(extcap_token_sentence *s) {
122     extcap_token_param *tv;
123
124     if (s == NULL)
125         return;
126
127     if (s->sentence != NULL)
128         g_free(s->sentence);
129
130     while (s->param_list != NULL ) {
131         tv = s->param_list;
132         s->param_list = tv->next_token;
133
134         extcap_free_tokenized_param(tv);
135     }
136     g_free(s);
137 }
138
139 void extcap_free_tokenized_sentence_list(extcap_token_sentence *f) {
140     extcap_token_sentence *t;
141
142     while (f != NULL ) {
143         t = f->next_sentence;
144         extcap_free_tokenized_sentence(f);
145         f = t;
146     }
147 }
148
149 extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
150     extcap_token_param *tv = NULL;
151     GRegex * regex = NULL;
152     GMatchInfo * match_info = NULL;
153     GError * error = NULL;
154
155     extcap_token_sentence *rs = g_new(extcap_token_sentence, 1);
156
157     rs->sentence = NULL;
158     rs->next_sentence = NULL;
159     rs->param_list = NULL;
160
161     /* Regex for catching just the allowed values for sentences */
162     if ( ( regex = g_regex_new ( "^[\\t| ]*(arg|value|interface|extcap|dlt)(?=[\\t| ]+\\{)",
163             (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL ) ) != NULL ) {
164         g_regex_match ( regex, s, (GRegexMatchFlags) 0, &match_info );
165
166         if ( g_match_info_matches ( match_info ) )
167             rs->sentence = g_match_info_fetch(match_info, 0);
168
169         g_match_info_free ( match_info );
170         g_regex_unref ( regex );
171     }
172     /* No valid sentence found, exiting here */
173     if ( rs->sentence == NULL ) {
174         extcap_free_tokenized_sentence(rs);
175         return NULL;
176     }
177
178     /* Capture the argument and the value of the list. This will ensure,
179      * that regex patterns given to {validation=} are parsed correctly,
180      * as long as }{ does not occur within the pattern */
181     regex = g_regex_new ( "\\{([a-zA-Z_-]*?)\\=(.*?)\\}(?=\\{|$|\\s)",
182             (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL );
183     if ( regex != NULL ) {
184         g_regex_match_full(regex, s, -1, 0, (GRegexMatchFlags) 0, &match_info, &error );
185         while(g_match_info_matches(match_info)) {
186             gchar * arg = g_match_info_fetch ( match_info, 1 );
187
188             if ( arg == NULL )
189                 break;
190
191             tv = g_new(extcap_token_param, 1);
192             tv->arg = arg;
193             tv->value = g_match_info_fetch ( match_info, 2 );
194
195             if (g_ascii_strcasecmp(tv->arg, "number") == 0) {
196                 tv->param_type = EXTCAP_PARAM_ARGNUM;
197             } else if (g_ascii_strcasecmp(tv->arg, "call") == 0) {
198                 tv->param_type = EXTCAP_PARAM_CALL;
199             } else if (g_ascii_strcasecmp(tv->arg, "display") == 0) {
200                 tv->param_type = EXTCAP_PARAM_DISPLAY;
201             } else if (g_ascii_strcasecmp(tv->arg, "type") == 0) {
202                 tv->param_type = EXTCAP_PARAM_TYPE;
203             } else if (g_ascii_strcasecmp(tv->arg, "arg") == 0) {
204                 tv->param_type = EXTCAP_PARAM_ARG;
205             } else if (g_ascii_strcasecmp(tv->arg, "default") == 0) {
206                 tv->param_type = EXTCAP_PARAM_DEFAULT;
207             } else if (g_ascii_strcasecmp(tv->arg, "value") == 0) {
208                 tv->param_type = EXTCAP_PARAM_VALUE;
209             } else if (g_ascii_strcasecmp(tv->arg, "range") == 0) {
210                 tv->param_type = EXTCAP_PARAM_RANGE;
211             } else if (g_ascii_strcasecmp(tv->arg, "tooltip") == 0) {
212                 tv->param_type = EXTCAP_PARAM_TOOLTIP;
213             } else if (g_ascii_strcasecmp(tv->arg, "mustexist") == 0) {
214                 tv->param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
215             } else if (g_ascii_strcasecmp(tv->arg, "fileext") == 0) {
216                 tv->param_type = EXTCAP_PARAM_FILE_EXTENSION;
217             } else if (g_ascii_strcasecmp(tv->arg, "name") == 0) {
218                 tv->param_type = EXTCAP_PARAM_NAME;
219             } else if (g_ascii_strcasecmp(tv->arg, "enabled") == 0) {
220                 tv->param_type = EXTCAP_PARAM_ENABLED;
221             } else if (g_ascii_strcasecmp(tv->arg, "parent") == 0) {
222                 tv->param_type = EXTCAP_PARAM_PARENT;
223             } else if (g_ascii_strcasecmp(tv->arg, "required") == 0) {
224                 tv->param_type = EXTCAP_PARAM_REQUIRED;
225             } else if (g_ascii_strcasecmp(tv->arg, "save") == 0) {
226                 tv->param_type = EXTCAP_PARAM_SAVE;
227             } else if (g_ascii_strcasecmp(tv->arg, "validation") == 0) {
228                 tv->param_type = EXTCAP_PARAM_VALIDATION;
229             } else if (g_ascii_strcasecmp(tv->arg, "version") == 0) {
230                 tv->param_type = EXTCAP_PARAM_VERSION;
231             } else {
232                 tv->param_type = EXTCAP_PARAM_UNKNOWN;
233             }
234
235             tv->next_token = rs->param_list;
236             rs->param_list = tv;
237
238             g_match_info_next(match_info, &error);
239         }
240         g_match_info_free(match_info);
241         g_regex_unref(regex);
242     }
243
244     return rs;
245 }
246
247 extcap_token_sentence *extcap_tokenize_sentences(const gchar *s) {
248     extcap_token_sentence *first = NULL, *cur = NULL, *last = NULL;
249
250     gchar **list, **list_iter;
251
252     list_iter = list = g_strsplit(s, "\n", 0);
253
254     while (*list_iter != NULL ) {
255         cur = extcap_tokenize_sentence(*list_iter);
256
257         if (cur != NULL) {
258             if (first == NULL) {
259                 first = cur;
260                 last = cur;
261             } else {
262                 last->next_sentence = cur;
263                 last = cur;
264             }
265         }
266
267         list_iter++;
268     }
269
270     g_strfreev(list);
271
272     return first;
273 }
274
275 extcap_token_param *extcap_find_param_by_type(extcap_token_param *first,
276         extcap_param_type t) {
277     while (first != NULL ) {
278         if (first->param_type == t) {
279             return first;
280         }
281
282         first = first->next_token;
283     }
284
285     return NULL ;
286 }
287
288 void extcap_free_value(extcap_value *v) {
289     if (v == NULL)
290         return;
291
292     g_free(v->call);
293     g_free(v->display);
294
295     g_free(v);
296 }
297
298 extcap_interface *extcap_new_interface(void) {
299     extcap_interface *r = g_new(extcap_interface, 1);
300
301     r->call = r->display = r->version = NULL;
302     r->if_type = EXTCAP_SENTENCE_UNKNOWN;
303     r->next_interface = NULL;
304
305     return r;
306 }
307
308 void extcap_free_interface(extcap_interface *i) {
309     extcap_interface *next_i = i;
310
311     while (i) {
312         next_i = i->next_interface;
313         g_free(i->call);
314         g_free(i->display);
315         g_free(i->version);
316         g_free(i);
317         i = next_i;
318     }
319 }
320
321 extcap_dlt *extcap_new_dlt(void) {
322     extcap_dlt *r = g_new(extcap_dlt, 1);
323
324     r->number = -1;
325     r->name = r->display = NULL;
326     r->next_dlt = NULL;
327
328     return r;
329 }
330
331 void extcap_free_dlt(extcap_dlt *d) {
332     if (d == NULL)
333         return;
334
335     g_free(d->name);
336     g_free(d->display);
337 }
338
339 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
340     extcap_free_value((extcap_value *) data);
341 }
342
343 void extcap_free_arg(extcap_arg *a) {
344
345     if (a == NULL)
346         return;
347
348     g_free(a->call);
349     g_free(a->display);
350     g_free(a->tooltip);
351     g_free(a->fileextension);
352     g_free(a->regexp);
353     g_free(a->device_name);
354
355     if (a->range_start != NULL)
356         extcap_free_complex(a->range_start);
357
358     if (a->range_end != NULL)
359         extcap_free_complex(a->range_end);
360
361     if (a->default_complex != NULL)
362         extcap_free_complex(a->default_complex);
363
364     g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
365 }
366
367 static void extcap_free_arg_list_cb(gpointer listentry, gpointer data _U_) {
368     if (listentry != NULL)
369         extcap_free_arg((extcap_arg *) listentry);
370 }
371
372 void extcap_free_arg_list(GList *a) {
373     g_list_foreach(a, extcap_free_arg_list_cb, NULL);
374     g_list_free(a);
375 }
376
377 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
378     if (((const extcap_arg *) listelem)->arg_num == *((const int*) needle))
379         return 0;
380     return 1;
381 }
382
383 extcap_arg *extcap_parse_arg_sentence(GList * args, extcap_token_sentence *s) {
384     extcap_token_param *v = NULL;
385     extcap_arg *target_arg = NULL;
386     extcap_value *value = NULL;
387     GList * entry = NULL;
388     int tint;
389     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
390
391     if (s == NULL)
392         return target_arg;
393
394     if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
395         sent = EXTCAP_SENTENCE_ARG;
396         /* printf("ARG sentence\n"); */
397     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
398         sent = EXTCAP_SENTENCE_VALUE;
399         /* printf("VALUE sentence\n"); */
400     }
401
402     if (sent == EXTCAP_SENTENCE_ARG) {
403         target_arg = g_new0(extcap_arg, 1);
404         target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
405         target_arg->save = TRUE;
406
407         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
408                 == NULL) {
409             extcap_free_arg(target_arg);
410             return NULL ;
411         }
412
413         if (sscanf(v->value, "%d", &(target_arg->arg_num)) != 1) {
414             extcap_free_arg(target_arg);
415             return NULL ;
416         }
417
418         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_CALL))
419                 == NULL) {
420             extcap_free_arg(target_arg);
421             return NULL ;
422         }
423         target_arg->call = g_strdup(v->value);
424
425         /* No value only parameters allowed */
426         if (strlen(target_arg->call) == 0) {
427             extcap_free_arg(target_arg);
428             return NULL ;
429         }
430
431         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
432                 == NULL) {
433             extcap_free_arg(target_arg);
434             return NULL ;
435         }
436         target_arg->display = g_strdup(v->value);
437
438         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TOOLTIP))
439                 != NULL) {
440             target_arg->tooltip = g_strdup(v->value);
441         }
442
443         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_MUSTEXIST))
444                 != NULL) {
445             target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
446         }
447
448         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_FILE_EXTENSION))
449                 != NULL) {
450             target_arg->fileextension = g_strdup(v->value);
451         }
452
453         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALIDATION))
454                 != NULL) {
455             target_arg->regexp = g_strdup(v->value);
456         }
457
458         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_REQUIRED))
459                 != NULL) {
460             target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
461         }
462
463         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_TYPE))
464                 == NULL) {
465             /* printf("no type in ARG sentence\n"); */
466             extcap_free_arg(target_arg);
467             return NULL ;
468         }
469
470         if (g_ascii_strcasecmp(v->value, "integer") == 0) {
471             target_arg->arg_type = EXTCAP_ARG_INTEGER;
472         } else if (g_ascii_strcasecmp(v->value, "unsigned") == 0) {
473             target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
474         } else if (g_ascii_strcasecmp(v->value, "long") == 0) {
475             target_arg->arg_type = EXTCAP_ARG_LONG;
476         } else if (g_ascii_strcasecmp(v->value, "double") == 0) {
477             target_arg->arg_type = EXTCAP_ARG_DOUBLE;
478         } else if (g_ascii_strcasecmp(v->value, "boolean") == 0) {
479             target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
480         } else if (g_ascii_strcasecmp(v->value, "boolflag") == 0) {
481             target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
482         } else if (g_ascii_strcasecmp(v->value, "selector") == 0) {
483             target_arg->arg_type = EXTCAP_ARG_SELECTOR;
484         } else if (g_ascii_strcasecmp(v->value, "radio") == 0) {
485             target_arg->arg_type = EXTCAP_ARG_RADIO;
486         } else if (g_ascii_strcasecmp(v->value, "string") == 0) {
487             target_arg->arg_type = EXTCAP_ARG_STRING;
488         } else if (g_ascii_strcasecmp(v->value, "password") == 0) {
489             target_arg->arg_type = EXTCAP_ARG_PASSWORD;
490             /* default setting is to not save passwords */
491             target_arg->save = FALSE;
492         } else if (g_ascii_strcasecmp(v->value, "fileselect") == 0) {
493             target_arg->arg_type = EXTCAP_ARG_FILESELECT;
494         } else if (g_ascii_strcasecmp(v->value, "multicheck") == 0) {
495             target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
496         } else {
497             printf("invalid type %s in ARG sentence\n", v->value);
498             extcap_free_arg(target_arg);
499             return NULL ;
500         }
501
502         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_SAVE))
503                 != NULL) {
504             target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
505         }
506
507         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_RANGE))
508                 != NULL) {
509             gchar *cp = g_strstr_len(v->value, -1, ",");
510
511             if (cp == NULL) {
512                 printf("invalid range, expected value,value got %s\n",
513                         v->value);
514                 extcap_free_arg(target_arg);
515                 return NULL ;
516             }
517
518             if ((target_arg->range_start = extcap_parse_complex(
519                     target_arg->arg_type, v->value)) == NULL) {
520                 printf("invalid range, expected value,value got %s\n",
521                         v->value);
522                 extcap_free_arg(target_arg);
523                 return NULL ;
524             }
525
526             if ((target_arg->range_end = extcap_parse_complex(
527                     target_arg->arg_type, cp + 1)) == NULL) {
528                 printf("invalid range, expected value,value got %s\n",
529                         v->value);
530                 extcap_free_arg(target_arg);
531                 return NULL ;
532             }
533         }
534
535         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
536                 != NULL) {
537             if ( target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR )
538             {
539                 if ((target_arg->default_complex = extcap_parse_complex(
540                         target_arg->arg_type, v->value)) == NULL) {
541                     printf("invalid default, couldn't parse %s\n", v->value);
542                 }
543             }
544         }
545
546     } else if (sent == EXTCAP_SENTENCE_VALUE) {
547         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARG))
548                 == NULL) {
549             printf("no arg in VALUE sentence\n");
550             return NULL ;
551         }
552
553         if (sscanf(v->value, "%d", &tint) != 1) {
554             printf("invalid arg in VALUE sentence\n");
555             return NULL ;
556         }
557
558         ;
559         if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
560                 == NULL) {
561             printf("couldn't find arg %d in list for VALUE sentence\n", tint);
562             return NULL ;
563         }
564
565         value = g_new0(extcap_value, 1);
566         value->arg_num = tint;
567
568         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
569                 == NULL) {
570             /* printf("no value in VALUE sentence\n"); */
571             extcap_free_value(value);
572             return NULL ;
573         }
574         value->call = g_strdup(v->value);
575
576         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
577                 == NULL) {
578             /* printf("no display in VALUE sentence\n"); */
579             extcap_free_value(value);
580             return NULL ;
581         }
582         value->display = g_strdup(v->value);
583
584         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_PARENT))
585                 != NULL) {
586             value->parent = g_strdup(v->value);
587         }
588
589         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DEFAULT))
590                 != NULL) {
591             /* printf("found default value\n"); */
592             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
593         }
594
595         if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ENABLED))
596                 != NULL) {
597             value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, v->value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
598         }
599
600         ((extcap_arg*) entry->data)->values = g_list_append(
601                 ((extcap_arg*) entry->data)->values, value);
602
603         return NULL ;
604     }
605
606     return target_arg;
607 }
608
609 GList * extcap_parse_args(extcap_token_sentence *first_s) {
610     GList * args = NULL;
611
612     while (first_s) {
613         extcap_arg *ra = NULL;
614
615         if ((ra = extcap_parse_arg_sentence(args, first_s)) != NULL)
616             args = g_list_append(args, (gpointer) ra);
617
618         first_s = first_s->next_sentence;
619     }
620
621     return args;
622 }
623
624 int extcap_parse_interface_sentence(extcap_token_sentence *s,
625         extcap_interface **ri) {
626     extcap_token_param *v = NULL;
627     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
628
629     *ri = NULL;
630
631     if (s == NULL)
632         return -1;
633
634     if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
635         sent = EXTCAP_SENTENCE_INTERFACE;
636     } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
637         sent = EXTCAP_SENTENCE_EXTCAP;
638     }
639
640     if (sent == EXTCAP_SENTENCE_UNKNOWN)
641         return -1;
642
643     *ri = extcap_new_interface();
644
645     (*ri)->if_type = sent;
646
647     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VALUE))
648             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
649         printf("No value in INTERFACE sentence\n");
650         extcap_free_interface(*ri);
651         return -1;
652     }
653     if ( v != NULL )
654        (*ri)->call = g_strdup(v->value);
655
656     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
657             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
658         printf("No display in INTERFACE sentence\n");
659         extcap_free_interface(*ri);
660         return -1;
661     }
662     if ( v != NULL )
663         (*ri)->display = g_strdup(v->value);
664
665     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_VERSION))
666             != NULL) {
667         (*ri)->version = g_strdup(v->value);
668     }
669
670     return 1;
671 }
672
673 int extcap_parse_interfaces(extcap_token_sentence *first_s,
674         extcap_interface **first_int) {
675     extcap_interface *first_i = NULL, *last_i = NULL;
676
677     while (first_s) {
678         extcap_interface *ri;
679
680         if (extcap_parse_interface_sentence(first_s, &ri) >= 0 && ri != NULL) {
681             if (first_i == NULL) {
682                 first_i = last_i = ri;
683             } else {
684                 last_i->next_interface = ri;
685                 last_i = ri;
686             }
687         }
688
689         first_s = first_s->next_sentence;
690     }
691
692     *first_int = first_i;
693
694     return 1;
695 }
696
697 int extcap_parse_dlt_sentence(extcap_token_sentence *s, extcap_dlt **rd) {
698     extcap_token_param *v = NULL;
699     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
700
701     *rd = NULL;
702
703     if (s == NULL)
704         return -1;
705
706     if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
707         sent = EXTCAP_SENTENCE_DLT;
708     }
709
710     if (sent == EXTCAP_SENTENCE_UNKNOWN)
711         return -1;
712
713     *rd = extcap_new_dlt();
714
715     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_ARGNUM))
716             == NULL) {
717         printf("No number in DLT sentence\n");
718         extcap_free_dlt(*rd);
719         return -1;
720     }
721     if (sscanf(v->value, "%d", &((*rd)->number)) != 1) {
722         printf("Invalid number in DLT sentence\n");
723         extcap_free_dlt(*rd);
724         return -1;
725     }
726
727     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_NAME))
728             == NULL) {
729         printf("No name in DLT sentence\n");
730         extcap_free_dlt(*rd);
731         return -1;
732     }
733     (*rd)->name = g_strdup(v->value);
734
735     if ((v = extcap_find_param_by_type(s->param_list, EXTCAP_PARAM_DISPLAY))
736             == NULL) {
737         printf("No display in DLT sentence\n");
738         extcap_free_dlt(*rd);
739         return -1;
740     }
741     (*rd)->display = g_strdup(v->value);
742
743     return 1;
744 }
745
746 int extcap_parse_dlts(extcap_token_sentence *first_s, extcap_dlt **first_dlt) {
747     extcap_dlt *first_d = NULL, *last_d = NULL;
748
749     while (first_s) {
750         extcap_dlt *rd;
751
752         if (extcap_parse_dlt_sentence(first_s, &rd) >= 0 && rd != NULL) {
753             if (first_d == NULL) {
754                 first_d = last_d = rd;
755             } else {
756                 last_d->next_dlt = rd;
757                 last_d = rd;
758             }
759         }
760
761         first_s = first_s->next_sentence;
762     }
763
764     *first_dlt = first_d;
765
766     return 1;
767 }
768
769 /*
770  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
771  *
772  * Local variables:
773  * c-basic-offset: 4
774  * tab-width: 8
775  * indent-tabs-mode: nil
776  * End:
777  *
778  * vi: set shiftwidth=4 tabstop=8 expandtab:
779  * :indentSize=4:tabSize=8:noTabs=true:
780  */