Qt: Handle retranslation events in the main window.
[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 {
412             printf("invalid type %s in ARG sentence\n", param_value);
413             extcap_free_arg(target_arg);
414             return NULL ;
415         }
416
417         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_SAVE)))
418                 != NULL) {
419             target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
420         }
421
422         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_RANGE)))
423                 != NULL) {
424             gchar *cp = g_strstr_len(param_value, -1, ",");
425
426             if (cp == NULL) {
427                 printf("invalid range, expected value,value got %s\n",
428                         param_value);
429                 extcap_free_arg(target_arg);
430                 return NULL ;
431             }
432
433             if ((target_arg->range_start = extcap_parse_complex(
434                     target_arg->arg_type, param_value)) == NULL) {
435                 printf("invalid range, expected value,value got %s\n",
436                         param_value);
437                 extcap_free_arg(target_arg);
438                 return NULL ;
439             }
440
441             if ((target_arg->range_end = extcap_parse_complex(
442                     target_arg->arg_type, cp + 1)) == NULL) {
443                 printf("invalid range, expected value,value got %s\n",
444                         param_value);
445                 extcap_free_arg(target_arg);
446                 return NULL ;
447             }
448         }
449
450         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
451                 != NULL) {
452             if ( target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR )
453             {
454                 if ((target_arg->default_complex = extcap_parse_complex(
455                         target_arg->arg_type, param_value)) == NULL) {
456                     printf("invalid default, couldn't parse %s\n", param_value);
457                 }
458             }
459         }
460
461     } else if (sent == EXTCAP_SENTENCE_VALUE) {
462         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARG)))
463                 == NULL) {
464             printf("no arg in VALUE sentence\n");
465             return NULL ;
466         }
467
468         if (sscanf(param_value, "%d", &tint) != 1) {
469             printf("invalid arg in VALUE sentence\n");
470             return NULL ;
471         }
472
473         ;
474         if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
475                 == NULL) {
476             printf("couldn't find arg %d in list for VALUE sentence\n", tint);
477             return NULL ;
478         }
479
480         value = g_new0(extcap_value, 1);
481         value->arg_num = tint;
482
483         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
484                 == NULL) {
485             /* printf("no value in VALUE sentence\n"); */
486             extcap_free_value(value);
487             return NULL ;
488         }
489         value->call = g_strdup(param_value);
490
491         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
492                 == NULL) {
493             /* printf("no display in VALUE sentence\n"); */
494             extcap_free_value(value);
495             return NULL ;
496         }
497         value->display = g_strdup(param_value);
498
499         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PARENT)))
500                 != NULL) {
501             value->parent = g_strdup(param_value);
502         }
503
504         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
505                 != NULL) {
506             /* printf("found default value\n"); */
507             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
508         }
509
510         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ENABLED)))
511                 != NULL) {
512             value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0 );
513         }
514
515         ((extcap_arg*) entry->data)->values = g_list_append(
516                 ((extcap_arg*) entry->data)->values, value);
517
518         return NULL ;
519     }
520
521     return target_arg;
522 }
523
524 GList * extcap_parse_args(gchar *output) {
525     GList * result = NULL;
526     GList * walker = NULL;
527     GList * temp = NULL;
528
529     walker = extcap_tokenize_sentences(output);
530     temp = walker;
531
532     while (walker) {
533         extcap_arg *ra = NULL;
534         extcap_token_sentence * sentence = (extcap_token_sentence *)walker->data;
535
536         if ((ra = extcap_parse_arg_sentence(result, sentence)) != NULL)
537             result = g_list_append(result, (gpointer) ra);
538
539         walker = g_list_next(walker);
540     }
541
542     extcap_free_tokenized_sentences(temp);
543
544     return result;
545 }
546
547 static extcap_interface * extcap_parse_interface_sentence(extcap_token_sentence *s) {
548     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
549     gchar * param_value = NULL;
550     extcap_interface * ri = NULL;
551
552     if (s == NULL)
553         return NULL;
554
555     if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
556         sent = EXTCAP_SENTENCE_INTERFACE;
557     } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
558         sent = EXTCAP_SENTENCE_EXTCAP;
559     }
560
561     if (sent == EXTCAP_SENTENCE_UNKNOWN)
562         return NULL;
563
564     ri = g_new0(extcap_interface, 1);
565
566     ri->if_type = sent;
567
568     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
569             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
570         printf("No value in INTERFACE sentence\n");
571         g_free(ri);
572         return NULL;
573     }
574     ri->call = g_strdup(param_value);
575
576     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
577             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
578         printf("No display in INTERFACE sentence\n");
579         g_free(ri->call);
580         g_free(ri);
581         return NULL;
582     }
583     ri->display = g_strdup(param_value);
584
585     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VERSION)))
586             != NULL) {
587         ri->version = g_strdup(param_value);
588     }
589
590     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_HELP)))
591             != NULL) {
592         ri->help = g_strdup(param_value);
593     }
594
595     return ri;
596 }
597
598 GList * extcap_parse_interfaces(gchar *output) {
599
600     GList * result = NULL;
601     GList * tokens = NULL;
602     GList * walker = extcap_tokenize_sentences(output);
603     tokens = walker;
604
605     while (walker) {
606         extcap_interface * ri = NULL;
607         extcap_token_sentence * if_sentence = (extcap_token_sentence *) walker->data;
608
609         if ( if_sentence != NULL && ( ri = extcap_parse_interface_sentence ( if_sentence ) ) != NULL )
610             result = g_list_append(result, ri);
611
612         walker = g_list_next(walker);
613     }
614
615     extcap_free_tokenized_sentences(tokens);
616
617     return result;
618 }
619
620 /* Parse a tokenized set of sentences and validate, looking for DLT definitions */
621 static extcap_dlt * extcap_parse_dlt_sentence(extcap_token_sentence *s) {
622     gchar *param_value = NULL;
623     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
624     extcap_dlt * result = NULL;
625
626     if (s == NULL)
627         return result;
628
629     if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
630         sent = EXTCAP_SENTENCE_DLT;
631     }
632
633     if (sent == EXTCAP_SENTENCE_UNKNOWN)
634         return result;
635
636     result = g_new0(extcap_dlt, 1);
637
638     result->number = -1;
639     result->name = NULL;
640     result->display = NULL;
641
642     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM)))
643             == NULL) {
644         printf("No number in DLT sentence\n");
645         g_free(result);
646         return NULL;
647     }
648     if (sscanf(param_value, "%d", &(result->number)) != 1) {
649         printf("Invalid number in DLT sentence\n");
650         g_free(result);
651         return NULL;
652     }
653
654     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_NAME)))
655             == NULL) {
656         printf("No name in DLT sentence\n");
657         g_free(result);
658         return NULL;
659     }
660     result->name = g_strdup(param_value);
661
662     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
663             == NULL) {
664         printf("No display in DLT sentence\n");
665         g_free(result->name);
666         g_free(result);
667         return NULL;
668     }
669     result->display = g_strdup(param_value);
670
671     return result;
672 }
673
674 GList * extcap_parse_dlts(gchar *output) {
675
676     GList * walker = NULL;
677     GList * temp = NULL;
678     GList * result = NULL;
679
680     walker = extcap_tokenize_sentences(output);
681
682     temp = walker;
683
684     while (walker) {
685         extcap_dlt *data = NULL;
686
687         if ((data = extcap_parse_dlt_sentence((extcap_token_sentence *)walker->data)) != NULL)
688             result = g_list_append(result, data);
689
690         walker = g_list_next(walker);
691     }
692
693     extcap_free_tokenized_sentences(temp);
694
695     return result;
696 }
697
698 /*
699  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
700  *
701  * Local variables:
702  * c-basic-offset: 4
703  * tab-width: 8
704  * indent-tabs-mode: nil
705  * End:
706  *
707  * vi: set shiftwidth=4 tabstop=8 expandtab:
708  * :indentSize=4:tabSize=8:noTabs=true:
709  */