#!/usr/bin/perl # Copyright (C) 2012 Trizen . # # 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 . # #------------------------------------------------------- # Appname: marif # Created on: 25 January 2012 # Latest edit on: 25 January 2012 # Website: http://trizen.googlecode.com #------------------------------------------------------- use strict; my ($tree, $regexp, $last, $verbose, $exit); foreach my $arg (@ARGV) { if ($arg =~ /^-r=(.+)/) { $regexp = qr/$1/; print "* Using regexp: $regexp\n\n"; } elsif ($arg eq '-t') { $tree = 1; } elsif ($arg eq '-l') { $last = 1; } elsif ($arg eq '-v') { $verbose = 1; } elsif ($arg eq '-e') { $exit = 1; } elsif ($arg eq '-s') { undef $/; } } sub usage { print <<"USAGE"; usage: $0 [options] Options: -t : search in all files from a path -l : exit file after the first match -e : exit program after the first match -s : slurp the entire file into memory -r= : define a regex to find something in a file for insensitive mode, use: (?^i:regex) Others: -v : verbose mode\n USAGE exit 0; } usage() unless defined $regexp; sub open_and_search { open my $fh, '<', $_[0] or return; print "* Searching: $_[0]\n" if $verbose; 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: $_[0] * Line num: $. * Found on: $line EOT exit 0 if $exit; last if $last; } } close $fh; } if ($tree) { require File::Find; my %files; foreach my $file (@ARGV) { if (-d $file) { File::Find::find( sub { if (-T) { open_and_search($File::Find::name); } }, $file ); } else { open_and_search($file); } } } else { foreach my $file (grep { -T } @ARGV) { open_and_search($file); } } exit 0;