Sprache:

Poor Window's man grep in Perl

Sprache: English
Programmiersprache: Perl
Veröffentlicht von: Laufer
Letzte Änderung: 06.09.2007
Aufrufe: 1

Lizenz: BSD-Lizenz

Beschreibung

This snippet is actually a tool I'm using when crawling through the Microsoft (and other) include files. You need ActivePerl 5.8.x installed on your machine in order to run it.

It needs two parameters, search path from where it will start recursive hunt and second parameter string in form of Perl regular expression, i.e. what are you looking for. You'll see one example in the very top of it what I'm sometimes doing :)

Oh, yes, the third parameter, if you specify, will filter out uninteresting files.
For example, in order to search C:\Program Files for line beginning with #define, but only in c and cpp files (and ATL macros) you'll key in the following:

rf "C:\Program Files" "(?:^\s*\#define)" "(?:\.c|\.cpp\.h|\.hpp|\.inc|\.cxx|\.hxx)$"

BTW, you can always download CygWin or UnxUtils, but we need more control, right? :)))

Code

1 #!c:/perl/bin/perl.exe 2 # 3 # (C)2004 Vladimir Kraljevic 4 # 5 6 =tuc 7 rf "C:\Program Files\Microsoft SDK\include" MIDL_INTERFACE\(\"([[:xdigit:]]{8}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{4}-[[:xdigit:]]{12})\"\)\s*[\r\n]+\s*([[:alpha:]][[:alnum:]]+)\s+ 8 =cut 9 10 use integer; 11 use warnings; 12 use bytes; 13 use Fcntl; 14 15 my $MAX_COLUMNS=80; 16 sub recurse($); 17 18 my $ARGC=scalar @ARGV; 19 die "Usage: $0 <path> <regex> [<file regex>]\n" if($ARGC < 2); 20 my ($dir)=$ARGV[0]; 21 my ($wrx)=$ARGV[1]; 22 my ($frx)=$ARGV[2] if($ARGC > 2); 23 my ($slurp)=($wrx=~/(\\r|\\n)/)?1:0; 24 my ($cols); #=scalar(split(/\(/, $wrx)) - scalar(split(/\\\(/, $wrx)); 25 sub cnt { $cols++; return shift; } 26 $wrx=~s/([^\\]\()/cnt($1)/ge; 27 $cols=1 unless($cols); 28 29 die "Directory \"$dir\" does not exist\n" if(!defined($dir) || (!-d $dir)); 30 $dir=substr($dir, 0, length($dir)-1) if(substr($dir, length($dir)-1, 1) eq '\\'); 31 recurse($dir); 32 33 sub binaryrf($) 34 { 35 my ($file, $hfile)=shift; 36 unless(sysopen($hfile, $file, O_RDONLY | O_BINARY)) { 37 print STDERR "*** skipping file \"$file\", can't open it because $!\n"; 38 return; 39 } 40 my ($buffer, $temp); 41 while(sysread($hfile, $temp, 4096)) { $buffer.=$temp; } 42 close($hfile); 43 my (@matches); 44 return unless((@matches)=($buffer=~/$wrx/g)); 45 print STDERR "\"$file\"\n"; 46 while(scalar(@matches) != 0) { 47 print join(';', splice(@matches, 0, $cols))."\n"; 48 } 49 close($hfile); 50 } 51 52 sub textrf($) 53 { 54 my ($file, $hfile)=shift; 55 unless(open($hfile, '<', $file)) { 56 print STDERR "*** skipping file \"$file\", can't open it because $!\n"; 57 return; 58 } 59 my ($out); 60 while(<$hfile>) { 61 next unless(/$wrx/); 62 print STDERR "\"$file\"\n" unless($out++); 63 $_=~s/^\s+//; 64 $_=~s/\s+$//; 65 $_=substr($_, 0, $MAX_COLUMNS-13) if(length($_) > $MAX_COLUMNS-13); 66 printf "%-11u:%s\n", $., $_; 67 } 68 close($hfile); 69 } 70 71 sub recurse($) 72 { 73 my ($dir, $hdir)=shift; 74 opendir($hdir, $dir) || die "Can't open dir \"$dir\" because $!\n"; 75 my (@files)=readdir($hdir); 76 closedir($hdir); 77 foreach(sort { uc($a) cmp uc($b) } @files) { 78 my ($file)="$dir\\$_"; 79 80 if(-d $file) { 81 next if(/^\.{1,2}$/); 82 recurse($file); 83 next; 84 } 85 86 next if($frx && $_ !~ /$frx/); 87 88 if($slurp || !(-T $file)) { 89 binaryrf($file); 90 } else { 91 textrf($file); 92 } 93 94 } 95 } 96 97

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS