home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / perl / 7746 < prev    next >
Encoding:
Internet Message Format  |  1993-01-11  |  2.2 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!rpi!ghost.dsi.unimi.it!univ-lyon1.fr!chx400!sicsun!siisun.epfl.ch!brossard
  2. From: brossard@siisun.epfl.ch (Alain Brossard EPFL-SIC/SII)
  3. Newsgroups: comp.lang.perl
  4. Subject: Re: Makefile parsing (challenge)
  5. Message-ID: <1993Jan11.121155@siisun.epfl.ch>
  6. Date: 11 Jan 93 11:11:55 GMT
  7. References: <1993Jan9.215626.25646@uvaarpa.Virginia.EDU> <1iri8tINNspv@bach.mentor.dk>
  8. Sender: news@sicsun.epfl.ch
  9. Reply-To: brossard@sic.epfl.ch
  10. Organization: Ecole Polytechnique Federale de Lausanne
  11. Lines: 53
  12.  
  13. |> Kevin Burton <noran!iowa!kburton@uunet.uu.net> writes:
  14. |> 
  15. |> >I want to extract certain variables defined in a makefile.
  16.    
  17.     I wrote the following because I needed to do just that.  You'll need
  18. to adapt it to your specific needs.
  19.  
  20. #  get_nis_passwd_file returns the name of the NIS password file and the
  21. # name of the shadow password file.  Either can be replaced with the
  22. # string NIS if it thinks this is the master NIS machine but it couldn't find
  23. # the password file; or it returns an empty string otherwise (not an NIS master)
  24. .
  25. # It assumes that the current directory is /var/yp
  26. #
  27. local( %var );
  28. sub get_nis_passwd_file {
  29.     local( $pwd, $pwd_adj );
  30.     if( -e 'binding' && -e 'Makefile' && -e 'passwd.time' &&
  31.                                 open( MAKE, "Makefile" ) ) {
  32.         while( $_ = <MAKE> ) {
  33.             if( /^\s*(\w+)\s*=\s*(.*)$/ ) {
  34.                 $var{ $1 } = $2;        # record Make variables   <===========
  35.             } elsif ( /^passwd.time:\s*([^\s]+)/ ) {
  36.                 $pwd = &gnpf_expand_make_name( $1 );
  37.                 $pwd = '' if ! -e $pwd;
  38.             }
  39.         }
  40.         close MAKE;
  41.         $pwd = 'NIS' if ! $pwd;
  42.     }
  43.  
  44.     return ( $pwd, $pwd_adj );
  45. }
  46. #
  47. #               Expand any variables in the file name
  48. #
  49. sub gnpf_expand_make_name {
  50.     local( $pwd ) = shift;
  51.     local( $begin, $var, $end );
  52.     while( $pwd =~ /^([^\$]*)\$[({](\w+)[)}](.*)$/ ) {
  53.         $begin = $1; $var = $2; $end = $3;
  54.         $var = $var{ $var };    # Doesn't matter if not defined!
  55.         $pwd = $begin.$var.$end;
  56.     }
  57.     return $pwd;
  58. }
  59. 1;
  60.  
  61. -- 
  62.  
  63. Alain Brossard, Ecole Polytechnique Federale de Lausanne,
  64.     SIC/SII, EL-Ecublens, CH-1015 Lausanne, Suisse, +41 21 693-2211
  65. brossard@sic.epfl.ch
  66.