aboutsummaryrefslogtreecommitdiff
path: root/bash/bash_completion.d/ftp.bash
blob: 335d711acd987d1e6b76eac1f5f3031b26a4f4f9 (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
32
33
34
35
36
37
38
39
# Completion for ftp(1) with .netrc machines
_ftp() {

    # Bail if the .netrc file is illegible
    local netrc
    netrc=$HOME/.netrc
    [[ -r $netrc ]] || return 1

    # Tokenize the file
    local -a tokens
    read -a tokens -d '' -r < "$netrc"

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

    # Generate completion reply
    local machine
    for machine in "${machines[@]}" ; do
        [[ $machine == "${COMP_WORDS[COMP_CWORD]}"* ]] || continue
        COMPREPLY[${#COMPREPLY[@]}]=$machine
    done
}

# bashdefault requires Bash >=3.0
if ((BASH_VERSINFO[0] >= 3)) ; then
    complete -F _ftp -o bashdefault -o default ftp
else
    complete -F _ftp -o default ftp
fi