#!/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;
my @worklist;
my $target;
my $source;
my $dep;
my %OPTIONS;
my %foundfile;
my %dependencies;
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) {
unless (open FILE, $filename) {
warn "$filename: $!\n";
}else {
my $target = $filename;
$target = ~s/\.c$/\.o/;
printf "$target: $filename\n";
push @{$foundfile{$filename}}, 0;
while (defined (my $line = <FILE>)) {
next unless $line =~ m/^\s*#include\s+(<.*?>|".*?")\s*$/;
my $include = $1;
# this is a user include, set flag
$include =~ s|"(.*)"|$1|;
# this is a sys include, set flag
$include =~ s|<(.*)>|/usr/include/$1|;
if ($foundfile{$filename}) {
$dependencies {"$target:$include"} = 1;
}
};
};
close FILE;
}
}
#
#my @targets = qw (target1 target2 target3 target4);
#my @sources = qw (source1 source2 source3 source4);
#my @worklist;
#
#while (@targets) {
# my $target = shift @targets;
# my $source = shift @sources;
# push @worklist, [$target, $source];
#}
#
#while (@worklist) {
# my $pair = shift @worklist;
# my $target = $pair->[0];
# my $source = $pair->[1];
#}
for my $dep (sort keys %dependencies) {
printf ""%s\n", $dep;
}