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