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