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