Home | History | Annotate | Download | only in bash_completion
      1 # Copyright (c) 2014, Intel Corporation
      2 # All rights reserved.
      3 #
      4 # Redistribution and use in source and binary forms, with or without modification,
      5 # are permitted provided that the following conditions are met:
      6 #
      7 # 1. Redistributions of source code must retain the above copyright notice, this
      8 # list of conditions and the following disclaimer.
      9 #
     10 # 2. Redistributions in binary form must reproduce the above copyright notice,
     11 # this list of conditions and the following disclaimer in the documentation and/or
     12 # other materials provided with the distribution.
     13 #
     14 # 3. Neither the name of the copyright holder nor the names of its contributors
     15 # may be used to endorse or promote products derived from this software without
     16 # specific prior written permission.
     17 #
     18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
     22 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
     25 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     27 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 # bash completion for remote-process
     30 #
     31 # Execute this file in bash with the built-in command "source",
     32 # it will add autocompletion to remote-process.
     33 #
     34 # To permanently add this autocompletion, add "source this_file"
     35 # in your .bashrc or copy this file in /etc/bash_completion.d/
     36 
     37 function _remote-process ()
     38 {
     39     # Get current word
     40     local cur prev words cword;
     41     _init_completion || return;
     42 
     43     local options=""
     44     if [ $cword -eq 1 ]
     45     then # Completing the hostname
     46         _known_hosts_real "$cur"; return
     47     elif [ $cword -eq 2 ] # Completing tcp port
     48     then
     49         options='5000 5001 5008 5009 5019';
     50     else
     51         _remoteProcessWrapper () {
     52             "${words[0]}" "${words[1]}" "${words[2]}" "$@" |sed 's#\r##;/^$/d'
     53         }
     54 
     55         # Get usage
     56         local _parameterHelp=$(_remoteProcessWrapper help)
     57 
     58         if [ $cword -eq 3 ]
     59         then # Completing command
     60             options=$(echo "$_parameterHelp" | awk '{print $1}')
     61         else # Completing command argument
     62             local command=${words[3]}
     63 
     64             # Get current command argument types
     65             # - keep in the help text only the line describing current command
     66             # - delete => and posterior
     67             # - replace space in balisa (<...>) by underscore then delete [<>]
     68             local argumentTypes=$( echo "$_parameterHelp" | grep "$command"  |\
     69                  sed -e 's# *=>.*##' -e 's#^[^ ]* *##' \
     70                      -e 's/> />#/g' -e 's# #_#g' -e 's/>#/> /g' -e 's#[<>]##g' )
     71 
     72             local currentArgumentType=$(echo $argumentTypes |
     73                                           awk '{print $('$cword'-3)}')
     74 
     75             # Take care of argument list type if it is the last argument
     76             # Ex : setElementSequence <domain> <configuration> <elem path list>
     77             if [ "$currentArgumentType" = "" ] &&
     78                  expr "$argumentTypes" : '.*list' > /dev/null ;
     79             then
     80                 # Set last argument type specified as the current type
     81                 currentArgumentType=$(echo "$argumentTypes" | awk '{print $NF}')
     82             fi
     83 
     84 
     85             case "${currentArgumentType}" in
     86                 elem_path*|param_path* )
     87                     local incompletPath=$(echo "${cur}" | sed 's#[^/]*$##')
     88 
     89                     local parameterCommand
     90                     if [ "$currentArgumentType" = elem_path_list ];
     91                     then
     92                         # <elem path list> is the parameter path list
     93                         #  of the domain specified by the second argument
     94                         parameterCommand="listDomainElements ${words[4]}"
     95                     else
     96                         # Otherwise suggest all parameter paths
     97                         parameterCommand="listParameters"
     98                     fi
     99                     # Get paths and delete everything after the first "/"
    100                     # following the current input (see complete -o filenames)
    101                     local options=$(_remoteProcessWrapper $parameterCommand ${incompletPath:-/} |
    102                                     sed -re "s#(/?${incompletPath}[^/ ]*/?).*#\1#")
    103                     compopt -o filenames
    104 
    105                     # If some options are folders, do not append a space
    106                     test "$(echo "$options" | sed '/[^/]$/d')" && compopt -o nospace
    107                 ;;
    108                 domain)
    109                     # Get all domain names
    110                     options=$(_remoteProcessWrapper listDomains | awk '{print $1}')
    111                 ;;
    112                 configuration)
    113                     # Get all configurations of the domain specified by
    114                     # the second argument.
    115                     # TODO: find the domain position using $argumentTypes
    116                     options=$(_remoteProcessWrapper listConfigurations "${words[4]}")
    117                 ;;
    118                 *\|*)
    119                     # Possible arguments are separated by "|". Ex : on|off
    120                     options=$(echo $currentArgumentType | sed -e 's#|# #g' -e 's#\*##g')
    121                 ;;
    122                 file_path)
    123                     _filedir;
    124                 ;;
    125             esac
    126         fi
    127     fi
    128     COMPREPLY+=( $(compgen -W "$options" -- "$cur") )
    129 
    130     unset _remoteProcessWrapper
    131 } && complete  -F _remote-process remote-process
    132