home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!haven.umd.edu!darwin.sura.net!gatech!concert!sas!mozart.unx.sas.com!bultman
- From: bultman@sonny.unx.sas.com (David Bultman)
- Subject: inctree, p. 274 in Programming Perl
- Originator: bultman@sonny.unx.sas.com
- Sender: news@unx.sas.com (Noter of Newsworthy Events)
- Message-ID: <BuDq2p.GM9@unx.sas.com>
- Date: Thu, 10 Sep 1992 20:25:37 GMT
- Nntp-Posting-Host: sonny.unx.sas.com
- Organization: SAS Institute Inc.
- Keywords: inctree #include tree
- Lines: 129
-
-
- I typed in the inctree program in Programming Perl and something is awry.
- (p. 274 in the First edition, I checked the latest edition and the program
- is identical)
-
- If anyone has any ideas or previous experience with this, your feedback
- would be greatly appreciated.
-
- When I try inctree main.c and main.c looks like,
- -- here --
- #include <stdio.h>
- main()
- {
- }
- -- to here --
-
- the output is,
-
- Start program
- main.c
- /usr/include/stdio.h
- /usr/include/sys/stdsyms.h
-
- /usr/include/stdio.h DUPLICATE
- main.c DUPLICATE
-
- -- cut here ---
- #!/usr/local/bin/perl
-
- # Usage: inctree [options] [files]
-
- # Configuration parameters.
-
- printf "Start program\n";
-
- $CPP = 'cc -E';
- # $CPP = "cc -P";
- # $CPP = "/lib/cpp";
-
- $shiftwidth = 4;
-
- # Process switches.
-
- while ($ARGV[0] =~ /^-/) {
-
- $_ = shift;
- if (/^-D(.*)/) {
- $defines .= " -D" . ($1 ? $1 : shift);
- }
- elsif (/^-I(.*)/) {
- $includes .= " -I" . ($1 ? $1 : shift);
- }
- elsif (/^-m(.*)/) {
- push(@pats, $1 ? $1 : shift);
- }
- elsif (/^-l/) {
- $lines++;
- }
- else {
- die "Unrecognized switch: $_\n";
- }
- }
-
- # Build a subroutine to scan for any specified patterns.
-
- if (@pats) {
- $sub = "sub pats {\n";
- foreach $pat (@pats) {
- $sub .= " print '>>>>>>> ',\$_ if m$pat;\n";
- }
- $sub .= "}\n";
- eval $sub;
- ++$pats;
- }
-
- # Now process each file on the command line.
-
- foreach $file (@ARGV) {
- open(CPP,"$CPP $defines $includes $file|")
- || die "Can't run cpp: $!\n";
- $line = 2;
-
- while (<CPP>) {
- ++$line;
- &pats if $pats; # Avoid expensive call if we can.
- next unless /^#/;
- next unless /^# \d/;
- ($junk,$newline,$filename) = split;
- $filename =~ s/"//g;
-
- # Now figure out if it's a push, a pop or neither.
-
- if ($stack[$#stack] eq $filename) { # Same file.
- $line = $newline-1;
- next;
- }
-
- if ($stack[$#stack-1] eq $filename) { # Leaving file.
- $indent -= $shiftwidth;
- $line = pop(@lines)-1;
- pop(@stack);
- }
- else {
- printf "%6d ", $line-2 if $lines;
- push(@lines,$line);
- $line = $newline;
- print "\t" x ($indent / 8), ' ' x ($indent % 8),
- $filename;
- print " DUPLICATE" if $seen{$filename}++;
- print "\n";
- $indent += $shiftwidth;
- push(@stack,$filename);
- }
- }
- close CPP;
- $indent = 0;
- %seen = ();
- print "\n\n";
- $line = 0;
- }
- --- end cut ---
-
-
-
-
- --
- David Bultman, bultman@unx.sas.com SAS Institute, Inc.
- Core Research and Development SAS Campus Dr, Cary, NC
- (919) 677-8000 x6875 27513-2414 USA
-