Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 2 | |
| 3 | use strict; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 4 | use Getopt::Long; |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 5 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 6 | # collect lines continued with a '\' into an array |
| 7 | sub continuation { |
| 8 | my $fh = shift; |
| 9 | my @line; |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 10 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 11 | 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 Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 20 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 21 | # regex && eval away unwanted strings from documentation |
| 22 | sub beautify { |
| 23 | my $text = shift; |
John Beppu | dbfff6c | 2001-02-23 17:55:03 +0000 | [diff] [blame] | 24 | $text =~ s/USAGE_NOT\w+\(.*?"\s*\)//sxg; |
John Beppu | df1e9da | 2001-02-23 16:15:34 +0000 | [diff] [blame] | 25 | $text =~ s/USAGE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg; |
| 26 | $text =~ s/"\s*"//sg; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 27 | my @line = split("\n", $text); |
| 28 | $text = join('', |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 29 | map { |
| 30 | s/^\s*//; |
| 31 | s/"//g; |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 32 | s/%/%%/g; |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 33 | s/\$/\\\$/g; |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 34 | eval qq[ sprintf(qq#$_#) ] |
| 35 | } @line |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 36 | ); |
| 37 | return $text; |
| 38 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 39 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 40 | # generate POD for an applet |
| 41 | sub pod_for_usage { |
| 42 | my $name = shift; |
| 43 | my $usage = shift; |
| 44 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 45 | # make options bold |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 46 | my $trivial = $usage->{trivial}; |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 47 | $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg; |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 48 | my @f0 = |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 49 | map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ } |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 50 | split("\n", $usage->{full}); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 51 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 52 | # add "\n" prior to certain lines to make indented |
| 53 | # lines look right |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 54 | my @f1; |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 55 | 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 Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 63 | my $full = join("\n", @f1); |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 64 | |
| 65 | # prepare example if one exists |
| 66 | my $example = (defined $usage->{example}) |
| 67 | ? "Example:\n\n$usage->{example}\n\n" |
| 68 | : ""; |
| 69 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 70 | return |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 71 | "=item I<$name>". |
| 72 | "\n\n" . |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 73 | "$name $trivial". |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 74 | "\n\n" . |
| 75 | $full . |
| 76 | "\n\n" . |
| 77 | $example. |
| 78 | "-------------------------------". |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 79 | "\n\n" |
| 80 | ; |
| 81 | } |
| 82 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 83 | # FIXME | generate SGML for an applet |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 84 | sub sgml_for_usage { |
| 85 | my $name = shift; |
| 86 | my $usage = shift; |
| 87 | return |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 88 | "<fixme>\n". |
| 89 | " $name\n". |
| 90 | "</fixme>\n" |
| 91 | ; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 92 | } |
| 93 | |
John Beppu | 8c16bc5 | 2001-02-23 02:54:31 +0000 | [diff] [blame] | 94 | # the keys are applet names, and |
| 95 | # the values will contain hashrefs of the form: |
| 96 | # |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 97 | # { |
| 98 | # trivial => "...", |
| 99 | # full => "...", |
John Beppu | 138ece0 | 2001-03-06 19:25:25 +0000 | [diff] [blame] | 100 | # example => "...", |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 101 | # } |
| 102 | my %docs; |
| 103 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 104 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 105 | # get command-line options |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 106 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 107 | my %opt; |
| 108 | |
| 109 | GetOptions( |
| 110 | \%opt, |
| 111 | "help|h", |
| 112 | "sgml|s", |
| 113 | "pod|p", |
| 114 | "verbose|v", |
| 115 | ); |
| 116 | |
| 117 | if (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 Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 128 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 129 | # collect documenation into %docs |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 130 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 131 | foreach (@ARGV) { |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 132 | open(USAGE, $_) || die("$0: $_: $!"); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 133 | my $fh = *USAGE; |
| 134 | my ($applet, $type, @line); |
| 135 | while (<$fh>) { |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 136 | if (/^#define (\w+)_(\w+)_usage/) { |
| 137 | $applet = $1; |
| 138 | $type = $2; |
| 139 | @line = continuation($fh); |
| 140 | my $doc = $docs{$applet} ||= { }; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 141 | my $text = join("\n", @line); |
| 142 | $doc->{$type} = beautify($text); |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 143 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 146 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 147 | |
| 148 | # generate structured documentation |
| 149 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 150 | my $generator = \&pod_for_usage; |
| 151 | if (defined $opt{sgml}) { |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 152 | $generator = \&sgml_for_usage; |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 153 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 154 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 155 | foreach my $applet (sort keys %docs) { |
| 156 | print $generator->($applet, $docs{$applet}); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | exit 0; |
| 160 | |
| 161 | __END__ |
| 162 | |
| 163 | =head1 NAME |
| 164 | |
| 165 | autodocifier.pl - generate docs for busybox based on usage.h |
| 166 | |
| 167 | =head1 SYNOPSIS |
| 168 | |
| 169 | autodocifier.pl usage.h > something |
| 170 | |
| 171 | =head1 DESCRIPTION |
| 172 | |
| 173 | The purpose of this script is to automagically generate documentation |
| 174 | for busybox using its usage.h as the original source for content. |
| 175 | Currently, the same content has to be duplicated in 3 places in |
| 176 | slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 177 | F<docs/busybox.sgml>. This is tedious, so Perl has come to the rescue. |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 178 | |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 179 | This script was based on a script by Erik Andersen (andersen@lineo.com). |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 180 | |
| 181 | =head1 OPTIONS |
| 182 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 183 | =over 4 |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 184 | |
| 185 | =item --help |
| 186 | |
| 187 | This displays the help message. |
| 188 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 189 | =item --pod |
| 190 | |
| 191 | Generate POD (this is the default) |
| 192 | |
| 193 | =item --sgml |
| 194 | |
| 195 | Generate SGML |
| 196 | |
| 197 | =item --verbose |
| 198 | |
| 199 | Be verbose (not implemented) |
| 200 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 201 | =back |
| 202 | |
| 203 | =head1 FILES |
| 204 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 205 | F<usage.h> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 206 | |
| 207 | =head1 COPYRIGHT |
| 208 | |
| 209 | Copyright (c) 2001 John BEPPU. All rights reserved. This program is |
| 210 | free software; you can redistribute it and/or modify it under the same |
| 211 | terms as Perl itself. |
| 212 | |
| 213 | =head1 AUTHOR |
| 214 | |
| 215 | John BEPPU <beppu@lineo.com> |
| 216 | |
| 217 | =cut |
| 218 | |
John Beppu | 138ece0 | 2001-03-06 19:25:25 +0000 | [diff] [blame] | 219 | # $Id: autodocifier.pl,v 1.14 2001/03/06 19:25:25 beppu Exp $ |