blob: 5c8e0504c67e406386da763df2268adf3c57aef2 [file] [log] [blame]
Linus Torvalds7683e9e2017-07-23 16:06:21 -07001#!/usr/bin/perl -w
2
3use strict;
4
Joe Perches61f74162017-08-05 18:45:47 -07005my %hash;
Linus Torvalds7683e9e2017-07-23 16:06:21 -07006
Joe Perches61f74162017-08-05 18:45:47 -07007# sort comparison functions
Linus Torvalds7683e9e2017-07-23 16:06:21 -07008sub 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 Perches61f74162017-08-05 18:45:47 -070018 return $a cmp $b;
19}
20
21sub 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 Torvalds7683e9e2017-07-23 16:06:21 -070046}
47
48sub alpha_output {
Joe Perches61f74162017-08-05 18:45:47 -070049 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 Torvalds7683e9e2017-07-23 16:06:21 -070059 }
60}
61
62sub trim {
63 my $s = shift;
64 $s =~ s/\s+$//;
65 $s =~ s/^\s+//;
66 return $s;
67}
68
69sub file_input {
70 my $lastline = "";
71 my $case = " ";
Joe Perches61f74162017-08-05 18:45:47 -070072 $hash{$case} = "";
Linus Torvalds7683e9e2017-07-23 16:06:21 -070073
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 Perches61f74162017-08-05 18:45:47 -070081 $hash{$case} = $hash{$case} . $line;
Linus Torvalds7683e9e2017-07-23 16:06:21 -070082 next;
83 }
84 $case = trim($lastline);
Joe Perches61f74162017-08-05 18:45:47 -070085 exists $hash{$case} and die "Header '$case' already exists";
86 $hash{$case} = $line;
Linus Torvalds7683e9e2017-07-23 16:06:21 -070087 $lastline = "";
88 next;
89 }
90
91 if ($case eq " ") {
Joe Perches61f74162017-08-05 18:45:47 -070092 $hash{$case} = $hash{$case} . $lastline;
Linus Torvalds7683e9e2017-07-23 16:06:21 -070093 $lastline = $line;
94 next;
95 }
96 trim($lastline) eq "" or die ("Odd non-pattern line '$lastline' for '$case'");
97 $lastline = $line;
98 }
Joe Perches61f74162017-08-05 18:45:47 -070099 $hash{$case} = $hash{$case} . $lastline;
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700100}
101
Joe Perches61f74162017-08-05 18:45:47 -0700102file_input();
103alpha_output();
104
Linus Torvalds7683e9e2017-07-23 16:06:21 -0700105exit(0);