Debian Codegraph @

numgrep

raw ◀ ▶
Source reconstructed at patch state 4 of 6 — not the installed (fully patched) source.
6 Debian patches touch this file
1 #!/usr/bin/perl -w
2
3 # numgrep: This program is the numeric equivilent of the grep
4 # utility. It searches for numbers, sets of numbers and so on.
5 #
6 # Copyright (C) 2002-2004 Suso Banderas
7
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License
10 # as published by the Free Software Foundation; either version 2
11 # of the License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 #
22 # You may contact the author at <[email protected]>.
23
24 #######################
25 # VARIABLES AND SETUP #
26 #######################
27
28 use Getopt::Std;
29 use strict;
30 use vars qw/ %opts $verbose /;
31
32 $| = 1;
33
34 getopts('hdVl', \%opts);
35
36 if ($opts{'h'}) {
37 &help;
38 exit(0);
39 }
40
41 if ($opts{'d'}) {
42 $verbose = 3;
43 print STDERR "Debug mode\n";
44 } elsif ($opts{'V'}) {
45 $verbose = 2;
46 print STDERR "Verbose mode\n";
47 } elsif($opts{'q'}) {
48 $verbose = 0; # No output except the final answer.
49 } else {
50 $verbose = 1; # Normal output.
51 }
52
53 my $match_expression = shift;
54 $match_expression =~ s/^\/(.*)\/$/$1/;
55
56 my $hit = 0;
57
58 # Now read in any extra arguments as filenames to do searches in.
59 my @files = @ARGV;
60 print STDERR "Arg files: " . @files . "\n" if ($verbose >= 3);
61
62 my @ranges;
63 my $file;
64
65 ################
66 # MAIN PROGRAM #
67 ################
68
69 if (@ARGV) {
70 foreach $file (@ARGV) {
71 print STDERR "Reading from file $file.\n" if ($verbose >= 2);
72 open (ARGFILE, "$file") && process_filehandle(\*ARGFILE, $match_expression)
73 || $verbose && warn "Couldn't open file $file for reading: $!\n";
74 close(ARGFILE);
75 }
76 } else {
77 print STDERR "Reading from STDIN.\n" if ($verbose >= 2);
78 process_filehandle(\*STDIN, $match_expression);
79 }
80
81 exit(1) if not $hit;
82 exit(0);
83
84
85 ###############
86 # SUBROUTINES #
87 ###############
88
89 sub help {
90 print <<"EOF";
91 -----------------------------------------------------------
92 numgrep: Search for a number using numeric expressions.
93 -----------------------------------------------------------
94 Usage:
95 numgrep [options] /expression/ file
96 | numgrep [options] /expression/
97 numgrep [options] /expression/
98
99 Options:
100 -l Print the matching numbers out one per line
101 instead of printing the entire line they are on.
102 -d Debug. For developers only.
103 -h Help: You're looking at it.
104 -V Increase verbosity.
105
106 Expressions:
107 .. Range. Use to specify a range of numbers such
108 as 1..10, 1.. or ..10. Which mean between 1 and 10, greater than
109 1, and less than 10 respectively
110
111 , Expression seperator. The comma seperates expressions within
112 the forward slashes. ie. /1..10,20..30,50,60,127/
113
114 m Multiple operator. Using the m followed by an integer will match
115 any multiple of that integer. So /m3/ would match 3, 6, 9, 12, etc.
116
117 f Factor operator. Using the f followed by an integer will match
118 any factor of that integer. So /m20/ would match 1, 2, 4, 5 and 10.
119
120 EOF
121 }
122
123 sub process_filehandle {
124 my $filehandle = shift;
125 my $returnvalue;
126
127 # The below array holds individual expressions that
128 # will be looped through to find matching values.
129
130 while (<$filehandle>) {
131 my $line = $_;
132 if ($line =~ /[0-9]/) { # This expression will determine whether we
133 # should even try to match. Maybe it's wrong
134 # to do this, but it will speed things up.
135 num_match($line);
136 } else {
137 next;
138 }
139 }
140 return 1;
141 }
142
143 # This subroutine determines what to do with matched lines.
144 sub num_match {
145 my $line = shift;
146
147 my $returnvalue = 0;
148 my $expression;
149 my $number;
150
151 my @expression_array = split(/,/, $match_expression);
152
153 my @numbers = ();
154
155 my $strlen = length($line);
156 my $n = 0;
157 while ($n < $strlen) {
158 my $char = substr($line, $n, 1);
159 if ($char =~ /[0-9\.-]/) {
160 ($n, $number) = parse_number(substr($line, $n, ($strlen - $n)), $n);
161 unless ($number eq "") {
162 push (@numbers, $number);
163 }
164 } else {
165 $n++;
166 }
167 }
168
169 if ($opts{'l'}) { # Follow this branch if the user just wants the numbers and not the context.
170
171 foreach $number (@numbers) { # SET represents the numbers found on one line.
172 chomp($number);
173 EXP: foreach $expression (@expression_array) {
174 print "Expression: $expression\n" if ($verbose >= 3);
175
176 my $exp = make_expression($expression, $number);
177
178 # Now if the $exp is true then go on.
179 if (eval($exp)) {
180 print "$number\n";
181 $hit = 1;
182 last EXP;
183 }
184 }
185 }
186 } else { # Else we'll just print out lines that match one of the expressions.
187 NUM: foreach $number (@numbers) {
188 foreach $expression (@expression_array) {
189 my $exp = make_expression($expression, $number);
190 if (eval($exp)) {
191 print "$line";
192 $hit = 1;
193 last NUM;
194 }
195 }
196 }
197 }
198 return $returnvalue;
199 }
200
201 sub make_expression {
202 my $expression = shift;
203 my $number = shift;
204 my $exp;
205
206 if ($expression =~ /^(-?[0-9\.]+)\.\.(-?[0-9\.]+)$/) { # normal range between two numbers.
207 $exp = "$1 <= $number && $number <= $2";
208 } elsif ($expression =~ /^\.\.(-?[0-9\.]+)$/) { # Anything lower than X
209 $exp = "$number <= $1";
210 } elsif ($expression =~ /^(-?[0-9\.]+)\.\.$/) { # Anything higher than X
211 $exp = "$number >= $1";
212 } elsif ($expression =~ /^(-?[0-9\.]+)$/) {
213 $exp = "$number == $1";
214 } elsif ($expression =~ /^m(-?[0-9\.]+)$/) { # Multiples.
215 if ($1 =~ /\./) {
216 die "Please use only integer values for m.\n";
217 $exp = "$number =~ /$expression/";
218 } else {
219 $exp = "!($number % $1)";
220 }
221 } elsif ($expression =~ /^f(-?[0-9\.]+)$/) { # Factors.
222 if ($1 =~ /\./) {
223 die "Please use only integer values for f.\n";
224 $exp = "$number =~ /$expression/";
225 } else {
226 $exp = "!($1 % $number)";
227 }
228 } else {
229 die "Invalid expression: $expression\n";
230 $exp = "$number =~ /$expression/";
231 }
232 print "Exp: '$exp'\n" if ($verbose >= 3);
233
234 return $exp
235 }
236
237 sub parse_number {
238 my $substring = shift;
239 my $placeholder = shift;
240
241 $substring =~ /^(-?[0-9]*\.?[0-9]*)[^0-9]/;
242 my $number = $1;
243
244 my $strlen;
245
246 if ($number =~ /^[\.-]*$/) {
247 $strlen = length($number);
248 $number = "";
249 } else {
250 $strlen = length($number);
251 }
252
253 # Fix the number somewhat.
254 #$number =~ s/^\./0./;
255 #$number =~ s/^-\./-0./;
256 #$number =~ s/-0+/0/;
257 #$number =~ s/\.$//;
258
259 chomp($number);
260
261 return ($placeholder + $strlen, $number);
262 }
263
264
265 # Lay down some of that perl pod action.
266 =pod
267
268 =head1 NAME
269
270 numgrep - This program is the numeric equivilent of the grep utility.
271
272
273 =head1 SYNOPSIS
274
275 B<numgrep> [-dhlV] <FILE>
276
277 | B<numgrep> [-dhlV] (Input on STDIN from pipeline.)
278
279 B<numgrep> [-dhlV] (Input on STDIN. Use Ctrl-D to stop.)
280
281 =head1 DESCRIPTION
282
283 B<numgrep> searches for different occurrences of numbers through the use
284 of numeric expressions.
285
286 =head1 OPTIONS
287
288 -l Print the matching numbers out one per line
289 instead of printing the entire line they are on.
290
291 -h Help: You're looking at it.
292 -V Increase verbosity.
293 -d Debug mode. For developers
294
295 =head1 EXPRESSIONS
296
297 B<numgrep> uses a special numeric expression matching system. Basically,
298 it searches for ranges, factors and sequences of numbers. Here is a list
299 of the syntax characters and some sample expressions that will get you
300 going:
301
302
303 /<expression>/
304 Put your expression or set of expressions between these
305 two forward slashes.
306
307 .. Range expression. A number must be used on the left
308 and/or right of this expression to specify that numbers
309 between, greater than or less than the numbers specified
310 should be matched.
311
312 , Expression separator. The comma separates one complete
313 expression from another in a set enclosed by //.
314
315 m<n> Multiples of <n>. This operator, followed by a number
316 <n> will match any number <x> that is an integer
317 multiple of <n>. Meaning that <x> = <n> times <y>,
318 where <y> is any integer.
319
320 f<n> Factors of <n>. This operator, followed by a number <n>
321 will match any number <x> that is an integer factor of
322 <n>. Meaning that <x> = <n> divided by <y>, where <y>
323 is any integer.
324
325 B< NOTE:> Checking for factors and multiples is very fast because it
326 is checked by doing a single modulus operation on two numbers.
327
328 B<Examples:>
329
330 /2..10/ Match any number between 2 and 10.
331
332 /2..10,20..30/ Match any number between 2 and 10 or between 20 and 30.
333
334 /56,34,512,45,67/ Match any of the numbers 56, 34, 512, 45 or 67.
335
336 /m3/ Match any integer that is a multiple of 3.
337
338 /f1024/ Match any integer that is a factor of 1024.
339
340
341 $ numrange -N /1..1000/ | numgrep /f1024/
342 1
343 2
344 4
345 8
346 16
347 32
348 64
349 128
350 256
351 512
352 $
353
354
355 =head1 BUGS
356
357 B<numgrep> can't handle certain situations properly. Such as if
358 it encounters a number with leading zeros, it will treat it as an
359 octal number and thus might not match the way you would expect.
360
361 B<numgrep> does not yet allow you to mix numbers and text in the
362 matching expression. So you can not do something like
363 'numgrep /port=0..1023/ firewall.log'. But this will be changed in the
364 future.
365
366 =head1 SEE ALSO
367
368 numaverage(1), numbound(1), numinterval(1), numnormalize(1), numprocess(1), numsum(1), numrandom(1), numrange(1), numround(1)
369
370 =head1 COPYRIGHT
371
372 numgrep is part of the num-utils package, which is copyrighted by
373 Suso Banderas and released under the GPL license. Please read
374 the COPYING and LICENSE files that came with the num-utils package
375
376 Developers can read the GOALS file and contact me about providing
377 submitions or help for the project.
378
379 =head1 BUGS
380
381 numgrep will round decimal numbers with more than 15 digits of accuracy. This is
382 mostly due to limit's in the way programming languages deal directly with numbers.
383
384 =head1 MORE INFO
385
386 More info on numgrep can be found at:
387
388 =over 1
389
390 =item B<http://suso.suso.org/programs/num-utils/>
391
392 =back
393
394 =cut
395