From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 1005 invoked from network); 18 Apr 2021 05:17:29 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 18 Apr 2021 05:17:29 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1618723049; b=pTandPkuqDcapD43TFyGS9qGYoPElr5ispySd3sbOAImasS6cIUQvsOqqoqosCg7avYWNukMGr dyMMjlsAHykVPbqswcy2lGbM9Fy7u1qd7kLQdl9oUoCL6nxtbhyARybedIhrxXnEq59sVLGGdv vJzvpiLnEB/qZCxQTa9FsyM0AwqnRAjphpLa7QeyRjd0GjIUBPFLjoHKVEkTIcFEy1PPKcJaU2 zajOpMHIJjkPWXKXiOzFJrxukHyDC8A83lVxo/tcZaDwAklD1cmR8VOAtHTwwlzrajuJ8pcQgs MKKcKDmH0theF7WGW8hz4MUhuRgoUkO9CPhzI6JmIAwt3Q==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-qk1-f175.google.com) smtp.remote-ip=209.85.222.175; dkim=pass header.d=dana-is.20150623.gappssmtp.com header.s=20150623 header.a=rsa-sha256; dmarc=none header.from=dana.is; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20200801; t=1618723049; bh=vHcT3Kdqa6Pu3DMCZoSEltl8n+RzOeAnv+qk4BvPAV0=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:To:References:Message-ID:Content-Transfer-Encoding:Cc:Date: In-Reply-To:From:Subject:MIME-Version:Content-Type:DKIM-Signature: DKIM-Signature; b=jKcByJEGgMeh1rGb6BlAX+Iu26jxrk1CV4kTtYvRWofqXtEdPswwJvDAEmrDFPxY1qjUdz3PJQ o5szDcxZeDFPOkbXAei9eabR2o0tHcVmSh+cieAXiDySeUkDIFkIhL3Y6zbnQFQni5G7uXHA9u 89JKHLut80AgYWY8YBA1kTaOZ5Kbh6GT053B/K4wh1/AOgtnAy/sEn0+EP6+zrwr9zKPlJsFH9 UHagQRFyT0vWjAV7E1umkguEfA/iENiL93yNYgmcmDO/3hGajLwdwBA/dhFuV0veYTGg3ft1I5 wkJyDmz0JsZpUekFKAozWPibrIAM6Bhwby8Llg25lZU2tg==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20200801; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:To:References:Message-Id: Content-Transfer-Encoding:Cc:Date:In-Reply-To:From:Subject:Mime-Version: Content-Type:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=DErgAzum0pmV8Gh/Omb1sM/fAUcmYoQn95IHMDA02y4=; b=gUIHSDabSJ57ySqE2ZkuH7p4Hn MZI64rIyPEI1p0Eiykefc3jVscjypCvh/saj+E9k9A8PO5bbRa0HOVFbBU7+CvmZtQHOIkgO7cDVs xwvDpHaiQ2zBiKS/4PLTYQ9hgPv30uJBTnwWwl+qlv7V+SQCw0hX6g8/7rrxBFCTP9Gzbkfd3Mfx9 EXzAF8RTTdytRuTVl97TJmCTKvzR0AUfocGYHslEegWVgID/OyGN78CvAc08N9k+yToL1PzDhIk4v iRokcGk21tArZkrhV2DayZL/+jFoNiaecrC6n8RczN2ZZNdmOb//8T7pYxpq0mIHV5Ng7CtNPtT4s Sm7NAAiA==; Received: from authenticated user by zero.zsh.org with local id 1lXzoF-000JFw-2E; Sun, 18 Apr 2021 05:17:27 +0000 Authentication-Results: zsh.org; iprev=pass (mail-qk1-f175.google.com) smtp.remote-ip=209.85.222.175; dkim=pass header.d=dana-is.20150623.gappssmtp.com header.s=20150623 header.a=rsa-sha256; dmarc=none header.from=dana.is; arc=none Received: from mail-qk1-f175.google.com ([209.85.222.175]:37403) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1lXznj-000J0z-AC; Sun, 18 Apr 2021 05:16:55 +0000 Received: by mail-qk1-f175.google.com with SMTP id t17so4493018qkg.4 for ; Sat, 17 Apr 2021 22:16:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=DErgAzum0pmV8Gh/Omb1sM/fAUcmYoQn95IHMDA02y4=; b=trojw8FnQCHcwMUynNYlSk5AAdCSWqeEHSwXYBrb1Yvw/b/vhOfL1nwTGaD8WWdETy 2MZWjrBmxvfoV8vkLHVpOKxgQ/jU9DFEn3fR335H6gk8qLKcz7xTidv2UDcHkz3hoP+4 Zuv8wdAZlUbcKIe7eVtPggx5Vk/7NxoXGa/uyYytyRHV5HNOhSsmrWdOC91yepXxjhWX PC4sh9WFS5Zw2rFfN33cG4IJv9WnT2mKT4M0lXixXR64PA/zWN5ANUiGbOXmWyj1Gwpe atb+cPMFp/aHHTERcifhi4W93e/Dx2PSegnhZ2NtXS+3tz7nNO269btzUSbBniei6fmm XCcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=DErgAzum0pmV8Gh/Omb1sM/fAUcmYoQn95IHMDA02y4=; b=t3VsbvpjVEzsB+PykvpvNrNjgqV5xgJD4sg7RK5zIA3ZbefiIxWrJUsedTMy6w7wHJ xVVjZJWAH0up5n2GPMu8bYd/te6ozSdDUd1op5yiekxPNee4YQQ+H3LnKTBbkokoopl5 vu2vRCn72xmoZpDPhs0FVvxNzx7bPL7RejGlu9R5TDH6XftIZm2HuOStSmiWOMX+RwdF I+lbLEtTkYxYtNO5JcRBTN7YJz1HMidxJXsGSRLIYZy34bA7pwSgSL3heGh0oD3sIFml 2iGfnIaiIJbm94KrtpeA9rHcHMcrwDNjWsfzw95JrKB2DMQz3lShttcG//tFCXENMMai X6lA== X-Gm-Message-State: AOAM533pahGJvZ26+Z7+6r4/5W6/giPVpVjXlOO5PCWhzunDHAj9iS5D QVrOOK6tU5Ful4tZIDy9vF3NUw== X-Google-Smtp-Source: ABdhPJw5a9Risb6TIIlAGfm9BpbxCZexYjPXnw1ODhq3kcM9a9JFD4xnta6Pp8wvcdwVo/vmAeU2Vw== X-Received: by 2002:a37:a0d6:: with SMTP id j205mr5246875qke.469.1618723014045; Sat, 17 Apr 2021 22:16:54 -0700 (PDT) Received: from heartswap.lan.dana.is (173-17-84-59.client.mchsi.com. [173.17.84.59]) by smtp.gmail.com with ESMTPSA id q125sm7344177qkf.68.2021.04.17.22.16.53 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Apr 2021 22:16:53 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.4\)) Subject: Re: [BUG] getopts OPTIND From: dana In-Reply-To: <20210414130802.GF3882@tarpaulin.shahaf.local2> Date: Sun, 18 Apr 2021 00:16:52 -0500 Cc: Zsh hackers list , franciscodezuviria@gmail.com Content-Transfer-Encoding: quoted-printable Message-Id: <1EB88C27-FA4E-4B1F-AEBD-15C2FFC21F95@dana.is> References: <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> <20210414130802.GF3882@tarpaulin.shahaf.local2> To: Daniel Shahaf X-Mailer: Apple Mail (2.3608.120.23.2.4) X-Seq: 48614 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: On 14 Apr 2021, at 08:08, Daniel Shahaf wrote: > Should the descriptions of OPTIND and/or POSIX_BUILTINS in the manual = be > extended as well? The latter, yes; done that. The former, idk, it doesn't currently = mention anything about how it's calculated or about POSIX_BUILTINS effects so = i'm inclined to leave it alone On 14 Apr 2021, at 08:08, Daniel Shahaf wrote: > Some more cases to test: > . > t 0 > t 1 foo > t 1 -- foo > t 1 -b > . > where -b doesn't take an argument. `t 0` doesn't test anything, the loop is just skipped. `t 1 -b` tests = the same thing as `t 1 -w`, but i guess it's confusing that i picked -a -w -e -r = as the options; i've changed them to -a -b -c -d. I've also added the two foo = ones you suggested, as well as just `t 1`. (All three behaved the same as = other shells already) dana diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo index a7afe42cf..4b9778e40 100644 --- a/Doc/Zsh/builtins.yo +++ b/Doc/Zsh/builtins.yo @@ -982,7 +982,8 @@ vindex(OPTARG, use of) 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 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 tt(getopts). If either of tt(OPTIND) or tt(OPTARG) is explicitly unset, it remains unset, and the index or option argument is not diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo index 714e8a1a1..ffe2d1a0d 100644 --- a/Doc/Zsh/options.yo +++ b/Doc/Zsh/options.yo @@ -2239,7 +2239,8 @@ command found in the path. =20 Furthermore, the tt(getopts) builtin behaves in a POSIX-compatible 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. =20 Moreover, the warning and special exit code from tt([[ -o )var(non_existent_option)tt( ]]) are suppressed. diff --git a/Src/builtin.c b/Src/builtin.c index 26335a2e8..13dfdf8be 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -5556,6 +5556,11 @@ bin_getopts(UNUSED(char *name), char **argv, = UNUSED(Options ops), UNUSED(int fun /* check for legality */ if(opch =3D=3D ':' || !(p =3D memchr(optstr, opch, lenoptstr))) { p =3D "?"; + /* Keep OPTIND correct if the user doesn't return after the = error */ + if (isset(POSIXBUILTINS)) { + optcind =3D 0; + zoptind++; + } zsfree(zoptarg); setsparam(var, ztrdup(p)); if(quiet) { @@ -5572,6 +5577,11 @@ bin_getopts(UNUSED(char *name), char **argv, = UNUSED(Options ops), UNUSED(int fun if(p[1] =3D=3D ':') { if(optcind =3D=3D lenstr) { if(!args[zoptind]) { + /* Fix OPTIND as above */ + if (isset(POSIXBUILTINS)) { + optcind =3D 0; + zoptind++; + } zsfree(zoptarg); if(quiet) { setsparam(var, ztrdup(":")); diff --git a/Test/B10getopts.ztst b/Test/B10getopts.ztst index 72c9e209e..e50d177c7 100644 --- a/Test/B10getopts.ztst +++ b/Test/B10getopts.ztst @@ -96,3 +96,32 @@ done 0:missing option-argument (quiet mode) >:,x + + # This function is written so it can be easily referenced against = other shells + t() { + local o i=3D0 n=3D$1 + shift + while [ $i -lt $n ]; do + i=3D$(( 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>