fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerMark private functions that don't need localizationa53db7256

master
6.5 KiB143 linesraw
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
11    begin
12        test -r /etc/hosts && read -z </etc/hosts | string replace -r '#.*$' ''
13        or type -q getent && getent hosts 2>/dev/null
14    end |
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.
18        string replace -irf '^\s*?(?!(?:0\.|127\.|ff0|fe0|::1))\S+\s*(.*?)\s*$' '$1' |
19        string split ' '
20
21    # Print nfs servers from /etc/fstab
22    if test -r /etc/fstab
23        string match -r '^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:|^[a-zA-Z\.]*:' </etc/fstab |
24            string replace -r ':.*' ''
25    end
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.
33    set -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.
36    set -l ssh_config ~/.ssh/config
37
38    # Inherit settings and parameters from `ssh` aliases, if any
39    if functions -q ssh
40        # Get alias and commandline options.
41        set -l ssh_func_tokens (functions ssh | string match '*command ssh *' | string split ' ')
42        set -l ssh_command $ssh_func_tokens (commandline -cpx)
43        # Extract ssh config path from last -F short option.
44        if contains -- -F $ssh_command
45            set -l ssh_config_path_is_next 1
46            for token in $ssh_command
47                if contains -- -F $token
48                    set ssh_config_path_is_next 0
49                else if test $ssh_config_path_is_next -eq 0
50                    set ssh_config (eval "echo $token")
51                    set ssh_config_path_is_next 1
52                end
53            end
54        end
55    end
56
57    # Extract ssh config paths from Include option
58    function _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
61        set -l relative_path $HOME/.ssh
62        if string match '/etc/ssh/*' -- $ssh_config
63            set relative_path /etc/ssh
64        end
65
66        function _recursive --no-scope-shadowing
67            set -l paths
68            for config in $argv
69                if test -r "$config" -a -f "$config"
70                    set paths $paths (
71                    # Keep only Include lines and remove Include syntax
72                    string replace -rfi '^\s*Include\s+' '' <$config \
73                    # Normalize whitespace
74                    | string trim | string replace -r -a '\s+' ' ')
75                end
76            end
77
78            set -l new_paths
79            for path in $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
84                set path (string replace -r '\${([^}]+)}' '$1' $path)
85                set -l expanded_path
86                # Scope "relative" paths in accordance to ssh path resolution
87                if string match -qrv '^[~/]' $path
88                    set path $relative_path/$path
89                end
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
92                eval set expanded_path $path
93                for path in $expanded_path
94                    # Skip unusable paths.
95                    test -r "$path" -a -f "$path"
96                    or continue
97                    echo $path
98                    set new_paths $new_paths $path
99                end
100            end
101
102            if test -n "$new_paths"
103                _recursive $new_paths
104            end
105        end
106        _recursive $ssh_config
107    end
108    set -l ssh_configs /etc/ssh/ssh_config (_ssh_include /etc/ssh/ssh_config) $ssh_config (_ssh_include $ssh_config)
109
110    for file in $ssh_configs
111        if test -r $file
112            # Don't read from $file twice. We could use `while read` instead, but that is extremely
113            # slow.
114            read -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).
118            string replace -rfi '^\s*Host\s+(\S.*?)\s*$' '$1' -- $contents | string split " " | string match -rv '[\*\?]'
119            # Also extract known_host paths.
120            set known_hosts $known_hosts (string replace -rfi '.*KnownHostsFile\s*' '' -- $contents | string split " ")
121        end
122    end
123
124    # Read all files and operate on their combined content
125    for file in $known_hosts
126        if test -r $file
127            read -z <$file
128        end
129    end |
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
139        string match -v -r '^\s*[!*|@#]' | string replace -rf '^\s*(\S+) .*' '$1' |
140        string split ',' | string replace -r '\[?([^\]]+).*' '$1'
141
142    return 0
143end