gen.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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, "This option overrides $mstr. ");
  331. }
  332. if($examples[0]) {
  333. my $s ="";
  334. $s="s" if($examples[1]);
  335. print "\nExample$s:\n.nf\n";
  336. foreach my $e (@examples) {
  337. $e =~ s!\$URL!https://example.com!g;
  338. print " curl $e\n";
  339. }
  340. print ".fi\n";
  341. }
  342. if($added) {
  343. push @foot, added($standalone, $added);
  344. }
  345. if($foot[0]) {
  346. print "\n";
  347. my $f = join("", @foot);
  348. $f =~ s/ +\z//; # remove trailing space
  349. print "$f\n";
  350. }
  351. return 0;
  352. }
  353. sub getshortlong {
  354. my ($f)=@_;
  355. open(F, "<:crlf", "$f");
  356. my $short;
  357. my $long;
  358. my $help;
  359. my $arg;
  360. my $protocols;
  361. my $category;
  362. while(<F>) {
  363. if(/^Short: (.)/i) {
  364. $short=$1;
  365. }
  366. elsif(/^Long: (.*)/i) {
  367. $long=$1;
  368. }
  369. elsif(/^Help: (.*)/i) {
  370. $help=$1;
  371. }
  372. elsif(/^Arg: (.*)/i) {
  373. $arg=$1;
  374. }
  375. elsif(/^Protocols: (.*)/i) {
  376. $protocols=$1;
  377. }
  378. elsif(/^Category: (.*)/i) {
  379. $category=$1;
  380. }
  381. elsif(/^---/) {
  382. last;
  383. }
  384. }
  385. close(F);
  386. if($short) {
  387. $optshort{$short}=$long;
  388. }
  389. if($long) {
  390. $optlong{$long}=$short;
  391. $helplong{$long}=$help;
  392. $arglong{$long}=$arg;
  393. $protolong{$long}=$protocols;
  394. $catlong{$long}=$category;
  395. }
  396. }
  397. sub indexoptions {
  398. my (@files) = @_;
  399. foreach my $f (@files) {
  400. getshortlong($f);
  401. }
  402. }
  403. sub header {
  404. my ($f)=@_;
  405. open(F, "<:crlf", "$f");
  406. my @d;
  407. while(<F>) {
  408. s/%DATE/$date/g;
  409. s/%VERSION/$version/g;
  410. push @d, $_;
  411. }
  412. close(F);
  413. printdesc(@d);
  414. }
  415. sub listhelp {
  416. print <<HEAD
  417. /***************************************************************************
  418. * _ _ ____ _
  419. * Project ___| | | | _ \\| |
  420. * / __| | | | |_) | |
  421. * | (__| |_| | _ <| |___
  422. * \\___|\\___/|_| \\_\\_____|
  423. *
  424. * Copyright (C) 1998 - $year, Daniel Stenberg, <daniel@haxx.se>, et al.
  425. *
  426. * This software is licensed as described in the file COPYING, which
  427. * you should have received as part of this distribution. The terms
  428. * are also available at https://curl.se/docs/copyright.html.
  429. *
  430. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  431. * copies of the Software, and permit persons to whom the Software is
  432. * furnished to do so, under the terms of the COPYING file.
  433. *
  434. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  435. * KIND, either express or implied.
  436. *
  437. ***************************************************************************/
  438. #include "tool_setup.h"
  439. #include "tool_help.h"
  440. /*
  441. * DO NOT edit tool_listhelp.c manually.
  442. * This source file is generated with the following command:
  443. cd \$srcroot/docs/cmdline-opts
  444. ./gen.pl listhelp *.d > \$srcroot/src/tool_listhelp.c
  445. */
  446. const struct helptxt helptext[] = {
  447. HEAD
  448. ;
  449. foreach my $f (sort keys %helplong) {
  450. my $long = $f;
  451. my $short = $optlong{$long};
  452. my @categories = split ' ', $catlong{$long};
  453. my $bitmask;
  454. my $opt;
  455. if(defined($short) && $long) {
  456. $opt = "-$short, --$long";
  457. }
  458. elsif($long && !$short) {
  459. $opt = " --$long";
  460. }
  461. for my $i (0 .. $#categories) {
  462. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  463. # If not last element, append |
  464. if($i < $#categories) {
  465. $bitmask .= ' | ';
  466. }
  467. }
  468. my $arg = $arglong{$long};
  469. if($arg) {
  470. $opt .= " $arg";
  471. }
  472. my $desc = $helplong{$f};
  473. $desc =~ s/\"/\\\"/g; # escape double quotes
  474. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  475. if(length($opt) > 78) {
  476. print STDERR "WARN: the --$long name is too long\n";
  477. }
  478. elsif(length($desc) > 78) {
  479. print STDERR "WARN: the --$long description is too long\n";
  480. }
  481. print $line;
  482. }
  483. print <<FOOT
  484. { NULL, NULL, CURLHELP_HIDDEN }
  485. };
  486. FOOT
  487. ;
  488. }
  489. sub listcats {
  490. my %allcats;
  491. foreach my $f (sort keys %helplong) {
  492. my @categories = split ' ', $catlong{$f};
  493. foreach (@categories) {
  494. $allcats{$_} = undef;
  495. }
  496. }
  497. my @categories;
  498. foreach my $key (keys %allcats) {
  499. push @categories, $key;
  500. }
  501. @categories = sort @categories;
  502. unshift @categories, 'hidden';
  503. for my $i (0..$#categories) {
  504. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  505. }
  506. }
  507. sub mainpage {
  508. my (@files) = @_;
  509. my $ret;
  510. # show the page header
  511. header("page-header");
  512. # output docs for all options
  513. foreach my $f (sort @files) {
  514. $ret += single($f, 0);
  515. }
  516. header("page-footer");
  517. exit $ret if($ret);
  518. }
  519. sub showonly {
  520. my ($f) = @_;
  521. if(single($f, 1)) {
  522. print STDERR "$f: failed\n";
  523. }
  524. }
  525. sub showprotocols {
  526. my %prots;
  527. foreach my $f (keys %optlong) {
  528. my @p = split(/ /, $protolong{$f});
  529. for my $p (@p) {
  530. $prots{$p}++;
  531. }
  532. }
  533. for(sort keys %prots) {
  534. printf "$_ (%d options)\n", $prots{$_};
  535. }
  536. }
  537. sub getargs {
  538. my ($f, @s) = @_;
  539. if($f eq "mainpage") {
  540. mainpage(@s);
  541. return;
  542. }
  543. elsif($f eq "listhelp") {
  544. listhelp();
  545. return;
  546. }
  547. elsif($f eq "single") {
  548. showonly($s[0]);
  549. return;
  550. }
  551. elsif($f eq "protos") {
  552. showprotocols();
  553. return;
  554. }
  555. elsif($f eq "listcats") {
  556. listcats();
  557. return;
  558. }
  559. print "Usage: gen.pl <mainpage/listhelp/single FILE/protos/listcats> [files]\n";
  560. }
  561. #------------------------------------------------------------------------
  562. my $cmd = shift @ARGV;
  563. my @files = @ARGV; # the rest are the files
  564. # learn all existing options
  565. indexoptions(@files);
  566. getargs($cmd, @files);