File: cdemu-bash-completion.sh

package info (click to toggle)
cdemu-client 3.2.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 400 kB
  • sloc: python: 1,072; sh: 186; xml: 87; makefile: 5
file content (307 lines) | stat: -rw-r--r-- 11,278 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# cdemu-bash-completion.sh: bash completion for cdemu client
# Copyright (C) 2011-2014 Henrik Stokseth
# Copyright (C) 2014 Rok Mandeljc
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#


# **********************************************************************
# *                          Utility functions                         *
# **********************************************************************
# Arguments:
#  $1 = list of values
#  $2 = test value
#
# This function checks if array in $1 contains value in $2 or not, and
# prints "y" or "n", respectively.
_contains ()
{
    local array=$1
    local target=$2

    for element in ${array[@]}; do
        if [ "${element}" = "${target}" ]; then
            echo "y"
            return
        fi
    done

    echo "n"
    return
}


# **********************************************************************
# *                                load                                *
# **********************************************************************
_cdemu_load ()
{
    local flag_options="-h --help --dvd-report-css"
    local argument_options="--encoding"

    # Is previous word an option?
    case "${prev}" in
        "--encoding")
            local encodings=$(iconv --list | sed -e 's/\/\/$//')
            COMPREPLY=( $(compgen -W "${encodings}" -- ${cur}) )
            return 0
            ;;
    esac

    # Grab list of devices from daemon and append "any"
    local devices="$(cdemu status 2>/dev/null | tail -n +3 | cut -d ' ' -f 1) any"
    local device_index=-1

    # Device is set if any of words after command word is a valid device
    # key-word and the word before it is not an option with argument
    for ((i=${command_index}+1; i < ${#words[@]}; i++)); do
        local word=${words[i]}
        if [ $(_contains "${devices[@]}" "${word}") = "y" -a $(_contains "${argument_options}" "${words[i-1]}") = "n" ]; then
            device_index=${i}
            break
        fi
    done

    if [ ${device_index} -eq -1 ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options} ${devices}" -- ${cur}) )
    elif [ ${device_index} -ge ${cword} ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options}" -- ${cur}) )
    else
        _filedir
    fi

    return 0
}


# **********************************************************************
# *                            create-blank                            *
# **********************************************************************
_cdemu_create_blank ()
{
    local flag_options="-h --help"
    local argument_options="--param"

    # Is previous word an option?
    case "${prev}" in
        "--param")
            # User must enter parameter; no completion
            return 0
            ;;
        "--writer-id")
            local writers="$(cdemu enum-writers 2>/dev/null | tail -n +2 | cut -d ':' -f 1)"
            COMPREPLY=( $(compgen -W "${writers}" -- ${cur}) )
            return 0
            ;;
        "--medium-type")
            local medium_types="cdr74 cdr80 cdr90 cdr99 dvd+r bdr"
            COMPREPLY=( $(compgen -W "${medium_types}" -- ${cur}) )
            return 0
            ;;
    esac

    # Grab list of devices from daemon and append "any"
    local devices="$(cdemu status 2>/dev/null | tail -n +3 | cut -d ' ' -f 1) any"
    local device_index=-1

    local writer_id_valid=0
    local medium_type_valid=0

    # Device is set if any of words after command word is a valid device
    # key-word and the word before it is not an option with argument. At
    # the same time, check if --writer-id and --medium-type are given.
    for ((i=${command_index}+1; i < ${#words[@]}; i++)); do
        local word=${words[i]}

        if [ "${word}" = "--writer-id" ]; then
            writer_id_valid=1
        fi

        if [ "${word}" = "--medium-type" ]; then
            medium_type_valid=1
        fi

        if [ $(_contains "${devices[@]}" "${word}") = "y" -a $(_contains "${argument_options[@]} --writer-id --medium-type" "${words[i-1]}") = "n" ]; then
            device_index=${i}
            break
        fi
    done

    local mandatory_arguments=""
    if [ ${writer_id_valid} -eq 0 ]; then
        mandatory_arguments="$mandatory_arguments --writer-id"
    fi
    if [ ${medium_type_valid} -eq 0 ]; then
        mandatory_arguments="$mandatory_arguments --medium-type"
    fi

    if [ ${device_index} -eq -1 ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options} ${mandatory_arguments} ${devices}" -- ${cur}) )
    elif [ ${device_index} -ge ${cword} ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options} ${mandatory_arguments}" -- ${cur}) )
    else
        _filedir
    fi

    return 0
}

# **********************************************************************
# *                           generic command                          *
# **********************************************************************
# Arguments:
#  $1 = list of valid auto-completion values for positional argument
#  $2 = list of valid auto-completion values for post-positional argument
#
# This function handles three classes of cdemu commands:
#  a) functions without any additional arguments (e.g., cdemu status);
#     in this case, no arguments should be passed to the function
#  b) functions where we have a positional argument at the end
#     (e.g., cdemu unload all); in this case, $1 should contain valid
#     values for positional argument. This signature should be also used
#     for option getting (e.g., cdemu daemon-debug-mask 0) and option
#     setting functions where we cannot auto-complete the option value
#     (e.g., cdemu daemon-debug-mask 0 0x01, or cdemu device-id 0 A B C D).
#  c) functions where we have a positional argument at the end, but we
#     can also auto-complete the value of option that comes after positional
#     argument; in this case $2 should contain the valid values. This
#     signature can be used for option setting functions where option is
#     boolean (e.g., cdemu dpm-emulation 0 true).
#
# In all three above cases, it is assumed that commands take no additonal
# argument parameters, and that only flag parameter it "-h"/"--help", which
# must appear before the positional argument.
_cdemu_command_generic ()
{
    local flag_options="-h --help"
    local argument_options=""

    # No positional arguments given?
    if [ $# -eq 0 ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options}" -- ${cur}) )
        return 0
    fi

    # Check if first positional keyword is set; i.e., if any of the words
    # after the command key-word is a valid positional key-word and is not
    # a value to argument option
    local positional="$1"
    local positional_index=-1
    for ((i=${command_index}+1; i < ${#words[@]}; i++)); do
        local word=${words[i]}
        if [ $(_contains "${positional[@]}" "${word}") = "y" -a $(_contains "${argument_options}" "${words[i-1]}") = "n" ]; then
            positional_index=${i}
            break
        fi
    done

    if [ ${positional_index} -eq -1 ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options} ${positional}" -- ${cur}) )
    elif [ ${positional_index} -ge ${cword} ]; then
        COMPREPLY=( $(compgen -W "${flag_options} ${argument_options} $2" -- ${cur}) )
    else
        COMPREPLY=( $(compgen -W "$2" -- ${cur}) )
    fi

    return 0
}


# **********************************************************************
# *                                Main                                *
# **********************************************************************
_cdemu()
{
    local cur prev prev2 cword words
    COMPREPLY=()
    _get_comp_words_by_ref cur prev cword words
    prev2="${words[cword - 2]}"

    # Check if we have an active command before word-to-be-completed
    local commands="load create-blank unload status add-device remove-device device-mapping daemon-debug-mask library-debug-mask dpm-emulation tr-emulation bad-sector-emulation device-id enum-parsers enum-writers enum-filter-streams enum-daemon-debug-masks enum-library-debug-masks enum-writer-parameters version"
    local command_index=-1

    for ((i=1; i < ${#words[@]}; i++)); do
        local word=${words[i]}
        for command in ${commands[@]}; do
            if [ "${command}" = "${word}" ]; then
                command_index=${i}
                break
            fi
        done
    done

    # No active command or before command keyword
    if [ ${command_index} -ge ${cword} -o ${command_index} -eq -1 ]; then
        local options="-h --help -b --bus -v --version"

        # Auto-complete --bus option
        case "${prev}" in
            "-b" | "--bus")
                local bus_types="session system"
                COMPREPLY=( $(compgen -W "${bus_types}" -- ${cur}) )
                return 0
                ;;
        esac

        # Complete with options or commands
        if [ ${command_index} -eq -1 ]; then
            COMPREPLY=( $(compgen -W "${options} ${commands}" -- ${cur}) )
        else
            COMPREPLY=( $(compgen -W "${options}" -- ${cur}) )
        fi

        return 0
    fi

    # A command is active, so process its specific inputs
    case "${words[${command_index}]}" in
        "load")
            _cdemu_load
            ;;
        "create-blank")
            _cdemu_create_blank
            ;;
        "unload")
            local devices="$(cdemu status 2>/dev/null | tail -n +3 | cut -d ' ' -f 1) all"
            _cdemu_command_generic "${devices}"
            ;;
        "status" | "add-device" | "remove-device" | "device-mapping" | "version")
            _cdemu_command_generic
            ;;
        "daemon-debug-mask" | "library-debug-mask" | "device-id")
            local devices="$(cdemu status 2>/dev/null | tail -n +3 | cut -d ' ' -f 1) all"
            _cdemu_command_generic "${devices}"
            ;;
        "dpm-emulation" | "tr-emulation" | "bad-sector-emulation")
            local devices="$(cdemu status 2>/dev/null | tail -n +3 | cut -d ' ' -f 1) all"
            _cdemu_command_generic "${devices}" "true false"
            ;;
        "enum-parsers" | "enum-writers" | "enum-filter-streams" | "enum-daemon-debug-masks" | "enum-library-debug-masks")
            _cdemu_command_generic
            ;;
        "enum-writer-parameters")
            local writers="$(cdemu enum-writers 2>/dev/null | tail -n +2 | cut -d ':' -f 1)"
            _cdemu_command_generic "${writers}"
            ;;
    esac

    return 0
}


complete -F _cdemu cdemu