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