aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/ftp.bash
blob: 8952cbb85a5020e45d84ff447bf8cb71e3696fed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Completion for ftp with .netrc machines
_ftp() {
    local netrc=$HOME/.netrc
    local word=${COMP_WORDS[COMP_CWORD]}
    local -a machines

    # Bail if the .netrc file is illegible
    if [[ ! -r $netrc ]]; then
        return 1
    fi

    # Tokenize the file
    local -a tokens
    IFS=$' \t\n' read -a tokens -d '' -r < "$netrc"

    # Iterate through tokens and collect machine names
    local machine=0
    for token in "${tokens[@]}"; do
        if ((machine)); then
            machines=("${machines[@]}" "$token")
            machine=0
        elif [[ $token == machine ]]; then
            machine=1
        fi
    done

    # Generate completion reply
    COMPREPLY=( $(compgen -W "${machines[*]}" -- "$word") )
}
complete -F _ftp ftp