mailing list of musl libc
 help / color / mirror / code / Atom feed
* Re: better compatibility with bsd getopt()
  2012-09-30 20:30 better compatibility with bsd getopt() Daniel Cegiełka
@ 2012-09-30 20:28 ` Rich Felker
  2012-09-30 21:02 ` John Spencer
  1 sibling, 0 replies; 12+ messages in thread
From: Rich Felker @ 2012-09-30 20:28 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 10:30:56PM +0200, Daniel Cegiełka wrote:
> http://www.openbsd.org/cgi-bin/man.cgi?query=getopt&sektion=3

As submitted, this patch makes it nonconforming; optreset is a name
reserved for use by the application. It might can be fixed with weak
symbols, but isn't there another fairly standard way to reset parsing
(setting optind to 0 or something)? Also, I think some other vars need
to be reset in this case...

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

* better compatibility with bsd getopt()
@ 2012-09-30 20:30 Daniel Cegiełka
  2012-09-30 20:28 ` Rich Felker
  2012-09-30 21:02 ` John Spencer
  0 siblings, 2 replies; 12+ messages in thread
From: Daniel Cegiełka @ 2012-09-30 20:30 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 79 bytes --]

http://www.openbsd.org/cgi-bin/man.cgi?query=getopt&sektion=3

regards,
Daniel

[-- Attachment #2: musl_getopt_optreset.diff --]
[-- Type: application/octet-stream, Size: 1000 bytes --]

diff -urN musl.orig/include/getopt.h musl/include/getopt.h
--- musl.orig/include/getopt.h	Sun Sep 30 20:08:45 2012
+++ musl/include/getopt.h	Sun Sep 30 20:09:09 2012
@@ -7,7 +7,7 @@
 
 int getopt(int, char * const [], const char *);
 extern char *optarg;
-extern int optind, opterr, optopt;
+extern int optind, opterr, optopt, optreset;
 
 struct option
 {
diff -urN musl.orig/src/misc/getopt.c musl/src/misc/getopt.c
--- musl.orig/src/misc/getopt.c	Sun Sep 30 20:08:45 2012
+++ musl/src/misc/getopt.c	Sun Sep 30 20:13:14 2012
@@ -5,7 +5,7 @@
 #include <stdlib.h>
 
 char *optarg;
-int optind=1, opterr=1, optopt;
+int optind=1, opterr=1, optopt, optreset=0;
 static int optpos;
 
 int getopt(int argc, char * const argv[], const char *optstring)
@@ -15,6 +15,10 @@
 	int k, l;
 	char *optchar;
 
+	if (optreset) {
+		optreset = 0;
+		optind = 1;
+	}
 	if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
 		return -1;
 	if (argv[optind][1] == '-' && !argv[optind][2])

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:02 ` John Spencer
@ 2012-09-30 21:00   ` Rich Felker
  2012-09-30 21:18     ` Daniel Cegiełka
  2012-09-30 21:06   ` Daniel Cegiełka
  1 sibling, 1 reply; 12+ messages in thread
From: Rich Felker @ 2012-09-30 21:00 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 11:02:26PM +0200, John Spencer wrote:
> On 09/30/2012 10:30 PM, Daniel Cegiełka wrote:
> >http://www.openbsd.org/cgi-bin/man.cgi?query=getopt&sektion=3
> >
> >regards,
> >Daniel
> an application that relies on non-posix getopt behaviour should come
> with a configure check and use an alternate impl if the getopt
> detected is not compatible.
> almost any program does so already, but for those that don't (like
> iptables) you can use https://github.com/rofl0r/gnu-getopt (tarball
> available on sabotage mirror, or in downloads section of the page).
> this bundles the netbsd getopt into a standalone library that can
> easily be linked to programs.

If the only need is option resetting, I think we can handle that
without any cost, and avoid linking in duplicate code to meet
applications' needs.

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 20:30 better compatibility with bsd getopt() Daniel Cegiełka
  2012-09-30 20:28 ` Rich Felker
@ 2012-09-30 21:02 ` John Spencer
  2012-09-30 21:00   ` Rich Felker
  2012-09-30 21:06   ` Daniel Cegiełka
  1 sibling, 2 replies; 12+ messages in thread
From: John Spencer @ 2012-09-30 21:02 UTC (permalink / raw)
  To: musl

On 09/30/2012 10:30 PM, Daniel Cegiełka wrote:
> http://www.openbsd.org/cgi-bin/man.cgi?query=getopt&sektion=3
>
> regards,
> Daniel
an application that relies on non-posix getopt behaviour should come 
with a configure check and use an alternate impl if the getopt detected 
is not compatible.
almost any program does so already, but for those that don't (like 
iptables) you can use https://github.com/rofl0r/gnu-getopt (tarball 
available on sabotage mirror, or in downloads section of the page).
this bundles the netbsd getopt into a standalone library that can easily 
be linked to programs.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:02 ` John Spencer
  2012-09-30 21:00   ` Rich Felker
@ 2012-09-30 21:06   ` Daniel Cegiełka
  2012-09-30 21:22     ` John Spencer
  1 sibling, 1 reply; 12+ messages in thread
From: Daniel Cegiełka @ 2012-09-30 21:06 UTC (permalink / raw)
  To: musl

2012/9/30 John Spencer <maillist-musl@barfooze.de>:


> an application that relies on non-posix getopt behaviour should come with a
> configure check and use an alternate impl if the getopt detected is not
> compatible.
> almost any program does so already, but for those that don't (like iptables)
> you can use https://github.com/rofl0r/gnu-getopt (tarball available on
> sabotage mirror, or in downloads section of the page).
> this bundles the netbsd getopt into a standalone library that can easily be
> linked to programs.

Without optreset support some BSD programs will not be able to
compile, like patch:

	if (!Argc)
		return;
	optreset = optind = 1;
	while ((ch = getopt_long(Argc, Argv, options, longopts, NULL)) != -1) {
		switch (ch) {
		case 'b':

http://www.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/patch.c?rev=1.50;content-type=text%2Fplain

Your example shows that the gnu-getopt is also a problem. It may be
better to keep musl clean and solve these problems outside of musl. So
a good solution for us is to use a getopt() wrapper for problematic apps,
like gnu_getopt() or bsd_getopt().

Daniel


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:18     ` Daniel Cegiełka
@ 2012-09-30 21:13       ` Rich Felker
  2012-09-30 21:32         ` Daniel Cegiełka
  0 siblings, 1 reply; 12+ messages in thread
From: Rich Felker @ 2012-09-30 21:13 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 11:18:33PM +0200, Daniel Cegiełka wrote:
> >
> > If the only need is option resetting, I think we can handle that
> > without any cost, and avoid linking in duplicate code to meet
> > applications' needs.
> >
> > Rich
> 
> OpenBSD solves gnu reset option in this way:
> 
> 	/*
> 	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
> 	 * XXX using optreset.  Work around this braindamage.
> 	 */
> 	if (optind == 0)
> 		optind = optreset = 1;

Except the BSD way is the braindamaged one because it violates the
namespace. The GNU way simply defines something that was previously
undefined by the standard without affecting the namespace.

If needed, I think it's possible to support both, with some weak
symbol hacks...

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:00   ` Rich Felker
@ 2012-09-30 21:18     ` Daniel Cegiełka
  2012-09-30 21:13       ` Rich Felker
  0 siblings, 1 reply; 12+ messages in thread
From: Daniel Cegiełka @ 2012-09-30 21:18 UTC (permalink / raw)
  To: musl

>
> If the only need is option resetting, I think we can handle that
> without any cost, and avoid linking in duplicate code to meet
> applications' needs.
>
> Rich

OpenBSD solves gnu reset option in this way:

	/*
	 * XXX Some GNU programs (like cvs) set optind to 0 instead of
	 * XXX using optreset.  Work around this braindamage.
	 */
	if (optind == 0)
		optind = optreset = 1;

http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/getopt_long.c?rev=1.25;content-type=text%2Fplain

So using the gnu reset (optind == 0) and BSD optreset should not be so
difficult.

Daniel


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:06   ` Daniel Cegiełka
@ 2012-09-30 21:22     ` John Spencer
  0 siblings, 0 replies; 12+ messages in thread
From: John Spencer @ 2012-09-30 21:22 UTC (permalink / raw)
  To: musl

On 09/30/2012 11:06 PM, Daniel Cegiełka wrote:
> 2012/9/30 John Spencer<maillist-musl@barfooze.de>:
>
>
>> an application that relies on non-posix getopt behaviour should come with a
>> configure check and use an alternate impl if the getopt detected is not
>> compatible.
>> almost any program does so already, but for those that don't (like iptables)
>> you can use https://github.com/rofl0r/gnu-getopt (tarball available on
>> sabotage mirror, or in downloads section of the page).
>> this bundles the netbsd getopt into a standalone library that can easily be
>> linked to programs.
> Without optreset support some BSD programs will not be able to
> compile, like patch:

gnu-getopt (which is in fact netbsd getopt) includes support for 
optreset, and is compatible with gnu and bsd.

from the readme:

gnu getopt compatibility package for musl

usage:
make; make prefix= DESTDIR= install

then to compile a package, add to
CFLAGS="-Dgetopt=gnu_getopt -Dgetopt_long=gnu_getopt_long 
-Dgetopt_long_only=gnu_getopt_long_only"
LDFLAGS="-lgnu_getopt"



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:13       ` Rich Felker
@ 2012-09-30 21:32         ` Daniel Cegiełka
  2012-09-30 23:53           ` Rich Felker
  2012-10-01  0:03           ` Rich Felker
  0 siblings, 2 replies; 12+ messages in thread
From: Daniel Cegiełka @ 2012-09-30 21:32 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 711 bytes --]

2012/9/30 Rich Felker <dalias@aerifal.cx>:

>> OpenBSD solves gnu reset option in this way:
>>
>>       /*
>>        * XXX Some GNU programs (like cvs) set optind to 0 instead of
>>        * XXX using optreset.  Work around this braindamage.
>>        */
>>       if (optind == 0)
>>               optind = optreset = 1;
>
> Except the BSD way is the braindamaged one because it violates the
> namespace. The GNU way simply defines something that was previously
> undefined by the standard without affecting the namespace.
>
> If needed, I think it's possible to support both, with some weak
> symbol hacks...

sample solution for gnu/bsd getopt() - of course this patch needs this
weak symbol hacks...

Daniel

[-- Attachment #2: musl_gnu_bsd_getopt_reset.diff --]
[-- Type: application/octet-stream, Size: 1041 bytes --]

diff -urN musl.orig/include/getopt.h musl/include/getopt.h
--- musl.orig/include/getopt.h	Sun Sep 30 21:20:38 2012
+++ musl/include/getopt.h	Sun Sep 30 21:23:14 2012
@@ -7,7 +7,7 @@
 
 int getopt(int, char * const [], const char *);
 extern char *optarg;
-extern int optind, opterr, optopt;
+extern int optind, opterr, optopt, optreset;
 
 struct option
 {
diff -urN musl.orig/src/misc/getopt.c musl/src/misc/getopt.c
--- musl.orig/src/misc/getopt.c	Sun Sep 30 21:20:38 2012
+++ musl/src/misc/getopt.c	Sun Sep 30 21:22:57 2012
@@ -5,7 +5,7 @@
 #include <stdlib.h>
 
 char *optarg;
-int optind=1, opterr=1, optopt;
+int optind=1, opterr=1, optopt, optreset=0;
 static int optpos;
 
 int getopt(int argc, char * const argv[], const char *optstring)
@@ -15,6 +15,12 @@
 	int k, l;
 	char *optchar;
 
+	if (!optind)
+		optind = optreset = 1;
+	if (optreset) {
+		optreset = 0;
+		optind = 1;
+	}
 	if (optind >= argc || !argv[optind] || argv[optind][0] != '-' || !argv[optind][1])
 		return -1;
 	if (argv[optind][1] == '-' && !argv[optind][2])

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:32         ` Daniel Cegiełka
@ 2012-09-30 23:53           ` Rich Felker
  2012-09-30 23:54             ` Rich Felker
  2012-10-01  0:03           ` Rich Felker
  1 sibling, 1 reply; 12+ messages in thread
From: Rich Felker @ 2012-09-30 23:53 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 11:32:44PM +0200, Daniel Cegiełka wrote:
> 2012/9/30 Rich Felker <dalias@aerifal.cx>:
> 
> >> OpenBSD solves gnu reset option in this way:
> >>
> >>       /*
> >>        * XXX Some GNU programs (like cvs) set optind to 0 instead of
> >>        * XXX using optreset.  Work around this braindamage.
> >>        */
> >>       if (optind == 0)
> >>               optind = optreset = 1;
> >
> > Except the BSD way is the braindamaged one because it violates the
> > namespace. The GNU way simply defines something that was previously
> > undefined by the standard without affecting the namespace.
> >
> > If needed, I think it's possible to support both, with some weak
> > symbol hacks...
> 
> sample solution for gnu/bsd getopt() - of course this patch needs this
> weak symbol hacks...

One more question... is optreset supposed to be declared in a header?
Or is the application responsible for declaring it?

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 23:53           ` Rich Felker
@ 2012-09-30 23:54             ` Rich Felker
  0 siblings, 0 replies; 12+ messages in thread
From: Rich Felker @ 2012-09-30 23:54 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 07:53:22PM -0400, Rich Felker wrote:
> On Sun, Sep 30, 2012 at 11:32:44PM +0200, Daniel Cegiełka wrote:
> > 2012/9/30 Rich Felker <dalias@aerifal.cx>:
> > 
> > >> OpenBSD solves gnu reset option in this way:
> > >>
> > >>       /*
> > >>        * XXX Some GNU programs (like cvs) set optind to 0 instead of
> > >>        * XXX using optreset.  Work around this braindamage.
> > >>        */
> > >>       if (optind == 0)
> > >>               optind = optreset = 1;
> > >
> > > Except the BSD way is the braindamaged one because it violates the
> > > namespace. The GNU way simply defines something that was previously
> > > undefined by the standard without affecting the namespace.
> > >
> > > If needed, I think it's possible to support both, with some weak
> > > symbol hacks...
> > 
> > sample solution for gnu/bsd getopt() - of course this patch needs this
> > weak symbol hacks...
> 
> One more question... is optreset supposed to be declared in a header?
> Or is the application responsible for declaring it?

Never mind -- I missed that you had it in the header this time. :)

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: better compatibility with bsd getopt()
  2012-09-30 21:32         ` Daniel Cegiełka
  2012-09-30 23:53           ` Rich Felker
@ 2012-10-01  0:03           ` Rich Felker
  1 sibling, 0 replies; 12+ messages in thread
From: Rich Felker @ 2012-10-01  0:03 UTC (permalink / raw)
  To: musl

On Sun, Sep 30, 2012 at 11:32:44PM +0200, Daniel Cegiełka wrote:
> 2012/9/30 Rich Felker <dalias@aerifal.cx>:
> 
> >> OpenBSD solves gnu reset option in this way:
> >>
> >>       /*
> >>        * XXX Some GNU programs (like cvs) set optind to 0 instead of
> >>        * XXX using optreset.  Work around this braindamage.
> >>        */
> >>       if (optind == 0)
> >>               optind = optreset = 1;
> >
> > Except the BSD way is the braindamaged one because it violates the
> > namespace. The GNU way simply defines something that was previously
> > undefined by the standard without affecting the namespace.
> >
> > If needed, I think it's possible to support both, with some weak
> > symbol hacks...
> 
> sample solution for gnu/bsd getopt() - of course this patch needs this
> weak symbol hacks...

I've committed a version with several additional issues addressed too.

Rich


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2012-10-01  0:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-30 20:30 better compatibility with bsd getopt() Daniel Cegiełka
2012-09-30 20:28 ` Rich Felker
2012-09-30 21:02 ` John Spencer
2012-09-30 21:00   ` Rich Felker
2012-09-30 21:18     ` Daniel Cegiełka
2012-09-30 21:13       ` Rich Felker
2012-09-30 21:32         ` Daniel Cegiełka
2012-09-30 23:53           ` Rich Felker
2012-09-30 23:54             ` Rich Felker
2012-10-01  0:03           ` Rich Felker
2012-09-30 21:06   ` Daniel Cegiełka
2012-09-30 21:22     ` John Spencer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).