summaryrefslogtreecommitdiffstats
path: root/msitools-completion.sh
blob: e164af349b2cfe1f05f36e319d338563bdd1edc0 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# msiinfo bash completion

__msiinfo_all_commands ()
{
        local i IFS=" "$'\n'
        for i in $(msiinfo --help | grep "^  [^-]"  | cut -d' ' -f 3)
        do
                case $i in
                *) echo $i;;
                esac
        done
}
__msiinfo_commands=
__msiinfo_commands="$(__msiinfo_all_commands 2>/dev/null)"

_msiinfo ()
{
    COMPREPLY=()

    in_array()
    {
        local i
        for i in $2; do
            [[ $i = $1 ]] && return 0
        done
        return 1
    }

    local cur prev
    # _get_comp_words_by_ref is in bash-completion >= 1.2, which EL-5 lacks.
    if type _get_comp_words_by_ref &>/dev/null; then
        _get_comp_words_by_ref cur prev
    else
        cur="${COMP_WORDS[COMP_CWORD]}"
        prev="${COMP_WORDS[COMP_CWORD-1]}"
    fi

    # parse main options and get command

    local options="--help --version"
    local command=
    local command_first=
    local path=

    local i w
    for (( i = 0; i < ${#COMP_WORDS[*]} - 1; i++ )); do
        w="${COMP_WORDS[$i]}"
        # command
        if in_array "$w" "$__msiinfo_commands"; then
            command="$w"
            command_first=$((i+1))
            break
        fi
    done

    # complete base options

    if [[ -z $command ]]; then
        if [[ $cur == -* ]]; then
            COMPREPLY=( $(compgen -W "$options $options_value" -- "$cur") )
            return 0
        fi

        case "$prev" in
            *)
                COMPREPLY=( $(compgen -W "$__msiinfo_commands" -- "$cur") )
                ;;
        esac

        return 0
    fi

    local after= after_more=
    case $command in
        streams|tables|extract|export|suminfo)
            after="msi"
            ;;
    esac

    local all_options="--help $options"
    local all_options_value=""

    # count non-option parameters

    local i w
    local last_option=
    local after_counter=0
    local after_word=
    for (( i = $command_first; i < ${#COMP_WORDS[*]} - 1; i++)); do
        w="${COMP_WORDS[$i]}"
        if [[ ${w:0:1} = - ]]; then
            if in_array "$w" "$all_options"; then
                last_option="$w"
                continue
            elif in_array "$w" "$all_options_value"; then
                last_option="$w"
                ((i++))
                continue
            fi
        else
            last_word="$w"
        fi
        in_array "$last_option" "$options_arches" || ((after_counter++))
    done

    local after_options=
    if [[ $after_counter -eq 0 ]] || [[ $after_more = true ]]; then
        case $after in
                msi)    _filedir msi;;
        esac
    fi
    if [[ $after_counter -eq 1 ]]; then
        case $command in
            extract)
                after_options="$(msiinfo streams "$last_word")" ;;
            export)
                after_options="$(msiinfo tables "$last_word")" ;;
        esac
    fi

    if [[ $cur != -* ]]; then
        all_options=
        all_options_value=
    fi

    COMPREPLY+=( $(compgen -W "$all_options $all_options_value $after_options" -- "$cur" ) )

    return 0
}

_msibuild ()
{
        local cur="${COMP_WORDS[COMP_CWORD]}"
        case "$cur" in
        -*)
                COMPREPLY=( $(compgen -W "-s -q -i -a" -- "$cur" ) )
                return 0
                ;;
        esac
}

complete -o default -o nospace -F _msiinfo msiinfo
complete -o default -o nospace -F _msibuild msibuild