Debian Codegraph @

numgrep

raw ◀ ▶
Source reconstructed at patch state 0 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 $exp = "$number =~ /$expression/";
225 }
226 print "Exp: '$exp'\n" if ($verbose >= 3);
227
228 return $exp
229 }
230
231 sub parse_number {
232 my $substring = shift;
233 my $placeholder = shift;
234
235 $substring =~ /^(-?[0-9]*\.?[0-9]*)[^0-9]/;
236 my $number = $1;
237
238 my $strlen;
239
240 if ($number =~ /^[\.-]*$/) {
241 $strlen = length($number);
242 $number = "";
243 } else {
244 $strlen = length($number);
245 }
246
247 # Fix the number somewhat.
248 #$number =~ s/^\./0./;
249 #$number =~ s/^-\./-0./;
250 #$number =~ s/-0+/0/;
251 #$number =~ s/\.$//;
252
253 chomp($number);
254
255 return ($placeholder + $strlen, $number);
256 }
257
258
259 # Lay down some of that perl pod action.
260 =pod
261
262 =head1 NAME
263
264 numgrep - This program is the numeric equivilent of the grep utility.
265
266
267 =head1 SYNOPSIS
268
269 B<numgrep> [-dhlV] <FILE>
270
271 | B<numgrep> [-dhlV] (Input on STDIN from pipeline.)
272
273 B<numgrep> [-dhlV] (Input on STDIN. Use Ctrl-D to stop.)
274
275 =head1 DESCRIPTION
276
277 B<numgrep> searches for different occurances of numbers through the use
278 of numeric expressions.
279
280 =head1 OPTIONS
281
282 -l Print the matching numbers out one per line
283 instead of printing the entire line they are on.
284
285 -h Help: You're looking at it.
286 -V Increase verbosity.
287 -d Debug mode. For developers
288
289 =head1 EXPRESSIONS
290
291 B<numgrep> uses a special numeric expression matching system. Basically,
292 it searches for ranges, factors and sequences of numbers. Here is a list
293 of the syntax characters and some sample expressions that will get you
294 going:
295
296
297 /<expression>/
298 Put your expression or set of expressions between these
299 two forward slashes.
300
301 .. Range expression. A number must be used on the left
302 and/or right of this expression to specify that numbers
303 between, greater than or less than the numbers specified
304 should be matched.
305
306 , Expression sepeartor. The comma sepearates one complete
307 expression from another in a set enclosed by //.
308
309 m<n> Multiples of <n>. This operator, followed by a number
310 <n> will match any number <x> that is an integer
311 multiple of <n>. Meaning that <x> = <n> times <y>,
312 where <y> is any integer.
313
314 f<n> Factors of <n>. This operator, followed by a number <n>
315 will match any number <x> that is an integer factor of
316 <n>. Meaning that <x> = <n> divided by <y>, where <y>
317 is any integer.
318
319 B< NOTE:> Checking for factors and multiples is very fast because it
320 is checked by doing a single modulus operation on two numbers.
321
322 B<Examples:>
323
324 /2..10/ Match any number between 2 and 10.
325
326 /2..10,20..30/ Match any number between 2 and 10 or between 20 and 30.
327
328 /56,34,512,45,67/ Match any of the numbers 56, 34, 512, 45 or 67.
329
330 /m3/ Match any integer that is a multiple of 3.
331
332 /f1024/ Match any integer that is a factor of 1024.
333
334
335 $ range -N /1..1000/ | numgrep /f1024/
336 1
337 2
338 4
339 8
340 16
341 32
342 64
343 128
344 256
345 512
346 $
347
348
349 =head1 BUGS
350
351 B<numgrep> can't handle certain situations properly. Such as if
352 it encounters a number with leading zeros, it will treat it as an
353 octal number and thus might not match the way you would expect.
354
355 B<numgrep> does not yet allow you to mix numbers and text in the
356 matching expression. So you can not do something like
357 'numgrep /port=0..1023/ firewall.log'. But this will be changed in the
358 future.
359
360 =head1 SEE ALSO
361
362 average(1), bound(1), interval(1), normalize(1), numprocess(1), numsum(1), random(1), range(1), round(1)
363
364 =head1 COPYRIGHT
365
366 numgrep is part of the num-utils package, which is copyrighted by
367 Suso Banderas and released under the GPL license. Please read
368 the COPYING and LICENSE files that came with the num-utils package
369
370 Developers can read the GOALS file and contact me about providing
371 submitions or help for the project.
372
373 =head1 BUGS
374
375 numgrep will round decimal numbers with more than 15 digits of accuracy. This is
376 mostly due to limit's in the way programming languages deal directly with numbers.
377
378 =head1 MORE INFO
379
380 More info on numgrep can be found at:
381
382 =over 1
383
384 =item B<http://suso.suso.org/programs/num-utils/>
385
386 =back
387
388 =cut
389