From: Martijn Dekker <martijn@inlv.org>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: [PATCH] getopts: fix returned value on missing option argument
Date: Sat, 29 Jun 2019 00:17:42 +0200 [thread overview]
Message-ID: <2363fe35-c213-d4b6-2bc5-642328dd28f4@inlv.org> (raw)
[-- Attachment #1: Type: text/plain, Size: 632 bytes --]
Problem:
$ zsh -c 'while getopts x: opt; do echo "$opt"; done' dummy -x
zsh:1: argument expected after -x option
:
Expected output: the ':' at the end should be a '?'. The opt variable
should only be set to ':' in "quiet" mode, i.e. if the option string
starts with a ':' (so in this case would be ':x:').
Ref.: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html
(the two bullet points following "If an option-argument is missing:").
The attached patch fixes this to match POSIX and the behaviour of every
other shell.
- Martijn
--
modernish -- harness the shell
https://github.com/modernish/modernish
[-- Attachment #2: BUG_GETOPTSMA.patch --]
[-- Type: text/plain, Size: 1799 bytes --]
diff --git a/Src/builtin.c b/Src/builtin.c
index e863cc4bb..9b9e76c77 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5511,14 +5511,12 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
/* check for legality */
if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
p = "?";
- err:
zsfree(zoptarg);
setsparam(var, ztrdup(p));
if(quiet) {
zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
} else {
- zwarn(*p == '?' ? "bad option: %c%c" :
- "argument expected after %c%c option",
+ zwarn("bad option: %c%c",
"?-+"[lenoptbuf], opch);
zoptarg=ztrdup("");
}
@@ -5529,8 +5527,17 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
if(p[1] == ':') {
if(optcind == lenstr) {
if(!args[zoptind]) {
- p = ":";
- goto err;
+ zsfree(zoptarg);
+ if(quiet) {
+ setsparam(var, ztrdup(":"));
+ zoptarg = metafy(optbuf, lenoptbuf, META_DUP);
+ } else {
+ setsparam(var, ztrdup("?"));
+ zoptarg = ztrdup("");
+ zwarn("argument expected after %c%c option",
+ "?-+"[lenoptbuf], opch);
+ }
+ return 0;
}
p = ztrdup(args[zoptind++]);
} else
diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst
index 7eba5a4b1..72c9e209e 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
+
+ set -- -x
+ OPTIND=1
+ while getopts x: opt; do
+ echo "$opt,${OPTARG:-Empty}"
+ done
+0:missing option-argument (error message mode)
+>?,Empty
+?(eval):3: argument expected after -x option
+
+ set -- -x
+ OPTIND=1
+ while getopts :x: opt; do
+ echo "$opt,${OPTARG:-Empty}"
+ done
+0:missing option-argument (quiet mode)
+>:,x
reply other threads:[~2019-06-28 22:18 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=2363fe35-c213-d4b6-2bc5-642328dd28f4@inlv.org \
--to=martijn@inlv.org \
--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).