fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
a53db7256
master
1# localization: skip(private) 2function __fish_print_hostnames -d"Print a list of known hostnames" 3# This function used to primarily query `getent hosts` and only read from `/etc/hosts` if 4# `getent` did not exist or `getent hosts` failed, based off the (documented) assumption that 5# the former *might* return more hosts than the latter, which has never been officially noted 6# to be the case. As `getent` is several times slower, involves shelling out, and is not 7# available on some platforms (Cygwin and at least some versions of macOS, such as 10.10), that 8# order is now reversed and `getent hosts` is only used if the hosts file is not found at 9# `/etc/hosts` for portability reasons. 10 11begin 12test -r /etc/hosts&& read -z< /etc/hosts| string replace -r'#.*$' '' 13or type -q getent&& getent hosts2> /dev/null 14end | 15# Ignore own IP addresses (127.*, 0.0[.0[.0]], ::1), non-host IPs (fe00::*, ff00::*), 16# and leading/trailing whitespace. Split results on whitespace to handle multiple aliases for 17# one IP. 18string replace -irf'^\s*?(?!(?:0\.|127\.|ff0|fe0|::1))\S+\s*(.*?)\s*$' '$1' | 19string split' ' 20 21# Print nfs servers from /etc/fstab 22if test -r /etc/fstab 23string match -r'^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:|^[a-zA-Z\.]*:' < /etc/fstab| 24string replace -r':.*' '' 25end 26 27# Check hosts known to ssh. 28# Yes, seriously - the default specifies both with and without "2". 29# Termux puts these in the android data directory if not rooted. 30# The directory is available as $PREFIX/etc, but that variable name is so generic that 31# it would cause false-positives. 32# Also, some people might use /usr/local/etc. 33set -l known_hosts ~/.ssh/known_hosts{, 2 } \ 34{ /data/data/com.termux/files/usr, /usr/local,} /etc/ssh/{, ssh_} known_hosts{, 2 } 35# Check default ssh configs. 36set -l ssh_config ~/.ssh/config 37 38# Inherit settings and parameters from `ssh` aliases, if any 39if functions -q ssh 40# Get alias and commandline options. 41set -l ssh_func_tokens( functions ssh| string match'*command ssh *' | string split' ' ) 42set -l ssh_command$ssh_func_tokens ( commandline -cpx) 43# Extract ssh config path from last -F short option. 44if contains -- -F$ssh_command 45set -l ssh_config_path_is_next1 46for tokenin $ssh_command 47if contains -- -F$token 48set ssh_config_path_is_next0 49else if test $ssh_config_path_is_next -eq 0 50set ssh_config( eval "echo $token " ) 51set ssh_config_path_is_next1 52end 53end 54end 55end 56 57# Extract ssh config paths from Include option 58function _ssh_include --argument-names ssh_config 59# Relative paths in Include directive use /etc/ssh or ~/.ssh depending on 60# system or user level config. -F will not override this behaviour 61set -l relative_path$HOME /.ssh 62if string match'/etc/ssh/*' --$ssh_config 63set relative_path /etc/ssh 64end 65 66function _recursive --no-scope-shadowing 67set -l paths 68for configin $argv 69if test -r " $config " -a -f " $config " 70set paths$paths ( 71# Keep only Include lines and remove Include syntax 72string replace -rfi'^\s*Include\s+' '' < $config \ 73# Normalize whitespace 74| string trim| string replace -r -a'\s+' ' ' ) 75end 76end 77 78set -l new_paths 79for pathin $paths 80# while ssh_config is using brackets to resolve env, they should be removed 81# example 82# in ssh_config: ${SOME_PATH} 83# in fish: $SOME_PATH 84set path( string replace -r'\${([^}]+)}' '$1' $path ) 85set -l expanded_path 86# Scope "relative" paths in accordance to ssh path resolution 87if string match -qrv'^[~/]' $path 88set path$relative_path /$path 89end 90# Use `eval` to expand paths (eg ~/.ssh/../test/* to /home/<user>/test/file1 /home/<user>/test/file2), 91# and `set` will prevent "No matches for wildcard" messages 92eval set expanded_path$path 93for pathin $expanded_path 94# Skip unusable paths. 95test -r " $path " -a -f " $path " 96or continue 97echo $path 98set new_paths$new_paths $path 99end 100end 101 102if test -n " $new_paths " 103_recursive $new_paths 104end 105end 106_recursive $ssh_config 107end 108set -l ssh_configs /etc/ssh/ssh_config( _ssh_include /etc/ssh/ssh_config) $ssh_config ( _ssh_include $ssh_config ) 109 110for filein $ssh_configs 111if test -r $file 112# Don't read from $file twice. We could use `while read` instead, but that is extremely 113# slow. 114read -alz -d\n contents< $file 115 116# Print hosts from system wide ssh configuration file 117# Multiple names for a single host can be given separated by spaces, so just split it explicitly (#6698). 118string replace -rfi'^\s*Host\s+(\S.*?)\s*$' '$1' --$contents | string split" " | string match -rv'[\*\?]' 119# Also extract known_host paths. 120set known_hosts$known_hosts ( string replace -rfi'.*KnownHostsFile\s*' '' --$contents | string split" " ) 121end 122end 123 124# Read all files and operate on their combined content 125for filein $known_hosts 126if test -r $file 127read -z< $file 128end 129end | 130# Ignore hosts that are hashed, commented or @-marked and strip the key 131# Handle multiple comma-separated hostnames sharing a key, too. 132# 133# This one regex does everything we need, finding all matches including comma-separated 134# values, but fish does not let us print only a capturing group without the entire match, 135# and we can't use `string replace` instead (because CSV then fails). 136# string match -ar "(?:^|,)(?![@|*!])\[?([^ ,:\]]+)\]?" 137# 138# Instead, manually piece together the regular expressions 139string match -v -r'^\s*[!*|@#]' | string replace -rf'^\s*(\S+) .*' '$1' | 140string split',' | string replace -r'\[?([^\]]+).*' '$1' 141 142return 0 143end