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