Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / scripts / parse-maintainers.pl
1 #!/usr/bin/perl -w
2 # SPDX-License-Identifier: GPL-2.0
3
4 use strict;
5
6 my $P = $0;
7
8 # sort comparison functions
9 sub by_category($$) {
10     my ($a, $b) = @_;
11
12     $a = uc $a;
13     $b = uc $b;
14
15     # This always sorts last
16     $a =~ s/THE REST/ZZZZZZ/g;
17     $b =~ s/THE REST/ZZZZZZ/g;
18
19     return $a cmp $b;
20 }
21
22 sub by_pattern($$) {
23     my ($a, $b) = @_;
24     my $preferred_order = 'MRPLSWTQBCFXNK';
25
26     my $a1 = uc(substr($a, 0, 1));
27     my $b1 = uc(substr($b, 0, 1));
28
29     my $a_index = index($preferred_order, $a1);
30     my $b_index = index($preferred_order, $b1);
31
32     $a_index = 1000 if ($a_index == -1);
33     $b_index = 1000 if ($b_index == -1);
34
35     if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
36         ($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
37         return $a cmp $b;
38     }
39
40     if ($a_index < $b_index) {
41         return -1;
42     } elsif ($a_index == $b_index) {
43         return 0;
44     } else {
45         return 1;
46     }
47 }
48
49 sub trim {
50     my $s = shift;
51     $s =~ s/\s+$//;
52     $s =~ s/^\s+//;
53     return $s;
54 }
55
56 sub alpha_output {
57     my ($hashref, $filename) = (@_);
58
59     open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!\n";
60     foreach my $key (sort by_category keys %$hashref) {
61         if ($key eq " ") {
62             chomp $$hashref{$key};
63             print $file $$hashref{$key};
64         } else {
65             print $file "\n" . $key . "\n";
66             foreach my $pattern (sort by_pattern split('\n', %$hashref{$key})) {
67                 print $file ($pattern . "\n");
68             }
69         }
70     }
71     close($file);
72 }
73
74 sub file_input {
75     my ($hashref, $filename) = (@_);
76
77     my $lastline = "";
78     my $case = " ";
79     $$hashref{$case} = "";
80
81     open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n";
82
83     while (<$file>) {
84         my $line = $_;
85
86         # Pattern line?
87         if ($line =~ m/^([A-Z]):\s*(.*)/) {
88             $line = $1 . ":\t" . trim($2) . "\n";
89             if ($lastline eq "") {
90                 $$hashref{$case} = $$hashref{$case} . $line;
91                 next;
92             }
93             $case = trim($lastline);
94             exists $$hashref{$case} and die "Header '$case' already exists";
95             $$hashref{$case} = $line;
96             $lastline = "";
97             next;
98         }
99
100         if ($case eq " ") {
101             $$hashref{$case} = $$hashref{$case} . $lastline;
102             $lastline = $line;
103             next;
104         }
105         trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
106         $lastline = $line;
107     }
108     $$hashref{$case} = $$hashref{$case} . $lastline;
109     close($file);
110 }
111
112 my %hash;
113 my %new_hash;
114
115 file_input(\%hash, "MAINTAINERS");
116
117 foreach my $type (@ARGV) {
118     foreach my $key (keys %hash) {
119         if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
120             $new_hash{$key} = $hash{$key};
121             delete $hash{$key};
122         }
123     }
124 }
125
126 alpha_output(\%hash, "MAINTAINERS.new");
127 alpha_output(\%new_hash, "SECTION.new");
128
129 exit(0);