blob: e02bca93ebf1fcf5ec26bd7ca51bae4bd992696c [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
84 "FIXME";
85}
86
John Beppu8c16bc52001-02-23 02:54:31 +000087# the keys are applet names, and
88# the values will contain hashrefs of the form:
89#
John Beppu4a25d8c2001-02-23 02:33:28 +000090# {
91# trivial => "...",
92# full => "...",
93# }
94my %docs;
95
96# get command-line options
97my %opt;
98
99GetOptions(
100 \%opt,
101 "help|h",
102 "sgml|s",
103 "pod|p",
104 "verbose|v",
105);
106
107if (defined $opt{help}) {
108 print
109 "$0 [OPTION]... [FILE]...\n",
110 "\t--help\n",
111 "\t--sgml\n",
112 "\t--pod\n",
113 "\t--verbose\n",
114 ;
115 exit 1;
116}
117
John Beppu4a25d8c2001-02-23 02:33:28 +0000118# collect documenation into %docs
119foreach (@ARGV) {
120 open(USAGE, $_) || die("$0: $!");
121 my $fh = *USAGE;
122 my ($applet, $type, @line);
123 while (<$fh>) {
124
125 if (/^#define (\w+)_(\w+)_usage/) {
126 $applet = $1;
127 $type = $2;
128 @line = continuation($fh);
129 my $doc = $docs{$applet} ||= { };
130
131 my $text = join("\n", @line);
132 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000133 }
John Beppu4a25d8c2001-02-23 02:33:28 +0000134
Eric Andersene13bc0b2001-02-22 22:47:06 +0000135 }
136}
John Beppu4a25d8c2001-02-23 02:33:28 +0000137
138#use Data::Dumper;
139#print Data::Dumper->Dump([\%docs], [qw(docs)]);
140
141foreach my $name (sort keys %docs) {
142 print pod_for_usage($name, $docs{$name});
143}
144
145exit 0;
146
147__END__
148
149=head1 NAME
150
151autodocifier.pl - generate docs for busybox based on usage.h
152
153=head1 SYNOPSIS
154
155autodocifier.pl usage.h > something
156
157=head1 DESCRIPTION
158
159The purpose of this script is to automagically generate documentation
160for busybox using its usage.h as the original source for content.
161Currently, the same content has to be duplicated in 3 places in
162slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and
John Beppub249fbb2001-02-23 03:12:45 +0000163F<docs/busybox.sgml>. This is tedious, so Perl has come to the rescue.
John Beppu4a25d8c2001-02-23 02:33:28 +0000164
John Beppub249fbb2001-02-23 03:12:45 +0000165This script was based on a script by Erik Andersen (andersen@lineo.com).
John Beppu4a25d8c2001-02-23 02:33:28 +0000166
167=head1 OPTIONS
168
169these control my behaviour
170
171=over 8
172
173=item --help
174
175This displays the help message.
176
177=back
178
179=head1 FILES
180
181files that I manipulate
182
183=head1 COPYRIGHT
184
185Copyright (c) 2001 John BEPPU. All rights reserved. This program is
186free software; you can redistribute it and/or modify it under the same
187terms as Perl itself.
188
189=head1 AUTHOR
190
191John BEPPU <beppu@lineo.com>
192
193=cut
194
John Beppu8373e702001-02-23 17:41:41 +0000195# $Id: autodocifier.pl,v 1.8 2001/02/23 17:41:41 beppu Exp $