package RDA::Macro::Env; # $Id: Env.pm,v 1.23 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Env.pm,v 1.23 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Env - Class Used for Environment Variable Macros =head1 SYNOPSIS require RDA::Macro::Env; =head1 DESCRIPTION The objects of the C class are used to interface with environment variable-related macros. The following methods are available: =cut use strict; BEGIN { use Exporter; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.23 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_fct = ( 'backupEnv' => [\&_m_backup_env, 'T'], 'findCommand' => [\&_m_find_cmd, 'T'], 'getEnv' => [\&_m_get_env, 'T'], 'grepEnv' => [\&_m_grep_env, 'L'], 'replaceEnv' => [\&_m_replace_env, 'T'], 'restoreEnv' => [\&_m_restore_env, 'T'], 'setEnv' => [\&_m_set_env, 'T'], 'source' => [\&_m_source, 'N'], ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Env-Enew($agt)> The object constructor. It takes the agent object reference as an argument. C is represented by a blessed hash reference. The following special keys are used: =over 16 =item B< '_agt' > Reference to the agent object =item B< '_bkp' > Copies of environment variables =item B< '_env' > Hash that contains the original environment variables =item B< '_sep' > Path separator =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Create the macro object $slf = bless {_agt => $agt}, $class; # Take a copy of the environment variables eval { foreach my $key (keys(%ENV)) { $slf->{'_env'}->{$key} = $ENV{$key}; } }; die "RDA-00515: Cannot access environment variables:\n$@\n" if $@; # Setup some attributes $slf->{'_sep'} = ($^O eq 'MSWin32') ? ';' : ':'; # Register the macros $agt->register($slf, [keys(%tb_fct)]); # Return the object reference $slf; } =head2 S<$h-Erun($name,$arg,$ctx)> This method executes the macro with the specified argument list in a given context. =cut sub run { my ($slf, $nam, $arg, $ctx) = @_; my ($fct, $ret, $typ); $fct = $tb_fct{$nam}; $typ = $fct->[1]; # Treat an array context return RDA::Value::new_list(&{$fct->[0]}($slf, $ctx, $arg->eval_as_array)) if $typ eq 'L'; # Treat a scalar context $ret = &{$fct->[0]}($slf, $ctx, $arg->eval_as_array); $typ = 'U' unless defined($ret); return RDA::Value->new($typ, $ret); } =head1 ENVIRONMENT VARIABLE MACROS =head2 S This macro creates a backup of the internal copy of environment variables under the specified name. It uses C as the default name. It returns the backup name. =cut sub _m_backup_env { my ($slf, $ctx, $nam) = @_; $nam = 'dft' unless defined($nam); $slf->{'_bkp'}->{$nam} = { %{$slf->{'_env'}} }; $nam; } =head2 S This macro explores the path to find where a command is located. When the command is found, it returns a full path name. Otherwise, it returns an undefined variable. It only considers files or symbolic links in its search. Unless the flag is set, the file path is quoted as required by a command shell. =cut sub _m_find_cmd { my ($slf, $ctx, $cmd, $flg) = @_; my ($fil, $win, @tbl); if ($cmd) { $win = $slf->{'_agt'}->is_windows; foreach my $dir (split(/$slf->{'_sep'}/, $ENV{'PATH'})) { if (opendir(DIR, $dir)) { if ($win) { @tbl = grep {m#^$cmd(\.(bat|exe))?$#i} readdir(DIR); # } else { @tbl = grep {m#^$cmd$#} readdir(DIR); # } closedir(DIR); foreach my $nam (@tbl) { $fil = File::Spec->catfile($dir, $nam); return $flg ? $fil : $slf->{'_agt'}->quote($fil) if -f $fil || -l $fil; } } } } undef; } =head2 S This macro returns the value of the specified environment variable. It returns the default value when the environment variable is not defined. If the flag is set, it retrieves the value from the current environment instead of the local copy. =cut sub _m_get_env { my ($slf, $ctx, $key, $val, $flg) = @_; if ($flg) { $val = $ENV{$key} if exists($ENV{$key}); } else { $val = $slf->{'_env'}->{$key} if exists($slf->{'_env'}->{$key}); } $val; } =head2 S This macro returns the list of all environment variables with names that match the regular expression. The following options are supported: =over 9 =item B< 'i' > Ignores case distinctions in both the pattern and the name =item B< 'v' > Inverts the sense of matching to select non-matching lines =back =cut sub _m_grep_env { my ($slf, $ctx, $re, $opt) = @_; my ($flg, $inv, @tbl); # Decode the options $opt = '' unless defined($opt); $re = (index($opt, 'i') < 0) ? qr#$re# : qr#$re#i; $inv = index($opt, 'v') >= 0; # Scan the variables foreach my $key (sort keys(%{$slf->{'_env'}})) { $flg = ($key =~ $re); push(@tbl, $key) if ($inv ? !$flg : $flg); } # Return the variable list @tbl; } =head2 S This macro replaces all environment variable references contained in the specified string. On UNIX, C<$name>, C<${name}>, and C<${name:-text}> are resolved. On Windows, the macro supports the C<%name%> format. No replacements are performed on VMS. =cut sub _m_replace_env { my ($slf, $ctx, $str) = @_; my ($agt); $agt = $slf->{'_agt'}; if ($agt->is_unix) { $str =~ s#\$(\w+)#_repl_unix1($slf, $1)#eg; $str =~ s#\$\{(\w+)\}#_repl_unix1($slf, $1)#eg; 1 while $str =~ s#\$\{(\w+)\:\-([^\}]*)\}#_repl_unix2($slf, $1, $2)#eg; } elsif ($agt->is_windows) { $str =~ s#\%(\w+)\%#_repl_windows($slf, $1)#eg; } $str; } sub _repl_unix1 { my ($slf, $nam) = @_; exists($slf->{'_env'}->{$nam}) ? $slf->{'_env'}->{$nam} : ''; } sub _repl_unix2 { my ($slf, $nam, $alt) = @_; my $str = exists($slf->{'_env'}->{$nam}) ? $slf->{'_env'}->{$nam} : ''; length($str) ? $str : $alt; } sub _repl_windows { my ($slf, $nam) = @_; return $slf->{'_env'}->{$nam} if exists($slf->{'_env'}->{$nam}); `echo \%$nam\%`; } =head2 S This macro restores a backup of the internal copy of the environment variables. It uses C as the default name. When the macro restores the backup, the backup is then deleted. When successful, it returns the name of the backup. Otherwise, it returns an undefined value. =cut sub _m_restore_env { my ($slf, $ctx, $nam) = @_; $nam = 'dft' unless defined($nam); return undef unless exists($slf->{'_bkp'}->{$nam}); $slf->{'_env'} = delete($slf->{'_bkp'}->{$nam}); $nam; } =head2 S This macro sets the value of the specified environment variable. When the value is undefined, the variable is deleted. When the flag is set, it adjusts the internal copy of the environment variables also. It returns the previous value of the environment variable. =cut sub _m_set_env { my ($slf, $ctx, $key, $val, $flg) = @_; my $old; $old = $ENV{$key} if exists($ENV{$key}); if (defined($val)) { $ENV{$key} = $val; $slf->{'_env'}->{$key} = $val if $flg; } else { delete($ENV{$key}); delete($slf->{'_env'}->{$key}) if $flg; } $old; } =head2 S This macro uses the program that is specified as an argument to modify the environment. The program is sourced in C on UNIX or executes with C on Windows. Unless the flag is set, the macro modifies the internal copy of the environment variables only. It returns the number of modified variables. =cut sub _m_source { my ($slf, $ctx, $pgm, $flg) = @_; my ($cmd, $cnt, $ref, %env); if ($^O eq 'MSWin32') { $ref = "cmd /C \"set\" |"; $cmd = "cmd /C \"$pgm 2>NUL && set\" |"; } else { $ref = "sh -c 'env' |"; $cmd = "sh -c '. $pgm 2>/dev/null ; env' |"; } # Take a copy of the current environment return 0 unless open(IN, $ref); while () { s/[\r\n]*$//; $env{$1} = $2 if m/^(\w+)=(.*)$/; } close(IN); # Compare with the modified environment return 0 unless $pgm && open(IN, $cmd); $cnt = 0; while () { s/[\r\n]*$//; if (m/^(\w+)=(.*)$/ && (!exists($env{$1}) || $env{$1} ne $2)) { $slf->{'_env'}->{$1} = $2; $ENV{$1} = $2 if $flg; ++$cnt; } } close(IN); # Returns the number of modified variables $cnt; } 1; __END__ =head1 SEE ALSO See also L, L, L, L. =head1 COPYRIGHT NOTICE Copyright (c) 2002-2008, Oracle. All rights reserved. =head1 TRADEMARK NOTICE Oracle, JD Edwards, PeopleSoft, and Siebel are registered trademarks of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. =cut