+int parse_trigger_match(char c)
+{
+ int match;
+
+ if (c == '0')
+ match = SR_TRIGGER_ZERO;
+ else if (c == '1')
+ match = SR_TRIGGER_ONE;
+ else if (c == 'r')
+ match = SR_TRIGGER_RISING;
+ else if (c == 'f')
+ match = SR_TRIGGER_FALLING;
+ else if (c == 'e')
+ match = SR_TRIGGER_EDGE;
+ else if (c == 'o')
+ match = SR_TRIGGER_OVER;
+ else if (c == 'u')
+ match = SR_TRIGGER_UNDER;
+ else
+ match = 0;
+
+ return match;
+}
+
+int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
+ struct sr_trigger **trigger)
+{
+ struct sr_channel *ch;
+ struct sr_trigger_stage *stage;
+ GVariant *gvar;
+ GSList *l, *channels;
+ gsize num_matches;
+ gboolean found_match, error;
+ const int32_t *matches;
+ int32_t match;
+ unsigned int j;
+ int t, i;
+ char **tokens, *sep;
+ struct sr_dev_driver *driver;
+
+ driver = sr_dev_inst_driver_get(sdi);
+ channels = sr_dev_inst_channels_get(sdi);
+
+ if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
+ &gvar) != SR_OK) {
+ g_critical("Device doesn't support any triggers.");
+ return FALSE;
+ }
+ matches = g_variant_get_fixed_array(gvar, &num_matches, sizeof(int32_t));
+
+ *trigger = sr_trigger_new(NULL);
+ error = FALSE;
+ tokens = g_strsplit(s, ",", -1);
+ for (i = 0; tokens[i]; i++) {
+ if (!(sep = strchr(tokens[i], '='))) {
+ g_critical("Invalid trigger '%s'.", tokens[i]);
+ error = TRUE;
+ break;
+ }
+ *sep++ = 0;
+ ch = NULL;
+ for (l = channels; l; l = l->next) {
+ ch = l->data;
+ if (ch->enabled && !strcmp(ch->name, tokens[i]))
+ break;
+ ch = NULL;
+ }
+ if (!ch) {
+ g_critical("Invalid channel '%s'.", tokens[i]);
+ error = TRUE;
+ break;
+ }
+ for (t = 0; sep[t]; t++) {
+ if (!(match = parse_trigger_match(sep[t]))) {
+ g_critical("Invalid trigger match '%c'.", sep[t]);
+ error = TRUE;
+ break;
+ }
+ found_match = FALSE;
+ for (j = 0; j < num_matches; j++) {
+ if (matches[j] == match) {
+ found_match = TRUE;
+ break;
+ }
+ }
+ if (!found_match) {
+ g_critical("Trigger match '%c' not supported by device.", sep[t]);
+ error = TRUE;
+ break;
+ }
+ /* Make sure this ends up in the right stage, creating
+ * them as needed. */
+ while (!(stage = g_slist_nth_data((*trigger)->stages, t)))
+ sr_trigger_stage_add(*trigger);
+ if (sr_trigger_match_add(stage, ch, match, 0) != SR_OK) {
+ error = TRUE;
+ break;
+ }
+ }
+ }
+ g_strfreev(tokens);
+ g_variant_unref(gvar);
+
+ if (error)
+ sr_trigger_free(*trigger);
+
+ return !error;
+}
+
+/**
+ * Split an input text into a key and value respectively ('=' separator).
+ *
+ * @param[in] text Writeable copy of the input text, gets modified.
+ * @param[out] key Position of the keyword.
+ * @param[out] val Position of the value.
+ *
+ * TODO In theory the returned key/value locations could be const pointers.
+ * Which even would be preferrable. Unfortunately most call sites deal with
+ * glib hashes, and their insert API seriously lacks the const attribute.
+ * So we drop it here as well to avoid clutter at callers'.
+ */
+static void split_key_value(char *text, char **key, char **val)
+{
+ char *k, *v;
+ char *pos;
+
+ if (key)
+ *key = NULL;
+ if (val)
+ *val = NULL;
+ if (!text || !*text)
+ return;
+
+ k = text;
+ v = NULL;
+ pos = strchr(k, '=');
+ if (pos) {
+ *pos = '\0';
+ v = ++pos;
+ }
+ if (key)
+ *key = k;
+ if (val)
+ *val = v;
+}
+
+/**
+ * Create hash table from colon separated key-value pairs input text.
+ *
+ * Accepts input text as it was specified by users. Splits the colon
+ * separated key-value pairs and creates a hash table from these items.
+ * Optionally supports special forms which are useful for different CLI
+ * features.
+ *
+ * Typical form: <key>=<val>[:<key>=<val>]*
+ * Generic list of key-value pairs, all items being equal. Mere set.
+ *
+ * ID form: <id>[:<key>=<val>]*
+ * First item is not a key-value pair, instead it's an identifier. Used
+ * to specify a protocol decoder, or a device driver, or an input/output
+ * file format, optionally followed by more parameters' values. The ID
+ * part of the input spec is not optional.
+ *
+ * Optional ID: [<sel>=<id>][:<key>=<val>]*
+ * All items are key-value pairs. The first item _may_ be an identifier,
+ * if its key matches a caller specified key name. Otherwise the input
+ * text is the above typical form, a mere list of key-value pairs while
+ * none of them is special.
+ *
+ * @param[in] arg Input text.
+ * @param[in] sep_first Boolean, whether ID form is required.
+ * @param[in] key_first Keyword name if optional ID is applicable.
+ *
+ * @returns A hash table which contains the key/value pairs, or #NULL
+ * when the input is invalid.
+ */
+GHashTable *parse_generic_arg(const char *arg,
+ gboolean sep_first, const char *key_first)