html2text: Prefix lists with a bullet.
[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 }
275
276 static void extcap_free_toolbar_control(iface_toolbar_control *c) {
277     if (c == NULL)
278         return;
279
280     g_free(c->display);
281     g_free(c->validation);
282     g_free(c->tooltip);
283     g_free(c->placeholder);
284     g_free(c);
285 }
286
287 void extcap_free_arg_list(GList *a) {
288     g_list_foreach(a, (GFunc)extcap_free_arg, NULL);
289     g_list_free(a);
290 }
291
292 static gint glist_find_numbered_arg(gconstpointer listelem, gconstpointer needle) {
293     if (((const extcap_arg *) listelem)->arg_num == *((const int *) needle))
294         return 0;
295     return 1;
296 }
297
298 static gint glist_find_numbered_control(gconstpointer listelem, gconstpointer needle) {
299     if (((const iface_toolbar_control *) listelem)->num == *((const int *) needle))
300         return 0;
301     return 1;
302 }
303
304 static void extcap_free_tokenized_sentence(gpointer s, gpointer user_data _U_) {
305     extcap_token_sentence *t = (extcap_token_sentence *)s;
306
307     if (t == NULL)
308         return;
309
310     g_free(t->sentence);
311     g_hash_table_destroy(t->param_list);
312     g_free(t);
313 }
314
315 static void extcap_free_tokenized_sentences(GList *sentences) {
316     if (sentences == NULL)
317         return;
318
319     g_list_foreach(sentences, extcap_free_tokenized_sentence, NULL);
320     g_list_free(sentences);
321 }
322
323 static extcap_arg *extcap_parse_arg_sentence(GList *args, extcap_token_sentence *s) {
324     gchar *param_value = NULL;
325
326     extcap_arg *target_arg = NULL;
327     extcap_value *value = NULL;
328     GList *entry = NULL;
329     int tint;
330     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
331
332     if (s == NULL)
333         return target_arg;
334
335     if (g_ascii_strcasecmp(s->sentence, "arg") == 0) {
336         sent = EXTCAP_SENTENCE_ARG;
337         /* printf("ARG sentence\n"); */
338     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
339         sent = EXTCAP_SENTENCE_VALUE;
340         /* printf("VALUE sentence\n"); */
341     }
342
343     if (sent == EXTCAP_SENTENCE_ARG) {
344         target_arg = g_new0(extcap_arg, 1);
345         target_arg->arg_type = EXTCAP_ARG_UNKNOWN;
346         target_arg->save = TRUE;
347
348
349         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM))) == NULL) {
350             extcap_free_arg(target_arg);
351             return NULL;
352         }
353
354         if (sscanf(param_value, "%d", &(target_arg->arg_num)) != 1) {
355             extcap_free_arg(target_arg);
356             return NULL;
357         }
358
359         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CALL))) == NULL) {
360             extcap_free_arg(target_arg);
361             return NULL;
362         }
363         target_arg->call = g_strdup(param_value);
364
365         /* No value only parameters allowed */
366         if (strlen(target_arg->call) == 0) {
367             extcap_free_arg(target_arg);
368             return NULL;
369         }
370
371         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY))) == NULL) {
372             extcap_free_arg(target_arg);
373             return NULL;
374         }
375         target_arg->display = g_strdup(param_value);
376
377         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP)))
378                 != NULL) {
379             target_arg->tooltip = g_strdup(param_value);
380         }
381
382         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PLACEHOLDER)))
383                 != NULL) {
384             target_arg->placeholder = g_strdup(param_value);
385         }
386
387         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_MUSTEXIST)))
388                 != NULL) {
389             target_arg->fileexists = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
390         }
391
392         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_FILE_EXTENSION)))
393                 != NULL) {
394             target_arg->fileextension = g_strdup(param_value);
395         }
396
397         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
398                 != NULL) {
399             target_arg->regexp = g_strdup(param_value);
400         }
401
402         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
403                 != NULL) {
404             target_arg->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
405         }
406
407         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE)))
408                 == NULL) {
409             /* printf("no type in ARG sentence\n"); */
410             extcap_free_arg(target_arg);
411             return NULL;
412         }
413
414         if (g_ascii_strcasecmp(param_value, "integer") == 0) {
415             target_arg->arg_type = EXTCAP_ARG_INTEGER;
416         } else if (g_ascii_strcasecmp(param_value, "unsigned") == 0) {
417             target_arg->arg_type = EXTCAP_ARG_UNSIGNED;
418         } else if (g_ascii_strcasecmp(param_value, "long") == 0) {
419             target_arg->arg_type = EXTCAP_ARG_LONG;
420         } else if (g_ascii_strcasecmp(param_value, "double") == 0) {
421             target_arg->arg_type = EXTCAP_ARG_DOUBLE;
422         } else if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
423             target_arg->arg_type = EXTCAP_ARG_BOOLEAN;
424         } else if (g_ascii_strcasecmp(param_value, "boolflag") == 0) {
425             target_arg->arg_type = EXTCAP_ARG_BOOLFLAG;
426         } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
427             target_arg->arg_type = EXTCAP_ARG_SELECTOR;
428         } else if (g_ascii_strcasecmp(param_value, "radio") == 0) {
429             target_arg->arg_type = EXTCAP_ARG_RADIO;
430         } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
431             target_arg->arg_type = EXTCAP_ARG_STRING;
432         } else if (g_ascii_strcasecmp(param_value, "password") == 0) {
433             target_arg->arg_type = EXTCAP_ARG_PASSWORD;
434             /* default setting is to not save passwords */
435             target_arg->save = FALSE;
436         } else if (g_ascii_strcasecmp(param_value, "fileselect") == 0) {
437             target_arg->arg_type = EXTCAP_ARG_FILESELECT;
438         } else if (g_ascii_strcasecmp(param_value, "multicheck") == 0) {
439             target_arg->arg_type = EXTCAP_ARG_MULTICHECK;
440         } else if (g_ascii_strcasecmp(param_value, "timestamp") == 0) {
441             target_arg->arg_type = EXTCAP_ARG_TIMESTAMP;
442         } else {
443             printf("invalid type %s in ARG sentence\n", param_value);
444             extcap_free_arg(target_arg);
445             return NULL;
446         }
447
448         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_SAVE)))
449                 != NULL) {
450             target_arg->save = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
451         }
452
453         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_RANGE)))
454                 != NULL) {
455             gchar *cp = g_strstr_len(param_value, -1, ",");
456
457             if (cp == NULL) {
458                 printf("invalid range, expected value,value got %s\n",
459                        param_value);
460                 extcap_free_arg(target_arg);
461                 return NULL;
462             }
463
464             if ((target_arg->range_start = extcap_parse_complex(
465                                                target_arg->arg_type, param_value)) == NULL) {
466                 printf("invalid range, expected value,value got %s\n",
467                        param_value);
468                 extcap_free_arg(target_arg);
469                 return NULL;
470             }
471
472             if ((target_arg->range_end = extcap_parse_complex(
473                                              target_arg->arg_type, cp + 1)) == NULL) {
474                 printf("invalid range, expected value,value got %s\n",
475                        param_value);
476                 extcap_free_arg(target_arg);
477                 return NULL;
478             }
479         }
480
481         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
482                 != NULL) {
483             if (target_arg->arg_type != EXTCAP_ARG_MULTICHECK && target_arg->arg_type != EXTCAP_ARG_SELECTOR)
484             {
485                 if ((target_arg->default_complex = extcap_parse_complex(
486                                                        target_arg->arg_type, param_value)) == NULL) {
487                     printf("invalid default, couldn't parse %s\n", param_value);
488                 }
489             }
490         }
491
492     } else if (sent == EXTCAP_SENTENCE_VALUE) {
493         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARG)))
494                 == NULL) {
495             printf("no arg in VALUE sentence\n");
496             return NULL;
497         }
498
499         if (sscanf(param_value, "%d", &tint) != 1) {
500             printf("invalid arg in VALUE sentence\n");
501             return NULL;
502         }
503
504         if ((entry = g_list_find_custom(args, &tint, glist_find_numbered_arg))
505                 == NULL) {
506             printf("couldn't find arg %d in list for VALUE sentence\n", tint);
507             return NULL;
508         }
509
510         value = g_new0(extcap_value, 1);
511         value->arg_num = tint;
512
513         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
514                 == NULL) {
515             /* printf("no value in VALUE sentence\n"); */
516             extcap_free_value(value);
517             return NULL;
518         }
519         value->call = g_strdup(param_value);
520
521         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
522                 == NULL) {
523             /* printf("no display in VALUE sentence\n"); */
524             extcap_free_value(value);
525             return NULL;
526         }
527         value->display = g_strdup(param_value);
528
529         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PARENT)))
530                 != NULL) {
531             value->parent = g_strdup(param_value);
532         }
533
534         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT)))
535                 != NULL) {
536             /* printf("found default value\n"); */
537             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
538         }
539
540         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ENABLED)))
541                 != NULL) {
542             value->enabled = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
543         }
544
545         ((extcap_arg *) entry->data)->values = g_list_append(
546                 ((extcap_arg *) entry->data)->values, value);
547
548         return NULL;
549     }
550
551     return target_arg;
552 }
553
554 GList *extcap_parse_args(gchar *output) {
555     GList *result = NULL;
556     GList *walker = NULL;
557     GList *temp = NULL;
558
559     walker = extcap_tokenize_sentences(output);
560     temp = walker;
561
562     while (walker) {
563         extcap_arg *ra = NULL;
564         extcap_token_sentence *sentence = (extcap_token_sentence *)walker->data;
565
566         if ((ra = extcap_parse_arg_sentence(result, sentence)) != NULL)
567             result = g_list_append(result, (gpointer) ra);
568
569         walker = g_list_next(walker);
570     }
571
572     extcap_free_tokenized_sentences(temp);
573
574     return result;
575 }
576
577 static extcap_interface *extcap_parse_interface_sentence(extcap_token_sentence *s) {
578     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
579     gchar *param_value = NULL;
580     extcap_interface *ri = NULL;
581
582     if (s == NULL)
583         return NULL;
584
585     if (g_ascii_strcasecmp(s->sentence, "interface") == 0) {
586         sent = EXTCAP_SENTENCE_INTERFACE;
587     } else if (g_ascii_strcasecmp(s->sentence, "extcap") == 0) {
588         sent = EXTCAP_SENTENCE_EXTCAP;
589     }
590
591     if (sent == EXTCAP_SENTENCE_UNKNOWN)
592         return NULL;
593
594     ri = g_new0(extcap_interface, 1);
595
596     ri->if_type = sent;
597
598     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE)))
599             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
600         printf("No value in INTERFACE sentence\n");
601         g_free(ri);
602         return NULL;
603     }
604     ri->call = g_strdup(param_value);
605
606     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
607             == NULL && sent == EXTCAP_SENTENCE_INTERFACE) {
608         printf("No display in INTERFACE sentence\n");
609         g_free(ri->call);
610         g_free(ri);
611         return NULL;
612     }
613     ri->display = g_strdup(param_value);
614
615     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VERSION)))
616             != NULL) {
617         ri->version = g_strdup(param_value);
618     }
619
620     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_HELP)))
621             != NULL) {
622         ri->help = g_strdup(param_value);
623     }
624
625     return ri;
626 }
627
628 static iface_toolbar_control *extcap_parse_control_sentence(GList *control_items, extcap_token_sentence *s)
629 {
630     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
631     gchar *param_value = NULL;
632     iface_toolbar_control *control = NULL;
633     iface_toolbar_value *value = NULL;
634     GList *entry = NULL;
635     guint32 num = 0;
636
637     if (s == NULL)
638         return NULL;
639
640     if (g_ascii_strcasecmp(s->sentence, "control") == 0) {
641         sent = EXTCAP_SENTENCE_CONTROL;
642     } else if (g_ascii_strcasecmp(s->sentence, "value") == 0) {
643         sent = EXTCAP_SENTENCE_VALUE;
644     }
645
646     if (sent == EXTCAP_SENTENCE_UNKNOWN)
647         return NULL;
648
649     if (sent == EXTCAP_SENTENCE_CONTROL) {
650         control = g_new0(iface_toolbar_control, 1);
651         control->ctrl_type = INTERFACE_TYPE_UNKNOWN;
652
653         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM));
654         if (param_value == NULL) {
655             extcap_free_toolbar_control(control);
656             return NULL;
657         }
658
659         if (!ws_strtou32(param_value, NULL, &num)) {
660             extcap_free_toolbar_control(control);
661             return NULL;
662         }
663         control->num = (int)num;
664
665         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY));
666         if (param_value == NULL) {
667             extcap_free_toolbar_control(control);
668             return NULL;
669         }
670         control->display = g_strdup(param_value);
671
672         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALIDATION)))
673             != NULL) {
674             control->validation = g_strdup(param_value);
675         }
676
677         if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_REQUIRED)))
678             != NULL) {
679             control->is_required = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
680         }
681
682         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TOOLTIP));
683         control->tooltip = g_strdup(param_value);
684
685         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_PLACEHOLDER));
686         control->placeholder = g_strdup(param_value);
687
688         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_TYPE));
689         if (param_value == NULL) {
690             extcap_free_toolbar_control(control);
691             return NULL;
692         }
693
694         extcap_arg_type arg_type = EXTCAP_ARG_UNKNOWN;
695         if (g_ascii_strcasecmp(param_value, "boolean") == 0) {
696             control->ctrl_type = INTERFACE_TYPE_BOOLEAN;
697             arg_type = EXTCAP_ARG_BOOLEAN;
698         } else if (g_ascii_strcasecmp(param_value, "button") == 0) {
699             control->ctrl_type = INTERFACE_TYPE_BUTTON;
700         } else if (g_ascii_strcasecmp(param_value, "selector") == 0) {
701             control->ctrl_type = INTERFACE_TYPE_SELECTOR;
702         } else if (g_ascii_strcasecmp(param_value, "string") == 0) {
703             control->ctrl_type = INTERFACE_TYPE_STRING;
704             arg_type = EXTCAP_ARG_STRING;
705         } else {
706             printf("invalid type %s in CONTROL sentence\n", param_value);
707             extcap_free_toolbar_control(control);
708             return NULL;
709         }
710
711         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ROLE));
712         if (param_value != NULL) {
713             if (g_ascii_strcasecmp(param_value, "control") == 0) {
714                 control->ctrl_role = INTERFACE_ROLE_CONTROL;
715             } else if (g_ascii_strcasecmp(param_value, "help") == 0) {
716                 control->ctrl_role = INTERFACE_ROLE_HELP;
717             } else if (g_ascii_strcasecmp(param_value, "logger") == 0) {
718                 control->ctrl_role = INTERFACE_ROLE_LOGGER;
719             } else if (g_ascii_strcasecmp(param_value, "restore") == 0) {
720                 control->ctrl_role = INTERFACE_ROLE_RESTORE;
721             } else {
722                 printf("invalid role %s in CONTROL sentence\n", param_value);
723                 control->ctrl_role = INTERFACE_ROLE_UNKNOWN;
724             }
725         } else {
726             /* Default role */
727             control->ctrl_role = INTERFACE_ROLE_CONTROL;
728         }
729
730         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT));
731         if (param_value != NULL) {
732             if (arg_type != EXTCAP_ARG_UNKNOWN) {
733                 extcap_complex *complex = extcap_parse_complex(arg_type, param_value);
734                 if (complex != NULL) {
735                     if (arg_type == EXTCAP_ARG_BOOLEAN) {
736                         control->default_value.boolean = extcap_complex_get_bool(complex);
737                     } else if (arg_type == EXTCAP_ARG_STRING) {
738                         control->default_value.string = g_strdup(complex->_val);
739                     }
740                     extcap_free_complex(complex);
741                 } else {
742                     printf("invalid default, couldn't parse %s\n", param_value);
743                 }
744             }
745         }
746
747     } else if (sent == EXTCAP_SENTENCE_VALUE) {
748         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_CONTROL));
749         if (param_value == NULL) {
750             printf("no control in VALUE sentence\n");
751             return NULL;
752         }
753
754         if (!ws_strtou32(param_value, NULL, &num)) {
755             extcap_free_toolbar_control(control);
756             return NULL;
757         }
758
759         entry = g_list_find_custom(control_items, &num, glist_find_numbered_control);
760         if (entry == NULL) {
761             printf("couldn't find control %u in list for VALUE sentence\n", num);
762             return NULL;
763         }
764
765         value = g_new0(iface_toolbar_value, 1);
766         value->num = (int)num;
767
768         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_VALUE));
769         if (param_value == NULL) {
770             extcap_free_toolbar_value(value);
771             return NULL;
772         }
773         value->value = g_strdup(param_value);
774
775         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY));
776         if (param_value == NULL) {
777             extcap_free_toolbar_value(value);
778             return NULL;
779         }
780         value->display = g_strdup(param_value);
781
782         param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DEFAULT));
783         if (param_value != NULL) {
784             value->is_default = g_regex_match_simple(EXTCAP_BOOLEAN_REGEX, param_value, G_REGEX_CASELESS, (GRegexMatchFlags)0);
785         }
786
787         control = (iface_toolbar_control *)entry->data;
788         control->values = g_list_append(control->values, value);
789
790         return NULL;
791     }
792
793     return control;
794 }
795
796 GList *extcap_parse_interfaces(gchar *output, GList **control_items) {
797
798     GList *result = NULL;
799     GList *tokens = NULL;
800     GList *walker = extcap_tokenize_sentences(output);
801     tokens = walker;
802
803     while (walker) {
804         extcap_interface *ri = NULL;
805         iface_toolbar_control *ti = NULL;
806         extcap_token_sentence *if_sentence = (extcap_token_sentence *) walker->data;
807
808         if (if_sentence) {
809             if ((g_ascii_strcasecmp(if_sentence->sentence, "interface") == 0) ||
810                 (g_ascii_strcasecmp(if_sentence->sentence, "extcap") == 0))
811             {
812                 if ((ri = extcap_parse_interface_sentence(if_sentence))) {
813                     result = g_list_append(result, ri);
814                 }
815             } else if (control_items &&
816                        ((g_ascii_strcasecmp(if_sentence->sentence, "control") == 0) ||
817                         (g_ascii_strcasecmp(if_sentence->sentence, "value") == 0)))
818             {
819                 if ((ti = extcap_parse_control_sentence(*control_items, if_sentence))) {
820                     *control_items = g_list_append(*control_items, ti);
821                 }
822             }
823         }
824
825         walker = g_list_next(walker);
826     }
827
828     extcap_free_tokenized_sentences(tokens);
829
830     return result;
831 }
832
833 /* Parse a tokenized set of sentences and validate, looking for DLT definitions */
834 static extcap_dlt *extcap_parse_dlt_sentence(extcap_token_sentence *s) {
835     gchar *param_value = NULL;
836     extcap_sentence_type sent = EXTCAP_SENTENCE_UNKNOWN;
837     extcap_dlt *result = NULL;
838
839     if (s == NULL)
840         return result;
841
842     if (g_ascii_strcasecmp(s->sentence, "dlt") == 0) {
843         sent = EXTCAP_SENTENCE_DLT;
844     }
845
846     if (sent == EXTCAP_SENTENCE_UNKNOWN)
847         return result;
848
849     result = g_new0(extcap_dlt, 1);
850
851     result->number = -1;
852     result->name = NULL;
853     result->display = NULL;
854
855     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_ARGNUM)))
856             == NULL) {
857         printf("No number in DLT sentence\n");
858         g_free(result);
859         return NULL;
860     }
861     if (sscanf(param_value, "%d", &(result->number)) != 1) {
862         printf("Invalid number in DLT sentence\n");
863         g_free(result);
864         return NULL;
865     }
866
867     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_NAME)))
868             == NULL) {
869         printf("No name in DLT sentence\n");
870         g_free(result);
871         return NULL;
872     }
873     result->name = g_strdup(param_value);
874
875     if ((param_value = (gchar *)g_hash_table_lookup(s->param_list, ENUM_KEY(EXTCAP_PARAM_DISPLAY)))
876             == NULL) {
877         printf("No display in DLT sentence\n");
878         g_free(result->name);
879         g_free(result);
880         return NULL;
881     }
882     result->display = g_strdup(param_value);
883
884     return result;
885 }
886
887 GList *extcap_parse_dlts(gchar *output) {
888
889     GList *walker = NULL;
890     GList *temp = NULL;
891     GList *result = NULL;
892
893     walker = extcap_tokenize_sentences(output);
894
895     temp = walker;
896
897     while (walker) {
898         extcap_dlt *data = NULL;
899
900         if ((data = extcap_parse_dlt_sentence((extcap_token_sentence *)walker->data)) != NULL)
901             result = g_list_append(result, data);
902
903         walker = g_list_next(walker);
904     }
905
906     extcap_free_tokenized_sentences(temp);
907
908     return result;
909 }
910
911 /*
912  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
913  *
914  * Local variables:
915  * c-basic-offset: 4
916  * tab-width: 8
917  * indent-tabs-mode: nil
918  * End:
919  *
920  * vi: set shiftwidth=4 tabstop=8 expandtab:
921  * :indentSize=4:tabSize=8:noTabs=true:
922  */