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