blob: 3a29512373ab6e82435d30abf3641ceacc726a1c [file] [log] [blame]
Eric Andersene13bc0b2001-02-22 22:47:06 +00001#!/usr/bin/perl -w
Eric Andersene13bc0b2001-02-22 22:47:06 +00002
3use strict;
John Beppu4a25d8c2001-02-23 02:33:28 +00004use Getopt::Long;
Eric Andersene13bc0b2001-02-22 22:47:06 +00005
Eric Andersen55c704c2004-03-13 08:32:14 +00006# collect lines continued with a '\' into an array
John Beppu4a25d8c2001-02-23 02:33:28 +00007sub continuation {
8 my $fh = shift;
9 my @line;
Eric Andersene13bc0b2001-02-22 22:47:06 +000010
John Beppu4a25d8c2001-02-23 02:33:28 +000011 while (<$fh>) {
12 my $s = $_;
13 $s =~ s/\\\s*$//;
John Beppu79359d82001-04-05 20:03:33 +000014 #$s =~ s/#.*$//;
John Beppu4a25d8c2001-02-23 02:33:28 +000015 push @line, $s;
16 last unless (/\\\s*$/);
17 }
18 return @line;
19}
Eric Andersene13bc0b2001-02-22 22:47:06 +000020
John Beppu4a25d8c2001-02-23 02:33:28 +000021# regex && eval away unwanted strings from documentation
22sub beautify {
23 my $text = shift;
John Beppudbfff6c2001-02-23 17:55:03 +000024 $text =~ s/USAGE_NOT\w+\(.*?"\s*\)//sxg;
John Beppudf1e9da2001-02-23 16:15:34 +000025 $text =~ s/USAGE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg;
26 $text =~ s/"\s*"//sg;
John Beppu4a25d8c2001-02-23 02:33:28 +000027 my @line = split("\n", $text);
28 $text = join('',
Eric Andersen55c704c2004-03-13 08:32:14 +000029 map {
Matt Kraai4e853562001-04-10 00:00:05 +000030 s/^\s*"//;
31 s/"\s*$//;
John Beppu7d597c42001-02-24 14:37:48 +000032 s/%/%%/g;
John Beppud11578f2001-02-26 02:50:11 +000033 s/\$/\\\$/g;
John Beppu79359d82001-04-05 20:03:33 +000034 eval qq[ sprintf(qq{$_}) ]
John Beppu7d597c42001-02-24 14:37:48 +000035 } @line
John Beppu4a25d8c2001-02-23 02:33:28 +000036 );
37 return $text;
38}
Eric Andersene13bc0b2001-02-22 22:47:06 +000039
John Beppu4a25d8c2001-02-23 02:33:28 +000040# generate POD for an applet
41sub pod_for_usage {
42 my $name = shift;
43 my $usage = shift;
44
Eric Andersen55c704c2004-03-13 08:32:14 +000045 # Sigh. Fixup the known odd-name applets.
46 $name =~ s/dpkg_deb/dpkg-deb/g;
47 $name =~ s/fsck_minix/fsck.minix/g;
48 $name =~ s/mkfs_minix/mkfs.minix/g;
49 $name =~ s/run_parts/run-parts/g;
50 $name =~ s/start_stop_daemon/start-stop-daemon/g;
51
John Beppu8373e702001-02-23 17:41:41 +000052 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000053 my $trivial = $usage->{trivial};
Mike Frysingerba9c4d12006-02-06 01:11:34 +000054 if (!defined $usage->{trivial}) {
55 $trivial = "";
56 } else {
57 $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg;
58 }
Eric Andersen55c704c2004-03-13 08:32:14 +000059 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000060 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
Mike Frysingerba9c4d12006-02-06 01:11:34 +000061 split("\n", (defined $usage->{full} ? $usage->{full} : ""));
John Beppu4a25d8c2001-02-23 02:33:28 +000062
John Beppu8373e702001-02-23 17:41:41 +000063 # add "\n" prior to certain lines to make indented
64 # lines look right
John Beppu7d597c42001-02-24 14:37:48 +000065 my @f1;
John Beppu8373e702001-02-23 17:41:41 +000066 my $len = @f0;
67 for (my $i = 0; $i < $len; $i++) {
68 push @f1, $f0[$i];
69 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
70 next if ($f0[$i] =~ /^$/);
71 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
72 }
73 }
John Beppu8373e702001-02-23 17:41:41 +000074 my $full = join("\n", @f1);
John Beppud11578f2001-02-26 02:50:11 +000075
John Beppu5d817682001-04-17 17:09:34 +000076 # prepare notes if they exist
Eric Andersen0d3a02e2001-03-15 18:14:25 +000077 my $notes = (defined $usage->{notes})
78 ? "$usage->{notes}\n\n"
79 : "";
80
John Beppu5d817682001-04-17 17:09:34 +000081 # prepare examples if they exist
John Beppud11578f2001-02-26 02:50:11 +000082 my $example = (defined $usage->{example})
Eric Andersen55c704c2004-03-13 08:32:14 +000083 ?
John Beppue708cb52001-03-15 21:08:01 +000084 "Example:\n\n" .
Eric Andersen55c704c2004-03-13 08:32:14 +000085 join ("\n",
86 map { "\t$_" }
John Beppue708cb52001-03-15 21:08:01 +000087 split("\n", $usage->{example})) . "\n\n"
John Beppud11578f2001-02-26 02:50:11 +000088 : "";
89
John Beppu4a25d8c2001-02-23 02:33:28 +000090 return
John Beppu9a1395b2001-04-05 19:35:17 +000091 "=item B<$name>".
Eric Andersen55c704c2004-03-13 08:32:14 +000092 "\n\n$name $trivial\n\n".
93 "$full\n\n" .
94 "$notes" .
95 "$example" .
John Beppu4a25d8c2001-02-23 02:33:28 +000096 "\n\n"
97 ;
98}
99
Eric Andersen55c704c2004-03-13 08:32:14 +0000100# the keys are applet names, and
John Beppu8c16bc52001-02-23 02:54:31 +0000101# the values will contain hashrefs of the form:
102#
John Beppu4a25d8c2001-02-23 02:33:28 +0000103# {
104# trivial => "...",
105# full => "...",
John Beppu5d817682001-04-17 17:09:34 +0000106# notes => "...",
John Beppu138ece02001-03-06 19:25:25 +0000107# example => "...",
John Beppu4a25d8c2001-02-23 02:33:28 +0000108# }
109my %docs;
110
John Beppu7d597c42001-02-24 14:37:48 +0000111
John Beppu4a25d8c2001-02-23 02:33:28 +0000112# get command-line options
John Beppu7d597c42001-02-24 14:37:48 +0000113
John Beppu4a25d8c2001-02-23 02:33:28 +0000114my %opt;
115
116GetOptions(
117 \%opt,
118 "help|h",
John Beppu4a25d8c2001-02-23 02:33:28 +0000119 "pod|p",
120 "verbose|v",
121);
122
123if (defined $opt{help}) {
124 print
125 "$0 [OPTION]... [FILE]...\n",
126 "\t--help\n",
John Beppu4a25d8c2001-02-23 02:33:28 +0000127 "\t--pod\n",
128 "\t--verbose\n",
129 ;
130 exit 1;
131}
132
John Beppu7d597c42001-02-24 14:37:48 +0000133
John Beppu4a25d8c2001-02-23 02:33:28 +0000134# collect documenation into %docs
John Beppu7d597c42001-02-24 14:37:48 +0000135
John Beppu4a25d8c2001-02-23 02:33:28 +0000136foreach (@ARGV) {
John Beppud11578f2001-02-26 02:50:11 +0000137 open(USAGE, $_) || die("$0: $_: $!");
John Beppu4a25d8c2001-02-23 02:33:28 +0000138 my $fh = *USAGE;
139 my ($applet, $type, @line);
140 while (<$fh>) {
John Beppu4a25d8c2001-02-23 02:33:28 +0000141 if (/^#define (\w+)_(\w+)_usage/) {
142 $applet = $1;
143 $type = $2;
144 @line = continuation($fh);
145 my $doc = $docs{$applet} ||= { };
John Beppu4a25d8c2001-02-23 02:33:28 +0000146 my $text = join("\n", @line);
147 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000148 }
Eric Andersene13bc0b2001-02-22 22:47:06 +0000149 }
150}
John Beppu4a25d8c2001-02-23 02:33:28 +0000151
John Beppu7d597c42001-02-24 14:37:48 +0000152
153# generate structured documentation
154
John Beppue6967b22001-02-23 17:51:08 +0000155my $generator = \&pod_for_usage;
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000156
157my @names = sort keys %docs;
Mike Frysinger03801662006-02-07 00:51:07 +0000158my $line = "\t[, [[, ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000159for (my $i = 0; $i < $#names; $i++) {
Mike Frysinger03801662006-02-07 00:51:07 +0000160 if (length ($line.$names[$i]) >= 65) {
161 print "$line\n\t";
162 $line = "";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000163 }
Mike Frysinger03801662006-02-07 00:51:07 +0000164 $line .= "$names[$i], ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000165}
Mike Frysinger03801662006-02-07 00:51:07 +0000166print $line . $names[-1];
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000167
168print "\n\n=head1 COMMAND DESCRIPTIONS\n";
169print "\n=over 4\n\n";
170
171foreach my $applet (@names) {
John Beppu7d597c42001-02-24 14:37:48 +0000172 print $generator->($applet, $docs{$applet});
John Beppu4a25d8c2001-02-23 02:33:28 +0000173}
174
175exit 0;
176
177__END__
178
179=head1 NAME
180
181autodocifier.pl - generate docs for busybox based on usage.h
182
183=head1 SYNOPSIS
184
John Beppu5d817682001-04-17 17:09:34 +0000185autodocifier.pl [OPTION]... [FILE]...
186
187Example:
188
189 ( cat docs/busybox_header.pod; \
190 docs/autodocifier.pl usage.h; \
191 cat docs/busybox_footer.pod ) > docs/busybox.pod
John Beppu4a25d8c2001-02-23 02:33:28 +0000192
193=head1 DESCRIPTION
194
Eric Andersenf7300882004-04-06 15:26:25 +0000195The purpose of this script is to automagically generate
196documentation for busybox using its usage.h as the original source
197for content. It used to be that same content has to be duplicated
198in 3 places in slightly different formats -- F<usage.h>,
199F<docs/busybox.pod>. This was tedious and error-prone, so it was
John Beppuce22fee2001-10-31 04:29:18 +0000200decided that F<usage.h> would contain all the text in a
201machine-readable form, and scripts could be used to transform this
202text into other forms if necessary.
John Beppu4a25d8c2001-02-23 02:33:28 +0000203
Eric Andersenf7300882004-04-06 15:26:25 +0000204F<autodocifier.pl> is one such script. It is based on a script by
205Erik Andersen <andersen@codepoet.org> which was in turn based on a
206script by Mark Whitley <markw@codepoet.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000207
208=head1 OPTIONS
209
John Beppue6967b22001-02-23 17:51:08 +0000210=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000211
John Beppu9a1395b2001-04-05 19:35:17 +0000212=item B<--help>
John Beppu4a25d8c2001-02-23 02:33:28 +0000213
214This displays the help message.
215
John Beppu9a1395b2001-04-05 19:35:17 +0000216=item B<--pod>
John Beppue6967b22001-02-23 17:51:08 +0000217
218Generate POD (this is the default)
219
John Beppu9a1395b2001-04-05 19:35:17 +0000220=item B<--verbose>
John Beppue6967b22001-02-23 17:51:08 +0000221
222Be verbose (not implemented)
223
John Beppu4a25d8c2001-02-23 02:33:28 +0000224=back
225
John Beppu9a1395b2001-04-05 19:35:17 +0000226=head1 FORMAT
227
228The following is an example of some data this script might parse.
229
230 #define length_trivial_usage \
231 "STRING"
232 #define length_full_usage \
233 "Prints out the length of the specified STRING."
234 #define length_example_usage \
John Beppu5d817682001-04-17 17:09:34 +0000235 "$ length Hello\n" \
John Beppu9a1395b2001-04-05 19:35:17 +0000236 "5\n"
237
238Each entry is a cpp macro that defines a string. The macros are
239named systematically in the form:
240
241 $name_$type_usage
242
243$name is the name of the applet. $type can be "trivial", "full", "notes",
244or "example". Every documentation macro must end with "_usage".
245
246The definition of the types is as follows:
247
248=over 4
249
250=item B<trivial>
251
252This should be a brief, one-line description of parameters that
253the command expects. This will be displayed when B<-h> is issued to
254a command. I<REQUIRED>
255
256=item B<full>
257
258This should contain descriptions of each option. This will also
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000259be displayed along with the trivial help if CONFIG_FEATURE_TRIVIAL_HELP
John Beppu9a1395b2001-04-05 19:35:17 +0000260is disabled. I<REQUIRED>
261
262=item B<notes>
263
264This is documentation that is intended to go in the POD or SGML, but
John Beppu5d817682001-04-17 17:09:34 +0000265not be printed when a B<-h> is given to a command. To see an example
John Beppuce22fee2001-10-31 04:29:18 +0000266of notes being used, see init_notes_usage in F<usage.h>. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000267
268=item B<example>
269
John Beppuce22fee2001-10-31 04:29:18 +0000270This should be an example of how the command is actually used.
John Beppu5d817682001-04-17 17:09:34 +0000271This will not be printed when a B<-h> is given to a command -- it
John Beppuce22fee2001-10-31 04:29:18 +0000272will only be included in the POD or SGML documentation. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000273
274=back
275
John Beppu4a25d8c2001-02-23 02:33:28 +0000276=head1 FILES
277
John Beppue6967b22001-02-23 17:51:08 +0000278F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000279
280=head1 COPYRIGHT
281
282Copyright (c) 2001 John BEPPU. All rights reserved. This program is
283free software; you can redistribute it and/or modify it under the same
284terms as Perl itself.
285
286=head1 AUTHOR
287
John Beppuce22fee2001-10-31 04:29:18 +0000288John BEPPU <b@ax9.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000289
290=cut
291
Eric Andersenf7300882004-04-06 15:26:25 +0000292# $Id: autodocifier.pl,v 1.26 2004/04/06 15:26:25 andersen Exp $