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