perl 3.0: (no announcement message available)
[p5sagit/p5-mst-13.2.git] / lib / validate.pl
1 ;# $Header: validate.pl,v 3.0 89/10/18 15:20:04 lwall Locked $
2
3 ;# The validate routine takes a single multiline string consisting of
4 ;# lines containing a filename plus a file test to try on it.  (The
5 ;# file test may also be a 'cd', causing subsequent relative filenames
6 ;# to be interpreted relative to that directory.)  After the file test
7 ;# you may put '|| die' to make it a fatal error if the file test fails.
8 ;# The default is '|| warn'.  The file test may optionally have a ! prepended
9 ;# to test for the opposite condition.  If you do a cd and then list some
10 ;# relative filenames, you may want to indent them slightly for readability.
11 ;# If you supply your own "die" or "warn" message, you can use $file to
12 ;# interpolate the filename.
13
14 ;# Filetests may be bunched:  -rwx tests for all of -r, -w and -x.
15 ;# Only the first failed test of the bunch will produce a warning.
16
17 ;# The routine returns the number of warnings issued.
18
19 ;# Usage:
20 ;#      $warnings += do validate('
21 ;#      /vmunix                 -e || die
22 ;#      /boot                   -e || die
23 ;#      /bin                    cd
24 ;#          csh                 -ex
25 ;#          csh                 !-ug
26 ;#          sh                  -ex
27 ;#          sh                  !-ug
28 ;#      /usr                    -d || warn "What happened to $file?\n"
29 ;#      ');
30
31 sub validate {
32     local($file,$test,$warnings,$oldwarnings);
33     foreach $check (split(/\n/,$_[0])) {
34         next if $check =~ /^#/;
35         next if $check =~ /^$/;
36         ($file,$test) = split(' ',$check,2);
37         if ($test =~ s/^(!?-)(\w{2,}\b)/$1Z/) {
38             $testlist = $2;
39             @testlist = split(//,$testlist);
40         }
41         else {
42             @testlist = ('Z');
43         }
44         $oldwarnings = $warnings;
45         foreach $one (@testlist) {
46             $this = $test;
47             $this =~ s/(-\w\b)/$1 \$file/g;
48             $this =~ s/-Z/-$one/;
49             $this .= ' || warn' unless $this =~ /\|\|/;
50             $this =~ s/^(.*\S)\s*\|\|\s*(die|warn)$/$1 || do valmess('$2','$1')/;
51             $this =~ s/\bcd\b/chdir (\$cwd = \$file)/g;
52             eval $this;
53             last if $warnings > $oldwarnings;
54         }
55     }
56     $warnings;
57 }
58
59 sub valmess {
60     local($disposition,$this) = @_;
61     $file = $cwd . '/' . $file unless $file =~ m|^/|;
62     if ($this =~ /^(!?)-(\w)\s+\$file\s*$/) {
63         $neg = $1;
64         $tmp = $2;
65         $tmp eq 'r' && ($mess = "$file is not readable by uid $>.");
66         $tmp eq 'w' && ($mess = "$file is not writable by uid $>.");
67         $tmp eq 'x' && ($mess = "$file is not executable by uid $>.");
68         $tmp eq 'o' && ($mess = "$file is not owned by uid $>.");
69         $tmp eq 'R' && ($mess = "$file is not readable by you.");
70         $tmp eq 'W' && ($mess = "$file is not writable by you.");
71         $tmp eq 'X' && ($mess = "$file is not executable by you.");
72         $tmp eq 'O' && ($mess = "$file is not owned by you.");
73         $tmp eq 'e' && ($mess = "$file does not exist.");
74         $tmp eq 'z' && ($mess = "$file does not have zero size.");
75         $tmp eq 's' && ($mess = "$file does not have non-zero size.");
76         $tmp eq 'f' && ($mess = "$file is not a plain file.");
77         $tmp eq 'd' && ($mess = "$file is not a directory.");
78         $tmp eq 'l' && ($mess = "$file is not a symbolic link.");
79         $tmp eq 'p' && ($mess = "$file is not a named pipe (FIFO).");
80         $tmp eq 'S' && ($mess = "$file is not a socket.");
81         $tmp eq 'b' && ($mess = "$file is not a block special file.");
82         $tmp eq 'c' && ($mess = "$file is not a character special file.");
83         $tmp eq 'u' && ($mess = "$file does not have the setuid bit set.");
84         $tmp eq 'g' && ($mess = "$file does not have the setgid bit set.");
85         $tmp eq 'k' && ($mess = "$file does not have the sticky bit set.");
86         $tmp eq 'T' && ($mess = "$file is not a text file.");
87         $tmp eq 'B' && ($mess = "$file is not a binary file.");
88         if ($neg eq '!') {
89             $mess =~ s/ is not / should not be / ||
90             $mess =~ s/ does not / should not / ||
91             $mess =~ s/ not / /;
92         }
93         print stderr $mess,"\n";
94     }
95     else {
96         $this =~ s/\$file/'$file'/g;
97         print stderr "Can't do $this.\n";
98     }
99     if ($disposition eq 'die') { exit 1; }
100     ++$warnings;
101 }
102
103 1;