1 # bash completion for jailhouse
3 # usage: - include the directory containing the `jailhouse`-tool into your
5 # - source this tile `. tools/jailhouse_bashcompletion`
6 # - alternatively you can but this file into your distributions
7 # bash-completion directory
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
16 # dependencies: - bash-completion (>= 1.3) package installed and activated in
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/
24 # only if jailhouse is in ${PATH}
25 ( which jailhouse &>/dev/null ) && \
28 # test for the required helper-functions
29 if ! type _filedir &>/dev/null; then
30 # functions not defined
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.
39 function _jailhouse_get_id() {
40 local names ids cur prev quoted quoted_cur toks
48 _quote_readline_by_ref "$cur" quoted_cur
50 # handle the '{ ID | [--name] NAME }' part of cell-calls
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
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' ' ')"
61 for n in /sys/devices/jailhouse/cells/*; do
62 _quote_readline_by_ref "${n##*/}" quoted
63 names="${names} ${quoted}"
67 COMPREPLY=( $( compgen -W "--name ${ids} ${names}" -- \
70 # if we are already at position 4, may enter a `NAME`, if `--name` was
72 elif [ "${COMP_CWORD}" -eq 4 ]; then
73 [ "${prev}" = "--name" ] || return 1
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}"
83 COMPREPLY=( $( compgen -W "${names}" -- ${quoted_cur} ) )
85 # the id or name is only accepted at position 3 or 4
93 function _jailhouse_cell() {
94 local cur prev quoted_cur
96 cur="${COMP_WORDS[COMP_CWORD]}"
97 prev="${COMP_WORDS[COMP_CWORD-1]}"
99 _quote_readline_by_ref "$cur" quoted_cur
101 # handle subcommand of the cell-command
104 # search for guest-cell configs
106 # this command takes only a argument at place 3
107 [ "${COMP_CWORD}" -gt 3 ] && return 1
112 # first, select the id/name of the cell we want to load a image
114 _jailhouse_get_id "${cur}" "${prev}" && return 0
116 # [image & address] can be repeated
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
126 # the first image has to be placed, after that it is:
128 # [image [<-a|--address> <address>]
129 # [image [<-a|--address> <address>] ... ]]
131 # prev was an addresse, no image here
132 if [[ "${prev}" = "-a" || "${prev}" = "--address" ]]; then
135 # prev was either an image or a address-number
137 # did we already start to type an other switch
138 if [[ "$cur" == -* ]]; then
139 COMPREPLY=( $( compgen -W "-a --address" -- \
143 # default to image-file
150 # takes only one argument (id/name)
151 _jailhouse_get_id "${cur}" "${prev}" || return 1
154 # takes only one argument (id/name)
155 _jailhouse_get_id "${cur}" "${prev}" || return 1
158 # takes only one argument (id/name)
159 _jailhouse_get_id "${cur}" "${prev}" || return 1
164 # this command takes only a argument at place 3
165 [ "${COMP_CWORD}" -gt 3 ] && return 1
167 COMPREPLY=( $( compgen -W "-h --help" -- "${cur}") )
170 # takes only one argument (id/name)
171 _jailhouse_get_id "${cur}" "${prev}" || return 1
173 if [ "${COMP_CWORD}" -eq 3 ]; then
174 COMPREPLY=( ${COMPREPLY[@]-} $( compgen -W "-h --help" \
185 function _jailhouse_config_create() {
188 cur="${COMP_WORDS[COMP_CWORD]}"
189 prev="${COMP_WORDS[COMP_CWORD-1]}"
191 options="-h --help -g --generate-collector -r --root -t --template-dir \
192 --mem-inmates --mem-hv"
194 # if we already have begun to write an option
195 if [[ "$cur" == -* ]]; then
196 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
198 # if the previous was on of the following options
200 -r|--root|-t|--template-dir)
201 # search a existing directory
205 --mem-inmates|--mem-hv)
206 # we can't really predict this
211 # neither option, nor followup of one. Lets assume we want the
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)
225 local command command_cell command_config cur prev subcommand
228 command="enable disable cell config --help"
231 command_cell="create load start shutdown destroy list stats"
232 command_config="create collect"
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]}"
239 # command line parsing for the jaihouse-tool is pretty static right
240 # now, the first two levels can be parsed simpy by comparing
243 # ${COMP_CWORD} contains at which argument-position we currently are
245 # if at level 1, we select from first level list
246 if [ "${COMP_CWORD}" -eq 1 ]; then
247 COMPREPLY=( $( compgen -W "${command}" -- "${cur}") )
249 # if at level 2, we have to evaluate level 1 and look for the main
251 elif [ "${COMP_CWORD}" -eq 2 ]; then
252 command="${COMP_WORDS[1]}"
256 # a root-cell configuration
260 # one of the following subcommands
261 COMPREPLY=( $( compgen -W "${command_cell}" -- \
265 # one of the following subcommands
266 COMPREPLY=( $( compgen -W "${command_config}" -- \
270 # these first level commands have no further subcommand
271 # or option OR we don't even know it
277 # after level 2 it gets more complecated in some cases
279 command="${COMP_WORDS[1]}"
280 subcommand="${COMP_WORDS[2]}"
284 # handle cell-commands
285 _jailhouse_cell "${subcommand}" || return 1
288 case "${subcommand}" in
290 _jailhouse_config_create || return 1
293 # config-collect writes to a new file
295 # this command takes only a argument at place 3
296 [ "${COMP_CWORD}" -gt 3 ] && return 1
305 # no further subsubcommand/option known for this
312 complete -F _jailhouse jailhouse