/usr/bin/pegrep is in hxtools 20170430-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #!/usr/bin/perl
use strict;
use warnings;
our($grepcolor, $filecolor, $coloncolor, $linenocolor, $stopcolor) = ($ENV{"GREP_COLOR"});
&main();
sub goback
{
my($s, $start) = @_;
if ($start == 0) {
return 0;
}
my $of = rindex($$s, "\n", $start - 1);
if ($of <= 0) {
return 0;
}
$of = rindex($$s, "\n", $of - 1);
return ($of <= 0) ? 0 : $of;
}
sub gofwd
{
my($s, $start) = @_;
my $max = length($$s);
if ($start >= $max) {
return $max;
}
my $of = index($$s, "\n", $start);
if ($of < 0 || $of >= $max) {
return $max;
}
$of = index($$s, "\n", $of + 1);
return ($of < 0 || $of >= $max) ? $max : $of;
}
sub nlcount
{
my $tmp = shift @_;
return $tmp =~ tr/\n//;
}
sub main
{
my $pattern = shift @ARGV;
if (length($grepcolor) > 0) {
$grepcolor = "\x1b\x5b${grepcolor}m";
$filecolor = "\x1b\x5b35m";
$coloncolor = "\x1b\x5b1;30m";
$linenocolor = "\x1b\x5b36m";
$stopcolor = "\x1b\x5b0m";
}
foreach my $file (@ARGV) {
&dofile($file, $pattern);
}
}
sub dofile
{
my($file, $pattern) = @_;
local *FH;
open(FH, "< $file");
my @lines = <FH>;
close FH;
my $data = join("", @lines);
while ($data =~ m{$pattern}gs) {
my $mainofs = $-[0];
my $postofs = $+[0];
my $mainlen = $postofs - $mainofs;
my $leadofs = &goback(\$data, $mainofs);
my $endofs = &gofwd(\$data, $postofs);
my $linestart = 1 + &nlcount(substr($data, 0, $mainofs));
my $lineend = $linestart + &nlcount(substr($data, $mainofs, $mainlen));
print "$filecolor$file$stopcolor";
print "$coloncolor:$stopcolor";
print "$linenocolor$linestart$stopcolor";
print "$coloncolor→$stopcolor";
print "$linenocolor$lineend$stopcolor";
print "$coloncolor:$stopcolor";
print substr($data, $leadofs, $mainofs - $leadofs);
# because less -R does not support color across \n
my $main = substr($data, $mainofs, $mainlen);
$main =~ s{\n}{$stopcolor\n$grepcolor}g;
print $grepcolor, $main, $stopcolor;
print substr($data, $postofs, $endofs - $postofs);
print "\n";
}
}
|