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