manpage-syntax.pl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 2019 - 2022, Daniel Stenberg, <[email protected]>, 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. # get the file name first
  32. my $symbolsinversions=shift @ARGV;
  33. # we may get the dir roots pointed out
  34. my @manpages=@ARGV;
  35. my $errors = 0;
  36. my %optblessed;
  37. my %funcblessed;
  38. my @optorder = (
  39. 'NAME',
  40. 'SYNOPSIS',
  41. 'DESCRIPTION',
  42. #'DEFAULT', # CURLINFO_ has no default
  43. 'PROTOCOLS',
  44. 'EXAMPLE',
  45. 'AVAILABILITY',
  46. 'RETURN VALUE',
  47. 'SEE ALSO'
  48. );
  49. my @funcorder = (
  50. 'NAME',
  51. 'SYNOPSIS',
  52. 'DESCRIPTION',
  53. 'EXAMPLE',
  54. 'AVAILABILITY',
  55. 'RETURN VALUE',
  56. 'SEE ALSO'
  57. );
  58. my %shline; # section => line number
  59. my %symbol;
  60. sub allsymbols {
  61. open(F, "<$symbolsinversions") ||
  62. die "$symbolsinversions: $|";
  63. while(<F>) {
  64. if($_ =~ /^([^ ]*)/) {
  65. $symbol{$1}=$1;
  66. }
  67. }
  68. close(F);
  69. }
  70. sub scanmanpage {
  71. my ($file) = @_;
  72. my $reqex = 0;
  73. my $inex = 0;
  74. my $insynop = 0;
  75. my $exsize = 0;
  76. my $synopsize = 0;
  77. my $shc = 0;
  78. my $optpage = 0; # option or function
  79. my @sh;
  80. open(M, "<$file") || die "no such file: $file";
  81. if($file =~ /[\/\\](CURL|curl_)[^\/\\]*.3/) {
  82. # This is a man page for libcurl. It requires an example!
  83. $reqex = 1;
  84. if($1 eq "CURL") {
  85. $optpage = 1;
  86. }
  87. }
  88. my $line = 1;
  89. while(<M>) {
  90. chomp;
  91. if($_ =~ /^.so /) {
  92. # this man page is just a referral
  93. close(M);
  94. return;
  95. }
  96. if(($_ =~ /^\.SH SYNOPSIS/i) && ($reqex)) {
  97. # this is for libcurl man page SYNOPSIS checks
  98. $insynop = 1;
  99. $inex = 0;
  100. }
  101. elsif($_ =~ /^\.SH EXAMPLE/i) {
  102. $insynop = 0;
  103. $inex = 1;
  104. }
  105. elsif($_ =~ /^\.SH/i) {
  106. $insynop = 0;
  107. $inex = 0;
  108. }
  109. elsif($inex) {
  110. $exsize++;
  111. if($_ =~ /[^\\]\\n/) {
  112. print STDERR "$file:$line '\\n' need to be '\\\\n'!\n";
  113. }
  114. }
  115. elsif($insynop) {
  116. $synopsize++;
  117. if(($synopsize == 1) && ($_ !~ /\.nf/)) {
  118. print STDERR "$file:$line:1:ERROR: be .nf for proper formatting\n";
  119. }
  120. }
  121. if($_ =~ /^\.SH ([^\r\n]*)/i) {
  122. my $n = $1;
  123. # remove enclosing quotes
  124. $n =~ s/\"(.*)\"\z/$1/;
  125. push @sh, $n;
  126. $shline{$n} = $line;
  127. }
  128. if($_ =~ /^\'/) {
  129. print STDERR "$file:$line line starts with single quote!\n";
  130. $errors++;
  131. }
  132. if($_ =~ /\\f([BI])(.*)/) {
  133. my ($format, $rest) = ($1, $2);
  134. if($rest !~ /\\fP/) {
  135. print STDERR "$file:$line missing \\f${format} terminator!\n";
  136. $errors++;
  137. }
  138. }
  139. if($_ =~ /[ \t]+$/) {
  140. print STDERR "$file:$line trailing whitespace\n";
  141. $errors++;
  142. }
  143. if($_ =~ /\\f([BI])([^\\]*)\\fP/) {
  144. my $r = $2;
  145. if($r =~ /^(CURL.*)\(3\)/) {
  146. my $rr = $1;
  147. if(!$symbol{$rr}) {
  148. print STDERR "$file:$line link to non-libcurl option $rr!\n";
  149. $errors++;
  150. }
  151. }
  152. }
  153. $line++;
  154. }
  155. close(M);
  156. if($reqex) {
  157. # only for libcurl options man-pages
  158. my $shcount = scalar(@sh); # before @sh gets shifted
  159. if($exsize < 2) {
  160. print STDERR "$file:$line missing EXAMPLE section\n";
  161. $errors++;
  162. }
  163. if($shcount < 3) {
  164. print STDERR "$file:$line too few man page sections!\n";
  165. $errors++;
  166. return;
  167. }
  168. my $got = "start";
  169. my $i = 0;
  170. my $shused = 1;
  171. my @shorig = @sh;
  172. my @order = $optpage ? @optorder : @funcorder;
  173. my $blessed = $optpage ? \%optblessed : \%funcblessed;
  174. while($got) {
  175. my $finesh;
  176. $got = shift(@sh);
  177. if($got) {
  178. if($$blessed{$got}) {
  179. $i = $$blessed{$got};
  180. $finesh = $got; # a mandatory one
  181. }
  182. }
  183. if($i && defined($finesh)) {
  184. # mandatory section
  185. if($i != $shused) {
  186. printf STDERR "$file:%u Got %s, when %s was expected\n",
  187. $shline{$finesh},
  188. $finesh,
  189. $order[$shused-1];
  190. $errors++;
  191. return;
  192. }
  193. $shused++;
  194. if($i == scalar(@order)) {
  195. # last mandatory one, exit
  196. last;
  197. }
  198. }
  199. }
  200. if($i != scalar(@order)) {
  201. printf STDERR "$file:$line missing mandatory section: %s\n",
  202. $order[$i];
  203. printf STDERR "$file:$line section found at index %u: '%s'\n",
  204. $i, $shorig[$i];
  205. printf STDERR " Found %u used sections\n", $shcount;
  206. $errors++;
  207. }
  208. }
  209. }
  210. allsymbols();
  211. if(!$symbol{'CURLALTSVC_H1'}) {
  212. print STDERR "didn't get the symbols-in-version!\n";
  213. exit;
  214. }
  215. my $ind = 1;
  216. for my $s (@optorder) {
  217. $optblessed{$s} = $ind++
  218. }
  219. $ind = 1;
  220. for my $s (@funcorder) {
  221. $funcblessed{$s} = $ind++
  222. }
  223. for my $m (@manpages) {
  224. scanmanpage($m);
  225. }
  226. print STDERR "ok\n" if(!$errors);
  227. exit $errors;