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 statement
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_var)(params);
46 return( 'var', 1, $1, $_ ) if /^.*?\(\*(\w+)\)\(.*\)/;
47 # type function(name);
48 return( 'fun', 1, $2, $1 ) if /^(.*?(\w+)\([^{]*\)[^{]*)/;
49 # typedef something name;
50 return( 'type', 1, $1, $_ ) if /^\s*typedef.*?(\w+);/;
52 return( 'var', 1, $1, $_ ) if /\s\**(\w+);/;
53 warn( "Unknown statement $_\n" );
54 return( '', 0, $_, $_ );
60 sub formatNote( $$ ) {
61 my( $head, $comment ) = @_;
62 $head =~ s/(\S)[ ]+/$1 /g;
66 my( $type, $semicolon, $name, $oneline ) = detect( $head );
67 # Just few transformations of the result
69 $oneline =~ s/;?$/;/ if( $semicolon );
70 $head =~ s/;?\s*$/;/ if( $semicolon );
71 $head =~ s/(\s|,|\()(\.\.\.)/$1\\$2/g; # Do not convert tripple dot into ellipsis
72 print OUT "[[${type}_$name]]\n";
73 $head = $oneline if $type eq 'fun';#Remove { from inline functions
74 # Remove the generic hack markup
75 $head =~ s/_OPEN_PAREN_/(/g;
76 $head =~ s/_CLOSE_PAREN_/)/g;
77 print OUT "..................\n";
79 print OUT "..................\n\n";
81 $oneline =~ s/_OPEN_PAREN_/(/g;
82 $oneline =~ s/_CLOSE_PAREN_/)/g;
83 my $symname = $type.'_'.$name;
84 $name =~ s/_OPEN_PAREN_/(/g;
85 $name =~ s/_CLOSE_PAREN_/)/g;
86 print DUMP "$outname,$symname,$type,$name,$oneline\n";
89 $comment =~ s/_OPEN_PAREN_/(/g;
90 $comment =~ s/_CLOSE_PAREN_/)/g;
91 $comment =~ s/_GENERIC_LINK_\|([^|]+)\|([^|]+)\|/${1}_OPEN_PAREN_${2}_CLOSE_PAREN_/g;
92 print OUT "$comment\n\n";
96 my( $file, $prefixes ) = @_;
97 open FILE, $file or die "Could nod read $file ($!)\n";
106 while( defined( $line = <FILE> ) ) {
108 # Generic macro hack - replaces the parenthesis so it is valid identifier
109 $line =~ s/$_\(([^()]+)\)/${_}_OPEN_PAREN_${1}_CLOSE_PAREN_/g foreach @{$prefixes};
112 $line =~ s/(\/\*.*?\*\/|\/\/.*)//g;
113 if( $line !~ /\\\s*$/ ) {
114 formatNote( $head, $buff );
116 $buff = $head = undef;
121 $sdepth += ($cp =~ tr/{//);
122 $sdepth -= ($cp =~ tr/}//);
124 formatNote( $head, $buff );
129 } elsif( $verbatim ) {
130 if( $line =~ /\*\// ) {
134 $line =~ s/^\s*\* ?//;
138 if( $line =~ /\*\// ) {
141 $line =~ s/^\s*\* ?//;
145 if( ( $line =~ /\S/ ) && ( defined $buff ) ) {
146 if( $line =~ /^\s*#define.*\\(\s*(\/\/.*|\/\*.*?\*\/|))*/ ) {
149 } elsif( $line =~ /\(/ || $line !~ /{/ || $line =~ /^\s*#define/ ) {
154 formatNote( $_, $buff );
159 $struct = $sdepth = 1;
161 } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) {
163 print OUT "$buff\n\n";
165 } elsif( ( $buff ) = ( $line =~ /^\s*\/\*\*(.*)\*\*\// ) ) {
168 } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) {
171 if( $head =~ /\(/ || $head !~ /{/ || $head =~/}/ ) {
173 $head =~ s/\/\*.*?\*\///gs;
174 formatNote( $head, $buff );
178 $struct = $sdepth = 1;
180 } elsif( $line =~ /\/\*\*\*/ ) {
182 } elsif( $line =~ /\/\*\*/ ) {
191 while( defined( $line = <IN> ) ) {
194 if( my( $fname, $prefixes ) = ( $line =~ /^!!\s*(\S+)(.*)/ ) ) {
195 $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir );
196 process( $fname, [ ( map( {
197 my( $result ) = /^\s*(.*\S)\s*$/;
199 } ( split /,/, $prefixes ) ) ) ] );
206 if( defined $depname ) {
207 open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n";
208 print DEP "$outname:";
209 print DEP " $_" foreach( @deps );
212 print DEP "$defdump:";
213 print DEP " $_" foreach( @deps );