Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH] Completion: Add _hostname (+ a question)
- X-seq: zsh-workers 43044
- From: dana <dana@xxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: [PATCH] Completion: Add _hostname (+ a question)
- Date: Sun, 17 Jun 2018 17:20:37 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=oAt+Mu3s1WfMLWcAIEc+SQTQWB2rYi7Ez4gMl97EaPk=; b=NCVGHaVwKWbkM+i8+xaxvx/+P6hGk3Rw0t+sD3pKs81mMUdzSZx2pMWs1K9AM7ADcd RWMbj4PSg4zOd68L15I22LmJNJEO42iSYjinSD/gflpn/ZN3WPUg9DkaeYVZc+VbACrf h85o2LVgGo/XWvfkoJyurkMxoF0q7/CVd6JXFmLLISD+/Iu1azeSALvD80sc5A1x1b9F klr6DdLygh6xeaeRy17zYRL1VxdD4aTO12hq9z0fDQDk3f9ZGCLstSpj12FaRLUBoiNV snng2raQ47knCqQLqf4ha2BeueyEhKJWAFkxoOO4x0c2SclCT9EMflcSSIDnkE33TSdN qCPQ==
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
This is a new completion function for the hostname command.
For Dragonfly i needed to complete locally bound IP addresses (see bottom). The
_bind_addresses type function was presumably intended for just this kind of
thing... except that all it does on many systems is call _hosts. That doesn't
seem appropriate at all in cases like this, nor in the cases where the function
is already being used, such as `ssh -b` and `rsync --address`.
Instead of doing my own thing here, would there be any issue with my changing
_bind_addresses to unconditionally return the actual interface addresses using a
similar method (maybe with a few flags to control inclusion of loop-backs, &c.)?
If so, i'll prepare an improved patch.
dana
diff --git a/Completion/Unix/Command/_hostname b/Completion/Unix/Command/_hostname
new file mode 100644
index 000000000..22127fed7
--- /dev/null
+++ b/Completion/Unix/Command/_hostname
@@ -0,0 +1,77 @@
+#compdef hostname
+
+local ret=1
+local -a context line state state_descr args aopts tmp
+local -A opt_args
+
+if _pick_variant net-tools=--yp unix --help; then
+ args=(
+ '(: * -)'{-h,--help}'[display help information]'
+ '(: * -)'{-V,--version}'[display version information]'
+ + '(g)' # Get
+ '(: gs s)'{-A,--all-fqdns}'[display FQDNs resolved from network interface addresses]'
+ '(: gs s)'{-d,--domain}'[display DNS domain only]'
+ '(: gs s)'{-f,--fqdn,--long}'[display host name with DNS domain (FQDN)]'
+ '(: gs s)'{-i,--ip-address}'[display IP addresses for host (via DNS resolution)]'
+ '(: gs s)'{-I,--all-ip-addresses}'[display IP addresses for host (via network interfaces)]'
+ '(: gs s)'{-s,--short}'[display short host name only]'
+ + '(gs)' # Get/set
+ '(g)'{-y,--yp,--nis}'[display NIS domain only]'
+ + s # Set
+ '(-b --boot g)'{-b,--boot}'[always set a host name]'
+ '(: -F --file g)'{-F+,--file=}'[read host name to set from specified file]: :_files'
+ + o
+ '(-F --file g)1: :->host-or-domain'
+ )
+else
+ aopts=( -A '-*' )
+ args=(
+ '(-4 -6 -d -f -i -r -s)1: :_guard "^-*" "host name"'
+ '(: -4 -6 -d -f)-s[display short host name only]'
+ )
+
+ case $OSTYPE in
+ darwin*|freebsd*)
+ args+=(
+ '(: -d -s)-f[display host name with DNS domain (FQDN)]'
+ )
+ ;| # MATCH AGAIN
+ freebsd*)
+ args+=(
+ '(: -f -s)-d[display DNS domain only]'
+ )
+ ;;
+ dragonfly*)
+ args+=(
+ '(: -6 -r)-4[use first IPv4 address on interface (with -i)]'
+ '(: -4 -r)-6[use first IPv6 address on interface (with -i)]'
+ '(: -r)-i+[retrieve host name via specified interface]: :_net_interfaces'
+ '(: -4 -6 -i)-r+[retrieve host name via specified IP address]: :->ip-address'
+ )
+ ;;
+ esac
+fi
+
+_arguments -s -S $aopts : $args && ret=0
+
+case $state in
+ host-or-domain)
+ [[ -prefix -* ]] ||
+ if [[ -n ${opt_args[(i)*-(-y|--yp|--nis)]} ]]; then
+ _message -e nis-domains 'NIS domain' && ret=0
+ else
+ _message -e host-names 'host name' && ret=0
+ fi
+ ;;
+ ip-address)
+ # @todo Should we have a proper type function for local IPs?
+ tmp=( ${(f)"$( _call_program ip-addresses ifconfig -a )"} )
+ tmp=( ${(@M)tmp##(|[[:space:]]##)inet(|6)(|:)[[:space:]]*} )
+ tmp=( ${(@)tmp#*inet(|6)(|:)[[:space:]]##} )
+ tmp=( ${(@)tmp%%[^0-9A-Fa-f:.]*} )
+ tmp=( ${(@u)${(@)tmp//:/\\:}} )
+ _describe -t ip-addresses 'IP address' tmp && ret=0
+ ;;
+esac
+
+return ret
Messages sorted by:
Reverse Date,
Date,
Thread,
Author