summaryrefslogtreecommitdiff
path: root/contrib/tar
blob: 5bc99893824bf73bf4575f703722561c27b3cd89 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# bash completion for GNU tar

have tar && {
_tar()
{
    local cur ext regex tar untar

    COMPREPLY=()
    cur=`_get_cword`

    if [ $COMP_CWORD -eq 1 ]; then
        COMPREPLY=( $( compgen -W 'c t x u r d A' -- "$cur" ) )
        return 0
    fi

    case ${COMP_WORDS[1]} in
    ?(-)[cr]*f)
        _filedir
        return 0
        ;;
    +([^IZzJjy])f)
        ext='t@(ar?(.@(Z|gz|bz?(2)|lz?(ma)))|gz|bz?(2)|lz?(ma))'
        regex='t\(ar\(\.\(Z\|gz\|bz2\?\|lzma\|xz\)\)\?\|gz\|bz2\?\|lzma\|xz\)'
        ;;
    *[Zz]*f)
        ext='t?(ar.)@(gz|Z)'
        regex='t\(ar\.\)\?\(gz\|Z\)'
        ;;
    *[Ijy]*f)
        ext='t?(ar.)bz?(2)'
        regex='t\(ar\.\)\?bz2\?'
        ;;
    *[J]*f)
        ext='t?(ar.)@(lz?(ma)|xz)'
        regex='t\(ar\.\)\?\(lzma\|xz\)\?'
        ;;
    *)
        _filedir
        return 0
        ;;

    esac

    if [[ "$COMP_LINE" == *$ext' ' ]]; then
        # complete on files in tar file
        #
        # get name of tar file from command line
        tar=$( sed -e 's/^.* \([^ ]*'$regex'\) .*$/\1/' <<<"$COMP_LINE" )
        # devise how to untar and list it
        untar=t${COMP_WORDS[1]//[^Izjyf]/}

        COMPREPLY=( $( compgen -W "$( printf '%s ' $( tar $untar $tar \
            2>/dev/null ) )" -- "$cur" ) )
        return 0
    fi

    # file completion on relevant files
    _filedir "$ext"

    return 0
}
[ -n "${COMP_TAR_INTERNAL_PATHS:-}" ] && complete -F _tar -o dirnames tar ||
    complete -F _tar -o filenames tar
}

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh