gen.pl 15 KB

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