gen.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 1998 - 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. =begin comment
  24. This script generates the manpage.
  25. Example: gen.pl <command> [files] > curl.1
  26. Dev notes:
  27. We open *input* files in :crlf translation (a no-op on many platforms) in
  28. case we have CRLF line endings in Windows but a perl that defaults to LF.
  29. Unfortunately it seems some perls like msysgit can't handle a global input-only
  30. :crlf so it has to be specified on each file open for text input.
  31. =end comment
  32. =cut
  33. my %optshort;
  34. my %optlong;
  35. my %helplong;
  36. my %arglong;
  37. my %redirlong;
  38. my %protolong;
  39. my %catlong;
  40. use POSIX qw(strftime);
  41. my $date = strftime "%B %d %Y", localtime;
  42. my $year = strftime "%Y", localtime;
  43. my $version = "unknown";
  44. open(INC, "<../../include/curl/curlver.h");
  45. while(<INC>) {
  46. if($_ =~ /^#define LIBCURL_VERSION \"([0-9.]*)/) {
  47. $version = $1;
  48. last;
  49. }
  50. }
  51. close(INC);
  52. # get the long name version, return the man page string
  53. sub manpageify {
  54. my ($k)=@_;
  55. my $l;
  56. if($optlong{$k} ne "") {
  57. # both short + long
  58. $l = "\\fI-".$optlong{$k}.", --$k\\fP";
  59. }
  60. else {
  61. # only long
  62. $l = "\\fI--$k\\fP";
  63. }
  64. return $l;
  65. }
  66. sub printdesc {
  67. my @desc = @_;
  68. for my $d (@desc) {
  69. if($d =~ /\(Added in ([0-9.]+)\)/i) {
  70. my $ver = $1;
  71. if(too_old($ver)) {
  72. $d =~ s/ *\(Added in $ver\)//gi;
  73. }
  74. }
  75. if($d !~ /^.\\"/) {
  76. # **bold**
  77. $d =~ s/\*\*([^ ]*)\*\*/\\fB$1\\fP/g;
  78. # *italics*
  79. $d =~ s/\*([^ ]*)\*/\\fI$1\\fP/g;
  80. }
  81. # skip lines starting with space (examples)
  82. if($d =~ /^[^ ]/) {
  83. for my $k (keys %optlong) {
  84. my $l = manpageify($k);
  85. $d =~ s/--$k([^a-z0-9_-])/$l$1/;
  86. }
  87. }
  88. # quote "bare" minuses in the output
  89. $d =~ s/( |\\fI|^)--/$1\\-\\-/g;
  90. $d =~ s/([ -]|\\fI|^)-/$1\\-/g;
  91. # handle single quotes first on the line
  92. $d =~ s/(\s*)\'/$1\\(aq/;
  93. print $d;
  94. }
  95. }
  96. sub seealso {
  97. my($standalone, $data)=@_;
  98. if($standalone) {
  99. return sprintf
  100. ".SH \"SEE ALSO\"\n$data\n";
  101. }
  102. else {
  103. return "See also $data. ";
  104. }
  105. }
  106. sub overrides {
  107. my ($standalone, $data)=@_;
  108. if($standalone) {
  109. return ".SH \"OVERRIDES\"\n$data\n";
  110. }
  111. else {
  112. return $data;
  113. }
  114. }
  115. sub protocols {
  116. my ($standalone, $data)=@_;
  117. if($standalone) {
  118. return ".SH \"PROTOCOLS\"\n$data\n";
  119. }
  120. else {
  121. return "($data) ";
  122. }
  123. }
  124. sub too_old {
  125. my ($version)=@_;
  126. my $a = 999999;
  127. if($version =~ /^(\d+)\.(\d+)\.(\d+)/) {
  128. $a = $1 * 1000 + $2 * 10 + $3;
  129. }
  130. elsif($version =~ /^(\d+)\.(\d+)/) {
  131. $a = $1 * 1000 + $2 * 10;
  132. }
  133. if($a < 7300) {
  134. # we consider everything before 7.30.0 to be too old to mention
  135. # specific changes for
  136. return 1;
  137. }
  138. return 0;
  139. }
  140. sub added {
  141. my ($standalone, $data)=@_;
  142. if(too_old($data)) {
  143. # don't mention ancient additions
  144. return "";
  145. }
  146. if($standalone) {
  147. return ".SH \"ADDED\"\nAdded in curl version $data\n";
  148. }
  149. else {
  150. return "Added in $data. ";
  151. }
  152. }
  153. sub single {
  154. my ($f, $standalone)=@_;
  155. open(F, "<:crlf", "$f") ||
  156. return 1;
  157. my $short;
  158. my $long;
  159. my $tags;
  160. my $added;
  161. my $protocols;
  162. my $arg;
  163. my $mutexed;
  164. my $requires;
  165. my $category;
  166. my $seealso;
  167. my @examples; # there can be more than one
  168. my $magic; # cmdline special option
  169. my $line;
  170. while(<F>) {
  171. $line++;
  172. if(/^Short: *(.)/i) {
  173. $short=$1;
  174. }
  175. elsif(/^Long: *(.*)/i) {
  176. $long=$1;
  177. }
  178. elsif(/^Added: *(.*)/i) {
  179. $added=$1;
  180. }
  181. elsif(/^Tags: *(.*)/i) {
  182. $tags=$1;
  183. }
  184. elsif(/^Arg: *(.*)/i) {
  185. $arg=$1;
  186. }
  187. elsif(/^Magic: *(.*)/i) {
  188. $magic=$1;
  189. }
  190. elsif(/^Mutexed: *(.*)/i) {
  191. $mutexed=$1;
  192. }
  193. elsif(/^Protocols: *(.*)/i) {
  194. $protocols=$1;
  195. }
  196. elsif(/^See-also: *(.*)/i) {
  197. $seealso=$1;
  198. }
  199. elsif(/^Requires: *(.*)/i) {
  200. $requires=$1;
  201. }
  202. elsif(/^Category: *(.*)/i) {
  203. $category=$1;
  204. }
  205. elsif(/^Example: *(.*)/i) {
  206. push @examples, $1;
  207. }
  208. elsif(/^Help: *(.*)/i) {
  209. ;
  210. }
  211. elsif(/^---/) {
  212. if(!$long) {
  213. print STDERR "ERROR: no 'Long:' in $f\n";
  214. exit 1;
  215. }
  216. if(!$category) {
  217. print STDERR "ERROR: no 'Category:' in $f\n";
  218. exit 2;
  219. }
  220. if(!$examples[0]) {
  221. print STDERR "$f:$line:1:ERROR: no 'Example:' present\n";
  222. exit 2;
  223. }
  224. if(!$added) {
  225. print STDERR "$f:$line:1:ERROR: no 'Added:' version present\n";
  226. exit 2;
  227. }
  228. last;
  229. }
  230. else {
  231. chomp;
  232. print STDERR "WARN: unrecognized line in $f, ignoring:\n:'$_';"
  233. }
  234. }
  235. my @desc;
  236. while(<F>) {
  237. push @desc, $_;
  238. }
  239. close(F);
  240. my $opt;
  241. if(defined($short) && $long) {
  242. $opt = "-$short, --$long";
  243. }
  244. elsif($short && !$long) {
  245. $opt = "-$short";
  246. }
  247. elsif($long && !$short) {
  248. $opt = "--$long";
  249. }
  250. if($arg) {
  251. $opt .= " $arg";
  252. }
  253. # quote "bare" minuses in opt
  254. $opt =~ s/( |^)--/$1\\-\\-/g;
  255. $opt =~ s/( |^)-/$1\\-/g;
  256. if($standalone) {
  257. print ".TH curl 1 \"30 Nov 2016\" \"curl 7.52.0\" \"curl manual\"\n";
  258. print ".SH OPTION\n";
  259. print "curl $opt\n";
  260. }
  261. else {
  262. print ".IP \"$opt\"\n";
  263. }
  264. if($protocols) {
  265. print protocols($standalone, $protocols);
  266. }
  267. if($standalone) {
  268. print ".SH DESCRIPTION\n";
  269. }
  270. printdesc(@desc);
  271. undef @desc;
  272. my @foot;
  273. if($seealso) {
  274. my @m=split(/ /, $seealso);
  275. my $mstr;
  276. my $and = 0;
  277. my $num = scalar(@m);
  278. if($num > 2) {
  279. # use commas up to this point
  280. $and = $num - 1;
  281. }
  282. my $i = 0;
  283. for my $k (@m) {
  284. if(!$helplong{$k}) {
  285. print STDERR "WARN: $f see-alsos a non-existing option: $k\n";
  286. }
  287. my $l = manpageify($k);
  288. my $sep = " and";
  289. if($and && ($i < $and)) {
  290. $sep = ",";
  291. }
  292. $mstr .= sprintf "%s$l", $mstr?"$sep ":"";
  293. $i++;
  294. }
  295. push @foot, seealso($standalone, $mstr);
  296. }
  297. if($requires) {
  298. my $l = manpageify($long);
  299. push @foot, "$l requires that the underlying libcurl".
  300. " was built to support $requires. ";
  301. }
  302. if($mutexed) {
  303. my @m=split(/ /, $mutexed);
  304. my $mstr;
  305. for my $k (@m) {
  306. if(!$helplong{$k}) {
  307. print STDERR "WARN: $f mutexes a non-existing option: $k\n";
  308. }
  309. my $l = manpageify($k);
  310. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  311. }
  312. push @foot, overrides($standalone, "This option overrides $mstr. ");
  313. }
  314. if($examples[0]) {
  315. my $s ="";
  316. $s="s" if($examples[1]);
  317. print "\nExample$s:\n.nf\n";
  318. foreach my $e (@examples) {
  319. $e =~ s!\$URL!https://example.com!g;
  320. print " curl $e\n";
  321. }
  322. print ".fi\n";
  323. }
  324. if($added) {
  325. push @foot, added($standalone, $added);
  326. }
  327. if($foot[0]) {
  328. print "\n";
  329. my $f = join("", @foot);
  330. $f =~ s/ +\z//; # remove trailing space
  331. print "$f\n";
  332. }
  333. return 0;
  334. }
  335. sub getshortlong {
  336. my ($f)=@_;
  337. open(F, "<:crlf", "$f");
  338. my $short;
  339. my $long;
  340. my $help;
  341. my $arg;
  342. my $protocols;
  343. my $category;
  344. while(<F>) {
  345. if(/^Short: (.)/i) {
  346. $short=$1;
  347. }
  348. elsif(/^Long: (.*)/i) {
  349. $long=$1;
  350. }
  351. elsif(/^Help: (.*)/i) {
  352. $help=$1;
  353. }
  354. elsif(/^Arg: (.*)/i) {
  355. $arg=$1;
  356. }
  357. elsif(/^Protocols: (.*)/i) {
  358. $protocols=$1;
  359. }
  360. elsif(/^Category: (.*)/i) {
  361. $category=$1;
  362. }
  363. elsif(/^---/) {
  364. last;
  365. }
  366. }
  367. close(F);
  368. if($short) {
  369. $optshort{$short}=$long;
  370. }
  371. if($long) {
  372. $optlong{$long}=$short;
  373. $helplong{$long}=$help;
  374. $arglong{$long}=$arg;
  375. $protolong{$long}=$protocols;
  376. $catlong{$long}=$category;
  377. }
  378. }
  379. sub indexoptions {
  380. my (@files) = @_;
  381. foreach my $f (@files) {
  382. getshortlong($f);
  383. }
  384. }
  385. sub header {
  386. my ($f)=@_;
  387. open(F, "<:crlf", "$f");
  388. my @d;
  389. while(<F>) {
  390. s/%DATE/$date/g;
  391. s/%VERSION/$version/g;
  392. push @d, $_;
  393. }
  394. close(F);
  395. printdesc(@d);
  396. }
  397. sub listhelp {
  398. print <<HEAD
  399. /***************************************************************************
  400. * _ _ ____ _
  401. * Project ___| | | | _ \\| |
  402. * / __| | | | |_) | |
  403. * | (__| |_| | _ <| |___
  404. * \\___|\\___/|_| \\_\\_____|
  405. *
  406. * Copyright (C) 1998 - $year, Daniel Stenberg, <[email protected]>, et al.
  407. *
  408. * This software is licensed as described in the file COPYING, which
  409. * you should have received as part of this distribution. The terms
  410. * are also available at https://curl.se/docs/copyright.html.
  411. *
  412. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  413. * copies of the Software, and permit persons to whom the Software is
  414. * furnished to do so, under the terms of the COPYING file.
  415. *
  416. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  417. * KIND, either express or implied.
  418. *
  419. ***************************************************************************/
  420. #include "tool_setup.h"
  421. #include "tool_help.h"
  422. /*
  423. * DO NOT edit tool_listhelp.c manually.
  424. * This source file is generated with the following command:
  425. cd \$srcroot/docs/cmdline-opts
  426. ./gen.pl listhelp *.d > \$srcroot/src/tool_listhelp.c
  427. */
  428. const struct helptxt helptext[] = {
  429. HEAD
  430. ;
  431. foreach my $f (sort keys %helplong) {
  432. my $long = $f;
  433. my $short = $optlong{$long};
  434. my @categories = split ' ', $catlong{$long};
  435. my $bitmask;
  436. my $opt;
  437. if(defined($short) && $long) {
  438. $opt = "-$short, --$long";
  439. }
  440. elsif($long && !$short) {
  441. $opt = " --$long";
  442. }
  443. for my $i (0 .. $#categories) {
  444. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  445. # If not last element, append |
  446. if($i < $#categories) {
  447. $bitmask .= ' | ';
  448. }
  449. }
  450. my $arg = $arglong{$long};
  451. if($arg) {
  452. $opt .= " $arg";
  453. }
  454. my $desc = $helplong{$f};
  455. $desc =~ s/\"/\\\"/g; # escape double quotes
  456. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  457. if(length($opt) > 78) {
  458. print STDERR "WARN: the --$long name is too long\n";
  459. }
  460. elsif(length($desc) > 78) {
  461. print STDERR "WARN: the --$long description is too long\n";
  462. }
  463. print $line;
  464. }
  465. print <<FOOT
  466. { NULL, NULL, CURLHELP_HIDDEN }
  467. };
  468. FOOT
  469. ;
  470. }
  471. sub listcats {
  472. my %allcats;
  473. foreach my $f (sort keys %helplong) {
  474. my @categories = split ' ', $catlong{$f};
  475. foreach (@categories) {
  476. $allcats{$_} = undef;
  477. }
  478. }
  479. my @categories;
  480. foreach my $key (keys %allcats) {
  481. push @categories, $key;
  482. }
  483. @categories = sort @categories;
  484. unshift @categories, 'hidden';
  485. for my $i (0..$#categories) {
  486. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  487. }
  488. }
  489. sub mainpage {
  490. my (@files) = @_;
  491. # show the page header
  492. header("page-header");
  493. # output docs for all options
  494. foreach my $f (sort @files) {
  495. if(single($f, 0)) {
  496. print STDERR "Can't read $f?\n";
  497. }
  498. }
  499. header("page-footer");
  500. }
  501. sub showonly {
  502. my ($f) = @_;
  503. if(single($f, 1)) {
  504. print STDERR "$f: failed\n";
  505. }
  506. }
  507. sub showprotocols {
  508. my %prots;
  509. foreach my $f (keys %optlong) {
  510. my @p = split(/ /, $protolong{$f});
  511. for my $p (@p) {
  512. $prots{$p}++;
  513. }
  514. }
  515. for(sort keys %prots) {
  516. printf "$_ (%d options)\n", $prots{$_};
  517. }
  518. }
  519. sub getargs {
  520. my ($f, @s) = @_;
  521. if($f eq "mainpage") {
  522. mainpage(@s);
  523. return;
  524. }
  525. elsif($f eq "listhelp") {
  526. listhelp();
  527. return;
  528. }
  529. elsif($f eq "single") {
  530. showonly($s[0]);
  531. return;
  532. }
  533. elsif($f eq "protos") {
  534. showprotocols();
  535. return;
  536. }
  537. elsif($f eq "listcats") {
  538. listcats();
  539. return;
  540. }
  541. print "Usage: gen.pl <mainpage/listhelp/single FILE/protos/listcats> [files]\n";
  542. }
  543. #------------------------------------------------------------------------
  544. my $cmd = shift @ARGV;
  545. my @files = @ARGV; # the rest are the files
  546. # learn all existing options
  547. indexoptions(@files);
  548. getargs($cmd, @files);