[sane-devel] [PATCH 1/2] use getopt()

Olaf Meeuwissen paddy-hack at member.fsf.org
Sun Sep 13 05:18:14 UTC 2015

Matteo Croce writes:

> Sorry about that, I'll fix in a new version, also what do you think
> about a nicer help string like:
> "Usage: %s [-a username] [-d n] [-s n] [-h]\n"

Your suggestion made me take a second look and I noticed that I totally
missed the fact that the usage message treats the options as mutually
exclusive with optional arguments.  On top of that, the manual page has
a slightly different idea of the usage yet.  Your code changes would
accept any number of the options and require arguments for three of

  Code's usage: [ -a [ username ] | -d [ n ] | -s [ n ] ] | -h
  Manual page : [ -a [ username ] | -d [ n ] | -s [ n ] | -h ]
  getopt      : [-a username] [-d n] [-s n] [-h]

An incompatible change like that of course cannot go in.  I liked the
explicit passing of username to run_standalone() and an optional socket
to run_inetd() though.  It better to have the argv handling all in one
function.  If you could split that from the getopt() part, that'd be
nice.  AFAIK, getopt() does not support mutually exclusive options so
you'd have to add checking for that after the command-line parsing.

Let's have a look at what the code on master accepts

 - a plain `saned`
 - `saned -a [username]`.  Any trailing arguments are silently ignored.
   The optional username is used by run_standalone or run_inetd.  For an
   invocation like `saned -a -d`, the username would be `-d`.  Oops!
 - `sane -d[n]`.  Again any trailing arguments are silently ignored.  An
   unquoted space between the -d and the optional n will cause n to be
 - for `saned -s[n]` the situation is the same as `saned -d[n]`
 - `saned -h` and `saned --help`

and on OS/2 it also accepts

 - `saned socketnumber`

but only if there are no other command-line arguments.
So that's different again from what the usage message and manual page
claim.  Yuck!  A usage message (and manual page synopsis) like

  saned [-h | --help]
  saned -a [username]
  saned -d[n]
  saned -s[n]
  saned socketnumber   # On OS/2 only

may be better.

> The main issue is that it seems that under OS/2 the socket handle is
> passed as an extra argument, what to do?
> Handle the extra argument only if HAVE_OS2_H is defined?

I'd a run_inetd(char *sock) everywhere and pass NULL if not on OS/2.
That would get rid of the #ifdef'd function signature.  In main() you
can then use something like

    else {
      char *sock = NULL;
  #ifdef HAVE_OS2_H
      if (argc == 2) sock = argv[1];

> BTW is OS/2 still used? The Sane OS/2 mailing list is gone, and the
> latest binary was built on 2012.

I have no idea but as long as supporting it is as simple as above there
no reason to drop it.  Is there?

Hope this helps,
Olaf Meeuwissen, LPIC-2            FSF Associate Member since 2004-01-27
Support Free Software               Support the Free Software Foundation
https://my.fsf.org/donate                        https://my.fsf.org/join

More information about the sane-devel mailing list