]>
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; |
de3379c9 | 354 | $line =~ s/LHASH_OF\([^)]+\)/int/g; |
c952780c | 355 | $line =~ s/STACK_OF\([^)]+\)/int/g; |
4460ad90 | 356 | $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g; |
c952780c | 357 | $line =~ s/__declspec\([^)]+\)//; |
93e32043 RL |
358 | |
359 | ## We don't prohibit that space, to allow typedefs looking like | |
360 | ## this: | |
361 | ## | |
362 | ## typedef int (fantastically_long_name_breaks_80char_limit) | |
363 | ## (fantastically_long_name_breaks_80char_limit *something); | |
364 | ## | |
365 | #if ( $line =~ /typedef.*\(\*?\S+\)\s+\(/ ) { | |
366 | # # a callback function with whitespace before the argument list: | |
367 | # # typedef ... (*NAME) (... | |
368 | # # typedef ... (NAME) (... | |
369 | # err($id, "Function typedef has space before arg list: $line"); | |
370 | #} | |
371 | ||
121677b4 RS |
372 | if ( $line =~ /env (\S*)=/ ) { |
373 | # environment variable env NAME=... | |
374 | $sym = $1; | |
b1415dc1 | 375 | } elsif ( $line =~ /typedef.*\(\*?($C_symbol)\)\s*\(/ ) { |
0ed78e78 | 376 | # a callback function pointer: typedef ... (*NAME)(... |
93e32043 | 377 | # a callback function signature: typedef ... (NAME)(... |
0ed78e78 | 378 | $sym = $1; |
b1415dc1 | 379 | } elsif ( $line =~ /typedef.*($C_symbol)\s*\(/ ) { |
0ed78e78 | 380 | # a callback function signature: typedef ... NAME(... |
121677b4 | 381 | $sym = $1; |
b1415dc1 | 382 | } elsif ( $line =~ /typedef.*($C_symbol);/ ) { |
121677b4 | 383 | # a simple typedef: typedef ... NAME; |
31d3a759 | 384 | $is_prototype = 0; |
8162f6f5 | 385 | $sym = $1; |
b1415dc1 | 386 | } elsif ( $line =~ /enum ($C_symbol) \{/ ) { |
d4ea9659 RS |
387 | # an enumeration: enum ... { |
388 | $sym = $1; | |
b1415dc1 | 389 | } elsif ( $line =~ /#\s*(?:define|undef) ($C_symbol)/ ) { |
31d3a759 | 390 | $is_prototype = 0; |
8162f6f5 | 391 | $sym = $1; |
b1415dc1 | 392 | } elsif ( $line =~ /^[^\(]*?\(\*($C_symbol)\s*\(/ ) { |
8eca4617 RL |
393 | # a function returning a function pointer: TYPE (*NAME(args))(args) |
394 | $sym = $1; | |
b1415dc1 | 395 | } elsif ( $line =~ /^[^\(]*?($C_symbol)\s*\(/ ) { |
8eca4617 | 396 | # a simple function declaration |
8162f6f5 RS |
397 | $sym = $1; |
398 | } | |
399 | else { | |
400 | next; | |
401 | } | |
8eca4617 RL |
402 | |
403 | print STDERR "DEBUG[name_synopsis] \$sym = '$sym'\n" if $debug; | |
404 | ||
fbad6e79 | 405 | err($id, "$sym missing from NAME section") |
8162f6f5 RS |
406 | unless defined $names{$sym}; |
407 | $names{$sym} = 2; | |
aebb9aac RS |
408 | |
409 | # Do some sanity checks on the prototype. | |
ad090d57 | 410 | err($id, "Prototype missing spaces around commas: $line") |
93e32043 | 411 | if $is_prototype && $line =~ /[a-z0-9],[^\s]/; |
35ea640a RS |
412 | } |
413 | ||
414 | foreach my $n ( keys %names ) { | |
415 | next if $names{$n} == 2; | |
fbad6e79 | 416 | err($id, "$n missing from SYNOPSIS") |
35ea640a RS |
417 | } |
418 | } | |
419 | ||
39a117d1 | 420 | # Check if SECTION ($3) is located before BEFORE ($4) |
fbad6e79 | 421 | sub check_section_location { |
39a117d1 | 422 | my $id = shift; |
cc838ee2 | 423 | my $contents = shift; |
95f92d57 JL |
424 | my $section = shift; |
425 | my $before = shift; | |
cc838ee2 | 426 | |
485d3361 RS |
427 | return unless $contents =~ /=head1 $section/ |
428 | and $contents =~ /=head1 $before/; | |
fbad6e79 | 429 | err($id, "$section should appear before $before section") |
95f92d57 | 430 | if $contents =~ /=head1 $before.*=head1 $section/ms; |
cc838ee2 PY |
431 | } |
432 | ||
485d3361 RS |
433 | # Check if a =head1 is duplicated, or a =headX is duplicated within a |
434 | # =head1. Treats =head2 =head3 as equivalent -- it doesn't reset the head3 | |
435 | # sets if it finds a =head2 -- but that is good enough for now. Also check | |
436 | # for proper capitalization, trailing periods, etc. | |
fbad6e79 | 437 | sub check_head_style { |
485d3361 RS |
438 | my $id = shift; |
439 | my $contents = shift; | |
440 | my %head1; | |
441 | my %subheads; | |
442 | ||
443 | foreach my $line ( split /\n+/, $contents ) { | |
444 | next unless $line =~ /^=head/; | |
445 | if ( $line =~ /head1/ ) { | |
ad090d57 | 446 | err($id, "Duplicate section $line") |
485d3361 RS |
447 | if defined $head1{$line}; |
448 | $head1{$line} = 1; | |
449 | %subheads = (); | |
450 | } else { | |
ad090d57 | 451 | err($id, "Duplicate subsection $line") |
485d3361 RS |
452 | if defined $subheads{$line}; |
453 | $subheads{$line} = 1; | |
454 | } | |
ad090d57 | 455 | err($id, "Period in =head") |
485d3361 | 456 | if $line =~ /\.[^\w]/ or $line =~ /\.$/; |
fbad6e79 | 457 | err($id, "not all uppercase in =head1") |
485d3361 | 458 | if $line =~ /head1.*[a-z]/; |
ad090d57 | 459 | err($id, "All uppercase in subhead") |
485d3361 RS |
460 | if $line =~ /head[234][ A-Z0-9]+$/; |
461 | } | |
462 | } | |
463 | ||
705128b0 RL |
464 | # Because we have options and symbols with extra markup, we need |
465 | # to take that into account, so we need a regexp that extracts | |
466 | # markup chunks, including recursive markup. | |
467 | # please read up on /(?R)/ in perlre(1) | |
468 | # (note: order is important, (?R) needs to come before .) | |
469 | # (note: non-greedy is important, or something like 'B<foo> and B<bar>' | |
470 | # will be captured as one item) | |
471 | my $markup_re = | |
472 | qr/( # Capture group | |
473 | [BIL]< # The start of what we recurse on | |
79c44b4e | 474 | (?:(?-1)|.)*? # recurse the whole regexp (referring to |
705128b0 RL |
475 | # the last opened capture group, i.e. the |
476 | # start of this regexp), or pick next | |
477 | # character. Do NOT be greedy! | |
478 | > # The end of what we recurse on | |
479 | )/x; # (the x allows this sort of split up regexp) | |
480 | ||
481 | # Options must start with a dash, followed by a letter, possibly | |
482 | # followed by letters, digits, dashes and underscores, and the last | |
483 | # character must be a letter or a digit. | |
484 | # We do also accept the single -? or -n, where n is a digit | |
485 | my $option_re = | |
486 | qr/(?: | |
487 | \? # Single question mark | |
488 | | | |
489 | \d # Single digit | |
490 | | | |
491 | - # Single dash (--) | |
492 | | | |
493 | [[:alpha:]](?:[-_[:alnum:]]*?[[:alnum:]])? | |
494 | )/x; | |
495 | ||
496 | # Helper function to check if a given $thing is properly marked up | |
497 | # option. It returns one of these values: | |
a397aca4 RS |
498 | # undef if it's not an option |
499 | # "" if it's a malformed option | |
500 | # $unwrapped the option with the outermost B<> wrapping removed. | |
705128b0 RL |
501 | sub normalise_option { |
502 | my $id = shift; | |
503 | my $filename = shift; | |
504 | my $thing = shift; | |
505 | ||
506 | my $unwrapped = $thing; | |
507 | my $unmarked = $thing; | |
508 | ||
509 | # $unwrapped is the option with the outer B<> markup removed | |
510 | $unwrapped =~ s/^B<//; | |
511 | $unwrapped =~ s/>$//; | |
512 | # $unmarked is the option with *all* markup removed | |
513 | $unmarked =~ s/[BIL]<|>//msg; | |
514 | ||
515 | ||
516 | # If we found an option, check it, collect it | |
517 | if ( $unwrapped =~ /^\s*-/ ) { | |
518 | return $unwrapped # return option with outer B<> removed | |
519 | if $unmarked =~ /^-${option_re}$/; | |
520 | return ""; # Malformed option | |
521 | } | |
522 | return undef; # Something else | |
523 | } | |
524 | ||
525 | # Checks of command option (man1) formatting. The man1 checks are | |
526 | # restricted to the SYNOPSIS and OPTIONS sections, the rest is too | |
527 | # free form, we simply cannot be too strict there. | |
528 | ||
529 | sub option_check { | |
530 | my $id = shift; | |
531 | my $filename = shift; | |
532 | my $contents = shift; | |
533 | ||
534 | my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1); | |
535 | ||
536 | # Some pages have more than one OPTIONS section, let's make sure | |
537 | # to get them all | |
538 | my $options = ''; | |
539 | while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) { | |
540 | $options .= $1; | |
541 | } | |
542 | ||
543 | # Look for options with no or incorrect markup | |
544 | while ( $synopsis =~ | |
545 | /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) { | |
546 | err($id, "Malformed option [1] in SYNOPSIS: $&"); | |
547 | } | |
548 | ||
549 | while ( $synopsis =~ /$markup_re/msg ) { | |
550 | my $found = $&; | |
551 | print STDERR "$id:DEBUG[option_check] SYNOPSIS: found $found\n" | |
552 | if $debug; | |
553 | my $option_uw = normalise_option($id, $filename, $found); | |
554 | err($id, "Malformed option [2] in SYNOPSIS: $found") | |
555 | if defined $option_uw && $option_uw eq ''; | |
556 | } | |
557 | ||
558 | # In OPTIONS, we look for =item paragraphs. | |
559 | # (?=^\s*$) detects an empty line. | |
560 | while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) { | |
561 | my $item = $&; | |
562 | ||
563 | while ( $item =~ /(\[\s*)?($markup_re)/msg ) { | |
564 | my $found = $2; | |
565 | print STDERR "$id:DEBUG[option_check] OPTIONS: found $&\n" | |
566 | if $debug; | |
567 | err($id, "Unexpected bracket in OPTIONS =item: $item") | |
568 | if ($1 // '') ne '' && $found =~ /^B<\s*-/; | |
569 | ||
570 | my $option_uw = normalise_option($id, $filename, $found); | |
571 | err($id, "Malformed option in OPTIONS: $found") | |
572 | if defined $option_uw && $option_uw eq ''; | |
573 | } | |
574 | } | |
575 | } | |
576 | ||
577 | # Normal symbol form | |
578 | my $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/; | |
579 | ||
580 | # Checks of function name (man3) formatting. The man3 checks are | |
581 | # easier than the man1 checks, we only check the names followed by (), | |
582 | # and only the names that have POD markup. | |
705128b0 RL |
583 | sub functionname_check { |
584 | my $id = shift; | |
585 | my $filename = shift; | |
586 | my $contents = shift; | |
587 | ||
588 | while ( $contents =~ /($markup_re)\(\)/msg ) { | |
589 | print STDERR "$id:DEBUG[functionname_check] SYNOPSIS: found $&\n" | |
590 | if $debug; | |
591 | ||
592 | my $symbol = $1; | |
593 | my $unmarked = $symbol; | |
594 | $unmarked =~ s/[BIL]<|>//msg; | |
595 | ||
596 | err($id, "Malformed symbol: $symbol") | |
8270c479 | 597 | unless $symbol =~ /^B<.*?>$/ && $unmarked =~ /^${symbol_re}$/ |
705128b0 RL |
598 | } |
599 | ||
600 | # We can't do the kind of collecting coolness that option_check() | |
601 | # does, because there are too many things that can't be found in | |
602 | # name repositories like the NAME sections, such as symbol names | |
603 | # with a variable part (typically marked up as B<foo_I<TYPE>_bar> | |
604 | } | |
605 | ||
60a7817c RS |
606 | # This is from http://man7.org/linux/man-pages/man7/man-pages.7.html |
607 | my %preferred_words = ( | |
490c8711 | 608 | '16bit' => '16-bit', |
8c1cbc72 | 609 | 'a.k.a.' => 'aka', |
60a7817c RS |
610 | 'bitmask' => 'bit mask', |
611 | 'builtin' => 'built-in', | |
612 | #'epoch' => 'Epoch', # handled specially, below | |
52b0bb38 | 613 | 'fall-back' => 'fallback', |
60a7817c RS |
614 | 'file name' => 'filename', |
615 | 'file system' => 'filesystem', | |
616 | 'host name' => 'hostname', | |
617 | 'i-node' => 'inode', | |
618 | 'lower case' => 'lowercase', | |
619 | 'lower-case' => 'lowercase', | |
490c8711 GN |
620 | 'manpage' => 'man page', |
621 | 'non-blocking' => 'nonblocking', | |
622 | 'non-default' => 'nondefault', | |
623 | 'non-empty' => 'nonempty', | |
624 | 'non-negative' => 'nonnegative', | |
60a7817c RS |
625 | 'non-zero' => 'nonzero', |
626 | 'path name' => 'pathname', | |
490c8711 | 627 | 'pre-allocated' => 'preallocated', |
60a7817c | 628 | 'pseudo-terminal' => 'pseudoterminal', |
60a7817c | 629 | 'real time' => 'real-time', |
490c8711 GN |
630 | 'realtime' => 'real-time', |
631 | 'reserved port' => 'privileged port', | |
60a7817c RS |
632 | 'runtime' => 'run time', |
633 | 'saved group ID'=> 'saved set-group-ID', | |
634 | 'saved set-GID' => 'saved set-group-ID', | |
60a7817c | 635 | 'saved set-UID' => 'saved set-user-ID', |
490c8711 | 636 | 'saved user ID' => 'saved set-user-ID', |
60a7817c | 637 | 'set-GID' => 'set-group-ID', |
60a7817c | 638 | 'set-UID' => 'set-user-ID', |
490c8711 | 639 | 'setgid' => 'set-group-ID', |
60a7817c | 640 | 'setuid' => 'set-user-ID', |
490c8711 | 641 | 'sub-system' => 'subsystem', |
60a7817c RS |
642 | 'super block' => 'superblock', |
643 | 'super-block' => 'superblock', | |
490c8711 GN |
644 | 'super user' => 'superuser', |
645 | 'super-user' => 'superuser', | |
646 | 'system port' => 'privileged port', | |
60a7817c RS |
647 | 'time stamp' => 'timestamp', |
648 | 'time zone' => 'timezone', | |
649 | 'upper case' => 'uppercase', | |
650 | 'upper-case' => 'uppercase', | |
651 | 'useable' => 'usable', | |
60a7817c | 652 | 'user name' => 'username', |
490c8711 | 653 | 'userspace' => 'user space', |
60a7817c RS |
654 | 'zeroes' => 'zeros' |
655 | ); | |
656 | ||
a397aca4 | 657 | # Search manpage for words that have a different preferred use. |
60a7817c RS |
658 | sub wording { |
659 | my $id = shift; | |
660 | my $contents = shift; | |
661 | ||
662 | foreach my $k ( keys %preferred_words ) { | |
9c0586d5 RS |
663 | # Sigh, trademark |
664 | next if $k eq 'file system' | |
665 | and $contents =~ /Microsoft Encrypted File System/; | |
ad090d57 | 666 | err($id, "Found '$k' should use '$preferred_words{$k}'") |
60a7817c RS |
667 | if $contents =~ /\b\Q$k\E\b/i; |
668 | } | |
ad090d57 | 669 | err($id, "Found 'epoch' should use 'Epoch'") |
60a7817c | 670 | if $contents =~ /\bepoch\b/; |
4b537191 RS |
671 | if ( $id =~ m@man1/@ ) { |
672 | err($id, "found 'tool' in NAME, should use 'command'") | |
673 | if $contents =~ /=head1 NAME.*\btool\b.*=head1 SYNOPSIS/s; | |
674 | err($id, "found 'utility' in NAME, should use 'command'") | |
675 | if $contents =~ /NAME.*\butility\b.*=head1 SYNOPSIS/s; | |
676 | ||
677 | } | |
60a7817c RS |
678 | } |
679 | ||
a397aca4 | 680 | # Perform all sorts of nit/error checks on a manpage |
fbad6e79 | 681 | sub check { |
8270c479 RL |
682 | my %podinfo = @_; |
683 | my $filename = $podinfo{filename}; | |
169a8e39 | 684 | my $dirname = basename(dirname($filename)); |
8270c479 | 685 | my $contents = $podinfo{contents}; |
843666ff RS |
686 | |
687 | my $id = "${filename}:1:"; | |
fbad6e79 | 688 | check_head_style($id, $contents); |
35ea640a | 689 | |
39a117d1 RS |
690 | # Check ordering of some sections in man3 |
691 | if ( $filename =~ m|man3/| ) { | |
fbad6e79 RS |
692 | check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES"); |
693 | check_section_location($id, $contents, "SEE ALSO", "HISTORY"); | |
694 | check_section_location($id, $contents, "EXAMPLES", "SEE ALSO"); | |
39a117d1 RS |
695 | } |
696 | ||
6e4618a0 RS |
697 | # Make sure every link has a section. |
698 | while ( $contents =~ /$markup_re/msg ) { | |
699 | my $target = $1; | |
76fde1db RL |
700 | next unless $target =~ /^L<(.*)>$/; # Skip if not L<...> |
701 | $target = $1; # Peal away L< and > | |
702 | $target =~ s/\/[^\/]*$//; # Peal away possible anchor | |
703 | $target =~ s/.*\|//g; # Peal away possible link text | |
704 | next if $target eq ''; # Skip if links within page, or | |
6e4618a0 | 705 | next if $target =~ /::/; # links to a Perl module, or |
76fde1db RL |
706 | next if $target =~ /^https?:/; # is a URL link, or |
707 | next if $target =~ /\([1357]\)$/; # it has a section | |
6e4618a0 RS |
708 | err($id, "Section missing in $target") |
709 | } | |
1903a9b7 RS |
710 | # Check for proper links to commands. |
711 | while ( $contents =~ /L<([^>]*)\(1\)(?:\/.*)?>/g ) { | |
712 | my $target = $1; | |
713 | next if $target =~ /openssl-?/; | |
1624ebdb RL |
714 | next if ( grep { basename($_) eq "$target.pod" } |
715 | files(TAGS => [ 'manual', 'man1' ]) ); | |
1903a9b7 RS |
716 | # TODO: Filter out "foreign manual" links. |
717 | next if $target =~ /ps|apropos|sha1sum|procmail|perl/; | |
718 | err($id, "Bad command link L<$target(1)>"); | |
719 | } | |
6e4618a0 RS |
720 | # Check for proper in-man-3 API links. |
721 | while ( $contents =~ /L<([^>]*)\(3\)(?:\/.*)?>/g ) { | |
722 | my $target = $1; | |
723 | err($id, "Bad L<$target>") | |
724 | unless $target =~ /^[_[:alpha:]][_[:alnum:]]*$/ | |
725 | } | |
726 | ||
8270c479 | 727 | unless ( $contents =~ /^=for openssl generic/ms ) { |
705128b0 RL |
728 | if ( $filename =~ m|man3/| ) { |
729 | name_synopsis($id, $filename, $contents); | |
730 | functionname_check($id, $filename, $contents); | |
731 | } elsif ( $filename =~ m|man1/| ) { | |
732 | option_check($id, $filename, $contents) | |
733 | } | |
734 | } | |
35ea640a | 735 | |
60a7817c RS |
736 | wording($id, $contents); |
737 | ||
ad090d57 | 738 | err($id, "Doesn't start with =pod") |
05ea606a | 739 | if $contents !~ /^=pod/; |
ad090d57 | 740 | err($id, "Doesn't end with =cut") |
05ea606a | 741 | if $contents !~ /=cut\n$/; |
ad090d57 | 742 | err($id, "More than one cut line.") |
05ea606a | 743 | if $contents =~ /=cut.*=cut/ms; |
fbad6e79 | 744 | err($id, "EXAMPLE not EXAMPLES section.") |
cda77422 | 745 | if $contents =~ /=head1 EXAMPLE[^S]/; |
fbad6e79 | 746 | err($id, "WARNING not WARNINGS section.") |
5e0d9c86 | 747 | if $contents =~ /=head1 WARNING[^S]/; |
ad090d57 | 748 | err($id, "Missing copyright") |
05ea606a | 749 | if $contents !~ /Copyright .* The OpenSSL Project Authors/; |
ad090d57 | 750 | err($id, "Copyright not last") |
05ea606a | 751 | if $contents =~ /head1 COPYRIGHT.*=head/ms; |
fbad6e79 | 752 | err($id, "head2 in All uppercase") |
843666ff | 753 | if $contents =~ /head2\s+[A-Z ]+\n/; |
ad090d57 | 754 | err($id, "Extra space after head") |
35ea640a | 755 | if $contents =~ /=head\d\s\s+/; |
ad090d57 | 756 | err($id, "Period in NAME section") |
35ea640a | 757 | if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms; |
fbad6e79 | 758 | err($id, "Duplicate $1 in L<>") |
5a3371e2 | 759 | if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2; |
fbad6e79 | 760 | err($id, "Bad =over $1") |
2f61bc2e | 761 | if $contents =~ /=over([^ ][^24])/; |
fbad6e79 | 762 | err($id, "Possible version style issue") |
e90fc053 | 763 | if $contents =~ /OpenSSL version [019]/; |
843666ff | 764 | |
bb82531f | 765 | if ( $contents !~ /=for openssl multiple includes/ ) { |
a95d7574 RS |
766 | # Look for multiple consecutive openssl #include lines |
767 | # (non-consecutive lines are okay; see man3/MD5.pod). | |
843666ff RS |
768 | if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) { |
769 | my $count = 0; | |
770 | foreach my $line ( split /\n+/, $1 ) { | |
771 | if ( $line =~ m@include <openssl/@ ) { | |
ad090d57 | 772 | err($id, "Has multiple includes") |
fbad6e79 | 773 | if ++$count == 2; |
843666ff RS |
774 | } else { |
775 | $count = 0; | |
776 | } | |
777 | } | |
778 | } | |
779 | } | |
05ea606a | 780 | |
35ea640a RS |
781 | open my $OUT, '>', $temp |
782 | or die "Can't open $temp, $!"; | |
bf57cab7 RL |
783 | err($id, "POD errors") |
784 | if podchecker($filename, $OUT) != 0; | |
35ea640a RS |
785 | close $OUT; |
786 | open $OUT, '<', $temp | |
787 | or die "Can't read $temp, $!"; | |
788 | while ( <$OUT> ) { | |
789 | next if /\(section\) in.*deprecated/; | |
790 | print; | |
791 | } | |
792 | close $OUT; | |
793 | unlink $temp || warn "Can't remove $temp, $!"; | |
a95d7574 RS |
794 | |
795 | # Find what section this page is in; assume 3. | |
796 | my $section = 3; | |
797 | $section = $1 if $dirname =~ /man([1-9])/; | |
798 | ||
a397aca4 | 799 | foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) { |
ad090d57 | 800 | err($id, "Missing $_ head1 section") |
a95d7574 RS |
801 | if $contents !~ /^=head1\s+${_}\s*$/m; |
802 | } | |
05ea606a | 803 | } |
1bc74519 | 804 | |
8270c479 RL |
805 | # Information database ############################################### |
806 | ||
807 | # Map of links in each POD file; filename => [ "foo(1)", "bar(3)", ... ] | |
808 | my %link_map = (); | |
809 | # Map of names in each POD file or from "missing" files; possible values are: | |
810 | # If found in a POD files, "name(s)" => filename | |
811 | # If found in a "missing" file or external, "name(s)" => '' | |
812 | my %name_map = (); | |
813 | ||
814 | # State of man-page names. | |
815 | # %state is affected by loading util/*.num and util/*.syms | |
816 | # Values may be one of: | |
817 | # 'crypto' : belongs in libcrypto (loaded from libcrypto.num) | |
818 | # 'ssl' : belongs in libssl (loaded from libssl.num) | |
819 | # 'other' : belongs in libcrypto or libssl (loaded from other.syms) | |
820 | # 'internal' : Internal | |
821 | # 'public' : Public (generic name or external documentation) | |
822 | # Any of these values except 'public' may be prefixed with 'missing_' | |
823 | # to indicate that they are known to be missing. | |
824 | my %state; | |
825 | # %missing is affected by loading util/missing*.txt. Values may be one of: | |
826 | # 'crypto' : belongs in libcrypto (loaded from libcrypto.num) | |
827 | # 'ssl' : belongs in libssl (loaded from libssl.num) | |
828 | # 'other' : belongs in libcrypto or libssl (loaded from other.syms) | |
829 | # 'internal' : Internal | |
830 | my %missing; | |
831 | ||
a397aca4 | 832 | # Parse libcrypto.num, etc., and return sorted list of what's there. |
8270c479 | 833 | sub loadnum ($;$) { |
71a8b855 | 834 | my $file = shift; |
8270c479 RL |
835 | my $type = shift; |
836 | my @symbols; | |
71a8b855 | 837 | |
1624ebdb | 838 | open my $IN, '<', catfile($config{sourcedir}, $file) |
71a8b855 RS |
839 | or die "Can't open $file, $!, stopped"; |
840 | ||
841 | while ( <$IN> ) { | |
274d1bee | 842 | next if /^#/; |
71a8b855 | 843 | next if /\bNOEXIST\b/; |
1722496f | 844 | my @fields = split(); |
bc6ca4cb | 845 | die "Malformed line $. in $file: $_" |
1722496f | 846 | if scalar @fields != 2 && scalar @fields != 4; |
8270c479 | 847 | $state{$fields[0].'(3)'} = $type // 'internal'; |
71a8b855 | 848 | } |
71a8b855 | 849 | close $IN; |
71a8b855 RS |
850 | } |
851 | ||
a397aca4 | 852 | # Load file of symbol names that we know aren't documented. |
8270c479 | 853 | sub loadmissing($;$) |
b5283535 MC |
854 | { |
855 | my $missingfile = shift; | |
8270c479 | 856 | my $type = shift; |
b5283535 | 857 | |
1624ebdb | 858 | open FH, catfile($config{sourcedir}, $missingfile) |
fadb57e5 | 859 | or die "Can't open $missingfile"; |
b5283535 MC |
860 | while ( <FH> ) { |
861 | chomp; | |
862 | next if /^#/; | |
8270c479 | 863 | $missing{$_} = $type // 'internal'; |
b5283535 MC |
864 | } |
865 | close FH; | |
8270c479 | 866 | } |
b5283535 | 867 | |
8270c479 RL |
868 | # Check that we have consistent public / internal documentation and declaration |
869 | sub checkstate () { | |
870 | # Collect all known names, no matter where they come from | |
871 | my %names = map { $_ => 1 } (keys %name_map, keys %state, keys %missing); | |
872 | ||
873 | # Check section 3, i.e. functions and macros | |
874 | foreach ( grep { $_ =~ /\(3\)$/ } sort keys %names ) { | |
875 | next if ( $name_map{$_} // '') eq '' || $_ =~ /$ignored/; | |
876 | ||
877 | # If a man-page isn't recorded public or if it's recorded missing | |
878 | # and internal, it's declared to be internal. | |
879 | my $declared_internal = | |
880 | ($state{$_} // 'internal') eq 'internal' | |
881 | || ($missing{$_} // '') eq 'internal'; | |
882 | # If a man-page isn't recorded internal or if it's recorded missing | |
883 | # and not internal, it's declared to be public | |
884 | my $declared_public = | |
885 | ($state{$_} // 'internal') ne 'internal' | |
886 | || ($missing{$_} // 'internal') ne 'internal'; | |
887 | ||
888 | err("$_ is supposedly public but is documented as internal") | |
889 | if ( $declared_public && $name_map{$_} =~ /\/internal\// ); | |
bf973d06 | 890 | err("$_ is supposedly internal (maybe missing from other.syms) but is documented as public") |
8270c479 | 891 | if ( $declared_internal && $name_map{$_} !~ /\/internal\// ); |
17fa385d | 892 | } |
b5283535 MC |
893 | } |
894 | ||
a397aca4 RS |
895 | # Check for undocumented macros; ignore those in the "missing" file |
896 | # and do simple check for #define in our header files. | |
fbad6e79 | 897 | sub checkmacros { |
9a2dfc0f | 898 | my $count = 0; |
ee4afacd | 899 | my %seen; |
b5283535 | 900 | |
1624ebdb | 901 | foreach my $f ( files(TAGS => 'public_header') ) { |
9a2dfc0f | 902 | # Skip some internals we don't want to document yet. |
1624ebdb RL |
903 | my $b = basename($f); |
904 | next if $b eq 'asn1.h'; | |
905 | next if $b eq 'asn1t.h'; | |
906 | next if $b eq 'err.h'; | |
fadb57e5 RS |
907 | open(IN, $f) |
908 | or die "Can't open $f, $!"; | |
9a2dfc0f RS |
909 | while ( <IN> ) { |
910 | next unless /^#\s*define\s*(\S+)\(/; | |
b4350db5 | 911 | my $macro = "$1(3)"; # We know they're all in section 3 |
8270c479 RL |
912 | next if defined $name_map{$macro} |
913 | || defined $missing{$macro} | |
914 | || defined $seen{$macro} | |
915 | || $macro =~ /$ignored/; | |
14ee781e | 916 | |
185ec4be | 917 | err("$f:", "macro $macro undocumented") |
fbad6e79 | 918 | if $opt_d || $opt_e; |
9a2dfc0f | 919 | $count++; |
ee4afacd | 920 | $seen{$macro} = 1; |
9a2dfc0f RS |
921 | } |
922 | close(IN); | |
923 | } | |
185ec4be RS |
924 | err("# $count macros undocumented (count is approximate)") |
925 | if $count > 0; | |
9a2dfc0f RS |
926 | } |
927 | ||
a397aca4 RS |
928 | # Find out what is undocumented (filtering out the known missing ones) |
929 | # and display them. | |
8270c479 RL |
930 | sub printem ($) { |
931 | my $type = shift; | |
71a8b855 | 932 | my $count = 0; |
b5283535 | 933 | |
c4de5d22 RL |
934 | foreach my $func ( grep { $state{$_} eq $type } sort keys %state ) { |
935 | next if defined $name_map{$func} | |
936 | || defined $missing{$func}; | |
8270c479 RL |
937 | |
938 | err("$type:", "function $func undocumented") | |
fbad6e79 | 939 | if $opt_d || $opt_e; |
71a8b855 RS |
940 | $count++; |
941 | } | |
8270c479 | 942 | err("# $count lib$type names are not documented") |
185ec4be | 943 | if $count > 0; |
71a8b855 RS |
944 | } |
945 | ||
a397aca4 | 946 | # Collect all the names in a manpage. |
9e183d22 | 947 | sub collectnames { |
8270c479 RL |
948 | my %podinfo = @_; |
949 | my $filename = $podinfo{filename}; | |
9e183d22 RS |
950 | $filename =~ m|man(\d)/|; |
951 | my $section = $1; | |
a397aca4 | 952 | my $simplename = basename($filename, ".pod"); |
9e183d22 | 953 | my $id = "${filename}:1:"; |
8270c479 | 954 | my $is_generic = $podinfo{contents} =~ /^=for openssl generic/ms; |
9e183d22 | 955 | |
b4350db5 | 956 | unless ( grep { $simplename eq $_ } @{$podinfo{names}} ) { |
d2b194d7 | 957 | err($id, "$simplename not in NAME section"); |
b4350db5 | 958 | push @{$podinfo{names}}, $simplename; |
9e183d22 | 959 | } |
fadb57e5 | 960 | foreach my $name ( @{$podinfo{names}} ) { |
9e183d22 | 961 | next if $name eq ""; |
6f72b210 | 962 | err($id, "'$name' contains whitespace") |
d2b194d7 | 963 | if $name =~ /\s/; |
9e183d22 | 964 | my $name_sec = "$name($section)"; |
8270c479 | 965 | if ( !defined $name_map{$name_sec} ) { |
a397aca4 | 966 | $name_map{$name_sec} = $filename; |
c4de5d22 | 967 | $state{$name_sec} //= |
8270c479 RL |
968 | ( $filename =~ /\/internal\// ? 'internal' : 'public' ) |
969 | if $is_generic; | |
a397aca4 | 970 | } elsif ( $filename eq $name_map{$name_sec} ) { |
b4350db5 | 971 | err($id, "$name_sec duplicated in NAME section of", |
a397aca4 | 972 | $name_map{$name_sec}); |
8270c479 | 973 | } elsif ( $name_map{$name_sec} ne '' ) { |
fbad6e79 | 974 | err($id, "$name_sec also in NAME section of", |
a397aca4 | 975 | $name_map{$name_sec}); |
9e183d22 RS |
976 | } |
977 | } | |
978 | ||
fadb57e5 RS |
979 | if ( $podinfo{contents} =~ /=for openssl foreign manual (.*)\n/ ) { |
980 | foreach my $f ( split / /, $1 ) { | |
8270c479 RL |
981 | $name_map{$f} = ''; # It still exists! |
982 | $state{$f} = 'public'; # We assume! | |
fadb57e5 | 983 | } |
9e183d22 RS |
984 | } |
985 | ||
b4350db5 RL |
986 | my @links = |
987 | $podinfo{contents} =~ /L< | |
9e183d22 RS |
988 | # if the link is of the form L<something|name(s)>, |
989 | # then remove 'something'. Note that 'something' | |
990 | # may contain POD codes as well... | |
991 | (?:(?:[^\|]|<[^>]*>)*\|)? | |
46f4e1be | 992 | # we're only interested in references that have |
9e183d22 RS |
993 | # a one digit section number |
994 | ([^\/>\(]+\(\d\)) | |
995 | /gx; | |
a397aca4 | 996 | $link_map{$filename} = [ @links ]; |
9e183d22 RS |
997 | } |
998 | ||
a397aca4 | 999 | # Look for L<> ("link") references that point to files that do not exist. |
9e183d22 | 1000 | sub checklinks { |
fadb57e5 RS |
1001 | foreach my $filename ( sort keys %link_map ) { |
1002 | foreach my $link ( @{$link_map{$filename}} ) { | |
fbad6e79 | 1003 | err("${filename}:1:", "reference to non-existing $link") |
8270c479 RL |
1004 | unless defined $name_map{$link} || defined $missing{$link}; |
1005 | err("${filename}:1:", "reference of internal $link in public documentation $filename") | |
1006 | if ( ( ($state{$link} // '') eq 'internal' | |
1007 | || ($missing{$link} // '') eq 'internal' ) | |
1008 | && $filename !~ /\/internal\// ); | |
9e183d22 RS |
1009 | } |
1010 | } | |
1011 | } | |
1012 | ||
a397aca4 RS |
1013 | # Cipher/digests to skip if they show up as "not implemented" |
1014 | # because they are, via the "-*" construct. | |
e75138ab RS |
1015 | my %skips = ( |
1016 | 'aes128' => 1, | |
1017 | 'aes192' => 1, | |
1018 | 'aes256' => 1, | |
1019 | 'aria128' => 1, | |
1020 | 'aria192' => 1, | |
1021 | 'aria256' => 1, | |
1022 | 'camellia128' => 1, | |
1023 | 'camellia192' => 1, | |
1024 | 'camellia256' => 1, | |
1025 | 'des' => 1, | |
1026 | 'des3' => 1, | |
1027 | 'idea' => 1, | |
1738c0ce RS |
1028 | 'cipher' => 1, |
1029 | 'digest' => 1, | |
e75138ab RS |
1030 | ); |
1031 | ||
a397aca4 | 1032 | # Check the flags of a command and see if everything is in the manpage |
fbad6e79 | 1033 | sub checkflags { |
e75138ab | 1034 | my $cmd = shift; |
bc5a8091 | 1035 | my $doc = shift; |
e75138ab RS |
1036 | my %cmdopts; |
1037 | my %docopts; | |
1738c0ce | 1038 | my %localskips; |
e75138ab RS |
1039 | |
1040 | # Get the list of options in the command. | |
912f8a98 | 1041 | open CFH, "$openssl list --options $cmd|" |
fadb57e5 | 1042 | or die "Can list options for $cmd, $!"; |
e75138ab RS |
1043 | while ( <CFH> ) { |
1044 | chop; | |
1045 | s/ .$//; | |
1046 | $cmdopts{$_} = 1; | |
1047 | } | |
1048 | close CFH; | |
1049 | ||
1050 | # Get the list of flags from the synopsis | |
bc5a8091 | 1051 | open CFH, "<$doc" |
fadb57e5 | 1052 | or die "Can't open $doc, $!"; |
e75138ab RS |
1053 | while ( <CFH> ) { |
1054 | chop; | |
1055 | last if /DESCRIPTION/; | |
9f3c076b | 1056 | if ( /=for openssl ifdef (.*)/ ) { |
1738c0ce RS |
1057 | foreach my $f ( split / /, $1 ) { |
1058 | $localskips{$f} = 1; | |
1059 | } | |
1060 | next; | |
1061 | } | |
65718c51 RS |
1062 | my $opt; |
1063 | if ( /\[B<-([^ >]+)/ ) { | |
1064 | $opt = $1; | |
1065 | } elsif ( /^B<-([^ >]+)/ ) { | |
1066 | $opt = $1; | |
1067 | } else { | |
1068 | next; | |
1069 | } | |
1738c0ce | 1070 | $opt = $1 if $opt =~ /I<(.*)/; |
e75138ab RS |
1071 | $docopts{$1} = 1; |
1072 | } | |
1073 | close CFH; | |
1074 | ||
1075 | # See what's in the command not the manpage. | |
a397aca4 RS |
1076 | my @undocced = sort grep { !defined $docopts{$_} } keys %cmdopts; |
1077 | foreach ( @undocced ) { | |
1078 | next if /-/; # Skip the -- end-of-flags marker | |
1079 | err("$doc: undocumented option -$_"); | |
e75138ab RS |
1080 | } |
1081 | ||
1082 | # See what's in the command not the manpage. | |
a397aca4 RS |
1083 | my @unimpl = sort grep { !defined $cmdopts{$_} } keys %docopts; |
1084 | foreach ( @unimpl ) { | |
1085 | next if defined $skips{$_} || defined $localskips{$_}; | |
65718c51 | 1086 | err("$doc: $cmd does not implement -$_"); |
e75138ab | 1087 | } |
e75138ab RS |
1088 | } |
1089 | ||
a397aca4 RS |
1090 | ## |
1091 | ## MAIN() | |
1092 | ## Do the work requested by the various getopt flags. | |
1093 | ## The flags are parsed in alphabetical order, just because we have | |
1094 | ## to have *some way* of listing them. | |
1095 | ## | |
1096 | ||
e75138ab | 1097 | if ( $opt_c ) { |
e75138ab | 1098 | my @commands = (); |
3dfda1a6 | 1099 | |
e75138ab | 1100 | # Get list of commands. |
912f8a98 | 1101 | open FH, "$openssl list -1 -commands|" |
fadb57e5 | 1102 | or die "Can't list commands, $!"; |
e75138ab RS |
1103 | while ( <FH> ) { |
1104 | chop; | |
1105 | push @commands, $_; | |
1106 | } | |
1107 | close FH; | |
1108 | ||
1109 | # See if each has a manpage. | |
bc5a8091 RS |
1110 | foreach my $cmd ( @commands ) { |
1111 | next if $cmd eq 'help' || $cmd eq 'exit'; | |
1624ebdb RL |
1112 | my @doc = ( grep { basename($_) eq "openssl-$cmd.pod" |
1113 | # For "tsget" and "CA.pl" pod pages | |
1114 | || basename($_) eq "$cmd.pod" } | |
1115 | files(TAGS => [ 'manual', 'man1' ]) ); | |
1116 | my $num = scalar @doc; | |
1117 | if ($num > 1) { | |
1118 | err("$num manuals for 'openssl $cmd': ".join(", ", @doc)); | |
1119 | } elsif ($num < 1) { | |
1120 | err("no manual for 'openssl $cmd'"); | |
e75138ab | 1121 | } else { |
1624ebdb | 1122 | checkflags($cmd, @doc); |
e75138ab | 1123 | } |
71a8b855 | 1124 | } |
e75138ab RS |
1125 | |
1126 | # See what help is missing. | |
912f8a98 | 1127 | open FH, "$openssl list --missing-help |" |
fadb57e5 | 1128 | or die "Can't list missing help, $!"; |
e75138ab RS |
1129 | while ( <FH> ) { |
1130 | chop; | |
1131 | my ($cmd, $flag) = split; | |
fbad6e79 | 1132 | err("$cmd has no help for -$flag"); |
e75138ab RS |
1133 | } |
1134 | close FH; | |
1135 | ||
fbad6e79 | 1136 | exit $status; |
71a8b855 | 1137 | } |
9e183d22 | 1138 | |
8270c479 RL |
1139 | # Populate %state |
1140 | loadnum('util/libcrypto.num', 'crypto'); | |
1141 | loadnum('util/libssl.num', 'ssl'); | |
1142 | loadnum('util/other.syms', 'other'); | |
1143 | loadnum('util/other-internal.syms'); | |
1144 | if ( $opt_o ) { | |
1145 | loadmissing('util/missingmacro111.txt', 'crypto'); | |
1146 | loadmissing('util/missingcrypto111.txt', 'crypto'); | |
1147 | loadmissing('util/missingssl111.txt', 'ssl'); | |
e3ce33b3 | 1148 | } elsif ( !$opt_u ) { |
8270c479 RL |
1149 | loadmissing('util/missingmacro.txt', 'crypto'); |
1150 | loadmissing('util/missingcrypto.txt', 'crypto'); | |
1151 | loadmissing('util/missingssl.txt', 'ssl'); | |
1152 | loadmissing('util/missingcrypto-internal.txt'); | |
1153 | loadmissing('util/missingssl-internal.txt'); | |
1154 | } | |
1155 | ||
1156 | if ( $opt_n || $opt_l || $opt_u || $opt_v ) { | |
1157 | my @files_to_read = ( $opt_n && @ARGV ) ? @ARGV : files(TAGS => 'manual'); | |
1158 | ||
1159 | foreach (@files_to_read) { | |
1160 | my %podinfo = extract_pod_info($_, { debug => $debug }); | |
1161 | ||
1162 | collectnames(%podinfo) | |
1163 | if ( $opt_l || $opt_u || $opt_v ); | |
1164 | ||
1165 | check(%podinfo) | |
1166 | if ( $opt_n ); | |
9e183d22 | 1167 | } |
b4350db5 RL |
1168 | } |
1169 | ||
1170 | if ( $opt_l ) { | |
9e183d22 RS |
1171 | checklinks(); |
1172 | } | |
1173 | ||
e75138ab | 1174 | if ( $opt_n ) { |
a6dd3a3a RS |
1175 | # If not given args, check that all man1 commands are named properly. |
1176 | if ( scalar @ARGV == 0 ) { | |
1624ebdb | 1177 | foreach ( files(TAGS => [ 'public_manual', 'man1' ]) ) { |
6b480ee3 DDO |
1178 | next if /openssl\.pod/ |
1179 | || /CA\.pl/ || /tsget\.pod/; # these commands are special cases | |
a6dd3a3a RS |
1180 | err("$_ doesn't start with openssl-") unless /openssl-/; |
1181 | } | |
1182 | } | |
e75138ab RS |
1183 | } |
1184 | ||
8270c479 RL |
1185 | checkstate(); |
1186 | ||
b5283535 | 1187 | if ( $opt_u || $opt_v) { |
8270c479 RL |
1188 | printem('crypto'); |
1189 | printem('ssl'); | |
fbad6e79 | 1190 | checkmacros(); |
1bc74519 | 1191 | } |
05ea606a | 1192 | |
fbad6e79 | 1193 | exit $status; |