find-doc-nits 40 KB

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