2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2014 Michael Tremer #
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. #
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. #
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/>. #
20 ###############################################################################
21 # network(8) completion
23 function _network_find_on_cmdline
() {
24 local word subcommand c
=0
25 while [ ${c} -lt ${cword} ]; do
27 for subcommand
in ${1}; do
28 if [ "${subcommand}" = "${word}" ]; then
37 function _network_complete_hooks
() {
40 COMPREPLY
=( $
(compgen
-W "$(network raw list-hooks "${type}")" -- "${cur}") )
43 function _network_complete_ports
() {
44 COMPREPLY
=( $
(compgen
-W "$(network raw list-ports)" -- "${cur}") )
47 function _network_complete_zones
() {
48 COMPREPLY
=( $
(compgen
-W "$(network raw list-zones)" -- "${cur}") )
51 function _network_device
() {
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}") )
61 local args
="${words[@]:1}"
67 _network_device_subcommand
${args}
72 function _network_device_subcommand
() {
75 local commands
="discover monitor status unlock ussd"
76 local cmd
="$(_network_find_on_cmdline "${commands}")"
77 if [[ -z "${cmd}" ]]; then
78 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
89 function _network_dhcpd
() {
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}") )
102 local args
="${words[@]:1}"
105 _network_dhcpd_subnet
"${proto}" ${args}
110 function _network_dhcpd_subnet
() {
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}") )
123 local subnet
="${words[0]}"
124 local args
="${words[@]:1}"
133 _network_dhcpd_subnet_subcommand
"${proto}" "${subnet}" ${args}
138 function _network_dhcpd_subnet_subcommand() {
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}") )
152 local args="${words[@]:1}"
158 _network_dhcpd_subnet_subcommand_options "${proto}" "${subnet}" ${args}
161 _network_dhcpd_subnet_subcommand_range
"${proto}" "${subnet}" ${args}
166 function _network_dhcpd_subnet_subcommand_options() {
172 for option in $(network raw list-dhcpd-subnet-options "${proto}"); do
173 options="${options} ${option}="
176 COMPREPLY=( $(compgen -W "${options}" -- "${cur}") )
179 function _network_dhcpd_subnet_subcommand_range() {
186 local commands="new remove
"
187 local cmd="$
(_network_find_on_cmdline
"${commands}")"
188 if [[ -z "${cmd}" ]]; then
189 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
195 COMPREPLY=( $(compgen -W "--end= --start=" -- "${cur}") )
198 COMPREPLY=( $(compgen -W "$
(network raw list-dhcpd-ranges-of-subnet
"${proto}" "${subnet}")" \
204 function _network_dns_server() {
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}") )
216 COMPREPLY=( $(compgen -W "$
(network raw list-dns-servers
)" \
222 function _network_port() {
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}") )
232 local args="${words[@]:1}"
235 _network_complete_hooks "port
"
238 _network_complete_ports
241 local args="${words[@]:1}"
242 _network_port_subcommand ${args}
247 function _network_port_subcommand() {
250 local commands="create down edit remove status up
"
251 local cmd="$
(_network_find_on_cmdline
"${commands}")"
252 if [[ -z "${cmd}" ]]; then
253 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
258 function _network_route() {
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}") )
270 if [[ "${cur}" = -* ]]; then
271 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
272 --prohibit --unreachable" -- "${cur}") )
276 # TODO auto-complete options like --protocol here
277 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
282 function _network_settings() {
286 for key in $(network raw list-settings); do
287 keys="${keys} ${key}="
289 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
292 function _network_zone() {
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}") )
302 local args="${words[@]:1}"
309 _network_complete_zones
313 local args="${words[@]:1}"
314 _network_zone_subcommand "${zone}" ${args}
319 function _network_zone_subcommand() {
325 local commands="config disable down edit
enable port status up
"
326 local cmd="$
(_network_find_on_cmdline
"${commands}")"
327 if [[ -z "${cmd}" ]]; then
328 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
332 local args="${words[@]:1}"
338 _network_zone_subcommand_port "${zone}" ${args}
343 function _network_zone_subcommand_port() {
349 local commands="attach detach $
(network raw list-ports-of-zone
"${zone}")"
350 local cmd="$
(_network_find_on_cmdline
"${commands}")"
351 if [[ -z "${cmd}" ]]; then
352 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
358 COMPREPLY=( $(compgen -W "$
(network raw list-free-ports
"${zone}")" \
362 COMPREPLY=( $(compgen -W "$
(network raw list-ports-of-zone
"${zone}")" \
367 local args="${words[@]:1}"
368 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
373 function _network_zone_subcommand_port_subcommand
() {
380 local commands
="edit"
381 local cmd
="$(_network_find_on_cmdline "${commands}")"
382 if [[ -z "${cmd}" ]]; then
383 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
389 # TODO auto-complete the zone-port hook
394 function _network
() {
395 local cur prev words cword
396 _init_completion ||
return
399 for (( i
=1; i
< ${#words[@]} - 1; i
++ )); do
400 [[ "${words[i]}" = -* ]] && continue
401 cmd
="${words[i]}" && break
404 if [[ -z "${cmd}" ]]; then
407 COMPREPLY
=( $
(compgen
-W "--debug" -- "${cur}") )
410 COMPREPLY
=( $
(compgen
-W "device dhcpv4 dhcpv6 dns-server \
411 help hostname port reset route settings status zone" \
419 local args
="${words[@]:i+1}"
422 _network_device
${args}
425 _network_dhcpd
"${cmd/dhcpv/ipv}" ${args}
428 _network_dns_server
${args}
431 _network_port
${args}
434 _network_route
${args}
437 _network_settings
${args}
440 # start, stop and status optionally take a zone
441 _network_complete_zones
444 _network_zone
${args}
447 } && complete
-F _network network