File: fpg

package info (click to toggle)
fex 20160919-2
  • links: PTS, VCS
  • area: non-free
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,944 kB
  • sloc: perl: 17,699; sh: 346; makefile: 67
file content (350 lines) | stat: -rwxr-xr-x 9,205 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/perl -w
#
# Programname:			fpg - Frams' Perl grep
# Author: 			framstag@rus.uni-stuttgart.de
# Licence:			Perl Artistic
#
# History:
#   2003-02-27 Framstag		initial version
#   2003-02-28 Framstag		added exit status
#   2007-03-09 Framstag		added option -Q
#   2007-06-01 Framstag		added options -s and -c
#                               and changed default output mode
#   2007-06-03 Framstag		added ReadLine-support
#   2007-08-31 Framstag		added option -x
#   2008-02-06 Framstag		added implicit gunzip
#				-F ==> -R, new -F option
#   2008-10-07 Framstag		added option -p
#                               -n ==> -S, new -n option
#   2008-10-14 Framstag		added option -M
#   2008-11-23 Framstag		added option -~
#   2016-06-12 Framstag		option -o respects (match)

use Getopt::Std;
use Term::ReadLine;
use locale;

$0 =~ s:.*/::;
$| = 1;

$usage  = <<EOD;
usage: $0 [options] 'EXP' [file...]
   or: $0 [options] -Q file...
options: -r        recursively scan through directories
         -i        ignore case
	 -v        print only lines that do NOT match
	 -s        verbose scanning/searching
	 -n        prefix with line number
	 -l        list filenames only
	 -L       list filenames only that do NOT match
	 -p       show paragraphs, not lines (multiline record separator)
	 -o       show only matched strings (in parenthesis), not whole lines
	 -M       mail-mode: search and show complete mails from mbox files
	 -c       print (count) only number of matches (NOT LINES!)
	 -F       EXP is a string, not a Perl regular expression
	 -e        EXP is any perl code which returns TRUE/FALSE
	 -S \#      minimum string length \# for binary files, default: 4
	 -C \#      \# lines of context
	 -R 'RS'   record separator, default: newline (\\n) if not set -p
	 -x 'exp'  extra regexp for highlighting (not used for searching)
	 -X 'exp'  exclude files (filename matching this regexp) when searching
         -~        search in backup files *~ #*#, too
	 -Q        query-loop-prompt for search expression (with readline)
arguments: EXP     is a Perl regular expression
           file... can be one or more files, even binary or compressed ones
EOD
#examples: $0 -r 'from.*STDIN' *
#          $0 -e 'length>30 and not /\\w/' script
#See "perldoc perlre" for help on regular expressions.


$maxlen = 0;

$opt_i = $opt_r = $opt_v = $opt_l = $opt_h = $opt_e = $opt_n = $opt_o = 0;
$opt_s = $opt_c = $opt_Q = $opt_F = $opt_p = $opt_M = $opt_C = $opt_S = 0;
${'opt_~'} = 0;
$opt_S = 4;
$opt_x = $opt_X = '';
$opt_R = "\n";

getopts('hirvlLFMopscQen~S:R:C:x:X:') or die $usage;

if ($opt_h) {
  print $usage;
  exit;
}

unless ($opt_Q) {
  $exp = shift or die $usage;
}

if ($opt_C and ($opt_l or $opt_L or $opt_s or $opt_v or $opt_p or $opt_M)) {
  die "$0: cannot mix option -C with any of -l -L -s -v -p -M\n";
}

if ($opt_M and ($opt_l or $opt_L or $opt_s or $opt_v or $opt_p or $opt_C)) {
  die "$0: cannot mix option -M with any of -l -L -s -v -p -C\n";
}

if ($opt_o and ($opt_v or $opt_l or $opt_L or $opt_c or $opt_F or $opt_C)) {
  die "$0: cannot mix option -E with any of -l -L -v -c -C -F\n";
}

$opt_XX = 0;
if (not ${'opt_~'}) {
  @bfiles = grep(/~$|^#.*#$/,@ARGV);
  if (@bfiles and
      (grep(/[^~]$/,@ARGV) or grep(/(^|\/)#[^\/]*#$/,@ARGV))) {
    $opt_XX = 1;
    warn "$0: ignoring @bfiles\n"; # unless $opt_r;
  }
}

if (-t STDOUT) {
  $B = "\033[1m";
  $N = "\033[m";
} else {
  $B = $N = '';
}

if ($opt_p) { $/ = '' }
else        { $/ = $opt_R }
#else        { eval '$/ = "'.$opt_R.'"' }

$opt_h = 1 if not $opt_r and @ARGV < 2;

if ($opt_Q) {
  $q = new Term::ReadLine $0;
  $q->ornaments(0) unless $ENV{PERL_RL};
  for (;;) {
    $exp = $q->readline("$B\nsearch-expression:$N ");
    last unless $exp;
    &scan;
  }
} else {
  &scan;
}

exit ($found?0:1);

sub scan {
  $egrep = '';
  if ($opt_e) {
    eval "\$egrep =  sub { $exp }";
  } else {
    $exp =~ s/([\@\$\%\^\&\*\(\)\+\[\]\{\}\\\|\.\?])/\\$1/g if $opt_F;
    $exp = '(?i)'.$exp if $opt_i;
    $exp = '(?s)'.$exp if $opt_p or $opt_R ne "\n";
    #? $exp =~ s/\.\*\*/[.\n]*/g;
  }

  $found = 0;

  if (@ARGV) {
    foreach $file (@ARGV) {
      next if $opt_X  and $file =~ /$opt_X/;
      next if $opt_XX and ($file =~ /~$/ or $file =~ m{(^|/)#[^/]*#$});
      my $error = ''; open $file,$file or $error = $!; close $file;
      if ($error) {
        warn "$0: cannot read file $file - $error\n";
        next;
      }
      unless (-f $file or -d $file or -c $file or -S $file or -p $file) {
        warn "$0: ignoring special file $file\n";
        next;
      }
      $maxlen = length $file if $maxlen < length $file;
      # printf "%s\r",substr("scanning $file".(" " x 255),0,$maxlen+9) if -t STDOUT;
      # print  $B."scanning $file\n".$N if -t STDOUT and not $opt_l||$opt_L;
      if ($opt_r and -d $file) {
        $found += grepd($file);
        next;
      }
      # next if -z $file; # Achtung: special files unter /proc sind "empty" !
      # $type = `file -L $file`;
      # if ($type =~ /text/i and open F,$file or open F,"strings $file|") {
      $fileq = quotemeta $file;
      if (-T $file) {
        open $file,$file;
        # warn "$file\n";
      } else {
        if ($file =~ /\.bz2$/) {
          open $file,"bunzip2 <$fileq|";
          # warn "gunzip <$file|\n";
        } elsif ($file =~ /\.gz$/) {
          open $file,"gunzip <$fileq|";
          # warn "gunzip <$file|\n";
        } else {
          open $file,"strings -a -n $opt_S $fileq|";
          # warn "strings -n $opt_S $file|\n";
        }
      }
      if (fileno $file) {
        $found += grepf($file,$file);
        close $file;
      } else {
        warn "$0: cannot open $file - $!\n";
        next;
      }
    }
    # print " " x ($maxlen+9),"\r" if -t STDOUT;
  } else {
    $found = grepf(STDIN);
  }
}

sub grepd {
  my $dir = shift;
  my $file;
  my $found = 0;

  opendir $dir,$dir or return;
  while (defined($file = readdir $dir)) {
    next if $file eq '.' or $file eq '..';
    if (not ${'opt_~'} and $file =~ /~$|^#[^\/]*#$/) {
      # warn "$0: ignoring $dir/$file\n";
      next;
    }
    $file = "$dir/$file";
    next unless -r $file;
    if (-d $file and not -l $file) {
      $found += grepd($file);
      next;
    }
    next unless -f $file or -c $file or -S $file or -p $file or -z $file;
    $fileq = quotemeta $file;
    if (-T $file and open $file,$file or
        open $file,"strings -a -n $opt_S $fileq|") {
      $found += grepf($file,$file);
      close $file;
    }
  }
  closedir $dir;
  return $found;
}


sub grepf {
  my $F = shift;
  my $file = shift;
  my $found = 0;
  my ($n,$l,$c);

  warn $B."scanning $file".$N."\n" if -t STDOUT and $opt_s;

  while (<$F>) {
    $_ .= "\n" unless /\n$/;
    if ($opt_M) {
      if ($mail and (/^From / or eof $F)) {
        my $__ = $_;
        $_ = $mail;
        $mail = $__;
      } else {
        $mail .= $_;
        next;
      }
    }
    $l++;
    $n = 0;
    if ($opt_C) {
      for (my $i=$opt_C;$i;$i--) {
        $C{$i} = $C{$i-1} if defined $C{$i-1};
      }
      $C{0} = [$l,$_];
    }
    if ($opt_e) {
      if ($opt_v) {
        next if &$egrep;
      } else {
        unless (&$egrep) {
          if ($opt_C and $c) {
            print "$l:" if $opt_n;
            print;
            $L{$l} = $l;
            $c--;
          }
          next;
        }
      }
      $n++;
    } else {
      if ($opt_v) {
        # print ">>>$_" if $opt_i and /$exp/oi or /$exp/o;
        if ($opt_Q) {
          next if /$exp/m;
        } else {
          next if /$exp/om;
        }
        $n++;
      } else {
        if ($opt_c) {
          if ($opt_Q) { $n++ while /$exp/mg }
          else	      { $n++ while /$exp/omg }
        } else {
          if ($opt_o) {
            if ($exp =~ /\([^?]+\)/) {
              if (/$exp/) {
                $n++;
                $_ = "$1\n";
              }
            } else {
              my $m = '';
              while (s/($exp)//) {
                $n++;
                $m .= "$1\n";
              }
              $_ = $m;
            }
          } elsif ($opt_Q) {
            $n += s/($exp)/$B$1$N/mg;
          } else {
            $n += s/($exp)/$B$1$N/omg;
          }
        }
      }
    }
    unless ($n) {
      if ($opt_C and $c) {
        print "$l:" if $opt_n;
        print;
        $L{$l} = $l;
        $c--;
      }
      next;
    }
    $found += $n;
    # print " " x ($maxlen+9),"\r" if -t STDOUT and $found==1;
    next if $opt_c;
    last if $opt_l or $opt_L;
    if ($file and not $opt_s) {
      print "\n$B$file$N:\n";
      $file = '';
    }
    if ($opt_x and $n) {
      if ($opt_i) { s/($opt_x)/$B$1$N/ogi }
      else        { s/($opt_x)/$B$1$N/og }
    }
    for (my $i=$opt_C;$i;$i--) {
      if (defined $C{$i}) {
        my ($ln,$ls) = @{$C{$i}};
        unless (defined $L{$ln}) {
          $L{$ln} = $ln;
          print "$ln:" if $opt_n;
          print $ls;
        }
      }
    }
    print "$l:" if $opt_n;
    print;
    $L{$l} = $l;
    $c = $opt_C;
  }

  if ($opt_c) {
    print "$file:" if @ARGV>1;
    print "$found\n";
  } else {
    print "$file\n" if $opt_l and $found or $opt_L and not $found;
  }
  return $found;
}