2
0

help2man 16 KB

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