blob: 1190160da66f82a5892ff56c57b5643638ad6bea [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
John Beppu4a25d8c2001-02-23 02:33:28 +00006# collect lines continued with a '\' into an array
7sub 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*$//;
14 $s =~ s/#.*$//;
15 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 Beppudf1e9da2001-02-23 16:15:34 +000024 $text =~ s/USAGE_NOT\w+\(.*?"\s*\)//sxg;
25 $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('',
29 map { eval }
30 map { qq[ sprintf(qq#$_#) ] }
31 map {
32 s/^\s*//;
33 s/"//g;
34 s/% /%% /g;
35 $_
Eric Andersene13bc0b2001-02-22 22:47:06 +000036 }
John Beppu4a25d8c2001-02-23 02:33:28 +000037 @line
38 );
39 return $text;
40}
Eric Andersene13bc0b2001-02-22 22:47:06 +000041
John Beppu4a25d8c2001-02-23 02:33:28 +000042# generate POD for an applet
43sub pod_for_usage {
44 my $name = shift;
45 my $usage = shift;
46
John Beppu8373e702001-02-23 17:41:41 +000047 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000048 my $trivial = $usage->{trivial};
John Beppu8c16bc52001-02-23 02:54:31 +000049 $trivial =~s/(?<!\w)(-\w+)/B<$1>/sxg;
John Beppu8373e702001-02-23 17:41:41 +000050 my @f1;
51 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000052 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
John Beppu8373e702001-02-23 17:41:41 +000053 split("\n", $usage->{full});
John Beppu4a25d8c2001-02-23 02:33:28 +000054
John Beppu8373e702001-02-23 17:41:41 +000055 # add "\n" prior to certain lines to make indented
56 # lines look right
57 my $len = @f0;
58 for (my $i = 0; $i < $len; $i++) {
59 push @f1, $f0[$i];
60 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
61 next if ($f0[$i] =~ /^$/);
62 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
63 }
64 }
65
66 my $full = join("\n", @f1);
John Beppu4a25d8c2001-02-23 02:33:28 +000067 return
68 "-------------------------------\n".
69 "\n".
70 "=item $name".
71 "\n\n".
72 "$name $trivial".
73 "\n\n".
John Beppub249fbb2001-02-23 03:12:45 +000074 $full.
John Beppu4a25d8c2001-02-23 02:33:28 +000075 "\n\n"
76 ;
77}
78
79# generate SGML for an applet
80sub sgml_for_usage {
81 my $name = shift;
82 my $usage = shift;
83 return
John Beppue6967b22001-02-23 17:51:08 +000084 "<fixme>\n".
85 " $name\n".
86 "</fixme>\n"
87 ;
John Beppu4a25d8c2001-02-23 02:33:28 +000088}
89
John Beppu8c16bc52001-02-23 02:54:31 +000090# the keys are applet names, and
91# the values will contain hashrefs of the form:
92#
John Beppu4a25d8c2001-02-23 02:33:28 +000093# {
94# trivial => "...",
95# full => "...",
96# }
97my %docs;
98
99# get command-line options
100my %opt;
101
102GetOptions(
103 \%opt,
104 "help|h",
105 "sgml|s",
106 "pod|p",
107 "verbose|v",
108);
109
110if (defined $opt{help}) {
111 print
112 "$0 [OPTION]... [FILE]...\n",
113 "\t--help\n",
114 "\t--sgml\n",
115 "\t--pod\n",
116 "\t--verbose\n",
117 ;
118 exit 1;
119}
120
John Beppu4a25d8c2001-02-23 02:33:28 +0000121# collect documenation into %docs
122foreach (@ARGV) {
123 open(USAGE, $_) || die("$0: $!");
124 my $fh = *USAGE;
125 my ($applet, $type, @line);
126 while (<$fh>) {
127
128 if (/^#define (\w+)_(\w+)_usage/) {
129 $applet = $1;
130 $type = $2;
131 @line = continuation($fh);
132 my $doc = $docs{$applet} ||= { };
133
134 my $text = join("\n", @line);
135 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000136 }
John Beppu4a25d8c2001-02-23 02:33:28 +0000137
Eric Andersene13bc0b2001-02-22 22:47:06 +0000138 }
139}
John Beppu4a25d8c2001-02-23 02:33:28 +0000140
John Beppue6967b22001-02-23 17:51:08 +0000141my $generator = \&pod_for_usage;
142if (defined $opt{sgml}) {
143 $generator = \&sgml_for_usage;
144}
John Beppu4a25d8c2001-02-23 02:33:28 +0000145
146foreach my $name (sort keys %docs) {
John Beppue6967b22001-02-23 17:51:08 +0000147 print $generator->($name, $docs{$name});
John Beppu4a25d8c2001-02-23 02:33:28 +0000148}
149
150exit 0;
151
152__END__
153
154=head1 NAME
155
156autodocifier.pl - generate docs for busybox based on usage.h
157
158=head1 SYNOPSIS
159
160autodocifier.pl usage.h > something
161
162=head1 DESCRIPTION
163
164The purpose of this script is to automagically generate documentation
165for busybox using its usage.h as the original source for content.
166Currently, the same content has to be duplicated in 3 places in
167slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and
John Beppub249fbb2001-02-23 03:12:45 +0000168F<docs/busybox.sgml>. This is tedious, so Perl has come to the rescue.
John Beppu4a25d8c2001-02-23 02:33:28 +0000169
John Beppub249fbb2001-02-23 03:12:45 +0000170This script was based on a script by Erik Andersen (andersen@lineo.com).
John Beppu4a25d8c2001-02-23 02:33:28 +0000171
172=head1 OPTIONS
173
John Beppue6967b22001-02-23 17:51:08 +0000174=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000175
176=item --help
177
178This displays the help message.
179
John Beppue6967b22001-02-23 17:51:08 +0000180=item --pod
181
182Generate POD (this is the default)
183
184=item --sgml
185
186Generate SGML
187
188=item --verbose
189
190Be verbose (not implemented)
191
John Beppu4a25d8c2001-02-23 02:33:28 +0000192=back
193
194=head1 FILES
195
John Beppue6967b22001-02-23 17:51:08 +0000196F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000197
198=head1 COPYRIGHT
199
200Copyright (c) 2001 John BEPPU. All rights reserved. This program is
201free software; you can redistribute it and/or modify it under the same
202terms as Perl itself.
203
204=head1 AUTHOR
205
206John BEPPU <beppu@lineo.com>
207
208=cut
209
John Beppue6967b22001-02-23 17:51:08 +0000210# $Id: autodocifier.pl,v 1.9 2001/02/23 17:51:08 beppu Exp $