]>
Commit | Line | Data |
---|---|---|
1bc74519 | 1 | #! /usr/bin/env perl |
4333b89f | 2 | # Copyright 2002-2021 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 | |
10 | require 5.10.0; | |
11 | use warnings; | |
12 | use strict; | |
a397aca4 | 13 | |
1624ebdb | 14 | use Carp qw(:DEFAULT cluck); |
1bc74519 RS |
15 | use Pod::Checker; |
16 | use File::Find; | |
169a8e39 | 17 | use File::Basename; |
71a8b855 | 18 | use File::Spec::Functions; |
35ea640a | 19 | use Getopt::Std; |
1624ebdb RL |
20 | use FindBin; |
21 | use lib "$FindBin::Bin/perl"; | |
22 | ||
71a8b855 | 23 | use OpenSSL::Util::Pod; |
35ea640a | 24 | |
1624ebdb RL |
25 | use lib '.'; |
26 | use configdata; | |
27 | ||
a397aca4 RS |
28 | # Set to 1 for debug output |
29 | my $debug = 0; | |
705128b0 | 30 | |
912f8a98 | 31 | # Where to find openssl command |
1624ebdb | 32 | my $openssl = "./util/opensslwrap.sh"; |
912f8a98 | 33 | |
71a8b855 | 34 | # Options. |
8d50b9c1 | 35 | our($opt_d); |
b5283535 MC |
36 | our($opt_e); |
37 | our($opt_s); | |
a03749a8 | 38 | our($opt_o); |
71a8b855 | 39 | our($opt_h); |
9e183d22 | 40 | our($opt_l); |
8d50b9c1 | 41 | our($opt_n); |
274d1bee | 42 | our($opt_p); |
8d50b9c1 | 43 | our($opt_u); |
b5283535 | 44 | our($opt_v); |
e75138ab | 45 | our($opt_c); |
71a8b855 | 46 | |
185ec4be | 47 | # Print usage message and exit. |
fbad6e79 | 48 | sub help { |
71a8b855 RS |
49 | print <<EOF; |
50 | Find 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 |
60 | EOF |
61 | exit; | |
62 | } | |
1bc74519 | 63 | |
185ec4be RS |
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 | ||
05ea606a RS |
80 | my $temp = '/tmp/docnits.txt'; |
81 | my $OUT; | |
fbad6e79 | 82 | my $status = 0; |
05ea606a | 83 | |
1624ebdb | 84 | my @sections = ( 'man1', 'man3', 'man5', 'man7' ); |
a397aca4 RS |
85 | my %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. | |
3b1bfd21 | 94 | # They are reserved macros that we currently don't document |
8270c479 RL |
95 | my $ignored = qr/(?| ^i2d_ |
96 | | ^d2i_ | |
97 | | ^DEPRECATEDIN | |
3b1bfd21 | 98 | | ^OSSL_DEPRECATED |
8270c479 RL |
99 | | \Q_fnsig(3)\E$ |
100 | | ^IMPLEMENT_ | |
101 | | ^_?DECLARE_ | |
89b46350 MC |
102 | | ^sk_ |
103 | | ^SKM_DEFINE_STACK_OF_INTERNAL | |
282de1cc MC |
104 | | ^lh_ |
105 | | ^DEFINE_LHASH_OF_INTERNAL | |
8270c479 | 106 | )/x; |
a397aca4 | 107 | |
b1415dc1 RL |
108 | # A common regexp for C symbol names |
109 | my $C_symbol = qr/\b[[:alpha:]][_[:alnum:]]*\b/; | |
110 | ||
1624ebdb RL |
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 | } | |
169a8e39 | 269 | |
fbad6e79 RS |
270 | # Print error message, set $status. |
271 | sub err { | |
272 | print join(" ", @_), "\n"; | |
273 | $status = 1 | |
274 | } | |
275 | ||
35ea640a | 276 | # Cross-check functions in the NAME and SYNOPSIS section. |
fbad6e79 | 277 | sub name_synopsis { |
35ea640a RS |
278 | my $id = shift; |
279 | my $filename = shift; | |
280 | my $contents = shift; | |
281 | ||
35ea640a RS |
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/ /; | |
ad090d57 | 286 | err($id, "Trailing comma before - in NAME") |
fbad6e79 | 287 | if $tmp =~ /, *-/; |
2bcb232e | 288 | $tmp =~ s/ -.*//g; |
fbad6e79 RS |
289 | err($id, "POD markup among the names in NAME") |
290 | if $tmp =~ /[<>]/; | |
2bcb232e | 291 | $tmp =~ s/ */ /g; |
ad090d57 | 292 | err($id, "Missing comma in NAME") |
fbad6e79 | 293 | if $tmp =~ /[^,] /; |
fbba5d11 RS |
294 | |
295 | my $dirname = dirname($filename); | |
1624ebdb RL |
296 | my $section = basename($dirname); |
297 | my $simplename = basename($filename, ".pod"); | |
fbba5d11 RS |
298 | my $foundfilename = 0; |
299 | my %foundfilenames = (); | |
35ea640a | 300 | my %names; |
23ab880d RL |
301 | foreach my $n ( split ',', $tmp ) { |
302 | $n =~ s/^\s+//; | |
303 | $n =~ s/\s+$//; | |
ad090d57 | 304 | err($id, "The name '$n' contains white-space") |
23ab880d | 305 | if $n =~ /\s/; |
35ea640a | 306 | $names{$n} = 1; |
fbba5d11 RS |
307 | $foundfilename++ if $n eq $simplename; |
308 | $foundfilenames{$n} = 1 | |
1624ebdb RL |
309 | if ( ( grep { basename($_) eq "$n.pod" } |
310 | files(TAGS => [ 'manual', $section ]) ) | |
311 | && $n ne $simplename ); | |
35ea640a | 312 | } |
ad090d57 | 313 | err($id, "The following exist as other .pod files:", |
fbad6e79 | 314 | sort keys %foundfilenames) |
fbba5d11 | 315 | if %foundfilenames; |
fbad6e79 | 316 | err($id, "$simplename (filename) missing from NAME section") |
fbba5d11 | 317 | unless $foundfilename; |
35ea640a RS |
318 | |
319 | # Find all functions in SYNOPSIS | |
320 | return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms; | |
321 | my $syn = $1; | |
8eca4617 | 322 | my $ignore_until = undef; # If defined, this is a regexp |
d3cb5904 RL |
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 | ||
8eca4617 RL |
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 | ||
8162f6f5 | 352 | my $sym; |
31d3a759 | 353 | my $is_prototype = 1; |
c952780c | 354 | $line =~ s/STACK_OF\([^)]+\)/int/g; |
4460ad90 | 355 | $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g; |
c952780c | 356 | $line =~ s/__declspec\([^)]+\)//; |
93e32043 RL |
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 | ||
121677b4 RS |
371 | if ( $line =~ /env (\S*)=/ ) { |
372 | # environment variable env NAME=... | |
373 | $sym = $1; | |
b1415dc1 | 374 | } elsif ( $line =~ /typedef.*\(\*?($C_symbol)\)\s*\(/ ) { |
0ed78e78 | 375 | # a callback function pointer: typedef ... (*NAME)(... |
93e32043 | 376 | # a callback function signature: typedef ... (NAME)(... |
0ed78e78 | 377 | $sym = $1; |
b1415dc1 | 378 | } elsif ( $line =~ /typedef.*($C_symbol)\s*\(/ ) { |
0ed78e78 | 379 | # a callback function signature: typedef ... NAME(... |
121677b4 | 380 | $sym = $1; |
b1415dc1 | 381 | } elsif ( $line =~ /typedef.*($C_symbol);/ ) { |
121677b4 | 382 | # a simple typedef: typedef ... NAME; |
31d3a759 | 383 | $is_prototype = 0; |
8162f6f5 | 384 | $sym = $1; |
b1415dc1 | 385 | } elsif ( $line =~ /enum ($C_symbol) \{/ ) { |
d4ea9659 RS |
386 | # an enumeration: enum ... { |
387 | $sym = $1; | |
b1415dc1 | 388 | } elsif ( $line =~ /#\s*(?:define|undef) ($C_symbol)/ ) { |
31d3a759 | 389 | $is_prototype = 0; |
8162f6f5 | 390 | $sym = $1; |
b1415dc1 | 391 | } elsif ( $line =~ /^[^\(]*?\(\*($C_symbol)\s*\(/ ) { |
8eca4617 RL |
392 | # a function returning a function pointer: TYPE (*NAME(args))(args) |
393 | $sym = $1; | |
b1415dc1 | 394 | } elsif ( $line =~ /^[^\(]*?($C_symbol)\s*\(/ ) { |
8eca4617 | 395 | # a simple function declaration |
8162f6f5 RS |
396 | $sym = $1; |
397 | } | |
398 | else { | |
399 | next; | |
400 | } | |
8eca4617 RL |
401 | |
402 | print STDERR "DEBUG[name_synopsis] \$sym = '$sym'\n" if $debug; | |
403 | ||
fbad6e79 | 404 | err($id, "$sym missing from NAME section") |
8162f6f5 RS |
405 | unless defined $names{$sym}; |
406 | $names{$sym} = 2; | |
aebb9aac RS |
407 | |
408 | # Do some sanity checks on the prototype. | |
ad090d57 | 409 | err($id, "Prototype missing spaces around commas: $line") |
93e32043 | 410 | if $is_prototype && $line =~ /[a-z0-9],[^\s]/; |
35ea640a RS |
411 | } |
412 | ||
413 | foreach my $n ( keys %names ) { | |
414 | next if $names{$n} == 2; | |
fbad6e79 | 415 | err($id, "$n missing from SYNOPSIS") |
35ea640a RS |
416 | } |
417 | } | |
418 | ||
39a117d1 | 419 | # Check if SECTION ($3) is located before BEFORE ($4) |
fbad6e79 | 420 | sub check_section_location { |
39a117d1 | 421 | my $id = shift; |
cc838ee2 | 422 | my $contents = shift; |
95f92d57 JL |
423 | my $section = shift; |
424 | my $before = shift; | |
cc838ee2 | 425 | |
485d3361 RS |
426 | return unless $contents =~ /=head1 $section/ |
427 | and $contents =~ /=head1 $before/; | |
fbad6e79 | 428 | err($id, "$section should appear before $before section") |
95f92d57 | 429 | if $contents =~ /=head1 $before.*=head1 $section/ms; |
cc838ee2 PY |
430 | } |
431 | ||
485d3361 RS |
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. | |
fbad6e79 | 436 | sub check_head_style { |
485d3361 RS |
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/ ) { | |
ad090d57 | 445 | err($id, "Duplicate section $line") |
485d3361 RS |
446 | if defined $head1{$line}; |
447 | $head1{$line} = 1; | |
448 | %subheads = (); | |
449 | } else { | |
ad090d57 | 450 | err($id, "Duplicate subsection $line") |
485d3361 RS |
451 | if defined $subheads{$line}; |
452 | $subheads{$line} = 1; | |
453 | } | |
ad090d57 | 454 | err($id, "Period in =head") |
485d3361 | 455 | if $line =~ /\.[^\w]/ or $line =~ /\.$/; |
fbad6e79 | 456 | err($id, "not all uppercase in =head1") |
485d3361 | 457 | if $line =~ /head1.*[a-z]/; |
ad090d57 | 458 | err($id, "All uppercase in subhead") |
485d3361 RS |
459 | if $line =~ /head[234][ A-Z0-9]+$/; |
460 | } | |
461 | } | |
462 | ||
705128b0 RL |
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 | |
79c44b4e | 473 | (?:(?-1)|.)*? # recurse the whole regexp (referring to |
705128b0 RL |
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: | |
a397aca4 RS |
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. | |
705128b0 RL |
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. | |
705128b0 RL |
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") | |
8270c479 | 596 | unless $symbol =~ /^B<.*?>$/ && $unmarked =~ /^${symbol_re}$/ |
705128b0 RL |
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 | ||
60a7817c RS |
605 | # This is from http://man7.org/linux/man-pages/man7/man-pages.7.html |
606 | my %preferred_words = ( | |
490c8711 | 607 | '16bit' => '16-bit', |
8c1cbc72 | 608 | 'a.k.a.' => 'aka', |
60a7817c RS |
609 | 'bitmask' => 'bit mask', |
610 | 'builtin' => 'built-in', | |
611 | #'epoch' => 'Epoch', # handled specially, below | |
52b0bb38 | 612 | 'fall-back' => 'fallback', |
60a7817c RS |
613 | 'file name' => 'filename', |
614 | 'file system' => 'filesystem', | |
615 | 'host name' => 'hostname', | |
616 | 'i-node' => 'inode', | |
617 | 'lower case' => 'lowercase', | |
618 | 'lower-case' => 'lowercase', | |
490c8711 GN |
619 | 'manpage' => 'man page', |
620 | 'non-blocking' => 'nonblocking', | |
621 | 'non-default' => 'nondefault', | |
622 | 'non-empty' => 'nonempty', | |
623 | 'non-negative' => 'nonnegative', | |
60a7817c RS |
624 | 'non-zero' => 'nonzero', |
625 | 'path name' => 'pathname', | |
490c8711 | 626 | 'pre-allocated' => 'preallocated', |
60a7817c | 627 | 'pseudo-terminal' => 'pseudoterminal', |
60a7817c | 628 | 'real time' => 'real-time', |
490c8711 GN |
629 | 'realtime' => 'real-time', |
630 | 'reserved port' => 'privileged port', | |
60a7817c RS |
631 | 'runtime' => 'run time', |
632 | 'saved group ID'=> 'saved set-group-ID', | |
633 | 'saved set-GID' => 'saved set-group-ID', | |
60a7817c | 634 | 'saved set-UID' => 'saved set-user-ID', |
490c8711 | 635 | 'saved user ID' => 'saved set-user-ID', |
60a7817c | 636 | 'set-GID' => 'set-group-ID', |
60a7817c | 637 | 'set-UID' => 'set-user-ID', |
490c8711 | 638 | 'setgid' => 'set-group-ID', |
60a7817c | 639 | 'setuid' => 'set-user-ID', |
490c8711 | 640 | 'sub-system' => 'subsystem', |
60a7817c RS |
641 | 'super block' => 'superblock', |
642 | 'super-block' => 'superblock', | |
490c8711 GN |
643 | 'super user' => 'superuser', |
644 | 'super-user' => 'superuser', | |
645 | 'system port' => 'privileged port', | |
60a7817c RS |
646 | 'time stamp' => 'timestamp', |
647 | 'time zone' => 'timezone', | |
648 | 'upper case' => 'uppercase', | |
649 | 'upper-case' => 'uppercase', | |
650 | 'useable' => 'usable', | |
60a7817c | 651 | 'user name' => 'username', |
490c8711 | 652 | 'userspace' => 'user space', |
60a7817c RS |
653 | 'zeroes' => 'zeros' |
654 | ); | |
655 | ||
a397aca4 | 656 | # Search manpage for words that have a different preferred use. |
60a7817c RS |
657 | sub wording { |
658 | my $id = shift; | |
659 | my $contents = shift; | |
660 | ||
661 | foreach my $k ( keys %preferred_words ) { | |
9c0586d5 RS |
662 | # Sigh, trademark |
663 | next if $k eq 'file system' | |
664 | and $contents =~ /Microsoft Encrypted File System/; | |
ad090d57 | 665 | err($id, "Found '$k' should use '$preferred_words{$k}'") |
60a7817c RS |
666 | if $contents =~ /\b\Q$k\E\b/i; |
667 | } | |
ad090d57 | 668 | err($id, "Found 'epoch' should use 'Epoch'") |
60a7817c | 669 | if $contents =~ /\bepoch\b/; |
4b537191 RS |
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 | } | |
60a7817c RS |
677 | } |
678 | ||
a397aca4 | 679 | # Perform all sorts of nit/error checks on a manpage |
fbad6e79 | 680 | sub check { |
8270c479 RL |
681 | my %podinfo = @_; |
682 | my $filename = $podinfo{filename}; | |
169a8e39 | 683 | my $dirname = basename(dirname($filename)); |
8270c479 | 684 | my $contents = $podinfo{contents}; |
843666ff RS |
685 | |
686 | my $id = "${filename}:1:"; | |
fbad6e79 | 687 | check_head_style($id, $contents); |
35ea640a | 688 | |
39a117d1 RS |
689 | # Check ordering of some sections in man3 |
690 | if ( $filename =~ m|man3/| ) { | |
fbad6e79 RS |
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"); | |
39a117d1 RS |
694 | } |
695 | ||
6e4618a0 RS |
696 | # Make sure every link has a section. |
697 | while ( $contents =~ /$markup_re/msg ) { | |
698 | my $target = $1; | |
76fde1db RL |
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 | |
6e4618a0 | 704 | next if $target =~ /::/; # links to a Perl module, or |
76fde1db RL |
705 | next if $target =~ /^https?:/; # is a URL link, or |
706 | next if $target =~ /\([1357]\)$/; # it has a section | |
6e4618a0 RS |
707 | err($id, "Section missing in $target") |
708 | } | |
1903a9b7 RS |
709 | # Check for proper links to commands. |
710 | while ( $contents =~ /L<([^>]*)\(1\)(?:\/.*)?>/g ) { | |
711 | my $target = $1; | |
712 | next if $target =~ /openssl-?/; | |
1624ebdb RL |
713 | next if ( grep { basename($_) eq "$target.pod" } |
714 | files(TAGS => [ 'manual', 'man1' ]) ); | |
1903a9b7 RS |
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 | } | |
6e4618a0 RS |
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 | ||
8270c479 | 726 | unless ( $contents =~ /^=for openssl generic/ms ) { |
705128b0 RL |
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 | } | |
35ea640a | 734 | |
60a7817c RS |
735 | wording($id, $contents); |
736 | ||
ad090d57 | 737 | err($id, "Doesn't start with =pod") |
05ea606a | 738 | if $contents !~ /^=pod/; |
ad090d57 | 739 | err($id, "Doesn't end with =cut") |
05ea606a | 740 | if $contents !~ /=cut\n$/; |
ad090d57 | 741 | err($id, "More than one cut line.") |
05ea606a | 742 | if $contents =~ /=cut.*=cut/ms; |
fbad6e79 | 743 | err($id, "EXAMPLE not EXAMPLES section.") |
cda77422 | 744 | if $contents =~ /=head1 EXAMPLE[^S]/; |
fbad6e79 | 745 | err($id, "WARNING not WARNINGS section.") |
5e0d9c86 | 746 | if $contents =~ /=head1 WARNING[^S]/; |
ad090d57 | 747 | err($id, "Missing copyright") |
05ea606a | 748 | if $contents !~ /Copyright .* The OpenSSL Project Authors/; |
ad090d57 | 749 | err($id, "Copyright not last") |
05ea606a | 750 | if $contents =~ /head1 COPYRIGHT.*=head/ms; |
fbad6e79 | 751 | err($id, "head2 in All uppercase") |
843666ff | 752 | if $contents =~ /head2\s+[A-Z ]+\n/; |
ad090d57 | 753 | err($id, "Extra space after head") |
35ea640a | 754 | if $contents =~ /=head\d\s\s+/; |
ad090d57 | 755 | err($id, "Period in NAME section") |
35ea640a | 756 | if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms; |
fbad6e79 | 757 | err($id, "Duplicate $1 in L<>") |
5a3371e2 | 758 | if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2; |
fbad6e79 | 759 | err($id, "Bad =over $1") |
2f61bc2e | 760 | if $contents =~ /=over([^ ][^24])/; |
fbad6e79 | 761 | err($id, "Possible version style issue") |
e90fc053 | 762 | if $contents =~ /OpenSSL version [019]/; |
843666ff | 763 | |
bb82531f | 764 | if ( $contents !~ /=for openssl multiple includes/ ) { |
a95d7574 RS |
765 | # Look for multiple consecutive openssl #include lines |
766 | # (non-consecutive lines are okay; see man3/MD5.pod). | |
843666ff RS |
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/@ ) { | |
ad090d57 | 771 | err($id, "Has multiple includes") |
fbad6e79 | 772 | if ++$count == 2; |
843666ff RS |
773 | } else { |
774 | $count = 0; | |
775 | } | |
776 | } | |
777 | } | |
778 | } | |
05ea606a | 779 | |
35ea640a RS |
780 | open my $OUT, '>', $temp |
781 | or die "Can't open $temp, $!"; | |
bf57cab7 RL |
782 | err($id, "POD errors") |
783 | if podchecker($filename, $OUT) != 0; | |
35ea640a RS |
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, $!"; | |
a95d7574 RS |
793 | |
794 | # Find what section this page is in; assume 3. | |
795 | my $section = 3; | |
796 | $section = $1 if $dirname =~ /man([1-9])/; | |
797 | ||
a397aca4 | 798 | foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) { |
ad090d57 | 799 | err($id, "Missing $_ head1 section") |
a95d7574 RS |
800 | if $contents !~ /^=head1\s+${_}\s*$/m; |
801 | } | |
05ea606a | 802 | } |
1bc74519 | 803 | |
8270c479 RL |
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 | ||
a397aca4 | 831 | # Parse libcrypto.num, etc., and return sorted list of what's there. |
8270c479 | 832 | sub loadnum ($;$) { |
71a8b855 | 833 | my $file = shift; |
8270c479 RL |
834 | my $type = shift; |
835 | my @symbols; | |
71a8b855 | 836 | |
1624ebdb | 837 | open my $IN, '<', catfile($config{sourcedir}, $file) |
71a8b855 RS |
838 | or die "Can't open $file, $!, stopped"; |
839 | ||
840 | while ( <$IN> ) { | |
274d1bee | 841 | next if /^#/; |
71a8b855 | 842 | next if /\bNOEXIST\b/; |
1722496f | 843 | my @fields = split(); |
bc6ca4cb | 844 | die "Malformed line $. in $file: $_" |
1722496f | 845 | if scalar @fields != 2 && scalar @fields != 4; |
8270c479 | 846 | $state{$fields[0].'(3)'} = $type // 'internal'; |
71a8b855 | 847 | } |
71a8b855 | 848 | close $IN; |
71a8b855 RS |
849 | } |
850 | ||
a397aca4 | 851 | # Load file of symbol names that we know aren't documented. |
8270c479 | 852 | sub loadmissing($;$) |
b5283535 MC |
853 | { |
854 | my $missingfile = shift; | |
8270c479 | 855 | my $type = shift; |
b5283535 | 856 | |
1624ebdb | 857 | open FH, catfile($config{sourcedir}, $missingfile) |
fadb57e5 | 858 | or die "Can't open $missingfile"; |
b5283535 MC |
859 | while ( <FH> ) { |
860 | chomp; | |
861 | next if /^#/; | |
8270c479 | 862 | $missing{$_} = $type // 'internal'; |
b5283535 MC |
863 | } |
864 | close FH; | |
8270c479 | 865 | } |
b5283535 | 866 | |
8270c479 RL |
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\// ); | |
bf973d06 | 889 | err("$_ is supposedly internal (maybe missing from other.syms) but is documented as public") |
8270c479 | 890 | if ( $declared_internal && $name_map{$_} !~ /\/internal\// ); |
17fa385d | 891 | } |
b5283535 MC |
892 | } |
893 | ||
a397aca4 RS |
894 | # Check for undocumented macros; ignore those in the "missing" file |
895 | # and do simple check for #define in our header files. | |
fbad6e79 | 896 | sub checkmacros { |
9a2dfc0f | 897 | my $count = 0; |
ee4afacd | 898 | my %seen; |
b5283535 | 899 | |
1624ebdb | 900 | foreach my $f ( files(TAGS => 'public_header') ) { |
9a2dfc0f | 901 | # Skip some internals we don't want to document yet. |
1624ebdb RL |
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'; | |
fadb57e5 RS |
906 | open(IN, $f) |
907 | or die "Can't open $f, $!"; | |
9a2dfc0f RS |
908 | while ( <IN> ) { |
909 | next unless /^#\s*define\s*(\S+)\(/; | |
b4350db5 | 910 | my $macro = "$1(3)"; # We know they're all in section 3 |
8270c479 RL |
911 | next if defined $name_map{$macro} |
912 | || defined $missing{$macro} | |
913 | || defined $seen{$macro} | |
914 | || $macro =~ /$ignored/; | |
14ee781e | 915 | |
185ec4be | 916 | err("$f:", "macro $macro undocumented") |
fbad6e79 | 917 | if $opt_d || $opt_e; |
9a2dfc0f | 918 | $count++; |
ee4afacd | 919 | $seen{$macro} = 1; |
9a2dfc0f RS |
920 | } |
921 | close(IN); | |
922 | } | |
185ec4be RS |
923 | err("# $count macros undocumented (count is approximate)") |
924 | if $count > 0; | |
9a2dfc0f RS |
925 | } |
926 | ||
a397aca4 RS |
927 | # Find out what is undocumented (filtering out the known missing ones) |
928 | # and display them. | |
8270c479 RL |
929 | sub printem ($) { |
930 | my $type = shift; | |
71a8b855 | 931 | my $count = 0; |
b5283535 | 932 | |
c4de5d22 RL |
933 | foreach my $func ( grep { $state{$_} eq $type } sort keys %state ) { |
934 | next if defined $name_map{$func} | |
935 | || defined $missing{$func}; | |
8270c479 RL |
936 | |
937 | err("$type:", "function $func undocumented") | |
fbad6e79 | 938 | if $opt_d || $opt_e; |
71a8b855 RS |
939 | $count++; |
940 | } | |
8270c479 | 941 | err("# $count lib$type names are not documented") |
185ec4be | 942 | if $count > 0; |
71a8b855 RS |
943 | } |
944 | ||
a397aca4 | 945 | # Collect all the names in a manpage. |
9e183d22 | 946 | sub collectnames { |
8270c479 RL |
947 | my %podinfo = @_; |
948 | my $filename = $podinfo{filename}; | |
9e183d22 RS |
949 | $filename =~ m|man(\d)/|; |
950 | my $section = $1; | |
a397aca4 | 951 | my $simplename = basename($filename, ".pod"); |
9e183d22 | 952 | my $id = "${filename}:1:"; |
8270c479 | 953 | my $is_generic = $podinfo{contents} =~ /^=for openssl generic/ms; |
9e183d22 | 954 | |
b4350db5 | 955 | unless ( grep { $simplename eq $_ } @{$podinfo{names}} ) { |
d2b194d7 | 956 | err($id, "$simplename not in NAME section"); |
b4350db5 | 957 | push @{$podinfo{names}}, $simplename; |
9e183d22 | 958 | } |
fadb57e5 | 959 | foreach my $name ( @{$podinfo{names}} ) { |
9e183d22 | 960 | next if $name eq ""; |
6f72b210 | 961 | err($id, "'$name' contains whitespace") |
d2b194d7 | 962 | if $name =~ /\s/; |
9e183d22 | 963 | my $name_sec = "$name($section)"; |
8270c479 | 964 | if ( !defined $name_map{$name_sec} ) { |
a397aca4 | 965 | $name_map{$name_sec} = $filename; |
c4de5d22 | 966 | $state{$name_sec} //= |
8270c479 RL |
967 | ( $filename =~ /\/internal\// ? 'internal' : 'public' ) |
968 | if $is_generic; | |
a397aca4 | 969 | } elsif ( $filename eq $name_map{$name_sec} ) { |
b4350db5 | 970 | err($id, "$name_sec duplicated in NAME section of", |
a397aca4 | 971 | $name_map{$name_sec}); |
8270c479 | 972 | } elsif ( $name_map{$name_sec} ne '' ) { |
fbad6e79 | 973 | err($id, "$name_sec also in NAME section of", |
a397aca4 | 974 | $name_map{$name_sec}); |
9e183d22 RS |
975 | } |
976 | } | |
977 | ||
fadb57e5 RS |
978 | if ( $podinfo{contents} =~ /=for openssl foreign manual (.*)\n/ ) { |
979 | foreach my $f ( split / /, $1 ) { | |
8270c479 RL |
980 | $name_map{$f} = ''; # It still exists! |
981 | $state{$f} = 'public'; # We assume! | |
fadb57e5 | 982 | } |
9e183d22 RS |
983 | } |
984 | ||
b4350db5 RL |
985 | my @links = |
986 | $podinfo{contents} =~ /L< | |
9e183d22 RS |
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 | (?:(?:[^\|]|<[^>]*>)*\|)? | |
46f4e1be | 991 | # we're only interested in references that have |
9e183d22 RS |
992 | # a one digit section number |
993 | ([^\/>\(]+\(\d\)) | |
994 | /gx; | |
a397aca4 | 995 | $link_map{$filename} = [ @links ]; |
9e183d22 RS |
996 | } |
997 | ||
a397aca4 | 998 | # Look for L<> ("link") references that point to files that do not exist. |
9e183d22 | 999 | sub checklinks { |
fadb57e5 RS |
1000 | foreach my $filename ( sort keys %link_map ) { |
1001 | foreach my $link ( @{$link_map{$filename}} ) { | |
fbad6e79 | 1002 | err("${filename}:1:", "reference to non-existing $link") |
8270c479 RL |
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\// ); | |
9e183d22 RS |
1008 | } |
1009 | } | |
1010 | } | |
1011 | ||
a397aca4 RS |
1012 | # Cipher/digests to skip if they show up as "not implemented" |
1013 | # because they are, via the "-*" construct. | |
e75138ab RS |
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, | |
1738c0ce RS |
1027 | 'cipher' => 1, |
1028 | 'digest' => 1, | |
e75138ab RS |
1029 | ); |
1030 | ||
a397aca4 | 1031 | # Check the flags of a command and see if everything is in the manpage |
fbad6e79 | 1032 | sub checkflags { |
e75138ab | 1033 | my $cmd = shift; |
bc5a8091 | 1034 | my $doc = shift; |
e75138ab RS |
1035 | my %cmdopts; |
1036 | my %docopts; | |
1738c0ce | 1037 | my %localskips; |
e75138ab RS |
1038 | |
1039 | # Get the list of options in the command. | |
912f8a98 | 1040 | open CFH, "$openssl list --options $cmd|" |
fadb57e5 | 1041 | or die "Can list options for $cmd, $!"; |
e75138ab RS |
1042 | while ( <CFH> ) { |
1043 | chop; | |
1044 | s/ .$//; | |
1045 | $cmdopts{$_} = 1; | |
1046 | } | |
1047 | close CFH; | |
1048 | ||
1049 | # Get the list of flags from the synopsis | |
bc5a8091 | 1050 | open CFH, "<$doc" |
fadb57e5 | 1051 | or die "Can't open $doc, $!"; |
e75138ab RS |
1052 | while ( <CFH> ) { |
1053 | chop; | |
1054 | last if /DESCRIPTION/; | |
9f3c076b | 1055 | if ( /=for openssl ifdef (.*)/ ) { |
1738c0ce RS |
1056 | foreach my $f ( split / /, $1 ) { |
1057 | $localskips{$f} = 1; | |
1058 | } | |
1059 | next; | |
1060 | } | |
65718c51 RS |
1061 | my $opt; |
1062 | if ( /\[B<-([^ >]+)/ ) { | |
1063 | $opt = $1; | |
1064 | } elsif ( /^B<-([^ >]+)/ ) { | |
1065 | $opt = $1; | |
1066 | } else { | |
1067 | next; | |
1068 | } | |
1738c0ce | 1069 | $opt = $1 if $opt =~ /I<(.*)/; |
e75138ab RS |
1070 | $docopts{$1} = 1; |
1071 | } | |
1072 | close CFH; | |
1073 | ||
1074 | # See what's in the command not the manpage. | |
a397aca4 RS |
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 -$_"); | |
e75138ab RS |
1079 | } |
1080 | ||
1081 | # See what's in the command not the manpage. | |
a397aca4 RS |
1082 | my @unimpl = sort grep { !defined $cmdopts{$_} } keys %docopts; |
1083 | foreach ( @unimpl ) { | |
1084 | next if defined $skips{$_} || defined $localskips{$_}; | |
65718c51 | 1085 | err("$doc: $cmd does not implement -$_"); |
e75138ab | 1086 | } |
e75138ab RS |
1087 | } |
1088 | ||
a397aca4 RS |
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 | ||
e75138ab | 1096 | if ( $opt_c ) { |
e75138ab | 1097 | my @commands = (); |
3dfda1a6 | 1098 | |
e75138ab | 1099 | # Get list of commands. |
912f8a98 | 1100 | open FH, "$openssl list -1 -commands|" |
fadb57e5 | 1101 | or die "Can't list commands, $!"; |
e75138ab RS |
1102 | while ( <FH> ) { |
1103 | chop; | |
1104 | push @commands, $_; | |
1105 | } | |
1106 | close FH; | |
1107 | ||
1108 | # See if each has a manpage. | |
bc5a8091 RS |
1109 | foreach my $cmd ( @commands ) { |
1110 | next if $cmd eq 'help' || $cmd eq 'exit'; | |
1624ebdb RL |
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'"); | |
e75138ab | 1120 | } else { |
1624ebdb | 1121 | checkflags($cmd, @doc); |
e75138ab | 1122 | } |
71a8b855 | 1123 | } |
e75138ab RS |
1124 | |
1125 | # See what help is missing. | |
912f8a98 | 1126 | open FH, "$openssl list --missing-help |" |
fadb57e5 | 1127 | or die "Can't list missing help, $!"; |
e75138ab RS |
1128 | while ( <FH> ) { |
1129 | chop; | |
1130 | my ($cmd, $flag) = split; | |
fbad6e79 | 1131 | err("$cmd has no help for -$flag"); |
e75138ab RS |
1132 | } |
1133 | close FH; | |
1134 | ||
fbad6e79 | 1135 | exit $status; |
71a8b855 | 1136 | } |
9e183d22 | 1137 | |
8270c479 RL |
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'); | |
e3ce33b3 | 1147 | } elsif ( !$opt_u ) { |
8270c479 RL |
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 ); | |
9e183d22 | 1166 | } |
b4350db5 RL |
1167 | } |
1168 | ||
1169 | if ( $opt_l ) { | |
9e183d22 RS |
1170 | checklinks(); |
1171 | } | |
1172 | ||
e75138ab | 1173 | if ( $opt_n ) { |
a6dd3a3a RS |
1174 | # If not given args, check that all man1 commands are named properly. |
1175 | if ( scalar @ARGV == 0 ) { | |
1624ebdb | 1176 | foreach ( files(TAGS => [ 'public_manual', 'man1' ]) ) { |
6b480ee3 DDO |
1177 | next if /openssl\.pod/ |
1178 | || /CA\.pl/ || /tsget\.pod/; # these commands are special cases | |
a6dd3a3a RS |
1179 | err("$_ doesn't start with openssl-") unless /openssl-/; |
1180 | } | |
1181 | } | |
e75138ab RS |
1182 | } |
1183 | ||
8270c479 RL |
1184 | checkstate(); |
1185 | ||
b5283535 | 1186 | if ( $opt_u || $opt_v) { |
8270c479 RL |
1187 | printem('crypto'); |
1188 | printem('ssl'); | |
fbad6e79 | 1189 | checkmacros(); |
1bc74519 | 1190 | } |
05ea606a | 1191 | |
fbad6e79 | 1192 | exit $status; |