summaryrefslogtreecommitdiff
path: root/etc/bash_completion.d/vyatta-op
blob: 6d1eb9bad5f4735a3fa9b865ccb22f4b4dcc179b (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

# **** License ****
# Version: VPL 1.0
# 
# The contents of this file are subject to the Vyatta Public License
# Version 1.0 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.vyatta.com/vpl
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2006, 2007 Vyatta, Inc.
# All Rights Reserved.
# 
# Author: Tom Grennan
# Date: 2007
# Description: setup bash completion for Vyatta operational commands
# 
# **** End License ****

# don't do this if we are going into configure mode
if [ "$_OFR_CONFIGURE" == "ok" ]; then
  return 0
fi

# first set vars per args of the "source ...vyatta-op VAR=FOO"
_vyatta_extglob=$(shopt -p extglob)
shopt -s extglob
for e ; do
    if [[ $e == *=* ]] ; then
	eval $e
    fi
done

test -f /etc/default/vyatta && source /etc/default/vyatta

: ${vyatta_op_templates:=/opt/vyatta/share/vyatta-op/templates}

declare -a _vyatta_op_comp_words
declare -a _vyatta_op_allowed
declare _vyatta_op_node_def
declare -x OFR_PAGER
declare -x OFR_DEFAULT_PAGER

_vyatta_op_get_node_def_field ()
{
    local file=$1 field=$2

    sed -n '/^'"$field"':/,$ {
# strip field name and hold rest of line
    s/[a-z]*: *//
    h
    :b
# at EOF, print hold buffer and quit
    $ { x; p; q }
# input next line
    n
# if start of another field def, print hold buf and quit
    /^[a-z]*:/ { x; p; q }
# add to hold buf and branch to input next line
    H
    bb
    }' $file
}

_vyatta_op_set_allowed_subdirs ()
{
    local dir=$1

    _vyatta_op_allowed=($( find ${vyatta_op_templates}$1/* -maxdepth 0 -type d -printf %f\\n ))
}

_vyatta_op_set_allowed ()
{
    local nullglob=$( shopt -p nullglob )
    shopt -s nullglob
    _vyatta_op_allowed=($( eval "$( _vyatta_op_get_node_def_field $_vyatta_op_node_def allowed )" ))
    eval $nullglob
}

_vyatta_op_is_allowed ()
{
    local arg=$1 allowed

    # return immediately if nothing allowed
    [ ${#_vyatta_op_allowed[@]} -ne 0 ] || return
    # -- is wildcard that allows anything
    [ ${_vyatta_op_allowed[0]} == -- ] && return
    for allowed in ${_vyatta_op_allowed[@]} ; do
	[ "$arg" == $allowed ] && return
    done
    false
}

_vyatta_op_scan ()
{
    local -a argv=( $@ )
    local -i i=0 argc=$#
    local arg dir node_def
    local -a allowed

    while true ; do
	if [ -d ${vyatta_op_templates}${dir}/node.tag ] ; then
	    node_def=${vyatta_op_templates}${dir}/node.tag/node.def
	    if [ ! -f $node_def ] ; then
		echo -e \\ninvalid template, missing:  >&2
		echo -e \\t$_vyatta_op_node_def >&2
		return 1
	    fi
	    _vyatta_op_node_def=$node_def
	    _vyatta_op_set_allowed $@
	    [[ $i -eq $argc ]] && return 0
	    arg=${argv[i]}; let i++
	    if _vyatta_op_is_allowed $arg ; then
		dir+=/node.tag
		_vyatta_op_set_allowed_subdirs $dir
		continue
	    elif [[ $i -ne $argc ]] ; then
		echo -e \\ninvalid option: $arg >&2
		return 1
	    fi
	fi
	[[ $i -eq $argc ]] && return 0
	arg=${argv[i]}; let i++
	node_def=${vyatta_op_templates}${dir}/${arg}/node.def
	if [ -f $node_def ] ; then
	    dir+=/${arg}
	    _vyatta_op_node_def=$node_def
	    _vyatta_op_set_allowed_subdirs $dir
	    continue
	elif [[ $i -ne $argc ]] ; then
	    echo -e \\ninvalid template, missing node.\{def,tag\} >&2
	    echo -e \\t${vyatta_op_templates}$dir/$arg >&2
	    return 1
	fi
    done
}

_vyatta_op_help ()
{
    local help
    local heading="\nPossible completions:"
    local nullglob
    local -a subnodes
    local subdir

    if grep -q allowed: $_vyatta_op_node_def ; then
	eval help=$( _vyatta_op_get_node_def_field $_vyatta_op_node_def help )
	if [ ${#_vyatta_op_allowed[@]} -ne 0 ] ; then
	    echo -ne $heading
	    for a in ${_vyatta_op_allowed[*]} ; do
		printf "\n  %-12s  $help" $a
	    done
	else
	    false
	fi
    else
	nullglob=$( shopt -p nullglob )
	shopt -s nullglob
	subnodes=( ${_vyatta_op_node_def%/node.def}/*/node.def )
	eval $nullglob
	if [ ${#subnodes[@]} -ne 0 ] ; then
	    echo -ne $heading
	    for n in ${subnodes[@]} ; do
		eval help=$( _vyatta_op_get_node_def_field $n help )
		subdir=${n%/node.def}
		printf "\n  %-12s  $help" ${subdir##*/}
	    done
	else
	    false
	fi
    fi
}

_vyatta_op_expand ()
{
    local cur=${COMP_WORDS[COMP_CWORD]}

    if _vyatta_op_scan "${COMP_WORDS[@]}" ; then
	COMPREPLY=($( compgen -W "${_vyatta_op_allowed[*]}" -- $cur ))
	if [[ ${#COMPREPLY[@]} -ne 0 &&
		    "${COMP_WORDS[*]}" == "${_vyatta_op_comp_words[*]}" ]]
	then
	    _vyatta_op_help && \
		COMPREPLY=($( compgen -W "== --" -- $cur ))
	    _vyatta_op_comp_words=
	else
	    _vyatta_op_comp_words=( "${COMP_WORDS[@]}" )
	fi
    fi
}

_vyatta_op_run ()
{
    if _vyatta_op_scan $@ ; then
	eval "$( _vyatta_op_get_node_def_field $_vyatta_op_node_def run )"
    fi
}

for p in $PAGER pager most less more cat ; do
    if type -t $p &>/dev/null ; then
	OFR_DEFAULT_PAGER=$p
	break
    fi
done

: ${OFR_PAGER:=${OFR_DEFAULT_PAGER}}

if [[ -d $vyatta_op_templates ]]
then
    for xd in $vyatta_op_templates/* ; do
	if [ -d $xd ] ; then
	    cmd=${xd##*/}
	    complete -F _vyatta_op_expand $cmd
	    eval alias $cmd=\'_vyatta_op_run $cmd\'
	fi
    done
fi

shopt -s histverify
eval $_vyatta_extglob
unset _vyatta_extglob

###  Local Variables:
###  mode: shell-script
###  End: