Fix InstrumentSwitch grob definition.
[lilypond.git] / buildscripts / help2man.pl
blob9cb09c4859921da605d4303b1fe44e2dedc7c015
1 #!@PERL@ -w
3 # Generate a short man page from --help and --version output.
4 # Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software
5 # Foundation, Inc.
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 2, or (at your option)
10 # any later version.
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.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software Foundation,
19 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 # Written by Brendan O'Dea <bod@debian.org>
22 # Available from ftp://ftp.gnu.org/gnu/help2man/
24 use 5.005;
25 use strict;
26 use Getopt::Long;
27 use Text::Tabs qw(expand);
28 use POSIX qw(strftime setlocale LC_TIME);
30 my $this_program = 'help2man';
31 my $this_version = '1.28';
32 my $version_info = <<EOT;
33 GNU $this_program $this_version
35 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
36 This is free software; see the source for copying conditions. There is NO
37 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
39 Written by Brendan O'Dea <bod\@debian.org>
40 EOT
42 my $help_info = <<EOT;
43 `$this_program' generates a man page out of `--help' and `--version' output.
45 Usage: $this_program [OPTIONS]... EXECUTABLE
47 -n, --name=STRING description for the NAME paragraph
48 -s, --section=SECTION section number for manual page (1, 6, 8)
49 -m, --manual=TEXT name of manual (User Commands, ...)
50 -S, --source=TEXT source of program (FSF, Debian, ...)
51 -i, --include=FILE include material from `FILE'
52 -I, --opt-include=FILE include material from `FILE' if it exists
53 -o, --output=FILE send output to `FILE'
54 -p, --info-page=TEXT name of Texinfo manual
55 -N, --no-info suppress pointer to Texinfo manual
56 --help print this help, then exit
57 --version print version number, then exit
59 EXECUTABLE should accept `--help' and `--version' options although
60 alternatives may be specified using:
62 -h, --help-option=STRING help option string
63 -v, --version-option=STRING version option string
65 Report bugs to <bug-help2man\@gnu.org>.
66 EOT
68 my $section = 1;
69 my $manual = '';
70 my $source = '';
71 my $help_option = '--help';
72 my $version_option = '--version';
73 my ($opt_name, @opt_include, $opt_output, $opt_info, $opt_no_info);
75 my %opt_def = (
76 'n|name=s' => \$opt_name,
77 's|section=s' => \$section,
78 'm|manual=s' => \$manual,
79 'S|source=s' => \$source,
80 'i|include=s' => sub { push @opt_include, [ pop, 1 ] },
81 'I|opt-include=s' => sub { push @opt_include, [ pop, 0 ] },
82 'o|output=s' => \$opt_output,
83 'p|info-page=s' => \$opt_info,
84 'N|no-info' => \$opt_no_info,
85 'h|help-option=s' => \$help_option,
86 'v|version-option=s' => \$version_option,
89 # Parse options.
90 Getopt::Long::config('bundling');
91 GetOptions (%opt_def,
92 help => sub { print $help_info; exit },
93 version => sub { print $version_info; exit },
94 ) or die $help_info;
96 die $help_info unless @ARGV == 1;
98 my %include = ();
99 my %append = ();
100 my @include = (); # retain order given in include file
102 # Process include file (if given). Format is:
104 # [section name]
105 # verbatim text
107 # or
109 # /pattern/
110 # verbatim text
113 while (@opt_include)
115 my ($inc, $required) = @{shift @opt_include};
117 next unless -f $inc or $required;
118 die "$this_program: can't open `$inc' ($!)\n"
119 unless open INC, $inc;
121 my $key;
122 my $hash = \%include;
124 while (<INC>)
126 # [section]
127 if (/^\[([^]]+)\]/)
129 $key = uc $1;
130 $key =~ s/^\s+//;
131 $key =~ s/\s+$//;
132 $hash = \%include;
133 push @include, $key unless $include{$key};
134 next;
137 # /pattern/
138 if (m!^/(.*)/([ims]*)!)
140 my $pat = $2 ? "(?$2)$1" : $1;
142 # Check pattern.
143 eval { $key = qr($pat) };
144 if ($@)
146 $@ =~ s/ at .*? line \d.*//;
147 die "$inc:$.:$@";
150 $hash = \%append;
151 next;
154 # Check for options before the first section--anything else is
155 # silently ignored, allowing the first for comments and
156 # revision info.
157 unless ($key)
159 # handle options
160 if (/^-/)
162 local @ARGV = split;
163 GetOptions %opt_def;
166 next;
169 $hash->{$key} ||= '';
170 $hash->{$key} .= $_;
173 close INC;
175 die "$this_program: no valid information found in `$inc'\n"
176 unless $key;
179 # Compress trailing blank lines.
180 for my $hash (\(%include, %append))
182 for (keys %$hash) { $hash->{$_} =~ s/\n+$/\n/ }
185 # Turn off localisation of executable's output.
186 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
188 # Turn off localisation of date (for strftime).
189 setlocale LC_TIME, 'C';
191 # Grab help and version info from executable.
192 my ($help_text, $version_text) = map {
193 join '', map { s/ +$//; expand $_ } `$ARGV[0] $_ 2>/dev/null`
194 or die "$this_program: can't get `$_' info from $ARGV[0]\n"
195 } $help_option, $version_option;
197 my $date = strftime "%B %Y", localtime;
198 (my $program = $ARGV[0]) =~ s!.*/!!;
199 my $package = $program;
200 my $version;
202 if ($opt_output)
204 unlink $opt_output
205 or die "$this_program: can't unlink $opt_output ($!)\n"
206 if -e $opt_output;
208 open STDOUT, ">$opt_output"
209 or die "$this_program: can't create $opt_output ($!)\n";
212 # The first line of the --version information is assumed to be in one
213 # of the following formats:
215 # <version>
216 # <program> <version>
217 # {GNU,Free} <program> <version>
218 # <program> ({GNU,Free} <package>) <version>
219 # <program> - {GNU,Free} <package> <version>
221 # and seperated from any copyright/author details by a blank line.
223 ($_, $version_text) = split /\n+/, $version_text, 2;
225 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
226 /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
228 $program = $1;
229 $package = $2;
230 $version = $3;
232 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
234 $program = $2;
235 $package = $1 ? "$1$2" : $2;
236 $version = $3;
238 else
240 $version = $_;
243 $program =~ s!.*/!!;
245 # No info for `info' itself.
246 $opt_no_info = 1 if $program eq 'info';
248 # --name overrides --include contents.
249 $include{NAME} = "$program \\- $opt_name\n" if $opt_name;
251 # Default (useless) NAME paragraph.
252 $include{NAME} ||= "$program \\- manual page for $program $version\n";
254 # Man pages traditionally have the page title in caps.
255 my $PROGRAM = uc $program;
257 # Set default page head/footers
258 $source ||= "$program $version";
259 unless ($manual)
261 for ($section)
263 if (/^(1[Mm]|8)/) { $manual = 'System Administration Utilities' }
264 elsif (/^6/) { $manual = 'Games' }
265 else { $manual = 'User Commands' }
269 # Extract usage clause(s) [if any] for SYNOPSIS.
270 if ($help_text =~ s/^Usage:( +(\S+))(.*)((?:\n(?: {6}\1| *or: +\S).*)*)//m)
272 my @syn = $2 . $3;
274 if ($_ = $4)
276 s/^\n//;
277 for (split /\n/) { s/^ *(or: +)?//; push @syn, $_ }
280 my $synopsis = '';
281 for (@syn)
283 $synopsis .= ".br\n" if $synopsis;
284 s!^\S*/!!;
285 s/^(\S+) *//;
286 $synopsis .= ".B $1\n";
287 s/\s+$//;
288 s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
289 s/^/\\fI/ unless s/^\\fR//;
290 $_ .= '\fR';
291 s/(\\fI)( *)/$2$1/g;
292 s/\\fI\\fR//g;
293 s/^\\fR//;
294 s/\\fI$//;
295 s/^\./\\&./;
297 $synopsis .= "$_\n";
300 $include{SYNOPSIS} ||= $synopsis;
303 # Process text, initial section is DESCRIPTION.
304 my $sect = 'DESCRIPTION';
305 $_ = "$help_text\n\n$version_text";
307 # Normalise paragraph breaks.
308 s/^\n+//;
309 s/\n*$/\n/;
310 s/\n\n+/\n\n/g;
312 # Temporarily exchange leading dots, apostrophes and backslashes for
313 # tokens.
314 s/^\./\x80/mg;
315 s/^'/\x81/mg;
316 s/\\/\x82/g;
318 # Start a new paragraph (if required) for these.
319 s/([^\n])\n(Report +bugs|Email +bug +reports +to|Written +by)/$1\n\n$2/g;
321 sub convert_option;
323 while (length)
325 # Convert some standard paragraph names.
326 if (s/^(Options|Examples): *\n//)
328 $sect = uc $1;
329 next;
332 # Copyright section
333 if (/^Copyright +[(\xa9]/)
335 $sect = 'COPYRIGHT';
336 $include{$sect} ||= '';
337 $include{$sect} .= ".PP\n" if $include{$sect};
339 my $copy;
340 ($copy, $_) = split /\n\n/, $_, 2;
342 for ($copy)
344 # Add back newline
345 s/\n*$/\n/;
347 # Convert iso9959-1 copyright symbol or (c) to nroff
348 # character.
349 s/^Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/mg;
351 # Insert line breaks before additional copyright messages
352 # and the disclaimer.
353 s/(.)\n(Copyright |This +is +free +software)/$1\n.br\n$2/g;
355 # Join hyphenated lines.
356 s/([A-Za-z])-\n */$1/g;
359 $include{$sect} .= $copy;
360 $_ ||= '';
361 next;
364 # Catch bug report text.
365 if (/^(Report +bugs|Email +bug +reports +to) /)
367 $sect = 'REPORTING BUGS';
370 # Author section.
371 elsif (/^Written +by/)
373 $sect = 'AUTHOR';
376 # Examples, indicated by an indented leading $, % or > are
377 # rendered in a constant width font.
378 if (/^( +)([\$\%>] )\S/)
380 my $indent = $1;
381 my $prefix = $2;
382 my $break = '.IP';
383 $include{$sect} ||= '';
384 while (s/^$indent\Q$prefix\E(\S.*)\n*//)
386 $include{$sect} .= "$break\n\\f(CW$prefix$1\\fR\n";
387 $break = '.br';
390 next;
393 my $matched = '';
394 $include{$sect} ||= '';
396 # Sub-sections have a trailing colon and the second line indented.
397 if (s/^(\S.*:) *\n / /)
399 $matched .= $& if %append;
400 $include{$sect} .= qq(.SS "$1"\n);
403 my $indent = 0;
404 my $content = '';
406 # Option with description.
407 if (s/^( {1,10}([+-]\S.*?))(?:( +(?!-))|\n( {20,}))(\S.*)\n//)
409 $matched .= $& if %append;
410 $indent = length ($4 || "$1$3");
411 $content = ".TP\n\x83$2\n\x83$5\n";
412 unless ($4)
414 # Indent may be different on second line.
415 $indent = length $& if /^ {20,}/;
419 # Option without description.
420 elsif (s/^ {1,10}([+-]\S.*)\n//)
422 $matched .= $& if %append;
423 $content = ".HP\n\x83$1\n";
424 $indent = 80; # not continued
427 # Indented paragraph with tag.
428 elsif (s/^( +(\S.*?) +)(\S.*)\n//)
430 $matched .= $& if %append;
431 $indent = length $1;
432 $content = ".TP\n\x83$2\n\x83$3\n";
435 # Indented paragraph.
436 elsif (s/^( +)(\S.*)\n//)
438 $matched .= $& if %append;
439 $indent = length $1;
440 $content = ".IP\n\x83$2\n";
443 # Left justified paragraph.
444 else
446 s/(.*)\n//;
447 $matched .= $& if %append;
448 $content = ".PP\n" if $include{$sect};
449 $content .= "$1\n";
452 # Append continuations.
453 while (s/^ {$indent}(\S.*)\n//)
455 $matched .= $& if %append;
456 $content .= "\x83$1\n"
459 # Move to next paragraph.
460 s/^\n+//;
462 for ($content)
464 # Leading dot and apostrophe protection.
465 s/\x83\./\x80/g;
466 s/\x83'/\x81/g;
467 s/\x83//g;
469 # Convert options.
470 s/(^| )(-[][\w=-]+)/$1 . convert_option $2/mge;
473 # Check if matched paragraph contains /pat/.
474 if (%append)
476 for my $pat (keys %append)
478 if ($matched =~ $pat)
480 $content .= ".PP\n" unless $append{$pat} =~ /^\./;
481 $content .= $append{$pat};
486 $include{$sect} .= $content;
489 # Refer to the real documentation.
490 unless ($opt_no_info)
492 my $info_page = $opt_info || $program;
494 $sect = 'SEE ALSO';
495 $include{$sect} ||= '';
496 $include{$sect} .= ".PP\n" if $include{$sect};
497 $include{$sect} .= <<EOT;
498 The full documentation for
499 .B $program
500 is maintained as a Texinfo manual. If the
501 .B info
503 .B $program
504 programs are properly installed at your site, the command
506 .B info $info_page
508 should give you access to the complete manual.
512 # Output header.
513 print <<EOT;
514 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
515 .TH $PROGRAM "$section" "$date" "$source" "$manual"
518 # Section ordering.
519 my @pre = qw(NAME SYNOPSIS DESCRIPTION OPTIONS EXAMPLES);
520 my @post = ('AUTHOR', 'REPORTING BUGS', 'COPYRIGHT', 'SEE ALSO');
521 my $filter = join '|', @pre, @post;
523 # Output content.
524 for (@pre, (grep ! /^($filter)$/o, @include), @post)
526 if ($include{$_})
528 my $quote = /\W/ ? '"' : '';
529 print ".SH $quote$_$quote\n";
531 for ($include{$_})
533 # Replace leading dot, apostrophe and backslash tokens.
534 s/\x80/\\&./g;
535 s/\x81/\\&'/g;
536 s/\x82/\\e/g;
537 print;
542 exit;
544 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
545 # embolden. Option arguments get italicised.
546 sub convert_option
548 local $_ = '\fB' . shift;
550 s/-/\\-/g;
551 unless (s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
553 s/=(.)/\\fR=\\fI$1/;
554 s/ (.)/ \\fI$1/;
555 $_ .= '\fR';