mirror of
https://github.com/zsh-users/zsh
synced 2024-10-07 08:30:06 +00:00
48614: getopts: Calculate OPTIND according to POSIX_BUILTINS
This commit is contained in:
parent
2da0d8b52f
commit
c23a0d84b0
|
@ -1,3 +1,9 @@
|
||||||
|
2021-05-03 dana <dana@dana.is>
|
||||||
|
|
||||||
|
* 48614 (tweaked per 48630): Doc/Zsh/builtins.yo,
|
||||||
|
Doc/Zsh/options.yo, README, Src/builtin.c, Test/B10getopts.ztst:
|
||||||
|
Calculate OPTIND according to POSIX_BUILTINS
|
||||||
|
|
||||||
2021-05-03 Daniel Shahaf <d.s@daniel.shahaf.name>
|
2021-05-03 Daniel Shahaf <d.s@daniel.shahaf.name>
|
||||||
|
|
||||||
* 48767: Doc/Zsh/params.yo: docs: $SECONDS: Clarify what types
|
* 48767: Doc/Zsh/params.yo: docs: $SECONDS: Clarify what types
|
||||||
|
|
|
@ -982,7 +982,8 @@ vindex(OPTARG, use of)
|
||||||
The first option to be examined may be changed by explicitly assigning
|
The first option to be examined may be changed by explicitly assigning
|
||||||
to tt(OPTIND). tt(OPTIND) has an initial value of tt(1), and is
|
to tt(OPTIND). tt(OPTIND) has an initial value of tt(1), and is
|
||||||
normally set to tt(1) upon entry to a shell function and restored
|
normally set to tt(1) upon entry to a shell function and restored
|
||||||
upon exit (this is disabled by the tt(POSIX_BUILTINS) option). tt(OPTARG)
|
upon exit. (The tt(POSIX_BUILTINS) option disables this, and also changes
|
||||||
|
the way the value is calculated to match other shells.) tt(OPTARG)
|
||||||
is not reset and retains its value from the most recent call to
|
is not reset and retains its value from the most recent call to
|
||||||
tt(getopts). If either of tt(OPTIND) or tt(OPTARG) is explicitly
|
tt(getopts). If either of tt(OPTIND) or tt(OPTARG) is explicitly
|
||||||
unset, it remains unset, and the index or option argument is not
|
unset, it remains unset, and the index or option argument is not
|
||||||
|
|
|
@ -2249,7 +2249,8 @@ command found in the path.
|
||||||
|
|
||||||
Furthermore, the tt(getopts) builtin behaves in a POSIX-compatible
|
Furthermore, the tt(getopts) builtin behaves in a POSIX-compatible
|
||||||
fashion in that the associated variable tt(OPTIND) is not made
|
fashion in that the associated variable tt(OPTIND) is not made
|
||||||
local to functions.
|
local to functions, and its value is calculated differently to match
|
||||||
|
other shells.
|
||||||
|
|
||||||
Moreover, the warning and special exit code from
|
Moreover, the warning and special exit code from
|
||||||
tt([[ -o )var(non_existent_option)tt( ]]) are suppressed.
|
tt([[ -o )var(non_existent_option)tt( ]]) are suppressed.
|
||||||
|
|
3
README
3
README
|
@ -99,6 +99,9 @@ emulate sh: When zsh emulates sh, the final command in a pipeline is now run in
|
||||||
a subshell. This differs from the behavior in the native (zsh) mode, but is
|
a subshell. This differs from the behavior in the native (zsh) mode, but is
|
||||||
consistent with most other sh implementations.
|
consistent with most other sh implementations.
|
||||||
|
|
||||||
|
getopts now calculates OPTIND in a similar manner to other shells when the
|
||||||
|
POSIX_BUILTINS option is enabled.
|
||||||
|
|
||||||
Incompatibilities between 5.7.1 and 5.8
|
Incompatibilities between 5.7.1 and 5.8
|
||||||
---------------------------------------
|
---------------------------------------
|
||||||
|
|
||||||
|
|
|
@ -5568,6 +5568,11 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
|
||||||
/* check for legality */
|
/* check for legality */
|
||||||
if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
|
if(opch == ':' || !(p = memchr(optstr, opch, lenoptstr))) {
|
||||||
p = "?";
|
p = "?";
|
||||||
|
/* Keep OPTIND correct if the user doesn't return after the error */
|
||||||
|
if (isset(POSIXBUILTINS)) {
|
||||||
|
optcind = 0;
|
||||||
|
zoptind++;
|
||||||
|
}
|
||||||
zsfree(zoptarg);
|
zsfree(zoptarg);
|
||||||
setsparam(var, ztrdup(p));
|
setsparam(var, ztrdup(p));
|
||||||
if(quiet) {
|
if(quiet) {
|
||||||
|
@ -5584,6 +5589,11 @@ bin_getopts(UNUSED(char *name), char **argv, UNUSED(Options ops), UNUSED(int fun
|
||||||
if(p[1] == ':') {
|
if(p[1] == ':') {
|
||||||
if(optcind == lenstr) {
|
if(optcind == lenstr) {
|
||||||
if(!args[zoptind]) {
|
if(!args[zoptind]) {
|
||||||
|
/* Fix OPTIND as above */
|
||||||
|
if (isset(POSIXBUILTINS)) {
|
||||||
|
optcind = 0;
|
||||||
|
zoptind++;
|
||||||
|
}
|
||||||
zsfree(zoptarg);
|
zsfree(zoptarg);
|
||||||
if(quiet) {
|
if(quiet) {
|
||||||
setsparam(var, ztrdup(":"));
|
setsparam(var, ztrdup(":"));
|
||||||
|
|
|
@ -96,3 +96,32 @@
|
||||||
done
|
done
|
||||||
0:missing option-argument (quiet mode)
|
0:missing option-argument (quiet mode)
|
||||||
>:,x
|
>:,x
|
||||||
|
|
||||||
|
# This function is written so it can be easily referenced against other shells
|
||||||
|
t() {
|
||||||
|
local o i=0 n=$1
|
||||||
|
shift
|
||||||
|
while [ $i -lt $n ]; do
|
||||||
|
i=$(( i + 1 ))
|
||||||
|
getopts a: o "$@" 2> /dev/null
|
||||||
|
done
|
||||||
|
printf '<%d>' "$OPTIND"
|
||||||
|
}
|
||||||
|
# Try all these the native way, then the POSIX_BUILTINS way
|
||||||
|
for 1 in no_posix_builtins posix_builtins; do (
|
||||||
|
setopt $1
|
||||||
|
print -rn - "$1: "
|
||||||
|
t 1
|
||||||
|
t 1 foo
|
||||||
|
t 1 -- foo
|
||||||
|
t 1 -a
|
||||||
|
t 1 -b
|
||||||
|
t 2 -a -b
|
||||||
|
t 4 -a -b -c -d -a
|
||||||
|
t 5 -a -b -c -a -b -c
|
||||||
|
t 5 -a -b -c -d -ax -a
|
||||||
|
print
|
||||||
|
); done
|
||||||
|
0:OPTIND calculation with and without POSIX_BUILTINS (workers/42248)
|
||||||
|
>no_posix_builtins: <1><1><2><1><1><3><5><7><6>
|
||||||
|
>posix_builtins: <1><1><2><2><2><3><6><7><7>
|
||||||
|
|
Loading…
Reference in a new issue