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_dns_server
() {
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}") )
101 COMPREPLY
=( $
(compgen
-W "$(network raw list-dns-servers)" \
107 function _network_port
() {
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}") )
117 local args
="${words[@]:1}"
120 _network_complete_hooks
"port"
123 _network_complete_ports
126 local args
="${words[@]:1}"
127 _network_port_subcommand
${args}
132 function _network_port_subcommand
() {
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}") )
143 function _network_route
() {
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}") )
155 if [[ "${cur}" = -* ]]; then
156 COMPREPLY
=( $
(compgen
-W "--blackhole --gateway= --mtu= \
157 --prohibit --unreachable" -- "${cur}") )
161 # TODO auto-complete options like --protocol here
162 COMPREPLY
=( $
(compgen
-W "--protocol=" -- "${cur}") )
167 function _network_settings
() {
171 for key
in $
(network raw list-settings
); do
172 keys
="${keys} ${key}="
174 COMPREPLY
=( $
(compgen
-W "${keys}" -- "${cur}") )
177 function _network_zone
() {
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}") )
187 local args
="${words[@]:1}"
194 _network_complete_zones
198 local args
="${words[@]:1}"
199 _network_zone_subcommand
"${zone}" ${args}
204 function _network_zone_subcommand
() {
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}") )
217 local args
="${words[@]:1}"
223 _network_zone_subcommand_port
"${zone}" ${args}
228 function _network_zone_subcommand_port
() {
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}") )
243 COMPREPLY
=( $
(compgen
-W "$(network raw list-free-ports "${zone}")" \
247 COMPREPLY
=( $
(compgen
-W "$(network raw list-ports-of-zone "${zone}")" \
252 local args
="${words[@]:1}"
253 _network_zone_subcommand_port_subcommand
"${zone}" "${port}" ${args}
258 function _network_zone_subcommand_port_subcommand() {
265 local commands="edit
"
266 local cmd="$
(_network_find_on_cmdline
"${commands}")"
267 if [[ -z "${cmd}" ]]; then
268 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
274 # TODO auto-complete the zone-port hook
279 function _network() {
280 local cur prev words cword
281 _init_completion || return
284 for (( i=1; i < ${#words[@]} - 1; i++ )); do
285 [[ "${words[i]}" = -* ]] && continue
286 cmd="${words[i]}" && break
289 if [[ -z "${cmd}" ]]; then
292 COMPREPLY=( $(compgen -W "--debug" -- "${cur}") )
295 COMPREPLY=( $(compgen -W "device dns-server
help hostname \
296 port
reset route settings status zone
" -- "${cur}") )
303 local args="${words[@]:i+1}"
306 _network_device ${args}
309 _network_dns_server ${args}
312 _network_port ${args}
315 _network_route ${args}
318 _network_settings ${args}
321 # start, stop and status optionally take a zone
322 _network_complete_zones
325 _network_zone ${args}
328 } && complete -F _network network