package File::Spec::VMS;
use strict;
use vars qw(@ISA $VERSION);
require File::Spec::Unix;
$VERSION = '1.4';
@ISA = qw(File::Spec::Unix);
use File::Basename;
use VMS::Filespec;
=head1 NAME
File::Spec::VMS - methods for VMS file specs
=head1 SYNOPSIS
require File::Spec::VMS; # Done internally by File::Spec if needed
=head1 DESCRIPTION
See File::Spec::Unix for a documentation of the methods provided
there. This package overrides the implementation of these methods, not
the semantics.
=over 4
=item canonpath (override)
Removes redundant portions of file specifications according to VMS syntax.
=cut
sub canonpath {
my($self,$path) = @_;
if ($path =~ m|/|) { my $pathify = $path =~ m|/\Z(?!\n)|;
$path = $self->SUPER::canonpath($path);
if ($pathify) { return vmspath($path); }
else { return vmsify($path); }
}
else {
$path =~ tr/<>/[]/; $path =~ s/\]\[\./\.\]\[/g; $path =~ s/\[000000\.\]\[/\[/g; $path =~ s/\[000000\./\[/g; $path =~ s/\.\]\[000000\]/\]/g; $path =~ s/\.\]\[/\./g; 1 while ($path =~ s/([\[\.])(-+)\.(-+)([\.\]])/$1$2$3$4/);
1 while ($path =~ s/([\[\.])[^\]\.]+\.-(-+)([\]\.])/$1$2$3/);
$path =~ s/\[\.-/[-/; $path =~ s/\.[^\]\.]+\.-\./\./g; $path =~ s/\[[^\]\.]+\.-\./\[/g; $path =~ s/\.[^\]\.]+\.-\]/\]/g; $path =~ s/\[[^\]\.]+\.-\]/\[000000\]/g; $path =~ s/\[\]// unless $path eq '[]'; # [] ==>
return $path;
}
}
=item catdir (override)
Concatenates a list of file specifications, and returns the result as a
VMS-syntax directory specification. No check is made for "impossible"
cases (e.g. elements other than the first being absolute filespecs).
=cut
sub catdir {
my $self = shift;
my $dir = pop;
my @dirs = grep {defined() && length()} @_;
my $rslt;
if (@dirs) {
my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
my ($spath,$sdir) = ($path,$dir);
$spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
$sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
$rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
}
else {
if (not defined $dir or not length $dir) { $rslt = ''; }
elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
else { $rslt = vmspath($dir); }
}
return $self->canonpath($rslt);
}
=item catfile (override)
Concatenates a list of file specifications, and returns the result as a
VMS-syntax file specification.
=cut
sub catfile {
my $self = shift;
my $file = $self->canonpath(pop());
my @files = grep {defined() && length()} @_;
my $rslt;
if (@files) {
my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
my $spath = $path;
$spath =~ s/\.dir\Z(?!\n)//;
if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
$rslt = "$spath$file";
}
else {
$rslt = $self->eliminate_macros($spath);
$rslt = vmsify($rslt.((defined $rslt) && ($rslt ne '') ? '/' : '').unixify($file));
}
}
else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
return $self->canonpath($rslt);
}
=item curdir (override)
Returns a string representation of the current directory: '[]'
=cut
sub curdir {
return '[]';
}
=item devnull (override)
Returns a string representation of the null device: '_NLA0:'
=cut
sub devnull {
return "_NLA0:";
}
=item rootdir (override)
Returns a string representation of the root directory: 'SYS$DISK:[000000]'
=cut
sub rootdir {
return 'SYS$DISK:[000000]';
}
=item tmpdir (override)
Returns a string representation of the first writable directory
from the following list or '' if none are writable:
sys$scratch:
$ENV{TMPDIR}
Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
is tainted, it is not used.
=cut
my $tmpdir;
sub tmpdir {
return $tmpdir if defined $tmpdir;
$tmpdir = $_[0]->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
}
=item updir (override)
Returns a string representation of the parent directory: '[-]'
=cut
sub updir {
return '[-]';
}
=item case_tolerant (override)
VMS file specification syntax is case-tolerant.
=cut
sub case_tolerant {
return 1;
}
=item path (override)
Translate logical name DCL$PATH as a searchlist, rather than trying
to C<split> string value of C<$ENV{'PATH'}>.
=cut
sub path {
my (@dirs,$dir,$i);
while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
return @dirs;
}
=item file_name_is_absolute (override)
Checks for VMS directory spec as well as Unix separators.
=cut
sub file_name_is_absolute {
my ($self,$file) = @_;
$file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
return scalar($file =~ m!^/!s ||
$file =~ m![<\[][^.\-\]>]! ||
$file =~ /:[^<\[]/);
}
=item splitpath (override)
Splits using VMS syntax.
=cut
sub splitpath {
my($self,$path) = @_;
my($dev,$dir,$file) = ('','','');
vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
return ($1 || '',$2 || '',$3);
}
=item splitdir (override)
Split dirspec using VMS syntax.
=cut
sub splitdir {
my($self,$dirspec) = @_;
$dirspec =~ tr/<>/[]/; $dirspec =~ s/\]\[\./\.\]\[/g; $dirspec =~ s/\[000000\.\]\[/\[/g; $dirspec =~ s/\[000000\./\[/g; $dirspec =~ s/\.\]\[000000\]/\]/g; $dirspec =~ s/\.\]\[/\./g; while ($dirspec =~ s/(^|[\[\<\.])\-(\-+)($|[\]\>\.])/$1-.$2$3/g) {}
$dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; my(@dirs) = split('\.', vmspath($dirspec));
$dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
@dirs;
}
=item catpath (override)
Construct a complete filespec using VMS syntax
=cut
sub catpath {
my($self,$dev,$dir,$file) = @_;
my ($dir_volume, $dir_dir, $dir_file) = $self->splitpath($dir);
$dev = $dir_volume unless length $dev;
$dir = length $dir_file ? $self->catfile($dir_dir, $dir_file) : $dir_dir;
if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
if (length($dev) or length($dir)) {
$dir = "[$dir]" unless $dir =~ /[\[<\/]/;
$dir = vmspath($dir);
}
"$dev$dir$file";
}
=item abs2rel (override)
Use VMS syntax when converting filespecs.
=cut
sub abs2rel {
my $self = shift;
return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
if grep m{/}, @_;
my($path,$base) = @_;
$base = $self->_cwd() unless defined $base and length $base;
for ($path, $base) { $_ = $self->canonpath($_) }
my ($path_volume, $path_directories, $path_file) = $self->splitpath($path);
my ($base_volume, $base_directories, $base_file) = $self->splitpath($base);
return $path unless lc($path_volume) eq lc($base_volume);
for ($path, $base) { $_ = $self->rel2abs($_) }
my @pathchunks = $self->splitdir( $path_directories );
my $pathchunks = @pathchunks;
unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
my @basechunks = $self->splitdir( $base_directories );
my $basechunks = @basechunks;
unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
while ( @pathchunks &&
@basechunks &&
lc( $pathchunks[0] ) eq lc( $basechunks[0] )
) {
shift @pathchunks ;
shift @basechunks ;
}
if ((@basechunks > 0) || ($basechunks != $pathchunks)) {
$path_directories = join '.', ('-' x @basechunks, @pathchunks) ;
}
else {
$path_directories = join '.', @pathchunks;
}
$path_directories = '['.$path_directories.']';
return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
}
=item rel2abs (override)
Use VMS syntax when converting filespecs.
=cut
sub rel2abs {
my $self = shift ;
my ($path,$base ) = @_;
return undef unless defined $path;
if ($path =~ m/\//) {
$path = ( -d $path || $path =~ m/\/\z/ ? vmspath($path) : vmsify($path) );
}
$base = vmspath($base) if defined $base && $base =~ m/\//;
if ( ! $self->file_name_is_absolute( $path ) ) {
if ( !defined( $base ) || $base eq '' ) {
$base = $self->_cwd;
}
elsif ( ! $self->file_name_is_absolute( $base ) ) {
$base = $self->rel2abs( $base ) ;
}
else {
$base = $self->canonpath( $base ) ;
}
my ( $path_directories, $path_file ) =
($self->splitpath( $path ))[1,2] ;
my ( $base_volume, $base_directories ) =
$self->splitpath( $base ) ;
$path_directories = '' if $path_directories eq '[]' ||
$path_directories eq '<>';
my $sep = '' ;
$sep = '.'
if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
$path_directories =~ m{^[^.\[<]}s
) ;
$base_directories = "$base_directories$sep$path_directories";
$base_directories =~ s{\.?[\]>][\[<]\.?}{.};
$path = $self->catpath( $base_volume, $base_directories, $path_file );
}
return $self->canonpath( $path ) ;
}
sub eliminate_macros {
my($self,$path) = @_;
return '' unless (defined $path) && ($path ne '');
$self = {} unless ref $self;
if ($path =~ /\s/) {
return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
}
my($npath) = unixify($path);
my($complex) = 0;
my($head,$macro,$tail);
while ($npath =~ m if ($self->{$2}) {
($head,$macro,$tail) = ($1,$2,$3);
if (ref $self->{$macro}) {
if (ref $self->{$macro} eq 'ARRAY') {
$macro = join ' ', @{$self->{$macro}};
}
else {
print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
"\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
$macro = "\cB$macro\cB";
$complex = 1;
}
}
else { ($macro = unixify($self->{$macro})) =~ s $npath = "$head$macro$tail";
}
}
if ($complex) { $npath =~ s $npath;
}
sub fixpath {
my($self,$path,$force_path) = @_;
return '' unless $path;
$self = bless {} unless ref $self;
my($fixedpath,$prefix,$name);
if ($path =~ /\s/) {
return join ' ',
map { $self->fixpath($_,$force_path) }
split /\s+/, $path;
}
if ($path =~ m if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
$fixedpath = vmspath($self->eliminate_macros($path));
}
else {
$fixedpath = vmsify($self->eliminate_macros($path));
}
}
elsif ((($prefix,$name) = ($path =~ m my($vmspre) = $self->eliminate_macros("\$($prefix)");
$vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
$fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
$fixedpath = vmspath($fixedpath) if $force_path;
}
else {
$fixedpath = $path;
$fixedpath = vmspath($fixedpath) if $force_path;
}
if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
$fixedpath = vmspath($fixedpath) if -d $fixedpath;
}
$fixedpath =~ s/\.000000([\]>])/$1/;
if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
$fixedpath;
}
=back
=head1 COPYRIGHT
Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 SEE ALSO
See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
implementation of these methods, not the semantics.
An explanation of VMS file specs can be found at
L<"http://h71000.www7.hp.com/doc/731FINAL/4506/4506pro_014.html#apps_locating_naming_files">.
=cut
1;