michael@428: ## michael@428: ## lint-fsl.pl -- OpenPKG fsl.* File Checker michael@428: ## Copyright (c) 2000-2012 OpenPKG GmbH michael@428: ## michael@428: ## This software is property of the OpenPKG GmbH, DE MUC HRB 160208. michael@428: ## All rights reserved. Licenses which grant limited permission to use, michael@428: ## copy, modify and distribute this software are available from the michael@428: ## OpenPKG GmbH. michael@428: ## michael@428: ## THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED michael@428: ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF michael@428: ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@428: ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR michael@428: ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@428: ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@428: ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF michael@428: ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND michael@428: ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, michael@428: ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT michael@428: ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@428: ## SUCH DAMAGE. michael@428: ## michael@428: michael@428: # Perl run-time requirement michael@428: require 5; michael@428: BEGIN { michael@428: eval "use Getopt::Long; use IO;"; michael@428: if ($@) { michael@428: print STDERR michael@428: "lint-fsl: ERROR: This command requires a full-size Perl installation!\n" . michael@428: "lint-fsl: HINT: Install OpenPKG \"perl\" package to use this command.\n"; michael@428: exit(1); michael@428: } michael@428: } michael@428: michael@428: # OpenPKG instance prefix michael@428: my $my_prefix = $ENV{'OPENPKG_PREFIX'}; michael@428: delete $ENV{'OPENPKG_PREFIX'}; michael@428: michael@428: # program information michael@428: my $progname = "lint-fsl"; michael@428: my $progvers = "1.0.0"; michael@428: michael@428: # parameters (defaults) michael@428: my $version = 0; michael@428: my $verbose = 0; michael@428: my $help = 0; michael@428: my $check = 'all'; michael@428: my $tmpdir = ($ENV{TMPDIR} || $ENV{TEMPDIR} || "/tmp") . "/$progname"; michael@428: my $rpm = "$my_prefix/bin/openpkg rpm"; michael@428: michael@428: # exception handling support michael@428: $SIG{__DIE__} = sub { michael@428: my ($err) = @_; michael@428: $err =~ s|\s+at\s+.*||s if (not $verbose); michael@428: print STDERR "$progname:ERROR: $err ". ($! ? "($!)" : "") . "\n"; michael@428: exit(1); michael@428: }; michael@428: michael@428: # command line parsing michael@428: Getopt::Long::Configure("bundling"); michael@428: my $result = GetOptions( michael@428: 'V|version' => \$version, michael@428: 'v|verbose' => \$verbose, michael@428: 'h|help' => \$help, michael@428: 'c|check=s' => \$check, michael@428: 't|tmpdir=s' => \$tmpdir, michael@428: 'r|rpm=s' => \$rpm, michael@428: ) || die "option parsing failed"; michael@428: if ($help) { michael@428: print "Usage: $progname [options] [RPMFILE ...]\n" . michael@428: "Available options:\n" . michael@428: " -v,--verbose enable verbose run-time mode\n" . michael@428: " -h,--help print out this usage page\n" . michael@428: " -c,--check=CHECKS select checks to perform (default='all')\n" . michael@428: " -r,--rpm=FILE filesystem path to RPM program\n" . michael@428: " -t,--tmpdir=PATH filesystem path to temporary directory\n" . michael@428: " -V,--version print program version\n"; michael@428: exit(0); michael@428: } michael@428: if ($version) { michael@428: print "OpenPKG $progname $progvers\n"; michael@428: exit(0); michael@428: } michael@428: michael@428: # verbose message printing michael@428: sub msg_verbose { michael@428: my ($msg) = @_; michael@428: print STDERR "$msg\n" if ($verbose); michael@428: } michael@428: michael@428: # warning message printing michael@428: sub msg_warning { michael@428: my ($msg) = @_; michael@428: print STDERR "$progname:WARNING: $msg\n"; michael@428: } michael@428: michael@428: # error message printing michael@428: sub msg_error { michael@428: my ($msg) = @_; michael@428: print STDERR "$progname:ERROR: $msg\n"; michael@428: } michael@428: michael@428: # determine check list michael@428: my @check_list = (qw( michael@428: blank michael@428: comment michael@428: ident michael@428: )); michael@428: my @checks = (); michael@428: if ($check eq 'all') { michael@428: @checks = @check_list; michael@428: } michael@428: else { michael@428: foreach my $c (split(/,/, $check)) { michael@428: if (not grep(/^$c$/, @check_list)) { michael@428: die "invalid check \"$c\""; michael@428: } michael@428: push(@checks, $c); michael@428: } michael@428: } michael@428: michael@428: # global return code michael@428: $main::GRC = 0; michael@428: michael@428: # environment preparation michael@428: system("rm -rf $tmpdir"); michael@428: system("mkdir -p $tmpdir"); michael@428: michael@428: # iterate over all fsl. files michael@428: foreach my $filename (@ARGV) { michael@428: my $io = new IO::File "<$filename" michael@428: or die "unable to open file \"$filename\" for reading"; michael@428: my $spec; { local $/ = undef; $spec = <$io>; } michael@428: $io->close; michael@428: foreach my $check (@checks) { michael@428: eval "\&check_$check(\$filename, \$spec);"; michael@428: } michael@428: } michael@428: michael@428: # environment cleanup michael@428: system("rm -rf $tmpdir"); michael@428: michael@428: # die gracefully michael@428: exit($main::GRC); michael@428: michael@428: ## _________________________________________________________________ michael@428: ## michael@428: ## COMMON SUBROUTINES michael@428: ## _________________________________________________________________ michael@428: ## michael@428: michael@428: sub lines { michael@428: my ($txt) = @_; michael@428: my $l = 0; michael@428: $txt =~ s|\n|$l++, ''|sge; michael@428: return $l; michael@428: } michael@428: michael@428: sub lint_message { michael@428: my ($type, $file, $done, $this, $msg) = @_; michael@428: if (defined($done) and defined($this)) { michael@428: my $start = &lines($done) + 1; michael@428: my $end = $start + &lines($this); michael@428: my $pos = $start; michael@428: $pos .= "-". $end if ($end > $start); michael@428: printf("%s:%s: %s:%s: %s\n", $progname, $type, $file, $pos, $msg); michael@428: } michael@428: else { michael@428: printf("%s:%s: %s: %s\n", $progname, $type, $file, $msg); michael@428: } michael@428: } michael@428: michael@428: sub lint_warning { michael@428: my ($file, $done, $this, $msg) = @_; michael@428: &lint_message("WARNING", $file, $done, $this, $msg); michael@428: $main::GRC = 1 if ($main::GRC < 1); michael@428: } michael@428: michael@428: sub lint_error { michael@428: my ($file, $done, $this, $msg) = @_; michael@428: &lint_message("ERROR", $file, $done, $this, $msg); michael@428: $main::GRC = 2 if ($main::GRC < 2); michael@428: } michael@428: michael@428: ## _________________________________________________________________ michael@428: ## michael@428: ## CHECK "blank": whitespace and blank lines michael@428: ## _________________________________________________________________ michael@428: ## michael@428: michael@428: sub check_blank { michael@428: my ($file, $spec) = @_; michael@428: michael@428: # check for CR-LF combination michael@428: my $done = ''; my $this = ''; my $todo = $spec; michael@428: while ($todo =~ m/\r\n/s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: &lint_warning($file, $done, $this, "carriage-return (CR, 0x0d) line-feed (NL, 0x0a) combination (expected just line-feed)"); michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for multiple blank lines michael@428: $done = ''; $this = ''; $todo = $spec; michael@428: while ($todo =~ m/(\r?\n[ \t]*){3,}/s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: &lint_warning($file, $done, $this, "multiple subsequent blank lines (expected single blank line)"); michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for trailing whitespaces michael@428: $done = ''; $this = ''; $todo = $spec; michael@428: while ($todo =~ m/[ \t]+\r?\n/s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: if ($done eq '' or $done =~ m|\n$|s) { michael@428: &lint_warning($file, $done, $this, "whitespace on empty line (expected none)"); michael@428: } michael@428: else { michael@428: &lint_warning($file, $done, $this, "trailing whitespace (expected none)"); michael@428: } michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for bogus line continuations michael@428: $done = ''; $this = ''; $todo = $spec; michael@428: while ($todo =~ m/\\[ \t]*\r?\n(?=[ \t]*\r?\n)/s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: &lint_warning($file, $done, $this, "bogus line continuation for following empty line (expect no line continuation)"); michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for leading whitespaces before line continuations michael@428: $done = ''; $this = ''; $todo = $spec; michael@428: while ($todo =~ m/[ \t]{2,}\\[ \t]*\r?\n/s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: &lint_warning($file, $done, $this, "multiple leading whitespace before line continuation (expected just a single space)"); michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for leading tabs michael@428: $done = ''; $this = ''; $todo = $spec; michael@428: while ($todo =~ m/^ *\t+ *[^ \t]/m) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: &lint_warning($file, $done, $this, "leading tabs (expected spaces)"); michael@428: $done .= $this; michael@428: } michael@428: michael@428: # check for mandatory/wished trailing blank line michael@428: if ($spec !~ m|\n\n$|) { michael@428: &lint_warning($file, $done, "", "mandatory/wished trailing blank line missing (expected one)"); michael@428: } michael@428: } michael@428: michael@428: ## _________________________________________________________________ michael@428: ## michael@428: ## CHECK "comment": sharp-comments michael@428: ## _________________________________________________________________ michael@428: ## michael@428: michael@428: sub check_comment { michael@428: my ($file, $spec) = @_; michael@428: my ($pkg); michael@428: michael@428: # determine package name michael@428: $pkg = $file; michael@428: $pkg =~ s|^.+/||; michael@428: $pkg =~ s|^fsl\.||; michael@428: michael@428: # check comment header michael@428: my $re = ""; michael@428: $re .= "##\\n## fsl.$pkg -- OSSP fsl configuration\\n##\\n\\n"; michael@428: if ($spec !~ m|^$re|os) { michael@428: &lint_warning($file, "", "", "invalid comment header (expected $re)"); michael@428: } michael@428: michael@428: # check for comment indentation michael@428: my $done .= $`; my $this = $&; my $todo = $'; michael@428: while ($todo =~ m/^([ \t]*)(#+)([ \t]*)(.*?)$/m) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: my ($lead, $sharp, $pad, $text) = ($1, $2, $3, $4); michael@428: if (length($lead) % 2 != 0) { michael@428: &lint_warning($file, $done, $this, "incorrect comment indentation (expected a multiple of 2 spaces)"); michael@428: } michael@428: if (length($lead) > 1 && length($sharp) > 1) { michael@428: &lint_warning($file, $done, $this, "indented comment has introduced with multiple sharps (expected single sharp character)"); michael@428: } michael@428: if (length($pad.$text) > 0 && length($sharp.$pad) % 4 != 0) { michael@428: &lint_warning($file, $done, $this, "incorrect comment text padding (expected a multiple of 4 sharps or spaces)"); michael@428: } michael@428: if (length($pad) == 0 && length($text) > 0) { michael@428: &lint_warning($file, $done, $this, "missing leading space before comment text (expected padding spaces)"); michael@428: } michael@428: if (length($pad) > 0 && length($text) == 0) { michael@428: &lint_warning($file, $done, $this, "empty comment text (expected a reasonable text)"); michael@428: } michael@428: $done .= $this; michael@428: } michael@428: } michael@428: michael@428: ## _________________________________________________________________ michael@428: ## michael@428: ## CHECK "ident" michael@428: ## _________________________________________________________________ michael@428: ## michael@428: michael@428: sub check_ident { michael@428: my ($file, $spec) = @_; michael@428: my ($pkg, $section); michael@428: michael@428: # determine package name michael@428: $pkg = $file; michael@428: $pkg =~ s|^.+/||; michael@428: $pkg =~ s|^fsl\.||; michael@428: michael@428: # check sections with ident/facility regex michael@428: my $done .= ""; my $this = ""; my $todo = $spec; michael@428: while ($todo =~ m:\n(\w+)(\s+)(\S+)/(\S+)(\s+)q\{(.*?)\};:s) { michael@428: $done .= $`; $this = $&; $todo = $'; michael@428: my ($section, $ws1, $ident, $facility, $ws2, $body) = ($1, $2, $3, $4, $5, $6); michael@428: michael@428: if ($pkg eq "fsl") { michael@428: # enforce default section for fsl michael@428: if ($section ne "default") { michael@428: &lint_warning($file, "", "", "section \"$section\" not allowed for package $pkg (expected default)"); michael@428: } michael@428: } michael@428: else { michael@428: # enforce ident section for any package othen than fsl michael@428: if ($section ne "ident") { michael@428: &lint_warning($file, "", "", "section \"$section\" not allowed for package $pkg (expected ident)"); michael@428: } michael@428: michael@428: # ident and facility wildcard-only would be a catch-all michael@428: if ($ident =~ m/^[(]?\.[\+\*][)]?$/ and $facility =~ m/^[(]?\.[\+\*][)]?$/) { michael@428: &lint_warning($file, "", "", "wildcard not allowed for both ident and facility (found $ident/$facility"); michael@428: } michael@428: } michael@428: michael@428: # enforce a single space michael@428: if (length($ws1) != 1) { michael@428: &lint_warning($file, "", "", "whitespace count wrong between section ($section) and ident ($ident)"); michael@428: } michael@428: michael@428: # enforce a single space michael@428: if (length($ws2) != 1) { michael@428: &lint_warning($file, "", "", "whitespace count wrong between facility ($facility) and end of line"); michael@428: } michael@428: michael@428: # ident same as facility is likely to be a typo michael@428: if ($ident eq $facility) { michael@428: &lint_warning($file, "", "", "unusual constellation ident equal to facility (found $ident/$facility"); michael@428: } michael@428: michael@428: # FIXME MTAs hardcoded here for /mail michael@428: if ($facility eq "mail" and $pkg !~ m/^(sendmail|ssmtp|postfix|exim)$/) { michael@428: &lint_warning($file, "", "", "only MTAs may match facility mail"); michael@428: } michael@428: michael@428: # FIXME inn hardcoded here for /news michael@428: if ($facility eq "news" and $pkg !~ m/^(inn)$/) { michael@428: &lint_warning($file, "", "", "only inn may match facility news"); michael@428: } michael@428: michael@428: # check prefix channel michael@428: if ($body =~ m/\n([ ]*)prefix(\s*?)\((.*?)\)/s) { michael@428: my ($ws1, $ws2, $options) = ($1, $2, $3); michael@428: michael@428: # enforce eight spaces michael@428: if (length($ws1) != 4) { michael@428: &lint_warning($file, "", "", "prefix channel whitespace count at start of line"); michael@428: } michael@428: michael@428: # enforce zero spaces michael@428: if (length($ws2) != 0) { michael@428: &lint_warning($file, "", "", "whitespace not allowed between prefix channel and round open bracket"); michael@428: } michael@428: michael@428: # enforce prefix options in prefix channel michael@428: if ($options !~ m/\sprefix="%b %d %H:%M:%S %N (<%L> )?\$1(\[%P\])?: "/) { michael@428: &lint_warning($file, "", "", "prefix option in prefix channel invalid or missing"); michael@428: } michael@428: $options = $'; michael@428: $options =~ s/,//; michael@428: michael@428: # detect superflous options in prefix channel michael@428: if ($options =~ m/\S+/s) { michael@428: $options =~ s/\n/\\n/; michael@428: &lint_warning($file, "", "", "superflous option in prefix channel unseparated line detected: $options"); michael@428: } michael@428: } michael@428: else { michael@428: &lint_warning($file, "", "", "prefix channel missing"); michael@428: } michael@428: michael@428: # check path branch michael@428: if ($body !~ m/\n([ ]*)->(\s*?)\{(.*)\}\n/s) { michael@428: &lint_warning($file, "", "", "no path branch found"); michael@428: return; michael@428: } michael@428: my ($ws1, $ws2, $body) = ($1, $2, $3); #FIXME check ws1/ws2 michael@428: michael@428: # check path channel michael@428: while ($body =~ m/\n([ ]*)(\w+):(\s+?)file(\s*?)\((.*?)\);/s) { michael@428: my ($ws1, $level, $ws2, $ws3, $options) = ($1, $2, $3, $4, $5); michael@428: $body = $'; michael@428: michael@428: # enforce eight spaces michael@428: if (length($ws1) != 8) { michael@428: &lint_warning($file, "", "", "path channel whitespace count at start of line"); michael@428: } michael@428: michael@428: # enforce spaces michael@428: if (length($ws2) < 1) { michael@428: &lint_warning($file, "", "", "whitespace required between level and file"); michael@428: } michael@428: michael@428: # enforce zero spaces michael@428: if (length($ws3) != 0) { michael@428: &lint_warning($file, "", "", "path channel whitespace not allowed between file channel and round open bracket"); michael@428: } michael@428: michael@428: # check for legal l2 level michael@428: if ($level !~ m/^(panic|critical|error|warning|notice|info|trace|debug)$/) { michael@428: &lint_warning($file, "", "", "illegal l2 level $level detected"); michael@428: } michael@428: michael@428: # enforce file option in file channel michael@428: if ($options !~ m;path="\@l_prefix\@/var/$pkg/(log\S+|$pkg\.log)";) { michael@428: &lint_warning($file, "", "", "path option in file channel invalid or missing"); michael@428: } michael@428: $options = $'; michael@428: $options =~ s/,//; michael@428: michael@428: # enforce perm option in file channel michael@428: if ($options !~ m;perm=0[0-7]{3};) { michael@428: &lint_warning($file, "", "", "perm option in file channel invalid or missing"); michael@428: } michael@428: $options = $'; michael@428: $options =~ s/,//; michael@428: michael@428: # detect superflous options in file channel michael@428: if ($options =~ m/\S+/s) { michael@428: $options =~ s/\n/\\n/; michael@428: &lint_warning($file, "", "", "superflous option in prefix channel detected: $options"); michael@428: } michael@428: } michael@428: michael@428: # check path channel michael@428: if ($body =~ m/\n([ ]*)(\w+):(\s*?)file(\s*?)\((.*?)\)/s) { michael@428: my ($ws1, $level, $ws2, $ws3, $options) = ($1, $2, $3, $4, $5); michael@428: michael@428: # enforce eight spaces michael@428: if (length($ws1) != 8) { michael@428: &lint_warning($file, "", "", "path channel whitespace count at start of unseparated line"); michael@428: } michael@428: michael@428: # enforce spaces michael@428: if (length($ws2) < 1) { michael@428: &lint_warning($file, "", "", "path channel whitespace required between level and file of unseparated line"); michael@428: } michael@428: michael@428: # enforce zero spaces michael@428: if (length($ws3) != 0) { michael@428: &lint_warning($file, "", "", "whitespace not allowed between file channel and round open bracket"); michael@428: } michael@428: michael@428: # check for legal l2 level michael@428: if ($level !~ m/^(panic|critical|error|warning|notice|info|trace|debug)$/) { michael@428: &lint_warning($file, "", "", "illegal l2 level $level detected on unseparated line"); michael@428: } michael@428: michael@428: # enforce file option in file channel michael@428: if ($options !~ m;path="\@l_prefix\@/var/$pkg/(log\S+|$pkg\.log)";) { michael@428: &lint_warning($file, "", "", "XXX path option in file channel invalid or missing on unseparated line"); michael@428: } michael@428: $options = $'; michael@428: $options =~ s/,//; michael@428: michael@428: # enforce perm option in file channel michael@428: if ($options !~ m;perm=0[0-7]{3};) { michael@428: &lint_warning($file, "", "", "perm option in file channel invalid or missing on unseparated line"); michael@428: } michael@428: michael@428: $options = $'; michael@428: $options =~ s/, jitter=[0-9]+//; michael@428: $options =~ s/, monitor=[0-9]+//; michael@428: $options =~ s/,//; michael@428: michael@428: # detect superflous options in file channel michael@428: if ($options =~ m/\S+/s) { michael@428: $options =~ s/\n/\\n/; michael@428: &lint_warning($file, "", "", "superflous option in file channel unseparated line detected: $options"); michael@428: } michael@428: } michael@428: else { michael@428: &lint_warning($file, "", "", "file channel missing"); michael@428: } michael@428: michael@428: $done .= $this; michael@428: } michael@428: return; michael@428: }