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.4 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 14393 invoked from network); 11 Jul 2022 00:14:27 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 11 Jul 2022 00:14:27 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1657498467; b=Cv+ZQKF+fSf6W6H0sTwNOoi8uKddNc7g7a4iOEjzi1TXnSSmTL23eGjS0CQgRyEDHAdb0LtLAC yLTICDJRuIYofmt5B7Y76U2ahtL/zeWUUUIjduekNiNbFKIfQ74BJbhVOU/k8+UuPutF+f1z3Y ma3yHBJDwIMAyuwxNIzhGOLZ4oyKQPB+hsoIRyj5LhwbU6Pndtvlyalm6GWb4yA6nAd4hrpoUe 4IepdY/PmMN5wU9LNKFpdY/cpTsAjIZxjZhkkeMcNx1A49hk4IVDckJzek1ZA/PJZpH36w1Tjj mTNVLrUc0adBiZqhpBf4d1/MoIdPKbfgQiBAuOU+u4CVjw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (out0.migadu.com) smtp.remote-ip=94.23.1.103; dkim=pass header.d=rjp.ie header.s=key1 header.a=rsa-sha256; dmarc=pass header.from=rjp.ie; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1657498467; bh=n9h6pAFccUKu7b9OzC14TWr+DwbvYoJTObkkL7GhgLo=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Transfer-Encoding:MIME-Version:Message-ID:Date: Subject:Cc:To:From:DKIM-Signature:DKIM-Signature; b=ioo9AefXn5cgW9PLfIN7XYTjvxvW37oG/kXhzeFili6nR/R32DD694LOL+lCrxqA//OI/Q/nfl y7pLNha/D51gdBIOiCk/j+/vy4Pr/qLsdwCaWlOCzjPzGx+25xvRlni9jYWnwUDqQ/7lQm8T1p jjT43Irf0yTGvKmFEsAUT3iv4jZo1Dwk5pCk+9a0Q6O62V3Fe8YaT6QCm1TOHak7JHwsNQh0pB TRypkZRFfpDD07sBzmO0S0DjbBJSUEV61QKzzO3z9TAApz9+2yHFfWkO9QUA5B1sAdGUlZbqZK lH271QcRegOR8RUM3VJt3ctpIKDIt9ZcYtsgW8po12dtYA==; 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:Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References; bh=Uj3q9rXGBNyQoZIIsiYafqfv3/45e78aXGdZTrKDF6U=; b=l0ly8ZG3UH2OSthrVC4xOpPBuP L8OcOd5PR1gAfcpB5GDG3W3G15SiXyc4VJWeTd3XRfqZlHMpGBsJ9fXBQDAZltGRspLyc8L9Z3r+C tNM/1+LZbmvy9Y7kZOMlBFZBt1UaZaKhs9VK0GlqhoyQOebVYeJc1VYf+04MEhxBbPyRSO3lwQzvT 1Cu9ud7zMpu/f2JI5R+wPm5DvJLLeKrm42EbudRNDIHX3AJvijnYBf0YFcDMhHhipr5IDrVnoQIrf NwMm+pl/OoGFgtRGhNh6Tln6Q9fCdb9wI/e9PjtMV8mnd9XdHIfxILqYUF18P5/3Sy+bFfUFiEOyo vSB7j+AQ==; Received: from authenticated user by zero.zsh.org with local id 1oAh4E-000LJg-6X; Mon, 11 Jul 2022 00:14:26 +0000 Authentication-Results: zsh.org; iprev=pass (out0.migadu.com) smtp.remote-ip=94.23.1.103; dkim=pass header.d=rjp.ie header.s=key1 header.a=rsa-sha256; dmarc=pass header.from=rjp.ie; arc=none Received: from out0.migadu.com ([94.23.1.103]:46053) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1oAgz5-000Kt6-2P; Mon, 11 Jul 2022 00:09:08 +0000 X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rjp.ie; s=key1; t=1657498144; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Uj3q9rXGBNyQoZIIsiYafqfv3/45e78aXGdZTrKDF6U=; b=TZ/+NPjHvYVca6EMNtRJTnpJPFPoHy3HcGOJUvgaOpm8/bVeKlbWNe1fRmh53XrN/3/3ry ZgL94t1uguPF0WzG6WaWxqXECJF0xBkZWuDAuXJJUnb9juZ7m2UreOjty/ARRAiKoxvGAa rqOcn4O/ULLNJIhuhQhU3+Sc8p5EX+c= From: ronan@rjp.ie To: zsh-workers@zsh.org Cc: Ronan Pigott Subject: [PATCH] _hosts: use separate cache for known-hosts Date: Sun, 10 Jul 2022 17:08:42 -0700 Message-Id: <20220711000842.14746-1-ronan@rjp.ie> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Migadu-Auth-User: rjp.ie X-Seq: 50408 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: From: Ronan Pigott The goal of this patch is to enable using different known-hosts sources with different commands. The current _hosts cache doesn't allow this because it relies on a single global cache that is used as long as the shell is alive. Consider the following: $ setopt completealias $ alias myssh='ssh -o UserKnownHostsFile=$HOME/myhosts' $ zstyle ':completion:*:complete:myssh:*' known-hosts-files ~/myhosts In the status quo, myssh picks up the usual hosts from ~/.ssh/known_hosts that are stored in _cache_hosts, but with this change it will now correctly use the specified hosts file(s) for completion. The new cache for known-hosts uses an associative array with known-host files as keys and hosts as the values. We could also consider just not caching these values altogether, since the known-hosts files are relatively quick to parse. --- Completion/Unix/Type/_hosts | 59 ++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/Completion/Unix/Type/_hosts b/Completion/Unix/Type/_hosts index 4057fee10..e08337876 100644 --- a/Completion/Unix/Type/_hosts +++ b/Completion/Unix/Type/_hosts @@ -5,7 +5,7 @@ local expl _hosts tmp useip if ! zstyle -a ":completion:${curcontext}:hosts" hosts _hosts; then - if (( $+_cache_hosts == 0 )); then + if (( $+_cache_hosts == 0 )) || ! zstyle -t ":completion:${curcontext}:hosts" use-cache; then # uniquify typeset -gUa _cache_hosts local ipstrip='[:blank:]#[^[:blank:]]#' @@ -23,23 +23,40 @@ if ! zstyle -a ":completion:${curcontext}:hosts" hosts _hosts; then else _cache_hosts=(${(s: :)${(ps:\t:)${${(f)~~"$(/dev/null); then + tmp=$(_call_program hosts ypcat hosts.byname 2>/dev/null); then _cache_hosts+=( ${=${(f)tmp}##${~ipstrip}} ) # If you use YP fi fi + fi - local khostfile - typeset -Ua khostfiles + local khostfile + typeset -Ua khostfiles - # This style specifies a list of files to look up for host names and IP - # addresses, if asked to. The files can contain comma separated host names - # and IP's, and any text on a line after the first whitespace,| or # is - # discarded. ssh's known_hosts files are thus supported. This style defaults - # to the list /etc/ssh/ssh_known_hosts, ~/.ssh/known_hosts - zstyle -a ":completion:${curcontext}:hosts" known-hosts-files khostfiles || - khostfiles=(/etc/ssh/ssh_known_hosts ~/.ssh/known_hosts) + # This style specifies a list of files to look up for host names and IP + # addresses, if asked to. The files can contain comma separated host names + # and IP's, and any text on a line after the first whitespace,| or # is + # discarded. ssh's known_hosts files are thus supported. This style defaults + # to the list /etc/ssh/ssh_known_hosts, ~/.ssh/known_hosts + zstyle -a ":completion:${curcontext}:hosts" known-hosts-files khostfiles || + khostfiles=(/etc/ssh/ssh_known_hosts ~/.ssh/known_hosts) + _hosts_default_cache_policy() { for khostfile in $khostfiles; do + [[ $1 -ot $khostfile ]] && return 0 + done + return 1 + } + + zstyle -s ":completion:$curcontext:hosts" cache-policy user_policy + if [[ -z "$user_policy" ]]; then + zstyle ":completion:$curcontext:" cache-policy _hosts_default_cache_policy + fi + + typeset -A khostcache + _cache_invalid khostcache || _retrieve_cache khostcache + + for khostfile in $khostfiles; do + if ! (( $+khostcache[$khostfile] )); then if [[ -r $khostfile ]]; then khosts=(${(s/,/j/,/u)${(f)"$(<$khostfile)"}%%[ |#]*}) @@ -62,16 +79,22 @@ if ! zstyle -a ":completion:${curcontext}:hosts" hosts _hosts; then fi done } "$khosts[@]" - - if [[ -z $useip ]]; then - khosts=(${${khosts:#(#s)[0-9]##.[0-9]##.[0-9]##.[0-9]##(#e)}:#(#s)[0-9a-f:]##(#e)}) + if [[ -n "$khosts" ]]; then + khostcache[$khostfile]=${(j. .)khosts} fi - _cache_hosts+=($khosts) fi - done - fi + fi + done + _store_cache khostcache khostcache - _hosts=( "$_cache_hosts[@]" ) + local -a khosts=() + for khostfile in $khostfiles; do + khosts+=($=khostcache[$khostfile]) + done + if [[ -z $useip ]]; then + khosts=(${${khosts:#(#s)[0-9]##.[0-9]##.[0-9]##.[0-9]##(#e)}:#(#s)[0-9a-f:]##(#e)}) + fi + _hosts=( "$_cache_hosts[@]" "$khosts[@]" ) fi _wanted hosts expl host \ -- 2.37.0