home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7752 < prev    next >
Encoding:
Text File  |  1993-01-11  |  2.6 KB  |  74 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!spillman!tye
  3. From: tye@spillman.uucp (E. Tye McQueen)
  4. Subject: Re: Makefile parsing (challenge)
  5. Message-ID: <1993Jan11.165901.26885@spillman.uucp>
  6. Date: Mon, 11 Jan 1993 16:59:01 GMT
  7. References: <1993Jan9.215626.25646@uvaarpa.Virginia.EDU>
  8. Organization: Spillman Data Systems
  9. Lines: 63
  10.  
  11. Kevin Burton <noran!iowa!kburton@uunet.uu.net> writes:
  12. )I want to extract certain variables defined in a makefile. If the makefile
  13. )looks like:
  14. )
  15. )   SOURCES= foo.c boo.c\
  16. )   tmp.c tmp1.c \
  17. )   a.c b.c c.c
  18. )
  19. )I want to end up with a single string of "foo.c boo.c tmp.c tmp1.c a.c b.c
  20. ) c.c".
  21. )I want to be able to specifie the string or expression for a string that
  22. )describes the variable that I am trying to extract.
  23.  
  24. A subsequent post to get make to tell you should work more often
  25. then this.  But it is still an interesting example (maybe you picked
  26. up a package with a makefile but you don't have make and you want to
  27. compile it by hand?).
  28.  
  29. It is not too hard to correct this to differentiate "$$(VAR)" from
  30. "$(VAR)", but I leave this as an exercise to the reader.  [Hint:
  31. it involves "(^|[^\$])(\$\$)*".]
  32.  
  33. Interestingly, I couldn't find any way to get (at least one version
  34. of) make to "quote" a # or tailing \ so this example is the same as
  35. make in that respect.
  36.  
  37. #!/usr/bin/perl
  38.  
  39. if(  ! @ARGV  ) {
  40.     die "Usage: $0 VAR1 [VAR2 [...]] <makefile\n";
  41. }
  42.  
  43. while(  <STDIN>  ) {
  44.     if(  m!^\s*([a-zA-Z]\w*)\s*=!  ) {  # "VAR=VAL" line:
  45.         $var= $1;       # Save the variable name.
  46.         $var{$var}= ""; # In case this redefines a variable.
  47.         $_= $';         # At least the first part of the value.
  48.         while(  1  ) {
  49.             if(  /#/  ) {           # Comment on end of line:
  50.                 $var{$var} .= $`;   # Just use part before comment.
  51.                 last;               # Value can't continue onto next line.
  52.             }
  53.             $var{$var} .= $_;   # Why isn't this an L-value?
  54.             chop( $var{$var} ); # Remove the newline
  55.          last  unless  m!\\$!;  # Value is not continued onto next line.
  56.             chop( $var{$var} ); # Remove the back slash
  57.             $_= <STDIN>;        # Get next line of value.
  58.         }
  59.         # Expand "$(var)" strings in value:
  60.         while(  $var{$var} =~ m!\$\(([a-zA-Z]\w*)\)!  ) { # Wrong for `$$(val)'
  61.             $var{$var}= $` . ( $var{$1} || $ENV{$1} ) . $';
  62.         }
  63.     }
  64. }
  65.  
  66. for(  @ARGV  ) {
  67.     print "$_=`$var{$_}'\n";
  68. }
  69.  
  70.  tye@spillman.com                         Tye McQueen, E.
  71. ----------------------------------------------------------
  72.  Nothing is obvious unless you are overlooking something. 
  73. ----------------------------------------------------------
  74.