2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2011 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
26 #include "sigrok-cli.h"
28 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
30 struct sr_channel *ch;
34 for (l = channellist; l; l = l->next) {
36 if (!strcmp(ch->name, channelname))
39 ch = l ? l->data : NULL;
44 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
46 struct sr_channel *ch;
47 GSList *channellist, *channels;
49 char **tokens, **range, **names, *eptr, str[8];
51 channels = sr_dev_inst_channels_get(sdi);
53 if (!channelstring || !channelstring[0])
54 /* Use all channels by default. */
55 return g_slist_copy(channels);
61 tokens = g_strsplit(channelstring, ",", 0);
62 for (i = 0; tokens[i]; i++) {
63 if (tokens[i][0] == '\0') {
64 g_critical("Invalid empty channel.");
68 if (strchr(tokens[i], '-')) {
70 * A range of channels in the form a-b. This will only work
71 * if the channels are named as numbers -- so every channel
72 * in the range must exist as a channel name string in the
75 range = g_strsplit(tokens[i], "-", 2);
76 if (!range[0] || !range[1] || range[2]) {
77 /* Need exactly two arguments. */
78 g_critical("Invalid channel syntax '%s'.", tokens[i]);
83 b = strtol(range[0], &eptr, 10);
84 if (eptr == range[0] || *eptr != '\0') {
85 g_critical("Invalid channel '%s'.", range[0]);
89 e = strtol(range[1], NULL, 10);
90 if (eptr == range[1] || *eptr != '\0') {
91 g_critical("Invalid channel '%s'.", range[1]);
95 if (b < 0 || b >= e) {
96 g_critical("Invalid channel range '%s'.", tokens[i]);
102 n = snprintf(str, 8, "%d", b);
103 if (n < 0 || n > 8) {
104 g_critical("Invalid channel '%d'.", b);
108 ch = find_channel(channels, str);
110 g_critical("unknown channel '%d'.", b);
114 channellist = g_slist_append(channellist, ch);
124 names = g_strsplit(tokens[i], "=", 2);
125 if (!names[0] || (names[1] && names[2])) {
126 /* Need one or two arguments. */
127 g_critical("Invalid channel '%s'.", tokens[i]);
133 ch = find_channel(channels, names[0]);
135 g_critical("unknown channel '%s'.", names[0]);
141 /* Rename channel. */
142 sr_dev_channel_name_set(ch, names[1]);
144 channellist = g_slist_append(channellist, ch);
151 g_slist_free(channellist);
160 int parse_trigger_match(char c)
165 match = SR_TRIGGER_ZERO;
167 match = SR_TRIGGER_ONE;
169 match = SR_TRIGGER_RISING;
171 match = SR_TRIGGER_FALLING;
173 match = SR_TRIGGER_EDGE;
175 match = SR_TRIGGER_OVER;
177 match = SR_TRIGGER_UNDER;
184 int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
185 struct sr_trigger **trigger)
187 struct sr_channel *ch;
188 struct sr_trigger_stage *stage;
190 GSList *l, *channels;
192 gboolean found_match, error;
193 const int32_t *matches;
198 struct sr_dev_driver *driver;
200 driver = sr_dev_inst_driver_get(sdi);
201 channels = sr_dev_inst_channels_get(sdi);
203 if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
205 g_critical("Device doesn't support any triggers.");
208 matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
210 *trigger = sr_trigger_new(NULL);
212 tokens = g_strsplit(s, ",", -1);
213 for (i = 0; tokens[i]; i++) {
214 if (!(sep = strchr(tokens[i], '='))) {
215 g_critical("Invalid trigger '%s'.", tokens[i]);
221 for (l = channels; l; l = l->next) {
223 if (ch->enabled && !strcmp(ch->name, tokens[i]))
228 g_critical("Invalid channel '%s'.", tokens[i]);
232 for (t = 0; sep[t]; t++) {
233 if (!(match = parse_trigger_match(sep[t]))) {
234 g_critical("Invalid trigger match '%c'.", sep[t]);
239 for (j = 0; j < num_matches; j++) {
240 if (matches[j] == match) {
246 g_critical("Trigger match '%c' not supported by device.", sep[t]);
250 /* Make sure this ends up in the right stage, creating
252 while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
253 sr_trigger_stage_add(*trigger);
254 if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
261 g_variant_unref(gvar);
264 sr_trigger_free(*trigger);
270 * Split an input text into a key and value respectively ('=' separator).
272 * @param[in] text Writeable copy of the input text, gets modified.
273 * @param[out] key Position of the keyword.
274 * @param[out] val Position of the value.
276 * TODO In theory the returned key/value locations could be const pointers.
277 * Which even would be preferrable. Unfortunately most call sites deal with
278 * glib hashes, and their insert API seriously lacks the const attribute.
279 * So we drop it here as well to avoid clutter at callers'.
281 static void split_key_value(char *text, char **key, char **val)
295 pos = strchr(k, '=');
307 * Create hash table from colon separated key-value pairs input text.
309 * Accepts input text as it was specified by users. Splits the colon
310 * separated key-value pairs and creates a hash table from these items.
311 * Optionally supports special forms which are useful for different CLI
314 * Typical form: <key>=<val>[:<key>=<val>]*
315 * Generic list of key-value pairs, all items being equal. Mere set.
317 * ID form: <id>[:<key>=<val>]*
318 * First item is not a key-value pair, instead it's an identifier. Used
319 * to specify a protocol decoder, or a device driver, or an input/output
320 * file format, optionally followed by more parameters' values. The ID
321 * part of the input spec is not optional.
323 * Optional ID: [<sel>=<id>][:<key>=<val>]*
324 * All items are key-value pairs. The first item _may_ be an identifier,
325 * if its key matches a caller specified key name. Otherwise the input
326 * text is the above typical form, a mere list of key-value pairs while
327 * none of them is special.
329 * @param[in] arg Input text.
330 * @param[in] sep_first Boolean, whether ID form is required.
331 * @param[in] key_first Keyword name if optional ID is applicable.
333 * @returns A hash table which contains the key/value pairs, or #NULL
334 * when the input is invalid.
336 GHashTable *parse_generic_arg(const char *arg,
337 gboolean sep_first, const char *key_first)
346 if (key_first && !key_first[0])
349 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
350 elements = g_strsplit(arg, ":", 0);
353 k = g_strdup("sigrok_key");
354 v = g_strdup(elements[i++]);
355 g_hash_table_insert(hash, k, v);
356 } else if (key_first) {
357 split_key_value(elements[i], &k, &v);
358 if (g_ascii_strcasecmp(k, key_first) == 0) {
363 g_hash_table_insert(hash, k, v);
366 for (; elements[i]; i++) {
369 split_key_value(elements[i], &k, &v);
371 v = v ? g_strdup(v) : NULL;
372 g_hash_table_insert(hash, k, v);
374 g_strfreev(elements);
379 GSList *check_unknown_keys(const struct sr_option **avail, GHashTable *used)
386 const char *avail_id, *found_id;
388 /* Collect a list of used but not available keywords. */
390 g_hash_table_iter_init(&iter, used);
391 while (g_hash_table_iter_next(&iter, &key, NULL)) {
394 for (avail_idx = 0; avail[avail_idx] && avail[avail_idx]->id; avail_idx++) {
395 avail_id = avail[avail_idx]->id;
396 if (strcmp(avail_id, used_id) == 0) {
402 unknown = g_slist_append(unknown, g_strdup(used_id));
405 /* Return the list of unknown keywords, or NULL if empty. */
409 gboolean warn_unknown_keys(const struct sr_option **avail, GHashTable *used,
413 gboolean had_unknown;
416 if (!caption || !*caption)
417 caption = "Unknown keyword";
419 unknown = check_unknown_keys(avail, used);
420 had_unknown = unknown != NULL;
421 for (l = unknown; l; l = l->next) {
423 g_warning("%s: %s.", caption, s);
425 g_slist_free_full(unknown, g_free);
430 GHashTable *generic_arg_to_opt(const struct sr_option **opts, GHashTable *genargs)
438 hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
439 (GDestroyNotify)g_variant_unref);
440 for (i = 0; opts[i]; i++) {
441 if (!(s = g_hash_table_lookup(genargs, opts[i]->id)))
443 if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT32)) {
444 gvar = g_variant_new_uint32(strtoul(s, NULL, 10));
445 g_hash_table_insert(hash, g_strdup(opts[i]->id),
446 g_variant_ref_sink(gvar));
447 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_INT32)) {
448 gvar = g_variant_new_int32(strtol(s, NULL, 10));
449 g_hash_table_insert(hash, g_strdup(opts[i]->id),
450 g_variant_ref_sink(gvar));
451 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_UINT64)) {
452 gvar = g_variant_new_uint64(strtoull(s, NULL, 10));
453 g_hash_table_insert(hash, g_strdup(opts[i]->id),
454 g_variant_ref_sink(gvar));
455 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_DOUBLE)) {
456 gvar = g_variant_new_double(strtod(s, NULL));
457 g_hash_table_insert(hash, g_strdup(opts[i]->id),
458 g_variant_ref_sink(gvar));
459 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_STRING)) {
460 gvar = g_variant_new_string(s);
461 g_hash_table_insert(hash, g_strdup(opts[i]->id),
462 g_variant_ref_sink(gvar));
463 } else if (g_variant_is_of_type(opts[i]->def, G_VARIANT_TYPE_BOOLEAN)) {
465 if (0 == strcmp(s, "false") || 0 == strcmp(s, "no")) {
467 } else if (!(0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))) {
468 g_critical("Unable to convert '%s' to boolean!", s);
471 gvar = g_variant_new_boolean(b);
472 g_hash_table_insert(hash, g_strdup(opts[i]->id),
473 g_variant_ref_sink(gvar));
475 g_critical("Don't know GVariant type for option '%s'!", opts[i]->id);
482 static char *strcanon(const char *str)
487 /* Returns newly allocated string. */
488 s = g_ascii_strdown(str, -1);
489 for (p0 = p1 = 0; str[p0]; p0++) {
490 if ((s[p0] >= 'a' && s[p0] <= 'z')
491 || (s[p0] >= '0' && s[p0] <= '9'))
499 int canon_cmp(const char *str1, const char *str2)
506 ret = g_ascii_strcasecmp(s1, s2);
513 /* Convert driver options hash to GSList of struct sr_config. */
514 static GSList *hash_to_hwopt(GHashTable *hash)
516 struct sr_config *src;
521 keys = g_hash_table_get_keys(hash);
523 for (gl = keys; gl; gl = gl->next) {
525 src = g_malloc(sizeof(struct sr_config));
526 if (opt_to_gvar(key, g_hash_table_lookup(hash, key), src) != 0)
528 opts = g_slist_append(opts, src);
535 int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
537 struct sr_dev_driver **drivers;
545 drvargs = parse_generic_arg(arg, TRUE, NULL);
547 drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
548 g_hash_table_remove(drvargs, "sigrok_key");
550 drivers = sr_driver_list(sr_ctx);
551 for (i = 0; drivers[i]; i++) {
552 if (strcmp(drivers[i]->name, drvname))
554 *driver = drivers[i];
557 g_critical("Driver %s not found.", drvname);
558 g_hash_table_destroy(drvargs);
563 if (sr_driver_init(sr_ctx, *driver) != SR_OK) {
564 g_critical("Failed to initialize driver.");
565 g_hash_table_destroy(drvargs);
571 if (g_hash_table_size(drvargs) > 0) {
572 if (!(*drvopts = hash_to_hwopt(drvargs))) {
573 /* Unknown options, already logged. */
574 g_hash_table_destroy(drvargs);
580 g_hash_table_destroy(drvargs);