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( $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;
31 return( $1, 0, $2, $_ ) if /(struct|enum)\s+(\w+)\s+{/;
32 return( 'def', 0, $1, $_ ) if /#define\s+(\w+)/;
34 warn( "Unknown multiline statement $_\n" );
35 return( '', 0, $_, $_ );
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, $_, $_ );
48 sub formatNote( $$ ) {
49 my( $head, $comment ) = @_;
50 $head =~ s/(\S)[ ]+/$1 /g;
54 my( $type, $semicolon, $name, $oneline ) = detect( $head );
55 # Just few transformations of the result
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";
67 print OUT "..................\n\n";
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";
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";
84 my( $file, $prefixes ) = @_;
85 open FILE, $file or die "Could nod read $file ($!)\n";
94 while( defined( $line = <FILE> ) ) {
96 # Generic macro hack - replaces the parenthesis so it is valid identifier
97 $line =~ s/$_\(([^()]+)\)/${_}_OPEN_PAREN_${1}_CLOSE_PAREN_/g foreach @{$prefixes};
100 $line =~ s/(\/\*.*?\*\/|\/\/.*)//g;
101 if( $line !~ /\\\s*$/ ) {
102 formatNote( $head, $buff );
104 $buff = $head = undef;
109 $sdepth += ($cp =~ tr/{//);
110 $sdepth -= ($cp =~ tr/}//);
112 formatNote( $head, $buff );
117 } elsif( $verbatim ) {
118 if( $line =~ /\*\// ) {
122 $line =~ s/^\s*\* ?//;
126 if( $line =~ /\*\// ) {
129 $line =~ s/^\s*\* ?//;
133 if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
134 if( $line =~ /^\s*#define.*\\(\s*(\/\/.*|\/\*.*?\*\/|))*/ ) {
137 } elsif( $line =~ /\(/ || $line !~ /{/ || $line =~ /^\s*#define/ ) {
142 formatNote( $_, $buff );
147 $struct = $sdepth = 1;
149 } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
151 print OUT "$buff\n\n";
153 } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
156 if( $head =~ /\(/ || $head !~ /{/ || $head =~/}/ ) {
158 $head =~ s/\/\*.*?\*\///gs;
159 formatNote( $head, $buff );
163 $struct = $sdepth = 1;
165 } elsif( $line =~ /\/\*\*\*/ ) {
167 } elsif( $line =~ /\/\*\*/ ) {
176 while( defined( $line = <IN> ) ) {
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*$/;
184 } ( split /,/, $prefixes ) ) ) ] );
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 );
197 print DEP "$defdump:";
198 print DEP " $_" foreach( @deps );