26763: fix problem on failed cd -s to relative path
[zsh.git] / Completion / Unix / Type / _hosts
blobc9339994552a196c76d55230c48472b671c3ad6e
1 #compdef ftp rwho rup xping traceroute host aaaa zone mx ns soa txt
3 # avoid calling variable "hosts", it's an obvious candidate for use in
4 #  zstyle -e '*' hosts 'reply=($hosts)'
5 local expl _hosts tmp useip
7 if ! zstyle -a ":completion:${curcontext}:hosts" hosts _hosts; then
8   if (( $+_cache_hosts == 0 )); then
9     # uniquify
10     typeset -gUa _cache_hosts
11     local ipstrip='[:blank:]#[^[:blank:]]#'
13     # This boolean style determines whether hosts completes IP addresses. The
14     # default is no (not using IP addresses).
15     zstyle -t ":completion:${curcontext}:hosts" use-ip && useip=yes
16     [[ -n $useip ]] && ipstrip=
17     if (( ${+commands[getent]} )); then
18       # pws: we were using the horrible ": ${(A)...:=}" syntax to assign
19       # to _cache_hosts, overriding the typeset as well as being unreadable
20       # and having obscure splitting behaviour.  Why?  We've just
21       # tested _cache_hosts doesn't exist.
22       _cache_hosts=(${(s: :)${(ps:\t:)${(f)~~"$(_call_program hosts getent hosts 2>/dev/null)"}##${~ipstrip}}})
23     else
24       _cache_hosts=(${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}##${~ipstrip}}})
25       if (( ${+commands[ypcat]} )) &&
26         tmp=$(_call_program hosts ypcat hosts.byname 2>/dev/null); then
27         _cache_hosts+=( ${=${(f)tmp}##${~ipstrip}} ) # If you use YP
28       fi
29     fi
31     local khostfile
32     typeset -Ua khostfiles
34     # This style specifies a list of files to look up for host names and IP
35     # addresses, if asked to. The files can contain comma separated host names
36     # and IP's, and any text on a line after the first whitespace,| or # is
37     # discarded. ssh's known_hosts files are thus supported. This style defaults
38     # to the list /etc/ssh/ssh_known_hosts, ~/.ssh/known_hosts
39     zstyle -a ":completion:${curcontext}:hosts" known-hosts-files khostfiles ||
40     khostfiles=(/etc/ssh/ssh_known_hosts ~/.ssh/known_hosts)
42     for khostfile in $khostfiles; do
43       if [[ -r $khostfile ]]; then
44         khosts=(${(s:,:)${(j:,:)${(u)${(f)"$(<$khostfile)"}%%[ |#]*}}})
45         if [[ -z $useip ]]; then
46           khosts=(${${khosts:#(#s)[0-9]##.[0-9]##.[0-9]##.[0-9]##(#e)}:#(#s)[0-9a-f:]##(#e)})
47         fi
48         _cache_hosts+=($khosts)
49       fi
50     done
51   fi
53   _hosts=( "$_cache_hosts[@]" )
56 _wanted hosts expl host \
57     compadd -M 'm:{a-zA-Z}={A-Za-z} r:|.=* r:|=*' -a "$@" - _hosts