fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Fabian BoehmReformat share/**.fish with newlines collapsed78c948282

master
6.4 KiB146 linesraw
1set -l __fish_iptables_tables filter nat mangle raw security
2
3function __fish_iptables_current_table
4    set -l next_is_table 1
5    for token in (commandline -xc)
6        switch $token
7            case "--table=*"
8                echo (string split -m1 = -- $token)[2]
9                return 0
10            case --table
11                set next_is_table 0
12            case "-*t*"
13                set next_is_table 0
14            case "*"
15                if test $next_is_table -eq 0
16                    echo $token
17                    return 0
18                end
19        end
20    end
21    return 1
22end
23
24function __fish_iptables_user_chains
25    # There can be user-defined chains so we need iptables' help
26    set -l tablearg
27    set -l table (__fish_iptables_current_table)
28    if __fish_iptables_current_table
29        set tablearg "--table=$table"
30    end
31    # This only works as root, so ignore errors
32    iptables $tablearg -L 2>/dev/null | string match '*Chain*' | while read -l a b c
33        echo $b
34    end
35end
36
37function __fish_iptables_chains
38    set -l table (__fish_iptables_current_table)
39    test -z $table
40    and set -l table "*"
41    set -l prerouting "PREROUTING	For packets that are coming in"
42    set -l input "INPUT	For packets destined to local sockets"
43    set -l output "OUTPUT	For locally-generated packets"
44    set -l forward "FORWARD	For packets being routed through"
45    set -l postrouting "POSTROUTING	For packets that are about to go out"
46    switch $table
47        case filter
48            echo $input
49            echo $forward
50            echo $output
51        case nat
52            echo $prerouting
53            echo $output
54            echo $postrouting
55        case mangle
56            echo $prerouting
57            echo $input
58            echo $output
59            echo $forward
60            echo $postrouting
61        case raw
62            echo $prerouting
63            echo $output
64        case security
65            echo $input
66            echo $output
67            echo $forward
68        case '*'
69            echo $prerouting
70            echo $input
71            echo $output
72            echo $forward
73            echo $postrouting
74    end
75    __fish_iptables_user_chains
76end
77
78function __fish_iptables_has_chain
79    # Remove descriptions
80    set -l chains (__fish_iptables_chains | string split -m1 "    " | while read -l a b; echo $a; end)
81    set -l cmdline (commandline -xp)
82    for c in $chains
83        if contains -- $c $cmdline
84            return 0
85        end
86    end
87    return 1
88end
89
90# A target is a user-defined chain, one of "ACCEPT DROP RETURN" or an extension (TODO)
91function __fish_iptables_targets
92    echo ACCEPT
93    echo DROP
94    echo RETURN
95    __fish_iptables_chains
96end
97
98### Commands
99complete -c iptables -s A -l append -d 'Append rules to the end of a chain' -a '(__fish_iptables_chains)' -f
100complete -c iptables -s C -l check -d 'Check whether a matching rule exists in a chain' -a '(__fish_iptables_chains)' -f
101complete -c iptables -s D -l delete -d 'Delete rules from a chain' -a '(__fish_iptables_chains)' -f
102# Rulespec is match (can't complete that) and then a target
103# TODO: This is only valid for some options, as others need a rulenum first or no rulespec at all
104complete -c iptables -n '__fish_contains_opt -s A -s C -s D append check delete; and __fish_iptables_has_chain' -s j -l jump -d 'Specify the target of a rule' -f -a\
105'(__fish_iptables_targets)'
106
107complete -c iptables -n '__fish_contains_opt -s A -s C -s D append check delete; and __fish_iptables_has_chain' -s m -l match -d 'Specify a match to use' -f
108complete -c iptables -n __fish_iptables_has_chain -a 'ACCEPT DROP RETURN' -f
109complete -c iptables -n __fish_iptables_has_chain -a '( __fish_iptables_user_chains)' -f
110complete -c iptables -s I -l insert -d 'Insert rules in the beginning of a chain' -a '(__fish_iptables_chains)' -f
111complete -c iptables -s R -l replace -d 'Replace a rule in a chain' -a '(__fish_iptables_chains)' -f
112complete -c iptables -s L -l list -d 'List all rules in a chain' -a '(__fish_iptables_chains)' -f
113complete -c iptables -s S -l list-rules -d 'Print all rules in a chain.' -a '(__fish_iptables_chains)' -f
114complete -c iptables -s F -l flush -d 'Delete ALL rules in a chain' -a '(__fish_iptables_chains)' -f
115complete -c iptables -s Z -l zero -d 'Zero the packet and byte counters in chains' -a '(__fish_iptables_chains)' -f
116complete -c iptables -s N -l new-chain -d 'Create a new user-defined chain by the given name' -a '(__fish_iptables_chains)' -f
117complete -c iptables -s X -l delete-chain -d 'Delete the optional user-defined chain specified' -a '(__fish_iptables_chains)' -f
118complete -c iptables -s P -l policy -d 'Set the policy for the chain to the given target' -a '(__fish_iptables_chains)' -f
119complete -c iptables -s E -l rename-chain -d 'Rename the user specified chain to the user supplied name' -a '(__fish_iptables_chains)' -f
120complete -c iptables -s h -d Help -f
121
122complete -c iptables -s p -l protocol -d 'The protocol of the rule or of the packet to check' -f
123complete -c iptables -s s -l source -d 'Source specification' -f
124complete -c iptables -s d -l destination -d 'Destination specification' -f
125complete -c iptables -s j -l jump -d 'Specify the target of a rule' -f
126complete -c iptables -s i -l in-interface -d 'Interface via which a packet was received' -f
127complete -c iptables -s o -l out-interface -d 'Interface via which packet is to be sent' -f
128complete -c iptables -s f -l fragment -d 'Rule only refers to second and further ipv4 fragments' -f
129complete -c iptables -s c -l set-counters -d 'Initialize packet and byte counters of a rule' -f
130complete -c iptables -s v -l verbose -d 'Verbose output' -f
131complete -c iptables -s w -l wait -d 'Wait for the xtables lock' -f
132complete -c iptables -s n -l numeric -d 'Numeric output' -f
133complete -c iptables -s x -l exact -d 'Expand numbers' -f
134complete -c iptables -l line-numbers -d 'When listing rules, add line numbers' -f
135complete -c iptables -s t -l table -d 'The table to operate on' -a "$__fish_iptables_tables" -f
136
137# Options that take files
138complete -c iptables -l modprobe -d 'Use this command to load modules' -r
139
140# I don't get these
141# complete -c iptables -s 4 -l ipv4 -d 'This option has no effect in iptables and iptables-restore.' -f
142# complete -c iptables -s 6 -l ipv6 -d 'If a rule using the -6 option is inserted with … [See Man Page]' -f
143# complete -c iptables -s g -l goto -d '' -f
144
145# Should aliased options be in the completion?
146# complete -c iptables -l dst -d 'Alias for -d' -f