]> sigrok.org Git - sigrok-cli.git/blob - parsers.c
ad5f37925f7d0e156e9158e0865f813c2de6d70d
[sigrok-cli.git] / parsers.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "sigrok-cli.h"
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <glib.h>
26
27 extern struct sr_context *sr_ctx;
28
29 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
30 {
31         struct sr_channel *ch;
32         GSList *l;
33
34         ch = NULL;
35         for (l = channellist; l; l = l->next) {
36                 ch = l->data;
37                 if (!strcmp(ch->name, channelname))
38                         break;
39         }
40         ch = l ? l->data : NULL;
41
42         return ch;
43 }
44
45 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
46 {
47         struct sr_channel *ch;
48         GSList *channellist;
49         int ret, n, b, e, i;
50         char **tokens, **range, **names, *eptr, str[8];
51
52         if (!channelstring || !channelstring[0])
53                 /* Use all channels by default. */
54                 return g_slist_copy(sdi->channels);
55
56         ret = SR_OK;
57         range = NULL;
58         names = NULL;
59         channellist = NULL;
60         tokens = g_strsplit(channelstring, ",", 0);
61         for (i = 0; tokens[i]; i++) {
62                 if (tokens[i][0] == '\0') {
63                         g_critical("Invalid empty channel.");
64                         ret = SR_ERR;
65                         break;
66                 }
67                 if (strchr(tokens[i], '-')) {
68                         /* A range of channels in the form a-b. This will only work
69                          * if the channels are named as numbers -- so every channel
70                          * in the range must exist as a channel name string in the
71                          * device. */
72                         range = g_strsplit(tokens[i], "-", 2);
73                         if (!range[0] || !range[1] || range[2]) {
74                                 /* Need exactly two arguments. */
75                                 g_critical("Invalid channel syntax '%s'.", tokens[i]);
76                                 ret = SR_ERR;
77                                 goto range_fail;
78                         }
79
80                         b = strtol(range[0], &eptr, 10);
81                         if (eptr == range[0] || *eptr != '\0') {
82                                 g_critical("Invalid channel '%s'.", range[0]);
83                                 ret = SR_ERR;
84                                 goto range_fail;
85                         }
86                         e = strtol(range[1], NULL, 10);
87                         if (eptr == range[1] || *eptr != '\0') {
88                                 g_critical("Invalid channel '%s'.", range[1]);
89                                 ret = SR_ERR;
90                                 goto range_fail;
91                         }
92                         if (b < 0 || b >= e) {
93                                 g_critical("Invalid channel range '%s'.", tokens[i]);
94                                 ret = SR_ERR;
95                                 goto range_fail;
96                         }
97
98                         while (b <= e) {
99                                 n = snprintf(str, 8, "%d", b);
100                                 if (n < 0 || n > 8) {
101                                         g_critical("Invalid channel '%d'.", b);
102                                         ret = SR_ERR;
103                                         break;
104                                 }
105                                 ch = find_channel(sdi->channels, str);
106                                 if (!ch) {
107                                         g_critical("unknown channel '%d'.", b);
108                                         ret = SR_ERR;
109                                         break;
110                                 }
111                                 channellist = g_slist_append(channellist, ch);
112                                 b++;
113                         }
114 range_fail:
115                         if (range)
116                                 g_strfreev(range);
117
118                         if (ret != SR_OK)
119                                 break;
120                 } else {
121                         names = g_strsplit(tokens[i], "=", 2);
122                         if (!names[0] || (names[1] && names[2])) {
123                                 /* Need one or two arguments. */
124                                 g_critical("Invalid channel '%s'.", tokens[i]);
125                                 g_strfreev(names);
126                                 ret = SR_ERR;
127                                 break;
128                         }
129
130                         ch = find_channel(sdi->channels, names[0]);
131                         if (!ch) {
132                                 g_critical("unknown channel '%s'.", names[0]);
133                                 g_strfreev(names);
134                                 ret = SR_ERR;
135                                 break;
136                         }
137                         if (names[1]) {
138                                 /* Rename channel. */
139                                 g_free(ch->name);
140                                 ch->name = g_strdup(names[1]);
141                         }
142                         channellist = g_slist_append(channellist, ch);
143
144                         g_strfreev(names);
145                 }
146         }
147
148         if (ret != SR_OK) {
149                 g_slist_free(channellist);
150                 channellist = NULL;
151         }
152
153         g_strfreev(tokens);
154
155         return channellist;
156 }
157
158 int parse_trigger_match(char c)
159 {
160         int match;
161
162         if (c == '0')
163                 match = SR_TRIGGER_ZERO;
164         else if (c == '1')
165                 match = SR_TRIGGER_ONE;
166         else if (c == 'r')
167                 match = SR_TRIGGER_RISING;
168         else if (c == 'f')
169                 match = SR_TRIGGER_FALLING;
170         else if (c == 'e')
171                 match = SR_TRIGGER_EDGE;
172         else if (c == 'o')
173                 match = SR_TRIGGER_OVER;
174         else if (c == 'u')
175                 match = SR_TRIGGER_UNDER;
176         else
177                 match = 0;
178
179         return match;
180 }
181
182 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
183                 struct sr_trigger **trigger)
184 {
185         struct sr_channel *ch;
186         struct sr_trigger_stage *stage;
187         GVariant *gvar;
188         GSList *l;
189         gsize num_matches;
190         gboolean found_match, error;
191         const int32_t *matches;
192         int32_t match;
193         unsigned int j;
194         int t, i;
195         char **tokens, *sep;
196
197         if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
198                         &gvar) != SR_OK) {
199                 g_critical("Device doesn't support any triggers.");
200                 return FALSE;
201         }
202         matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
203
204         *trigger = sr_trigger_new(NULL);
205         error = FALSE;
206         tokens = g_strsplit(s, ",", -1);
207         for (i = 0; tokens[i]; i++) {
208                 if (!(sep = strchr(tokens[i], '='))) {
209                         g_critical("Invalid trigger '%s'.", tokens[i]);
210                         error = TRUE;
211                         break;
212                 }
213                 *sep++ = 0;
214                 ch = NULL;
215                 for (l = sdi->channels; l; l = l->next) {
216                         ch = l->data;
217                         if (ch->enabled && !strcmp(ch->name, tokens[i]))
218                                 break;
219                         ch = NULL;
220                 }
221                 if (!ch) {
222                         g_critical("Invalid channel '%s'.", tokens[i]);
223                         error = TRUE;
224                         break;
225                 }
226                 for (t = 0; sep[t]; t++) {
227                         if (!(match = parse_trigger_match(sep[t]))) {
228                                 g_critical("Invalid trigger match '%c'.", sep[t]);
229                                 error = TRUE;
230                                 break;
231                         }
232                         found_match = FALSE;
233                         for (j = 0; j < num_matches; j++) {
234                                 if (matches[j] == match) {
235                                         found_match = TRUE;
236                                         break;
237                                 }
238                         }
239                         if (!found_match) {
240                                 g_critical("Trigger match '%c' not supported by device.", sep[t]);
241                                 error = TRUE;
242                                 break;
243                         }
244                         /* Make sure this ends up in the right stage, creating
245                          * them as needed. */
246                         while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
247                                 sr_trigger_stage_add(*trigger);
248                         if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
249                                 error = TRUE;
250                                 break;
251                         }
252                 }
253         }
254         g_strfreev(tokens);
255         g_variant_unref(gvar);
256
257         if (error)
258                 sr_trigger_free(*trigger);
259
260         return !error;
261 }
262
263 GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
264 {
265         GHashTable *hash;
266         int i;
267         char **elements, *e;
268
269         if (!arg || !arg[0])
270                 return NULL;
271
272         i = 0;
273         hash = g_hash_table_new_full(g_str_hash, g_str_equal,
274                         g_free, g_free);
275         elements = g_strsplit(arg, ":", 0);
276         if (sep_first)
277                 g_hash_table_insert(hash, g_strdup("sigrok_key"),
278                                 g_strdup(elements[i++]));
279         for (; elements[i]; i++) {
280                 e = strchr(elements[i], '=');
281                 if (!e)
282                         g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
283                 else {
284                         *e++ = '\0';
285                         g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
286                 }
287         }
288         g_strfreev(elements);
289
290         return hash;
291 }
292
293 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
294 {
295         GHashTable *hash;
296         GVariant *gvar;
297         int i;
298         char *s;
299         gboolean b;
300
301         hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
302                         (GDestroyNotify)g_variant_unref);
303         for (i = 0; opts[i]; i++) {
304                 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
305                         continue;
306                 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
307                         gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
308                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
309                                         g_variant_ref_sink(gvar));
310                 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
311                         gvar = g_variant_new_int32(strtoul(s, NULL, 10));
312                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
313                                         g_variant_ref_sink(gvar));
314                 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
315                         gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
316                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
317                                         g_variant_ref_sink(gvar));
318                 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
319                         gvar = g_variant_new_double(strtod(s, NULL));
320                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
321                                         g_variant_ref_sink(gvar));
322                 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
323                         gvar = g_variant_new_string(s);
324                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
325                                         g_variant_ref_sink(gvar));
326                 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
327                         b = TRUE;
328                         if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
329                                 b = FALSE;
330                         } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
331                                 g_critical("Unable to convert '%s' to boolean!", s);
332                         }
333
334                         gvar = g_variant_new_boolean(b);
335                         g_hash_table_insert(hash, g_strdup(opts[i]->id),
336                                         g_variant_ref_sink(gvar));
337                 } else {
338                         g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
339                  }
340         }
341
342         return hash;
343 }
344
345 static char *strcanon(const char *str)
346 {
347         int p0, p1;
348         char *s;
349
350         /* Returns newly allocated string. */
351         s = g_ascii_strdown(str, -1);
352         for (p0 = p1 = 0; str[p0]; p0++) {
353                 if ((s[p0] >= 'a' && s[p0] <= 'z')
354                                 || (s[p0] >= '0' && s[p0] <= '9'))
355                         s[p1++] = s[p0];
356         }
357         s[p1] = '\0';
358
359         return s;
360 }
361
362 int canon_cmp(const char *str1, const char *str2)
363 {
364         int ret;
365         char *s1, *s2;
366
367         s1 = strcanon(str1);
368         s2 = strcanon(str2);
369         ret = g_ascii_strcasecmp(s1, s2);
370         g_free(s2);
371         g_free(s1);
372
373         return ret;
374 }
375
376 /* Convert driver options hash to GSList of struct sr_config. */
377 static GSList *hash_to_hwopt(GHashTable *hash)
378 {
379         struct sr_config *src;
380         GList *gl, *keys;
381         GSList *opts;
382         char *key;
383
384         keys = g_hash_table_get_keys(hash);
385         opts = NULL;
386         for (gl = keys; gl; gl = gl->next) {
387                 key = gl->data;
388                 src = g_malloc(sizeof(struct sr_config));
389                 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
390                         return NULL;
391                 opts = g_slist_append(opts, src);
392         }
393         g_list_free(keys);
394
395         return opts;
396 }
397
398 int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
399 {
400         struct sr_dev_driver **drivers;
401         GHashTable *drvargs;
402         int i;
403         char *drvname;
404
405         drvargs = parse_generic_arg(arg, TRUE);
406
407         drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
408         g_hash_table_remove(drvargs, "sigrok_key");
409         *driver = NULL;
410         drivers = sr_driver_list();
411         for (i = 0; drivers[i]; i++) {
412                 if (strcmp(drivers[i]->name, drvname))
413                         continue;
414                 *driver = drivers[i];
415         }
416         if (!*driver) {
417                 g_critical("Driver %s not found.", drvname);
418                 g_hash_table_destroy(drvargs);
419                 g_free(drvname);
420                 return FALSE;
421         }
422         g_free(drvname);
423         if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
424                 g_critical("Failed to initialize driver.");
425                 g_hash_table_destroy(drvargs);
426                 return FALSE;
427         }
428
429         if (drvopts) {
430                 *drvopts = NULL;
431                 if (g_hash_table_size(drvargs) > 0) {
432                         if (!(*drvopts = hash_to_hwopt(drvargs))) {
433                                 /* Unknown options, already logged. */
434                                 g_hash_table_destroy(drvargs);
435                                 return FALSE;
436                         }
437                 }
438         }
439
440         g_hash_table_destroy(drvargs);
441
442         return TRUE;
443 }
444