summaryrefslogtreecommitdiffstats
path: root/tools/bash_completion/remote-process
blob: c1b55bf824fdb0caa49b90135dc4696121a5d46e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (c) 2014, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# bash completion for remote-process
#
# Execute this file in bash with the built-in command "source",
# it will add autocompletion to remote-process.
#
# To permanently add this autocompletion, add "source this_file"
# in your .bashrc or copy this file in /etc/bash_completion.d/

function _remote-process ()
{
    # Get current word
    local cur prev words cword;
    _init_completion || return;

    local options=""
    if [ $cword -eq 1 ]
    then # Completing the hostname
        _known_hosts_real "$cur"; return
    elif [ $cword -eq 2 ] # Completing tcp port
    then
        options='5000 5001 5008 5009 5019';
    else
        _remoteProcessWrapper () {
            "${words[0]}" "${words[1]}" "${words[2]}" "$@" |sed 's#\r##;/^$/d'
        }

        # Get usage
        local _parameterHelp=$(_remoteProcessWrapper help)

        if [ $cword -eq 3 ]
        then # Completing command
            options=$(echo "$_parameterHelp" | awk '{print $1}')
        else # Completing command argument
            local command=${words[3]}

            # Get current command argument types
            # - keep in the help text only the line describing current command
            # - delete => and posterior
            # - replace space in balisa (<...>) by underscore then delete [<>]
            local argumentTypes=$( echo "$_parameterHelp" | grep "$command"  |\
                 sed -e 's# *=>.*##' -e 's#^[^ ]* *##' \
                     -e 's/> />#/g' -e 's# #_#g' -e 's/>#/> /g' -e 's#[<>]##g' )

            local currentArgumentType=$(echo $argumentTypes |
                                          awk '{print $('$cword'-3)}')

            # Take care of argument list type if it is the last argument
            # Ex : setElementSequence <domain> <configuration> <elem path list>
            if [ "$currentArgumentType" = "" ] &&
                 expr "$argumentTypes" : '.*list' > /dev/null ;
            then
                # Set last argument type specified as the current type
                currentArgumentType=$(echo "$argumentTypes" | awk '{print $NF}')
            fi


            case "${currentArgumentType}" in
                elem_path*|param_path* )
                    local incompletPath=$(echo "${cur}" | sed 's#[^/]*$##')

                    local parameterCommand
                    if [ "$currentArgumentType" = elem_path_list ];
                    then
                        # <elem path list> is the parameter path list
                        #  of the domain specified by the second argument
                        parameterCommand="listDomainElements ${words[4]}"
                    else
                        # Otherwise suggest all parameter paths
                        parameterCommand="listParameters"
                    fi
                    # Get paths and delete everything after the first "/"
                    # following the current input (see complete -o filenames)
                    local options=$(_remoteProcessWrapper $parameterCommand ${incompletPath:-/} |
                                    sed -re "s#(/?${incompletPath}[^/ ]*/?).*#\1#")
                    compopt -o filenames

                    # If some options are folders, do not append a space
                    test "$(echo "$options" | sed '/[^/]$/d')" && compopt -o nospace
                ;;
                domain)
                    # Get all domain names
                    options=$(_remoteProcessWrapper listDomains | awk '{print $1}')
                ;;
                configuration)
                    # Get all configurations of the domain specified by
                    # the second argument.
                    # TODO: find the domain position using $argumentTypes
                    options=$(_remoteProcessWrapper listConfigurations "${words[4]}")
                ;;
                *\|*)
                    # Possible arguments are separated by "|". Ex : on|off
                    options=$(echo $currentArgumentType | sed -e 's#|# #g' -e 's#\*##g')
                ;;
                file_path)
                    _filedir;
                ;;
            esac
        fi
    fi
    COMPREPLY+=( $(compgen -W "$options" -- "$cur") )

    unset _remoteProcessWrapper
} && complete  -F _remote-process remote-process