blob: a9c549231f0eef3d8ec82e638fe631890a5dd4ab (
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
|
# **** 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 ****
test -z "$_vyatta_default_pager" && \
declare -r _vyatta_default_pager="less\
--QUIT-AT-EOF\
--quit-if-one-screen\
--LONG-PROMPT\
--RAW-CONTROL-CHARS\
--squeeze-blank-lines\
--buffers=64\
--auto-buffers\
--no-lessopen"
declare -x VYATTA_PAGER=$_vyatta_default_pager
test -f /etc/default/vyatta && \
source /etc/default/vyatta
test ! -d "$vyatta_op_templates" && \
return 0
declare _vyatta_op_last_comp
declare _vyatta_op_node_path
declare -a _vyatta_op_noncompletions _vyatta_op_completions
# $1: label
# #2...: strings
_vyatta_op_debug ()
{
echo -ne \\n$1:
shift
for s ; do
echo -ne " \"$s\""
done
}
_vyatta_op_init ()
{
complete -F _vyatta_op_expand ''
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
bind 'set show-all-if-ambiguous on'
shopt -s histverify
}
_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
}
# $1: label
# $2...: help
_vyatta_op_print_help ()
{
local label=$1 help=$2
if [ ${#help} -eq 0 ] ; then
echo -ne "\n $label"
elif [ ${#label} -lt 6 ] ; then
echo -ne "\n $label\t\t$help"
elif [ ${#label} -lt 14 ] ; then
echo -ne "\n $label\t$help"
else
echo -ne "\n $label\n\t\t$help"
fi
}
# $1: $cur
# $2...: possible completions
_vyatta_op_help ()
{
local cur=$1; shift
local ndef node_tag_help node_run help last_help
ndef=${_vyatta_op_node_path}/node.tag/node.def
[ -f $ndef ] && \
node_tag_help=$( _vyatta_op_get_node_def_field $ndef help )
ndef=${_vyatta_op_node_path}/node.def
[ -f $ndef ] && \
node_run=$( _vyatta_op_get_node_def_field $ndef run )
echo -en "\nPossible completions:"
if [ -z "$cur" -a -n "$node_run" ]; then
_vyatta_op_print_help '<Enter>' "Execute the current command"
fi
for comp ; do
if [ -z "$comp" ] ; then
if [ "X$node_tag_help" == "X$last_help" ] ; then
help=""
else
last_help=$node_tag_help
help=$node_tag_help
fi
_vyatta_op_print_help '*' "$help"
elif [[ -z "$cur" || $comp == ${cur}* ]] ; then
ndef=${_vyatta_op_node_path}/$comp/node.def
if [ -f $ndef ] ; then
help=$( _vyatta_op_get_node_def_field $ndef help )
else
help=$node_tag_help
fi
if [ "X$help" == "X$last_help" ] ; then
help=""
else
last_help=$help
fi
_vyatta_op_print_help "$comp" "$help"
fi
done
}
_vyatta_op_set_node_path ()
{
_vyatta_op_node_path=$vyatta_op_templates
for (( i=0 ; i<COMP_CWORD ; i++ )) ; do
if [ -f "${_vyatta_op_node_path}/${COMP_WORDS[i]}/node.def" ] ; then
_vyatta_op_node_path+=/${COMP_WORDS[i]}
elif [ -f ${_vyatta_op_node_path}/node.tag/node.def ] ; then
_vyatta_op_node_path+=/node.tag
else
return 1
fi
done
}
_vyatta_op_set_completions ()
{
local -a allowed completions
for ndef in ${_vyatta_op_node_path}/*/node.def ; do
if [[ $ndef == */node.tag/node.def ]] ; then
local acmd=$( _vyatta_op_get_node_def_field $ndef allowed )
local -a a=($( eval "$acmd" ))
if [ ${#a[@]} -ne 0 ] ; then
allowed+=( "${a[@]}" )
else
allowed+=( "" )
fi
else
local sdir=${ndef%/*}
allowed+=( ${sdir##*/} )
fi
done
# donot complete entries like <HOSTNAME> or <A.B.C.D>
_vyatta_op_noncompletions=( )
completions=( )
for (( i=0 ; i<${#allowed[@]} ; i++ )) ; do
if [[ "${allowed[i]}" == \<*\> ]] ; then
_vyatta_op_noncompletions+=( ${allowed[i]} )
else
completions+=( ${allowed[i]} )
fi
done
_vyatta_op_completions=($( printf "%s\n" ${completions[@]} | sort -u ))
}
_vyatta_op_expand ()
{
local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; )
shopt -s extglob nullglob
local cur=""
if (( ${#COMP_WORDS[@]} > 0 )); then
cur=${COMP_WORDS[COMP_CWORD]}
else
(( COMP_CWORD = ${#COMP_WORDS[@]} ))
fi
if [ "${COMP_WORDS[*]}" != "$_vyatta_op_last_comp" ] ; then
if ! _vyatta_op_set_node_path ; then
echo -e \\a
COMPREPLY=( "" " " )
eval "$restore_shopts"
return 1
fi
_vyatta_op_set_completions
fi
COMPREPLY=($( compgen -W "${_vyatta_op_completions[*]}" -- $cur ))
# if the last command line arg is empty and we have
# an empty completion option (meaning wild card),
# append a blank(s) to the completion array to force ambiguity
if [ -z "$cur" ] ; then
for comp ; do
if [ -z "$comp" ] ; then
if [ ${#COMPREPLY[@]} -eq 0 ] ; then
COMPREPLY+=( " " "" )
else
COMPREPLY+=( " " )
fi
fi
done
fi
if [ "${COMP_WORDS[*]}" == "$_vyatta_op_last_comp" ] ; then
_vyatta_op_help "$cur" \
${_vyatta_op_noncompletions[@]} \
${_vyatta_op_completions[@]} \
| eval ${VYATTA_PAGER:-cat}
COMPREPLY=( "" " " )
_vyatta_op_last_comp=""
else
_vyatta_op_last_comp="${COMP_WORDS[*]}"
fi
eval "$restore_shopts"
}
_vyatta_op_run ()
{
local -i estat
local tpath=$vyatta_op_templates
local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; )
shopt -s extglob nullglob
_vyatta_op_last_comp=""
false; estat=$?
for arg ; do
if [ -f "$tpath/$arg/node.def" ] ; then
tpath+=/$arg
elif [ -f $tpath/node.tag/node.def ] ; then
tpath+=/node.tag
else
echo "Invalid command" >&2
eval $restore_shopts
return 1
fi
done
local run_cmd=$(_vyatta_op_get_node_def_field $tpath/node.def run)
local ret=0
if [ -n "$run_cmd" ]; then
if [ "$1" == "show" ] ; then
( eval "$run_cmd" ) | eval "${VYATTA_PAGER:-cat}"
else
eval "$run_cmd"
fi
else
echo "Incomplete command" >&2
ret=1
fi
eval $restore_shopts
return $ret
}
# don't initialize if we are in configure mode
if [ "$_OFR_CONFIGURE" == "ok" ]; then
return 0
fi
nullglob_save=$( shopt -p nullglob )
shopt -s nullglob
for f in ${vyatta_datadir}/vyatta-op/functions/allowed/* ; do
source $f
done
eval $nullglob_save
unset nullglob_save
alias no-more=cat
_vyatta_op_init $@
### Local Variables:
### mode: shell-script
### End:
|