test1173.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  10. #
  11. # This software is licensed as described in the file COPYING, which
  12. # you should have received as part of this distribution. The terms
  13. # are also available at https://curl.se/docs/copyright.html.
  14. #
  15. # You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. # copies of the Software, and permit persons to whom the Software is
  17. # furnished to do so, under the terms of the COPYING file.
  18. #
  19. # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. # KIND, either express or implied.
  21. #
  22. # SPDX-License-Identifier: curl
  23. #
  24. ###########################################################################
  25. #
  26. # Scan man page(s) and detect some simple and yet common formatting mistakes.
  27. #
  28. # Output all deviances to stderr.
  29. use strict;
  30. use warnings;
  31. use File::Basename;
  32. # get the file name first
  33. my $symbolsinversions=shift @ARGV;
  34. # we may get the dir roots pointed out
  35. my @manpages=@ARGV;
  36. my $errors = 0;
  37. my %docsdirs;
  38. my %optblessed;
  39. my %funcblessed;
  40. my @optorder = (
  41. 'NAME',
  42. 'SYNOPSIS',
  43. 'DESCRIPTION',
  44. #'DEFAULT', # CURLINFO_ has no default
  45. 'PROTOCOLS',
  46. 'EXAMPLE',
  47. 'AVAILABILITY',
  48. 'RETURN VALUE',
  49. 'SEE ALSO'
  50. );
  51. my @funcorder = (
  52. 'NAME',
  53. 'SYNOPSIS',
  54. 'DESCRIPTION',
  55. 'EXAMPLE',
  56. 'AVAILABILITY',
  57. 'RETURN VALUE',
  58. 'SEE ALSO'
  59. );
  60. my %shline; # section => line number
  61. my %symbol;
  62. # some CURLINFO_ symbols are not actual options for curl_easy_getinfo,
  63. # mark them as "deprecated" to hide them from link-warnings
  64. my %deprecated = (
  65. CURLINFO_TEXT => 1,
  66. CURLINFO_HEADER_IN => 1,
  67. CURLINFO_HEADER_OUT => 1,
  68. CURLINFO_DATA_IN => 1,
  69. CURLINFO_DATA_OUT => 1,
  70. CURLINFO_SSL_DATA_IN => 1,
  71. CURLINFO_SSL_DATA_OUT => 1,
  72. );
  73. sub allsymbols {
  74. open(my $f, "<", "$symbolsinversions") ||
  75. die "$symbolsinversions: $|";
  76. while(<$f>) {
  77. if($_ =~ /^([^ ]*) +(.*)/) {
  78. my ($name, $info) = ($1, $2);
  79. $symbol{$name}=$name;
  80. if($info =~ /([0-9.]+) +([0-9.]+)/) {
  81. $deprecated{$name}=$info;
  82. }
  83. }
  84. }
  85. close($f);
  86. }
  87. my %ref = (
  88. 'curl.1' => 1
  89. );
  90. sub checkref {
  91. my ($f, $sec, $file, $line)=@_;
  92. my $present = 0;
  93. #print STDERR "check $f.$sec\n";
  94. if($ref{"$f.$sec"}) {
  95. # present
  96. return;
  97. }
  98. foreach my $d (keys %docsdirs) {
  99. if( -f "$d/$f.$sec") {
  100. $present = 1;
  101. $ref{"$f.$sec"}=1;
  102. last;
  103. }
  104. }
  105. if(!$present) {
  106. print STDERR "$file:$line broken reference to $f($sec)\n";
  107. $errors++;
  108. }
  109. }
  110. sub scanmanpage {
  111. my ($file) = @_;
  112. my $reqex = 0;
  113. my $inseealso = 0;
  114. my $inex = 0;
  115. my $insynop = 0;
  116. my $exsize = 0;
  117. my $synopsize = 0;
  118. my $shc = 0;
  119. my $optpage = 0; # option or function
  120. my @sh;
  121. my $SH="";
  122. my @separators;
  123. my @sepline;
  124. open(my $m, "<", "$file") ||
  125. die "test1173.pl could not open $file";
  126. if($file =~ /[\/\\](CURL|curl_)[^\/\\]*.3/) {
  127. # This is a man page for libcurl. It requires an example!
  128. $reqex = 1;
  129. if($1 eq "CURL") {
  130. $optpage = 1;
  131. }
  132. }
  133. my $line = 1;
  134. while(<$m>) {
  135. chomp;
  136. if($_ =~ /^.so /) {
  137. # this man page is just a referral
  138. close($m);
  139. return;
  140. }
  141. if(($_ =~ /^\.SH SYNOPSIS/i) && ($reqex)) {
  142. # this is for libcurl man page SYNOPSIS checks
  143. $insynop = 1;
  144. $inex = 0;
  145. }
  146. elsif($_ =~ /^\.SH EXAMPLE/i) {
  147. $insynop = 0;
  148. $inex = 1;
  149. }
  150. elsif($_ =~ /^\.SH \"SEE ALSO\"/i) {
  151. $inseealso = 1;
  152. }
  153. elsif($_ =~ /^\.SH/i) {
  154. $insynop = 0;
  155. $inex = 0;
  156. }
  157. elsif($inseealso) {
  158. if($_ =~ /^\.BR (.*)/i) {
  159. my $f = $1;
  160. if($f =~ /^(lib|)curl/i) {
  161. $f =~ s/[\n\r]//g;
  162. if($f =~ s/([a-z_0-9-]*) \(([13])\)([, ]*)//i) {
  163. push @separators, $3;
  164. push @sepline, $line;
  165. checkref($1, $2, $file, $line);
  166. }
  167. if($f !~ /^ *$/) {
  168. print STDERR "$file:$line bad SEE ALSO format\n";
  169. $errors++;
  170. }
  171. }
  172. else {
  173. if($f =~ /.*(, *)\z/) {
  174. push @separators, $1;
  175. push @sepline, $line;
  176. }
  177. else {
  178. push @separators, " ";
  179. push @sepline, $line;
  180. }
  181. }
  182. }
  183. }
  184. elsif($inex) {
  185. $exsize++;
  186. if($_ =~ /[^\\]\\n/) {
  187. print STDERR "$file:$line '\\n' need to be '\\\\n'!\n";
  188. }
  189. }
  190. elsif($insynop) {
  191. $synopsize++;
  192. if(($synopsize == 1) && ($_ !~ /\.nf/)) {
  193. print STDERR "$file:$line:1:ERROR: be .nf for proper formatting\n";
  194. }
  195. }
  196. if($_ =~ /^\.SH ([^\r\n]*)/i) {
  197. my $n = $1;
  198. # remove enclosing quotes
  199. $n =~ s/\"(.*)\"\z/$1/;
  200. push @sh, $n;
  201. $shline{$n} = $line;
  202. $SH = $n;
  203. }
  204. if($_ =~ /^\'/) {
  205. print STDERR "$file:$line line starts with single quote!\n";
  206. $errors++;
  207. }
  208. if($_ =~ /\\f([BI])(.*)/) {
  209. my ($format, $rest) = ($1, $2);
  210. if($rest !~ /\\fP/) {
  211. print STDERR "$file:$line missing \\f${format} terminator!\n";
  212. $errors++;
  213. }
  214. }
  215. my $c = $_;
  216. while($c =~ s/\\f([BI])((lib|)curl[a-z_0-9-]*)\(([13])\)//i) {
  217. checkref($2, $4, $file, $line);
  218. }
  219. if(($_ =~ /\\f([BI])((libcurl|CURLOPT_|CURLSHOPT_|CURLINFO_|CURLMOPT_|curl_easy_|curl_multi_|curl_url|curl_mime|curl_global|curl_share)[a-zA-Z_0-9-]+)(.)/) &&
  220. ($4 ne "(")) {
  221. print STDERR "$file:$line curl ref to $2 without section\n";
  222. $errors++;
  223. }
  224. if($_ =~ /(.*)\\f([^BIP])/) {
  225. my ($pre, $format) = ($1, $2);
  226. if($pre !~ /\\\z/) {
  227. # only if there wasn't another backslash before the \f
  228. print STDERR "$file:$line suspicious \\f format!\n";
  229. $errors++;
  230. }
  231. }
  232. if(($SH =~ /^(DESCRIPTION|RETURN VALUE|AVAILABILITY)/i) &&
  233. ($_ =~ /(.*)((curl_multi|curl_easy|curl_url|curl_global|curl_url|curl_share)[a-zA-Z_0-9-]+)/) &&
  234. ($1 !~ /\\fI$/)) {
  235. print STDERR "$file:$line unrefed curl call: $2\n";
  236. $errors++;
  237. }
  238. if($optpage && $SH && ($SH !~ /^(SYNOPSIS|EXAMPLE|NAME|SEE ALSO)/i) &&
  239. ($_ =~ /(.*)(CURL(OPT_|MOPT_|INFO_|SHOPT_)[A-Z0-9_]*)/)) {
  240. # an option with its own man page, check that it is tagged
  241. # for linking
  242. my ($pref, $symbol) = ($1, $2);
  243. if($deprecated{$symbol}) {
  244. # let it be
  245. }
  246. elsif($pref !~ /\\fI\z/) {
  247. print STDERR "$file:$line option $symbol missing \\fI tagging\n";
  248. $errors++;
  249. }
  250. }
  251. if($_ =~ /[ \t]+$/) {
  252. print STDERR "$file:$line trailing whitespace\n";
  253. $errors++;
  254. }
  255. $line++;
  256. }
  257. close($m);
  258. if(@separators) {
  259. # all except the last one need comma
  260. for(0 .. $#separators - 1) {
  261. my $l = $_;
  262. my $sep = $separators[$l];
  263. if($sep ne ",") {
  264. printf STDERR "$file:%d: bad not-last SEE ALSO separator: '%s'\n",
  265. $sepline[$l], $sep;
  266. $errors++;
  267. }
  268. }
  269. # the last one should not do comma
  270. my $sep = $separators[$#separators];
  271. if($sep eq ",") {
  272. printf STDERR "$file:%d: superfluous comma separator\n",
  273. $sepline[$#separators];
  274. $errors++;
  275. }
  276. }
  277. if($reqex) {
  278. # only for libcurl options man-pages
  279. my $shcount = scalar(@sh); # before @sh gets shifted
  280. if($exsize < 2) {
  281. print STDERR "$file:$line missing EXAMPLE section\n";
  282. $errors++;
  283. }
  284. if($shcount < 3) {
  285. print STDERR "$file:$line too few man page sections!\n";
  286. $errors++;
  287. return;
  288. }
  289. my $got = "start";
  290. my $i = 0;
  291. my $shused = 1;
  292. my @shorig = @sh;
  293. my @order = $optpage ? @optorder : @funcorder;
  294. my $blessed = $optpage ? \%optblessed : \%funcblessed;
  295. while($got) {
  296. my $finesh;
  297. $got = shift(@sh);
  298. if($got) {
  299. if($$blessed{$got}) {
  300. $i = $$blessed{$got};
  301. $finesh = $got; # a mandatory one
  302. }
  303. }
  304. if($i && defined($finesh)) {
  305. # mandatory section
  306. if($i != $shused) {
  307. printf STDERR "$file:%u Got %s, when %s was expected\n",
  308. $shline{$finesh},
  309. $finesh,
  310. $order[$shused-1];
  311. $errors++;
  312. return;
  313. }
  314. $shused++;
  315. if($i == scalar(@order)) {
  316. # last mandatory one, exit
  317. last;
  318. }
  319. }
  320. }
  321. if($i != scalar(@order)) {
  322. printf STDERR "$file:$line missing mandatory section: %s\n",
  323. $order[$i];
  324. printf STDERR "$file:$line section found at index %u: '%s'\n",
  325. $i, $shorig[$i];
  326. printf STDERR " Found %u used sections\n", $shcount;
  327. $errors++;
  328. }
  329. }
  330. }
  331. allsymbols();
  332. if(!$symbol{'CURLALTSVC_H1'}) {
  333. print STDERR "didn't get the symbols-in-version!\n";
  334. exit;
  335. }
  336. my $ind = 1;
  337. for my $s (@optorder) {
  338. $optblessed{$s} = $ind++
  339. }
  340. $ind = 1;
  341. for my $s (@funcorder) {
  342. $funcblessed{$s} = $ind++
  343. }
  344. for my $m (@manpages) {
  345. $docsdirs{dirname($m)}++;
  346. }
  347. for my $m (@manpages) {
  348. scanmanpage($m);
  349. }
  350. print STDERR "ok\n" if(!$errors);
  351. exit $errors;