fix previous NEWS commit, oops
[ferm.git] / test / sort.pl
blob797f918487fe472d1bdba8969028f36f676a2a99
1 #!/usr/bin/perl -w
4 # ferm, a firewall setup program that makes firewall rules easy!
6 # Copyright 2001-2017 Max Kellermann
8 # Bug reports and patches for this program may be sent to the GitHub
9 # repository: L<https://github.com/MaxKellermann/ferm>
12 # This script is sorts the tables and chains in the ferm output so the
13 # unit test suite can use "diff" to verify it. It's a kludge that is
14 # necessary because ferm outputs these in random order (because Perl
15 # does).
17 use strict;
19 my %rules;
21 my $table;
23 while (<>) {
24 next if /^#/;
26 if (/^(\w+)tables -t (\w+) -([NAP]) (\S+)/ or
27 /^(\w+)tables -t (\w+) -([FX])()$/) {
28 my $key = $3 eq 'P' ? "$1 $2 $4" : "$1 $2 $4";
29 $key .= ' z' if $4 eq '';
30 my $array = $rules{$key} ||= [];
31 push @$array, $_;
32 } elsif (/^\*(\S+)/) {
33 $table = $1;
34 my $key = $table;
35 my $array = $rules{$key} ||= [];
36 push @$array, $_;
37 } elsif (/^COMMIT/) {
38 my $key = $table . 'z';
39 my $array = $rules{$key} ||= [];
40 push @$array, $_;
41 } elsif (/^(:)(\S+)/ or /^-(A) (\S+)/) {
42 my $key = $table . $1 . $2;
43 my $array = $rules{$key} ||= [];
44 push @$array, $_;
45 } else {
46 die;
50 foreach my $key (sort keys %rules) {
51 foreach my $line (@{$rules{$key}}) {
52 print $line;