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