gen.pl 15 KB

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