ba600367f6abe8a35251b5d370625ec4c189b2f2
[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     # Get NAME section and all words in it.
42     return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
43     my $tmp = $1;
44     $tmp =~ tr/\n/ /;
45     $tmp =~ s/-.*//g;
46     $tmp =~ s/,//g;
47
48     my $dirname = dirname($filename);
49     my $simplename = basename($filename);
50     $simplename =~ s/.pod$//;
51     my $foundfilename = 0;
52     my %foundfilenames = ();
53     my %names;
54     foreach my $n ( split ' ', $tmp ) {
55         $names{$n} = 1;
56         $foundfilename++ if $n eq $simplename;
57         $foundfilenames{$n} = 1
58             if -f "$dirname/$n.pod" && $n ne $simplename;
59     }
60     print "$id the following exist as other .pod files:\n",
61         join(" ", sort keys %foundfilenames), "\n"
62         if %foundfilenames;
63     print "$id $simplename (filename) missing from NAME section\n",
64         unless $foundfilename;
65
66     # Find all functions in SYNOPSIS
67     return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
68     my $syn = $1;
69     foreach my $line ( split /\n+/, $syn ) {
70         my $sym;
71         $line =~ s/STACK_OF\([^)]+\)/int/g;
72         $line =~ s/__declspec\([^)]+\)//;
73         if ( $line =~ /typedef.* (\S+);/ ) {
74             $sym = $1;
75         } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
76             $sym = $1;
77         } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
78             $sym = $1;
79         }
80         else {
81             next;
82         }
83         print "$id $sym missing from NAME section\n"
84             unless defined $names{$sym};
85         $names{$sym} = 2;
86     }
87
88     foreach my $n ( keys %names ) {
89         next if $names{$n} == 2;
90         print "$id $n missing from SYNOPSIS\n";
91     }
92 }
93
94 sub check()
95 {
96     my $filename = shift;
97     my $dirname = basename(dirname($filename));
98
99     my $contents = '';
100     {
101         local $/ = undef;
102         open POD, $filename or die "Couldn't open $filename, $!";
103         $contents = <POD>;
104         close POD;
105     }
106
107     my $id = "${filename}:1:";
108
109     &name_synopsis($id, $filename, $contents)
110         unless $contents =~ /=for comment generic/
111             or $contents =~ /=for comment openssl_manual_section:7/
112             or $filename =~ m@/apps/@;
113
114     print "$id doesn't start with =pod\n"
115         if $contents !~ /^=pod/;
116     print "$id doesn't end with =cut\n"
117         if $contents !~ /=cut\n$/;
118     print "$id more than one cut line.\n"
119         if $contents =~ /=cut.*=cut/ms;
120     print "$id missing copyright\n"
121         if $contents !~ /Copyright .* The OpenSSL Project Authors/;
122     print "$id copyright not last\n"
123         if $contents =~ /head1 COPYRIGHT.*=head/ms;
124     print "$id head2 in All uppercase\n"
125         if $contents =~ /head2\s+[A-Z ]+\n/;
126     print "$id extra space after head\n"
127         if $contents =~ /=head\d\s\s+/;
128     print "$id period in NAME section\n"
129         if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
130     print "$id POD markup in NAME section\n"
131         if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
132
133     # Look for multiple consecutive openssl #include lines.
134     # Consecutive because of files like md5.pod. Sometimes it's okay
135     # or necessary, as in ssl/SSL_set1_host.pod
136     if ( $contents !~ /=for comment multiple includes/ ) {
137         if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
138             my $count = 0;
139             foreach my $line ( split /\n+/, $1 ) {
140                 if ( $line =~ m@include <openssl/@ ) {
141                     if ( ++$count == 2 ) {
142                         print "$id has multiple includes\n";
143                     }
144                 } else {
145                     $count = 0;
146                 }
147             }
148         }
149     }
150
151     return unless $opt_s;
152
153     # Find what section this page is in.  If run from "." assume
154     # section 3.
155     my $section = $default_sections{$dirname} || 3;
156     if ($contents =~ /^=for\s+comment\s+openssl_manual_section:\s*(\d+)\s*$/m) {
157         $section = $1;
158     }
159
160     foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
161         print "$id doesn't have a head1 section matching $_\n"
162             if $contents !~ /^=head1\s+${_}\s*$/m;
163     }
164
165     open my $OUT, '>', $temp
166         or die "Can't open $temp, $!";
167     podchecker($filename, $OUT);
168     close $OUT;
169     open $OUT, '<', $temp
170         or die "Can't read $temp, $!";
171     while ( <$OUT> ) {
172         next if /\(section\) in.*deprecated/;
173         print;
174     }
175     close $OUT;
176     unlink $temp || warn "Can't remove $temp, $!";
177 }
178
179 getopts('s');
180
181 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
182     &check($_);
183 }
184
185 exit;