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 | df1e9da | 2001-02-23 16:15:34 +0000 | [diff] [blame] | 24 | $text =~ s/USAGE_NOT\w+\(.*?"\s*\)//sxg; |
| 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('', |
| 29 | map { eval } |
| 30 | map { qq[ sprintf(qq#$_#) ] } |
| 31 | map { |
| 32 | s/^\s*//; |
| 33 | s/"//g; |
| 34 | s/% /%% /g; |
| 35 | $_ |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 36 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 37 | @line |
| 38 | ); |
| 39 | return $text; |
| 40 | } |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 41 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 42 | # generate POD for an applet |
| 43 | sub pod_for_usage { |
| 44 | my $name = shift; |
| 45 | my $usage = shift; |
| 46 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame^] | 47 | # make options bold |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 48 | my $trivial = $usage->{trivial}; |
John Beppu | 8c16bc5 | 2001-02-23 02:54:31 +0000 | [diff] [blame] | 49 | $trivial =~s/(?<!\w)(-\w+)/B<$1>/sxg; |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame^] | 50 | my @f1; |
| 51 | my @f0 = |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 52 | map { $_ !~ /^\s/ && s/(?<!\w)(-\w+)/B<$1>/g; $_ } |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame^] | 53 | split("\n", $usage->{full}); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 54 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame^] | 55 | # add "\n" prior to certain lines to make indented |
| 56 | # lines look right |
| 57 | my $len = @f0; |
| 58 | for (my $i = 0; $i < $len; $i++) { |
| 59 | push @f1, $f0[$i]; |
| 60 | if (($i+1) != $len && $f0[$i] !~ /^\s/ && $f0[$i+1] =~ /^\s/) { |
| 61 | next if ($f0[$i] =~ /^$/); |
| 62 | push(@f1, "") unless ($f0[$i+1] =~ /^\s*$/s); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | my $full = join("\n", @f1); |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 67 | return |
| 68 | "-------------------------------\n". |
| 69 | "\n". |
| 70 | "=item $name". |
| 71 | "\n\n". |
| 72 | "$name $trivial". |
| 73 | "\n\n". |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 74 | $full. |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 75 | "\n\n" |
| 76 | ; |
| 77 | } |
| 78 | |
| 79 | # generate SGML for an applet |
| 80 | sub sgml_for_usage { |
| 81 | my $name = shift; |
| 82 | my $usage = shift; |
| 83 | return |
| 84 | "FIXME"; |
| 85 | } |
| 86 | |
John Beppu | 8c16bc5 | 2001-02-23 02:54:31 +0000 | [diff] [blame] | 87 | # the keys are applet names, and |
| 88 | # the values will contain hashrefs of the form: |
| 89 | # |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 90 | # { |
| 91 | # trivial => "...", |
| 92 | # full => "...", |
| 93 | # } |
| 94 | my %docs; |
| 95 | |
| 96 | # get command-line options |
| 97 | my %opt; |
| 98 | |
| 99 | GetOptions( |
| 100 | \%opt, |
| 101 | "help|h", |
| 102 | "sgml|s", |
| 103 | "pod|p", |
| 104 | "verbose|v", |
| 105 | ); |
| 106 | |
| 107 | if (defined $opt{help}) { |
| 108 | print |
| 109 | "$0 [OPTION]... [FILE]...\n", |
| 110 | "\t--help\n", |
| 111 | "\t--sgml\n", |
| 112 | "\t--pod\n", |
| 113 | "\t--verbose\n", |
| 114 | ; |
| 115 | exit 1; |
| 116 | } |
| 117 | |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 118 | # collect documenation into %docs |
| 119 | foreach (@ARGV) { |
| 120 | open(USAGE, $_) || die("$0: $!"); |
| 121 | my $fh = *USAGE; |
| 122 | my ($applet, $type, @line); |
| 123 | while (<$fh>) { |
| 124 | |
| 125 | if (/^#define (\w+)_(\w+)_usage/) { |
| 126 | $applet = $1; |
| 127 | $type = $2; |
| 128 | @line = continuation($fh); |
| 129 | my $doc = $docs{$applet} ||= { }; |
| 130 | |
| 131 | my $text = join("\n", @line); |
| 132 | $doc->{$type} = beautify($text); |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 133 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 134 | |
Eric Andersen | e13bc0b | 2001-02-22 22:47:06 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
John Beppu | 4a25d8c | 2001-02-23 02:33:28 +0000 | [diff] [blame] | 137 | |
| 138 | #use Data::Dumper; |
| 139 | #print Data::Dumper->Dump([\%docs], [qw(docs)]); |
| 140 | |
| 141 | foreach my $name (sort keys %docs) { |
| 142 | print pod_for_usage($name, $docs{$name}); |
| 143 | } |
| 144 | |
| 145 | exit 0; |
| 146 | |
| 147 | __END__ |
| 148 | |
| 149 | =head1 NAME |
| 150 | |
| 151 | autodocifier.pl - generate docs for busybox based on usage.h |
| 152 | |
| 153 | =head1 SYNOPSIS |
| 154 | |
| 155 | autodocifier.pl usage.h > something |
| 156 | |
| 157 | =head1 DESCRIPTION |
| 158 | |
| 159 | The purpose of this script is to automagically generate documentation |
| 160 | for busybox using its usage.h as the original source for content. |
| 161 | Currently, the same content has to be duplicated in 3 places in |
| 162 | slightly different formats -- F<usage.h>, F<docs/busybox.pod>, and |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 163 | 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] | 164 | |
John Beppu | b249fbb | 2001-02-23 03:12:45 +0000 | [diff] [blame] | 165 | 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] | 166 | |
| 167 | =head1 OPTIONS |
| 168 | |
| 169 | these control my behaviour |
| 170 | |
| 171 | =over 8 |
| 172 | |
| 173 | =item --help |
| 174 | |
| 175 | This displays the help message. |
| 176 | |
| 177 | =back |
| 178 | |
| 179 | =head1 FILES |
| 180 | |
| 181 | files that I manipulate |
| 182 | |
| 183 | =head1 COPYRIGHT |
| 184 | |
| 185 | Copyright (c) 2001 John BEPPU. All rights reserved. This program is |
| 186 | free software; you can redistribute it and/or modify it under the same |
| 187 | terms as Perl itself. |
| 188 | |
| 189 | =head1 AUTHOR |
| 190 | |
| 191 | John BEPPU <beppu@lineo.com> |
| 192 | |
| 193 | =cut |
| 194 | |
John Beppu | 8373e70 | 2001-02-23 17:41:41 +0000 | [diff] [blame^] | 195 | # $Id: autodocifier.pl,v 1.8 2001/02/23 17:41:41 beppu Exp $ |