blob: 576e31281c7d22845f780a381e73a79e831f9a67 [file] [log] [blame]
Eric Andersene13bc0b2001-02-22 22:47:06 +00001#!/usr/bin/perl -w
Bernhard Reutner-Fischerd129d7c2008-09-01 15:23:04 +00002# vi: set sw=4 ts=4:
Eric Andersene13bc0b2001-02-22 22:47:06 +00003
4use strict;
John Beppu4a25d8c2001-02-23 02:33:28 +00005use Getopt::Long;
Eric Andersene13bc0b2001-02-22 22:47:06 +00006
Eric Andersen55c704c2004-03-13 08:32:14 +00007# collect lines continued with a '\' into an array
John Beppu4a25d8c2001-02-23 02:33:28 +00008sub continuation {
9 my $fh = shift;
10 my @line;
Eric Andersene13bc0b2001-02-22 22:47:06 +000011
John Beppu4a25d8c2001-02-23 02:33:28 +000012 while (<$fh>) {
13 my $s = $_;
14 $s =~ s/\\\s*$//;
John Beppu79359d82001-04-05 20:03:33 +000015 #$s =~ s/#.*$//;
John Beppu4a25d8c2001-02-23 02:33:28 +000016 push @line, $s;
17 last unless (/\\\s*$/);
18 }
19 return @line;
20}
Eric Andersene13bc0b2001-02-22 22:47:06 +000021
John Beppu4a25d8c2001-02-23 02:33:28 +000022# regex && eval away unwanted strings from documentation
23sub beautify {
24 my $text = shift;
Rob Landley52c7d7e2006-07-27 15:12:21 +000025 for (;;) {
26 my $text2 = $text;
27 $text =~ s/SKIP_\w+\(.*?"\s*\)//sxg;
28 $text =~ s/USE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg;
Rob Landley44c79172006-10-24 21:46:19 +000029 $text =~ s/USAGE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg;
Rob Landley52c7d7e2006-07-27 15:12:21 +000030 last if ( $text2 eq $text );
31 }
John Beppudf1e9da2001-02-23 16:15:34 +000032 $text =~ s/"\s*"//sg;
John Beppu4a25d8c2001-02-23 02:33:28 +000033 my @line = split("\n", $text);
34 $text = join('',
Eric Andersen55c704c2004-03-13 08:32:14 +000035 map {
Matt Kraai4e853562001-04-10 00:00:05 +000036 s/^\s*"//;
37 s/"\s*$//;
John Beppu7d597c42001-02-24 14:37:48 +000038 s/%/%%/g;
John Beppud11578f2001-02-26 02:50:11 +000039 s/\$/\\\$/g;
Denis Vlasenko1045d002008-08-31 21:29:35 +000040 s/\@/\\\@/g;
John Beppu79359d82001-04-05 20:03:33 +000041 eval qq[ sprintf(qq{$_}) ]
John Beppu7d597c42001-02-24 14:37:48 +000042 } @line
John Beppu4a25d8c2001-02-23 02:33:28 +000043 );
44 return $text;
45}
Eric Andersene13bc0b2001-02-22 22:47:06 +000046
John Beppu4a25d8c2001-02-23 02:33:28 +000047# generate POD for an applet
48sub pod_for_usage {
49 my $name = shift;
50 my $usage = shift;
51
Eric Andersen55c704c2004-03-13 08:32:14 +000052 # Sigh. Fixup the known odd-name applets.
Bernhard Reutner-Fischerd129d7c2008-09-01 15:23:04 +000053# Perhaps we can use some of APPLET_ODDNAME from include/applets.h ?
Eric Andersen55c704c2004-03-13 08:32:14 +000054 $name =~ s/dpkg_deb/dpkg-deb/g;
55 $name =~ s/fsck_minix/fsck.minix/g;
56 $name =~ s/mkfs_minix/mkfs.minix/g;
57 $name =~ s/run_parts/run-parts/g;
58 $name =~ s/start_stop_daemon/start-stop-daemon/g;
Bernhard Reutner-Fischerd129d7c2008-09-01 15:23:04 +000059 $name =~ s/ether_wake/ether-wake/g;
Eric Andersen55c704c2004-03-13 08:32:14 +000060
John Beppu8373e702001-02-23 17:41:41 +000061 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000062 my $trivial = $usage->{trivial};
Mike Frysingerba9c4d12006-02-06 01:11:34 +000063 if (!defined $usage->{trivial}) {
64 $trivial = "";
65 } else {
66 $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg;
67 }
Eric Andersen55c704c2004-03-13 08:32:14 +000068 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000069 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
Mike Frysingerba9c4d12006-02-06 01:11:34 +000070 split("\n", (defined $usage->{full} ? $usage->{full} : ""));
John Beppu4a25d8c2001-02-23 02:33:28 +000071
John Beppu8373e702001-02-23 17:41:41 +000072 # add "\n" prior to certain lines to make indented
73 # lines look right
John Beppu7d597c42001-02-24 14:37:48 +000074 my @f1;
John Beppu8373e702001-02-23 17:41:41 +000075 my $len = @f0;
76 for (my $i = 0; $i < $len; $i++) {
77 push @f1, $f0[$i];
78 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
79 next if ($f0[$i] =~ /^$/);
80 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
81 }
82 }
John Beppu8373e702001-02-23 17:41:41 +000083 my $full = join("\n", @f1);
John Beppud11578f2001-02-26 02:50:11 +000084
John Beppu5d817682001-04-17 17:09:34 +000085 # prepare notes if they exist
Eric Andersen0d3a02e2001-03-15 18:14:25 +000086 my $notes = (defined $usage->{notes})
87 ? "$usage->{notes}\n\n"
88 : "";
89
John Beppu5d817682001-04-17 17:09:34 +000090 # prepare examples if they exist
John Beppud11578f2001-02-26 02:50:11 +000091 my $example = (defined $usage->{example})
Eric Andersen55c704c2004-03-13 08:32:14 +000092 ?
John Beppue708cb52001-03-15 21:08:01 +000093 "Example:\n\n" .
Eric Andersen55c704c2004-03-13 08:32:14 +000094 join ("\n",
95 map { "\t$_" }
John Beppue708cb52001-03-15 21:08:01 +000096 split("\n", $usage->{example})) . "\n\n"
John Beppud11578f2001-02-26 02:50:11 +000097 : "";
98
Mike Frysinger27a74e82006-02-07 00:58:11 +000099 # Pad the name so that the applet name gets a line
100 # by itself in BusyBox.txt
101 my $spaces = 10 - length($name);
102 if ($spaces > 0) {
103 $name .= " " x $spaces;
104 }
105
John Beppu4a25d8c2001-02-23 02:33:28 +0000106 return
John Beppu9a1395b2001-04-05 19:35:17 +0000107 "=item B<$name>".
Eric Andersen55c704c2004-03-13 08:32:14 +0000108 "\n\n$name $trivial\n\n".
109 "$full\n\n" .
110 "$notes" .
111 "$example" .
John Beppu4a25d8c2001-02-23 02:33:28 +0000112 "\n\n"
113 ;
114}
115
Eric Andersen55c704c2004-03-13 08:32:14 +0000116# the keys are applet names, and
John Beppu8c16bc52001-02-23 02:54:31 +0000117# the values will contain hashrefs of the form:
118#
John Beppu4a25d8c2001-02-23 02:33:28 +0000119# {
120# trivial => "...",
121# full => "...",
John Beppu5d817682001-04-17 17:09:34 +0000122# notes => "...",
John Beppu138ece02001-03-06 19:25:25 +0000123# example => "...",
John Beppu4a25d8c2001-02-23 02:33:28 +0000124# }
125my %docs;
126
John Beppu7d597c42001-02-24 14:37:48 +0000127
John Beppu4a25d8c2001-02-23 02:33:28 +0000128# get command-line options
John Beppu7d597c42001-02-24 14:37:48 +0000129
John Beppu4a25d8c2001-02-23 02:33:28 +0000130my %opt;
131
132GetOptions(
133 \%opt,
134 "help|h",
John Beppu4a25d8c2001-02-23 02:33:28 +0000135 "pod|p",
136 "verbose|v",
137);
138
139if (defined $opt{help}) {
140 print
141 "$0 [OPTION]... [FILE]...\n",
142 "\t--help\n",
John Beppu4a25d8c2001-02-23 02:33:28 +0000143 "\t--pod\n",
144 "\t--verbose\n",
145 ;
146 exit 1;
147}
148
John Beppu7d597c42001-02-24 14:37:48 +0000149
John Beppu4a25d8c2001-02-23 02:33:28 +0000150# collect documenation into %docs
John Beppu7d597c42001-02-24 14:37:48 +0000151
John Beppu4a25d8c2001-02-23 02:33:28 +0000152foreach (@ARGV) {
John Beppud11578f2001-02-26 02:50:11 +0000153 open(USAGE, $_) || die("$0: $_: $!");
John Beppu4a25d8c2001-02-23 02:33:28 +0000154 my $fh = *USAGE;
155 my ($applet, $type, @line);
156 while (<$fh>) {
John Beppu4a25d8c2001-02-23 02:33:28 +0000157 if (/^#define (\w+)_(\w+)_usage/) {
158 $applet = $1;
159 $type = $2;
160 @line = continuation($fh);
161 my $doc = $docs{$applet} ||= { };
John Beppu4a25d8c2001-02-23 02:33:28 +0000162 my $text = join("\n", @line);
163 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000164 }
Eric Andersene13bc0b2001-02-22 22:47:06 +0000165 }
166}
John Beppu4a25d8c2001-02-23 02:33:28 +0000167
John Beppu7d597c42001-02-24 14:37:48 +0000168
169# generate structured documentation
170
John Beppue6967b22001-02-23 17:51:08 +0000171my $generator = \&pod_for_usage;
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000172
173my @names = sort keys %docs;
Mike Frysinger03801662006-02-07 00:51:07 +0000174my $line = "\t[, [[, ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000175for (my $i = 0; $i < $#names; $i++) {
Mike Frysinger03801662006-02-07 00:51:07 +0000176 if (length ($line.$names[$i]) >= 65) {
177 print "$line\n\t";
178 $line = "";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000179 }
Mike Frysinger03801662006-02-07 00:51:07 +0000180 $line .= "$names[$i], ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000181}
Mike Frysinger03801662006-02-07 00:51:07 +0000182print $line . $names[-1];
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000183
184print "\n\n=head1 COMMAND DESCRIPTIONS\n";
185print "\n=over 4\n\n";
186
187foreach my $applet (@names) {
John Beppu7d597c42001-02-24 14:37:48 +0000188 print $generator->($applet, $docs{$applet});
John Beppu4a25d8c2001-02-23 02:33:28 +0000189}
190
191exit 0;
192
193__END__
194
195=head1 NAME
196
197autodocifier.pl - generate docs for busybox based on usage.h
198
199=head1 SYNOPSIS
200
John Beppu5d817682001-04-17 17:09:34 +0000201autodocifier.pl [OPTION]... [FILE]...
202
203Example:
204
205 ( cat docs/busybox_header.pod; \
206 docs/autodocifier.pl usage.h; \
207 cat docs/busybox_footer.pod ) > docs/busybox.pod
John Beppu4a25d8c2001-02-23 02:33:28 +0000208
209=head1 DESCRIPTION
210
Eric Andersenf7300882004-04-06 15:26:25 +0000211The purpose of this script is to automagically generate
212documentation for busybox using its usage.h as the original source
213for content. It used to be that same content has to be duplicated
214in 3 places in slightly different formats -- F<usage.h>,
215F<docs/busybox.pod>. This was tedious and error-prone, so it was
John Beppuce22fee2001-10-31 04:29:18 +0000216decided that F<usage.h> would contain all the text in a
217machine-readable form, and scripts could be used to transform this
218text into other forms if necessary.
John Beppu4a25d8c2001-02-23 02:33:28 +0000219
Eric Andersenf7300882004-04-06 15:26:25 +0000220F<autodocifier.pl> is one such script. It is based on a script by
221Erik Andersen <andersen@codepoet.org> which was in turn based on a
222script by Mark Whitley <markw@codepoet.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000223
224=head1 OPTIONS
225
John Beppue6967b22001-02-23 17:51:08 +0000226=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000227
John Beppu9a1395b2001-04-05 19:35:17 +0000228=item B<--help>
John Beppu4a25d8c2001-02-23 02:33:28 +0000229
230This displays the help message.
231
John Beppu9a1395b2001-04-05 19:35:17 +0000232=item B<--pod>
John Beppue6967b22001-02-23 17:51:08 +0000233
234Generate POD (this is the default)
235
John Beppu9a1395b2001-04-05 19:35:17 +0000236=item B<--verbose>
John Beppue6967b22001-02-23 17:51:08 +0000237
238Be verbose (not implemented)
239
John Beppu4a25d8c2001-02-23 02:33:28 +0000240=back
241
John Beppu9a1395b2001-04-05 19:35:17 +0000242=head1 FORMAT
243
244The following is an example of some data this script might parse.
245
246 #define length_trivial_usage \
247 "STRING"
248 #define length_full_usage \
249 "Prints out the length of the specified STRING."
250 #define length_example_usage \
John Beppu5d817682001-04-17 17:09:34 +0000251 "$ length Hello\n" \
John Beppu9a1395b2001-04-05 19:35:17 +0000252 "5\n"
253
254Each entry is a cpp macro that defines a string. The macros are
255named systematically in the form:
256
257 $name_$type_usage
258
259$name is the name of the applet. $type can be "trivial", "full", "notes",
260or "example". Every documentation macro must end with "_usage".
261
262The definition of the types is as follows:
263
264=over 4
265
266=item B<trivial>
267
268This should be a brief, one-line description of parameters that
269the command expects. This will be displayed when B<-h> is issued to
270a command. I<REQUIRED>
271
272=item B<full>
273
274This should contain descriptions of each option. This will also
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000275be displayed along with the trivial help if CONFIG_FEATURE_TRIVIAL_HELP
John Beppu9a1395b2001-04-05 19:35:17 +0000276is disabled. I<REQUIRED>
277
278=item B<notes>
279
280This is documentation that is intended to go in the POD or SGML, but
John Beppu5d817682001-04-17 17:09:34 +0000281not be printed when a B<-h> is given to a command. To see an example
John Beppuce22fee2001-10-31 04:29:18 +0000282of notes being used, see init_notes_usage in F<usage.h>. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000283
284=item B<example>
285
John Beppuce22fee2001-10-31 04:29:18 +0000286This should be an example of how the command is actually used.
John Beppu5d817682001-04-17 17:09:34 +0000287This will not be printed when a B<-h> is given to a command -- it
John Beppuce22fee2001-10-31 04:29:18 +0000288will only be included in the POD or SGML documentation. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000289
290=back
291
John Beppu4a25d8c2001-02-23 02:33:28 +0000292=head1 FILES
293
John Beppue6967b22001-02-23 17:51:08 +0000294F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000295
296=head1 COPYRIGHT
297
298Copyright (c) 2001 John BEPPU. All rights reserved. This program is
299free software; you can redistribute it and/or modify it under the same
300terms as Perl itself.
301
302=head1 AUTHOR
303
John Beppuce22fee2001-10-31 04:29:18 +0000304John BEPPU <b@ax9.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000305
306=cut
307