-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathip-array_bash_completion
221 lines (205 loc) · 9.19 KB
/
ip-array_bash_completion
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
#!/usr/bin/env bash
# ------------------------------------------------------------------------- #
#*# ###### #
# # # # # ##### ##### ## # #
# # # # # # # # # # # # #
# ###### ##### # # # # # # # # #
# # ####### ##### ##### ###### #
# # # # # # # # # # #
### # # # # # # # # # #
# ------------------------------------------------------------------------- #
#
# Copyright (C) 2005-2018 Mart Frauenlob aka AllKind
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
# ------------------------------------------------------------------------- #
#
# IP-ARRAY BASH COMPLETION
#
# ------------------------------------------------------------------------- #
_ip-array_complete() {
local cur prev cword words
# expecting _get_comp_words_by_ref() to exist from bash_completion
if declare -f _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref -n : cur prev cword words || return
else
return 0
fi
local str_act=""
local -i i x
local -a arr_opt=()
# main parameters
local -a arr_par=( start stop restart test open tc-start tc-stop lockdown
dry-run diff-last-activated save restore prepare-restore-on-start
save-commands save-iptables-commands save-modprobe-commands
save-sysctl-commands save-tc-commands save-shaping-commands parse-xml
compat-check interactive show version help )
# core options - can always be used
local -a arr_core_opt=( --debug --err-exit --base-dir --config-file --config-dir
--defaults-file --exe-dir --lib-dir --run-dir --color --shell --verbose
--syslog-verbose --no-scripts --set-option )
# gather information about the command line
if [[ ${words[1]} = @($(IFS='|' ;printf "%s\n" "${arr_par[*]}")) ]]; then
str_act="${words[1]}" # this is the main parameter
fi
# here we go
COMPREPLY=()
# main parameter
if ((cword == 1)); then
COMPREPLY=( $( compgen -W '${arr_par[@]}' -- "$cur" ) )
return 0
elif ((cword > 1)); then
if [[ -z $str_act ]]; then
return 0 # if main parameter is missing, we don't continue
fi
fi
# catch variables and command substitution
if [[ $cur == \$\(* ]]; then # command substitution
COMPREPLY=( $(compgen -c -P '$(' ${cur#??}) )
return 0
elif [[ $cur == \$\{* ]]; then # variables with a leading `${'
COMPREPLY=( $(compgen -v -P '${' -S '}' ${cur#??}) )
return 0
elif [[ $cur == \$* ]]; then # variables with a leading `$'
COMPREPLY=( $(compgen -v -P '$' ${cur#?} ) )
return 0
fi
# gather options depending on main parameter
if [[ $str_act = @(start|restart|test) ]]; then
arr_opt=( "${arr_core_opt[@]}" --dry-run --no-autosave --no-compat-check --no-diff
--no-iface-check --no-modprobe --no-sysctl --parse-xml --parse-ruleblocks
--parse-rulefiles --parse-templates --use-ipset --gen-format --reload-time )
if [[ $str_act = start ]]; then
arr_opt[${#arr_opt[@]}]="--restore-on-start"
fi
if [[ $str_act = @(start|restart) ]]; then
arr_opt[${#arr_opt[@]}]="--test"
fi
elif [[ $str_act = stop ]]; then
arr_opt=( "${arr_core_opt[@]}" --dry-run --no-iface-check --no-modprobe
--no-sysctl --use-ipset --gen-format --test)
elif [[ $str_act = @(open|tc-start|tc-stop) ]]; then
arr_opt=( "${arr_core_opt[@]}" --dry-run --no-autosave --no-compat-check
--no-diff --no-iface-check --parse-xml --parse-ruleblocks --parse-rulefiles
--parse-templates --use-ipset --test --gen-format --reload-time )
elif [[ $str_act = dry-run ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --no-modprobe
--no-sysctl --parse-xml --parse-ruleblocks --parse-rulefiles --parse-templates
--use-ipset --gen-format )
elif [[ $str_act = lockdown ]]; then
arr_opt=( "${arr_core_opt[@]}" --dry-run --no-compat-check --test)
elif [[ $str_act = @(save|restore) ]]; then
arr_opt=( "${arr_core_opt[@]}" --ipt-counters --save-file --save-ips-file )
elif [[ $str_act = compat-check ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-modprobe --no-sysctl )
elif [[ $str_act = parse-xml ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check
--parse-ruleblocks --parse-rulefiles --parse-templates --use-ipset )
elif [[ $str_act = prepare-restore-on-start ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --parse-xml
--parse-ruleblocks --parse-rulefiles --parse-templates --use-ipset
'--set-option' --gen-format --save-file )
elif [[ $str_act = save-sysctl-commands ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --parse-xml
--parse-rulefiles --save-file )
elif [[ $str_act = save-modprobe-commands ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --save-file )
elif [[ $str_act = save-tc-commands ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --parse-xml
--parse-ruleblocks --save-file )
elif [[ $str_act = @(save-iptables-commands|save-shaping-commands) ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --no-modprobe
--parse-xml --parse-ruleblocks --parse-rulefiles --parse-templates --use-ipset
--gen-format --save-file --save-ips-file )
elif [[ $str_act = save-commands ]]; then
arr_opt=( "${arr_core_opt[@]}" --no-compat-check --no-iface-check --no-modprobe
--no-sysctl --parse-xml --parse-ruleblocks --parse-rulefiles --parse-templates
--use-ipset --gen-format --save-file --save-ips-file )
elif [[ $str_act = show ]]; then
arr_opt=( "${arr_core_opt[@]}" --show-config )
elif [[ $str_act = @(diff-last-activated|interactive) ]]; then
arr_opt=( "${arr_core_opt[@]}" )
elif [[ $str_act = @(help|version) ]]; then
return 0 # no options to complete on with this parameters
fi
# option requests
if [[ $cur = -* ]]; then
if [[ $prev = --@(color|use-ipset|set-option|gen-format|restore-on-start|reload-time|syslog-verbose|verbose) ]]
then return 0 # arg required
elif [[ $prev = @(-co|-ui|-o|-gf|-rs|-rt|-sc|-sv|-v) ]]; then
return 0 # arg required
elif [[ $prev = @(-b|--base-dir|-c|--config-file|-C|--config-dir|-d|--defaults-file|-e|--exe-dir|-l|--lib-dir|-r|--run-dir|-s|--save-file|-is|--save-ips-file|-sh|--shell) ]]
then _filedir # still could be a file / dir name
else
# present options depending on main parameter
if ((${#arr_opt[@]})); then
for ((i=2; i<$((${#words[@]} - 1)); i++)); do # de-dupe options
[[ ${words[i]} = --?* ]] || continue
[[ ${words[i]} = @(--set-option|--show-config) ]] && continue # allowed multiple times
for x in ${!arr_opt[@]}; do
if [[ ${words[i]} = ${arr_opt[x]} ]]; then
unset arr_opt[x]
break
fi
done
done
COMPREPLY=( $( compgen -W '${arr_opt[@]}' -- "$cur" ) )
fi
fi
elif [[ $cur = * ]]; then # anything else requested
# handle options with predictable args
if [[ $prev = @(-o|--set-option|-rt|--reload-time) ]]; then
return 0 # arg required
elif [[ $prev = @(-gf|--gen-format) ]]; then
COMPREPLY=( $( compgen -W 'cmd ipt' -- "$cur" ) )
elif [[ $prev = @(-ns|--no-scripts) ]]; then
COMPREPLY=( $( compgen -W 'all prolog epilog' -- "$cur" ) )
elif [[ $prev = @(-sc|--show-config) ]]; then
COMPREPLY=( $( compgen -W 'all ansi_codes colors defaults main targets matches \
rules ipt_args sysctl ruleblocks templates public_functions find_templates' \
-- "$cur" ) )
if ((${#COMPREPLY[@]} == 1)); then
if [[ ${COMPREPLY[0]} = @(defaults|main|ipt_args|ruleblocks|templates|public_functions|find_templates) ]]
then compopt -o nospace
fi
fi
elif [[ $prev = @(-co|--color|-ui|--use-ipset|-rs|--restore-on-start) ]]; then
COMPREPLY=( $( compgen -W '0 1' -- "$cur" ) )
elif [[ $prev = @(-sv|--syslog-verbose) ]]; then
COMPREPLY=( $( compgen -W '0 1 2 3 4 5 6' -- "$cur" ) )
elif [[ $prev = @(-v|--verbose) ]]; then
COMPREPLY=( $( compgen -W '0 1 2 3 4 5 6 7 8 9' -- "$cur" ) )
elif [[ $prev = @(-b|--base-dir|-c|--config-file|-C|--config-dir|-d|--defaults-file|-e|--exe-dir|-l|--lib-dir|-r|--run-dir|-s|--save-file|-is|--save-ips-file|-sh|--shell) ]]
then _filedir # still could be a file / dir name
else
# present options depending on main parameter
if ((${#arr_opt[@]})); then
for ((i=2; i<$((${#words[@]} - 1)); i++)); do # de-dupe options
[[ ${words[i]} = --?* ]] || continue
[[ ${words[i]} = @(--set-option|--show-config) ]] && continue # allowed multiple times
for x in ${!arr_opt[@]}; do
if [[ ${words[i]} = ${arr_opt[x]} ]]; then
unset arr_opt[x]
break
fi
done
done
COMPREPLY=( $( compgen -W '${arr_opt[@]}' -- "$cur" ) )
fi
fi
fi
}
complete -F _ip-array_complete ip-array /etc/init.d/ip-array