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