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($)
165 my $readsavedlines = 0;
168 if (open(FL, $log)) {
171 for my $pat (@patterns) {
172 if ($pat =~ /^Comment:\s*(.*)$/) {
174 $comment =~ s/\s+$//;
175 if ($comment ne "") {
176 print "............ $comment ..........\n";
180 #print "Searching for \"$pat\"\n";
182 my $prevlines = () = $pat =~ /\\n/g; # Number of lines in pattern
184 for (my $i = 0; $i <= $prevlines; $i++) {
188 if ($readsavedlines) {
189 # Last regex not found
190 @lines = @savedlines;
199 if ($readsavedlines) {
206 for (my $i = 0; $i < $prevlines; $i++) {
207 $prevl[$i] = $prevl[$i+1];
209 $prevl[$prevlines] = $l;
210 my $check = join("", @prevl);
212 if ($check =~ /$pat/) {
213 my $fline = $line - $prevlines;
214 print "$fline:\tfound \"$pat\"\n";
216 # Do not search in already found area
217 for (my $i = 0; $i <= $prevlines; $i++) {
220 if ($readsavedlines) {
221 @savedlines = @lines;
230 push(@savedlines, $l);
235 print "\tNOT found \"$pat\" in remainder of file\n";