From: Bert Vermeulen Date: Sun, 15 Jul 2012 23:03:30 +0000 (+0200) Subject: cli: more flexible generic arg parser X-Git-Tag: sigrok-cli-0.4.0~96 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=63bb454cc1cec4a3cfdb7bcf5cb7ea37ea50e93a;p=sigrok-cli.git cli: more flexible generic arg parser --- diff --git a/parsers.c b/parsers.c index 659bbad..65a2653 100644 --- a/parsers.c +++ b/parsers.c @@ -96,7 +96,7 @@ char **parse_probestring(int max_probes, const char *probestring) return probelist; } -GHashTable *parse_generic_arg(const char *arg) +GHashTable *parse_generic_arg(const char *arg, gboolean sep_first) { GHashTable *hash; int i; @@ -105,10 +105,14 @@ GHashTable *parse_generic_arg(const char *arg) if (!arg || !arg[0]) return NULL; - hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + i = 0; + hash = g_hash_table_new_full(g_str_hash, g_str_equal, + g_free, g_free); elements = g_strsplit(arg, ":", 0); - g_hash_table_insert(hash, g_strdup("sigrok_key"), g_strdup(elements[0])); - for (i = 1; elements[i]; i++) { + if (sep_first) + g_hash_table_insert(hash, g_strdup("sigrok_key"), + g_strdup(elements[i++])); + for (; elements[i]; i++) { e = strchr(elements[i], '='); if (!e) g_hash_table_insert(hash, g_strdup(elements[i]), NULL); diff --git a/sigrok-cli.c b/sigrok-cli.c index a0e4f08..e31df93 100644 --- a/sigrok-cli.c +++ b/sigrok-cli.c @@ -700,7 +700,7 @@ static int register_pds(struct sr_dev *dev, const char *pdstring) pd_opthash = NULL; pdtokens = g_strsplit(pdstring, ",", 0); for (pdtok = pdtokens; *pdtok; pdtok++) { - if (!(pd_opthash = parse_generic_arg(*pdtok))) { + if (!(pd_opthash = parse_generic_arg(*pdtok, TRUE))) { g_critical("Invalid protocol decoder option '%s'.", *pdtok); goto err_out; } @@ -868,7 +868,7 @@ int setup_output_format(void) default_output_format = TRUE; } - fmtargs = parse_generic_arg(opt_output_format); + fmtargs = parse_generic_arg(opt_output_format, TRUE); fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); if (!fmtspec) { g_critical("Invalid output format."); diff --git a/sigrok-cli.h b/sigrok-cli.h index 75a2947..979b008 100644 --- a/sigrok-cli.h +++ b/sigrok-cli.h @@ -26,7 +26,7 @@ int num_real_devs(void); /* parsers.c */ char **parse_probestring(int max_probes, const char *probestring); char **sr_parse_triggerstring(struct sr_dev *dev, const char *triggerstring); -GHashTable *parse_generic_arg(const char *arg); +GHashTable *parse_generic_arg(const char *arg, gboolean sep_first); struct sr_dev *parse_devstring(const char *devstring); uint64_t sr_parse_timestring(const char *timestring); char *strcanon(const char *str);