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