blob: 68b6f3c0585e43ae6efc155747b436882bc8ea04 [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;
Rob Landley52c7d7e2006-07-27 15:12:21 +000024 for (;;) {
25 my $text2 = $text;
26 $text =~ s/SKIP_\w+\(.*?"\s*\)//sxg;
27 $text =~ s/USE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg;
Rob Landley44c79172006-10-24 21:46:19 +000028 $text =~ s/USAGE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg;
Rob Landley52c7d7e2006-07-27 15:12:21 +000029 last if ( $text2 eq $text );
30 }
John Beppudf1e9da2001-02-23 16:15:34 +000031 $text =~ s/"\s*"//sg;
John Beppu4a25d8c2001-02-23 02:33:28 +000032 my @line = split("\n", $text);
33 $text = join('',
Eric Andersen55c704c2004-03-13 08:32:14 +000034 map {
Matt Kraai4e853562001-04-10 00:00:05 +000035 s/^\s*"//;
36 s/"\s*$//;
John Beppu7d597c42001-02-24 14:37:48 +000037 s/%/%%/g;
John Beppud11578f2001-02-26 02:50:11 +000038 s/\$/\\\$/g;
John Beppu79359d82001-04-05 20:03:33 +000039 eval qq[ sprintf(qq{$_}) ]
John Beppu7d597c42001-02-24 14:37:48 +000040 } @line
John Beppu4a25d8c2001-02-23 02:33:28 +000041 );
42 return $text;
43}
Eric Andersene13bc0b2001-02-22 22:47:06 +000044
John Beppu4a25d8c2001-02-23 02:33:28 +000045# generate POD for an applet
46sub pod_for_usage {
47 my $name = shift;
48 my $usage = shift;
49
Eric Andersen55c704c2004-03-13 08:32:14 +000050 # Sigh. Fixup the known odd-name applets.
51 $name =~ s/dpkg_deb/dpkg-deb/g;
52 $name =~ s/fsck_minix/fsck.minix/g;
53 $name =~ s/mkfs_minix/mkfs.minix/g;
54 $name =~ s/run_parts/run-parts/g;
55 $name =~ s/start_stop_daemon/start-stop-daemon/g;
56
John Beppu8373e702001-02-23 17:41:41 +000057 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000058 my $trivial = $usage->{trivial};
Mike Frysingerba9c4d12006-02-06 01:11:34 +000059 if (!defined $usage->{trivial}) {
60 $trivial = "";
61 } else {
62 $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg;
63 }
Eric Andersen55c704c2004-03-13 08:32:14 +000064 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000065 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
Mike Frysingerba9c4d12006-02-06 01:11:34 +000066 split("\n", (defined $usage->{full} ? $usage->{full} : ""));
John Beppu4a25d8c2001-02-23 02:33:28 +000067
John Beppu8373e702001-02-23 17:41:41 +000068 # add "\n" prior to certain lines to make indented
69 # lines look right
John Beppu7d597c42001-02-24 14:37:48 +000070 my @f1;
John Beppu8373e702001-02-23 17:41:41 +000071 my $len = @f0;
72 for (my $i = 0; $i < $len; $i++) {
73 push @f1, $f0[$i];
74 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
75 next if ($f0[$i] =~ /^$/);
76 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
77 }
78 }
John Beppu8373e702001-02-23 17:41:41 +000079 my $full = join("\n", @f1);
John Beppud11578f2001-02-26 02:50:11 +000080
John Beppu5d817682001-04-17 17:09:34 +000081 # prepare notes if they exist
Eric Andersen0d3a02e2001-03-15 18:14:25 +000082 my $notes = (defined $usage->{notes})
83 ? "$usage->{notes}\n\n"
84 : "";
85
John Beppu5d817682001-04-17 17:09:34 +000086 # prepare examples if they exist
John Beppud11578f2001-02-26 02:50:11 +000087 my $example = (defined $usage->{example})
Eric Andersen55c704c2004-03-13 08:32:14 +000088 ?
John Beppue708cb52001-03-15 21:08:01 +000089 "Example:\n\n" .
Eric Andersen55c704c2004-03-13 08:32:14 +000090 join ("\n",
91 map { "\t$_" }
John Beppue708cb52001-03-15 21:08:01 +000092 split("\n", $usage->{example})) . "\n\n"
John Beppud11578f2001-02-26 02:50:11 +000093 : "";
94
Mike Frysinger27a74e82006-02-07 00:58:11 +000095 # Pad the name so that the applet name gets a line
96 # by itself in BusyBox.txt
97 my $spaces = 10 - length($name);
98 if ($spaces > 0) {
99 $name .= " " x $spaces;
100 }
101
John Beppu4a25d8c2001-02-23 02:33:28 +0000102 return
John Beppu9a1395b2001-04-05 19:35:17 +0000103 "=item B<$name>".
Eric Andersen55c704c2004-03-13 08:32:14 +0000104 "\n\n$name $trivial\n\n".
105 "$full\n\n" .
106 "$notes" .
107 "$example" .
John Beppu4a25d8c2001-02-23 02:33:28 +0000108 "\n\n"
109 ;
110}
111
Eric Andersen55c704c2004-03-13 08:32:14 +0000112# the keys are applet names, and
John Beppu8c16bc52001-02-23 02:54:31 +0000113# the values will contain hashrefs of the form:
114#
John Beppu4a25d8c2001-02-23 02:33:28 +0000115# {
116# trivial => "...",
117# full => "...",
John Beppu5d817682001-04-17 17:09:34 +0000118# notes => "...",
John Beppu138ece02001-03-06 19:25:25 +0000119# example => "...",
John Beppu4a25d8c2001-02-23 02:33:28 +0000120# }
121my %docs;
122
John Beppu7d597c42001-02-24 14:37:48 +0000123
John Beppu4a25d8c2001-02-23 02:33:28 +0000124# get command-line options
John Beppu7d597c42001-02-24 14:37:48 +0000125
John Beppu4a25d8c2001-02-23 02:33:28 +0000126my %opt;
127
128GetOptions(
129 \%opt,
130 "help|h",
John Beppu4a25d8c2001-02-23 02:33:28 +0000131 "pod|p",
132 "verbose|v",
133);
134
135if (defined $opt{help}) {
136 print
137 "$0 [OPTION]... [FILE]...\n",
138 "\t--help\n",
John Beppu4a25d8c2001-02-23 02:33:28 +0000139 "\t--pod\n",
140 "\t--verbose\n",
141 ;
142 exit 1;
143}
144
John Beppu7d597c42001-02-24 14:37:48 +0000145
John Beppu4a25d8c2001-02-23 02:33:28 +0000146# collect documenation into %docs
John Beppu7d597c42001-02-24 14:37:48 +0000147
John Beppu4a25d8c2001-02-23 02:33:28 +0000148foreach (@ARGV) {
John Beppud11578f2001-02-26 02:50:11 +0000149 open(USAGE, $_) || die("$0: $_: $!");
John Beppu4a25d8c2001-02-23 02:33:28 +0000150 my $fh = *USAGE;
151 my ($applet, $type, @line);
152 while (<$fh>) {
John Beppu4a25d8c2001-02-23 02:33:28 +0000153 if (/^#define (\w+)_(\w+)_usage/) {
154 $applet = $1;
155 $type = $2;
156 @line = continuation($fh);
157 my $doc = $docs{$applet} ||= { };
John Beppu4a25d8c2001-02-23 02:33:28 +0000158 my $text = join("\n", @line);
159 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000160 }
Eric Andersene13bc0b2001-02-22 22:47:06 +0000161 }
162}
John Beppu4a25d8c2001-02-23 02:33:28 +0000163
John Beppu7d597c42001-02-24 14:37:48 +0000164
165# generate structured documentation
166
John Beppue6967b22001-02-23 17:51:08 +0000167my $generator = \&pod_for_usage;
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000168
169my @names = sort keys %docs;
Mike Frysinger03801662006-02-07 00:51:07 +0000170my $line = "\t[, [[, ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000171for (my $i = 0; $i < $#names; $i++) {
Mike Frysinger03801662006-02-07 00:51:07 +0000172 if (length ($line.$names[$i]) >= 65) {
173 print "$line\n\t";
174 $line = "";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000175 }
Mike Frysinger03801662006-02-07 00:51:07 +0000176 $line .= "$names[$i], ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000177}
Mike Frysinger03801662006-02-07 00:51:07 +0000178print $line . $names[-1];
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000179
180print "\n\n=head1 COMMAND DESCRIPTIONS\n";
181print "\n=over 4\n\n";
182
183foreach my $applet (@names) {
John Beppu7d597c42001-02-24 14:37:48 +0000184 print $generator->($applet, $docs{$applet});
John Beppu4a25d8c2001-02-23 02:33:28 +0000185}
186
187exit 0;
188
189__END__
190
191=head1 NAME
192
193autodocifier.pl - generate docs for busybox based on usage.h
194
195=head1 SYNOPSIS
196
John Beppu5d817682001-04-17 17:09:34 +0000197autodocifier.pl [OPTION]... [FILE]...
198
199Example:
200
201 ( cat docs/busybox_header.pod; \
202 docs/autodocifier.pl usage.h; \
203 cat docs/busybox_footer.pod ) > docs/busybox.pod
John Beppu4a25d8c2001-02-23 02:33:28 +0000204
205=head1 DESCRIPTION
206
Eric Andersenf7300882004-04-06 15:26:25 +0000207The purpose of this script is to automagically generate
208documentation for busybox using its usage.h as the original source
209for content. It used to be that same content has to be duplicated
210in 3 places in slightly different formats -- F<usage.h>,
211F<docs/busybox.pod>. This was tedious and error-prone, so it was
John Beppuce22fee2001-10-31 04:29:18 +0000212decided that F<usage.h> would contain all the text in a
213machine-readable form, and scripts could be used to transform this
214text into other forms if necessary.
John Beppu4a25d8c2001-02-23 02:33:28 +0000215
Eric Andersenf7300882004-04-06 15:26:25 +0000216F<autodocifier.pl> is one such script. It is based on a script by
217Erik Andersen <andersen@codepoet.org> which was in turn based on a
218script by Mark Whitley <markw@codepoet.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000219
220=head1 OPTIONS
221
John Beppue6967b22001-02-23 17:51:08 +0000222=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000223
John Beppu9a1395b2001-04-05 19:35:17 +0000224=item B<--help>
John Beppu4a25d8c2001-02-23 02:33:28 +0000225
226This displays the help message.
227
John Beppu9a1395b2001-04-05 19:35:17 +0000228=item B<--pod>
John Beppue6967b22001-02-23 17:51:08 +0000229
230Generate POD (this is the default)
231
John Beppu9a1395b2001-04-05 19:35:17 +0000232=item B<--verbose>
John Beppue6967b22001-02-23 17:51:08 +0000233
234Be verbose (not implemented)
235
John Beppu4a25d8c2001-02-23 02:33:28 +0000236=back
237
John Beppu9a1395b2001-04-05 19:35:17 +0000238=head1 FORMAT
239
240The following is an example of some data this script might parse.
241
242 #define length_trivial_usage \
243 "STRING"
244 #define length_full_usage \
245 "Prints out the length of the specified STRING."
246 #define length_example_usage \
John Beppu5d817682001-04-17 17:09:34 +0000247 "$ length Hello\n" \
John Beppu9a1395b2001-04-05 19:35:17 +0000248 "5\n"
249
250Each entry is a cpp macro that defines a string. The macros are
251named systematically in the form:
252
253 $name_$type_usage
254
255$name is the name of the applet. $type can be "trivial", "full", "notes",
256or "example". Every documentation macro must end with "_usage".
257
258The definition of the types is as follows:
259
260=over 4
261
262=item B<trivial>
263
264This should be a brief, one-line description of parameters that
265the command expects. This will be displayed when B<-h> is issued to
266a command. I<REQUIRED>
267
268=item B<full>
269
270This should contain descriptions of each option. This will also
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000271be displayed along with the trivial help if CONFIG_FEATURE_TRIVIAL_HELP
John Beppu9a1395b2001-04-05 19:35:17 +0000272is disabled. I<REQUIRED>
273
274=item B<notes>
275
276This is documentation that is intended to go in the POD or SGML, but
John Beppu5d817682001-04-17 17:09:34 +0000277not be printed when a B<-h> is given to a command. To see an example
John Beppuce22fee2001-10-31 04:29:18 +0000278of notes being used, see init_notes_usage in F<usage.h>. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000279
280=item B<example>
281
John Beppuce22fee2001-10-31 04:29:18 +0000282This should be an example of how the command is actually used.
John Beppu5d817682001-04-17 17:09:34 +0000283This will not be printed when a B<-h> is given to a command -- it
John Beppuce22fee2001-10-31 04:29:18 +0000284will only be included in the POD or SGML documentation. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000285
286=back
287
John Beppu4a25d8c2001-02-23 02:33:28 +0000288=head1 FILES
289
John Beppue6967b22001-02-23 17:51:08 +0000290F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000291
292=head1 COPYRIGHT
293
294Copyright (c) 2001 John BEPPU. All rights reserved. This program is
295free software; you can redistribute it and/or modify it under the same
296terms as Perl itself.
297
298=head1 AUTHOR
299
John Beppuce22fee2001-10-31 04:29:18 +0000300John BEPPU <b@ax9.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000301
302=cut
303