gen.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. #!/usr/bin/env perl
  2. =begin comment
  3. This script generates the manpage.
  4. Example: gen.pl mainpage > curl.1
  5. Dev notes:
  6. We open *input* files in :crlf translation (a no-op on many platforms) in
  7. case we have CRLF line endings in Windows but a perl that defaults to LF.
  8. Unfortunately it seems some perls like msysgit can't handle a global input-only
  9. :crlf so it has to be specified on each file open for text input.
  10. =end comment
  11. =cut
  12. my $some_dir=$ARGV[1] || ".";
  13. opendir(my $dh, $some_dir) || die "Can't opendir $some_dir: $!";
  14. my @s = grep { /\.d$/ && -f "$some_dir/$_" } readdir($dh);
  15. closedir $dh;
  16. my %optshort;
  17. my %optlong;
  18. my %helplong;
  19. my %arglong;
  20. my %redirlong;
  21. my %protolong;
  22. # get the long name version, return the man page string
  23. sub manpageify {
  24. my ($k)=@_;
  25. my $l;
  26. if($optlong{$k} ne "") {
  27. # both short + long
  28. $l = "\\fI-".$optlong{$k}.", --$k\\fP";
  29. }
  30. else {
  31. # only long
  32. $l = "\\fI--$k\\fP";
  33. }
  34. return $l;
  35. }
  36. sub printdesc {
  37. my @desc = @_;
  38. for my $d (@desc) {
  39. # skip lines starting with space (examples)
  40. if($d =~ /^[^ ]/) {
  41. for my $k (keys %optlong) {
  42. my $l = manpageify($k);
  43. $d =~ s/--$k([^a-z0-9_-])/$l$1/;
  44. }
  45. }
  46. print $d;
  47. }
  48. }
  49. sub seealso {
  50. my($standalone, $data)=@_;
  51. if($standalone) {
  52. return sprintf
  53. ".SH \"SEE ALSO\"\n$data\n";
  54. }
  55. else {
  56. return "See also $data. ";
  57. }
  58. }
  59. sub overrides {
  60. my ($standalone, $data)=@_;
  61. if($standalone) {
  62. return ".SH \"OVERRIDES\"\n$data\n";
  63. }
  64. else {
  65. return $data;
  66. }
  67. }
  68. sub protocols {
  69. my ($standalone, $data)=@_;
  70. if($standalone) {
  71. return ".SH \"PROTOCOLS\"\n$data\n";
  72. }
  73. else {
  74. return "($data) ";
  75. }
  76. }
  77. sub added {
  78. my ($standalone, $data)=@_;
  79. if($standalone) {
  80. return ".SH \"ADDED\"\nAdded in curl version $data\n";
  81. }
  82. else {
  83. return "Added in $data. ";
  84. }
  85. }
  86. sub single {
  87. my ($f, $standalone)=@_;
  88. open(F, "<:crlf", "$some_dir/$f") ||
  89. return 1;
  90. my $short;
  91. my $long;
  92. my $tags;
  93. my $added;
  94. my $protocols;
  95. my $arg;
  96. my $mutexed;
  97. my $requires;
  98. my $seealso;
  99. my $magic; # cmdline special option
  100. while(<F>) {
  101. if(/^Short: *(.)/i) {
  102. $short=$1;
  103. }
  104. elsif(/^Long: *(.*)/i) {
  105. $long=$1;
  106. }
  107. elsif(/^Added: *(.*)/i) {
  108. $added=$1;
  109. }
  110. elsif(/^Tags: *(.*)/i) {
  111. $tags=$1;
  112. }
  113. elsif(/^Arg: *(.*)/i) {
  114. $arg=$1;
  115. }
  116. elsif(/^Magic: *(.*)/i) {
  117. $magic=$1;
  118. }
  119. elsif(/^Mutexed: *(.*)/i) {
  120. $mutexed=$1;
  121. }
  122. elsif(/^Protocols: *(.*)/i) {
  123. $protocols=$1;
  124. }
  125. elsif(/^See-also: *(.*)/i) {
  126. $seealso=$1;
  127. }
  128. elsif(/^Requires: *(.*)/i) {
  129. $requires=$1;
  130. }
  131. elsif(/^Help: *(.*)/i) {
  132. ;
  133. }
  134. elsif(/^---/) {
  135. if(!$long) {
  136. print STDERR "WARN: no 'Long:' in $f\n";
  137. }
  138. last;
  139. }
  140. else {
  141. chomp;
  142. print STDERR "WARN: unrecognized line in $f, ignoring:\n:'$_';"
  143. }
  144. }
  145. my @dest;
  146. while(<F>) {
  147. push @desc, $_;
  148. }
  149. close(F);
  150. my $opt;
  151. if(defined($short) && $long) {
  152. $opt = "-$short, --$long";
  153. }
  154. elsif($short && !$long) {
  155. $opt = "-$short";
  156. }
  157. elsif($long && !$short) {
  158. $opt = "--$long";
  159. }
  160. if($arg) {
  161. $opt .= " $arg";
  162. }
  163. if($standalone) {
  164. print ".TH curl 1 \"30 Nov 2016\" \"curl 7.52.0\" \"curl manual\"\n";
  165. print ".SH OPTION\n";
  166. print "curl $opt\n";
  167. }
  168. else {
  169. print ".IP \"$opt\"\n";
  170. }
  171. if($protocols) {
  172. print protocols($standalone, $protocols);
  173. }
  174. if($standalone) {
  175. print ".SH DESCRIPTION\n";
  176. }
  177. printdesc(@desc);
  178. undef @desc;
  179. my @foot;
  180. if($seealso) {
  181. my @m=split(/ /, $seealso);
  182. my $mstr;
  183. for my $k (@m) {
  184. if(!$helplong{$k}) {
  185. print STDERR "WARN: $f see-alsos a non-existing option: $k\n";
  186. }
  187. my $l = manpageify($k);
  188. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  189. }
  190. push @foot, seealso($standalone, $mstr);
  191. }
  192. if($requires) {
  193. my $l = manpageify($long);
  194. push @foot, "$l requires that the underlying libcurl".
  195. " was built to support $requires. ";
  196. }
  197. if($mutexed) {
  198. my @m=split(/ /, $mutexed);
  199. my $mstr;
  200. for my $k (@m) {
  201. if(!$helplong{$k}) {
  202. print STDERR "WARN: $f mutexes a non-existing option: $k\n";
  203. }
  204. my $l = manpageify($k);
  205. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  206. }
  207. push @foot, overrides($standalone, "This option overrides $mstr. ");
  208. }
  209. if($added) {
  210. push @foot, added($standalone, $added);
  211. }
  212. if($foot[0]) {
  213. print "\n";
  214. my $f = join("", @foot);
  215. $f =~ s/ +\z//; # remove trailing space
  216. print "$f\n";
  217. }
  218. return 0;
  219. }
  220. sub getshortlong {
  221. my ($f)=@_;
  222. open(F, "<:crlf", "$some_dir/$f");
  223. my $short;
  224. my $long;
  225. my $help;
  226. my $arg;
  227. my $protocols;
  228. while(<F>) {
  229. if(/^Short: (.)/i) {
  230. $short=$1;
  231. }
  232. elsif(/^Long: (.*)/i) {
  233. $long=$1;
  234. }
  235. elsif(/^Help: (.*)/i) {
  236. $help=$1;
  237. }
  238. elsif(/^Arg: (.*)/i) {
  239. $arg=$1;
  240. }
  241. elsif(/^Protocols: (.*)/i) {
  242. $protocols=$1;
  243. }
  244. elsif(/^---/) {
  245. last;
  246. }
  247. }
  248. close(F);
  249. if($short) {
  250. $optshort{$short}=$long;
  251. }
  252. if($long) {
  253. $optlong{$long}=$short;
  254. $helplong{$long}=$help;
  255. $arglong{$long}=$arg;
  256. $protolong{$long}=$protocols;
  257. }
  258. }
  259. sub indexoptions {
  260. foreach my $f (@s) {
  261. getshortlong($f);
  262. }
  263. }
  264. sub header {
  265. my ($f)=@_;
  266. open(F, "<:crlf", "$some_dir/$f");
  267. my @d;
  268. while(<F>) {
  269. push @d, $_;
  270. }
  271. close(F);
  272. printdesc(@d);
  273. }
  274. sub listhelp {
  275. foreach my $f (sort keys %helplong) {
  276. my $long = $f;
  277. my $short = $optlong{$long};
  278. my $opt;
  279. if(defined($short) && $long) {
  280. $opt = "-$short, --$long";
  281. }
  282. elsif($long && !$short) {
  283. $opt = " --$long";
  284. }
  285. my $arg = $arglong{$long};
  286. if($arg) {
  287. $opt .= " $arg";
  288. }
  289. my $desc = $helplong{$f};
  290. $desc =~ s/\"/\\\"/g; # escape double quotes
  291. my $line = sprintf " {\"%s\",\n \"%s\"},\n", $opt, $desc;
  292. if(length($opt) + length($desc) > 78) {
  293. print STDERR "WARN: the --$long line is too long\n";
  294. }
  295. print $line;
  296. }
  297. }
  298. sub mainpage {
  299. # show the page header
  300. header("page-header");
  301. # output docs for all options
  302. foreach my $f (sort @s) {
  303. single($f, 0);
  304. }
  305. header("page-footer");
  306. }
  307. sub showonly {
  308. my ($f) = @_;
  309. if(single($f, 1)) {
  310. print STDERR "$f: failed\n";
  311. }
  312. }
  313. sub showprotocols {
  314. my %prots;
  315. foreach my $f (keys %optlong) {
  316. my @p = split(/ /, $protolong{$f});
  317. for my $p (@p) {
  318. $prots{$p}++;
  319. }
  320. }
  321. for(sort keys %prots) {
  322. printf "$_ (%d options)\n", $prots{$_};
  323. }
  324. }
  325. sub getargs {
  326. my $f;
  327. do {
  328. $f = shift @ARGV;
  329. if($f eq "mainpage") {
  330. mainpage();
  331. return;
  332. }
  333. elsif($f eq "listhelp") {
  334. listhelp();
  335. return;
  336. }
  337. elsif($f eq "single") {
  338. showonly(shift @ARGV);
  339. return;
  340. }
  341. elsif($f eq "protos") {
  342. showprotocols();
  343. return;
  344. }
  345. } while($f);
  346. print "Usage: gen.pl <mainpage/listhelp/single FILE/protos> [srcdir]\n";
  347. }
  348. #------------------------------------------------------------------------
  349. # learn all existing options
  350. indexoptions();
  351. getargs();