]> mj.ucw.cz Git - libucw.git/blob - build/doc-extract
9ce8a3c5e06547e7cf6ae35e8404cabc16b25b9c
[libucw.git] / build / doc-extract
1 #!/usr/bin/perl
2 # Script for extracting documentation out of header files
3 # (c) 2008 Michal Vaner <vorner@ucw.cz>
4
5 use strict;
6 use warnings;
7
8 my( $inname, $outname, $depname, $basedir, $defdump ) = @ARGV;
9 if( defined $inname ) {
10         open IN, $inname or die "Could not read $inname ($!)\n";
11 } else {
12         open IN, "<&STDIN" or die "Could not read stdin ($!)\n";
13 }
14 if( defined $outname ) {
15         open OUT, ">$outname" or die "Could not write $outname ($!)\n";
16 } else {
17         open OUT, ">&STDOUT" or die "Could not write to stdout ($!)\n";
18 }
19 my $hasdump;
20 if( defined $defdump ) {
21         open DUMP, ">$defdump" or die "Could not write definition dump $defdump ($!)\n";
22         $hasdump = 1;
23 }
24
25 sub detect( $ ) {
26         ( $_ ) = @_;
27         return( 'struct', 1, $1, "typedef struct { ... } $1;" ) if( /^\s*typedef\s+struct\s*{.*}\s*(\w+)\s*;\s*$/s );
28         return( 'enum', 1, $1, "typedef enum { ... } $1;" ) if( /^\s*typedef\s+enum\s*{.*}\s*(\w+)\s*;\s*$/s );
29         my $l = length;
30         s/\n.*//s;
31         return( 'struct', 0, $1, $_ ) if( /struct\s+(\w+)\s+{/ );
32         return( 'enum', 0, $1, $_ ) if( /enum\s+(\w+)\s+{/ );
33         if( $l > length ) {
34                 warn( "Unknown statement $_\n" );
35                 return( '', 0, $_, $_ );
36         }
37         return( 'def', 0, $1, $_ ) if( /#define\s+(\w+)/ );
38         return( 'fun', 1, $2, $1 ) if( /^(.*?(\w+)\([^{]*\)[^{]*)/ );
39         return( 'var', 1, $1, $_ ) if( /\s(\w+);/ );
40         warn( "Unknown statement $_\n" );
41         return( '', 0, $_, $_ );
42 }
43
44 my @deps;
45 my $id = 0;
46
47 sub formatNote( $$ ) {
48         my( $head, $comment ) = @_;
49         $head =~ s/(\S)[ ]+/$1 /g;
50         print OUT "\n";
51         print OUT "''''\n";
52         chomp $head;
53         my( $type, $semicolon, $name, $oneline ) = detect( $head );
54         $oneline =~ s/\s+$//;
55         $oneline =~ s/;?$/;/ if( $semicolon );
56         $head =~ s/;?\s*$/;/ if( $semicolon );
57         $head =~ s/(\.\.\.)/\\$1/g;
58         print OUT "[[${type}_$name]]\n";
59         if( $type eq 'fun' ) {
60                 print OUT "!!f!$head!!!\n\n";
61         } else {
62                 print OUT "..................\n";
63                 print OUT "$head\n";
64                 print OUT "..................\n\n";
65         }
66         if( $hasdump ) {
67                 print DUMP "$outname,${type}_$name,$type,$name,$oneline\n";
68                 $id ++;
69         }
70         print OUT "$comment\n\n";
71 }
72
73 sub process( $ ) {
74         my $file = shift;
75         open FILE, $file or die "Could nod read $file ($!)\n";
76         my $line;
77         my $active;
78         my $verbatim;
79         my $buff;
80         my $head;
81         my $struct;
82         while( defined( $line = <FILE> ) ) {
83                 chomp $line;
84                 if( $struct ) {
85                         $head .= "\n".$line;
86                         if( $line =~ /}/ ) {
87                                 formatNote( $head, $buff );
88                                 $struct = 0;
89                                 $buff = undef;
90                                 $head = undef;
91                         }
92                 } elsif( $verbatim ) {
93                         if( $line =~ /\*\// ) {
94                                 $verbatim = 0;
95                                 print OUT "\n";
96                         } else {
97                                 $line =~ s/^\s*\* ?//;
98                                 print OUT "$line\n";
99                         }
100                 } elsif( $active ) {
101                         if( $line =~ /\*\// ) {
102                                 $active = 0;
103                         } else {
104                                 $line =~ s/^\s*\* ?//;
105                                 $buff .= "$line\n";
106                         }
107                 } else {
108                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
109                                 if( $line =~ /\(/ || $line !~ /{/ ) {
110                                         $_ = $line;
111                                         s/^\s*\s?//;
112                                         s/\/\/.*//;
113                                         s/\/\*.*?\*\///gs;
114                                         formatNote( $_, $buff );
115                                         $head = undef;
116                                         $buff = undef;
117                                 } else {
118                                         $head = $line;
119                                         $struct = 1;
120                                 }
121                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
122                                 $buff =~ s/\s?//;
123                                 print OUT "$buff\n\n";
124                                 $buff = undef;
125                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
126                                 $buff =~ s/^\s*//;
127                                 $buff =~ s/\s*$//;
128                                 if( $head =~ /\(/ || $head !~ /{/ ) {
129                                         $head =~ s/^\s*//;
130                                         $head =~ s/\/\*.*?\*\///gs;
131                                         formatNote( $head, $buff );
132                                         $head = undef;
133                                         $buff = undef;
134                                 } else {
135                                         $struct = 1;
136                                 }
137                         } elsif( $line =~ /\/\*\*\*/ ) {
138                                 $verbatim = 1;
139                         } elsif( $line =~ /\/\*\*/ ) {
140                                 $active = 1;
141                         }
142                 }
143         }
144         close FILE;
145 }
146
147 my $line;
148 while( defined( $line = <IN> ) ) {
149         chomp $line;
150         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
151                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
152                 process( $fname );
153                 push @deps, $fname;
154         } else {
155                 print OUT "$line\n";
156         }
157 }
158
159 if( defined $depname ) {
160         open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
161         print DEP "$outname:";
162         print DEP " $_" foreach( @deps );
163         print DEP "\n";
164         if( $hasdump ) {
165                 print DEP "$defdump:";
166                 print DEP " $_" foreach( @deps );
167                 print DEP "\n";
168         }
169         close DEP;
170 }
171
172 close IN;
173 close OUT;
174 close DUMP;