]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/find-doc-nits
Configuration and build: Fix solaris tags
[thirdparty/openssl.git] / util / find-doc-nits
CommitLineData
1bc74519 1#! /usr/bin/env perl
33388b44 2# Copyright 2002-2020 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;
a397aca4 13
1624ebdb 14use Carp qw(:DEFAULT cluck);
1bc74519
RS
15use Pod::Checker;
16use File::Find;
169a8e39 17use File::Basename;
71a8b855 18use File::Spec::Functions;
35ea640a 19use Getopt::Std;
1624ebdb
RL
20use FindBin;
21use lib "$FindBin::Bin/perl";
22
71a8b855 23use OpenSSL::Util::Pod;
35ea640a 24
1624ebdb
RL
25use lib '.';
26use configdata;
27
a397aca4
RS
28# Set to 1 for debug output
29my $debug = 0;
705128b0 30
912f8a98 31# Where to find openssl command
1624ebdb 32my $openssl = "./util/opensslwrap.sh";
912f8a98 33
71a8b855 34# Options.
8d50b9c1 35our($opt_d);
b5283535
MC
36our($opt_e);
37our($opt_s);
a03749a8 38our($opt_o);
71a8b855 39our($opt_h);
9e183d22 40our($opt_l);
8d50b9c1 41our($opt_n);
274d1bee 42our($opt_p);
8d50b9c1 43our($opt_u);
b5283535 44our($opt_v);
e75138ab 45our($opt_c);
71a8b855 46
185ec4be 47# Print usage message and exit.
fbad6e79 48sub help {
71a8b855
RS
49 print <<EOF;
50Find small errors (nits) in documentation. Options:
185ec4be 51 -c List undocumented commands and options
8d50b9c1 52 -d Detailed list of undocumented (implies -u)
b5283535 53 -e Detailed list of new undocumented (implies -v)
185ec4be 54 -h Print this help message
9e183d22 55 -l Print bogus links
71a8b855 56 -n Print nits in POD pages
185ec4be 57 -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v)
ee4afacd 58 -u Count undocumented functions
b5283535 59 -v Count new undocumented functions
71a8b855
RS
60EOF
61 exit;
62}
1bc74519 63
185ec4be
RS
64getopts('cdehlnouv');
65
66help() if $opt_h;
67$opt_u = 1 if $opt_d;
68$opt_v = 1 if $opt_o || $opt_e;
69die "Cannot use both -u and -v"
70 if $opt_u && $opt_v;
71die "Cannot use both -d and -e"
72 if $opt_d && $opt_e;
73
74# We only need to check c, l, n, u and v.
75# Options d, e, o imply one of the above.
76die "Need one of -[cdehlnouv] flags.\n"
77 unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v;
78
79
05ea606a
RS
80my $temp = '/tmp/docnits.txt';
81my $OUT;
fbad6e79 82my $status = 0;
05ea606a 83
1624ebdb 84my @sections = ( 'man1', 'man3', 'man5', 'man7' );
a397aca4
RS
85my %mandatory_sections = (
86 '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ],
87 1 => [ 'SYNOPSIS', 'OPTIONS' ],
88 3 => [ 'SYNOPSIS', 'RETURN VALUES' ],
89 5 => [ ],
90 7 => [ ]
8270c479
RL
91 );
92
93# Symbols that we ignored.
94# They are internal macros that we currently don't document
95my $ignored = qr/(?| ^i2d_
96 | ^d2i_
97 | ^DEPRECATEDIN
98 | \Q_fnsig(3)\E$
99 | ^IMPLEMENT_
100 | ^_?DECLARE_
101 )/x;
a397aca4 102
1624ebdb
RL
103# Collect all POD files, both internal and public, and regardless of location
104# We collect them in a hash table with each file being a key, so we can attach
105# tags to them. For example, internal docs will have the word "internal"
106# attached to them.
107my %files = ();
108# We collect files names on the fly, on known tag basis
109my %collected_tags = ();
110# We cache results based on tags
111my %collected_results = ();
112
113# files OPTIONS
114#
115# Example:
116#
117# files(TAGS => 'manual');
118# files(TAGS => [ 'manual', 'man1' ]);
119#
120# This function returns an array of files corresponding to a set of tags
121# given with the options "TAGS". The value of this option can be a single
122# word, or an array of several words, which work as inclusive or exclusive
123# selectors. Inclusive selectors are used to add one more set of files to
124# the returned array, while exclusive selectors limit the set of files added
125# to the array. The recognised tag values are:
126#
127# 'public_manual' - inclusive selector, adds public manuals to the
128# returned array of files.
129# 'internal_manual' - inclusive selector, adds internal manuals to the
130# returned array of files.
131# 'manual' - inclusive selector, adds any manual to the returned
132# array of files. This is really a shorthand for
133# 'public_manual' and 'internal_manual' combined.
134# 'public_header' - inclusive selector, adds public headers to the
135# returned array of files.
136# 'header' - inclusive selector, adds any header file to the
137# returned array of files. Since we currently only
138# care about public headers, this is exactly
139# equivalent to 'public_header', but is present for
140# consistency.
141#
142# 'man1', 'man3', 'man5', 'man7'
143# - exclusive selectors, only applicable together with
144# any of the manual selectors. If any of these are
145# present, only the manuals from the given sections
146# will be include. If none of these are present,
147# the manuals from all sections will be returned.
148#
149# All returned manual files come from configdata.pm.
150# All returned header files come from looking inside
151# "$config{sourcedir}/include/openssl"
152#
153sub files {
154 my %opts = ( @_ ); # Make a copy of the arguments
155
156 $opts{TAGS} = [ $opts{TAGS} ] if ref($opts{TAGS}) eq '';
157
158 croak "No tags given, or not an array"
159 unless exists $opts{TAGS} && ref($opts{TAGS}) eq 'ARRAY';
160
161 my %tags = map { $_ => 1 } @{$opts{TAGS}};
162 $tags{public_manual} = 1
163 if $tags{manual} && ($tags{public} // !$tags{internal});
164 $tags{internal_manual} = 1
165 if $tags{manual} && ($tags{internal} // !$tags{public});
166 $tags{public_header} = 1
167 if $tags{header} && ($tags{public} // !$tags{internal});
168 delete $tags{manual};
169 delete $tags{header};
170 delete $tags{public};
171 delete $tags{internal};
172
173 my $tags_as_key = join(':', sort keys %tags);
174
175 cluck "DEBUG[files]: This is how we got here!" if $debug;
176 print STDERR "DEBUG[files]: tags: $tags_as_key\n" if $debug;
177
178 my %tags_to_collect = ( map { $_ => 1 }
179 grep { !exists $collected_tags{$_} }
180 keys %tags );
181
182 if ($tags_to_collect{public_manual}) {
183 print STDERR "DEBUG[files]: collecting public manuals\n"
184 if $debug;
185
186 # The structure in configdata.pm is that $unified_info{mandocs}
187 # contains lists of man files, and in turn, $unified_info{depends}
188 # contains hash tables showing which POD file each of those man
189 # files depend on. We use that information to find the POD files,
190 # and to attach the man section they belong to as tags
191 foreach my $mansect ( @sections ) {
192 foreach ( map { @{$unified_info{depends}->{$_}} }
193 @{$unified_info{mandocs}->{$mansect}} ) {
194 $files{$_} = { $mansect => 1, public_manual => 1 };
195 }
196 }
197 $collected_tags{public_manual} = 1;
198 }
199
200 if ($tags_to_collect{internal_manual}) {
201 print STDERR "DEBUG[files]: collecting internal manuals\n"
202 if $debug;
203
204 # We don't have the internal docs in configdata.pm. However, they
205 # are all in the source tree, so they're easy to find.
206 foreach my $mansect ( @sections ) {
207 foreach ( glob(catfile($config{sourcedir},
208 'doc', 'internal', $mansect, '*.pod')) ) {
209 $files{$_} = { $mansect => 1, internal_manual => 1 };
210 }
211 }
212 $collected_tags{internal_manual} = 1;
213 }
214
215 if ($tags_to_collect{public_header}) {
216 print STDERR "DEBUG[files]: collecting public headers\n"
217 if $debug;
218
219 foreach ( glob(catfile($config{sourcedir},
220 'include', 'openssl', '*.h')) ) {
221 $files{$_} = { public_header => 1 };
222 }
223 }
224
225 my @result = @{$collected_results{$tags_as_key} // []};
226
227 if (!@result) {
228 # Produce a result based on caller tags
229 foreach my $type ( ( 'public_manual', 'internal_manual' ) ) {
230 next unless $tags{$type};
231
232 # If caller asked for specific sections, we care about sections.
233 # Otherwise, we give back all of them.
234 my @selected_sections =
235 grep { $tags{$_} } @sections;
236 @selected_sections = @sections unless @selected_sections;
237
238 foreach my $section ( ( @selected_sections ) ) {
239 push @result,
240 ( sort { basename($a) cmp basename($b) }
241 grep { $files{$_}->{$type} && $files{$_}->{$section} }
242 keys %files );
243 }
244 }
245 if ($tags{public_header}) {
246 push @result,
247 ( sort { basename($a) cmp basename($b) }
248 grep { $files{$_}->{public_header} }
249 keys %files );
250 }
251
252 if ($debug) {
253 print STDERR "DEBUG[files]: result:\n";
254 print STDERR "DEBUG[files]: $_\n" foreach @result;
255 }
256 $collected_results{$tags_as_key} = [ @result ];
257 }
258
259 return @result;
260}
169a8e39 261
fbad6e79
RS
262# Print error message, set $status.
263sub err {
264 print join(" ", @_), "\n";
265 $status = 1
266}
267
35ea640a 268# Cross-check functions in the NAME and SYNOPSIS section.
fbad6e79 269sub name_synopsis {
35ea640a
RS
270 my $id = shift;
271 my $filename = shift;
272 my $contents = shift;
273
35ea640a
RS
274 # Get NAME section and all words in it.
275 return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms;
276 my $tmp = $1;
277 $tmp =~ tr/\n/ /;
ad090d57 278 err($id, "Trailing comma before - in NAME")
fbad6e79 279 if $tmp =~ /, *-/;
2bcb232e 280 $tmp =~ s/ -.*//g;
fbad6e79
RS
281 err($id, "POD markup among the names in NAME")
282 if $tmp =~ /[<>]/;
2bcb232e 283 $tmp =~ s/ */ /g;
ad090d57 284 err($id, "Missing comma in NAME")
fbad6e79 285 if $tmp =~ /[^,] /;
fbba5d11
RS
286
287 my $dirname = dirname($filename);
1624ebdb
RL
288 my $section = basename($dirname);
289 my $simplename = basename($filename, ".pod");
fbba5d11
RS
290 my $foundfilename = 0;
291 my %foundfilenames = ();
35ea640a 292 my %names;
23ab880d
RL
293 foreach my $n ( split ',', $tmp ) {
294 $n =~ s/^\s+//;
295 $n =~ s/\s+$//;
ad090d57 296 err($id, "The name '$n' contains white-space")
23ab880d 297 if $n =~ /\s/;
35ea640a 298 $names{$n} = 1;
fbba5d11
RS
299 $foundfilename++ if $n eq $simplename;
300 $foundfilenames{$n} = 1
1624ebdb
RL
301 if ( ( grep { basename($_) eq "$n.pod" }
302 files(TAGS => [ 'manual', $section ]) )
303 && $n ne $simplename );
35ea640a 304 }
ad090d57 305 err($id, "The following exist as other .pod files:",
fbad6e79 306 sort keys %foundfilenames)
fbba5d11 307 if %foundfilenames;
fbad6e79 308 err($id, "$simplename (filename) missing from NAME section")
fbba5d11 309 unless $foundfilename;
35ea640a
RS
310
311 # Find all functions in SYNOPSIS
312 return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms;
313 my $syn = $1;
314 foreach my $line ( split /\n+/, $syn ) {
be80b21d 315 next unless $line =~ /^\s/;
8162f6f5 316 my $sym;
31d3a759 317 my $is_prototype = 1;
c952780c 318 $line =~ s/STACK_OF\([^)]+\)/int/g;
4460ad90 319 $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g;
c952780c 320 $line =~ s/__declspec\([^)]+\)//;
28104cdd
DDO
321 if ( $line =~ /typedef.*\(\*\S+\)\s+\(/ ) {
322 # a callback function with whitespace before the argument list:
323 # typedef ... (*NAME) (...
ad090d57 324 err($id, "Function typedef has space before arg list: $line");
28104cdd 325 }
121677b4
RS
326 if ( $line =~ /env (\S*)=/ ) {
327 # environment variable env NAME=...
328 $sym = $1;
86a15d83 329 } elsif ( $line =~ /typedef.*\(\*(\S+)\)\s*\(/ ) {
0ed78e78
RL
330 # a callback function pointer: typedef ... (*NAME)(...
331 $sym = $1;
86a15d83 332 } elsif ( $line =~ /typedef.* (\S+)\(/ ) {
0ed78e78 333 # a callback function signature: typedef ... NAME(...
121677b4
RS
334 $sym = $1;
335 } elsif ( $line =~ /typedef.* (\S+);/ ) {
336 # a simple typedef: typedef ... NAME;
31d3a759 337 $is_prototype = 0;
8162f6f5 338 $sym = $1;
5d583521 339 } elsif ( $line =~ /enum (\S*) \{/ ) {
d4ea9659
RS
340 # an enumeration: enum ... {
341 $sym = $1;
0695b193 342 } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) {
31d3a759 343 $is_prototype = 0;
8162f6f5
RS
344 $sym = $1;
345 } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) {
346 $sym = $1;
347 }
348 else {
349 next;
350 }
fbad6e79 351 err($id, "$sym missing from NAME section")
8162f6f5
RS
352 unless defined $names{$sym};
353 $names{$sym} = 2;
aebb9aac
RS
354
355 # Do some sanity checks on the prototype.
ad090d57 356 err($id, "Prototype missing spaces around commas: $line")
31d3a759 357 if $is_prototype && $line =~ /[a-z0-9],[^ ]/;
35ea640a
RS
358 }
359
360 foreach my $n ( keys %names ) {
361 next if $names{$n} == 2;
fbad6e79 362 err($id, "$n missing from SYNOPSIS")
35ea640a
RS
363 }
364}
365
39a117d1 366# Check if SECTION ($3) is located before BEFORE ($4)
fbad6e79 367sub check_section_location {
39a117d1 368 my $id = shift;
cc838ee2 369 my $contents = shift;
95f92d57
JL
370 my $section = shift;
371 my $before = shift;
cc838ee2 372
485d3361
RS
373 return unless $contents =~ /=head1 $section/
374 and $contents =~ /=head1 $before/;
fbad6e79 375 err($id, "$section should appear before $before section")
95f92d57 376 if $contents =~ /=head1 $before.*=head1 $section/ms;
cc838ee2
PY
377}
378
485d3361
RS
379# Check if a =head1 is duplicated, or a =headX is duplicated within a
380# =head1. Treats =head2 =head3 as equivalent -- it doesn't reset the head3
381# sets if it finds a =head2 -- but that is good enough for now. Also check
382# for proper capitalization, trailing periods, etc.
fbad6e79 383sub check_head_style {
485d3361
RS
384 my $id = shift;
385 my $contents = shift;
386 my %head1;
387 my %subheads;
388
389 foreach my $line ( split /\n+/, $contents ) {
390 next unless $line =~ /^=head/;
391 if ( $line =~ /head1/ ) {
ad090d57 392 err($id, "Duplicate section $line")
485d3361
RS
393 if defined $head1{$line};
394 $head1{$line} = 1;
395 %subheads = ();
396 } else {
ad090d57 397 err($id, "Duplicate subsection $line")
485d3361
RS
398 if defined $subheads{$line};
399 $subheads{$line} = 1;
400 }
ad090d57 401 err($id, "Period in =head")
485d3361 402 if $line =~ /\.[^\w]/ or $line =~ /\.$/;
fbad6e79 403 err($id, "not all uppercase in =head1")
485d3361 404 if $line =~ /head1.*[a-z]/;
ad090d57 405 err($id, "All uppercase in subhead")
485d3361
RS
406 if $line =~ /head[234][ A-Z0-9]+$/;
407 }
408}
409
705128b0
RL
410# Because we have options and symbols with extra markup, we need
411# to take that into account, so we need a regexp that extracts
412# markup chunks, including recursive markup.
413# please read up on /(?R)/ in perlre(1)
414# (note: order is important, (?R) needs to come before .)
415# (note: non-greedy is important, or something like 'B<foo> and B<bar>'
416# will be captured as one item)
417my $markup_re =
418 qr/( # Capture group
419 [BIL]< # The start of what we recurse on
79c44b4e 420 (?:(?-1)|.)*? # recurse the whole regexp (referring to
705128b0
RL
421 # the last opened capture group, i.e. the
422 # start of this regexp), or pick next
423 # character. Do NOT be greedy!
424 > # The end of what we recurse on
425 )/x; # (the x allows this sort of split up regexp)
426
427# Options must start with a dash, followed by a letter, possibly
428# followed by letters, digits, dashes and underscores, and the last
429# character must be a letter or a digit.
430# We do also accept the single -? or -n, where n is a digit
431my $option_re =
432 qr/(?:
433 \? # Single question mark
434 |
435 \d # Single digit
436 |
437 - # Single dash (--)
438 |
439 [[:alpha:]](?:[-_[:alnum:]]*?[[:alnum:]])?
440 )/x;
441
442# Helper function to check if a given $thing is properly marked up
443# option. It returns one of these values:
a397aca4
RS
444# undef if it's not an option
445# "" if it's a malformed option
446# $unwrapped the option with the outermost B<> wrapping removed.
705128b0
RL
447sub normalise_option {
448 my $id = shift;
449 my $filename = shift;
450 my $thing = shift;
451
452 my $unwrapped = $thing;
453 my $unmarked = $thing;
454
455 # $unwrapped is the option with the outer B<> markup removed
456 $unwrapped =~ s/^B<//;
457 $unwrapped =~ s/>$//;
458 # $unmarked is the option with *all* markup removed
459 $unmarked =~ s/[BIL]<|>//msg;
460
461
462 # If we found an option, check it, collect it
463 if ( $unwrapped =~ /^\s*-/ ) {
464 return $unwrapped # return option with outer B<> removed
465 if $unmarked =~ /^-${option_re}$/;
466 return ""; # Malformed option
467 }
468 return undef; # Something else
469}
470
471# Checks of command option (man1) formatting. The man1 checks are
472# restricted to the SYNOPSIS and OPTIONS sections, the rest is too
473# free form, we simply cannot be too strict there.
474
475sub option_check {
476 my $id = shift;
477 my $filename = shift;
478 my $contents = shift;
479
480 my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1);
481
482 # Some pages have more than one OPTIONS section, let's make sure
483 # to get them all
484 my $options = '';
485 while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) {
486 $options .= $1;
487 }
488
489 # Look for options with no or incorrect markup
490 while ( $synopsis =~
491 /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) {
492 err($id, "Malformed option [1] in SYNOPSIS: $&");
493 }
494
495 while ( $synopsis =~ /$markup_re/msg ) {
496 my $found = $&;
497 print STDERR "$id:DEBUG[option_check] SYNOPSIS: found $found\n"
498 if $debug;
499 my $option_uw = normalise_option($id, $filename, $found);
500 err($id, "Malformed option [2] in SYNOPSIS: $found")
501 if defined $option_uw && $option_uw eq '';
502 }
503
504 # In OPTIONS, we look for =item paragraphs.
505 # (?=^\s*$) detects an empty line.
506 while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) {
507 my $item = $&;
508
509 while ( $item =~ /(\[\s*)?($markup_re)/msg ) {
510 my $found = $2;
511 print STDERR "$id:DEBUG[option_check] OPTIONS: found $&\n"
512 if $debug;
513 err($id, "Unexpected bracket in OPTIONS =item: $item")
514 if ($1 // '') ne '' && $found =~ /^B<\s*-/;
515
516 my $option_uw = normalise_option($id, $filename, $found);
517 err($id, "Malformed option in OPTIONS: $found")
518 if defined $option_uw && $option_uw eq '';
519 }
520 }
521}
522
523# Normal symbol form
524my $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/;
525
526# Checks of function name (man3) formatting. The man3 checks are
527# easier than the man1 checks, we only check the names followed by (),
528# and only the names that have POD markup.
705128b0
RL
529sub functionname_check {
530 my $id = shift;
531 my $filename = shift;
532 my $contents = shift;
533
534 while ( $contents =~ /($markup_re)\(\)/msg ) {
535 print STDERR "$id:DEBUG[functionname_check] SYNOPSIS: found $&\n"
536 if $debug;
537
538 my $symbol = $1;
539 my $unmarked = $symbol;
540 $unmarked =~ s/[BIL]<|>//msg;
541
542 err($id, "Malformed symbol: $symbol")
8270c479 543 unless $symbol =~ /^B<.*?>$/ && $unmarked =~ /^${symbol_re}$/
705128b0
RL
544 }
545
546 # We can't do the kind of collecting coolness that option_check()
547 # does, because there are too many things that can't be found in
548 # name repositories like the NAME sections, such as symbol names
549 # with a variable part (typically marked up as B<foo_I<TYPE>_bar>
550}
551
60a7817c
RS
552# This is from http://man7.org/linux/man-pages/man7/man-pages.7.html
553my %preferred_words = (
554 'bitmask' => 'bit mask',
555 'builtin' => 'built-in',
556 #'epoch' => 'Epoch', # handled specially, below
557 'file name' => 'filename',
558 'file system' => 'filesystem',
559 'host name' => 'hostname',
560 'i-node' => 'inode',
561 'lower case' => 'lowercase',
562 'lower-case' => 'lowercase',
563 'non-zero' => 'nonzero',
564 'path name' => 'pathname',
565 'pseudo-terminal' => 'pseudoterminal',
566 'reserved port' => 'privileged port',
567 'system port' => 'privileged port',
568 'realtime' => 'real-time',
569 'real time' => 'real-time',
570 'runtime' => 'run time',
571 'saved group ID'=> 'saved set-group-ID',
572 'saved set-GID' => 'saved set-group-ID',
573 'saved user ID' => 'saved set-user-ID',
574 'saved set-UID' => 'saved set-user-ID',
575 'set-GID' => 'set-group-ID',
576 'setgid' => 'set-group-ID',
577 'set-UID' => 'set-user-ID',
578 'setuid' => 'set-user-ID',
579 'super user' => 'superuser',
580 'super-user' => 'superuser',
581 'super block' => 'superblock',
582 'super-block' => 'superblock',
583 'time stamp' => 'timestamp',
584 'time zone' => 'timezone',
585 'upper case' => 'uppercase',
586 'upper-case' => 'uppercase',
587 'useable' => 'usable',
588 'userspace' => 'user space',
589 'user name' => 'username',
590 'zeroes' => 'zeros'
591);
592
a397aca4 593# Search manpage for words that have a different preferred use.
60a7817c
RS
594sub wording {
595 my $id = shift;
596 my $contents = shift;
597
598 foreach my $k ( keys %preferred_words ) {
9c0586d5
RS
599 # Sigh, trademark
600 next if $k eq 'file system'
601 and $contents =~ /Microsoft Encrypted File System/;
ad090d57 602 err($id, "Found '$k' should use '$preferred_words{$k}'")
60a7817c
RS
603 if $contents =~ /\b\Q$k\E\b/i;
604 }
ad090d57 605 err($id, "Found 'epoch' should use 'Epoch'")
60a7817c 606 if $contents =~ /\bepoch\b/;
4b537191
RS
607 if ( $id =~ m@man1/@ ) {
608 err($id, "found 'tool' in NAME, should use 'command'")
609 if $contents =~ /=head1 NAME.*\btool\b.*=head1 SYNOPSIS/s;
610 err($id, "found 'utility' in NAME, should use 'command'")
611 if $contents =~ /NAME.*\butility\b.*=head1 SYNOPSIS/s;
612
613 }
60a7817c
RS
614}
615
a397aca4 616# Perform all sorts of nit/error checks on a manpage
fbad6e79 617sub check {
8270c479
RL
618 my %podinfo = @_;
619 my $filename = $podinfo{filename};
169a8e39 620 my $dirname = basename(dirname($filename));
8270c479 621 my $contents = $podinfo{contents};
843666ff
RS
622
623 my $id = "${filename}:1:";
fbad6e79 624 check_head_style($id, $contents);
35ea640a 625
39a117d1
RS
626 # Check ordering of some sections in man3
627 if ( $filename =~ m|man3/| ) {
fbad6e79
RS
628 check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES");
629 check_section_location($id, $contents, "SEE ALSO", "HISTORY");
630 check_section_location($id, $contents, "EXAMPLES", "SEE ALSO");
39a117d1
RS
631 }
632
6e4618a0
RS
633 # Make sure every link has a section.
634 while ( $contents =~ /$markup_re/msg ) {
635 my $target = $1;
76fde1db
RL
636 next unless $target =~ /^L<(.*)>$/; # Skip if not L<...>
637 $target = $1; # Peal away L< and >
638 $target =~ s/\/[^\/]*$//; # Peal away possible anchor
639 $target =~ s/.*\|//g; # Peal away possible link text
640 next if $target eq ''; # Skip if links within page, or
6e4618a0 641 next if $target =~ /::/; # links to a Perl module, or
76fde1db
RL
642 next if $target =~ /^https?:/; # is a URL link, or
643 next if $target =~ /\([1357]\)$/; # it has a section
6e4618a0
RS
644 err($id, "Section missing in $target")
645 }
1903a9b7
RS
646 # Check for proper links to commands.
647 while ( $contents =~ /L<([^>]*)\(1\)(?:\/.*)?>/g ) {
648 my $target = $1;
649 next if $target =~ /openssl-?/;
1624ebdb
RL
650 next if ( grep { basename($_) eq "$target.pod" }
651 files(TAGS => [ 'manual', 'man1' ]) );
1903a9b7
RS
652 # TODO: Filter out "foreign manual" links.
653 next if $target =~ /ps|apropos|sha1sum|procmail|perl/;
654 err($id, "Bad command link L<$target(1)>");
655 }
6e4618a0
RS
656 # Check for proper in-man-3 API links.
657 while ( $contents =~ /L<([^>]*)\(3\)(?:\/.*)?>/g ) {
658 my $target = $1;
659 err($id, "Bad L<$target>")
660 unless $target =~ /^[_[:alpha:]][_[:alnum:]]*$/
661 }
662
8270c479 663 unless ( $contents =~ /^=for openssl generic/ms ) {
705128b0
RL
664 if ( $filename =~ m|man3/| ) {
665 name_synopsis($id, $filename, $contents);
666 functionname_check($id, $filename, $contents);
667 } elsif ( $filename =~ m|man1/| ) {
668 option_check($id, $filename, $contents)
669 }
670 }
35ea640a 671
60a7817c
RS
672 wording($id, $contents);
673
ad090d57 674 err($id, "Doesn't start with =pod")
05ea606a 675 if $contents !~ /^=pod/;
ad090d57 676 err($id, "Doesn't end with =cut")
05ea606a 677 if $contents !~ /=cut\n$/;
ad090d57 678 err($id, "More than one cut line.")
05ea606a 679 if $contents =~ /=cut.*=cut/ms;
fbad6e79 680 err($id, "EXAMPLE not EXAMPLES section.")
cda77422 681 if $contents =~ /=head1 EXAMPLE[^S]/;
fbad6e79 682 err($id, "WARNING not WARNINGS section.")
5e0d9c86 683 if $contents =~ /=head1 WARNING[^S]/;
ad090d57 684 err($id, "Missing copyright")
05ea606a 685 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
ad090d57 686 err($id, "Copyright not last")
05ea606a 687 if $contents =~ /head1 COPYRIGHT.*=head/ms;
fbad6e79 688 err($id, "head2 in All uppercase")
843666ff 689 if $contents =~ /head2\s+[A-Z ]+\n/;
ad090d57 690 err($id, "Extra space after head")
35ea640a 691 if $contents =~ /=head\d\s\s+/;
ad090d57 692 err($id, "Period in NAME section")
35ea640a 693 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
fbad6e79 694 err($id, "Duplicate $1 in L<>")
5a3371e2 695 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
fbad6e79 696 err($id, "Bad =over $1")
2f61bc2e 697 if $contents =~ /=over([^ ][^24])/;
fbad6e79 698 err($id, "Possible version style issue")
e90fc053 699 if $contents =~ /OpenSSL version [019]/;
843666ff 700
bb82531f 701 if ( $contents !~ /=for openssl multiple includes/ ) {
a95d7574
RS
702 # Look for multiple consecutive openssl #include lines
703 # (non-consecutive lines are okay; see man3/MD5.pod).
843666ff
RS
704 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
705 my $count = 0;
706 foreach my $line ( split /\n+/, $1 ) {
707 if ( $line =~ m@include <openssl/@ ) {
ad090d57 708 err($id, "Has multiple includes")
fbad6e79 709 if ++$count == 2;
843666ff
RS
710 } else {
711 $count = 0;
712 }
713 }
714 }
715 }
05ea606a 716
35ea640a
RS
717 open my $OUT, '>', $temp
718 or die "Can't open $temp, $!";
169a8e39 719 podchecker($filename, $OUT);
35ea640a
RS
720 close $OUT;
721 open $OUT, '<', $temp
722 or die "Can't read $temp, $!";
723 while ( <$OUT> ) {
724 next if /\(section\) in.*deprecated/;
725 print;
726 }
727 close $OUT;
728 unlink $temp || warn "Can't remove $temp, $!";
a95d7574
RS
729
730 # Find what section this page is in; assume 3.
731 my $section = 3;
732 $section = $1 if $dirname =~ /man([1-9])/;
733
a397aca4 734 foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) {
ad090d57 735 err($id, "Missing $_ head1 section")
a95d7574
RS
736 if $contents !~ /^=head1\s+${_}\s*$/m;
737 }
05ea606a 738}
1bc74519 739
8270c479
RL
740# Information database ###############################################
741
742# Map of links in each POD file; filename => [ "foo(1)", "bar(3)", ... ]
743my %link_map = ();
744# Map of names in each POD file or from "missing" files; possible values are:
745# If found in a POD files, "name(s)" => filename
746# If found in a "missing" file or external, "name(s)" => ''
747my %name_map = ();
748
749# State of man-page names.
750# %state is affected by loading util/*.num and util/*.syms
751# Values may be one of:
752# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
753# 'ssl' : belongs in libssl (loaded from libssl.num)
754# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
755# 'internal' : Internal
756# 'public' : Public (generic name or external documentation)
757# Any of these values except 'public' may be prefixed with 'missing_'
758# to indicate that they are known to be missing.
759my %state;
760# %missing is affected by loading util/missing*.txt. Values may be one of:
761# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
762# 'ssl' : belongs in libssl (loaded from libssl.num)
763# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
764# 'internal' : Internal
765my %missing;
766
a397aca4 767# Parse libcrypto.num, etc., and return sorted list of what's there.
8270c479 768sub loadnum ($;$) {
71a8b855 769 my $file = shift;
8270c479
RL
770 my $type = shift;
771 my @symbols;
71a8b855 772
1624ebdb 773 open my $IN, '<', catfile($config{sourcedir}, $file)
71a8b855
RS
774 or die "Can't open $file, $!, stopped";
775
776 while ( <$IN> ) {
274d1bee 777 next if /^#/;
71a8b855 778 next if /\bNOEXIST\b/;
1722496f 779 my @fields = split();
bc6ca4cb 780 die "Malformed line $. in $file: $_"
1722496f 781 if scalar @fields != 2 && scalar @fields != 4;
8270c479 782 $state{$fields[0].'(3)'} = $type // 'internal';
71a8b855 783 }
71a8b855 784 close $IN;
71a8b855
RS
785}
786
a397aca4 787# Load file of symbol names that we know aren't documented.
8270c479 788sub loadmissing($;$)
b5283535
MC
789{
790 my $missingfile = shift;
8270c479 791 my $type = shift;
b5283535 792
1624ebdb 793 open FH, catfile($config{sourcedir}, $missingfile)
fadb57e5 794 or die "Can't open $missingfile";
b5283535
MC
795 while ( <FH> ) {
796 chomp;
797 next if /^#/;
8270c479 798 $missing{$_} = $type // 'internal';
b5283535
MC
799 }
800 close FH;
8270c479 801}
b5283535 802
8270c479
RL
803# Check that we have consistent public / internal documentation and declaration
804sub checkstate () {
805 # Collect all known names, no matter where they come from
806 my %names = map { $_ => 1 } (keys %name_map, keys %state, keys %missing);
807
808 # Check section 3, i.e. functions and macros
809 foreach ( grep { $_ =~ /\(3\)$/ } sort keys %names ) {
810 next if ( $name_map{$_} // '') eq '' || $_ =~ /$ignored/;
811
812 # If a man-page isn't recorded public or if it's recorded missing
813 # and internal, it's declared to be internal.
814 my $declared_internal =
815 ($state{$_} // 'internal') eq 'internal'
816 || ($missing{$_} // '') eq 'internal';
817 # If a man-page isn't recorded internal or if it's recorded missing
818 # and not internal, it's declared to be public
819 my $declared_public =
820 ($state{$_} // 'internal') ne 'internal'
821 || ($missing{$_} // 'internal') ne 'internal';
822
823 err("$_ is supposedly public but is documented as internal")
824 if ( $declared_public && $name_map{$_} =~ /\/internal\// );
825 err("$_ is supposedly internal but is documented as public")
826 if ( $declared_internal && $name_map{$_} !~ /\/internal\// );
17fa385d 827 }
b5283535
MC
828}
829
a397aca4
RS
830# Check for undocumented macros; ignore those in the "missing" file
831# and do simple check for #define in our header files.
fbad6e79 832sub checkmacros {
9a2dfc0f 833 my $count = 0;
ee4afacd 834 my %seen;
b5283535 835
1624ebdb 836 foreach my $f ( files(TAGS => 'public_header') ) {
9a2dfc0f 837 # Skip some internals we don't want to document yet.
1624ebdb
RL
838 my $b = basename($f);
839 next if $b eq 'asn1.h';
840 next if $b eq 'asn1t.h';
841 next if $b eq 'err.h';
fadb57e5
RS
842 open(IN, $f)
843 or die "Can't open $f, $!";
9a2dfc0f
RS
844 while ( <IN> ) {
845 next unless /^#\s*define\s*(\S+)\(/;
b4350db5 846 my $macro = "$1(3)"; # We know they're all in section 3
8270c479
RL
847 next if defined $name_map{$macro}
848 || defined $missing{$macro}
849 || defined $seen{$macro}
850 || $macro =~ /$ignored/;
14ee781e 851
185ec4be 852 err("$f:", "macro $macro undocumented")
fbad6e79 853 if $opt_d || $opt_e;
9a2dfc0f 854 $count++;
ee4afacd 855 $seen{$macro} = 1;
9a2dfc0f
RS
856 }
857 close(IN);
858 }
185ec4be
RS
859 err("# $count macros undocumented (count is approximate)")
860 if $count > 0;
9a2dfc0f
RS
861}
862
a397aca4
RS
863# Find out what is undocumented (filtering out the known missing ones)
864# and display them.
8270c479
RL
865sub printem ($) {
866 my $type = shift;
71a8b855 867 my $count = 0;
b5283535 868
c4de5d22
RL
869 foreach my $func ( grep { $state{$_} eq $type } sort keys %state ) {
870 next if defined $name_map{$func}
871 || defined $missing{$func};
8270c479
RL
872
873 err("$type:", "function $func undocumented")
fbad6e79 874 if $opt_d || $opt_e;
71a8b855
RS
875 $count++;
876 }
8270c479 877 err("# $count lib$type names are not documented")
185ec4be 878 if $count > 0;
71a8b855
RS
879}
880
a397aca4 881# Collect all the names in a manpage.
9e183d22 882sub collectnames {
8270c479
RL
883 my %podinfo = @_;
884 my $filename = $podinfo{filename};
9e183d22
RS
885 $filename =~ m|man(\d)/|;
886 my $section = $1;
a397aca4 887 my $simplename = basename($filename, ".pod");
9e183d22 888 my $id = "${filename}:1:";
8270c479 889 my $is_generic = $podinfo{contents} =~ /^=for openssl generic/ms;
9e183d22 890
b4350db5 891 unless ( grep { $simplename eq $_ } @{$podinfo{names}} ) {
d2b194d7 892 err($id, "$simplename not in NAME section");
b4350db5 893 push @{$podinfo{names}}, $simplename;
9e183d22 894 }
fadb57e5 895 foreach my $name ( @{$podinfo{names}} ) {
9e183d22 896 next if $name eq "";
6f72b210 897 err($id, "'$name' contains whitespace")
d2b194d7 898 if $name =~ /\s/;
9e183d22 899 my $name_sec = "$name($section)";
8270c479 900 if ( !defined $name_map{$name_sec} ) {
a397aca4 901 $name_map{$name_sec} = $filename;
c4de5d22 902 $state{$name_sec} //=
8270c479
RL
903 ( $filename =~ /\/internal\// ? 'internal' : 'public' )
904 if $is_generic;
a397aca4 905 } elsif ( $filename eq $name_map{$name_sec} ) {
b4350db5 906 err($id, "$name_sec duplicated in NAME section of",
a397aca4 907 $name_map{$name_sec});
8270c479 908 } elsif ( $name_map{$name_sec} ne '' ) {
fbad6e79 909 err($id, "$name_sec also in NAME section of",
a397aca4 910 $name_map{$name_sec});
9e183d22
RS
911 }
912 }
913
fadb57e5
RS
914 if ( $podinfo{contents} =~ /=for openssl foreign manual (.*)\n/ ) {
915 foreach my $f ( split / /, $1 ) {
8270c479
RL
916 $name_map{$f} = ''; # It still exists!
917 $state{$f} = 'public'; # We assume!
fadb57e5 918 }
9e183d22
RS
919 }
920
b4350db5
RL
921 my @links =
922 $podinfo{contents} =~ /L<
9e183d22
RS
923 # if the link is of the form L<something|name(s)>,
924 # then remove 'something'. Note that 'something'
925 # may contain POD codes as well...
926 (?:(?:[^\|]|<[^>]*>)*\|)?
46f4e1be 927 # we're only interested in references that have
9e183d22
RS
928 # a one digit section number
929 ([^\/>\(]+\(\d\))
930 /gx;
a397aca4 931 $link_map{$filename} = [ @links ];
9e183d22
RS
932}
933
a397aca4 934# Look for L<> ("link") references that point to files that do not exist.
9e183d22 935sub checklinks {
fadb57e5
RS
936 foreach my $filename ( sort keys %link_map ) {
937 foreach my $link ( @{$link_map{$filename}} ) {
fbad6e79 938 err("${filename}:1:", "reference to non-existing $link")
8270c479
RL
939 unless defined $name_map{$link} || defined $missing{$link};
940 err("${filename}:1:", "reference of internal $link in public documentation $filename")
941 if ( ( ($state{$link} // '') eq 'internal'
942 || ($missing{$link} // '') eq 'internal' )
943 && $filename !~ /\/internal\// );
9e183d22
RS
944 }
945 }
946}
947
a397aca4
RS
948# Cipher/digests to skip if they show up as "not implemented"
949# because they are, via the "-*" construct.
e75138ab
RS
950my %skips = (
951 'aes128' => 1,
952 'aes192' => 1,
953 'aes256' => 1,
954 'aria128' => 1,
955 'aria192' => 1,
956 'aria256' => 1,
957 'camellia128' => 1,
958 'camellia192' => 1,
959 'camellia256' => 1,
960 'des' => 1,
961 'des3' => 1,
962 'idea' => 1,
1738c0ce
RS
963 'cipher' => 1,
964 'digest' => 1,
e75138ab
RS
965);
966
a397aca4 967# Check the flags of a command and see if everything is in the manpage
fbad6e79 968sub checkflags {
e75138ab 969 my $cmd = shift;
bc5a8091 970 my $doc = shift;
e75138ab
RS
971 my %cmdopts;
972 my %docopts;
1738c0ce 973 my %localskips;
e75138ab
RS
974
975 # Get the list of options in the command.
912f8a98 976 open CFH, "$openssl list --options $cmd|"
fadb57e5 977 or die "Can list options for $cmd, $!";
e75138ab
RS
978 while ( <CFH> ) {
979 chop;
980 s/ .$//;
981 $cmdopts{$_} = 1;
982 }
983 close CFH;
984
985 # Get the list of flags from the synopsis
bc5a8091 986 open CFH, "<$doc"
fadb57e5 987 or die "Can't open $doc, $!";
e75138ab
RS
988 while ( <CFH> ) {
989 chop;
990 last if /DESCRIPTION/;
9f3c076b 991 if ( /=for openssl ifdef (.*)/ ) {
1738c0ce
RS
992 foreach my $f ( split / /, $1 ) {
993 $localskips{$f} = 1;
994 }
995 next;
996 }
65718c51
RS
997 my $opt;
998 if ( /\[B<-([^ >]+)/ ) {
999 $opt = $1;
1000 } elsif ( /^B<-([^ >]+)/ ) {
1001 $opt = $1;
1002 } else {
1003 next;
1004 }
1738c0ce 1005 $opt = $1 if $opt =~ /I<(.*)/;
e75138ab
RS
1006 $docopts{$1} = 1;
1007 }
1008 close CFH;
1009
1010 # See what's in the command not the manpage.
a397aca4
RS
1011 my @undocced = sort grep { !defined $docopts{$_} } keys %cmdopts;
1012 foreach ( @undocced ) {
1013 next if /-/; # Skip the -- end-of-flags marker
1014 err("$doc: undocumented option -$_");
e75138ab
RS
1015 }
1016
1017 # See what's in the command not the manpage.
a397aca4
RS
1018 my @unimpl = sort grep { !defined $cmdopts{$_} } keys %docopts;
1019 foreach ( @unimpl ) {
1020 next if defined $skips{$_} || defined $localskips{$_};
65718c51 1021 err("$doc: $cmd does not implement -$_");
e75138ab 1022 }
e75138ab
RS
1023}
1024
a397aca4
RS
1025##
1026## MAIN()
1027## Do the work requested by the various getopt flags.
1028## The flags are parsed in alphabetical order, just because we have
1029## to have *some way* of listing them.
1030##
1031
e75138ab 1032if ( $opt_c ) {
e75138ab 1033 my @commands = ();
3dfda1a6 1034
e75138ab 1035 # Get list of commands.
912f8a98 1036 open FH, "$openssl list -1 -commands|"
fadb57e5 1037 or die "Can't list commands, $!";
e75138ab
RS
1038 while ( <FH> ) {
1039 chop;
1040 push @commands, $_;
1041 }
1042 close FH;
1043
1044 # See if each has a manpage.
bc5a8091
RS
1045 foreach my $cmd ( @commands ) {
1046 next if $cmd eq 'help' || $cmd eq 'exit';
1624ebdb
RL
1047 my @doc = ( grep { basename($_) eq "openssl-$cmd.pod"
1048 # For "tsget" and "CA.pl" pod pages
1049 || basename($_) eq "$cmd.pod" }
1050 files(TAGS => [ 'manual', 'man1' ]) );
1051 my $num = scalar @doc;
1052 if ($num > 1) {
1053 err("$num manuals for 'openssl $cmd': ".join(", ", @doc));
1054 } elsif ($num < 1) {
1055 err("no manual for 'openssl $cmd'");
e75138ab 1056 } else {
1624ebdb 1057 checkflags($cmd, @doc);
e75138ab 1058 }
71a8b855 1059 }
e75138ab
RS
1060
1061 # See what help is missing.
912f8a98 1062 open FH, "$openssl list --missing-help |"
fadb57e5 1063 or die "Can't list missing help, $!";
e75138ab
RS
1064 while ( <FH> ) {
1065 chop;
1066 my ($cmd, $flag) = split;
fbad6e79 1067 err("$cmd has no help for -$flag");
e75138ab
RS
1068 }
1069 close FH;
1070
fbad6e79 1071 exit $status;
71a8b855 1072}
9e183d22 1073
8270c479
RL
1074# Populate %state
1075loadnum('util/libcrypto.num', 'crypto');
1076loadnum('util/libssl.num', 'ssl');
1077loadnum('util/other.syms', 'other');
1078loadnum('util/other-internal.syms');
1079if ( $opt_o ) {
1080 loadmissing('util/missingmacro111.txt', 'crypto');
1081 loadmissing('util/missingcrypto111.txt', 'crypto');
1082 loadmissing('util/missingssl111.txt', 'ssl');
e3ce33b3 1083} elsif ( !$opt_u ) {
8270c479
RL
1084 loadmissing('util/missingmacro.txt', 'crypto');
1085 loadmissing('util/missingcrypto.txt', 'crypto');
1086 loadmissing('util/missingssl.txt', 'ssl');
1087 loadmissing('util/missingcrypto-internal.txt');
1088 loadmissing('util/missingssl-internal.txt');
1089}
1090
1091if ( $opt_n || $opt_l || $opt_u || $opt_v ) {
1092 my @files_to_read = ( $opt_n && @ARGV ) ? @ARGV : files(TAGS => 'manual');
1093
1094 foreach (@files_to_read) {
1095 my %podinfo = extract_pod_info($_, { debug => $debug });
1096
1097 collectnames(%podinfo)
1098 if ( $opt_l || $opt_u || $opt_v );
1099
1100 check(%podinfo)
1101 if ( $opt_n );
9e183d22 1102 }
b4350db5
RL
1103}
1104
1105if ( $opt_l ) {
9e183d22
RS
1106 checklinks();
1107}
1108
e75138ab 1109if ( $opt_n ) {
a6dd3a3a
RS
1110 # If not given args, check that all man1 commands are named properly.
1111 if ( scalar @ARGV == 0 ) {
1624ebdb 1112 foreach ( files(TAGS => [ 'public_manual', 'man1' ]) ) {
6f02932e 1113 next if /CA.pl/ || /openssl\.pod/ || /tsget\.pod/;
a6dd3a3a
RS
1114 err("$_ doesn't start with openssl-") unless /openssl-/;
1115 }
1116 }
e75138ab
RS
1117}
1118
8270c479
RL
1119checkstate();
1120
b5283535 1121if ( $opt_u || $opt_v) {
8270c479
RL
1122 printem('crypto');
1123 printem('ssl');
fbad6e79 1124 checkmacros();
1bc74519 1125}
05ea606a 1126
fbad6e79 1127exit $status;