gen.pl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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 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. 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 "bare" 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. # don't 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. # escape minus
  305. $long =~ s/-/\\-/g;
  306. if(defined($short) && $long) {
  307. $opt = "-$short, --$long";
  308. }
  309. elsif($short && !$long) {
  310. $opt = "-$short";
  311. }
  312. elsif($long && !$short) {
  313. $opt = "--$long";
  314. }
  315. if($arg) {
  316. $opt .= " $arg";
  317. }
  318. # quote "bare" minuses in opt
  319. $opt =~ s/( |^)--/$1\\-\\-/g;
  320. $opt =~ s/( |^)-/$1\\-/g;
  321. if($standalone) {
  322. print ".TH curl 1 \"30 Nov 2016\" \"curl 7.52.0\" \"curl manual\"\n";
  323. print ".SH OPTION\n";
  324. print "curl $opt\n";
  325. }
  326. else {
  327. print ".IP \"$opt\"\n";
  328. }
  329. if($protocols) {
  330. print protocols($standalone, $protocols);
  331. }
  332. if($standalone) {
  333. print ".SH DESCRIPTION\n";
  334. }
  335. if($experimental) {
  336. print "**WARNING**: this option is experimental. Do not use in production.\n\n";
  337. }
  338. printdesc(@desc);
  339. undef @desc;
  340. if($scope) {
  341. if($scope eq "global") {
  342. print "\nThis option is global and does not need to be specified for each use of --next.\n";
  343. }
  344. else {
  345. print STDERR "$f:$line:1:ERROR: unrecognized scope: '$scope'\n";
  346. return 2;
  347. }
  348. }
  349. my @extra;
  350. if($multi eq "single") {
  351. push @extra, "\nIf --$long is provided several times, the last set ".
  352. "value will be used.\n";
  353. }
  354. elsif($multi eq "append") {
  355. push @extra, "\n--$long can be used several times in a command line\n";
  356. }
  357. elsif($multi eq "boolean") {
  358. my $rev = "no-$long";
  359. # for options that start with "no-" the reverse is then without
  360. # the no- prefix
  361. if($long =~ /^no-/) {
  362. $rev = $long;
  363. $rev =~ s/^no-//;
  364. }
  365. push @extra,
  366. "\nProviding --$long multiple times has no extra effect.\n".
  367. "Disable it again with --$rev.\n";
  368. }
  369. elsif($multi eq "mutex") {
  370. push @extra,
  371. "\nProviding --$long multiple times has no extra effect.\n";
  372. }
  373. elsif($multi eq "custom") {
  374. ; # left for the text to describe
  375. }
  376. else {
  377. print STDERR "$f:$line:1:ERROR: unrecognized Multi: '$multi'\n";
  378. return 2;
  379. }
  380. printdesc(@extra);
  381. my @foot;
  382. if($seealso) {
  383. my @m=split(/ /, $seealso);
  384. my $mstr;
  385. my $and = 0;
  386. my $num = scalar(@m);
  387. if($num > 2) {
  388. # use commas up to this point
  389. $and = $num - 1;
  390. }
  391. my $i = 0;
  392. for my $k (@m) {
  393. if(!$helplong{$k}) {
  394. print STDERR "$f:$line:1:WARN: see-also a non-existing option: $k\n";
  395. }
  396. my $l = manpageify($k);
  397. my $sep = " and";
  398. if($and && ($i < $and)) {
  399. $sep = ",";
  400. }
  401. $mstr .= sprintf "%s$l", $mstr?"$sep ":"";
  402. $i++;
  403. }
  404. push @foot, seealso($standalone, $mstr);
  405. }
  406. if($requires) {
  407. my $l = manpageify($long);
  408. push @foot, "$l requires that the underlying libcurl".
  409. " was built to support $requires. ";
  410. }
  411. if($mutexed) {
  412. my @m=split(/ /, $mutexed);
  413. my $mstr;
  414. for my $k (@m) {
  415. if(!$helplong{$k}) {
  416. print STDERR "WARN: $f mutexes a non-existing option: $k\n";
  417. }
  418. my $l = manpageify($k);
  419. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  420. }
  421. push @foot, overrides($standalone,
  422. "This option is mutually exclusive to $mstr. ");
  423. }
  424. if($examples[0]) {
  425. my $s ="";
  426. $s="s" if($examples[1]);
  427. print "\nExample$s:\n.nf\n";
  428. foreach my $e (@examples) {
  429. $e =~ s!\$URL!https://example.com!g;
  430. $e =~ s/\-/\\-/g;
  431. $e =~ s/\'/\\(aq/g;
  432. print " curl $e\n";
  433. }
  434. print ".fi\n";
  435. }
  436. if($added) {
  437. push @foot, added($standalone, $added);
  438. }
  439. if($foot[0]) {
  440. print "\n";
  441. my $f = join("", @foot);
  442. $f =~ s/ +\z//; # remove trailing space
  443. print "$f\n";
  444. }
  445. return 0;
  446. }
  447. sub getshortlong {
  448. my ($f)=@_;
  449. open(F, "<:crlf", "$f");
  450. my $short;
  451. my $long;
  452. my $help;
  453. my $arg;
  454. my $protocols;
  455. my $category;
  456. while(<F>) {
  457. if(/^Short: (.)/i) {
  458. $short=$1;
  459. }
  460. elsif(/^Long: (.*)/i) {
  461. $long=$1;
  462. }
  463. elsif(/^Help: (.*)/i) {
  464. $help=$1;
  465. }
  466. elsif(/^Arg: (.*)/i) {
  467. $arg=$1;
  468. }
  469. elsif(/^Protocols: (.*)/i) {
  470. $protocols=$1;
  471. }
  472. elsif(/^Category: (.*)/i) {
  473. $category=$1;
  474. }
  475. elsif(/^---/) {
  476. last;
  477. }
  478. }
  479. close(F);
  480. if($short) {
  481. $optshort{$short}=$long;
  482. }
  483. if($long) {
  484. $optlong{$long}=$short;
  485. $helplong{$long}=$help;
  486. $arglong{$long}=$arg;
  487. $protolong{$long}=$protocols;
  488. $catlong{$long}=$category;
  489. }
  490. }
  491. sub indexoptions {
  492. my (@files) = @_;
  493. foreach my $f (@files) {
  494. getshortlong($f);
  495. }
  496. }
  497. sub header {
  498. my ($f)=@_;
  499. open(F, "<:crlf", "$f");
  500. my @d;
  501. while(<F>) {
  502. s/%DATE/$date/g;
  503. s/%VERSION/$version/g;
  504. s/%GLOBALS/$globals/g;
  505. push @d, $_;
  506. }
  507. close(F);
  508. printdesc(@d);
  509. }
  510. sub listhelp {
  511. print <<HEAD
  512. /***************************************************************************
  513. * _ _ ____ _
  514. * Project ___| | | | _ \\| |
  515. * / __| | | | |_) | |
  516. * | (__| |_| | _ <| |___
  517. * \\___|\\___/|_| \\_\\_____|
  518. *
  519. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  520. *
  521. * This software is licensed as described in the file COPYING, which
  522. * you should have received as part of this distribution. The terms
  523. * are also available at https://curl.se/docs/copyright.html.
  524. *
  525. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  526. * copies of the Software, and permit persons to whom the Software is
  527. * furnished to do so, under the terms of the COPYING file.
  528. *
  529. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  530. * KIND, either express or implied.
  531. *
  532. * SPDX-License-Identifier: curl
  533. *
  534. ***************************************************************************/
  535. #include "tool_setup.h"
  536. #include "tool_help.h"
  537. /*
  538. * DO NOT edit tool_listhelp.c manually.
  539. * This source file is generated with the following command:
  540. cd \$srcroot/docs/cmdline-opts
  541. ./gen.pl listhelp *.d > \$srcroot/src/tool_listhelp.c
  542. */
  543. const struct helptxt helptext[] = {
  544. HEAD
  545. ;
  546. foreach my $f (sort keys %helplong) {
  547. my $long = $f;
  548. my $short = $optlong{$long};
  549. my @categories = split ' ', $catlong{$long};
  550. my $bitmask = ' ';
  551. my $opt;
  552. if(defined($short) && $long) {
  553. $opt = "-$short, --$long";
  554. }
  555. elsif($long && !$short) {
  556. $opt = " --$long";
  557. }
  558. for my $i (0 .. $#categories) {
  559. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  560. # If not last element, append |
  561. if($i < $#categories) {
  562. $bitmask .= ' | ';
  563. }
  564. }
  565. $bitmask =~ s/(?=.{76}).{1,76}\|/$&\n /g;
  566. my $arg = $arglong{$long};
  567. if($arg) {
  568. $opt .= " $arg";
  569. }
  570. my $desc = $helplong{$f};
  571. $desc =~ s/\"/\\\"/g; # escape double quotes
  572. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  573. if(length($opt) > 78) {
  574. print STDERR "WARN: the --$long name is too long\n";
  575. }
  576. elsif(length($desc) > 78) {
  577. print STDERR "WARN: the --$long description is too long\n";
  578. }
  579. print $line;
  580. }
  581. print <<FOOT
  582. { NULL, NULL, CURLHELP_HIDDEN }
  583. };
  584. FOOT
  585. ;
  586. }
  587. sub listcats {
  588. my %allcats;
  589. foreach my $f (sort keys %helplong) {
  590. my @categories = split ' ', $catlong{$f};
  591. foreach (@categories) {
  592. $allcats{$_} = undef;
  593. }
  594. }
  595. my @categories;
  596. foreach my $key (keys %allcats) {
  597. push @categories, $key;
  598. }
  599. @categories = sort @categories;
  600. unshift @categories, 'hidden';
  601. for my $i (0..$#categories) {
  602. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  603. }
  604. }
  605. sub listglobals {
  606. my (@files) = @_;
  607. my @globalopts;
  608. # Find all global options and output them
  609. foreach my $f (sort @files) {
  610. open(F, "<:crlf", "$f") ||
  611. next;
  612. my $long;
  613. while(<F>) {
  614. if(/^Long: *(.*)/i) {
  615. $long=$1;
  616. }
  617. elsif(/^Scope: global/i) {
  618. push @globalopts, $long;
  619. last;
  620. }
  621. elsif(/^---/) {
  622. last;
  623. }
  624. }
  625. close(F);
  626. }
  627. return $ret if($ret);
  628. for my $e (0 .. $#globalopts) {
  629. $globals .= sprintf "%s--%s", $e?($globalopts[$e+1] ? ", " : " and "):"",
  630. $globalopts[$e],;
  631. }
  632. }
  633. sub mainpage {
  634. my (@files) = @_;
  635. my $ret;
  636. # show the page header
  637. header("page-header");
  638. # output docs for all options
  639. foreach my $f (sort @files) {
  640. $ret += single($f, 0);
  641. }
  642. if(!$ret) {
  643. header("page-footer");
  644. }
  645. exit $ret if($ret);
  646. }
  647. sub showonly {
  648. my ($f) = @_;
  649. if(single($f, 1)) {
  650. print STDERR "$f: failed\n";
  651. }
  652. }
  653. sub showprotocols {
  654. my %prots;
  655. foreach my $f (keys %optlong) {
  656. my @p = split(/ /, $protolong{$f});
  657. for my $p (@p) {
  658. $prots{$p}++;
  659. }
  660. }
  661. for(sort keys %prots) {
  662. printf "$_ (%d options)\n", $prots{$_};
  663. }
  664. }
  665. sub getargs {
  666. my ($f, @s) = @_;
  667. if($f eq "mainpage") {
  668. listglobals(@s);
  669. mainpage(@s);
  670. return;
  671. }
  672. elsif($f eq "listhelp") {
  673. listhelp();
  674. return;
  675. }
  676. elsif($f eq "single") {
  677. showonly($s[0]);
  678. return;
  679. }
  680. elsif($f eq "protos") {
  681. showprotocols();
  682. return;
  683. }
  684. elsif($f eq "listcats") {
  685. listcats();
  686. return;
  687. }
  688. print "Usage: gen.pl <mainpage/listhelp/single FILE/protos/listcats> [files]\n";
  689. }
  690. #------------------------------------------------------------------------
  691. my $cmd = shift @ARGV;
  692. my @files = @ARGV; # the rest are the files
  693. # learn all existing options
  694. indexoptions(@files);
  695. getargs($cmd, @files);