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 5102 invoked from network); 13 Apr 2021 23:29:11 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 13 Apr 2021 23:29:11 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20200801; t=1618356551; b=WWwNGiGRXDWJzDmhKadmeWQjle6zBiNT0x/Yy/8o7xzDZJTuMxg6sTtFksBW4vHo74AdCDCZfQ haytLM4Es7MMD/nPHwM7i7VuXBuPW4wzA4bt6SE58YbZmr0c2dgRMza+3mSj3edr2RcwlZBqLE V6EoM4lhpyiIi0+qoTLCLhJqo29C6q0SXfjVyyHbjXEtiCsp0ciC+AFCd0q3aID5gSbve8hxBn 2BeVrB+DA5c+ArUfKNEQd28vyXXELUWLGwS3hm7MY/GfVXAVMgVfQAPYWl3/oytuyeUw1VDWnb EqIf43TKTo+sCR3bs+wsqE86+E2/CB+h3vMmnwhoSfAZOQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (mail-qv1-f54.google.com) smtp.remote-ip=209.85.219.54; 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=1618356551; bh=MhHbK9MzndaBrIJCqIbHQz6Qoph6PkoYhVIk1k3qkrc=; 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=DJzP2ve+dMwnAH0aDo7HcBniaoemKgBvnfLn2crshKhd6KLUntPHB2LPaOq1UWKw3+gJx9pNHX twit+GHL2xkRcwO6OTMaljyxaNePEtHzzmOoCSF3AJBq5Fihc/LMqq/0dmbVg9UJ8HcnaeR2px CCiBKkeewIfwdSL8cRDvYwi3wYH0UEE0qSeHuKz1qQfUHGMoqSRFInznBLUr7SW/98kk5pT/Bm WvrzMEhz8fIoJyLr51LH8kyiDBTERcOxIuemQO/pgdOOxqd8g6aQ+HUAxkr7Y6WLP0DPNGIlci 6ST78YeFZiv2TkTJV/A4vdpNxsJDzDu8GPtFLI5tw1bc1Q==; 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=jyDfmF9mCh0mBvFGRdvhvFq+uXs22PFmCI1uMsimGMY=; b=bs724aEXNvXM10U7Du3aajRbA8 7WSrpdUtBtgM2dCv3/qQ308eJN0AFyNK1NGP1QJ+8GdRmDLWACjP1jsqL1F56QoB86so5XpT1WebG 8n8LakVRxPjkOgbtp9+IAD1+aXdUSnNVsxRRBPuR1Ipdjh01B8Dw4jKHbfucivWAGd8p7e6YNeNEi 5zUFihFj0lhpQn//y/d9BsHcqR/mCUi+jrCRdk48/pBOLnYBishJgokK/VkRSbnR20h+rMTRj5641 FN4ft00u3QD2rFe6E80ecYAKIRXJcSmkyJqxoTvNjSefuQY7ESgO24UxAPEbQNk3DsdZmtnhZAas+ Vvg5VW6w==; Received: from authenticated user by zero.zsh.org with local id 1lWST0-0000SO-3A; Tue, 13 Apr 2021 23:29:10 +0000 Authentication-Results: zsh.org; iprev=pass (mail-qv1-f54.google.com) smtp.remote-ip=209.85.219.54; 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-qv1-f54.google.com ([209.85.219.54]:34420) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_128_GCM_SHA256:128) id 1lWSSj-0000Eb-JN; Tue, 13 Apr 2021 23:28:54 +0000 Received: by mail-qv1-f54.google.com with SMTP id j3so9028636qvs.1 for ; Tue, 13 Apr 2021 16:28:53 -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=jyDfmF9mCh0mBvFGRdvhvFq+uXs22PFmCI1uMsimGMY=; b=CuDQfpe4aAeh9xog0U3CD6kzmatbT31o6ey+1rHycEfrGzI9xeE3ibyyUaB89fJaAx jui3DfoJXW6ZPjc+2w/FwH66Rey4yoBrC4AfWn6uyXQQV0/sJ9+vW/ctfZv6saB/KFy2 o3brbNpU19+8PE1uX1nyQks+VBOQ05QyGMY2GVIfZBM1ng6r4R920Hez18eEiMaB1Yh1 gAZW6mx2YtcEGt2ScJezbXyImoh51Q1EITKeah9wXl3HonmrkfrGaMusib6pUo39jGFn vjr7W67uQ2HA2hrCp18IOOX1f1tWpbEjYV+YJKu0io4X8zaxN7NzjX6lKcp841IGcDQz QIvQ== 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=jyDfmF9mCh0mBvFGRdvhvFq+uXs22PFmCI1uMsimGMY=; b=aBJe/LXDS8l+4NXN1k4Ca1Fwq7vu29Y1VT4lPf7Xh2E2YBjKZOe8X84c+a6uSDNJ6j GjCTTUdVDSdwezQnUpMs+ypbM6FstOYRuxAxKmXMFLZXItUMgeSIVlvmlWYSz/ihY79i e0GAXKF9tvxPjcz15derGMDsZv3bUcDRu+FYnD5ndTCBnLR7TzMNo2S6+qY9Js+fCZmV uvFIk3hKzR7sLD0YhMUp6gpbfDTG0VH7OGItEuVaa44/qIPQX888IdvG2kXJFbwMaFOu nerWFqsBnG5SJ/HC8O+No5WlzT/f/o3OgZfM+PlXH3jjJNx6CQczLb5VyBFVD428jZnO AuGQ== X-Gm-Message-State: AOAM530YbQrybkgMIdiM1/YjauMYk3M/DjD/3Nls+rFN8eS23wzjGJAf B0Nbo35vYk5L5464leHr08sxjDpbrLZqd4uc X-Google-Smtp-Source: ABdhPJyVImrsIdhiW9uDCRbs2cgTKs7B0Qv3x6xmwUcDKBBsDBqeHpRwG+BvWerT8hxAcuXG9uFxIA== X-Received: by 2002:ad4:444f:: with SMTP id l15mr10068610qvt.0.1618356532218; Tue, 13 Apr 2021 16:28:52 -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 u9sm9943364qtf.76.2021.04.13.16.28.51 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 13 Apr 2021 16:28:51 -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: <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> Date: Tue, 13 Apr 2021 18:28:50 -0500 Cc: franciscodezuviria@gmail.com, Bart Schaefer Content-Transfer-Encoding: quoted-printable Message-Id: References: <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> To: Zsh hackers list X-Mailer: Apple Mail (2.3608.120.23.2.4) X-Seq: 48555 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: (Resurrecting this per workers/48509) bin_getopts() has changed a little since i posted my patch before, this = looks a bit weirder. The test is also weird. But i confirmed that this makes = it behave like dash, bash, and mksh (my ksh93 doesn't support local, and i = still don't know what yash is doing): % pbpaste | zsh-dev -f <1><1><3><5><7><6> % pbpaste | zsh-dev -f --posix-builtins <2><2><3><6><7><7> % pbpaste | dash <2><2><3><6><7><7> % pbpaste | bash <2><2><3><6><7><7> % pbpaste | mksh <2><2><3><6><7><7> % pbpaste | yash <3><3><3><7><7><7> tbh i don't think i fully understand why this needs to work this way, or whether there are other cases that should be tested. Open to review obv 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/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..69b3d63f4 100644 --- a/Test/B10getopts.ztst +++ b/Test/B10getopts.ztst @@ -96,3 +96,29 @@ 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 -a + t 1 -w + t 2 -a -w + t 4 -a -w -e -r -a + t 5 -a -w -e -a -w -e + t 5 -a -w -e -r -ax -a + print + ); done +0:OPTIND calculation with and without POSIX_BUILTINS (workers/42248) +>no_posix_builtins: <1><1><3><5><7><6> +>posix_builtins: <2><2><3><6><7><7>