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