]> mj.ucw.cz Git - libucw.git/blob - build/doc-extract
Merge branch 'master' into dev-lib
[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( $1, 1, $2, "typedef $1 { ... } $2;" ) if /^\s*typedef\s+(struct|enum)\s*{.*}\s*(\w+)\s*;\s*$/s;
28         return( $1, 1, $2, $_ ) if /^\s*(struct|enum)\s+(\w+)\s*;\s*$/s;
29         my $l = length;
30         s/\n.*//s;
31         return( $1, 0, $2, $_ ) if /(struct|enum)\s+(\w+)\s+{/;
32         return( 'def', 0, $1, $_ ) if /#define\s+(\w+)/;
33         if( $l > length ) {
34                 warn( "Unknown multiline statement $_\n" );
35                 return( '', 0, $_, $_ );
36         }
37         return( 'type', 1, $2, $_ ) if /^\s*typedef[^()]+?(\(\s*?\*\s*?)?(\w+)(\s*\))?\s*\(.*\)/;
38         return( 'fun', 1, $2, $1 ) if /^(.*?(\w+)\([^{]*\)[^{]*)/;
39         return( 'type', 1, $1, $_ ) if /^\s*typedef.*?(\w+);/;
40         return( 'var', 1, $1, $_ ) if /\s\**(\w+);/;
41         warn( "Unknown statement $_\n" );
42         return( '', 0, $_, $_ );
43 }
44
45 my @deps;
46 my $id = 0;
47
48 sub formatNote( $$ ) {
49         my( $head, $comment ) = @_;
50         $head =~ s/(\S)[ ]+/$1 /g;
51         print OUT "\n";
52         print OUT "''''\n";
53         chomp $head;
54         my( $type, $semicolon, $name, $oneline ) = detect( $head );
55         # Just few transformations of the result
56         $oneline =~ s/\s+$//;
57         $oneline =~ s/;?$/;/ if( $semicolon );
58         $head =~ s/;?\s*$/;/ if( $semicolon );
59         $head =~ s/(\.\.\.)/\\$1/g;
60         print OUT "[[${type}_$name]]\n";
61         $head = $oneline if $type eq 'fun';#Remove { from inline functions
62         # Remove the generic hack markup
63         $head =~ s/_OPEN_PAREN_/(/g;
64         $head =~ s/_CLOSE_PAREN_/)/g;
65         print OUT "..................\n";
66         print OUT "$head\n";
67         print OUT "..................\n\n";
68         if( $hasdump ) {
69                 $oneline =~ s/_OPEN_PAREN_/(/g;
70                 $oneline =~ s/_CLOSE_PAREN_/)/g;
71                 my $symname = $type.'_'.$name;
72                 $name =~ s/_OPEN_PAREN_/(/g;
73                 $name =~ s/_CLOSE_PAREN_/)/g;
74                 print DUMP "$outname,$symname,$type,$name,$oneline\n";
75                 $id ++;
76         }
77         $comment =~ s/_OPEN_PAREN_/(/g;
78         $comment =~ s/_CLOSE_PAREN_/)/g;
79         $comment =~ s/_GENERIC_LINK_\|([^|]+)\|([^|]+)\|/${1}_OPEN_PAREN_${2}_CLOSE_PAREN_/g;
80         print OUT "$comment\n\n";
81 }
82
83 sub process( $$ ) {
84         my( $file, $prefixes ) = @_;
85         open FILE, $file or die "Could nod read $file ($!)\n";
86         my $line;
87         my $active;
88         my $verbatim;
89         my $buff;
90         my $head;
91         my $struct;
92         my $def;
93         my $sdepth;
94         while( defined( $line = <FILE> ) ) {
95                 chomp $line;
96                 # Generic macro hack - replaces the parenthesis so it is valid identifier
97                 $line =~ s/$_\(([^()]+)\)/${_}_OPEN_PAREN_${1}_CLOSE_PAREN_/g foreach @{$prefixes};
98                 if( $def ) {
99                         $head .= "\n".$line;
100                         $line =~ s/(\/\*.*?\*\/|\/\/.*)//g;
101                         if( $line !~ /\\\s*$/ ) {
102                                 formatNote( $head, $buff );
103                                 $def = 0;
104                                 $buff = $head = undef;
105                         }
106                 } elsif( $struct ) {
107                         $head .= "\n".$line;
108                         my $cp = $line;
109                         $sdepth += ($cp =~ tr/{//);
110                         $sdepth -= ($cp =~ tr/}//);
111                         if( !$sdepth ) {
112                                 formatNote( $head, $buff );
113                                 $struct = 0;
114                                 $buff = undef;
115                                 $head = undef;
116                         }
117                 } elsif( $verbatim ) {
118                         if( $line =~ /\*\// ) {
119                                 $verbatim = 0;
120                                 print OUT "\n";
121                         } else {
122                                 $line =~ s/^\s*\* ?//;
123                                 print OUT "$line\n";
124                         }
125                 } elsif( $active ) {
126                         if( $line =~ /\*\// ) {
127                                 $active = 0;
128                         } else {
129                                 $line =~ s/^\s*\* ?//;
130                                 $buff .= "$line\n";
131                         }
132                 } else {
133                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
134                                 if( $line =~ /^\s*#define.*\\(\s*(\/\/.*|\/\*.*?\*\/|))*/ ) {
135                                         $head = $line;
136                                         $def = 1;
137                                 } elsif( $line =~ /\(/ || $line !~ /{/ || $line =~ /^\s*#define/ ) {
138                                         $_ = $line;
139                                         s/^\s*\s?//;
140                                         s/\/\/.*//;
141                                         s/\/\*.*?\*\///gs;
142                                         formatNote( $_, $buff );
143                                         $head = undef;
144                                         $buff = undef;
145                                 } else {
146                                         $head = $line;
147                                         $struct = $sdepth = 1;
148                                 }
149                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
150                                 $buff =~ s/\s?//;
151                                 print OUT "$buff\n\n";
152                                 $buff = undef;
153                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
154                                 $buff =~ s/^\s*//;
155                                 $buff =~ s/\s*$//;
156                                 if( $head =~ /\(/ || $head !~ /{/ || $head =~/}/ ) {
157                                         $head =~ s/^\s*//;
158                                         $head =~ s/\/\*.*?\*\///gs;
159                                         formatNote( $head, $buff );
160                                         $head = undef;
161                                         $buff = undef;
162                                 } else {
163                                         $struct = $sdepth = 1;
164                                 }
165                         } elsif( $line =~ /\/\*\*\*/ ) {
166                                 $verbatim = 1;
167                         } elsif( $line =~ /\/\*\*/ ) {
168                                 $active = 1;
169                         }
170                 }
171         }
172         close FILE;
173 }
174
175 my $line;
176 while( defined( $line = <IN> ) ) {
177         chomp $line;
178         my $prefixes;
179         if( my( $fname, $prefixes ) = ( $line =~ /^!!\s*(\S+)(.*)/ ) ) {
180                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
181                 process( $fname, [ ( map( {
182                         my( $result ) = /^\s*(.*\S)\s*$/;
183                         $result;
184                 } ( split /,/, $prefixes ) ) ) ] );
185                 push @deps, $fname;
186         } else {
187                 print OUT "$line\n";
188         }
189 }
190
191 if( defined $depname ) {
192         open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
193         print DEP "$outname:";
194         print DEP " $_" foreach( @deps );
195         print DEP "\n";
196         if( $hasdump ) {
197                 print DEP "$defdump:";
198                 print DEP " $_" foreach( @deps );
199                 print DEP "\n";
200         }
201         close DEP;
202 }
203
204 close IN;
205 close OUT;
206 close DUMP;