[Automatic update for 2017-01-22]
[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 static extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
112     GRegex *regex = NULL;
113     GMatchInfo *match_info = NULL;
114     GError *error = NULL;
115     gchar *param_value = NULL;
116     guint param_type = EXTCAP_PARAM_UNKNOWN;
117
118     extcap_token_sentence *rs = g_new0(extcap_token_sentence, 1);
119
120     rs->sentence = NULL;
121
122     /* Regex for catching just the allowed values for sentences */
123     if ((regex = g_regex_new("^[\\t| ]*(arg|value|interface|extcap|dlt)(?=[\\t| ]+\\{)",
124                              (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL)) != NULL) {
125         g_regex_match(regex, s, (GRegexMatchFlags) 0, &match_info);
126
127         if (g_match_info_matches(match_info))
128             rs->sentence = g_match_info_fetch(match_info, 0);
129
130         g_match_info_free(match_info);
131         g_regex_unref(regex);
132     }
133     /* No valid sentence found, exiting here */
134     if (rs->sentence == NULL) {
135         g_free(rs);
136         return NULL;
137     }
138
139     rs->param_list = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
140
141     /* Capture the argument and the value of the list. This will ensure,
142      * that regex patterns given to {validation=} are parsed correctly,
143      * as long as }{ does not occur within the pattern */
144     regex = g_regex_new("\\{([a-zA-Z_-]*?)\\=(.*?)\\}(?=\\{|$|\\s)",
145                         (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL);
146     if (regex != NULL) {
147         g_regex_match_full(regex, s, -1, 0, (GRegexMatchFlags) 0, &match_info, &error);
148         while (g_match_info_matches(match_info)) {
149             gchar *arg = g_match_info_fetch(match_info, 1);
150
151             if (arg == NULL)
152                 break;
153
154             param_value = g_match_info_fetch(match_info, 2);
155
156             if (g_ascii_strcasecmp(arg, "number") == 0) {
157                 param_type = EXTCAP_PARAM_ARGNUM;
158             } else if (g_ascii_strcasecmp(arg, "call") == 0) {
159                 param_type = EXTCAP_PARAM_CALL;
160             } else if (g_ascii_strcasecmp(arg, "display") == 0) {
161                 param_type = EXTCAP_PARAM_DISPLAY;
162             } else if (g_ascii_strcasecmp(arg, "type") == 0) {
163                 param_type = EXTCAP_PARAM_TYPE;
164             } else if (g_ascii_strcasecmp(arg, "arg") == 0) {
165                 param_type = EXTCAP_PARAM_ARG;
166             } else if (g_ascii_strcasecmp(arg, "default") == 0) {
167                 param_type = EXTCAP_PARAM_DEFAULT;
168             } else if (g_ascii_strcasecmp(arg, "value") == 0) {
169                 param_type = EXTCAP_PARAM_VALUE;
170             } else if (g_ascii_strcasecmp(arg, "range") == 0) {
171                 param_type = EXTCAP_PARAM_RANGE;
172             } else if (g_ascii_strcasecmp(arg, "tooltip") == 0) {
173                 param_type = EXTCAP_PARAM_TOOLTIP;
174             } else if (g_ascii_strcasecmp(arg, "mustexist") == 0) {
175                 param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
176             } else if (g_ascii_strcasecmp(arg, "fileext") == 0) {
177                 param_type = EXTCAP_PARAM_FILE_EXTENSION;
178             } else if (g_ascii_strcasecmp(arg, "name") == 0) {
179                 param_type = EXTCAP_PARAM_NAME;
180             } else if (g_ascii_strcasecmp(arg, "enabled") == 0) {
181                 param_type = EXTCAP_PARAM_ENABLED;
182             } else if (g_ascii_strcasecmp(arg, "parent") == 0) {
183                 param_type = EXTCAP_PARAM_PARENT;
184             } else if (g_ascii_strcasecmp(arg, "required") == 0) {
185                 param_type = EXTCAP_PARAM_REQUIRED;
186             } else if (g_ascii_strcasecmp(arg, "save") == 0) {
187                 param_type = EXTCAP_PARAM_SAVE;
188             } else if (g_ascii_strcasecmp(arg, "validation") == 0) {
189                 param_type = EXTCAP_PARAM_VALIDATION;
190             } else if (g_ascii_strcasecmp(arg, "version") == 0) {
191                 param_type = EXTCAP_PARAM_VERSION;
192             } else if (g_ascii_strcasecmp(arg, "help") == 0) {
193                 param_type = EXTCAP_PARAM_HELP;
194             } else {
195                 param_type = EXTCAP_PARAM_UNKNOWN;
196             }
197
198             g_hash_table_insert(rs->param_list, ENUM_KEY(param_type), param_value);
199
200             g_match_info_next(match_info, &error);
201             g_free(arg);
202         }
203         g_match_info_free(match_info);
204         g_regex_unref(regex);
205     }
206
207     return rs;
208 }
209
210 static GList *extcap_tokenize_sentences(const gchar *s) {
211
212     GList *sentences = NULL;
213     extcap_token_sentence *item = NULL;
214     gchar **list, **list_iter;
215
216     list_iter = list = g_strsplit(s, "\n", 0);
217     while (*list_iter != NULL) {
218         item = extcap_tokenize_sentence(*list_iter);
219         if (item)
220             sentences = g_list_append(sentences, item);
221         list_iter++;
222     }
223
224     g_strfreev(list);
225
226     return sentences;
227 }
228
229 static void extcap_free_value(extcap_value *v) {
230     if (v == NULL)
231         return;
232
233     g_free(v->call);
234     g_free(v->display);
235
236     g_free(v);
237 }
238
239 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
240     extcap_free_value((extcap_value *) data);
241 }
242
243 void extcap_free_arg(extcap_arg *a) {
244
245     if (a == NULL)
246         return;
247
248     g_free(a->call);
249     g_free(a->display);
250     g_free(a->tooltip);
251     g_free(a->fileextension);
252     g_free(a->regexp);
253     g_free(a->device_name);
254
255     if (a->range_start != NULL)
256         extcap_free_complex(a->range_start);
257
258     if (a->range_end != NULL)
259         extcap_free_complex(a->range_end);
260
261     if (a->default_complex != NULL)
262         extcap_free_complex(a->default_complex);
263
264     g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
265     g_list_free(a->values);
266     g_free(a);
267 }
268
269 void extcap_free_arg_list(GList *a) {
270     g_list_foreach(a, (GFunc)extcap_free_arg, NULL);
271     g_list_free(a);
272 }
273
274 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
275     if (((const extcap_arg *) listelem)->arg_num == *((const int *) needle))
276         return 0;
277     return 1;
278 }
279
280 static void extcap_free_tokenized_sentence(gpointer s, gpointer user_data _U_) {
281     extcap_token_sentence *t = (extcap_token_sentence *)s;
282
283     if (t == NULL)
284         return;
285
286     g_free(t->sentence);
287     g_hash_table_destroy(t->param_list);
288     g_free(t);
289 }
290
291 static void extcap_free_tokenized_sentences(GList *sentences) {
292     if (sentences == NULL)
293         return;
294
295     g_list_foreach(sentences, extcap_free_tokenized_sentence, NULL);
296     g_list_free(sentences);
297 }
298
299 static extcap_arg *extcap_parse_arg_sentence(GList *args, extcap_token_sentence *s) {
300     gchar *param_value = NULL;
301
302     extcap_arg *target_arg = NULL;
303     extcap_value *value = NULL;
304     GList *entry = NULL;
305     int tint;
306     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
307
308     if (s == NULL)
309         return target_arg;
310
311     if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
312         sent = EXTCAP_SENTENCE_ARG;
313         /* printf("ARG sentence\n"); */
314     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
315         sent = EXTCAP_SENTENCE_VALUE;
316         /* printf("VALUE sentence\n"); */
317     }
318
319     if (sent == EXTCAP_SENTENCE_ARG) {
320         target_arg = g_new0(extcap_arg, 1);
321         target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
322         target_arg->save = TRUE;
323
324
325         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM))) == NULL) {
326             extcap_free_arg(target_arg);
327             return NULL;
328         }
329
330         if (sscanf(param_value, "%d", &(target_arg->arg_num)) != 1) {
331             extcap_free_arg(target_arg);
332             return NULL;
333         }
334
335         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CALL))) == NULL) {
336             extcap_free_arg(target_arg);
337             return NULL;
338         }
339         target_arg->call = g_strdup(param_value);
340
341         /* No value only parameters allowed */
342         if (strlen(target_arg->call) == 0) {
343             extcap_free_arg(target_arg);
344             return NULL;
345         }
346
347         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY))) == NULL) {
348             extcap_free_arg(target_arg);
349             return NULL;
350         }
351         target_arg->display = g_strdup(param_value);
352
353         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP)))
354                 != NULL) {
355             target_arg->tooltip = g_strdup(param_value);
356         }
357
358         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_MUSTEXIST)))
359                 != NULL) {
360             target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
361         }
362
363         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_EXTENSION)))
364                 != NULL) {
365             target_arg->fileextension = g_strdup(param_value);
366         }
367
368         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
369                 != NULL) {
370             target_arg->regexp = g_strdup(param_value);
371         }
372
373         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
374                 != NULL) {
375             target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
376         }
377
378         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE)))
379                 == NULL) {
380             /* printf("no type in ARG sentence\n"); */
381             extcap_free_arg(target_arg);
382             return NULL;
383         }
384
385         if (g_ascii_strcasecmp(param_value, "integer") == 0) {
386             target_arg->arg_type = EXTCAP_ARG_INTEGER;
387         } else if (g_ascii_strcasecmp(param_value, "unsigned") == 0) {
388             target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
389         } else if (g_ascii_strcasecmp(param_value, "long") == 0) {
390             target_arg->arg_type = EXTCAP_ARG_LONG;
391         } else if (g_ascii_strcasecmp(param_value, "double") == 0) {
392             target_arg->arg_type = EXTCAP_ARG_DOUBLE;
393         } else if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
394             target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
395         } else if (g_ascii_strcasecmp(param_value, "boolflag") == 0) {
396             target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
397         } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
398             target_arg->arg_type = EXTCAP_ARG_SELECTOR;
399         } else if (g_ascii_strcasecmp(param_value, "radio") == 0) {
400             target_arg->arg_type = EXTCAP_ARG_RADIO;
401         } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
402             target_arg->arg_type = EXTCAP_ARG_STRING;
403         } else if (g_ascii_strcasecmp(param_value, "password") == 0) {
404             target_arg->arg_type = EXTCAP_ARG_PASSWORD;
405             /* default setting is to not save passwords */
406             target_arg->save = FALSE;
407         } else if (g_ascii_strcasecmp(param_value, "fileselect") == 0) {
408             target_arg->arg_type = EXTCAP_ARG_FILESELECT;
409         } else if (g_ascii_strcasecmp(param_value, "multicheck") == 0) {
410             target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
411         } else if (g_ascii_strcasecmp(param_value, "timestamp") == 0) {
412             target_arg->arg_type = EXTCAP_ARG_TIMESTAMP;
413         } else {
414             printf("invalid type %s in ARG sentence\n", param_value);
415             extcap_free_arg(target_arg);
416             return NULL;
417         }
418
419         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_SAVE)))
420                 != NULL) {
421             target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
422         }
423
424         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_RANGE)))
425                 != NULL) {
426             gchar *cp = g_strstr_len(param_value, -1, ",");
427
428             if (cp == NULL) {
429                 printf("invalid range, expected value,value got %s\n",
430                        param_value);
431                 extcap_free_arg(target_arg);
432                 return NULL;
433             }
434
435             if ((target_arg->range_start = extcap_parse_complex(
436                                                target_arg->arg_type, param_value)) == NULL) {
437                 printf("invalid range, expected value,value got %s\n",
438                        param_value);
439                 extcap_free_arg(target_arg);
440                 return NULL;
441             }
442
443             if ((target_arg->range_end = extcap_parse_complex(
444                                              target_arg->arg_type, cp + 1)) == NULL) {
445                 printf("invalid range, expected value,value got %s\n",
446                        param_value);
447                 extcap_free_arg(target_arg);
448                 return NULL;
449             }
450         }
451
452         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
453                 != NULL) {
454             if (target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR)
455             {
456                 if ((target_arg->default_complex = extcap_parse_complex(
457                                                        target_arg->arg_type, param_value)) == NULL) {
458                     printf("invalid default, couldn't parse %s\n", param_value);
459                 }
460             }
461         }
462
463     } else if (sent == EXTCAP_SENTENCE_VALUE) {
464         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARG)))
465                 == NULL) {
466             printf("no arg in VALUE sentence\n");
467             return NULL;
468         }
469
470         if (sscanf(param_value, "%d", &tint) != 1) {
471             printf("invalid arg in VALUE sentence\n");
472             return NULL;
473         }
474
475         if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
476                 == NULL) {
477             printf("couldn't find arg %d in list for VALUE sentence\n", tint);
478             return NULL;
479         }
480
481         value = g_new0(extcap_value, 1);
482         value->arg_num = tint;
483
484         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
485                 == NULL) {
486             /* printf("no value in VALUE sentence\n"); */
487             extcap_free_value(value);
488             return NULL;
489         }
490         value->call = g_strdup(param_value);
491
492         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
493                 == NULL) {
494             /* printf("no display in VALUE sentence\n"); */
495             extcap_free_value(value);
496             return NULL;
497         }
498         value->display = g_strdup(param_value);
499
500         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PARENT)))
501                 != NULL) {
502             value->parent = g_strdup(param_value);
503         }
504
505         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
506                 != NULL) {
507             /* printf("found default value\n"); */
508             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
509         }
510
511         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ENABLED)))
512                 != NULL) {
513             value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
514         }
515
516         ((extcap_arg *) entry->data)->values = g_list_append(
517                 ((extcap_arg *) entry->data)->values, value);
518
519         return NULL;
520     }
521
522     return target_arg;
523 }
524
525 GList *extcap_parse_args(gchar *output) {
526     GList *result = NULL;
527     GList *walker = NULL;
528     GList *temp = NULL;
529
530     walker = extcap_tokenize_sentences(output);
531     temp = walker;
532
533     while (walker) {
534         extcap_arg *ra = NULL;
535         extcap_token_sentence *sentence = (extcap_token_sentence *)walker->data;
536
537         if ((ra = extcap_parse_arg_sentence(result, sentence)) != NULL)
538             result = g_list_append(result, (gpointer) ra);
539
540         walker = g_list_next(walker);
541     }
542
543     extcap_free_tokenized_sentences(temp);
544
545     return result;
546 }
547
548 static extcap_interface *extcap_parse_interface_sentence(extcap_token_sentence *s) {
549     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
550     gchar *param_value = NULL;
551     extcap_interface *ri = NULL;
552
553     if (s == NULL)
554         return NULL;
555
556     if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
557         sent = EXTCAP_SENTENCE_INTERFACE;
558     } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
559         sent = EXTCAP_SENTENCE_EXTCAP;
560     }
561
562     if (sent == EXTCAP_SENTENCE_UNKNOWN)
563         return NULL;
564
565     ri = g_new0(extcap_interface, 1);
566
567     ri->if_type = sent;
568
569     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
570             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
571         printf("No value in INTERFACE sentence\n");
572         g_free(ri);
573         return NULL;
574     }
575     ri->call = g_strdup(param_value);
576
577     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
578             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
579         printf("No display in INTERFACE sentence\n");
580         g_free(ri->call);
581         g_free(ri);
582         return NULL;
583     }
584     ri->display = g_strdup(param_value);
585
586     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VERSION)))
587             != NULL) {
588         ri->version = g_strdup(param_value);
589     }
590
591     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_HELP)))
592             != NULL) {
593         ri->help = g_strdup(param_value);
594     }
595
596     return ri;
597 }
598
599 GList *extcap_parse_interfaces(gchar *output) {
600
601     GList *result = NULL;
602     GList *tokens = NULL;
603     GList *walker = extcap_tokenize_sentences(output);
604     tokens = walker;
605
606     while (walker) {
607         extcap_interface *ri = NULL;
608         extcap_token_sentence *if_sentence = (extcap_token_sentence *) walker->data;
609
610         if (if_sentence != NULL && (ri = extcap_parse_interface_sentence(if_sentence)) != NULL)
611             result = g_list_append(result, ri);
612
613         walker = g_list_next(walker);
614     }
615
616     extcap_free_tokenized_sentences(tokens);
617
618     return result;
619 }
620
621 /* Parse a tokenized set of sentences and validate, looking for DLT definitions */
622 static extcap_dlt *extcap_parse_dlt_sentence(extcap_token_sentence *s) {
623     gchar *param_value = NULL;
624     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
625     extcap_dlt *result = NULL;
626
627     if (s == NULL)
628         return result;
629
630     if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
631         sent = EXTCAP_SENTENCE_DLT;
632     }
633
634     if (sent == EXTCAP_SENTENCE_UNKNOWN)
635         return result;
636
637     result = g_new0(extcap_dlt, 1);
638
639     result->number = -1;
640     result->name = NULL;
641     result->display = NULL;
642
643     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM)))
644             == NULL) {
645         printf("No number in DLT sentence\n");
646         g_free(result);
647         return NULL;
648     }
649     if (sscanf(param_value, "%d", &(result->number)) != 1) {
650         printf("Invalid number in DLT sentence\n");
651         g_free(result);
652         return NULL;
653     }
654
655     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_NAME)))
656             == NULL) {
657         printf("No name in DLT sentence\n");
658         g_free(result);
659         return NULL;
660     }
661     result->name = g_strdup(param_value);
662
663     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
664             == NULL) {
665         printf("No display in DLT sentence\n");
666         g_free(result->name);
667         g_free(result);
668         return NULL;
669     }
670     result->display = g_strdup(param_value);
671
672     return result;
673 }
674
675 GList *extcap_parse_dlts(gchar *output) {
676
677     GList *walker = NULL;
678     GList *temp = NULL;
679     GList *result = NULL;
680
681     walker = extcap_tokenize_sentences(output);
682
683     temp = walker;
684
685     while (walker) {
686         extcap_dlt *data = NULL;
687
688         if ((data = extcap_parse_dlt_sentence((extcap_token_sentence *)walker->data)) != NULL)
689             result = g_list_append(result, data);
690
691         walker = g_list_next(walker);
692     }
693
694     extcap_free_tokenized_sentences(temp);
695
696     return result;
697 }
698
699 /*
700  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
701  *
702  * Local variables:
703  * c-basic-offset: 4
704  * tab-width: 8
705  * indent-tabs-mode: nil
706  * End:
707  *
708  * vi: set shiftwidth=4 tabstop=8 expandtab:
709  * :indentSize=4:tabSize=8:noTabs=true:
710  */