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