public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin] newlib: getopt now permutes multi-flag options correctly
@ 2018-06-21  7:35 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2018-06-21  7:35 UTC (permalink / raw)
  To: newlib-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=9dd3c3b0ad176683e2c004cb1c734588b63e59d7

commit 9dd3c3b0ad176683e2c004cb1c734588b63e59d7
Author: Thomas Kindler <mail+newlib@t-kindler.de>
Date:   Tue May 29 14:04:56 2018 +0200

    newlib: getopt now permutes multi-flag options correctly
    
    Previously, "test 1 2 3 -a -b -c"  was permuted to "test -a -b -c 1 2 3",
    but "test 1 2 3 -abc" was left as "test 1 2 3 -abc".
    
    Signed-off-by: Thomas Kindler <mail+newlib@t-kindler.de>

Diff:
---
 newlib/libc/include/getopt.h |  3 ++-
 newlib/libc/stdlib/getopt.c  | 33 +++++++++++++++++++++------------
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/newlib/libc/include/getopt.h b/newlib/libc/include/getopt.h
index 9bced42..ac88524 100644
--- a/newlib/libc/include/getopt.h
+++ b/newlib/libc/include/getopt.h
@@ -129,7 +129,7 @@ extern "C"
 
   /* The GETOPT_DATA_INITIALIZER macro is used to initialize a statically-
      allocated variable of type struct getopt_data.  */
-  #define GETOPT_DATA_INITIALIZER	{0,0,0,0,0}
+  #define GETOPT_DATA_INITIALIZER	{0,0,0,0,0,0,0}
 
   /* These #defines are to make accessing the reentrant functions easier.  */
   #define getopt_r		__getopt_r
@@ -142,6 +142,7 @@ extern "C"
   {
     char *optarg;
     int optind, opterr, optopt, optwhere;
+    int permute_from, num_nonopts;
   } getopt_data;
 
 #endif /* __need_getopt_newlib */
diff --git a/newlib/libc/stdlib/getopt.c b/newlib/libc/stdlib/getopt.c
index 1094747..d4f225a 100644
--- a/newlib/libc/stdlib/getopt.c
+++ b/newlib/libc/stdlib/getopt.c
@@ -118,6 +118,8 @@ int optopt = '?';
 
 /* static variables */
 static int optwhere = 0;
+static int permute_from = 0;
+static int num_nonopts = 0;
 
 /* functions */
 
@@ -163,6 +165,8 @@ read_globals (struct getopt_data *data)
   data->opterr = opterr;
   data->optopt = optopt;
   data->optwhere = optwhere;
+  data->permute_from = permute_from;
+  data->num_nonopts = num_nonopts;
 }
 
 /* write_globals: write the values into the globals from a getopt_data
@@ -175,6 +179,8 @@ write_globals (struct getopt_data *data)
   opterr = data->opterr;
   optopt = data->optopt;
   optwhere = data->optwhere;
+  permute_from = data->permute_from;
+  num_nonopts = data->num_nonopts;
 }
 
 /* getopt_internal:  the function that does all the dirty work
@@ -186,8 +192,6 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 		 struct getopt_data *data)
 {
   GETOPT_ORDERING_T ordering = PERMUTE;
-  size_t permute_from = 0;
-  int num_nonopts = 0;
   int optindex = 0;
   size_t match_chars = 0;
   char *possible_arg = 0;
@@ -209,7 +213,12 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 
   /* if this is our first time through */
   if (data->optind == 0)
-    data->optind = data->optwhere = 1;
+    {
+      data->optind = 1;
+      data->optwhere = 1;
+      data->permute_from = 0;
+      data->num_nonopts = 0;
+    }
 
   /* define ordering */
   if (shortopts != 0 && (*shortopts == '-' || *shortopts == '+'))
@@ -237,24 +246,24 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
 	{
 	default:		/* shouldn't happen */
 	case PERMUTE:
-	  permute_from = data->optind;
-	  num_nonopts = 0;
+	  data->permute_from = data->optind;
+	  data->num_nonopts = 0;
 	  while (!is_option (argv[data->optind], only))
 	    {
 	      data->optind++;
-	      num_nonopts++;
+	      data->num_nonopts++;
 	    }
 	  if (argv[data->optind] == 0)
 	    {
 	      /* no more options */
-	      data->optind = permute_from;
+	      data->optind = data->permute_from;
 	      return EOF;
 	    }
 	  else if (strcmp (argv[data->optind], "--") == 0)
 	    {
 	      /* no more options, but have to get `--' out of the way */
-	      permute (argv + permute_from, num_nonopts, 1);
-	      data->optind = permute_from + 1;
+	      permute (argv + data->permute_from, data->num_nonopts, 1);
+	      data->optind = data->permute_from + 1;
 	      return EOF;
 	    }
 	  break;
@@ -426,10 +435,10 @@ getopt_internal (int argc, char *const argv[], const char *shortopts,
     }
 
   /* do we have to permute or otherwise modify data->optind? */
-  if (ordering == PERMUTE && data->optwhere == 1 && num_nonopts != 0)
+  if (ordering == PERMUTE && data->optwhere == 1 && data->num_nonopts != 0)
     {
-      permute (argv + permute_from, num_nonopts, 1 + arg_next);
-      data->optind = permute_from + 1 + arg_next;
+      permute (argv + data->permute_from, data->num_nonopts, 1 + arg_next);
+      data->optind = data->permute_from + 1 + arg_next;
     }
   else if (data->optwhere == 1)
     data->optind = data->optind + 1 + arg_next;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-06-21  7:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-21  7:35 [newlib-cygwin] newlib: getopt now permutes multi-flag options correctly Corinna Vinschen

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 read-only IMAP folder(s) and NNTP newsgroup(s).