Skip to content

Commit

Permalink
--channels: Allow dash in name when renaming channels.
Browse files Browse the repository at this point in the history
Arguments to the "--channels" option are checked for the range
separator (a dash) first before handling a possible rename.

This makes it impossible to rename a channel to anything that
includes a dash.

So instead check for a rename (equals character) first and handle
a possible range later.

It makes no sense to rename a range of channels so report an error
if there is a dash character in the part preceding the equals sign,
except if a channel with this exact name exists.
  • Loading branch information
thierer committed Jun 11, 2020
1 parent 0171a4a commit 360f4b6
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions parsers.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,42 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
ret = SR_ERR;
break;
}
if (strchr(tokens[i], '-')) {

names = g_strsplit(tokens[i], "=", 2);
if (!names[0] || (names[1] && names[2])) {
/* Need one or two arguments. */
g_critical("Invalid channel '%s'.", tokens[i]);
g_strfreev(names);
ret = SR_ERR;
break;
}

/* Check first if a channel with the given name already exists, as it
* might contain a dash from a rename in an earlier option.
*/
ch = find_channel(channels, names[0]);
if (ch) {
if (names[1]) {
/* Rename channel. */
g_free(ch->name);
ch->name = g_strdup(names[1]);
}
channellist = g_slist_append(channellist, ch);
} else if (strchr(names[0], '-')) {
/*
* A range of channels in the form a-b. This will only work
* if the channels are named as numbers -- so every channel
* in the range must exist as a channel name string in the
* device.
*/
range = g_strsplit(tokens[i], "-", 2);
if (names[1]) {
/* Channel range syntax not allowed when renaming. */
g_critical("Invalid rename for channel range '%s'.", names[0]);
ret = SR_ERR;
goto range_fail;
}

range = g_strsplit(names[0], "-", 2);
if (!range[0] || !range[1] || range[2]) {
/* Need exactly two arguments. */
g_critical("Invalid channel syntax '%s'.", tokens[i]);
Expand All @@ -93,7 +121,7 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
goto range_fail;
}
if (b < 0 || b >= e) {
g_critical("Invalid channel range '%s'.", tokens[i]);
g_critical("Invalid channel range '%s'.", names[0]);
ret = SR_ERR;
goto range_fail;
}
Expand All @@ -117,35 +145,15 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
range_fail:
if (range)
g_strfreev(range);
} else { /* Channel name not found and not a channel range. */
g_critical("unknown channel '%s'.", names[0]);
ret = SR_ERR;
}

if (ret != SR_OK)
break;
} else {
names = g_strsplit(tokens[i], "=", 2);
if (!names[0] || (names[1] && names[2])) {
/* Need one or two arguments. */
g_critical("Invalid channel '%s'.", tokens[i]);
g_strfreev(names);
ret = SR_ERR;
break;
}

ch = find_channel(channels, names[0]);
if (!ch) {
g_critical("unknown channel '%s'.", names[0]);
g_strfreev(names);
ret = SR_ERR;
break;
}
if (names[1]) {
/* Rename channel. */
g_free(ch->name);
ch->name = g_strdup(names[1]);
}
channellist = g_slist_append(channellist, ch);
g_strfreev(names);

g_strfreev(names);
}
if (ret != SR_OK)
break;
}

if (ret != SR_OK) {
Expand Down

0 comments on commit 360f4b6

Please sign in to comment.