1 # bash completion for jailhouse
3 # Copyright (c) Benjamin Block, 2014
6 # Benjamin Block <bebl@mageta.org>
8 # This work is licensed under the terms of the GNU GPL, version 2. See
9 # the COPYING file in the top-level directory.
12 # usage: - include the directory containing the `jailhouse`-tool into your
14 # - source this tile `. tools/jailhouse_bashcompletion`
15 # - alternatively you can but this file into your distributions
16 # bash-completion directory
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
25 # dependencies: - bash-completion (>= 1.3) package installed and activated in
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/
33 # only if jailhouse is in ${PATH}
34 ( which jailhouse &>/dev/null ) && \
37 # test for the required helper-functions
38 if ! type _filedir &>/dev/null; then
39 # functions not defined
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.
48 function _jailhouse_get_id() {
49 local names ids cur prev quoted quoted_cur toks
57 _quote_readline_by_ref "$cur" quoted_cur
59 # handle the '{ ID | [--name] NAME }' part of cell-calls
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
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' ' ')"
70 for n in /sys/devices/jailhouse/cells/*; do
71 _quote_readline_by_ref "${n##*/}" quoted
72 names="${names} ${quoted}"
76 COMPREPLY=( $( compgen -W "--name ${ids} ${names}" -- \
79 # if we are already at position 4, may enter a `NAME`, if `--name` was
81 elif [ "${COMP_CWORD}" -eq 4 ]; then
82 [ "${prev}" = "--name" ] || return 1
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}"
92 COMPREPLY=( $( compgen -W "${names}" -- ${quoted_cur} ) )
94 # the id or name is only accepted at position 3 or 4
102 function _jailhouse_cell() {
103 local cur prev quoted_cur
105 cur="${COMP_WORDS[COMP_CWORD]}"
106 prev="${COMP_WORDS[COMP_CWORD-1]}"
108 _quote_readline_by_ref "$cur" quoted_cur
110 # handle subcommand of the cell-command
113 # search for guest-cell configs
115 # this command takes only a argument at place 3
116 [ "${COMP_CWORD}" -gt 3 ] && return 1
121 # first, select the id/name of the cell we want to load a image
123 _jailhouse_get_id "${cur}" "${prev}" && return 0
125 # [image & address] can be repeated
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
135 # the first image has to be placed, after that it is:
137 # [image [<-a|--address> <address>]
138 # [image [<-a|--address> <address>] ... ]]
140 # prev was an addresse, no image here
141 if [[ "${prev}" = "-a" || "${prev}" = "--address" ]]; then
144 # prev was either an image or a address-number
146 # did we already start to type an other switch
147 if [[ "$cur" == -* ]]; then
148 COMPREPLY=( $( compgen -W "-a --address" -- \
152 # default to image-file
159 # takes only one argument (id/name)
160 _jailhouse_get_id "${cur}" "${prev}" || return 1
163 # takes only one argument (id/name)
164 _jailhouse_get_id "${cur}" "${prev}" || return 1
167 # takes only one argument (id/name)
168 _jailhouse_get_id "${cur}" "${prev}" || return 1
173 # this command takes only a argument at place 3
174 [ "${COMP_CWORD}" -gt 3 ] && return 1
176 COMPREPLY=( $( compgen -W "-h --help" -- "${cur}") )
179 # takes only one argument (id/name)
180 _jailhouse_get_id "${cur}" "${prev}" || return 1
182 if [ "${COMP_CWORD}" -eq 3 ]; then
183 COMPREPLY=( ${COMPREPLY[@]-} $( compgen -W "-h --help" \
194 function _jailhouse_config_create() {
197 cur="${COMP_WORDS[COMP_CWORD]}"
198 prev="${COMP_WORDS[COMP_CWORD-1]}"
200 options="-h --help -g --generate-collector -r --root -t --template-dir \
201 --mem-inmates --mem-hv"
203 # if we already have begun to write an option
204 if [[ "$cur" == -* ]]; then
205 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
207 # if the previous was on of the following options
209 -r|--root|-t|--template-dir)
210 # search a existing directory
214 --mem-inmates|--mem-hv)
215 # we can't really predict this
220 # neither option, nor followup of one. Lets assume we want the
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)
234 local command command_cell command_config cur prev subcommand
237 command="enable disable cell config --help"
240 command_cell="create load start shutdown destroy list stats"
241 command_config="create collect"
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]}"
248 # command line parsing for the jaihouse-tool is pretty static right
249 # now, the first two levels can be parsed simpy by comparing
252 # ${COMP_CWORD} contains at which argument-position we currently are
254 # if at level 1, we select from first level list
255 if [ "${COMP_CWORD}" -eq 1 ]; then
256 COMPREPLY=( $( compgen -W "${command}" -- "${cur}") )
258 # if at level 2, we have to evaluate level 1 and look for the main
260 elif [ "${COMP_CWORD}" -eq 2 ]; then
261 command="${COMP_WORDS[1]}"
265 # a root-cell configuration
269 # one of the following subcommands
270 COMPREPLY=( $( compgen -W "${command_cell}" -- \
274 # one of the following subcommands
275 COMPREPLY=( $( compgen -W "${command_config}" -- \
279 # these first level commands have no further subcommand
280 # or option OR we don't even know it
286 # after level 2 it gets more complecated in some cases
288 command="${COMP_WORDS[1]}"
289 subcommand="${COMP_WORDS[2]}"
293 # handle cell-commands
294 _jailhouse_cell "${subcommand}" || return 1
297 case "${subcommand}" in
299 _jailhouse_config_create || return 1
302 # config-collect writes to a new file
304 # this command takes only a argument at place 3
305 [ "${COMP_CWORD}" -gt 3 ] && return 1
314 # no further subsubcommand/option known for this
321 complete -F _jailhouse jailhouse