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 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 26151 invoked from network); 1 Nov 2023 16:17:09 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 1 Nov 2023 16:17:09 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1698855430; b=O8VSvILto6uTEnh/xit045U4znp/91ORxcfJBkdx57WFX7nTtjWrnWM70VigVRLbma8lWQT7z6 k7ZLbyXy9O5ZwtNjomQili/SR+NGwQheFtjyUfjJMNYRqqE9FLPAn7GiBwwHNupXsbCK8I5D5D QYbYTmbfA4x/VUkGNbBTD5HliDyQshRsH4my4yWzKQ31xUM88Iby8Sj5CS+kzQPRl1kTLhh9bZ gan4qnofHuSIiCtemRZMjtVFhFKM7Nzx7R+TvYp7kD7fU2qk2bXMcfpaq9cCOvDvr5cIvjQOSV evUdW9KaZYavLusYKr/voizAjEv9FQOwsCngSG2P2fGymQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (divok.kovidgoyal.net) smtp.remote-ip=166.78.104.224; dmarc=none header.from=kovidgoyal.net; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1698855430; bh=cfELoYj8J4ySwpglQEMdBLOKiMehE4fz00XZ8H6lrsk=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=J0j7GSiZZob9gnHQJqUsSf6cUI7Pee0ZqQHpj+b6GnFFs8kc9ndaCFjQc00DGdis5bV6Msm/JH 2aDL9/uPmPi0LOeaKZxJ5+V5ge1BYogIImWvk1xmZEE8/wr2EGbaQS8IkG9KbLZs6K+LNkS/ha 9tb0TDGzTaWFVgf9N0nCPzohaSKqEr0cMS73ZpIHcm57wLQhzqkKlviDU4MndL2DcbnnUdSLFu /Swx48nZzcoHpyXSOaryez09VvpQa7Qkpy93M7GJU2E39hpu7mn6WdMO8SRFa4c/yAyJe4fCDk JlKG9IBYqjtYjROJPsV3D7SXaLOW6XWv5ROhQ1CqucKRMQ==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=pXGgbSyFQkATClSv20kLks33YSYNcA91jZGEmXnbNXI=; b=QNg4CpUo88PvH3YMzYayyle451 XrHMitgKHkpo/2ooBNDnIs7eSLrjz0uHhIGgdQ3Kzvacj6sGGBa+Bmw4/2h9D+MA/Lf+rpyPgt3YK 2xsxrADPZoMha7gM+LQJ7TBmB7w9RprVLhUbEtQQVpu14gkf00FWSf1FEST8amMlgFjShw4AfuQX2 x5TMLxzZF2MjVbICM1rXIv+YNgo1QOtFYfj7ZrNHSCQF0PItMHwcccM3bTGsj9koEh394ApVB3awt tK62+k4i4aRRfyBpNvlAdl5F+cMHKvMo1H+gXWk7GS+ZJk7LIv4oLcpbgVfWZ3mxcGk4iMxtZFnYd Clb0LdwQ==; Received: by zero.zsh.org with local id 1qyDu0-000JHx-US; Wed, 01 Nov 2023 16:17:08 +0000 Authentication-Results: zsh.org; iprev=pass (divok.kovidgoyal.net) smtp.remote-ip=166.78.104.224; dmarc=none header.from=kovidgoyal.net; arc=none Received: from divok.kovidgoyal.net ([166.78.104.224]:48810) by zero.zsh.org with esmtp id 1qyDtL-000IxX-D7; Wed, 01 Nov 2023 16:16:28 +0000 Received: from [192.168.1.5] (unknown [114.143.34.5]) by divok.kovidgoyal.net (Postfix) with ESMTPSA id 8C7B0C021F; Wed, 1 Nov 2023 21:46:25 +0530 (IST) Date: Wed, 1 Nov 2023 21:46:19 +0530 From: Kovid Goyal To: Oliver Kiddle Cc: zsh-workers@zsh.org Subject: Re: Bug in sudo completions Message-ID: References: <13019-1698778100.248612@OXOy.8kHS._hjm> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <13019-1698778100.248612@OXOy.8kHS._hjm> X-Seq: 52264 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: Thanks! On Tue, Oct 31, 2023 at 07:48:20PM +0100, Oliver Kiddle wrote: > Kovid Goyal wrote: > > Completions for sudo fail when specifying environment variables on the > > command line. For example: > > > > sudo a=1 ls - > > > > gives > > > > No matches for: `file' > > Yes, and I can see that it is annoying that if you use a variable > it breaks completion on what follows. > > A patch is attached. _normal is not flexible enough to adapt to this > case so this is fairly manual. It also needs a certain amount of special > care over $curcontext. > > > Note that VAR=value can occur before -i or -s and before command. > > This patch handles that in a fairly rudimentary manner. I also noted > that it is not applicable with -l or -v. There are probably more option > exclusions that could be applied to them. > > It also adds the -N/--no-update option which must be new since _sudo was > last updated. extended_glob is in _comp_options so setting that > explicitly was superfluous. > > Oliver > > diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo > index 29e5e6d75..c334c6765 100644 > --- a/Completion/Unix/Command/_sudo > +++ b/Completion/Unix/Command/_sudo > @@ -1,9 +1,7 @@ > #compdef sudo sudoedit > > -setopt localoptions extended_glob > - > -local environ e cmd cpp > -local -a args _comp_priv_prefix > +local curcontext="$curcontext" environ e cmd cpp ret=1 > +local -a context state line args _comp_priv_prefix > local -A opt_args > > zstyle -a ":completion:${curcontext}:" environ environ > @@ -20,20 +18,21 @@ args=( > '(-g --group)'{-g+,--group=}'[run command as the specified group name or ID]:group:_groups' > '(-)'{-h,--help}'[display help message and exit]' > '(-h --host)'{-h+,--host=}'[run command on host]:host:_hosts' > - '(-K --remove-timestamp)'{-K,--remove-timestamp}'[remove timestamp file completely]' > - '(-k --reset-timestamp)'{-k,--reset-timestamp}'[invalidate timestamp file]' > + '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-K,--remove-timestamp}'[remove timestamp file completely]' > + '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-k,--reset-timestamp}'[invalidate timestamp file]' > \*{-l,--list}"[list user's privileges or check a specific command]" > '(-n --non-interactive)'{-n,--non-interactive}'[non-interactive mode, no prompts are used]' > + '(-k --reset-timestamp -K --remove-timestamp -N --no-update)'{-N,--no-update}"[don't update user's cached credentials]" > '(-p --prompt)'{-p+,--prompt=}'[use the specified password prompt]:prompt' > '(-R --chroot)'{-R+,--chroot=}'[change the root directory before running command]:directory:_directories' > '(-r --role)'{-r+,--role=}'[create SELinux security context with specified role]: :_selinux_roles' > '(-S --stdin)'{-S,--stdin}'[read password from standard input]' > '(-t --type)'{-t+,--type=}'[create SELinux security context with specified type]: :_selinux_types' > '(-T --command-timeout)'{-T+,--command-timeout=}'[terminate command after specified time limit]:timeout' > - '(-U --other-user)'{-U+,--other-user=}'[in list mode, display privileges for user]:user:_users' > + '(-U --other-user -v --validate)'{-U+,--other-user=}'[in list mode, display privileges for user]:user:_users' > '(-u --user)'{-u+,--user=}'[run command (or edit file) as specified user]:user:_users' > '(-)'{-V,--version}'[display version information and exit]' > - '(-v --validate)'{-v,--validate}"[update user's timestamp without running a command]" > + '(-v --validate -U --other-user *)'{-v,--validate}"[update user's timestamp without running a command]" > ) > > # Does -e appears before the first word that doesn't begin with a hyphen? > @@ -45,10 +44,6 @@ if [[ $service = sudoedit ]] || (( $words[(i)-e] < $words[(i)^(*sudo|-[^-]*)] )) > args=( -A "-*" $args '!(-V --version -h --help)-e' '*:file:_files' ) > else > cmd="$words[1]" > - cpp='_comp_priv_prefix=( > - $cmd -n > - ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} > - )' > args+=( > '(-e --edit 1 *)'{-e,--edit}'[edit files instead of running a command]' \ > '(-s --shell)'{-s,--shell}'[run shell as the target user; a command may also be specified]' \ > @@ -58,9 +53,40 @@ else > '(-E -i --login -s --shell -e --edit)--preserve-env=-[preserve user environment when running command]::environment variable:_sequence _parameters -g "*export*"' \ > '(-H --set-home -i --login -s --shell -e --edit)'{-H,--set-home}"[set HOME variable to target user's home dir]" \ > '(-P --preserve-groups -i -login -s --shell -e --edit)'{-P,--preserve-groups}"[preserve group vector instead of setting to target's]" \ > - "(-)1: :{ $cpp; _command_names -e }" > - "*:: :{ $cpp; _normal }" > + '*:: :->normal' > ) > fi > > -_arguments -s -S $args > +_arguments -s -S $args && ret=0 > + > +if [[ $state = normal ]]; then > + _comp_priv_prefix=( > + $cmd -n > + ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} > + ) > + if (( $+opt_args[-l] || $+opt_args[--list] )); then > + _normal -p $service > + return > + fi > + while [[ $words[1] = *=* ]]; do > + if (( CURRENT == 1 )); then > + compstate[parameter]="${PREFIX%%\=*}" > + compset -P 1 '*=' > + _value && ret=0 > + return ret > + fi > + shift words > + (( CURRENT-- )) > + done > + if (( CURRENT == 1 )); then > + curcontext="${curcontext%:*:*}:-command-:" > + _alternative \ > + 'commands:: _command_names -e' \ > + 'options:option:(-s --shell -l --login)' \ > + 'parameters: :_parameters -g "*export*~*readonly*" -qS=' && ret=0 > + else > + _normal > + fi > +fi > + > +return ret -- _____________________________________ Dr. Kovid Goyal https://www.kovidgoyal.net https://calibre-ebook.com _____________________________________