2
0

gen.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. #!/usr/bin/env perl
  2. #***************************************************************************
  3. # _ _ ____ _
  4. # Project ___| | | | _ \| |
  5. # / __| | | | |_) | |
  6. # | (__| |_| | _ <| |___
  7. # \___|\___/|_| \_\_____|
  8. #
  9. # Copyright (C) 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 cannot 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. my $globals;
  47. open(INC, "<../../include/curl/curlver.h");
  48. while(<INC>) {
  49. if($_ =~ /^#define LIBCURL_VERSION \"([0-9.]*)/) {
  50. $version = $1;
  51. last;
  52. }
  53. }
  54. close(INC);
  55. # get the long name version, return the man page string
  56. sub manpageify {
  57. my ($k)=@_;
  58. my $l;
  59. my $klong = $k;
  60. # quote "bare" minuses in the long name
  61. $klong =~ s/-/\\-/g;
  62. if($optlong{$k} ne "") {
  63. # both short + long
  64. $l = "\\fI-".$optlong{$k}.", \\-\\-$klong\\fP";
  65. }
  66. else {
  67. # only long
  68. $l = "\\fI\\-\\-$klong\\fP";
  69. }
  70. return $l;
  71. }
  72. sub printdesc {
  73. my @desc = @_;
  74. my $exam = 0;
  75. for my $d (@desc) {
  76. if($d =~ /\(Added in ([0-9.]+)\)/i) {
  77. my $ver = $1;
  78. if(too_old($ver)) {
  79. $d =~ s/ *\(Added in $ver\)//gi;
  80. }
  81. }
  82. if($d !~ /^.\\"/) {
  83. # **bold**
  84. $d =~ s/\*\*([^ ]*)\*\*/\\fB$1\\fP/g;
  85. # *italics*
  86. $d =~ s/\*([^ ]*)\*/\\fI$1\\fP/g;
  87. }
  88. if(!$exam && ($d =~ /^ /)) {
  89. # start of example
  90. $exam = 1;
  91. print ".nf\n"; # no-fill
  92. }
  93. elsif($exam && ($d !~ /^ /)) {
  94. # end of example
  95. $exam = 0;
  96. print ".fi\n"; # fill-in
  97. }
  98. # skip lines starting with space (examples)
  99. if($d =~ /^[^ ]/ && $d =~ /--/) {
  100. # scan for options in longest-names first order
  101. for my $k (sort {length($b) <=> length($a)} keys %optlong) {
  102. # --tlsv1 is complicated since --tlsv1.2 etc are also
  103. # acceptable options!
  104. if(($k eq "tlsv1") && ($d =~ /--tlsv1\.[0-9]\\f/)) {
  105. next;
  106. }
  107. my $l = manpageify($k);
  108. $d =~ s/\-\-$k([^a-z0-9-])/$l$1/g;
  109. }
  110. }
  111. # quote minuses in the output
  112. $d =~ s/([^\\])-/$1\\-/g;
  113. # replace single quotes
  114. $d =~ s/\'/\\(aq/g;
  115. # handle double quotes first on the line
  116. $d =~ s/^(\s*)\"/$1\\(dq/;
  117. print $d;
  118. }
  119. if($exam) {
  120. print ".fi\n"; # fill-in
  121. }
  122. }
  123. sub seealso {
  124. my($standalone, $data)=@_;
  125. if($standalone) {
  126. return sprintf
  127. ".SH \"SEE ALSO\"\n$data\n";
  128. }
  129. else {
  130. return "See also $data. ";
  131. }
  132. }
  133. sub overrides {
  134. my ($standalone, $data)=@_;
  135. if($standalone) {
  136. return ".SH \"OVERRIDES\"\n$data\n";
  137. }
  138. else {
  139. return $data;
  140. }
  141. }
  142. sub protocols {
  143. my ($standalone, $data)=@_;
  144. if($standalone) {
  145. return ".SH \"PROTOCOLS\"\n$data\n";
  146. }
  147. else {
  148. return "($data) ";
  149. }
  150. }
  151. sub too_old {
  152. my ($version)=@_;
  153. my $a = 999999;
  154. if($version =~ /^(\d+)\.(\d+)\.(\d+)/) {
  155. $a = $1 * 1000 + $2 * 10 + $3;
  156. }
  157. elsif($version =~ /^(\d+)\.(\d+)/) {
  158. $a = $1 * 1000 + $2 * 10;
  159. }
  160. if($a < 7500) {
  161. # we consider everything before 7.50.0 to be too old to mention
  162. # specific changes for
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. sub added {
  168. my ($standalone, $data)=@_;
  169. if(too_old($data)) {
  170. # do not mention ancient additions
  171. return "";
  172. }
  173. if($standalone) {
  174. return ".SH \"ADDED\"\nAdded in curl version $data\n";
  175. }
  176. else {
  177. return "Added in $data. ";
  178. }
  179. }
  180. sub single {
  181. my ($f, $standalone)=@_;
  182. open(F, "<:crlf", "$f") ||
  183. return 1;
  184. my $short;
  185. my $long;
  186. my $tags;
  187. my $added;
  188. my $protocols;
  189. my $arg;
  190. my $mutexed;
  191. my $requires;
  192. my $category;
  193. my $seealso;
  194. my $copyright;
  195. my $spdx;
  196. my @examples; # there can be more than one
  197. my $magic; # cmdline special option
  198. my $line;
  199. my $multi;
  200. my $scope;
  201. my $experimental;
  202. while(<F>) {
  203. $line++;
  204. if(/^Short: *(.)/i) {
  205. $short=$1;
  206. }
  207. elsif(/^Long: *(.*)/i) {
  208. $long=$1;
  209. }
  210. elsif(/^Added: *(.*)/i) {
  211. $added=$1;
  212. }
  213. elsif(/^Tags: *(.*)/i) {
  214. $tags=$1;
  215. }
  216. elsif(/^Arg: *(.*)/i) {
  217. $arg=$1;
  218. }
  219. elsif(/^Magic: *(.*)/i) {
  220. $magic=$1;
  221. }
  222. elsif(/^Mutexed: *(.*)/i) {
  223. $mutexed=$1;
  224. }
  225. elsif(/^Protocols: *(.*)/i) {
  226. $protocols=$1;
  227. }
  228. elsif(/^See-also: *(.*)/i) {
  229. if($seealso) {
  230. print STDERR "ERROR: duplicated See-also in $f\n";
  231. return 1;
  232. }
  233. $seealso=$1;
  234. }
  235. elsif(/^Requires: *(.*)/i) {
  236. $requires=$1;
  237. }
  238. elsif(/^Category: *(.*)/i) {
  239. $category=$1;
  240. }
  241. elsif(/^Example: *(.*)/i) {
  242. push @examples, $1;
  243. }
  244. elsif(/^Multi: *(.*)/i) {
  245. $multi=$1;
  246. }
  247. elsif(/^Scope: *(.*)/i) {
  248. $scope=$1;
  249. }
  250. elsif(/^Experimental: yes/i) {
  251. $experimental=1;
  252. }
  253. elsif(/^C: (.*)/i) {
  254. $copyright=$1;
  255. }
  256. elsif(/^SPDX-License-Identifier: (.*)/i) {
  257. $spdx=$1;
  258. }
  259. elsif(/^Help: *(.*)/i) {
  260. ;
  261. }
  262. elsif(/^---/) {
  263. if(!$long) {
  264. print STDERR "ERROR: no 'Long:' in $f\n";
  265. return 1;
  266. }
  267. if(!$category) {
  268. print STDERR "ERROR: no 'Category:' in $f\n";
  269. return 2;
  270. }
  271. if(!$examples[0]) {
  272. print STDERR "$f:$line:1:ERROR: no 'Example:' present\n";
  273. return 2;
  274. }
  275. if(!$added) {
  276. print STDERR "$f:$line:1:ERROR: no 'Added:' version present\n";
  277. return 2;
  278. }
  279. if(!$seealso) {
  280. print STDERR "$f:$line:1:ERROR: no 'See-also:' field present\n";
  281. return 2;
  282. }
  283. if(!$copyright) {
  284. print STDERR "$f:$line:1:ERROR: no 'C:' field present\n";
  285. return 2;
  286. }
  287. if(!$spdx) {
  288. print STDERR "$f:$line:1:ERROR: no 'SPDX-License-Identifier:' field present\n";
  289. return 2;
  290. }
  291. last;
  292. }
  293. else {
  294. chomp;
  295. print STDERR "WARN: unrecognized line in $f, ignoring:\n:'$_';"
  296. }
  297. }
  298. my @desc;
  299. while(<F>) {
  300. push @desc, $_;
  301. }
  302. close(F);
  303. my $opt;
  304. if(defined($short) && $long) {
  305. $opt = "-$short, --$long";
  306. }
  307. elsif($short && !$long) {
  308. $opt = "-$short";
  309. }
  310. elsif($long && !$short) {
  311. $opt = "--$long";
  312. }
  313. if($arg) {
  314. $opt .= " $arg";
  315. }
  316. # quote "bare" minuses in opt
  317. $opt =~ s/-/\\-/g;
  318. if($standalone) {
  319. print ".TH curl 1 \"30 Nov 2016\" \"curl 7.52.0\" \"curl manual\"\n";
  320. print ".SH OPTION\n";
  321. print "curl $opt\n";
  322. }
  323. else {
  324. print ".IP \"$opt\"\n";
  325. }
  326. if($protocols) {
  327. print protocols($standalone, $protocols);
  328. }
  329. if($standalone) {
  330. print ".SH DESCRIPTION\n";
  331. }
  332. if($experimental) {
  333. print "**WARNING**: this option is experimental. Do not use in production.\n\n";
  334. }
  335. printdesc(@desc);
  336. undef @desc;
  337. if($scope) {
  338. if($scope eq "global") {
  339. print "\nThis option is global and does not need to be specified for each use of --next.\n";
  340. }
  341. else {
  342. print STDERR "$f:$line:1:ERROR: unrecognized scope: '$scope'\n";
  343. return 2;
  344. }
  345. }
  346. my @extra;
  347. if($multi eq "single") {
  348. push @extra, "\nIf --$long is provided several times, the last set ".
  349. "value is used.\n";
  350. }
  351. elsif($multi eq "append") {
  352. push @extra, "\n--$long can be used several times in a command line\n";
  353. }
  354. elsif($multi eq "boolean") {
  355. my $rev = "no-$long";
  356. # for options that start with "no-" the reverse is then without
  357. # the no- prefix
  358. if($long =~ /^no-/) {
  359. $rev = $long;
  360. $rev =~ s/^no-//;
  361. }
  362. push @extra,
  363. "\nProviding --$long multiple times has no extra effect.\n".
  364. "Disable it again with \\-\\-$rev.\n";
  365. }
  366. elsif($multi eq "mutex") {
  367. push @extra,
  368. "\nProviding --$long multiple times has no extra effect.\n";
  369. }
  370. elsif($multi eq "custom") {
  371. ; # left for the text to describe
  372. }
  373. else {
  374. print STDERR "$f:$line:1:ERROR: unrecognized Multi: '$multi'\n";
  375. return 2;
  376. }
  377. printdesc(@extra);
  378. my @foot;
  379. if($seealso) {
  380. my @m=split(/ /, $seealso);
  381. my $mstr;
  382. my $and = 0;
  383. my $num = scalar(@m);
  384. if($num > 2) {
  385. # use commas up to this point
  386. $and = $num - 1;
  387. }
  388. my $i = 0;
  389. for my $k (@m) {
  390. if(!$helplong{$k}) {
  391. print STDERR "$f:$line:1:WARN: see-also a non-existing option: $k\n";
  392. }
  393. my $l = manpageify($k);
  394. my $sep = " and";
  395. if($and && ($i < $and)) {
  396. $sep = ",";
  397. }
  398. $mstr .= sprintf "%s$l", $mstr?"$sep ":"";
  399. $i++;
  400. }
  401. push @foot, seealso($standalone, $mstr);
  402. }
  403. if($requires) {
  404. my $l = manpageify($long);
  405. push @foot, "$l requires that the underlying libcurl".
  406. " was built to support $requires. ";
  407. }
  408. if($mutexed) {
  409. my @m=split(/ /, $mutexed);
  410. my $mstr;
  411. for my $k (@m) {
  412. if(!$helplong{$k}) {
  413. print STDERR "WARN: $f mutexes a non-existing option: $k\n";
  414. }
  415. my $l = manpageify($k);
  416. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  417. }
  418. push @foot, overrides($standalone,
  419. "This option is mutually exclusive to $mstr. ");
  420. }
  421. if($examples[0]) {
  422. my $s ="";
  423. $s="s" if($examples[1]);
  424. print "\nExample$s:\n.nf\n";
  425. foreach my $e (@examples) {
  426. $e =~ s!\$URL!https://example.com!g;
  427. $e =~ s/-/\\-/g;
  428. $e =~ s/\'/\\(aq/g;
  429. print " curl $e\n";
  430. }
  431. print ".fi\n";
  432. }
  433. if($added) {
  434. push @foot, added($standalone, $added);
  435. }
  436. if($foot[0]) {
  437. print "\n";
  438. my $f = join("", @foot);
  439. $f =~ s/ +\z//; # remove trailing space
  440. print "$f\n";
  441. }
  442. return 0;
  443. }
  444. sub getshortlong {
  445. my ($f)=@_;
  446. open(F, "<:crlf", "$f");
  447. my $short;
  448. my $long;
  449. my $help;
  450. my $arg;
  451. my $protocols;
  452. my $category;
  453. while(<F>) {
  454. if(/^Short: (.)/i) {
  455. $short=$1;
  456. }
  457. elsif(/^Long: (.*)/i) {
  458. $long=$1;
  459. }
  460. elsif(/^Help: (.*)/i) {
  461. $help=$1;
  462. }
  463. elsif(/^Arg: (.*)/i) {
  464. $arg=$1;
  465. }
  466. elsif(/^Protocols: (.*)/i) {
  467. $protocols=$1;
  468. }
  469. elsif(/^Category: (.*)/i) {
  470. $category=$1;
  471. }
  472. elsif(/^---/) {
  473. last;
  474. }
  475. }
  476. close(F);
  477. if($short) {
  478. $optshort{$short}=$long;
  479. }
  480. if($long) {
  481. $optlong{$long}=$short;
  482. $helplong{$long}=$help;
  483. $arglong{$long}=$arg;
  484. $protolong{$long}=$protocols;
  485. $catlong{$long}=$category;
  486. }
  487. }
  488. sub indexoptions {
  489. my (@files) = @_;
  490. foreach my $f (@files) {
  491. getshortlong($f);
  492. }
  493. }
  494. sub header {
  495. my ($f)=@_;
  496. open(F, "<:crlf", "$f");
  497. my @d;
  498. while(<F>) {
  499. s/%DATE/$date/g;
  500. s/%VERSION/$version/g;
  501. s/%GLOBALS/$globals/g;
  502. push @d, $_;
  503. }
  504. close(F);
  505. printdesc(@d);
  506. }
  507. sub listhelp {
  508. print <<HEAD
  509. /***************************************************************************
  510. * _ _ ____ _
  511. * Project ___| | | | _ \\| |
  512. * / __| | | | |_) | |
  513. * | (__| |_| | _ <| |___
  514. * \\___|\\___/|_| \\_\\_____|
  515. *
  516. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  517. *
  518. * This software is licensed as described in the file COPYING, which
  519. * you should have received as part of this distribution. The terms
  520. * are also available at https://curl.se/docs/copyright.html.
  521. *
  522. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  523. * copies of the Software, and permit persons to whom the Software is
  524. * furnished to do so, under the terms of the COPYING file.
  525. *
  526. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  527. * KIND, either express or implied.
  528. *
  529. * SPDX-License-Identifier: curl
  530. *
  531. ***************************************************************************/
  532. #include "tool_setup.h"
  533. #include "tool_help.h"
  534. /*
  535. * DO NOT edit tool_listhelp.c manually.
  536. * This source file is generated with the following command:
  537. cd \$srcroot/docs/cmdline-opts
  538. ./gen.pl listhelp *.d > \$srcroot/src/tool_listhelp.c
  539. */
  540. const struct helptxt helptext[] = {
  541. HEAD
  542. ;
  543. foreach my $f (sort keys %helplong) {
  544. my $long = $f;
  545. my $short = $optlong{$long};
  546. my @categories = split ' ', $catlong{$long};
  547. my $bitmask = ' ';
  548. my $opt;
  549. if(defined($short) && $long) {
  550. $opt = "-$short, --$long";
  551. }
  552. elsif($long && !$short) {
  553. $opt = " --$long";
  554. }
  555. for my $i (0 .. $#categories) {
  556. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  557. # If not last element, append |
  558. if($i < $#categories) {
  559. $bitmask .= ' | ';
  560. }
  561. }
  562. $bitmask =~ s/(?=.{76}).{1,76}\|/$&\n /g;
  563. my $arg = $arglong{$long};
  564. if($arg) {
  565. $opt .= " $arg";
  566. }
  567. my $desc = $helplong{$f};
  568. $desc =~ s/\"/\\\"/g; # escape double quotes
  569. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  570. if(length($opt) > 78) {
  571. print STDERR "WARN: the --$long name is too long\n";
  572. }
  573. elsif(length($desc) > 78) {
  574. print STDERR "WARN: the --$long description is too long\n";
  575. }
  576. print $line;
  577. }
  578. print <<FOOT
  579. { NULL, NULL, CURLHELP_HIDDEN }
  580. };
  581. FOOT
  582. ;
  583. }
  584. sub listcats {
  585. my %allcats;
  586. foreach my $f (sort keys %helplong) {
  587. my @categories = split ' ', $catlong{$f};
  588. foreach (@categories) {
  589. $allcats{$_} = undef;
  590. }
  591. }
  592. my @categories;
  593. foreach my $key (keys %allcats) {
  594. push @categories, $key;
  595. }
  596. @categories = sort @categories;
  597. unshift @categories, 'hidden';
  598. for my $i (0..$#categories) {
  599. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  600. }
  601. }
  602. sub listglobals {
  603. my (@files) = @_;
  604. my @globalopts;
  605. # Find all global options and output them
  606. foreach my $f (sort @files) {
  607. open(F, "<:crlf", "$f") ||
  608. next;
  609. my $long;
  610. while(<F>) {
  611. if(/^Long: *(.*)/i) {
  612. $long=$1;
  613. }
  614. elsif(/^Scope: global/i) {
  615. push @globalopts, $long;
  616. last;
  617. }
  618. elsif(/^---/) {
  619. last;
  620. }
  621. }
  622. close(F);
  623. }
  624. return $ret if($ret);
  625. for my $e (0 .. $#globalopts) {
  626. $globals .= sprintf "%s--%s", $e?($globalopts[$e+1] ? ", " : " and "):"",
  627. $globalopts[$e],;
  628. }
  629. }
  630. sub mainpage {
  631. my (@files) = @_;
  632. my $ret;
  633. # show the page header
  634. header("page-header");
  635. # output docs for all options
  636. foreach my $f (sort @files) {
  637. $ret += single($f, 0);
  638. }
  639. if(!$ret) {
  640. header("page-footer");
  641. }
  642. exit $ret if($ret);
  643. }
  644. sub showonly {
  645. my ($f) = @_;
  646. if(single($f, 1)) {
  647. print STDERR "$f: failed\n";
  648. }
  649. }
  650. sub showprotocols {
  651. my %prots;
  652. foreach my $f (keys %optlong) {
  653. my @p = split(/ /, $protolong{$f});
  654. for my $p (@p) {
  655. $prots{$p}++;
  656. }
  657. }
  658. for(sort keys %prots) {
  659. printf "$_ (%d options)\n", $prots{$_};
  660. }
  661. }
  662. sub getargs {
  663. my ($f, @s) = @_;
  664. if($f eq "mainpage") {
  665. listglobals(@s);
  666. mainpage(@s);
  667. return;
  668. }
  669. elsif($f eq "listhelp") {
  670. listhelp();
  671. return;
  672. }
  673. elsif($f eq "single") {
  674. showonly($s[0]);
  675. return;
  676. }
  677. elsif($f eq "protos") {
  678. showprotocols();
  679. return;
  680. }
  681. elsif($f eq "listcats") {
  682. listcats();
  683. return;
  684. }
  685. print "Usage: gen.pl <mainpage/listhelp/single FILE/protos/listcats> [files]\n";
  686. }
  687. #------------------------------------------------------------------------
  688. my $cmd = shift @ARGV;
  689. my @files = @ARGV; # the rest are the files
  690. # learn all existing options
  691. indexoptions(@files);
  692. getargs($cmd, @files);