File: sort.pl

package info (click to toggle)
ferm 2.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,140 kB
  • sloc: perl: 2,839; sh: 126; makefile: 120
file content (65 lines) | stat: -rwxr-xr-x 1,719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/perl -w

#
# ferm, a firewall setup program that makes firewall rules easy!
#
# Copyright 2001-2021 Max Kellermann
#
# Bug reports and patches for this program may be sent to the GitHub
# repository: L<https://github.com/MaxKellermann/ferm>
#

# This script sorts the tables and chains in ferm output so the
# unit test suite can use "diff" to verify it.  It's a kludge that is
# necessary because ferm outputs these in random order (because Perl
# does).

use strict;

my %rules;
my $table;

sub flush_output() {
    foreach my $key (sort keys %rules) {
        foreach my $line (@{$rules{$key}}) {
            print $line;
        }
    }
    undef %rules;
    undef $table;
}

while (<>) {
    if (/^(# Generated .+) on .+/) {
        flush_output;
        print $1, "\n";
    } elsif (/^#/) {
        next;
    } elsif (/^(\w+)tables -t (\w+) -([NAP]) (\S+)/ or
          /^(\w+)tables(?: --atomic-file \w+)? -t (\w+) -([FX])()$/) {
        my $key = $3 eq 'P' ? "$1 $2  $4" : "$1 $2 $4";
        $key .= ' z' if $4 eq '';
        my $array = $rules{$key} ||= [];
        push @$array, $_;
    } elsif (/^\*(\S+)/) {
        $table = $1;
        my $key = $table;
        my $array = $rules{$key} ||= [];
        push @$array, $_;
    } elsif (/^COMMIT/) {
        my $key = $table . 'z';
        my $array = $rules{$key} ||= [];
        push @$array, $_;
    } elsif (/^(:)(\S+)/ or /^-(A) (\S+)/) {
        my $key = $table . $1 . $2;
        my $array = $rules{$key} ||= [];
        push @$array, $_;
    } elsif (/^ebtables -t (\w+) --atomic-file (\S+) (\N+)/) {
        my $key = $2;
        my $array = $rules{$key} ||= [];
        push @$array, $_;
    } else {
        die;
    }
}
flush_output;