Debian Codegraph @

bin/urifind

raw
1 #!/usr/local/bin/perl -w
2
3 # ----------------------------------------------------------------------
4 # urifind - find URIs in a document and dump them to STDOUT.
5 # Copyright (C) 2003 darren chamberlain <[email protected]>
6 # ----------------------------------------------------------------------
7
8 use strict;
9
10 our $VERSION = 20160806;
11
12 use File::Basename qw(basename);
13 use Getopt::Long qw(GetOptions);
14 use IO::File;
15 use URI::Find;
16 use URI::Find::Schemeless;
17
18 # What to do, and how
19 my $help = 0;
20 my $version = 0;
21 my $sort = 0;
22 my $reverse = 0;
23 my $unique = 0;
24 my $prefix = 0;
25 my $noprefix = 0;
26 my @pats = ();
27 my @schemes = ();
28 my $dump = 0;
29 my $schemeless = 0;
30
31 Getopt::Long::Configure(qw{no_ignore_case bundling});
32 GetOptions(
33 's!' => \$sort,
34 'u!' => \$unique,
35 'p!' => \$prefix,
36 'n!' => \$noprefix,
37 'r!' => \$reverse,
38 'h!' => \$help,
39 'v!' => \$version,
40 'd!' => sub { $dump = 1 },
41 'D!' => sub { $dump = 2 },
42 'P=s@' => \@pats,
43 'S=s@' => \@schemes,
44 'schemeless!' => \$schemeless,
45 );
46
47 if ($help || $version) {
48 my $prog = basename($0);
49
50 if ($help) {
51 print <<HELP;
52 $prog - find URIs in a document and dump them to STDOUT.
53
54 $prog [OPTIONS] file1 [file2[, file3[, ...]]]
55
56 Options:
57
58 -s Sort results.
59 -r Reverse sort results (implies -s).
60 -u Return unique results only.
61 -n Don't include filename in output.
62 -p Include filename in output (0 by default, but 1 if
63 multiple files are included on the command line).
64 -P \$re Print only lines matching regex '\$re'
65 (may be specified multiple times).
66 -S \$scheme Only this scheme (may be specified multiple times).
67 -h This help screen.
68 -v Display version and exit.
69 -d Dump compiled regexes and continue.
70 -D Dump compiled regexes and exit.
71
72 HELP
73 }
74 else {
75 printf "$prog v.%.02f\n", $VERSION;
76 }
77
78 exit(0);
79 }
80
81 my (@uris, $count);
82 unshift @ARGV, \*STDIN unless @ARGV;
83
84 if (($prefix + $noprefix) > 1) {
85 my $prog = basename $0;
86 die "Can't specify -p and -n at the same time; try $prog -h\n";
87 }
88
89
90 # Print filename with matches? -p / -n
91 # If there is more than one file, then show filenames by
92 # default, unless explicitly asked not to (-n)
93 if (@ARGV > 1) {
94 $prefix = 1 unless $noprefix;
95 }
96 else {
97 $prefix = 0 unless $prefix;
98 }
99
100 # Add schemes to the list of regexen
101 if (@schemes) {
102 unshift @pats => sprintf '^(\b%s\b):' => join '\b|\b' => @schemes;
103 }
104
105 # If we are dumping (-d, -D), then dump. Exit if -D.
106 if ($dump) {
107 print STDERR "\$scheme = '" . (defined $pats[0] ? $pats[0] : '') . "'\n";
108 print STDERR "\@pats = ('" . join("', '", @pats) . "')\n";
109 exit if $dump == 2;
110 }
111
112 # Find the URIs
113 for my $argv (@ARGV) {
114 my ($name, $fh, $data);
115
116 $argv = \*STDIN if ($argv eq '-');
117
118 if (ref $argv eq 'GLOB') {
119 local $/;
120 $data = <$argv>;
121 $name = '<stdin>'
122 }
123 else {
124 local $/;
125 $fh = IO::File->new($argv) or die "Can't open $argv: $!";
126 $data = <$fh>;
127 $name = $argv;
128 }
129
130 my $class = $schemeless ? "URI::Find::Schemeless" : "URI::Find";
131 my $finder = $class->new(sub { push @uris => [ $name, $_[0] ] });
132 $finder->find(\$data);
133 }
134
135 # Apply patterns, in @pats
136 for my $pat (@pats) {
137 @uris = grep { $_->[1] =~ /$pat/ } @uris;
138 }
139
140 # Remove redundant links
141 if ($unique) {
142 my %unique;
143 @uris = grep { ++$unique{$_->[1]} == 1 } @uris;
144 }
145
146 # Sort links, possibly in reverse
147 if ($sort || $reverse) {
148 if ($reverse) {
149 @uris = sort { $b->[1] cmp $a->[1] } @uris;
150 }
151 else {
152 @uris = sort { $a->[1] cmp $b->[1] } @uris;
153 }
154 }
155
156 # Flatten the arrayrefs
157 if ($prefix) {
158 @uris = map { join ': ' => @$_ } @uris;
159 }
160 else {
161 @uris = map { $_->[1] } @uris;
162 }
163
164 print map { "$_\n" } @uris;
165
166 exit 0;
167
168 __END__
169
170 =head1 NAME
171
172 urifind - find URIs in a document and dump them to STDOUT.
173
174 =head1 SYNOPSIS
175
176 $ urifind file
177
178 =head1 DESCRIPTION
179
180 F<urifind> is a simple script that finds URIs in one or more files
181 (using C<URI::Find>), and outputs them to to STDOUT. That's it.
182
183 To find all the URIs in F<file1>, use:
184
185 $ urifind file1
186
187 To find the URIs in multiple files, simply list them as arguments:
188
189 $ urifind file1 file2 file3
190
191 F<urifind> will read from C<STDIN> if no files are given or if a
192 filename of C<-> is specified:
193
194 $ wget http://www.boston.com/ -O - | urifind
195
196 When multiple files are listed, F<urifind> prefixes each found URI
197 with the file from which it came:
198
199 $ urifind file1 file2
200 file1: http://www.boston.com/index.html
201 file2: http://use.perl.org/
202
203 This can be turned on for single files with the C<-p> ("prefix") switch:
204
205 $urifind -p file3
206 file1: http://fsck.com/rt/
207
208 It can also be turned off for multiple files with the C<-n> ("no
209 prefix") switch:
210
211 $ urifind -n file1 file2
212 http://www.boston.com/index.html
213 http://use.perl.org/
214
215 By default, URIs will be displayed in the order found; to sort them
216 ascii-betically, use the C<-s> ("sort") option. To reverse sort them,
217 use the C<-r> ("reverse") flag (C<-r> implies C<-s>).
218
219 $ urifind -s file1 file2
220 http://use.perl.org/
221 http://www.boston.com/index.html
222 mailto:[email protected]
223
224 $ urifind -r file1 file2
225 mailto:[email protected]
226 http://www.boston.com/index.html
227 http://use.perl.org/
228
229 Finally, F<urifind> supports limiting the returned URIs by scheme or
230 by arbitrary pattern, using the C<-S> option (for schemes) and the
231 C<-P> option. Both C<-S> and C<-P> can be specified multiple times:
232
233 $ urifind -S mailto file1
234 mailto:[email protected]
235
236 $ urifind -S mailto -S http file1
237 mailto:[email protected]
238 http://www.boston.com/index.html
239
240 C<-P> takes an arbitrary Perl regex. It might need to be protected
241 from the shell:
242
243 $ urifind -P 's?html?' file1
244 http://www.boston.com/index.html
245
246 $ urifind -P '\.org\b' -S http file4
247 http://www.gnu.org/software/wget/wget.html
248
249 Add a C<-d> to have F<urifind> dump the refexen generated from C<-S>
250 and C<-P> to C<STDERR>. C<-D> does the same but exits immediately:
251
252 $ urifind -P '\.org\b' -S http -D
253 $scheme = '^(\bhttp\b):'
254 @pats = ('^(\bhttp\b):', '\.org\b')
255
256 To remove duplicates from the results, use the C<-u> ("unique")
257 switch.
258
259 =head1 OPTION SUMMARY
260
261 =over 4
262
263 =item -s
264
265 Sort results.
266
267 =item -r
268
269 Reverse sort results (implies -s).
270
271 =item -u
272
273 Return unique results only.
274
275 =item -n
276
277 Don't include filename in output.
278
279 =item -p
280
281 Include filename in output (0 by default, but 1 if multiple files are
282 included on the command line).
283
284 =item -P $re
285
286 Print only lines matching regex '$re' (may be specified multiple times).
287
288 =item -S $scheme
289
290 Only this scheme (may be specified multiple times).
291
292 =item -h
293
294 Help summary.
295
296 =item -v
297
298 Display version and exit.
299
300 =item -d
301
302 Dump compiled regexes for C<-S> and C<-P> to C<STDERR>.
303
304 =item -D
305
306 Same as C<-d>, but exit after dumping.
307
308 =back
309
310 =head1 AUTHOR
311
312 darren chamberlain E<lt>[email protected]<gt>
313
314 =head1 COPYRIGHT
315
316 (C) 2003 darren chamberlain
317
318 This library is free software; you may distribute it and/or modify it
319 under the same terms as Perl itself.
320
321 =head1 SEE ALSO
322
323 L<URI::Find>