After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 723160 - GOption: add strict posix mode
GOption: add strict posix mode
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
: 738659 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2014-01-28 11:24 UTC by Allison Karlitskaya (desrt)
Modified: 2014-10-20 12:36 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
GOption: add strict posix mode (5.24 KB, patch)
2014-01-28 11:24 UTC, Allison Karlitskaya (desrt)
reviewed Details | Review
GOption: add strict posix mode (7.93 KB, patch)
2014-10-15 18:14 UTC, Lars Karlitski
committed Details | Review
GOption: stop calling getopt() (1.97 KB, patch)
2014-10-20 12:31 UTC, Allison Karlitskaya (desrt)
accepted-commit_now Details | Review
GOption: stop calling getopt() (2.89 KB, patch)
2014-10-20 12:34 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Allison Karlitskaya (desrt) 2014-01-28 11:24:14 UTC
Add a "posixly correct" mode to GOption to stop parsing arguments as
soon as the first non-option argument is encountered.

We determine the default value on the basis of duplicating the behaviour
of the system getopt() implementation (which we directly check the
behaviour of at runtime).  On GNU systems this allows the user to modify
our behaviour using POSIXLY_CORRECT.

The user can change the value by g_option_context_set_posixly_correct(),
which might be useful for some usecases of GOptionContext (as mentioned
in the doc string of this new function).
Comment 1 Allison Karlitskaya (desrt) 2014-01-28 11:24:15 UTC
Created attachment 267396 [details] [review]
GOption: add strict posix mode
Comment 2 Matthias Clasen 2014-01-29 00:49:08 UTC
Review of attachment 267396 [details] [review]:

I like the idea

::: glib/goption.c
@@ +375,3 @@
+     * no arguments then we are in strict POSIX mode.
+     */
+    context->strict_posix = getopt (3, (char **) argv, "a") != 'a';

Wouldn't it be more straightforward to check POSIXLY_CORRECT directly ?

@@ +508,3 @@
+ * @context: a #GoptionContext
+ *
+ * Sets strict posix mode.

We should be consistent in capitalizing POSIX.

@@ +538,3 @@
+void
+g_option_context_set_posixly_correct (GOptionContext *context,
+                                      gboolean        posixly_correct)

I like the idea of introducing this mode, but I don't like the name very much. Could we call it 'strict' or 'incremental' or some other more descriptive name ?
Comment 3 Allison Karlitskaya (desrt) 2014-01-29 10:18:09 UTC
(In reply to comment #2)
> Wouldn't it be more straightforward to check POSIXLY_CORRECT directly ?

Although this would give the correct behaviour on Linux systems, I think we may want to copy the strict POSIX behaviour that getopt() has on BSD systems.

Matching the behaviour of the system getopt seems like it will get us a sane default in all cases.

> We should be consistent in capitalizing POSIX.

Sure.

> I like the idea of introducing this mode, but I don't like the name very much.
> Could we call it 'strict' or 'incremental' or some other more descriptive name

I think I agree -- I only used this name on the API because it matches the variable.  Internally I call it "strict posix" so how about _set_strict_posix()?
Comment 4 Matthias Clasen 2014-01-29 19:38:00 UTC
(In reply to comment #3)
> (In reply to comment #2)
> > Wouldn't it be more straightforward to check POSIXLY_CORRECT directly ?
> 
> Although this would give the correct behaviour on Linux systems, I think we may
> want to copy the strict POSIX behaviour that getopt() has on BSD systems.
> 
> Matching the behaviour of the system getopt seems like it will get us a sane
> default in all cases.

ok
 
> > I like the idea of introducing this mode, but I don't like the name very much.
> > Could we call it 'strict' or 'incremental' or some other more descriptive name
> 
> I think I agree -- I only used this name on the API because it matches the
> variable.  Internally I call it "strict posix" so how about
> _set_strict_posix()?

Fine with me
Comment 5 Lars Karlitski 2014-10-15 18:14:29 UTC
Created attachment 288614 [details] [review]
GOption: add strict posix mode

I like the idea as well and fixed a couple of things in the patch:

- make it compile (wrong function signature)
- rename the functions to _strict_posix and capitalized all occurences of
  POSIX, as discussed
- reset optind to 1 before calling getopt(), otherwise every call after the
  first one will fail (option-context tests failed, for example)
- update AVAILABLE_IN
- add a test case
Comment 6 Matthias Clasen 2014-10-15 19:21:17 UTC
Review of attachment 288614 [details] [review]:

looks good to me, otherwise

::: glib/goption.c
@@ +524,3 @@
+ * parsing).
+ *
+ * Since: 2.40

needs to be 2.44 now (already ?!)
Comment 7 Lars Karlitski 2014-10-15 21:41:05 UTC
Attachment 288614 [details] pushed as ae52ab3 - GOption: add strict posix mode
Comment 8 Ting-Wei Lan 2014-10-17 05:56:12 UTC
This commit breaks g-ir-compiler on FreeBSD. The getopt check causes GOption to work in strict posix mode by default, so the command

g-ir-compiler --includedir=. --includedir=./gir --includedir=. --includedir=. --includedir=. gir/DBus-1.0.gir -o gir/DBus-1.0.typelib

used to generate .typelib file when building gobject-introspection will cause the compiled file to be written to stdout instead of the specified file.
Comment 9 Ting-Wei Lan 2014-10-17 06:24:12 UTC
I found it also breaks other programs, including glib-compile-resources and valac.
Comment 10 Allison Karlitskaya (desrt) 2014-10-17 09:04:34 UTC
I guess this isn't too surprising.

We have two real options here:

 1) directly inspect POSIXLY_CORRECT for ourselves, irrespective of the platform
     (also solves bug 738659)

 2) never automatically enable strict-posix mode

and the unrealistic option:

 3) fix the world
Comment 11 Ting-Wei Lan 2014-10-17 10:29:45 UTC
I think we can choose 1). This behavior is expected by existing GNU users.
Comment 12 Lars Karlitski 2014-10-17 11:23:45 UTC
Option (1) won't fix the problem. The build would still be broken on platforms (or for users) that have POSIXLY_CORRECT defined.

I think we should just go for (2).
Comment 13 Allison Karlitskaya (desrt) 2014-10-20 12:31:54 UTC
Created attachment 288929 [details] [review]
GOption: stop calling getopt()

We called getopt() to try to find out of the platform on which we are
running defaults to strict POSIX-style argument handling (ie: flags
following the first filename are considered as further filenames rather
than flags).

This is the default case on BSDs, for example.  It is also the case on
GNU systems with the POSIXLY_CORRECT environment variable set.

Unfortunately many of our tools rely on being able to accept commandline
arguments in the non-strict ordering and the code for making these calls
is spread widely (for example in MAkefile fragments invoking some of our
build tools).

For this reason we need to revert the getopt() check and only enable
strict POSIX mode in the case that the application explicitly opts into
it using the _set_strict_posix() API.

This also fixs a failure to build on Windows due to missing getopt().
Comment 14 Lars Karlitski 2014-10-20 12:33:49 UTC
Review of attachment 288929 [details] [review]:

Thanks. Please also update the doc string which still says that the default is system-dependant.
Comment 15 Allison Karlitskaya (desrt) 2014-10-20 12:34:26 UTC
Created attachment 288930 [details] [review]
GOption: stop calling getopt()

Updated with a couple of quick fixes after a review from larsu.
Comment 16 Allison Karlitskaya (desrt) 2014-10-20 12:35:25 UTC
Comment on attachment 288930 [details] [review]
GOption: stop calling getopt()

Attachment 288930 [details] pushed as 817f82d - GOption: stop calling getopt()
Comment 17 Allison Karlitskaya (desrt) 2014-10-20 12:36:11 UTC
*** Bug 738659 has been marked as a duplicate of this bug. ***