package RDA::Macro::Hcve; # $Id: Hcve.pm,v 1.23 2009/03/30 16:28:38 mschenke Exp $ # ARCS: $Header: /u02/webapps/workbench/cvs/RDA_4/src/scripting/lib/RDA/Macro/Hcve.pm,v 1.23 2009/03/30 16:28:38 mschenke Exp $ # # Change History # 20090330 MSC Extend getHcveValues. =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::Object::Data; 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.23 $ =~ /(\d+)\.(\d+)/); # Define the global private variables # Define the global private variables my $OBJECT = qr/^RDA::Object::/; my $VALUE = 'RDA::Value'; my $TRC = 'TRACE/HCVE/'; my %tb_fam = ( 'aix' => 'Unix', 'bsdos' => 'Unix', 'cygwin' => 'Cygwin', 'darwin' => 'Unix', 'dec_osf' => 'Unix', 'dgux' => 'Unix', 'dynixptx' => 'Unix', 'freebsd' => 'Unix', 'hpux' => 'Unix', 'irix' => 'Unix', 'linux' => 'Unix', 'MacOs' => 'Mac', 'MSWin32' => 'Windows', 'MSWin64' => 'Windows', 'next' => 'Unix', 'openbsd' => 'Unix', 'svr4' => 'Unix', 'sco_sv' => 'Unix', 'solaris' => 'Unix', 'sunos' => 'Unix', 'VMS' => 'Vms', 'Windows_NT' => 'Windows', ); my %tb_fct = ( 'addHcveParameter' => [\&_m_add_parameter, 'N'], 'addHcveVariable' => [\&_m_add_variable, 'N'], 'evalHcveCommand' => [\&_m_eval_command, 'L'], 'getHcveFact' => [\&_m_get_fact, 'V'], 'getHcveFamily' => [\&_m_get_family, 'T'], 'getHcveFile' => [\&_m_get_file, 'T'], 'getHcveName' => [\&_m_get_name, 'T'], 'getHcveParameter' => [\&_m_get_parameter, 'V'], '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'], 'setHcveFact' => [\&_m_set_fact, 'N'], 'setHcveParameter' => [\&_m_set_parameter, 'T'], 'setHcveResult' => [\&_m_set_result, 'T'], 'setHcveRule' => [\&_m_set_rule, 'T'], 'setHcveVariable' => [\&_m_set_variable, 'N'], ); my %tb_inf = ( family => 5, platform => 4, product => 3, title => 2, type => 1, ); my %tb_osn = ( 'sunos' => 'solaris', 'MSWin64' => 'MSWin32', 'Windows_NT' => 'MSWin32', ); my %tb_stl = ( '*' => '**', "'" => "''", '`' => '``', ); # 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 12 =item S< B<'_agt'> > Reference to the agent object =item S< B<'_ctx'> > Action evaluation context =item S< B<'_col'> > Fact collector hash =item S< B<'_dir'> > HCVE directory =item S< B<'_flg'> > HCVE rule set load indicator =item S< B<'_map'> > Parameter to collector mapping =item S< B<'_mod'> > Module directory =item S< B<'_res'> > HCVE rule result hash =item S< B<'_rul'> > HCVE rule indentifier =item S< B<'_set'> > HCVE rule set hash =item S< 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->{'_col'} = {}; $slf->{'_dir'} = $agt->get_setting('HCVE_DIRECTORY', 'hcve'); $slf->{'_flg'} = 0; $slf->{'_map'} = {}; $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-Ecall($name,...)> This method executes the macro code. =cut sub call { my ($slf, $nam, @arg) = @_; &{$tb_fct{$nam}->[0]}($slf, @arg); } =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 functions without value conversion return &{$fct->[0]}($slf, $ctx, $arg->eval_as_array) if $typ eq 'V'; # 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 adds a result to the specified parameter. When needed, it converts the current value to a list. =cut sub _m_add_parameter { my ($slf, $ctx, $nam, $val) = @_; my ($key, $ptr, @tbl); @tbl = split(/\./, uc($nam)); if (defined($key = pop(@tbl))) { $ptr = $slf->{'_res'}; foreach my $itm (@tbl) { $ptr->{$itm} = {} unless ref($ptr->{$itm}) eq 'HASH'; $ptr = $ptr->{$itm}; } if (exists($ptr->{$key})) { $ptr->{$key} = [$ptr->{$key}] unless ref($ptr->{$key}) eq 'ARRAY'; push(@{$ptr->{$key}}, $val); } else { $ptr->{$key} = [$val]; } } $val; } =head2 S This macro defines a scalar variable in the action evaluation context and assigns the specified variable to it. It returns the number of variables that have been effectively created. =cut sub _m_add_variable { my ($slf, $ctx, $nam, $val) = @_; # Validate the request return 0 unless exists($slf->{'_ctx'}) && $nam =~ m/^\$\w+$/; # Create the variable RDA::Value->new('V', $slf->{'_ctx'}, $nam)->assign_value( RDA::Value->new('T', $val)); # Indicate the number of variables that have been created 1; } =head2 S This macro evaluates a command block. It returns a list containing the value, possibly followed by error details. =cut sub _m_eval_command { my ($slf, $ctx, $typ, $cod, $nam) = @_; my ($fct, $msg, $ret, $val); $nam = 'rule'.$slf->{'_rul'} unless defined($nam); if ($typ eq 'OS') { $val = join("\n", $ctx->call('command', $ctx, $cod)); $val =~ s/[\n\r\s]+$//m; return ($val, 'Execution Error', 'Exit code: '.$ret) if ($ret = $ctx->call('status', $ctx)); } elsif ($typ eq 'PERL') { my ($pgm, $ret); return ($val, 'Perl not available') unless ($pgm = $slf->{'_agt'}->get_setting('RDA_PERL')); $cod =~ s/"/\\"/g; $cod =~ s/\$/\$/g; $cod =~ s/\n/ /g; $cod = $pgm.' -e "'.$cod.'"'; $ret = $ctx->call('loadCommand', $ctx, $cod); $val = join("\n", $ctx->call('getLines', $ctx)); $val =~ s/[\n\r\s]+$//m; return ($val, 'Execution Error', 'Exit code: '.$ctx->call('statCommand', $ctx)) unless $ret; } elsif ($typ eq 'RDA') { my ($ret); $ret = _eval_command($slf, $nam, $cod); $val = _m_get_result($slf, $ctx); return ($val, $ret) if $ret; } elsif ($typ eq 'SQL') { my ($ret); $ret = $ctx->call('loadSql', $ctx, "SET define on\n$cod"); $val = join("\n", $ctx->call('grepLastSql', $ctx, '.?')); $val =~ s/^\s+//; $val =~ s/[\n\r\s]+$//m; return ($val, 'SQL Error', $ctx->call('getSqlMessage', $ctx)) unless $ret; return ($val, 'SQL Error') if $ctx->call('grepLastSql', $ctx, '^(ORA|SP2)-\d+','f') } else { return ($val, "Invalid command type $typ"); } # Indicate the successful completion ($val); } sub _eval_command { my ($slf, $nam, $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($nam, $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 performs required fact collection and returns the value of the specified parameter. =cut sub _m_get_fact { my ($slf, $ctx, $nam, $dft) = @_; my ($cod, $col, $err, $key, $uid, $val, @err); # Perform the fact collection when needed $nam = uc($nam); if (exists($slf->{'_map'}->{$nam}) && defined($col = delete($slf->{'_col'}->{$uid = $slf->{'_map'}->{$nam}}))) { foreach my $cmd (@$col) { next unless ($cod = $cmd->data); # Execute the command ($val, $err, @err) = _m_eval_command($slf, $ctx, $cmd->{'type'} || '', $cod, "fact$uid"); die join('\n', "Errors encountered when collecting fact '$uid':", $err, @err, '') if $err; # Define parameter and variable _m_set_parameter($slf, $ctx, $key, $val) if defined($key = $cmd->{'parameter'}) && $key =~ m/^(\w+\.)+\w+$/; _m_add_variable($slf, $ctx, $key, $val) if defined($key = $cmd->{'variable'}) && $key =~ m/^\$\w+$/; } } # Get the parameter value _m_get_parameter($slf, $ctx, $nam, $dft); } =head2 S This macro returns the operating system family related to a HCVE rule set. It returns an undefined value when the rule set cannot be found. =cut sub _m_get_family { my ($slf, $ctx, $set) = @_; return undef unless $set; _get_info($slf, $set) unless exists($slf->{'_set'}->{$set}); $slf->{'_set'}->{$set}->[5]; } =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 value of the specified parameter. =cut sub _m_get_parameter { my ($slf, $ctx, $nam, $dft) = @_; my ($val, @tbl); @tbl = split(/\./, uc($nam)); $val = $slf->{'_res'}; $dft = _conv_value($dft, $VAL_UNDEF); foreach my $itm (@tbl) { return $dft unless ref($val) eq 'HASH' && exists($val->{$itm}); $val = $val->{$itm}; } _conv_value($val, $dft, 1); } sub _conv_array { my ($val) = @_; RDA::Value->new('L', [map {_conv_value($_, $VAL_UNDEF)} @$val]); } sub _conv_hash { my ($val) = @_; RDA::Value->new('O', RDA::Object::Data->new(%$val)); } sub _conv_value { my ($val, $dft, $flg) = @_; my $ref; $ref = ref($val); ($ref eq $VALUE) ? $val : ($ref eq 'ARRAY' && $flg) ? _conv_array($val) : ($ref eq 'HASH') ? _conv_hash($val) : ($ref =~ $OBJECT) ? RDA::Value->new('O', $val) : $ref ? $dft : defined($val) ? RDA::Value->new('T', $val) : $dft; } =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'}->{'rule'}->{$rul})) ? $slf->{'_res'}->{'rule'}->{$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 ($fam, $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}); $fam = exists($tb_fam{$osn}) ? $tb_fam{$osn} : 'Unix'; foreach my $set (sort keys(%{$slf->{'_set'}})) { $rec = $slf->{'_set'}->{$set}; next unless $rec->[1] && $rec->[1] eq $typ; # Apply the platform restrictions if (defined($rec->[4])) { next unless _tst_member($rec->[4], $osn); } elsif (defined($rec->[5])) { next unless _tst_member($rec->[5], $fam); } # Add the diaglet to the list push(@tbl, $set); } } # Return the resuls @tbl; } sub _tst_member { my ($lst, $str) = @_; foreach my $itm (split(/,/, $lst)) { return 1 if $itm eq $str; } 0; } =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. It supports nested references. The following reference formats are supported: =over 20 =item B< ${nam}> Replaces the reference with the variable values. When the variable is not defined, it replaces the reference with an empty string. =item B< ${nam:dft}> Replaces the reference with the variable values. When the variable is not defined, it replaces the reference with the default text. =item B< ${nam?txt:dft}> Replaces the reference with the specified text when the variable exists. Otherwise, it replaces the reference with the default text. =back You can prefix the key by a character indicating how the variable value must be emphasized. It is not used for other replacement texts. The valid style characters are as follows: =over 6 =item S< *> for bold =item S< '> (single quote) for italic =item S< `> (back quote) for code =back It returns the resulting value. =cut sub _m_get_values { my ($slf, $blk, $str) = @_; if ($str && exists($slf->{'_ctx'})) { my $ctx = $slf->{'_ctx'}; 1 while $str =~ s/\$\{([\*\'\`])?(\w+)((\?)([^\{\}]*?))?(\:([^\{\}]*?))?\}/ _resolve($ctx, $1, $2, $4, $5, $7)/eg; $str =~ s#\$\{(\w+)(:([^\{\}]*))?\}#_get_value($ctx, $1, $3)#eg; } $str; } sub _resolve { my ($ctx, $stl, $nam, $tst, $txt, $dft) = @_; my $str; $str = RDA::Value->new('R', $ctx, '$'.$nam)->eval_value->eval_value(1); unless ($str->is_undef) { return defined($txt) ? $txt : '' if $tst; $stl = ($stl && exists($tb_stl{$stl})) ? $tb_stl{$stl} : ''; $str = $str->eval_as_string(1); $str =~ s/([\042\045\047\050\051\053\055\074\076\133\135\173-\175])/ sprintf("&#x%X;", ord($1))/ge; return $stl.$str.$stl; } 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'} = {rule => {}}; # Execute the context initialisation $blk->eval($ctx, 0, $TRC, $slf->{'_trc'}) ? 2 : 0; } =head2 S This method defines a new fact collector. =cut sub _m_set_fact { my ($slf, $ctx, $uid, $xml) = @_; my ($nam, @tbl); if ($uid && ref($xml) && (@tbl = $xml->find('sdp_command'))) { $slf->{'_col'}->{$uid} = [@tbl]; foreach my $itm ($xml->find('sdp_parameters/sdp_parameter')) { $slf->{'_map'}->{uc($nam)} = $uid if defined($nam = $itm->{'name'}) && $nam =~ m/^((\w+\.)+\w+)$/; } } 0; } =head2 S This macro assigns a result to the specified parameter. =cut sub _m_set_parameter { my ($slf, $ctx, $nam, $val) = @_; my ($key, $ptr, @tbl); @tbl = split(/\./, uc($nam)); return $val unless defined($key = pop(@tbl)); $ptr = $slf->{'_res'}; foreach my $itm (@tbl) { $ptr->{$itm} = {} unless ref($ptr->{$itm}) eq 'HASH'; $ptr = $ptr->{$itm}; } $ptr->{$key} = $val; } =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'}->{'rule'}->{$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 (@_) { next 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, $cnt, $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 = ''; $cnt = 10; 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 $rec->[0] = $fil if $rec->[1]; last; } } close(HCVE); } } 1; __END__ =head1 SEE ALSO L, L, L, L, L =head1 COPYRIGHT NOTICE Copyright (c) 2002-2009, 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