2017-07-24 02:06:21 +03:00
#!/usr/bin/perl -w
use strict ;
2017-08-06 04:45:48 +03:00
my $ P = $ 0 ;
2017-07-24 02:06:21 +03:00
2017-08-06 04:45:47 +03:00
# sort comparison functions
2017-07-24 02:06:21 +03:00
sub by_category ($$) {
my ( $ a , $ b ) = @ _ ;
$ a = uc $ a ;
$ b = uc $ b ;
# This always sorts last
$ a =~ s/THE REST/ZZZZZZ/g ;
$ b =~ s/THE REST/ZZZZZZ/g ;
2017-08-06 04:45:47 +03:00
return $ a cmp $ b ;
}
sub by_pattern ($$) {
my ( $ a , $ b ) = @ _ ;
my $ preferred_order = 'MRPLSWTQBCFXNK' ;
my $ a1 = uc ( substr ( $ a , 0 , 1 ) ) ;
my $ b1 = uc ( substr ( $ b , 0 , 1 ) ) ;
my $ a_index = index ( $ preferred_order , $ a1 ) ;
my $ b_index = index ( $ preferred_order , $ b1 ) ;
$ a_index = 1000 if ( $ a_index == - 1 ) ;
$ b_index = 1000 if ( $ b_index == - 1 ) ;
if ( ( $ a1 =~ /^F$/ && $ b1 =~ /^F$/ ) ||
( $ a1 =~ /^X$/ && $ b1 =~ /^X$/ ) ) {
return $ a cmp $ b ;
}
if ( $ a_index < $ b_index ) {
return - 1 ;
} elsif ( $ a_index == $ b_index ) {
return 0 ;
} else {
return 1 ;
}
2017-07-24 02:06:21 +03:00
}
2017-08-06 04:45:48 +03:00
sub trim {
my $ s = shift ;
$ s =~ s/\s+$// ;
$ s =~ s/^\s+// ;
return $ s ;
}
2017-07-24 02:06:21 +03:00
sub alpha_output {
2017-08-06 04:45:48 +03:00
my ( $ hashref , $ filename ) = ( @ _ ) ;
open ( my $ file , '>' , "$filename" ) or die "$P: $filename: open failed - $!\n" ;
foreach my $ key ( sort by_category keys %$ hashref ) {
2017-08-06 04:45:47 +03:00
if ( $ key eq " " ) {
2017-08-06 04:45:48 +03:00
chomp $$ hashref { $ key } ;
print $ file $$ hashref { $ key } ;
2017-08-06 04:45:47 +03:00
} else {
2017-08-06 04:45:48 +03:00
print $ file "\n" . $ key . "\n" ;
foreach my $ pattern ( sort by_pattern split ( '\n' , %$ hashref { $ key } ) ) {
print $ file ( $ pattern . "\n" ) ;
2017-08-06 04:45:47 +03:00
}
}
2017-07-24 02:06:21 +03:00
}
2017-08-06 04:45:48 +03:00
close ( $ file ) ;
2017-07-24 02:06:21 +03:00
}
sub file_input {
2017-08-06 04:45:48 +03:00
my ( $ hashref , $ filename ) = ( @ _ ) ;
2017-07-24 02:06:21 +03:00
my $ lastline = "" ;
my $ case = " " ;
2017-08-06 04:45:48 +03:00
$$ hashref { $ case } = "" ;
open ( my $ file , '<' , "$filename" ) or die "$P: $filename: open failed - $!\n" ;
2017-07-24 02:06:21 +03:00
2017-08-06 04:45:48 +03:00
while ( <$file> ) {
2017-07-24 02:06:21 +03:00
my $ line = $ _ ;
# Pattern line?
if ( $ line =~ m/^([A-Z]):\s*(.*)/ ) {
$ line = $ 1 . ":\t" . trim ( $ 2 ) . "\n" ;
if ( $ lastline eq "" ) {
2017-08-06 04:45:48 +03:00
$$ hashref { $ case } = $$ hashref { $ case } . $ line ;
2017-07-24 02:06:21 +03:00
next ;
}
$ case = trim ( $ lastline ) ;
2017-08-06 04:45:48 +03:00
exists $$ hashref { $ case } and die "Header '$case' already exists" ;
$$ hashref { $ case } = $ line ;
2017-07-24 02:06:21 +03:00
$ lastline = "" ;
next ;
}
if ( $ case eq " " ) {
2017-08-06 04:45:48 +03:00
$$ hashref { $ case } = $$ hashref { $ case } . $ lastline ;
2017-07-24 02:06:21 +03:00
$ lastline = $ line ;
next ;
}
trim ( $ lastline ) eq "" or die ( "Odd non-pattern line '$lastline' for '$case'" ) ;
$ lastline = $ line ;
}
2017-08-06 04:45:48 +03:00
$$ hashref { $ case } = $$ hashref { $ case } . $ lastline ;
close ( $ file ) ;
2017-07-24 02:06:21 +03:00
}
2017-08-06 04:45:48 +03:00
my % hash ;
2017-08-06 04:45:49 +03:00
my % new_hash ;
2017-08-06 04:45:48 +03:00
file_input ( \ % hash , "MAINTAINERS" ) ;
2017-08-06 04:45:49 +03:00
foreach my $ type ( @ ARGV ) {
foreach my $ key ( keys % hash ) {
if ( $ key =~ /$type/ || $ hash { $ key } =~ /$type/ ) {
$ new_hash { $ key } = $ hash { $ key } ;
delete $ hash { $ key } ;
}
}
}
2017-08-06 04:45:48 +03:00
alpha_output ( \ % hash , "MAINTAINERS.new" ) ;
2017-08-06 04:45:49 +03:00
alpha_output ( \ % new_hash , "SECTION.new" ) ;
2017-08-06 04:45:47 +03:00
2017-07-24 02:06:21 +03:00
exit ( 0 ) ;