Skip to content

Commit

Permalink
cli: more flexible generic arg parser
Browse files Browse the repository at this point in the history
  • Loading branch information
biot committed Aug 3, 2012
1 parent 943d0c0 commit 63bb454
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions sigrok-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.");
Expand Down
2 changes: 1 addition & 1 deletion sigrok-cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 63bb454

Please sign in to comment.