home *** CD-ROM | disk | FTP | other *** search
/ ftp.cse.unsw.edu.au / 2014.06.ftp.cse.unsw.edu.au.tar / ftp.cse.unsw.edu.au / pub / doc / languages / perl / nutshell / ch6 / w4 < prev    next >
Encoding:
Text File  |  1992-10-18  |  1.0 KB  |  58 lines

  1. #!/usr/bin/perl
  2.  
  3. # Usage: w4 [options] [files]
  4.  
  5. # Configuration parameters.
  6.  
  7. $CPP = "cc -E";
  8. # $CPP = "cc -P";
  9. # $CPP = "/lib/cpp";
  10.  
  11. # Process switches.
  12.  
  13. while ($ARGV[0] =~ /^-/) {
  14.     $_ = shift;
  15.     if (/^-D(.*)/) {
  16.     $defines .= " -D" . ($1 ? $1 : shift);
  17.     }
  18.     elsif (/^-I(.*)/) {
  19.     $includes .= " -I" . ($1 ? $1 : shift);
  20.     }
  21.     elsif (/^-d(.*)/) {
  22.     $dir = ($1 ? $1 : shift);
  23.     }
  24.     else {
  25.     die "Unrecognized switch: $_\n";
  26.     }
  27. }
  28.  
  29. # Do each file on command line.
  30.  
  31. foreach $file (@ARGV) {
  32.     open(CPP,"$CPP $defines $includes $file|")
  33.     || die "Can't run cpp: $!\n";
  34.  
  35.     # Scan output for line directives.
  36.  
  37.     %seen = ();
  38.     while (<CPP>) {
  39.     next unless /^#/;
  40.     next unless ($filename) = /^# \d.*"(.*)"/;
  41.     $seen{$filename}++;
  42.     }
  43.     close CPP;
  44.  
  45.     # Figure out the corresponding object file name.
  46.  
  47.     ($ofile = $file) =~ s/\.c$/.o/;
  48.     $ofile =~ s#.*/##;
  49.     $ofile = "$dir/$ofile" if $dir;
  50.  
  51.     # Print out the dependencies.
  52.  
  53.     foreach $dep (sort keys(%seen)) {
  54.     print "$ofile: $dep\n";
  55.     }
  56.     print "\n";
  57. }
  58.