Fix nftables completion

- remove needless debug print
- fix wrong value overwriting
This commit is contained in:
Shohei YOSHIDA 2026-03-08 21:56:09 +09:00
parent f6d745a6ef
commit d736522d58
No known key found for this signature in database
GPG Key ID: C9A1BB11BB940CF2
1 changed files with 16 additions and 9 deletions

View File

@ -31,8 +31,8 @@ options=(
{-I,--includepath}'[add specified directory to the paths searched for include files]:include directory [/usr/share]:include directory:_directories' {-I,--includepath}'[add specified directory to the paths searched for include files]:include directory [/usr/share]:include directory:_directories'
) )
# start a state machine. The state is modified by _arguments if the # start a state machine. The state is modified by _arguments if the
# current argument (descriptors) cannot be completed. Each state has to define is successive state and the # current argument (descriptors) cannot be completed. Each state has to define is successive state and the
# 'descriptors' for _arguments, which essentially tells _arguments how to complete # 'descriptors' for _arguments, which essentially tells _arguments how to complete
local _i=0 local _i=0
while true;do while true;do
@ -42,10 +42,10 @@ while true;do
descriptors=() descriptors=()
nextstate="end" nextstate="end"
case $state in case $state in
(start) (start)
##if line is empty (at the start) or ends with semicolon, autocomplete subcommands, ##if line is empty (at the start) or ends with semicolon, autocomplete subcommands,
# else if we are after a space,complete a semicolon (end the current nft command) and start anew # else if we are after a space,complete a semicolon (end the current nft command) and start anew
if [[ $line[1] = "" || $line[1] =~ ';$' ]] ; then if [[ $line[1] = "" || $line[1] =~ ';$' ]] ; then
descriptors=( ":: :_nft_subcommands" ) descriptors=( ":: :_nft_subcommands" )
nextstate="category" nextstate="category"
@ -98,7 +98,7 @@ while true;do
esac esac
;; ;;
#all completions for create and insert match with the completions of add #all completions for create and insert match with the completions of add
(create | insert) (create | insert)
state="add" state="add"
;| ;|
#all completions for reset and flush match with the completions of list #all completions for reset and flush match with the completions of list
@ -234,7 +234,7 @@ while true;do
#TODO #TODO
# _nft_rule $cmd_fam $cmd_tab $cmd_chain\ # _nft_rule $cmd_fam $cmd_tab $cmd_chain\
# && return 0; # && return 0;
descriptors=":expression: " descriptors=(":expression: ")
nextstate="start" nextstate="start"
;; ;;
(list-set | list-map | delete-map | list-chain | list-flowtable | delete-flowtable | list-ct\\ helper | list-counter | list-quota | list-meter) (list-set | list-map | delete-map | list-chain | list-flowtable | delete-flowtable | list-ct\\ helper | list-counter | list-quota | list-meter)
@ -440,7 +440,7 @@ _nft_table(){
1="${1/-handle/}" 1="${1/-handle/}"
_describe -t tables "table" tables -V "handle" _describe -t tables "table" tables -V "handle"
fi fi
case $1 in case $1 in
(arp | bridge | inet | ip | ip6 | netdev) (arp | bridge | inet | ip | ip6 | netdev)
tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \ tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\2:type \1, handle \3/' )"} ) | grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\2:type \1, handle \3/' )"} )
@ -454,7 +454,6 @@ _nft_table_handle(){
#$1:protocol family #$1:protocol family
local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \ local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} ) | grep '^table '"$1" | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} )
echo $1 > /tmp/znfttab
_describe -t tables "table handle" tables _describe -t tables "table handle" tables
} }
@ -462,7 +461,7 @@ _nft_table_handle_all(){
# complete the handles of tables of all families (with the table name in the description) # complete the handles of tables of all families (with the table name in the description)
local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \ local tables=( ${(f)"$(_call_program -p tables nft -a list ruleset 2>/dev/null \
| grep '^table' | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} ) | grep '^table' | sed 's/table // ;s/{ # handle // ;s/\(\S*\) \(\S*\) \(\S*\)/\3:\2(type \1)/' )"} )
_describe -t tables "table handle" tables _describe -t tables "table handle" tables
} }
_nft_object(){ _nft_object(){
@ -502,3 +501,11 @@ _nft_rule_handle(){
#currently, only the `nft` command is covered by this script. #currently, only the `nft` command is covered by this script.
_nft "$@" _nft "$@"
# Local Variables:
# mode: Shell-Script
# sh-indentation: 2
# indent-tabs-mode: nil
# sh-basic-offset: 2
# End:
# vim: ft=zsh sw=2 ts=2 et