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($); # check for regex, comment
19 sub convertSimplePattern($); # escape some chars, (e.g. ']' ==> '\]')
29 if ($arg eq "-help") {
32 if ($arg =~ /^([^=]+)=(.+)$/) {
33 my ($what, $val) = ($1, $2);
34 if (exists($options{$what})) {
35 if (defined($options{$what})) {
36 print "Value for \"$what\" already defined\n";
39 $options{$what} = $val;
42 print "Unknown param \"$what\"\n";
47 print "Wrong param syntax for \"$arg\"\n";
52 for my $k (keys %options) {
53 if (! defined($options{$k})) {
56 if (! -r $options{$k}) {
57 print "File \"$options{$k}\" is not readable\n";
63 &readPatterns($options{"patterns"});
64 if (&processLogFile($options{"log"}) > 0) {
65 print "Errors occured, exiting\n";
75 for my $k (keys %options) {
76 print " $k=<filename>";
94 return $pat if ($pat =~ /^Comment:/);
95 if ($pat =~ s/^Regex:\s+//) {
99 elsif ($pat =~ s/^Simple:\s+//) {
100 return convertSimplePattern($pat);
103 # This should not happen.
108 sub convertSimplePattern($)
110 # Convert all chars '[]()+'
115 if ($pat =~ /^(.*)(\\n)(.*)$/) {
116 # do not convert '\n'
117 my ($first, $found, $third) = ($1, $2, $3);
118 $first = &convertSimplePattern($first);
119 $third = &convertSimplePattern($third);
120 return("$first$found$third");
122 if ($pat =~ /^(.*)([\[\]\(\)\+\^\{\}\\])(.*)$/) {
123 my ($first, $found, $third) = ($1, $2, $3);
124 $first = &convertSimplePattern($first);
125 $third = &convertSimplePattern($third);
126 return($first . "\\$found" . $third);
128 # Substitue white spaces
129 while ($pat =~ s/[\s]+/\\s\+/) {};
138 if (open(FP, $patfile)) {
140 while (my $p = <FP>) {
143 $p = &convertPattern($p);
145 push(@patterns, $p) if ($p ne "");
148 print "Wrong entry in patterns-file at line $line\n";
159 sub processLogFile($)
167 my $readsavedlines = 0;
170 if (open(FL, $log)) {
173 for my $pat (@patterns) {
174 if ($pat =~ /^Comment:\s*(.*)$/) {
176 $comment =~ s/\s+$//;
177 if ($comment ne "") {
178 print "............ $comment ..........\n";
182 #print "Searching for \"$pat\"\n";
185 if ($readsavedlines) {
186 # Last regex not found
187 @lines = @savedlines;
196 if ($readsavedlines) {
203 my $check = $prevl . $l;
206 if ($check =~ /$pat/) {
207 print "$line:\tfound \"$pat\"\n";
209 $prevl = "\n"; # Don't search this line again
210 if ($readsavedlines) {
211 @savedlines = @lines;
220 push(@savedlines, $l);
225 print "\tNOT found \"$pat\" in remainder of file\n";