First pass at making up an automagical usage message extractor, which
[oweals/busybox.git] / docs / autodocifier.pl
1 #!/usr/bin/perl -w
2 #
3 # autodocufier.pl - extracts usage messages from busybox usage.c and
4 # pretty-prints them to stdout.
5
6 use strict;
7
8 my $line;
9 my $applet;
10 my $count;
11 my $full_usage;
12
13 open(USAGE, 'usage.h') or die "usage.h: $!";
14
15 while (defined($line = <USAGE>)) {
16         $count=0;
17         if ($line =~ /^#define (\w+)_trivial_usage/) {
18                 # grab the applet name
19                 $applet = $1;
20                 print "\n$applet:\n";
21
22                 while (defined($line = <USAGE>)) {
23                         if ( $count==0 ) {
24                             $count++;
25                             print "\t$applet ";
26                         } else { print "\t"; }
27                         $full_usage = $applet . "_full_usage";
28                         last if ( $line =~ /$full_usage/ );
29                         # Skip preprocessor stuff
30                         next if $line =~ /^\s*#/;
31                         # Strip the continuation char
32                         $line =~ s/\\$//;
33                         # strip quotes off
34                         $line =~ s/^\s*"//;
35                         $line =~ s/"\s*$//;
36                         # substitute escape sequences
37                         # (there's probably a better way to do this...)
38                         $line =~ s/\\t/ /g;
39                         $line =~ s/\\n//g;
40                         # fix up preprocessor macros
41                         $line =~ s/USAGE_\w+\([\s]*?(".*?").*?\)/$1/sg;
42                         # Strip any empty quotes out
43                         $line =~ s/"[\s]*"//sg;
44                         # strip line end quotes, again
45                         $line =~ s/^\s*"//;
46                         $line =~ s/"\s*$//;
47
48                         # Finally, print it
49                         print "$line\n";
50                 }
51                 printf("\n");
52                 while (defined($line = <USAGE>)) {
53                         if ( $count==0 ) {
54                             $count++;
55                             print "\t$applet ";
56                         } else { print "\t"; }
57                         # we're done if we hit a line lacking a '\' at the end
58                         #last if ! $line !~ /\\$/;
59                         if ( $line !~ /\\$/ ) {
60                             #print "Got one at $line\n";
61                             last;
62                         }
63                         # Skip preprocessor stuff
64                         next if $line =~ /^\s*#/;
65                         # Strip the continuation char
66                         $line =~ s/\\$//;
67                         # strip quotes off
68                         $line =~ s/^\s*"//;
69                         $line =~ s/"\s*$//;
70                         # substitute escape sequences
71                         # (there's probably a better way to do this...)
72                         $line =~ s/\\t/ /g;
73                         $line =~ s/\\n//g;
74                         # Automagically #define all preprocessor lines
75                         #$line =~ s/USAGE_\w+\([\s]*?(".*?")\s,\s".*"\s\)/$1/sg;
76                         $line =~ s/USAGE_\w+\(\s*?(".*").*\)/$1/sg;
77                         # Strip any empty quotes out
78                         $line =~ s/"[\s]*"//sg;
79                         # strip line end quotes, again
80                         $line =~ s/^\s*"//;
81                         $line =~ s/"\s*$//;
82
83                         # Finally, print it
84                         print "$line\n";
85                 }
86                 printf("\n\n");
87         }
88 }