From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 554 invoked by alias); 18 Jun 2018 00:35:45 -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: 43047 Received: (qmail 5269 invoked by uid 1010); 18 Jun 2018 00:35:45 -0000 X-Qmail-Scanner-Diagnostics: from mail-io0-f193.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.223.193):SA:0(-1.9/5.0):. Processed in 1.035125 secs); 18 Jun 2018 00:35: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,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_PASS,T_DKIMWL_WL_MED,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: dana@dana.is X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dana-is.20150623.gappssmtp.com; s=20150623; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=+vkK8J+OBqQSV5kbNjSLppEFDS9Qs68lRpMwgIHiscc=; b=rogEYRyxNDg4bS3OrBuIduuqtNAjl0jnb0XIvQ7UVNnhfyGMRXJoxe8LnhWdvcSSgn ByS0zpV44DmA2mVZ0qhnfsLUHAgOYd4MbughALPMG4+Rf73uPMbZfzIh9aLOl5iUIcFr uWXYYG90CLlZQjk7l9oKN/J8tYNoFLjaJ5ynLScPniFKedr+xLu6bbM056H9KHtoAJTP VNOEap6MtBZBls3WBfPwWLuEn45E7nZ2hZiXHWdIyWGVm1kU3nMJBOG9BwOrHVa+KFzU 93aKPekPNvNDBX6gYqf3Ue9L9C0nTgUFkmrT4vS8jToVnwn2hq7rM9FwDevzSPtSKx/H 6Fmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=+vkK8J+OBqQSV5kbNjSLppEFDS9Qs68lRpMwgIHiscc=; b=PnU0OdP998dbD5+bPwlVsiXnq2w/k/mCF6gsI4X8yGHMOzxpm8fnGLlO1qlhP+MbsP o9qI8K6JEEBhTzY752NVjKKvZFzpipkrVilzn8nUwypUtU1/dfZrRdqI+1E9zA3B3aKC gOeD/wM/KFgkYlFRCR55dyOXg4H+7G/UnxjdD0s7+WxnmPmQazYJPAY8xxc8r7coXda+ 3E8Ic3suXdnwpbA/ym8yQXosFczYQr2S5TFd85gTZOoie+WCdEC5+Ce5aqz3pqPChwXI RIigfzafYJ11Sw5V9ei2NMTuAPO7WaS2VIS4agNt1FlE288OzZ7gl7yfOuHO/fPvOQGT jkfQ== X-Gm-Message-State: APt69E09dTY7zy+Y75eBUxcjeQ2Xt05eSmWiHW/MiqWZ8hBgMAreIe+w h7B5UpheNfDtlr/lQzLgS4Y++c/6TAg= X-Google-Smtp-Source: ADUXVKLHQzlqRaAMWv0i9BQpwoZkIqvxznvTxLOZosD+0XzZuL4jWabhWEBvB+uJ8gzz4KLMX3WBjg== X-Received: by 2002:a6b:2607:: with SMTP id m7-v6mr8134568iom.42.1529282141731; Sun, 17 Jun 2018 17:35:41 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 10.3 \(3273\)) Subject: Re: [PATCH] Completion: Add _hostname (+ a question) From: dana In-Reply-To: <1535.1529276526@thecus> Date: Sun, 17 Jun 2018 19:35:40 -0500 Cc: Zsh workers Content-Transfer-Encoding: quoted-printable Message-Id: <2DD0579A-F489-4FAF-A6F5-3F5A45863DE0@dana.is> References: <6B9F94D1-6E43-4970-8571-C935B6167EB0@dana.is> <1535.1529276526@thecus> To: Oliver Kiddle X-Mailer: Apple Mail (2.3273) On 17 Jun 2018, at 18:02, Oliver Kiddle wrote: >We can't do this. On some systems, e.g. Solaris, this will change the >system's hostname to "--help" if someone tries to complete hostname as >root. Hm, that's not the behaviour i get with either Oracle's or Illumos's = hostname. (I was actually under the impression that Solaris didn't *have* a = hostname command, which is why i didn't bother with it. And if i had, i would = have run into that issue where OSTYPE isn't good enough to tell the different = variants apart.) You're right though, it's risky =E2=80=94 even on some Linux = systems. I think the attached should be good enough (better than nothing = certainly) until we can come up with a better way to safely differentiate commands like = these. Maybe _pick_variant could take an option to evaluate arbitrary code or something, not sure. Included patch has the reworked variant handling and _bind_addresses. Thanks! dana diff --git a/Completion/Unix/Command/_hostname = b/Completion/Unix/Command/_hostname new file mode 100644 index 000000000..40a3189ae --- /dev/null +++ b/Completion/Unix/Command/_hostname @@ -0,0 +1,76 @@ +#compdef hostname + +local variant=3D$OSTYPE ret=3D1 +local -a context line state state_descr args aopts=3D( -A '-*' ) +local -A opt_args + +# May be overridden below +args=3D( '(-4 -6 -d -f -i -r -s)1: :_guard "^-*" "host name"' ) + +# On badly behaved systems it may be dangerous to call `hostname = --help` or +# similar, so for now we'll avoid _pick_variant and just trust this. In = the +# future, we should replace this by a method that can safely detect = Solaris, +# BusyBox, etc. (@todo) +[[ $OSTYPE =3D=3D linux* ]] && +(( $+commands[nisdomainname] || $+commands[ypdomainname] )) && +variant+=3D-net-tools + +case $variant in + *-net-tools) + aopts=3D( ) + args=3D( + '(: * -)'{-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=3D}'[read host name to set from = specified file]: :_files' + + o + '(-F --file g)1: :->host-or-domain' + ) + ;; + darwin*|dragonfly*|freebsd*|netbsd*|openbsd*) + args+=3D( + '(: -4 -6 -d -f)-s[display short host name only]' + ) + ;| # MATCH AGAIN + darwin*|freebsd*) + args+=3D( + '(: -d -s)-f[display host name with DNS domain (FQDN)]' + ) + ;| # MATCH AGAIN + freebsd*) + args+=3D( + '(: -f -s)-d[display DNS domain only]' + ) + ;; + dragonfly*) + args+=3D( + '(: -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]: = :_bind_addresses' + ) + ;; +esac + +_arguments -s -S $aopts : $args && ret=3D0 + +[[ $state =3D=3D host-or-domain ]] && { + [[ -prefix -* ]] || + if [[ -n ${opt_args[(i)*-(-y|--yp|--nis)]} ]]; then + _message -e nis-domains 'NIS domain' && ret=3D0 + else + _message -e host-names 'host name' && ret=3D0 + fi +} + +return ret diff --git a/Completion/Unix/Type/_bind_addresses = b/Completion/Unix/Type/_bind_addresses index 3460b7959..6042eaf1e 100644 --- a/Completion/Unix/Type/_bind_addresses +++ b/Completion/Unix/Type/_bind_addresses @@ -1,15 +1,17 @@ #autoload =20 -local expl - -case $OSTYPE in - aix*) _hosts "$@" ;; - darwin*|freebsd*|dragonfly*) _hosts "$@" ;; - irix*) _hosts "$@" ;; - # Couldn't find anything special for linux except for /proc/net/dev - # Is there any proc file which gives the formatted ip? - linux*) ;& - *) - _wanted bind-addresses expl 'bind address' compadd "$@" - \ - ${${${(M)${(f)"$(ifconfig -a)"}:#*addr:*}##*addr:( |)}%%(/| )*} -esac +# @todo In the future it might be useful to have this function take a = glob or +# similar to filter out loop-back addresses, only return IPv4/6, etc. + +local -a expl tmp cmd=3D( ifconfig -a ) + +# A lot of Linux systems have ifconfig, but this is probably safer (and = it's +# parsed the same way) +[[ $OSTYPE =3D=3D linux* ]] && (( $+commands[ip] )) && cmd=3D( ip addr = show ) + +tmp=3D( ${(f)"$( _call_program bind-addresses $cmd )"} ) +tmp=3D( ${(@M)tmp##(|[[:space:]]##)inet(|6)(|:)[[:space:]]*} ) +tmp=3D( ${(@)tmp#*inet(|6)(|:)[[:space:]]##} ) +tmp=3D( ${(@)tmp%%[^0-9A-Fa-f:.]*} ) + +_wanted bind-addresses expl 'bind address' compadd -a "$@" - tmp