2
0

managen 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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: managen <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 @ts;
  44. if (defined($ENV{SOURCE_DATE_EPOCH})) {
  45. @ts = gmtime($ENV{SOURCE_DATE_EPOCH});
  46. } else {
  47. @ts = localtime;
  48. }
  49. my $date = strftime "%Y-%m-%d", @ts;
  50. my $year = strftime "%Y", @ts;
  51. my $version = "unknown";
  52. my $globals;
  53. # get the long name version, return the man page string
  54. sub manpageify {
  55. my ($k)=@_;
  56. my $l;
  57. my $trail;
  58. # the matching pattern might include a trailing dot that cannot be part of
  59. # the option name
  60. if($k =~ s/\.$//) {
  61. # cut off trailing dot
  62. $trail = ".";
  63. }
  64. my $klong = $k;
  65. # quote "bare" minuses in the long name
  66. $klong =~ s/-/\\-/g;
  67. if($optlong{$k}) {
  68. # both short + long
  69. $l = "\\fI-".$optlong{$k}.", \\-\\-$klong\\fP$trail";
  70. }
  71. else {
  72. # only long
  73. $l = "\\fI\\-\\-$klong\\fP$trail";
  74. }
  75. return $l;
  76. }
  77. my $colwidth=78; # max number of columns
  78. sub justline {
  79. my ($lvl, @line) = @_;
  80. my $w = -1;
  81. my $spaces = -1;
  82. my $width = $colwidth - ($lvl * 4);
  83. for(@line) {
  84. $w += length($_);
  85. $w++;
  86. $spaces++;
  87. }
  88. my $inject = $width - $w;
  89. my $ratio = 0; # stay at zero if no spaces at all
  90. if($spaces) {
  91. $ratio = $inject / $spaces;
  92. }
  93. my $spare = 0;
  94. print ' ' x ($lvl * 4);
  95. my $prev;
  96. for(@line) {
  97. while($spare >= 0.90) {
  98. print " ";
  99. $spare--;
  100. }
  101. printf "%s%s", $prev?" ":"", $_;
  102. $prev = 1;
  103. $spare += $ratio;
  104. }
  105. print "\n";
  106. }
  107. sub lastline {
  108. my ($lvl, @line) = @_;
  109. print ' ' x ($lvl * 4);
  110. my $prev = 0;
  111. for(@line) {
  112. printf "%s%s", $prev?" ":"", $_;
  113. $prev = 1;
  114. }
  115. print "\n";
  116. }
  117. sub outputpara {
  118. my ($lvl, $f) = @_;
  119. $f =~ s/\n/ /g;
  120. my $w = 0;
  121. my @words = split(/ */, $f);
  122. my $width = $colwidth - ($lvl * 4);
  123. my @line;
  124. for my $e (@words) {
  125. my $l = length($e);
  126. my $spaces = scalar(@line);
  127. if(($w + $l + $spaces) >= $width) {
  128. justline($lvl, @line);
  129. undef @line;
  130. $w = 0;
  131. }
  132. push @line, $e;
  133. $w += $l; # new width
  134. }
  135. if($w) {
  136. lastline($lvl, @line);
  137. print "\n";
  138. }
  139. }
  140. sub printdesc {
  141. my ($manpage, $baselvl, @desc) = @_;
  142. if($manpage) {
  143. for my $d (@desc) {
  144. print $d;
  145. }
  146. }
  147. else {
  148. my $p = -1;
  149. my $para;
  150. for my $l (@desc) {
  151. my $lvl;
  152. if($l !~ /^[\n\r]+/) {
  153. # get the indent level off the string
  154. $l =~ s/^\[([0-9q]*)\]//;
  155. $lvl = $1;
  156. }
  157. if(($p =~ /q/) && ($lvl !~ /q/)) {
  158. # the previous was quoted, this is not
  159. print "\n";
  160. }
  161. if($lvl != $p) {
  162. outputpara($baselvl + $p, $para);
  163. $para = "";
  164. }
  165. if($lvl =~ /q/) {
  166. # quoted, do not right-justify
  167. chomp $l;
  168. lastline($baselvl + $lvl + 1, $l);
  169. }
  170. else {
  171. $para .= $l;
  172. }
  173. $p = $lvl;
  174. }
  175. outputpara($baselvl + $p, $para);
  176. }
  177. }
  178. sub seealso {
  179. my($standalone, $data)=@_;
  180. if($standalone) {
  181. return sprintf
  182. ".SH \"SEE ALSO\"\n$data\n";
  183. }
  184. else {
  185. return "See also $data. ";
  186. }
  187. }
  188. sub overrides {
  189. my ($standalone, $data)=@_;
  190. if($standalone) {
  191. return ".SH \"OVERRIDES\"\n$data\n";
  192. }
  193. else {
  194. return $data;
  195. }
  196. }
  197. sub protocols {
  198. my ($manpage, $standalone, $data)=@_;
  199. if($standalone) {
  200. return ".SH \"PROTOCOLS\"\n$data\n";
  201. }
  202. else {
  203. return "($data) " if($manpage);
  204. return "[1]($data) " if(!$manpage);
  205. }
  206. }
  207. sub too_old {
  208. my ($version)=@_;
  209. my $a = 999999;
  210. if($version =~ /^(\d+)\.(\d+)\.(\d+)/) {
  211. $a = $1 * 1000 + $2 * 10 + $3;
  212. }
  213. elsif($version =~ /^(\d+)\.(\d+)/) {
  214. $a = $1 * 1000 + $2 * 10;
  215. }
  216. if($a < 7500) {
  217. # we consider everything before 7.50.0 to be too old to mention
  218. # specific changes for
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. sub added {
  224. my ($standalone, $data)=@_;
  225. if(too_old($data)) {
  226. # do not mention ancient additions
  227. return "";
  228. }
  229. if($standalone) {
  230. return ".SH \"ADDED\"\nAdded in curl version $data\n";
  231. }
  232. else {
  233. return "Added in $data. ";
  234. }
  235. }
  236. sub render {
  237. my ($manpage, $fh, $f, $line) = @_;
  238. my @desc;
  239. my $tablemode = 0;
  240. my $header = 0;
  241. # if $top is TRUE, it means a top-level page and not a command line option
  242. my $top = ($line == 1);
  243. my $quote;
  244. my $level;
  245. $start = 0;
  246. while(<$fh>) {
  247. my $d = $_;
  248. $line++;
  249. if($d =~ /^\.(SH|BR|IP|B)/) {
  250. print STDERR "$f:$line:1:ERROR: nroff instruction in input: \".$1\"\n";
  251. return 4;
  252. }
  253. if(/^ *<!--/) {
  254. # skip comments
  255. next;
  256. }
  257. if((!$start) && ($_ =~ /^[\r\n]*\z/)) {
  258. # skip leading blank lines
  259. next;
  260. }
  261. $start = 1;
  262. if(/^# (.*)/) {
  263. $header = 1;
  264. if($top != 1) {
  265. # ignored for command line options
  266. $blankline++;
  267. next;
  268. }
  269. push @desc, ".SH $1\n" if($manpage);
  270. push @desc, "[0]$1\n" if(!$manpage);
  271. next;
  272. }
  273. elsif(/^###/) {
  274. print STDERR "$f:$line:1:ERROR: ### header is not supported\n";
  275. exit 3;
  276. }
  277. elsif(/^## (.*)/) {
  278. my $word = $1;
  279. # if there are enclosing quotes, remove them first
  280. $word =~ s/[\"\'](.*)[\"\']\z/$1/;
  281. # remove backticks from headers
  282. $words =~ s/\`//g;
  283. # if there is a space, it needs quotes for man page
  284. if(($word =~ / /) && $manpage) {
  285. $word = "\"$word\"";
  286. }
  287. $level = 1;
  288. if($top == 1) {
  289. push @desc, ".IP $word\n" if($manpage);
  290. push @desc, "\n" if(!$manpage);
  291. push @desc, "[1]$word\n" if(!$manpage);
  292. }
  293. else {
  294. if(!$tablemode) {
  295. push @desc, ".RS\n" if($manpage);
  296. $tablemode = 1;
  297. }
  298. push @desc, ".IP $word\n" if($manpage);
  299. push @desc, "\n" if(!$manpage);
  300. push @desc, "[1]$word\n" if(!$manpage);
  301. }
  302. $header = 1;
  303. next;
  304. }
  305. elsif(/^##/) {
  306. if($top == 1) {
  307. print STDERR "$f:$line:1:ERROR: ## empty header top-level mode\n";
  308. exit 3;
  309. }
  310. if($tablemode) {
  311. # end of table
  312. push @desc, ".RE\n.IP\n";
  313. $tablemode = 0;
  314. }
  315. $header = 1;
  316. next;
  317. }
  318. elsif(/^\.(IP|RS|RE)/) {
  319. my ($cmd) = ($1);
  320. print STDERR "$f:$line:1:ERROR: $cmd detected, use ##-style\n";
  321. return 3;
  322. }
  323. elsif(/^[ \t]*\n/) {
  324. # count and ignore blank lines
  325. $blankline++;
  326. next;
  327. }
  328. elsif($d =~ /^ (.*)/) {
  329. my $word = $1;
  330. if(!$quote && $manpage) {
  331. push @desc, ".nf\n";
  332. }
  333. $quote = 1;
  334. $d = "$word\n";
  335. }
  336. elsif($quote && ($d !~ /^ (.*)/)) {
  337. # end of quote
  338. push @desc, ".fi\n" if($manpage);
  339. $quote = 0;
  340. }
  341. $d =~ s/`%DATE`/$date/g;
  342. $d =~ s/`%VERSION`/$version/g;
  343. $d =~ s/`%GLOBALS`/$globals/g;
  344. # convert backticks to double quotes
  345. $d =~ s/\`/\"/g;
  346. if($d =~ /\(Added in ([0-9.]+)\)/i) {
  347. my $ver = $1;
  348. if(too_old($ver)) {
  349. $d =~ s/ *\(Added in $ver\)//gi;
  350. }
  351. }
  352. if(!$quote) {
  353. if($d =~ /^(.*) /) {
  354. printf STDERR "$f:$line:%d:ERROR: 2 spaces detected\n",
  355. length($1);
  356. return 3;
  357. }
  358. elsif($d =~ /[^\\][\<\>]/) {
  359. print STDERR "$f:$line:1:WARN: un-escaped < or > used\n";
  360. return 3;
  361. }
  362. }
  363. # convert backslash-'<' or '> to just the second character
  364. $d =~ s/\\([><])/$1/g;
  365. # convert single backslash to double-backslash
  366. $d =~ s/\\/\\\\/g if($manpage);
  367. if($manpage) {
  368. if(!$quote && $d =~ /--/) {
  369. $d =~ s/--([a-z0-9.-]+)/manpageify($1)/ge;
  370. }
  371. # quote minuses in the output
  372. $d =~ s/([^\\])-/$1\\-/g;
  373. # replace single quotes
  374. $d =~ s/\'/\\(aq/g;
  375. # handle double quotes or periods first on the line
  376. $d =~ s/^([\.\"])/\\&$1/;
  377. # **bold**
  378. $d =~ s/\*\*(\S.*?)\*\*/\\fB$1\\fP/g;
  379. # *italics*
  380. $d =~ s/\*(\S.*?)\*/\\fI$1\\fP/g;
  381. }
  382. else {
  383. # **bold**
  384. $d =~ s/\*\*(\S.*?)\*\*/$1/g;
  385. # *italics*
  386. $d =~ s/\*(\S.*?)\*/$1/g;
  387. }
  388. # trim trailing spaces
  389. $d =~ s/[ \t]+\z//;
  390. push @desc, "\n" if($blankline && !$header);
  391. $blankline = 0;
  392. push @desc, $d if($manpage);
  393. my $qstr = $quote ? "q": "";
  394. push @desc, "[".(1 + $level)."$qstr]$d" if(!$manpage);
  395. $header = 0;
  396. }
  397. if($tablemode) {
  398. # end of table
  399. push @desc, ".RE\n.IP\n" if($manpage);
  400. }
  401. return @desc;
  402. }
  403. sub single {
  404. my ($dir, $manpage, $f, $standalone)=@_;
  405. my $fh;
  406. open($fh, "<:crlf", "$dir/$f") ||
  407. die "could not find $dir/$f";
  408. my $short;
  409. my $long;
  410. my $tags;
  411. my $added;
  412. my $protocols;
  413. my $arg;
  414. my $mutexed;
  415. my $requires;
  416. my $category;
  417. my @seealso;
  418. my $copyright;
  419. my $spdx;
  420. my @examples; # there can be more than one
  421. my $magic; # cmdline special option
  422. my $line;
  423. my $dline;
  424. my $multi;
  425. my $scope;
  426. my $experimental;
  427. my $start;
  428. my $list; # identifies the list, 1 example, 2 see-also
  429. while(<$fh>) {
  430. $line++;
  431. if(/^ *<!--/) {
  432. next;
  433. }
  434. if(!$start) {
  435. if(/^---/) {
  436. $start = 1;
  437. }
  438. next;
  439. }
  440. if(/^Short: *(.)/i) {
  441. $short=$1;
  442. }
  443. elsif(/^Long: *(.*)/i) {
  444. $long=$1;
  445. }
  446. elsif(/^Added: *(.*)/i) {
  447. $added=$1;
  448. }
  449. elsif(/^Tags: *(.*)/i) {
  450. $tags=$1;
  451. }
  452. elsif(/^Arg: *(.*)/i) {
  453. $arg=$1;
  454. }
  455. elsif(/^Magic: *(.*)/i) {
  456. $magic=$1;
  457. }
  458. elsif(/^Mutexed: *(.*)/i) {
  459. $mutexed=$1;
  460. }
  461. elsif(/^Protocols: *(.*)/i) {
  462. $protocols=$1;
  463. }
  464. elsif(/^See-also: +(.+)/i) {
  465. if($seealso) {
  466. print STDERR "ERROR: duplicated See-also in $f\n";
  467. return 1;
  468. }
  469. push @seealso, $1;
  470. }
  471. elsif(/^See-also:/i) {
  472. $list=2;
  473. }
  474. elsif(/^ *- (.*)/i && ($list == 2)) {
  475. push @seealso, $1;
  476. }
  477. elsif(/^Requires: *(.*)/i) {
  478. $requires=$1;
  479. }
  480. elsif(/^Category: *(.*)/i) {
  481. $category=$1;
  482. }
  483. elsif(/^Example: +(.+)/i) {
  484. push @examples, $1;
  485. }
  486. elsif(/^Example:/i) {
  487. # '1' is the example list
  488. $list = 1;
  489. }
  490. elsif(/^ *- (.*)/i && ($list == 1)) {
  491. push @examples, $1;
  492. }
  493. elsif(/^Multi: *(.*)/i) {
  494. $multi=$1;
  495. }
  496. elsif(/^Scope: *(.*)/i) {
  497. $scope=$1;
  498. }
  499. elsif(/^Experimental: yes/i) {
  500. $experimental=1;
  501. }
  502. elsif(/^C: (.*)/i) {
  503. $copyright=$1;
  504. }
  505. elsif(/^SPDX-License-Identifier: (.*)/i) {
  506. $spdx=$1;
  507. }
  508. elsif(/^Help: *(.*)/i) {
  509. ;
  510. }
  511. elsif(/^---/) {
  512. $start++;
  513. if(!$long) {
  514. print STDERR "ERROR: no 'Long:' in $f\n";
  515. return 1;
  516. }
  517. if(!$category) {
  518. print STDERR "ERROR: no 'Category:' in $f\n";
  519. return 2;
  520. }
  521. if(!$examples[0]) {
  522. print STDERR "$f:$line:1:ERROR: no 'Example:' present\n";
  523. return 2;
  524. }
  525. if(!$added) {
  526. print STDERR "$f:$line:1:ERROR: no 'Added:' version present\n";
  527. return 2;
  528. }
  529. if(!$seealso[0]) {
  530. print STDERR "$f:$line:1:ERROR: no 'See-also:' field present\n";
  531. return 2;
  532. }
  533. if(!$copyright) {
  534. print STDERR "$f:$line:1:ERROR: no 'C:' field present\n";
  535. return 2;
  536. }
  537. if(!$spdx) {
  538. print STDERR "$f:$line:1:ERROR: no 'SPDX-License-Identifier:' field present\n";
  539. return 2;
  540. }
  541. last;
  542. }
  543. else {
  544. chomp;
  545. print STDERR "$f:$line:1:WARN: unrecognized line in $f, ignoring:\n:'$_';"
  546. }
  547. }
  548. if($start < 2) {
  549. print STDERR "$f:1:1:ERROR: no proper meta-data header\n";
  550. return 2;
  551. }
  552. my @desc = render($manpage, $fh, $f, $line);
  553. close($fh);
  554. if($tablemode) {
  555. # end of table
  556. push @desc, ".RE\n.IP\n";
  557. }
  558. my $opt;
  559. if(defined($short) && $long) {
  560. $opt = "-$short, --$long";
  561. }
  562. elsif($short && !$long) {
  563. $opt = "-$short";
  564. }
  565. elsif($long && !$short) {
  566. $opt = "--$long";
  567. }
  568. if($arg) {
  569. $opt .= " $arg";
  570. }
  571. # quote "bare" minuses in opt
  572. $opt =~ s/-/\\-/g if($manpage);
  573. if($standalone) {
  574. print ".TH curl 1 \"30 Nov 2016\" \"curl 7.52.0\" \"curl manual\"\n";
  575. print ".SH OPTION\n";
  576. print "curl $opt\n";
  577. }
  578. elsif($manpage) {
  579. print ".IP \"$opt\"\n";
  580. }
  581. else {
  582. lastline(1, $opt);
  583. }
  584. my @leading;
  585. if($protocols) {
  586. push @leading, protocols($manpage, $standalone, $protocols);
  587. }
  588. if($standalone) {
  589. print ".SH DESCRIPTION\n";
  590. }
  591. if($experimental) {
  592. push @leading, "**WARNING**: this option is experimental. Do not use in production.\n\n";
  593. }
  594. my $pre = $manpage ? "\n": "[1]";
  595. if($scope) {
  596. if($scope eq "global") {
  597. push @desc, "\n" if(!$manpage);
  598. push @desc, "${pre}This option is global and does not need to be specified for each use of --next.\n";
  599. }
  600. else {
  601. print STDERR "$f:$line:1:ERROR: unrecognized scope: '$scope'\n";
  602. return 2;
  603. }
  604. }
  605. printdesc($manpage, 2, (@leading, @desc));
  606. undef @desc;
  607. my @extra;
  608. if($multi eq "single") {
  609. push @extra, "${pre}If --$long is provided several times, the last set ".
  610. "value is used.\n";
  611. }
  612. elsif($multi eq "append") {
  613. push @extra, "${pre}--$long can be used several times in a command line\n";
  614. }
  615. elsif($multi eq "boolean") {
  616. my $rev = "no-$long";
  617. # for options that start with "no-" the reverse is then without
  618. # the no- prefix
  619. if($long =~ /^no-/) {
  620. $rev = $long;
  621. $rev =~ s/^no-//;
  622. }
  623. my $dashes = $manpage ? "\\-\\-" : "--";
  624. push @extra,
  625. "${pre}Providing --$long multiple times has no extra effect.\n".
  626. "Disable it again with $dashes$rev.\n";
  627. }
  628. elsif($multi eq "mutex") {
  629. push @extra,
  630. "${pre}Providing --$long multiple times has no extra effect.\n";
  631. }
  632. elsif($multi eq "custom") {
  633. ; # left for the text to describe
  634. }
  635. else {
  636. print STDERR "$f:$line:1:ERROR: unrecognized Multi: '$multi'\n";
  637. return 2;
  638. }
  639. printdesc($manpage, 2, @extra);
  640. my @foot;
  641. my $mstr;
  642. my $and = 0;
  643. my $num = scalar(@seealso);
  644. if($num > 2) {
  645. # use commas up to this point
  646. $and = $num - 1;
  647. }
  648. my $i = 0;
  649. for my $k (@seealso) {
  650. if(!$helplong{$k}) {
  651. print STDERR "$f:$line:1:WARN: see-also a non-existing option: $k\n";
  652. }
  653. my $l = $manpage ? manpageify($k) : "--$k";
  654. my $sep = " and";
  655. if($and && ($i < $and)) {
  656. $sep = ",";
  657. }
  658. $mstr .= sprintf "%s$l", $mstr?"$sep ":"";
  659. $i++;
  660. }
  661. push @foot, seealso($standalone, $mstr);
  662. if($requires) {
  663. my $l = $manpage ? manpageify($long) : "--$long";
  664. push @foot, "$l requires that the underlying libcurl".
  665. " was built to support $requires. ";
  666. }
  667. if($mutexed) {
  668. my @m=split(/ /, $mutexed);
  669. my $mstr;
  670. for my $k (@m) {
  671. if(!$helplong{$k}) {
  672. print STDERR "WARN: $f mutexes a non-existing option: $k\n";
  673. }
  674. my $l = $manpage ? manpageify($k) : "--$k";
  675. $mstr .= sprintf "%s$l", $mstr?" and ":"";
  676. }
  677. push @foot, overrides($standalone,
  678. "This option is mutually exclusive to $mstr. ");
  679. }
  680. if($examples[0]) {
  681. my $s ="";
  682. $s="s" if($examples[1]);
  683. if($manpage) {
  684. print "\nExample$s:\n";
  685. print ".nf\n";
  686. foreach my $e (@examples) {
  687. $e =~ s!\$URL!https://example.com!g;
  688. # convert single backslahes to doubles
  689. $e =~ s/\\/\\\\/g;
  690. print " curl $e\n";
  691. }
  692. print ".fi\n";
  693. }
  694. else {
  695. my @ex;
  696. push @ex, "[0q]Example$s:\n";
  697. foreach my $e (@examples) {
  698. $e =~ s!\$URL!https://example.com!g;
  699. push @ex, "[0q] curl $e\n";
  700. }
  701. printdesc($manpage, 2, @ex);
  702. }
  703. }
  704. if($added) {
  705. push @foot, added($standalone, $added);
  706. }
  707. if($foot[0]) {
  708. print "\n";
  709. my $f = join("", @foot);
  710. if($manpage) {
  711. $f =~ s/ +\z//; # remove trailing space
  712. print "$f\n";
  713. }
  714. else {
  715. printdesc($manpage, 2, "[1]$f");
  716. }
  717. }
  718. return 0;
  719. }
  720. sub getshortlong {
  721. my ($dir, $f)=@_;
  722. $f =~ s/^.*\///;
  723. open(F, "<:crlf", "$dir/$f") ||
  724. die "could not find $dir/$f";
  725. my $short;
  726. my $long;
  727. my $help;
  728. my $arg;
  729. my $protocols;
  730. my $category;
  731. my $start = 0;
  732. while(<F>) {
  733. if(!$start) {
  734. if(/^---/) {
  735. $start = 1;
  736. }
  737. next;
  738. }
  739. if(/^Short: (.)/i) {
  740. $short=$1;
  741. }
  742. elsif(/^Long: (.*)/i) {
  743. $long=$1;
  744. }
  745. elsif(/^Help: (.*)/i) {
  746. $help=$1;
  747. }
  748. elsif(/^Arg: (.*)/i) {
  749. $arg=$1;
  750. }
  751. elsif(/^Protocols: (.*)/i) {
  752. $protocols=$1;
  753. }
  754. elsif(/^Category: (.*)/i) {
  755. $category=$1;
  756. }
  757. elsif(/^---/) {
  758. last;
  759. }
  760. }
  761. close(F);
  762. if($short) {
  763. $optshort{$short}=$long;
  764. }
  765. if($long) {
  766. $optlong{$long}=$short;
  767. $helplong{$long}=$help;
  768. $arglong{$long}=$arg;
  769. $protolong{$long}=$protocols;
  770. $catlong{$long}=$category;
  771. }
  772. }
  773. sub indexoptions {
  774. my ($dir, @files) = @_;
  775. foreach my $f (@files) {
  776. getshortlong($dir, $f);
  777. }
  778. }
  779. sub header {
  780. my ($dir, $manpage, $f)=@_;
  781. my $fh;
  782. open($fh, "<:crlf", "$dir/$f") ||
  783. die "could not find $dir/$f";
  784. my @d = render($manpage, $fh, $f, 1);
  785. close($fh);
  786. printdesc($manpage, 0, @d);
  787. }
  788. sub listhelp {
  789. print <<HEAD
  790. /***************************************************************************
  791. * _ _ ____ _
  792. * Project ___| | | | _ \\| |
  793. * / __| | | | |_) | |
  794. * | (__| |_| | _ <| |___
  795. * \\___|\\___/|_| \\_\\_____|
  796. *
  797. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  798. *
  799. * This software is licensed as described in the file COPYING, which
  800. * you should have received as part of this distribution. The terms
  801. * are also available at https://curl.se/docs/copyright.html.
  802. *
  803. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  804. * copies of the Software, and permit persons to whom the Software is
  805. * furnished to do so, under the terms of the COPYING file.
  806. *
  807. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  808. * KIND, either express or implied.
  809. *
  810. * SPDX-License-Identifier: curl
  811. *
  812. ***************************************************************************/
  813. #include "tool_setup.h"
  814. #include "tool_help.h"
  815. /*
  816. * DO NOT edit tool_listhelp.c manually.
  817. * This source file is generated with the following command in an autotools
  818. * build:
  819. *
  820. * "make listhelp"
  821. */
  822. const struct helptxt helptext[] = {
  823. HEAD
  824. ;
  825. foreach my $f (sort keys %helplong) {
  826. my $long = $f;
  827. my $short = $optlong{$long};
  828. my @categories = split ' ', $catlong{$long};
  829. my $bitmask = ' ';
  830. my $opt;
  831. if(defined($short) && $long) {
  832. $opt = "-$short, --$long";
  833. }
  834. elsif($long && !$short) {
  835. $opt = " --$long";
  836. }
  837. for my $i (0 .. $#categories) {
  838. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  839. # If not last element, append |
  840. if($i < $#categories) {
  841. $bitmask .= ' | ';
  842. }
  843. }
  844. $bitmask =~ s/(?=.{76}).{1,76}\|/$&\n /g;
  845. my $arg = $arglong{$long};
  846. if($arg) {
  847. $opt .= " $arg";
  848. }
  849. my $desc = $helplong{$f};
  850. $desc =~ s/\"/\\\"/g; # escape double quotes
  851. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  852. if(length($opt) > 78) {
  853. print STDERR "WARN: the --$long name is too long\n";
  854. }
  855. elsif(length($desc) > 78) {
  856. print STDERR "WARN: the --$long description is too long\n";
  857. }
  858. print $line;
  859. }
  860. print <<FOOT
  861. { NULL, NULL, CURLHELP_HIDDEN }
  862. };
  863. FOOT
  864. ;
  865. }
  866. sub listcats {
  867. my %allcats;
  868. foreach my $f (sort keys %helplong) {
  869. my @categories = split ' ', $catlong{$f};
  870. foreach (@categories) {
  871. $allcats{$_} = undef;
  872. }
  873. }
  874. my @categories;
  875. foreach my $key (keys %allcats) {
  876. push @categories, $key;
  877. }
  878. @categories = sort @categories;
  879. unshift @categories, 'hidden';
  880. for my $i (0..$#categories) {
  881. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  882. }
  883. }
  884. sub listglobals {
  885. my ($dir, @files) = @_;
  886. my @globalopts;
  887. # Find all global options and output them
  888. foreach my $f (sort @files) {
  889. open(F, "<:crlf", "$dir/$f") ||
  890. die "could not read $dir/$f";
  891. my $long;
  892. my $start = 0;
  893. while(<F>) {
  894. if(/^---/) {
  895. if(!$start) {
  896. $start = 1;
  897. next;
  898. }
  899. else {
  900. last;
  901. }
  902. }
  903. if(/^Long: *(.*)/i) {
  904. $long=$1;
  905. }
  906. elsif(/^Scope: global/i) {
  907. push @globalopts, $long;
  908. last;
  909. }
  910. }
  911. close(F);
  912. }
  913. return $ret if($ret);
  914. for my $e (0 .. $#globalopts) {
  915. $globals .= sprintf "%s--%s", $e?($globalopts[$e+1] ? ", " : " and "):"",
  916. $globalopts[$e],;
  917. }
  918. }
  919. sub noext {
  920. my $in = $_[0];
  921. $in =~ s/\.md//;
  922. return $in;
  923. }
  924. sub sortnames {
  925. return noext($a) cmp noext($b);
  926. }
  927. sub mainpage {
  928. my ($dir, $manpage, @files) = @_;
  929. # $manpage is 1 for nroff, 0 for ASCII
  930. my $ret;
  931. my $fh;
  932. open($fh, "<:crlf", "$dir/mainpage.idx") ||
  933. die "no $dir/mainpage.idx file";
  934. print <<HEADER
  935. .\\" **************************************************************************
  936. .\\" * _ _ ____ _
  937. .\\" * Project ___| | | | _ \\| |
  938. .\\" * / __| | | | |_) | |
  939. .\\" * | (__| |_| | _ <| |___
  940. .\\" * \\___|\\___/|_| \\_\\_____|
  941. .\\" *
  942. .\\" * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  943. .\\" *
  944. .\\" * This software is licensed as described in the file COPYING, which
  945. .\\" * you should have received as part of this distribution. The terms
  946. .\\" * are also available at https://curl.se/docs/copyright.html.
  947. .\\" *
  948. .\\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  949. .\\" * copies of the Software, and permit persons to whom the Software is
  950. .\\" * furnished to do so, under the terms of the COPYING file.
  951. .\\" *
  952. .\\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  953. .\\" * KIND, either express or implied.
  954. .\\" *
  955. .\\" * SPDX-License-Identifier: curl
  956. .\\" *
  957. .\\" **************************************************************************
  958. .\\"
  959. .\\" DO NOT EDIT. Generated by the curl project managen man page generator.
  960. .\\"
  961. .TH curl 1 "$date" "curl $version" "curl Manual"
  962. HEADER
  963. if ($manpage);
  964. while(<$fh>) {
  965. my $f = $_;
  966. chomp $f;
  967. if($f =~ /^#/) {
  968. # standard comment
  969. next;
  970. }
  971. if(/^%options/) {
  972. # output docs for all options
  973. foreach my $f (sort sortnames @files) {
  974. $ret += single($dir, $manpage, $f, 0);
  975. }
  976. }
  977. else {
  978. # render the file
  979. header($dir, $manpage, $f);
  980. }
  981. }
  982. close($fh);
  983. exit $ret if($ret);
  984. }
  985. sub showonly {
  986. my ($f) = @_;
  987. if(single($f, 1)) {
  988. print STDERR "$f: failed\n";
  989. }
  990. }
  991. sub showprotocols {
  992. my %prots;
  993. foreach my $f (keys %optlong) {
  994. my @p = split(/ /, $protolong{$f});
  995. for my $p (@p) {
  996. $prots{$p}++;
  997. }
  998. }
  999. for(sort keys %prots) {
  1000. printf "$_ (%d options)\n", $prots{$_};
  1001. }
  1002. }
  1003. sub getargs {
  1004. my ($dir, $f, @s) = @_;
  1005. if($f eq "mainpage") {
  1006. listglobals($dir, @s);
  1007. mainpage($dir, 1, @s);
  1008. return;
  1009. }
  1010. elsif($f eq "ascii") {
  1011. listglobals($dir, @s);
  1012. mainpage($dir, 0, @s);
  1013. return;
  1014. }
  1015. elsif($f eq "listhelp") {
  1016. listhelp();
  1017. return;
  1018. }
  1019. elsif($f eq "single") {
  1020. showonly($s[0]);
  1021. return;
  1022. }
  1023. elsif($f eq "protos") {
  1024. showprotocols();
  1025. return;
  1026. }
  1027. elsif($f eq "listcats") {
  1028. listcats();
  1029. return;
  1030. }
  1031. print "Usage: managen ".
  1032. "[-d dir] <mainpage/ascii/listhelp/single FILE/protos/listcats> [files]\n";
  1033. }
  1034. #------------------------------------------------------------------------
  1035. my $dir = ".";
  1036. my $include = "../../include";
  1037. my $cmd = shift @ARGV;
  1038. check:
  1039. if($cmd eq "-d") {
  1040. # specifies source directory
  1041. $dir = shift @ARGV;
  1042. $cmd = shift @ARGV;
  1043. goto check;
  1044. }
  1045. elsif($cmd eq "-I") {
  1046. # include path root
  1047. $include = shift @ARGV;
  1048. $cmd = shift @ARGV;
  1049. goto check;
  1050. }
  1051. my @files = @ARGV; # the rest are the files
  1052. open(INC, "<$include/curl/curlver.h");
  1053. while(<INC>) {
  1054. if($_ =~ /^#define LIBCURL_VERSION \"([0-9.]*)/) {
  1055. $version = $1;
  1056. last;
  1057. }
  1058. }
  1059. close(INC);
  1060. # learn all existing options
  1061. indexoptions($dir, @files);
  1062. getargs($dir, $cmd, @files);