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