Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 3 | |
| 4 | use strict; |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 5 | use Getopt::Long qw(:config no_auto_abbrev); |
| 6 | |
| 7 | my $input_file = "MAINTAINERS"; |
| 8 | my $output_file = "MAINTAINERS.new"; |
| 9 | my $output_section = "SECTION.new"; |
| 10 | my $help = 0; |
Joe Perches | 5cdbec1 | 2020-03-07 18:59:05 -0800 | [diff] [blame] | 11 | my $order = 0; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 12 | my $P = $0; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 13 | |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 14 | if (!GetOptions( |
| 15 | 'input=s' => \$input_file, |
| 16 | 'output=s' => \$output_file, |
| 17 | 'section=s' => \$output_section, |
Joe Perches | 5cdbec1 | 2020-03-07 18:59:05 -0800 | [diff] [blame] | 18 | 'order!' => \$order, |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 19 | 'h|help|usage' => \$help, |
| 20 | )) { |
| 21 | die "$P: invalid argument - use --help if necessary\n"; |
| 22 | } |
| 23 | |
| 24 | if ($help != 0) { |
| 25 | usage(); |
| 26 | exit 0; |
| 27 | } |
| 28 | |
| 29 | sub usage { |
| 30 | print <<EOT; |
| 31 | usage: $P [options] <pattern matching regexes> |
| 32 | |
| 33 | --input => MAINTAINERS file to read (default: MAINTAINERS) |
| 34 | --output => sorted MAINTAINERS file to write (default: MAINTAINERS.new) |
| 35 | --section => new sorted MAINTAINERS file to write to (default: SECTION.new) |
Joe Perches | 5cdbec1 | 2020-03-07 18:59:05 -0800 | [diff] [blame] | 36 | --order => Use the preferred section content output ordering (default: 0) |
| 37 | Preferred ordering of section output is: |
| 38 | M: Person acting as a maintainer |
| 39 | R: Person acting as a patch reviewer |
| 40 | L: Mailing list where patches should be sent |
| 41 | S: Maintenance status |
| 42 | W: URI for general information |
| 43 | Q: URI for patchwork tracking |
| 44 | B: URI for bug tracking/submission |
| 45 | C: URI for chat |
| 46 | P: URI or file for subsystem specific coding styles |
| 47 | T: SCM tree type and location |
| 48 | F: File and directory pattern |
| 49 | X: File and directory exclusion pattern |
| 50 | N: File glob |
| 51 | K: Keyword - patch content regex |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 52 | |
| 53 | If <pattern match regexes> exist, then the sections that match the |
| 54 | regexes are not written to the output file but are written to the |
| 55 | section file. |
| 56 | |
| 57 | EOT |
| 58 | } |
| 59 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 60 | # sort comparison functions |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 61 | sub by_category($$) { |
| 62 | my ($a, $b) = @_; |
| 63 | |
| 64 | $a = uc $a; |
| 65 | $b = uc $b; |
| 66 | |
| 67 | # This always sorts last |
| 68 | $a =~ s/THE REST/ZZZZZZ/g; |
| 69 | $b =~ s/THE REST/ZZZZZZ/g; |
| 70 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 71 | return $a cmp $b; |
| 72 | } |
| 73 | |
| 74 | sub by_pattern($$) { |
| 75 | my ($a, $b) = @_; |
Joe Perches | 5cdbec1 | 2020-03-07 18:59:05 -0800 | [diff] [blame] | 76 | my $preferred_order = 'MRLSWQBCPTFXNK'; |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 77 | |
| 78 | my $a1 = uc(substr($a, 0, 1)); |
| 79 | my $b1 = uc(substr($b, 0, 1)); |
| 80 | |
| 81 | my $a_index = index($preferred_order, $a1); |
| 82 | my $b_index = index($preferred_order, $b1); |
| 83 | |
| 84 | $a_index = 1000 if ($a_index == -1); |
| 85 | $b_index = 1000 if ($b_index == -1); |
| 86 | |
| 87 | if (($a1 =~ /^F$/ && $b1 =~ /^F$/) || |
| 88 | ($a1 =~ /^X$/ && $b1 =~ /^X$/)) { |
| 89 | return $a cmp $b; |
| 90 | } |
| 91 | |
| 92 | if ($a_index < $b_index) { |
| 93 | return -1; |
| 94 | } elsif ($a_index == $b_index) { |
| 95 | return 0; |
| 96 | } else { |
| 97 | return 1; |
| 98 | } |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 101 | sub trim { |
| 102 | my $s = shift; |
| 103 | $s =~ s/\s+$//; |
| 104 | $s =~ s/^\s+//; |
| 105 | return $s; |
| 106 | } |
| 107 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 108 | sub alpha_output { |
| 109 | my ($hashref, $filename) = (@_); |
| 110 | |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 111 | return if ! scalar(keys %$hashref); |
| 112 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 113 | open(my $file, '>', "$filename") or die "$P: $filename: open failed - $!\n"; |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 114 | my $separator; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 115 | foreach my $key (sort by_category keys %$hashref) { |
| 116 | if ($key eq " ") { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 117 | print $file $$hashref{$key}; |
| 118 | } else { |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 119 | if (! defined $separator) { |
| 120 | $separator = "\n"; |
| 121 | } else { |
| 122 | print $file $separator; |
| 123 | } |
| 124 | print $file $key . "\n"; |
Joe Perches | 5cdbec1 | 2020-03-07 18:59:05 -0800 | [diff] [blame] | 125 | if ($order) { |
| 126 | foreach my $pattern (sort by_pattern split('\n', %$hashref{$key})) { |
| 127 | print $file ($pattern . "\n"); |
| 128 | } |
| 129 | } else { |
| 130 | foreach my $pattern (split('\n', %$hashref{$key})) { |
| 131 | print $file ($pattern . "\n"); |
| 132 | } |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | close($file); |
| 137 | } |
| 138 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 139 | sub file_input { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 140 | my ($hashref, $filename) = (@_); |
| 141 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 142 | my $lastline = ""; |
| 143 | my $case = " "; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 144 | $$hashref{$case} = ""; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 145 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 146 | open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n"; |
| 147 | |
| 148 | while (<$file>) { |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 149 | my $line = $_; |
| 150 | |
| 151 | # Pattern line? |
| 152 | if ($line =~ m/^([A-Z]):\s*(.*)/) { |
| 153 | $line = $1 . ":\t" . trim($2) . "\n"; |
| 154 | if ($lastline eq "") { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 155 | $$hashref{$case} = $$hashref{$case} . $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 156 | next; |
| 157 | } |
| 158 | $case = trim($lastline); |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 159 | exists $$hashref{$case} and die "Header '$case' already exists"; |
| 160 | $$hashref{$case} = $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 161 | $lastline = ""; |
| 162 | next; |
| 163 | } |
| 164 | |
| 165 | if ($case eq " ") { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 166 | $$hashref{$case} = $$hashref{$case} . $lastline; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 167 | $lastline = $line; |
| 168 | next; |
| 169 | } |
| 170 | trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'"); |
| 171 | $lastline = $line; |
| 172 | } |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 173 | $$hashref{$case} = $$hashref{$case} . $lastline; |
| 174 | close($file); |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 177 | my %hash; |
Joe Perches | b95c29a | 2017-08-05 18:45:49 -0700 | [diff] [blame] | 178 | my %new_hash; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 179 | |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 180 | file_input(\%hash, $input_file); |
Joe Perches | b95c29a | 2017-08-05 18:45:49 -0700 | [diff] [blame] | 181 | |
| 182 | foreach my $type (@ARGV) { |
| 183 | foreach my $key (keys %hash) { |
| 184 | if ($key =~ /$type/ || $hash{$key} =~ /$type/) { |
| 185 | $new_hash{$key} = $hash{$key}; |
| 186 | delete $hash{$key}; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
Joe Perches | 1e6270d | 2017-11-17 15:27:10 -0800 | [diff] [blame] | 191 | alpha_output(\%hash, $output_file); |
| 192 | alpha_output(\%new_hash, $output_section); |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 193 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 194 | exit(0); |