zsh-workers
 help / color / mirror / code / Atom feed
From: dana <dana@dana.is>
To: franciscodezuviria@gmail.com
Cc: zsh-workers@zsh.org
Subject: Re: [BUG] getopts OPTIND
Date: Tue, 9 Jan 2018 16:48:17 -0600	[thread overview]
Message-ID: <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> (raw)
In-Reply-To: <CA++-COw=KgUBJc1PMq=L5be2VeJGXBjwtsRfRx7+7GSMspcQJQ@mail.gmail.com>

On 9 Jan 2018, at 10:22, Francisco de Zuviría Allende <franciscodezuviria@gmail.com> wrote:
>Execution in bash: ... OPTIND is 5, next -a
>execution in zsh:  ... OPTIND is 4, next -r

I think this fixes it? At least, zsh gives the same output as bash and dash when
i do this. Peter's left an ominous warning about changes to this function that
deserves recognition though...

dana


diff --git a/Src/builtin.c b/Src/builtin.c
index 0211f2721..2550b29f2 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5437,6 +5437,9 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
     if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
 	p = "?";
     err:
+	/* Keep OPTIND correct if the user doesn't return after the error */
+	optcind = 0;
+	zoptind++;
 	zsfree(zoptarg);
 	setsparam(var, ztrdup(p));
 	if(quiet) {
diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst
index 7eba5a4b1..f6977accb 100644
--- a/Test/B10getopts.ztst
+++ b/Test/B10getopts.ztst
@@ -79,3 +79,20 @@
   test_getopts +x
 1:one illegal option, + variant
 >test_getopts:3: bad option: +x
+
+  t() { local o; repeat $1 { getopts a: o "${@:2}" 2>&1 }; print -r $OPTIND }
+  t 4 -a -w -e -r -a
+  t 5 -a -w -e -a -w -e
+  t 5 -a -w -e -r -ax -a
+0:OPTIND calculation after error (workers/42248)
+*>*: bad option: -e
+*>*: bad option: -r
+*>*: argument expected after -a option
+>6
+*>*: bad option: -e
+*>*: bad option: -e
+>7
+*>*: bad option: -e
+*>*: bad option: -r
+*>*: argument expected after -a option
+>7



  reply	other threads:[~2018-01-09 22:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-09 16:22 Francisco de Zuviría Allende
2018-01-09 22:48 ` dana [this message]
2018-01-09 22:57   ` Bart Schaefer
2018-01-09 23:58     ` dana
2018-01-10  1:32       ` Francisco de Zuviría Allende
2018-01-10  9:05   ` Peter Stephenson
2021-04-13 23:28   ` dana
2021-04-14 13:04     ` [BUG] getopts OPTIND - yash's behaviour Daniel Shahaf
2021-04-14 13:08     ` [BUG] getopts OPTIND Daniel Shahaf
2021-04-18  5:16       ` dana
2021-04-20 21:31         ` Daniel Shahaf
2021-05-03 23:38           ` dana

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=0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is \
    --to=dana@dana.is \
    --cc=franciscodezuviria@gmail.com \
    --cc=zsh-workers@zsh.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.
Code repositories for project(s) associated with this public inbox

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

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).