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