gen.pl 15 KB

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