]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
parsers: warn about unknown input/output module option keys
[sigrok-cli.git] / parsers.c
CommitLineData
43e5747a 1/*
630293b4 2 * This file is part of the sigrok-cli project.
43e5747a
UH
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
d486cbdd 20#include <config.h>
43e5747a
UH
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdint.h>
24#include <string.h>
25#include <glib.h>
662a1e27 26#include "sigrok-cli.h"
43e5747a 27
029d73fe 28struct sr_channel *find_channel(GSList *channellist, const char *channelname)
43e5747a 29{
029d73fe 30 struct sr_channel *ch;
497f5362 31 GSList *l;
43e5747a 32
029d73fe
UH
33 ch = NULL;
34 for (l = channellist; l; l = l->next) {
35 ch = l->data;
36 if (!strcmp(ch->name, channelname))
497f5362 37 break;
c2c4a0de 38 }
029d73fe 39 ch = l ? l->data : NULL;
497f5362 40
029d73fe 41 return ch;
497f5362
BV
42}
43
029d73fe 44GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
497f5362 45{
029d73fe 46 struct sr_channel *ch;
c6fa2b2e 47 GSList *channellist, *channels;
497f5362
BV
48 int ret, n, b, e, i;
49 char **tokens, **range, **names, *eptr, str[8];
43e5747a 50
c6fa2b2e
UH
51 channels = sr_dev_inst_channels_get(sdi);
52
029d73fe
UH
53 if (!channelstring || !channelstring[0])
54 /* Use all channels by default. */
c6fa2b2e 55 return g_slist_copy(channels);
497f5362
BV
56
57 ret = SR_OK;
58 range = NULL;
66149c20 59 names = NULL;
029d73fe
UH
60 channellist = NULL;
61 tokens = g_strsplit(channelstring, ",", 0);
43e5747a 62 for (i = 0; tokens[i]; i++) {
497f5362 63 if (tokens[i][0] == '\0') {
029d73fe 64 g_critical("Invalid empty channel.");
497f5362
BV
65 ret = SR_ERR;
66 break;
67 }
43e5747a 68 if (strchr(tokens[i], '-')) {
f0f54487
UH
69 /*
70 * A range of channels in the form a-b. This will only work
029d73fe
UH
71 * if the channels are named as numbers -- so every channel
72 * in the range must exist as a channel name string in the
f0f54487
UH
73 * device.
74 */
43e5747a
UH
75 range = g_strsplit(tokens[i], "-", 2);
76 if (!range[0] || !range[1] || range[2]) {
77 /* Need exactly two arguments. */
029d73fe 78 g_critical("Invalid channel syntax '%s'.", tokens[i]);
497f5362 79 ret = SR_ERR;
8ec20386 80 goto range_fail;
43e5747a
UH
81 }
82
497f5362
BV
83 b = strtol(range[0], &eptr, 10);
84 if (eptr == range[0] || *eptr != '\0') {
029d73fe 85 g_critical("Invalid channel '%s'.", range[0]);
497f5362 86 ret = SR_ERR;
8ec20386 87 goto range_fail;
497f5362 88 }
43e5747a 89 e = strtol(range[1], NULL, 10);
497f5362 90 if (eptr == range[1] || *eptr != '\0') {
029d73fe 91 g_critical("Invalid channel '%s'.", range[1]);
497f5362 92 ret = SR_ERR;
8ec20386 93 goto range_fail;
497f5362
BV
94 }
95 if (b < 0 || b >= e) {
029d73fe 96 g_critical("Invalid channel range '%s'.", tokens[i]);
497f5362 97 ret = SR_ERR;
8ec20386 98 goto range_fail;
43e5747a
UH
99 }
100
101 while (b <= e) {
497f5362
BV
102 n = snprintf(str, 8, "%d", b);
103 if (n < 0 || n > 8) {
029d73fe 104 g_critical("Invalid channel '%d'.", b);
497f5362
BV
105 ret = SR_ERR;
106 break;
107 }
c6fa2b2e 108 ch = find_channel(channels, str);
029d73fe
UH
109 if (!ch) {
110 g_critical("unknown channel '%d'.", b);
497f5362
BV
111 ret = SR_ERR;
112 break;
113 }
029d73fe 114 channellist = g_slist_append(channellist, ch);
43e5747a
UH
115 b++;
116 }
8ec20386
DJ
117range_fail:
118 if (range)
119 g_strfreev(range);
120
497f5362
BV
121 if (ret != SR_OK)
122 break;
43e5747a 123 } else {
497f5362
BV
124 names = g_strsplit(tokens[i], "=", 2);
125 if (!names[0] || (names[1] && names[2])) {
126 /* Need one or two arguments. */
029d73fe 127 g_critical("Invalid channel '%s'.", tokens[i]);
6df458b7 128 g_strfreev(names);
497f5362 129 ret = SR_ERR;
43e5747a
UH
130 break;
131 }
132
c6fa2b2e 133 ch = find_channel(channels, names[0]);
029d73fe
UH
134 if (!ch) {
135 g_critical("unknown channel '%s'.", names[0]);
6df458b7 136 g_strfreev(names);
497f5362
BV
137 ret = SR_ERR;
138 break;
139 }
140 if (names[1]) {
029d73fe
UH
141 /* Rename channel. */
142 g_free(ch->name);
143 ch->name = g_strdup(names[1]);
43e5747a 144 }
029d73fe 145 channellist = g_slist_append(channellist, ch);
8ec20386 146
6df458b7 147 g_strfreev(names);
43e5747a
UH
148 }
149 }
66149c20 150
497f5362 151 if (ret != SR_OK) {
029d73fe
UH
152 g_slist_free(channellist);
153 channellist = NULL;
43e5747a
UH
154 }
155
156 g_strfreev(tokens);
43e5747a 157
029d73fe 158 return channellist;
43e5747a
UH
159}
160
6b27bde4
BV
161int parse_trigger_match(char c)
162{
163 int match;
164
165 if (c == '0')
166 match = SR_TRIGGER_ZERO;
167 else if (c == '1')
168 match = SR_TRIGGER_ONE;
169 else if (c == 'r')
170 match = SR_TRIGGER_RISING;
171 else if (c == 'f')
172 match = SR_TRIGGER_FALLING;
173 else if (c == 'e')
174 match = SR_TRIGGER_EDGE;
175 else if (c == 'o')
176 match = SR_TRIGGER_OVER;
177 else if (c == 'u')
178 match = SR_TRIGGER_UNDER;
179 else
180 match = 0;
181
182 return match;
183}
184
4bf77ec6
BV
185int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
186 struct sr_trigger **trigger)
6b27bde4
BV
187{
188 struct sr_channel *ch;
6b27bde4
BV
189 struct sr_trigger_stage *stage;
190 GVariant *gvar;
c6fa2b2e 191 GSList *l, *channels;
6b27bde4
BV
192 gsize num_matches;
193 gboolean found_match, error;
194 const int32_t *matches;
195 int32_t match;
196 unsigned int j;
197 int t, i;
198 char **tokens, *sep;
c6fa2b2e
UH
199 struct sr_dev_driver *driver;
200
201 driver = sr_dev_inst_driver_get(sdi);
202 channels = sr_dev_inst_channels_get(sdi);
6b27bde4 203
24bd9719 204 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
6b27bde4
BV
205 &gvar) != SR_OK) {
206 g_critical("Device doesn't support any triggers.");
207 return FALSE;
208 }
209 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
210
4bf77ec6 211 *trigger = sr_trigger_new(NULL);
6b27bde4
BV
212 error = FALSE;
213 tokens = g_strsplit(s, ",", -1);
214 for (i = 0; tokens[i]; i++) {
215 if (!(sep = strchr(tokens[i], '='))) {
216 g_critical("Invalid trigger '%s'.", tokens[i]);
217 error = TRUE;
218 break;
219 }
220 *sep++ = 0;
221 ch = NULL;
c6fa2b2e 222 for (l = channels; l; l = l->next) {
6b27bde4
BV
223 ch = l->data;
224 if (ch->enabled && !strcmp(ch->name, tokens[i]))
225 break;
226 ch = NULL;
227 }
228 if (!ch) {
229 g_critical("Invalid channel '%s'.", tokens[i]);
230 error = TRUE;
231 break;
232 }
233 for (t = 0; sep[t]; t++) {
234 if (!(match = parse_trigger_match(sep[t]))) {
235 g_critical("Invalid trigger match '%c'.", sep[t]);
236 error = TRUE;
237 break;
238 }
239 found_match = FALSE;
240 for (j = 0; j < num_matches; j++) {
241 if (matches[j] == match) {
242 found_match = TRUE;
243 break;
244 }
245 }
246 if (!found_match) {
247 g_critical("Trigger match '%c' not supported by device.", sep[t]);
248 error = TRUE;
249 break;
250 }
251 /* Make sure this ends up in the right stage, creating
252 * them as needed. */
4bf77ec6
BV
253 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
254 sr_trigger_stage_add(*trigger);
6b27bde4
BV
255 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
256 error = TRUE;
257 break;
258 }
259 }
260 }
261 g_strfreev(tokens);
262 g_variant_unref(gvar);
263
264 if (error)
4bf77ec6 265 sr_trigger_free(*trigger);
6b27bde4
BV
266
267 return !error;
268}
269
63bb454c 270GHashTable *parse_generic_arg(const char *arg, gboolean sep_first)
43e5747a
UH
271{
272 GHashTable *hash;
273 int i;
274 char **elements, *e;
275
276 if (!arg || !arg[0])
277 return NULL;
278
63bb454c
BV
279 i = 0;
280 hash = g_hash_table_new_full(g_str_hash, g_str_equal,
281 g_free, g_free);
43e5747a 282 elements = g_strsplit(arg, ":", 0);
63bb454c
BV
283 if (sep_first)
284 g_hash_table_insert(hash, g_strdup("sigrok_key"),
285 g_strdup(elements[i++]));
286 for (; elements[i]; i++) {
43e5747a
UH
287 e = strchr(elements[i], '=');
288 if (!e)
289 g_hash_table_insert(hash, g_strdup(elements[i]), NULL);
290 else {
291 *e++ = '\0';
292 g_hash_table_insert(hash, g_strdup(elements[i]), g_strdup(e));
293 }
294 }
295 g_strfreev(elements);
296
297 return hash;
298}
299
cfad6a30
GS
300GSList *check_unknown_keys(const struct sr_option **avail, GHashTable *used)
301{
302 GSList *unknown;
303 GHashTableIter iter;
304 void *key;
305 const char *used_id;
306 size_t avail_idx;
307 const char *avail_id, *found_id;
308
309 /* Collect a list of used but not available keywords. */
310 unknown = NULL;
311 g_hash_table_iter_init(&iter, used);
312 while (g_hash_table_iter_next(&iter, &key, NULL)) {
313 used_id = key;
314 found_id = NULL;
315 for (avail_idx = 0; avail[avail_idx] && avail[avail_idx]->id; avail_idx++) {
316 avail_id = avail[avail_idx]->id;
317 if (strcmp(avail_id, used_id) == 0) {
318 found_id = avail_id;
319 break;
320 }
321 }
322 if (!found_id)
323 unknown = g_slist_append(unknown, g_strdup(used_id));
324 }
325
326 /* Return the list of unknown keywords, or NULL if empty. */
327 return unknown;
328}
329
330gboolean warn_unknown_keys(const struct sr_option **avail, GHashTable *used,
331 const char *caption)
332{
333 GSList *unknown, *l;
334 gboolean had_unknown;
335 const char *s;
336
337 if (!caption || !*caption)
338 caption = "Unknown keyword";
339
340 unknown = check_unknown_keys(avail, used);
341 had_unknown = unknown != NULL;
342 for (l = unknown; l; l = l->next) {
343 s = l->data;
344 g_warning("%s: %s.", caption, s);
345 }
346 g_slist_free_full(unknown, g_free);
347
348 return had_unknown;
349}
350
87e24fed
BV
351GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
352{
353 GHashTable *hash;
354 GVariant *gvar;
355 int i;
356 char *s;
41ef1ae4 357 gboolean b;
87e24fed
BV
358
359 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
360 (GDestroyNotify)g_variant_unref);
361 for (i = 0; opts[i]; i++) {
362 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
363 continue;
364 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
365 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
366 g_hash_table_insert(hash, g_strdup(opts[i]->id),
367 g_variant_ref_sink(gvar));
368 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
369 gvar = g_variant_new_int32(strtoul(s, NULL, 10));
370 g_hash_table_insert(hash, g_strdup(opts[i]->id),
371 g_variant_ref_sink(gvar));
902e368e
BV
372 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
373 gvar = g_variant_new_uint64(strtoul(s, NULL, 10));
374 g_hash_table_insert(hash, g_strdup(opts[i]->id),
375 g_variant_ref_sink(gvar));
87e24fed
BV
376 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
377 gvar = g_variant_new_double(strtod(s, NULL));
378 g_hash_table_insert(hash, g_strdup(opts[i]->id),
379 g_variant_ref_sink(gvar));
380 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
381 gvar = g_variant_new_string(s);
382 g_hash_table_insert(hash, g_strdup(opts[i]->id),
383 g_variant_ref_sink(gvar));
41ef1ae4
JS
384 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
385 b = TRUE;
386 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
387 b = FALSE;
388 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
389 g_critical("Unable to convert '%s' to boolean!", s);
390 }
391
392 gvar = g_variant_new_boolean(b);
393 g_hash_table_insert(hash, g_strdup(opts[i]->id),
394 g_variant_ref_sink(gvar));
87e24fed
BV
395 } else {
396 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
397 }
398 }
399
400 return hash;
401}
402
2be182e6 403static char *strcanon(const char *str)
432de709
BV
404{
405 int p0, p1;
406 char *s;
407
408 /* Returns newly allocated string. */
409 s = g_ascii_strdown(str, -1);
410 for (p0 = p1 = 0; str[p0]; p0++) {
411 if ((s[p0] >= 'a' && s[p0] <= 'z')
412 || (s[p0] >= '0' && s[p0] <= '9'))
413 s[p1++] = s[p0];
414 }
415 s[p1] = '\0';
416
417 return s;
418}
419
d2ee5eea 420int canon_cmp(const char *str1, const char *str2)
432de709
BV
421{
422 int ret;
423 char *s1, *s2;
424
425 s1 = strcanon(str1);
426 s2 = strcanon(str2);
427 ret = g_ascii_strcasecmp(s1, s2);
428 g_free(s2);
429 g_free(s1);
430
431 return ret;
432}
d75c8529
BV
433
434/* Convert driver options hash to GSList of struct sr_config. */
435static GSList *hash_to_hwopt(GHashTable *hash)
436{
437 struct sr_config *src;
438 GList *gl, *keys;
439 GSList *opts;
440 char *key;
441
442 keys = g_hash_table_get_keys(hash);
443 opts = NULL;
444 for (gl = keys; gl; gl = gl->next) {
445 key = gl->data;
446 src = g_malloc(sizeof(struct sr_config));
447 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
448 return NULL;
449 opts = g_slist_append(opts, src);
450 }
451 g_list_free(keys);
452
453 return opts;
454}
455
456int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
457{
458 struct sr_dev_driver **drivers;
459 GHashTable *drvargs;
460 int i;
461 char *drvname;
462
bc5b66d7
JS
463 if (!arg)
464 return FALSE;
465
d75c8529
BV
466 drvargs = parse_generic_arg(arg, TRUE);
467
468 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
469 g_hash_table_remove(drvargs, "sigrok_key");
470 *driver = NULL;
59e421bb 471 drivers = sr_driver_list(sr_ctx);
d75c8529
BV
472 for (i = 0; drivers[i]; i++) {
473 if (strcmp(drivers[i]->name, drvname))
474 continue;
475 *driver = drivers[i];
476 }
477 if (!*driver) {
478 g_critical("Driver %s not found.", drvname);
479 g_hash_table_destroy(drvargs);
480 g_free(drvname);
481 return FALSE;
482 }
483 g_free(drvname);
484 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
485 g_critical("Failed to initialize driver.");
486 g_hash_table_destroy(drvargs);
487 return FALSE;
488 }
489
490 if (drvopts) {
491 *drvopts = NULL;
492 if (g_hash_table_size(drvargs) > 0) {
493 if (!(*drvopts = hash_to_hwopt(drvargs))) {
494 /* Unknown options, already logged. */
495 g_hash_table_destroy(drvargs);
496 return FALSE;
497 }
498 }
499 }
500
501 g_hash_table_destroy(drvargs);
502
503 return TRUE;
504}