]>
Commit | Line | Data |
---|---|---|
bae37360 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2014 Michael Tremer # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | # network(8) completion | |
22 | ||
23 | function _network_find_on_cmdline () { | |
24 | local word subcommand c=0 | |
25 | while [ ${c} -lt ${cword} ]; do | |
26 | word="${words[c]}" | |
27 | for subcommand in ${1}; do | |
28 | if [ "${subcommand}" = "${word}" ]; then | |
29 | echo "${subcommand}" | |
30 | return | |
31 | fi | |
32 | done | |
33 | ((c++)) | |
34 | done | |
35 | } | |
36 | ||
37 | function _network_complete_hooks() { | |
38 | local type="${1}" | |
39 | ||
40 | COMPREPLY=( $(compgen -W "$(network raw list-hooks "${type}")" -- "${cur}") ) | |
41 | } | |
42 | ||
43 | function _network_complete_ports() { | |
44 | COMPREPLY=( $(compgen -W "$(network raw list-ports)" -- "${cur}") ) | |
45 | } | |
46 | ||
47 | function _network_complete_zones() { | |
48 | COMPREPLY=( $(compgen -W "$(network raw list-zones)" -- "${cur}") ) | |
49 | } | |
50 | ||
51 | function _network_device() { | |
52 | local words=( $@ ) | |
53 | ||
54 | local commands="list $(network raw list-devices)" | |
55 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
56 | if [[ -z "${cmd}" ]]; then | |
57 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
58 | return 0 | |
59 | fi | |
60 | ||
61 | local args="${words[@]:1}" | |
62 | case "${cmd}" in | |
63 | list) | |
64 | return 0 | |
65 | ;; | |
66 | *) | |
67 | _network_device_subcommand ${args} | |
68 | ;; | |
69 | esac | |
70 | } | |
71 | ||
72 | function _network_device_subcommand() { | |
73 | local words=( $@ ) | |
74 | ||
75 | local commands="discover monitor status unlock ussd" | |
76 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
bae37360 | 77 | if [[ -z "${cmd}" ]]; then |
8d1d2745 | 78 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) |
bae37360 MT |
79 | return 0 |
80 | fi | |
81 | ||
82 | case "${cmd}" in | |
83 | ussd) | |
84 | # TODO | |
85 | ;; | |
86 | esac | |
87 | } | |
88 | ||
8d1d2745 MT |
89 | function _network_dns_server() { |
90 | local words=( $@ ) | |
91 | ||
92 | local commands="add list remove update" | |
93 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
94 | if [[ -z "${cmd}" ]]; then | |
95 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
96 | return 0 | |
97 | fi | |
98 | ||
99 | case "${cmd}" in | |
100 | remove) | |
101 | COMPREPLY=( $(compgen -W "$(network raw list-dns-servers)" \ | |
102 | -- "${cur}") ) | |
103 | ;; | |
104 | esac | |
105 | } | |
106 | ||
bae37360 MT |
107 | function _network_port() { |
108 | local words=( $@ ) | |
109 | ||
110 | local commands="new destroy $(network raw list-ports)" | |
111 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
112 | if [[ -z "${cmd}" ]]; then | |
113 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
114 | return 0 | |
115 | fi | |
116 | ||
117 | local args="${words[@]:1}" | |
118 | case "${cmd}" in | |
119 | new) | |
120 | _network_complete_hooks "port" | |
121 | ;; | |
122 | destroy) | |
123 | _network_complete_ports | |
124 | ;; | |
125 | *) | |
126 | local args="${words[@]:1}" | |
127 | _network_port_subcommand ${args} | |
128 | ;; | |
129 | esac | |
130 | } | |
131 | ||
132 | function _network_port_subcommand() { | |
133 | local words=( $@ ) | |
134 | ||
135 | local commands="create down edit remove status up" | |
136 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
137 | if [[ -z "${cmd}" ]]; then | |
138 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
139 | return 0 | |
140 | fi | |
141 | } | |
142 | ||
8d1d2745 MT |
143 | function _network_route() { |
144 | local words=( $@ ) | |
145 | ||
146 | local commands="add list remove" | |
147 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
148 | if [[ -z "${cmd}" ]]; then | |
149 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
150 | return 0 | |
151 | fi | |
152 | ||
153 | case "${cmd}" in | |
154 | add) | |
155 | if [[ "${cur}" = -* ]]; then | |
156 | COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \ | |
157 | --prohibit --unreachable" -- "${cur}") ) | |
158 | fi | |
159 | ;; | |
160 | list) | |
161 | # TODO auto-complete options like --protocol here | |
162 | COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") ) | |
163 | ;; | |
164 | esac | |
165 | } | |
166 | ||
bae37360 MT |
167 | function _network_settings() { |
168 | local words=( $@ ) | |
169 | ||
170 | local key keys | |
171 | for key in $(network raw list-settings); do | |
172 | keys="${keys} ${key}=" | |
173 | done | |
174 | COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") ) | |
175 | } | |
176 | ||
177 | function _network_zone() { | |
178 | local words=( $@ ) | |
179 | ||
180 | local commands="new destroy $(network raw list-zones)" | |
181 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
182 | if [[ -z "${cmd}" ]]; then | |
183 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
184 | return 0 | |
185 | fi | |
186 | ||
187 | local args="${words[@]:1}" | |
188 | case "${cmd}" in | |
189 | new) | |
190 | # TODO | |
191 | return 0 | |
192 | ;; | |
193 | destroy) | |
194 | _network_complete_zones | |
195 | ;; | |
196 | *) | |
197 | local zone="${cmd}" | |
198 | local args="${words[@]:1}" | |
199 | _network_zone_subcommand "${zone}" ${args} | |
200 | ;; | |
201 | esac | |
202 | } | |
203 | ||
204 | function _network_zone_subcommand() { | |
205 | local zone="${1}" | |
206 | shift | |
207 | ||
208 | local words=( $@ ) | |
209 | ||
210 | local commands="config disable down edit enable port status up" | |
211 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
212 | if [[ -z "${cmd}" ]]; then | |
213 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
214 | return 0 | |
215 | fi | |
216 | ||
217 | local args="${words[@]:1}" | |
218 | case "${cmd}" in | |
219 | config) | |
220 | # TODO | |
221 | ;; | |
222 | port) | |
223 | _network_zone_subcommand_port "${zone}" ${args} | |
224 | ;; | |
225 | esac | |
226 | } | |
227 | ||
228 | function _network_zone_subcommand_port() { | |
229 | local zone="${1}" | |
230 | shift | |
231 | ||
232 | local words=( $@ ) | |
233 | ||
234 | local commands="attach detach $(network raw list-ports-of-zone "${zone}")" | |
235 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
236 | if [[ -z "${cmd}" ]]; then | |
237 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
238 | return 0 | |
239 | fi | |
240 | ||
241 | case "${cmd}" in | |
242 | attach) | |
243 | COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \ | |
244 | -- "${cur}") ) | |
245 | ;; | |
246 | detach) | |
247 | COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \ | |
248 | -- "${cur}") ) | |
249 | ;; | |
250 | *) | |
251 | local port="${cmd}" | |
252 | local args="${words[@]:1}" | |
253 | _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args} | |
254 | ;; | |
255 | esac | |
256 | } | |
257 | ||
258 | function _network_zone_subcommand_port_subcommand() { | |
259 | local zone="${1}" | |
260 | local port="${2}" | |
261 | shift 2 | |
262 | ||
263 | local words=( $@ ) | |
264 | ||
265 | local commands="edit" | |
266 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
267 | if [[ -z "${cmd}" ]]; then | |
268 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
269 | return 0 | |
270 | fi | |
271 | ||
272 | case "${cmd}" in | |
273 | edit) | |
274 | # TODO auto-complete the zone-port hook | |
275 | ;; | |
276 | esac | |
277 | } | |
278 | ||
279 | function _network() { | |
280 | local cur prev words cword | |
281 | _init_completion || return | |
282 | ||
283 | local cmd i | |
284 | for (( i=1; i < ${#words[@]} - 1; i++ )); do | |
285 | [[ "${words[i]}" = -* ]] && continue | |
286 | cmd="${words[i]}" && break | |
287 | done | |
288 | ||
289 | if [[ -z "${cmd}" ]]; then | |
290 | case "${cur}" in | |
291 | -*) | |
292 | COMPREPLY=( $(compgen -W "--debug" -- "${cur}") ) | |
293 | ;; | |
294 | *) | |
8d1d2745 MT |
295 | COMPREPLY=( $(compgen -W "device dns-server help hostname \ |
296 | port reset route settings status zone" -- "${cur}") ) | |
bae37360 MT |
297 | ;; |
298 | esac | |
299 | ||
300 | return 0 | |
301 | fi | |
302 | ||
303 | local args="${words[@]:i+1}" | |
304 | case "${cmd}" in | |
305 | device) | |
306 | _network_device ${args} | |
307 | ;; | |
8d1d2745 MT |
308 | dns-server) |
309 | _network_dns_server ${args} | |
310 | ;; | |
bae37360 MT |
311 | port) |
312 | _network_port ${args} | |
313 | ;; | |
8d1d2745 MT |
314 | route) |
315 | _network_route ${args} | |
316 | ;; | |
bae37360 MT |
317 | settings) |
318 | _network_settings ${args} | |
319 | ;; | |
320 | start|stop|status) | |
321 | # start, stop and status optionally take a zone | |
322 | _network_complete_zones | |
323 | ;; | |
324 | zone) | |
325 | _network_zone ${args} | |
326 | ;; | |
327 | esac | |
328 | } && complete -F _network network |