#!/usr/local/bin/perl -w
use strict;
use warnings;
use Getopt::Std;
my $RCSID = '$Id: include.perl,v 1.2 2007-01-31 14:42:16-08 - - $';
$0 =~ s|/*$||; $0 =~ s|^.*/||;
my $EXITCODE = 0;
END { exit $EXITCODE; }
sub note(@){ print STDERR "$0: @_"; };
$SIG{'__WARN__'} = sub { note @_; $EXITCODE = 1; };
$SIG{'__DIE__'} = sub { warn @_; exit; };
my $MAN_PAGE = <<__END_MAN_PAGE__;
#
# NAME
# $0 - search C source for #include statements
#
# SYNOPSIS
# $0 -h | csourcefile...
#
# DESCRIPTION
# Takes a sequence of C source files as arguments and prints
# each line number and reference to an include file.
#
# OPTIONS
# -h Prints out help information in man page format.
#
# $RCSID
__END_MAN_PAGE__
$MAN_PAGE =~ s/^#[ ]?//mg;
use Getopt::Std;
my %dependencies;
my %foundfile;
my @worklist;
my %OPTIONS;
push@worklist , [undef, $ARGV[0]];
getopts "mhsu", \%OPTIONS;
print $MAN_PAGE and exit if $OPTIONS{'h'};
die "Usage: $0 -h | csourcefile...\n" unless @ARGV;
for my $filename (@ARGV) {
while (@worklist) {
my $pair = shift @worklist;
my $target = $pair->[0];
my $source = $pair->[1];
print ("test entire @worklist\n");
unless (open FILE, $filename) {
warn "$filename: $!\n";
}else {
my $target = $filename;
$target =~ s|\.c$|\.o|;
$dependencies {"$target: $filename"} = 1;
$foundfile{$filename} = 1;
while (defined (my $line = <FILE>)) {
next unless $line =~ m/^\s*#include\s+(<.*?>|".*?")\s*$/;
my $include = $1;
$include =~ s|"(.*)"|$1|;
$include =~ s|<(.*)>|/usr/include/$1|;
$dependencies {"$target: $include"} = 1;
print "$filename: $.: $include\n";
push@worklist, [$target, $include] unless $foundfile{$include};
$foundfile{$include} = 1;
};
};
close FILE;
}
}