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