help2man 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. #!/usr/bin/perl -w
  2. # Generate a short man page from --help and --version output.
  3. # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009,
  4. # 2010, 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3, or (at your option)
  8. # any later version.
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, see <https://www.gnu.org/licenses/>.
  15. # Written by Brendan O'Dea <bod@debian.org>
  16. # Available from https://ftp.gnu.org/gnu/help2man/
  17. use 5.008;
  18. use strict;
  19. use Getopt::Long;
  20. use Text::ParseWords qw(shellwords);
  21. use Text::Tabs qw(expand);
  22. use POSIX qw(strftime setlocale LC_ALL);
  23. my $this_program = 'help2man';
  24. my $this_version = '1.47.3';
  25. sub _ { $_[0] }
  26. sub configure_locale
  27. {
  28. my $locale = shift;
  29. die "$this_program: no locale support (Locale::gettext required)\n"
  30. unless $locale eq 'C';
  31. }
  32. sub dec { $_[0] }
  33. sub enc { $_[0] }
  34. sub enc_user { $_[0] }
  35. sub kark { die +(sprintf shift, @_), "\n" }
  36. sub N_ { $_[0] }
  37. sub program_basename;
  38. sub get_option_value;
  39. sub convert_option;
  40. sub fix_italic_spacing;
  41. my $version_info = enc_user sprintf _(<<'EOT'), $this_program, $this_version;
  42. GNU %s %s
  43. Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009, 2010,
  44. 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
  45. This is free software; see the source for copying conditions. There is NO
  46. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  47. Written by Brendan O'Dea <bod@debian.org>
  48. EOT
  49. my $help_info = enc_user sprintf _(<<'EOT'), $this_program, $this_program;
  50. `%s' generates a man page out of `--help' and `--version' output.
  51. Usage: %s [OPTION]... EXECUTABLE
  52. -n, --name=STRING description for the NAME paragraph
  53. -s, --section=SECTION section number for manual page (1, 6, 8)
  54. -m, --manual=TEXT name of manual (User Commands, ...)
  55. -S, --source=TEXT source of program (FSF, Debian, ...)
  56. -L, --locale=STRING select locale (default "C")
  57. -i, --include=FILE include material from `FILE'
  58. -I, --opt-include=FILE include material from `FILE' if it exists
  59. -o, --output=FILE send output to `FILE'
  60. -p, --info-page=TEXT name of Texinfo manual
  61. -N, --no-info suppress pointer to Texinfo manual
  62. -l, --libtool exclude the `lt-' from the program name
  63. --help print this help, then exit
  64. --version print version number, then exit
  65. EXECUTABLE should accept `--help' and `--version' options and produce output on
  66. stdout although alternatives may be specified using:
  67. -h, --help-option=STRING help option string
  68. -v, --version-option=STRING version option string
  69. --version-string=STRING version string
  70. --no-discard-stderr include stderr when parsing option output
  71. Report bugs to <bug-help2man@gnu.org>.
  72. EOT
  73. my $section = 1;
  74. my $manual = '';
  75. my $source = '';
  76. my $help_option = '--help';
  77. my $version_option = '--version';
  78. my $discard_stderr = 1;
  79. my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info, $opt_libtool,
  80. $version_text);
  81. my %opt_def = (
  82. 'n|name=s' => \$opt_name,
  83. 's|section=s' => \$section,
  84. 'm|manual=s' => \$manual,
  85. 'S|source=s' => \$source,
  86. 'L|locale=s' => sub { configure_locale pop },
  87. 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
  88. 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
  89. 'o|output=s' => \$opt_output,
  90. 'p|info-page=s' => \$opt_info,
  91. 'N|no-info' => \$opt_no_info,
  92. 'l|libtool' => \$opt_libtool,
  93. 'help' => sub { print $help_info; exit },
  94. 'version' => sub { print $version_info; exit },
  95. 'h|help-option=s' => \$help_option,
  96. 'v|version-option=s' => \$version_option,
  97. 'version-string=s' => \$version_text,
  98. 'discard-stderr!' => \$discard_stderr,
  99. );
  100. # Parse options.
  101. Getopt::Long::config('bundling');
  102. die $help_info unless GetOptions %opt_def and @ARGV == 1;
  103. my %include = ();
  104. my %replace = ();
  105. my %append = ();
  106. my %append_match = ();
  107. my @sections = (); # retain order of include file or in-line *section*s
  108. # Process include file (if given). Format is:
  109. #
  110. # Optional initial text, ignored. May include lines starting with `-'
  111. # which are processed as options.
  112. #
  113. # [section]
  114. # Verbatim text to be included in the named section. By default at
  115. # the start, but in the case of `name' and `synopsis' the content
  116. # will replace the autogenerated contents.
  117. #
  118. # [<section]
  119. # Verbatim text to be inserted at the start of the named section.
  120. #
  121. # [=section]
  122. # Verbatim text to replace the named section.
  123. #
  124. # [>section]
  125. # Verbatim text to be appended to the end of the named section.
  126. #
  127. # /pattern/
  128. # Verbatim text for inclusion below a paragraph matching `pattern'.
  129. #
  130. while (@opt_include)
  131. {
  132. my ($inc, $required) = @{shift @opt_include};
  133. next unless -f $inc or $required;
  134. kark N_("%s: can't open `%s' (%s)"), $this_program, $inc, $!
  135. unless open INC, $inc;
  136. my $key;
  137. my $hash;
  138. while (<INC>)
  139. {
  140. # Convert input to internal Perl format, so that multibyte
  141. # sequences are treated as single characters.
  142. $_ = dec $_;
  143. # [section]
  144. if (/^\[([^]]+)\]\s*$/)
  145. {
  146. $key = uc $1;
  147. $key =~ s/^\s+//;
  148. $key =~ s/\s+$//;
  149. $hash = \%include;
  150. # Handle explicit [<section], [=section] and [>section]
  151. if ($key =~ s/^([<>=])\s*//)
  152. {
  153. if ($1 eq '>') { $hash = \%append; }
  154. elsif ($1 eq '=') { $hash = \%replace; }
  155. }
  156. # NAME/SYNOPSIS replace by default
  157. elsif ($key eq _('NAME') or $key eq _('SYNOPSIS'))
  158. {
  159. $hash = \%replace;
  160. }
  161. else
  162. {
  163. $hash = \%include;
  164. }
  165. push @sections, $key;
  166. next;
  167. }
  168. # /pattern/
  169. if (m!^/(.*)/([ims]*)\s*$!)
  170. {
  171. my $pat = $2 ? "(?$2)$1" : $1;
  172. # Check pattern.
  173. eval { $key = qr($pat) };
  174. if ($@)
  175. {
  176. $@ =~ s/ at .*? line \d.*//;
  177. die "$inc:$.:$@";
  178. }
  179. $hash = \%append_match;
  180. next;
  181. }
  182. # Check for options before the first section--anything else is
  183. # silently ignored, allowing the first for comments and
  184. # revision info.
  185. unless ($key)
  186. {
  187. # handle options
  188. if (/^-/)
  189. {
  190. local @ARGV = shellwords $_;
  191. GetOptions %opt_def;
  192. }
  193. next;
  194. }
  195. $hash->{$key} .= $_;
  196. }
  197. close INC;
  198. kark N_("%s: no valid information found in `%s'"), $this_program, $inc
  199. unless $key;
  200. }
  201. # Compress trailing blank lines.
  202. for my $hash (\(%include, %replace, %append, %append_match))
  203. {
  204. for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
  205. }
  206. # Grab help and version info from executable.
  207. my $help_text = get_option_value $ARGV[0], $help_option;
  208. $version_text ||= get_option_value $ARGV[0], $version_option;
  209. # By default the generated manual pages will include the current date. This may
  210. # however be overriden by setting the environment variable $SOURCE_DATE_EPOCH
  211. # to an integer value of the seconds since the UNIX epoch. This is primarily
  212. # intended to support reproducible builds (wiki.debian.org/ReproducibleBuilds)
  213. # and will additionally ensure that the output date string is UTC.
  214. my $epoch_secs = time;
  215. if (exists $ENV{SOURCE_DATE_EPOCH} and $ENV{SOURCE_DATE_EPOCH} =~ /^(\d+)$/)
  216. {
  217. $epoch_secs = $1;
  218. $ENV{TZ} = 'UTC0';
  219. }
  220. # Translators: the following message is a strftime(3) format string, which in
  221. # the English version expands to the month as a word and the full year. It
  222. # is used on the footer of the generated manual pages. If in doubt, you may
  223. # just use %x as the value (which should be the full locale-specific date).
  224. my $date = enc strftime _("%B %Y"), localtime $epoch_secs;
  225. my $program = program_basename $ARGV[0];
  226. my $package = $program;
  227. my $version;
  228. if ($opt_output)
  229. {
  230. unlink $opt_output or kark N_("%s: can't unlink %s (%s)"),
  231. $this_program, $opt_output, $! if -e $opt_output;
  232. open STDOUT, ">$opt_output"
  233. or kark N_("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
  234. }
  235. # The first line of the --version information is assumed to be in one
  236. # of the following formats:
  237. #
  238. # <version>
  239. # <program> <version>
  240. # {GNU,Free} <program> <version>
  241. # <program> ({GNU,Free} <package>) <version>
  242. # <program> - {GNU,Free} <package> <version>
  243. #
  244. # and separated from any copyright/author details by a blank line.
  245. ($_, $version_text) = ((split /\n+/, $version_text, 2), '');
  246. if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
  247. /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
  248. {
  249. $program = program_basename $1;
  250. $package = $2;
  251. $version = $3;
  252. }
  253. elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
  254. {
  255. $program = program_basename $2;
  256. $package = $1 ? "$1$program" : $program;
  257. $version = $3;
  258. }
  259. else
  260. {
  261. $version = $_;
  262. }
  263. # No info for `info' itself.
  264. $opt_no_info = 1 if $program eq 'info';
  265. if ($opt_name)
  266. {
  267. # --name overrides --include contents.
  268. $replace{_('NAME')} = "$program \\- $opt_name\n";
  269. }
  270. # Translators: "NAME", "SYNOPSIS" and other one or two word strings in all
  271. # upper case are manual page section headings. The man(1) manual page in your
  272. # language, if available should provide the conventional translations.
  273. for ($replace{_('NAME')} || ($include{_('NAME')} ||= ''))
  274. {
  275. if ($_) # Use first name given as $program
  276. {
  277. $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
  278. }
  279. else # Set a default (useless) NAME paragraph.
  280. {
  281. $_ = sprintf _("%s \\- manual page for %s %s") . "\n", $program,
  282. $program, $version;
  283. }
  284. }
  285. # Man pages traditionally have the page title in caps.
  286. my $PROGRAM = uc $program;
  287. # Set default page head/footers
  288. $source ||= "$program $version";
  289. unless ($manual)
  290. {
  291. for ($section)
  292. {
  293. if (/^(1[Mm]|8)/) { $manual = enc _('System Administration Utilities') }
  294. elsif (/^6/) { $manual = enc _('Games') }
  295. else { $manual = enc _('User Commands') }
  296. }
  297. }
  298. # Extract usage clause(s) [if any] for SYNOPSIS.
  299. # Translators: "Usage" and "or" here are patterns (regular expressions) which
  300. # are used to match the usage synopsis in program output. An example from cp
  301. # (GNU coreutils) which contains both strings:
  302. # Usage: cp [OPTION]... [-T] SOURCE DEST
  303. # or: cp [OPTION]... SOURCE... DIRECTORY
  304. # or: cp [OPTION]... -t DIRECTORY SOURCE...
  305. my $PAT_USAGE = _('Usage');
  306. my $PAT_USAGE_CONT = _('or');
  307. if ($help_text =~ s/^($PAT_USAGE):( +(\S+))(.*)((?:\n(?: {6}\1| *($PAT_USAGE_CONT): +\S).*)*)//om)
  308. {
  309. my @syn = $3 . $4;
  310. if ($_ = $5)
  311. {
  312. s/^\n//;
  313. for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
  314. }
  315. my $synopsis = '';
  316. for (@syn)
  317. {
  318. $synopsis .= ".br\n" if $synopsis;
  319. s!^\S*/!!;
  320. s/^lt-// if $opt_libtool;
  321. s/^(\S+) *//;
  322. $synopsis .= ".B $1\n";
  323. s/\s+$//;
  324. s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
  325. s/^/\\fI/ unless s/^\\fR//;
  326. $_ .= '\fR';
  327. s/(\\fI)( *)/$2$1/g;
  328. s/\\fI\\fR//g;
  329. s/^\\fR//;
  330. s/\\fI$//;
  331. s/^\./\\&./;
  332. $_ = fix_italic_spacing $_;
  333. $synopsis .= "$_\n";
  334. }
  335. $include{_('SYNOPSIS')} .= $synopsis;
  336. }
  337. # Process text, initial section is DESCRIPTION.
  338. my $sect = _('DESCRIPTION');
  339. $_ = "$help_text\n\n$version_text";
  340. # Normalise paragraph breaks.
  341. s/^\n+//;
  342. s/\n*$/\n/;
  343. s/\n\n+/\n\n/g;
  344. # Join hyphenated lines.
  345. s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
  346. # Temporarily exchange leading dots, apostrophes and backslashes for
  347. # tokens.
  348. s/^\./\x80/mg;
  349. s/^'/\x81/mg;
  350. s/\\/\x82/g;
  351. # Translators: patterns are used to match common program output. In the source
  352. # these strings are all of the form of "my $PAT_something = _('...');" and are
  353. # regular expressions. If there is more than one commonly used string, you
  354. # may separate alternatives with "|". Spaces in these expressions are written
  355. # as " +" to indicate that more than one space may be matched. The string
  356. # "(?:[\\w-]+ +)?" in the bug reporting pattern is used to indicate an
  357. # optional word, so that either "Report bugs" or "Report _program_ bugs" will
  358. # be matched.
  359. my $PAT_BUGS = _('Report +(?:[\w-]+ +)?bugs|' .
  360. 'Email +bug +reports +to|' .
  361. '.* +online +help:');
  362. my $PAT_AUTHOR = _('Written +by');
  363. my $PAT_OPTIONS = _('Options');
  364. my $PAT_ENVIRONMENT = _('Environment');
  365. my $PAT_FILES = _('Files');
  366. my $PAT_EXAMPLES = _('Examples');
  367. my $PAT_FREE_SOFTWARE = _('This +is +free +software');
  368. my $PAT_SEE_ALSO = _('Full +documentation');
  369. # Start a new paragraph (if required) for these.
  370. s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR|$PAT_SEE_ALSO) /$1\n\n$2 /og;
  371. # Convert iso-8859-1 copyright symbol or (c) to nroff
  372. # character.
  373. s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
  374. while (length)
  375. {
  376. # Convert some standard paragraph names.
  377. if (s/^($PAT_OPTIONS): *\n+//o)
  378. {
  379. $sect = _('OPTIONS');
  380. next;
  381. }
  382. if (s/^($PAT_ENVIRONMENT): *\n+//o)
  383. {
  384. $sect = _('ENVIRONMENT');
  385. next;
  386. }
  387. if (s/^($PAT_FILES): *\n+//o)
  388. {
  389. $sect = _('FILES');
  390. next;
  391. }
  392. elsif (s/^($PAT_EXAMPLES): *\n+//o)
  393. {
  394. $sect = _('EXAMPLES');
  395. next;
  396. }
  397. # Custom section indicated by a line containing "*Section Name*".
  398. if (s/^\*(\w(.*\w)?)\* *\n+//)
  399. {
  400. $sect = uc $1;
  401. $sect =~ tr/*/ /; # also accept *Section*Name*
  402. push @sections, $sect;
  403. next;
  404. }
  405. # Copyright section.
  406. if (/^Copyright /)
  407. {
  408. $sect = _('COPYRIGHT');
  409. }
  410. # Bug reporting section.
  411. elsif (/^($PAT_BUGS) /o)
  412. {
  413. $sect = _('REPORTING BUGS');
  414. }
  415. # Author section.
  416. elsif (/^($PAT_AUTHOR)/o)
  417. {
  418. $sect = _('AUTHOR');
  419. }
  420. elsif (/^($PAT_SEE_ALSO)/o)
  421. {
  422. $sect = _('SEE ALSO');
  423. $opt_no_info = 1;
  424. }
  425. # Examples, indicated by an indented leading $, % or > are
  426. # rendered in a constant width font.
  427. if (/^( +)([\$\%>] )\S/)
  428. {
  429. my $indent = $1;
  430. my $prefix = $2;
  431. my $break = '.IP';
  432. while (s/^$indent\Q$prefix\E(\S.*)\n*//)
  433. {
  434. $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
  435. $break = '.br';
  436. }
  437. next;
  438. }
  439. my $matched = '';
  440. # Sub-sections have a trailing colon and the second line indented.
  441. if (s/^(\S.*:) *\n / /)
  442. {
  443. $matched .= $& if %append_match;
  444. $include{$sect} .= qq(.SS "$1"\n);
  445. }
  446. my $indent = 0;
  447. my $content = '';
  448. # Option with description.
  449. if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
  450. {
  451. $matched .= $& if %append_match;
  452. $indent = length ($4 || "$1$3");
  453. $content = ".TP\n\x84$2\n\x84$5\n";
  454. unless ($4)
  455. {
  456. # Indent may be different on second line.
  457. $indent = length $& if /^ {20,}/;
  458. }
  459. }
  460. # Option without description.
  461. elsif (s/^ {1,10}([+-]\S.*)\n//)
  462. {
  463. $matched .= $& if %append_match;
  464. $content = ".HP\n\x84$1\n";
  465. $indent = 80; # not continued
  466. }
  467. # Indented paragraph with tag.
  468. elsif (s/^( +(\S.*?) +)(\S.*)\n//)
  469. {
  470. $matched .= $& if %append_match;
  471. $indent = length $1;
  472. $content = ".TP\n\x84$2\n\x84$3\n";
  473. }
  474. # Indented paragraph.
  475. elsif (s/^( +)(\S.*)\n//)
  476. {
  477. $matched .= $& if %append_match;
  478. $indent = length $1;
  479. $content = ".IP\n\x84$2\n";
  480. }
  481. # Left justified paragraph.
  482. else
  483. {
  484. s/(.*)\n//;
  485. $matched .= $& if %append_match;
  486. $content = ".PP\n" if $include{$sect};
  487. $content .= "$1\n";
  488. }
  489. # Append continuations.
  490. while ($indent ? s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
  491. {
  492. $matched .= $& if %append_match;
  493. $content .= "\x84$1\n";
  494. }
  495. # Move to next paragraph.
  496. s/^\n+//;
  497. for ($content)
  498. {
  499. # Leading dot and apostrophe protection.
  500. s/\x84\./\x80/g;
  501. s/\x84'/\x81/g;
  502. s/\x84//g;
  503. # Examples should be verbatim.
  504. unless ($sect eq _('EXAMPLES'))
  505. {
  506. # Convert options.
  507. s/(^|[ (])(-[][\w=-]+)/$1 . convert_option $2/mge;
  508. # Italicise filenames: /a/b, $VAR/c/d, ~/e/f
  509. s!
  510. (^|[ (]) # space/punctuation before
  511. (
  512. (?:\$\w+|~)? # leading variable, or tilde
  513. (?:/\w(?:[\w.-]*\w)?)+ # path components
  514. )
  515. ($|[ ,;.)]) # space/punctuation after
  516. !$1\\fI$2\\fP$3!xmg;
  517. $_ = fix_italic_spacing $_;
  518. }
  519. # Escape remaining hyphens.
  520. s/-/\x83/g;
  521. if ($sect eq _('COPYRIGHT'))
  522. {
  523. # Insert line breaks before additional copyright messages
  524. # and the disclaimer.
  525. s/\n(Copyright |$PAT_FREE_SOFTWARE)/\n.br\n$1/og;
  526. }
  527. elsif ($sect eq _('REPORTING BUGS'))
  528. {
  529. # Handle multi-line bug reporting sections of the form:
  530. #
  531. # Report <program> bugs to <addr>
  532. # GNU <package> home page: <url>
  533. # ...
  534. s/\n([[:upper:]])/\n.br\n$1/g;
  535. }
  536. elsif ($sect eq _('SEE ALSO'))
  537. {
  538. # Handle external references of the form:
  539. #
  540. # GNU <package> online resources: <addr>
  541. # Full documentation at: <addr>
  542. # or available locally via: info ...
  543. #
  544. s/\'/\\(aq/g; # shell quotes for info command
  545. s/\n(.)/\n.br\n$1/g; # separate lines for each item
  546. }
  547. }
  548. # Check if matched paragraph contains /pat/.
  549. if (%append_match)
  550. {
  551. for my $pat (keys %append_match)
  552. {
  553. if ($matched =~ $pat)
  554. {
  555. $content .= ".PP\n" unless $append_match{$pat} =~ /^\./;
  556. $content .= $append_match{$pat};
  557. }
  558. }
  559. }
  560. $include{$sect} .= $content;
  561. }
  562. # Refer to the real documentation.
  563. unless ($opt_no_info)
  564. {
  565. my $info_page = $opt_info || $program;
  566. $sect = _('SEE ALSO');
  567. $include{$sect} .= ".PP\n" if $include{$sect};
  568. $include{$sect} .= sprintf _(<<'EOT'), $program, $program, $info_page;
  569. The full documentation for
  570. .B %s
  571. is maintained as a Texinfo manual. If the
  572. .B info
  573. and
  574. .B %s
  575. programs are properly installed at your site, the command
  576. .IP
  577. .B info %s
  578. .PP
  579. should give you access to the complete manual.
  580. EOT
  581. }
  582. # Append additional text.
  583. while (my ($sect, $text) = each %append)
  584. {
  585. $include{$sect} .= $append{$sect};
  586. }
  587. # Replace sections.
  588. while (my ($sect, $text) = each %replace)
  589. {
  590. $include{$sect} = $replace{$sect};
  591. }
  592. # Output header.
  593. print <<EOT;
  594. .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
  595. .TH $PROGRAM "$section" "$date" "$source" "$manual"
  596. EOT
  597. # Section ordering.
  598. my @pre = (_('NAME'), _('SYNOPSIS'), _('DESCRIPTION'), _('OPTIONS'),
  599. _('EXAMPLES'));
  600. my @post = (_('ENVIRONMENT'), _('FILES'), _('AUTHOR'),
  601. _('REPORTING BUGS'), _('COPYRIGHT'), _('SEE ALSO'));
  602. my %filter = map { $_ => 1 } @pre, @post;
  603. # Output content.
  604. my %done;
  605. for my $sect (@pre, (grep !$filter{$_}, @sections), @post)
  606. {
  607. next if $done{$sect}++; # ignore duplicates
  608. next unless $include{$sect};
  609. if ($include{$sect})
  610. {
  611. my $quote = $sect =~ /\W/ ? '"' : '';
  612. print enc ".SH $quote$sect$quote\n";
  613. for ($include{$sect})
  614. {
  615. # Replace leading dot, apostrophe, backslash and hyphen
  616. # tokens.
  617. s/\x80/\\&./g;
  618. s/\x81/\\&'/g;
  619. s/\x82/\\e/g;
  620. s/\x83/\\-/g;
  621. # Convert some latin1 chars to troff equivalents
  622. s/\xa0/\\ /g; # non-breaking space
  623. print enc $_;
  624. }
  625. }
  626. }
  627. close STDOUT or kark N_("%s: error writing to %s (%s)"), $this_program,
  628. $opt_output || 'stdout', $!;
  629. exit;
  630. # Get program basename, and strip libtool "lt-" prefix if required.
  631. sub program_basename
  632. {
  633. local $_ = shift;
  634. s!.*/!!;
  635. s/^lt-// if $opt_libtool;
  636. $_;
  637. }
  638. # Call program with given option and return results.
  639. sub get_option_value
  640. {
  641. my ($prog, $opt) = @_;
  642. my $stderr = $discard_stderr ? '/dev/null' : '&1';
  643. my $value = join '',
  644. map { s/ +$//; expand $_ }
  645. map { dec $_ }
  646. `$prog $opt 2>$stderr`;
  647. unless ($value)
  648. {
  649. my $err = N_("%s: can't get `%s' info from %s%s");
  650. my $extra = $discard_stderr
  651. ? "\n" . N_("Try `--no-discard-stderr' if option outputs to stderr")
  652. : '';
  653. kark $err, $this_program, $opt, $prog, $extra;
  654. }
  655. $value;
  656. }
  657. # Convert option dashes to \- to stop nroff from hyphenating 'em, and
  658. # embolden. Option arguments get italicised.
  659. sub convert_option
  660. {
  661. local $_ = '\fB' . shift;
  662. s/-/\x83/g;
  663. unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
  664. {
  665. s/=(.)/\\fR=\\fI$1/;
  666. s/ (.)/ \\fI$1/;
  667. $_ .= '\fR';
  668. }
  669. $_;
  670. }
  671. # Insert spacing escape characters \, and \/ before and after italic text. See
  672. # https://www.gnu.org/software/groff/manual/html_node/Ligatures-and-Kerning.html
  673. sub fix_italic_spacing
  674. {
  675. local $_ = shift;
  676. s!\\fI(.*?)\\f([BRP])!\\fI\\,$1\\/\\f$2!g;
  677. return $_;
  678. }