2
0

help2man 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #!/usr/bin/perl -w
  2. # Generate a short man page from --help and --version output.
  3. # Copyright © 1997, 98 Free Software Foundation, Inc.
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2, or (at your option)
  7. # any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. # You should have received a copy of the GNU General Public License
  13. # along with this program; if not, write to the Free Software Foundation,
  14. # Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. # Written by Brendan O'Dea <bod@compusol.com.au>
  16. use 5.004;
  17. use strict;
  18. use Getopt::Long;
  19. use POSIX qw(strftime setlocale LC_TIME);
  20. my $this_program = 'help2man';
  21. my $this_version = '1.006';
  22. my $version_info = <<EOT;
  23. $this_program $this_version
  24. Copyright (C) 1997, 98 Free Software Foundation, Inc.
  25. This is free software; see the source for copying conditions. There is NO
  26. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  27. Written by Brendan O'Dea <bod\@compusol.com.au>
  28. EOT
  29. my $help_info = <<EOT;
  30. `$this_program' generates a man page out of `--help' and `--version' output.
  31. Usage: $this_program [OPTION]... EXECUTABLE
  32. --name=STRING use `STRING' as the description for the NAME paragraph
  33. --include=FILE include material from `FILE'
  34. --opt-include=FILE include material from `FILE' if it exists
  35. --output=FILE send output to `FILE'
  36. --no-info suppress pointer to Texinfo manual
  37. --help print this help, then exit
  38. --version print $this_program program version number, then exit
  39. EXECUTABLE should accept `--help' and `version' options.
  40. EOT
  41. my ($include, $opt_name, $opt_include, $opt_output, $opt_no_info);
  42. # Parse options.
  43. GetOptions (
  44. 'name=s' => \$opt_name,
  45. 'include=s' => \$include,
  46. 'opt-include=s' => \$opt_include,
  47. 'output=s' => \$opt_output,
  48. 'no-info' => \$opt_no_info,
  49. help => sub { print $help_info; exit },
  50. version => sub { print $version_info; exit },
  51. ) or die $help_info;
  52. die $help_info unless @ARGV == 1;
  53. my %include = ();
  54. my @include = (); # to retain order
  55. # Process include file (if given). Format is:
  56. #
  57. # [section name]
  58. # verbatim text
  59. if ($include or $opt_include)
  60. {
  61. if (open INC, $include || $opt_include)
  62. {
  63. my $sect;
  64. while (<INC>)
  65. {
  66. if (/^\[([^]]+)\]/)
  67. {
  68. $sect = uc $1;
  69. $sect =~ s/^\s+//;
  70. $sect =~ s/\s+$//;
  71. next;
  72. }
  73. # Silently ignore anything before the first
  74. # section--allows for comments and revision info.
  75. next unless $sect;
  76. push @include, $sect unless $include{$sect};
  77. $include{$sect} ||= '';
  78. $include{$sect} .= $_;
  79. }
  80. close INC;
  81. die "$this_program: no valid information found in `$include'\n"
  82. unless %include;
  83. # Compress trailing blank lines.
  84. for (keys %include)
  85. {
  86. $include{$_} =~ s/\n+$//;
  87. $include{$_} .= "\n" unless /^NAME$/;
  88. }
  89. }
  90. else
  91. {
  92. die "$this_program: can't open `$include' ($!)\n" if $include;
  93. }
  94. }
  95. # Turn off localisation of executable's ouput.
  96. @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
  97. # Turn off localisation of date (for strftime)
  98. setlocale LC_TIME, 'C';
  99. # Grab help and version paragraphs from executable
  100. my @help = split /\n\n+/, `$ARGV[0] --help 2>/dev/null`
  101. or die "$this_program: can't get `--help' info from $ARGV[0]\n";
  102. my @version = split /\n\n+/, `$ARGV[0] --version 2>/dev/null`
  103. or die "$this_program: can't get `--version' info from $ARGV[0]\n";
  104. my $date = strftime "%B %Y", localtime;
  105. my $program = $ARGV[0]; $program =~ s!.*/!!;
  106. my $package = $program;
  107. my $version;
  108. if ($opt_output)
  109. {
  110. unlink $opt_output
  111. or die "$this_program: can't unlink $opt_output ($!)\n"
  112. if -e $opt_output;
  113. open STDOUT, ">$opt_output"
  114. or die "$this_program: can't create $opt_output ($!)\n";
  115. }
  116. # The first line of the --version information is assumed to be in one
  117. # of the following formats:
  118. #
  119. # <version>
  120. # <program> <version>
  121. # GNU <program> <version>
  122. # <program> (GNU <package>) <version>
  123. # <program> - GNU <package> <version>
  124. #
  125. # and seperated from any copyright/author details by a blank line.
  126. $_ = shift @version;
  127. if (/^(\S+)\s+\((GNU\s+[^)]+)\)\s+(.*)/ or
  128. /^(\S+)\s+-\s*(GNU\s+\S+)\s+(.*)/)
  129. {
  130. $program = $1;
  131. $package = $2;
  132. $version = $3;
  133. }
  134. elsif (/^(GNU\s+)?(\S+)\s+(.*)/)
  135. {
  136. $program = $2;
  137. $package = $1 ? "$1$2" : $2;
  138. $version = $3;
  139. }
  140. else
  141. {
  142. $version = $_;
  143. }
  144. $program =~ s!.*/!!;
  145. # no info for `info' itself
  146. $opt_no_info = 1 if $program eq 'info';
  147. # --name overrides --include contents
  148. $include{NAME} = "$program \\- $opt_name" if $opt_name;
  149. # Default (useless) NAME paragraph
  150. $include{NAME} ||= "$program \\- manual page for $program $version";
  151. # Man pages traditionally have the page title in caps.
  152. my $PROGRAM = uc $program;
  153. # Header.
  154. print <<EOT;
  155. .\" DO NOT MODIFY THIS FILE! It was generated by $this_program $this_version.
  156. .TH $PROGRAM 1 "$date" "$package $version" "FSF"
  157. .SH NAME
  158. $include{NAME}
  159. EOT
  160. my $accumulate = 1;
  161. my @description = ();
  162. sub convert_option;
  163. # Output converted --help information.
  164. for (@help)
  165. {
  166. chomp;
  167. if (s/^Usage:\s+\S+\s+(.*)\n?//)
  168. {
  169. # Turn the usage clause into a synopsis.
  170. my $synopsis = '';
  171. do {
  172. my $syn = $1;
  173. $syn =~ s/(([][]|\.\.+)+)/\\fR$1\\fI/g;
  174. $syn =~ s/^/\\fI/ unless $syn =~ s/^\\fR//;
  175. $syn .= '\fR';
  176. $syn =~ s/\\fI(\s*)\\fR/$1/g;
  177. $synopsis .= ".br\n" unless $accumulate;
  178. $synopsis .= ".B $program\n";
  179. $synopsis .= "$syn\n";
  180. $accumulate = 0;
  181. } while s/^(?:Usage|\s*or):\s+\S+\s+(.*)\n?//;
  182. # Include file overrides SYNOPSIS.
  183. print ".SH SYNOPSIS\n", $include{SYNOPSIS} || $synopsis;
  184. # Dump any accumulated description text.
  185. print ".SH DESCRIPTION\n";
  186. print @description;
  187. # Add additional description text from include file.
  188. if ($include{DESCRIPTION})
  189. {
  190. print ".PP\n" unless $include{DESCRIPTION} =~ /^\..P/;
  191. print $include{DESCRIPTION};
  192. }
  193. next unless $_;
  194. }
  195. # Accumulate text if the synopsis has not been produced yet.
  196. if ($accumulate)
  197. {
  198. push @description, ".PP\n" if @description;
  199. push @description, "$_\n";
  200. next;
  201. }
  202. # Catch start of options.
  203. if (/^Options:/)
  204. {
  205. print qq(.SH OPTIONS\n);
  206. s/Options://;
  207. }
  208. # Catch bug report text.
  209. if (/^Report bugs |^Email bug reports to /)
  210. {
  211. print qq(.SH "REPORTING BUGS"\n$_\n);
  212. next;
  213. }
  214. # Special case for tar 1.12: --label=NAME\nPATTERN.
  215. s{(\n[ \t]*)(-V,[ \t]+--label=NAME.*)\n[ \t]+PATTERN[ \t]+}
  216. {$1$2$1\\&...=PATTERN };
  217. # Convert options.
  218. s/(\s)(-[][\w=-]+|\\&\S+)/$1 . convert_option $2/ge;
  219. # Option subsections have second line indented.
  220. print qq(.SS "$1"\n) if s/^(\S.*)\n(\s)/$2/;
  221. # Lines indented more than about 10 spaces may be assumed to be
  222. # continuations of the previous line.
  223. s/\n {10,}/ /g;
  224. # Lines following dotted (*) or numbered points may also be
  225. # continued if indented to the same level as the text following
  226. # the point.
  227. 1 while s{((?:^|\n)(\s+)(?:[1-9][.)]|\*)(\s+)(?:[^\n]+))\n\2 \3(\S)}
  228. {$1 $4}g;
  229. # Indented paragraph.
  230. if (/^\s/)
  231. {
  232. for (split /\n/)
  233. {
  234. s/^\s+//;
  235. s/([^,])\s+/$1\n/;
  236. print ".TP\n$_\n" if $_;
  237. }
  238. }
  239. # Anything else.
  240. else
  241. {
  242. print ".PP\n$_\n";
  243. }
  244. }
  245. # Print any include items other than the ones we have already dealt
  246. # with.
  247. for (@include)
  248. {
  249. print qq(.SH "$_"\n$include{$_})
  250. unless /^(NAME|SYNOPSIS|DESCRIPTION|SEE ALSO)$/;
  251. }
  252. # Refer to the real documentation.
  253. if ($include{'SEE ALSO'} or !$opt_no_info)
  254. {
  255. print qq(.SH "SEE ALSO"\n);
  256. print $include{'SEE ALSO'}, ".PP\n" if $include{'SEE ALSO'};
  257. print <<EOT unless $opt_no_info;
  258. The full documentation for
  259. .B $program
  260. is maintained as a Texinfo manual. If the
  261. .B info
  262. and
  263. .B $program
  264. programs are properly installed at your site, the command
  265. .IP
  266. .B info $program
  267. .PP
  268. should give you access to the complete manual.
  269. EOT
  270. }
  271. # Output converted --version information.
  272. for (@version)
  273. {
  274. chomp;
  275. # Join hyphenated lines.
  276. s/([A-Za-z])-\n */$1/g;
  277. # Convert copyright symbol or (c) to nroff character.
  278. s/Copyright\s+(?:\xa9|\([Cc]\))/Copyright \\(co/g;
  279. # Insert appropriate headings for copyright and author.
  280. if (/^Copyright\s\\/) { print ".SH COPYRIGHT\n" }
  281. elsif (/^Written\s+by/) { print ".SH AUTHOR\n" }
  282. else { print ".PP\n"; }
  283. # Insert line breaks before additional copyright messages and the
  284. # disclaimer.
  285. s/(.)\n(Copyright\s|This is free software)/$1\n.br\n$2/g;
  286. print "$_\n";
  287. }
  288. exit;
  289. # Convert option dashes to \- to stop nroff from hyphenating 'em, and
  290. # embolden. Option arguments get italicised.
  291. sub convert_option
  292. {
  293. my $option = '\fB' . shift;
  294. $option =~ s/-/\\-/g;
  295. unless ($option =~ s/\[=(.*)\]$/\\fR[=\\fI$1\\fR]/)
  296. {
  297. $option =~ s/=(.)/\\fR=\\fI$1/;
  298. $option =~ s/ (.)/ \\fI$1/;
  299. $option .= '\fR';
  300. }
  301. $option;
  302. }