]> mj.ucw.cz Git - libucw.git/blob - build/doc-extract
Doc. system: fix semicolon handling
[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                                         formatNote( $_, $buff );
111                                         $head = undef;
112                                         $buff = undef;
113                                 } else {
114                                         $head = $line;
115                                         $struct = 1;
116                                 }
117                         } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
118                                 $buff =~ s/\s?//;
119                                 print OUT "$buff\n\n";
120                                 $buff = undef;
121                         } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
122                                 $buff =~ s/^\s*//;
123                                 $buff =~ s/\s*$//;
124                                 if( $head =~ /\(/ || $head !~ /{/ ) {
125                                         $head =~ s/^\s*//;
126                                         $head =~ s/\/\*.*?\*\///gs;
127                                         formatNote( $head, $buff );
128                                         $head = undef;
129                                         $buff = undef;
130                                 } else {
131                                         $struct = 1;
132                                 }
133                         } elsif( $line =~ /\/\*\*\*/ ) {
134                                 $verbatim = 1;
135                         } elsif( $line =~ /\/\*\*/ ) {
136                                 $active = 1;
137                         }
138                 }
139         }
140         close FILE;
141 }
142
143 my $line;
144 while( defined( $line = <IN> ) ) {
145         chomp $line;
146         if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
147                 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
148                 process( $fname );
149                 push @deps, $fname;
150         } else {
151                 print OUT "$line\n";
152         }
153 }
154
155 if( defined $depname ) {
156         open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
157         print DEP "$outname:";
158         print DEP " $_" foreach( @deps );
159         print DEP "\n";
160         if( $hasdump ) {
161                 print DEP "$defdump:";
162                 print DEP " $_" foreach( @deps );
163                 print DEP "\n";
164         }
165         close DEP;
166 }
167
168 close IN;
169 close OUT;
170 close DUMP;