lilypond-1.3.145
[lilypond.git] / buildscripts / help2man.pl
blob35201b3fb9038f6bd47c6789e70d7090802958d3
1 #!@PERL@
3 # Generate a short man page from --help and --version output.
4 # Copyright © 1997, 98, 99 Free Software Foundation, Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2, or (at your option)
9 # any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 # Written by Brendan O'Dea <bod@compusol.com.au>
22 use 5.004;
23 use strict;
24 use Getopt::Long;
25 use Text::Tabs qw(expand);
26 use POSIX qw(strftime setlocale LC_TIME);
28 my $this_program = 'help2man';
29 my $this_version = '1.012';
30 my $version_info = <<EOT;
31 $this_program $this_version
33 Copyright (C) 1997, 98, 99 Free Software Foundation, Inc.
34 This is free software; see the source for copying conditions. There is NO
35 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
37 Written by Brendan O'Dea <bod\@compusol.com.au>
38 EOT
40 my $help_info = <<EOT;
41 `$this_program' generates a man page out of `--help' and `--version' output.
43 Usage: $this_program [OPTION]... EXECUTABLE
45 -n, --name=STRING use `STRING' as the description for the NAME paragraph
46 -s, --section=SECTION use `SECTION' as the section for the man page
47 -i, --include=FILE include material from `FILE'
48 -I, --opt-include=FILE include material from `FILE' if it exists
49 -o, --output=FILE send output to `FILE'
50 -N, --no-info suppress pointer to Texinfo manual
51 --help print this help, then exit
52 --version print $this_program program version number, then exit
54 EXECUTABLE should accept `--help' and `version' options.
55 EOT
57 my $section = 1;
58 my ($include, $opt_name, $opt_include, $opt_output, $opt_no_info);
60 # Parse options.
61 Getopt::Long::config('bundling');
62 GetOptions (
63 'n|name=s' => \$opt_name,
64 's|section=s' => \$section,
65 'i|include=s' => \$include,
66 'I|opt-include=s' => \$opt_include,
67 'o|output=s' => \$opt_output,
68 'N|no-info' => \$opt_no_info,
69 help => sub { print $help_info; exit },
70 version => sub { print $version_info; exit },
71 ) or die $help_info;
73 die $help_info unless @ARGV == 1;
75 my %include = ();
76 my @include = (); # to retain order
78 # Process include file (if given). Format is:
80 # [section name]
81 # verbatim text
83 if ($include or $opt_include)
85 if (open INC, $include || $opt_include)
87 my $sect;
89 while (<INC>)
91 if (/^\[([^]]+)\]/)
93 $sect = uc $1;
94 $sect =~ s/^\s+//;
95 $sect =~ s/\s+$//;
96 next;
99 # Silently ignore anything before the first
100 # section--allows for comments and revision info.
101 next unless $sect;
103 push @include, $sect unless $include{$sect};
104 $include{$sect} ||= '';
105 $include{$sect} .= $_;
108 close INC;
110 die "$this_program: no valid information found in `$include'\n"
111 unless %include;
113 # Compress trailing blank lines.
114 for (keys %include)
116 $include{$_} =~ s/\n+$//;
117 $include{$_} .= "\n" unless /^NAME$/;
120 else
122 die "$this_program: can't open `$include' ($!)\n" if $include;
126 # Turn off localisation of executable's ouput.
127 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
129 # Turn off localisation of date (for strftime)
130 setlocale LC_TIME, 'C';
132 # Expand tabs, strip trailing spaces and break into paragraphs
133 sub paragraphs { split /\n\n+/, join '', expand @_ }
135 # Grab help and version paragraphs from executable
136 my @help = paragraphs `$ARGV[0] --help 2>/dev/null`
137 or die "$this_program: can't get `--help' info from $ARGV[0]\n";
139 my @version = paragraphs `$ARGV[0] --version 2>/dev/null`
140 or die "$this_program: can't get `--version' info from $ARGV[0]\n";
142 my $date = strftime "%B %Y", localtime;
143 (my $program = $ARGV[0]) =~ s!.*/!!;
144 my $package = $program;
145 my $version;
147 if ($opt_output)
149 unlink $opt_output
150 or die "$this_program: can't unlink $opt_output ($!)\n"
151 if -e $opt_output;
153 open STDOUT, ">$opt_output"
154 or die "$this_program: can't create $opt_output ($!)\n";
157 # The first line of the --version information is assumed to be in one
158 # of the following formats:
160 # <version>
161 # <program> <version>
162 # {GNU,Free} <program> <version>
163 # <program> ({GNU,Free} <package>) <version>
164 # <program> - {GNU,Free} <package> <version>
166 # and seperated from any copyright/author details by a blank line.
168 $_ = shift @version;
170 if (/^(\S+) +\(((?:GNU|Free) +[^)]+)\) +(.*)/ or
171 /^(\S+) +- *((?:GNU|Free) +\S+) +(.*)/)
173 $program = $1;
174 $package = $2;
175 $version = $3;
177 elsif (/^((?:GNU|Free) +)?(\S+) +(.*)/)
179 $program = $2;
180 $package = $1 ? "$1$2" : $2;
181 $version = $3;
183 else
185 $version = $_;
188 $program =~ s!.*/!!;
190 # no info for `info' itself
191 $opt_no_info = 1 if $program eq 'info';
193 # --name overrides --include contents
194 $include{NAME} = "$program \\- $opt_name" if $opt_name;
196 # Default (useless) NAME paragraph
197 $include{NAME} ||= "$program \\- manual page for $program $version";
199 # Man pages traditionally have the page title in caps.
200 my $PROGRAM = uc $program;
202 # Header.
203 print <<EOT;
204 .\\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
205 .TH $PROGRAM "$section" "$date" "$package $version" FSF
206 .SH NAME
207 $include{NAME}
210 my $break;
211 my $accumulate = 1;
212 my @description = ();
214 sub convert_option;
216 # Output converted --help information.
217 for (@help)
219 chomp;
221 if (s/^Usage: +\S+ +(.*)\n?//)
223 # Turn the usage clause into a synopsis.
224 my $synopsis = '';
226 do {
227 my $syn = $1;
228 $syn =~ s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
229 $syn =~ s/^/\\fI/ unless $syn =~ s/^\\fR//;
230 $syn .= '\fR';
231 $syn =~ s/\\fI( *)\\fR/$1/g;
233 $synopsis .= ".br\n" unless $accumulate;
234 $synopsis .= ".B $program\n";
235 $synopsis .= "$syn\n";
236 $accumulate = 0;
237 } while s/^(?:Usage| *or): +\S+ +(.*)\n?//;
239 # Include file overrides SYNOPSIS.
240 print ".SH SYNOPSIS\n", $include{SYNOPSIS} || $synopsis;
242 # Dump any accumulated description text.
243 print ".SH DESCRIPTION\n";
244 print @description;
246 # Add additional description text from include file.
247 if ($include{DESCRIPTION})
249 print ".PP\n" unless $include{DESCRIPTION} =~ /^\..P/;
250 print $include{DESCRIPTION};
253 $break = 1;
254 next unless $_;
257 # Accumulate text if the synopsis has not been produced yet.
258 if ($accumulate)
260 push @description, ".PP\n" if @description;
261 push @description, "$_\n";
262 next;
265 # Convert some standard paragraph names
266 if (s/^(Options|Examples): *\n//)
268 print qq(.SH \U$1\n);
269 $break = '';
270 next unless length;
273 # Catch bug report text.
274 if (/^Report bugs |^Email bug reports to /)
276 print qq(.SH "REPORTING BUGS"\n$_\n);
277 $break = '';
278 next;
281 # Option subsections have second line indented.
282 if (s/^(\S.*)\n / /)
284 print qq(.SS "$1"\n);
285 $break = '';
288 my $output = '';
289 while (length)
291 my $indent = 0;
293 # Tagged paragraph
294 if (s/^( +(\S.*?) +)(\S.*)\n?//)
296 $indent = length $1;
297 $output .= ".TP\n$2\n$3\n";
298 $break = 1;
301 # Indented paragraph
302 elsif (s/^( +)(\S.*)\n?//)
304 $indent = length $1;
305 $output .= ".IP\n$2\n";
306 $break = 1;
309 # Left justified paragraph
310 else
312 s/(.*)\n?//;
313 $output .= ".PP\n" if $break;
314 $output .= "$1\n";
315 $break = 1;
318 # Continuations
319 $output .= "$1\n" while s/^ {$indent}(\S.*)\n?//;
322 $_ = $output;
324 # Convert options.
325 s/(^| )(-[][\w=-]+)/$1 . convert_option $2/mge;
326 print;
329 # Print any include items other than the ones we have already dealt
330 # with.
331 for (@include)
333 print qq(.SH "$_"\n$include{$_})
334 unless /^(NAME|SYNOPSIS|DESCRIPTION|SEE ALSO)$/;
337 # Refer to the real documentation.
338 if ($include{'SEE ALSO'} or !$opt_no_info)
340 print qq(.SH "SEE ALSO"\n);
341 print $include{'SEE ALSO'}, ".PP\n" if $include{'SEE ALSO'};
343 print <<EOT unless $opt_no_info;
344 The full documentation for
345 .B $program
346 is maintained as a Texinfo manual. If the
347 .B info
349 .B $program
350 programs are properly installed at your site, the command
352 .B info $program
354 should give you access to the complete manual.
358 # Output converted --version information.
359 for (@version)
361 chomp;
363 # Join hyphenated lines.
364 s/([A-Za-z])-\n */$1/g;
366 # Convert copyright symbol or (c) to nroff character.
367 s/Copyright +(?:\xa9|\([Cc]\))/Copyright \\(co/g;
369 # Insert appropriate headings for copyright and author.
370 if (/^Copyright \\/) { print ".SH COPYRIGHT\n" }
371 elsif (/^Written +by/) { print ".SH AUTHOR\n" }
372 else { print ".PP\n"; }
374 # Insert line breaks before additional copyright messages and the
375 # disclaimer.
376 s/(.)\n(Copyright |This is free software)/$1\n.br\n$2/g;
378 print "$_\n";
381 exit;
383 # Convert option dashes to \- to stop nroff from hyphenating 'em, and
384 # embolden. Option arguments get italicised.
385 sub convert_option
387 my $option = '\fB' . shift;
389 $option =~ s/-/\\-/g;
390 unless ($option =~ s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
392 $option =~ s/=(.)/\\fR=\\fI$1/;
393 $option =~ s/ (.)/ \\fI$1/;
394 $option .= '\fR';
397 $option;