1 # bash completion for jailhouse
3 # Copyright (c) Benjamin Block, 2014
4 # Copyright (c) Siemens AG, 2015
7 # Benjamin Block <bebl@mageta.org>
8 # Jan Kiszka <jan.kiszka@siemens.com>
10 # This work is licensed under the terms of the GNU GPL, version 2. See
11 # the COPYING file in the top-level directory.
14 # usage: - include the directory containing the `jailhouse`-tool into your
16 # - source this tile `. tools/jailhouse_bashcompletion`
17 # - alternatively you can but this file into your distributions
18 # bash-completion directory
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
27 # dependencies: - bash-completion (>= 1.3) package installed and activated in
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/
35 # only if jailhouse is in ${PATH}
36 ( which jailhouse &>/dev/null ) && \
39 # test for the required helper-functions
40 if ! type _filedir &>/dev/null; then
41 # functions not defined
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.
50 function _jailhouse_get_id() {
51 local names ids cur prev quoted quoted_cur toks
59 _quote_readline_by_ref "$cur" quoted_cur
61 # handle the '{ ID | [--name] NAME }' part of cell-calls
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
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' ' ')"
72 for n in /sys/devices/jailhouse/cells/*; do
73 _quote_readline_by_ref "${n##*/}" quoted
74 names="${names} ${quoted}"
78 COMPREPLY=( $( compgen -W "--name ${ids} ${names}" -- \
81 # if we are already at position 4, may enter a `NAME`, if `--name` was
83 elif [ "${COMP_CWORD}" -eq 4 ]; then
84 [ "${prev}" = "--name" ] || return 1
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}"
94 COMPREPLY=( $( compgen -W "${names}" -- ${quoted_cur} ) )
96 # the id or name is only accepted at position 3 or 4
104 function _jailhouse_cell_linux() {
107 cur="${COMP_WORDS[COMP_CWORD]}"
108 prev="${COMP_WORDS[COMP_CWORD-1]}"
110 options="-h --help -i --initrd -c --cmdline -w --write-params"
112 # if we already have begun to write an option
113 if [[ "$cur" == -* ]]; then
114 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
116 # if the previous was on of the following options
118 -i|--initrd|-w|--write-params)
119 # search an existing file
124 # we can't really predict this
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
146 function _jailhouse_cell() {
147 local cur prev quoted_cur
149 cur="${COMP_WORDS[COMP_CWORD]}"
150 prev="${COMP_WORDS[COMP_CWORD-1]}"
152 _quote_readline_by_ref "$cur" quoted_cur
154 # handle subcommand of the cell-command
157 # search for guest-cell configs
159 # this command takes only a argument at place 3
160 [ "${COMP_CWORD}" -gt 3 ] && return 1
165 # first, select the id/name of the cell we want to load a image
167 _jailhouse_get_id "${cur}" "${prev}" && return 0
169 # [image & address] can be repeated
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
175 # did we already start to type string switch?
176 if [[ "${COMP_CWORD}" -eq 4 && "$cur" == -* ]]; then
177 COMPREPLY=( $( compgen \
178 -W "-s --string" -- \
186 # the first image or string have to be given, after that it is:
188 # [{image | <-s|--string> string} [<-a|--address> <address>]
189 # [{image | <-s|--string> string} [...] ... ]]
191 # prev was an address or a string switch, no image here
192 if [[ "${prev}" = "-a" || "${prev}" = "--address" ||
193 "${prev}" = "-s" || "${prev}" = "--string" ]]; then
196 # prev was an image, a string or an address-number
198 # did we already start to type another switch
199 if [[ "$cur" == -* ]]; then
200 COMPREPLY=( $( compgen \
201 -W "-a --address -s --string" -- \
205 # default to image-file
212 # takes only one argument (id/name)
213 _jailhouse_get_id "${cur}" "${prev}" || return 1
216 # takes only one argument (id/name)
217 _jailhouse_get_id "${cur}" "${prev}" || return 1
220 # takes only one argument (id/name)
221 _jailhouse_get_id "${cur}" "${prev}" || return 1
224 _jailhouse_cell_linux || return 1
229 # this command takes only a argument at place 3
230 [ "${COMP_CWORD}" -gt 3 ] && return 1
232 COMPREPLY=( $( compgen -W "-h --help" -- "${cur}") )
235 # takes only one argument (id/name)
236 _jailhouse_get_id "${cur}" "${prev}" || return 1
238 if [ "${COMP_CWORD}" -eq 3 ]; then
239 COMPREPLY=( ${COMPREPLY[@]-} $( compgen -W "-h --help" \
250 function _jailhouse_config_create() {
253 cur="${COMP_WORDS[COMP_CWORD]}"
254 prev="${COMP_WORDS[COMP_CWORD-1]}"
256 options="-h --help -g --generate-collector -r --root -t --template-dir \
257 --mem-inmates --mem-hv"
259 # if we already have begun to write an option
260 if [[ "$cur" == -* ]]; then
261 COMPREPLY=( $( compgen -W "${options}" -- "${cur}") )
263 # if the previous was on of the following options
265 -r|--root|-t|--template-dir)
266 # search a existing directory
270 --mem-inmates|--mem-hv)
271 # we can't really predict this
276 # neither option, nor followup of one. Lets assume we want the
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)
290 local command command_cell command_config cur prev subcommand
293 command="enable disable cell config --help"
296 command_cell="create load start shutdown destroy linux list stats"
297 command_config="create collect"
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]}"
304 # command line parsing for the jaihouse-tool is pretty static right
305 # now, the first two levels can be parsed simpy by comparing
308 # ${COMP_CWORD} contains at which argument-position we currently are
310 # if at level 1, we select from first level list
311 if [ "${COMP_CWORD}" -eq 1 ]; then
312 COMPREPLY=( $( compgen -W "${command}" -- "${cur}") )
314 # if at level 2, we have to evaluate level 1 and look for the main
316 elif [ "${COMP_CWORD}" -eq 2 ]; then
317 command="${COMP_WORDS[1]}"
321 # a root-cell configuration
325 # one of the following subcommands
326 COMPREPLY=( $( compgen -W "${command_cell}" -- \
330 # one of the following subcommands
331 COMPREPLY=( $( compgen -W "${command_config}" -- \
335 # these first level commands have no further subcommand
336 # or option OR we don't even know it
342 # after level 2 it gets more complecated in some cases
344 command="${COMP_WORDS[1]}"
345 subcommand="${COMP_WORDS[2]}"
349 # handle cell-commands
350 _jailhouse_cell "${subcommand}" || return 1
353 case "${subcommand}" in
355 _jailhouse_config_create || return 1
358 # config-collect writes to a new file
360 # this command takes only a argument at place 3
361 [ "${COMP_CWORD}" -gt 3 ] && return 1
370 # no further subsubcommand/option known for this
377 complete -F _jailhouse jailhouse