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