This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Abandon plans to change viacode's return of unassigned
[perl5.git] / lib / File / CheckTree.pm
index ae18777..d7948f2 100644 (file)
@@ -1,31 +1,40 @@
 package File::CheckTree;
-require 5.000;
-require Exporter;
+
+use 5.006;
+use Cwd;
+use Exporter;
+use File::Spec;
+use warnings;
+use strict;
+
+our $VERSION = '4.4';
+our @ISA     = qw(Exporter);
+our @EXPORT  = qw(validate);
 
 =head1 NAME
 
-validate - run many filetest checks on a tree
+File::CheckTree - run many filetest checks on a tree
 
 =head1 SYNOPSIS
 
     use File::CheckTree;
 
-    $warnings += validate( q{
-       /vmunix                 -e || die
-       /boot                   -e || die
-       /bin                    cd
-           csh                 -ex
-           csh                 !-ug
-           sh                  -ex
-           sh                  !-ug
-       /usr                    -d || warn "What happened to $file?\n"
+    $num_warnings = validate( q{
+        /vmunix                 -e || die
+        /boot                   -e || die
+        /bin                    cd
+            csh                 -ex
+            csh                 !-ug
+            sh                  -ex
+            sh                  !-ug
+        /usr                    -d || warn "What happened to $file?\n"
     });
 
 =head1 DESCRIPTION
 
 The validate() routine takes a single multiline string consisting of
-lines containing a filename plus a file test to try on it.  (The
-file test may also be a "cd", causing subsequent relative filenames
+directives, each containing a filename plus a file test to try on it.
+(The file test may also be a "cd", causing subsequent relative filenames
 to be interpreted relative to that directory.)  After the file test
 you may put C<|| die> to make it a fatal error if the file test fails.
 The default is C<|| warn>.  The file test may optionally have a "!' prepended
@@ -39,113 +48,191 @@ Only the first failed test of the bunch will produce a warning.
 
 The routine returns the number of warnings issued.
 
+=head1 AUTHOR
+
+File::CheckTree was derived from lib/validate.pl which was
+written by Larry Wall.
+Revised by Paul Grassie <F<grassie@perl.com>> in 2002.
+
+=head1 HISTORY
+
+File::CheckTree used to not display fatal error messages.
+It used to count only those warnings produced by a generic C<|| warn>
+(and not those in which the user supplied the message).  In addition,
+the validate() routine would leave the user program in whatever
+directory was last entered through the use of "cd" directives.
+These bugs were fixed during the development of perl 5.8.
+The first fixed version of File::CheckTree was 4.2.
+
 =cut
 
-@ISA = qw(Exporter);
-@EXPORT = qw(validate);
-
-# $RCSfile: validate.pl,v $$Revision: 4.1 $$Date: 92/08/07 18:24:19 $
-
-# The validate routine takes a single multiline string consisting of
-# lines containing a filename plus a file test to try on it.  (The
-# file test may also be a 'cd', causing subsequent relative filenames
-# to be interpreted relative to that directory.)  After the file test
-# you may put '|| die' to make it a fatal error if the file test fails.
-# The default is '|| warn'.  The file test may optionally have a ! prepended
-# to test for the opposite condition.  If you do a cd and then list some
-# relative filenames, you may want to indent them slightly for readability.
-# If you supply your own "die" or "warn" message, you can use $file to
-# interpolate the filename.
-
-# Filetests may be bunched:  -rwx tests for all of -r, -w and -x.
-# Only the first failed test of the bunch will produce a warning.
-
-# The routine returns the number of warnings issued.
-
-# Usage:
-#      use File::CheckTree;
-#      $warnings += validate('
-#      /vmunix                 -e || die
-#      /boot                   -e || die
-#      /bin                    cd
-#          csh                 -ex
-#          csh                 !-ug
-#          sh                  -ex
-#          sh                  !-ug
-#      /usr                    -d || warn "What happened to $file?\n"
-#      ');
+my $Warnings;
 
 sub validate {
-    local($file,$test,$warnings,$oldwarnings);
-    foreach $check (split(/\n/,$_[0])) {
-       next if $check =~ /^#/;
-       next if $check =~ /^$/;
-       ($file,$test) = split(' ',$check,2);
-       if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
-           $testlist = $2;
-           @testlist = split(//,$testlist);
-       }
-       else {
-           @testlist = ('Z');
-       }
-       $oldwarnings = $warnings;
-       foreach $one (@testlist) {
-           $this = $test;
-           $this =~ s/(-\w\b)/$1 \$file/g;
-           $this =~ s/-Z/-$one/;
-           $this .= ' || warn' unless $this =~ /\|\|/;
-           $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || valmess('$2','$1')/;
-           $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
-           eval $this;
-           last if $warnings > $oldwarnings;
-       }
+    my ($starting_dir, $file, $test, $cwd, $oldwarnings);
+
+    $starting_dir = cwd;
+
+    $cwd = "";
+    $Warnings = 0;
+
+    foreach my $check (split /\n/, $_[0]) {
+        my ($testlist, @testlist);
+
+        # skip blanks/comments
+        next if $check =~ /^\s*#/ || $check =~ /^\s*$/;
+
+        # Todo:
+        # should probably check for invalid directives and die
+        # but earlier versions of File::CheckTree did not do this either
+
+        # split a line like "/foo -r || die"
+        # so that $file is "/foo", $test is "-r || die"
+        # (making special allowance for quoted filenames).
+        if ($check =~ m/^\s*"([^"]+)"\s+(.*?)\s*$/ or
+            $check =~ m/^\s*'([^']+)'\s+(.*?)\s*$/ or
+            $check =~ m/^\s*(\S+?)\s+(\S.*?)\s*$/)
+        {
+            ($file, $test) = ($1,$2);
+        }
+        else {
+            die "Malformed line: '$check'";
+        };
+
+        # change a $test like "!-ug || die" to "!-Z || die",
+        # capturing the bundled tests (e.g. "ug") in $2
+        if ($test =~ s/ ^ (!?-) (\w{2,}) \b /$1Z/x) {
+            $testlist = $2;
+            # split bundled tests, e.g. "ug" to 'u', 'g'
+            @testlist = split(//, $testlist);
+        }
+        else {
+            # put in placeholder Z for stand-alone test
+            @testlist = ('Z');
+        }
+
+        # will compare these two later to stop on 1st warning w/in a bundle
+        $oldwarnings = $Warnings;
+
+        foreach my $one (@testlist) {
+            # examples of $test: "!-Z || die" or "-w || warn"
+            my $this = $test;
+
+            # expand relative $file to full pathname if preceded by cd directive
+            $file = File::Spec->catfile($cwd, $file) 
+                    if $cwd && !File::Spec->file_name_is_absolute($file);
+
+            # put filename in after the test operator
+            $this =~ s/(-\w\b)/$1 "\$file"/g;
+
+            # change the "-Z" representing a bundle with the $one test
+            $this =~ s/-Z/-$one/;
+
+            # if it's a "cd" directive...
+            if ($this =~ /^cd\b/) {
+                # add "|| die ..."
+                $this .= ' || die "cannot cd to $file\n"';
+                # expand "cd" directive with directory name
+                $this =~ s/\bcd\b/chdir(\$cwd = '$file')/;
+            }
+            else {
+                # add "|| warn" as a default disposition
+                $this .= ' || warn' unless $this =~ /\|\|/; 
+
+                # change a generic ".. || die" or ".. || warn"
+                # to call valmess instead of die/warn directly
+                # valmess will look up the error message from %Val_Message
+                $this =~ s/ ^ ( (\S+) \s+ \S+ ) \s* \|\| \s* (die|warn) \s* $
+                          /$1 || valmess('$3', '$2', \$file)/x;
+            }
+
+            {
+                # count warnings, either from valmess or '-r || warn "my msg"'
+                # also, call any pre-existing signal handler for __WARN__
+                my $orig_sigwarn = $SIG{__WARN__};
+                local $SIG{__WARN__} = sub {
+                    ++$Warnings;
+                    if ( $orig_sigwarn ) {
+                        $orig_sigwarn->(@_);
+                    }
+                    else {
+                        warn "@_";
+                    }
+                };
+
+                # do the test
+                eval $this;
+
+                # re-raise an exception caused by a "... || die" test 
+                if (my $err = $@) {
+                    # in case of any cd directives, return from whence we came
+                    if ($starting_dir ne cwd) {
+                        chdir($starting_dir) || die "$starting_dir: $!";
+                    }
+                    die $err;
+                }
+            }
+
+            # stop on 1st warning within a bundle of tests
+            last if $Warnings > $oldwarnings;
+        }
+    }
+
+    # in case of any cd directives, return from whence we came
+    if ($starting_dir ne cwd) {
+        chdir($starting_dir) || die "chdir $starting_dir: $!";
     }
-    $warnings;
+
+    return $Warnings;
 }
 
+my %Val_Message = (
+    'r' => "is not readable by uid $>.",
+    'w' => "is not writable by uid $>.",
+    'x' => "is not executable by uid $>.",
+    'o' => "is not owned by uid $>.",
+    'R' => "is not readable by you.",
+    'W' => "is not writable by you.",
+    'X' => "is not executable by you.",
+    'O' => "is not owned by you.",
+    'e' => "does not exist.",
+    'z' => "does not have zero size.",
+    's' => "does not have non-zero size.",
+    'f' => "is not a plain file.",
+    'd' => "is not a directory.",
+    'l' => "is not a symbolic link.",
+    'p' => "is not a named pipe (FIFO).",
+    'S' => "is not a socket.",
+    'b' => "is not a block special file.",
+    'c' => "is not a character special file.",
+    'u' => "does not have the setuid bit set.",
+    'g' => "does not have the setgid bit set.",
+    'k' => "does not have the sticky bit set.",
+    'T' => "is not a text file.",
+    'B' => "is not a binary file."
+);
+
 sub valmess {
-    local($disposition,$this) = @_;
-    $file = $cwd . '/' . $file unless $file =~ m|^/|s;
-    if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
-       $neg = $1;
-       $tmp = $2;
-       $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
-       $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
-       $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
-       $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
-       $tmp eq 'R' && ($mess = "$file is not readable by you.");
-       $tmp eq 'W' && ($mess = "$file is not writable by you.");
-       $tmp eq 'X' && ($mess = "$file is not executable by you.");
-       $tmp eq 'O' && ($mess = "$file is not owned by you.");
-       $tmp eq 'e' && ($mess = "$file does not exist.");
-       $tmp eq 'z' && ($mess = "$file does not have zero size.");
-       $tmp eq 's' && ($mess = "$file does not have non-zero size.");
-       $tmp eq 'f' && ($mess = "$file is not a plain file.");
-       $tmp eq 'd' && ($mess = "$file is not a directory.");
-       $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
-       $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
-       $tmp eq 'S' && ($mess = "$file is not a socket.");
-       $tmp eq 'b' && ($mess = "$file is not a block special file.");
-       $tmp eq 'c' && ($mess = "$file is not a character special file.");
-       $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
-       $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
-       $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
-       $tmp eq 'T' && ($mess = "$file is not a text file.");
-       $tmp eq 'B' && ($mess = "$file is not a binary file.");
-       if ($neg eq '!') {
-           $mess =~ s/ is not / should not be / ||
-           $mess =~ s/ does not / should not / ||
-           $mess =~ s/ not / /;
-       }
+    my ($disposition, $test, $file) = @_;
+    my $ferror;
+
+    if ($test =~ / ^ (!?) -(\w) \s* $ /x) {
+        my ($neg, $ftype) = ($1, $2);
+
+        $ferror = "$file $Val_Message{$ftype}";
+
+        if ($neg eq '!') {
+            $ferror =~ s/ is not / should not be / ||
+            $ferror =~ s/ does not / should not / ||
+            $ferror =~ s/ not / /;
+        }
     }
     else {
-       $this =~ s/\$file/'$file'/g;
-       $mess = "Can't do $this.\n";
+        $ferror = "Can't do $test $file.\n";
     }
-    die "$mess\n" if $disposition eq 'die';
-    warn "$mess\n";
-    ++$warnings;
+
+    die "$ferror\n" if $disposition eq 'die';
+    warn "$ferror\n";
 }
 
 1;
-