Fix brace matching on multiline constructs in perl.req (RhBug:752119)
[platform/upstream/rpm.git] / scripts / perl.req
1 #!/usr/bin/perl
2
3 # RPM (and its source code) is covered under two separate licenses.
4
5 # The entire code base may be distributed under the terms of the GNU
6 # General Public License (GPL), which appears immediately below.
7 # Alternatively, all of the source code in the lib subdirectory of the
8 # RPM source code distribution as well as any code derived from that
9 # code may instead be distributed under the GNU Library General Public
10 # License (LGPL), at the choice of the distributor. The complete text
11 # of the LGPL appears at the bottom of this file.
12
13 # This alternatively is allowed to enable applications to be linked
14 # against the RPM library (commonly called librpm) without forcing
15 # such applications to be distributed under the GPL.
16
17 # Any questions regarding the licensing of RPM should be addressed to
18 # Erik Troan <ewt@redhat.com>.
19
20 # a simple makedepend like script for perl.
21
22 # To save development time I do not parse the perl grammar but
23 # instead just lex it looking for what I want.  I take special care to
24 # ignore comments and pod's.
25
26 # It would be much better if perl could tell us the dependencies of a
27 # given script.
28
29 # The filenames to scan are either passed on the command line or if
30 # that is empty they are passed via stdin.
31
32 # If there are strings in the file which match the pattern
33 #     m/^\s*\$RPM_Requires\s*=\s*["'](.*)['"]/i
34 # then these are treated as additional names which are required by the
35 # file and are printed as well.
36
37 # I plan to rewrite this in C so that perl is not required by RPM at
38 # build time.
39
40 # by Ken Estes Mail.com kestes@staff.mail.com
41
42 $HAVE_VERSION = 0;
43 eval { require version; $HAVE_VERSION = 1; };
44
45
46 if ("@ARGV") {
47   foreach (@ARGV) {
48     process_file($_);
49   }
50 } else {
51
52   # notice we are passed a list of filenames NOT as common in unix the
53   # contents of the file.
54
55   foreach (<>) {
56     process_file($_);
57   }
58 }
59
60
61 foreach $perlver (sort keys %perlreq) {
62   print "perl >= $perlver\n";
63 }
64 foreach $module (sort keys %require) {
65   if (length($require{$module}) == 0) {
66     print "perl($module)\n";
67   } else {
68
69     # I am not using rpm3.0 so I do not want spaces around my
70     # operators. Also I will need to change the processing of the
71     # $RPM_* variable when I upgrade.
72
73     print "perl($module) >= $require{$module}\n";
74   }
75 }
76
77 exit 0;
78
79
80
81 sub add_require {
82   my ($module, $newver) = @_;
83   my $oldver = $require{$module};
84   if ($oldver) {
85     $require{$module} = $newver
86       if ($HAVE_VERSION && $newver && version->new($oldver) < $newver);
87   }
88   else {
89     $require{$module} = $newver;
90   }
91 }
92
93 sub process_file {
94
95   my ($file) = @_;
96   chomp $file;
97
98   if (!open(FILE, $file)) {
99     warn("$0: Warning: Could not open file '$file' for reading: $!\n");
100     return;
101   }
102
103   while (<FILE>) {
104
105     # skip the "= <<" block
106
107     if (m/^\s*\$(?:.*)\s*=\s*<<\s*(["'`])(.+?)\1/ ||
108         m/^\s*\$(.*)\s*=\s*<<(\w+)\s*;/) {
109       $tag = $2;
110       while (<FILE>) {
111         chomp;
112         ( $_ eq $tag ) && last;
113       }
114       $_ = <FILE>;
115     }
116
117     # skip q{} quoted sections - just hope we don't have curly brackets
118     # within the quote, nor an escaped hash mark that isn't a comment
119     # marker, such as occurs right here. Draw the line somewhere.
120     if ( m/^.*\Wq[qxwr]?\s*([{([#|\/])[^})\]#|\/]*$/ && ! m/^\s*(require|use)\s/ ) {
121       $tag = $1;
122       $tag =~ tr/{\(\[\#|\//})]#|\//;
123       $tag = quotemeta($tag);
124       while (<FILE>) {
125         ( $_ =~ m/$tag/ ) && last;
126       }
127     }
128
129     # skip the documentation
130
131     # we should not need to have item in this if statement (it
132     # properly belongs in the over/back section) but people do not
133     # read the perldoc.
134
135     if (/^=(head[1-4]|pod|for|item)/) {
136       /^=cut/ && next while <FILE>;
137     }
138
139     if (/^=over/) {
140       /^=back/ && next while <FILE>;
141     }
142
143     # skip the data section
144     if (m/^__(DATA|END)__$/) {
145       last;
146     }
147
148     # Each keyword can appear multiple times.  Don't
149     #  bother with datastructures to store these strings,
150     #  if we need to print it print it now.
151     #
152         # Again allow for "our".
153     if (m/^\s*(our\s+)?\$RPM_Requires\s*=\s*["'](.*)['"]/i) {
154       foreach $_ (split(/\s+/, $2)) {
155         print "$_\n";
156       }
157     }
158
159     my $modver_re = qr/[.0-9]+/;
160
161     if (
162
163 # ouch could be in a eval, perhaps we do not want these since we catch
164 # an exception they must not be required
165
166 #   eval { require Term::ReadLine } or die $@;
167 #   eval "require Term::Rendezvous;" or die $@;
168 #   eval { require Carp } if defined $^S; # If error/warning during compilation,
169
170
171         (m/^(\s*)         # we hope the inclusion starts the line
172          (require|use)\s+(?!\{)     # do not want 'do {' loops
173          # quotes around name are always legal
174          ['"]?([^; '"\t#]+)['"]?[\t; ]
175          # the syntax for 'use' allows version requirements
176          # the latter part is for "use base qw(Foo)" and friends special case
177          \s*($modver_re|(qw\s*[(\/'"]\s*|['"])[^)\/"'\$]*?\s*[)\/"'])?
178          /x)
179        ) {
180       my ($whitespace, $statement, $module, $version) = ($1, $2, $3, $4);
181
182       # we only consider require statements that are flushed against
183       # the left edge. any other require statements give too many
184       # false positives, as they are usually inside of an if statement
185       # as a fallback module or a rarely used option
186
187       ($whitespace ne "" && $statement eq "require") && next;
188
189       # if there is some interpolation of variables just skip this
190       # dependency, we do not want
191       #        do "$ENV{LOGDIR}/$rcfile";
192
193       ($module =~ m/\$/) && next;
194
195       # skip if the phrase was "use of" -- shows up in gimp-perl, et al.
196       next if $module eq 'of';
197
198       # if the module ends in a comma we probably caught some
199       # documentation of the form 'check stuff,\n do stuff, clean
200       # stuff.' there are several of these in the perl distribution
201
202       ($module  =~ m/[,>]$/) && next;
203
204       # if the module name starts in a dot it is not a module name.
205       # Is this necessary?  Please give me an example if you turn this
206       # back on.
207
208       #      ($module =~ m/^\./) && next;
209
210       # if the module starts with /, it is an absolute path to a file
211       if ($module =~ m(^/)) {
212         print "$module\n";
213         next;
214       }
215
216       # sometimes people do use POSIX qw(foo), or use POSIX(qw(foo)) etc.
217       # we can strip qw.*$, as well as (.*$:
218       $module =~ s/qw.*$//;
219       $module =~ s/\(.*$//;
220
221       # if the module ends with .pm, strip it to leave only basename.
222       $module =~ s/\.pm$//;
223
224       # some perl programmers write 'require URI/URL;' when
225       # they mean 'require URI::URL;'
226
227       $module =~ s/\//::/;
228
229       # trim off trailing parentheses if any.  Sometimes people pass
230       # the module an empty list.
231
232       $module =~ s/\(\s*\)$//;
233
234       if ( $module =~ m/^v?([0-9._]+)$/ ) {
235       # if module is a number then both require and use interpret that
236       # to mean that a particular version of perl is specified
237
238       my $ver = $1;
239       if ($ver =~ /5.00/) {
240         $perlreq{"0:$ver"} = 1;
241         next;
242       }
243       else {
244         $perlreq{"1:$ver"} = 1;
245         next;
246       }
247
248       };
249
250       # ph files do not use the package name inside the file.
251       # perlmodlib documentation says:
252
253       #       the .ph files made by h2ph will probably end up as
254       #       extension modules made by h2xs.
255
256       # so do not expend much effort on these.
257
258
259       # there is no easy way to find out if a file named systeminfo.ph
260       # will be included with the name sys/systeminfo.ph so only use the
261       # basename of *.ph files
262
263       ($module =~ m/\.ph$/) && next;
264
265       # use base|parent qw(Foo) dependencies
266       if ($statement eq "use" && ($module eq "base" || $module eq "parent")) {
267         add_require($module, undef);
268         if ($version =~ /^qw\s*[(\/'"]\s*([^)\/"']+?)\s*[)\/"']/) {
269           add_require($_, undef) for split(' ', $1);
270         }
271         elsif ($version =~ /(["'])([^"']+)\1/) {
272           add_require($2, undef);
273         }
274         next;
275       }
276       $version = undef unless $version =~ /^$modver_re$/o;
277
278       add_require($module, $version);
279     }
280
281   }
282
283   close(FILE) ||
284     die("$0: Could not close file: '$file' : $!\n");
285
286   return;
287 }