Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various changes to improve parsing of the "--channels" option #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

thierer
Copy link

@thierer thierer commented Jun 11, 2020

  1. Right now, it isn't possible to rename a channel to anything that includes a dash ("-") character, as the parsing code then tries to handle the option as a channel range, which it is not. This makes it impossible to for example correctly name a usb data "D-" signal. So check if the option string contains an equals character (for a channel rename) first.

  2. As it is, the whole "channel range" feature isn't very useful, as it only works for purely numeric channel names and most (all?) drivers seem to name their channels with a common, non-numeric prefix (like D0, D1, ...). So allow a common prefix in a channel range.

  3. The third patch isn't strictly necessary, it only removes two checks that I consider useless but right now don't do any harm (that could change as one of the checks seems to rely on undocumented glib behaviour, see the commit message).

@thierer thierer force-pushed the improve_parsing_of_channels_option branch from 44dd34b to 7a0e13d Compare June 12, 2020 09:07
@thierer
Copy link
Author

thierer commented Jun 12, 2020

At first I had the (now third) patch that removes the g_strsplit() checks as the first change, but reordered them to make it easier to only apply the first two patches. I just found that the wording of the commit message still reflected the situation before the other two patches so I just force-pushed an amended commit message for the third patch.

@gsigh
Copy link
Contributor

gsigh commented Jul 27, 2020

There are devices which use all-number channel names. Don't assume that all devices have to start with letters just because the popular ones do. :-)

Are you certain about the split related part of the PR? I was to understand that the number 2 was the maximum number of splits, which is differrent from the number of resulting "words", which again differs from the number of array items when the terminating NULL is counted.

@thierer
Copy link
Author

thierer commented Jul 31, 2020

There are devices which use all-number channel names. Don't assume that all devices have to start with letters just because the popular ones do. :-)

I'm not sure if your comment implies that you think my patch breaks ranges for numeric channel names? I don't think that's the case?

Are you certain about the split related part of the PR? I was to understand that the number 2 was the maximum number of splits, which is differrent from the number of resulting "words", which again differs from the number of array items when the terminating NULL is counted.

The docs for g_strsplit() state

gchar **g_strsplit (const gchar *string, const gchar *delimiter, gint max_tokens)

Splits a string into a maximum of max_tokens pieces, using the given delimiter. If max_tokens is reached, the remainder of string is appended to the last token.

And this program

#include <stdio.h>
#include <glib.h>

void main() {
  gchar **p = g_strsplit("ab|cd|e", "|", 2);
  printf("p[0]==%s p[1]==%s p[2]==%p\n", p[0], p[1], p[2]);
  g_strfreev(p);
}

has this output

p[0]==ab p[1]==cd|e p[2]==(nil)

So the number is the maximum of what you refer to as "words" to return (plus an additional NULL to mark the end of the vector), not the number of splits (which I assume you would expect to return 3 strings in this example, right?).

@gsigh
Copy link
Contributor

gsigh commented Aug 2, 2020

Must have confused the glib split call with Python's, sorry. Will have another look later.

thierer added 3 commits July 31, 2024 12:35
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.
Selecting a range of channels only works if the channel names are
purely numeric, but not if they have a common, non-numeric
prefix (eg as in "D0", "D1", ..).

This patch allows to use such channels in ranges by checking for
a common prefix. The syntax to for example select channels "D2"
through to "D5" is "D2-5".

Also adjust the manpage to include the new syntax and fix the
example that actually didn't work as the fx2lafw driver uses
"D0", "D1", ... as channel names.
1. !names[0] could only be true if tokens[i] is an empty string,
   but we already checked at the beginning of the for() loop that
   this is not the case.

   "(names[1] && names[2])" can also never be true, as
   g_strsplit(x, 2) returns a NULL-terminated pointer array with
   either one or a maximum of two non-NULL strings, so either
   names[1] or names[2] is always == NULL.

2. Likewise, !range[0] could only be true if names[0] is an empty
   string. But we already asserted that it contains at least a dash
   character.

   So both range[0] and range[1] at least contain pointers to
   empty strings. g_strsplit(x, 2) returns a NULL-terminated
   pointer array with a maximum of (and in this case, exactly)
   two non-NULL strings, which means range[2] is always == NULL.

   As a result, "!range[0] || !range[1] || range[2]" can never be
   true.
@thierer thierer force-pushed the improve_parsing_of_channels_option branch from 7a0e13d to d0d2754 Compare July 31, 2024 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants