util/find-doc-nits: more precise option and function name checker
[openssl.git] / util / find-doc-nits
1 #! /usr/bin/env perl
2 # Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 require 5.10.0;
11 use warnings;
12 use strict;
13 use Pod::Checker;
14 use File::Find;
15 use File::Basename;
16 use File::Spec::Functions;
17 use Getopt::Std;
18 use lib catdir(dirname($0), "perl");
19 use OpenSSL::Util::Pod;
20
21 my $debug = 0;                  # Set to 1 for debug output
22
23 # Options.
24 our($opt_d);
25 our($opt_e);
26 our($opt_s);
27 our($opt_o);
28 our($opt_h);
29 our($opt_l);
30 our($opt_n);
31 our($opt_p);
32 our($opt_u);
33 our($opt_v);
34 our($opt_c);
35
36 sub help {
37     print <<EOF;
38 Find small errors (nits) in documentation.  Options:
39     -d Detailed list of undocumented (implies -u)
40     -e Detailed list of new undocumented (implies -v)
41     -s Same as -e except no output is generated if nothing is undocumented
42     -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
43     -l Print bogus links
44     -n Print nits in POD pages
45     -p Warn if non-public name documented (implies -n)
46     -u Count undocumented functions
47     -v Count new undocumented functions
48     -h Print this help message
49     -c List undocumented commands and options
50 EOF
51     exit;
52 }
53
54 my $temp = '/tmp/docnits.txt';
55 my $OUT;
56 my %public;
57 my $status = 0;
58
59 my %mandatory_sections =
60     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
61       1      => [ 'SYNOPSIS', 'OPTIONS' ],
62       3      => [ 'SYNOPSIS', 'RETURN VALUES' ],
63       5      => [ ],
64       7      => [ ] );
65
66 # Print error message, set $status.
67 sub err {
68     print join(" ", @_), "\n";
69     $status = 1
70 }
71
72 # Cross-check functions in the NAME and SYNOPSIS section.
73 sub name_synopsis {
74     my $id = shift;
75     my $filename = shift;
76     my $contents = shift;
77
78     # Get NAME section and all words in it.
79     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
80     my $tmp = $1;
81     $tmp =~ tr/\n/ /;
82     err($id, "trailing comma before - in NAME")
83         if $tmp =~ /, *-/;
84     $tmp =~ s/ -.*//g;
85     err($id, "POD markup among the names in NAME")
86         if $tmp =~ /[<>]/;
87     $tmp =~ s/  */ /g;
88     err($id, "missing comma in NAME")
89         if $tmp =~ /[^,] /;
90
91     my $dirname = dirname($filename);
92     my $simplename = basename(basename($filename, ".in"), ".pod");
93     my $foundfilename = 0;
94     my %foundfilenames = ();
95     my %names;
96     foreach my $n ( split ',', $tmp ) {
97         $n =~ s/^\s+//;
98         $n =~ s/\s+$//;
99         err($id, "the name '$n' contains white-space")
100             if $n =~ /\s/;
101         $names{$n} = 1;
102         $foundfilename++ if $n eq $simplename;
103         $foundfilenames{$n} = 1
104             if ((-f "$dirname/$n.pod.in" || -f "$dirname/$n.pod")
105                 && $n ne $simplename);
106     }
107     err($id, "the following exist as other .pod or .pod.in files:",
108          sort keys %foundfilenames)
109         if %foundfilenames;
110     err($id, "$simplename (filename) missing from NAME section")
111         unless $foundfilename;
112     foreach my $n ( keys %names ) {
113         err($id, "$n is not public")
114             if $opt_p and !defined $public{$n};
115     }
116
117     # Find all functions in SYNOPSIS
118     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
119     my $syn = $1;
120     foreach my $line ( split /\n+/, $syn ) {
121         next unless $line =~ /^\s/;
122         my $sym;
123         $line =~ s/STACK_OF\([^)]+\)/int/g;
124         $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
125         $line =~ s/__declspec\([^)]+\)//;
126         if ( $line =~ /env (\S*)=/ ) {
127             # environment variable env NAME=...
128             $sym = $1;
129         } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
130             # a callback function pointer: typedef ... (*NAME)(...
131             $sym = $1;
132         } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
133             # a callback function signature: typedef ... NAME(...
134             $sym = $1;
135         } elsif ( $line =~ /typedef.* (\S+);/ ) {
136             # a simple typedef: typedef ... NAME;
137             $sym = $1;
138         } elsif ( $line =~ /enum (\S*) \{/ ) {
139             # an enumeration: enum ... {
140             $sym = $1;
141         } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
142             $sym = $1;
143         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
144             $sym = $1;
145         }
146         else {
147             next;
148         }
149         err($id, "$sym missing from NAME section")
150             unless defined $names{$sym};
151         $names{$sym} = 2;
152
153         # Do some sanity checks on the prototype.
154         err($id, "prototype missing spaces around commas: $line")
155             if ( $line =~ /[a-z0-9],[^ ]/ );
156     }
157
158     foreach my $n ( keys %names ) {
159         next if $names{$n} == 2;
160         err($id, "$n missing from SYNOPSIS")
161     }
162 }
163
164 # Check if SECTION ($3) is located before BEFORE ($4)
165 sub check_section_location {
166     my $id = shift;
167     my $contents = shift;
168     my $section = shift;
169     my $before = shift;
170
171     return unless $contents =~ /=head1 $section/
172         and $contents =~ /=head1 $before/;
173     err($id, "$section should appear before $before section")
174         if $contents =~ /=head1 $before.*=head1 $section/ms;
175 }
176
177 # Check if a =head1 is duplicated, or a =headX is duplicated within a
178 # =head1.  Treats =head2 =head3 as equivalent -- it doesn't reset the head3
179 # sets if it finds a =head2 -- but that is good enough for now. Also check
180 # for proper capitalization, trailing periods, etc.
181 sub check_head_style {
182     my $id = shift;
183     my $contents = shift;
184     my %head1;
185     my %subheads;
186
187     foreach my $line ( split /\n+/, $contents ) {
188         next unless $line =~ /^=head/;
189         if ( $line =~ /head1/ ) {
190             err($id, "duplicate section $line")
191                 if defined $head1{$line};
192             $head1{$line} = 1;
193             %subheads = ();
194         } else {
195             err($id, "duplicate subsection $line")
196                 if defined $subheads{$line};
197             $subheads{$line} = 1;
198         }
199         err($id, "period in =head")
200             if $line =~ /\.[^\w]/ or $line =~ /\.$/;
201         err($id, "not all uppercase in =head1")
202             if $line =~ /head1.*[a-z]/;
203         err($id, "all uppercase in subhead")
204             if $line =~ /head[234][ A-Z0-9]+$/;
205     }
206 }
207
208 # Because we have options and symbols with extra markup, we need
209 # to take that into account, so we need a regexp that extracts
210 # markup chunks, including recursive markup.
211 # please read up on /(?R)/ in perlre(1)
212 # (note: order is important, (?R) needs to come before .)
213 # (note: non-greedy is important, or something like 'B<foo> and B<bar>'
214 # will be captured as one item)
215 my $markup_re =
216     qr/(                        # Capture group
217            [BIL]<               # The start of what we recurse on
218            (?:(?-1)|.)*?        # recurse the whole regexp (refering to
219                                 # the last opened capture group, i.e. the
220                                 # start of this regexp), or pick next
221                                 # character.  Do NOT be greedy!
222            >                    # The end of what we recurse on
223        )/x;                     # (the x allows this sort of split up regexp)
224
225 # Options must start with a dash, followed by a letter, possibly
226 # followed by letters, digits, dashes and underscores, and the last
227 # character must be a letter or a digit.
228 # We do also accept the single -? or -n, where n is a digit
229 my $option_re =
230     qr/(?:
231             \?                  # Single question mark
232             |
233             \d                  # Single digit
234             |
235             -                   # Single dash (--)
236             |
237             [[:alpha:]](?:[-_[:alnum:]]*?[[:alnum:]])?
238        )/x;
239
240 # Helper function to check if a given $thing is properly marked up
241 # option.  It returns one of these values:
242 #
243 # undef         if it's not an option
244 # ""            if it's a malformed option
245 # $unwrapped    the option with the outermost B<> wrapping removed.
246 sub normalise_option {
247     my $id = shift;
248     my $filename = shift;
249     my $thing = shift;
250
251     my $unwrapped = $thing;
252     my $unmarked = $thing;
253
254     # $unwrapped is the option with the outer B<> markup removed
255     $unwrapped =~ s/^B<//;
256     $unwrapped =~ s/>$//;
257     # $unmarked is the option with *all* markup removed
258     $unmarked =~ s/[BIL]<|>//msg;
259
260
261     # If we found an option, check it, collect it
262     if ( $unwrapped =~ /^\s*-/ ) {
263         return $unwrapped       # return option with outer B<> removed
264             if $unmarked =~ /^-${option_re}$/;
265         return "";              # Malformed option
266     }
267     return undef;               # Something else
268 }
269
270 # Checks of command option (man1) formatting.  The man1 checks are
271 # restricted to the SYNOPSIS and OPTIONS sections, the rest is too
272 # free form, we simply cannot be too strict there.
273
274 sub option_check {
275     my $id = shift;
276     my $filename = shift;
277     my $contents = shift;
278
279     my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1);
280
281     # Some pages have more than one OPTIONS section, let's make sure
282     # to get them all
283     my $options = '';
284     while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) {
285         $options .= $1;
286     }
287
288     # Look for options with no or incorrect markup
289     while ( $synopsis =~
290             /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) {
291         err($id, "Malformed option [1] in SYNOPSIS: $&");
292     }
293
294     while ( $synopsis =~ /$markup_re/msg ) {
295         my $found = $&;
296         print STDERR "$id:DEBUG[option_check] SYNOPSIS: found $found\n"
297             if $debug;
298         my $option_uw = normalise_option($id, $filename, $found);
299         err($id, "Malformed option [2] in SYNOPSIS: $found")
300             if defined $option_uw && $option_uw eq '';
301     }
302
303     # In OPTIONS, we look for =item paragraphs.
304     # (?=^\s*$) detects an empty line.
305     while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) {
306         my $item = $&;
307
308         while ( $item =~ /(\[\s*)?($markup_re)/msg ) {
309             my $found = $2;
310             print STDERR "$id:DEBUG[option_check] OPTIONS: found $&\n"
311                 if $debug;
312             err($id, "Unexpected bracket in OPTIONS =item: $item")
313                 if ($1 // '') ne '' && $found =~ /^B<\s*-/;
314
315             my $option_uw = normalise_option($id, $filename, $found);
316             err($id, "Malformed option in OPTIONS: $found")
317                 if defined $option_uw && $option_uw eq '';
318         }
319     }
320 }
321
322 # Normal symbol form
323 my $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/;
324
325 # Checks of function name (man3) formatting.  The man3 checks are
326 # easier than the man1 checks, we only check the names followed by (),
327 # and only the names that have POD markup.
328
329 sub functionname_check {
330     my $id = shift;
331     my $filename = shift;
332     my $contents = shift;
333
334     while ( $contents =~ /($markup_re)\(\)/msg ) {
335         print STDERR "$id:DEBUG[functionname_check] SYNOPSIS: found $&\n"
336             if $debug;
337
338         my $symbol = $1;
339         my $unmarked = $symbol;
340         $unmarked =~ s/[BIL]<|>//msg;
341
342         err($id, "Malformed symbol: $symbol")
343             unless $symbol =~ /^B<.*>$/ && $unmarked =~ /^${symbol_re}$/
344     }
345
346     # We can't do the kind of collecting coolness that option_check()
347     # does, because there are too many things that can't be found in
348     # name repositories like the NAME sections, such as symbol names
349     # with a variable part (typically marked up as B<foo_I<TYPE>_bar>
350 }
351
352 sub check {
353     my $filename = shift;
354     my $dirname = basename(dirname($filename));
355
356     my $contents = '';
357     {
358         local $/ = undef;
359         open POD, $filename or die "Couldn't open $filename, $!";
360         $contents = <POD>;
361         close POD;
362     }
363
364     my $id = "${filename}:1:";
365     check_head_style($id, $contents);
366
367     # Check ordering of some sections in man3
368     if ( $filename =~ m|man3/| ) {
369         check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES");
370         check_section_location($id, $contents, "SEE ALSO", "HISTORY");
371         check_section_location($id, $contents, "EXAMPLES", "SEE ALSO");
372     }
373
374     unless ( $contents =~ /=for comment generic/ ) {
375         if ( $filename =~ m|man3/| ) {
376             name_synopsis($id, $filename, $contents);
377             functionname_check($id, $filename, $contents);
378         } elsif ( $filename =~ m|man1/| ) {
379             option_check($id, $filename, $contents)
380         }
381     }
382
383     err($id, "doesn't start with =pod")
384         if $contents !~ /^=pod/;
385     err($id, "doesn't end with =cut")
386         if $contents !~ /=cut\n$/;
387     err($id, "more than one cut line.")
388         if $contents =~ /=cut.*=cut/ms;
389     err($id, "EXAMPLE not EXAMPLES section.")
390         if $contents =~ /=head1 EXAMPLE[^S]/;
391     err($id, "WARNING not WARNINGS section.")
392         if $contents =~ /=head1 WARNING[^S]/;
393     err($id, "missing copyright")
394         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
395     err($id, "copyright not last")
396         if $contents =~ /head1 COPYRIGHT.*=head/ms;
397     err($id, "head2 in All uppercase")
398         if $contents =~ /head2\s+[A-Z ]+\n/;
399     err($id, "extra space after head")
400         if $contents =~ /=head\d\s\s+/;
401     err($id, "period in NAME section")
402         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
403     err($id, "Duplicate $1 in L<>")
404         if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
405     err($id, "Bad =over $1")
406         if $contents =~ /=over([^ ][^24])/;
407     err($id, "Possible version style issue")
408         if $contents =~ /OpenSSL version [019]/;
409
410     if ( $contents !~ /=for comment multiple includes/ ) {
411         # Look for multiple consecutive openssl #include lines
412         # (non-consecutive lines are okay; see man3/MD5.pod).
413         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
414             my $count = 0;
415             foreach my $line ( split /\n+/, $1 ) {
416                 if ( $line =~ m@include <openssl/@ ) {
417                     err($id, "has multiple includes")
418                         if ++$count == 2;
419                 } else {
420                     $count = 0;
421                 }
422             }
423         }
424     }
425
426     open my $OUT, '>', $temp
427         or die "Can't open $temp, $!";
428     podchecker($filename, $OUT);
429     close $OUT;
430     open $OUT, '<', $temp
431         or die "Can't read $temp, $!";
432     while ( <$OUT> ) {
433         next if /\(section\) in.*deprecated/;
434         print;
435     }
436     close $OUT;
437     unlink $temp || warn "Can't remove $temp, $!";
438
439     # Find what section this page is in; assume 3.
440     my $section = 3;
441     $section = $1 if $dirname =~ /man([1-9])/;
442
443     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
444         # Skip "return values" if not -s
445         err($id, "missing $_ head1 section")
446             if $contents !~ /^=head1\s+${_}\s*$/m;
447     }
448 }
449
450 my %dups;
451
452 sub parsenum {
453     my $file = shift;
454     my @apis;
455
456     open my $IN, '<', $file
457         or die "Can't open $file, $!, stopped";
458
459     while ( <$IN> ) {
460         next if /^#/;
461         next if /\bNOEXIST\b/;
462         my @fields = split();
463         die "Malformed line $_"
464             if scalar @fields != 2 && scalar @fields != 4;
465         push @apis, $fields[0];
466     }
467
468     close $IN;
469
470     print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
471     return sort @apis;
472 }
473
474 sub getdocced
475 {
476     my $dir = shift;
477     my %return;
478
479     foreach my $pod ( glob("$dir/*.pod"), glob("$dir/*.pod.in") ) {
480         my %podinfo = extract_pod_info($pod);
481         foreach my $n ( @{$podinfo{names}} ) {
482             $return{$n} = $pod;
483             print "# Duplicate $n in $pod and $dups{$n}\n"
484                 if defined $dups{$n} && $dups{$n} ne $pod;
485             $dups{$n} = $pod;
486         }
487     }
488
489     return %return;
490 }
491
492 my %docced;
493
494 sub loadmissing($)
495 {
496     my $missingfile = shift;
497     my @missing;
498
499     open FH, $missingfile
500         || die "Can't open $missingfile";
501     while ( <FH> ) {
502         chomp;
503         next if /^#/;
504         push @missing, $_;
505     }
506     close FH;
507
508     return @missing;
509 }
510
511 sub checkmacros {
512     my $count = 0;
513     my %seen;
514     my @missing;
515
516     if ($opt_o) {
517         @missing = loadmissing('util/missingmacro111.txt');
518     } elsif ($opt_v) {
519         @missing = loadmissing('util/missingmacro.txt');
520     }
521
522     print "# Checking macros (approximate)\n"
523         if !$opt_s;
524     foreach my $f ( glob('include/openssl/*.h') ) {
525         # Skip some internals we don't want to document yet.
526         next if $f eq 'include/openssl/asn1.h';
527         next if $f eq 'include/openssl/asn1t.h';
528         next if $f eq 'include/openssl/err.h';
529         open(IN, $f) || die "Can't open $f, $!";
530         while ( <IN> ) {
531             next unless /^#\s*define\s*(\S+)\(/;
532             my $macro = $1;
533             next if $docced{$macro} || defined $seen{$macro};
534             next if $macro =~ /i2d_/
535                 || $macro =~ /d2i_/
536                 || $macro =~ /DEPRECATEDIN/
537                 || $macro =~ /IMPLEMENT_/
538                 || $macro =~ /DECLARE_/;
539
540             # Skip macros known to be missing
541             next if $opt_v && grep( /^$macro$/, @missing);
542     
543             print "$f:$macro\n"
544                 if $opt_d || $opt_e;
545             $count++;
546             $seen{$macro} = 1;
547         }
548         close(IN);
549     }
550     print "# Found $count macros missing\n"
551         if !$opt_s || $count > 0;
552 }
553
554 sub printem {
555     my $libname = shift;
556     my $numfile = shift;
557     my $missingfile = shift;
558     my $count = 0;
559     my %seen;
560
561     my @missing = loadmissing($missingfile) if ($opt_v);
562
563     foreach my $func ( parsenum($numfile) ) {
564         next if $docced{$func} || defined $seen{$func};
565
566         # Skip ASN1 utilities
567         next if $func =~ /^ASN1_/;
568
569         # Skip functions known to be missing
570         next if $opt_v && grep( /^$func$/, @missing);
571
572         print "$libname:$func\n"
573             if $opt_d || $opt_e;
574         $count++;
575         $seen{$func} = 1;
576     }
577     print "# Found $count missing from $numfile\n\n"
578         if !$opt_s || $count > 0;
579 }
580
581
582 # Collection of links in each POD file.
583 # filename => [ "foo(1)", "bar(3)", ... ]
584 my %link_collection = ();
585 # Collection of names in each POD file.
586 # "name(s)" => filename
587 my %name_collection = ();
588
589 sub collectnames {
590     my $filename = shift;
591     $filename =~ m|man(\d)/|;
592     my $section = $1;
593     my $simplename = basename(basename($filename, ".in"), ".pod");
594     my $id = "${filename}:1:";
595
596     my $contents = '';
597     {
598         local $/ = undef;
599         open POD, $filename or die "Couldn't open $filename, $!";
600         $contents = <POD>;
601         close POD;
602     }
603
604     $contents =~ /=head1 NAME([^=]*)=head1 /ms;
605     my $tmp = $1;
606     unless (defined $tmp) {
607         err($id, "weird name section");
608         return;
609     }
610     $tmp =~ tr/\n/ /;
611     $tmp =~ s/ -.*//g;
612
613     my @names =
614         map { s|/|-|g; $_ }              # Treat slash as dash
615         map { s/^\s+//g; s/\s+$//g; $_ } # Trim prefix and suffix blanks
616         split(/,/, $tmp);
617     unless (grep { $simplename eq $_ } @names) {
618         err($id, "missing $simplename");
619         push @names, $simplename;
620     }
621     foreach my $name (@names) {
622         next if $name eq "";
623         if ($name =~ /\s/) {
624             err($id, "'$name' contains white space")
625         }
626         my $name_sec = "$name($section)";
627         if (! exists $name_collection{$name_sec}) {
628             $name_collection{$name_sec} = $filename;
629         } elsif ($filename eq $name_collection{$name_sec}) {
630             err($id, "$name_sec repeated in NAME section of",
631                  $name_collection{$name_sec});
632         } else {
633             err($id, "$name_sec also in NAME section of",
634                  $name_collection{$name_sec});
635         }
636     }
637
638     my @foreign_names =
639         map { map { s/\s+//g; $_ } split(/,/, $_) }
640         $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
641     foreach (@foreign_names) {
642         $name_collection{$_} = undef; # It still exists!
643     }
644
645     my @links = $contents =~ /L<
646                               # if the link is of the form L<something|name(s)>,
647                               # then remove 'something'.  Note that 'something'
648                               # may contain POD codes as well...
649                               (?:(?:[^\|]|<[^>]*>)*\|)?
650                               # we're only interested in references that have
651                               # a one digit section number
652                               ([^\/>\(]+\(\d\))
653                              /gx;
654     $link_collection{$filename} = [ @links ];
655 }
656
657 sub checklinks {
658     foreach my $filename (sort keys %link_collection) {
659         foreach my $link (@{$link_collection{$filename}}) {
660             err("${filename}:1:", "reference to non-existing $link")
661                 unless exists $name_collection{$link};
662         }
663     }
664 }
665
666 sub publicize {
667     foreach my $name ( parsenum('util/libcrypto.num') ) {
668         $public{$name} = 1;
669     }
670     foreach my $name ( parsenum('util/libssl.num') ) {
671         $public{$name} = 1;
672     }
673     foreach my $name ( parsenum('util/private.num') ) {
674         $public{$name} = 1;
675     }
676 }
677
678 # Cipher/digests to skip if not documented
679 my %skips = (
680     'aes128' => 1,
681     'aes192' => 1,
682     'aes256' => 1,
683     'aria128' => 1,
684     'aria192' => 1,
685     'aria256' => 1,
686     'camellia128' => 1,
687     'camellia192' => 1,
688     'camellia256' => 1,
689     'des' => 1,
690     'des3' => 1,
691     'idea' => 1,
692     'cipher' => 1,
693     'digest' => 1,
694 );
695
696 sub checkflags {
697     my $cmd = shift;
698     my $doc = shift;
699     my %cmdopts;
700     my %docopts;
701     my %localskips;
702
703     # Get the list of options in the command.
704     open CFH, "./apps/openssl list --options $cmd|"
705         || die "Can list options for $cmd, $!";
706     while ( <CFH> ) {
707         chop;
708         s/ .$//;
709         $cmdopts{$_} = 1;
710     }
711     close CFH;
712
713     # Get the list of flags from the synopsis
714     open CFH, "<$doc"
715         || die "Can't open $doc, $!";
716     while ( <CFH> ) {
717         chop;
718         last if /DESCRIPTION/;
719         if ( /=for comment ifdef (.*)/ ) {
720             foreach my $f ( split / /, $1 ) {
721                 $localskips{$f} = 1;
722             }
723             next;
724         }
725         next unless /\[B<-([^ >]+)/;
726         my $opt = $1;
727         $opt = $1 if $opt =~ /I<(.*)/;
728         $docopts{$1} = 1;
729     }
730     close CFH;
731
732     # See what's in the command not the manpage.
733     my @undocced = ();
734     foreach my $k ( keys %cmdopts ) {
735         push @undocced, $k unless $docopts{$k};
736     }
737     if ( scalar @undocced > 0 ) {
738         foreach ( @undocced ) {
739             err("$doc: undocumented option -$_");
740         }
741     }
742
743     # See what's in the command not the manpage.
744     my @unimpl = ();
745     foreach my $k ( keys %docopts ) {
746         push @unimpl, $k unless $cmdopts{$k};
747     }
748     if ( scalar @unimpl > 0 ) {
749         foreach ( @unimpl ) {
750             next if defined $skips{$_} || defined $localskips{$_};
751             err("$cmd documented but not implemented -$_");
752         }
753     }
754 }
755
756 getopts('cdesolnphuv');
757
758 help() if $opt_h;
759
760 $opt_n = 1 if $opt_p;
761 $opt_u = 1 if $opt_d;
762 $opt_e = 1 if $opt_s;
763 $opt_v = 1 if $opt_o || $opt_e;
764
765 die "Cannot use both -u and -v"
766     if $opt_u && $opt_v;
767 die "Cannot use both -d and -e"
768     if $opt_d && $opt_e;
769
770 # We only need to check c, l, n, u and v.
771 # Options d, e, s, o and p imply one of the above.
772 die "Need one of -[cdesolnpuv] flags.\n"
773     unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
774
775 if ( $opt_c ) {
776     my @commands = ();
777
778     # Get list of commands.
779     open FH, "./apps/openssl list -1 -commands|"
780         || die "Can't list commands, $!";
781     while ( <FH> ) {
782         chop;
783         push @commands, $_;
784     }
785     close FH;
786
787     # See if each has a manpage.
788     foreach my $cmd ( @commands ) {
789         next if $cmd eq 'help' || $cmd eq 'exit';
790         my $doc = "doc/man1/$cmd.pod";
791         $doc = "doc/man1/openssl-$cmd.pod" if -f "doc/man1/openssl-$cmd.pod";
792         if ( ! -f "$doc" ) {
793             err("$doc does not exist");
794         } else {
795             checkflags($cmd, $doc);
796         }
797     }
798
799     # See what help is missing.
800     open FH, "./apps/openssl list --missing-help |"
801         || die "Can't list missing help, $!";
802     while ( <FH> ) {
803         chop;
804         my ($cmd, $flag) = split;
805         err("$cmd has no help for -$flag");
806     }
807     close FH;
808
809     exit $status;
810 }
811
812 if ( $opt_l ) {
813     foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'),
814                               glob('doc/internal/*/*.pod'))) {
815         collectnames($_);
816     }
817     checklinks();
818 }
819
820 if ( $opt_n ) {
821     publicize() if $opt_p;
822     foreach (@ARGV ? @ARGV : (glob('doc/*/*.pod'), glob('doc/*/*.pod.in'))) {
823         check($_);
824     }
825     {
826         local $opt_p = undef;
827         foreach (@ARGV ? @ARGV : glob('doc/internal/*/*.pod')) {
828             check($_);
829         }
830     }
831
832     # If not given args, check that all man1 commands are named properly.
833     if ( scalar @ARGV == 0 ) {
834         foreach (glob('doc/man1/*.pod')) {
835             next if /CA.pl/ || /openssl.pod/;
836             err("$_ doesn't start with openssl-") unless /openssl-/;
837         }
838     }
839 }
840
841 if ( $opt_u || $opt_v) {
842     my %temp = getdocced('doc/man3');
843     foreach ( keys %temp ) {
844         $docced{$_} = $temp{$_};
845     }
846     if ($opt_o) {
847         printem('crypto', 'util/libcrypto.num', 'util/missingcrypto111.txt');
848         printem('ssl', 'util/libssl.num', 'util/missingssl111.txt');
849     } else {
850         printem('crypto', 'util/libcrypto.num', 'util/missingcrypto.txt');
851         printem('ssl', 'util/libssl.num', 'util/missingssl.txt');
852     }
853     checkmacros();
854 }
855
856 exit $status;