#!/usr/bin/perl
use
Getopt::Std ; getopts
"b:e:f:gtqzA:B:E0125789"
,\
my
%o
;
use
Term::ANSIColor
qw[ color :constants ]
;
$Term::ANSIColor::AUTORESET
= 1 ;
eval
"use PerlIO::gzip;1"
or
die
"Can't import PerlIO::gzip despite -z instruction. ($Script)\n"
if
$o
{z} ;
sub
lineOut ( $ ) ;
sub
eachFile ( $ ) ;
sub
niceNumber ( $ ) ;
$| = 1 ;
my
@nums
;
$o
{b} //= 10 ;
$o
{e} //= 3 ;
$o
{f} //= 1 ;
$o
{A} //= 0 ;
$o
{B} //= 0 ;
& Init ;
& traverse ;
exit
0 ;
sub
Init ( ) {
sub
lineOutSub ( $ ) ;
sub
lineOutColon ( $ ) {
$_
[0]->[0],
":\t"
,
$_
[0]->[1] }
sub
lineOutG ( $ ) { GREEN
$_
[0]->[0] , RESET
"\t"
,
$_
[0]->[1] }
sub
lineOutQ ( $ ) {
$_
[0]->[1] }
sub
lineOutTime ( $ ) {
sprintf
(
"%02d:%02d:%02d\t"
, @{[
localtime
]}[2,1,0]), lineOutSub
$_
[0] }
* lineOutSub =
$o
{g} ? * lineOutG :
$o
{
q} ? * lineOutQ : * lineOutColon ;
* lineOut = $o{t}
?
*lineOutTime
: * lineOutSub ;
select
* STDERR
if
$o
{E} ;
@nums
= (1,2,5) ;
@nums
= (
"inf"
)
if
$o
{0} ;
@nums
= (1)
if
$o
{1} ;
@nums
= (1,2,4,8)
if
$o
{2} ;
@nums
= (1,2,5)
if
$o
{5} ;
@nums
= (1,1.5,2,3,5,7)
if
$o
{7} ;
@nums
= (1,1.5,2,3,5,8)
if
$o
{8} ;
@nums
= (1..9)
if
$o
{9} ;
@nums
=
grep
{
$_
<
$o
{b} }
@nums
;
}
sub
traverse ( ) {
while
( 1 ) {
my
$fn
=
shift
@ARGV
;
my
$fh
;
if
(
defined
$fn
) {
open
$fh
,
"<"
,
$fn
or
warn
"File `$fn' does not open."
and
next
;
}
else
{
$fh
=
*STDIN
;
}
binmode
$fh
,
":gzip(autopop)"
if
$o
{z} ;
eachFile
$fh
;
close
$fh
;
last
if
!
@ARGV
;
print
"\n"
;
}
}
sub
eachFile ( $ ) {
my
$fh
=
$_
[0] ;
my
$rd
;
my
@stockLines
;
if
(
$o
{e} >= 1 ) {
while
( <
$fh
> ) {
print
STDOUT
$_
if
$o
{E} ;
push
@stockLines
, [ $. ,
$_
] ;
last
if
$. >=
$o
{e} ;
}
}
print
lineOut
$_
for
@stockLines
;
while
( <
$fh
> ) {
print
STDOUT
$_
if
$o
{E} ;
push
@stockLines
, [ $. ,
$_
] ;
my
$lf
=
shift
@stockLines
;
$rd
=
$o
{A}+1
if
niceNumber
$lf
->[0] ;
print
lineOut
$lf
if
$rd
-- > 0 &&
$lf
->[0] >
$o
{e} ;
}
print
lineOut
$_
for
@stockLines
;
}
sub
niceNumber ( $ ) {
my
$head
=
shift
@nums
;
while
(
$head
<
$_
[0] ) {
push
@nums
,
$head
*
$o
{b} ;
$head
=
shift
@nums
}
if
(
$head
- 1 <
$_
[0] ) {
push
@nums
,
$head
*
$o
{b} ;
return
$_
[0] >=
$o
{f} ? 1 : 0 ;
}
unshift
@nums
,
$head
;
return
1
if
$_
[0] >=
$head
-
$o
{B} &&
$_
[0] <=
$head
+
$o
{A} ;
return
0 ;
}
sub
VERSION_MESSAGE {}
sub
HELP_MESSAGE {
$ARGV
[1] //=
''
;
open
my
$FH
,
'<'
, $0 ;
while
(<
$FH
>){
s/\$0/
$Script
/g ;
print
$_
if
s/^=head1// .. s/^=cut// and
$ARGV
[1] =~ /^o(p(t(i(o(ns?)?)?)?)?)?$/i ? m/^\s+\-/ : 1;
}
close
$FH
;
exit
0 ;
}