]> mj.ucw.cz Git - libucw.git/blob - build/doc-extract
Doc. system: fix ... in function parameters
[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( 'define', 0, $1, $_ ) if( /#define\s+(\w+)/ );
38         return( 'function', 1, $1, $_ ) if( /(\w+)\(.*\)/ );
39         return( 'variable', 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         print OUT "[[auto_$id]]\n";
54         my( $type, $semicolon, $name, $oneline ) = detect( $head );
55         $oneline =~ s/\s+$//;
56         $head =~ s/;?\s*$/;/ if( $semicolon );
57         $head =~ s/(\.\.\.)/\\$1/g;
58         if( $type eq 'function' ) {
59                 print OUT "!!f!$head!!!\n\n";
60         } else {
61                 print OUT "..................\n";
62                 print OUT "$head\n";
63                 print OUT "..................\n\n";
64         }
65         if( $hasdump ) {
66                 print DUMP "$outname,$id,$type,$name,$oneline\n";
67                 $id ++;
68         }
69         print OUT "$comment\n\n";
70 }
71
72 sub process( $ ) {
73         my $file = shift;
74         open FILE, $file or die "Could nod read $file ($!)\n";
75         my $line;
76         my $active;
77         my $verbatim;
78         my $buff;
79         my $head;
80         my $struct;
81         while( defined( $line = <FILE> ) ) {
82                 chomp $line;
83                 if( $struct ) {
84                         $head .= "\n".$line;
85                         if( $line =~ /}/ ) {
86                                 formatNote( $head, $buff );
87                                 $struct = 0;
88                                 $buff = undef;
89                                 $head = undef;
90                         }
91                 } elsif( $verbatim ) {
92                         if( $line =~ /\*\// ) {
93                                 $verbatim = 0;
94                                 print OUT "\n";
95                         } else {
96                                 $line =~ s/^\s*\* ?//;
97                                 print OUT "$line\n";
98                         }
99                 } elsif( $active ) {
100                         if( $line =~ /\*\// ) {
101                                 $active = 0;
102                         } else {
103                                 $line =~ s/^\s*\* ?//;
104                                 $buff .= "$line\n";
105                         }
106                 } else {
107                         if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
108                                 if( $line =~ /\(/ || $line !~ /{/ ) {
109                                         $_ = $line;
110                                         s/^\s*\s?//;
111                                         s/\/\/.*//;
112                                         s/\/\*.*?\*\///gs;
113                                         formatNote( $_, $buff );
114                                         $head = undef;
115                                         $buff = undef;
116                                 } else {
117                                         $head = $line;
118                                         $struct = 1;
119                                 }
120                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
121                                 $buff =~ s/\s?//;
122                                 print OUT "$buff\n\n";
123                                 $buff = undef;
124                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
125                                 $buff =~ s/^\s*//;
126                                 $buff =~ s/\s*$//;
127                                 if( $head =~ /\(/ || $head !~ /{/ ) {
128                                         $head =~ s/^\s*//;
129                                         $head =~ s/\/\*.*?\*\///gs;
130                                         formatNote( $head, $buff );
131                                         $head = undef;
132                                         $buff = undef;
133                                 } else {
134                                         $struct = 1;
135                                 }
136                         } elsif( $line =~ /\/\*\*\*/ ) {
137                                 $verbatim = 1;
138                         } elsif( $line =~ /\/\*\*/ ) {
139                                 $active = 1;
140                         }
141                 }
142         }
143         close FILE;
144 }
145
146 my $line;
147 while( defined( $line = <IN> ) ) {
148         chomp $line;
149         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
150                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
151                 process( $fname );
152                 push @deps, $fname;
153         } else {
154                 print OUT "$line\n";
155         }
156 }
157
158 if( defined $depname ) {
159         open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
160         print DEP "$outname:";
161         print DEP " $_" foreach( @deps );
162         print DEP "\n";
163         if( $hasdump ) {
164                 print DEP "$defdump:";
165                 print DEP " $_" foreach( @deps );
166                 print DEP "\n";
167         }
168         close DEP;
169 }
170
171 close IN;
172 close OUT;
173 close DUMP;