public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: newlib-cvs@sourceware.org
Subject: [newlib-cygwin] newlib: getopt now permutes multi-flag options correctly
Date: Thu, 21 Jun 2018 07:35:00 -0000	[thread overview]
Message-ID: <20180621073512.84761.qmail@sourceware.org> (raw)

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;


                 reply	other threads:[~2018-06-21  7:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180621073512.84761.qmail@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=newlib-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).