zsh-users
 help / color / mirror / code / Atom feed
* How to complete alias after alias of sudo?
@ 2017-05-31  6:46 ` Han Pingtian
  2017-05-31  8:43   ` Peter Stephenson
  0 siblings, 1 reply; 10+ messages in thread
From: Han Pingtian @ 2017-05-31  6:46 UTC (permalink / raw)
  To: zsh-users

Hi,

I have

    alias sd='sd -i '
    alias foo=ls

And 

    % sd foo
    [sudo] password for hpt:
    anaconda-ks.cfg  initial-setup-ks.cfg  install-ocfedora.sh

works just fine. 

But looks like I cannot complete the second alias by <tab>:

    % sd f<tab>
    external command
    fold                                   foomatic-kitload
    ...

only external commands listed.

Is it possible to complete alias here? 

Thanks in advance!


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-05-31  6:46 ` How to complete alias after alias of sudo? Han Pingtian
@ 2017-05-31  8:43   ` Peter Stephenson
  2017-06-01  5:43     ` Han Pingtian
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2017-05-31  8:43 UTC (permalink / raw)
  To: zsh-users

On Wed, 31 May 2017 14:46:55 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> But looks like I cannot complete the second alias by <tab>:
> 
>     % sd f<tab>
>     external command
>     fold                                   foomatic-kitload
>     ...
> 
> only external commands listed.

This is down to how completion for "sd" is defined.  As far as I can see
that's not a standard completion --- I would guess it's simply adding
external commands rather than all commands in that context.  (I've
confirmed that _precommand, which is the nearest standard completion
that I can think of, does complete aliases in the corresponding case.)

pws


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-05-31  8:43   ` Peter Stephenson
@ 2017-06-01  5:43     ` Han Pingtian
  2017-06-01  9:19       ` Peter Stephenson
  0 siblings, 1 reply; 10+ messages in thread
From: Han Pingtian @ 2017-06-01  5:43 UTC (permalink / raw)
  To: zsh-users

On Wed, May 31, 2017 at 09:43:10AM +0100, Peter Stephenson wrote:
> On Wed, 31 May 2017 14:46:55 +0800
> Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> > But looks like I cannot complete the second alias by <tab>:
> > 
> >     % sd f<tab>
> >     external command
> >     fold                                   foomatic-kitload
> >     ...
> > 
> > only external commands listed.
> 
> This is down to how completion for "sd" is defined.  As far as I can see
> that's not a standard completion --- I would guess it's simply adding
> external commands rather than all commands in that context.  (I've
> confirmed that _precommand, which is the nearest standard completion
> that I can think of, does complete aliases in the corresponding case.)
> 
> pws

Looks like if change the line 52 of _sudo from

    52     '(-)1:command: _command_names -e'

to

    52     '(-)1:command: _command_names'

then alias will be listed. But looks like it's not a good idea to
complete alias after sudo? I'll try to remember all those aliases and
type them directly.


Thanks.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-01  5:43     ` Han Pingtian
@ 2017-06-01  9:19       ` Peter Stephenson
  2017-06-02  3:57         ` Han Pingtian
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2017-06-01  9:19 UTC (permalink / raw)
  To: zsh-users

On Thu, 1 Jun 2017 13:43:20 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> Looks like if change the line 52 of _sudo from
> 
>     52     '(-)1:command: _command_names -e'
> 
> to
> 
>     52     '(-)1:command: _command_names'
> 
> then alias will be listed. But looks like it's not a good idea to
> complete alias after sudo?

With -s or -i (which is what you said yesterday, and it went slightly
over my head, so I missed the point), it is executing a shell command
line.  Then actually I think there *is* an argument for making the
completion more complete. I'm not sure how good the argument is, though
--- the -s and -i arguments are potentially dangerous and ideally the
shell being run is a pruned version that may not have the same aliases
defined.  That said, what you complete isn't determining how safe your
sudo environment is anyway, so maybe this is a non-issue.

Anyway, we could do this.

pws

diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo
index aa7a1a4..0b09ded 100644
--- a/Completion/Unix/Command/_sudo
+++ b/Completion/Unix/Command/_sudo
@@ -41,6 +41,8 @@ if [[ $service = sudoedit ]] || (( $words[(i)-e] < $words[(i)^(*sudo|-[^-]*)] ))
   args=( -A "-*" $args '!(-V --version -h --help)-e' '*:file:_files' )
 else
   cmd="$words[1]"
+  local ext
+  (( ${words[(I)-[is]]} == 0 )) && ext=" -e"
   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]' \
@@ -49,7 +51,7 @@ else
     '(-E --preserve-env -i --login -s --shell -e --edit)'{-E,--preserve-env}'[preserve user environment when running command]' \
     '(-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:command: _command_names -e'
+    "(-)1:command: _command_names$ext"
     '*::arguments:{ _comp_priv_prefix=( $cmd -n ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} ) ; _normal }'
   )
 fi


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-01  9:19       ` Peter Stephenson
@ 2017-06-02  3:57         ` Han Pingtian
  2017-06-02  8:46           ` Peter Stephenson
  0 siblings, 1 reply; 10+ messages in thread
From: Han Pingtian @ 2017-06-02  3:57 UTC (permalink / raw)
  To: zsh-users

On Thu, Jun 01, 2017 at 10:19:45AM +0100, Peter Stephenson wrote:
> With -s or -i (which is what you said yesterday, and it went slightly
> over my head, so I missed the point), it is executing a shell command
> line.  Then actually I think there *is* an argument for making the
> completion more complete. I'm not sure how good the argument is, though
> --- the -s and -i arguments are potentially dangerous and ideally the
> shell being run is a pruned version that may not have the same aliases
> defined.  That said, what you complete isn't determining how safe your
> sudo environment is anyway, so maybe this is a non-issue.
> 
> Anyway, we could do this.
> 
> pws
> 
> diff --git a/Completion/Unix/Command/_sudo b/Completion/Unix/Command/_sudo
> index aa7a1a4..0b09ded 100644
> --- a/Completion/Unix/Command/_sudo
> +++ b/Completion/Unix/Command/_sudo
> @@ -41,6 +41,8 @@ if [[ $service = sudoedit ]] || (( $words[(i)-e] < $words[(i)^(*sudo|-[^-]*)] ))
>    args=( -A "-*" $args '!(-V --version -h --help)-e' '*:file:_files' )
>  else
>    cmd="$words[1]"
> +  local ext
> +  (( ${words[(I)-[is]]} == 0 )) && ext=" -e"
>    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]' \
> @@ -49,7 +51,7 @@ else
>      '(-E --preserve-env -i --login -s --shell -e --edit)'{-E,--preserve-env}'[preserve user environment when running command]' \
>      '(-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:command: _command_names -e'
> +    "(-)1:command: _command_names$ext"
>      '*::arguments:{ _comp_priv_prefix=( $cmd -n ${(kv)opt_args[(I)(-[ugHEP]|--(user|group|set-home|preserve-env|preserve-groups))]} ) ; _normal }'
>    )
>  fi
Thanks. But looks like the situation is a bit different. I have aliased
'sd' to 'sudo -i ', so another alias after 'sd' will be expanded before
command running. So the alias won't be passed to another shell. But
looks like it's not easy to enable alias completion in this situation?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-02  3:57         ` Han Pingtian
@ 2017-06-02  8:46           ` Peter Stephenson
  2017-06-02 18:26             ` Daniel Shahaf
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2017-06-02  8:46 UTC (permalink / raw)
  To: zsh-users

On Fri, 2 Jun 2017 11:57:46 +0800
Han Pingtian <hanpt@linux.vnet.ibm.com> wrote:
> Thanks. But looks like the situation is a bit different. I have aliased
> 'sd' to 'sudo -i ', so another alias after 'sd' will be expanded before
> command running. So the alias won't be passed to another shell. But
> looks like it's not easy to enable alias completion in this situation?

Possibly my patch --- which I think ought to do what you want in this
as a side effect --- is the best we've got?  To be clear:

Your case: you have a special alias for "sudo -i" so that the next word
is expanded at an alias in the current shell --- before sudo is called.

Standard case: "sudo -i" runs the command in a shell, so it will use
whatever shell environment has been provided in the sudo environment.
In general we don't know what that is, and quite like using the local
environment for completing functions and aliases isn't right.  But
without much more digging this may be the best solution.

pws


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-02  8:46           ` Peter Stephenson
@ 2017-06-02 18:26             ` Daniel Shahaf
  2017-06-05  8:38               ` Peter Stephenson
  0 siblings, 1 reply; 10+ messages in thread
From: Daniel Shahaf @ 2017-06-02 18:26 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote on Fri, 02 Jun 2017 09:46 +0100:
> Standard case: "sudo -i" runs the command in a shell, so it will use
> whatever shell environment has been provided in the sudo environment.
> In general we don't know what that is, and quite like using the local
> environment for completing functions and aliases isn't right.  But
> without much more digging this may be the best solution.

There's no easy way for us to complete aliases for root; we don't even
know what shell sudo will run its command under.

However, we could add a layer of indirection so people can fill this in
their own configs:

[[[
_arguments ... '(-)1:command:_sudo_command'

_sudo_command() {
  if -i or -s passed:
    _sudo_aliases
  else
    _command_names
  fi
}

_sudo_aliases() {
  _command_names -e
}
]]]

... and document that _sudo_aliases is intended to be
overwritten/redefined by a user's .zshrc file.


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-02 18:26             ` Daniel Shahaf
@ 2017-06-05  8:38               ` Peter Stephenson
  2017-06-05 14:27                 ` Bart Schaefer
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2017-06-05  8:38 UTC (permalink / raw)
  To: zsh-users

On Fri, 02 Jun 2017 18:26:01 +0000
Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> However, we could add a layer of indirection so people can fill this in
> their own configs:
> 
> ... and document that _sudo_aliases is intended to be
> overwritten/redefined by a user's .zshrc file.

That seems reasonable; the only question is what it defaults to doing,
which isn't a big issue and the current behaviour ought to be fine.

pws


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-05  8:38               ` Peter Stephenson
@ 2017-06-05 14:27                 ` Bart Schaefer
  2017-06-05 22:56                   ` Daniel Shahaf
  0 siblings, 1 reply; 10+ messages in thread
From: Bart Schaefer @ 2017-06-05 14:27 UTC (permalink / raw)
  To: zsh-users

On Jun 5,  9:38am, Peter Stephenson wrote:
} Subject: Re: How to complete alias after alias of sudo?
}
} On Fri, 02 Jun 2017 18:26:01 +0000
} Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
} > However, we could add a layer of indirection so people can fill this in
} > their own configs:
} > 
} > ... and document that _sudo_aliases is intended to be
} > overwritten/redefined by a user's .zshrc file.
} 
} That seems reasonable; the only question is what it defaults to doing,
} which isn't a big issue and the current behaviour ought to be fine.

Why do this with a function override rather than a zstyle?


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: How to complete alias after alias of sudo?
  2017-06-05 14:27                 ` Bart Schaefer
@ 2017-06-05 22:56                   ` Daniel Shahaf
  0 siblings, 0 replies; 10+ messages in thread
From: Daniel Shahaf @ 2017-06-05 22:56 UTC (permalink / raw)
  To: zsh-users

Bart Schaefer wrote on Mon, 05 Jun 2017 07:27 -0700:
> On Jun 5,  9:38am, Peter Stephenson wrote:
> } Subject: Re: How to complete alias after alias of sudo?
> }
> } On Fri, 02 Jun 2017 18:26:01 +0000
> } Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> } > However, we could add a layer of indirection so people can fill this in
> } > their own configs:
> } > 
> } > ... and document that _sudo_aliases is intended to be
> } > overwritten/redefined by a user's .zshrc file.
> } 
> } That seems reasonable; the only question is what it defaults to doing,
> } which isn't a big issue and the current behaviour ought to be fine.
> 
> Why do this with a function override rather than a zstyle?

Good point.


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2017-06-05 22:56 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <CGME20170531082709epcas5p4cb74739bb440049fd96ca18a08489bbd@epcas5p4.samsung.com>
2017-05-31  6:46 ` How to complete alias after alias of sudo? Han Pingtian
2017-05-31  8:43   ` Peter Stephenson
2017-06-01  5:43     ` Han Pingtian
2017-06-01  9:19       ` Peter Stephenson
2017-06-02  3:57         ` Han Pingtian
2017-06-02  8:46           ` Peter Stephenson
2017-06-02 18:26             ` Daniel Shahaf
2017-06-05  8:38               ` Peter Stephenson
2017-06-05 14:27                 ` Bart Schaefer
2017-06-05 22:56                   ` Daniel Shahaf

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).