blob: 9e18dc3c87f0c6a2d1cd8e5656230899f967f66d [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 Beppudbfff6c2001-02-23 17:55:03 +000024 $text =~ s/USAGE_NOT\w+\(.*?"\s*\)//sxg;
John Beppudf1e9da2001-02-23 16:15:34 +000025 $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('',
John Beppu4a25d8c2001-02-23 02:33:28 +000029 map {
30 s/^\s*//;
31 s/"//g;
John Beppu7d597c42001-02-24 14:37:48 +000032 s/%/%%/g;
John Beppud11578f2001-02-26 02:50:11 +000033 s/\$/\\\$/g;
John Beppu7d597c42001-02-24 14:37:48 +000034 eval qq[ sprintf(qq#$_#) ]
35 } @line
John Beppu4a25d8c2001-02-23 02:33:28 +000036 );
37 return $text;
38}
Eric Andersene13bc0b2001-02-22 22:47:06 +000039
John Beppu4a25d8c2001-02-23 02:33:28 +000040# generate POD for an applet
41sub pod_for_usage {
42 my $name = shift;
43 my $usage = shift;
44
John Beppu8373e702001-02-23 17:41:41 +000045 # make options bold
John Beppu4a25d8c2001-02-23 02:33:28 +000046 my $trivial = $usage->{trivial};
John Beppud11578f2001-02-26 02:50:11 +000047 $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg;
John Beppu8373e702001-02-23 17:41:41 +000048 my @f0 =
John Beppu4a25d8c2001-02-23 02:33:28 +000049 map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ }
John Beppu8373e702001-02-23 17:41:41 +000050 split("\n", $usage->{full});
John Beppu4a25d8c2001-02-23 02:33:28 +000051
John Beppu8373e702001-02-23 17:41:41 +000052 # add "\n" prior to certain lines to make indented
53 # lines look right
John Beppu7d597c42001-02-24 14:37:48 +000054 my @f1;
John Beppu8373e702001-02-23 17:41:41 +000055 my $len = @f0;
56 for (my $i = 0; $i < $len; $i++) {
57 push @f1, $f0[$i];
58 if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) {
59 next if ($f0[$i] =~ /^$/);
60 push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s);
61 }
62 }
John Beppu8373e702001-02-23 17:41:41 +000063 my $full = join("\n", @f1);
John Beppud11578f2001-02-26 02:50:11 +000064
65 # prepare example if one exists
66 my $example = (defined $usage->{example})
67 ? "Example:\n\n$usage->{example}\n\n"
68 : "";
69
John Beppu4a25d8c2001-02-23 02:33:28 +000070 return
John Beppud11578f2001-02-26 02:50:11 +000071 "=item I<$name>".
72 "\n\n" .
John Beppu4a25d8c2001-02-23 02:33:28 +000073 "$name $trivial".
John Beppud11578f2001-02-26 02:50:11 +000074 "\n\n" .
75 $full .
76 "\n\n" .
77 $example.
78 "-------------------------------".
John Beppu4a25d8c2001-02-23 02:33:28 +000079 "\n\n"
80 ;
81}
82
John Beppu7d597c42001-02-24 14:37:48 +000083# FIXME | generate SGML for an applet
John Beppu4a25d8c2001-02-23 02:33:28 +000084sub sgml_for_usage {
85 my $name = shift;
86 my $usage = shift;
87 return
John Beppue6967b22001-02-23 17:51:08 +000088 "<fixme>\n".
89 " $name\n".
90 "</fixme>\n"
91 ;
John Beppu4a25d8c2001-02-23 02:33:28 +000092}
93
John Beppu8c16bc52001-02-23 02:54:31 +000094# the keys are applet names, and
95# the values will contain hashrefs of the form:
96#
John Beppu4a25d8c2001-02-23 02:33:28 +000097# {
98# trivial => "...",
99# full => "...",
John Beppu138ece02001-03-06 19:25:25 +0000100# example => "...",
John Beppu4a25d8c2001-02-23 02:33:28 +0000101# }
102my %docs;
103
John Beppu7d597c42001-02-24 14:37:48 +0000104
John Beppu4a25d8c2001-02-23 02:33:28 +0000105# get command-line options
John Beppu7d597c42001-02-24 14:37:48 +0000106
John Beppu4a25d8c2001-02-23 02:33:28 +0000107my %opt;
108
109GetOptions(
110 \%opt,
111 "help|h",
112 "sgml|s",
113 "pod|p",
114 "verbose|v",
115);
116
117if (defined $opt{help}) {
118 print
119 "$0 [OPTION]... [FILE]...\n",
120 "\t--help\n",
121 "\t--sgml\n",
122 "\t--pod\n",
123 "\t--verbose\n",
124 ;
125 exit 1;
126}
127
John Beppu7d597c42001-02-24 14:37:48 +0000128
John Beppu4a25d8c2001-02-23 02:33:28 +0000129# collect documenation into %docs
John Beppu7d597c42001-02-24 14:37:48 +0000130
John Beppu4a25d8c2001-02-23 02:33:28 +0000131foreach (@ARGV) {
John Beppud11578f2001-02-26 02:50:11 +0000132 open(USAGE, $_) || die("$0: $_: $!");
John Beppu4a25d8c2001-02-23 02:33:28 +0000133 my $fh = *USAGE;
134 my ($applet, $type, @line);
135 while (<$fh>) {
John Beppu4a25d8c2001-02-23 02:33:28 +0000136 if (/^#define (\w+)_(\w+)_usage/) {
137 $applet = $1;
138 $type = $2;
139 @line = continuation($fh);
140 my $doc = $docs{$applet} ||= { };
John Beppu4a25d8c2001-02-23 02:33:28 +0000141 my $text = join("\n", @line);
142 $doc->{$type} = beautify($text);
Eric Andersene13bc0b2001-02-22 22:47:06 +0000143 }
Eric Andersene13bc0b2001-02-22 22:47:06 +0000144 }
145}
John Beppu4a25d8c2001-02-23 02:33:28 +0000146
John Beppu7d597c42001-02-24 14:37:48 +0000147
148# generate structured documentation
149
John Beppue6967b22001-02-23 17:51:08 +0000150my $generator = \&pod_for_usage;
151if (defined $opt{sgml}) {
John Beppu7d597c42001-02-24 14:37:48 +0000152 $generator = \&sgml_for_usage;
John Beppue6967b22001-02-23 17:51:08 +0000153}
John Beppu4a25d8c2001-02-23 02:33:28 +0000154
John Beppu7d597c42001-02-24 14:37:48 +0000155foreach my $applet (sort keys %docs) {
156 print $generator->($applet, $docs{$applet});
John Beppu4a25d8c2001-02-23 02:33:28 +0000157}
158
159exit 0;
160
161__END__
162
163=head1 NAME
164
165autodocifier.pl - generate docs for busybox based on usage.h
166
167=head1 SYNOPSIS
168
169autodocifier.pl usage.h > something
170
171=head1 DESCRIPTION
172
173The purpose of this script is to automagically generate documentation
174for busybox using its usage.h as the original source for content.
175Currently, the same content has to be duplicated in 3 places in
176slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and
John Beppub249fbb2001-02-23 03:12:45 +0000177F<docs/busybox.sgml>. This is tedious, so Perl has come to the rescue.
John Beppu4a25d8c2001-02-23 02:33:28 +0000178
John Beppub249fbb2001-02-23 03:12:45 +0000179This script was based on a script by Erik Andersen (andersen@lineo.com).
John Beppu4a25d8c2001-02-23 02:33:28 +0000180
181=head1 OPTIONS
182
John Beppue6967b22001-02-23 17:51:08 +0000183=over 4
John Beppu4a25d8c2001-02-23 02:33:28 +0000184
185=item --help
186
187This displays the help message.
188
John Beppue6967b22001-02-23 17:51:08 +0000189=item --pod
190
191Generate POD (this is the default)
192
193=item --sgml
194
195Generate SGML
196
197=item --verbose
198
199Be verbose (not implemented)
200
John Beppu4a25d8c2001-02-23 02:33:28 +0000201=back
202
203=head1 FILES
204
John Beppue6967b22001-02-23 17:51:08 +0000205F<usage.h>
John Beppu4a25d8c2001-02-23 02:33:28 +0000206
207=head1 COPYRIGHT
208
209Copyright (c) 2001 John BEPPU. All rights reserved. This program is
210free software; you can redistribute it and/or modify it under the same
211terms as Perl itself.
212
213=head1 AUTHOR
214
215John BEPPU <beppu@lineo.com>
216
217=cut
218
John Beppu138ece02001-03-06 19:25:25 +0000219# $Id: autodocifier.pl,v 1.14 2001/03/06 19:25:25 beppu Exp $