blob: 4acb6bdc8105e082e6e66104344b56286573b5f0 [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;
28 last if ( $text2 eq $text );
29 }
John Beppudf1e9da2001-02-23 16:15:34 +000030 $text =~ s/"\s*"//sg;
John Beppu4a25d8c2001-02-23 02:33:28 +000031 my @line = split("\n", $text);
32 $text = join('',
Eric Andersen55c704c2004-03-13 08:32:14 +000033 map {
Matt Kraai4e853562001-04-10 00:00:05 +000034 s/^\s*"//;
35 s/"\s*$//;
John Beppu7d597c42001-02-24 14:37:48 +000036 s/%/%%/g;
John Beppud11578f2001-02-26 02:50:11 +000037 s/\$/\\\$/g;
John Beppu79359d82001-04-05 20:03:33 +000038 eval qq[ sprintf(qq{$_}) ]
John Beppu7d597c42001-02-24 14:37:48 +000039 } @line
John Beppu4a25d8c2001-02-23 02:33:28 +000040 );
41 return $text;
42}
Eric Andersene13bc0b2001-02-22 22:47:06 +000043
John Beppu4a25d8c2001-02-23 02:33:28 +000044# generate POD for an applet
45sub pod_for_usage {
46 my $name = shift;
47 my $usage = shift;
48
Eric Andersen55c704c2004-03-13 08:32:14 +000049 # Sigh. Fixup the known odd-name applets.
50 $name =~ s/dpkg_deb/dpkg-deb/g;
51 $name =~ s/fsck_minix/fsck.minix/g;
52 $name =~ s/mkfs_minix/mkfs.minix/g;
53 $name =~ s/run_parts/run-parts/g;
54 $name =~ s/start_stop_daemon/start-stop-daemon/g;
55
John Beppu8373e702001-02-23 17:41:41 +000056 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000057 my $trivial = $usage->{trivial};
Mike Frysingerba9c4d12006-02-06 01:11:34 +000058 if (!defined $usage->{trivial}) {
59 $trivial = "";
60 } else {
61 $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg;
62 }
Eric Andersen55c704c2004-03-13 08:32:14 +000063 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000064 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
Mike Frysingerba9c4d12006-02-06 01:11:34 +000065 split("\n", (defined $usage->{full} ? $usage->{full} : ""));
John Beppu4a25d8c2001-02-23 02:33:28 +000066
John Beppu8373e702001-02-23 17:41:41 +000067 # add "\n" prior to certain lines to make indented
68 # lines look right
John Beppu7d597c42001-02-24 14:37:48 +000069 my @f1;
John Beppu8373e702001-02-23 17:41:41 +000070 my $len = @f0;
71 for (my $i = 0; $i < $len; $i++) {
72 push @f1, $f0[$i];
73 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
74 next if ($f0[$i] =~ /^$/);
75 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
76 }
77 }
John Beppu8373e702001-02-23 17:41:41 +000078 my $full = join("\n", @f1);
John Beppud11578f2001-02-26 02:50:11 +000079
John Beppu5d817682001-04-17 17:09:34 +000080 # prepare notes if they exist
Eric Andersen0d3a02e2001-03-15 18:14:25 +000081 my $notes = (defined $usage->{notes})
82 ? "$usage->{notes}\n\n"
83 : "";
84
John Beppu5d817682001-04-17 17:09:34 +000085 # prepare examples if they exist
John Beppud11578f2001-02-26 02:50:11 +000086 my $example = (defined $usage->{example})
Eric Andersen55c704c2004-03-13 08:32:14 +000087 ?
John Beppue708cb52001-03-15 21:08:01 +000088 "Example:\n\n" .
Eric Andersen55c704c2004-03-13 08:32:14 +000089 join ("\n",
90 map { "\t$_" }
John Beppue708cb52001-03-15 21:08:01 +000091 split("\n", $usage->{example})) . "\n\n"
John Beppud11578f2001-02-26 02:50:11 +000092 : "";
93
Mike Frysinger27a74e82006-02-07 00:58:11 +000094 # Pad the name so that the applet name gets a line
95 # by itself in BusyBox.txt
96 my $spaces = 10 - length($name);
97 if ($spaces > 0) {
98 $name .= " " x $spaces;
99 }
100
John Beppu4a25d8c2001-02-23 02:33:28 +0000101 return
John Beppu9a1395b2001-04-05 19:35:17 +0000102 "=item B<$name>".
Eric Andersen55c704c2004-03-13 08:32:14 +0000103 "\n\n$name $trivial\n\n".
104 "$full\n\n" .
105 "$notes" .
106 "$example" .
John Beppu4a25d8c2001-02-23 02:33:28 +0000107 "\n\n"
108 ;
109}
110
Eric Andersen55c704c2004-03-13 08:32:14 +0000111# the keys are applet names, and
John Beppu8c16bc52001-02-23 02:54:31 +0000112# the values will contain hashrefs of the form:
113#
John Beppu4a25d8c2001-02-23 02:33:28 +0000114# {
115# trivial => "...",
116# full => "...",
John Beppu5d817682001-04-17 17:09:34 +0000117# notes => "...",
John Beppu138ece02001-03-06 19:25:25 +0000118# example => "...",
John Beppu4a25d8c2001-02-23 02:33:28 +0000119# }
120my %docs;
121
John Beppu7d597c42001-02-24 14:37:48 +0000122
John Beppu4a25d8c2001-02-23 02:33:28 +0000123# get command-line options
John Beppu7d597c42001-02-24 14:37:48 +0000124
John Beppu4a25d8c2001-02-23 02:33:28 +0000125my %opt;
126
127GetOptions(
128 \%opt,
129 "help|h",
John Beppu4a25d8c2001-02-23 02:33:28 +0000130 "pod|p",
131 "verbose|v",
132);
133
134if (defined $opt{help}) {
135 print
136 "$0 [OPTION]... [FILE]...\n",
137 "\t--help\n",
John Beppu4a25d8c2001-02-23 02:33:28 +0000138 "\t--pod\n",
139 "\t--verbose\n",
140 ;
141 exit 1;
142}
143
John Beppu7d597c42001-02-24 14:37:48 +0000144
John Beppu4a25d8c2001-02-23 02:33:28 +0000145# collect documenation into %docs
John Beppu7d597c42001-02-24 14:37:48 +0000146
John Beppu4a25d8c2001-02-23 02:33:28 +0000147foreach (@ARGV) {
John Beppud11578f2001-02-26 02:50:11 +0000148 open(USAGE, $_) || die("$0: $_: $!");
John Beppu4a25d8c2001-02-23 02:33:28 +0000149 my $fh = *USAGE;
150 my ($applet, $type, @line);
151 while (<$fh>) {
John Beppu4a25d8c2001-02-23 02:33:28 +0000152 if (/^#define (\w+)_(\w+)_usage/) {
153 $applet = $1;
154 $type = $2;
155 @line = continuation($fh);
156 my $doc = $docs{$applet} ||= { };
John Beppu4a25d8c2001-02-23 02:33:28 +0000157 my $text = join("\n", @line);
158 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000159 }
Eric Andersene13bc0b2001-02-22 22:47:06 +0000160 }
161}
John Beppu4a25d8c2001-02-23 02:33:28 +0000162
John Beppu7d597c42001-02-24 14:37:48 +0000163
164# generate structured documentation
165
John Beppue6967b22001-02-23 17:51:08 +0000166my $generator = \&pod_for_usage;
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000167
168my @names = sort keys %docs;
Mike Frysinger03801662006-02-07 00:51:07 +0000169my $line = "\t[, [[, ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000170for (my $i = 0; $i < $#names; $i++) {
Mike Frysinger03801662006-02-07 00:51:07 +0000171 if (length ($line.$names[$i]) >= 65) {
172 print "$line\n\t";
173 $line = "";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000174 }
Mike Frysinger03801662006-02-07 00:51:07 +0000175 $line .= "$names[$i], ";
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000176}
Mike Frysinger03801662006-02-07 00:51:07 +0000177print $line . $names[-1];
Mike Frysingerb0ed3d72006-02-05 22:10:40 +0000178
179print "\n\n=head1 COMMAND DESCRIPTIONS\n";
180print "\n=over 4\n\n";
181
182foreach my $applet (@names) {
John Beppu7d597c42001-02-24 14:37:48 +0000183 print $generator->($applet, $docs{$applet});
John Beppu4a25d8c2001-02-23 02:33:28 +0000184}
185
186exit 0;
187
188__END__
189
190=head1 NAME
191
192autodocifier.pl - generate docs for busybox based on usage.h
193
194=head1 SYNOPSIS
195
John Beppu5d817682001-04-17 17:09:34 +0000196autodocifier.pl [OPTION]... [FILE]...
197
198Example:
199
200 ( cat docs/busybox_header.pod; \
201 docs/autodocifier.pl usage.h; \
202 cat docs/busybox_footer.pod ) > docs/busybox.pod
John Beppu4a25d8c2001-02-23 02:33:28 +0000203
204=head1 DESCRIPTION
205
Eric Andersenf7300882004-04-06 15:26:25 +0000206The purpose of this script is to automagically generate
207documentation for busybox using its usage.h as the original source
208for content. It used to be that same content has to be duplicated
209in 3 places in slightly different formats -- F<usage.h>,
210F<docs/busybox.pod>. This was tedious and error-prone, so it was
John Beppuce22fee2001-10-31 04:29:18 +0000211decided that F<usage.h> would contain all the text in a
212machine-readable form, and scripts could be used to transform this
213text into other forms if necessary.
John Beppu4a25d8c2001-02-23 02:33:28 +0000214
Eric Andersenf7300882004-04-06 15:26:25 +0000215F<autodocifier.pl> is one such script. It is based on a script by
216Erik Andersen <andersen@codepoet.org> which was in turn based on a
217script by Mark Whitley <markw@codepoet.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000218
219=head1 OPTIONS
220
John Beppue6967b22001-02-23 17:51:08 +0000221=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000222
John Beppu9a1395b2001-04-05 19:35:17 +0000223=item B<--help>
John Beppu4a25d8c2001-02-23 02:33:28 +0000224
225This displays the help message.
226
John Beppu9a1395b2001-04-05 19:35:17 +0000227=item B<--pod>
John Beppue6967b22001-02-23 17:51:08 +0000228
229Generate POD (this is the default)
230
John Beppu9a1395b2001-04-05 19:35:17 +0000231=item B<--verbose>
John Beppue6967b22001-02-23 17:51:08 +0000232
233Be verbose (not implemented)
234
John Beppu4a25d8c2001-02-23 02:33:28 +0000235=back
236
John Beppu9a1395b2001-04-05 19:35:17 +0000237=head1 FORMAT
238
239The following is an example of some data this script might parse.
240
241 #define length_trivial_usage \
242 "STRING"
243 #define length_full_usage \
244 "Prints out the length of the specified STRING."
245 #define length_example_usage \
John Beppu5d817682001-04-17 17:09:34 +0000246 "$ length Hello\n" \
John Beppu9a1395b2001-04-05 19:35:17 +0000247 "5\n"
248
249Each entry is a cpp macro that defines a string. The macros are
250named systematically in the form:
251
252 $name_$type_usage
253
254$name is the name of the applet. $type can be "trivial", "full", "notes",
255or "example". Every documentation macro must end with "_usage".
256
257The definition of the types is as follows:
258
259=over 4
260
261=item B<trivial>
262
263This should be a brief, one-line description of parameters that
264the command expects. This will be displayed when B<-h> is issued to
265a command. I<REQUIRED>
266
267=item B<full>
268
269This should contain descriptions of each option. This will also
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000270be displayed along with the trivial help if CONFIG_FEATURE_TRIVIAL_HELP
John Beppu9a1395b2001-04-05 19:35:17 +0000271is disabled. I<REQUIRED>
272
273=item B<notes>
274
275This is documentation that is intended to go in the POD or SGML, but
John Beppu5d817682001-04-17 17:09:34 +0000276not be printed when a B<-h> is given to a command. To see an example
John Beppuce22fee2001-10-31 04:29:18 +0000277of notes being used, see init_notes_usage in F<usage.h>. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000278
279=item B<example>
280
John Beppuce22fee2001-10-31 04:29:18 +0000281This should be an example of how the command is actually used.
John Beppu5d817682001-04-17 17:09:34 +0000282This will not be printed when a B<-h> is given to a command -- it
John Beppuce22fee2001-10-31 04:29:18 +0000283will only be included in the POD or SGML documentation. I<OPTIONAL>
John Beppu9a1395b2001-04-05 19:35:17 +0000284
285=back
286
John Beppu4a25d8c2001-02-23 02:33:28 +0000287=head1 FILES
288
John Beppue6967b22001-02-23 17:51:08 +0000289F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000290
291=head1 COPYRIGHT
292
293Copyright (c) 2001 John BEPPU. All rights reserved. This program is
294free software; you can redistribute it and/or modify it under the same
295terms as Perl itself.
296
297=head1 AUTHOR
298
John Beppuce22fee2001-10-31 04:29:18 +0000299John BEPPU <b@ax9.org>
John Beppu4a25d8c2001-02-23 02:33:28 +0000300
301=cut
302
Eric Andersenf7300882004-04-06 15:26:25 +0000303# $Id: autodocifier.pl,v 1.26 2004/04/06 15:26:25 andersen Exp $