package RDA::Macro::Unix::Local; # $Id: Local.pm,v 1.5 2008/02/25 14:55:26 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/Unix/Local.pm,v 1.5 2008/02/25 14:55:26 mschenke Exp $ # # Change History # 20080225 MSC Change the copyright notice. =head1 NAME RDA::Macro::Unix::Local - Class Used for Local UNIX Macros =head1 SYNOPSIS require RDA::Macro::Unix::Local; =head1 DESCRIPTION The objects of the C class are used to interface with UNIX-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.5 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my %tb_fct = ( 'getRegValue' => [\&_m_get_reg_value, 'T'], 'grepRegValue' => [\&_m_grep_reg_value, 'L'], 'loadRegistry' => [\&_m_load_registry, 'N'], 'writeRegistry' => [\&_m_write_registry, 'N'], ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::Unix::Local-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< '_reg' > Registry data =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; # Abort if not executed in a Unix environment return unless $agt->is_unix; # Create the macro object $slf = bless {_agt => $agt}, $class; # Setup some parameters by default $slf->{'_reg'} = []; # 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 UNIX MACROS =head2 S This macro returns the value of a registry key name. It returns the default value when it does not find the registry key. =cut sub _m_get_reg_value { my ($slf, $ctx, $key, $nam, $val) = @_; my $flg; if ($key && $nam) { $key =~ s/\\/\\\\/g; $key =~ s/^HKLM\\/HKEY_LOCAL_MACHINE\\/i; for (@{$slf->{'_reg'}}) { if ($flg && m/^\s+$nam\s/i) { s/[\s\n\r]+$//; my @tbl = split(/\s+/, $_, 4); $val = $tbl[3] if defined($tbl[3]); } elsif (m/^$key$/i) { $flg = 1; } elsif (m/^[A-Z]/) { $flg = 0; } } } $val; } =head2 S This macro returns the list of registry keys that contain the specified value name. =cut sub _m_grep_reg_value { my ($slf, $ctx, $key, $nam) = @_; my ($lst, @tbl); if ($key && $nam) { $key =~ s/\\/\\\\/g; $key =~ s/^HKLM\\/HKEY_LOCAL_MACHINE\\/i; for (@{$slf->{'_reg'}}) { if (m/^\s+$nam\s/i) { push(@tbl, $lst) if $lst; } elsif (m/^$key/i) { $lst = $_; } elsif (m/^[A-Z]/) { $lst = undef; } } } @tbl; } =head2 S This macro loads the registry data from a file. =cut sub _m_load_registry { my ($slf, $ctx, $fil) = @_; my ($lin, $reg); if (open(REG, "<$fil")) { $slf->{'_reg'} = $reg = []; $lin = ''; while () { s/[\n\r]+$//; $lin .= $_; next if $lin =~ s/\\$//; if ($lin =~ m/^\[([^\]]*)\]/) { push(@$reg, "HKEY_LOCAL_MACHINE\\$1"); } elsif ($lin =~ s/\s+=\s+\((REG_[A-Z_]+)\)\s+"?/ $1 /) { $lin =~ s/"?\s*$//; push(@$reg, $lin) } $lin = ''; } close(REG); 1; } 0; } =head2 S This macro writes the registry key to the output file. The level (starting from 0) indicates the highest branch level to include in the report table of contents. By default, it takes one more than the level of the specified key. It returns the number of the lines written. =cut sub _m_write_registry { my ($slf, $ctx, $key, $lvl) = @_; my ($cnt, $flg, $hit, $nam, $ofh, $toc, $typ, $val); # Determine the indexation level $lvl = 1 + ($key =~ tr/\\/\\/) unless defined($lvl); $key =~ s/\\/\\\\/g; $key =~ s/^HKLM\\/HKEY_LOCAL_MACHINE\\/i; # Write the registry key $cnt = 0; if (scalar(@{$slf->{'_reg'}})) { # Get the output file handle $ofh = $ctx->get_handle; # Reformat the command output for (@{$slf->{'_reg'}}) { s/[\s\n\r]+$//; next if m/^$/ || m/^!/ || m/^Error:/; if (m/^[A-Z]/) { print $ofh "\n" if $flg; $flg = 0; next unless $hit = m/^$key/i; $toc = tr/\\/\\/; $toc = ($toc > $lvl) ? '+!!' : ''; print $ofh "\n---++$toc ``[$_]``\n"; ++$cnt; } elsif ($hit && m/^\s+(.*)\sREG_([A-Z_]+)\s(.*)$/) { ($nam, $typ, $val) = ($1, $2, $3); print $ofh "\n" unless $flg++; $nam = '(Default)' if $nam eq ''; if ($typ =~ m/SZ$/) { print $ofh "$nam = \"$val\"\n"; } else { $typ = lc($typ); print $ofh "$nam = $typ:$val\n"; } ++$cnt; } } close(IN); } print $ofh "\n" if $flg; # Return the number of lines written $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