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