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";
25 # Function to guess type of stytement
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;
37 return( $1, 0, $2, $_ ) if /(struct|enum)\s+(\w+)\s+{/;
38 return( 'def', 0, $1, $_ ) if /#define\s+(\w+)/;
40 warn( "Unknown multiline statement $_\n" );
41 return( '', 0, $_, $_ );
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+);/;
50 return( 'var', 1, $1, $_ ) if /\s\**(\w+);/;
51 warn( "Unknown statement $_\n" );
52 return( '', 0, $_, $_ );
58 sub formatNote( $$ ) {
59 my( $head, $comment ) = @_;
60 $head =~ s/(\S)[ ]+/$1 /g;
64 my( $type, $semicolon, $name, $oneline ) = detect( $head );
65 # Just few transformations of the result
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";
77 print OUT "..................\n\n";
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";
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";
94 my( $file, $prefixes ) = @_;
95 open FILE, $file or die "Could nod read $file ($!)\n";
104 while( defined( $line = <FILE> ) ) {
106 # Generic macro hack - replaces the parenthesis so it is valid identifier
107 $line =~ s/$_\(([^()]+)\)/${_}_OPEN_PAREN_${1}_CLOSE_PAREN_/g foreach @{$prefixes};
110 $line =~ s/(\/\*.*?\*\/|\/\/.*)//g;
111 if( $line !~ /\\\s*$/ ) {
112 formatNote( $head, $buff );
114 $buff = $head = undef;
119 $sdepth += ($cp =~ tr/{//);
120 $sdepth -= ($cp =~ tr/}//);
122 formatNote( $head, $buff );
127 } elsif( $verbatim ) {
128 if( $line =~ /\*\// ) {
132 $line =~ s/^\s*\* ?//;
136 if( $line =~ /\*\// ) {
139 $line =~ s/^\s*\* ?//;
143 if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
144 if( $line =~ /^\s*#define.*\\(\s*(\/\/.*|\/\*.*?\*\/|))*/ ) {
147 } elsif( $line =~ /\(/ || $line !~ /{/ || $line =~ /^\s*#define/ ) {
152 formatNote( $_, $buff );
157 $struct = $sdepth = 1;
159 } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
161 print OUT "$buff\n\n";
163 } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
166 if( $head =~ /\(/ || $head !~ /{/ || $head =~/}/ ) {
168 $head =~ s/\/\*.*?\*\///gs;
169 formatNote( $head, $buff );
173 $struct = $sdepth = 1;
175 } elsif( $line =~ /\/\*\*\*/ ) {
177 } elsif( $line =~ /\/\*\*/ ) {
186 while( defined( $line = <IN> ) ) {
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*$/;
194 } ( split /,/, $prefixes ) ) ) ] );
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 );
207 print DEP "$defdump:";
208 print DEP " $_" foreach( @deps );