manpage-syntax.pl 5.9 KB

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