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