]> rtime.felk.cvut.cz Git - jailhouse.git/blob - tools/jailhouse-completion.bash
core, tools: Introduce cache regions to the cell configuration
[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 or string switch (always true)
172                 if [ "${COMP_CWORD}" -eq 4 ] || ( [ "${COMP_CWORD}" -eq 5 ] && \
173                         [ "${COMP_WORDS[3]}" = "--name" ] ); then
174
175                         # did we already start to type string switch?
176                         if [[ "${COMP_CWORD}" -eq 4 && "$cur" == -* ]]; then
177                                 COMPREPLY=( $( compgen \
178                                         -W "-s --string" -- \
179                                         "${cur}") )
180                         fi
181
182                         _filedir
183                         return 0
184                 fi
185
186                 # the first image or string have to be given, after that it is:
187                 #
188                 # [{image | <-s|--string> string} [<-a|--address> <address>]
189                 #  [{image | <-s|--string> string} [...] ... ]]
190
191                 # prev was an address or a string switch, no image here
192                 if [[ "${prev}" = "-a" || "${prev}" = "--address" ||
193                       "${prev}" = "-s" || "${prev}" = "--string" ]]; then
194                         return 0
195
196                 # prev was an image, a string or an address-number
197                 else
198                         # did we already start to type another switch
199                         if [[ "$cur" == -* ]]; then
200                                 COMPREPLY=( $( compgen \
201                                         -W "-a --address -s --string" -- \
202                                         "${cur}") )
203                         fi
204
205                         # default to image-file
206                         _filedir
207                         return 0
208                 fi
209
210                 ;;
211         start)
212                 # takes only one argument (id/name)
213                 _jailhouse_get_id "${cur}" "${prev}" || return 1
214                 ;;
215         shutdown)
216                 # takes only one argument (id/name)
217                 _jailhouse_get_id "${cur}" "${prev}" || return 1
218                 ;;
219         destroy)
220                 # takes only one argument (id/name)
221                 _jailhouse_get_id "${cur}" "${prev}" || return 1
222                 ;;
223         linux)
224                 _jailhouse_cell_linux || return 1
225                 ;;
226         list)
227                 # list all cells
228
229                 # this command takes only a argument at place 3
230                 [ "${COMP_CWORD}" -gt 3 ] && return 1
231
232                 COMPREPLY=( $( compgen -W "-h --help" -- "${cur}") )
233                 return 0;;
234         stats)
235                 # takes only one argument (id/name)
236                 _jailhouse_get_id "${cur}" "${prev}" || return 1
237
238                 if [ "${COMP_CWORD}" -eq 3 ]; then
239                         COMPREPLY=( ${COMPREPLY[@]-} $( compgen -W "-h --help" \
240                                                         -- ${quoted_cur} ) )
241                 fi
242                 ;;
243         *)
244                 return 1;;
245         esac
246
247         return 0
248 }
249
250 function _jailhouse_config_create() {
251         local cur prev
252
253         cur="${COMP_WORDS[COMP_CWORD]}"
254         prev="${COMP_WORDS[COMP_CWORD-1]}"
255
256         options="-h --help -g --generate-collector -r --root -t --template-dir \
257                 --mem-inmates --mem-hv"
258
259         # if we already have begun to write an option
260         if [[ "$cur" == -* ]]; then
261                 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
262         else
263                 # if the previous was on of the following options
264                 case "${prev}" in
265                 -r|--root|-t|--template-dir)
266                         # search a existing directory
267                         _filedir -d
268                         return $?
269                         ;;
270                 --mem-inmates|--mem-hv)
271                         # we can't really predict this
272                         return 0
273                         ;;
274                 esac
275
276                 # neither option, nor followup of one. Lets assume we want the
277                 # target-filename
278                 _filedir
279         fi
280
281         return 0
282 }
283
284 function _jailhouse() {
285         # returns two value: - numeric from "return" (success/failure)
286         #                    - ${COMPREPLY}; an bash-array from which bash will
287         #                      read the possible completions (reset this here)
288         COMPREPLY=()
289
290         local command command_cell command_config cur prev subcommand
291
292         # first level
293         command="enable disable cell config --help"
294
295         # second level
296         command_cell="create load start shutdown destroy linux list stats"
297         command_config="create collect"
298
299         # ${COMP_WORDS} array containing the words on the current command line
300         # ${COMP_CWORD} index into COMP_WORDS, pointing at the current position
301         cur="${COMP_WORDS[COMP_CWORD]}"
302         prev="${COMP_WORDS[COMP_CWORD-1]}"
303
304         # command line parsing for the jaihouse-tool is pretty static right
305         # now, the first two levels can be parsed simpy by comparing
306         # postions
307
308         # ${COMP_CWORD} contains at which argument-position we currently are
309         #
310         # if at level 1, we select from first level list
311         if [ "${COMP_CWORD}" -eq 1 ]; then
312                 COMPREPLY=( $( compgen -W "${command}" -- "${cur}") )
313
314         # if at level 2, we have to evaluate level 1 and look for the main
315         # command
316         elif [ "${COMP_CWORD}" -eq 2 ]; then
317                 command="${COMP_WORDS[1]}"
318
319                 case "${command}" in
320                 enable)
321                         # a root-cell configuration
322                         _filedir "cell"
323                         ;;
324                 cell)
325                         # one of the following subcommands
326                         COMPREPLY=( $( compgen -W "${command_cell}" -- \
327                                         "${cur}") )
328                         ;;
329                 config)
330                         # one of the following subcommands
331                         COMPREPLY=( $( compgen -W "${command_config}" -- \
332                                         "${cur}") )
333                         ;;
334                 --help|disable)
335                         # these first level commands have no further subcommand
336                         # or option OR we don't even know it
337                         return 0;;
338                 *)
339                         return 1;;
340                 esac
341
342         # after level 2 it gets more complecated in some cases
343         else
344                 command="${COMP_WORDS[1]}"
345                 subcommand="${COMP_WORDS[2]}"
346
347                 case "${command}" in
348                 cell)
349                         # handle cell-commands
350                         _jailhouse_cell "${subcommand}" || return 1
351                         ;;
352                 config)
353                         case "${subcommand}" in
354                         create)
355                                 _jailhouse_config_create || return 1
356                                 ;;
357                         collect)
358                                 # config-collect writes to a new file
359
360                                 # this command takes only a argument at place 3
361                                 [ "${COMP_CWORD}" -gt 3 ] && return 1
362
363                                 _filedir
364                                 ;;
365                         *)
366                                 return 1;;
367                         esac
368                         ;;
369                 *)
370                         # no further subsubcommand/option known for this
371                         return 1;;
372                 esac
373         fi
374
375         return 0
376 }
377 complete -F _jailhouse jailhouse
378
379 }