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; |
| 5 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 6 | my $P = $0; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 7 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 8 | # sort comparison functions |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 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 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 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 | } |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 49 | sub trim { |
| 50 | my $s = shift; |
| 51 | $s =~ s/\s+$//; |
| 52 | $s =~ s/^\s+//; |
| 53 | return $s; |
| 54 | } |
| 55 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 74 | sub file_input { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 75 | my ($hashref, $filename) = (@_); |
| 76 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 77 | my $lastline = ""; |
| 78 | my $case = " "; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 79 | $$hashref{$case} = ""; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 80 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 81 | open(my $file, '<', "$filename") or die "$P: $filename: open failed - $!\n"; |
| 82 | |
| 83 | while (<$file>) { |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 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 "") { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 90 | $$hashref{$case} = $$hashref{$case} . $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 91 | next; |
| 92 | } |
| 93 | $case = trim($lastline); |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 94 | exists $$hashref{$case} and die "Header '$case' already exists"; |
| 95 | $$hashref{$case} = $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 96 | $lastline = ""; |
| 97 | next; |
| 98 | } |
| 99 | |
| 100 | if ($case eq " ") { |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 101 | $$hashref{$case} = $$hashref{$case} . $lastline; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 102 | $lastline = $line; |
| 103 | next; |
| 104 | } |
| 105 | trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'"); |
| 106 | $lastline = $line; |
| 107 | } |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 108 | $$hashref{$case} = $$hashref{$case} . $lastline; |
| 109 | close($file); |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 112 | my %hash; |
Joe Perches | b95c29a | 2017-08-05 18:45:49 -0700 | [diff] [blame] | 113 | my %new_hash; |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 114 | |
| 115 | file_input(\%hash, "MAINTAINERS"); |
Joe Perches | b95c29a | 2017-08-05 18:45:49 -0700 | [diff] [blame] | 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 | |
Joe Perches | fe90903 | 2017-08-05 18:45:48 -0700 | [diff] [blame] | 126 | alpha_output(\%hash, "MAINTAINERS.new"); |
Joe Perches | b95c29a | 2017-08-05 18:45:49 -0700 | [diff] [blame] | 127 | alpha_output(\%new_hash, "SECTION.new"); |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame] | 128 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 129 | exit(0); |