parse-maintainers.pl 2.75 KB
Newer Older
1
#!/usr/bin/perl -w
2
# SPDX-License-Identifier: GPL-2.0
3 4 5

use strict;

6
my $P = $0;
7

8
# sort comparison functions
9 10 11 12 13 14 15 16 17 18
sub by_category($$) {
    my ($a, $b) = @_;

    $a = uc $a;
    $b = uc $b;

    # This always sorts last
    $a =~ s/THE REST/ZZZZZZ/g;
    $b =~ s/THE REST/ZZZZZZ/g;

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
    return $a cmp $b;
}

sub by_pattern($$) {
    my ($a, $b) = @_;
    my $preferred_order = 'MRPLSWTQBCFXNK';

    my $a1 = uc(substr($a, 0, 1));
    my $b1 = uc(substr($b, 0, 1));

    my $a_index = index($preferred_order, $a1);
    my $b_index = index($preferred_order, $b1);

    $a_index = 1000 if ($a_index == -1);
    $b_index = 1000 if ($b_index == -1);

    if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
	($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
	return $a cmp $b;
    }

    if ($a_index < $b_index) {
	return -1;
    } elsif ($a_index == $b_index) {
	return 0;
    } else {
	return 1;
    }
47 48
}

49 50 51 52 53 54 55
sub trim {
    my $s = shift;
    $s =~ s/\s+$//;
    $s =~ s/^\s+//;
    return $s;
}

56
sub alpha_output {
57 58 59 60
    my ($hashref, $filename) = (@_);

    open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!\n";
    foreach my $key (sort by_category keys %$hashref) {
61
	if ($key eq " ") {
62 63
	    chomp $$hashref{$key};
	    print $file $$hashref{$key};
64
	} else {
65 66 67
	    print $file "\n" . $key . "\n";
	    foreach my $pattern (sort by_pattern split('\n', %$hashref{$key})) {
		print $file ($pattern . "\n");
68 69
	    }
	}
70
    }
71
    close($file);
72 73 74
}

sub file_input {
75 76
    my ($hashref, $filename) = (@_);

77 78
    my $lastline = "";
    my $case = " ";
79 80 81
    $$hashref{$case} = "";

    open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n";
82

83
    while (<$file>) {
84 85 86 87 88 89
        my $line = $_;

        # Pattern line?
        if ($line =~ m/^([A-Z]):\s*(.*)/) {
            $line = $1 . ":\t" . trim($2) . "\n";
            if ($lastline eq "") {
90
                $$hashref{$case} = $$hashref{$case} . $line;
91 92 93
                next;
            }
            $case = trim($lastline);
94 95
            exists $$hashref{$case} and die "Header '$case' already exists";
            $$hashref{$case} = $line;
96 97 98 99 100
            $lastline = "";
            next;
        }

        if ($case eq " ") {
101
            $$hashref{$case} = $$hashref{$case} . $lastline;
102 103 104 105 106 107
            $lastline = $line;
            next;
        }
        trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
        $lastline = $line;
    }
108 109
    $$hashref{$case} = $$hashref{$case} . $lastline;
    close($file);
110 111
}

112
my %hash;
113
my %new_hash;
114 115

file_input(\%hash, "MAINTAINERS");
116 117 118 119 120 121 122 123 124 125

foreach my $type (@ARGV) {
    foreach my $key (keys %hash) {
	if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
	    $new_hash{$key} = $hash{$key};
	    delete $hash{$key};
	}
    }
}

126
alpha_output(\%hash, "MAINTAINERS.new");
127
alpha_output(\%new_hash, "SECTION.new");
128

129
exit(0);