package RDA::Macro::Hcve; # $Id: Hcve.pm,v 1.11 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Hcve.pm,v 1.11 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Hcve - Class Used for HCVE Rule Sets =head1 SYNOPSIS require RDA::Macro::Hcve; =head1 DESCRIPTION The objects of the C class are used to manage HCVE rule sets. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Spec; use IO::File; use RDA::Block; use RDA::Value; use RDA::Object::Buffer; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.11 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my $TRC = 'TRACE/HCVE/'; my %tb_fct = ( 'evalHcveAction' => [\&_m_eval_action, 'N'], 'getHcveFile' => [\&_m_get_file, 'T'], 'getHcveName' => [\&_m_get_name, 'T'], 'getHcvePlatform' => [\&_m_get_platform, 'T'], 'getHcveProduct' => [\&_m_get_product, 'T'], 'getHcveResult' => [\&_m_get_result, 'T'], 'getHcveSets' => [\&_m_get_sets, 'L'], 'getHcveType' => [\&_m_get_type, 'T'], 'getHcveValues' => [\&_m_get_values, 'T'], 'setHcveContext' => [\&_m_set_context, 'N'], 'setHcveResult' => [\&_m_set_result, 'T'], 'setHcveRule' => [\&_m_set_rule, 'T'], 'setHcveVariable' => [\&_m_set_variable, 'N'], ); my %tb_inf = ( name => 2, platform => 4, product => 3, type => 1, ); my %tb_osn = ( 'aix' => 'aix|Unix', 'bsdos' => 'Unix', 'cygwin' => 'cygwin|MSWin32', 'darwin' => 'darwin|Unix', 'dec_osf' => 'dec_osf|Unix', 'dgux' => 'Unix', 'dynixptx' => 'dynixptx|Unix', 'freebsd' => 'Unix', 'hpux' => 'hpux|Unix', 'irix' => 'Unix', 'linux' => 'linux|Unix', 'next' => 'Unix', 'openbsd' => 'Unix', 'sco_sv' => 'Unix', 'solaris' => 'solaris|Unix', 'sunos' => 'solaris|Unix', 'svr4' => 'Unix', 'Windows_NT' => 'MSWin32' ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Hcve-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< '_ctx' > Action evaluation context =item B< '_dir' > HCVE directory =item B< '_flg' > HCVE rule set load indicator =item B< '_mod' > Module directory =item B< '_res' > HCVE rule result hash =item B< '_rul' > HCVE rule indentifier =item B< '_set' > HCVE rule set hash =item B< '_trc' > Trace level =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; # Setup some parameters by default $slf->{'_dir'} = $agt->get_setting('HCVE_DIRECTORY', 'hcve'); $slf->{'_flg'} = 0; $slf->{'_mod'} = $agt->get_setting('MOD_DIRECTORY', 'modules'); $slf->{'_res'} = {}; $slf->{'_rul'} = 0; $slf->{'_set'} = {}; $slf->{'_trc'} = $agt->get_setting('HCVE_TRACE', 0); # 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 HCVE MACROS =head2 S This macro evaluates the rule code. It returns a zero value when the action code is successfully parsed and executed. It returns -1 for a missing action evaluation context, 1 for parsing errors, and 2 for execution errors. =cut sub _m_eval_action { my ($slf, $ctx, $cod) = @_; my ($blk); # Abort if the evaluation context is not available return -1 unless exists($slf->{'_ctx'}); # Parse the action code $blk = RDA::Block->new('rule'.$slf->{'_rul'}, $slf->{'_mod'}, $slf->{'_agt'}); $cod = '' unless defined($cod); return 1 if $blk->parse(RDA::Object::Buffer->new($cod)); # Execute the action and indicate its success $blk->eval($slf->{'_ctx'}, 1, $TRC, $slf->{'_trc'}) ? 2 : 0; } =head2 S This macro returns the file name containing the HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_file { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[0]; } =head2 S This macro returns the name of a HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_name { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[2]; } =head2 S This macro returns the platform related to a HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_platform { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[4]; } =head2 S This macro returns the product related to a HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_product { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[3]; } =head2 S This macro returns the result string of the specified rule. By default, it returns the current one. =cut sub _m_get_result { my ($slf, $ctx, $rul) = @_; $rul = $slf->{'_rul'} unless defined($rul); ($rul && exists($slf->{'_res'}->{$rul})) ? $slf->{'_res'}->{$rul} : undef; } =head2 S This macro determines the list of all rule sets from the specified type. You can specify an alternative operating system as a second argument. =cut sub _m_get_sets { my ($slf, $ctx, $typ, $osn) = @_; my ($rec, @tbl); # On the first call load the HCVE rule set information unless ($slf->{'_flg'}) { if (opendir(DIR, $slf->{'_dir'})) { foreach my $set (readdir(DIR)) { next unless $set =~ m/\.xml$/; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); } closedir(DIR) } $slf->{'_flg'} = 1; } # Select rule sets if ($typ) { $osn = $^O unless defined($osn); $osn = $tb_osn{$osn} if exists($tb_osn{$osn}); foreach my $set (sort keys(%{$slf->{'_set'}})) { $rec = $slf->{'_set'}->{$set}; push(@tbl, $set) if $rec->[1] && $rec->[1] eq $typ && $rec->[4] =~ m/^(generic|$osn)/; } } # Return the resuls @tbl; } =head2 S This macro returns the type of a HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_type { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[1]; } =head2 S This macro returns the string with all HCVE variable references replaced by their respective values. You cannot nest variable references. =cut sub _m_get_values { my ($slf, $blk, $str) = @_; if ($str && exists($slf->{'_ctx'})) { my $ctx = $slf->{'_ctx'}; $str =~ s#\$\{(\w+)(:([^\{\}]*))?\}#_get_value($ctx, $1, $3)#eg; } $str; } sub _get_value { my ($ctx, $nam, $dft) = @_; my $val = RDA::Value->new('R', $ctx, '$'.$nam)->eval_value->eval_value(1); (!$val->is_undef) ? $val->eval_as_string(1) : defined($dft) ? $dft : ''; } =head2 S This macro initializes a context for evaluating action code. It deduced the initialization file name from the specified object identifier by adding a C<.def> suffix to it. It deletes the previous context and it clears previous test results. It returns zero when that file has been successfully loaded and executed. It returns -1 for a missing initialization file, -2 when the initialization file cannot be opened, 1 for parsing errors, and 2 for execution errors. =cut sub _m_set_context { my ($slf, $ctx, $nam) = @_; my ($blk, $fil, $ifh); # Delete the previous context delete($slf->{'_ctx'}); # Determine the data collection specification file return -1 unless $nam; $blk = RDA::Block->new($nam, $slf->{'_mod'}, $slf->{'_agt'}); $fil = File::Spec->catfile($slf->{'_mod'}, $nam); $ifh = IO::File->new; return -2 unless $ifh->open("<$fil.def") || $ifh->open("<$fil.cfg"); # Load and parse the file return 1 if $blk->parse($ifh); $slf->{'_ctx'} = $blk; $slf->{'_res'} = {}; # Execute the context initialisation $blk->eval($ctx, 0, $TRC, $slf->{'_trc'}) ? 2 : 0; } =head2 S This macro specifies the results of the current rule and returns the result string. =cut sub _m_set_result { my ($slf, $ctx, $str) = @_; $slf->{'_res'}->{$slf->{'_rul'}} = $str; } =head2 S This macro specifies the identifier of the current rule. It returns the previous value. The current value is not changed if an undefined value is specified as an argument. =cut sub _m_set_rule { my ($slf, $ctx, $rul) = @_; my $old; $old = $slf->{'_rul'}; $slf->{'_rul'} = $rul if defined($rul); $old; } =head2 S This macro shares the action variables with the action evaluation context. The variables are specified by their name. It returns the number of variables that have been effectively shared. =cut sub _m_set_variable { my $slf = shift; my $blk = shift; my ($cnt, $ctx, $glb, $loc); # Abort if the evaluation context is not available return 0 unless exists($slf->{'_ctx'}); $ctx = $slf->{'_ctx'}; # Share the local variables foreach my $nam (@_) { return 1 unless $nam =~ m/^[\$\@\%]\w+$/; $loc = RDA::Value->new('R', $blk, $nam); $glb = RDA::Value->new('R', $ctx, $nam); $glb->assign_value($loc); ++$cnt; } # Indicate the number of variables that have been shared. $cnt; } # --- Internal functions ----------------------------------------------------- # Get the rule set information sub _get_info { my ($slf, $set) = @_; my ($buf, $fil, $key, $rec, $val); $set =~ s/\.xml$//i; $fil = File::Spec->catfile($slf->{'_dir'}, "$set.xml"); $slf->{'_set'}->{$set} = $rec = []; if (open(HCVE, "<$fil")) { $buf = ''; while () { $buf .= $_; $buf =~ s/[\n\r\s]*$/ /; if ($buf !~ m##) { # Decode the attributes $buf =~ s#.*[$tb_inf{$1}] = $2 if exists($tb_inf{$1}); } # Complete the information when a valid rule set has been found if ($rec->[1]) { $rec->[0] = $fil; $rec->[4] = 'generic' unless defined($rec->[4]); } last; } } close(HCVE); } } 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