Debian Codegraph @

man/help2man

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