4 # file searchPatterns.pl
5 # Uses patterns-file to consecutively process given tex-file
6 # Command succedes if each pattern matches the file content in given order
10 # searchPatterns.pl patterns=<name of file with patterns> log=<name of file to check against>
15 sub sexit($); # Print synax and exit
16 sub readPatterns($); # Process patterns file
17 sub processLogFile($);
18 sub convertPattern($); # escape some chars, (e.g. ']' ==> '\]')
28 if ($arg eq "-help") {
31 if ($arg =~ /^([^=]+)=(.+)$/) {
32 my ($what, $val) = ($1, $2);
33 if (exists($options{$what})) {
34 if (defined($options{$what})) {
35 print "Value for \"$what\" already defined\n";
38 $options{$what} = $val;
41 print "Unknown param \"$what\"\n";
46 print "Wrong param syntax for \"$arg\"\n";
51 for my $k (keys %options) {
52 if (! defined($options{$k})) {
55 if (! -r $options{$k}) {
56 print "File \"$options{$k}\" is not readable\n";
62 &readPatterns($options{"patterns"});
63 if (&processLogFile($options{"log"}) > 0) {
64 print "Errors occured, exiting\n";
74 for my $k (keys %options) {
75 print " $k=<filename>";
89 # Convert all chars '[]()+'
94 if ($pat =~ /^(.*)([\[\]\(\)\+\^\{\}])(.*)$/) {
95 my ($first, $found, $third) = ($1, $2, $3);
96 $first = &convertPattern($first);
97 $third = &convertPattern($third);
98 return($first . "\\$found" . $third);
100 # Substitue white spaces
101 while ($pat =~ s/[\s]+/\\s\+/) {};
109 if (open(FP, $patfile)) {
110 while (my $p = <FP>) {
112 $p = &convertPattern($p);
119 sub processLogFile($)
127 my $readsavedlines = 0;
129 if (open(FL, $log)) {
132 for my $pat (@patterns) {
133 #print "Searching for \"$pat\"\n";
136 if ($readsavedlines) {
137 # Last regex not found
138 @lines = @savedlines;
147 if ($readsavedlines) {
154 my $check = $prevl . $l;
157 if ($check =~ /$pat/) {
158 print "$line:\tfound \"$pat\"\n";
160 $prevl = "\n"; # Don't search this line again
161 if ($readsavedlines) {
162 @savedlines = @lines;
171 push(@savedlines, $l);
176 print "\tNOT found \"$pat\" in remainder of file\n";