xref: /freebsd/crypto/openssl/util/find-doc-nits (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertrequire 5.10.0;
11*e0c4386eSCy Schubertuse warnings;
12*e0c4386eSCy Schubertuse strict;
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubertuse Carp qw(:DEFAULT cluck);
15*e0c4386eSCy Schubertuse Pod::Checker;
16*e0c4386eSCy Schubertuse File::Find;
17*e0c4386eSCy Schubertuse File::Basename;
18*e0c4386eSCy Schubertuse File::Spec::Functions;
19*e0c4386eSCy Schubertuse Getopt::Std;
20*e0c4386eSCy Schubertuse FindBin;
21*e0c4386eSCy Schubertuse lib "$FindBin::Bin/perl";
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertuse OpenSSL::Util::Pod;
24*e0c4386eSCy Schubert
25*e0c4386eSCy Schubertuse lib '.';
26*e0c4386eSCy Schubertuse configdata;
27*e0c4386eSCy Schubert
28*e0c4386eSCy Schubert# Set to 1 for debug output
29*e0c4386eSCy Schubertmy $debug = 0;
30*e0c4386eSCy Schubert
31*e0c4386eSCy Schubert# Options.
32*e0c4386eSCy Schubertour($opt_d);
33*e0c4386eSCy Schubertour($opt_e);
34*e0c4386eSCy Schubertour($opt_s);
35*e0c4386eSCy Schubertour($opt_o);
36*e0c4386eSCy Schubertour($opt_h);
37*e0c4386eSCy Schubertour($opt_l);
38*e0c4386eSCy Schubertour($opt_m);
39*e0c4386eSCy Schubertour($opt_n);
40*e0c4386eSCy Schubertour($opt_p);
41*e0c4386eSCy Schubertour($opt_u);
42*e0c4386eSCy Schubertour($opt_v);
43*e0c4386eSCy Schubertour($opt_c);
44*e0c4386eSCy Schubert
45*e0c4386eSCy Schubert# Print usage message and exit.
46*e0c4386eSCy Schubertsub help {
47*e0c4386eSCy Schubert    print <<EOF;
48*e0c4386eSCy SchubertFind small errors (nits) in documentation.  Options:
49*e0c4386eSCy Schubert    -c List undocumented commands, undocumented options and unimplemented options.
50*e0c4386eSCy Schubert    -d Detailed list of undocumented (implies -u)
51*e0c4386eSCy Schubert    -e Detailed list of new undocumented (implies -v)
52*e0c4386eSCy Schubert    -h Print this help message
53*e0c4386eSCy Schubert    -l Print bogus links
54*e0c4386eSCy Schubert    -m Name(s) of manuals to focus on. Default: man1,man3,man5,man7
55*e0c4386eSCy Schubert    -n Print nits in POD pages
56*e0c4386eSCy Schubert    -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
57*e0c4386eSCy Schubert    -u Count undocumented functions
58*e0c4386eSCy Schubert    -v Count new undocumented functions
59*e0c4386eSCy SchubertEOF
60*e0c4386eSCy Schubert    exit;
61*e0c4386eSCy Schubert}
62*e0c4386eSCy Schubert
63*e0c4386eSCy Schubertgetopts('cdehlm:nouv');
64*e0c4386eSCy Schubert
65*e0c4386eSCy Schuberthelp() if $opt_h;
66*e0c4386eSCy Schubert$opt_u = 1 if $opt_d;
67*e0c4386eSCy Schubert$opt_v = 1 if $opt_o || $opt_e;
68*e0c4386eSCy Schubertdie "Cannot use both -u and -v"
69*e0c4386eSCy Schubert    if $opt_u && $opt_v;
70*e0c4386eSCy Schubertdie "Cannot use both -d and -e"
71*e0c4386eSCy Schubert    if $opt_d && $opt_e;
72*e0c4386eSCy Schubert
73*e0c4386eSCy Schubert# We only need to check c, l, n, u and v.
74*e0c4386eSCy Schubert# Options d, e, o imply one of the above.
75*e0c4386eSCy Schubertdie "Need one of -[cdehlnouv] flags.\n"
76*e0c4386eSCy Schubert    unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
77*e0c4386eSCy Schubert
78*e0c4386eSCy Schubert
79*e0c4386eSCy Schubertmy $temp = '/tmp/docnits.txt';
80*e0c4386eSCy Schubertmy $OUT;
81*e0c4386eSCy Schubertmy $status = 0;
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert$opt_m = "man1,man3,man5,man7" unless $opt_m;
84*e0c4386eSCy Schubertdie "Argument of -m option may contain only man1, man3, man5, and/or man7"
85*e0c4386eSCy Schubert    unless $opt_m =~ /^(man[1357][, ]?)*$/;
86*e0c4386eSCy Schubertmy @sections = ( split /[, ]/, $opt_m );
87*e0c4386eSCy Schubert
88*e0c4386eSCy Schubertmy %mandatory_sections = (
89*e0c4386eSCy Schubert    '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
90*e0c4386eSCy Schubert    1   => [ 'SYNOPSIS', 'OPTIONS' ],
91*e0c4386eSCy Schubert    3   => [ 'SYNOPSIS', 'RETURN VALUES' ],
92*e0c4386eSCy Schubert    5   => [ ],
93*e0c4386eSCy Schubert    7   => [ ]
94*e0c4386eSCy Schubert                         );
95*e0c4386eSCy Schubert
96*e0c4386eSCy Schubert# Symbols that we ignored.
97*e0c4386eSCy Schubert# They are reserved macros that we currently don't document
98*e0c4386eSCy Schubertmy $ignored = qr/(?| ^i2d_
99*e0c4386eSCy Schubert                 |   ^d2i_
100*e0c4386eSCy Schubert                 |   ^DEPRECATEDIN
101*e0c4386eSCy Schubert                 |   ^OSSL_DEPRECATED
102*e0c4386eSCy Schubert                 |   \Q_fnsig(3)\E$
103*e0c4386eSCy Schubert                 |   ^IMPLEMENT_
104*e0c4386eSCy Schubert                 |   ^_?DECLARE_
105*e0c4386eSCy Schubert                 |   ^sk_
106*e0c4386eSCy Schubert                 |   ^SKM_DEFINE_STACK_OF_INTERNAL
107*e0c4386eSCy Schubert                 |   ^lh_
108*e0c4386eSCy Schubert                 |   ^DEFINE_LHASH_OF_INTERNAL
109*e0c4386eSCy Schubert                 )/x;
110*e0c4386eSCy Schubert
111*e0c4386eSCy Schubert# A common regexp for C symbol names
112*e0c4386eSCy Schubertmy $C_symbol = qr/\b[[:alpha:]][_[:alnum:]]*\b/;
113*e0c4386eSCy Schubert
114*e0c4386eSCy Schubert# Collect all POD files, both internal and public, and regardless of location
115*e0c4386eSCy Schubert# We collect them in a hash table with each file being a key, so we can attach
116*e0c4386eSCy Schubert# tags to them.  For example, internal docs will have the word "internal"
117*e0c4386eSCy Schubert# attached to them.
118*e0c4386eSCy Schubertmy %files = ();
119*e0c4386eSCy Schubert# We collect files names on the fly, on known tag basis
120*e0c4386eSCy Schubertmy %collected_tags = ();
121*e0c4386eSCy Schubert# We cache results based on tags
122*e0c4386eSCy Schubertmy %collected_results = ();
123*e0c4386eSCy Schubert
124*e0c4386eSCy Schubert# files OPTIONS
125*e0c4386eSCy Schubert#
126*e0c4386eSCy Schubert# Example:
127*e0c4386eSCy Schubert#
128*e0c4386eSCy Schubert#       files(TAGS => 'manual');
129*e0c4386eSCy Schubert#       files(TAGS => [ 'manual', 'man1' ]);
130*e0c4386eSCy Schubert#
131*e0c4386eSCy Schubert# This function returns an array of files corresponding to a set of tags
132*e0c4386eSCy Schubert# given with the options "TAGS".  The value of this option can be a single
133*e0c4386eSCy Schubert# word, or an array of several words, which work as inclusive or exclusive
134*e0c4386eSCy Schubert# selectors.  Inclusive selectors are used to add one more set of files to
135*e0c4386eSCy Schubert# the returned array, while exclusive selectors limit the set of files added
136*e0c4386eSCy Schubert# to the array.  The recognised tag values are:
137*e0c4386eSCy Schubert#
138*e0c4386eSCy Schubert# 'public_manual'       - inclusive selector, adds public manuals to the
139*e0c4386eSCy Schubert#                         returned array of files.
140*e0c4386eSCy Schubert# 'internal_manual'     - inclusive selector, adds internal manuals to the
141*e0c4386eSCy Schubert#                         returned array of files.
142*e0c4386eSCy Schubert# 'manual'              - inclusive selector, adds any manual to the returned
143*e0c4386eSCy Schubert#                         array of files.  This is really a shorthand for
144*e0c4386eSCy Schubert#                         'public_manual' and 'internal_manual' combined.
145*e0c4386eSCy Schubert# 'public_header'       - inclusive selector, adds public headers to the
146*e0c4386eSCy Schubert#                         returned array of files.
147*e0c4386eSCy Schubert# 'header'              - inclusive selector, adds any header file to the
148*e0c4386eSCy Schubert#                         returned array of files.  Since we currently only
149*e0c4386eSCy Schubert#                         care about public headers, this is exactly
150*e0c4386eSCy Schubert#                         equivalent to 'public_header', but is present for
151*e0c4386eSCy Schubert#                         consistency.
152*e0c4386eSCy Schubert#
153*e0c4386eSCy Schubert# 'man1', 'man3', 'man5', 'man7'
154*e0c4386eSCy Schubert#                       - exclusive selectors, only applicable together with
155*e0c4386eSCy Schubert#                         any of the manual selectors.  If any of these are
156*e0c4386eSCy Schubert#                         present, only the manuals from the given sections
157*e0c4386eSCy Schubert#                         will be included.  If none of these are present,
158*e0c4386eSCy Schubert#                         the manuals from all sections will be returned.
159*e0c4386eSCy Schubert#
160*e0c4386eSCy Schubert# All returned manual files come from configdata.pm.
161*e0c4386eSCy Schubert# All returned header files come from looking inside
162*e0c4386eSCy Schubert# "$config{sourcedir}/include/openssl"
163*e0c4386eSCy Schubert#
164*e0c4386eSCy Schubertsub files {
165*e0c4386eSCy Schubert    my %opts = ( @_ );          # Make a copy of the arguments
166*e0c4386eSCy Schubert
167*e0c4386eSCy Schubert    $opts{TAGS} = [ $opts{TAGS} ] if ref($opts{TAGS}) eq '';
168*e0c4386eSCy Schubert
169*e0c4386eSCy Schubert    croak "No tags given, or not an array"
170*e0c4386eSCy Schubert        unless exists $opts{TAGS} && ref($opts{TAGS}) eq 'ARRAY';
171*e0c4386eSCy Schubert
172*e0c4386eSCy Schubert    my %tags = map { $_ => 1 } @{$opts{TAGS}};
173*e0c4386eSCy Schubert    $tags{public_manual} = 1
174*e0c4386eSCy Schubert        if $tags{manual} && ($tags{public} // !$tags{internal});
175*e0c4386eSCy Schubert    $tags{internal_manual} = 1
176*e0c4386eSCy Schubert        if $tags{manual} && ($tags{internal} // !$tags{public});
177*e0c4386eSCy Schubert    $tags{public_header} = 1
178*e0c4386eSCy Schubert        if $tags{header} && ($tags{public} // !$tags{internal});
179*e0c4386eSCy Schubert    delete $tags{manual};
180*e0c4386eSCy Schubert    delete $tags{header};
181*e0c4386eSCy Schubert    delete $tags{public};
182*e0c4386eSCy Schubert    delete $tags{internal};
183*e0c4386eSCy Schubert
184*e0c4386eSCy Schubert    my $tags_as_key = join(':', sort keys %tags);
185*e0c4386eSCy Schubert
186*e0c4386eSCy Schubert    cluck  "DEBUG[files]: This is how we got here!" if $debug;
187*e0c4386eSCy Schubert    print STDERR "DEBUG[files]: tags: $tags_as_key\n" if $debug;
188*e0c4386eSCy Schubert
189*e0c4386eSCy Schubert    my %tags_to_collect = ( map { $_ => 1 }
190*e0c4386eSCy Schubert                            grep { !exists $collected_tags{$_} }
191*e0c4386eSCy Schubert                            keys %tags );
192*e0c4386eSCy Schubert
193*e0c4386eSCy Schubert    if ($tags_to_collect{public_manual}) {
194*e0c4386eSCy Schubert        print STDERR "DEBUG[files]: collecting public manuals\n"
195*e0c4386eSCy Schubert            if $debug;
196*e0c4386eSCy Schubert
197*e0c4386eSCy Schubert        # The structure in configdata.pm is that $unified_info{mandocs}
198*e0c4386eSCy Schubert        # contains lists of man files, and in turn, $unified_info{depends}
199*e0c4386eSCy Schubert        # contains hash tables showing which POD file each of those man
200*e0c4386eSCy Schubert        # files depend on.  We use that information to find the POD files,
201*e0c4386eSCy Schubert        # and to attach the man section they belong to as tags
202*e0c4386eSCy Schubert        foreach my $mansect ( @sections ) {
203*e0c4386eSCy Schubert            foreach ( map { @{$unified_info{depends}->{$_}} }
204*e0c4386eSCy Schubert                      @{$unified_info{mandocs}->{$mansect}} ) {
205*e0c4386eSCy Schubert                $files{$_} = { $mansect => 1, public_manual => 1 };
206*e0c4386eSCy Schubert            }
207*e0c4386eSCy Schubert        }
208*e0c4386eSCy Schubert        $collected_tags{public_manual} = 1;
209*e0c4386eSCy Schubert    }
210*e0c4386eSCy Schubert
211*e0c4386eSCy Schubert    if ($tags_to_collect{internal_manual}) {
212*e0c4386eSCy Schubert        print STDERR "DEBUG[files]: collecting internal manuals\n"
213*e0c4386eSCy Schubert            if $debug;
214*e0c4386eSCy Schubert
215*e0c4386eSCy Schubert        # We don't have the internal docs in configdata.pm.  However, they
216*e0c4386eSCy Schubert        # are all in the source tree, so they're easy to find.
217*e0c4386eSCy Schubert        foreach my $mansect ( @sections ) {
218*e0c4386eSCy Schubert            foreach ( glob(catfile($config{sourcedir},
219*e0c4386eSCy Schubert                                   'doc', 'internal', $mansect, '*.pod')) ) {
220*e0c4386eSCy Schubert                $files{$_} = { $mansect => 1, internal_manual => 1 };
221*e0c4386eSCy Schubert            }
222*e0c4386eSCy Schubert        }
223*e0c4386eSCy Schubert        $collected_tags{internal_manual} = 1;
224*e0c4386eSCy Schubert    }
225*e0c4386eSCy Schubert
226*e0c4386eSCy Schubert    if ($tags_to_collect{public_header}) {
227*e0c4386eSCy Schubert        print STDERR "DEBUG[files]: collecting public headers\n"
228*e0c4386eSCy Schubert            if $debug;
229*e0c4386eSCy Schubert
230*e0c4386eSCy Schubert        foreach ( glob(catfile($config{sourcedir},
231*e0c4386eSCy Schubert                               'include', 'openssl', '*.h')) ) {
232*e0c4386eSCy Schubert            $files{$_} = { public_header => 1 };
233*e0c4386eSCy Schubert        }
234*e0c4386eSCy Schubert    }
235*e0c4386eSCy Schubert
236*e0c4386eSCy Schubert    my @result = @{$collected_results{$tags_as_key} // []};
237*e0c4386eSCy Schubert
238*e0c4386eSCy Schubert    if (!@result) {
239*e0c4386eSCy Schubert        # Produce a result based on caller tags
240*e0c4386eSCy Schubert        foreach my $type ( ( 'public_manual', 'internal_manual' ) ) {
241*e0c4386eSCy Schubert            next unless $tags{$type};
242*e0c4386eSCy Schubert
243*e0c4386eSCy Schubert            # If caller asked for specific sections, we care about sections.
244*e0c4386eSCy Schubert            # Otherwise, we give back all of them.
245*e0c4386eSCy Schubert            my @selected_sections =
246*e0c4386eSCy Schubert                grep { $tags{$_} } @sections;
247*e0c4386eSCy Schubert            @selected_sections = @sections unless @selected_sections;
248*e0c4386eSCy Schubert
249*e0c4386eSCy Schubert            foreach my $section ( ( @selected_sections ) ) {
250*e0c4386eSCy Schubert                push @result,
251*e0c4386eSCy Schubert                    ( sort { basename($a) cmp basename($b) }
252*e0c4386eSCy Schubert                      grep { $files{$_}->{$type} && $files{$_}->{$section} }
253*e0c4386eSCy Schubert                      keys %files );
254*e0c4386eSCy Schubert            }
255*e0c4386eSCy Schubert        }
256*e0c4386eSCy Schubert        if ($tags{public_header}) {
257*e0c4386eSCy Schubert            push @result,
258*e0c4386eSCy Schubert                ( sort { basename($a) cmp basename($b) }
259*e0c4386eSCy Schubert                  grep { $files{$_}->{public_header} }
260*e0c4386eSCy Schubert                  keys %files );
261*e0c4386eSCy Schubert        }
262*e0c4386eSCy Schubert
263*e0c4386eSCy Schubert        if ($debug) {
264*e0c4386eSCy Schubert            print STDERR "DEBUG[files]: result:\n";
265*e0c4386eSCy Schubert            print STDERR "DEBUG[files]:     $_\n" foreach @result;
266*e0c4386eSCy Schubert        }
267*e0c4386eSCy Schubert        $collected_results{$tags_as_key} = [ @result ];
268*e0c4386eSCy Schubert    }
269*e0c4386eSCy Schubert
270*e0c4386eSCy Schubert    return @result;
271*e0c4386eSCy Schubert}
272*e0c4386eSCy Schubert
273*e0c4386eSCy Schubert# Print error message, set $status.
274*e0c4386eSCy Schubertsub err {
275*e0c4386eSCy Schubert    print join(" ", @_), "\n";
276*e0c4386eSCy Schubert    $status = 1
277*e0c4386eSCy Schubert}
278*e0c4386eSCy Schubert
279*e0c4386eSCy Schubert# Cross-check functions in the NAME and SYNOPSIS section.
280*e0c4386eSCy Schubertsub name_synopsis {
281*e0c4386eSCy Schubert    my $id = shift;
282*e0c4386eSCy Schubert    my $filename = shift;
283*e0c4386eSCy Schubert    my $contents = shift;
284*e0c4386eSCy Schubert
285*e0c4386eSCy Schubert    # Get NAME section and all words in it.
286*e0c4386eSCy Schubert    return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
287*e0c4386eSCy Schubert    my $tmp = $1;
288*e0c4386eSCy Schubert    $tmp =~ tr/\n/ /;
289*e0c4386eSCy Schubert    err($id, "Trailing comma before - in NAME")
290*e0c4386eSCy Schubert        if $tmp =~ /, *-/;
291*e0c4386eSCy Schubert    $tmp =~ s/ -.*//g;
292*e0c4386eSCy Schubert    err($id, "POD markup among the names in NAME")
293*e0c4386eSCy Schubert        if $tmp =~ /[<>]/;
294*e0c4386eSCy Schubert    $tmp =~ s/  */ /g;
295*e0c4386eSCy Schubert    err($id, "Missing comma in NAME")
296*e0c4386eSCy Schubert        if $tmp =~ /[^,] /;
297*e0c4386eSCy Schubert
298*e0c4386eSCy Schubert    my $dirname = dirname($filename);
299*e0c4386eSCy Schubert    my $section = basename($dirname);
300*e0c4386eSCy Schubert    my $simplename = basename($filename, ".pod");
301*e0c4386eSCy Schubert    my $foundfilename = 0;
302*e0c4386eSCy Schubert    my %foundfilenames = ();
303*e0c4386eSCy Schubert    my %names;
304*e0c4386eSCy Schubert    foreach my $n ( split ',', $tmp ) {
305*e0c4386eSCy Schubert        $n =~ s/^\s+//;
306*e0c4386eSCy Schubert        $n =~ s/\s+$//;
307*e0c4386eSCy Schubert        err($id, "The name '$n' contains white-space")
308*e0c4386eSCy Schubert            if $n =~ /\s/;
309*e0c4386eSCy Schubert        $names{$n} = 1;
310*e0c4386eSCy Schubert        $foundfilename++ if $n eq $simplename;
311*e0c4386eSCy Schubert        $foundfilenames{$n} = 1
312*e0c4386eSCy Schubert            if ( ( grep { basename($_) eq "$n.pod" }
313*e0c4386eSCy Schubert                   files(TAGS => [ 'manual', $section ]) )
314*e0c4386eSCy Schubert                 && $n ne $simplename );
315*e0c4386eSCy Schubert    }
316*e0c4386eSCy Schubert    err($id, "The following exist as other .pod files:",
317*e0c4386eSCy Schubert         sort keys %foundfilenames)
318*e0c4386eSCy Schubert        if %foundfilenames;
319*e0c4386eSCy Schubert    err($id, "$simplename (filename) missing from NAME section")
320*e0c4386eSCy Schubert        unless $foundfilename;
321*e0c4386eSCy Schubert
322*e0c4386eSCy Schubert    # Find all functions in SYNOPSIS
323*e0c4386eSCy Schubert    return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
324*e0c4386eSCy Schubert    my $syn = $1;
325*e0c4386eSCy Schubert    my $ignore_until = undef;   # If defined, this is a regexp
326*e0c4386eSCy Schubert    # Remove all non-code lines
327*e0c4386eSCy Schubert    $syn =~ s/^(?:\s*?|\S.*?)$//msg;
328*e0c4386eSCy Schubert    # Remove all comments
329*e0c4386eSCy Schubert    $syn =~ s/\/\*.*?\*\///msg;
330*e0c4386eSCy Schubert    while ( $syn ) {
331*e0c4386eSCy Schubert        # "env" lines end at a newline.
332*e0c4386eSCy Schubert        # Preprocessor lines start with a # and end at a newline.
333*e0c4386eSCy Schubert        # Other lines end with a semicolon, and may cover more than
334*e0c4386eSCy Schubert        # one physical line.
335*e0c4386eSCy Schubert        if ( $syn !~ /^ \s*(env .*?|#.*?|.*?;)\s*$/ms ) {
336*e0c4386eSCy Schubert            err($id, "Can't parse rest of synopsis:\n$syn\n(declarations not ending with a semicolon (;)?)");
337*e0c4386eSCy Schubert            last;
338*e0c4386eSCy Schubert        }
339*e0c4386eSCy Schubert        my $line = $1;
340*e0c4386eSCy Schubert        $syn = $';
341*e0c4386eSCy Schubert
342*e0c4386eSCy Schubert        print STDERR "DEBUG[name_synopsis] \$line = '$line'\n" if $debug;
343*e0c4386eSCy Schubert
344*e0c4386eSCy Schubert        # Special code to skip over documented structures
345*e0c4386eSCy Schubert        if ( defined $ignore_until) {
346*e0c4386eSCy Schubert            next if $line !~ /$ignore_until/;
347*e0c4386eSCy Schubert            $ignore_until = undef;
348*e0c4386eSCy Schubert            next;
349*e0c4386eSCy Schubert        }
350*e0c4386eSCy Schubert        if ( $line =~ /^\s*(?:typedef\s+)?struct(?:\s+\S+)\s*\{/ ) {
351*e0c4386eSCy Schubert            $ignore_until = qr/\}.*?;/;
352*e0c4386eSCy Schubert            next;
353*e0c4386eSCy Schubert        }
354*e0c4386eSCy Schubert
355*e0c4386eSCy Schubert        my $sym;
356*e0c4386eSCy Schubert        my $is_prototype = 1;
357*e0c4386eSCy Schubert        $line =~ s/LHASH_OF\([^)]+\)/int/g;
358*e0c4386eSCy Schubert        $line =~ s/STACK_OF\([^)]+\)/int/g;
359*e0c4386eSCy Schubert        $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
360*e0c4386eSCy Schubert        $line =~ s/__declspec\([^)]+\)//;
361*e0c4386eSCy Schubert
362*e0c4386eSCy Schubert        ## We don't prohibit that space, to allow typedefs looking like
363*e0c4386eSCy Schubert        ## this:
364*e0c4386eSCy Schubert        ##
365*e0c4386eSCy Schubert        ## typedef int (fantastically_long_name_breaks_80char_limit)
366*e0c4386eSCy Schubert        ##     (fantastically_long_name_breaks_80char_limit *something);
367*e0c4386eSCy Schubert        ##
368*e0c4386eSCy Schubert        #if ( $line =~ /typedef.*\(\*?\S+\)\s+\(/ ) {
369*e0c4386eSCy Schubert        #    # a callback function with whitespace before the argument list:
370*e0c4386eSCy Schubert        #    # typedef ... (*NAME) (...
371*e0c4386eSCy Schubert        #    # typedef ... (NAME) (...
372*e0c4386eSCy Schubert        #    err($id, "Function typedef has space before arg list: $line");
373*e0c4386eSCy Schubert        #}
374*e0c4386eSCy Schubert
375*e0c4386eSCy Schubert        if ( $line =~ /env (\S*)=/ ) {
376*e0c4386eSCy Schubert            # environment variable env NAME=...
377*e0c4386eSCy Schubert            $sym = $1;
378*e0c4386eSCy Schubert        } elsif ( $line =~ /typedef.*\(\*?($C_symbol)\)\s*\(/ ) {
379*e0c4386eSCy Schubert            # a callback function pointer: typedef ... (*NAME)(...
380*e0c4386eSCy Schubert            # a callback function signature: typedef ... (NAME)(...
381*e0c4386eSCy Schubert            $sym = $1;
382*e0c4386eSCy Schubert        } elsif ( $line =~ /typedef.*($C_symbol)\s*\(/ ) {
383*e0c4386eSCy Schubert            # a callback function signature: typedef ... NAME(...
384*e0c4386eSCy Schubert            $sym = $1;
385*e0c4386eSCy Schubert        } elsif ( $line =~ /typedef.*($C_symbol);/ ) {
386*e0c4386eSCy Schubert            # a simple typedef: typedef ... NAME;
387*e0c4386eSCy Schubert            $is_prototype = 0;
388*e0c4386eSCy Schubert            $sym = $1;
389*e0c4386eSCy Schubert        } elsif ( $line =~ /enum ($C_symbol) \{/ ) {
390*e0c4386eSCy Schubert            # an enumeration: enum ... {
391*e0c4386eSCy Schubert            $sym = $1;
392*e0c4386eSCy Schubert        } elsif ( $line =~ /#\s*(?:define|undef) ($C_symbol)/ ) {
393*e0c4386eSCy Schubert            $is_prototype = 0;
394*e0c4386eSCy Schubert            $sym = $1;
395*e0c4386eSCy Schubert        } elsif ( $line =~ /^[^\(]*?\(\*($C_symbol)\s*\(/ ) {
396*e0c4386eSCy Schubert            # a function returning a function pointer: TYPE (*NAME(args))(args)
397*e0c4386eSCy Schubert            $sym = $1;
398*e0c4386eSCy Schubert        } elsif ( $line =~ /^[^\(]*?($C_symbol)\s*\(/ ) {
399*e0c4386eSCy Schubert            # a simple function declaration
400*e0c4386eSCy Schubert            $sym = $1;
401*e0c4386eSCy Schubert        }
402*e0c4386eSCy Schubert        else {
403*e0c4386eSCy Schubert            next;
404*e0c4386eSCy Schubert        }
405*e0c4386eSCy Schubert
406*e0c4386eSCy Schubert        print STDERR "DEBUG[name_synopsis] \$sym = '$sym'\n" if $debug;
407*e0c4386eSCy Schubert
408*e0c4386eSCy Schubert        err($id, "$sym missing from NAME section")
409*e0c4386eSCy Schubert            unless defined $names{$sym};
410*e0c4386eSCy Schubert        $names{$sym} = 2;
411*e0c4386eSCy Schubert
412*e0c4386eSCy Schubert        # Do some sanity checks on the prototype.
413*e0c4386eSCy Schubert        err($id, "Prototype missing spaces around commas: $line")
414*e0c4386eSCy Schubert            if $is_prototype && $line =~ /[a-z0-9],[^\s]/;
415*e0c4386eSCy Schubert    }
416*e0c4386eSCy Schubert
417*e0c4386eSCy Schubert    foreach my $n ( keys %names ) {
418*e0c4386eSCy Schubert        next if $names{$n} == 2;
419*e0c4386eSCy Schubert        err($id, "$n missing from SYNOPSIS")
420*e0c4386eSCy Schubert    }
421*e0c4386eSCy Schubert}
422*e0c4386eSCy Schubert
423*e0c4386eSCy Schubert# Check if SECTION ($3) is located before BEFORE ($4)
424*e0c4386eSCy Schubertsub check_section_location {
425*e0c4386eSCy Schubert    my $id = shift;
426*e0c4386eSCy Schubert    my $contents = shift;
427*e0c4386eSCy Schubert    my $section = shift;
428*e0c4386eSCy Schubert    my $before = shift;
429*e0c4386eSCy Schubert
430*e0c4386eSCy Schubert    return unless $contents =~ /=head1 $section/
431*e0c4386eSCy Schubert        and $contents =~ /=head1 $before/;
432*e0c4386eSCy Schubert    err($id, "$section should appear before $before section")
433*e0c4386eSCy Schubert        if $contents =~ /=head1 $before.*=head1 $section/ms;
434*e0c4386eSCy Schubert}
435*e0c4386eSCy Schubert
436*e0c4386eSCy Schubert# Check if a =head1 is duplicated, or a =headX is duplicated within a
437*e0c4386eSCy Schubert# =head1.  Treats =head2 =head3 as equivalent -- it doesn't reset the head3
438*e0c4386eSCy Schubert# sets if it finds a =head2 -- but that is good enough for now. Also check
439*e0c4386eSCy Schubert# for proper capitalization, trailing periods, etc.
440*e0c4386eSCy Schubertsub check_head_style {
441*e0c4386eSCy Schubert    my $id = shift;
442*e0c4386eSCy Schubert    my $contents = shift;
443*e0c4386eSCy Schubert    my %head1;
444*e0c4386eSCy Schubert    my %subheads;
445*e0c4386eSCy Schubert
446*e0c4386eSCy Schubert    foreach my $line ( split /\n+/, $contents ) {
447*e0c4386eSCy Schubert        next unless $line =~ /^=head/;
448*e0c4386eSCy Schubert        if ( $line =~ /head1/ ) {
449*e0c4386eSCy Schubert            err($id, "Duplicate section $line")
450*e0c4386eSCy Schubert                if defined $head1{$line};
451*e0c4386eSCy Schubert            $head1{$line} = 1;
452*e0c4386eSCy Schubert            %subheads = ();
453*e0c4386eSCy Schubert        } else {
454*e0c4386eSCy Schubert            err($id, "Duplicate subsection $line")
455*e0c4386eSCy Schubert                if defined $subheads{$line};
456*e0c4386eSCy Schubert            $subheads{$line} = 1;
457*e0c4386eSCy Schubert        }
458*e0c4386eSCy Schubert        err($id, "Period in =head")
459*e0c4386eSCy Schubert            if $line =~ /\.[^\w]/ or $line =~ /\.$/;
460*e0c4386eSCy Schubert        err($id, "not all uppercase in =head1")
461*e0c4386eSCy Schubert            if $line =~ /head1.*[a-z]/;
462*e0c4386eSCy Schubert        err($id, "All uppercase in subhead")
463*e0c4386eSCy Schubert            if $line =~ /head[234][ A-Z0-9]+$/;
464*e0c4386eSCy Schubert    }
465*e0c4386eSCy Schubert}
466*e0c4386eSCy Schubert
467*e0c4386eSCy Schubert# Because we have options and symbols with extra markup, we need
468*e0c4386eSCy Schubert# to take that into account, so we need a regexp that extracts
469*e0c4386eSCy Schubert# markup chunks, including recursive markup.
470*e0c4386eSCy Schubert# please read up on /(?R)/ in perlre(1)
471*e0c4386eSCy Schubert# (note: order is important, (?R) needs to come before .)
472*e0c4386eSCy Schubert# (note: non-greedy is important, or something like 'B<foo> and B<bar>'
473*e0c4386eSCy Schubert# will be captured as one item)
474*e0c4386eSCy Schubertmy $markup_re =
475*e0c4386eSCy Schubert    qr/(                        # Capture group
476*e0c4386eSCy Schubert           [BIL]<               # The start of what we recurse on
477*e0c4386eSCy Schubert           (?:(?-1)|.)*?        # recurse the whole regexp (referring to
478*e0c4386eSCy Schubert                                # the last opened capture group, i.e. the
479*e0c4386eSCy Schubert                                # start of this regexp), or pick next
480*e0c4386eSCy Schubert                                # character.  Do NOT be greedy!
481*e0c4386eSCy Schubert           >                    # The end of what we recurse on
482*e0c4386eSCy Schubert       )/x;                     # (the x allows this sort of split up regexp)
483*e0c4386eSCy Schubert
484*e0c4386eSCy Schubert# Options must start with a dash, followed by a letter, possibly
485*e0c4386eSCy Schubert# followed by letters, digits, dashes and underscores, and the last
486*e0c4386eSCy Schubert# character must be a letter or a digit.
487*e0c4386eSCy Schubert# We do also accept the single -? or -n, where n is a digit
488*e0c4386eSCy Schubertmy $option_re =
489*e0c4386eSCy Schubert    qr/(?:
490*e0c4386eSCy Schubert            \?                  # Single question mark
491*e0c4386eSCy Schubert            |
492*e0c4386eSCy Schubert            \d                  # Single digit
493*e0c4386eSCy Schubert            |
494*e0c4386eSCy Schubert            -                   # Single dash (--)
495*e0c4386eSCy Schubert            |
496*e0c4386eSCy Schubert            [[:alpha:]](?:[-_[:alnum:]]*?[[:alnum:]])?
497*e0c4386eSCy Schubert       )/x;
498*e0c4386eSCy Schubert
499*e0c4386eSCy Schubert# Helper function to check if a given $thing is properly marked up
500*e0c4386eSCy Schubert# option.  It returns one of these values:
501*e0c4386eSCy Schubert#     undef         if it's not an option
502*e0c4386eSCy Schubert#     ""            if it's a malformed option
503*e0c4386eSCy Schubert#     $unwrapped    the option with the outermost B<> wrapping removed.
504*e0c4386eSCy Schubertsub normalise_option {
505*e0c4386eSCy Schubert    my $id = shift;
506*e0c4386eSCy Schubert    my $filename = shift;
507*e0c4386eSCy Schubert    my $thing = shift;
508*e0c4386eSCy Schubert
509*e0c4386eSCy Schubert    my $unwrapped = $thing;
510*e0c4386eSCy Schubert    my $unmarked = $thing;
511*e0c4386eSCy Schubert
512*e0c4386eSCy Schubert    # $unwrapped is the option with the outer B<> markup removed
513*e0c4386eSCy Schubert    $unwrapped =~ s/^B<//;
514*e0c4386eSCy Schubert    $unwrapped =~ s/>$//;
515*e0c4386eSCy Schubert    # $unmarked is the option with *all* markup removed
516*e0c4386eSCy Schubert    $unmarked =~ s/[BIL]<|>//msg;
517*e0c4386eSCy Schubert
518*e0c4386eSCy Schubert
519*e0c4386eSCy Schubert    # If we found an option, check it, collect it
520*e0c4386eSCy Schubert    if ( $unwrapped =~ /^\s*-/ ) {
521*e0c4386eSCy Schubert        return $unwrapped       # return option with outer B<> removed
522*e0c4386eSCy Schubert            if $unmarked =~ /^-${option_re}$/;
523*e0c4386eSCy Schubert        return "";              # Malformed option
524*e0c4386eSCy Schubert    }
525*e0c4386eSCy Schubert    return undef;               # Something else
526*e0c4386eSCy Schubert}
527*e0c4386eSCy Schubert
528*e0c4386eSCy Schubert# Checks of command option (man1) formatting.  The man1 checks are
529*e0c4386eSCy Schubert# restricted to the SYNOPSIS and OPTIONS sections, the rest is too
530*e0c4386eSCy Schubert# free form, we simply cannot be too strict there.
531*e0c4386eSCy Schubert
532*e0c4386eSCy Schubertsub option_check {
533*e0c4386eSCy Schubert    my $id = shift;
534*e0c4386eSCy Schubert    my $filename = shift;
535*e0c4386eSCy Schubert    my $contents = shift;
536*e0c4386eSCy Schubert
537*e0c4386eSCy Schubert    my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1);
538*e0c4386eSCy Schubert
539*e0c4386eSCy Schubert    # Some pages have more than one OPTIONS section, let's make sure
540*e0c4386eSCy Schubert    # to get them all
541*e0c4386eSCy Schubert    my $options = '';
542*e0c4386eSCy Schubert    while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) {
543*e0c4386eSCy Schubert        $options .= $1;
544*e0c4386eSCy Schubert    }
545*e0c4386eSCy Schubert
546*e0c4386eSCy Schubert    # Look for options with no or incorrect markup
547*e0c4386eSCy Schubert    while ( $synopsis =~
548*e0c4386eSCy Schubert            /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) {
549*e0c4386eSCy Schubert        err($id, "Malformed option [1] in SYNOPSIS: $&");
550*e0c4386eSCy Schubert    }
551*e0c4386eSCy Schubert
552*e0c4386eSCy Schubert    my @synopsis;
553*e0c4386eSCy Schubert    while ( $synopsis =~ /$markup_re/msg ) {
554*e0c4386eSCy Schubert        my $found = $&;
555*e0c4386eSCy Schubert        push @synopsis, $found if $found =~ /^B<-/;
556*e0c4386eSCy Schubert        print STDERR "$id:DEBUG[option_check] SYNOPSIS: found $found\n"
557*e0c4386eSCy Schubert            if $debug;
558*e0c4386eSCy Schubert        my $option_uw = normalise_option($id, $filename, $found);
559*e0c4386eSCy Schubert        err($id, "Malformed option [2] in SYNOPSIS: $found")
560*e0c4386eSCy Schubert            if defined $option_uw && $option_uw eq '';
561*e0c4386eSCy Schubert    }
562*e0c4386eSCy Schubert
563*e0c4386eSCy Schubert    # In OPTIONS, we look for =item paragraphs.
564*e0c4386eSCy Schubert    # (?=^\s*$) detects an empty line.
565*e0c4386eSCy Schubert    my @options;
566*e0c4386eSCy Schubert    while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) {
567*e0c4386eSCy Schubert        my $item = $&;
568*e0c4386eSCy Schubert
569*e0c4386eSCy Schubert        while ( $item =~ /(\[\s*)?($markup_re)/msg ) {
570*e0c4386eSCy Schubert            my $found = $2;
571*e0c4386eSCy Schubert            print STDERR "$id:DEBUG[option_check] OPTIONS: found $&\n"
572*e0c4386eSCy Schubert                if $debug;
573*e0c4386eSCy Schubert            err($id, "Unexpected bracket in OPTIONS =item: $item")
574*e0c4386eSCy Schubert                if ($1 // '') ne '' && $found =~ /^B<\s*-/;
575*e0c4386eSCy Schubert
576*e0c4386eSCy Schubert            my $option_uw = normalise_option($id, $filename, $found);
577*e0c4386eSCy Schubert            err($id, "Malformed option in OPTIONS: $found")
578*e0c4386eSCy Schubert                if defined $option_uw && $option_uw eq '';
579*e0c4386eSCy Schubert            if ($found =~ /^B<-/) {
580*e0c4386eSCy Schubert                push @options, $found;
581*e0c4386eSCy Schubert                err($id, "OPTIONS entry $found missing from SYNOPSIS")
582*e0c4386eSCy Schubert                    unless (grep /^\Q$found\E$/, @synopsis)
583*e0c4386eSCy Schubert                         || $id =~ /(openssl|-options)\.pod:1:$/;
584*e0c4386eSCy Schubert            }
585*e0c4386eSCy Schubert        }
586*e0c4386eSCy Schubert    }
587*e0c4386eSCy Schubert    foreach (@synopsis) {
588*e0c4386eSCy Schubert        my $option = $_;
589*e0c4386eSCy Schubert        err($id, "SYNOPSIS entry $option missing from OPTIONS")
590*e0c4386eSCy Schubert            unless (grep /^\Q$option\E$/, @options);
591*e0c4386eSCy Schubert    }
592*e0c4386eSCy Schubert}
593*e0c4386eSCy Schubert
594*e0c4386eSCy Schubert# Normal symbol form
595*e0c4386eSCy Schubertmy $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/;
596*e0c4386eSCy Schubert
597*e0c4386eSCy Schubert# Checks of function name (man3) formatting.  The man3 checks are
598*e0c4386eSCy Schubert# easier than the man1 checks, we only check the names followed by (),
599*e0c4386eSCy Schubert# and only the names that have POD markup.
600*e0c4386eSCy Schubertsub functionname_check {
601*e0c4386eSCy Schubert    my $id = shift;
602*e0c4386eSCy Schubert    my $filename = shift;
603*e0c4386eSCy Schubert    my $contents = shift;
604*e0c4386eSCy Schubert
605*e0c4386eSCy Schubert    while ( $contents =~ /($markup_re)\(\)/msg ) {
606*e0c4386eSCy Schubert        print STDERR "$id:DEBUG[functionname_check] SYNOPSIS: found $&\n"
607*e0c4386eSCy Schubert            if $debug;
608*e0c4386eSCy Schubert
609*e0c4386eSCy Schubert        my $symbol = $1;
610*e0c4386eSCy Schubert        my $unmarked = $symbol;
611*e0c4386eSCy Schubert        $unmarked =~ s/[BIL]<|>//msg;
612*e0c4386eSCy Schubert
613*e0c4386eSCy Schubert        err($id, "Malformed symbol: $symbol")
614*e0c4386eSCy Schubert            unless $symbol =~ /^B<.*?>$/ && $unmarked =~ /^${symbol_re}$/
615*e0c4386eSCy Schubert    }
616*e0c4386eSCy Schubert
617*e0c4386eSCy Schubert    # We can't do the kind of collecting coolness that option_check()
618*e0c4386eSCy Schubert    # does, because there are too many things that can't be found in
619*e0c4386eSCy Schubert    # name repositories like the NAME sections, such as symbol names
620*e0c4386eSCy Schubert    # with a variable part (typically marked up as B<foo_I<TYPE>_bar>
621*e0c4386eSCy Schubert}
622*e0c4386eSCy Schubert
623*e0c4386eSCy Schubert# This is from http://man7.org/linux/man-pages/man7/man-pages.7.html
624*e0c4386eSCy Schubertmy %preferred_words = (
625*e0c4386eSCy Schubert    '16bit'         => '16-bit',
626*e0c4386eSCy Schubert    'a.k.a.'        => 'aka',
627*e0c4386eSCy Schubert    'bitmask'       => 'bit mask',
628*e0c4386eSCy Schubert    'builtin'       => 'built-in',
629*e0c4386eSCy Schubert   #'epoch'         => 'Epoch', # handled specially, below
630*e0c4386eSCy Schubert    'fall-back'     => 'fallback',
631*e0c4386eSCy Schubert    'file name'     => 'filename',
632*e0c4386eSCy Schubert    'file system'   => 'filesystem',
633*e0c4386eSCy Schubert    'host name'     => 'hostname',
634*e0c4386eSCy Schubert    'i-node'        => 'inode',
635*e0c4386eSCy Schubert    'lower case'    => 'lowercase',
636*e0c4386eSCy Schubert    'lower-case'    => 'lowercase',
637*e0c4386eSCy Schubert    'manpage'       => 'man page',
638*e0c4386eSCy Schubert    'non-blocking'  => 'nonblocking',
639*e0c4386eSCy Schubert    'non-default'   => 'nondefault',
640*e0c4386eSCy Schubert    'non-empty'     => 'nonempty',
641*e0c4386eSCy Schubert    'non-negative'  => 'nonnegative',
642*e0c4386eSCy Schubert    'non-zero'      => 'nonzero',
643*e0c4386eSCy Schubert    'path name'     => 'pathname',
644*e0c4386eSCy Schubert    'pre-allocated' => 'preallocated',
645*e0c4386eSCy Schubert    'pseudo-terminal' => 'pseudoterminal',
646*e0c4386eSCy Schubert    'real time'     => 'real-time',
647*e0c4386eSCy Schubert    'realtime'      => 'real-time',
648*e0c4386eSCy Schubert    'reserved port' => 'privileged port',
649*e0c4386eSCy Schubert    'runtime'       => 'run time',
650*e0c4386eSCy Schubert    'saved group ID'=> 'saved set-group-ID',
651*e0c4386eSCy Schubert    'saved set-GID' => 'saved set-group-ID',
652*e0c4386eSCy Schubert    'saved set-UID' => 'saved set-user-ID',
653*e0c4386eSCy Schubert    'saved user ID' => 'saved set-user-ID',
654*e0c4386eSCy Schubert    'set-GID'       => 'set-group-ID',
655*e0c4386eSCy Schubert    'set-UID'       => 'set-user-ID',
656*e0c4386eSCy Schubert    'setgid'        => 'set-group-ID',
657*e0c4386eSCy Schubert    'setuid'        => 'set-user-ID',
658*e0c4386eSCy Schubert    'sub-system'    => 'subsystem',
659*e0c4386eSCy Schubert    'super block'   => 'superblock',
660*e0c4386eSCy Schubert    'super-block'   => 'superblock',
661*e0c4386eSCy Schubert    'super user'    => 'superuser',
662*e0c4386eSCy Schubert    'super-user'    => 'superuser',
663*e0c4386eSCy Schubert    'system port'   => 'privileged port',
664*e0c4386eSCy Schubert    'time stamp'    => 'timestamp',
665*e0c4386eSCy Schubert    'time zone'     => 'timezone',
666*e0c4386eSCy Schubert    'upper case'    => 'uppercase',
667*e0c4386eSCy Schubert    'upper-case'    => 'uppercase',
668*e0c4386eSCy Schubert    'useable'       => 'usable',
669*e0c4386eSCy Schubert    'user name'     => 'username',
670*e0c4386eSCy Schubert    'userspace'     => 'user space',
671*e0c4386eSCy Schubert    'zeroes'        => 'zeros'
672*e0c4386eSCy Schubert);
673*e0c4386eSCy Schubert
674*e0c4386eSCy Schubert# Search manpage for words that have a different preferred use.
675*e0c4386eSCy Schubertsub wording {
676*e0c4386eSCy Schubert    my $id = shift;
677*e0c4386eSCy Schubert    my $contents = shift;
678*e0c4386eSCy Schubert
679*e0c4386eSCy Schubert    foreach my $k ( keys %preferred_words ) {
680*e0c4386eSCy Schubert        # Sigh, trademark
681*e0c4386eSCy Schubert        next if $k eq 'file system'
682*e0c4386eSCy Schubert            and $contents =~ /Microsoft Encrypted File System/;
683*e0c4386eSCy Schubert        err($id, "Found '$k' should use '$preferred_words{$k}'")
684*e0c4386eSCy Schubert            if $contents =~ /\b\Q$k\E\b/i;
685*e0c4386eSCy Schubert    }
686*e0c4386eSCy Schubert    err($id, "Found 'epoch' should use 'Epoch'")
687*e0c4386eSCy Schubert        if $contents =~ /\bepoch\b/;
688*e0c4386eSCy Schubert    if ( $id =~ m@man1/@ ) {
689*e0c4386eSCy Schubert        err($id, "found 'tool' in NAME, should use 'command'")
690*e0c4386eSCy Schubert            if $contents =~ /=head1 NAME.*\btool\b.*=head1 SYNOPSIS/s;
691*e0c4386eSCy Schubert        err($id, "found 'utility' in NAME, should use 'command'")
692*e0c4386eSCy Schubert            if $contents =~ /NAME.*\butility\b.*=head1 SYNOPSIS/s;
693*e0c4386eSCy Schubert
694*e0c4386eSCy Schubert    }
695*e0c4386eSCy Schubert}
696*e0c4386eSCy Schubert
697*e0c4386eSCy Schubert# Perform all sorts of nit/error checks on a manpage
698*e0c4386eSCy Schubertsub check {
699*e0c4386eSCy Schubert    my %podinfo = @_;
700*e0c4386eSCy Schubert    my $filename = $podinfo{filename};
701*e0c4386eSCy Schubert    my $dirname = basename(dirname($filename));
702*e0c4386eSCy Schubert    my $contents = $podinfo{contents};
703*e0c4386eSCy Schubert
704*e0c4386eSCy Schubert    # Find what section this page is in; presume 3.
705*e0c4386eSCy Schubert    my $mansect = 3;
706*e0c4386eSCy Schubert    $mansect = $1 if $filename =~ /man([1-9])/;
707*e0c4386eSCy Schubert
708*e0c4386eSCy Schubert    my $id = "${filename}:1:";
709*e0c4386eSCy Schubert    check_head_style($id, $contents);
710*e0c4386eSCy Schubert
711*e0c4386eSCy Schubert    # Check ordering of some sections in man3
712*e0c4386eSCy Schubert    if ( $mansect == 3 ) {
713*e0c4386eSCy Schubert        check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES");
714*e0c4386eSCy Schubert        check_section_location($id, $contents, "SEE ALSO", "HISTORY");
715*e0c4386eSCy Schubert        check_section_location($id, $contents, "EXAMPLES", "SEE ALSO");
716*e0c4386eSCy Schubert    }
717*e0c4386eSCy Schubert
718*e0c4386eSCy Schubert    # Make sure every link has a man section number.
719*e0c4386eSCy Schubert    while ( $contents =~ /$markup_re/msg ) {
720*e0c4386eSCy Schubert        my $target = $1;
721*e0c4386eSCy Schubert        next unless $target =~ /^L<(.*)>$/;     # Skip if not L<...>
722*e0c4386eSCy Schubert        $target = $1;                           # Peal away L< and >
723*e0c4386eSCy Schubert        $target =~ s/\/[^\/]*$//;               # Peal away possible anchor
724*e0c4386eSCy Schubert        $target =~ s/.*\|//g;                   # Peal away possible link text
725*e0c4386eSCy Schubert        next if $target eq '';                  # Skip if links within page, or
726*e0c4386eSCy Schubert        next if $target =~ /::/;                #   links to a Perl module, or
727*e0c4386eSCy Schubert        next if $target =~ /^https?:/;          #   is a URL link, or
728*e0c4386eSCy Schubert        next if $target =~ /\([1357]\)$/;       #   it has a section
729*e0c4386eSCy Schubert        err($id, "Missing man section number (likely, $mansect) in L<$target>")
730*e0c4386eSCy Schubert    }
731*e0c4386eSCy Schubert    # Check for proper links to commands.
732*e0c4386eSCy Schubert    while ( $contents =~ /L<([^>]*)\(1\)(?:\/.*)?>/g ) {
733*e0c4386eSCy Schubert        my $target = $1;
734*e0c4386eSCy Schubert        next if $target =~ /openssl-?/;
735*e0c4386eSCy Schubert        next if ( grep { basename($_) eq "$target.pod" }
736*e0c4386eSCy Schubert                  files(TAGS => [ 'manual', 'man1' ]) );
737*e0c4386eSCy Schubert        next if $target =~ /ps|apropos|sha1sum|procmail|perl/;
738*e0c4386eSCy Schubert        err($id, "Bad command link L<$target(1)>") if grep /man1/, @sections;
739*e0c4386eSCy Schubert    }
740*e0c4386eSCy Schubert    # Check for proper in-man-3 API links.
741*e0c4386eSCy Schubert    while ( $contents =~ /L<([^>]*)\(3\)(?:\/.*)?>/g ) {
742*e0c4386eSCy Schubert        my $target = $1;
743*e0c4386eSCy Schubert        err($id, "Bad L<$target>")
744*e0c4386eSCy Schubert            unless $target =~ /^[_[:alpha:]][_[:alnum:]]*$/
745*e0c4386eSCy Schubert    }
746*e0c4386eSCy Schubert
747*e0c4386eSCy Schubert    unless ( $contents =~ /^=for openssl generic/ms ) {
748*e0c4386eSCy Schubert        if ( $mansect == 3 ) {
749*e0c4386eSCy Schubert            name_synopsis($id, $filename, $contents);
750*e0c4386eSCy Schubert            functionname_check($id, $filename, $contents);
751*e0c4386eSCy Schubert        } elsif ( $mansect == 1 ) {
752*e0c4386eSCy Schubert            option_check($id, $filename, $contents)
753*e0c4386eSCy Schubert        }
754*e0c4386eSCy Schubert    }
755*e0c4386eSCy Schubert
756*e0c4386eSCy Schubert    wording($id, $contents);
757*e0c4386eSCy Schubert
758*e0c4386eSCy Schubert    err($id, "Doesn't start with =pod")
759*e0c4386eSCy Schubert        if $contents !~ /^=pod/;
760*e0c4386eSCy Schubert    err($id, "Doesn't end with =cut")
761*e0c4386eSCy Schubert        if $contents !~ /=cut\n$/;
762*e0c4386eSCy Schubert    err($id, "More than one cut line.")
763*e0c4386eSCy Schubert        if $contents =~ /=cut.*=cut/ms;
764*e0c4386eSCy Schubert    err($id, "EXAMPLE not EXAMPLES section.")
765*e0c4386eSCy Schubert        if $contents =~ /=head1 EXAMPLE[^S]/;
766*e0c4386eSCy Schubert    err($id, "WARNING not WARNINGS section.")
767*e0c4386eSCy Schubert        if $contents =~ /=head1 WARNING[^S]/;
768*e0c4386eSCy Schubert    err($id, "Missing copyright")
769*e0c4386eSCy Schubert        if $contents !~ /Copyright .* The OpenSSL Project Authors/;
770*e0c4386eSCy Schubert    err($id, "Copyright not last")
771*e0c4386eSCy Schubert        if $contents =~ /head1 COPYRIGHT.*=head/ms;
772*e0c4386eSCy Schubert    err($id, "head2 in All uppercase")
773*e0c4386eSCy Schubert        if $contents =~ /head2\s+[A-Z ]+\n/;
774*e0c4386eSCy Schubert    err($id, "Extra space after head")
775*e0c4386eSCy Schubert        if $contents =~ /=head\d\s\s+/;
776*e0c4386eSCy Schubert    err($id, "Period in NAME section")
777*e0c4386eSCy Schubert        if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
778*e0c4386eSCy Schubert    err($id, "Duplicate $1 in L<>")
779*e0c4386eSCy Schubert        if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
780*e0c4386eSCy Schubert    err($id, "Bad =over $1")
781*e0c4386eSCy Schubert        if $contents =~ /=over([^ ][^24])/;
782*e0c4386eSCy Schubert    err($id, "Possible version style issue")
783*e0c4386eSCy Schubert        if $contents =~ /OpenSSL version [019]/;
784*e0c4386eSCy Schubert
785*e0c4386eSCy Schubert    if ( $contents !~ /=for openssl multiple includes/ ) {
786*e0c4386eSCy Schubert        # Look for multiple consecutive openssl #include lines
787*e0c4386eSCy Schubert        # (non-consecutive lines are okay; see man3/MD5.pod).
788*e0c4386eSCy Schubert        if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
789*e0c4386eSCy Schubert            my $count = 0;
790*e0c4386eSCy Schubert            foreach my $line ( split /\n+/, $1 ) {
791*e0c4386eSCy Schubert                if ( $line =~ m@include <openssl/@ ) {
792*e0c4386eSCy Schubert                    err($id, "Has multiple includes")
793*e0c4386eSCy Schubert                        if ++$count == 2;
794*e0c4386eSCy Schubert                } else {
795*e0c4386eSCy Schubert                    $count = 0;
796*e0c4386eSCy Schubert                }
797*e0c4386eSCy Schubert            }
798*e0c4386eSCy Schubert        }
799*e0c4386eSCy Schubert    }
800*e0c4386eSCy Schubert
801*e0c4386eSCy Schubert    open my $OUT, '>', $temp
802*e0c4386eSCy Schubert        or die "Can't open $temp, $!";
803*e0c4386eSCy Schubert    err($id, "POD errors")
804*e0c4386eSCy Schubert        if podchecker($filename, $OUT) != 0;
805*e0c4386eSCy Schubert    close $OUT;
806*e0c4386eSCy Schubert    open $OUT, '<', $temp
807*e0c4386eSCy Schubert        or die "Can't read $temp, $!";
808*e0c4386eSCy Schubert    while ( <$OUT> ) {
809*e0c4386eSCy Schubert        next if /\(section\) in.*deprecated/;
810*e0c4386eSCy Schubert        print;
811*e0c4386eSCy Schubert    }
812*e0c4386eSCy Schubert    close $OUT;
813*e0c4386eSCy Schubert    unlink $temp || warn "Can't remove $temp, $!";
814*e0c4386eSCy Schubert
815*e0c4386eSCy Schubert    # Find what section this page is in; presume 3.
816*e0c4386eSCy Schubert    my $section = 3;
817*e0c4386eSCy Schubert    $section = $1 if $dirname =~ /man([1-9])/;
818*e0c4386eSCy Schubert
819*e0c4386eSCy Schubert    foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) {
820*e0c4386eSCy Schubert        err($id, "Missing $_ head1 section")
821*e0c4386eSCy Schubert            if $contents !~ /^=head1\s+${_}\s*$/m;
822*e0c4386eSCy Schubert    }
823*e0c4386eSCy Schubert}
824*e0c4386eSCy Schubert
825*e0c4386eSCy Schubert# Information database ###############################################
826*e0c4386eSCy Schubert
827*e0c4386eSCy Schubert# Map of links in each POD file; filename => [ "foo(1)", "bar(3)", ... ]
828*e0c4386eSCy Schubertmy %link_map = ();
829*e0c4386eSCy Schubert# Map of names in each POD file or from "missing" files; possible values are:
830*e0c4386eSCy Schubert# If found in a POD files, "name(s)" => filename
831*e0c4386eSCy Schubert# If found in a "missing" file or external, "name(s)" => ''
832*e0c4386eSCy Schubertmy %name_map = ();
833*e0c4386eSCy Schubert
834*e0c4386eSCy Schubert# State of man-page names.
835*e0c4386eSCy Schubert# %state is affected by loading util/*.num and util/*.syms
836*e0c4386eSCy Schubert# Values may be one of:
837*e0c4386eSCy Schubert# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
838*e0c4386eSCy Schubert# 'ssl' : belongs in libssl (loaded from libssl.num)
839*e0c4386eSCy Schubert# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
840*e0c4386eSCy Schubert# 'internal' : Internal
841*e0c4386eSCy Schubert# 'public' : Public (generic name or external documentation)
842*e0c4386eSCy Schubert# Any of these values except 'public' may be prefixed with 'missing_'
843*e0c4386eSCy Schubert# to indicate that they are known to be missing.
844*e0c4386eSCy Schubertmy %state;
845*e0c4386eSCy Schubert# %missing is affected by loading util/missing*.txt.  Values may be one of:
846*e0c4386eSCy Schubert# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
847*e0c4386eSCy Schubert# 'ssl' : belongs in libssl (loaded from libssl.num)
848*e0c4386eSCy Schubert# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
849*e0c4386eSCy Schubert# 'internal' : Internal
850*e0c4386eSCy Schubertmy %missing;
851*e0c4386eSCy Schubert
852*e0c4386eSCy Schubert# Parse libcrypto.num, etc., and return sorted list of what's there.
853*e0c4386eSCy Schubertsub loadnum ($;$) {
854*e0c4386eSCy Schubert    my $file = shift;
855*e0c4386eSCy Schubert    my $type = shift;
856*e0c4386eSCy Schubert    my @symbols;
857*e0c4386eSCy Schubert
858*e0c4386eSCy Schubert    open my $IN, '<', catfile($config{sourcedir}, $file)
859*e0c4386eSCy Schubert        or die "Can't open $file, $!, stopped";
860*e0c4386eSCy Schubert
861*e0c4386eSCy Schubert    while ( <$IN> ) {
862*e0c4386eSCy Schubert        next if /^#/;
863*e0c4386eSCy Schubert        next if /\bNOEXIST\b/;
864*e0c4386eSCy Schubert        my @fields = split();
865*e0c4386eSCy Schubert        die "Malformed line $. in $file: $_"
866*e0c4386eSCy Schubert            if scalar @fields != 2 && scalar @fields != 4;
867*e0c4386eSCy Schubert        $state{$fields[0].'(3)'} = $type // 'internal';
868*e0c4386eSCy Schubert    }
869*e0c4386eSCy Schubert    close $IN;
870*e0c4386eSCy Schubert}
871*e0c4386eSCy Schubert
872*e0c4386eSCy Schubert# Load file of symbol names that we know aren't documented.
873*e0c4386eSCy Schubertsub loadmissing($;$)
874*e0c4386eSCy Schubert{
875*e0c4386eSCy Schubert    my $missingfile = shift;
876*e0c4386eSCy Schubert    my $type = shift;
877*e0c4386eSCy Schubert
878*e0c4386eSCy Schubert    open FH, catfile($config{sourcedir}, $missingfile)
879*e0c4386eSCy Schubert        or die "Can't open $missingfile";
880*e0c4386eSCy Schubert    while ( <FH> ) {
881*e0c4386eSCy Schubert        chomp;
882*e0c4386eSCy Schubert        next if /^#/;
883*e0c4386eSCy Schubert        $missing{$_} = $type // 'internal';
884*e0c4386eSCy Schubert    }
885*e0c4386eSCy Schubert    close FH;
886*e0c4386eSCy Schubert}
887*e0c4386eSCy Schubert
888*e0c4386eSCy Schubert# Check that we have consistent public / internal documentation and declaration
889*e0c4386eSCy Schubertsub checkstate () {
890*e0c4386eSCy Schubert    # Collect all known names, no matter where they come from
891*e0c4386eSCy Schubert    my %names = map { $_ => 1 } (keys %name_map, keys %state, keys %missing);
892*e0c4386eSCy Schubert
893*e0c4386eSCy Schubert    # Check section 3, i.e. functions and macros
894*e0c4386eSCy Schubert    foreach ( grep { $_ =~ /\(3\)$/ } sort keys %names ) {
895*e0c4386eSCy Schubert        next if ( $name_map{$_} // '') eq '' || $_ =~ /$ignored/;
896*e0c4386eSCy Schubert
897*e0c4386eSCy Schubert        # If a man-page isn't recorded public or if it's recorded missing
898*e0c4386eSCy Schubert        # and internal, it's declared to be internal.
899*e0c4386eSCy Schubert        my $declared_internal =
900*e0c4386eSCy Schubert            ($state{$_} // 'internal') eq 'internal'
901*e0c4386eSCy Schubert            || ($missing{$_} // '') eq 'internal';
902*e0c4386eSCy Schubert        # If a man-page isn't recorded internal or if it's recorded missing
903*e0c4386eSCy Schubert        # and not internal, it's declared to be public
904*e0c4386eSCy Schubert        my $declared_public =
905*e0c4386eSCy Schubert            ($state{$_} // 'internal') ne 'internal'
906*e0c4386eSCy Schubert            || ($missing{$_} // 'internal') ne 'internal';
907*e0c4386eSCy Schubert
908*e0c4386eSCy Schubert        err("$_ is supposedly public but is documented as internal")
909*e0c4386eSCy Schubert            if ( $declared_public && $name_map{$_} =~ /\/internal\// );
910*e0c4386eSCy Schubert        err("$_ is supposedly internal (maybe missing from other.syms) but is documented as public")
911*e0c4386eSCy Schubert            if ( $declared_internal && $name_map{$_} !~ /\/internal\// );
912*e0c4386eSCy Schubert    }
913*e0c4386eSCy Schubert}
914*e0c4386eSCy Schubert
915*e0c4386eSCy Schubert# Check for undocumented macros; ignore those in the "missing" file
916*e0c4386eSCy Schubert# and do simple check for #define in our header files.
917*e0c4386eSCy Schubertsub checkmacros {
918*e0c4386eSCy Schubert    my $count = 0;
919*e0c4386eSCy Schubert    my %seen;
920*e0c4386eSCy Schubert
921*e0c4386eSCy Schubert    foreach my $f ( files(TAGS => 'public_header') ) {
922*e0c4386eSCy Schubert        # Skip some internals we don't want to document yet.
923*e0c4386eSCy Schubert        my $b = basename($f);
924*e0c4386eSCy Schubert        next if $b eq 'asn1.h';
925*e0c4386eSCy Schubert        next if $b eq 'asn1t.h';
926*e0c4386eSCy Schubert        next if $b eq 'err.h';
927*e0c4386eSCy Schubert        open(IN, $f)
928*e0c4386eSCy Schubert            or die "Can't open $f, $!";
929*e0c4386eSCy Schubert        while ( <IN> ) {
930*e0c4386eSCy Schubert            next unless /^#\s*define\s*(\S+)\(/;
931*e0c4386eSCy Schubert            my $macro = "$1(3)"; # We know they're all in section 3
932*e0c4386eSCy Schubert            next if defined $name_map{$macro}
933*e0c4386eSCy Schubert                || defined $missing{$macro}
934*e0c4386eSCy Schubert                || defined $seen{$macro}
935*e0c4386eSCy Schubert                || $macro =~ /$ignored/;
936*e0c4386eSCy Schubert
937*e0c4386eSCy Schubert            err("$f:", "macro $macro undocumented")
938*e0c4386eSCy Schubert                if $opt_d || $opt_e;
939*e0c4386eSCy Schubert            $count++;
940*e0c4386eSCy Schubert            $seen{$macro} = 1;
941*e0c4386eSCy Schubert        }
942*e0c4386eSCy Schubert        close(IN);
943*e0c4386eSCy Schubert    }
944*e0c4386eSCy Schubert    err("# $count macros undocumented (count is approximate)")
945*e0c4386eSCy Schubert        if $count > 0;
946*e0c4386eSCy Schubert}
947*e0c4386eSCy Schubert
948*e0c4386eSCy Schubert# Find out what is undocumented (filtering out the known missing ones)
949*e0c4386eSCy Schubert# and display them.
950*e0c4386eSCy Schubertsub printem ($) {
951*e0c4386eSCy Schubert    my $type = shift;
952*e0c4386eSCy Schubert    my $count = 0;
953*e0c4386eSCy Schubert
954*e0c4386eSCy Schubert    foreach my $func ( grep { $state{$_} eq $type } sort keys %state ) {
955*e0c4386eSCy Schubert        next if defined $name_map{$func}
956*e0c4386eSCy Schubert            || defined $missing{$func};
957*e0c4386eSCy Schubert
958*e0c4386eSCy Schubert        err("$type:", "function $func undocumented")
959*e0c4386eSCy Schubert            if $opt_d || $opt_e;
960*e0c4386eSCy Schubert        $count++;
961*e0c4386eSCy Schubert    }
962*e0c4386eSCy Schubert    err("# $count lib$type names are not documented")
963*e0c4386eSCy Schubert        if $count > 0;
964*e0c4386eSCy Schubert}
965*e0c4386eSCy Schubert
966*e0c4386eSCy Schubert# Collect all the names in a manpage.
967*e0c4386eSCy Schubertsub collectnames {
968*e0c4386eSCy Schubert    my %podinfo = @_;
969*e0c4386eSCy Schubert    my $filename = $podinfo{filename};
970*e0c4386eSCy Schubert    $filename =~ m|man(\d)/|;
971*e0c4386eSCy Schubert    my $section = $1;
972*e0c4386eSCy Schubert    my $simplename = basename($filename, ".pod");
973*e0c4386eSCy Schubert    my $id = "${filename}:1:";
974*e0c4386eSCy Schubert    my $is_generic = $podinfo{contents} =~ /^=for openssl generic/ms;
975*e0c4386eSCy Schubert
976*e0c4386eSCy Schubert    unless ( grep { $simplename eq $_ } @{$podinfo{names}} ) {
977*e0c4386eSCy Schubert        err($id, "$simplename not in NAME section");
978*e0c4386eSCy Schubert        push @{$podinfo{names}}, $simplename;
979*e0c4386eSCy Schubert    }
980*e0c4386eSCy Schubert    foreach my $name ( @{$podinfo{names}} ) {
981*e0c4386eSCy Schubert        next if $name eq "";
982*e0c4386eSCy Schubert        err($id, "'$name' contains whitespace")
983*e0c4386eSCy Schubert            if $name =~ /\s/;
984*e0c4386eSCy Schubert        my $name_sec = "$name($section)";
985*e0c4386eSCy Schubert        if ( !defined $name_map{$name_sec} ) {
986*e0c4386eSCy Schubert            $name_map{$name_sec} = $filename;
987*e0c4386eSCy Schubert            $state{$name_sec} //=
988*e0c4386eSCy Schubert                ( $filename =~ /\/internal\// ? 'internal' : 'public' )
989*e0c4386eSCy Schubert                if $is_generic;
990*e0c4386eSCy Schubert        } elsif ( $filename eq $name_map{$name_sec} ) {
991*e0c4386eSCy Schubert            err($id, "$name_sec duplicated in NAME section of",
992*e0c4386eSCy Schubert                 $name_map{$name_sec});
993*e0c4386eSCy Schubert        } elsif ( $name_map{$name_sec} ne '' ) {
994*e0c4386eSCy Schubert            err($id, "$name_sec also in NAME section of",
995*e0c4386eSCy Schubert                 $name_map{$name_sec});
996*e0c4386eSCy Schubert        }
997*e0c4386eSCy Schubert    }
998*e0c4386eSCy Schubert
999*e0c4386eSCy Schubert    if ( $podinfo{contents} =~ /=for openssl foreign manual (.*)\n/ ) {
1000*e0c4386eSCy Schubert        foreach my $f ( split / /, $1 ) {
1001*e0c4386eSCy Schubert            $name_map{$f} = ''; # It still exists!
1002*e0c4386eSCy Schubert            $state{$f} = 'public'; # We assume!
1003*e0c4386eSCy Schubert        }
1004*e0c4386eSCy Schubert    }
1005*e0c4386eSCy Schubert
1006*e0c4386eSCy Schubert    my @links = ();
1007*e0c4386eSCy Schubert    # Don't use this regexp directly on $podinfo{contents}, as it causes
1008*e0c4386eSCy Schubert    # a regexp recursion, which fails on really big PODs.  Instead, use
1009*e0c4386eSCy Schubert    # $markup_re to pick up general markup, and use this regexp to check
1010*e0c4386eSCy Schubert    # that the markup that was found is indeed a link.
1011*e0c4386eSCy Schubert    my $linkre = qr/L<
1012*e0c4386eSCy Schubert                    # if the link is of the form L<something|name(s)>,
1013*e0c4386eSCy Schubert                    # then remove 'something'.  Note that 'something'
1014*e0c4386eSCy Schubert                    # may contain POD codes as well...
1015*e0c4386eSCy Schubert                    (?:(?:[^\|]|<[^>]*>)*\|)?
1016*e0c4386eSCy Schubert                    # we're only interested in references that have
1017*e0c4386eSCy Schubert                    # a one digit section number
1018*e0c4386eSCy Schubert                    ([^\/>\(]+\(\d\))
1019*e0c4386eSCy Schubert                   /x;
1020*e0c4386eSCy Schubert    while ( $podinfo{contents} =~ /$markup_re/msg ) {
1021*e0c4386eSCy Schubert        my $x = $1;
1022*e0c4386eSCy Schubert
1023*e0c4386eSCy Schubert        if ($x =~ $linkre) {
1024*e0c4386eSCy Schubert            push @links, $1;
1025*e0c4386eSCy Schubert        }
1026*e0c4386eSCy Schubert    }
1027*e0c4386eSCy Schubert    $link_map{$filename} = [ @links ];
1028*e0c4386eSCy Schubert}
1029*e0c4386eSCy Schubert
1030*e0c4386eSCy Schubert# Look for L<> ("link") references that point to files that do not exist.
1031*e0c4386eSCy Schubertsub checklinks {
1032*e0c4386eSCy Schubert    foreach my $filename ( sort keys %link_map ) {
1033*e0c4386eSCy Schubert        foreach my $link ( @{$link_map{$filename}} ) {
1034*e0c4386eSCy Schubert            err("${filename}:1:", "reference to non-existing $link")
1035*e0c4386eSCy Schubert                unless defined $name_map{$link} || defined $missing{$link};
1036*e0c4386eSCy Schubert            err("${filename}:1:", "reference of internal $link in public documentation $filename")
1037*e0c4386eSCy Schubert                if ( ( ($state{$link} // '') eq 'internal'
1038*e0c4386eSCy Schubert                       || ($missing{$link} // '') eq 'internal' )
1039*e0c4386eSCy Schubert                     && $filename !~ /\/internal\// );
1040*e0c4386eSCy Schubert        }
1041*e0c4386eSCy Schubert    }
1042*e0c4386eSCy Schubert}
1043*e0c4386eSCy Schubert
1044*e0c4386eSCy Schubert# Cipher/digests to skip if they show up as "not implemented"
1045*e0c4386eSCy Schubert# because they are, via the "-*" construct.
1046*e0c4386eSCy Schubertmy %skips = (
1047*e0c4386eSCy Schubert    'aes128' => 1,
1048*e0c4386eSCy Schubert    'aes192' => 1,
1049*e0c4386eSCy Schubert    'aes256' => 1,
1050*e0c4386eSCy Schubert    'aria128' => 1,
1051*e0c4386eSCy Schubert    'aria192' => 1,
1052*e0c4386eSCy Schubert    'aria256' => 1,
1053*e0c4386eSCy Schubert    'camellia128' => 1,
1054*e0c4386eSCy Schubert    'camellia192' => 1,
1055*e0c4386eSCy Schubert    'camellia256' => 1,
1056*e0c4386eSCy Schubert    'des' => 1,
1057*e0c4386eSCy Schubert    'des3' => 1,
1058*e0c4386eSCy Schubert    'idea' => 1,
1059*e0c4386eSCy Schubert    'cipher' => 1,
1060*e0c4386eSCy Schubert    'digest' => 1,
1061*e0c4386eSCy Schubert);
1062*e0c4386eSCy Schubert
1063*e0c4386eSCy Schubertmy %genopts; # generic options parsed from apps/include/opt.h
1064*e0c4386eSCy Schubert
1065*e0c4386eSCy Schubert# Check the flags of a command and see if everything is in the manpage
1066*e0c4386eSCy Schubertsub checkflags {
1067*e0c4386eSCy Schubert    my $cmd = shift;
1068*e0c4386eSCy Schubert    my $doc = shift;
1069*e0c4386eSCy Schubert    my @cmdopts;
1070*e0c4386eSCy Schubert    my %docopts;
1071*e0c4386eSCy Schubert
1072*e0c4386eSCy Schubert    # Get the list of options in the command source file.
1073*e0c4386eSCy Schubert    my $active = 0;
1074*e0c4386eSCy Schubert    my $expect_helpstr = "";
1075*e0c4386eSCy Schubert    open CFH, "apps/$cmd.c"
1076*e0c4386eSCy Schubert        or die "Can't open apps/$cmd.c to list options for $cmd, $!";
1077*e0c4386eSCy Schubert    while ( <CFH> ) {
1078*e0c4386eSCy Schubert        chop;
1079*e0c4386eSCy Schubert        if ($active) {
1080*e0c4386eSCy Schubert            last if m/^\s*};/;
1081*e0c4386eSCy Schubert            if ($expect_helpstr ne "") {
1082*e0c4386eSCy Schubert                next if m/^\s*#\s*if/;
1083*e0c4386eSCy Schubert                err("$cmd does not implement help for -$expect_helpstr") unless m/^\s*"/;
1084*e0c4386eSCy Schubert                $expect_helpstr = "";
1085*e0c4386eSCy Schubert            }
1086*e0c4386eSCy Schubert            if (m/\{\s*"([^"]+)"\s*,\s*OPT_[A-Z0-9_]+\s*,\s*('[-\/:<>cEfFlMnNpsuU]'|0)(.*)$/
1087*e0c4386eSCy Schubert                    && !($cmd eq "s_client" && $1 eq "wdebug")) {
1088*e0c4386eSCy Schubert                push @cmdopts, $1;
1089*e0c4386eSCy Schubert                $expect_helpstr = $1;
1090*e0c4386eSCy Schubert                $expect_helpstr = "" if $3 =~ m/^\s*,\s*"/;
1091*e0c4386eSCy Schubert            } elsif (m/[\s,](OPT_[A-Z]+_OPTIONS?)\s*(,|$)/) {
1092*e0c4386eSCy Schubert                push @cmdopts, @{ $genopts{$1} };
1093*e0c4386eSCy Schubert            }
1094*e0c4386eSCy Schubert        } elsif (m/^const\s+OPTIONS\s*/) {
1095*e0c4386eSCy Schubert            $active = 1;
1096*e0c4386eSCy Schubert        }
1097*e0c4386eSCy Schubert    }
1098*e0c4386eSCy Schubert    close CFH;
1099*e0c4386eSCy Schubert
1100*e0c4386eSCy Schubert    # Get the list of flags from the synopsis
1101*e0c4386eSCy Schubert    open CFH, "<$doc"
1102*e0c4386eSCy Schubert        or die "Can't open $doc, $!";
1103*e0c4386eSCy Schubert    while ( <CFH> ) {
1104*e0c4386eSCy Schubert        chop;
1105*e0c4386eSCy Schubert        last if /DESCRIPTION/;
1106*e0c4386eSCy Schubert        my $opt;
1107*e0c4386eSCy Schubert        if ( /\[B<-([^ >]+)/ ) {
1108*e0c4386eSCy Schubert            $opt = $1;
1109*e0c4386eSCy Schubert        } elsif ( /^B<-([^ >]+)/ ) {
1110*e0c4386eSCy Schubert            $opt = $1;
1111*e0c4386eSCy Schubert        } else {
1112*e0c4386eSCy Schubert            next;
1113*e0c4386eSCy Schubert        }
1114*e0c4386eSCy Schubert        $opt = $1 if $opt =~ /I<(.*)/;
1115*e0c4386eSCy Schubert        $docopts{$1} = 1;
1116*e0c4386eSCy Schubert    }
1117*e0c4386eSCy Schubert    close CFH;
1118*e0c4386eSCy Schubert
1119*e0c4386eSCy Schubert    # See what's in the command not the manpage.
1120*e0c4386eSCy Schubert    my @undocced = sort grep { !defined $docopts{$_} } @cmdopts;
1121*e0c4386eSCy Schubert    foreach ( @undocced ) {
1122*e0c4386eSCy Schubert        err("$doc: undocumented $cmd option -$_");
1123*e0c4386eSCy Schubert    }
1124*e0c4386eSCy Schubert
1125*e0c4386eSCy Schubert    # See what's in the command not the manpage.
1126*e0c4386eSCy Schubert    my @unimpl = sort grep { my $e = $_; !(grep /^\Q$e\E$/, @cmdopts) } keys %docopts;
1127*e0c4386eSCy Schubert    foreach ( @unimpl ) {
1128*e0c4386eSCy Schubert        next if $_ eq "-"; # Skip the -- end-of-flags marker
1129*e0c4386eSCy Schubert        next if defined $skips{$_};
1130*e0c4386eSCy Schubert        err("$doc: $cmd does not implement -$_");
1131*e0c4386eSCy Schubert    }
1132*e0c4386eSCy Schubert}
1133*e0c4386eSCy Schubert
1134*e0c4386eSCy Schubert##
1135*e0c4386eSCy Schubert##  MAIN()
1136*e0c4386eSCy Schubert##  Do the work requested by the various getopt flags.
1137*e0c4386eSCy Schubert##  The flags are parsed in alphabetical order, just because we have
1138*e0c4386eSCy Schubert##  to have *some way* of listing them.
1139*e0c4386eSCy Schubert##
1140*e0c4386eSCy Schubert
1141*e0c4386eSCy Schubertif ( $opt_c ) {
1142*e0c4386eSCy Schubert    my @commands = ();
1143*e0c4386eSCy Schubert
1144*e0c4386eSCy Schubert    # Get the lists of generic options.
1145*e0c4386eSCy Schubert    my $active = "";
1146*e0c4386eSCy Schubert    open OFH, catdir($config{sourcedir}, "apps/include/opt.h")
1147*e0c4386eSCy Schubert        or die "Can't open apps/include/opt.h to list generic options, $!";
1148*e0c4386eSCy Schubert    while ( <OFH> ) {
1149*e0c4386eSCy Schubert        chop;
1150*e0c4386eSCy Schubert        push @{ $genopts{$active} }, $1 if $active ne "" && m/^\s+\{\s*"([^"]+)"\s*,\s*OPT_/;
1151*e0c4386eSCy Schubert        $active = $1 if m/^\s*#\s*define\s+(OPT_[A-Z]+_OPTIONS?)\s*\\\s*$/;
1152*e0c4386eSCy Schubert        $active = "" if m/^\s*$/;
1153*e0c4386eSCy Schubert    }
1154*e0c4386eSCy Schubert    close OFH;
1155*e0c4386eSCy Schubert
1156*e0c4386eSCy Schubert    # Get list of commands.
1157*e0c4386eSCy Schubert    opendir(DIR, "apps");
1158*e0c4386eSCy Schubert    @commands = grep(/\.c$/, readdir(DIR));
1159*e0c4386eSCy Schubert    closedir(DIR);
1160*e0c4386eSCy Schubert
1161*e0c4386eSCy Schubert    # See if each has a manpage.
1162*e0c4386eSCy Schubert    foreach my $cmd ( @commands ) {
1163*e0c4386eSCy Schubert        $cmd =~ s/\.c$//;
1164*e0c4386eSCy Schubert        next if $cmd eq 'progs' || $cmd eq 'vms_decc_init';
1165*e0c4386eSCy Schubert        my @doc = ( grep { basename($_) eq "openssl-$cmd.pod"
1166*e0c4386eSCy Schubert                           # For "tsget" and "CA.pl" pod pages
1167*e0c4386eSCy Schubert                           || basename($_) eq "$cmd.pod" }
1168*e0c4386eSCy Schubert                    files(TAGS => [ 'manual', 'man1' ]) );
1169*e0c4386eSCy Schubert        my $num = scalar @doc;
1170*e0c4386eSCy Schubert        if ($num > 1) {
1171*e0c4386eSCy Schubert            err("$num manuals for 'openssl $cmd': ".join(", ", @doc));
1172*e0c4386eSCy Schubert        } elsif ($num < 1) {
1173*e0c4386eSCy Schubert            err("no manual for 'openssl $cmd'");
1174*e0c4386eSCy Schubert        } else {
1175*e0c4386eSCy Schubert            checkflags($cmd, @doc);
1176*e0c4386eSCy Schubert        }
1177*e0c4386eSCy Schubert    }
1178*e0c4386eSCy Schubert}
1179*e0c4386eSCy Schubert
1180*e0c4386eSCy Schubert# Populate %state
1181*e0c4386eSCy Schubertloadnum('util/libcrypto.num', 'crypto');
1182*e0c4386eSCy Schubertloadnum('util/libssl.num', 'ssl');
1183*e0c4386eSCy Schubertloadnum('util/other.syms', 'other');
1184*e0c4386eSCy Schubertloadnum('util/other-internal.syms');
1185*e0c4386eSCy Schubertif ( $opt_o ) {
1186*e0c4386eSCy Schubert    loadmissing('util/missingmacro111.txt', 'crypto');
1187*e0c4386eSCy Schubert    loadmissing('util/missingcrypto111.txt', 'crypto');
1188*e0c4386eSCy Schubert    loadmissing('util/missingssl111.txt', 'ssl');
1189*e0c4386eSCy Schubert} elsif ( !$opt_u ) {
1190*e0c4386eSCy Schubert    loadmissing('util/missingmacro.txt', 'crypto');
1191*e0c4386eSCy Schubert    loadmissing('util/missingcrypto.txt', 'crypto');
1192*e0c4386eSCy Schubert    loadmissing('util/missingssl.txt', 'ssl');
1193*e0c4386eSCy Schubert    loadmissing('util/missingcrypto-internal.txt');
1194*e0c4386eSCy Schubert    loadmissing('util/missingssl-internal.txt');
1195*e0c4386eSCy Schubert}
1196*e0c4386eSCy Schubert
1197*e0c4386eSCy Schubertif ( $opt_n || $opt_l || $opt_u || $opt_v ) {
1198*e0c4386eSCy Schubert    my @files_to_read = ( $opt_n && @ARGV ) ? @ARGV : files(TAGS => 'manual');
1199*e0c4386eSCy Schubert
1200*e0c4386eSCy Schubert    foreach (@files_to_read) {
1201*e0c4386eSCy Schubert        my %podinfo = extract_pod_info($_, { debug => $debug });
1202*e0c4386eSCy Schubert
1203*e0c4386eSCy Schubert        collectnames(%podinfo)
1204*e0c4386eSCy Schubert            if ( $opt_l || $opt_u || $opt_v );
1205*e0c4386eSCy Schubert
1206*e0c4386eSCy Schubert        check(%podinfo)
1207*e0c4386eSCy Schubert            if ( $opt_n );
1208*e0c4386eSCy Schubert    }
1209*e0c4386eSCy Schubert}
1210*e0c4386eSCy Schubert
1211*e0c4386eSCy Schubertif ( $opt_l ) {
1212*e0c4386eSCy Schubert    checklinks();
1213*e0c4386eSCy Schubert}
1214*e0c4386eSCy Schubert
1215*e0c4386eSCy Schubertif ( $opt_n ) {
1216*e0c4386eSCy Schubert    # If not given args, check that all man1 commands are named properly.
1217*e0c4386eSCy Schubert    if ( scalar @ARGV == 0 && grep /man1/, @sections ) {
1218*e0c4386eSCy Schubert        foreach ( files(TAGS => [ 'public_manual', 'man1' ]) ) {
1219*e0c4386eSCy Schubert            next if /openssl\.pod/
1220*e0c4386eSCy Schubert                || /CA\.pl/ || /tsget\.pod/; # these commands are special cases
1221*e0c4386eSCy Schubert            err("$_ doesn't start with openssl-") unless /openssl-/;
1222*e0c4386eSCy Schubert        }
1223*e0c4386eSCy Schubert    }
1224*e0c4386eSCy Schubert}
1225*e0c4386eSCy Schubert
1226*e0c4386eSCy Schubertcheckstate();
1227*e0c4386eSCy Schubert
1228*e0c4386eSCy Schubertif ( $opt_u || $opt_v) {
1229*e0c4386eSCy Schubert    printem('crypto');
1230*e0c4386eSCy Schubert    printem('ssl');
1231*e0c4386eSCy Schubert    checkmacros();
1232*e0c4386eSCy Schubert}
1233*e0c4386eSCy Schubert
1234*e0c4386eSCy Schubertexit $status;
1235