Remove all trace of FIPS_mode functions
[openssl.git] / util / lang-compress.pl
1 #! /usr/bin/env perl
2 #
3 # C source compressor.  This:
4 #
5 # - merges continuation lines
6 # - removes comments (not in strings)
7 # - removes empty lines (not in strings)
8
9 use strict;
10 use warnings;
11
12 my $debug = defined $ENV{DEBUG};
13 my $lang = shift @ARGV;
14
15 # Slurp the file
16 $/ = undef;
17 $_ = <>;
18
19 if ($lang eq 'C') {
20     # Merge continuation lines
21     s{\\\n}{}g;
22
23     # Regexp for things that should be preserved
24     my $preserved =
25         qr{
26               (?:
27                   "                 # String start
28                   (?: \\. | [^\"])* # Any character, including escaped ones
29                   "                 # String end
30               )
31
32           |                         # OR
33
34               (?:
35                   '                 # Character start (multi-chars supported)
36                   (?: \\. | [^\'])+ # Any character, including escaped ones
37                   '                 # String end
38               )
39         }x;
40
41     # Remove comments while preserving strings
42     s{
43          (?|                        # All things preserved end up in $1
44
45              /\*                    # C comment start
46              .*?                    # Contents up until
47              \*/                    # C comment end
48
49          |                          # OR
50
51              (                      # Grouping for the replacement
52                  $preserved
53              )
54
55          )
56     }{
57         if ($debug) {
58             print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
59             print STDERR "DEBUG: '$&' removed\n" unless defined $1;
60         }
61         defined $1 ? $1 : ""
62     }gsxe;
63
64     # Remove empty lines
65     s{
66          (?|                        # All things preserved end up in $1
67
68              (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
69
70          |                          # OR
71
72              (                      # Grouping for the replacement
73                  $preserved
74              )
75
76          )
77     }{$1}gsx;
78
79     # Remove extra spaces
80     s{
81          (?|                        # All things preserved end up in $1
82
83              (\n)\h+                # Spaces at start of lines removed
84
85          |
86
87              \h+(\n)                # Spaces at end of lines removed
88
89          |
90
91              \h+                    # Other horizontal spaces replaced with one
92
93          |                          # OR
94
95              (                      # Grouping for the replacement
96                  $preserved
97              )
98
99          )
100     }{
101         if ($debug) {
102             print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
103             print STDERR "DEBUG: '$&' => ' '\n" unless defined $1;
104         }
105         defined $1 ? $1 : " "
106     }gsxe;
107 } elsif ($lang eq 'S') {
108     # Because we use C++ style comments in our .S files, all we can do
109     # is to drop them
110     s{
111          ^([^\n]*?)//[^\n]*?$   # Any line with a // comment
112     }{
113         if ($debug) {
114             print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
115             print STDERR "DEBUG: '$&' removed\n" unless defined $1;
116         }
117         defined $1 ? $1 : ""
118     }mgsxe;
119
120     # Drop all empty lines
121     s{
122          (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
123     }{$1}gsx;
124 } elsif ($lang eq 'perl') {
125     # Merge continuation lines
126     s{\\\n}{}g;
127
128     # Regexp for things that should be preserved
129     my $preserved =
130         qr{
131               (?:
132                   <<["']?(\w+)["']? # HERE document start
133                   .*?               # Its contents
134                   ^\g{-1}$
135               )
136           |
137               (?:
138                   "                 # Double quoted string start
139                   (?: \\. | [^\"])* # Any character, including escaped ones
140                   "                 # Double quoted string end
141               )
142
143           |                         # OR
144
145               (?:
146                   '                 # Single quoted string start
147                   [^\']*            # Any character
148                   '                 # Single quoted string end
149               )
150         }msx;
151
152     # Remove comments while preserving strings
153     s{
154          (?|                        # All things preserved end up in $1
155
156              \#.*?(\n|$)            # Perl comments
157
158          |                          # OR
159
160              (                      # Grouping for the replacement
161                  $preserved
162              )
163
164          )
165     }{
166         if ($debug) {
167             print STDERR "DEBUG: '$&' => '$1'\n" if defined $1;
168             print STDERR "DEBUG: '$&' removed\n" unless defined $1;
169         }
170         defined $1 ? $1 : ""
171     }gsxe;
172
173     # Remove empty lines
174     s{
175          (?|                        # All things preserved end up in $1
176
177              (^|\n)(?:\s*(?:\n|$))+ # Empty lines, preserve one newline
178
179          |                          # OR
180
181              (                      # Grouping for the replacement
182                  $preserved
183              )
184
185          )
186     }{$1}gsx;
187 }
188
189 print;