2
0

manpage-syntax.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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") || die "no such file: $file";
  125. if($file =~ /[\/\\](CURL|curl_)[^\/\\]*.3/) {
  126. # This is a man page for libcurl. It requires an example!
  127. $reqex = 1;
  128. if($1 eq "CURL") {
  129. $optpage = 1;
  130. }
  131. }
  132. my $line = 1;
  133. while(<$m>) {
  134. chomp;
  135. if($_ =~ /^.so /) {
  136. # this man page is just a referral
  137. close($m);
  138. return;
  139. }
  140. if(($_ =~ /^\.SH SYNOPSIS/i) && ($reqex)) {
  141. # this is for libcurl man page SYNOPSIS checks
  142. $insynop = 1;
  143. $inex = 0;
  144. }
  145. elsif($_ =~ /^\.SH EXAMPLE/i) {
  146. $insynop = 0;
  147. $inex = 1;
  148. }
  149. elsif($_ =~ /^\.SH \"SEE ALSO\"/i) {
  150. $inseealso = 1;
  151. }
  152. elsif($_ =~ /^\.SH/i) {
  153. $insynop = 0;
  154. $inex = 0;
  155. }
  156. elsif($inseealso) {
  157. if($_ =~ /^\.BR (.*)/i) {
  158. my $f = $1;
  159. if($f =~ /^(lib|)curl/i) {
  160. $f =~ s/[\n\r]//g;
  161. if($f =~ s/([a-z_0-9-]*) \(([13])\)([, ]*)//i) {
  162. push @separators, $3;
  163. push @sepline, $line;
  164. checkref($1, $2, $file, $line);
  165. }
  166. if($f !~ /^ *$/) {
  167. print STDERR "$file:$line bad SEE ALSO format\n";
  168. $errors++;
  169. }
  170. }
  171. else {
  172. if($f =~ /.*(, *)\z/) {
  173. push @separators, $1;
  174. push @sepline, $line;
  175. }
  176. else {
  177. push @separators, " ";
  178. push @sepline, $line;
  179. }
  180. }
  181. }
  182. }
  183. elsif($inex) {
  184. $exsize++;
  185. if($_ =~ /[^\\]\\n/) {
  186. print STDERR "$file:$line '\\n' need to be '\\\\n'!\n";
  187. }
  188. }
  189. elsif($insynop) {
  190. $synopsize++;
  191. if(($synopsize == 1) && ($_ !~ /\.nf/)) {
  192. print STDERR "$file:$line:1:ERROR: be .nf for proper formatting\n";
  193. }
  194. }
  195. if($_ =~ /^\.SH ([^\r\n]*)/i) {
  196. my $n = $1;
  197. # remove enclosing quotes
  198. $n =~ s/\"(.*)\"\z/$1/;
  199. push @sh, $n;
  200. $shline{$n} = $line;
  201. $SH = $n;
  202. }
  203. if($_ =~ /^\'/) {
  204. print STDERR "$file:$line line starts with single quote!\n";
  205. $errors++;
  206. }
  207. if($_ =~ /\\f([BI])(.*)/) {
  208. my ($format, $rest) = ($1, $2);
  209. if($rest !~ /\\fP/) {
  210. print STDERR "$file:$line missing \\f${format} terminator!\n";
  211. $errors++;
  212. }
  213. }
  214. my $c = $_;
  215. while($c =~ s/\\f([BI])((lib|)curl[a-z_0-9-]*)\(([13])\)//i) {
  216. checkref($2, $4, $file, $line);
  217. }
  218. 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-]+)(.)/) &&
  219. ($4 ne "(")) {
  220. print STDERR "$file:$line curl ref to $2 without section\n";
  221. $errors++;
  222. }
  223. if($_ =~ /(.*)\\f([^BIP])/) {
  224. my ($pre, $format) = ($1, $2);
  225. if($pre !~ /\\\z/) {
  226. # only if there wasn't another backslash before the \f
  227. print STDERR "$file:$line suspicious \\f format!\n";
  228. $errors++;
  229. }
  230. }
  231. if(($SH =~ /^(DESCRIPTION|RETURN VALUE|AVAILABILITY)/i) &&
  232. ($_ =~ /(.*)((curl_multi|curl_easy|curl_url|curl_global|curl_url|curl_share)[a-zA-Z_0-9-]+)/) &&
  233. ($1 !~ /\\fI$/)) {
  234. print STDERR "$file:$line unrefed curl call: $2\n";
  235. $errors++;
  236. }
  237. if($optpage && $SH && ($SH !~ /^(SYNOPSIS|EXAMPLE|NAME|SEE ALSO)/i) &&
  238. ($_ =~ /(.*)(CURL(OPT_|MOPT_|INFO_|SHOPT_)[A-Z0-9_]*)/)) {
  239. # an option with its own man page, check that it is tagged
  240. # for linking
  241. my ($pref, $symbol) = ($1, $2);
  242. if($deprecated{$symbol}) {
  243. # let it be
  244. }
  245. elsif($pref !~ /\\fI\z/) {
  246. print STDERR "$file:$line option $symbol missing \\fI tagging\n";
  247. $errors++;
  248. }
  249. }
  250. if($_ =~ /[ \t]+$/) {
  251. print STDERR "$file:$line trailing whitespace\n";
  252. $errors++;
  253. }
  254. $line++;
  255. }
  256. close($m);
  257. if(@separators) {
  258. # all except the last one need comma
  259. for(0 .. $#separators - 1) {
  260. my $l = $_;
  261. my $sep = $separators[$l];
  262. if($sep ne ",") {
  263. printf STDERR "$file:%d: bad not-last SEE ALSO separator: '%s'\n",
  264. $sepline[$l], $sep;
  265. $errors++;
  266. }
  267. }
  268. # the last one should not do comma
  269. my $sep = $separators[$#separators];
  270. if($sep eq ",") {
  271. printf STDERR "$file:%d: superfluous comma separator\n",
  272. $sepline[$#separators];
  273. $errors++;
  274. }
  275. }
  276. if($reqex) {
  277. # only for libcurl options man-pages
  278. my $shcount = scalar(@sh); # before @sh gets shifted
  279. if($exsize < 2) {
  280. print STDERR "$file:$line missing EXAMPLE section\n";
  281. $errors++;
  282. }
  283. if($shcount < 3) {
  284. print STDERR "$file:$line too few man page sections!\n";
  285. $errors++;
  286. return;
  287. }
  288. my $got = "start";
  289. my $i = 0;
  290. my $shused = 1;
  291. my @shorig = @sh;
  292. my @order = $optpage ? @optorder : @funcorder;
  293. my $blessed = $optpage ? \%optblessed : \%funcblessed;
  294. while($got) {
  295. my $finesh;
  296. $got = shift(@sh);
  297. if($got) {
  298. if($$blessed{$got}) {
  299. $i = $$blessed{$got};
  300. $finesh = $got; # a mandatory one
  301. }
  302. }
  303. if($i && defined($finesh)) {
  304. # mandatory section
  305. if($i != $shused) {
  306. printf STDERR "$file:%u Got %s, when %s was expected\n",
  307. $shline{$finesh},
  308. $finesh,
  309. $order[$shused-1];
  310. $errors++;
  311. return;
  312. }
  313. $shused++;
  314. if($i == scalar(@order)) {
  315. # last mandatory one, exit
  316. last;
  317. }
  318. }
  319. }
  320. if($i != scalar(@order)) {
  321. printf STDERR "$file:$line missing mandatory section: %s\n",
  322. $order[$i];
  323. printf STDERR "$file:$line section found at index %u: '%s'\n",
  324. $i, $shorig[$i];
  325. printf STDERR " Found %u used sections\n", $shcount;
  326. $errors++;
  327. }
  328. }
  329. }
  330. allsymbols();
  331. if(!$symbol{'CURLALTSVC_H1'}) {
  332. print STDERR "didn't get the symbols-in-version!\n";
  333. exit;
  334. }
  335. my $ind = 1;
  336. for my $s (@optorder) {
  337. $optblessed{$s} = $ind++
  338. }
  339. $ind = 1;
  340. for my $s (@funcorder) {
  341. $funcblessed{$s} = $ind++
  342. }
  343. for my $m (@manpages) {
  344. $docsdirs{dirname($m)}++;
  345. }
  346. for my $m (@manpages) {
  347. scanmanpage($m);
  348. }
  349. print STDERR "ok\n" if(!$errors);
  350. exit $errors;