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