]> sigrok.org Git - sigrok-cli.git/blame - parsers.c
doc: update sigrok-cli(1) for channel assignment to decoder inputs
[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
a2dbd848
GS
28struct sr_channel *find_channel(GSList *channellist, const char *channelname,
29 gboolean exact_case)
43e5747a 30{
029d73fe 31 struct sr_channel *ch;
497f5362 32 GSList *l;
43e5747a 33
029d73fe
UH
34 ch = NULL;
35 for (l = channellist; l; l = l->next) {
36 ch = l->data;
a2dbd848
GS
37 if (exact_case) {
38 if (strcmp(ch->name, channelname) == 0)
39 break;
40 } else {
41 if (g_ascii_strcasecmp(ch->name, channelname) == 0)
42 break;
43 }
c2c4a0de 44 }
029d73fe 45 ch = l ? l->data : NULL;
497f5362 46
029d73fe 47 return ch;
497f5362
BV
48}
49
029d73fe 50GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
497f5362 51{
029d73fe 52 struct sr_channel *ch;
c6fa2b2e 53 GSList *channellist, *channels;
497f5362
BV
54 int ret, n, b, e, i;
55 char **tokens, **range, **names, *eptr, str[8];
43e5747a 56
c6fa2b2e
UH
57 channels = sr_dev_inst_channels_get(sdi);
58
029d73fe
UH
59 if (!channelstring || !channelstring[0])
60 /* Use all channels by default. */
c6fa2b2e 61 return g_slist_copy(channels);
497f5362
BV
62
63 ret = SR_OK;
64 range = NULL;
66149c20 65 names = NULL;
029d73fe
UH
66 channellist = NULL;
67 tokens = g_strsplit(channelstring, ",", 0);
43e5747a 68 for (i = 0; tokens[i]; i++) {
497f5362 69 if (tokens[i][0] == '\0') {
029d73fe 70 g_critical("Invalid empty channel.");
497f5362
BV
71 ret = SR_ERR;
72 break;
73 }
43e5747a 74 if (strchr(tokens[i], '-')) {
f0f54487
UH
75 /*
76 * A range of channels in the form a-b. This will only work
029d73fe
UH
77 * if the channels are named as numbers -- so every channel
78 * in the range must exist as a channel name string in the
f0f54487
UH
79 * device.
80 */
43e5747a
UH
81 range = g_strsplit(tokens[i], "-", 2);
82 if (!range[0] || !range[1] || range[2]) {
83 /* Need exactly two arguments. */
029d73fe 84 g_critical("Invalid channel syntax '%s'.", tokens[i]);
497f5362 85 ret = SR_ERR;
8ec20386 86 goto range_fail;
43e5747a
UH
87 }
88
497f5362
BV
89 b = strtol(range[0], &eptr, 10);
90 if (eptr == range[0] || *eptr != '\0') {
029d73fe 91 g_critical("Invalid channel '%s'.", range[0]);
497f5362 92 ret = SR_ERR;
8ec20386 93 goto range_fail;
497f5362 94 }
43e5747a 95 e = strtol(range[1], NULL, 10);
497f5362 96 if (eptr == range[1] || *eptr != '\0') {
029d73fe 97 g_critical("Invalid channel '%s'.", range[1]);
497f5362 98 ret = SR_ERR;
8ec20386 99 goto range_fail;
497f5362
BV
100 }
101 if (b < 0 || b >= e) {
029d73fe 102 g_critical("Invalid channel range '%s'.", tokens[i]);
497f5362 103 ret = SR_ERR;
8ec20386 104 goto range_fail;
43e5747a
UH
105 }
106
107 while (b <= e) {
497f5362
BV
108 n = snprintf(str, 8, "%d", b);
109 if (n < 0 || n > 8) {
029d73fe 110 g_critical("Invalid channel '%d'.", b);
497f5362
BV
111 ret = SR_ERR;
112 break;
113 }
a2dbd848 114 ch = find_channel(channels, str, TRUE);
029d73fe
UH
115 if (!ch) {
116 g_critical("unknown channel '%d'.", b);
497f5362
BV
117 ret = SR_ERR;
118 break;
119 }
029d73fe 120 channellist = g_slist_append(channellist, ch);
43e5747a
UH
121 b++;
122 }
8ec20386
DJ
123range_fail:
124 if (range)
125 g_strfreev(range);
126
497f5362
BV
127 if (ret != SR_OK)
128 break;
43e5747a 129 } else {
497f5362
BV
130 names = g_strsplit(tokens[i], "=", 2);
131 if (!names[0] || (names[1] && names[2])) {
132 /* Need one or two arguments. */
029d73fe 133 g_critical("Invalid channel '%s'.", tokens[i]);
6df458b7 134 g_strfreev(names);
497f5362 135 ret = SR_ERR;
43e5747a
UH
136 break;
137 }
138
a2dbd848 139 ch = find_channel(channels, names[0], TRUE);
029d73fe
UH
140 if (!ch) {
141 g_critical("unknown channel '%s'.", names[0]);
6df458b7 142 g_strfreev(names);
497f5362
BV
143 ret = SR_ERR;
144 break;
145 }
146 if (names[1]) {
029d73fe 147 /* Rename channel. */
99595de1 148 sr_dev_channel_name_set(ch, names[1]);
43e5747a 149 }
029d73fe 150 channellist = g_slist_append(channellist, ch);
8ec20386 151
6df458b7 152 g_strfreev(names);
43e5747a
UH
153 }
154 }
66149c20 155
497f5362 156 if (ret != SR_OK) {
029d73fe
UH
157 g_slist_free(channellist);
158 channellist = NULL;
43e5747a
UH
159 }
160
161 g_strfreev(tokens);
43e5747a 162
029d73fe 163 return channellist;
43e5747a
UH
164}
165
6b27bde4
BV
166int parse_trigger_match(char c)
167{
168 int match;
169
170 if (c == '0')
171 match = SR_TRIGGER_ZERO;
172 else if (c == '1')
173 match = SR_TRIGGER_ONE;
174 else if (c == 'r')
175 match = SR_TRIGGER_RISING;
176 else if (c == 'f')
177 match = SR_TRIGGER_FALLING;
178 else if (c == 'e')
179 match = SR_TRIGGER_EDGE;
180 else if (c == 'o')
181 match = SR_TRIGGER_OVER;
182 else if (c == 'u')
183 match = SR_TRIGGER_UNDER;
184 else
185 match = 0;
186
187 return match;
188}
189
4bf77ec6
BV
190int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
191 struct sr_trigger **trigger)
6b27bde4
BV
192{
193 struct sr_channel *ch;
6b27bde4
BV
194 struct sr_trigger_stage *stage;
195 GVariant *gvar;
c6fa2b2e 196 GSList *l, *channels;
6b27bde4
BV
197 gsize num_matches;
198 gboolean found_match, error;
199 const int32_t *matches;
200 int32_t match;
201 unsigned int j;
202 int t, i;
203 char **tokens, *sep;
c6fa2b2e
UH
204 struct sr_dev_driver *driver;
205
206 driver = sr_dev_inst_driver_get(sdi);
207 channels = sr_dev_inst_channels_get(sdi);
6b27bde4 208
24bd9719 209 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
6b27bde4
BV
210 &gvar) != SR_OK) {
211 g_critical("Device doesn't support any triggers.");
212 return FALSE;
213 }
214 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
215
4bf77ec6 216 *trigger = sr_trigger_new(NULL);
6b27bde4
BV
217 error = FALSE;
218 tokens = g_strsplit(s, ",", -1);
219 for (i = 0; tokens[i]; i++) {
220 if (!(sep = strchr(tokens[i], '='))) {
221 g_critical("Invalid trigger '%s'.", tokens[i]);
222 error = TRUE;
223 break;
224 }
225 *sep++ = 0;
226 ch = NULL;
c6fa2b2e 227 for (l = channels; l; l = l->next) {
6b27bde4
BV
228 ch = l->data;
229 if (ch->enabled && !strcmp(ch->name, tokens[i]))
230 break;
231 ch = NULL;
232 }
233 if (!ch) {
234 g_critical("Invalid channel '%s'.", tokens[i]);
235 error = TRUE;
236 break;
237 }
238 for (t = 0; sep[t]; t++) {
239 if (!(match = parse_trigger_match(sep[t]))) {
240 g_critical("Invalid trigger match '%c'.", sep[t]);
241 error = TRUE;
242 break;
243 }
244 found_match = FALSE;
245 for (j = 0; j < num_matches; j++) {
246 if (matches[j] == match) {
247 found_match = TRUE;
248 break;
249 }
250 }
251 if (!found_match) {
252 g_critical("Trigger match '%c' not supported by device.", sep[t]);
253 error = TRUE;
254 break;
255 }
256 /* Make sure this ends up in the right stage, creating
257 * them as needed. */
4bf77ec6
BV
258 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
259 sr_trigger_stage_add(*trigger);
6b27bde4
BV
260 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
261 error = TRUE;
262 break;
263 }
264 }
265 }
266 g_strfreev(tokens);
267 g_variant_unref(gvar);
268
269 if (error)
4bf77ec6 270 sr_trigger_free(*trigger);
6b27bde4
BV
271
272 return !error;
273}
274
c9c30f53
GS
275/**
276 * Split an input text into a key and value respectively ('=' separator).
277 *
278 * @param[in] text Writeable copy of the input text, gets modified.
279 * @param[out] key Position of the keyword.
280 * @param[out] val Position of the value.
281 *
282 * TODO In theory the returned key/value locations could be const pointers.
283 * Which even would be preferrable. Unfortunately most call sites deal with
284 * glib hashes, and their insert API seriously lacks the const attribute.
285 * So we drop it here as well to avoid clutter at callers'.
286 */
287static void split_key_value(char *text, char **key, char **val)
288{
289 char *k, *v;
290 char *pos;
291
292 if (key)
293 *key = NULL;
294 if (val)
295 *val = NULL;
296 if (!text || !*text)
297 return;
298
299 k = text;
300 v = NULL;
301 pos = strchr(k, '=');
302 if (pos) {
303 *pos = '\0';
304 v = ++pos;
305 }
306 if (key)
307 *key = k;
308 if (val)
309 *val = v;
310}
311
cad0cba6
GS
312/**
313 * Create hash table from colon separated key-value pairs input text.
314 *
315 * Accepts input text as it was specified by users. Splits the colon
316 * separated key-value pairs and creates a hash table from these items.
317 * Optionally supports special forms which are useful for different CLI
318 * features.
319 *
320 * Typical form: <key>=<val>[:<key>=<val>]*
321 * Generic list of key-value pairs, all items being equal. Mere set.
322 *
323 * ID form: <id>[:<key>=<val>]*
324 * First item is not a key-value pair, instead it's an identifier. Used
325 * to specify a protocol decoder, or a device driver, or an input/output
326 * file format, optionally followed by more parameters' values. The ID
327 * part of the input spec is not optional.
328 *
329 * Optional ID: [<sel>=<id>][:<key>=<val>]*
330 * All items are key-value pairs. The first item _may_ be an identifier,
331 * if its key matches a caller specified key name. Otherwise the input
332 * text is the above typical form, a mere list of key-value pairs while
333 * none of them is special.
334 *
335 * @param[in] arg Input text.
336 * @param[in] sep_first Boolean, whether ID form is required.
337 * @param[in] key_first Keyword name if optional ID is applicable.
338 *
339 * @returns A hash table which contains the key/value pairs, or #NULL
340 * when the input is invalid.
341 */
342GHashTable *parse_generic_arg(const char *arg,
343 gboolean sep_first, const char *key_first)
43e5747a
UH
344{
345 GHashTable *hash;
c9c30f53 346 char **elements;
43e5747a 347 int i;
c9c30f53 348 char *k, *v;
43e5747a
UH
349
350 if (!arg || !arg[0])
351 return NULL;
c9c30f53
GS
352 if (key_first && !key_first[0])
353 key_first = NULL;
43e5747a 354
c9c30f53 355 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
43e5747a 356 elements = g_strsplit(arg, ":", 0);
c9c30f53 357 i = 0;
cad0cba6 358 if (sep_first) {
c9c30f53
GS
359 k = g_strdup("sigrok_key");
360 v = g_strdup(elements[i++]);
361 g_hash_table_insert(hash, k, v);
362 } else if (key_first) {
363 split_key_value(elements[i], &k, &v);
364 if (g_ascii_strcasecmp(k, key_first) == 0) {
365 k = "sigrok_key";
cad0cba6 366 }
c9c30f53
GS
367 k = g_strdup(k);
368 v = g_strdup(v);
369 g_hash_table_insert(hash, k, v);
370 i++;
cad0cba6 371 }
63bb454c 372 for (; elements[i]; i++) {
af9fa8c5
GS
373 if (!elements[i][0])
374 continue;
c9c30f53
GS
375 split_key_value(elements[i], &k, &v);
376 k = g_strdup(k);
377 v = v ? g_strdup(v) : NULL;
378 g_hash_table_insert(hash, k, v);
43e5747a
UH
379 }
380 g_strfreev(elements);
381
382 return hash;
383}
384
cfad6a30
GS
385GSList *check_unknown_keys(const struct sr_option **avail, GHashTable *used)
386{
387 GSList *unknown;
388 GHashTableIter iter;
389 void *key;
390 const char *used_id;
391 size_t avail_idx;
392 const char *avail_id, *found_id;
393
394 /* Collect a list of used but not available keywords. */
395 unknown = NULL;
396 g_hash_table_iter_init(&iter, used);
397 while (g_hash_table_iter_next(&iter, &key, NULL)) {
398 used_id = key;
399 found_id = NULL;
400 for (avail_idx = 0; avail[avail_idx] && avail[avail_idx]->id; avail_idx++) {
401 avail_id = avail[avail_idx]->id;
402 if (strcmp(avail_id, used_id) == 0) {
403 found_id = avail_id;
404 break;
405 }
406 }
407 if (!found_id)
408 unknown = g_slist_append(unknown, g_strdup(used_id));
409 }
410
411 /* Return the list of unknown keywords, or NULL if empty. */
412 return unknown;
413}
414
415gboolean warn_unknown_keys(const struct sr_option **avail, GHashTable *used,
416 const char *caption)
417{
418 GSList *unknown, *l;
419 gboolean had_unknown;
420 const char *s;
421
422 if (!caption || !*caption)
423 caption = "Unknown keyword";
424
425 unknown = check_unknown_keys(avail, used);
426 had_unknown = unknown != NULL;
427 for (l = unknown; l; l = l->next) {
428 s = l->data;
429 g_warning("%s: %s.", caption, s);
430 }
431 g_slist_free_full(unknown, g_free);
432
433 return had_unknown;
434}
435
87e24fed
BV
436GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
437{
438 GHashTable *hash;
439 GVariant *gvar;
440 int i;
441 char *s;
41ef1ae4 442 gboolean b;
87e24fed
BV
443
444 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
445 (GDestroyNotify)g_variant_unref);
446 for (i = 0; opts[i]; i++) {
447 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
448 continue;
449 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
450 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
451 g_hash_table_insert(hash, g_strdup(opts[i]->id),
452 g_variant_ref_sink(gvar));
453 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
e8e8b5e1 454 gvar = g_variant_new_int32(strtol(s, NULL, 10));
87e24fed
BV
455 g_hash_table_insert(hash, g_strdup(opts[i]->id),
456 g_variant_ref_sink(gvar));
902e368e 457 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
e8e8b5e1 458 gvar = g_variant_new_uint64(strtoull(s, NULL, 10));
902e368e
BV
459 g_hash_table_insert(hash, g_strdup(opts[i]->id),
460 g_variant_ref_sink(gvar));
87e24fed
BV
461 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
462 gvar = g_variant_new_double(strtod(s, NULL));
463 g_hash_table_insert(hash, g_strdup(opts[i]->id),
464 g_variant_ref_sink(gvar));
465 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
466 gvar = g_variant_new_string(s);
467 g_hash_table_insert(hash, g_strdup(opts[i]->id),
468 g_variant_ref_sink(gvar));
41ef1ae4
JS
469 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
470 b = TRUE;
471 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
472 b = FALSE;
473 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
474 g_critical("Unable to convert '%s' to boolean!", s);
475 }
476
477 gvar = g_variant_new_boolean(b);
478 g_hash_table_insert(hash, g_strdup(opts[i]->id),
479 g_variant_ref_sink(gvar));
87e24fed
BV
480 } else {
481 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
482 }
483 }
484
485 return hash;
486}
487
2be182e6 488static char *strcanon(const char *str)
432de709
BV
489{
490 int p0, p1;
491 char *s;
492
493 /* Returns newly allocated string. */
494 s = g_ascii_strdown(str, -1);
495 for (p0 = p1 = 0; str[p0]; p0++) {
496 if ((s[p0] >= 'a' && s[p0] <= 'z')
497 || (s[p0] >= '0' && s[p0] <= '9'))
498 s[p1++] = s[p0];
499 }
500 s[p1] = '\0';
501
502 return s;
503}
504
d2ee5eea 505int canon_cmp(const char *str1, const char *str2)
432de709
BV
506{
507 int ret;
508 char *s1, *s2;
509
510 s1 = strcanon(str1);
511 s2 = strcanon(str2);
512 ret = g_ascii_strcasecmp(s1, s2);
513 g_free(s2);
514 g_free(s1);
515
516 return ret;
517}
d75c8529
BV
518
519/* Convert driver options hash to GSList of struct sr_config. */
520static GSList *hash_to_hwopt(GHashTable *hash)
521{
522 struct sr_config *src;
523 GList *gl, *keys;
524 GSList *opts;
525 char *key;
526
527 keys = g_hash_table_get_keys(hash);
528 opts = NULL;
529 for (gl = keys; gl; gl = gl->next) {
530 key = gl->data;
531 src = g_malloc(sizeof(struct sr_config));
532 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
533 return NULL;
534 opts = g_slist_append(opts, src);
535 }
536 g_list_free(keys);
537
538 return opts;
539}
540
541int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
542{
543 struct sr_dev_driver **drivers;
544 GHashTable *drvargs;
545 int i;
546 char *drvname;
547
bc5b66d7
JS
548 if (!arg)
549 return FALSE;
550
cad0cba6 551 drvargs = parse_generic_arg(arg, TRUE, NULL);
d75c8529
BV
552
553 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
554 g_hash_table_remove(drvargs, "sigrok_key");
555 *driver = NULL;
59e421bb 556 drivers = sr_driver_list(sr_ctx);
d75c8529
BV
557 for (i = 0; drivers[i]; i++) {
558 if (strcmp(drivers[i]->name, drvname))
559 continue;
560 *driver = drivers[i];
561 }
562 if (!*driver) {
563 g_critical("Driver %s not found.", drvname);
564 g_hash_table_destroy(drvargs);
565 g_free(drvname);
566 return FALSE;
567 }
568 g_free(drvname);
569 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
570 g_critical("Failed to initialize driver.");
571 g_hash_table_destroy(drvargs);
572 return FALSE;
573 }
574
575 if (drvopts) {
576 *drvopts = NULL;
577 if (g_hash_table_size(drvargs) > 0) {
578 if (!(*drvopts = hash_to_hwopt(drvargs))) {
579 /* Unknown options, already logged. */
580 g_hash_table_destroy(drvargs);
581 return FALSE;
582 }
583 }
584 }
585
586 g_hash_table_destroy(drvargs);
587
588 return TRUE;
589}