CMake RPM build: Try to get rid of %globals in favor of %bcond.
[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  * SPDX-License-Identifier: GPL-2.0-or-later
11  */
12
13 #include <config.h>
14
15 #include <stdio.h>
16 #include <glib.h>
17 #include <string.h>
18
19 #include "ui/iface_toolbar.h"
20 #include "wsutil/strtoi.h"
21
22 #include "extcap.h"
23 #include "extcap_parser.h"
24 #include "ws_attributes.h"
25
26 void extcap_printf_complex(extcap_complex *comp) {
27     gchar *ret = extcap_get_complex_as_string(comp);
28     printf("%s", ret);
29     g_free(ret);
30 }
31
32 gchar *extcap_get_complex_as_string(extcap_complex *comp) {
33     return (comp ? g_strdup(comp->_val) : NULL);
34 }
35
36 extcap_complex *extcap_parse_complex(extcap_arg_type complex_type,
37                                      const gchar *data) {
38
39     extcap_complex *rc = g_new0(extcap_complex, 1);
40
41     rc->_val = g_strdup(data);
42     rc->complex_type = complex_type;
43
44     return rc;
45 }
46
47 gboolean extcap_compare_is_default(extcap_arg *element, extcap_complex *test) {
48     if (element == NULL || element->default_complex == NULL || test == NULL)
49         return FALSE;
50
51     if (g_strcmp0(element->default_complex->_val, test->_val) == 0)
52         return TRUE;
53
54     return FALSE;
55 }
56
57 void extcap_free_complex(extcap_complex *comp) {
58     if (comp)
59         g_free(comp->_val);
60     g_free(comp);
61 }
62
63 gint extcap_complex_get_int(extcap_complex *comp) {
64     if (comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_INTEGER)
65         return (gint)0;
66
67     return (gint) g_ascii_strtoll(comp->_val, NULL, 10);
68 }
69
70 guint extcap_complex_get_uint(extcap_complex *comp) {
71     if (comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_UNSIGNED)
72         return (guint)0;
73     return (guint) g_ascii_strtoull(comp->_val, NULL, 10);
74 }
75
76 gint64 extcap_complex_get_long(extcap_complex *comp) {
77     if (comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_LONG)
78         return (gint64)0;
79     return g_ascii_strtoll(comp->_val, NULL, 10);
80 }
81
82 gdouble extcap_complex_get_double(extcap_complex *comp) {
83     if (comp == NULL || comp->_val == NULL || comp->complex_type != EXTCAP_ARG_DOUBLE)
84         return (gdouble)0;
85     return g_strtod(comp->_val, NULL);
86 }
87
88 gboolean extcap_complex_get_bool(extcap_complex *comp) {
89     if (comp == NULL || comp->_val == NULL)
90         return FALSE;
91
92     if (comp->complex_type != EXTCAP_ARG_BOOLEAN && comp->complex_type != EXTCAP_ARG_BOOLFLAG)
93         return FALSE;
94
95     return g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, comp->_val, G_REGEX_CASELESS, (GRegexMatchFlags)0);
96 }
97
98 gchar *extcap_complex_get_string(extcap_complex *comp) {
99     /* Not checking for argument type, to use this method as fallback if only strings are needed */
100     return comp != NULL ? comp->_val : NULL;
101 }
102
103 static extcap_token_sentence *extcap_tokenize_sentence(const gchar *s) {
104     GRegex *regex = NULL;
105     GMatchInfo *match_info = NULL;
106     GError *error = NULL;
107     gchar *param_value = NULL;
108     guint param_type = EXTCAP_PARAM_UNKNOWN;
109
110     extcap_token_sentence *rs = g_new0(extcap_token_sentence, 1);
111
112     rs->sentence = NULL;
113
114     /* Regex for catching just the allowed values for sentences */
115     if ((regex = g_regex_new("^[\\t| ]*(arg|value|interface|extcap|dlt|control)(?=[\\t| ]+\\{)",
116                              (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL)) != NULL) {
117         g_regex_match(regex, s, (GRegexMatchFlags) 0, &match_info);
118
119         if (g_match_info_matches(match_info))
120             rs->sentence = g_match_info_fetch(match_info, 0);
121
122         g_match_info_free(match_info);
123         g_regex_unref(regex);
124     }
125     /* No valid sentence found, exiting here */
126     if (rs->sentence == NULL) {
127         g_free(rs);
128         return NULL;
129     }
130
131     rs->param_list = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free);
132
133     /* Capture the argument and the value of the list. This will ensure,
134      * that regex patterns given to {validation=} are parsed correctly,
135      * as long as }{ does not occur within the pattern */
136     regex = g_regex_new("\\{([a-zA-Z_-]*?)\\=(.*?)\\}(?=\\{|$|\\s)",
137                         (GRegexCompileFlags) G_REGEX_CASELESS, (GRegexMatchFlags) 0, NULL);
138     if (regex != NULL) {
139         g_regex_match_full(regex, s, -1, 0, (GRegexMatchFlags) 0, &match_info, &error);
140         while (g_match_info_matches(match_info)) {
141             gchar *arg = g_match_info_fetch(match_info, 1);
142
143             if (arg == NULL)
144                 break;
145
146             param_value = g_match_info_fetch(match_info, 2);
147
148             if (g_ascii_strcasecmp(arg, "number") == 0) {
149                 param_type = EXTCAP_PARAM_ARGNUM;
150             } else if (g_ascii_strcasecmp(arg, "call") == 0) {
151                 param_type = EXTCAP_PARAM_CALL;
152             } else if (g_ascii_strcasecmp(arg, "display") == 0) {
153                 param_type = EXTCAP_PARAM_DISPLAY;
154             } else if (g_ascii_strcasecmp(arg, "type") == 0) {
155                 param_type = EXTCAP_PARAM_TYPE;
156             } else if (g_ascii_strcasecmp(arg, "arg") == 0) {
157                 param_type = EXTCAP_PARAM_ARG;
158             } else if (g_ascii_strcasecmp(arg, "default") == 0) {
159                 param_type = EXTCAP_PARAM_DEFAULT;
160             } else if (g_ascii_strcasecmp(arg, "value") == 0) {
161                 param_type = EXTCAP_PARAM_VALUE;
162             } else if (g_ascii_strcasecmp(arg, "range") == 0) {
163                 param_type = EXTCAP_PARAM_RANGE;
164             } else if (g_ascii_strcasecmp(arg, "tooltip") == 0) {
165                 param_type = EXTCAP_PARAM_TOOLTIP;
166             } else if (g_ascii_strcasecmp(arg, "placeholder") == 0) {
167                 param_type = EXTCAP_PARAM_PLACEHOLDER;
168             } else if (g_ascii_strcasecmp(arg, "mustexist") == 0) {
169                 param_type = EXTCAP_PARAM_FILE_MUSTEXIST;
170             } else if (g_ascii_strcasecmp(arg, "fileext") == 0) {
171                 param_type = EXTCAP_PARAM_FILE_EXTENSION;
172             } else if (g_ascii_strcasecmp(arg, "name") == 0) {
173                 param_type = EXTCAP_PARAM_NAME;
174             } else if (g_ascii_strcasecmp(arg, "enabled") == 0) {
175                 param_type = EXTCAP_PARAM_ENABLED;
176             } else if (g_ascii_strcasecmp(arg, "parent") == 0) {
177                 param_type = EXTCAP_PARAM_PARENT;
178             } else if (g_ascii_strcasecmp(arg, "required") == 0) {
179                 param_type = EXTCAP_PARAM_REQUIRED;
180             } else if (g_ascii_strcasecmp(arg, "save") == 0) {
181                 param_type = EXTCAP_PARAM_SAVE;
182             } else if (g_ascii_strcasecmp(arg, "validation") == 0) {
183                 param_type = EXTCAP_PARAM_VALIDATION;
184             } else if (g_ascii_strcasecmp(arg, "version") == 0) {
185                 param_type = EXTCAP_PARAM_VERSION;
186             } else if (g_ascii_strcasecmp(arg, "help") == 0) {
187                 param_type = EXTCAP_PARAM_HELP;
188             } else if (g_ascii_strcasecmp(arg, "control") == 0) {
189                 param_type = EXTCAP_PARAM_CONTROL;
190             } else if (g_ascii_strcasecmp(arg, "role") == 0) {
191                 param_type = EXTCAP_PARAM_ROLE;
192             } else {
193                 param_type = EXTCAP_PARAM_UNKNOWN;
194             }
195
196             g_hash_table_insert(rs->param_list, ENUM_KEY(param_type), param_value);
197
198             g_match_info_next(match_info, &error);
199             g_free(arg);
200         }
201         g_match_info_free(match_info);
202         g_regex_unref(regex);
203     }
204
205     return rs;
206 }
207
208 static GList *extcap_tokenize_sentences(const gchar *s) {
209
210     GList *sentences = NULL;
211     extcap_token_sentence *item = NULL;
212     gchar **list, **list_iter;
213
214     list_iter = list = g_strsplit(s, "\n", 0);
215     while (*list_iter != NULL) {
216         item = extcap_tokenize_sentence(*list_iter);
217         if (item)
218             sentences = g_list_append(sentences, item);
219         list_iter++;
220     }
221
222     g_strfreev(list);
223
224     return sentences;
225 }
226
227 static void extcap_free_value(extcap_value *v) {
228     if (v == NULL)
229         return;
230
231     g_free(v->call);
232     g_free(v->display);
233
234     g_free(v);
235 }
236
237 static void extcap_free_valuelist(gpointer data, gpointer user_data _U_) {
238     extcap_free_value((extcap_value *) data);
239 }
240
241 void extcap_free_arg(extcap_arg *a) {
242
243     if (a == NULL)
244         return;
245
246     g_free(a->call);
247     g_free(a->display);
248     g_free(a->tooltip);
249     g_free(a->placeholder);
250     g_free(a->fileextension);
251     g_free(a->regexp);
252     g_free(a->device_name);
253
254     if (a->range_start != NULL)
255         extcap_free_complex(a->range_start);
256
257     if (a->range_end != NULL)
258         extcap_free_complex(a->range_end);
259
260     if (a->default_complex != NULL)
261         extcap_free_complex(a->default_complex);
262
263     g_list_foreach(a->values, (GFunc) extcap_free_valuelist, NULL);
264     g_list_free(a->values);
265     g_free(a);
266 }
267
268 static void extcap_free_toolbar_value(iface_toolbar_value *v) {
269     if (v == NULL)
270         return;
271
272     g_free(v->value);
273     g_free(v->display);
274     g_free(v);
275 }
276
277 static void extcap_free_toolbar_control(iface_toolbar_control *c) {
278     if (c == NULL)
279         return;
280
281     g_free(c->display);
282     g_free(c->validation);
283     g_free(c->tooltip);
284     g_free(c->placeholder);
285     g_free(c);
286 }
287
288 void extcap_free_arg_list(GList *a) {
289     g_list_foreach(a, (GFunc)extcap_free_arg, NULL);
290     g_list_free(a);
291 }
292
293 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
294     if (((const extcap_arg *) listelem)->arg_num == *((const int *) needle))
295         return 0;
296     return 1;
297 }
298
299 static gint glist_find_numbered_control(gconstpointer listelem, gconstpointer needle) {
300     if (((const iface_toolbar_control *) listelem)->num == *((const int *) needle))
301         return 0;
302     return 1;
303 }
304
305 static void extcap_free_tokenized_sentence(gpointer s, gpointer user_data _U_) {
306     extcap_token_sentence *t = (extcap_token_sentence *)s;
307
308     if (t == NULL)
309         return;
310
311     g_free(t->sentence);
312     g_hash_table_destroy(t->param_list);
313     g_free(t);
314 }
315
316 static void extcap_free_tokenized_sentences(GList *sentences) {
317     if (sentences == NULL)
318         return;
319
320     g_list_foreach(sentences, extcap_free_tokenized_sentence, NULL);
321     g_list_free(sentences);
322 }
323
324 static extcap_arg *extcap_parse_arg_sentence(GList *args, extcap_token_sentence *s) {
325     gchar *param_value = NULL;
326
327     extcap_arg *target_arg = NULL;
328     extcap_value *value = NULL;
329     GList *entry = NULL;
330     int tint;
331     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
332
333     if (s == NULL)
334         return target_arg;
335
336     if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
337         sent = EXTCAP_SENTENCE_ARG;
338         /* printf("ARG sentence\n"); */
339     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
340         sent = EXTCAP_SENTENCE_VALUE;
341         /* printf("VALUE sentence\n"); */
342     }
343
344     if (sent == EXTCAP_SENTENCE_ARG) {
345         target_arg = g_new0(extcap_arg, 1);
346         target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
347         target_arg->save = TRUE;
348
349
350         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM))) == NULL) {
351             extcap_free_arg(target_arg);
352             return NULL;
353         }
354
355         if (sscanf(param_value, "%d", &(target_arg->arg_num)) != 1) {
356             extcap_free_arg(target_arg);
357             return NULL;
358         }
359
360         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CALL))) == NULL) {
361             extcap_free_arg(target_arg);
362             return NULL;
363         }
364         target_arg->call = g_strdup(param_value);
365
366         /* No value only parameters allowed */
367         if (strlen(target_arg->call) == 0) {
368             extcap_free_arg(target_arg);
369             return NULL;
370         }
371
372         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY))) == NULL) {
373             extcap_free_arg(target_arg);
374             return NULL;
375         }
376         target_arg->display = g_strdup(param_value);
377
378         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP)))
379                 != NULL) {
380             target_arg->tooltip = g_strdup(param_value);
381         }
382
383         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PLACEHOLDER)))
384                 != NULL) {
385             target_arg->placeholder = g_strdup(param_value);
386         }
387
388         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_MUSTEXIST)))
389                 != NULL) {
390             target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
391         }
392
393         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_EXTENSION)))
394                 != NULL) {
395             target_arg->fileextension = g_strdup(param_value);
396         }
397
398         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
399                 != NULL) {
400             target_arg->regexp = g_strdup(param_value);
401         }
402
403         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
404                 != NULL) {
405             target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
406         }
407
408         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE)))
409                 == NULL) {
410             /* printf("no type in ARG sentence\n"); */
411             extcap_free_arg(target_arg);
412             return NULL;
413         }
414
415         if (g_ascii_strcasecmp(param_value, "integer") == 0) {
416             target_arg->arg_type = EXTCAP_ARG_INTEGER;
417         } else if (g_ascii_strcasecmp(param_value, "unsigned") == 0) {
418             target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
419         } else if (g_ascii_strcasecmp(param_value, "long") == 0) {
420             target_arg->arg_type = EXTCAP_ARG_LONG;
421         } else if (g_ascii_strcasecmp(param_value, "double") == 0) {
422             target_arg->arg_type = EXTCAP_ARG_DOUBLE;
423         } else if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
424             target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
425         } else if (g_ascii_strcasecmp(param_value, "boolflag") == 0) {
426             target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
427         } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
428             target_arg->arg_type = EXTCAP_ARG_SELECTOR;
429         } else if (g_ascii_strcasecmp(param_value, "radio") == 0) {
430             target_arg->arg_type = EXTCAP_ARG_RADIO;
431         } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
432             target_arg->arg_type = EXTCAP_ARG_STRING;
433         } else if (g_ascii_strcasecmp(param_value, "password") == 0) {
434             target_arg->arg_type = EXTCAP_ARG_PASSWORD;
435             /* default setting is to not save passwords */
436             target_arg->save = FALSE;
437         } else if (g_ascii_strcasecmp(param_value, "fileselect") == 0) {
438             target_arg->arg_type = EXTCAP_ARG_FILESELECT;
439         } else if (g_ascii_strcasecmp(param_value, "multicheck") == 0) {
440             target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
441         } else if (g_ascii_strcasecmp(param_value, "timestamp") == 0) {
442             target_arg->arg_type = EXTCAP_ARG_TIMESTAMP;
443         } else {
444             printf("invalid type %s in ARG sentence\n", param_value);
445             extcap_free_arg(target_arg);
446             return NULL;
447         }
448
449         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_SAVE)))
450                 != NULL) {
451             target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
452         }
453
454         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_RANGE)))
455                 != NULL) {
456             gchar *cp = g_strstr_len(param_value, -1, ",");
457
458             if (cp == NULL) {
459                 printf("invalid range, expected value,value got %s\n",
460                        param_value);
461                 extcap_free_arg(target_arg);
462                 return NULL;
463             }
464
465             if ((target_arg->range_start = extcap_parse_complex(
466                                                target_arg->arg_type, param_value)) == NULL) {
467                 printf("invalid range, expected value,value got %s\n",
468                        param_value);
469                 extcap_free_arg(target_arg);
470                 return NULL;
471             }
472
473             if ((target_arg->range_end = extcap_parse_complex(
474                                              target_arg->arg_type, cp + 1)) == NULL) {
475                 printf("invalid range, expected value,value got %s\n",
476                        param_value);
477                 extcap_free_arg(target_arg);
478                 return NULL;
479             }
480         }
481
482         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
483                 != NULL) {
484             if (target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR)
485             {
486                 if ((target_arg->default_complex = extcap_parse_complex(
487                                                        target_arg->arg_type, param_value)) == NULL) {
488                     printf("invalid default, couldn't parse %s\n", param_value);
489                 }
490             }
491         }
492
493     } else if (sent == EXTCAP_SENTENCE_VALUE) {
494         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARG)))
495                 == NULL) {
496             printf("no arg in VALUE sentence\n");
497             return NULL;
498         }
499
500         if (sscanf(param_value, "%d", &tint) != 1) {
501             printf("invalid arg in VALUE sentence\n");
502             return NULL;
503         }
504
505         if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
506                 == NULL) {
507             printf("couldn't find arg %d in list for VALUE sentence\n", tint);
508             return NULL;
509         }
510
511         value = g_new0(extcap_value, 1);
512         value->arg_num = tint;
513
514         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
515                 == NULL) {
516             /* printf("no value in VALUE sentence\n"); */
517             extcap_free_value(value);
518             return NULL;
519         }
520         value->call = g_strdup(param_value);
521
522         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
523                 == NULL) {
524             /* printf("no display in VALUE sentence\n"); */
525             extcap_free_value(value);
526             return NULL;
527         }
528         value->display = g_strdup(param_value);
529
530         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PARENT)))
531                 != NULL) {
532             value->parent = g_strdup(param_value);
533         }
534
535         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
536                 != NULL) {
537             /* printf("found default value\n"); */
538             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
539         }
540
541         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ENABLED)))
542                 != NULL) {
543             value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
544         }
545
546         ((extcap_arg *) entry->data)->values = g_list_append(
547                 ((extcap_arg *) entry->data)->values, value);
548
549         return NULL;
550     }
551
552     return target_arg;
553 }
554
555 GList *extcap_parse_args(gchar *output) {
556     GList *result = NULL;
557     GList *walker = NULL;
558     GList *temp = NULL;
559
560     walker = extcap_tokenize_sentences(output);
561     temp = walker;
562
563     while (walker) {
564         extcap_arg *ra = NULL;
565         extcap_token_sentence *sentence = (extcap_token_sentence *)walker->data;
566
567         if ((ra = extcap_parse_arg_sentence(result, sentence)) != NULL)
568             result = g_list_append(result, (gpointer) ra);
569
570         walker = g_list_next(walker);
571     }
572
573     extcap_free_tokenized_sentences(temp);
574
575     return result;
576 }
577
578 static extcap_interface *extcap_parse_interface_sentence(extcap_token_sentence *s) {
579     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
580     gchar *param_value = NULL;
581     extcap_interface *ri = NULL;
582
583     if (s == NULL)
584         return NULL;
585
586     if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
587         sent = EXTCAP_SENTENCE_INTERFACE;
588     } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
589         sent = EXTCAP_SENTENCE_EXTCAP;
590     }
591
592     if (sent == EXTCAP_SENTENCE_UNKNOWN)
593         return NULL;
594
595     ri = g_new0(extcap_interface, 1);
596
597     ri->if_type = sent;
598
599     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
600             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
601         printf("No value in INTERFACE sentence\n");
602         g_free(ri);
603         return NULL;
604     }
605     ri->call = g_strdup(param_value);
606
607     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
608             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
609         printf("No display in INTERFACE sentence\n");
610         g_free(ri->call);
611         g_free(ri);
612         return NULL;
613     }
614     ri->display = g_strdup(param_value);
615
616     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VERSION)))
617             != NULL) {
618         ri->version = g_strdup(param_value);
619     }
620
621     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_HELP)))
622             != NULL) {
623         ri->help = g_strdup(param_value);
624     }
625
626     return ri;
627 }
628
629 static iface_toolbar_control *extcap_parse_control_sentence(GList *control_items, extcap_token_sentence *s)
630 {
631     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
632     gchar *param_value = NULL;
633     iface_toolbar_control *control = NULL;
634     iface_toolbar_value *value = NULL;
635     GList *entry = NULL;
636     guint32 num = 0;
637
638     if (s == NULL)
639         return NULL;
640
641     if (g_ascii_strcasecmp(s->sentence, "control") == 0) {
642         sent = EXTCAP_SENTENCE_CONTROL;
643     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
644         sent = EXTCAP_SENTENCE_VALUE;
645     }
646
647     if (sent == EXTCAP_SENTENCE_UNKNOWN)
648         return NULL;
649
650     if (sent == EXTCAP_SENTENCE_CONTROL) {
651         control = g_new0(iface_toolbar_control, 1);
652         control->ctrl_type = INTERFACE_TYPE_UNKNOWN;
653
654         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM));
655         if (param_value == NULL) {
656             extcap_free_toolbar_control(control);
657             return NULL;
658         }
659
660         if (!ws_strtou32(param_value, NULL, &num)) {
661             extcap_free_toolbar_control(control);
662             return NULL;
663         }
664         control->num = (int)num;
665
666         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY));
667         if (param_value == NULL) {
668             extcap_free_toolbar_control(control);
669             return NULL;
670         }
671         control->display = g_strdup(param_value);
672
673         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
674             != NULL) {
675             control->validation = g_strdup(param_value);
676         }
677
678         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
679             != NULL) {
680             control->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
681         }
682
683         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP));
684         control->tooltip = g_strdup(param_value);
685
686         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PLACEHOLDER));
687         control->placeholder = g_strdup(param_value);
688
689         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE));
690         if (param_value == NULL) {
691             extcap_free_toolbar_control(control);
692             return NULL;
693         }
694
695         extcap_arg_type arg_type = EXTCAP_ARG_UNKNOWN;
696         if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
697             control->ctrl_type = INTERFACE_TYPE_BOOLEAN;
698             arg_type = EXTCAP_ARG_BOOLEAN;
699         } else if (g_ascii_strcasecmp(param_value, "button") == 0) {
700             control->ctrl_type = INTERFACE_TYPE_BUTTON;
701         } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
702             control->ctrl_type = INTERFACE_TYPE_SELECTOR;
703         } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
704             control->ctrl_type = INTERFACE_TYPE_STRING;
705             arg_type = EXTCAP_ARG_STRING;
706         } else {
707             printf("invalid type %s in CONTROL sentence\n", param_value);
708             extcap_free_toolbar_control(control);
709             return NULL;
710         }
711
712         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ROLE));
713         if (param_value != NULL) {
714             if (g_ascii_strcasecmp(param_value, "control") == 0) {
715                 control->ctrl_role = INTERFACE_ROLE_CONTROL;
716             } else if (g_ascii_strcasecmp(param_value, "help") == 0) {
717                 control->ctrl_role = INTERFACE_ROLE_HELP;
718             } else if (g_ascii_strcasecmp(param_value, "logger") == 0) {
719                 control->ctrl_role = INTERFACE_ROLE_LOGGER;
720             } else if (g_ascii_strcasecmp(param_value, "restore") == 0) {
721                 control->ctrl_role = INTERFACE_ROLE_RESTORE;
722             } else {
723                 printf("invalid role %s in CONTROL sentence\n", param_value);
724                 control->ctrl_role = INTERFACE_ROLE_UNKNOWN;
725             }
726         } else {
727             /* Default role */
728             control->ctrl_role = INTERFACE_ROLE_CONTROL;
729         }
730
731         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT));
732         if (param_value != NULL) {
733             if (arg_type != EXTCAP_ARG_UNKNOWN) {
734                 extcap_complex *complex = extcap_parse_complex(arg_type, param_value);
735                 if (complex != NULL) {
736                     if (arg_type == EXTCAP_ARG_BOOLEAN) {
737                         control->default_value.boolean = extcap_complex_get_bool(complex);
738                     } else if (arg_type == EXTCAP_ARG_STRING) {
739                         control->default_value.string = g_strdup(complex->_val);
740                     }
741                     extcap_free_complex(complex);
742                 } else {
743                     printf("invalid default, couldn't parse %s\n", param_value);
744                 }
745             }
746         }
747
748     } else if (sent == EXTCAP_SENTENCE_VALUE) {
749         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CONTROL));
750         if (param_value == NULL) {
751             printf("no control in VALUE sentence\n");
752             return NULL;
753         }
754
755         if (!ws_strtou32(param_value, NULL, &num)) {
756             extcap_free_toolbar_control(control);
757             return NULL;
758         }
759
760         entry = g_list_find_custom(control_items, &num, glist_find_numbered_control);
761         if (entry == NULL) {
762             printf("couldn't find control %u in list for VALUE sentence\n", num);
763             return NULL;
764         }
765
766         value = g_new0(iface_toolbar_value, 1);
767         value->num = (int)num;
768
769         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE));
770         if (param_value == NULL) {
771             extcap_free_toolbar_value(value);
772             return NULL;
773         }
774         value->value = g_strdup(param_value);
775
776         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY));
777         if (param_value == NULL) {
778             extcap_free_toolbar_value(value);
779             return NULL;
780         }
781         value->display = g_strdup(param_value);
782
783         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT));
784         if (param_value != NULL) {
785             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
786         }
787
788         control = (iface_toolbar_control *)entry->data;
789         control->values = g_list_append(control->values, value);
790
791         return NULL;
792     }
793
794     return control;
795 }
796
797 GList *extcap_parse_interfaces(gchar *output, GList **control_items) {
798
799     GList *result = NULL;
800     GList *tokens = NULL;
801     GList *walker = extcap_tokenize_sentences(output);
802     tokens = walker;
803
804     while (walker) {
805         extcap_interface *ri = NULL;
806         iface_toolbar_control *ti = NULL;
807         extcap_token_sentence *if_sentence = (extcap_token_sentence *) walker->data;
808
809         if (if_sentence) {
810             if ((g_ascii_strcasecmp(if_sentence->sentence, "interface") == 0) ||
811                 (g_ascii_strcasecmp(if_sentence->sentence, "extcap") == 0))
812             {
813                 if ((ri = extcap_parse_interface_sentence(if_sentence))) {
814                     result = g_list_append(result, ri);
815                 }
816             } else if (control_items &&
817                        ((g_ascii_strcasecmp(if_sentence->sentence, "control") == 0) ||
818                         (g_ascii_strcasecmp(if_sentence->sentence, "value") == 0)))
819             {
820                 if ((ti = extcap_parse_control_sentence(*control_items, if_sentence))) {
821                     *control_items = g_list_append(*control_items, ti);
822                 }
823             }
824         }
825
826         walker = g_list_next(walker);
827     }
828
829     extcap_free_tokenized_sentences(tokens);
830
831     return result;
832 }
833
834 /* Parse a tokenized set of sentences and validate, looking for DLT definitions */
835 static extcap_dlt *extcap_parse_dlt_sentence(extcap_token_sentence *s) {
836     gchar *param_value = NULL;
837     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
838     extcap_dlt *result = NULL;
839
840     if (s == NULL)
841         return result;
842
843     if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
844         sent = EXTCAP_SENTENCE_DLT;
845     }
846
847     if (sent == EXTCAP_SENTENCE_UNKNOWN)
848         return result;
849
850     result = g_new0(extcap_dlt, 1);
851
852     result->number = -1;
853     result->name = NULL;
854     result->display = NULL;
855
856     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM)))
857             == NULL) {
858         printf("No number in DLT sentence\n");
859         g_free(result);
860         return NULL;
861     }
862     if (sscanf(param_value, "%d", &(result->number)) != 1) {
863         printf("Invalid number in DLT sentence\n");
864         g_free(result);
865         return NULL;
866     }
867
868     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_NAME)))
869             == NULL) {
870         printf("No name in DLT sentence\n");
871         g_free(result);
872         return NULL;
873     }
874     result->name = g_strdup(param_value);
875
876     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
877             == NULL) {
878         printf("No display in DLT sentence\n");
879         g_free(result->name);
880         g_free(result);
881         return NULL;
882     }
883     result->display = g_strdup(param_value);
884
885     return result;
886 }
887
888 GList *extcap_parse_dlts(gchar *output) {
889
890     GList *walker = NULL;
891     GList *temp = NULL;
892     GList *result = NULL;
893
894     walker = extcap_tokenize_sentences(output);
895
896     temp = walker;
897
898     while (walker) {
899         extcap_dlt *data = NULL;
900
901         if ((data = extcap_parse_dlt_sentence((extcap_token_sentence *)walker->data)) != NULL)
902             result = g_list_append(result, data);
903
904         walker = g_list_next(walker);
905     }
906
907     extcap_free_tokenized_sentences(temp);
908
909     return result;
910 }
911
912 /*
913  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
914  *
915  * Local variables:
916  * c-basic-offset: 4
917  * tab-width: 8
918  * indent-tabs-mode: nil
919  * End:
920  *
921  * vi: set shiftwidth=4 tabstop=8 expandtab:
922  * :indentSize=4:tabSize=8:noTabs=true:
923  */