#! /usr/bin/env perl
# Copyright 2002-2019 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (the "License"). You may not use
# this file except in compliance with the License. You can obtain a copy
# in the file LICENSE in the source distribution or at
# https://www.openssl.org/source/license.html
require 5.10.0;
use warnings;
use strict;
use Pod::Checker;
use File::Find;
use File::Basename;
use File::Spec::Functions;
use Getopt::Std;
use lib catdir(dirname($0), "perl");
use OpenSSL::Util::Pod;
# Set to 1 for debug output
my $debug = 0;
# Options.
our($opt_d);
our($opt_e);
our($opt_s);
our($opt_o);
our($opt_h);
our($opt_l);
our($opt_n);
our($opt_p);
our($opt_u);
our($opt_v);
our($opt_c);
# Print usage message and exit.
sub help {
print <<EOF;
Find small errors (nits) in documentation. Options:
-c List undocumented commands and options
-d Detailed list of undocumented (implies -u)
-e Detailed list of new undocumented (implies -v)
-h Print this help message
-l Print bogus links
-n Print nits in POD pages
-o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
-u Count undocumented functions
-v Count new undocumented functions
EOF
exit;
}
getopts('cdehlnouv');
help() if $opt_h;
$opt_u = 1 if $opt_d;
$opt_v = 1 if $opt_o || $opt_e;
die "Cannot use both -u and -v"
if $opt_u && $opt_v;
die "Cannot use both -d and -e"
if $opt_d && $opt_e;
# We only need to check c, l, n, u and v.
# Options d, e, o imply one of the above.
die "Need one of -[cdehlnouv] flags.\n"
unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
my $temp = '/tmp/docnits.txt';
my $OUT;
my %public;
my $status = 0;
my %mandatory_sections = (
'*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
1 => [ 'SYNOPSIS', 'OPTIONS' ],
3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
5 => [ ],
7 => [ ]
);
# Print error message, set $status.
sub err {
print join(" ", @_), "\n";
$status = 1
}
# Cross-check functions in the NAME and SYNOPSIS section.
sub name_synopsis {
my $id = shift;
my $filename = shift;
my $contents = shift;
# Get NAME section and all words in it.
return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
my $tmp = $1;
$tmp =~ tr/\n/ /;
err($id, "trailing comma before - in NAME")
if $tmp =~ /, *-/;
$tmp =~ s/ -.*//g;
err($id, "POD markup among the names in NAME")
if $tmp =~ /[<>]/;
$tmp =~ s/ */ /g;
err($id, "missing comma in NAME")
if $tmp =~ /[^,] /;
my $dirname = dirname($filename);
my $simplename = basename(basename($filename, ".in"), ".pod");
my $foundfilename = 0;
my %foundfilenames = ();
my %names;
foreach my $n ( split ',', $tmp ) {
$n =~ s/^\s+//;
$n =~ s/\s+$//;
err($id, "the name '$n' contains white-space")
if $n =~ /\s/;
$names{$n} = 1;
$foundfilename++ if $n eq $simplename;
$foundfilenames{$n} = 1
if -f "$dirname/$n.pod" && $n ne $simplename;
}
err($id, "the following exist as other .pod files:",
sort keys %foundfilenames)
if %foundfilenames;
err($id, "$simplename (filename) missing from NAME section")
unless $foundfilename;
if ( $filename !~ /internal/ ) {
foreach my $n ( keys %names ) {
err($id, "$n is not public")
if !defined $public{$n};
}
}
# Find all functions in SYNOPSIS
return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
my $syn = $1;
foreach my $line ( split /\n+/, $syn ) {
next unless $line =~ /^\s/;
my $sym;
my $is_prototype = 1;
$line =~ s/STACK_OF\([^)]+\)/int/g;
$line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
$line =~ s/__declspec\([^)]+\)//;
if ( $line =~ /typedef.*\(\*\S+\)\s+\(/ ) {
# a callback function with whitespace before the argument list:
# typedef ... (*NAME) (...
err($id, "function typedef has space before arg list: $line");
}
if ( $line =~ /env (\S*)=/ ) {
# environment variable env NAME=...
$sym = $1;
} elsif ( $line =~ /typedef.*\(\*(\S+)\)\s*\(/ ) {
# a callback function pointer: typedef ... (*NAME)(...