cd30dfeb26f51e6e6ab47f46f46bffa35ce85bcc
[openssl.git] / util / find-doc-nits.pl
1 #! /usr/bin/env perl
2 # Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 require 5.10.0;
11 use warnings;
12 use strict;
13 use Pod::Checker;
14 use File::Find;
15 use File::Basename;
16 use Getopt::Std;
17
18 our($opt_s);
19
20 my $temp = '/tmp/docnits.txt';
21 my $OUT;
22
23 my %mandatory_sections =
24     ( '*'    => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
25       1      => [ 'SYNOPSIS', '(COMMAND\s+)?OPTIONS' ],
26       3      => [ 'SYNOPSIS', 'RETURN\s+VALUES' ],
27       5      => [ ],
28       7      => [ ] );
29 my %default_sections =
30     ( apps   => 1,
31       crypto => 3,
32       ssl    => 3 );
33
34 # Cross-check functions in the NAME and SYNOPSIS section.
35 sub name_synopsis()
36 {
37     my $id = shift;
38     my $filename = shift;
39     my $contents = shift;
40
41     # If it's a generic page (all lowercase), or apps, skip it.
42     return if $filename =~ /[a-z]+\.pod/;
43     return if $filename =~ m@/apps/@;
44
45     # Get NAME section and all words in it.
46     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
47     my $tmp = $1;
48     $tmp =~ tr/\n/ /;
49     $tmp =~ s/-.*//g;
50     $tmp =~ s/,//g;
51
52     my $dirname = dirname($filename);
53     my $simplename = basename($filename);
54     $simplename =~ s/.pod$//;
55     my $foundfilename = 0;
56     my %foundfilenames = ();
57     my %names;
58     foreach my $n ( split ' ', $tmp ) {
59         $names{$n} = 1;
60         $foundfilename++ if $n eq $simplename;
61         $foundfilenames{$n} = 1
62             if -f "$dirname/$n.pod" && $n ne $simplename;
63     }
64     print "$id the following exist as other .pod files:\n",
65         join(" ", sort keys %foundfilenames), "\n"
66         if %foundfilenames;
67     print "$id $simplename (filename) missing from NAME section\n",
68         unless $foundfilename;
69
70     # Find all functions in SYNOPSIS
71     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
72     my $syn = $1;
73     foreach my $line ( split /\n+/, $syn ) {
74         next if $line =~ /typedef/;
75         next if $line =~ /STACK_OF/;
76         next unless $line =~ /([A-Za-z0-9_]+)\(/;
77         print "$id $1 missing from NAME section\n"
78             unless defined $names{$1};
79         $names{$1} = 2;
80     }
81
82     foreach my $n ( keys %names ) {
83         next if $names{$n} == 2;
84         print "$id $n missing from SYNOPSIS\n";
85     }
86 }
87
88 sub check()
89 {
90     my $filename = shift;
91     my $dirname = basename(dirname($filename));
92
93     my $contents = '';
94     {
95         local $/ = undef;
96         open POD, $filename or die "Couldn't open $filename, $!";
97         $contents = <POD>;
98         close POD;
99     }
100
101     my $id = "${filename}:1:";
102
103     &name_synopsis($id, $filename, $contents)
104         unless $contents =~ /=for comment generic/;
105
106     print "$id doesn't start with =pod\n"
107         if $contents !~ /^=pod/;
108     print "$id doesn't end with =cut\n"
109         if $contents !~ /=cut\n$/;
110     print "$id more than one cut line.\n"
111         if $contents =~ /=cut.*=cut/ms;
112     print "$id missing copyright\n"
113         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
114     print "$id copyright not last\n"
115         if $contents =~ /head1 COPYRIGHT.*=head/ms;
116     print "$id head2 in All uppercase\n"
117         if $contents =~ /head2\s+[A-Z ]+\n/;
118     print "$id extra space after head\n"
119         if $contents =~ /=head\d\s\s+/;
120     print "$id period in NAME section\n"
121         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
122     print "$id POD markup in NAME section\n"
123         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
124
125     # Look for multiple consecutive openssl #include lines.
126     # Consecutive because of files like md5.pod. Sometimes it's okay
127     # or necessary, as in ssl/SSL_set1_host.pod
128     if ( $contents !~ /=for comment multiple includes/ ) {
129         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
130             my $count = 0;
131             foreach my $line ( split /\n+/, $1 ) {
132                 if ( $line =~ m@include <openssl/@ ) {
133                     if ( ++$count == 2 ) {
134                         print "$id has multiple includes\n";
135                     }
136                 } else {
137                     $count = 0;
138                 }
139             }
140         }
141     }
142
143     return unless $opt_s;
144
145     # Find what section this page is in.  If run from "." assume
146     # section 3.
147     my $section = $default_sections{$dirname} || 3;
148     if ($contents =~ /^=for\s+comment\s+openssl_manual_section:\s*(\d+)\s*$/m) {
149         $section = $1;
150     }
151
152     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
153         print "$id doesn't have a head1 section matching $_\n"
154             if $contents !~ /^=head1\s+${_}\s*$/m;
155     }
156
157     open my $OUT, '>', $temp
158         or die "Can't open $temp, $!";
159     podchecker($filename, $OUT);
160     close $OUT;
161     open $OUT, '<', $temp
162         or die "Can't read $temp, $!";
163     while ( <$OUT> ) {
164         next if /\(section\) in.*deprecated/;
165         print;
166     }
167     close $OUT;
168     unlink $temp || warn "Can't remove $temp, $!";
169 }
170
171 getopts('s');
172
173 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
174     &check($_);
175 }
176
177 exit;