Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 1 | #!/usr/bin/perl -w |
Bernhard Reutner-Fischer | d129d7c | 2008-09-01 15:23:04 +0000 | [diff] [blame] | 2 | # vi: set sw=4 ts=4: |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 3 | |
| 4 | use strict; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 5 | use Getopt::Long; |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 6 | |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 7 | # collect lines continued with a '\' into an array |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 8 | sub continuation { |
| 9 | my $fh = shift; |
| 10 | my @line; |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 11 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 12 | while (<$fh>) { |
| 13 | my $s = $_; |
| 14 | $s =~ s/\\\s*$//; |
John Beppu | 79359d8 | 2001-04-05 20:03:33 +0000 | [diff] [blame] | 15 | #$s =~ s/#.*$//; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 16 | push @line, $s; |
| 17 | last unless (/\\\s*$/); |
| 18 | } |
| 19 | return @line; |
| 20 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 21 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 22 | # regex && eval away unwanted strings from documentation |
| 23 | sub beautify { |
| 24 | my $text = shift; |
Rob Landley | 52c7d7e | 2006-07-27 15:12:21 +0000 | [diff] [blame] | 25 | for (;;) { |
| 26 | my $text2 = $text; |
| 27 | $text =~ s/SKIP_\w+\(.*?"\s*\)//sxg; |
| 28 | $text =~ s/USE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg; |
Rob Landley | 44c7917 | 2006-10-24 21:46:19 +0000 | [diff] [blame] | 29 | $text =~ s/USAGE_\w+\(\s*?(.*?)"\s*\)/$1"/sxg; |
Rob Landley | 52c7d7e | 2006-07-27 15:12:21 +0000 | [diff] [blame] | 30 | last if ( $text2 eq $text ); |
| 31 | } |
John Beppu | df1e9da | 2001-02-23 16:15:34 +0000 | [diff] [blame] | 32 | $text =~ s/"\s*"//sg; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 33 | my @line = split("\n", $text); |
| 34 | $text = join('', |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 35 | map { |
Matt Kraai | 4e85356 | 2001-04-10 00:00:05 +0000 | [diff] [blame] | 36 | s/^\s*"//; |
| 37 | s/"\s*$//; |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 38 | s/%/%%/g; |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 39 | s/\$/\\\$/g; |
Denis Vlasenko | 1045d00 | 2008-08-31 21:29:35 +0000 | [diff] [blame] | 40 | s/\@/\\\@/g; |
John Beppu | 79359d8 | 2001-04-05 20:03:33 +0000 | [diff] [blame] | 41 | eval qq[ sprintf(qq{$_}) ] |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 42 | } @line |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 43 | ); |
| 44 | return $text; |
| 45 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 46 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 47 | # generate POD for an applet |
| 48 | sub pod_for_usage { |
| 49 | my $name = shift; |
| 50 | my $usage = shift; |
| 51 | |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 52 | # Sigh. Fixup the known odd-name applets. |
Bernhard Reutner-Fischer | d129d7c | 2008-09-01 15:23:04 +0000 | [diff] [blame] | 53 | # Perhaps we can use some of APPLET_ODDNAME from include/applets.h ? |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 54 | $name =~ s/dpkg_deb/dpkg-deb/g; |
| 55 | $name =~ s/fsck_minix/fsck.minix/g; |
| 56 | $name =~ s/mkfs_minix/mkfs.minix/g; |
| 57 | $name =~ s/run_parts/run-parts/g; |
| 58 | $name =~ s/start_stop_daemon/start-stop-daemon/g; |
Bernhard Reutner-Fischer | d129d7c | 2008-09-01 15:23:04 +0000 | [diff] [blame] | 59 | $name =~ s/ether_wake/ether-wake/g; |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 60 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 61 | # make options bold |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 62 | my $trivial = $usage->{trivial}; |
Mike Frysinger | ba9c4d1 | 2006-02-06 01:11:34 +0000 | [diff] [blame] | 63 | if (!defined $usage->{trivial}) { |
| 64 | $trivial = ""; |
| 65 | } else { |
| 66 | $trivial =~ s/(?<!\w)(-\w+)/B<$1>/sxg; |
| 67 | } |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 68 | my @f0 = |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 69 | map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ } |
Mike Frysinger | ba9c4d1 | 2006-02-06 01:11:34 +0000 | [diff] [blame] | 70 | split("\n", (defined $usage->{full} ? $usage->{full} : "")); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 71 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 72 | # add "\n" prior to certain lines to make indented |
| 73 | # lines look right |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 74 | my @f1; |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 75 | my $len = @f0; |
| 76 | for (my $i = 0; $i < $len; $i++) { |
| 77 | push @f1, $f0[$i]; |
| 78 | if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) { |
| 79 | next if ($f0[$i] =~ /^$/); |
| 80 | push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s); |
| 81 | } |
| 82 | } |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame] | 83 | my $full = join("\n", @f1); |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 84 | |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 85 | # prepare notes if they exist |
Eric Andersen | 0d3a02e | 2001-03-15 18:14:25 +0000 | [diff] [blame] | 86 | my $notes = (defined $usage->{notes}) |
| 87 | ? "$usage->{notes}\n\n" |
| 88 | : ""; |
| 89 | |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 90 | # prepare examples if they exist |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 91 | my $example = (defined $usage->{example}) |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 92 | ? |
John Beppu | e708cb5 | 2001-03-15 21:08:01 +0000 | [diff] [blame] | 93 | "Example:\n\n" . |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 94 | join ("\n", |
| 95 | map { "\t$_" } |
John Beppu | e708cb5 | 2001-03-15 21:08:01 +0000 | [diff] [blame] | 96 | split("\n", $usage->{example})) . "\n\n" |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 97 | : ""; |
| 98 | |
Mike Frysinger | 27a74e8 | 2006-02-07 00:58:11 +0000 | [diff] [blame] | 99 | # Pad the name so that the applet name gets a line |
| 100 | # by itself in BusyBox.txt |
| 101 | my $spaces = 10 - length($name); |
| 102 | if ($spaces > 0) { |
| 103 | $name .= " " x $spaces; |
| 104 | } |
| 105 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 106 | return |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 107 | "=item B<$name>". |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 108 | "\n\n$name $trivial\n\n". |
| 109 | "$full\n\n" . |
| 110 | "$notes" . |
| 111 | "$example" . |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 112 | "\n\n" |
| 113 | ; |
| 114 | } |
| 115 | |
Eric Andersen | 55c704c | 2004-03-13 08:32:14 +0000 | [diff] [blame] | 116 | # the keys are applet names, and |
John Beppu | 8c16bc5 | 2001-02-23 02:54:31 +0000 | [diff] [blame] | 117 | # the values will contain hashrefs of the form: |
| 118 | # |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 119 | # { |
| 120 | # trivial => "...", |
| 121 | # full => "...", |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 122 | # notes => "...", |
John Beppu | 138ece0 | 2001-03-06 19:25:25 +0000 | [diff] [blame] | 123 | # example => "...", |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 124 | # } |
| 125 | my %docs; |
| 126 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 127 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 128 | # get command-line options |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 129 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 130 | my %opt; |
| 131 | |
| 132 | GetOptions( |
| 133 | \%opt, |
| 134 | "help|h", |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 135 | "pod|p", |
| 136 | "verbose|v", |
| 137 | ); |
| 138 | |
| 139 | if (defined $opt{help}) { |
| 140 | print |
| 141 | "$0 [OPTION]... [FILE]...\n", |
| 142 | "\t--help\n", |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 143 | "\t--pod\n", |
| 144 | "\t--verbose\n", |
| 145 | ; |
| 146 | exit 1; |
| 147 | } |
| 148 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 149 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 150 | # collect documenation into %docs |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 151 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 152 | foreach (@ARGV) { |
John Beppu | d11578f | 2001-02-26 02:50:11 +0000 | [diff] [blame] | 153 | open(USAGE, $_) || die("$0: $_: $!"); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 154 | my $fh = *USAGE; |
| 155 | my ($applet, $type, @line); |
| 156 | while (<$fh>) { |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 157 | if (/^#define (\w+)_(\w+)_usage/) { |
| 158 | $applet = $1; |
| 159 | $type = $2; |
| 160 | @line = continuation($fh); |
| 161 | my $doc = $docs{$applet} ||= { }; |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 162 | my $text = join("\n", @line); |
| 163 | $doc->{$type} = beautify($text); |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 164 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 167 | |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 168 | |
| 169 | # generate structured documentation |
| 170 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 171 | my $generator = \&pod_for_usage; |
Mike Frysinger | b0ed3d7 | 2006-02-05 22:10:40 +0000 | [diff] [blame] | 172 | |
| 173 | my @names = sort keys %docs; |
Mike Frysinger | 0380166 | 2006-02-07 00:51:07 +0000 | [diff] [blame] | 174 | my $line = "\t[, [[, "; |
Mike Frysinger | b0ed3d7 | 2006-02-05 22:10:40 +0000 | [diff] [blame] | 175 | for (my $i = 0; $i < $#names; $i++) { |
Mike Frysinger | 0380166 | 2006-02-07 00:51:07 +0000 | [diff] [blame] | 176 | if (length ($line.$names[$i]) >= 65) { |
| 177 | print "$line\n\t"; |
| 178 | $line = ""; |
Mike Frysinger | b0ed3d7 | 2006-02-05 22:10:40 +0000 | [diff] [blame] | 179 | } |
Mike Frysinger | 0380166 | 2006-02-07 00:51:07 +0000 | [diff] [blame] | 180 | $line .= "$names[$i], "; |
Mike Frysinger | b0ed3d7 | 2006-02-05 22:10:40 +0000 | [diff] [blame] | 181 | } |
Mike Frysinger | 0380166 | 2006-02-07 00:51:07 +0000 | [diff] [blame] | 182 | print $line . $names[-1]; |
Mike Frysinger | b0ed3d7 | 2006-02-05 22:10:40 +0000 | [diff] [blame] | 183 | |
| 184 | print "\n\n=head1 COMMAND DESCRIPTIONS\n"; |
| 185 | print "\n=over 4\n\n"; |
| 186 | |
| 187 | foreach my $applet (@names) { |
John Beppu | 7d597c4 | 2001-02-24 14:37:48 +0000 | [diff] [blame] | 188 | print $generator->($applet, $docs{$applet}); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | exit 0; |
| 192 | |
| 193 | __END__ |
| 194 | |
| 195 | =head1 NAME |
| 196 | |
| 197 | autodocifier.pl - generate docs for busybox based on usage.h |
| 198 | |
| 199 | =head1 SYNOPSIS |
| 200 | |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 201 | autodocifier.pl [OPTION]... [FILE]... |
| 202 | |
| 203 | Example: |
| 204 | |
| 205 | ( cat docs/busybox_header.pod; \ |
| 206 | docs/autodocifier.pl usage.h; \ |
| 207 | cat docs/busybox_footer.pod ) > docs/busybox.pod |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 208 | |
| 209 | =head1 DESCRIPTION |
| 210 | |
Eric Andersen | f730088 | 2004-04-06 15:26:25 +0000 | [diff] [blame] | 211 | The purpose of this script is to automagically generate |
| 212 | documentation for busybox using its usage.h as the original source |
| 213 | for content. It used to be that same content has to be duplicated |
| 214 | in 3 places in slightly different formats -- F<usage.h>, |
| 215 | F<docs/busybox.pod>. This was tedious and error-prone, so it was |
John Beppu | ce22fee | 2001-10-31 04:29:18 +0000 | [diff] [blame] | 216 | decided that F<usage.h> would contain all the text in a |
| 217 | machine-readable form, and scripts could be used to transform this |
| 218 | text into other forms if necessary. |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 219 | |
Eric Andersen | f730088 | 2004-04-06 15:26:25 +0000 | [diff] [blame] | 220 | F<autodocifier.pl> is one such script. It is based on a script by |
| 221 | Erik Andersen <andersen@codepoet.org> which was in turn based on a |
| 222 | script by Mark Whitley <markw@codepoet.org> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 223 | |
| 224 | =head1 OPTIONS |
| 225 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 226 | =over 4 |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 227 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 228 | =item B<--help> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 229 | |
| 230 | This displays the help message. |
| 231 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 232 | =item B<--pod> |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 233 | |
| 234 | Generate POD (this is the default) |
| 235 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 236 | =item B<--verbose> |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 237 | |
| 238 | Be verbose (not implemented) |
| 239 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 240 | =back |
| 241 | |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 242 | =head1 FORMAT |
| 243 | |
| 244 | The following is an example of some data this script might parse. |
| 245 | |
| 246 | #define length_trivial_usage \ |
| 247 | "STRING" |
| 248 | #define length_full_usage \ |
| 249 | "Prints out the length of the specified STRING." |
| 250 | #define length_example_usage \ |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 251 | "$ length Hello\n" \ |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 252 | "5\n" |
| 253 | |
| 254 | Each entry is a cpp macro that defines a string. The macros are |
| 255 | named systematically in the form: |
| 256 | |
| 257 | $name_$type_usage |
| 258 | |
| 259 | $name is the name of the applet. $type can be "trivial", "full", "notes", |
| 260 | or "example". Every documentation macro must end with "_usage". |
| 261 | |
| 262 | The definition of the types is as follows: |
| 263 | |
| 264 | =over 4 |
| 265 | |
| 266 | =item B<trivial> |
| 267 | |
| 268 | This should be a brief, one-line description of parameters that |
| 269 | the command expects. This will be displayed when B<-h> is issued to |
| 270 | a command. I<REQUIRED> |
| 271 | |
| 272 | =item B<full> |
| 273 | |
| 274 | This should contain descriptions of each option. This will also |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 275 | be displayed along with the trivial help if CONFIG_FEATURE_TRIVIAL_HELP |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 276 | is disabled. I<REQUIRED> |
| 277 | |
| 278 | =item B<notes> |
| 279 | |
| 280 | This is documentation that is intended to go in the POD or SGML, but |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 281 | not be printed when a B<-h> is given to a command. To see an example |
John Beppu | ce22fee | 2001-10-31 04:29:18 +0000 | [diff] [blame] | 282 | of notes being used, see init_notes_usage in F<usage.h>. I<OPTIONAL> |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 283 | |
| 284 | =item B<example> |
| 285 | |
John Beppu | ce22fee | 2001-10-31 04:29:18 +0000 | [diff] [blame] | 286 | This should be an example of how the command is actually used. |
John Beppu | 5d81768 | 2001-04-17 17:09:34 +0000 | [diff] [blame] | 287 | This will not be printed when a B<-h> is given to a command -- it |
John Beppu | ce22fee | 2001-10-31 04:29:18 +0000 | [diff] [blame] | 288 | will only be included in the POD or SGML documentation. I<OPTIONAL> |
John Beppu | 9a1395b | 2001-04-05 19:35:17 +0000 | [diff] [blame] | 289 | |
| 290 | =back |
| 291 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 292 | =head1 FILES |
| 293 | |
John Beppu | e6967b2 | 2001-02-23 17:51:08 +0000 | [diff] [blame] | 294 | F<usage.h> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 295 | |
| 296 | =head1 COPYRIGHT |
| 297 | |
| 298 | Copyright (c) 2001 John BEPPU. All rights reserved. This program is |
| 299 | free software; you can redistribute it and/or modify it under the same |
| 300 | terms as Perl itself. |
| 301 | |
| 302 | =head1 AUTHOR |
| 303 | |
John Beppu | ce22fee | 2001-10-31 04:29:18 +0000 | [diff] [blame] | 304 | John BEPPU <b@ax9.org> |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 305 | |
| 306 | =cut |
| 307 | |