2017-05-14 16:57:25 +00:00
|
|
|
#! /usr/bin/env perl
|
|
|
|
# -*- mode: perl; -*-
|
|
|
|
#
|
|
|
|
# file searchPatterns.pl
|
|
|
|
# Uses patterns-file to consecutively process given tex-file
|
|
|
|
# Command succedes if each pattern matches the file content in given order
|
|
|
|
#
|
|
|
|
# How to use:
|
|
|
|
#
|
|
|
|
# searchPatterns.pl patterns=<name of file with patterns> log=<name of file to check against>
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
|
|
|
|
sub sexit($); # Print synax and exit
|
|
|
|
sub readPatterns($); # Process patterns file
|
2017-05-16 07:16:18 +00:00
|
|
|
sub processLogFile($); #
|
|
|
|
sub convertPattern($); # check for regex, comment
|
|
|
|
sub convertSimplePattern($); # escape some chars, (e.g. ']' ==> '\]')
|
2017-05-14 16:57:25 +00:00
|
|
|
|
|
|
|
my %options = (
|
|
|
|
"log" => undef,
|
|
|
|
"patterns" => undef,
|
|
|
|
);
|
|
|
|
|
|
|
|
my @patterns = ();
|
|
|
|
|
|
|
|
for my $arg (@ARGV) {
|
|
|
|
if ($arg eq "-help") {
|
|
|
|
&sexit(0);
|
|
|
|
}
|
|
|
|
if ($arg =~ /^([^=]+)=(.+)$/) {
|
|
|
|
my ($what, $val) = ($1, $2);
|
|
|
|
if (exists($options{$what})) {
|
|
|
|
if (defined($options{$what})) {
|
|
|
|
print "Value for \"$what\" already defined\n";
|
|
|
|
&sexit(1);
|
|
|
|
}
|
|
|
|
$options{$what} = $val;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Unknown param \"$what\"\n";
|
|
|
|
&sexit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Wrong param syntax for \"$arg\"\n";
|
|
|
|
&sexit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for my $k (keys %options) {
|
|
|
|
if (! defined($options{$k})) {
|
|
|
|
&sexit(1);
|
|
|
|
}
|
|
|
|
if (! -r $options{$k}) {
|
|
|
|
print "File \"$options{$k}\" is not readable\n";
|
|
|
|
&sexit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Read patterns
|
|
|
|
&readPatterns($options{"patterns"});
|
|
|
|
if (&processLogFile($options{"log"}) > 0) {
|
|
|
|
print "Errors occured, exiting\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
sub syntax()
|
|
|
|
{
|
|
|
|
print "Syntax:\n";
|
|
|
|
print " $0";
|
|
|
|
for my $k (keys %options) {
|
|
|
|
print " $k=<filename>";
|
|
|
|
}
|
|
|
|
print "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
sub sexit($)
|
|
|
|
{
|
|
|
|
my ($exval) = @_;
|
|
|
|
&syntax();
|
|
|
|
exit($exval);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub convertPattern($)
|
2017-05-16 07:16:18 +00:00
|
|
|
{
|
|
|
|
my ($pat) = @_;
|
|
|
|
if ($pat eq "") {
|
|
|
|
return("");
|
|
|
|
}
|
|
|
|
return $pat if ($pat =~ /^Comment:/);
|
|
|
|
if ($pat =~ s/^Regex:\s+//) {
|
|
|
|
# PassThrough variant
|
|
|
|
return($pat);
|
|
|
|
}
|
|
|
|
elsif ($pat =~ s/^Simple:\s+//) {
|
|
|
|
return convertSimplePattern($pat);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
# This should not happen.
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub convertSimplePattern($)
|
2017-05-14 16:57:25 +00:00
|
|
|
{
|
|
|
|
# Convert all chars '[]()+'
|
|
|
|
my ($pat) = @_;
|
|
|
|
if ($pat eq "") {
|
|
|
|
return("");
|
|
|
|
}
|
2017-05-16 07:16:18 +00:00
|
|
|
if ($pat =~ /^(.*)(\\n)(.*)$/) {
|
|
|
|
# do not convert '\n'
|
2017-05-14 16:57:25 +00:00
|
|
|
my ($first, $found, $third) = ($1, $2, $3);
|
2017-05-16 07:16:18 +00:00
|
|
|
$first = &convertSimplePattern($first);
|
|
|
|
$third = &convertSimplePattern($third);
|
|
|
|
return("$first$found$third");
|
|
|
|
}
|
|
|
|
if ($pat =~ /^(.*)([\[\]\(\)\+\^\{\}\\])(.*)$/) {
|
|
|
|
my ($first, $found, $third) = ($1, $2, $3);
|
|
|
|
$first = &convertSimplePattern($first);
|
|
|
|
$third = &convertSimplePattern($third);
|
2017-05-14 16:57:25 +00:00
|
|
|
return($first . "\\$found" . $third);
|
|
|
|
}
|
|
|
|
# Substitue white spaces
|
|
|
|
while ($pat =~ s/[\s]+/\\s\+/) {};
|
|
|
|
return($pat);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub readPatterns($)
|
|
|
|
{
|
|
|
|
my ($patfile) = @_;
|
|
|
|
|
2017-05-16 07:16:18 +00:00
|
|
|
my $errors = 0;
|
2017-05-14 16:57:25 +00:00
|
|
|
if (open(FP, $patfile)) {
|
2017-05-16 07:16:18 +00:00
|
|
|
my $line = 0;
|
2017-05-14 16:57:25 +00:00
|
|
|
while (my $p = <FP>) {
|
2017-05-16 07:16:18 +00:00
|
|
|
$line++;
|
2017-05-14 16:57:25 +00:00
|
|
|
chomp($p);
|
|
|
|
$p = &convertPattern($p);
|
2017-05-16 07:16:18 +00:00
|
|
|
if (defined($p)) {
|
|
|
|
push(@patterns, $p) if ($p ne "");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print "Wrong entry in patterns-file at line $line\n";
|
|
|
|
$errors++;
|
|
|
|
}
|
2017-05-14 16:57:25 +00:00
|
|
|
}
|
|
|
|
close(FP);
|
|
|
|
}
|
2017-05-16 07:16:18 +00:00
|
|
|
if ($errors > 0) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-14 16:57:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sub processLogFile($)
|
|
|
|
{
|
|
|
|
my ($log) = @_;
|
|
|
|
my $found;
|
|
|
|
my $errors = 1;
|
|
|
|
my @savedlines = ();
|
|
|
|
my $readsavedlines = 0;
|
|
|
|
my $savedline;
|
2017-05-16 07:16:18 +00:00
|
|
|
my $comment = "";
|
2017-05-14 16:57:25 +00:00
|
|
|
if (open(FL, $log)) {
|
|
|
|
$errors = 0;
|
|
|
|
my $line = 0;
|
|
|
|
for my $pat (@patterns) {
|
2017-05-16 07:16:18 +00:00
|
|
|
if ($pat =~ /^Comment:\s*(.*)$/) {
|
|
|
|
$comment = $1;
|
|
|
|
$comment =~ s/\s+$//;
|
|
|
|
if ($comment ne "") {
|
|
|
|
print "............ $comment ..........\n";
|
|
|
|
}
|
|
|
|
next;
|
|
|
|
}
|
2017-05-14 16:57:25 +00:00
|
|
|
#print "Searching for \"$pat\"\n";
|
|
|
|
$found = 0;
|
2017-05-16 14:45:18 +00:00
|
|
|
my $prevlines = () = $pat =~ /\\n/g; # Number of lines in pattern
|
|
|
|
my @prevl = ();
|
|
|
|
for (my $i = 0; $i <= $prevlines; $i++) {
|
|
|
|
push(@prevl, "\n");
|
|
|
|
}
|
2017-05-14 16:57:25 +00:00
|
|
|
my @lines = ();
|
|
|
|
if ($readsavedlines) {
|
|
|
|
# Last regex not found
|
|
|
|
@lines = @savedlines;
|
|
|
|
@savedlines = ();
|
|
|
|
$line = $savedline;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$savedline = $line;
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
my $l;
|
|
|
|
if ($readsavedlines) {
|
|
|
|
$l = shift(@lines);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$l = <FL>;
|
|
|
|
}
|
|
|
|
last if (! $l);
|
2017-05-16 14:45:18 +00:00
|
|
|
for (my $i = 0; $i < $prevlines; $i++) {
|
|
|
|
$prevl[$i] = $prevl[$i+1];
|
|
|
|
}
|
|
|
|
$prevl[$prevlines] = $l;
|
|
|
|
my $check = join("", @prevl);
|
2017-05-14 16:57:25 +00:00
|
|
|
$line++;
|
|
|
|
if ($check =~ /$pat/) {
|
2017-05-16 14:45:18 +00:00
|
|
|
my $fline = $line - $prevlines;
|
|
|
|
print "$fline:\tfound \"$pat\"\n";
|
2017-05-14 16:57:25 +00:00
|
|
|
$found = 1;
|
2017-05-16 14:45:18 +00:00
|
|
|
# Do not search in already found area
|
|
|
|
for (my $i = 0; $i <= $prevlines; $i++) {
|
|
|
|
$prevl[$i] = "\n";
|
|
|
|
}
|
2017-05-14 16:57:25 +00:00
|
|
|
if ($readsavedlines) {
|
|
|
|
@savedlines = @lines;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
@savedlines = ();
|
|
|
|
}
|
|
|
|
$savedline = $line;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push(@savedlines, $l);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (! $found) {
|
|
|
|
$errors++;
|
|
|
|
print "\tNOT found \"$pat\" in remainder of file\n";
|
|
|
|
$readsavedlines = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(FL);
|
|
|
|
}
|
|
|
|
return($errors);
|
|
|
|
}
|