]>
Commit | Line | Data |
---|---|---|
1bc74519 | 1 | #! /usr/bin/env perl |
95f92d57 | 2 | # Copyright 2002-2019 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 | |
1bc74519 RS |
14 | use Pod::Checker; |
15 | use File::Find; | |
169a8e39 | 16 | use File::Basename; |
71a8b855 | 17 | use File::Spec::Functions; |
35ea640a | 18 | use Getopt::Std; |
71a8b855 RS |
19 | use lib catdir(dirname($0), "perl"); |
20 | use OpenSSL::Util::Pod; | |
35ea640a | 21 | |
a397aca4 RS |
22 | # Set to 1 for debug output |
23 | my $debug = 0; | |
705128b0 | 24 | |
71a8b855 | 25 | # Options. |
8d50b9c1 | 26 | our($opt_d); |
b5283535 MC |
27 | our($opt_e); |
28 | our($opt_s); | |
a03749a8 | 29 | our($opt_o); |
71a8b855 | 30 | our($opt_h); |
9e183d22 | 31 | our($opt_l); |
8d50b9c1 | 32 | our($opt_n); |
274d1bee | 33 | our($opt_p); |
8d50b9c1 | 34 | our($opt_u); |
b5283535 | 35 | our($opt_v); |
e75138ab | 36 | our($opt_c); |
71a8b855 | 37 | |
185ec4be | 38 | # Print usage message and exit. |
fbad6e79 | 39 | sub help { |
71a8b855 RS |
40 | print <<EOF; |
41 | Find small errors (nits) in documentation. Options: | |
185ec4be | 42 | -c List undocumented commands and options |
8d50b9c1 | 43 | -d Detailed list of undocumented (implies -u) |
b5283535 | 44 | -e Detailed list of new undocumented (implies -v) |
185ec4be | 45 | -h Print this help message |
9e183d22 | 46 | -l Print bogus links |
71a8b855 | 47 | -n Print nits in POD pages |
185ec4be | 48 | -o Causes -e/-v to count symbols added since 1.1.1 as new (implies -v) |
ee4afacd | 49 | -u Count undocumented functions |
b5283535 | 50 | -v Count new undocumented functions |
71a8b855 RS |
51 | EOF |
52 | exit; | |
53 | } | |
1bc74519 | 54 | |
185ec4be RS |
55 | getopts('cdehlnouv'); |
56 | ||
57 | help() if $opt_h; | |
58 | $opt_u = 1 if $opt_d; | |
59 | $opt_v = 1 if $opt_o || $opt_e; | |
60 | die "Cannot use both -u and -v" | |
61 | if $opt_u && $opt_v; | |
62 | die "Cannot use both -d and -e" | |
63 | if $opt_d && $opt_e; | |
64 | ||
65 | # We only need to check c, l, n, u and v. | |
66 | # Options d, e, o imply one of the above. | |
67 | die "Need one of -[cdehlnouv] flags.\n" | |
68 | unless $opt_c or $opt_l or $opt_n or $opt_u or $opt_v; | |
69 | ||
70 | ||
05ea606a RS |
71 | my $temp = '/tmp/docnits.txt'; |
72 | my $OUT; | |
274d1bee | 73 | my %public; |
fbad6e79 | 74 | my $status = 0; |
05ea606a | 75 | |
a397aca4 RS |
76 | my %mandatory_sections = ( |
77 | '*' => [ 'NAME', 'DESCRIPTION', 'COPYRIGHT' ], | |
78 | 1 => [ 'SYNOPSIS', 'OPTIONS' ], | |
79 | 3 => [ 'SYNOPSIS', 'RETURN VALUES' ], | |
80 | 5 => [ ], | |
81 | 7 => [ ] | |
82 | ); | |
83 | ||
169a8e39 | 84 | |
fbad6e79 RS |
85 | # Print error message, set $status. |
86 | sub err { | |
87 | print join(" ", @_), "\n"; | |
88 | $status = 1 | |
89 | } | |
90 | ||
35ea640a | 91 | # Cross-check functions in the NAME and SYNOPSIS section. |
fbad6e79 | 92 | sub name_synopsis { |
35ea640a RS |
93 | my $id = shift; |
94 | my $filename = shift; | |
95 | my $contents = shift; | |
96 | ||
35ea640a RS |
97 | # Get NAME section and all words in it. |
98 | return unless $contents =~ /=head1 NAME(.*)=head1 SYNOPSIS/ms; | |
99 | my $tmp = $1; | |
100 | $tmp =~ tr/\n/ /; | |
fbad6e79 RS |
101 | err($id, "trailing comma before - in NAME") |
102 | if $tmp =~ /, *-/; | |
2bcb232e | 103 | $tmp =~ s/ -.*//g; |
fbad6e79 RS |
104 | err($id, "POD markup among the names in NAME") |
105 | if $tmp =~ /[<>]/; | |
2bcb232e | 106 | $tmp =~ s/ */ /g; |
fbad6e79 RS |
107 | err($id, "missing comma in NAME") |
108 | if $tmp =~ /[^,] /; | |
fbba5d11 RS |
109 | |
110 | my $dirname = dirname($filename); | |
f6800e37 | 111 | my $simplename = basename(basename($filename, ".in"), ".pod"); |
fbba5d11 RS |
112 | my $foundfilename = 0; |
113 | my %foundfilenames = (); | |
35ea640a | 114 | my %names; |
23ab880d RL |
115 | foreach my $n ( split ',', $tmp ) { |
116 | $n =~ s/^\s+//; | |
117 | $n =~ s/\s+$//; | |
fbad6e79 | 118 | err($id, "the name '$n' contains white-space") |
23ab880d | 119 | if $n =~ /\s/; |
35ea640a | 120 | $names{$n} = 1; |
fbba5d11 RS |
121 | $foundfilename++ if $n eq $simplename; |
122 | $foundfilenames{$n} = 1 | |
a397aca4 | 123 | if -f "$dirname/$n.pod" && $n ne $simplename; |
35ea640a | 124 | } |
a397aca4 | 125 | err($id, "the following exist as other .pod files:", |
fbad6e79 | 126 | sort keys %foundfilenames) |
fbba5d11 | 127 | if %foundfilenames; |
fbad6e79 | 128 | err($id, "$simplename (filename) missing from NAME section") |
fbba5d11 | 129 | unless $foundfilename; |
185ec4be RS |
130 | if ( $filename !~ /internal/ ) { |
131 | foreach my $n ( keys %names ) { | |
132 | err($id, "$n is not public") | |
133 | if !defined $public{$n}; | |
134 | } | |
1722496f | 135 | } |
35ea640a RS |
136 | |
137 | # Find all functions in SYNOPSIS | |
138 | return unless $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms; | |
139 | my $syn = $1; | |
140 | foreach my $line ( split /\n+/, $syn ) { | |
be80b21d | 141 | next unless $line =~ /^\s/; |
8162f6f5 | 142 | my $sym; |
c952780c | 143 | $line =~ s/STACK_OF\([^)]+\)/int/g; |
4460ad90 | 144 | $line =~ s/SPARSE_ARRAY_OF\([^)]+\)/int/g; |
c952780c | 145 | $line =~ s/__declspec\([^)]+\)//; |
121677b4 RS |
146 | if ( $line =~ /env (\S*)=/ ) { |
147 | # environment variable env NAME=... | |
148 | $sym = $1; | |
149 | } elsif ( $line =~ /typedef.*\(\*(\S+)\)\(.*/ ) { | |
0ed78e78 RL |
150 | # a callback function pointer: typedef ... (*NAME)(... |
151 | $sym = $1; | |
152 | } elsif ( $line =~ /typedef.* (\S+)\(.*/ ) { | |
153 | # a callback function signature: typedef ... NAME(... | |
121677b4 RS |
154 | $sym = $1; |
155 | } elsif ( $line =~ /typedef.* (\S+);/ ) { | |
156 | # a simple typedef: typedef ... NAME; | |
8162f6f5 | 157 | $sym = $1; |
5d583521 | 158 | } elsif ( $line =~ /enum (\S*) \{/ ) { |
d4ea9659 RS |
159 | # an enumeration: enum ... { |
160 | $sym = $1; | |
0695b193 | 161 | } elsif ( $line =~ /#(?:define|undef) ([A-Za-z0-9_]+)/ ) { |
8162f6f5 RS |
162 | $sym = $1; |
163 | } elsif ( $line =~ /([A-Za-z0-9_]+)\(/ ) { | |
164 | $sym = $1; | |
165 | } | |
166 | else { | |
167 | next; | |
168 | } | |
fbad6e79 | 169 | err($id, "$sym missing from NAME section") |
8162f6f5 RS |
170 | unless defined $names{$sym}; |
171 | $names{$sym} = 2; | |
aebb9aac RS |
172 | |
173 | # Do some sanity checks on the prototype. | |
fbad6e79 | 174 | err($id, "prototype missing spaces around commas: $line") |
aebb9aac | 175 | if ( $line =~ /[a-z0-9],[^ ]/ ); |
35ea640a RS |
176 | } |
177 | ||
178 | foreach my $n ( keys %names ) { | |
179 | next if $names{$n} == 2; | |
fbad6e79 | 180 | err($id, "$n missing from SYNOPSIS") |
35ea640a RS |
181 | } |
182 | } | |
183 | ||
39a117d1 | 184 | # Check if SECTION ($3) is located before BEFORE ($4) |
fbad6e79 | 185 | sub check_section_location { |
39a117d1 | 186 | my $id = shift; |
cc838ee2 | 187 | my $contents = shift; |
95f92d57 JL |
188 | my $section = shift; |
189 | my $before = shift; | |
cc838ee2 | 190 | |
485d3361 RS |
191 | return unless $contents =~ /=head1 $section/ |
192 | and $contents =~ /=head1 $before/; | |
fbad6e79 | 193 | err($id, "$section should appear before $before section") |
95f92d57 | 194 | if $contents =~ /=head1 $before.*=head1 $section/ms; |
cc838ee2 PY |
195 | } |
196 | ||
485d3361 RS |
197 | # Check if a =head1 is duplicated, or a =headX is duplicated within a |
198 | # =head1. Treats =head2 =head3 as equivalent -- it doesn't reset the head3 | |
199 | # sets if it finds a =head2 -- but that is good enough for now. Also check | |
200 | # for proper capitalization, trailing periods, etc. | |
fbad6e79 | 201 | sub check_head_style { |
485d3361 RS |
202 | my $id = shift; |
203 | my $contents = shift; | |
204 | my %head1; | |
205 | my %subheads; | |
206 | ||
207 | foreach my $line ( split /\n+/, $contents ) { | |
208 | next unless $line =~ /^=head/; | |
209 | if ( $line =~ /head1/ ) { | |
fbad6e79 | 210 | err($id, "duplicate section $line") |
485d3361 RS |
211 | if defined $head1{$line}; |
212 | $head1{$line} = 1; | |
213 | %subheads = (); | |
214 | } else { | |
fbad6e79 | 215 | err($id, "duplicate subsection $line") |
485d3361 RS |
216 | if defined $subheads{$line}; |
217 | $subheads{$line} = 1; | |
218 | } | |
fbad6e79 | 219 | err($id, "period in =head") |
485d3361 | 220 | if $line =~ /\.[^\w]/ or $line =~ /\.$/; |
fbad6e79 | 221 | err($id, "not all uppercase in =head1") |
485d3361 | 222 | if $line =~ /head1.*[a-z]/; |
fbad6e79 | 223 | err($id, "all uppercase in subhead") |
485d3361 RS |
224 | if $line =~ /head[234][ A-Z0-9]+$/; |
225 | } | |
226 | } | |
227 | ||
705128b0 RL |
228 | # Because we have options and symbols with extra markup, we need |
229 | # to take that into account, so we need a regexp that extracts | |
230 | # markup chunks, including recursive markup. | |
231 | # please read up on /(?R)/ in perlre(1) | |
232 | # (note: order is important, (?R) needs to come before .) | |
233 | # (note: non-greedy is important, or something like 'B<foo> and B<bar>' | |
234 | # will be captured as one item) | |
235 | my $markup_re = | |
236 | qr/( # Capture group | |
237 | [BIL]< # The start of what we recurse on | |
238 | (?:(?-1)|.)*? # recurse the whole regexp (refering to | |
239 | # the last opened capture group, i.e. the | |
240 | # start of this regexp), or pick next | |
241 | # character. Do NOT be greedy! | |
242 | > # The end of what we recurse on | |
243 | )/x; # (the x allows this sort of split up regexp) | |
244 | ||
245 | # Options must start with a dash, followed by a letter, possibly | |
246 | # followed by letters, digits, dashes and underscores, and the last | |
247 | # character must be a letter or a digit. | |
248 | # We do also accept the single -? or -n, where n is a digit | |
249 | my $option_re = | |
250 | qr/(?: | |
251 | \? # Single question mark | |
252 | | | |
253 | \d # Single digit | |
254 | | | |
255 | - # Single dash (--) | |
256 | | | |
257 | [[:alpha:]](?:[-_[:alnum:]]*?[[:alnum:]])? | |
258 | )/x; | |
259 | ||
260 | # Helper function to check if a given $thing is properly marked up | |
261 | # option. It returns one of these values: | |
a397aca4 RS |
262 | # undef if it's not an option |
263 | # "" if it's a malformed option | |
264 | # $unwrapped the option with the outermost B<> wrapping removed. | |
705128b0 RL |
265 | sub normalise_option { |
266 | my $id = shift; | |
267 | my $filename = shift; | |
268 | my $thing = shift; | |
269 | ||
270 | my $unwrapped = $thing; | |
271 | my $unmarked = $thing; | |
272 | ||
273 | # $unwrapped is the option with the outer B<> markup removed | |
274 | $unwrapped =~ s/^B<//; | |
275 | $unwrapped =~ s/>$//; | |
276 | # $unmarked is the option with *all* markup removed | |
277 | $unmarked =~ s/[BIL]<|>//msg; | |
278 | ||
279 | ||
280 | # If we found an option, check it, collect it | |
281 | if ( $unwrapped =~ /^\s*-/ ) { | |
282 | return $unwrapped # return option with outer B<> removed | |
283 | if $unmarked =~ /^-${option_re}$/; | |
284 | return ""; # Malformed option | |
285 | } | |
286 | return undef; # Something else | |
287 | } | |
288 | ||
289 | # Checks of command option (man1) formatting. The man1 checks are | |
290 | # restricted to the SYNOPSIS and OPTIONS sections, the rest is too | |
291 | # free form, we simply cannot be too strict there. | |
292 | ||
293 | sub option_check { | |
294 | my $id = shift; | |
295 | my $filename = shift; | |
296 | my $contents = shift; | |
297 | ||
298 | my $synopsis = ($contents =~ /=head1\s+SYNOPSIS(.*?)=head1/s, $1); | |
299 | ||
300 | # Some pages have more than one OPTIONS section, let's make sure | |
301 | # to get them all | |
302 | my $options = ''; | |
303 | while ( $contents =~ /=head1\s+[A-Z ]*?OPTIONS$(.*?)(?==head1)/msg ) { | |
304 | $options .= $1; | |
305 | } | |
306 | ||
307 | # Look for options with no or incorrect markup | |
308 | while ( $synopsis =~ | |
309 | /(?<![-<[:alnum:]])-(?:$markup_re|.)*(?![->[:alnum:]])/msg ) { | |
310 | err($id, "Malformed option [1] in SYNOPSIS: $&"); | |
311 | } | |
312 | ||
313 | while ( $synopsis =~ /$markup_re/msg ) { | |
314 | my $found = $&; | |
315 | print STDERR "$id:DEBUG[option_check] SYNOPSIS: found $found\n" | |
316 | if $debug; | |
317 | my $option_uw = normalise_option($id, $filename, $found); | |
318 | err($id, "Malformed option [2] in SYNOPSIS: $found") | |
319 | if defined $option_uw && $option_uw eq ''; | |
320 | } | |
321 | ||
322 | # In OPTIONS, we look for =item paragraphs. | |
323 | # (?=^\s*$) detects an empty line. | |
324 | while ( $options =~ /=item\s+(.*?)(?=^\s*$)/msg ) { | |
325 | my $item = $&; | |
326 | ||
327 | while ( $item =~ /(\[\s*)?($markup_re)/msg ) { | |
328 | my $found = $2; | |
329 | print STDERR "$id:DEBUG[option_check] OPTIONS: found $&\n" | |
330 | if $debug; | |
331 | err($id, "Unexpected bracket in OPTIONS =item: $item") | |
332 | if ($1 // '') ne '' && $found =~ /^B<\s*-/; | |
333 | ||
334 | my $option_uw = normalise_option($id, $filename, $found); | |
335 | err($id, "Malformed option in OPTIONS: $found") | |
336 | if defined $option_uw && $option_uw eq ''; | |
337 | } | |
338 | } | |
339 | } | |
340 | ||
341 | # Normal symbol form | |
342 | my $symbol_re = qr/[[:alpha:]_][_[:alnum:]]*?/; | |
343 | ||
344 | # Checks of function name (man3) formatting. The man3 checks are | |
345 | # easier than the man1 checks, we only check the names followed by (), | |
346 | # and only the names that have POD markup. | |
705128b0 RL |
347 | sub functionname_check { |
348 | my $id = shift; | |
349 | my $filename = shift; | |
350 | my $contents = shift; | |
351 | ||
352 | while ( $contents =~ /($markup_re)\(\)/msg ) { | |
353 | print STDERR "$id:DEBUG[functionname_check] SYNOPSIS: found $&\n" | |
354 | if $debug; | |
355 | ||
356 | my $symbol = $1; | |
357 | my $unmarked = $symbol; | |
358 | $unmarked =~ s/[BIL]<|>//msg; | |
359 | ||
360 | err($id, "Malformed symbol: $symbol") | |
361 | unless $symbol =~ /^B<.*>$/ && $unmarked =~ /^${symbol_re}$/ | |
362 | } | |
363 | ||
364 | # We can't do the kind of collecting coolness that option_check() | |
365 | # does, because there are too many things that can't be found in | |
366 | # name repositories like the NAME sections, such as symbol names | |
367 | # with a variable part (typically marked up as B<foo_I<TYPE>_bar> | |
368 | } | |
369 | ||
60a7817c RS |
370 | # This is from http://man7.org/linux/man-pages/man7/man-pages.7.html |
371 | my %preferred_words = ( | |
372 | 'bitmask' => 'bit mask', | |
373 | 'builtin' => 'built-in', | |
374 | #'epoch' => 'Epoch', # handled specially, below | |
375 | 'file name' => 'filename', | |
376 | 'file system' => 'filesystem', | |
377 | 'host name' => 'hostname', | |
378 | 'i-node' => 'inode', | |
379 | 'lower case' => 'lowercase', | |
380 | 'lower-case' => 'lowercase', | |
381 | 'non-zero' => 'nonzero', | |
382 | 'path name' => 'pathname', | |
383 | 'pseudo-terminal' => 'pseudoterminal', | |
384 | 'reserved port' => 'privileged port', | |
385 | 'system port' => 'privileged port', | |
386 | 'realtime' => 'real-time', | |
387 | 'real time' => 'real-time', | |
388 | 'runtime' => 'run time', | |
389 | 'saved group ID'=> 'saved set-group-ID', | |
390 | 'saved set-GID' => 'saved set-group-ID', | |
391 | 'saved user ID' => 'saved set-user-ID', | |
392 | 'saved set-UID' => 'saved set-user-ID', | |
393 | 'set-GID' => 'set-group-ID', | |
394 | 'setgid' => 'set-group-ID', | |
395 | 'set-UID' => 'set-user-ID', | |
396 | 'setuid' => 'set-user-ID', | |
397 | 'super user' => 'superuser', | |
398 | 'super-user' => 'superuser', | |
399 | 'super block' => 'superblock', | |
400 | 'super-block' => 'superblock', | |
401 | 'time stamp' => 'timestamp', | |
402 | 'time zone' => 'timezone', | |
403 | 'upper case' => 'uppercase', | |
404 | 'upper-case' => 'uppercase', | |
405 | 'useable' => 'usable', | |
406 | 'userspace' => 'user space', | |
407 | 'user name' => 'username', | |
408 | 'zeroes' => 'zeros' | |
409 | ); | |
410 | ||
a397aca4 | 411 | # Search manpage for words that have a different preferred use. |
60a7817c RS |
412 | sub wording { |
413 | my $id = shift; | |
414 | my $contents = shift; | |
415 | ||
416 | foreach my $k ( keys %preferred_words ) { | |
9c0586d5 RS |
417 | # Sigh, trademark |
418 | next if $k eq 'file system' | |
419 | and $contents =~ /Microsoft Encrypted File System/; | |
60a7817c RS |
420 | err($id, "found '$k' should use '$preferred_words{$k}'") |
421 | if $contents =~ /\b\Q$k\E\b/i; | |
422 | } | |
423 | err($id, "found 'epoch' should use 'Epoch'") | |
424 | if $contents =~ /\bepoch\b/; | |
425 | } | |
426 | ||
a397aca4 | 427 | # Perform all sorts of nit/error checks on a manpage |
fbad6e79 | 428 | sub check { |
169a8e39 RL |
429 | my $filename = shift; |
430 | my $dirname = basename(dirname($filename)); | |
843666ff | 431 | |
1bc74519 RS |
432 | my $contents = ''; |
433 | { | |
434 | local $/ = undef; | |
169a8e39 | 435 | open POD, $filename or die "Couldn't open $filename, $!"; |
1bc74519 RS |
436 | $contents = <POD>; |
437 | close POD; | |
438 | } | |
843666ff RS |
439 | |
440 | my $id = "${filename}:1:"; | |
fbad6e79 | 441 | check_head_style($id, $contents); |
35ea640a | 442 | |
39a117d1 RS |
443 | # Check ordering of some sections in man3 |
444 | if ( $filename =~ m|man3/| ) { | |
fbad6e79 RS |
445 | check_section_location($id, $contents, "RETURN VALUES", "EXAMPLES"); |
446 | check_section_location($id, $contents, "SEE ALSO", "HISTORY"); | |
447 | check_section_location($id, $contents, "EXAMPLES", "SEE ALSO"); | |
39a117d1 RS |
448 | } |
449 | ||
bb82531f | 450 | unless ( $contents =~ /=for openssl generic/ ) { |
705128b0 RL |
451 | if ( $filename =~ m|man3/| ) { |
452 | name_synopsis($id, $filename, $contents); | |
453 | functionname_check($id, $filename, $contents); | |
454 | } elsif ( $filename =~ m|man1/| ) { | |
455 | option_check($id, $filename, $contents) | |
456 | } | |
457 | } | |
35ea640a | 458 | |
60a7817c RS |
459 | wording($id, $contents); |
460 | ||
fbad6e79 | 461 | err($id, "doesn't start with =pod") |
05ea606a | 462 | if $contents !~ /^=pod/; |
fbad6e79 | 463 | err($id, "doesn't end with =cut") |
05ea606a | 464 | if $contents !~ /=cut\n$/; |
fbad6e79 | 465 | err($id, "more than one cut line.") |
05ea606a | 466 | if $contents =~ /=cut.*=cut/ms; |
fbad6e79 | 467 | err($id, "EXAMPLE not EXAMPLES section.") |
cda77422 | 468 | if $contents =~ /=head1 EXAMPLE[^S]/; |
fbad6e79 | 469 | err($id, "WARNING not WARNINGS section.") |
5e0d9c86 | 470 | if $contents =~ /=head1 WARNING[^S]/; |
fbad6e79 | 471 | err($id, "missing copyright") |
05ea606a | 472 | if $contents !~ /Copyright .* The OpenSSL Project Authors/; |
fbad6e79 | 473 | err($id, "copyright not last") |
05ea606a | 474 | if $contents =~ /head1 COPYRIGHT.*=head/ms; |
fbad6e79 | 475 | err($id, "head2 in All uppercase") |
843666ff | 476 | if $contents =~ /head2\s+[A-Z ]+\n/; |
fbad6e79 | 477 | err($id, "extra space after head") |
35ea640a | 478 | if $contents =~ /=head\d\s\s+/; |
fbad6e79 | 479 | err($id, "period in NAME section") |
35ea640a | 480 | if $contents =~ /=head1 NAME.*\.\n.*=head1 SYNOPSIS/ms; |
fbad6e79 | 481 | err($id, "Duplicate $1 in L<>") |
5a3371e2 | 482 | if $contents =~ /L<([^>]*)\|([^>]*)>/ && $1 eq $2; |
fbad6e79 | 483 | err($id, "Bad =over $1") |
2f61bc2e | 484 | if $contents =~ /=over([^ ][^24])/; |
fbad6e79 | 485 | err($id, "Possible version style issue") |
e90fc053 | 486 | if $contents =~ /OpenSSL version [019]/; |
843666ff | 487 | |
bb82531f | 488 | if ( $contents !~ /=for openssl multiple includes/ ) { |
a95d7574 RS |
489 | # Look for multiple consecutive openssl #include lines |
490 | # (non-consecutive lines are okay; see man3/MD5.pod). | |
843666ff RS |
491 | if ( $contents =~ /=head1 SYNOPSIS(.*)=head1 DESCRIPTION/ms ) { |
492 | my $count = 0; | |
493 | foreach my $line ( split /\n+/, $1 ) { | |
494 | if ( $line =~ m@include <openssl/@ ) { | |
fbad6e79 RS |
495 | err($id, "has multiple includes") |
496 | if ++$count == 2; | |
843666ff RS |
497 | } else { |
498 | $count = 0; | |
499 | } | |
500 | } | |
501 | } | |
502 | } | |
05ea606a | 503 | |
35ea640a RS |
504 | open my $OUT, '>', $temp |
505 | or die "Can't open $temp, $!"; | |
169a8e39 | 506 | podchecker($filename, $OUT); |
35ea640a RS |
507 | close $OUT; |
508 | open $OUT, '<', $temp | |
509 | or die "Can't read $temp, $!"; | |
510 | while ( <$OUT> ) { | |
511 | next if /\(section\) in.*deprecated/; | |
512 | print; | |
513 | } | |
514 | close $OUT; | |
515 | unlink $temp || warn "Can't remove $temp, $!"; | |
a95d7574 RS |
516 | |
517 | # Find what section this page is in; assume 3. | |
518 | my $section = 3; | |
519 | $section = $1 if $dirname =~ /man([1-9])/; | |
520 | ||
a397aca4 | 521 | foreach ( (@{$mandatory_sections{'*'}}, @{$mandatory_sections{$section}}) ) { |
fbad6e79 | 522 | err($id, "missing $_ head1 section") |
a95d7574 RS |
523 | if $contents !~ /^=head1\s+${_}\s*$/m; |
524 | } | |
05ea606a | 525 | } |
1bc74519 | 526 | |
a397aca4 | 527 | # Parse libcrypto.num, etc., and return sorted list of what's there. |
fbad6e79 | 528 | sub parsenum { |
71a8b855 RS |
529 | my $file = shift; |
530 | my @apis; | |
531 | ||
532 | open my $IN, '<', $file | |
533 | or die "Can't open $file, $!, stopped"; | |
534 | ||
535 | while ( <$IN> ) { | |
274d1bee | 536 | next if /^#/; |
71a8b855 | 537 | next if /\bNOEXIST\b/; |
1722496f RS |
538 | my @fields = split(); |
539 | die "Malformed line $_" | |
540 | if scalar @fields != 2 && scalar @fields != 4; | |
541 | push @apis, $fields[0]; | |
71a8b855 RS |
542 | } |
543 | ||
544 | close $IN; | |
545 | ||
71a8b855 RS |
546 | return sort @apis; |
547 | } | |
548 | ||
a397aca4 RS |
549 | # Parse all the manpages, getting return map of what they document |
550 | # (by looking at their NAME sections). | |
23ab880d | 551 | sub getdocced |
71a8b855 RS |
552 | { |
553 | my $dir = shift; | |
554 | my %return; | |
a397aca4 | 555 | my %dups; |
71a8b855 | 556 | |
a397aca4 | 557 | foreach my $pod ( glob("$dir/*.pod") ) { |
71a8b855 RS |
558 | my %podinfo = extract_pod_info($pod); |
559 | foreach my $n ( @{$podinfo{names}} ) { | |
560 | $return{$n} = $pod; | |
185ec4be | 561 | err("# Duplicate $n in $pod and $dups{$n}") |
71a8b855 RS |
562 | if defined $dups{$n} && $dups{$n} ne $pod; |
563 | $dups{$n} = $pod; | |
564 | } | |
565 | } | |
566 | ||
567 | return %return; | |
568 | } | |
569 | ||
a397aca4 | 570 | # Map of documented functions; function => manpage |
71a8b855 | 571 | my %docced; |
a397aca4 RS |
572 | # Map of links in each POD file; filename => [ "foo(1)", "bar(3)", ... ] |
573 | my %link_map = (); | |
574 | # Map of names in each POD file; "name(s)" => filename | |
575 | my %name_map = (); | |
71a8b855 | 576 | |
a397aca4 | 577 | # Load file of symbol names that we know aren't documented. |
b5283535 MC |
578 | sub loadmissing($) |
579 | { | |
580 | my $missingfile = shift; | |
581 | my @missing; | |
582 | ||
583 | open FH, $missingfile | |
584 | || die "Can't open $missingfile"; | |
585 | while ( <FH> ) { | |
586 | chomp; | |
587 | next if /^#/; | |
588 | push @missing, $_; | |
589 | } | |
590 | close FH; | |
591 | ||
592 | return @missing; | |
593 | } | |
594 | ||
a397aca4 RS |
595 | # Check for undocumented macros; ignore those in the "missing" file |
596 | # and do simple check for #define in our header files. | |
fbad6e79 | 597 | sub checkmacros { |
9a2dfc0f | 598 | my $count = 0; |
ee4afacd | 599 | my %seen; |
a03749a8 | 600 | my @missing; |
9a2dfc0f | 601 | |
a397aca4 | 602 | if ( $opt_o ) { |
a03749a8 | 603 | @missing = loadmissing('util/missingmacro111.txt'); |
a397aca4 | 604 | } elsif ( $opt_v ) { |
a03749a8 MC |
605 | @missing = loadmissing('util/missingmacro.txt'); |
606 | } | |
b5283535 | 607 | |
9a2dfc0f RS |
608 | foreach my $f ( glob('include/openssl/*.h') ) { |
609 | # Skip some internals we don't want to document yet. | |
610 | next if $f eq 'include/openssl/asn1.h'; | |
611 | next if $f eq 'include/openssl/asn1t.h'; | |
612 | next if $f eq 'include/openssl/err.h'; | |
613 | open(IN, $f) || die "Can't open $f, $!"; | |
614 | while ( <IN> ) { | |
615 | next unless /^#\s*define\s*(\S+)\(/; | |
616 | my $macro = $1; | |
ee4afacd | 617 | next if $docced{$macro} || defined $seen{$macro}; |
9a2dfc0f RS |
618 | next if $macro =~ /i2d_/ |
619 | || $macro =~ /d2i_/ | |
620 | || $macro =~ /DEPRECATEDIN/ | |
621 | || $macro =~ /IMPLEMENT_/ | |
622 | || $macro =~ /DECLARE_/; | |
b5283535 MC |
623 | |
624 | # Skip macros known to be missing | |
625 | next if $opt_v && grep( /^$macro$/, @missing); | |
626 | ||
185ec4be | 627 | err("$f:", "macro $macro undocumented") |
fbad6e79 | 628 | if $opt_d || $opt_e; |
9a2dfc0f | 629 | $count++; |
ee4afacd | 630 | $seen{$macro} = 1; |
9a2dfc0f RS |
631 | } |
632 | close(IN); | |
633 | } | |
185ec4be RS |
634 | err("# $count macros undocumented (count is approximate)") |
635 | if $count > 0; | |
9a2dfc0f RS |
636 | } |
637 | ||
a397aca4 RS |
638 | # Find out what is undocumented (filtering out the known missing ones) |
639 | # and display them. | |
fbad6e79 | 640 | sub printem { |
71a8b855 RS |
641 | my $libname = shift; |
642 | my $numfile = shift; | |
b5283535 | 643 | my $missingfile = shift; |
71a8b855 | 644 | my $count = 0; |
ee4afacd | 645 | my %seen; |
71a8b855 | 646 | |
a397aca4 | 647 | my @missing = loadmissing($missingfile) if ( $opt_v ); |
b5283535 | 648 | |
fbad6e79 | 649 | foreach my $func ( parsenum($numfile) ) { |
ee4afacd | 650 | next if $docced{$func} || defined $seen{$func}; |
71a8b855 RS |
651 | |
652 | # Skip ASN1 utilities | |
653 | next if $func =~ /^ASN1_/; | |
654 | ||
b5283535 MC |
655 | # Skip functions known to be missing |
656 | next if $opt_v && grep( /^$func$/, @missing); | |
657 | ||
185ec4be | 658 | err("$libname:", "function $func undocumented") |
fbad6e79 | 659 | if $opt_d || $opt_e; |
71a8b855 | 660 | $count++; |
ee4afacd | 661 | $seen{$func} = 1; |
71a8b855 | 662 | } |
185ec4be RS |
663 | err("# $count in $numfile are not documented") |
664 | if $count > 0; | |
71a8b855 RS |
665 | } |
666 | ||
a397aca4 | 667 | # Collect all the names in a manpage. |
9e183d22 RS |
668 | sub collectnames { |
669 | my $filename = shift; | |
670 | $filename =~ m|man(\d)/|; | |
671 | my $section = $1; | |
a397aca4 | 672 | my $simplename = basename($filename, ".pod"); |
9e183d22 RS |
673 | my $id = "${filename}:1:"; |
674 | ||
675 | my $contents = ''; | |
676 | { | |
677 | local $/ = undef; | |
678 | open POD, $filename or die "Couldn't open $filename, $!"; | |
679 | $contents = <POD>; | |
680 | close POD; | |
681 | } | |
682 | ||
683 | $contents =~ /=head1 NAME([^=]*)=head1 /ms; | |
684 | my $tmp = $1; | |
a397aca4 | 685 | unless ( defined $tmp ) { |
fbad6e79 | 686 | err($id, "weird name section"); |
9e183d22 RS |
687 | return; |
688 | } | |
689 | $tmp =~ tr/\n/ /; | |
f6800e37 | 690 | $tmp =~ s/ -.*//g; |
9e183d22 | 691 | |
f6800e37 RL |
692 | my @names = |
693 | map { s|/|-|g; $_ } # Treat slash as dash | |
694 | map { s/^\s+//g; s/\s+$//g; $_ } # Trim prefix and suffix blanks | |
695 | split(/,/, $tmp); | |
a397aca4 | 696 | unless ( grep { $simplename eq $_ } @names ) { |
fbad6e79 | 697 | err($id, "missing $simplename"); |
9e183d22 RS |
698 | push @names, $simplename; |
699 | } | |
700 | foreach my $name (@names) { | |
701 | next if $name eq ""; | |
a397aca4 | 702 | if ( $name =~ /\s/ ) { |
fbad6e79 | 703 | err($id, "'$name' contains white space") |
23ab880d | 704 | } |
9e183d22 | 705 | my $name_sec = "$name($section)"; |
a397aca4 RS |
706 | if ( !exists $name_map{$name_sec} ) { |
707 | $name_map{$name_sec} = $filename; | |
708 | } elsif ( $filename eq $name_map{$name_sec} ) { | |
fbad6e79 | 709 | err($id, "$name_sec repeated in NAME section of", |
a397aca4 | 710 | $name_map{$name_sec}); |
f6800e37 | 711 | } else { |
fbad6e79 | 712 | err($id, "$name_sec also in NAME section of", |
a397aca4 | 713 | $name_map{$name_sec}); |
9e183d22 RS |
714 | } |
715 | } | |
716 | ||
717 | my @foreign_names = | |
718 | map { map { s/\s+//g; $_ } split(/,/, $_) } | |
719 | $contents =~ /=for\s+comment\s+foreign\s+manuals:\s*(.*)\n\n/; | |
a397aca4 RS |
720 | foreach ( @foreign_names ) { |
721 | $name_map{$_} = undef; # It still exists! | |
9e183d22 RS |
722 | } |
723 | ||
724 | my @links = $contents =~ /L< | |
725 | # if the link is of the form L<something|name(s)>, | |
726 | # then remove 'something'. Note that 'something' | |
727 | # may contain POD codes as well... | |
728 | (?:(?:[^\|]|<[^>]*>)*\|)? | |
46f4e1be | 729 | # we're only interested in references that have |
9e183d22 RS |
730 | # a one digit section number |
731 | ([^\/>\(]+\(\d\)) | |
732 | /gx; | |
a397aca4 | 733 | $link_map{$filename} = [ @links ]; |
9e183d22 RS |
734 | } |
735 | ||
a397aca4 | 736 | # Look for L<> ("link") references that point to files that do not exist. |
9e183d22 | 737 | sub checklinks { |
a397aca4 RS |
738 | foreach my $filename (sort keys %link_map) { |
739 | foreach my $link (@{$link_map{$filename}}) { | |
fbad6e79 | 740 | err("${filename}:1:", "reference to non-existing $link") |
a397aca4 | 741 | unless exists $name_map{$link}; |
9e183d22 RS |
742 | } |
743 | } | |
744 | } | |
745 | ||
185ec4be | 746 | # Load the public symbol/macro names |
fbad6e79 RS |
747 | sub publicize { |
748 | foreach my $name ( parsenum('util/libcrypto.num') ) { | |
274d1bee RS |
749 | $public{$name} = 1; |
750 | } | |
fbad6e79 | 751 | foreach my $name ( parsenum('util/libssl.num') ) { |
274d1bee RS |
752 | $public{$name} = 1; |
753 | } | |
185ec4be | 754 | foreach my $name ( parsenum('util/other.syms') ) { |
274d1bee RS |
755 | $public{$name} = 1; |
756 | } | |
757 | } | |
758 | ||
a397aca4 RS |
759 | # Cipher/digests to skip if they show up as "not implemented" |
760 | # because they are, via the "-*" construct. | |
e75138ab RS |
761 | my %skips = ( |
762 | 'aes128' => 1, | |
763 | 'aes192' => 1, | |
764 | 'aes256' => 1, | |
765 | 'aria128' => 1, | |
766 | 'aria192' => 1, | |
767 | 'aria256' => 1, | |
768 | 'camellia128' => 1, | |
769 | 'camellia192' => 1, | |
770 | 'camellia256' => 1, | |
771 | 'des' => 1, | |
772 | 'des3' => 1, | |
773 | 'idea' => 1, | |
1738c0ce RS |
774 | 'cipher' => 1, |
775 | 'digest' => 1, | |
e75138ab RS |
776 | ); |
777 | ||
a397aca4 | 778 | # Check the flags of a command and see if everything is in the manpage |
fbad6e79 | 779 | sub checkflags { |
e75138ab | 780 | my $cmd = shift; |
bc5a8091 | 781 | my $doc = shift; |
e75138ab RS |
782 | my %cmdopts; |
783 | my %docopts; | |
1738c0ce | 784 | my %localskips; |
e75138ab RS |
785 | |
786 | # Get the list of options in the command. | |
787 | open CFH, "./apps/openssl list --options $cmd|" | |
788 | || die "Can list options for $cmd, $!"; | |
789 | while ( <CFH> ) { | |
790 | chop; | |
791 | s/ .$//; | |
792 | $cmdopts{$_} = 1; | |
793 | } | |
794 | close CFH; | |
795 | ||
796 | # Get the list of flags from the synopsis | |
bc5a8091 RS |
797 | open CFH, "<$doc" |
798 | || die "Can't open $doc, $!"; | |
e75138ab RS |
799 | while ( <CFH> ) { |
800 | chop; | |
801 | last if /DESCRIPTION/; | |
9f3c076b | 802 | if ( /=for openssl ifdef (.*)/ ) { |
1738c0ce RS |
803 | foreach my $f ( split / /, $1 ) { |
804 | $localskips{$f} = 1; | |
805 | } | |
806 | next; | |
807 | } | |
e75138ab | 808 | next unless /\[B<-([^ >]+)/; |
1738c0ce RS |
809 | my $opt = $1; |
810 | $opt = $1 if $opt =~ /I<(.*)/; | |
e75138ab RS |
811 | $docopts{$1} = 1; |
812 | } | |
813 | close CFH; | |
814 | ||
815 | # See what's in the command not the manpage. | |
a397aca4 RS |
816 | my @undocced = sort grep { !defined $docopts{$_} } keys %cmdopts; |
817 | foreach ( @undocced ) { | |
818 | next if /-/; # Skip the -- end-of-flags marker | |
819 | err("$doc: undocumented option -$_"); | |
e75138ab RS |
820 | } |
821 | ||
822 | # See what's in the command not the manpage. | |
a397aca4 RS |
823 | my @unimpl = sort grep { !defined $cmdopts{$_} } keys %docopts; |
824 | foreach ( @unimpl ) { | |
825 | next if defined $skips{$_} || defined $localskips{$_}; | |
826 | err("$cmd documented but not implemented -$_"); | |
e75138ab | 827 | } |
e75138ab RS |
828 | } |
829 | ||
a397aca4 RS |
830 | ## |
831 | ## MAIN() | |
832 | ## Do the work requested by the various getopt flags. | |
833 | ## The flags are parsed in alphabetical order, just because we have | |
834 | ## to have *some way* of listing them. | |
835 | ## | |
836 | ||
e75138ab | 837 | if ( $opt_c ) { |
e75138ab | 838 | my @commands = (); |
3dfda1a6 | 839 | |
e75138ab RS |
840 | # Get list of commands. |
841 | open FH, "./apps/openssl list -1 -commands|" | |
842 | || die "Can't list commands, $!"; | |
843 | while ( <FH> ) { | |
844 | chop; | |
845 | push @commands, $_; | |
846 | } | |
847 | close FH; | |
848 | ||
849 | # See if each has a manpage. | |
bc5a8091 RS |
850 | foreach my $cmd ( @commands ) { |
851 | next if $cmd eq 'help' || $cmd eq 'exit'; | |
852 | my $doc = "doc/man1/$cmd.pod"; | |
853 | $doc = "doc/man1/openssl-$cmd.pod" if -f "doc/man1/openssl-$cmd.pod"; | |
854 | if ( ! -f "$doc" ) { | |
fbad6e79 | 855 | err("$doc does not exist"); |
e75138ab | 856 | } else { |
fbad6e79 | 857 | checkflags($cmd, $doc); |
e75138ab | 858 | } |
71a8b855 | 859 | } |
e75138ab RS |
860 | |
861 | # See what help is missing. | |
862 | open FH, "./apps/openssl list --missing-help |" | |
863 | || die "Can't list missing help, $!"; | |
864 | while ( <FH> ) { | |
865 | chop; | |
866 | my ($cmd, $flag) = split; | |
fbad6e79 | 867 | err("$cmd has no help for -$flag"); |
e75138ab RS |
868 | } |
869 | close FH; | |
870 | ||
fbad6e79 | 871 | exit $status; |
71a8b855 | 872 | } |
9e183d22 RS |
873 | |
874 | if ( $opt_l ) { | |
a397aca4 | 875 | foreach ( @ARGV ? @ARGV : glob('doc/*/*.pod doc/internal/*/*.pod') ) { |
9e183d22 RS |
876 | collectnames($_); |
877 | } | |
878 | checklinks(); | |
879 | } | |
880 | ||
e75138ab | 881 | if ( $opt_n ) { |
185ec4be | 882 | publicize(); |
a397aca4 | 883 | foreach ( @ARGV ? @ARGV : glob('doc/*/*.pod doc/internal/*/*.pod') ) { |
185ec4be | 884 | check($_); |
23ab880d | 885 | } |
a6dd3a3a RS |
886 | |
887 | # If not given args, check that all man1 commands are named properly. | |
888 | if ( scalar @ARGV == 0 ) { | |
889 | foreach (glob('doc/man1/*.pod')) { | |
6f02932e | 890 | next if /CA.pl/ || /openssl\.pod/ || /tsget\.pod/; |
a6dd3a3a RS |
891 | err("$_ doesn't start with openssl-") unless /openssl-/; |
892 | } | |
893 | } | |
e75138ab RS |
894 | } |
895 | ||
b5283535 | 896 | if ( $opt_u || $opt_v) { |
23ab880d | 897 | my %temp = getdocced('doc/man3'); |
71a8b855 RS |
898 | foreach ( keys %temp ) { |
899 | $docced{$_} = $temp{$_}; | |
900 | } | |
a397aca4 | 901 | if ( $opt_o ) { |
fbad6e79 RS |
902 | printem('crypto', 'util/libcrypto.num', 'util/missingcrypto111.txt'); |
903 | printem('ssl', 'util/libssl.num', 'util/missingssl111.txt'); | |
a03749a8 | 904 | } else { |
fbad6e79 RS |
905 | printem('crypto', 'util/libcrypto.num', 'util/missingcrypto.txt'); |
906 | printem('ssl', 'util/libssl.num', 'util/missingssl.txt'); | |
a03749a8 | 907 | } |
fbad6e79 | 908 | checkmacros(); |
1bc74519 | 909 | } |
05ea606a | 910 | |
fbad6e79 | 911 | exit $status; |