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*$//; |
John Beppu | 79359d8 | 2001-04-05 20:03:33 +0000 | [diff] [blame^] | 14 | #$s =~ s/#.*$//; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 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 | 79359d8 | 2001-04-05 20:03:33 +0000 | [diff] [blame^] | 34 | eval qq[ sprintf(qq{$_}) ] |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 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 | |
Eric Andersen | 0d3a02e | 2001-03-15 18:14:25 +0000 | [diff] [blame] | 65 | # prepare notes if they exists |
| 66 | my $notes = (defined $usage->{notes}) |
| 67 | ? "$usage->{notes}\n\n" |
| 68 | : ""; |
| 69 | |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 70 | # prepare example if one exists |
| 71 | my $example = (defined $usage->{example}) |
John Beppu | e708cb5 | 2001-03-15 21:08:01 +0000 | [diff] [blame] | 72 | ? |
| 73 | "Example:\n\n" . |
| 74 | join ("\n", |
| 75 | map { "\t$_" } |
| 76 | split("\n", $usage->{example})) . "\n\n" |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 77 | : ""; |
| 78 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 79 | return |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 80 | "=item B<$name>". |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 81 | "\n\n" . |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 82 | "$name $trivial". |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 83 | "\n\n" . |
| 84 | $full . |
| 85 | "\n\n" . |
Eric Andersen | 0d3a02e | 2001-03-15 18:14:25 +0000 | [diff] [blame] | 86 | $notes . |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 87 | $example. |
| 88 | "-------------------------------". |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 89 | "\n\n" |
| 90 | ; |
| 91 | } |
| 92 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 93 | # FIXME | generate SGML for an applet |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 94 | sub sgml_for_usage { |
| 95 | my $name = shift; |
| 96 | my $usage = shift; |
| 97 | return |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 98 | "<fixme>\n". |
| 99 | " $name\n". |
| 100 | "</fixme>\n" |
| 101 | ; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 102 | } |
| 103 | |
John Beppu | 8c16bc5 | 2001-02-23 02:54:31 +0000 | [diff] [blame] | 104 | # the keys are applet names, and |
| 105 | # the values will contain hashrefs of the form: |
| 106 | # |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 107 | # { |
| 108 | # trivial => "...", |
| 109 | # full => "...", |
John Beppu | 138ece0 | 2001-03-06 19:25:25 +0000 | [diff] [blame] | 110 | # example => "...", |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 111 | # } |
| 112 | my %docs; |
| 113 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 114 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 115 | # get command-line options |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 116 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 117 | my %opt; |
| 118 | |
| 119 | GetOptions( |
| 120 | \%opt, |
| 121 | "help|h", |
| 122 | "sgml|s", |
| 123 | "pod|p", |
| 124 | "verbose|v", |
| 125 | ); |
| 126 | |
| 127 | if (defined $opt{help}) { |
| 128 | print |
| 129 | "$0 [OPTION]... [FILE]...\n", |
| 130 | "\t--help\n", |
| 131 | "\t--sgml\n", |
| 132 | "\t--pod\n", |
| 133 | "\t--verbose\n", |
| 134 | ; |
| 135 | exit 1; |
| 136 | } |
| 137 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 138 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 139 | # collect documenation into %docs |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 140 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 141 | foreach (@ARGV) { |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 142 | open(USAGE, $_) || die("$0: $_: $!"); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 143 | my $fh = *USAGE; |
| 144 | my ($applet, $type, @line); |
| 145 | while (<$fh>) { |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 146 | if (/^#define (\w+)_(\w+)_usage/) { |
| 147 | $applet = $1; |
| 148 | $type = $2; |
| 149 | @line = continuation($fh); |
| 150 | my $doc = $docs{$applet} ||= { }; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 151 | my $text = join("\n", @line); |
| 152 | $doc->{$type} = beautify($text); |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 153 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 156 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 157 | |
| 158 | # generate structured documentation |
| 159 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 160 | my $generator = \&pod_for_usage; |
| 161 | if (defined $opt{sgml}) { |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 162 | $generator = \&sgml_for_usage; |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 163 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 164 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 165 | foreach my $applet (sort keys %docs) { |
| 166 | print $generator->($applet, $docs{$applet}); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | exit 0; |
| 170 | |
| 171 | __END__ |
| 172 | |
| 173 | =head1 NAME |
| 174 | |
| 175 | autodocifier.pl - generate docs for busybox based on usage.h |
| 176 | |
| 177 | =head1 SYNOPSIS |
| 178 | |
| 179 | autodocifier.pl usage.h > something |
| 180 | |
| 181 | =head1 DESCRIPTION |
| 182 | |
| 183 | The purpose of this script is to automagically generate documentation |
| 184 | for busybox using its usage.h as the original source for content. |
| 185 | Currently, the same content has to be duplicated in 3 places in |
| 186 | slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 187 | 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] | 188 | |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 189 | 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] | 190 | |
| 191 | =head1 OPTIONS |
| 192 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 193 | =over 4 |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 194 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 195 | =item B<--help> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 196 | |
| 197 | This displays the help message. |
| 198 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 199 | =item B<--pod> |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 200 | |
| 201 | Generate POD (this is the default) |
| 202 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 203 | =item B<--sgml> |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 204 | |
| 205 | Generate SGML |
| 206 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 207 | =item B<--verbose> |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 208 | |
| 209 | Be verbose (not implemented) |
| 210 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 211 | =back |
| 212 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 213 | =head1 FORMAT |
| 214 | |
| 215 | The following is an example of some data this script might parse. |
| 216 | |
| 217 | #define length_trivial_usage \ |
| 218 | "STRING" |
| 219 | #define length_full_usage \ |
| 220 | "Prints out the length of the specified STRING." |
| 221 | #define length_example_usage \ |
| 222 | "$ length "Hello"\n" \ |
| 223 | "5\n" |
| 224 | |
| 225 | Each entry is a cpp macro that defines a string. The macros are |
| 226 | named systematically in the form: |
| 227 | |
| 228 | $name_$type_usage |
| 229 | |
| 230 | $name is the name of the applet. $type can be "trivial", "full", "notes", |
| 231 | or "example". Every documentation macro must end with "_usage". |
| 232 | |
| 233 | The definition of the types is as follows: |
| 234 | |
| 235 | =over 4 |
| 236 | |
| 237 | =item B<trivial> |
| 238 | |
| 239 | This should be a brief, one-line description of parameters that |
| 240 | the command expects. This will be displayed when B<-h> is issued to |
| 241 | a command. I<REQUIRED> |
| 242 | |
| 243 | =item B<full> |
| 244 | |
| 245 | This should contain descriptions of each option. This will also |
| 246 | be displayed along with the trivial help if BB_FEATURE_TRIVIAL_HELP |
| 247 | is disabled. I<REQUIRED> |
| 248 | |
| 249 | =item B<notes> |
| 250 | |
| 251 | This is documentation that is intended to go in the POD or SGML, but |
| 252 | not be output when a B<-h> is given to a command. To see an example |
| 253 | of notes being used, see init_notes_usage. I<OPTIONAL> |
| 254 | |
| 255 | =item B<example> |
| 256 | |
| 257 | This should be an example of how the command is acutally used. |
| 258 | I<OPTIONAL> |
| 259 | |
| 260 | =back |
| 261 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 262 | =head1 FILES |
| 263 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 264 | F<usage.h> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 265 | |
| 266 | =head1 COPYRIGHT |
| 267 | |
| 268 | Copyright (c) 2001 John BEPPU. All rights reserved. This program is |
| 269 | free software; you can redistribute it and/or modify it under the same |
| 270 | terms as Perl itself. |
| 271 | |
| 272 | =head1 AUTHOR |
| 273 | |
| 274 | John BEPPU <beppu@lineo.com> |
| 275 | |
| 276 | =cut |
| 277 | |
John Beppu | 79359d8 | 2001-04-05 20:03:33 +0000 | [diff] [blame^] | 278 | # $Id: autodocifier.pl,v 1.19 2001/04/05 20:03:33 beppu Exp $ |