#!/usr/bin/perl # Script for extracting documentation out of header files # (c) 2008 Michal Vaner use strict; use warnings; my( $inname, $outname, $depname, $basedir, $defdump ) = @ARGV; if( defined $inname ) { open IN, $inname or die "Could not read $inname ($!)\n"; } else { open IN, "<&STDIN" or die "Could not read stdin ($!)\n"; } if( defined $outname ) { open OUT, ">$outname" or die "Could not write $outname ($!)\n"; } else { open OUT, ">&STDOUT" or die "Could not write to stdout ($!)\n"; } my $hasdump; if( defined $defdump ) { open DUMP, ">$defdump" or die "Could not write definition dump $defdump ($!)\n"; $hasdump = 1; } sub detect( $ ) { ( $_ ) = @_; return( 'struct', 1, $1, "typedef struct { ... } $1;" ) if /^\s*typedef\s+struct\s*{.*}\s*(\w+)\s*;\s*$/s; return( 'enum', 1, $1, "typedef enum { ... } $1;" ) if /^\s*typedef\s+enum\s*{.*}\s*(\w+)\s*;\s*$/s; my $l = length; s/\n.*//s; return( 'struct', 0, $1, $_ ) if /struct\s+(\w+)\s+{/; return( 'enum', 0, $1, $_ ) if /enum\s+(\w+)\s+{/; if( $l > length ) { warn( "Unknown statement $_\n" ); return( '', 0, $_, $_ ); } return( 'def', 0, $1, $_ ) if /#define\s+(\w+)/; return( 'fun', 1, $2, $1 ) if /^(.*?(\w+)\([^{]*\)[^{]*)/; return( 'type', 1, $1, $_ ) if /^\s*typedef.*?(\w+);/; return( 'var', 1, $1, $_ ) if /\s(\w+);/; warn( "Unknown statement $_\n" ); return( '', 0, $_, $_ ); } my @deps; my $id = 0; sub formatNote( $$ ) { my( $head, $comment ) = @_; $head =~ s/(\S)[ ]+/$1 /g; print OUT "\n"; print OUT "''''\n"; chomp $head; my( $type, $semicolon, $name, $oneline ) = detect( $head ); $oneline =~ s/\s+$//; $oneline =~ s/;?$/;/ if( $semicolon ); $head =~ s/;?\s*$/;/ if( $semicolon ); $head =~ s/(\.\.\.)/\\$1/g; print OUT "[[${type}_$name]]\n"; $head = $oneline if $type eq 'fun';#Remove { from inline functions print OUT "..................\n"; print OUT "$head\n"; print OUT "..................\n\n"; if( $hasdump ) { print DUMP "$outname,${type}_$name,$type,$name,$oneline\n"; $id ++; } print OUT "$comment\n\n"; } sub process( $ ) { my $file = shift; open FILE, $file or die "Could nod read $file ($!)\n"; my $line; my $active; my $verbatim; my $buff; my $head; my $struct; while( defined( $line = ) ) { chomp $line; if( $struct ) { $head .= "\n".$line; if( $line =~ /}/ ) { formatNote( $head, $buff ); $struct = 0; $buff = undef; $head = undef; } } elsif( $verbatim ) { if( $line =~ /\*\// ) { $verbatim = 0; print OUT "\n"; } else { $line =~ s/^\s*\* ?//; print OUT "$line\n"; } } elsif( $active ) { if( $line =~ /\*\// ) { $active = 0; } else { $line =~ s/^\s*\* ?//; $buff .= "$line\n"; } } else { if( ( $line =~ /\S/ ) && ( defined $buff ) ) { if( $line =~ /\(/ || $line !~ /{/ ) { $_ = $line; s/^\s*\s?//; s/\/\/.*//; s/\/\*.*?\*\///gs; formatNote( $_, $buff ); $head = undef; $buff = undef; } else { $head = $line; $struct = 1; } } elsif( ( $buff ) = ( $line =~ /\/\*\*\*(.*)\*\*\*\// ) ) { $buff =~ s/\s?//; print OUT "$buff\n\n"; $buff = undef; } elsif( ( $head, $buff ) = ( $line =~ /^(.*)\/\*\*(.*)\*\*\// ) ) { $buff =~ s/^\s*//; $buff =~ s/\s*$//; if( $head =~ /\(/ || $head !~ /{/ ) { $head =~ s/^\s*//; $head =~ s/\/\*.*?\*\///gs; formatNote( $head, $buff ); $head = undef; $buff = undef; } else { $struct = 1; } } elsif( $line =~ /\/\*\*\*/ ) { $verbatim = 1; } elsif( $line =~ /\/\*\*/ ) { $active = 1; } } } close FILE; } my $line; while( defined( $line = ) ) { chomp $line; if( my( $fname ) = ( $line =~ /^!!\s*(.*\S)/ ) ) { $fname = "$basedir/$fname" if( ( $fname !~ /^\// ) && defined $basedir ); process( $fname ); push @deps, $fname; } else { print OUT "$line\n"; } } if( defined $depname ) { open DEP, ">>$depname" or die "Could not write dep file $depname ($!)\n"; print DEP "$outname:"; print DEP " $_" foreach( @deps ); print DEP "\n"; if( $hasdump ) { print DEP "$defdump:"; print DEP " $_" foreach( @deps ); print DEP "\n"; } close DEP; } close IN; close OUT; close DUMP;