find-doc-nits 40 KB

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