Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
| 2 | |
| 3 | use strict; |
| 4 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 5 | my %hash; |
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 | |
| 48 | sub alpha_output { |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 49 | foreach my $key (sort by_category keys %hash) { |
| 50 | if ($key eq " ") { |
| 51 | chomp $hash{$key}; |
| 52 | print $hash{$key}; |
| 53 | } else { |
| 54 | print "\n" . $key . "\n"; |
| 55 | foreach my $pattern (sort by_pattern split('\n', $hash{$key})) { |
| 56 | print($pattern . "\n"); |
| 57 | } |
| 58 | } |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | sub trim { |
| 63 | my $s = shift; |
| 64 | $s =~ s/\s+$//; |
| 65 | $s =~ s/^\s+//; |
| 66 | return $s; |
| 67 | } |
| 68 | |
| 69 | sub file_input { |
| 70 | my $lastline = ""; |
| 71 | my $case = " "; |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 72 | $hash{$case} = ""; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 73 | |
| 74 | while (<>) { |
| 75 | my $line = $_; |
| 76 | |
| 77 | # Pattern line? |
| 78 | if ($line =~ m/^([A-Z]):\s*(.*)/) { |
| 79 | $line = $1 . ":\t" . trim($2) . "\n"; |
| 80 | if ($lastline eq "") { |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 81 | $hash{$case} = $hash{$case} . $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 82 | next; |
| 83 | } |
| 84 | $case = trim($lastline); |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 85 | exists $hash{$case} and die "Header '$case' already exists"; |
| 86 | $hash{$case} = $line; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 87 | $lastline = ""; |
| 88 | next; |
| 89 | } |
| 90 | |
| 91 | if ($case eq " ") { |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 92 | $hash{$case} = $hash{$case} . $lastline; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 93 | $lastline = $line; |
| 94 | next; |
| 95 | } |
| 96 | trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'"); |
| 97 | $lastline = $line; |
| 98 | } |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 99 | $hash{$case} = $hash{$case} . $lastline; |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Joe Perches | 61f7416 | 2017-08-05 18:45:47 -0700 | [diff] [blame^] | 102 | file_input(); |
| 103 | alpha_output(); |
| 104 | |
Linus Torvalds | 7683e9e | 2017-07-23 16:06:21 -0700 | [diff] [blame] | 105 | exit(0); |