blob: fe402ef2f6abd4ea90ef34198ee931fec89b6676 (
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
|
# **** 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 _vyatta_op_node_path
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
local n=${vyatta_op_templates}/${dir}/node.tag/node.def
local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; )
shopt -s extglob nullglob
unset _vyatta_op_allowed
for a in ${vyatta_op_templates}/${dir}/!(node.tag) ; do
[[ -d $a && -f $a/node.def ]] && \
_vyatta_op_allowed+=( ${a##*/} )
done
if [ -f $n ] ; then
local acmd=$( _vyatta_op_get_node_def_field $n allowed )
_vyatta_op_allowed+=($( eval "$acmd" ))
fi
eval $restore_shopts
}
_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
for allowed in ${_vyatta_op_allowed[@]} ; do
# -- is wildcard that allows anything
[ $allowed == -- ] && return
[ "$arg" == $allowed ] && return
done
false
}
_vyatta_op_scan ()
{
local -a argv=( $@ )
local -i i=0 argc=$#
local arg dir node_def
local -a allowed
local is_run=${argv[0]}
argv=( "${argv[@]:1}" )
(( argc-- ))
local last_tag=0
local tpath=${vyatta_op_templates}
local end_space=0
if [ "${argv[argc-1]}" == "" ]; then
end_space=1
argv=( "${argv[@]:0:((argc-1))}" )
(( argc-- ))
fi
local last_idx
(( last_idx = argc - 1 ))
for (( i = 0; i < argc; i++ )); do
last_tag=0
arg=${argv[i]}
local _tpath=$tpath/$arg
if [ -d $_tpath ]; then
tpath=$_tpath
continue
fi
_tpath=$tpath/node.tag
if [ -d $_tpath ]; then
if (( i != last_idx || is_run )); then
# validate value if (not last component) OR (called from "run")
local ndef=$_tpath/node.def
if [ ! -f $ndef ]; then
# no template
break
fi
_vyatta_op_node_def=$ndef
_vyatta_op_set_allowed
if ! _vyatta_op_is_allowed $arg ; then
# invalid tag value.
# XXX probably display "allowed:"-specific help string when
# that is supported?
break
fi
fi
last_tag=1
tpath=$_tpath
continue
fi
break
done
if (( i < last_idx )); then
# some component before the last is invalid. return failure.
return 1
fi
if (( i == last_idx && end_space )); then
# last component is a complete word and does not match template.
# return failure.
return 1
fi
if (( is_run )); then
# called from "run"
if (( i == last_idx )); then
# whole line must match
return 1
fi
# return the path
_vyatta_op_node_path=$tpath
return 0
fi
if (( i == last_idx )); then
# last component is an incomplete word.
# this could be an incomplete "non-tag" node.
_vyatta_op_node_path=$tpath
_vyatta_op_set_allowed_subdirs ${tpath:${#vyatta_op_templates}}
return 0
fi
if (( !end_space )); then
# if !last_tag: a complete non-tag node (without space at the end).
# if last_tag: an incomplete tag node.
# in either case, the possible completions include any subdirs plus
# any allowed tag values
_vyatta_op_node_path=${tpath%/*}
_vyatta_op_set_allowed_subdirs \
${_vyatta_op_node_path:${#vyatta_op_templates}}
return 0
else
# last component is a complete node.
_vyatta_op_node_path=$tpath
_vyatta_op_set_allowed_subdirs ${tpath:${#vyatta_op_templates}}
return 0
fi
}
# $1: \@completions
# $2: \@help_strs
# assume the arrays are not empty
_vyatta_op_print_help ()
{
local -a comps=()
local -a helps=()
local -i taglen=12
eval "comps=( \"\${$1[@]}\" )"
eval "helps=( \"\${$2[@]}\" )"
echo -en "\nPossible completions:"
for (( idx = 0 ; idx < ${#comps[@]} ; idx++ )) ; do
if [ ${#comps[idx]} -lt $taglen ] ; then
printf "\n %-${taglen}s\t%s" "${comps[idx]}" "${helps[idx]}"
else
printf "\n %s\n\t\t%s" "${comps[idx]}" "${helps[idx]}"
fi
done
}
_vyatta_op_help ()
{
local help
local -a subnodes
local -a allowed
local subdir
local subtag
local -a hcomps
local -a hstrs
if [ -d $_vyatta_op_node_path ]; then
local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; )
shopt -s extglob nullglob
subnodes=( $_vyatta_op_node_path/!(node.tag)/node.def )
subtag=$_vyatta_op_node_path/node.tag/node.def
eval $restore_shopts
if [ -f $subtag ]; then
allowed=($( eval "$( _vyatta_op_get_node_def_field $subtag allowed )" ))
eval help="$( _vyatta_op_get_node_def_field $subtag help )"
if [ ${#allowed[@]} -ne 0 ] ; then
for a in "${allowed[@]}"; do
hcomps+=( "$a" )
hstrs+=( "$help" )
done
fi
fi
if [ ${#subnodes[@]} -ne 0 ] ; then
for n in ${subnodes[@]} ; do
eval help="$( _vyatta_op_get_node_def_field $n help )"
subdir=${n%/node.def}
hcomps+=( "${subdir##*/}" )
hstrs+=( "$help" )
done
fi
if [ ${#hcomps[@]} -eq 0 ]; then
# no completions available
return 1
fi
_vyatta_op_print_help hcomps hstrs
return 0
else
return 1
fi
}
_vyatta_op_expand ()
{
local cur=${COMP_WORDS[COMP_CWORD]}
local a w
if _vyatta_op_scan 0 "${COMP_WORDS[@]}" ; then
a="${_vyatta_op_allowed[*]}"
if [ "$a" != '--' ] ; then
w=$a
else
w="" # donot expand wildcard
fi
COMPREPLY=($( compgen -W "$w" -- $cur ))
if [[ ${#COMPREPLY[@]} -ne 0 || "$a" == '--' ]] &&
[[ "${COMP_WORDS[*]}" == "${_vyatta_op_comp_words[*]}" ]]
then
_vyatta_op_help
COMPREPLY=($( compgen -o nospace -W "______________ ..." ))
_vyatta_op_comp_words=( )
else
_vyatta_op_comp_words=( "${COMP_WORDS[@]}" )
fi
fi
}
_vyatta_op_run ()
{
if _vyatta_op_scan 1 $@ ; then
local ndef=$_vyatta_op_node_path/node.def
if [ -f "$ndef" ]; then
eval "$( _vyatta_op_get_node_def_field $ndef run )"
return 0
fi
fi
echo "Invalid command"
}
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
bind 'set show-all-if-ambiguous on'
### Local Variables:
### mode: shell-script
### End:
|