[freetds] getopt compatibility

ZIGLIO, Frediano, VF-IT Frediano.Ziglio at vodafone.com
Tue May 27 10:52:39 EDT 2008

> > > > In tsql
> > > > getopt is restarted to read options. It's currently 
> > > > restarted using GNU getopt way, that is setting optind with 0. 
> > > 
> > > Looking at the code, I think the most portable choice -- and 
> > > probably the
> > > least work -- is to refactor tsql.c: call getopt(3) in only 
> > > one place, and
> > > don't reset optind/optreset.  
> > 
> > ehm.... that is only for main argc/argv ??
> So ... are you suggesting I should read the code before 
> offering advice? 
> Don't you understand how much harder that is?  

No, just I don't understand if you supported one of my options or just
add a new one to think about :)

About code getopt is called in three cases:
- main argc/argv parameters
- -o options argument, for instance "-o fq" cause a "go -fq" to be built
and parsed
- go command with options
The portability problem is to be able to parse two set of parameters. My
problem raise from a command line like 

tsql -S sss -U uuu -P ppp -o fhq 

getopt is used to parse { "tsql", "-S", "sss", "-U", "uuu", "-P", "ppp",
"-o", "fhq" } array, then getopt is used again to parse { "go", "-fhq" }

> OK, I see: the "go flags".  
> www.opengroup.org/onlinepubs/ 000095399/functions/getopt.html
> There is no standard for resetting getopt.  
> Do what you think is easiest or most interesting.  I don't 
> see the need --
> unless you want to -- to develop an autoconf test for this.  (A quick
> search didn't turn up anything, and "getopt" doesn't appear in the
> autoconf info file.  It seems it's a void that needs filling.)  

personally I don't like that much autoconf tests and this is quite
difficult to test. I found a test about optreset but nothing about HP-UX
This is why I suggested to write a small parse for our options cause
handling no argument is quite easy to implement (just skip no options
argument and return all letters we need). 

> The quickest and least kludgy answer is probably to write a function:
> 	static int reset_getopt();
> that does what you want, complete with all the #ifs you need.  :-/  
> At least it won't be very big!  

yes, this is also a good idea. Testing for HP-UX from code is not that
difficult and optreset variable can be tested quite easily in autoconf,
something like

static int 
#if hpux
  optind = 1;
  optreset = 1; 
  optind = 0;

> >>> try to detect HP-UX and reset optind with 1?
> ISTR we already test HP/UX symbols somewhere?  

I remember a linker problem with... mumble... gethostaddr  ?? Something
network related.

> > > I think refactoring is best because otherwise you need an 
> > > autoconf test ...
> > > with a bunch of #define macros.  It would look like 
> > > src/tds/net.c.  :-(
> > 
> > I know.. net.c is ugly but it does a lot of system 
> optimizations and all
> > contained in a single file... all others use mainly plain C and some
> > allocation/deallocation function...
> Yeah, a dancing bear: not graceful, but impressive that it 
> does what it
> does.  


yes, I think I'll try to review get_opt_flags


More information about the FreeTDS mailing list