From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 27525 invoked by alias); 10 Jan 2018 01:32:46 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 42252 Received: (qmail 19125 invoked by uid 1010); 10 Jan 2018 01:32:45 -0000 X-Qmail-Scanner-Diagnostics: from mail-lf0-f65.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.215.65):SA:0(-1.9/5.0):. Processed in 1.466577 secs); 10 Jan 2018 01:32:45 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_PASS, T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: franciscodezuviria@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc; bh=AIkIDIp0zSaz8y8CUFZXyerwNMXE1TDZplJLAhNc03I=; b=OG8uZGitMoV49EoLwWQinDuetU7qm+wMkYvM1+/GKExHvoqAzzsQch6tYbN5QzJZ0/ mTyXGl8vSLanwIGwp8CCe/pJs70Vi5DYIHB8FOORRxEfZKwcqR92xvr+Ot31JZronONa JUOn4FMN3m0dtkJguTxJuzbmxMGAlrlYqlpRnQepN39Jry2YvaxJ8N9CY9hQ0AN/xiam 6nDYjNLUW2/FpDOZB8StgQrlrq5zm1TKfovpMmfxbqdC1OuIC9Tw0N6X6w/y7GJ43i22 SrFCOcWVdQqsybqp5XhF/F/ZwF1z7Ciql07k1MT/Ti0rdq7ZZlcjvs6hMJJqPKWt6B8D rYTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:reply-to:in-reply-to:references :from:date:message-id:subject:to:cc; bh=AIkIDIp0zSaz8y8CUFZXyerwNMXE1TDZplJLAhNc03I=; b=APwml0WDZtmDzmmzuMeQ2/+LTgWKYURLQ4H6S2dKU2wd2DObUrOvSm033kes5xV8UY 4/UN7rocMyhx8javNDcXnjAgwqL3eXDA3JgNHnpOI6qIfvpU6SMb3seYgwmG/tuhNkGe LU2EsBWSMgiesN+ByrA4vX10cxS84/Doz5Vun3aPqGHwdEqa/Z07hKDrgFjfeMOrvpvo TjFc9yOyRYr4+QkKMMOBxPUT8Qao77WYr0I08WsyevEdqn7DBWb73BNsWJRnK5jRiowC hljX536CGvkwC0/6s2vpLNrGyykKPMpMzwb9U9OrFlpIWtSE6+SqfPLxO7KFJbvnbkTp czpg== X-Gm-Message-State: AKwxytcTGoavH6VVfDQSVyjKA5S40ZLprtLWnmHP7AmlysylkgPcY0Fk a0725cauL95miASofbxt4PYsLO+WWJTqX7d+aWp2pw== X-Google-Smtp-Source: ACJfBov4yQBsaDGIKHsG7tGKlO5uWYUxDfg9XqCYt3ks9eRbZDsLmTyJM5w4a3jh2F2Fa+1xHGCiPsEzFKyXOJbnAsM= X-Received: by 10.46.95.84 with SMTP id t81mr3565392ljb.59.1515547960024; Tue, 09 Jan 2018 17:32:40 -0800 (PST) MIME-Version: 1.0 Reply-To: franciscodezuviria@gmail.com In-Reply-To: <043D5A33-6258-4C87-9505-4AEBE17FA9FC@dana.is> References: <0877C4E8-4CA3-453F-A16B-99E576F60E8D@dana.is> <043D5A33-6258-4C87-9505-4AEBE17FA9FC@dana.is> From: =?UTF-8?Q?Francisco_de_Zuvir=C3=ADa_Allende?= Date: Tue, 9 Jan 2018 22:32:38 -0300 Message-ID: Subject: Re: [BUG] getopts OPTIND To: dana Cc: Bart Schaefer , "zsh-workers@zsh.org" Content-Type: text/plain; charset="UTF-8" Such quick, much wow! On Jan 9, 2018 8:58 PM, "dana" wrote: > > On 9 Jan 2018, at 16:57, Bart Schaefer wrote: > >There's a whole thread from 2015 related to this, beginning with workers/35317 > OK, I just read workers/35317 and also the previous thread http://www.zsh.org/mla/workers//2015/msg00196.html and also the previous from 2007 http://www.zsh.org/mla/users/2007/msg01191.html 2 key responses from Peter Stephenson: 2007: > This is the way you'd normally use it: you don't know how many options > there are, if any, until getopts returns 1. > > However, it's still a bit fishy. The internal option index handling logic > has always been a bit curious; I've lost count of the number of bugs we've > had in it. And also On Thu, 28 May 2015 18:17:40 +0100 Peter Stephenson wrote: > This is documented behaviour (well, sort of -- the documentation didn't > say quite what we actually do) so this needs another compatibility fix. > POSIX_BUILTINS seems appropriate. (and then submits a cool patch) I checked man zshbuiltins getopts optstring name [ arg ... ] (...) The first option to be examined may be changed by explicitly assigning to OPTIND. OPTIND has an initial value of 1, and is normally set to 1 upon entry to a shell function and restored upon exit (this is disabled by the POSIX_BUILTINS option). and man zshoptions POSIX_BUILTINS (...) Furthermore, the getopts builtin behaves in a POSIX-compatible fashion in that the associated variable OPTIND is not made local to functions. Now here things get funky. If I try this: ( testfunc() { echo POSIX_BUILTINS is $options[posixbuiltins] echo $*; for i in 1 2 3 4; do echo "OPTIND is $OPTIND, `(shift "$(($OPTIND - 1))"; echo next $1)`"; echo 'I do getopts :a: varname'; getopts ':a:' varname; done } ( setopt POSIX_BUILTINS testfunc -a -w -e -r ) ( unsetopt POSIX_BUILTINS testfunc -a -w -e -r ) ) I get: POSIX_BUILTINS is on -a -w -e -r OPTIND is 1, next -a I do getopts :a: varname OPTIND is 3, next -e I do getopts :a: varname OPTIND is 3, next -e I do getopts :a: varname OPTIND is 4, next -r I do getopts :a: varname POSIX_BUILTINS is off -a -w -e -r OPTIND is 1, next -a I do getopts :a: varname OPTIND is 3, next -e I do getopts :a: varname OPTIND is 3, next -e I do getopts :a: varname OPTIND is 4, next -r I do getopts :a: varname hmmmmmmm... I's it a bug? > Well, i'm not sure about POSIX correctness, but for whatever it's worth, i can > confirm that all of the following are in agreement as to how it should behave: > > ash (BusyBox) > bash > dash > ksh93 > mksh > posh > I think there are 2 things to discuss, one is this (possible) bug. The other is if POSIX_BUILTINS should be on by default or not. If off by default, New users will get bugs in their code if they migrate from bash If on by default, downstream projects will get bugs in their working code This options does other things as well: When this option is set the command builtin can be used to execute shell builtin commands. Parameter assignments specified before shell functions and special builtins are kept after the command completes unless the special builtin is prefixed with the command builtin. Special builtins are ., :, break, continue, declare, eval, exit, export, integer, local, readonly, return, set, shift, source, times, trap and unset. In addition, various error conditions associated with the above builtins or exec cause a non-interactive shell to exit and an interactive shell to return to its top-level processing. So this is a broather subject and may be better discussed in a new thread with a suitable title