integrate cfgperl and vmsperl contents into mainline
[p5sagit/p5-mst-13.2.git] / lib / File / Spec / VMS.pm
index 71c38f2..a2ac8ca 100644 (file)
@@ -5,6 +5,7 @@ use vars qw(@ISA);
 require File::Spec::Unix;
 @ISA = qw(File::Spec::Unix);
 
+use Cwd;
 use File::Basename;
 use VMS::Filespec;
 
@@ -41,7 +42,7 @@ sub eliminate_macros {
     my($head,$macro,$tail);
 
     # perform m##g in scalar context so it acts as an iterator
-    while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#g) { 
+    while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) { 
         if ($self->{$2}) {
             ($head,$macro,$tail) = ($1,$2,$3);
             if (ref $self->{$macro}) {
@@ -55,11 +56,11 @@ sub eliminate_macros {
                     $complex = 1;
                 }
             }
-            else { ($macro = unixify($self->{$macro})) =~ s#/$##; }
+            else { ($macro = unixify($self->{$macro})) =~ s#/\z##; }
             $npath = "$head$macro$tail";
         }
     }
-    if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#g; }
+    if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
     $npath;
 }
 
@@ -85,18 +86,18 @@ sub fixpath {
     $self = bless {} unless ref $self;
     my($fixedpath,$prefix,$name);
 
-    if ($path =~ m#^\$\([^\)]+\)$# || $path =~ m#[/:>\]]#) { 
-        if ($force_path or $path =~ /(?:DIR\)|\])$/) {
+    if ($path =~ m#^\$\([^\)]+\)\z#s || $path =~ m#[/:>\]]#) { 
+        if ($force_path or $path =~ /(?:DIR\)|\])\z/) {
             $fixedpath = vmspath($self->eliminate_macros($path));
         }
         else {
             $fixedpath = vmsify($self->eliminate_macros($path));
         }
     }
-    elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#)) && $self->{$prefix}) {
+    elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
         my($vmspre) = $self->eliminate_macros("\$($prefix)");
         # is it a dir or just a name?
-        $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR$/) ? vmspath($vmspre) : '';
+        $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\z/) ? vmspath($vmspre) : '';
         $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
         $fixedpath = vmspath($fixedpath) if $force_path;
     }
@@ -108,8 +109,14 @@ sub fixpath {
     if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
         $fixedpath = vmspath($fixedpath) if -d $fixedpath;
     }
+
     # Trim off root dirname if it's had other dirs inserted in front of it.
     $fixedpath =~ s/\.000000([\]>])/$1/;
+    # Special case for VMS absolute directory specs: these will have had device
+    # prepended during trip through Unix syntax in eliminate_macros(), since
+    # Unix syntax has no way to express "absolute from the top of this device's
+    # directory tree".
+    if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
     $fixedpath;
 }
 
@@ -119,10 +126,38 @@ sub fixpath {
 
 =over
 
+=item canonpath (override)
+
+Removes redundant portions of file specifications according to VMS syntax.
+
+=cut
+
+sub canonpath {
+    my($self,$path) = @_;
+
+    if ($path =~ m|/|) { # Fake Unix
+      my $pathify = $path =~ m|/\z|;
+      $path = $self->SUPER::canonpath($path);
+      if ($pathify) { return vmspath($path); }
+      else          { return vmsify($path);  }
+    }
+    else {
+      $path =~ s-\]\[--g;  $path =~ s/><//g;            # foo.][bar       ==> foo.bar
+      $path =~ s/([\[<])000000\./$1/;                   # [000000.foo     ==> foo
+      1 while $path =~ s{([\[<-])\.-}{$1-};             # [.-.-           ==> [--
+      $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/;            # bar.foo.-]      ==> bar]
+      $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
+      $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g;    # bar.-.foo       ==> foo
+      $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
+      return $path;
+    }
+}
+
 =item catdir
 
 Concatenates a list of file specifications, and returns the result as a
-VMS-syntax directory specification.
+VMS-syntax directory specification.  No check is made for "impossible"
+cases (e.g. elements other than the first being absolute filespecs).
 
 =cut
 
@@ -134,21 +169,28 @@ sub catdir {
     if (@dirs) {
        my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
        my ($spath,$sdir) = ($path,$dir);
-       $spath =~ s/.dir$//; $sdir =~ s/.dir$//; 
-       $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+$/;
+       $spath =~ s/\.dir\z//; $sdir =~ s/\.dir\z//; 
+       $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\z/s;
        $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
+
+       # Special case for VMS absolute directory specs: these will have had device
+       # prepended during trip through Unix syntax in eliminate_macros(), since
+       # Unix syntax has no way to express "absolute from the top of this device's
+       # directory tree".
+       if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
     }
     else {
-       if ($dir =~ /^\$\([^\)]+\)$/) { $rslt = $dir; }
-       else                          { $rslt = vmspath($dir); }
+       if    (not defined $dir or not length $dir) { $rslt = ''; }
+       elsif ($dir =~ /^\$\([^\)]+\)\z/s)          { $rslt = $dir; }
+       else                                        { $rslt = vmspath($dir); }
     }
-    return $rslt;
+    return $self->canonpath($rslt);
 }
 
 =item catfile
 
 Concatenates a list of file specifications, and returns the result as a
-VMS-syntax directory specification.
+VMS-syntax file specification.
 
 =cut
 
@@ -160,8 +202,8 @@ sub catfile {
     if (@files) {
        my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
        my $spath = $path;
-       $spath =~ s/.dir$//;
-       if ($spath =~ /^[^\)\]\/:>]+\)$/ && basename($file) eq $file) {
+       $spath =~ s/\.dir\z//;
+       if ($spath =~ /^[^\)\]\/:>]+\)\z/s && basename($file) eq $file) {
            $rslt = "$spath$file";
        }
        else {
@@ -169,10 +211,11 @@ sub catfile {
            $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
        }
     }
-    else { $rslt = vmsify($file); }
-    return $rslt;
+    else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
+    return $self->canonpath($rslt);
 }
 
+
 =item curdir (override)
 
 Returns a string representation of the current directory: '[]'
@@ -208,7 +251,7 @@ sub rootdir {
 Returns a string representation of the first writable directory
 from the following list or '' if none are writable:
 
-    /sys$scratch
+    sys$scratch
     $ENV{TMPDIR}
 
 =cut
@@ -216,7 +259,7 @@ from the following list or '' if none are writable:
 my $tmpdir;
 sub tmpdir {
     return $tmpdir if defined $tmpdir;
-    foreach ('/sys$scratch', $ENV{TMPDIR}) {
+    foreach ('sys$scratch', $ENV{TMPDIR}) {
        next unless defined && -d && -w _;
        $tmpdir = $_;
        last;
@@ -235,6 +278,16 @@ 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
@@ -257,127 +310,69 @@ Checks for VMS directory spec as well as Unix separators.
 sub file_name_is_absolute {
     my ($self,$file) = @_;
     # If it's a logical name, expand it.
-    $file = $ENV{$file} while $file =~ /^[\w\$\-]+$/ && $ENV{$file};
-    return scalar($file =~ m!^/!              ||
+    $file = $ENV{$file} while $file =~ /^[\w\$\-]+\z/s && $ENV{$file};
+    return scalar($file =~ m!^/!s             ||
                  $file =~ m![<\[][^.\-\]>]!  ||
                  $file =~ /:[^<\[]/);
 }
 
-=item splitpath
-
-    ($volume,$directories,$file) = File::Spec->splitpath( $path );
-    ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
-
-Splits a VMS path in to volume, directory, and filename portions.
-Ignores $no_file, if present, since VMS paths indicate the 'fileness' of a 
-file.
+=item splitpath (override)
 
-The results can be passed to L</catpath()> to get back a path equivalent to
-(usually identical to) the original path.
+Splits using VMS syntax.
 
 =cut
 
 sub splitpath {
-    my $self = shift ;
-    my ($path, $nofile) = @_;
-
-    my ($volume,$directory,$file) ;
-
-    if ( $path =~ m{/} ) {
-        $path =~ 
-            m{^ ( (?: /[^/]* )? )
-                ( (?: .*/(?:[^/]+.dir)? )? )
-                (.*)
-             }x;
-        $volume    = $1;
-        $directory = $2;
-        $file      = $3;
-    }
-    else {
-        $path =~ 
-            m{^ ( (?: (?: (?: [\w\$-]+ (?: "[^"]*")?:: )? [\w\$-]+: )? ) )
-                ( (?:\[.*\])? )
-                (.*)
-             }x;
-        $volume    = $1;
-        $directory = $2;
-        $file      = $3;
-    }
-
-    $directory = $1
-        if $directory =~ /^\[(.*)\]$/ ;
+    my($self,$path) = @_;
+    my($dev,$dir,$file) = ('','','');
 
-    return ($volume,$directory,$file);
+    vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
+    return ($1 || '',$2 || '',$3);
 }
 
+=item splitdir (override)
 
-=item splitdir
-
-The opposite of L</catdir()>.
-
-    @dirs = File::Spec->splitdir( $directories );
-
-$directories must be only the directory portion of the path.
-
-'[' and ']' delimiters are optional. An empty string argument is
-equivalent to '[]': both return an array with no elements.
+Split dirspec using VMS syntax.
 
 =cut
 
 sub splitdir {
-    my $self = shift ;
-    my $directories = $_[0] ;
-
-    return File::Spec::Unix::splitdir( $self, @_ )
-        if ( $directories =~ m{/} ) ;
-
-    $directories =~ s/^\[(.*)\]$/$1/ ;
-
-    #
-    # split() likes to forget about trailing null fields, so here we
-    # check to be sure that there will not be any before handling the
-    # simple case.
-    #
-    if ( $directories !~ m{\.$} ) {
-        return split( m{\.}, $directories );
-    }
-    else {
-        #
-        # since there was a trailing separator, add a file name to the end, 
-        # then do the split, then replace it with ''.
-        #
-        my( @directories )= split( m{\.}, "${directories}dummy" ) ;
-        $directories[ $#directories ]= '' ;
-        return @directories ;
-    }
+    my($self,$dirspec) = @_;
+    $dirspec =~ s/\]\[//g;  $dirspec =~ s/\-\-/-.-/g;
+    $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
+    my(@dirs) = split('\.', vmspath($dirspec));
+    $dirs[0] =~ s/^[\[<]//s;  $dirs[-1] =~ s/[\]>]\z//s;
+    @dirs;
 }
 
 
-sub catpath {
-    my $self = shift;
-
-    return File::Spec::Unix::catpath( $self, @_ )
-        if ( join( '', @_ ) =~ m{/} ) ;
+=item catpath (override)
 
-    my ($volume,$directory,$file) = @_;
+Construct a complete filespec using VMS syntax
 
-    $volume .= ':'
-        if $volume =~ /[^:]$/ ;
+=cut
 
-    $directory = "[$directory"
-        if $directory =~ /^[^\[]/ ;
+sub catpath {
+    my($self,$dev,$dir,$file) = @_;
+    if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
+    else { $dev .= ':' unless $dev eq '' or $dev =~ /:\z/; }
+    if (length($dev) or length($dir)) {
+      $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
+      $dir = vmspath($dir);
+    }
+    "$dev$dir$file";
+}
 
-    $directory .= ']'
-        if $directory =~ /[^\]]$/ ;
+=item abs2rel (override)
 
-    return "$volume$directory$file" ;
-}
+Use VMS syntax when converting filespecs.
 
+=cut
 
 sub abs2rel {
     my $self = shift;
 
-    return File::Spec::Unix::abs2rel( $self, @_ )
+    return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
         if ( join( '', @_ ) =~ m{/} ) ;
 
     my($path,$base) = @_;
@@ -394,12 +389,12 @@ sub abs2rel {
     }
 
     # Figure out the effective $base and clean it up.
-    if ( ! $self->file_name_is_absolute( $base ) ) {
-        $base = $self->rel2abs( $base ) ;
-    }
-    elsif ( !defined( $base ) || $base eq '' ) {
+    if ( !defined( $base ) || $base eq '' ) {
         $base = cwd() ;
     }
+    elsif ( ! $self->file_name_is_absolute( $base ) ) {
+        $base = $self->rel2abs( $base ) ;
+    }
     else {
         $base = $self->canonpath( $base ) ;
     }
@@ -409,13 +404,13 @@ sub abs2rel {
         $self->splitpath( $path, 1 ) ;
 
     $path_directories = $1
-        if $path_directories =~ /^\[(.*)\]$/ ;
+        if $path_directories =~ /^\[(.*)\]\z/s ;
 
     my ( undef, $base_directories, undef ) =
         $self->splitpath( $base, 1 ) ;
 
     $base_directories = $1
-        if $base_directories =~ /^\[(.*)\]$/ ;
+        if $base_directories =~ /^\[(.*)\]\z/s ;
 
     # Now, remove all leading components that are the same
     my @pathchunks = $self->splitdir( $path_directories );
@@ -432,14 +427,20 @@ sub abs2rel {
     # @basechunks now contains the directories to climb out of,
     # @pathchunks now has the directories to descend in to.
     $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
-    $path_directories =~ s{\.$}{} ;
-    return $self->catpath( '', $path_directories, $path_file ) ;
+    $path_directories =~ s{\.\z}{} ;
+    return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
 }
 
 
+=item rel2abs (override)
+
+Use VMS syntax when converting filespecs.
+
+=cut
+
 sub rel2abs($;$;) {
     my $self = shift ;
-    return File::Spec::Unix::rel2abs( $self, @_ )
+    return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
         if ( join( '', @_ ) =~ m{/} ) ;
 
     my ($path,$base ) = @_;
@@ -463,12 +464,15 @@ sub rel2abs($;$;) {
         my ( $base_volume, $base_directories, undef ) =
             $self->splitpath( $base ) ;
 
+        $path_directories = '' if $path_directories eq '[]' ||
+                                  $path_directories eq '<>';
         my $sep = '' ;
         $sep = '.'
-            if ( $base_directories =~ m{[^.]$} &&
-                 $path_directories =~ m{^[^.]}
+            if ( $base_directories =~ m{[^.\]>]\z} &&
+                 $path_directories =~ m{^[^.\[<]}s
             ) ;
-        $base_directories = "$base_directories$sep$path_directories" ;
+        $base_directories = "$base_directories$sep$path_directories";
+        $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
 
         $path = $self->catpath( $base_volume, $base_directories, $path_file );
    }