blob: 255cef1b098d8ffdf82caf5022a965175f9f2685 [file] [log] [blame]
Linus Torvalds7683e9e2017-07-23 16:06:21 -07001#!/usr/bin/perl -w
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01002# SPDX-License-Identifier: GPL-2.0
Linus Torvalds7683e9e2017-07-23 16:06:21 -07003
4use strict;
Joe Perches1e6270d2017-11-17 15:27:10 -08005use Getopt::Long qw(:config no_auto_abbrev);
6
7my $input_file = "MAINTAINERS";
8my $output_file = "MAINTAINERS.new";
9my $output_section = "SECTION.new";
10my $help = 0;
Linus Torvalds7683e9e2017-07-23 16:06:21 -070011
Joe Perchesfe909032017-08-05 18:45:48 -070012my $P = $0;
Linus Torvalds7683e9e2017-07-23 16:06:21 -070013
Joe Perches1e6270d2017-11-17 15:27:10 -080014if (!GetOptions(
15 'input=s' => \$input_file,
16 'output=s' => \$output_file,
17 'section=s' => \$output_section,
18 'h|help|usage' => \$help,
19 )) {
20 die "$P: invalid argument - use --help if necessary\n";
21}
22
23if ($help != 0) {
24 usage();
25 exit 0;
26}
27
28sub usage {
29 print <<EOT;
30usage: $P [options] <pattern matching regexes>
31
32 --input => MAINTAINERS file to read (default: MAINTAINERS)
33 --output => sorted MAINTAINERS file to write (default: MAINTAINERS.new)
34 --section => new sorted MAINTAINERS file to write to (default: SECTION.new)
35
36If <pattern match regexes> exist, then the sections that match the
37regexes are not written to the output file but are written to the
38section file.
39
40EOT
41}
42
Joe Perches61f74162017-08-05 18:45:47 -070043# sort comparison functions
Linus Torvalds7683e9e2017-07-23 16:06:21 -070044sub by_category($$) {
45 my ($a, $b) = @_;
46
47 $a = uc $a;
48 $b = uc $b;
49
50 # This always sorts last
51 $a =~ s/THE REST/ZZZZZZ/g;
52 $b =~ s/THE REST/ZZZZZZ/g;
53
Joe Perches61f74162017-08-05 18:45:47 -070054 return $a cmp $b;
55}
56
57sub by_pattern($$) {
58 my ($a, $b) = @_;
59 my $preferred_order = 'MRPLSWTQBCFXNK';
60
61 my $a1 = uc(substr($a, 0, 1));
62 my $b1 = uc(substr($b, 0, 1));
63
64 my $a_index = index($preferred_order, $a1);
65 my $b_index = index($preferred_order, $b1);
66
67 $a_index = 1000 if ($a_index == -1);
68 $b_index = 1000 if ($b_index == -1);
69
70 if (($a1 =~ /^F$/ && $b1 =~ /^F$/) ||
71 ($a1 =~ /^X$/ && $b1 =~ /^X$/)) {
72 return $a cmp $b;
73 }
74
75 if ($a_index < $b_index) {
76 return -1;
77 } elsif ($a_index == $b_index) {
78 return 0;
79 } else {
80 return 1;
81 }
Linus Torvalds7683e9e2017-07-23 16:06:21 -070082}
83
Linus Torvalds7683e9e2017-07-23 16:06:21 -070084sub trim {
85 my $s = shift;
86 $s =~ s/\s+$//;
87 $s =~ s/^\s+//;
88 return $s;
89}
90
Joe Perchesfe909032017-08-05 18:45:48 -070091sub alpha_output {
92 my ($hashref, $filename) = (@_);
93
Joe Perches1e6270d2017-11-17 15:27:10 -080094 return if ! scalar(keys %$hashref);
95
Joe Perchesfe909032017-08-05 18:45:48 -070096 open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!\n";
Joe Perches1e6270d2017-11-17 15:27:10 -080097 my $separator;
Joe Perchesfe909032017-08-05 18:45:48 -070098 foreach my $key (sort by_category keys %$hashref) {
99 if ($key eq " ") {
Joe Perchesfe909032017-08-05 18:45:48 -0700100 print $file $$hashref{$key};
101 } else {
Joe Perches1e6270d2017-11-17 15:27:10 -0800102 if (! defined $separator) {
103 $separator = "\n";
104 } else {
105 print $file $separator;
106 }
107 print $file $key . "\n";
Joe Perchesfe909032017-08-05 18:45:48 -0700108 foreach my $pattern (sort by_pattern split('\n', %$hashref{$key})) {
109 print $file ($pattern . "\n");
110 }
111 }
112 }
113 close($file);
114}
115
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700116sub file_input {
Joe Perchesfe909032017-08-05 18:45:48 -0700117 my ($hashref, $filename) = (@_);
118
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700119 my $lastline = "";
120 my $case = " ";
Joe Perchesfe909032017-08-05 18:45:48 -0700121 $$hashref{$case} = "";
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700122
Joe Perchesfe909032017-08-05 18:45:48 -0700123 open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n";
124
125 while (<$file>) {
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700126 my $line = $_;
127
128 # Pattern line?
129 if ($line =~ m/^([A-Z]):\s*(.*)/) {
130 $line = $1 . ":\t" . trim($2) . "\n";
131 if ($lastline eq "") {
Joe Perchesfe909032017-08-05 18:45:48 -0700132 $$hashref{$case} = $$hashref{$case} . $line;
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700133 next;
134 }
135 $case = trim($lastline);
Joe Perchesfe909032017-08-05 18:45:48 -0700136 exists $$hashref{$case} and die "Header '$case' already exists";
137 $$hashref{$case} = $line;
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700138 $lastline = "";
139 next;
140 }
141
142 if ($case eq " ") {
Joe Perchesfe909032017-08-05 18:45:48 -0700143 $$hashref{$case} = $$hashref{$case} . $lastline;
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700144 $lastline = $line;
145 next;
146 }
147 trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
148 $lastline = $line;
149 }
Joe Perchesfe909032017-08-05 18:45:48 -0700150 $$hashref{$case} = $$hashref{$case} . $lastline;
151 close($file);
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700152}
153
Joe Perchesfe909032017-08-05 18:45:48 -0700154my %hash;
Joe Perchesb95c29a2017-08-05 18:45:49 -0700155my %new_hash;
Joe Perchesfe909032017-08-05 18:45:48 -0700156
Joe Perches1e6270d2017-11-17 15:27:10 -0800157file_input(\%hash, $input_file);
Joe Perchesb95c29a2017-08-05 18:45:49 -0700158
159foreach my $type (@ARGV) {
160 foreach my $key (keys %hash) {
161 if ($key =~ /$type/ || $hash{$key} =~ /$type/) {
162 $new_hash{$key} = $hash{$key};
163 delete $hash{$key};
164 }
165 }
166}
167
Joe Perches1e6270d2017-11-17 15:27:10 -0800168alpha_output(\%hash, $output_file);
169alpha_output(\%new_hash, $output_section);
Joe Perches61f74162017-08-05 18:45:47 -0700170
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700171exit(0);