managen 30 KB

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