2 # Script for extracting documentation out of header files
3 # (c) 2008 Michal Vaner <vorner@ucw.cz>
8 my( $inname, $outname, $depname, $basedir, $defdump ) = @ARGV;
9 if( defined $inname ) {
10 open IN, $inname or die "Could not read $inname ($!)\n";
12 open IN, "<&STDIN" or die "Could not read stdin ($!)\n";
14 if( defined $outname ) {
15 open OUT, ">$outname" or die "Could not write $outname ($!)\n";
17 open OUT, ">&STDOUT" or die "Could not write to stdout ($!)\n";
20 if( defined $defdump ) {
21 open DUMP, ">$defdump" or die "Could not write definition dump $defdump ($!)\n";
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;
31 return( 'struct', 0, $1, $_ ) if /struct\s+(\w+)\s+{/;
32 return( 'enum', 0, $1, $_ ) if /enum\s+(\w+)\s+{/;
34 warn( "Unknown statement $_\n" );
35 return( '', 0, $_, $_ );
37 return( 'def', 0, $1, $_ ) if /#define\s+(\w+)/;
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, $_, $_ );
48 sub formatNote( $$ ) {
49 my( $head, $comment ) = @_;
50 $head =~ s/(\S)[ ]+/$1 /g;
54 my( $type, $semicolon, $name, $oneline ) = detect( $head );
56 $oneline =~ s/;?$/;/ if( $semicolon );
57 $head =~ s/;?\s*$/;/ if( $semicolon );
58 $head =~ s/(\.\.\.)/\\$1/g;
59 print OUT "[[${type}_$name]]\n";
60 $head = $oneline if $type eq 'fun';#Remove { from inline functions
61 print OUT "..................\n";
63 print OUT "..................\n\n";
65 print DUMP "$outname,${type}_$name,$type,$name,$oneline\n";
68 print OUT "$comment\n\n";
73 open FILE, $file or die "Could nod read $file ($!)\n";
80 while( defined( $line = <FILE> ) ) {
85 formatNote( $head, $buff );
90 } elsif( $verbatim ) {
91 if( $line =~ /\*\// ) {
95 $line =~ s/^\s*\* ?//;
99 if( $line =~ /\*\// ) {
102 $line =~ s/^\s*\* ?//;
106 if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
107 if( $line =~ /\(/ || $line !~ /{/ ) {
112 formatNote( $_, $buff );
119 } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
121 print OUT "$buff\n\n";
123 } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
126 if( $head =~ /\(/ || $head !~ /{/ ) {
128 $head =~ s/\/\*.*?\*\///gs;
129 formatNote( $head, $buff );
135 } elsif( $line =~ /\/\*\*\*/ ) {
137 } elsif( $line =~ /\/\*\*/ ) {
146 while( defined( $line = <IN> ) ) {
148 if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) {
149 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
157 if( defined $depname ) {
158 open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
159 print DEP "$outname:";
160 print DEP " $_" foreach( @deps );
163 print DEP "$defdump:";
164 print DEP " $_" foreach( @deps );