home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / perl / 5847 < prev    next >
Encoding:
Text File  |  1992-09-10  |  3.2 KB  |  143 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!sun-barr!ames!haven.umd.edu!darwin.sura.net!gatech!concert!sas!mozart.unx.sas.com!bultman
  3. From: bultman@sonny.unx.sas.com (David Bultman)
  4. Subject: inctree, p. 274 in Programming Perl
  5. Originator: bultman@sonny.unx.sas.com
  6. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  7. Message-ID: <BuDq2p.GM9@unx.sas.com>
  8. Date: Thu, 10 Sep 1992 20:25:37 GMT
  9. Nntp-Posting-Host: sonny.unx.sas.com
  10. Organization: SAS Institute Inc.
  11. Keywords: inctree #include tree
  12. Lines: 129
  13.  
  14.  
  15. I typed in the inctree program in Programming Perl and something is awry.
  16. (p. 274 in the First edition, I checked the latest edition and the program
  17.  is identical)
  18.  
  19. If anyone has any ideas or previous experience with this, your feedback 
  20. would be greatly appreciated.
  21.  
  22. When I try inctree main.c and main.c looks like,
  23. -- here --
  24. #include <stdio.h>
  25. main()
  26. {
  27. }
  28. -- to here --
  29.  
  30. the output is,
  31.  
  32. Start program
  33. main.c
  34.     /usr/include/stdio.h
  35.     /usr/include/sys/stdsyms.h
  36.         
  37.         /usr/include/stdio.h  DUPLICATE
  38.         main.c  DUPLICATE
  39.  
  40. -- cut here ---
  41. #!/usr/local/bin/perl
  42.  
  43. # Usage: inctree [options] [files]
  44.  
  45. # Configuration parameters.
  46.  
  47. printf "Start program\n";
  48.  
  49. $CPP = 'cc -E';
  50. # $CPP = "cc -P";
  51. # $CPP = "/lib/cpp";
  52.  
  53. $shiftwidth = 4;
  54.  
  55. # Process switches.
  56.  
  57. while ($ARGV[0] =~ /^-/) {
  58.  
  59.     $_ = shift;
  60.     if (/^-D(.*)/) {
  61.         $defines .= " -D" . ($1 ? $1 : shift);
  62.     }
  63.     elsif (/^-I(.*)/) {
  64.         $includes .= " -I" . ($1 ? $1 : shift);
  65.     }
  66.     elsif (/^-m(.*)/) {
  67.         push(@pats, $1 ? $1 : shift);
  68.     }
  69.     elsif (/^-l/) {
  70.         $lines++;
  71.     }
  72.     else {
  73.         die "Unrecognized switch: $_\n";
  74.     }
  75. }
  76.  
  77. # Build a subroutine to scan for any specified patterns.
  78.  
  79. if (@pats) {
  80.     $sub = "sub pats {\n";
  81.     foreach $pat (@pats) {
  82.         $sub .= "    print '>>>>>>> ',\$_ if m$pat;\n";
  83.     }
  84.     $sub .= "}\n";
  85.     eval $sub;
  86.     ++$pats;
  87. }
  88.  
  89. # Now process each file on the command line.
  90.  
  91. foreach $file (@ARGV) {
  92.     open(CPP,"$CPP $defines $includes $file|")
  93.         || die "Can't run cpp: $!\n";
  94.     $line = 2;
  95.  
  96.     while (<CPP>) {
  97.         ++$line;
  98.         &pats if $pats;  # Avoid expensive call if we can.
  99.         next unless /^#/;
  100.         next unless /^# \d/;
  101.         ($junk,$newline,$filename) = split;
  102.         $filename =~ s/"//g;
  103.  
  104.         # Now figure out if it's a push, a pop or neither.
  105.  
  106.         if ($stack[$#stack] eq $filename) {  # Same file.
  107.             $line = $newline-1;
  108.             next;
  109.         }
  110.  
  111.         if ($stack[$#stack-1] eq $filename) {  # Leaving file.
  112.             $indent -= $shiftwidth;
  113.             $line = pop(@lines)-1;
  114.             pop(@stack);
  115.         }
  116.         else {
  117.             printf "%6d  ", $line-2 if $lines;
  118.             push(@lines,$line);
  119.             $line = $newline;
  120.             print "\t" x ($indent / 8), ' ' x ($indent % 8),
  121.                 $filename;
  122.             print "  DUPLICATE" if $seen{$filename}++;
  123.             print "\n";
  124.             $indent += $shiftwidth;
  125.             push(@stack,$filename);
  126.         }
  127.     }
  128.     close CPP;
  129.     $indent = 0;
  130.     %seen = ();
  131.     print "\n\n";
  132.     $line = 0;
  133. }
  134. --- end cut ---
  135.    
  136.  
  137.  
  138.  
  139. -- 
  140. David Bultman, bultman@unx.sas.com    SAS Institute, Inc.
  141. Core Research and Development        SAS Campus Dr, Cary, NC
  142. (919) 677-8000 x6875            27513-2414 USA
  143.