parse-maintainers.pl 2.8 KB

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