]> mj.ucw.cz Git - libucw.git/blob - build/doc-extract
5373a5a2898eba9e8d8966ec0a55a134a7589d92
[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         my $l = length;
28         s/\n.*//s;
29         return( 'struct', 0, $1 ) if( /struct\s+(\w+)\s+{/ );
30         return( 'enum', 0, $1 ) if( /enum\s+(\w+)\s+{/ );
31         if( $l > length ) {
32                 warn( "Unknown statemen $_\n" );
33                 return( '', 0, $_ );
34         }
35         return( 'define', 0, $1 ) if( /#define\s+(\w+)/ );
36         return( 'function', 1, $1 ) if( /(\w+)\(.*\)/ );
37         return( 'variable', 1, $1 ) if( /\s(\w+);/ );
38         warn( "Unknown statemen $_\n" );
39         return( '', 0, $_ );
40 }
41
42 my @deps;
43 my $id = 0;
44
45 sub formatNote( $$ ) {
46         my( $head, $comment ) = @_;
47         $head =~ s/(\S)[ ]+/$1 /g;
48         print OUT "\n";
49         print OUT "''''\n";
50         chomp $head;
51         print OUT "[[auto_$id]]\n";
52         my( $type, $semicolon, $name ) = detect( $head );
53         $head =~ s/;?\s+$/;/ if( $semicolon );
54         if( $type eq 'function' ) {
55                 print OUT "!!f!$head!!!\n\n";
56         } else {
57                 print OUT "..................\n";
58                 print OUT "$head\n";
59                 print OUT "..................\n\n";
60         }
61         if( $hasdump ) {
62                 $head =~ s/\n.*//s;
63                 print DUMP "$outname,$id,$head\n";
64                 $id ++;
65         }
66         print OUT "$comment\n\n";
67 }
68
69 sub process( $ ) {
70         my $file = shift;
71         open FILE, $file or die "Could nod read $file ($!)\n";
72         my $line;
73         my $active;
74         my $verbatim;
75         my $buff;
76         my $head;
77         my $struct;
78         while( defined( $line = <FILE> ) ) {
79                 chomp $line;
80                 if( $struct ) {
81                         $head .= "\n".$line;
82                         if( $line =~ /}/ ) {
83                                 formatNote( $head, $buff );
84                                 $struct = 0;
85                                 $buff = undef;
86                                 $head = undef;
87                         }
88                 } elsif( $verbatim ) {
89                         if( $line =~ /\*\// ) {
90                                 $verbatim = 0;
91                                 print OUT "\n";
92                         } else {
93                                 $line =~ s/^\s*\* ?//;
94                                 print OUT "$line\n";
95                         }
96                 } elsif( $active ) {
97                         if( $line =~ /\*\// ) {
98                                 $active = 0;
99                         } else {
100                                 $line =~ s/^\s*\* ?//;
101                                 $buff .= "$line\n";
102                         }
103                 } else {
104                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
105                                 if( $line =~ /\(/ || $line !~ /{/ ) {
106                                         $_ = $line;
107                                         s/^\s*\s?//;
108                                         s/\/\/.*//;
109                                         s/\/\*.*?\*\///gs;
110                                         s/([;{]).*/";"x length( $1 )/e;
111                                         s/\)\s*;$/);/;
112                                         formatNote( $_, $buff );
113                                         $head = undef;
114                                         $buff = undef;
115                                 } else {
116                                         $head = $line;
117                                         $struct = 1;
118                                 }
119                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
120                                 $buff =~ s/\s?//;
121                                 print OUT "$buff\n\n";
122                                 $buff = undef;
123                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
124                                 $buff =~ s/^\s*//;
125                                 $buff =~ s/\s*$//;
126                                 if( $head =~ /\(/ || $head !~ /{/ ) {
127                                         $head =~ s/^\s*//;
128                                         $head =~ s/\/\*.*?\*\///gs;
129                                         $head =~ s/([;{]).*/";"x length( $1 )/e;
130                                         $head =~ s/\)\s*;$/);/;
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;