3 # Copyright (c) 2013 Sine Nomine Associates
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 # notice, this list of conditions and the following disclaimer in the
15 # documentation and/or other materials provided with the distribution.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18 # IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24 # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27 # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 # checkman - run everything in the given directory of binaries, and try to
30 # find mismatches between the -help output, and the man page for that command
42 print STDERR
"WARNING: Running checkman can be dangerous, as it tries to \n";
43 print STDERR
"blindly run almost everything in the given binaries dir.\n\n";
44 print STDERR
"Usage: $0 --bindir <binaries_dir> --mandir <manpages_dir>";
48 "b|bindir=s" => \
$bindir,
49 "M|mandir=s" => \
$mandir,
50 ) or die("Error while parsing options\n");
52 if (not defined($bindir)) {
55 if (not defined($mandir)) {
60 die("--bindir $bindir is not a directory\n");
63 die("--mandir $mandir is not a directory\n");
65 if (not -d
"$mandir/man1") {
66 die("--mandir must point to a dir containing man1, man8, etc\n");
80 # find a list of all possible commands we can run, and map them to their full
83 if (-f
and -x
and -s
) {
84 $cmd_map{$_} = $File::Find
::name
;
96 my ($manstr, $helpout) = @_;
104 $helpout =~ tr/\n/ /;
106 # match everything that looks like an option
107 # basically, find stuff that begins with a hyphen, and is surrounded by
108 # brackets or spaces, or precedes a '='
109 for ($helpout =~ m/(?:\[| )-([a-zA-Z0-9_-]+)(?=\s|[][]|=)/g) {
110 #print " help str $manstr opt -$_\n" if ($manstr =~ /ptserver/);
112 # Almost everything lists '-c' as an alias for '-cell'.
113 # We don't put that in the first synopsis for each man
114 # page, so just pretend it's not there.
120 my $manout = `man -s 8,1 -M '$mandir' $manstr 2>/dev/null`;
125 my $syn_sections = 0;
129 for (split /^/, $manout) {
130 $lastline = $curline if (defined($curline));
145 # don't need anything after OPTIONS
152 if (m/^\s+[a-z]/ and $insyn) {
154 if ($syn_sections > 1) {
155 # don't need anything in the synopsis after the first area
162 # check for options in the synopsis...
163 for (m/(?:\[|\s)-([a-zA-Z0-9_-]+)(?=\s|\]|\[)/g) {
164 #print " man page $manstr syn opt -$_\n" if ($manstr =~ /ptserver/);
169 # check for options in the OPTIONS section
170 #print "last: $lastline, cur: $_\n";
171 if ($lastline =~ m/^(\s*|OPTIONS)$/ && m/^\s+-[a-zA-Z0-9_-]+/) {
172 # Options only appear after a blank line (or right after the
173 # OPTIONS line), so only go here if the last
174 # line was blank, and we see what looks like an
175 # option as the first thing on the current
178 # Find all options on the current line. Option
179 # aliases can appear on the same =items line,
180 # so get all of the aliases.
181 for (m/\s-([a-zA-Z0-9_-]+)/g) {
182 $man_just_opts{$_} = 1;
183 if (exists $syn_opts{$_}) {
184 # only count them if they also appeared in the synopsis earlier
192 if (not %man_opts and not %syn_opts) {
193 # we found no options in the man page output; so probably, we didn't
194 # actually get a man page back. just print a single message, so we don't
195 # print out something for every single option
196 print "man page $manstr missing\n";
200 for (keys %help_opts) {
201 if (not exists $man_opts{$_}) {
203 if (exists $syn_opts{$_}) {
204 $extra = " from OPTIONS";
205 } elsif (exists $man_just_opts{$_}) {
206 $extra = " from synopsis";
209 print "man page $manstr missing option -$_$extra\n";
213 my %tmphash = (%syn_opts, %man_just_opts);
214 for (keys %tmphash) {
215 if (not exists $help_opts{$_}) {
217 if (not exists $syn_opts{$_}) {
218 $extra = " in OPTIONS";
219 } elsif (not exists $man_just_opts{$_}) {
220 $extra = " in synopsis";
223 print "man page $manstr extra option -$_$extra\n";
231 my ($cmd, $path, $subcmd) = @_;
239 if (defined($subcmd)) {
240 $runstr = "$path $subcmd";
241 if ($subcmd ne "initcmd") {
242 $manstr = "$cmd"."_"."$subcmd";
246 if (defined($cmd_blacklist{$cmd})) {
250 my $out = `$runstr -help 2>&1`;
252 if ($out =~ m/^Usage: /) {
253 # actual help output, listing options etc
254 check_opts
($manstr, $out);
258 if ($out =~ m/Commands are:$/m) {
259 # multi-command program
260 if (defined($subcmd)) {
261 die("Subcommand $cmd $subcmd gave more subcommands?");
264 if ($out =~ m/^initcmd.*initialize the program$/m) {
265 # not actually multi-command; we just need to give the initcmd
267 parsehelp
($cmd, $path, "initcmd");
271 # find all of the subcommands, and call parsehelp() on them
272 for (split /^/, $out) {
274 next if m/Commands are:$/;
275 next if m/^apropos\s/ or m/^help\s/;
276 if (m/^(\S+)\s+[\S ]+$/) {
277 parsehelp
($cmd, $path, $1);
279 print "for cmd $cmd got unmatched line $_\n";
287 if (not defined($subcmd)) {
291 print "Skipped command $path $subcmd\n";
293 # not sure what to do about this one
294 push @error_cmds, "$path $subcmd";
297 for my $cmd (keys %cmd_map) {
298 my $path = $cmd_map{$cmd};
300 parsehelp
($cmd, $path);