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