managen 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150
  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. my $line = 0;
  733. while(<F>) {
  734. $line++;
  735. if(!$start) {
  736. if(/^---/) {
  737. $start = 1;
  738. }
  739. next;
  740. }
  741. if(/^Short: (.)/i) {
  742. $short=$1;
  743. }
  744. elsif(/^Long: (.*)/i) {
  745. $long=$1;
  746. }
  747. elsif(/^Help: (.*)/i) {
  748. $help=$1;
  749. my $len = length($help);
  750. if($len >= 49) {
  751. printf STDERR "$f:$line:1:WARN: oversized help text: %d characters\n",
  752. $len;
  753. }
  754. }
  755. elsif(/^Arg: (.*)/i) {
  756. $arg=$1;
  757. }
  758. elsif(/^Protocols: (.*)/i) {
  759. $protocols=$1;
  760. }
  761. elsif(/^Category: (.*)/i) {
  762. $category=$1;
  763. }
  764. elsif(/^---/) {
  765. last;
  766. }
  767. }
  768. close(F);
  769. if($short) {
  770. $optshort{$short}=$long;
  771. }
  772. if($long) {
  773. $optlong{$long}=$short;
  774. $helplong{$long}=$help;
  775. $arglong{$long}=$arg;
  776. $protolong{$long}=$protocols;
  777. $catlong{$long}=$category;
  778. }
  779. }
  780. sub indexoptions {
  781. my ($dir, @files) = @_;
  782. foreach my $f (@files) {
  783. getshortlong($dir, $f);
  784. }
  785. }
  786. sub header {
  787. my ($dir, $manpage, $f)=@_;
  788. my $fh;
  789. open($fh, "<:crlf", "$dir/$f") ||
  790. die "could not find $dir/$f";
  791. my @d = render($manpage, $fh, $f, 1);
  792. close($fh);
  793. printdesc($manpage, 0, @d);
  794. }
  795. sub listhelp {
  796. print <<HEAD
  797. /***************************************************************************
  798. * _ _ ____ _
  799. * Project ___| | | | _ \\| |
  800. * / __| | | | |_) | |
  801. * | (__| |_| | _ <| |___
  802. * \\___|\\___/|_| \\_\\_____|
  803. *
  804. * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  805. *
  806. * This software is licensed as described in the file COPYING, which
  807. * you should have received as part of this distribution. The terms
  808. * are also available at https://curl.se/docs/copyright.html.
  809. *
  810. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  811. * copies of the Software, and permit persons to whom the Software is
  812. * furnished to do so, under the terms of the COPYING file.
  813. *
  814. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  815. * KIND, either express or implied.
  816. *
  817. * SPDX-License-Identifier: curl
  818. *
  819. ***************************************************************************/
  820. #include "tool_setup.h"
  821. #include "tool_help.h"
  822. /*
  823. * DO NOT edit tool_listhelp.c manually.
  824. * This source file is generated with the following command in an autotools
  825. * build:
  826. *
  827. * "make listhelp"
  828. */
  829. const struct helptxt helptext[] = {
  830. HEAD
  831. ;
  832. foreach my $f (sort keys %helplong) {
  833. my $long = $f;
  834. my $short = $optlong{$long};
  835. my @categories = split ' ', $catlong{$long};
  836. my $bitmask = ' ';
  837. my $opt;
  838. if(defined($short) && $long) {
  839. $opt = "-$short, --$long";
  840. }
  841. elsif($long && !$short) {
  842. $opt = " --$long";
  843. }
  844. for my $i (0 .. $#categories) {
  845. $bitmask .= 'CURLHELP_' . uc $categories[$i];
  846. # If not last element, append |
  847. if($i < $#categories) {
  848. $bitmask .= ' | ';
  849. }
  850. }
  851. $bitmask =~ s/(?=.{76}).{1,76}\|/$&\n /g;
  852. my $arg = $arglong{$long};
  853. if($arg) {
  854. $opt .= " $arg";
  855. }
  856. my $desc = $helplong{$f};
  857. $desc =~ s/\"/\\\"/g; # escape double quotes
  858. my $line = sprintf " {\"%s\",\n \"%s\",\n %s},\n", $opt, $desc, $bitmask;
  859. if(length($opt) > 78) {
  860. print STDERR "WARN: the --$long name is too long\n";
  861. }
  862. elsif(length($desc) > 78) {
  863. print STDERR "WARN: the --$long description is too long\n";
  864. }
  865. print $line;
  866. }
  867. print <<FOOT
  868. { NULL, NULL, CURLHELP_HIDDEN }
  869. };
  870. FOOT
  871. ;
  872. }
  873. sub listcats {
  874. my %allcats;
  875. foreach my $f (sort keys %helplong) {
  876. my @categories = split ' ', $catlong{$f};
  877. foreach (@categories) {
  878. $allcats{$_} = undef;
  879. }
  880. }
  881. my @categories;
  882. foreach my $key (keys %allcats) {
  883. push @categories, $key;
  884. }
  885. @categories = sort @categories;
  886. unshift @categories, 'hidden';
  887. for my $i (0..$#categories) {
  888. print '#define ' . 'CURLHELP_' . uc($categories[$i]) . ' ' . "1u << " . $i . "u\n";
  889. }
  890. }
  891. sub listglobals {
  892. my ($dir, @files) = @_;
  893. my @globalopts;
  894. # Find all global options and output them
  895. foreach my $f (sort @files) {
  896. open(F, "<:crlf", "$dir/$f") ||
  897. die "could not read $dir/$f";
  898. my $long;
  899. my $start = 0;
  900. while(<F>) {
  901. if(/^---/) {
  902. if(!$start) {
  903. $start = 1;
  904. next;
  905. }
  906. else {
  907. last;
  908. }
  909. }
  910. if(/^Long: *(.*)/i) {
  911. $long=$1;
  912. }
  913. elsif(/^Scope: global/i) {
  914. push @globalopts, $long;
  915. last;
  916. }
  917. }
  918. close(F);
  919. }
  920. return $ret if($ret);
  921. for my $e (0 .. $#globalopts) {
  922. $globals .= sprintf "%s--%s", $e?($globalopts[$e+1] ? ", " : " and "):"",
  923. $globalopts[$e],;
  924. }
  925. }
  926. sub noext {
  927. my $in = $_[0];
  928. $in =~ s/\.md//;
  929. return $in;
  930. }
  931. sub sortnames {
  932. return noext($a) cmp noext($b);
  933. }
  934. sub mainpage {
  935. my ($dir, $manpage, @files) = @_;
  936. # $manpage is 1 for nroff, 0 for ASCII
  937. my $ret;
  938. my $fh;
  939. open($fh, "<:crlf", "$dir/mainpage.idx") ||
  940. die "no $dir/mainpage.idx file";
  941. print <<HEADER
  942. .\\" **************************************************************************
  943. .\\" * _ _ ____ _
  944. .\\" * Project ___| | | | _ \\| |
  945. .\\" * / __| | | | |_) | |
  946. .\\" * | (__| |_| | _ <| |___
  947. .\\" * \\___|\\___/|_| \\_\\_____|
  948. .\\" *
  949. .\\" * Copyright (C) Daniel Stenberg, <daniel\@haxx.se>, et al.
  950. .\\" *
  951. .\\" * This software is licensed as described in the file COPYING, which
  952. .\\" * you should have received as part of this distribution. The terms
  953. .\\" * are also available at https://curl.se/docs/copyright.html.
  954. .\\" *
  955. .\\" * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  956. .\\" * copies of the Software, and permit persons to whom the Software is
  957. .\\" * furnished to do so, under the terms of the COPYING file.
  958. .\\" *
  959. .\\" * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  960. .\\" * KIND, either express or implied.
  961. .\\" *
  962. .\\" * SPDX-License-Identifier: curl
  963. .\\" *
  964. .\\" **************************************************************************
  965. .\\"
  966. .\\" DO NOT EDIT. Generated by the curl project managen man page generator.
  967. .\\"
  968. .TH curl 1 "$date" "curl $version" "curl Manual"
  969. HEADER
  970. if ($manpage);
  971. while(<$fh>) {
  972. my $f = $_;
  973. chomp $f;
  974. if($f =~ /^#/) {
  975. # standard comment
  976. next;
  977. }
  978. if(/^%options/) {
  979. # output docs for all options
  980. foreach my $f (sort sortnames @files) {
  981. $ret += single($dir, $manpage, $f, 0);
  982. }
  983. }
  984. else {
  985. # render the file
  986. header($dir, $manpage, $f);
  987. }
  988. }
  989. close($fh);
  990. exit $ret if($ret);
  991. }
  992. sub showonly {
  993. my ($f) = @_;
  994. if(single($f, 1)) {
  995. print STDERR "$f: failed\n";
  996. }
  997. }
  998. sub showprotocols {
  999. my %prots;
  1000. foreach my $f (keys %optlong) {
  1001. my @p = split(/ /, $protolong{$f});
  1002. for my $p (@p) {
  1003. $prots{$p}++;
  1004. }
  1005. }
  1006. for(sort keys %prots) {
  1007. printf "$_ (%d options)\n", $prots{$_};
  1008. }
  1009. }
  1010. sub getargs {
  1011. my ($dir, $f, @s) = @_;
  1012. if($f eq "mainpage") {
  1013. listglobals($dir, @s);
  1014. mainpage($dir, 1, @s);
  1015. return;
  1016. }
  1017. elsif($f eq "ascii") {
  1018. listglobals($dir, @s);
  1019. mainpage($dir, 0, @s);
  1020. return;
  1021. }
  1022. elsif($f eq "listhelp") {
  1023. listhelp();
  1024. return;
  1025. }
  1026. elsif($f eq "single") {
  1027. showonly($s[0]);
  1028. return;
  1029. }
  1030. elsif($f eq "protos") {
  1031. showprotocols();
  1032. return;
  1033. }
  1034. elsif($f eq "listcats") {
  1035. listcats();
  1036. return;
  1037. }
  1038. print "Usage: managen ".
  1039. "[-d dir] <mainpage/ascii/listhelp/single FILE/protos/listcats> [files]\n";
  1040. }
  1041. #------------------------------------------------------------------------
  1042. my $dir = ".";
  1043. my $include = "../../include";
  1044. my $cmd = shift @ARGV;
  1045. check:
  1046. if($cmd eq "-d") {
  1047. # specifies source directory
  1048. $dir = shift @ARGV;
  1049. $cmd = shift @ARGV;
  1050. goto check;
  1051. }
  1052. elsif($cmd eq "-I") {
  1053. # include path root
  1054. $include = shift @ARGV;
  1055. $cmd = shift @ARGV;
  1056. goto check;
  1057. }
  1058. my @files = @ARGV; # the rest are the files
  1059. open(INC, "<$include/curl/curlver.h");
  1060. while(<INC>) {
  1061. if($_ =~ /^#define LIBCURL_VERSION \"([0-9.]*)/) {
  1062. $version = $1;
  1063. last;
  1064. }
  1065. }
  1066. close(INC);
  1067. # learn all existing options
  1068. indexoptions($dir, @files);
  1069. getargs($dir, $cmd, @files);