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