resetting manifest requested domain to floor
[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.40.10');
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 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 my $version_info = enc_user sprintf _(<<'EOT'), $this_program, $this_version;
157 GNU %s %s
158
159 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2009, 2010,
160 2011, 2012 Free Software Foundation, Inc.
161 This is free software; see the source for copying conditions.  There is NO
162 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
163
164 Written by Brendan O'Dea <bod@debian.org>
165 EOT
166
167 my $help_info = enc_user sprintf _(<<'EOT'), $this_program, $this_program;
168 `%s' generates a man page out of `--help' and `--version' output.
169
170 Usage: %s [OPTION]... EXECUTABLE
171
172  -n, --name=STRING       description for the NAME paragraph
173  -s, --section=SECTION   section number for manual page (1, 6, 8)
174  -m, --manual=TEXT       name of manual (User Commands, ...)
175  -S, --source=TEXT       source of program (FSF, Debian, ...)
176  -L, --locale=STRING     select locale (default "C")
177  -i, --include=FILE      include material from `FILE'
178  -I, --opt-include=FILE  include material from `FILE' if it exists
179  -o, --output=FILE       send output to `FILE'
180  -p, --info-page=TEXT    name of Texinfo manual
181  -N, --no-info           suppress pointer to Texinfo manual
182  -l, --libtool           exclude the `lt-' from the program name
183      --help              print this help, then exit
184      --version           print version number, then exit
185
186 EXECUTABLE should accept `--help' and `--version' options and produce output on
187 stdout although alternatives may be specified using:
188
189  -h, --help-option=STRING     help option string
190  -v, --version-option=STRING  version option string
191  --version-string=STRING      version string
192  --no-discard-stderr          include stderr when parsing option output
193
194 Report bugs to <bug-help2man@gnu.org>.
195 EOT
196
197 my $section = 1;
198 my $manual = '';
199 my $source = '';
200 my $help_option = '--help';
201 my $version_option = '--version';
202 my $discard_stderr = 1;
203 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info, $opt_libtool,
204     $version_text);
205
206 my %opt_def = (
207     'n|name=s'           => \$opt_name,
208     's|section=s'        => \$section,
209     'm|manual=s'         => \$manual,
210     'S|source=s'         => \$source,
211     'L|locale=s'         => sub { configure_locale pop },
212     'i|include=s'        => sub { push @opt_include, [ pop, 1 ] },
213     'I|opt-include=s'    => sub { push @opt_include, [ pop, 0 ] },
214     'o|output=s'         => \$opt_output,
215     'p|info-page=s'      => \$opt_info,
216     'N|no-info'          => \$opt_no_info,
217     'l|libtool'          => \$opt_libtool,
218     'help'               => sub { print $help_info; exit },
219     'version'            => sub { print $version_info; exit },
220     'h|help-option=s'    => \$help_option,
221     'v|version-option=s' => \$version_option,
222     'version-string=s'   => \$version_text,
223     'discard-stderr!'    => \$discard_stderr,
224 );
225
226 # Parse options.
227 Getopt::Long::config('bundling');
228 die $help_info unless GetOptions %opt_def and @ARGV == 1;
229
230 !NO!SUBS!
231
232 print OUT <<'!NO!SUBS!' if $opts{'with-gettext'};
233 configure_locale unless $encoding;
234
235 !NO!SUBS!
236
237 # No substitutions for the remainder of the script:
238 print OUT <<'!NO!SUBS!';
239 my %include = ();
240 my %append = ();
241 my @include = (); # retain order given in include file
242
243 # Process include file (if given).  Format is:
244 #
245 #   [section name]
246 #   verbatim text
247 #
248 # or
249 #
250 #   /pattern/
251 #   verbatim text
252 #
253
254 while (@opt_include)
255 {
256     my ($inc, $required) = @{shift @opt_include};
257
258     next unless -f $inc or $required;
259     kark N_("%s: can't open `%s' (%s)"), $this_program, $inc, $!
260         unless open INC, $inc;
261
262     my $key;
263     my $hash = \%include;
264
265     while (<INC>)
266     {
267         # Convert input to internal Perl format, so that multibyte
268         # sequences are treated as single characters.
269         $_ = dec $_;
270
271         # [section]
272         if (/^\[([^]]+)\]\s*$/)
273         {
274             $key = uc $1;
275             $key =~ s/^\s+//;
276             $key =~ s/\s+$//;
277             $hash = \%include;
278             push @include, $key unless $include{$key};
279             next;
280         }
281
282         # /pattern/
283         if (m!^/(.*)/([ims]*)\s*$!)
284         {
285             my $pat = $2 ? "(?$2)$1" : $1;
286
287             # Check pattern.
288             eval { $key = qr($pat) };
289             if ($@)
290             {
291                 $@ =~ s/ at .*? line \d.*//;
292                 die "$inc:$.:$@";
293             }
294
295             $hash = \%append;
296             next;
297         }
298
299         # Check for options before the first section--anything else is
300         # silently ignored, allowing the first for comments and
301         # revision info.
302         unless ($key)
303         {
304             # handle options
305             if (/^-/)
306             {
307                 local @ARGV = split;
308                 GetOptions %opt_def;
309             }
310
311             next;
312         }
313
314         $hash->{$key} ||= '';
315         $hash->{$key} .= $_;
316     }
317
318     close INC;
319
320     kark N_("%s: no valid information found in `%s'"), $this_program, $inc
321         unless $key;
322 }
323
324 # Compress trailing blank lines.
325 for my $hash (\(%include, %append))
326 {
327     for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
328 }
329
330 sub get_option_value;
331
332 # Grab help and version info from executable.
333 my $help_text   = get_option_value $ARGV[0], $help_option;
334 $version_text ||= get_option_value $ARGV[0], $version_option;
335
336 # Translators: the following message is a strftime(3) format string, which in
337 # the English version expands to the month as a word and the full year.  It
338 # is used on the footer of the generated manual pages.  If in doubt, you may
339 # just use %x as the value (which should be the full locale-specific date).
340 my $date = enc strftime _("%B %Y"), localtime;
341 (my $program = $ARGV[0]) =~ s!.*/!!;
342 my $package = $program;
343 my $version;
344
345 if ($opt_output)
346 {
347     unlink $opt_output or kark N_("%s: can't unlink %s (%s)"),
348         $this_program, $opt_output, $! if -e $opt_output;
349
350     open STDOUT, ">$opt_output"
351         or kark N_("%s: can't create %s (%s)"), $this_program, $opt_output, $!;
352 }
353
354 # The first line of the --version information is assumed to be in one
355 # of the following formats:
356 #
357 #   <version>
358 #   <program> <version>
359 #   {GNU,Free} <program> <version>
360 #   <program> ({GNU,Free} <package>) <version>
361 #   <program> - {GNU,Free} <package> <version>
362 #
363 # and separated from any copyright/author details by a blank line.
364
365 ($_, $version_text) = ((split /\n+/, $version_text, 2), '');
366
367 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
368     /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
369 {
370     $program = $1;
371     $package = $2;
372     $version = $3;
373 }
374 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
375 {
376     $program = $2;
377     $package = $1 ? "$1$2" : $2;
378     $version = $3;
379 }
380 else
381 {
382     $version = $_;
383 }
384
385 $program =~ s!.*/!!;
386
387 # No info for `info' itself.
388 $opt_no_info = 1 if $program eq 'info';
389
390 # Translators: "NAME", "SYNOPSIS" and other one or two word strings in all
391 # upper case are manual page section headings.  The man(1) manual page in your
392 # language, if available should provide the conventional translations.
393 for ($include{_('NAME')})
394 {
395     if ($opt_name) # --name overrides --include contents.
396     {
397         $_ = "$program \\- $opt_name\n";
398     }
399     elsif ($_) # Use first name given as $program
400     {
401         $program = $1 if /^([^\s,]+)(?:,?\s*[^\s,\\-]+)*\s+\\?-/;
402     }
403     else # Set a default (useless) NAME paragraph.
404     {
405         $_ = sprintf _("%s \\- manual page for %s %s") . "\n", $program,
406             $program, $version;
407     }
408 }
409
410 # Man pages traditionally have the page title in caps.
411 my $PROGRAM = uc $program;
412
413 # Set default page head/footers
414 $source ||= "$program $version";
415 unless ($manual)
416 {
417     for ($section)
418     {
419         if (/^(1[Mm]|8)/) { $manual = enc _('System Administration Utilities') }
420         elsif (/^6/)      { $manual = enc _('Games') }
421         else              { $manual = enc _('User Commands') }
422     }
423 }
424
425 # Extract usage clause(s) [if any] for SYNOPSIS.
426 # Translators: "Usage" and "or" here are patterns (regular expressions) which
427 # are used to match the usage synopsis in program output.  An example from cp
428 # (GNU coreutils) which contains both strings:
429 #  Usage: cp [OPTION]... [-T] SOURCE DEST
430 #    or:  cp [OPTION]... SOURCE... DIRECTORY
431 #    or:  cp [OPTION]... -t DIRECTORY SOURCE...
432 my $PAT_USAGE = _('Usage');
433 my $PAT_USAGE_CONT = _('or');
434 if ($help_text =~ s/^($PAT_USAGE):( +(\S+))(.*)((?:\n(?: {6}\1| *($PAT_USAGE_CONT): +\S).*)*)//om)
435 {
436     my @syn = $3 . $4;
437
438     if ($_ = $5)
439     {
440         s/^\n//;
441         for (split /\n/) { s/^ *(($PAT_USAGE_CONT): +)?//o; push @syn, $_ }
442     }
443
444     my $synopsis = '';
445     for (@syn)
446     {
447         $synopsis .= ".br\n" if $synopsis;
448         s!^\S*/!!;
449         s/^lt-// if $opt_libtool;
450         s/^(\S+) *//;
451         $synopsis .= ".B $1\n";
452         s/\s+$//;
453         s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
454         s/^/\\fI/ unless s/^\\fR//;
455         $_ .= '\fR';
456         s/(\\fI)( *)/$2$1/g;
457         s/\\fI\\fR//g;
458         s/^\\fR//;
459         s/\\fI$//;
460         s/^\./\\&./;
461
462         $synopsis .= "$_\n";
463     }
464
465     $include{_('SYNOPSIS')} ||= $synopsis;
466 }
467
468 # Process text, initial section is DESCRIPTION.
469 my $sect = _('DESCRIPTION');
470 $_ = "$help_text\n\n$version_text";
471
472 # Normalise paragraph breaks.
473 s/^\n+//;
474 s/\n*$/\n/;
475 s/\n\n+/\n\n/g;
476
477 # Join hyphenated lines.
478 s/([A-Za-z])-\n *([A-Za-z])/$1$2/g;
479
480 # Temporarily exchange leading dots, apostrophes and backslashes for
481 # tokens.
482 s/^\./\x80/mg;
483 s/^'/\x81/mg;
484 s/\\/\x82/g;
485
486 # Translators: patterns are used to match common program output. In the source
487 # these strings are all of the form of "my $PAT_something = _('...');" and are
488 # regular expressions.  If there is more than one commonly used string, you
489 # may separate alternatives with "|".  Spaces in these expressions are written
490 # as " +" to indicate that more than one space may be matched.  The string
491 # "(?:[\\w-]+ +)?" in the bug reporting pattern is used to indicate an
492 # optional word, so that either "Report bugs" or "Report _program_ bugs" will
493 # be matched.
494 my $PAT_BUGS            = _('Report +(?:[\w-]+ +)?bugs|Email +bug +reports +to');
495 my $PAT_AUTHOR          = _('Written +by');
496 my $PAT_OPTIONS         = _('Options');
497 my $PAT_ENVIRONMENT     = _('Environment');
498 my $PAT_FILES           = _('Files');
499 my $PAT_EXAMPLES        = _('Examples');
500 my $PAT_FREE_SOFTWARE   = _('This +is +free +software');
501
502 # Start a new paragraph (if required) for these.
503 s/([^\n])\n($PAT_BUGS|$PAT_AUTHOR) /$1\n\n$2 /og;
504
505 # Convert iso-8859-1 copyright symbol or (c) to nroff
506 # character.
507 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
508
509 sub convert_option;
510
511 while (length)
512 {
513     # Convert some standard paragraph names.
514     if (s/^($PAT_OPTIONS): *\n//o)
515     {
516         $sect = _('OPTIONS');
517         next;
518     }
519     if (s/^($PAT_ENVIRONMENT): *\n//o)
520     {
521         $sect = _('ENVIRONMENT');
522         next;
523     }
524     if (s/^($PAT_FILES): *\n//o)
525     {
526         $sect = _('FILES');
527         next;
528     }
529     elsif (s/^($PAT_EXAMPLES): *\n//o)
530     {
531         $sect = _('EXAMPLES');
532         next;
533     }
534
535     # Copyright section
536     if (/^Copyright /)
537     {
538         $sect = _('COPYRIGHT');
539     }
540
541     # Bug reporting section.
542     elsif (/^($PAT_BUGS) /o)
543     {
544         $sect = _('REPORTING BUGS');
545     }
546
547     # Author section.
548     elsif (/^($PAT_AUTHOR)/o)
549     {
550         $sect = _('AUTHOR');
551     }
552
553     # Examples, indicated by an indented leading $, % or > are
554     # rendered in a constant width font.
555     if (/^( +)([\$\%>] )\S/)
556     {
557         my $indent = $1;
558         my $prefix = $2;
559         my $break = '.IP';
560         $include{$sect} ||= '';
561         while (s/^$indent\Q$prefix\E(\S.*)\n*//)
562         {
563             $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
564             $break = '.br';
565         }
566
567         next;
568     }
569
570     my $matched = '';
571     $include{$sect} ||= '';
572
573     # Sub-sections have a trailing colon and the second line indented.
574     if (s/^(\S.*:) *\n / /)
575     {
576         $matched .= $& if %append;
577         $include{$sect} .= qq(.SS "$1"\n);
578     }
579
580     my $indent = 0;
581     my $content = '';
582
583     # Option with description.
584     if (s/^( {1,10}([+-]\S.*?))(?:(  +(?!-))|\n( {20,}))(\S.*)\n//)
585     {
586         $matched .= $& if %append;
587         $indent = length ($4 || "$1$3");
588         $content = ".TP\n\x84$2\n\x84$5\n";
589         unless ($4)
590         {
591             # Indent may be different on second line.
592             $indent = length $& if /^ {20,}/;
593         }
594     }
595
596     # Option without description.
597     elsif (s/^ {1,10}([+-]\S.*)\n//)
598     {
599         $matched .= $& if %append;
600         $content = ".HP\n\x84$1\n";
601         $indent = 80; # not continued
602     }
603
604     # Indented paragraph with tag.
605     elsif (s/^( +(\S.*?)  +)(\S.*)\n//)
606     {
607         $matched .= $& if %append;
608         $indent = length $1;
609         $content = ".TP\n\x84$2\n\x84$3\n";
610     }
611
612     # Indented paragraph.
613     elsif (s/^( +)(\S.*)\n//)
614     {
615         $matched .= $& if %append;
616         $indent = length $1;
617         $content = ".IP\n\x84$2\n";
618     }
619
620     # Left justified paragraph.
621     else
622     {
623         s/(.*)\n//;
624         $matched .= $& if %append;
625         $content = ".PP\n" if $include{$sect};
626         $content .= "$1\n";
627     }
628
629     # Append continuations.
630     while ($indent ? s/^ {$indent}(\S.*)\n// : s/^(\S.*)\n//)
631     {
632         $matched .= $& if %append;
633         $content .= "\x84$1\n";
634     }
635
636     # Move to next paragraph.
637     s/^\n+//;
638
639     for ($content)
640     {
641         # Leading dot and apostrophe protection.
642         s/\x84\./\x80/g;
643         s/\x84'/\x81/g;
644         s/\x84//g;
645
646         # Convert options.
647         s/(^| |\()(-[][\w=-]+)/$1 . convert_option $2/mge;
648
649         # Escape remaining hyphens
650         s/-/\x83/g;
651
652         if ($sect eq 'COPYRIGHT')
653         {
654             # Insert line breaks before additional copyright messages
655             # and the disclaimer.
656             s/\n(Copyright |$PAT_FREE_SOFTWARE)/\n.br\n$1/og;
657         }
658         elsif ($sect eq 'REPORTING BUGS')
659         {
660             # Handle multi-line bug reporting sections of the form:
661             #
662             #   Report <program> bugs to <addr>
663             #   GNU <package> home page: <url>
664             #   ...
665             s/\n([[:upper:]])/\n.br\n$1/g;
666         }
667     }
668
669     # Check if matched paragraph contains /pat/.
670     if (%append)
671     {
672         for my $pat (keys %append)
673         {
674             if ($matched =~ $pat)
675             {
676                 $content .= ".PP\n" unless $append{$pat} =~ /^\./;
677                 $content .= $append{$pat};
678             }
679         }
680     }
681
682     $include{$sect} .= $content;
683 }
684
685 # Refer to the real documentation.
686 unless ($opt_no_info)
687 {
688     my $info_page = $opt_info || $program;
689
690     $sect = _('SEE ALSO');
691     $include{$sect} ||= '';
692     $include{$sect} .= ".PP\n" if $include{$sect};
693     $include{$sect} .= sprintf _(<<'EOT'), $program, $program, $info_page;
694 The full documentation for
695 .B %s
696 is maintained as a Texinfo manual.  If the
697 .B info
698 and
699 .B %s
700 programs are properly installed at your site, the command
701 .IP
702 .B info %s
703 .PP
704 should give you access to the complete manual.
705 EOT
706 }
707
708 # Output header.
709 print <<EOT;
710 .\\" DO NOT MODIFY THIS FILE!  It was generated by $this_program $this_version.
711 .TH $PROGRAM "$section" "$date" "$source" "$manual"
712 EOT
713
714 # Section ordering.
715 my @pre = (_('NAME'), _('SYNOPSIS'), _('DESCRIPTION'), _('OPTIONS'),
716     _('ENVIRONMENT'), _('FILES'), _('EXAMPLES'));
717
718 my @post = (_('AUTHOR'), _('REPORTING BUGS'), _('COPYRIGHT'), _('SEE ALSO'));
719 my $filter = join '|', @pre, @post;
720
721 # Output content.
722 for my $sect (@pre, (grep ! /^($filter)$/o, @include), @post)
723 {
724     if ($include{$sect})
725     {
726         my $quote = $sect =~ /\W/ ? '"' : '';
727         print enc ".SH $quote$sect$quote\n";
728
729         for ($include{$sect})
730         {
731             # Replace leading dot, apostrophe, backslash and hyphen
732             # tokens.
733             s/\x80/\\&./g;
734             s/\x81/\\&'/g;
735             s/\x82/\\e/g;
736             s/\x83/\\-/g;
737
738             # Convert some latin1 chars to troff equivalents
739             s/\xa0/\\ /g; # non-breaking space
740
741             print enc $_;
742         }
743     }
744 }
745
746 close STDOUT or kark N_("%s: error writing to %s (%s)"), $this_program,
747     $opt_output || 'stdout', $!;
748
749 exit;
750
751 # Call program with given option and return results.
752 sub get_option_value
753 {
754     my ($prog, $opt) = @_;
755     my $stderr = $discard_stderr ? '/dev/null' : '&1';
756     my $value = join '',
757         map { s/ +$//; expand $_ }
758         map { dec $_ }
759         `$prog $opt 2>$stderr`;
760
761     unless ($value)
762     {
763         my $err = N_("%s: can't get `%s' info from %s%s");
764         my $extra = $discard_stderr
765             ? "\n" . N_("Try `--no-discard-stderr' if option outputs to stderr")
766             : '';
767
768         kark $err, $this_program, $opt, $prog, $extra;
769     }
770
771     return $value;
772 }
773
774 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
775 # embolden.  Option arguments get italicised.
776 sub convert_option
777 {
778     local $_ = '\fB' . shift;
779
780     s/-/\x83/g;
781     unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
782     {
783         s/=(.)/\\fR=\\fI$1/;
784         s/ (.)/ \\fI$1/;
785         $_ .= '\fR';
786     }
787
788     $_;
789 }
790 !NO!SUBS!
791
792 # Rename output and fix permissions
793 unless ($opts{stdout})
794 {
795     close OUT;
796     rename $tmp, $target or die "$0: can't rename $tmp to $target ($!)\n";
797     chmod 0555, $target  or warn "$0: can't change mode of $target ($!)\n";
798 }
799
800 exit 0;