]>
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 | ||
1c6a4e30 | 23 | _network_find_on_cmdline () { |
bae37360 MT |
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 | ||
1c6a4e30 | 37 | _network_complete_hooks() { |
bae37360 MT |
38 | local type="${1}" |
39 | ||
40 | COMPREPLY=( $(compgen -W "$(network raw list-hooks "${type}")" -- "${cur}") ) | |
41 | } | |
42 | ||
1c6a4e30 | 43 | _network_complete_ports() { |
bae37360 MT |
44 | COMPREPLY=( $(compgen -W "$(network raw list-ports)" -- "${cur}") ) |
45 | } | |
46 | ||
1c6a4e30 | 47 | _network_complete_zones() { |
bae37360 MT |
48 | COMPREPLY=( $(compgen -W "$(network raw list-zones)" -- "${cur}") ) |
49 | } | |
50 | ||
1c6a4e30 | 51 | _network_device() { |
bae37360 MT |
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 | ||
1c6a4e30 | 72 | _network_device_subcommand() { |
bae37360 MT |
73 | local words=( $@ ) |
74 | ||
f5ee091e | 75 | local commands="discover identify monitor status unlock ussd" |
bae37360 | 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 | ||
1c6a4e30 | 89 | _network_dhcpd() { |
cc02f6be MT |
90 | local proto="${1}" |
91 | shift | |
92 | ||
93 | local words=( $@ ) | |
94 | ||
95 | local commands="edit reload restart show start stop subnet" | |
96 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
97 | if [[ -z "${cmd}" ]]; then | |
98 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
99 | return 0 | |
100 | fi | |
101 | ||
102 | local args="${words[@]:1}" | |
103 | case "${cmd}" in | |
104 | subnet) | |
105 | _network_dhcpd_subnet "${proto}" ${args} | |
106 | ;; | |
107 | esac | |
108 | } | |
109 | ||
1c6a4e30 | 110 | _network_dhcpd_subnet() { |
cc02f6be MT |
111 | local proto="${1}" |
112 | shift | |
113 | ||
114 | local words=( $@ ) | |
115 | ||
116 | local commands="new remove show $(network raw list-dhcpd-subnets "${proto}")" | |
117 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
118 | if [[ -z "${cmd}" ]]; then | |
119 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
120 | return 0 | |
121 | fi | |
122 | ||
123 | local subnet="${words[0]}" | |
124 | local args="${words[@]:1}" | |
125 | case "${cmd}" in | |
126 | new) | |
127 | : # TODO | |
128 | ;; | |
129 | remove) | |
130 | : # TODO | |
131 | ;; | |
132 | [0-9]*) | |
133 | _network_dhcpd_subnet_subcommand "${proto}" "${subnet}" ${args} | |
134 | ;; | |
135 | esac | |
136 | } | |
137 | ||
1c6a4e30 | 138 | _network_dhcpd_subnet_subcommand() { |
cc02f6be MT |
139 | local proto="${1}" |
140 | local subnet="${2}" | |
141 | shift 2 | |
142 | ||
143 | local words=( $@ ) | |
144 | ||
145 | local commands="edit options range show" | |
146 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
147 | if [[ -z "${cmd}" ]]; then | |
148 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
149 | return 0 | |
150 | fi | |
151 | ||
152 | local args="${words[@]:1}" | |
153 | case "${cmd}" in | |
154 | edit) | |
155 | : # TODO | |
156 | ;; | |
157 | options) | |
158 | _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args} | |
159 | ;; | |
160 | range) | |
161 | _network_dhcpd_subnet_subcommand_range "${proto}" "${subnet}" ${args} | |
162 | ;; | |
163 | esac | |
164 | } | |
165 | ||
1c6a4e30 | 166 | _network_dhcpd_subnet_subcommand_options() { |
cc02f6be MT |
167 | local proto="${1}" |
168 | local subnet="${2}" | |
169 | shift 2 | |
170 | ||
171 | local options option | |
172 | for option in $(network raw list-dhcpd-subnet-options "${proto}"); do | |
173 | options="${options} ${option}=" | |
174 | done | |
175 | ||
176 | COMPREPLY=( $(compgen -W "${options}" -- "${cur}") ) | |
177 | } | |
178 | ||
1c6a4e30 | 179 | _network_dhcpd_subnet_subcommand_range() { |
cc02f6be MT |
180 | local proto="${1}" |
181 | local subnet="${2}" | |
182 | shift 2 | |
183 | ||
184 | local words=( $@ ) | |
185 | ||
186 | local commands="new remove" | |
187 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
188 | if [[ -z "${cmd}" ]]; then | |
189 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
190 | return 0 | |
191 | fi | |
192 | ||
193 | case "${cmd}" in | |
194 | new) | |
195 | COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") ) | |
196 | ;; | |
197 | remove) | |
198 | COMPREPLY=( $(compgen -W "$(network raw list-dhcpd-ranges-of-subnet "${proto}" "${subnet}")" \ | |
199 | -- "${cur}") ) | |
200 | ;; | |
201 | esac | |
202 | } | |
203 | ||
1c6a4e30 | 204 | _network_dns_server() { |
8d1d2745 MT |
205 | local words=( $@ ) |
206 | ||
207 | local commands="add list remove update" | |
208 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
209 | if [[ -z "${cmd}" ]]; then | |
210 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
211 | return 0 | |
212 | fi | |
213 | ||
214 | case "${cmd}" in | |
215 | remove) | |
216 | COMPREPLY=( $(compgen -W "$(network raw list-dns-servers)" \ | |
217 | -- "${cur}") ) | |
218 | ;; | |
219 | esac | |
220 | } | |
221 | ||
1c6a4e30 | 222 | _network_port() { |
bae37360 MT |
223 | local words=( $@ ) |
224 | ||
225 | local commands="new destroy $(network raw list-ports)" | |
226 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
227 | if [[ -z "${cmd}" ]]; then | |
228 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
229 | return 0 | |
230 | fi | |
231 | ||
232 | local args="${words[@]:1}" | |
233 | case "${cmd}" in | |
234 | new) | |
235 | _network_complete_hooks "port" | |
236 | ;; | |
237 | destroy) | |
238 | _network_complete_ports | |
239 | ;; | |
240 | *) | |
241 | local args="${words[@]:1}" | |
242 | _network_port_subcommand ${args} | |
243 | ;; | |
244 | esac | |
245 | } | |
246 | ||
1c6a4e30 | 247 | _network_port_subcommand() { |
bae37360 MT |
248 | local words=( $@ ) |
249 | ||
f5ee091e | 250 | local commands="create down edit identify remove status up" |
bae37360 MT |
251 | local cmd="$(_network_find_on_cmdline "${commands}")" |
252 | if [[ -z "${cmd}" ]]; then | |
253 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
254 | return 0 | |
255 | fi | |
256 | } | |
257 | ||
1c6a4e30 | 258 | _network_route() { |
8d1d2745 MT |
259 | local words=( $@ ) |
260 | ||
261 | local commands="add list remove" | |
262 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
263 | if [[ -z "${cmd}" ]]; then | |
264 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
265 | return 0 | |
266 | fi | |
267 | ||
268 | case "${cmd}" in | |
269 | add) | |
270 | if [[ "${cur}" = -* ]]; then | |
271 | COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \ | |
272 | --prohibit --unreachable" -- "${cur}") ) | |
273 | fi | |
274 | ;; | |
275 | list) | |
276 | # TODO auto-complete options like --protocol here | |
277 | COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") ) | |
278 | ;; | |
279 | esac | |
280 | } | |
281 | ||
1c6a4e30 | 282 | _network_settings() { |
bae37360 MT |
283 | local words=( $@ ) |
284 | ||
285 | local key keys | |
286 | for key in $(network raw list-settings); do | |
287 | keys="${keys} ${key}=" | |
288 | done | |
289 | COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") ) | |
290 | } | |
291 | ||
1c6a4e30 | 292 | _network_zone() { |
bae37360 MT |
293 | local words=( $@ ) |
294 | ||
295 | local commands="new destroy $(network raw list-zones)" | |
296 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
297 | if [[ -z "${cmd}" ]]; then | |
298 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
299 | return 0 | |
300 | fi | |
301 | ||
302 | local args="${words[@]:1}" | |
303 | case "${cmd}" in | |
304 | new) | |
305 | # TODO | |
306 | return 0 | |
307 | ;; | |
308 | destroy) | |
309 | _network_complete_zones | |
310 | ;; | |
311 | *) | |
312 | local zone="${cmd}" | |
313 | local args="${words[@]:1}" | |
314 | _network_zone_subcommand "${zone}" ${args} | |
315 | ;; | |
316 | esac | |
317 | } | |
318 | ||
1c6a4e30 | 319 | _network_zone_subcommand() { |
bae37360 MT |
320 | local zone="${1}" |
321 | shift | |
322 | ||
323 | local words=( $@ ) | |
324 | ||
f5ee091e | 325 | local commands="config disable down edit enable identify port rename status up" |
bae37360 MT |
326 | local cmd="$(_network_find_on_cmdline "${commands}")" |
327 | if [[ -z "${cmd}" ]]; then | |
328 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
329 | return 0 | |
330 | fi | |
331 | ||
332 | local args="${words[@]:1}" | |
333 | case "${cmd}" in | |
334 | config) | |
2a6b2397 | 335 | _network_zone_subcommand_config "${zone}" ${args} |
bae37360 MT |
336 | ;; |
337 | port) | |
338 | _network_zone_subcommand_port "${zone}" ${args} | |
339 | ;; | |
340 | esac | |
341 | } | |
342 | ||
2a6b2397 MT |
343 | _network_zone_subcommand_config() { |
344 | local zone="${1}" | |
345 | shift | |
346 | ||
347 | local words=( $@ ) | |
348 | ||
349 | local commands="new destroy" | |
350 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
351 | if [[ -z "${cmd}" ]]; then | |
352 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
353 | return 0 | |
354 | fi | |
355 | ||
356 | case "${cmd}" in | |
357 | new) | |
358 | _network_complete_hooks "config" | |
359 | ;; | |
360 | esac | |
361 | } | |
362 | ||
1c6a4e30 | 363 | _network_zone_subcommand_port() { |
bae37360 MT |
364 | local zone="${1}" |
365 | shift | |
366 | ||
367 | local words=( $@ ) | |
368 | ||
369 | local commands="attach detach $(network raw list-ports-of-zone "${zone}")" | |
370 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
371 | if [[ -z "${cmd}" ]]; then | |
372 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
373 | return 0 | |
374 | fi | |
375 | ||
376 | case "${cmd}" in | |
377 | attach) | |
378 | COMPREPLY=( $(compgen -W "$(network raw list-free-ports "${zone}")" \ | |
379 | -- "${cur}") ) | |
380 | ;; | |
381 | detach) | |
382 | COMPREPLY=( $(compgen -W "$(network raw list-ports-of-zone "${zone}")" \ | |
383 | -- "${cur}") ) | |
384 | ;; | |
385 | *) | |
386 | local port="${cmd}" | |
387 | local args="${words[@]:1}" | |
388 | _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args} | |
389 | ;; | |
390 | esac | |
391 | } | |
392 | ||
1c6a4e30 | 393 | _network_zone_subcommand_port_subcommand() { |
bae37360 MT |
394 | local zone="${1}" |
395 | local port="${2}" | |
396 | shift 2 | |
397 | ||
398 | local words=( $@ ) | |
399 | ||
400 | local commands="edit" | |
401 | local cmd="$(_network_find_on_cmdline "${commands}")" | |
402 | if [[ -z "${cmd}" ]]; then | |
403 | COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") ) | |
404 | return 0 | |
405 | fi | |
406 | ||
407 | case "${cmd}" in | |
408 | edit) | |
409 | # TODO auto-complete the zone-port hook | |
410 | ;; | |
411 | esac | |
412 | } | |
413 | ||
1c6a4e30 | 414 | _network() { |
bae37360 MT |
415 | local cur prev words cword |
416 | _init_completion || return | |
417 | ||
418 | local cmd i | |
419 | for (( i=1; i < ${#words[@]} - 1; i++ )); do | |
420 | [[ "${words[i]}" = -* ]] && continue | |
421 | cmd="${words[i]}" && break | |
422 | done | |
423 | ||
424 | if [[ -z "${cmd}" ]]; then | |
425 | case "${cur}" in | |
426 | -*) | |
427 | COMPREPLY=( $(compgen -W "--debug" -- "${cur}") ) | |
428 | ;; | |
429 | *) | |
cc02f6be MT |
430 | COMPREPLY=( $(compgen -W "device dhcpv4 dhcpv6 dns-server \ |
431 | help hostname port reset route settings status zone" \ | |
432 | -- "${cur}") ) | |
bae37360 MT |
433 | ;; |
434 | esac | |
435 | ||
436 | return 0 | |
437 | fi | |
438 | ||
439 | local args="${words[@]:i+1}" | |
440 | case "${cmd}" in | |
441 | device) | |
442 | _network_device ${args} | |
443 | ;; | |
cc02f6be MT |
444 | dhcpv[64]) |
445 | _network_dhcpd "${cmd/dhcpv/ipv}" ${args} | |
446 | ;; | |
8d1d2745 MT |
447 | dns-server) |
448 | _network_dns_server ${args} | |
449 | ;; | |
bae37360 MT |
450 | port) |
451 | _network_port ${args} | |
452 | ;; | |
8d1d2745 MT |
453 | route) |
454 | _network_route ${args} | |
455 | ;; | |
bae37360 MT |
456 | settings) |
457 | _network_settings ${args} | |
458 | ;; | |
459 | start|stop|status) | |
460 | # start, stop and status optionally take a zone | |
461 | _network_complete_zones | |
462 | ;; | |
463 | zone) | |
464 | _network_zone ${args} | |
465 | ;; | |
466 | esac | |
467 | } && complete -F _network network |