1#!/usr/bin/perl
2#
3# Special wrapper script to generate the actual pod2man script.  This is
4# required for proper start-up code on non-UNIX platforms, and is used inside
5# Perl core.
6
7use 5.006;
8use strict;
9use warnings;
10
11use Config qw(%Config);
12use Cwd qw(cwd);
13use File::Basename qw(basename dirname);
14
15# List explicitly here the variables you want Configure to generate.
16# Metaconfig only looks for shell variables, so you have to mention them as if
17# they were shell variables, not %Config entries.  Thus you write
18#  $startperl
19# to ensure Configure will look for $Config{startperl}.
20
21# This forces PL files to create target in same directory as PL file.
22# This is so that make depend always knows where to find PL derivatives.
23chdir(dirname($0)) or die "Cannot change directories: $!\n";
24my $file = basename($0, '.PL');
25if ($^O eq 'VMS') {
26    $file .= '.com';
27}
28
29# Create the generated script.
30## no critic (InputOutput::RequireBriefOpen)
31## no critic (InputOutput::RequireCheckedSyscalls)
32open(my $out, '>', $file) or die "Cannot create $file: $!\n";
33print "Extracting $file (with variable substitutions)\n";
34## use critic
35
36# In this section, Perl variables will be expanded during extraction.  You can
37# use $Config{...} to use Configure variables.
38print {$out} <<"PREAMBLE" or die "Cannot write to $file: $!\n";
39$Config{startperl}
40    eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
41        if \$running_under_some_shell;
42PREAMBLE
43
44# In the following, Perl variables are not expanded during extraction.
45print {$out} <<'SCRIPT_BODY' or die "Cannot write to $file: $!\n";
46
47# Convert POD data to formatted *roff input.
48#
49# The driver script for Pod::Man.
50#
51# SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
52
53use 5.006;
54use strict;
55use warnings;
56
57use Getopt::Long qw(GetOptions);
58use Pod::Man ();
59use Pod::Usage qw(pod2usage);
60
61use strict;
62
63# Clean up $0 for error reporting.
64$0 =~ s%.*/%%;
65
66# Insert -- into @ARGV before any single dash argument to hide it from
67# Getopt::Long; we want to interpret it as meaning stdin.
68my $stdin;
69@ARGV = map { $_ eq '-' && !$stdin++ ? ('--', $_) : $_ } @ARGV;
70
71# Parse our options, trying to retain backward compatibility with pod2man but
72# allowing short forms as well.  --lax is currently ignored.
73my %options;
74Getopt::Long::config ('bundling_override');
75GetOptions (\%options, 'center|c=s', 'date|d=s', 'errors=s', 'fixed=s',
76            'fixedbold=s', 'fixeditalic=s', 'fixedbolditalic=s', 'help|h',
77            'lax|l', 'lquote=s', 'name|n=s', 'nourls', 'official|o',
78            'quotes|q=s', 'release|r=s', 'rquote=s', 'section|s=s', 'stderr',
79            'verbose|v', 'utf8|u')
80    or exit 1;
81pod2usage (0) if $options{help};
82
83# Official sets --center, but don't override things explicitly set.
84if ($options{official} && !defined $options{center}) {
85    $options{center} = 'Perl Programmers Reference Guide';
86}
87
88# Verbose is only our flag, not a Pod::Man flag.
89my $verbose = $options{verbose};
90delete $options{verbose};
91
92# This isn't a valid Pod::Man option and is only accepted for backward
93# compatibility.
94delete $options{lax};
95
96# If neither stderr nor errors is set, default to errors = die.
97if (!defined $options{stderr} && !defined $options{errors}) {
98    $options{errors} = 'die';
99}
100
101# Initialize and run the formatter, pulling a pair of input and output off at
102# a time.  For each file, we check whether the document was completely empty
103# and, if so, will remove the created file and exit with a non-zero exit
104# status.
105my $parser = Pod::Man->new (%options);
106my $status = 0;
107my @files;
108do {
109    @files = splice (@ARGV, 0, 2);
110    print "  $files[1]\n" if $verbose;
111    $parser->parse_from_file (@files);
112    if ($parser->{CONTENTLESS}) {
113        $status = 1;
114        if (defined $files[0]) {
115            warn "$0: unable to format $files[0]\n";
116        } else {
117            warn "$0: unable to format standard input\n";
118        }
119        if (defined ($files[1]) and $files[1] ne '-') {
120            unlink $files[1] unless (-s $files[1]);
121        }
122    }
123} while (@ARGV);
124exit $status;
125
126__END__
127
128=for stopwords
129en em --stderr stderr --utf8 UTF-8 overdo markup MT-LEVEL Allbery Solaris URL
130troff troff-specific formatters uppercased Christiansen --nourls UTC prepend
131lquote rquote
132
133=head1 NAME
134
135pod2man - Convert POD data to formatted *roff input
136
137=head1 SYNOPSIS
138
139pod2man [B<--center>=I<string>] [B<--date>=I<string>] [B<--errors>=I<style>]
140    [B<--fixed>=I<font>] [B<--fixedbold>=I<font>] [B<--fixeditalic>=I<font>]
141    [B<--fixedbolditalic>=I<font>] [B<--name>=I<name>] [B<--nourls>]
142    [B<--official>] [B<--release>=I<version>] [B<--section>=I<manext>]
143    [B<--quotes>=I<quotes>] [B<--lquote>=I<quote>] [B<--rquote>=I<quote>]
144    [B<--stderr>] [B<--utf8>] [B<--verbose>] [I<input> [I<output>] ...]
145
146pod2man B<--help>
147
148=head1 DESCRIPTION
149
150B<pod2man> is a front-end for Pod::Man, using it to generate *roff input
151from POD source.  The resulting *roff code is suitable for display on a
152terminal using nroff(1), normally via man(1), or printing using troff(1).
153
154I<input> is the file to read for POD source (the POD can be embedded in
155code).  If I<input> isn't given, it defaults to C<STDIN>.  I<output>, if
156given, is the file to which to write the formatted output.  If I<output>
157isn't given, the formatted output is written to C<STDOUT>.  Several POD
158files can be processed in the same B<pod2man> invocation (saving module
159load and compile times) by providing multiple pairs of I<input> and
160I<output> files on the command line.
161
162B<--section>, B<--release>, B<--center>, B<--date>, and B<--official> can
163be used to set the headers and footers to use; if not given, Pod::Man will
164assume various defaults.  See below or L<Pod::Man> for details.
165
166B<pod2man> assumes that your *roff formatters have a fixed-width font
167named C<CW>.  If yours is called something else (like C<CR>), use
168B<--fixed> to specify it.  This generally only matters for troff output
169for printing.  Similarly, you can set the fonts used for bold, italic, and
170bold italic fixed-width output.
171
172Besides the obvious pod conversions, Pod::Man, and therefore pod2man also
173takes care of formatting func(), func(n), and simple variable references
174like $foo or @bar so you don't have to use code escapes for them; complex
175expressions like C<$fred{'stuff'}> will still need to be escaped, though.
176It also translates dashes that aren't used as hyphens into en dashes, makes
177long dashes--like this--into proper em dashes, fixes "paired quotes," and
178takes care of several other troff-specific tweaks.  See L<Pod::Man> for
179complete information.
180
181=head1 OPTIONS
182
183=over 4
184
185=item B<-c> I<string>, B<--center>=I<string>
186
187Sets the centered page header for the C<.TH> macro to I<string>.  The
188default is "User Contributed Perl Documentation", but also see
189B<--official> below.
190
191=item B<-d> I<string>, B<--date>=I<string>
192
193Set the left-hand footer string for the C<.TH> macro to I<string>.  By
194default, the modification date of the input file will be used, or the
195current date if input comes from C<STDIN>, and will be based on UTC (so
196that the output will be reproducible regardless of local time zone).
197
198=item B<--errors>=I<style>
199
200Set the error handling style.  C<die> says to throw an exception on any
201POD formatting error.  C<stderr> says to report errors on standard error,
202but not to throw an exception.  C<pod> says to include a POD ERRORS
203section in the resulting documentation summarizing the errors.  C<none>
204ignores POD errors entirely, as much as possible.
205
206The default is C<die>.
207
208=item B<--fixed>=I<font>
209
210The fixed-width font to use for verbatim text and code.  Defaults to
211C<CW>.  Some systems may want C<CR> instead.  Only matters for troff(1)
212output.
213
214=item B<--fixedbold>=I<font>
215
216Bold version of the fixed-width font.  Defaults to C<CB>.  Only matters
217for troff(1) output.
218
219=item B<--fixeditalic>=I<font>
220
221Italic version of the fixed-width font (actually, something of a misnomer,
222since most fixed-width fonts only have an oblique version, not an italic
223version).  Defaults to C<CI>.  Only matters for troff(1) output.
224
225=item B<--fixedbolditalic>=I<font>
226
227Bold italic (probably actually oblique) version of the fixed-width font.
228Pod::Man doesn't assume you have this, and defaults to C<CB>.  Some
229systems (such as Solaris) have this font available as C<CX>.  Only matters
230for troff(1) output.
231
232=item B<-h>, B<--help>
233
234Print out usage information.
235
236=item B<-l>, B<--lax>
237
238No longer used.  B<pod2man> used to check its input for validity as a
239manual page, but this should now be done by L<podchecker(1)> instead.
240Accepted for backward compatibility; this option no longer does anything.
241
242=item B<--lquote>=I<quote>
243
244=item B<--rquote>=I<quote>
245
246Sets the quote marks used to surround CE<lt>> text.  B<--lquote> sets the
247left quote mark and B<--rquote> sets the right quote mark.  Either may also
248be set to the special value C<none>, in which case no quote mark is added
249on that side of CE<lt>> text (but the font is still changed for troff
250output).
251
252Also see the B<--quotes> option, which can be used to set both quotes at once.
253If both B<--quotes> and one of the other options is set, B<--lquote> or
254B<--rquote> overrides B<--quotes>.
255
256=item B<-n> I<name>, B<--name>=I<name>
257
258Set the name of the manual page for the C<.TH> macro to I<name>.  Without
259this option, the manual name is set to the uppercased base name of the
260file being converted unless the manual section is 3, in which case the
261path is parsed to see if it is a Perl module path.  If it is, a path like
262C<.../lib/Pod/Man.pm> is converted into a name like C<Pod::Man>.  This
263option, if given, overrides any automatic determination of the name.
264
265Although one does not have to follow this convention, be aware that the
266convention for UNIX man pages for commands is for the man page title to be
267in all-uppercase, even if the command isn't.
268
269This option is probably not useful when converting multiple POD files at
270once.
271
272When converting POD source from standard input, the name will be set to
273C<STDIN> if this option is not provided.  Providing this option is strongly
274recommended to set a meaningful manual page name.
275
276=item B<--nourls>
277
278Normally, LZ<><> formatting codes with a URL but anchor text are formatted
279to show both the anchor text and the URL.  In other words:
280
281    L<foo|http://example.com/>
282
283is formatted as:
284
285    foo <http://example.com/>
286
287This flag, if given, suppresses the URL when anchor text is given, so this
288example would be formatted as just C<foo>.  This can produce less
289cluttered output in cases where the URLs are not particularly important.
290
291=item B<-o>, B<--official>
292
293Set the default header to indicate that this page is part of the standard
294Perl release, if B<--center> is not also given.
295
296=item B<-q> I<quotes>, B<--quotes>=I<quotes>
297
298Sets the quote marks used to surround CE<lt>> text to I<quotes>.  If
299I<quotes> is a single character, it is used as both the left and right
300quote.  Otherwise, it is split in half, and the first half of the string
301is used as the left quote and the second is used as the right quote.
302
303I<quotes> may also be set to the special value C<none>, in which case no
304quote marks are added around CE<lt>> text (but the font is still changed for
305troff output).
306
307Also see the B<--lquote> and B<--rquote> options, which can be used to set the
308left and right quotes independently.  If both B<--quotes> and one of the other
309options is set, B<--lquote> or B<--rquote> overrides B<--quotes>.
310
311=item B<-r> I<version>, B<--release>=I<version>
312
313Set the centered footer for the C<.TH> macro to I<version>.  By default,
314this is set to the version of Perl you run B<pod2man> under.  Setting this
315to the empty string will cause some *roff implementations to use the
316system default value.
317
318Note that some system C<an> macro sets assume that the centered footer
319will be a modification date and will prepend something like "Last
320modified: ".  If this is the case for your target system, you may want to
321set B<--release> to the last modified date and B<--date> to the version
322number.
323
324=item B<-s> I<string>, B<--section>=I<string>
325
326Set the section for the C<.TH> macro.  The standard section numbering
327convention is to use 1 for user commands, 2 for system calls, 3 for
328functions, 4 for devices, 5 for file formats, 6 for games, 7 for
329miscellaneous information, and 8 for administrator commands.  There is a lot
330of variation here, however; some systems (like Solaris) use 4 for file
331formats, 5 for miscellaneous information, and 7 for devices.  Still others
332use 1m instead of 8, or some mix of both.  About the only section numbers
333that are reliably consistent are 1, 2, and 3.
334
335By default, section 1 will be used unless the file ends in C<.pm>, in
336which case section 3 will be selected.
337
338=item B<--stderr>
339
340By default, B<pod2man> dies if any errors are detected in the POD input.
341If B<--stderr> is given and no B<--errors> flag is present, errors are
342sent to standard error, but B<pod2man> does not abort.  This is equivalent
343to C<--errors=stderr> and is supported for backward compatibility.
344
345=item B<-u>, B<--utf8>
346
347By default, B<pod2man> produces the most conservative possible *roff
348output to try to ensure that it will work with as many different *roff
349implementations as possible.  Many *roff implementations cannot handle
350non-ASCII characters, so this means all non-ASCII characters are converted
351either to a *roff escape sequence that tries to create a properly accented
352character (at least for troff output) or to C<X>.
353
354This option says to instead output literal UTF-8 characters.  If your
355*roff implementation can handle it, this is the best output format to use
356and avoids corruption of documents containing non-ASCII characters.
357However, be warned that *roff source with literal UTF-8 characters is not
358supported by many implementations and may even result in segfaults and
359other bad behavior.
360
361Be aware that, when using this option, the input encoding of your POD
362source should be properly declared unless it's US-ASCII.  Pod::Simple will
363attempt to guess the encoding and may be successful if it's Latin-1 or
364UTF-8, but it will warn, which by default results in a B<pod2man> failure.
365Use the C<=encoding> command to declare the encoding.  See L<perlpod(1)>
366for more information.
367
368=item B<-v>, B<--verbose>
369
370Print out the name of each output file as it is being generated.
371
372=back
373
374=head1 EXIT STATUS
375
376As long as all documents processed result in some output, even if that
377output includes errata (a C<POD ERRORS> section generated with
378C<--errors=pod>), B<pod2man> will exit with status 0.  If any of the
379documents being processed do not result in an output document, B<pod2man>
380will exit with status 1.  If there are syntax errors in a POD document
381being processed and the error handling style is set to the default of
382C<die>, B<pod2man> will abort immediately with exit status 255.
383
384=head1 DIAGNOSTICS
385
386If B<pod2man> fails with errors, see L<Pod::Man> and L<Pod::Simple> for
387information about what those errors might mean.
388
389=head1 EXAMPLES
390
391    pod2man program > program.1
392    pod2man SomeModule.pm /usr/perl/man/man3/SomeModule.3
393    pod2man --section=7 note.pod > note.7
394
395If you would like to print out a lot of man page continuously, you probably
396want to set the C and D registers to set contiguous page numbering and
397even/odd paging, at least on some versions of man(7).
398
399    troff -man -rC1 -rD1 perl.1 perldata.1 perlsyn.1 ...
400
401To get index entries on C<STDERR>, turn on the F register, as in:
402
403    troff -man -rF1 perl.1
404
405The indexing merely outputs messages via C<.tm> for each major page,
406section, subsection, item, and any C<XE<lt>E<gt>> directives.  See
407L<Pod::Man> for more details.
408
409=head1 BUGS
410
411Lots of this documentation is duplicated from L<Pod::Man>.
412
413=head1 AUTHOR
414
415Russ Allbery <rra@cpan.org>, based I<very> heavily on the original
416B<pod2man> by Larry Wall and Tom Christiansen.
417
418=head1 COPYRIGHT AND LICENSE
419
420Copyright 1999-2001, 2004, 2006, 2008, 2010, 2012-2018 Russ Allbery
421<rra@cpan.org>
422
423This program is free software; you may redistribute it and/or modify it
424under the same terms as Perl itself.
425
426=head1 SEE ALSO
427
428L<Pod::Man>, L<Pod::Simple>, L<man(1)>, L<nroff(1)>, L<perlpod(1)>,
429L<podchecker(1)>, L<perlpodstyle(1)>, L<troff(1)>, L<man(7)>
430
431The man page documenting the an macro set may be L<man(5)> instead of
432L<man(7)> on your system.
433
434The current version of this script is always available from its web site at
435L<https://www.eyrie.org/~eagle/software/podlators/>.  It is also part of the
436Perl core distribution as of 5.6.0.
437
438=cut
439SCRIPT_BODY
440
441# Finish the generation of the script.
442close($out) or die "Cannot close $file: $!\n";
443chmod(0755, $file) or die "Cannot reset permissions for $file: $!\n";
444if ($Config{'eunicefix'} ne q{:}) {
445    exec("$Config{'eunicefix'} $file");
446}
447
448# Local Variables:
449# copyright-at-end-flag: t
450# End:
451