]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/find-doc-nits
Fix typos and repeated words
[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 = (
8c1cbc72 554 'a.k.a.' => 'aka',
60a7817c
RS
555 'bitmask' => 'bit mask',
556 'builtin' => 'built-in',
557 #'epoch' => 'Epoch', # handled specially, below
558 'file name' => 'filename',
559 'file system' => 'filesystem',
560 'host name' => 'hostname',
561 'i-node' => 'inode',
562 'lower case' => 'lowercase',
563 'lower-case' => 'lowercase',
564 'non-zero' => 'nonzero',
565 'path name' => 'pathname',
566 'pseudo-terminal' => 'pseudoterminal',
567 'reserved port' => 'privileged port',
568 'system port' => 'privileged port',
569 'realtime' => 'real-time',
570 'real time' => 'real-time',
571 'runtime' => 'run time',
572 'saved group ID'=> 'saved set-group-ID',
573 'saved set-GID' => 'saved set-group-ID',
574 'saved user ID' => 'saved set-user-ID',
575 'saved set-UID' => 'saved set-user-ID',
576 'set-GID' => 'set-group-ID',
577 'setgid' => 'set-group-ID',
578 'set-UID' => 'set-user-ID',
579 'setuid' => 'set-user-ID',
580 'super user' => 'superuser',
581 'super-user' => 'superuser',
582 'super block' => 'superblock',
583 'super-block' => 'superblock',
584 'time stamp' => 'timestamp',
585 'time zone' => 'timezone',
586 'upper case' => 'uppercase',
587 'upper-case' => 'uppercase',
588 'useable' => 'usable',
589 'userspace' => 'user space',
590 'user name' => 'username',
591 'zeroes' => 'zeros'
592);
593
a397aca4 594# Search manpage for words that have a different preferred use.
60a7817c
RS
595sub wording {
596 my $id = shift;
597 my $contents = shift;
598
599 foreach my $k ( keys %preferred_words ) {
9c0586d5
RS
600 # Sigh, trademark
601 next if $k eq 'file system'
602 and $contents =~ /Microsoft Encrypted File System/;
ad090d57 603 err($id, "Found '$k' should use '$preferred_words{$k}'")
60a7817c
RS
604 if $contents =~ /\b\Q$k\E\b/i;
605 }
ad090d57 606 err($id, "Found 'epoch' should use 'Epoch'")
60a7817c 607 if $contents =~ /\bepoch\b/;
4b537191
RS
608 if ( $id =~ m@man1/@ ) {
609 err($id, "found 'tool' in NAME, should use 'command'")
610 if $contents =~ /=head1 NAME.*\btool\b.*=head1 SYNOPSIS/s;
611 err($id, "found 'utility' in NAME, should use 'command'")
612 if $contents =~ /NAME.*\butility\b.*=head1 SYNOPSIS/s;
613
614 }
60a7817c
RS
615}
616
a397aca4 617# Perform all sorts of nit/error checks on a manpage
fbad6e79 618sub check {
8270c479
RL
619 my %podinfo = @_;
620 my $filename = $podinfo{filename};
169a8e39 621 my $dirname = basename(dirname($filename));
8270c479 622 my $contents = $podinfo{contents};
843666ff
RS
623
624 my $id = "${filename}:1:";
fbad6e79 625 check_head_style($id, $contents);
35ea640a 626
39a117d1
RS
627 # Check ordering of some sections in man3
628 if ( $filename =~ m|man3/| ) {
fbad6e79
RS
629 check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES");
630 check_section_location($id, $contents, "SEE ALSO", "HISTORY");
631 check_section_location($id, $contents, "EXAMPLES", "SEE ALSO");
39a117d1
RS
632 }
633
6e4618a0
RS
634 # Make sure every link has a section.
635 while ( $contents =~ /$markup_re/msg ) {
636 my $target = $1;
76fde1db
RL
637 next unless $target =~ /^L<(.*)>$/; # Skip if not L<...>
638 $target = $1; # Peal away L< and >
639 $target =~ s/\/[^\/]*$//; # Peal away possible anchor
640 $target =~ s/.*\|//g; # Peal away possible link text
641 next if $target eq ''; # Skip if links within page, or
6e4618a0 642 next if $target =~ /::/; # links to a Perl module, or
76fde1db
RL
643 next if $target =~ /^https?:/; # is a URL link, or
644 next if $target =~ /\([1357]\)$/; # it has a section
6e4618a0
RS
645 err($id, "Section missing in $target")
646 }
1903a9b7
RS
647 # Check for proper links to commands.
648 while ( $contents =~ /L<([^>]*)\(1\)(?:\/.*)?>/g ) {
649 my $target = $1;
650 next if $target =~ /openssl-?/;
1624ebdb
RL
651 next if ( grep { basename($_) eq "$target.pod" }
652 files(TAGS => [ 'manual', 'man1' ]) );
1903a9b7
RS
653 # TODO: Filter out "foreign manual" links.
654 next if $target =~ /ps|apropos|sha1sum|procmail|perl/;
655 err($id, "Bad command link L<$target(1)>");
656 }
6e4618a0
RS
657 # Check for proper in-man-3 API links.
658 while ( $contents =~ /L<([^>]*)\(3\)(?:\/.*)?>/g ) {
659 my $target = $1;
660 err($id, "Bad L<$target>")
661 unless $target =~ /^[_[:alpha:]][_[:alnum:]]*$/
662 }
663
8270c479 664 unless ( $contents =~ /^=for openssl generic/ms ) {
705128b0
RL
665 if ( $filename =~ m|man3/| ) {
666 name_synopsis($id, $filename, $contents);
667 functionname_check($id, $filename, $contents);
668 } elsif ( $filename =~ m|man1/| ) {
669 option_check($id, $filename, $contents)
670 }
671 }
35ea640a 672
60a7817c
RS
673 wording($id, $contents);
674
ad090d57 675 err($id, "Doesn't start with =pod")
05ea606a 676 if $contents !~ /^=pod/;
ad090d57 677 err($id, "Doesn't end with =cut")
05ea606a 678 if $contents !~ /=cut\n$/;
ad090d57 679 err($id, "More than one cut line.")
05ea606a 680 if $contents =~ /=cut.*=cut/ms;
fbad6e79 681 err($id, "EXAMPLE not EXAMPLES section.")
cda77422 682 if $contents =~ /=head1 EXAMPLE[^S]/;
fbad6e79 683 err($id, "WARNING not WARNINGS section.")
5e0d9c86 684 if $contents =~ /=head1 WARNING[^S]/;
ad090d57 685 err($id, "Missing copyright")
05ea606a 686 if $contents !~ /Copyright .* The OpenSSL Project Authors/;
ad090d57 687 err($id, "Copyright not last")
05ea606a 688 if $contents =~ /head1 COPYRIGHT.*=head/ms;
fbad6e79 689 err($id, "head2 in All uppercase")
843666ff 690 if $contents =~ /head2\s+[A-Z ]+\n/;
ad090d57 691 err($id, "Extra space after head")
35ea640a 692 if $contents =~ /=head\d\s\s+/;
ad090d57 693 err($id, "Period in NAME section")
35ea640a 694 if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms;
fbad6e79 695 err($id, "Duplicate $1 in L<>")
5a3371e2 696 if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2;
fbad6e79 697 err($id, "Bad =over $1")
2f61bc2e 698 if $contents =~ /=over([^ ][^24])/;
fbad6e79 699 err($id, "Possible version style issue")
e90fc053 700 if $contents =~ /OpenSSL version [019]/;
843666ff 701
bb82531f 702 if ( $contents !~ /=for openssl multiple includes/ ) {
a95d7574
RS
703 # Look for multiple consecutive openssl #include lines
704 # (non-consecutive lines are okay; see man3/MD5.pod).
843666ff
RS
705 if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) {
706 my $count = 0;
707 foreach my $line ( split /\n+/, $1 ) {
708 if ( $line =~ m@include <openssl/@ ) {
ad090d57 709 err($id, "Has multiple includes")
fbad6e79 710 if ++$count == 2;
843666ff
RS
711 } else {
712 $count = 0;
713 }
714 }
715 }
716 }
05ea606a 717
35ea640a
RS
718 open my $OUT, '>', $temp
719 or die "Can't open $temp, $!";
169a8e39 720 podchecker($filename, $OUT);
35ea640a
RS
721 close $OUT;
722 open $OUT, '<', $temp
723 or die "Can't read $temp, $!";
724 while ( <$OUT> ) {
725 next if /\(section\) in.*deprecated/;
726 print;
727 }
728 close $OUT;
729 unlink $temp || warn "Can't remove $temp, $!";
a95d7574
RS
730
731 # Find what section this page is in; assume 3.
732 my $section = 3;
733 $section = $1 if $dirname =~ /man([1-9])/;
734
a397aca4 735 foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) {
ad090d57 736 err($id, "Missing $_ head1 section")
a95d7574
RS
737 if $contents !~ /^=head1\s+${_}\s*$/m;
738 }
05ea606a 739}
1bc74519 740
8270c479
RL
741# Information database ###############################################
742
743# Map of links in each POD file; filename => [ "foo(1)", "bar(3)", ... ]
744my %link_map = ();
745# Map of names in each POD file or from "missing" files; possible values are:
746# If found in a POD files, "name(s)" => filename
747# If found in a "missing" file or external, "name(s)" => ''
748my %name_map = ();
749
750# State of man-page names.
751# %state is affected by loading util/*.num and util/*.syms
752# Values may be one of:
753# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
754# 'ssl' : belongs in libssl (loaded from libssl.num)
755# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
756# 'internal' : Internal
757# 'public' : Public (generic name or external documentation)
758# Any of these values except 'public' may be prefixed with 'missing_'
759# to indicate that they are known to be missing.
760my %state;
761# %missing is affected by loading util/missing*.txt. Values may be one of:
762# 'crypto' : belongs in libcrypto (loaded from libcrypto.num)
763# 'ssl' : belongs in libssl (loaded from libssl.num)
764# 'other' : belongs in libcrypto or libssl (loaded from other.syms)
765# 'internal' : Internal
766my %missing;
767
a397aca4 768# Parse libcrypto.num, etc., and return sorted list of what's there.
8270c479 769sub loadnum ($;$) {
71a8b855 770 my $file = shift;
8270c479
RL
771 my $type = shift;
772 my @symbols;
71a8b855 773
1624ebdb 774 open my $IN, '<', catfile($config{sourcedir}, $file)
71a8b855
RS
775 or die "Can't open $file, $!, stopped";
776
777 while ( <$IN> ) {
274d1bee 778 next if /^#/;
71a8b855 779 next if /\bNOEXIST\b/;
1722496f 780 my @fields = split();
bc6ca4cb 781 die "Malformed line $. in $file: $_"
1722496f 782 if scalar @fields != 2 && scalar @fields != 4;
8270c479 783 $state{$fields[0].'(3)'} = $type // 'internal';
71a8b855 784 }
71a8b855 785 close $IN;
71a8b855
RS
786}
787
a397aca4 788# Load file of symbol names that we know aren't documented.
8270c479 789sub loadmissing($;$)
b5283535
MC
790{
791 my $missingfile = shift;
8270c479 792 my $type = shift;
b5283535 793
1624ebdb 794 open FH, catfile($config{sourcedir}, $missingfile)
fadb57e5 795 or die "Can't open $missingfile";
b5283535
MC
796 while ( <FH> ) {
797 chomp;
798 next if /^#/;
8270c479 799 $missing{$_} = $type // 'internal';
b5283535
MC
800 }
801 close FH;
8270c479 802}
b5283535 803
8270c479
RL
804# Check that we have consistent public / internal documentation and declaration
805sub checkstate () {
806 # Collect all known names, no matter where they come from
807 my %names = map { $_ => 1 } (keys %name_map, keys %state, keys %missing);
808
809 # Check section 3, i.e. functions and macros
810 foreach ( grep { $_ =~ /\(3\)$/ } sort keys %names ) {
811 next if ( $name_map{$_} // '') eq '' || $_ =~ /$ignored/;
812
813 # If a man-page isn't recorded public or if it's recorded missing
814 # and internal, it's declared to be internal.
815 my $declared_internal =
816 ($state{$_} // 'internal') eq 'internal'
817 || ($missing{$_} // '') eq 'internal';
818 # If a man-page isn't recorded internal or if it's recorded missing
819 # and not internal, it's declared to be public
820 my $declared_public =
821 ($state{$_} // 'internal') ne 'internal'
822 || ($missing{$_} // 'internal') ne 'internal';
823
824 err("$_ is supposedly public but is documented as internal")
825 if ( $declared_public && $name_map{$_} =~ /\/internal\// );
826 err("$_ is supposedly internal but is documented as public")
827 if ( $declared_internal && $name_map{$_} !~ /\/internal\// );
17fa385d 828 }
b5283535
MC
829}
830
a397aca4
RS
831# Check for undocumented macros; ignore those in the "missing" file
832# and do simple check for #define in our header files.
fbad6e79 833sub checkmacros {
9a2dfc0f 834 my $count = 0;
ee4afacd 835 my %seen;
b5283535 836
1624ebdb 837 foreach my $f ( files(TAGS => 'public_header') ) {
9a2dfc0f 838 # Skip some internals we don't want to document yet.
1624ebdb
RL
839 my $b = basename($f);
840 next if $b eq 'asn1.h';
841 next if $b eq 'asn1t.h';
842 next if $b eq 'err.h';
fadb57e5
RS
843 open(IN, $f)
844 or die "Can't open $f, $!";
9a2dfc0f
RS
845 while ( <IN> ) {
846 next unless /^#\s*define\s*(\S+)\(/;
b4350db5 847 my $macro = "$1(3)"; # We know they're all in section 3
8270c479
RL
848 next if defined $name_map{$macro}
849 || defined $missing{$macro}
850 || defined $seen{$macro}
851 || $macro =~ /$ignored/;
14ee781e 852
185ec4be 853 err("$f:", "macro $macro undocumented")
fbad6e79 854 if $opt_d || $opt_e;
9a2dfc0f 855 $count++;
ee4afacd 856 $seen{$macro} = 1;
9a2dfc0f
RS
857 }
858 close(IN);
859 }
185ec4be
RS
860 err("# $count macros undocumented (count is approximate)")
861 if $count > 0;
9a2dfc0f
RS
862}
863
a397aca4
RS
864# Find out what is undocumented (filtering out the known missing ones)
865# and display them.
8270c479
RL
866sub printem ($) {
867 my $type = shift;
71a8b855 868 my $count = 0;
b5283535 869
c4de5d22
RL
870 foreach my $func ( grep { $state{$_} eq $type } sort keys %state ) {
871 next if defined $name_map{$func}
872 || defined $missing{$func};
8270c479
RL
873
874 err("$type:", "function $func undocumented")
fbad6e79 875 if $opt_d || $opt_e;
71a8b855
RS
876 $count++;
877 }
8270c479 878 err("# $count lib$type names are not documented")
185ec4be 879 if $count > 0;
71a8b855
RS
880}
881
a397aca4 882# Collect all the names in a manpage.
9e183d22 883sub collectnames {
8270c479
RL
884 my %podinfo = @_;
885 my $filename = $podinfo{filename};
9e183d22
RS
886 $filename =~ m|man(\d)/|;
887 my $section = $1;
a397aca4 888 my $simplename = basename($filename, ".pod");
9e183d22 889 my $id = "${filename}:1:";
8270c479 890 my $is_generic = $podinfo{contents} =~ /^=for openssl generic/ms;
9e183d22 891
b4350db5 892 unless ( grep { $simplename eq $_ } @{$podinfo{names}} ) {
d2b194d7 893 err($id, "$simplename not in NAME section");
b4350db5 894 push @{$podinfo{names}}, $simplename;
9e183d22 895 }
fadb57e5 896 foreach my $name ( @{$podinfo{names}} ) {
9e183d22 897 next if $name eq "";
6f72b210 898 err($id, "'$name' contains whitespace")
d2b194d7 899 if $name =~ /\s/;
9e183d22 900 my $name_sec = "$name($section)";
8270c479 901 if ( !defined $name_map{$name_sec} ) {
a397aca4 902 $name_map{$name_sec} = $filename;
c4de5d22 903 $state{$name_sec} //=
8270c479
RL
904 ( $filename =~ /\/internal\// ? 'internal' : 'public' )
905 if $is_generic;
a397aca4 906 } elsif ( $filename eq $name_map{$name_sec} ) {
b4350db5 907 err($id, "$name_sec duplicated in NAME section of",
a397aca4 908 $name_map{$name_sec});
8270c479 909 } elsif ( $name_map{$name_sec} ne '' ) {
fbad6e79 910 err($id, "$name_sec also in NAME section of",
a397aca4 911 $name_map{$name_sec});
9e183d22
RS
912 }
913 }
914
fadb57e5
RS
915 if ( $podinfo{contents} =~ /=for openssl foreign manual (.*)\n/ ) {
916 foreach my $f ( split / /, $1 ) {
8270c479
RL
917 $name_map{$f} = ''; # It still exists!
918 $state{$f} = 'public'; # We assume!
fadb57e5 919 }
9e183d22
RS
920 }
921
b4350db5
RL
922 my @links =
923 $podinfo{contents} =~ /L<
9e183d22
RS
924 # if the link is of the form L<something|name(s)>,
925 # then remove 'something'. Note that 'something'
926 # may contain POD codes as well...
927 (?:(?:[^\|]|<[^>]*>)*\|)?
46f4e1be 928 # we're only interested in references that have
9e183d22
RS
929 # a one digit section number
930 ([^\/>\(]+\(\d\))
931 /gx;
a397aca4 932 $link_map{$filename} = [ @links ];
9e183d22
RS
933}
934
a397aca4 935# Look for L<> ("link") references that point to files that do not exist.
9e183d22 936sub checklinks {
fadb57e5
RS
937 foreach my $filename ( sort keys %link_map ) {
938 foreach my $link ( @{$link_map{$filename}} ) {
fbad6e79 939 err("${filename}:1:", "reference to non-existing $link")
8270c479
RL
940 unless defined $name_map{$link} || defined $missing{$link};
941 err("${filename}:1:", "reference of internal $link in public documentation $filename")
942 if ( ( ($state{$link} // '') eq 'internal'
943 || ($missing{$link} // '') eq 'internal' )
944 && $filename !~ /\/internal\// );
9e183d22
RS
945 }
946 }
947}
948
a397aca4
RS
949# Cipher/digests to skip if they show up as "not implemented"
950# because they are, via the "-*" construct.
e75138ab
RS
951my %skips = (
952 'aes128' => 1,
953 'aes192' => 1,
954 'aes256' => 1,
955 'aria128' => 1,
956 'aria192' => 1,
957 'aria256' => 1,
958 'camellia128' => 1,
959 'camellia192' => 1,
960 'camellia256' => 1,
961 'des' => 1,
962 'des3' => 1,
963 'idea' => 1,
1738c0ce
RS
964 'cipher' => 1,
965 'digest' => 1,
e75138ab
RS
966);
967
a397aca4 968# Check the flags of a command and see if everything is in the manpage
fbad6e79 969sub checkflags {
e75138ab 970 my $cmd = shift;
bc5a8091 971 my $doc = shift;
e75138ab
RS
972 my %cmdopts;
973 my %docopts;
1738c0ce 974 my %localskips;
e75138ab
RS
975
976 # Get the list of options in the command.
912f8a98 977 open CFH, "$openssl list --options $cmd|"
fadb57e5 978 or die "Can list options for $cmd, $!";
e75138ab
RS
979 while ( <CFH> ) {
980 chop;
981 s/ .$//;
982 $cmdopts{$_} = 1;
983 }
984 close CFH;
985
986 # Get the list of flags from the synopsis
bc5a8091 987 open CFH, "<$doc"
fadb57e5 988 or die "Can't open $doc, $!";
e75138ab
RS
989 while ( <CFH> ) {
990 chop;
991 last if /DESCRIPTION/;
9f3c076b 992 if ( /=for openssl ifdef (.*)/ ) {
1738c0ce
RS
993 foreach my $f ( split / /, $1 ) {
994 $localskips{$f} = 1;
995 }
996 next;
997 }
65718c51
RS
998 my $opt;
999 if ( /\[B<-([^ >]+)/ ) {
1000 $opt = $1;
1001 } elsif ( /^B<-([^ >]+)/ ) {
1002 $opt = $1;
1003 } else {
1004 next;
1005 }
1738c0ce 1006 $opt = $1 if $opt =~ /I<(.*)/;
e75138ab
RS
1007 $docopts{$1} = 1;
1008 }
1009 close CFH;
1010
1011 # See what's in the command not the manpage.
a397aca4
RS
1012 my @undocced = sort grep { !defined $docopts{$_} } keys %cmdopts;
1013 foreach ( @undocced ) {
1014 next if /-/; # Skip the -- end-of-flags marker
1015 err("$doc: undocumented option -$_");
e75138ab
RS
1016 }
1017
1018 # See what's in the command not the manpage.
a397aca4
RS
1019 my @unimpl = sort grep { !defined $cmdopts{$_} } keys %docopts;
1020 foreach ( @unimpl ) {
1021 next if defined $skips{$_} || defined $localskips{$_};
65718c51 1022 err("$doc: $cmd does not implement -$_");
e75138ab 1023 }
e75138ab
RS
1024}
1025
a397aca4
RS
1026##
1027## MAIN()
1028## Do the work requested by the various getopt flags.
1029## The flags are parsed in alphabetical order, just because we have
1030## to have *some way* of listing them.
1031##
1032
e75138ab 1033if ( $opt_c ) {
e75138ab 1034 my @commands = ();
3dfda1a6 1035
e75138ab 1036 # Get list of commands.
912f8a98 1037 open FH, "$openssl list -1 -commands|"
fadb57e5 1038 or die "Can't list commands, $!";
e75138ab
RS
1039 while ( <FH> ) {
1040 chop;
1041 push @commands, $_;
1042 }
1043 close FH;
1044
1045 # See if each has a manpage.
bc5a8091
RS
1046 foreach my $cmd ( @commands ) {
1047 next if $cmd eq 'help' || $cmd eq 'exit';
1624ebdb
RL
1048 my @doc = ( grep { basename($_) eq "openssl-$cmd.pod"
1049 # For "tsget" and "CA.pl" pod pages
1050 || basename($_) eq "$cmd.pod" }
1051 files(TAGS => [ 'manual', 'man1' ]) );
1052 my $num = scalar @doc;
1053 if ($num > 1) {
1054 err("$num manuals for 'openssl $cmd': ".join(", ", @doc));
1055 } elsif ($num < 1) {
1056 err("no manual for 'openssl $cmd'");
e75138ab 1057 } else {
1624ebdb 1058 checkflags($cmd, @doc);
e75138ab 1059 }
71a8b855 1060 }
e75138ab
RS
1061
1062 # See what help is missing.
912f8a98 1063 open FH, "$openssl list --missing-help |"
fadb57e5 1064 or die "Can't list missing help, $!";
e75138ab
RS
1065 while ( <FH> ) {
1066 chop;
1067 my ($cmd, $flag) = split;
fbad6e79 1068 err("$cmd has no help for -$flag");
e75138ab
RS
1069 }
1070 close FH;
1071
fbad6e79 1072 exit $status;
71a8b855 1073}
9e183d22 1074
8270c479
RL
1075# Populate %state
1076loadnum('util/libcrypto.num', 'crypto');
1077loadnum('util/libssl.num', 'ssl');
1078loadnum('util/other.syms', 'other');
1079loadnum('util/other-internal.syms');
1080if ( $opt_o ) {
1081 loadmissing('util/missingmacro111.txt', 'crypto');
1082 loadmissing('util/missingcrypto111.txt', 'crypto');
1083 loadmissing('util/missingssl111.txt', 'ssl');
e3ce33b3 1084} elsif ( !$opt_u ) {
8270c479
RL
1085 loadmissing('util/missingmacro.txt', 'crypto');
1086 loadmissing('util/missingcrypto.txt', 'crypto');
1087 loadmissing('util/missingssl.txt', 'ssl');
1088 loadmissing('util/missingcrypto-internal.txt');
1089 loadmissing('util/missingssl-internal.txt');
1090}
1091
1092if ( $opt_n || $opt_l || $opt_u || $opt_v ) {
1093 my @files_to_read = ( $opt_n && @ARGV ) ? @ARGV : files(TAGS => 'manual');
1094
1095 foreach (@files_to_read) {
1096 my %podinfo = extract_pod_info($_, { debug => $debug });
1097
1098 collectnames(%podinfo)
1099 if ( $opt_l || $opt_u || $opt_v );
1100
1101 check(%podinfo)
1102 if ( $opt_n );
9e183d22 1103 }
b4350db5
RL
1104}
1105
1106if ( $opt_l ) {
9e183d22
RS
1107 checklinks();
1108}
1109
e75138ab 1110if ( $opt_n ) {
a6dd3a3a
RS
1111 # If not given args, check that all man1 commands are named properly.
1112 if ( scalar @ARGV == 0 ) {
1624ebdb 1113 foreach ( files(TAGS => [ 'public_manual', 'man1' ]) ) {
6f02932e 1114 next if /CA.pl/ || /openssl\.pod/ || /tsget\.pod/;
a6dd3a3a
RS
1115 err("$_ doesn't start with openssl-") unless /openssl-/;
1116 }
1117 }
e75138ab
RS
1118}
1119
8270c479
RL
1120checkstate();
1121
b5283535 1122if ( $opt_u || $opt_v) {
8270c479
RL
1123 printem('crypto');
1124 printem('ssl');
fbad6e79 1125 checkmacros();
1bc74519 1126}
05ea606a 1127
fbad6e79 1128exit $status;