Debian Codegraph @

numgrep

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