-
Notifications
You must be signed in to change notification settings - Fork 33
/
marif
executable file
·133 lines (115 loc) · 3.11 KB
/
marif
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
#!/usr/bin/perl
# Copyright (C) 2012 Daniel "Trizen" Șuteu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#-------------------------------------------------------
# Appname: marif
# Created on: 25 January 2012
# Latest edit on: 13 November 2012
# https://github.com/trizen
#-------------------------------------------------------
use 5.010;
use utf8;
use strict;
use warnings;
use open IO => ':utf8';
use Getopt::Std qw(getopts);
my %opts;
getopts('tlvesr:h', \%opts);
my $tree = $opts{t};
my $last = $opts{l};
my $verbose = $opts{v};
my $exit = $opts{e};
my $slurp = $opts{s};
my $regexp = $opts{r};
sub usage {
print <<"USAGE";
usage: $0 [options] <dir|files>
Options:
-t : search in all files from a path
-l : close file after the first match
-e : exit program after the first match
-s : slurp the entire file into memory
-r <regex> : define a regex to find something in a file
for case-insensitive mode, use: (?^i:regex)
Others:
-v : verbose mode\n
USAGE
exit shift;
}
if ($opts{h}) {
usage(0);
}
elsif (not defined $regexp) {
usage(1);
}
utf8::decode($regexp);
sub open_and_search {
my ($file) = @_;
local $/ = $slurp ? undef : "\n";
open my $fh, '<:encoding(UTF-8)', $file or return;
say ">Searching: $file" if $verbose;
local $SIG{__WARN__} = sub { return };
while (defined(my $line = <$fh>)) {
if ($line =~ /($regexp)/o) {
substr($line, $-[0], 0, "\e[1;31m");
substr($line, $+[0] + 7, 0, "\e[0m");
print <<"EOT";
* Filename: $file
* Line num: $.
* Found on: $line
EOT
exit 0 if $exit;
last if $last;
}
}
return close $fh;
}
if ($tree) {
require File::Find;
foreach my $file (@ARGV) {
if (-d $file) {
File::Find::find(
{
no_chdir => 1,
wanted => sub {
if (-f -T and not /\.pdf\z/i) {
open_and_search($_);
}
},
} => $file
);
}
else {
open_and_search($file);
}
}
}
else {
foreach my $file (@ARGV) {
if (-f $file) {
if (-T _) {
open_and_search($file);
}
else {
warn "[!] Not a text file: $file\n";
}
}
else {
warn "[!] Not a file: $file\n";
}
}
}
exit 0;