]> rtime.felk.cvut.cz Git - jailhouse.git/blob - tools/jailhouse-completion.bash
tools: make jailhouse-cell-linux run under Python3
[jailhouse.git] / tools / jailhouse-completion.bash
1 # bash completion for jailhouse
2 #
3 # Copyright (c) Benjamin Block, 2014
4 # Copyright (c) Siemens AG, 2015
5 #
6 # Authors:
7 #  Benjamin Block <bebl@mageta.org>
8 #  Jan Kiszka <jan.kiszka@siemens.com>
9 #
10 # This work is licensed under the terms of the GNU GPL, version 2.  See
11 # the COPYING file in the top-level directory.
12 #
13
14 # usage: - include the directory containing the `jailhouse`-tool into your
15 #          ${PATH}-variable
16 #        - source this tile `. tools/jailhouse_bashcompletion`
17 #        - alternatively you can but this file into your distributions
18 #          bash-completion directory
19 #
20 #          there is a broad variety of places where distris may put this:
21 #               - /usr/share/bash-completion/
22 #               - /etc/bash_completion.d/
23 #               - $BASH_COMPLETION_COMPAT_DIR
24 #               - $BASH_COMPLETION_DIR
25 #               - ...
26
27 # dependencies: - bash-completion (>= 1.3) package installed and activated in
28 #                 your bash
29
30 # bash-completion websites:
31 #     - http://bash-completion.alioth.debian.org/
32 #     - https://wiki.debian.org/Teams/BashCompletion
33 #     - http://anonscm.debian.org/cgit/bash-completion/bash-completion.git/
34
35 # only if jailhouse is in ${PATH}
36 ( which jailhouse &>/dev/null ) && \
37 {
38
39 # test for the required helper-functions
40 if ! type _filedir &>/dev/null; then
41         # functions not defined
42         #
43         # The distributions seem to handle the inclusion of these function in a
44         # broad variety of ways. To keep this script as simple as possible we
45         # depend on this to work.
46
47         return 1
48 fi
49
50 function _jailhouse_get_id() {
51         local names ids cur prev quoted quoted_cur toks
52
53         cur="${1}"
54         prev="${2}"
55
56         ids=""
57         names=""
58
59         _quote_readline_by_ref "$cur" quoted_cur
60
61         # handle the '{ ID | [--name] NAME }'  part of cell-calls
62         #
63         # if we are at position 3 of the commnadline we can either input a
64         # concrete `ID`/`NAME` or the option `--name`
65         if [ "${COMP_CWORD}" -eq 3 ]; then
66
67                 # get possible ids and names
68                 if [ -d /sys/devices/jailhouse/cells ]; then
69                         for i in /sys/devices/jailhouse/cells/*/id; do
70                                 ids="${ids} $(cat "${i}" | tr '\n' ' ')"
71                         done
72                         for n in /sys/devices/jailhouse/cells/*; do
73                                 _quote_readline_by_ref "${n##*/}" quoted
74                                 names="${names} ${quoted}"
75                         done
76                 fi
77
78                 COMPREPLY=( $( compgen -W "--name ${ids} ${names}" -- \
79                                         ${quoted_cur} ) )
80
81         # if we are already at position 4, may enter a `NAME`, if `--name` was
82         # given before
83         elif [ "${COMP_CWORD}" -eq 4 ]; then
84                 [ "${prev}" = "--name" ] || return 1
85
86                 # get possible names
87                 if [ -d /sys/devices/jailhouse/cells ]; then
88                         for n in /sys/devices/jailhouse/cells/*; do
89                                 _quote_readline_by_ref "${n##*/}" quoted
90                                 names="${names} ${quoted}"
91                         done
92                 fi
93
94                 COMPREPLY=( $( compgen -W "${names}" -- ${quoted_cur} ) )
95
96         # the id or name is only accepted at position 3 or 4
97         else
98                 return 1;
99         fi
100
101         return 0;
102 }
103
104 function _jailhouse_cell_linux() {
105         local cur prev word
106
107         cur="${COMP_WORDS[COMP_CWORD]}"
108         prev="${COMP_WORDS[COMP_CWORD-1]}"
109
110         options="-h --help -i --initrd -c --cmdline -w --write-params"
111
112         # if we already have begun to write an option
113         if [[ "$cur" == -* ]]; then
114                 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
115         else
116                 # if the previous was on of the following options
117                 case "${prev}" in
118                 -i|--initrd|-w|--write-params)
119                         # search an existing file
120                         _filedir
121                         return $?
122                         ;;
123                 -c|--cmdline)
124                         # we can't really predict this
125                         return 0
126                         ;;
127                 esac
128
129                 # neither option, nor followup of one. Lets assume we want
130                 # the cell or the kernel
131                 for n in `seq ${COMP_CWORD-1}`; do
132                         word="${COMP_WORDS[n]}"
133                         if [[ "${word}" == *.cell ]] && ( [ $n -eq 1 ] ||
134                             [[ "${COMP_WORDS[n-1]}" != -* ]] ); then
135                                 # we already have a cell, this is the kernel
136                                 _filedir
137                                 return 0
138                         fi
139                 done
140                 _filedir "cell"
141         fi
142
143         return 0
144 }
145
146 function _jailhouse_cell() {
147         local cur prev quoted_cur
148
149         cur="${COMP_WORDS[COMP_CWORD]}"
150         prev="${COMP_WORDS[COMP_CWORD-1]}"
151
152         _quote_readline_by_ref "$cur" quoted_cur
153
154         # handle subcommand of the cell-command
155         case "${1}" in
156         create)
157                 # search for guest-cell configs
158
159                 # this command takes only a argument at place 3
160                 [ "${COMP_CWORD}" -gt 3 ] && return 1
161
162                 _filedir "cell"
163                 ;;
164         load)
165                 # first, select the id/name of the cell we want to load a image
166                 # for
167                 _jailhouse_get_id "${cur}" "${prev}" && return 0
168
169                 # [image & address] can be repeated
170
171                 # after id/name insert image-file (always true)
172                 if [ "${COMP_CWORD}" -eq 4 ] || ( [ "${COMP_CWORD}" -eq 5 ] && \
173                         [ "${COMP_WORDS[3]}" = "--name" ] ); then
174
175                         _filedir
176                         return 0
177                 fi
178
179                 # the first image has to be placed, after that it is:
180                 #
181                 # [image [<-a|--address> <address>]
182                 #        [image [<-a|--address> <address>] ... ]]
183
184                 # prev was an addresse, no image here
185                 if [[ "${prev}" = "-a" || "${prev}" = "--address" ]]; then
186                         return 0
187
188                 # prev was either an image or a address-number
189                 else
190                         # did we already start to type an other switch
191                         if [[ "$cur" == -* ]]; then
192                                 COMPREPLY=( $( compgen -W "-a --address" -- \
193                                                 "${cur}") )
194                         fi
195
196                         # default to image-file
197                         _filedir
198                         return 0
199                 fi
200
201                 ;;
202         start)
203                 # takes only one argument (id/name)
204                 _jailhouse_get_id "${cur}" "${prev}" || return 1
205                 ;;
206         shutdown)
207                 # takes only one argument (id/name)
208                 _jailhouse_get_id "${cur}" "${prev}" || return 1
209                 ;;
210         destroy)
211                 # takes only one argument (id/name)
212                 _jailhouse_get_id "${cur}" "${prev}" || return 1
213                 ;;
214         linux)
215                 _jailhouse_cell_linux || return 1
216                 ;;
217         list)
218                 # list all cells
219
220                 # this command takes only a argument at place 3
221                 [ "${COMP_CWORD}" -gt 3 ] && return 1
222
223                 COMPREPLY=( $( compgen -W "-h --help" -- "${cur}") )
224                 return 0;;
225         stats)
226                 # takes only one argument (id/name)
227                 _jailhouse_get_id "${cur}" "${prev}" || return 1
228
229                 if [ "${COMP_CWORD}" -eq 3 ]; then
230                         COMPREPLY=( ${COMPREPLY[@]-} $( compgen -W "-h --help" \
231                                                         -- ${quoted_cur} ) )
232                 fi
233                 ;;
234         *)
235                 return 1;;
236         esac
237
238         return 0
239 }
240
241 function _jailhouse_config_create() {
242         local cur prev
243
244         cur="${COMP_WORDS[COMP_CWORD]}"
245         prev="${COMP_WORDS[COMP_CWORD-1]}"
246
247         options="-h --help -g --generate-collector -r --root -t --template-dir \
248                 --mem-inmates --mem-hv"
249
250         # if we already have begun to write an option
251         if [[ "$cur" == -* ]]; then
252                 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
253         else
254                 # if the previous was on of the following options
255                 case "${prev}" in
256                 -r|--root|-t|--template-dir)
257                         # search a existing directory
258                         _filedir -d
259                         return $?
260                         ;;
261                 --mem-inmates|--mem-hv)
262                         # we can't really predict this
263                         return 0
264                         ;;
265                 esac
266
267                 # neither option, nor followup of one. Lets assume we want the
268                 # target-filename
269                 _filedir
270         fi
271
272         return 0
273 }
274
275 function _jailhouse() {
276         # returns two value: - numeric from "return" (success/failure)
277         #                    - ${COMPREPLY}; an bash-array from which bash will
278         #                      read the possible completions (reset this here)
279         COMPREPLY=()
280
281         local command command_cell command_config cur prev subcommand
282
283         # first level
284         command="enable disable cell config --help"
285
286         # second level
287         command_cell="create load start shutdown destroy linux list stats"
288         command_config="create collect"
289
290         # ${COMP_WORDS} array containing the words on the current command line
291         # ${COMP_CWORD} index into COMP_WORDS, pointing at the current position
292         cur="${COMP_WORDS[COMP_CWORD]}"
293         prev="${COMP_WORDS[COMP_CWORD-1]}"
294
295         # command line parsing for the jaihouse-tool is pretty static right
296         # now, the first two levels can be parsed simpy by comparing
297         # postions
298
299         # ${COMP_CWORD} contains at which argument-position we currently are
300         #
301         # if at level 1, we select from first level list
302         if [ "${COMP_CWORD}" -eq 1 ]; then
303                 COMPREPLY=( $( compgen -W "${command}" -- "${cur}") )
304
305         # if at level 2, we have to evaluate level 1 and look for the main
306         # command
307         elif [ "${COMP_CWORD}" -eq 2 ]; then
308                 command="${COMP_WORDS[1]}"
309
310                 case "${command}" in
311                 enable)
312                         # a root-cell configuration
313                         _filedir "cell"
314                         ;;
315                 cell)
316                         # one of the following subcommands
317                         COMPREPLY=( $( compgen -W "${command_cell}" -- \
318                                         "${cur}") )
319                         ;;
320                 config)
321                         # one of the following subcommands
322                         COMPREPLY=( $( compgen -W "${command_config}" -- \
323                                         "${cur}") )
324                         ;;
325                 --help|disable)
326                         # these first level commands have no further subcommand
327                         # or option OR we don't even know it
328                         return 0;;
329                 *)
330                         return 1;;
331                 esac
332
333         # after level 2 it gets more complecated in some cases
334         else
335                 command="${COMP_WORDS[1]}"
336                 subcommand="${COMP_WORDS[2]}"
337
338                 case "${command}" in
339                 cell)
340                         # handle cell-commands
341                         _jailhouse_cell "${subcommand}" || return 1
342                         ;;
343                 config)
344                         case "${subcommand}" in
345                         create)
346                                 _jailhouse_config_create || return 1
347                                 ;;
348                         collect)
349                                 # config-collect writes to a new file
350
351                                 # this command takes only a argument at place 3
352                                 [ "${COMP_CWORD}" -gt 3 ] && return 1
353
354                                 _filedir
355                                 ;;
356                         *)
357                                 return 1;;
358                         esac
359                         ;;
360                 *)
361                         # no further subsubcommand/option known for this
362                         return 1;;
363                 esac
364         fi
365
366         return 0
367 }
368 complete -F _jailhouse jailhouse
369
370 }