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 _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 _network_complete_hooks
() {
40 COMPREPLY
=( $
(compgen
-W "$(network raw list-hooks "${type}")" -- "${cur}") )
43 _network_complete_ports
() {
44 COMPREPLY
=( $
(compgen
-W "$(network raw list-ports)" -- "${cur}") )
47 _network_complete_zones
() {
48 COMPREPLY
=( $
(compgen
-W "$(network raw list-zones)" -- "${cur}") )
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 _network_device_subcommand
() {
75 local commands
="discover identify monitor status unlock ussd"
76 local cmd
="$(_network_find_on_cmdline "${commands}")"
77 if [[ -z "${cmd}" ]]; then
78 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
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 _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 _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 _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 _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 _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
)" \
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 _network_port_subcommand() {
250 local commands="create down edit identify remove status up
"
251 local cmd="$
(_network_find_on_cmdline
"${commands}")"
252 if [[ -z "${cmd}" ]]; then
253 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
261 local commands="static
"
262 local cmd="$
(_network_find_on_cmdline
"${commands}")"
263 if [[ -z "${cmd}" ]]; then
264 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
270 local args="${words[@]}"
271 _network_route_static ${args}
277 _network_route_static() {
280 local commands="add list remove reload
"
281 local cmd="$
(_network_find_on_cmdline
"${commands}")"
282 if [[ -z "${cmd}" ]]; then
283 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
289 if [[ "${cur}" = -* ]]; then
290 COMPREPLY=( $(compgen -W "--blackhole --gateway= --mtu= \
291 --prohibit --unreachable" -- "${cur}") )
295 # TODO auto-complete options like --protocol here
296 COMPREPLY=( $(compgen -W "--protocol=" -- "${cur}") )
301 _network_settings() {
305 for key in $(network raw list-settings); do
306 keys="${keys} ${key}="
308 COMPREPLY=( $(compgen -W "${keys}" -- "${cur}") )
314 local commands="new destroy $
(network raw list-zones
)"
315 local cmd="$
(_network_find_on_cmdline
"${commands}")"
316 if [[ -z "${cmd}" ]]; then
317 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
322 local args="${words[@]:1}"
325 _network_zone_new ${args}
328 _network_complete_zones
332 local args="${words[@]:1}"
333 _network_zone_subcommand "${zone}" ${args}
338 _network_zone_new() {
340 local cmd=${words[@]:0:1}
342 # Suggest useful zone names
343 if [[ -z "${cmd}" ]]; then
344 local commands="$
(network raw list-next-free-zones
)"
345 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
347 # If a valid zone name was entered, we can move on
348 elif network raw zone-name-is-valid ${cmd}; then
349 local args="${words[@]:1}"
350 _network_complete_hooks zone ${args}
356 _network_zone_subcommand() {
362 local commands="config disable down edit
enable identify port rename status up
"
363 local cmd="$
(_network_find_on_cmdline
"${commands}")"
364 if [[ -z "${cmd}" ]]; then
365 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
369 local args="${words[@]:1}"
372 _network_zone_subcommand_config "${zone}" ${args}
375 _network_zone_subcommand_port "${zone}" ${args}
380 _network_zone_subcommand_config() {
386 local commands="new destroy
"
387 local cmd="$
(_network_find_on_cmdline
"${commands}")"
388 if [[ -z "${cmd}" ]]; then
389 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
395 _network_complete_hooks "config
"
400 _network_zone_subcommand_port() {
406 local commands="attach detach $
(network raw list-ports-of-zone
"${zone}")"
407 local cmd="$
(_network_find_on_cmdline
"${commands}")"
408 if [[ -z "${cmd}" ]]; then
409 COMPREPLY=( $(compgen -W "${commands}" -- "${cur}") )
415 COMPREPLY=( $(compgen -W "$
(network raw list-free-ports
"${zone}")" \
419 COMPREPLY=( $(compgen -W "$
(network raw list-ports-of-zone
"${zone}")" \
424 local args="${words[@]:1}"
425 _network_zone_subcommand_port_subcommand "${zone}" "${port}" ${args}
430 _network_zone_subcommand_port_subcommand
() {
437 local commands
="edit"
438 local cmd
="$(_network_find_on_cmdline "${commands}")"
439 if [[ -z "${cmd}" ]]; then
440 COMPREPLY
=( $
(compgen
-W "${commands}" -- "${cur}") )
446 # TODO auto-complete the zone-port hook
452 local cur prev words cword
453 _init_completion ||
return
456 for (( i
=1; i
< ${#words[@]} - 1; i
++ )); do
457 [[ "${words[i]}" = -* ]] && continue
458 cmd
="${words[i]}" && break
461 if [[ -z "${cmd}" ]]; then
464 COMPREPLY
=( $
(compgen
-W "--debug" -- "${cur}") )
467 COMPREPLY
=( $
(compgen
-W "device dhcpv4 dhcpv6 dns-server \
468 help hostname port reset route settings status zone" \
476 local args
="${words[@]:i+1}"
479 _network_device
${args}
482 _network_dhcpd
"${cmd/dhcpv/ipv}" ${args}
485 _network_dns_server
${args}
488 _network_port
${args}
491 _network_route
${args}
494 _network_settings
${args}
497 # start, stop and status optionally take a zone
498 _network_complete_zones
501 _network_zone
${args}
504 } && complete
-F _network network