]> Savannah Git Hosting - gnulib.git/commitdiff
getopt: tidy up _getopt_initialize a bit
authorZack Weinberg <zackw@panix.com>
Thu, 6 Apr 2017 18:14:14 +0000 (11:14 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 6 Apr 2017 22:42:03 +0000 (15:42 -0700)
_getopt_data.__posixly_correct is completely redundant to
_getopt_data.__ordering, and some work that logically belongs in
_getopt_initialize was being done by _getopt_internal_r, making the
code harder to understand.

As a side effect, getenv will no longer be called if the first
character of the options string is '+' or '-', which is probably a
Good Thing.  (Perhaps we should have a flag character that
specifically asks for the permutation behavior?)

* lib/getopt_int.h (_getopt_data): Remove __posixly_correct field.
* lib/getopt.c (_getopt_internal_r): Move some initialization code...
(_getopt_initialize): ...here. Don't set d->__posixly_correct.

ChangeLog
lib/getopt.c
lib/getopt_int.h

index dc573dbec4287f6f338f9e0e9fdfa9946c38276a..1bbe41ecd4beaa442e5640b667aa4149f787839c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,22 @@
 2017-04-06  Zack Weinberg  <zackw@panix.com>
 
+       getopt: tidy up _getopt_initialize a bit
+
+       _getopt_data.__posixly_correct is completely redundant to
+       _getopt_data.__ordering, and some work that logically belongs in
+       _getopt_initialize was being done by _getopt_internal_r, making the
+       code harder to understand.
+
+       As a side effect, getenv will no longer be called if the first
+       character of the options string is '+' or '-', which is probably a
+       Good Thing.  (Perhaps we should have a flag character that
+       specifically asks for the permutation behavior?)
+
+       * lib/getopt_int.h (_getopt_data): Remove __posixly_correct field.
+       * lib/getopt.c (_getopt_internal_r): Move some initialization code...
+       (_getopt_initialize): ...here. Don't set d->__posixly_correct.
+
+
        getopt: merge from glibc: repetition reduction
 
        The definitions of the entry point functions 'getopt' and
index 77528728970f9879877b9012fa6f2681fad336dd..835767528f7b625d7c03617261783e6e6e02e232 100644 (file)
@@ -189,15 +189,13 @@ _getopt_initialize (int argc _GL_UNUSED,
   /* Start processing options with ARGV-element 1 (since ARGV-element 0
      is the program name); the sequence of previously skipped
      non-option ARGV-elements is empty.  */
+  if (d->optind == 0)
+    d->optind = 1;
 
   d->__first_nonopt = d->__last_nonopt = d->optind;
-
   d->__nextchar = NULL;
 
-  d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
-
   /* Determine how to handle the ordering of options and nonoptions.  */
-
   if (optstring[0] == '-')
     {
       d->__ordering = RETURN_IN_ORDER;
@@ -208,11 +206,12 @@ _getopt_initialize (int argc _GL_UNUSED,
       d->__ordering = REQUIRE_ORDER;
       ++optstring;
     }
-  else if (d->__posixly_correct)
+  else if (posixly_correct || !!getenv ("POSIXLY_CORRECT"))
     d->__ordering = REQUIRE_ORDER;
   else
     d->__ordering = PERMUTE;
 
+  d->__initialized = 1;
   return optstring;
 }
 \f
@@ -285,15 +284,10 @@ _getopt_internal_r (int argc, char **argv, const char *optstring,
   d->optarg = NULL;
 
   if (d->optind == 0 || !d->__initialized)
-    {
-      if (d->optind == 0)
-       d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
-      optstring = _getopt_initialize (argc, argv, optstring, d,
-                                     posixly_correct);
-      d->__initialized = 1;
-    }
+    optstring = _getopt_initialize (argc, argv, optstring, d, posixly_correct);
   else if (optstring[0] == '-' || optstring[0] == '+')
     optstring++;
+
   if (optstring[0] == ':')
     print_errors = 0;
 
index 762679a30598b9c8a128abf6f7abaa626886f5a4..6a029542ab1b42f5cf9331867e214b1520a18a58 100644 (file)
@@ -83,10 +83,6 @@ struct _getopt_data
   /* See __ord above.  */
   enum __ord __ordering;
 
-  /* True if behaving strictly as specified by POSIX.  */
-  int __posixly_correct;
-
-
   /* Handle permutation of arguments.  */
 
   /* Describe the part of ARGV that contains non-options that have