]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/find-doc-nits
Rename SSL[_CTX]_add1_CA_list -> SSL[_CTX]_add1_to_CA_list
[thirdparty/openssl.git] / util / find-doc-nits
CommitLineData
1bc74519 1#! /usr/bin/env perl
48e5119a 2# Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
05ea606a
RS
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
1bc74519
RS
9
10require 5.10.0;
11use warnings;
12use strict;
13use Pod::Checker;
14use File::Find;
169a8e39 15use File::Basename;
71a8b855 16use File::Spec::Functions;
35ea640a 17use Getopt::Std;
71a8b855
RS
18use lib catdir(dirname($0), "perl");
19use OpenSSL::Util::Pod;
35ea640a 20
71a8b855 21# Options.
8d50b9c1 22our($opt_d);
71a8b855 23our($opt_h);
9e183d22 24our($opt_l);
8d50b9c1 25our($opt_n);
274d1bee 26our($opt_p);
8d50b9c1 27our($opt_u);
e75138ab 28our($opt_c);
71a8b855
RS
29
30sub help()
31{
32 print <<EOF;
33Find small errors (nits) in documentation. Options:
8d50b9c1 34 -d Detailed list of undocumented (implies -u)
9e183d22 35 -l Print bogus links
71a8b855 36 -n Print nits in POD pages
274d1bee 37 -p Warn if non-public name documented (implies -n)
71a8b855
RS
38 -u List undocumented functions
39 -h Print this help message
e75138ab 40 -c List undocumented commands and options
71a8b855
RS
41EOF
42 exit;
43}
1bc74519 44
05ea606a
RS
45my $temp = '/tmp/docnits.txt';
46my $OUT;
274d1bee 47my %public;
05ea606a 48
169a8e39
RL
49my %mandatory_sections =
50 ( '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
3dfda1a6
RS
51 1 => [ 'SYNOPSIS', 'OPTIONS' ],
52 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
169a8e39
RL
53 5 => [ ],
54 7 => [ ] );
169a8e39 55
35ea640a
RS
56# Cross-check functions in the NAME and SYNOPSIS section.
57sub name_synopsis()
58{
59 my $id = shift;
60 my $filename = shift;
61 my $contents = shift;
62
35ea640a
RS
63 # Get NAME section and all words in it.
64 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
65 my $tmp = $1;
66 $tmp =~ tr/\n/ /;
3ba4dac6 67 print "$id trailing comma before - in NAME\n" if $tmp =~ /, *-/;
2bcb232e
RS
68 $tmp =~ s/ -.*//g;
69 $tmp =~ s/ */ /g;
70 print "$id missing comma in NAME\n" if $tmp =~ /[^,] /;
35ea640a 71 $tmp =~ s/,//g;
fbba5d11
RS
72
73 my $dirname = dirname($filename);
74 my $simplename = basename($filename);
75 $simplename =~ s/.pod$//;
76 my $foundfilename = 0;
77 my %foundfilenames = ();
35ea640a
RS
78 my %names;
79 foreach my $n ( split ' ', $tmp ) {
80 $names{$n} = 1;
fbba5d11
RS
81 $foundfilename++ if $n eq $simplename;
82 $foundfilenames{$n} = 1
83 if -f "$dirname/$n.pod" && $n ne $simplename;
35ea640a 84 }
fbba5d11
RS
85 print "$id the following exist as other .pod files:\n",
86 join(" ", sort keys %foundfilenames), "\n"
87 if %foundfilenames;
274d1bee 88 print "$id $simplename (filename) missing from NAME section\n"
fbba5d11 89 unless $foundfilename;
1722496f
RS
90 foreach my $n ( keys %names ) {
91 print "$id $n is not public\n"
92 if $opt_p and !defined $public{$n};
93 }
35ea640a
RS
94
95 # Find all functions in SYNOPSIS
96 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
97 my $syn = $1;
98 foreach my $line ( split /\n+/, $syn ) {
8162f6f5 99 my $sym;
c952780c
RS
100 $line =~ s/STACK_OF\([^)]+\)/int/g;
101 $line =~ s/__declspec\([^)]+\)//;
121677b4
RS
102 if ( $line =~ /env (\S*)=/ ) {
103 # environment variable env NAME=...
104 $sym = $1;
105 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) {
0ed78e78
RL
106 # a callback function pointer: typedef ... (*NAME)(...
107 $sym = $1;
108 } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) {
109 # a callback function signature: typedef ... NAME(...
121677b4
RS
110 $sym = $1;
111 } elsif ( $line =~ /typedef.* (\S+);/ ) {
112 # a simple typedef: typedef ... NAME;
8162f6f5 113 $sym = $1;
5d583521 114 } elsif ( $line =~ /enum (\S*) \{/ ) {
d4ea9659
RS
115 # an enumeration: enum ... {
116 $sym = $1;
c952780c 117 } elsif ( $line =~ /#define ([A-Za-z0-9_]+)/ ) {
8162f6f5
RS
118 $sym = $1;
119 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
120 $sym = $1;
121 }
122 else {
123 next;
124 }
125 print "$id $sym missing from NAME section\n"
126 unless defined $names{$sym};
127 $names{$sym} = 2;
aebb9aac
RS
128
129 # Do some sanity checks on the prototype.
130 print "$id prototype missing spaces around commas: $line\n"
131 if ( $line =~ /[a-z0-9],[^ ]/ );
35ea640a
RS
132 }
133
134 foreach my $n ( keys %names ) {
135 next if $names{$n} == 2;
136 print "$id $n missing from SYNOPSIS\n";
137 }
138}
139
1bc74519
RS
140sub check()
141{
169a8e39
RL
142 my $filename = shift;
143 my $dirname = basename(dirname($filename));
843666ff 144
1bc74519
RS
145 my $contents = '';
146 {
147 local $/ = undef;
169a8e39 148 open POD, $filename or die "Couldn't open $filename, $!";
1bc74519
RS
149 $contents = <POD>;
150 close POD;
151 }
843666ff
RS
152
153 my $id = "${filename}:1:";
35ea640a 154
4692340e 155 &name_synopsis($id, $filename, $contents)
8162f6f5 156 unless $contents =~ /=for comment generic/
99d63d46 157 or $filename =~ m@man[157]/@;
35ea640a
RS
158
159 print "$id doesn't start with =pod\n"
05ea606a 160 if $contents !~ /^=pod/;
35ea640a 161 print "$id doesn't end with =cut\n"
05ea606a 162 if $contents !~ /=cut\n$/;
35ea640a 163 print "$id more than one cut line.\n"
05ea606a 164 if $contents =~ /=cut.*=cut/ms;
35ea640a 165 print "$id missing copyright\n"
05ea606a 166 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
35ea640a 167 print "$id copyright not last\n"
05ea606a 168 if $contents =~ /head1 COPYRIGHT.*=head/ms;
35ea640a 169 print "$id head2 in All uppercase\n"
843666ff 170 if $contents =~ /head2\s+[A-Z ]+\n/;
35ea640a
RS
171 print "$id extra space after head\n"
172 if $contents =~ /=head\d\s\s+/;
173 print "$id period in NAME section\n"
174 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
175 print "$id POD markup in NAME section\n"
176 if $contents =~ /=head1 NAME.*[<>].*=head1 SYNOPSIS/ms;
5a3371e2
RS
177 print "$id Duplicate $1 in L<>\n"
178 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
e1271ac2 179 print "$id Bad =over $1\n"
2f61bc2e 180 if $contents =~ /=over([^ ][^24])/;
e90fc053
RS
181 print "$id Possible version style issue\n"
182 if $contents =~ /OpenSSL version [019]/;
843666ff 183
843666ff 184 if ( $contents !~ /=for comment multiple includes/ ) {
a95d7574
RS
185 # Look for multiple consecutive openssl #include lines
186 # (non-consecutive lines are okay; see man3/MD5.pod).
843666ff
RS
187 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
188 my $count = 0;
189 foreach my $line ( split /\n+/, $1 ) {
190 if ( $line =~ m@include <openssl/@ ) {
a95d7574 191 print "$id has multiple includes\n" if ++$count == 2;
843666ff
RS
192 } else {
193 $count = 0;
194 }
195 }
196 }
197 }
05ea606a 198
35ea640a
RS
199 open my $OUT, '>', $temp
200 or die "Can't open $temp, $!";
169a8e39 201 podchecker($filename, $OUT);
35ea640a
RS
202 close $OUT;
203 open $OUT, '<', $temp
204 or die "Can't read $temp, $!";
205 while ( <$OUT> ) {
206 next if /\(section\) in.*deprecated/;
207 print;
208 }
209 close $OUT;
210 unlink $temp || warn "Can't remove $temp, $!";
a95d7574
RS
211
212 # Find what section this page is in; assume 3.
213 my $section = 3;
214 $section = $1 if $dirname =~ /man([1-9])/;
215
216 foreach ((@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}})) {
217 # Skip "return values" if not -s
a95d7574
RS
218 print "$id: missing $_ head1 section\n"
219 if $contents !~ /^=head1\s+${_}\s*$/m;
220 }
05ea606a 221}
1bc74519 222
71a8b855
RS
223my %dups;
224
225sub parsenum()
226{
227 my $file = shift;
228 my @apis;
229
230 open my $IN, '<', $file
231 or die "Can't open $file, $!, stopped";
232
233 while ( <$IN> ) {
274d1bee 234 next if /^#/;
71a8b855
RS
235 next if /\bNOEXIST\b/;
236 next if /\bEXPORT_VAR_AS_FUNC\b/;
1722496f
RS
237 my @fields = split();
238 die "Malformed line $_"
239 if scalar @fields != 2 && scalar @fields != 4;
240 push @apis, $fields[0];
71a8b855
RS
241 }
242
243 close $IN;
244
274d1bee 245 print "# Found ", scalar(@apis), " in $file\n" unless $opt_p;
71a8b855
RS
246 return sort @apis;
247}
248
249sub getdocced()
250{
251 my $dir = shift;
252 my %return;
253
254 foreach my $pod ( glob("$dir/*.pod") ) {
255 my %podinfo = extract_pod_info($pod);
256 foreach my $n ( @{$podinfo{names}} ) {
257 $return{$n} = $pod;
258 print "# Duplicate $n in $pod and $dups{$n}\n"
259 if defined $dups{$n} && $dups{$n} ne $pod;
260 $dups{$n} = $pod;
261 }
262 }
263
264 return %return;
265}
266
267my %docced;
268
9a2dfc0f
RS
269sub checkmacros()
270{
271 my $count = 0;
272
273 print "# Checking macros (approximate)\n";
274 foreach my $f ( glob('include/openssl/*.h') ) {
275 # Skip some internals we don't want to document yet.
276 next if $f eq 'include/openssl/asn1.h';
277 next if $f eq 'include/openssl/asn1t.h';
278 next if $f eq 'include/openssl/err.h';
279 open(IN, $f) || die "Can't open $f, $!";
280 while ( <IN> ) {
281 next unless /^#\s*define\s*(\S+)\(/;
282 my $macro = $1;
283 next if $docced{$macro};
284 next if $macro =~ /i2d_/
285 || $macro =~ /d2i_/
286 || $macro =~ /DEPRECATEDIN/
287 || $macro =~ /IMPLEMENT_/
288 || $macro =~ /DECLARE_/;
8d50b9c1 289 print "$f:$macro\n" if $opt_d;
9a2dfc0f
RS
290 $count++;
291 }
292 close(IN);
293 }
44e69951 294 print "# Found $count macros missing (not all should be documented)\n"
9a2dfc0f
RS
295}
296
71a8b855
RS
297sub printem()
298{
299 my $libname = shift;
300 my $numfile = shift;
301 my $count = 0;
302
303 foreach my $func ( &parsenum($numfile) ) {
304 next if $docced{$func};
305
306 # Skip ASN1 utilities
307 next if $func =~ /^ASN1_/;
308
8d50b9c1 309 print "$libname:$func\n" if $opt_d;
71a8b855
RS
310 $count++;
311 }
312 print "# Found $count missing from $numfile\n\n";
313}
314
315
9e183d22
RS
316# Collection of links in each POD file.
317# filename => [ "foo(1)", "bar(3)", ... ]
318my %link_collection = ();
319# Collection of names in each POD file.
320# "name(s)" => filename
321my %name_collection = ();
322
323sub collectnames {
324 my $filename = shift;
325 $filename =~ m|man(\d)/|;
326 my $section = $1;
327 my $simplename = basename($filename, ".pod");
328 my $id = "${filename}:1:";
329
330 my $contents = '';
331 {
332 local $/ = undef;
333 open POD, $filename or die "Couldn't open $filename, $!";
334 $contents = <POD>;
335 close POD;
336 }
337
338 $contents =~ /=head1 NAME([^=]*)=head1 /ms;
339 my $tmp = $1;
340 unless (defined $tmp) {
341 print "$id weird name section\n";
342 return;
343 }
344 $tmp =~ tr/\n/ /;
345 $tmp =~ s/-.*//g;
346
347 my @names = map { s/\s+//g; $_ } split(/,/, $tmp);
348 unless (grep { $simplename eq $_ } @names) {
349 print "$id missing $simplename\n";
350 push @names, $simplename;
351 }
352 foreach my $name (@names) {
353 next if $name eq "";
354 my $name_sec = "$name($section)";
355 if (! exists $name_collection{$name_sec}) {
356 $name_collection{$name_sec} = $filename;
357 } else { #elsif ($filename ne $name_collection{$name_sec}) {
358 print "$id $name_sec also in $name_collection{$name_sec}\n";
359 }
360 }
361
362 my @foreign_names =
363 map { map { s/\s+//g; $_ } split(/,/, $_) }
364 $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/;
365 foreach (@foreign_names) {
366 $name_collection{$_} = undef; # It still exists!
367 }
368
369 my @links = $contents =~ /L<
370 # if the link is of the form L<something|name(s)>,
371 # then remove 'something'. Note that 'something'
372 # may contain POD codes as well...
373 (?:(?:[^\|]|<[^>]*>)*\|)?
46f4e1be 374 # we're only interested in references that have
9e183d22
RS
375 # a one digit section number
376 ([^\/>\(]+\(\d\))
377 /gx;
378 $link_collection{$filename} = [ @links ];
379}
380
381sub checklinks {
382 foreach my $filename (sort keys %link_collection) {
383 foreach my $link (@{$link_collection{$filename}}) {
384 print "${filename}:1: reference to non-existing $link\n"
385 unless exists $name_collection{$link};
386 }
387 }
388}
389
274d1bee
RS
390sub publicize() {
391 foreach my $name ( &parsenum('util/libcrypto.num') ) {
392 $public{$name} = 1;
393 }
394 foreach my $name ( &parsenum('util/libssl.num') ) {
395 $public{$name} = 1;
396 }
397 foreach my $name ( &parsenum('util/private.num') ) {
398 $public{$name} = 1;
399 }
400}
401
e75138ab
RS
402my %skips = (
403 'aes128' => 1,
404 'aes192' => 1,
405 'aes256' => 1,
406 'aria128' => 1,
407 'aria192' => 1,
408 'aria256' => 1,
409 'camellia128' => 1,
410 'camellia192' => 1,
411 'camellia256' => 1,
412 'des' => 1,
413 'des3' => 1,
414 'idea' => 1,
415 '[cipher]' => 1,
416 '[digest]' => 1,
417);
418
419sub checkflags() {
420 my $cmd = shift;
421 my %cmdopts;
422 my %docopts;
423 my $ok = 1;
424
425 # Get the list of options in the command.
426 open CFH, "./apps/openssl list --options $cmd|"
427 || die "Can list options for $cmd, $!";
428 while ( <CFH> ) {
429 chop;
430 s/ .$//;
431 $cmdopts{$_} = 1;
432 }
433 close CFH;
434
435 # Get the list of flags from the synopsis
436 open CFH, "<doc/man1/$cmd.pod"
437 || die "Can't open $cmd.pod, $!";
438 while ( <CFH> ) {
439 chop;
440 last if /DESCRIPTION/;
441 next unless /\[B<-([^ >]+)/;
442 $docopts{$1} = 1;
443 }
444 close CFH;
445
446 # See what's in the command not the manpage.
447 my @undocced = ();
448 foreach my $k ( keys %cmdopts ) {
449 push @undocced, $k unless $docopts{$k};
450 }
451 if ( scalar @undocced > 0 ) {
452 $ok = 0;
453 foreach ( @undocced ) {
454 print "doc/man1/$cmd.pod: Missing -$_\n";
455 }
456 }
457
458 # See what's in the command not the manpage.
459 my @unimpl = ();
460 foreach my $k ( keys %docopts ) {
461 push @unimpl, $k unless $cmdopts{$k};
462 }
463 if ( scalar @unimpl > 0 ) {
464 $ok = 0;
465 foreach ( @unimpl ) {
466 next if defined $skips{$_};
467 print "doc/man1/$cmd.pod: Not implemented -$_\n";
468 }
469 }
470
471 return $ok;
472}
473
a085f43f 474getopts('cdlnphu');
274d1bee
RS
475
476&help() if $opt_h;
a085f43f 477$opt_n = 1 if $opt_p;
8d50b9c1 478$opt_u = 1 if $opt_d;
35ea640a 479
a085f43f 480die "Need one of -[cdlnpu] flags.\n"
e75138ab 481 unless $opt_c or $opt_l or $opt_n or $opt_u;
71a8b855 482
e75138ab
RS
483if ( $opt_c ) {
484 my $ok = 1;
485 my @commands = ();
3dfda1a6 486
e75138ab
RS
487 # Get list of commands.
488 open FH, "./apps/openssl list -1 -commands|"
489 || die "Can't list commands, $!";
490 while ( <FH> ) {
491 chop;
492 push @commands, $_;
493 }
494 close FH;
495
496 # See if each has a manpage.
497 foreach ( @commands ) {
498 next if $_ eq 'help' || $_ eq 'exit';
499 if ( ! -f "doc/man1/$_.pod" ) {
500 print "doc/man1/$_.pod does not exist\n";
501 $ok = 0;
502 } else {
503 $ok = 0 if not &checkflags($_);
504 }
71a8b855 505 }
e75138ab
RS
506
507 # See what help is missing.
508 open FH, "./apps/openssl list --missing-help |"
509 || die "Can't list missing help, $!";
510 while ( <FH> ) {
511 chop;
512 my ($cmd, $flag) = split;
513 print "$cmd has no help for -$flag\n";
514 $ok = 0;
515 }
516 close FH;
517
518 exit 1 if not $ok;
71a8b855 519}
9e183d22
RS
520
521if ( $opt_l ) {
522 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
523 collectnames($_);
524 }
525 checklinks();
526}
527
e75138ab
RS
528if ( $opt_n ) {
529 &publicize() if $opt_p;
530 foreach (@ARGV ? @ARGV : glob('doc/*/*.pod')) {
531 &check($_);
532 }
533}
534
71a8b855
RS
535if ( $opt_u ) {
536 my %temp = &getdocced('doc/man3');
537 foreach ( keys %temp ) {
538 $docced{$_} = $temp{$_};
539 }
540 &printem('crypto', 'util/libcrypto.num');
541 &printem('ssl', 'util/libssl.num');
9a2dfc0f 542 &checkmacros();
1bc74519 543}
05ea606a 544
35ea640a 545exit;