aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/doc/libgfsm/doxy-filter.perl
blob: 632c54d5734a7253c4de4cde625a5b694b0a80d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl -w

#----------------------------------------------------------------------
# File: doxy-filter.perl
# Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
# Description: doxygen filter for use with the GNU C preprocessor
#
# Usage: $0 INPUT_FILENAME
#-----------------------------------------------------------------------

use Getopt::Long;
use File::Basename qw(basename fileparse);
use vars qw($filepath $filesuff $pppath);

$PP_ONLY = 0;
$DO_CPP = 1;
$logfile = undef;
#$logfile = 'doxy-filter.log';
@includes = qw();
$cmdline = join(' ', $0, @ARGV);
GetOptions(
	   "only-preprocess|o!"=>\$PP_ONLY,
	   "preprocess|do-cpp|p"=>\$DO_CPP,
	   "no-preprocess|no-cpp|np|n"=>sub { $DO_CPP = 0; },
	   "logfile|l=s"=>\$logfile,
	   "I=s"=>\@includes,
	   'help|h'=>\$help,
	  );

if ($help) {
  print STDERR "Usage: $0 [-(only|no|)-preprocess|-logfile FILE|-IDIR] FILE(s)...\n";
  exit 0;
}

$config_cppflags = join(' ', map { "-I$_" } @includes);
do "doxy-filter.cfg";

#-----------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------
$CPP = $ENV{CPP} || 'cpp';
$CPP = $config_cpp if (defined($config_cpp));
$CPPFLAGS = (''
	     #.' -C '         ## -- preserve comments
	     #.' -x c++'      ## -- parse c++ code
	     .' '.($ENV{CPPFLAGS} || '')
	     .' '.$config_cppflags
	    );

$ppextra = '(?s:\bbison\.h\b)|(?s:\bflexskel\.h\b)';
%ppextra =
  ('(?:Lexer)' => '(?s:\bflexskel\.h\b)',
   '(?:Parser)' => '(?s:\bbison\.h\b)',
  );
@suffixes = qw(\.c \.h \.cc \.cpp \.cxx \.l \.y \.ll \.yy);

##-- temp
$TMPDIR = $ENV{TMP} || '/tmp';
$TMPDIR = $config_tmpdir if (defined($config_tmpdir));

#-----------------------------------------------------------------------
# DEBUG
#-----------------------------------------------------------------------
#$DEBUG = 2;
#$DEBUG = 1;
$DEBUG=0;
sub logopen {
  $logfile = 'doxy-filter.log' if (!defined($logfile));
  if ($logfile ne '-') {
    open(LOG,">>$logfile") or die("$0: open failed for log-file '$logfile': $!");
    open(STDERR,">&LOG") or die("$0: could not redirect STDERR to LOG: $!");
  } else {
    open(LOG, ">&STDERR") or die ("$0: could not redirect LOG to STDERR: $!");
  }
  print LOG
    ("\n", ("-" x 72), "\n", `date`,
     "> ARGV=$cmdline\n",
     "> PWD=", `pwd`,
     "> PP_ONLY=", $PP_ONLY ? 1 : 0, "\n",
     "> DO_CPP=", $DO_CPP ? 1 : 0, "\n",
     "> CPPFLAGS=$CPPFLAGS\n",
    );
}

#-----------------------------------------------------------------------
# MAIN
#-----------------------------------------------------------------------
push(@ARGV,'-') if (!@ARGV);
logopen() if ($DEBUG>0);
foreach $file (@ARGV) {
  print LOG ("> CPPCMD=$CPP $CPPFLAGS $file |\n") if ($DEBUG>0);

  if ($DO_CPP) {
    open(CPP,"$CPP $CPPFLAGS $file|")
      or die("$0: could not open pipe from '$CPP $CPPFLAGS $file': $!");
  } else {
    open(CPP, "${file}.pp")
      or die("$0: could not open '${file}.pp': $!");
  }


  ($filebase,$filepath,$filesuff) = fileparse($file, @suffixes);
  $ppfile = undef;
  while (defined($line = <CPP>)) {
    if (!$PP_ONLY) {
      if ($line =~ /^\#\s*\d+\s*\"([^\"]*)\"/) {
	## -- data from a different file
	$ppfile = $1;
	($ppbase,$pppath,$ppsuff) = fileparse($ppfile, @suffixes);

	if ($DEBUG>2) {
	  print LOG ("> filebase='$filebase', ppbase='$ppbase', ppsuff='$ppsuff'\n");
	}

	if ($ppfile eq $file) {
	  $wantline = 1;
	  print LOG ("> ACCEPT: literal filename match: $line") if ($DEBUG>1);
	}
	elsif (grep { $file =~ $_ && $ppfile =~ $ppextra{$_} } keys(%ppextra)) {
	  $wantline = 1;
	  print LOG ("> ACCEPT: filename convention match: $line\n") if ($DEBUG>1);
	}
	elsif (grep { $ppbase eq $filebase && $ppsuff =~ /$_$/ } @suffixes) {
	  $wantline = 1;
	  print LOG ("> ACCEPT: suffix match: $line\n") if ($DEBUG>1);
	}
	else {
	  $wantline = 0;
	  print LOG ("> REJECT: $line") if ($DEBUG>1);
	}

	next;
      }
      next if (!defined($ppfile) || !$wantline);
    }
    print $line;
  }

  close(CPP);
}

close(LOG) if ($DEBUG>0);

# useless comment
# again