package RDA::Macro::File; # $Id: File.pm,v 1.78 2008/02/25 14:27:42 mschenke Exp $ # ARCS: $Header: /home/cvs/repository/RDA_4/src/scripting/lib/RDA/Macro/File.pm,v 1.78 2008/02/25 14:27:42 mschenke Exp $ # # Change History # 20080225 MSC Improve the documentation. =head1 NAME RDA::Macro::File - Class Used for File Management Macros =head1 SYNOPSIS require RDA::Macro::File; =head1 DESCRIPTION The objects of the C class are used to interface with file management macros. The following methods are available: =cut use strict; BEGIN { use Exporter; use File::Basename; use File::Spec; use IO::File; } # Define the global public variables use vars qw($VERSION); $VERSION = sprintf("%d.%02d", q$Revision: 1.78 $ =~ /(\d+)\.(\d+)/); # Define the global private variables my @tb_bit = qw( --- --x -w- -wx r-- r-x rw- rwx --S --s -wS -ws r-S r-s rwS rws); my @tb_mon = qw(? Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); my %tb_fct = ( 'basename' => [\&_m_basename, 'T'], 'catCommand' => [\&_m_cat_command, 'T'], 'catDir' => [\&_m_cat_dir, 'T'], 'catFile' => [\&_m_cat_file, 'T'], 'checksum' => [\&_m_checksum, 'T'], 'clearLastFile' => [\&_m_clear_last, 'N'], 'countCommand' => [\&_m_count_cmd, 'L'], 'countFile' => [\&_m_count_file, 'L'], 'curDir' => [\&_m_cur_dir, 'T'], 'dirname' => [\&_m_dirname, 'T'], 'getHeader' => [\&_m_get_header, 'T'], 'getLastAccess' => [\&_m_get_atime, 'T'], 'getLastChange' => [\&_m_get_ctime, 'T'], 'getLastLength' => [\&_m_get_last_lgt, 'N'], 'getLastModify' => [\&_m_get_mtime, 'T'], 'getLength' => [\&_m_get_file_lgt, 'N'], 'getLines' => [\&_m_get_lines, 'L'], 'getOwner' => [\&_m_get_owner, 'T'], 'getSize' => [\&_m_get_size, 'N'], 'getStat' => [\&_m_get_stat, 'L'], 'getTimeout' => [\&_m_get_timeout, 'N'], 'grepCommand' => [\&_m_grep_cmd, 'L'], 'grepDir' => [\&_m_grep_dir, 'L'], 'grepFile' => [\&_m_grep_file, 'L'], 'grepLastFile' => [\&_m_grep_last, 'L'], 'isAbsolute' => [\&_m_is_absolute, 'N'], 'isNewer' => [\&_m_is_newer, 'N'], 'isOlder' => [\&_m_is_older, 'N'], 'kill' => [\&_m_kill, 'N'], 'lastCommand' => [\&_m_last_command, 'T'], 'lastDir' => [\&_m_last_dir, 'T'], 'lastFile' => [\&_m_last_file, 'T'], 'loadCommand' => [\&_m_load_cmd, 'N'], 'loadFile' => [\&_m_load_file, 'N'], 'loadString' => [\&_m_load_string, 'N'], 'purge' => [\&_m_purge, 'N'], 'sameDir' => [\&_m_same_file, 'N'], 'sameFile' => [\&_m_same_file, 'N'], 'setTimeout' => [\&_m_set_timeout, 'N'], 'sortLastFile' => [\&_m_sort_last, 'N'], 'splitDir' => [\&_m_split_dir, 'L'], 'statCommand' => [\&_m_stat_cmd, 'N'], 'statDir' => [\&_m_stat_dir, 'N'], 'statFile', => [\&_m_stat_file, 'N'], 'testCommand' => [\&_m_test_cmd, 'T'], 'testDir' => [\&_m_test_file, 'N'], 'testFile' => [\&_m_test_file, 'N'], 'umask' => [\&_m_umask, 'N'], 'upDir' => [\&_m_up_dir, 'T'], 'writeCommand' => [\&_m_write_cmd, 'N'], 'writeData' => [\&_m_write_data, 'N'], 'writeLines' => [\&_m_write_lines, 'N'], 'writeFile' => [\&_m_write_file, 'N'], 'writeFilter' => [\&_m_write_filter, 'N'], 'writeLastFile' => [\&_m_write_last, 'N'], 'writeTail' => [\&_m_write_tail, 'N'], ); my %tb_mon = ( JAN => 1, FEB => 2, MAR => 3, APR => 4, MAY => 5, JUN => 6, JUL => 7, AUG => 8, SEP => 9, OCT => 10, NOV => 11, DEC => 12, ); my %tb_srt = ( ps_time => { 'aix' => [\&_sort_ps_ms], 'darwin' => [\&_sort_ps_msc], 'dec_osf' => [\&_sort_ps_hmsc, 43], 'dynixptx' => [\&_sort_ps_ms], 'hpux' => [\&_sort_ps_hms, 33], 'linux' => [\&_sort_ps_hms, 57], 'solaris' => [\&_sort_ps_ms], '?' => [\&_sort_ps_ms], } ); my %tb_sub = ( E => 'extern', O => undef, P => 'remote', R => 'ref', S => 'sample', ); # Report the module version number sub Version { $VERSION; } =head2 S<$h = RDA::Macro::File-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< 'cnt' > Number of lines read for the last load =item B< 'err' > Last command exit code =item B< 'hdr' > First line for a grep operation =item B< 'lgt' > Number of lines read for the last command/file =item B< 'lim' > Execution time limit (in sec) =item B< 'max' > Default highest line number to write =item B< '_agt' > Reference to the agent object =item B< '_buf' > Last command/line buffer =item B< '_cks' > Checksum function =item B< '_dir' > Last directory =item B< '_fil' > Last file =item B< '_grp' > Report group =item B< '_mod' > Function to get the last modification day of a file =item B< '_not' > Statistics note =item B< '_out' > Number of operating system requests timed out =item B< '_req' > Number of operating system requests =item B< '_rpt' > Report directory =item B< '_sam' > Function to compare directory or file paths =item B< '_seq' > Temporary file sequence =item B< '_skp' > List of file tests to skip =item B< '_spl' > Function to split directories =item B< '_sta' > Function to get the file statistics =item B< '_tmp' > Temporary file radix =item B< '_vms' > VMS indicator =item B< '_win' > Windows indicator =back Internal keys are prefixed by an underscore. =cut sub new { my $slf = shift; my $class = ref($slf) || $slf; my $agt = shift; my ($fil, $grp, $rpt); # Create the macro object $slf = bless {_agt => $agt}, $class; # Setup some parameters by default $slf->{'cnt'} = 0; $slf->{'err'} = 0; $slf->{'lgt'} = 0; $slf->{'lim'} = _chk_alarm($agt->get_setting('RDA_TIMEOUT', 30)); $slf->{'max'} = $agt->get_setting('RDA_TAIL', 30000); $slf->{'_grp'} = $grp = $agt->get_setting('RPT_GROUP'); $slf->{'_req'} = $slf->{'_out'} = 0; $slf->{'_rpt'} = $rpt = $agt->get_setting('RPT_DIRECTORY', File::Spec->curdir); $slf->{'_skp'} = $agt->get_setting('RDA_BIN_CHECK',1) ? '-' : '-BT'; # Determine which functions to use $slf->{'_win'} = $^O eq 'MSWin32'; if ($slf->{'_vms'} = $agt->is_vms) { $slf->{'_mod'} = \&_mod; $slf->{'_sam'} = \&_same_i; $slf->{'_sta'} = \&_stat; $slf->{'_spl'} = \&_split_v; } else { $slf->{'_mod'} = \&_lmod; $slf->{'_sam'} = $agt->is_windows ? \&_same_i : \&_same_c; $slf->{'_sta'} = \&_lstat; $slf->{'_spl'} = $slf->{'_win'} ? \&_split_w : \&_split_u; } # Determine the request method if ($agt->get_setting('RDA_USE_TEMP',$slf->{'_win'})) { $slf->{'_seq'} = 1; $slf->{'_tmp'} = File::Spec->catfile($rpt, "tmp_$grp\_os$$\_"); # Delete previous temporary files if (opendir(DIR, $rpt)) { while (defined($fil = readdir(DIR))) { next unless $fil =~ m/^tmp_$grp\_os\d+_\d+.txt$/i; $fil = File::Spec->catfile($rpt, $fil); 1 while unlink($fil); } closedir(DIR); } } # Register the macros $agt->register($slf, [keys(%tb_fct)], qw(stat thread)); # Return the object reference $slf; } =head2 S<$h-Eclr_stats($name)> This method resets the statistics and clears corresponding module settings. =cut sub clr_stats { my ($slf, $nam) = @_; my $agt = $slf->{'_agt'}; $slf->{'_not'} = ''; $slf->{'_req'} = $slf->{'_out'} = 0; } =head2 S<$h-Eget_stats($name)> This method reports the library statistics in the specified module. =cut sub get_stats { my ($slf, $nam) = @_; if ($slf->{'_req'}) { my $agt = $slf->{'_agt'}; # Indicate the current timeout when there is no other note $slf->{'_not'} = $agt->get_setting("OS_STAT_$nam\_NOTE", 'Command execution limited to '.$slf->{'lim'}.'s') unless $slf->{'_not'} || $slf->{'lim'} <= 0; $agt->log('s', $nam, 'File', $VERSION, $slf->{'_req'}, 'N/A', $slf->{'_out'}, 'N/A', $slf->{'_not'}); # Get other macro library contributions $slf->{'_req'} += $agt->get_setting("OS_STAT_$nam\_REQ", 0); $slf->{'_out'} += $agt->get_setting("OS_STAT_$nam\_OUT", 0); # Generate the module statistics $agt->set_setting("OS_STAT_$nam\_REQ", $slf->{'_req'}, 'N', 'Number of Operating System requests'); $agt->set_setting("OS_STAT_$nam\_OUT", $slf->{'_out'}, 'N', 'Number of Operating System requests timed out'); $agt->set_setting("OS_STAT_$nam\_NOTE", $slf->{'_not'}, 'T', 'Operating System statistics note'); } } =head2 S<$h-Ereset> This method resets the object for its new environment to allow a thread-save execution. =cut sub reset { my ($slf) = @_; if (exists($slf->{'_tmp'})) { my $agt = $slf->{'_agt'}; $slf->{'_seq'} = 1; $slf->{'_tmp'} = File::Spec->catfile( $agt->get_setting('RPT_DIRECTORY', File::Spec->curdir), 'tmp_'.$agt->get_setting('RPT_GROUP')."_os$$\_" ); } } =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 FILE MACROS =head2 S This macro extracts the base name of the file specification and removes the suffix when it belongs to the suffix list. Each element of this list is interpreted as a regular expression. It is matched against the end of the name. When opening existing files from systems that are not case-sensitive, the pattern matching for suffix removal ignores case. =cut sub _m_basename { my $slf = shift; my $ctx = shift; basename(@_); } =head2 S This macro concatenates one or more directory names and a file name to form a complete path ending with a file name. It returns the name quoted as appropriate to execute it in a shell. =cut sub _m_cat_command { my $slf = shift; my $ctx = shift; $slf->{'_agt'}->quote( $slf->{'_fil'} = (scalar(@_) > 0) ? File::Spec->catfile(@_) : undef); } =head2 S This macro concatenates two or more directory names to form a complete path ending with a directory. It removes the trailing slash from the resulting string. If it is the root directory, then the trailing slash is not removed. =cut sub _m_cat_dir { my $slf = shift; my $ctx = shift; $slf->{'_dir'} = (scalar(@_) > 0) ? File::Spec->catdir(@_) : undef; } =head2 S This macro concatenates one or more directory names and a file name to form a complete path ending with a file name. =cut sub _m_cat_file { my $slf = shift; my $ctx = shift; $slf->{'_fil'} = (scalar(@_) > 0) ? File::Spec->catfile(@_) : undef; } =head2 S This macro returns the checksum of the file. Based on available tools, it can use a MD5 digest, the C command, or an internal basic checksum. It returns an undefined value in case of problems. =cut sub _m_checksum { my ($slf, $ctx, $fil) = @_; # Identify the checksum approach on first usage unless (exists($slf->{'_cks'})) { $slf->{'_cks'} = \&_get_sum; # Check the availability of the MD5 package eval { require Digest::MD5; $slf->{'_cks'} = \&_get_md5; }; # Check the availability of the cksum command if ($@) { my %tbl = ( '/bin/cksum' => \&_get_cksum1, '/usr/bin/cksum' => \&_get_cksum2, ); foreach my $pgm (keys(%tbl)) { `$pgm rda.sh`; next if $?; $slf->{'_cks'} = $tbl{$pgm}; last; } } } # Compute the checksum ($fil && -f $fil && -r $fil) ? &{$slf->{'_cks'}}($fil) : undef; } sub _get_cksum1 { my ($fil) = @_; my ($lin); ($lin) = `/bin/cksum "$fil"`; ($lin =~ m/(\d+)/) ? $1 : undef; } sub _get_cksum2 { my ($fil) = @_; my ($lin); ($lin) = `/usr/bin/cksum "$fil"`; ($lin =~ m/(\d+)/) ? $1 : undef; } sub _get_md5 { my ($fil) = @_; my ($ifh, $val); if (($ifh = IO::File->new)->open("<$fil")) { binmode($ifh); $val = Digest::MD5->new->addfile($ifh)->hexdigest; $ifh->close; } $val; } sub _get_sum { my ($fil) = @_; my ($buf, $ifh, $sum); if (($ifh = IO::File->new)->open("<$fil")) { binmode($ifh); $sum = 0; while ($ifh->sysread($buf, 1024)) { $sum = (($sum + (unpack('%32C*', $buf) << 12) + unpack('%32B*', $buf)) ^ ($sum << 8)) & 0xffffffff; } $ifh->close; } $sum; } =head2 S This macro clears the information about the last command/file results. =cut sub _m_clear_last { my ($slf) = @_; delete($slf->{'_buf'}); $slf->{'cnt'} = $slf->{'err'} = 0; } =head2 S This macro returns the number of lines in the specified command. You can also search additional regular expressions. It returns a list containing the respective counters. =head2 S This macro returns the number of lines in the specified file. You can also search additional regular expressions. It returns a list containing the respective counters. =cut sub _m_count_cmd { my $slf = shift; my $ctx = shift; my $cmd = shift; my ($err, $pid, $tmp, @tbl); ++$slf->{'_req'}; # When requested, treat the command output through a temporary file if (exists($slf->{'_tmp'})) { ($err, undef, $tmp) = _get_output($slf, $ctx, $cmd, 1); if ($tmp && open(IN, "<$tmp")) { @tbl = _count_in($slf, $ctx, 0, 0, 0, $err, @_); 1 while unlink($tmp); ++($slf->{'_seq'}) if -e $tmp; } else { $slf->{'err'} = $err; } return @tbl; } # Treat the output on the fly local $SIG{'__WARN__'} = sub { }; return () unless ($cmd = _conv_cmd($slf, $cmd)) && ($pid = open(IN, "$cmd |")); _count_in($slf, $ctx, $cmd, $slf->{'lim'}, $pid, 0, @_) } sub _m_count_file { my $slf = shift; my $ctx = shift; my $fil = shift; return () unless $fil && open(IN, "<$fil"); _count_in($slf, $ctx, 0, 0, 0, 0, @_); } sub _count_in { my ($slf, $ctx, $cmd, $lim, $pid, $err, @arg) = @_; my ($off, $sta, @tb_cnt, @tb_re); $slf->{'err'} = $err; eval { local $SIG{'ALRM'} = sub { die "Alarm\n"; } if $lim; # Get the regular expressions push(@tb_cnt, 0); foreach my $str (@arg) { next unless defined($str); push(@tb_re, qr#$str#); push(@tb_cnt, 0); } # Scan the input alarm($lim) if $lim; while () { ++$tb_cnt[$off = 0]; s/^\000+$//; foreach my $re (@tb_re) { ++$off; ++$tb_cnt[$off] if $_ =~ $re; } } alarm(0) if $lim; }; kill(9, $pid) if ($sta = $@) && $pid; close(IN); if ($sta) { _log_timeout($slf, $ctx, $cmd); } elsif ($cmd) { $slf->{'err'} = $?; } $slf->{'lgt'} = $tb_cnt[0]; # Return the counter array @tb_cnt; } =head2 S This macro returns a string representation of the current directory. =cut sub _m_cur_dir { File::Spec->curdir; } =head2 S This macro returns the directory portion of the input file specification. =cut sub _m_dirname { my $slf = shift; my $ctx = shift; dirname(@_); } =head2 S This macro returns the first line of the last C or C. =cut sub _m_get_header { my ($slf, $ctx, $str) = @_; $str = $slf->{'hdr'} if exists($slf->{'hdr'}); $str; } =head2 S This macro returns the last access time. Unless a format is specified, it returns the number of seconds since the epoch. =cut sub _m_get_atime { my ($slf, $ctx, $fil, $fmt) = @_; my $tim = (stat($fil))[8]; defined($tim) ? _fmt_time($tim, $fmt) : $tim; } sub _fmt_time { my ($tim, $fmt) = @_; if ($tim && $fmt) { eval { require POSIX; $tim = &POSIX::strftime($fmt, gmtime($tim)); }; return $tim unless $@; } $tim; } =head2 S This macro returns the inode change time. Unless a format is specified, it returns the number of seconds since the epoch. =cut sub _m_get_ctime { my ($slf, $ctx, $fil, $fmt) = @_; my $tim = (stat($fil))[10]; defined($tim) ? _fmt_time($tim, $fmt) : $tim; } =head2 S This macro returns the number of lines in the last file/command buffer. =cut sub _m_get_last_lgt { shift->{'cnt'}; } =head2 S This macro returns the last modify time. Unless a format is specified, it returns the number of seconds since the epoch. =cut sub _m_get_mtime { my ($slf, $ctx, $fil, $fmt) = @_; my $tim = (stat($fil))[9]; defined($tim) ? _fmt_time($tim, $fmt) : $tim; } =head2 S This macro returns the number of lines read for the last file operation (cf. C, C, C macros). =cut sub _m_get_file_lgt { shift->{'lgt'}; } =head2 S This macro returns a range of the lines stored in the last file/command buffer. It assumes the first and last line as the default for the range definition. You can use negative line numbers to specify lines from the buffer end. =cut sub _m_get_lines { my ($slf, $ctx, $min, $max) = @_; my $buf; return () unless exists($slf->{'_buf'}); # Validate the range $buf = $slf->{'_buf'}; $min = (!defined($min) || ($#$buf + $min) < -1) ? 0 : ($min < 0) ? $#$buf + $min + 1 : $min; $max = (!defined($max)) ? $#$buf : (($#$buf + $max) < -1) ? 0 : ($max < 0) ? $#$buf + $max + 1 : ($max > $#$buf) ? $#$buf : $max; # Return the line range @$buf[$min..$max]; } =head2 S This macro returns the user identifier of the file owner or an undefined value in case of error. =cut sub _m_get_owner { my ($slf, $ctx, $fil) = @_; (stat($fil))[4]; } =head2 S This macro returns the total size of the file, in bytes, or an undefined value in case of error. =cut sub _m_get_size { my ($slf, $ctx, $fil) = @_; (stat($fil))[7]; } =head2 S This macro returns the status information of the file, in bytes, or an empty list in case of error. The following fields are present: =over 8 =item B< 0 > Device number of file system =item B< 1 > Inode number =item B< 2 > File mode (type and permissions) =item B< 3 > Number of (hard) links to the file =item B< 4 > User identifier of file owner =item B< 5 > Group identifier of file owner =item B< 6 > Device identifier (special files only) =item B< 7 > Total size of file (in bytes) =item B< 8 > Last access time in seconds since the epoch =item B< 9 > Last modify time in seconds since the epoch =item B< 10 > Inode change time in seconds since the epoch =item B< 11 > Preferred block size for file system I/O =item B< 12 > Actual number of blocks allocated =back Their effective use depends on file systems. =cut sub _m_get_stat { my ($slf, $ctx, $fil) = @_; stat($fil); } =head2 S This macro returns the current duration of the timeout for executing operating system commands or 0 when this mechanism is disabled. =cut sub _m_get_timeout { shift->{'lim'}; } =head2 S This macro returns the files that correspond to the specified pattern from the specified directory. It supports the following options: =over 12 =item B< 'd' > Sorts the files per directory, then by name =item B< 'i' > Ignores case distinctions in both the pattern and the file =item B< 'n' > Sorts the files by name =item B< 'm(n)' > Keeps files modified during last (n) days only =item B< 'p' > Returns the full file path =item B< 'r' > Reads files under each directory recursively (implies 'p') =item B< 't' > Sorts the files by modification time =item B< 'v' > Inverts the sense of matching to select non-matching files =back The depth argument controls how far you can descend in the sub directories. It is limited to 8 levels by default. =cut sub _m_grep_dir { my ($slf, $ctx, $dir, $re, $opt, $max) = @_; my ($fct, $flg, $f_m, $f_p, $f_r, $f_v, @tbl, %mod); # Abort if we can access to that directory return @tbl unless $dir && opendir(DIR, $dir); # Decode the options $opt = '' unless defined($opt); $re = (index($opt, 'i') < 0) ? qr#$re# : qr#$re#i; $f_m = ($opt =~ m/m(\d+)/) ? (time - 86400 * $1) : 0; $f_p = index($opt, 'p') >= 0; $f_r = index($opt, 'r') >= 0; $f_v = index($opt, 'v') >= 0; $f_p = 1 if $f_r; $max = 8 unless defined($max) && $max >= 0; # Read the directory content $fct = $slf->{'_mod'}; _grep_dir(\@tbl, $dir, $re, 0, $max, $fct, $f_m, $f_p, $f_r, $f_v); # Sort the file names when requested if (index($opt, 'd') >= 0) { return sort {dirname($a) cmp dirname($b) || basename($a) cmp basename($b)} @tbl; } if (index($opt, 'n') >= 0) { return sort {$a cmp $b} @tbl; } if (index($opt, 't') >= 0) { %mod = map {$_ => &$fct($f_p ? $_ : File::Spec->catfile($dir, $_), 0)} @tbl; return sort {$mod{$b} <=> $mod{$a} || $a cmp $b} keys(%mod); } # Return the list of the files @tbl; } sub _grep_dir { my ($tbl, $dir, $re, $lvl, $max, $fct, $f_m, $f_p, $f_r, $f_v) = @_; my ($flg, $pth, @sub); # Read the directory content foreach my $nam (readdir(DIR)) { $flg = $f_v ? ($nam !~ $re) : ($nam =~ $re); $pth = File::Spec->catfile($dir, $nam) if $f_m || $f_p; $flg = (&$fct($pth, 0) > $f_m) if $flg && $f_m; push(@$tbl, ($f_p ? $pth : $nam)) if $flg; push(@sub, $pth) if $f_r && -d $pth && -r $pth && $nam !~ m/^\.+$/; } closedir(DIR); # Explore sub directories unless (++$lvl > $max) { foreach my $sub (@sub) { _grep_dir($tbl, $sub, $re, $lvl, $max, $fct, $f_m, $f_p, $f_r, $f_v) if opendir(DIR, $sub); } } } =head2 S This macro returns the command output lines that match the regular expression. =head2 S This macro returns the file lines that match the regular expression. The following options are supported: =over 9 =item B< 'c' > Returns the match count instead of the match list =item B< 'f' > Stops scanning on the first match =item B< 'i' > Ignores case distinctions in both the pattern and the line =item B< 'n' > Prefixes lines with a line number =item B< 'v' > Inverts the sense of matching to select non matching lines =back You can limit the number of matched lines to the specified number. When the number is positive, it returns the first matches only. When it is negative, it returns the last matches only. You can restrict the search to a line range. When the file is read completely, the macro stores the number of lines contained in the file and this number is accessible by the C macro. =cut sub _m_grep_cmd { my ($slf, $ctx, $cmd, $re, @arg) = @_; my ($err, $pid, $tmp, @tbl); ++$slf->{'_req'}; # When requested, treat the command output through a temporary file if (exists($slf->{'_tmp'})) { ($err, undef, $tmp) = _get_output($slf, $ctx, $cmd, 1); if ($tmp && open(IN, "<$tmp")) { @tbl = _grep_in($slf, $ctx, 0, 0, 0, $err, $re, @arg); 1 while unlink($tmp); ++($slf->{'_seq'}) if -e $tmp; } else { $slf->{'err'} = $err; } return @tbl; } # Treat the command output on the fly local $SIG{'__WARN__'} = sub { }; return () unless ($cmd = _conv_cmd($slf, $cmd)) && $re && ($pid = open(IN, "$cmd |")); _grep_in($slf, $ctx, $cmd, $slf->{'lim'}, $pid, 0, $re, @arg) } sub _m_grep_file { my ($slf, $ctx, $fil, $re, @arg) = @_; return () unless $fil && $re && open(IN, "<$fil"); _grep_in($slf, $ctx, 0, 0, 0, 0, $re, @arg); } sub _grep_in { my ($slf, $ctx, $cmd, $lim, $pid, $err, $re, $opt, $lgt, $min, $max) = @_; my ($all, $beg, $cnt, $end, $flg, $f_c, $f_i, $f_n, $sta, @tbl); # Determine the options $min = 0 unless $min && $min > 0; $max = 0 unless $max && $max > 0; $opt = '' unless defined($opt); $re = (index($opt, 'i') < 0) ? qr#$re# : qr#$re#i; $f_c = index($opt, 'c') >= 0; $f_i = index($opt, 'v') >= 0; $f_n = index($opt, 'n') >= 0; # Check the file $slf->{'err'} = $err; eval { local $SIG{'ALRM'} = sub { die "Alarm\n"; } if $lim; # Restrict the number of records returned $beg = $end = $cnt = 0; if ($lgt) { $beg = $lgt if $lgt > 0; $end = -$lgt if $lgt < 0; } elsif (index($opt, 'f') >= 0) { $beg = 1; } # Scan the file $all = 1; $tbl[0] = 0 if $f_c; delete($slf->{'hdr'}); alarm($lim) if $lim; while () { s/[\r\n]+$//; s/^\000+$//; $slf->{'hdr'} = $_ unless $cnt++; next if $cnt < $min; $all = 0; last if $max && $cnt > $max; $flg = ($_ =~ $re); if ($f_i ? !$flg : $flg) { if ($f_c) { ++$tbl[0]; } else { push(@tbl, $f_n ? "$cnt:$_" : $_); last if $beg && scalar(@tbl) == $beg; shift(@tbl) if $end && scalar(@tbl) > $end; } } $all = 1; } alarm(0) if $lim; }; kill(9, $pid) if ($sta = $@) && $pid; close(IN); if ($sta) { _log_timeout($slf, $ctx, $cmd); } elsif ($cmd) { $slf->{'err'} = $?; } $slf->{'lgt'} = $cnt if $all; # Return the matches @tbl; } =head2 S This macro returns the lines in the last file/command buffer that match the regular expression. It supports the same options as C. =cut sub _m_grep_last { my ($slf, $ctx, $re, $opt, $lgt, $min, $max) = @_; my ($beg, $cnt, $end, $flg, $f_c, $f_i, $f_n, @tbl); if (exists($slf->{'_buf'}) && $re) { # Determine the options $min = 0 unless $min && $min > 0; $max = 0 unless $max && $max > 0; $opt = '' unless defined($opt); $re = (index($opt, 'i') < 0) ? qr#$re# : qr#$re#i; $f_c = index($opt, 'c') >= 0; $f_i = index($opt, 'v') >= 0; $f_n = index($opt, 'n') >= 0; # Restrict the number of records returned $beg = $end = 0; if ($lgt) { $beg = $lgt if $lgt > 0; $end = -$lgt if $lgt < 0; } elsif (index($opt, 'f') >= 0) { $beg = 1; } # Check the file $tbl[0] = 0 if $f_c; $cnt = 0; foreach my $lin (@{$slf->{'_buf'}}) { ++$cnt; next if $cnt < $min; last if $max && $cnt > $max; $flg = ($lin =~ $re); if ($f_i ? !$flg : $flg) { if ($f_c) { ++$tbl[0]; } else { push(@tbl, $f_n ? "$cnt:$lin" : $lin); last if $beg && scalar(@tbl) == $beg; shift(@tbl) if $end && scalar(@tbl) > $end; } } } } @tbl; } =head2 S This macro indicates if the specified argument is an absolute path. =cut sub _m_is_absolute { my ($slf, $ctx, $fil) = @_; File::Spec->file_name_is_absolute($fil); } =head2 S This macro indicates if the file is newer than the specified period. It returns a false value if it cannot obtain the status information of the file. =cut sub _m_is_newer { my ($slf, $ctx, $fil, $day, $sec) = @_; my ($ref, @sta); # Get the reference time $day = 0 unless defined($day); $sec = 0 unless defined($sec); $ref = time - $day * 86400 - $sec; # Check the modification time of the file &{$slf->{'_mod'}}($fil, 0) > $ref; } =head2 S This macro indicates if the file is older than the specified period. It returns a false value if it cannot obtain the status information of the file. =cut sub _m_is_older { my ($slf, $ctx, $fil, $day, $sec) = @_; my ($ref, @sta); # Get the reference time $day = 0 unless defined($day); $sec = 0 unless defined($sec); $ref = time - $day * 86400 - $sec; # Check the modification time of the file &{$slf->{'_mod'}}($fil, $ref) < $ref; } =head2 S This macro sends a signal to a list of processes. It returns the number of processes successfully signaled, which is not necessarily the same as the number of processes killed. If the signal number is zero, then no signal is sent to the process. This is a useful way to check that a child process is alive and has not changed its user identifier. If the signal number is negative, then it kills process groups instead of processes. =cut sub _m_kill { my ($slf, $ctx, $sig, @pid) = @_; kill($sig, @pid); } =head2 S This macro returns the last file produced by the C or C macro, quoted as appropriate to execute it in a shell. =cut sub _m_last_command { my $slf = shift; $slf->{'_agt'}->quote($slf->{'_fil'}); } =head2 S This macro returns the last directory produced by the C macro. =cut sub _m_last_dir { shift->{'_dir'}; } =head2 S This macro returns the last file produced by the C or C macro. =cut sub _m_last_file { shift->{'_fil'}; } =head2 S This macro loads the result of the specified command. It returns 1 for a successful load. Otherwise, it returns 0. It saves the effective command exit code and it is accessible through the C macro. When the flag is set, the load is considered successful regardless of the exit code. It is possible to increase the execution limit by specifying an increasing factor as an argument. A negative or null (zero) value disables any timeout. It is possible also to limit the number of lines loaded to the specified number. When the number is positive, it loads the first lines only. When the number is negative, it loads the last lines only. =cut sub _m_load_cmd { my ($slf, $ctx, $cmd, $flg, $inc, $lgt) = @_; $slf->{'err'} = 0; ++$slf->{'_req'}; if (exists($slf->{'_tmp'})) { my ($err, $ret, $sta, $tmp); # Write the command output using a temporary file ($err, $sta, $tmp) = _get_output($slf, $ctx, $cmd, $inc); $ret = _m_load_file($slf, $ctx, $tmp, $lgt) if $tmp; $slf->{'err'} = $err; # Remove the temporary file 1 while unlink($tmp); ++($slf->{'_seq'}) if -e $tmp; # Indicate the successful completion unless ($sta || !$tmp) { return ($flg || $? == 0) ? $ret : 0; } } else { my ($beg, $buf, $cnt, $end, $err, $lim, $pid); local $SIG{'__WARN__'} = sub { }; if (($cmd = _conv_cmd($slf, $cmd)) && ($pid = open(IN, "$cmd |"))) { eval { $lim = $slf->_get_alarm($inc); local $SIG{'ALRM'} = sub { die "Alarm\n"; } if $lim; # Restrict the number of lines loaded $beg = $end = $cnt = 0; if ($lgt) { $beg = $lgt if $lgt > 0; $end = -$lgt if $lgt < 0; } # Load the command result, taking care on end of lines $slf->{'_buf'} = $buf = []; alarm($lim) if $lim; while () { s/[\r\n]+$//; s/^\000+$//; ++$cnt; push(@$buf, $_); last if $beg && scalar(@$buf) == $beg; if ($end && scalar(@$buf) > $end) { shift(@$buf); --$cnt; } } alarm(0) if $lim; }; kill(9, $pid) if ($err = $@) && $pid; close(IN); $slf->{'err'} = ($? == 13) ? 0 : $?; $slf->{'cnt'} = $cnt; unless ($err) { return ($flg || $slf->{'err'} == 0) ? 1 : 0; } _log_timeout($slf, $ctx, $cmd); } } # Indicate the error delete($slf->{'_buf'}); $slf->{'cnt'} = 0; } =head2 S This macro loads the content of the file. It returns 1 for a successful completion. Otherwise, it returns 0. It is also possible to limit the number of lines loaded to the specified number. When the number is positive, it loads the first lines only. When the number is negative, it loads the last lines only. =cut sub _m_load_file { my ($slf, $ctx, $fil, $lgt) = @_; my ($beg, $buf, $cnt, $end); $slf->{'err'} = 0; if ($fil && open(IN, "<$fil")) { # Restrict the number of lines loaded $beg = $end = 0; if ($lgt) { $beg = $lgt if $lgt > 0; $end = -$lgt if $lgt < 0; } # Load the file, taking care on end of lines $slf->{'_buf'} = $buf = []; $cnt = 0; while () { s/[\r\n]+$//; s/^\000+$//; ++$cnt; push(@$buf, $_); last if $beg && scalar(@$buf) == $beg; if ($end && scalar(@$buf) > $end) { shift(@$buf); --$cnt; } } close(IN); $slf->{'cnt'} = $cnt; # Indicate the successful completion return 1; } # Indicate the error delete($slf->{'_buf'}); $slf->{'cnt'} = 0; } =head2 S This macro loads the character sequences that match the pattern. It returns 1 for a successful completion. Otherwise, it returns 0. =cut sub _m_load_string { my ($slf, $ctx, $fil, $re) = @_; my ($buf, $nxt); $slf->{'cnt'} = $slf->{'err'} = 0; if ($re && $fil && open(IN, "<$fil")) { $slf->{'_buf'} = []; binmode(IN); for ($buf = '' ; sysread(IN, $buf, 65536, length($buf)) ; $buf = $nxt) { $nxt = ($buf =~ s/([\040-\176]+)$//) ? $1 : ''; foreach my $str ($buf =~ m#($re)#g) { push(@{$slf->{'_buf'}}, $str); ++$slf->{'cnt'}; } } if ($nxt =~ $re) { push(@{$slf->{'_buf'}}, $nxt); ++$slf->{'cnt'}; } close(IN); # Indicate the successful completion return 1; } # Indicate the error delete($slf->{'_buf'}); 0; } =head2 S This macro removes all files that match the regular expression and that are older than the specified age from the specified report sub directory. The regular expression is automatically prefixed with the report group. Valid sub directory types are as follows: =over 8 =item B< E > For the C sub directory =item B< O > For the output/report directory itself =item B< P > For the remote packages (C sub directory) =item B< R > For the C sub directory =item B< S > For the C sub directory =back When the flag is set, it creates a missing directory. It returns the number of removed files. =cut sub _m_purge { my ($slf, $ctx, $typ, $re, $day, $sec, $flg) = @_; my ($cnt, $dir, $fil, $ref); $cnt = 0; if ($typ && $re && defined($day) && exists($tb_sub{$typ})) { $dir = $slf->{'_rpt'}; $dir = File::Spec->catdir($dir, $tb_sub{$typ}) if $tb_sub{$typ}; $sec = 0 unless defined($sec); $ref = time - $day * 86400 - $sec; $re = qr#^$slf->{'_grp'}_$re#i; if (opendir(DIR, $dir)) { foreach my $nam (readdir(DIR)) { next unless $nam =~ $re; $fil = File::Spec->catfile($dir, $nam); next unless &{$slf->{'_mod'}}($fil, $ref) < $ref; ++$cnt while unlink($fil); } closedir(DIR); } elsif ($flg) { mkdir($dir,0770) unless -d $dir; } } $cnt; } =head2 S or S This macro indicates whether two directory or file paths are identical. On VMS and on Windows, it ignores case differences. No path cleanup is performed. =cut sub _m_same_file { my ($slf, $ctx, $fil1, $fil2) = @_; $fil1 && $fil2 && &{$slf->{'_sam'}}($fil1, $fil2); } sub _same_c { my ($fil1, $fil2) = @_; $fil1 eq $fil2; } sub _same_i { my ($fil1, $fil2) = @_; $fil1 eq $fil2 || lc($fil1) eq lc($fil2); } =head2 S This macro sets the timeout for executing operating system commands, specified in seconds, only if the value is greater than zero. Otherwise, the timeout mechanism is disabled. It is disabled also if the alarm function is not implemented. It returns the effective value. =cut sub _m_set_timeout { my ($slf, $ctx, $val) = @_; $slf->{'lim'} = _chk_alarm($val); } =head2 S This macro sorts the last file buffer according to the specified criteria. It returns the number of records on successful completion. Otherwise, it returns 0. The following sort types are supported: =over 12 =item B< ps_time> Sorts the C lines by decreasing CPU time. =back It ignores empty lines. Lines that do not contain the sort field are placed at the top of the list. =cut sub _m_sort_last { my ($slf, $ctx, $typ) = @_; my ($fct, $key, $new, $off, $rec, @tbl); if (exists($slf->{'_buf'}) && $typ && exists($tb_srt{$typ})) { # Get the sort key function key $new = []; $rec = $tb_srt{$typ}; $rec = $rec->{exists($rec->{$^O}) ? $^O : '*'}; $fct = $rec->[0]; $off = $rec->[1]; # Create the sort key foreach my $lin (@{$slf->{'_buf'}}) { if (defined($key = &$fct($lin, $off))) { push(@tbl, [$key, $lin]); } elsif ($lin !~ m/^\s*$/) { push(@$new, $lin); } } # Sort the records foreach my $rec (sort {$b->[0] <=> $a->[0]} @tbl) { push(@$new, $rec->[1]); } $slf->{'_buf'} = $new; # Return the number of records return scalar(@$new); } 0; } sub _sort_ps_hms { my ($lin, $off) = @_; $lin = substr($lin, $off); return undef unless $lin =~ m/\s(((\d+)-)?(\d+)\:)?(\d+)\:(\d+)\s/; my $tps = $5 * 60 + $6; $tps += $4 * 3600 if $4; $tps += $3 * 86400 if $3; $tps; } sub _sort_ps_hmsc { my ($lin, $off) = @_; # Possible formats: # 2-16:17:48 # 04:00:14 # 0:01.31 $lin = substr($lin, $off); return undef unless $lin =~ m/\s(((\d+)-)?(\d+)\:)?(\d+)\:(\d+)(\.(\d+))?\s/; my $tps = $5 * 60 + $6; $tps += $4 * 3600 if $4; $tps += $3 * 86400 if $3; $tps += $8 / 100 if $8; $tps; } sub _sort_ps_ms { my ($lin) = @_; ($lin =~ m/\s(\d+)\:(\d+)\s/) ? ($1 * 60 + $2) : undef; } sub _sort_ps_msc { my ($lin) = @_; ($lin =~ m/\s(\d+)\:(\d+\.\d+)\s/) ? ($1 * 60 + $2) : undef; } =head2 S This macro splits the directory in its components. When a base directory is specified, only the relative part is treated. =cut sub _m_split_dir { my ($slf, $ctx, $dir, $bas) = @_; &{$slf->{'_spl'}}($dir, $bas) if $dir; } sub _split_u { my ($dir, $bas) = @_; $dir =~ s#/*$#/#; if ($bas) { $bas =~ s#/*$#/#; $dir =~ s#^\Q$bas\E##; } $dir =~ s#/$##; split(/\//, $dir); } sub _split_v { my ($dir, $bas) = @_; ($dir); } sub _split_w { my ($dir, $bas) = @_; $dir =~ s#\\*$#\\#; if ($bas) { $bas =~ s#\\*$#\\#; $dir =~ s#^\Q$bas\E##i; } $dir =~ s#\\$##; split(/\\/, $dir); } =head2 S This macro returns the exit code of the last command. It clears this information when the last command information is changed. =cut sub _m_stat_cmd { shift->{'err'}; } =head2 S This macro writes the content of a directory with the status information of each file into the output file. The supported options are as follows: =over 9 =item B< 'a' > Does not hide entries starting with C<.> =item B< 'n' > Sorts by name (ascending) =item B< 't' > Sorts by modification time (descending) =back It returns the number of files displayed, or 0 if the directory cannot be opened, or -1 if no files are displayed. =cut sub _m_stat_dir { my ($slf, $ctx, $opt, $dir) = @_; my ($all, $fct, $max, $sta, @tbl); # Abort if we can access to that directory return 0 unless $dir && -d $dir; # Decode the options $opt = '' unless defined($opt); $all = index($opt, 'a') >= 0; $fct = (index($opt, 't') >= 0) ? 't' : (index($opt, 'n') < 0) ? '-' : $slf->{'_vms'} ? 'v' : 'n'; # Read the directory content $max = [0, 0, 0, 0, 0]; if($slf->{'_vms'}) { _get_vms_stat(\@tbl, $dir, 1, $max) } else { return 0 unless opendir(DIR, $dir); $sta = $slf->{'_sta'}; foreach my $nam (readdir(DIR)) { _get_stat(\@tbl, $sta, File::Spec->catfile($dir, $nam), $nam, $max) if $all || $nam !~ m/^\./; } closedir(DIR); } # Produce the directory listing return -1 unless scalar(@tbl); _write_stat($ctx, \@tbl, $max, $fct, $dir); } =head2 S This macro reports the status information of the specified files. The supported options are as follows: =over 9 =item B< 'b' > Displays the basename of the file only (default) =item B< 'p' > Keeps the full path of the file =back It returns the number of files that have been successfully treated. =cut sub _m_stat_file { my $slf = shift; my $ctx = shift; my $opt = shift; my ($all, $fct, $flg, $max, $sta, $vms, @fil, @tbl); # Decode the options $opt = 'b' unless defined($opt); $flg = index($opt, 'p') < 0; $vms = $slf->{'_vms'}; # Get the status information $max = [0, 0, 0, 0, 0]; $sta = $slf->{'_sta'}; foreach my $fil (@_) { if ($vms) { _get_vms_stat(\@tbl, $fil, $flg, $max); } else { _get_stat(\@tbl, $sta, $fil, ($flg ? basename($fil) : $fil), $max); } } # Write the status information in the output file return 0 unless scalar(@tbl); _write_stat($ctx, \@tbl, $max, ''); } =head2 S This macro returns the first command from the list that executes successfully. Otherwise, it returns an undefined value. Specified commands should complete their execution quickly and produce less output. =cut sub _m_test_cmd { my $slf = shift; my $ctx = shift; my ($cmd, $sta); foreach my $arg (@_) { eval { local $SIG{'__WARN__'} = sub { }; local $SIG{'PIPE'} = 'IGNORE'; $cmd = _conv_cmd($slf, "$arg 2>&1"); open(PIPE, "$cmd |") || die "Bad open\n"; while () { ; # Need a loop to prevent pipe errors on platforms like AIX } close(PIPE) || die "Bad close\n";; }; return $arg unless $@ || $?; } undef; } =head2 S This macro applies one or more tests on the specified file or directory. Possible tests are as follows: =over 10 =item B< 'r' > File is readable by effective uid/gid =item B< 'w' > File is writable by effective uid/gid =item B< 'x' > File is executable by effective uid/gid =item B< 'o' > File is owned by effective uid =item B< 'e' > File exists =item B< 'z' > File has zero size (is empty) =item B< 's' > File has non zero size =item B< 'f' > File is a plain file =item B< 'd' > File is a directory =item B< 'l' > File is a symbolic link =item B< 't' > File handle is opened to a tty =item B< 'T' > File is an ASCII text file (heuristic guess) =item B< 'B' > File is a binary file (opposite of C) =back It returns 1 if all specified tests are successful. Otherwise, it returns 0. =cut sub _m_test_file { my ($slf, $ctx, $opt, $fil) = @_; my ($flg, $skp); $opt = '' unless defined($opt); $skp = $slf->{'_skp'}; return 0 unless $fil; $fil =~ s/\\+$/\\\\/; foreach my $tst (split(//, $opt)) { next if index($skp, $tst) >= 0; $flg = index('deflorstwxzBT', $tst) >= 0 && eval "-$tst '$fil'"; return 0 if !$flg || $@; } 1; } =head2 S This macro sets the umask for the process to the specified value and returns the previous value. To specify an octal representation, you can use a string starting with a zero. If the argument is omitted, then it returns the current umask. =cut sub _m_umask { my ($slf, $ctx, $msk) = @_; return umask unless defined($msk); $msk = oct($msk) if $msk =~ m/^0/; umask($msk); } =head2 S This macro returns a string representation of the parent directory. =cut sub _m_up_dir { File::Spec->updir; } =head2 S This macro writes the result of the specified command in the output file. It returns 1 for successful completion. Otherwise, it returns 0. It stores the effective command exit code. This code is accessible through the C macro. When the flag is set, the write is considered successful regardless of the exit code. It is possible to increase the execution limit by specifying an increasing factor as an argument. A non positive value disables any timeout. =cut sub _m_write_cmd { my ($slf, $ctx, $cmd, $flg, $inc) = @_; $slf->{'err'} = 0; ++$slf->{'_req'}; if (exists($slf->{'_tmp'})) { my ($err, $old, $ret, $sta, $tmp); # Write the command output using a temporary file ($err, $sta, $tmp) = _get_output($slf, $ctx, $cmd, $inc); if ($tmp && -s $tmp) { $old = $slf->{'lgt'}; $ret = _m_write_file($slf, $ctx, $tmp); $slf->{'lgt'} = $old; } else { $ret = $tmp ? 1 : 0; } $slf->{'err'} = $err; # Remove the temporary file 1 while unlink($tmp); ++($slf->{'_seq'}) if -e $tmp; # Indicate the successful completion unless ($sta) { return ($flg || $err == 0) ? $ret : 0; } } else { my ($err, $lim, $ofh, $pid); local $SIG{'__WARN__'} = sub { }; if (($cmd = _conv_cmd($slf, $cmd)) && ($pid = open(IN, "$cmd |"))) { eval { $lim = $slf->_get_alarm($inc); local $SIG{'ALRM'} = sub { die "Alarm\n"; } if $lim; # Load the command result, taking care on end of lines alarm($lim) if $lim; while () { s/[\r\n]+$//; s/^\000+$//; unless ($ofh) { $lim = alarm(0) + 1 if $lim; $ofh = $ctx->get_handle; print $ofh "\n"; alarm($lim) if $lim; } print $ofh "$_\n"; } alarm(0) if $lim; }; kill(9, $pid) if ($err = $@) && $pid; close(IN); $slf->{'err'} = $?; print $ofh "\n" if $ofh; # Indicate the successful completion unless ($err) { return ($flg || $? == 0) ? 1 : 0; } _log_timeout($slf, $ctx, $cmd); } } # Indicate the error 0; } =head2 S This macro writes the content of the file in the output file without any transformation. It returns 1 for a successful completion. Otherwise, it returns 0. =cut sub _m_write_data { my ($slf, $ctx, $fil) = @_; my ($buf, $lgt, $ofh); if ($fil && open(IN, "<$fil")) { # Get the output file handle $ofh = $ctx->get_handle; # Write the file to the output file without any transformation $ofh->flush; binmode(IN); while ($lgt = sysread(IN, $buf, 4096)) { $ofh->write($buf, $lgt); } close(IN); # Indicate the successful completion return 1; } 0; } =head2 S This macro writes the content of the file in the output file. It returns 1 for a successful completion. Otherwise, it returns 0. It stores the number of lines contained in the file. This number is accessible by the C macro. =cut sub _m_write_file { my ($slf, $ctx, $fil) = @_; my ($cnt, $ofh); if ($fil && open(IN, "<$fil")) { # Get the output file handle $ofh = $ctx->get_handle; # Write the file to the output file, taking care on end of lines print $ofh "\n"; $cnt = 0; while () { s/[\r\n]+$//; s/^\000+$//; ++$cnt; print $ofh "$_\n"; } print $ofh "\n"; close(IN); $slf->{'lgt'} = $cnt; # Indicate the successful completion return 1; } 0; } =head2 S This macro writes the content of the file in the output file. It applies the specified regular expression on each line. It ignores case distinctions between the file and pattern. You can specify the replacement string as an extra argument. It returns 1 for a successful completion. Otherwise, it returns 0. It stores the number of lines contained in the file. This number is accessible by the C macro. =cut sub _m_write_filter { my ($slf, $ctx, $fil, $re, $alt) = @_; my ($cnt, $ofh); if ($fil && open(IN, "<$fil")) { # Get the output file handle $ofh = $ctx->get_handle; # Write the file to the output file, taking care on end of lines print $ofh "\n"; $alt = '...' unless defined($alt); $cnt = 0; while () { s/[\r\n]+$//; s/^\000+$//; s#$re#$1$alt#ig if $re; ++$cnt; print $ofh "$_\n"; } print $ofh "\n"; close(IN); $slf->{'lgt'} = $cnt; # Indicate the successful completion return 1; } 0; } =head2 S This macro writes a line range from the last file/command buffer in the output file. It assumes the first and last line as default for the range definition. You can use negative line numbers to specify lines from the buffer end. It returns 1 for a successful completion. Otherwise, it returns 0. =cut sub _m_write_last { my ($slf, $ctx, $min, $max) = @_; my ($buf, $ofh); if (exists($slf->{'_buf'})) { # Get the output file handle $ofh = $ctx->get_handle; # Validate the range $buf = $slf->{'_buf'}; $min = (!defined($min) || ($#$buf + $min) < -1) ? 0 : ($min < 0) ? $#$buf + $min + 1 : $min; $max = (!defined($max)) ? $#$buf : (($#$buf + $max) < -1) ? 0 : ($max < 0) ? $#$buf + $max + 1 : ($max > $#$buf) ? $#$buf : $max; # Write the file to the output file, taking care on end of lines print $ofh "\n"; foreach my $lin (@$buf[$min..$max]) { print $ofh "$lin\n" if defined($lin); } print $ofh "\n"; # Indicate the successful completion return 1; } 0; } =head2 S This macro writes the specified line range of a file in the output file. It assumes that lines are written if the range is valid. By default, it starts at the first line and stops at the end of the file or when the maximum number of lines is reached. When the start line number is negative, it indicates an offset from the end line number. For instance, ranges -10, 100 and 90, 100 are equivalent. It returns 1 for a successful completion. Otherwise, it returns 0. =cut sub _m_write_lines { my ($slf, $ctx, $fil, $min, $max) = @_; my ($cnt, $ofh); # Validate the line numbers $max = $slf->{'max'} unless defined($max); $min = 1 unless defined($min); $min = $max + $min if $min < 0; # if ($fil && $min <= $max && open(IN, "<$fil")) { # Get the output file handle $ofh = $ctx->get_handle; # Write the file to the output file, taking care on end of lines $cnt = 0; print $ofh "\n"; while () { next if ++$cnt < $min; last if $cnt > $max; s/[\r\n]+$//; s/^\000+$//; print $ofh "$_\n"; } print $ofh "\n"; close(IN); return 1; } 0; } =head2 S This macro writes the tail of the file in the output file. It returns 1 for a successful completion. Otherwise, it returns 0. It stores the number of lines contained in the file. This number is accessible by the C macro. =cut sub _m_write_tail { my ($slf, $ctx, $fil, $lgt) = @_; my ($cnt, $ofh, @buf); if ($fil && open(IN, "<$fil")) { # Get the output file handle $ofh = $ctx->get_handle; # Read the file keeping the last lines in a buffer $cnt = 0; while () { push(@buf, m/^\000+[\r\n]+$/ ? '' : $_); shift(@buf) if scalar(@buf) > $lgt; ++$cnt; } close(IN); $slf->{'lgt'} = $cnt; # Write the last lines of the file in the output file. print $ofh "\n"; for (@buf) { s/[\r\n]+$//; print $ofh "$_\n"; } print $ofh "\n"; # Indicate the successful completion return 1; } 0; } # --- Internal functions ------------------------------------------------------ # Check if alarm is implemented sub _chk_alarm { my ($lim) = @_; return 0 unless $lim > 0; eval {alarm(0);}; $@ ? 0 : $lim; } # Get the alarm duration sub _get_alarm { my ($slf, $val) = @_; return $slf->{'lim'} unless defined($val); return 0 unless $slf->{'lim'} > 0 && $val > 0; $val *= $slf->{'lim'}; ($val > 1) ? int($val) : 1; } # Adapt the command for VMS sub _conv_cmd { my ($slf, $cmd) = @_; return $cmd unless $cmd; if ($slf->{'_win'}) { $cmd =~ s#/dev/null#NULL#; } elsif ($slf->{'_vms'} && $cmd =~ m/[\<\>]/ && $cmd !~ m/^PIPE /i) { $cmd = "PIPE $cmd"; $cmd =~ s/2>&1/2>SYS\$OUTPUT/g; } $cmd; } # Get the output of a command in a temporary file sub _get_output { my ($slf, $ctx, $cmd, $inc) = @_; my ($arg, $err, $lim, $out, $pid, $ret, $sta, $tmp); local $SIG{'__WARN__'} = sub { }; # Abort when the command is missing return (0) unless $cmd; # Execute the command $lim = $slf->_get_alarm($inc); $tmp = sprintf('%s%d.txt', $slf->{'_tmp'}, $slf->{'_seq'}); $out = $slf->{'_agt'}->quote($tmp); $arg = $cmd; $arg =~ s#(\s+2>&1)?\s*$#>$out $1#; $arg =~ s#/dev/null\b#NULL#g if $slf->{'_win'}; eval { local $SIG{'ALRM'} = sub { die "Alarm\n" } if $lim; local $SIG{'PIPE'} = 'IGNORE'; # Limit its execution to prevent RDA hangs alarm($lim) if $lim; # Execute the command close(OUT) if ($pid = open(OUT, "| $arg")); # Disable alarms alarm(0) if $lim; }; $err = $?; return ($err) unless $pid; if ($sta = $@) { kill(9, $pid); $err = _log_timeout($slf, $ctx, $cmd); } # Return the file name ($err, $sta, $tmp); } # Get the status information sub _get_stat { my ($tbl, $sta, $fil, $nam, $max) = @_; my ($grp, $lgt, $usr, @sta); # Get the status information @sta = &$sta($fil); return unless scalar(@sta); # Resolve the user and group IDs $usr = _fmt_uid($sta[4]); $grp = _fmt_gid($sta[5]); # Adjust the information for the column sizes if ($max) { $max->[0] = 10; $max->[1] = $sta[3] if $sta[3] > $max->[1]; $max->[2] = $lgt if ($lgt = length($usr)) > $max->[2]; $max->[3] = $lgt if ($lgt = length($grp)) > $max->[3]; $max->[4] = $sta[7] if $sta[7] > $max->[4]; } # Add the record to the list push( @$tbl, [ _fmt_mod($sta[2]), $sta[3], $usr, $grp, $sta[7], $sta[10], $sta[9], $nam ] ); } sub _get_vms_stat { my ($tbl, $pth, $flg, $max) = @_; my ($lgt, $rec, @sta); # Get the status information if (open(VMS, "dir/size/date=modified/owner/prot/noheading/". "notrailing/width=(filename=1) $pth |")) { while () { s/[\n\r\s]+$//; if (m/^\%DIRECT-W-NOFILES,/i) { next; } elsif (m/^\S/) { s/^.*\]// if $flg; $rec = [ '', 1, '', '', 0, 0, 0, $_ ]; push(@$tbl, $rec); } elsif ($rec) { @sta = split(/\s+/, $_); if (scalar(@sta) == 6) { my ($grp, $usr); # Extract the information and update the record ($usr, $grp) = ($2, $1) if $sta[4] =~ m/^\[(.*),(.*)]$/; $rec->[0] = $sta[5]; $rec->[2] = $usr || $sta[4]; $rec->[3] = $grp || ''; $rec->[4] = $sta[1]; $rec->[6] = _dec_dat($sta[2].' '.$sta[3]); # Adjust the information for the column sizes if ($max) { $max->[0] = $lgt if ($lgt = length($sta[5])) > $max->[0]; $max->[1] = 1; $max->[2] = $lgt if ($lgt = length($usr)) > $max->[2]; $max->[3] = $lgt if ($lgt = length($grp)) > $max->[3]; $max->[4] = $sta[1] if $sta[1] > $max->[4]; } $rec = undef; } else { $rec->[6] .= $_; } } } close(VMS); } } # Log a timeout event sub _log_timeout { my $slf = shift; my $ctx = shift; $slf->{'_agt'}->log_timeout($ctx, 'OS', @_); ++$slf->{'_out'}; $slf->{'err'} = -1; } # Display the status information sub _write_stat { my ($ctx, $tbl, $max, $fct, $dir) = @_; my $ofh; # Get the output file handle $ofh = $ctx->get_handle; # Determine the column sizes $max->[1] = length(sprintf("%d", $max->[1])); $max->[2]++ if $max->[2]; $max->[3]++ if $max->[3]; $max->[4] = length(sprintf("%d", $max->[4])); # Sort the files if ($fct eq 'n') { $tbl = [sort {$a->[7] cmp $b->[7]} @$tbl]; } elsif ($fct eq 't') { $tbl = [sort {$b->[6] <=> $a->[6] || $a->[7] cmp $b->[7]} @$tbl]; } if ($fct eq 'v') { $tbl = [sort {lc($a->[7]) cmp lc($b->[7])} @$tbl]; } # Produce the directory listing print $ofh "\n"; print $ofh "$dir:\n" if $dir; foreach my $sta (@$tbl) { printf $ofh " %-*s %*d %-*s%-*s%*d%s%s %s\n", $max->[0], $sta->[0], $max->[1], $sta->[1], $max->[2], $sta->[2], $max->[3], $sta->[3], $max->[4], $sta->[4], _fmt_dat($sta->[5]), _fmt_dat($sta->[6]), $sta->[7]; } print $ofh "\n"; # Indicate the number of files scalar(@$tbl); } # Decode date/time sub _dec_dat { my ($str) = @_; ($str =~ m/^(\d{1,2})-(\w{3})-(\d{4})\s+(\d{2}):(\d{2}):(\d{2})/) ? sprintf('%04d%02d%02d%02d%02d%02d', $3, $tb_mon{uc($2)} || 0, $1, $4, $5, $6) : 0; } # Convert the mode in its symbolic format sub _fmt_bit { my ($val, $flg) = @_; $val += 8 if $flg; $tb_bit[$val]; } sub _fmt_mod { my $mod = shift; substr("?pc?d?b?-?l?s???", ($mod >> 12) & 017, 1) ._fmt_bit(($mod >> 6) & 07, $mod & 04000) ._fmt_bit(($mod >> 3) & 07, $mod & 02000) ._fmt_bit($mod & 07, $mod & 01000); } # Simplify the date/time sub _fmt_dat { my ($str) = @_; return '' unless $str; return sprintf(' %s %2d %s:%s %s', $tb_mon[substr($str, 4, 2)], substr($str, 6, 2), substr($str, 8, 2), substr($str, 10, 2), substr($str,0, 4)) if length($str) == 14; $str = gmtime($str); ' '.substr($str,4,12).' '.substr($str,20,4); } # Resolve the group ID sub _fmt_gid { my $gid = shift; my $str; eval {$str = getgrgid($gid);}; return $gid ? "$gid" : '' if $@ || !defined($str); $str; } # Resolve the user ID sub _fmt_uid { my $uid = shift; my $str; eval {$str = getpwuid($uid);}; return $uid ? "$uid" : '' if $@ || !defined($str); $str; } # Get the file statistics sub _lstat { return lstat(shift); } sub _stat { return stat(shift); } # Get the last modification date of the file sub _lmod { my ($fil, $dft) = @_; my @sta = lstat($fil); defined($sta[9]) ? $sta[9] : $dft; } sub _mod { my ($fil, $dft) = @_; my @sta = stat($fil); defined($sta[9]) ? $sta[9] : $dft; } 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