1 package File::Spec::VMS;
4 use vars qw(@ISA $VERSION);
5 require File::Spec::Unix;
9 @ISA = qw(File::Spec::Unix);
16 File::Spec::VMS - methods for VMS file specs
20 require File::Spec::VMS; # Done internally by File::Spec if needed
24 See File::Spec::Unix for a documentation of the methods provided
25 there. This package overrides the implementation of these methods, not
30 =item canonpath (override)
32 Removes redundant portions of file specifications according to VMS syntax.
39 if ($path =~ m|/|) { # Fake Unix
40 my $pathify = $path =~ m|/\Z(?!\n)|;
41 $path = $self->SUPER::canonpath($path);
42 if ($pathify) { return vmspath($path); }
43 else { return vmsify($path); }
46 $path =~ tr/<>/[]/; # < and > ==> [ and ]
47 $path =~ s/\]\[\./\.\]\[/g; # ][. ==> .][
48 $path =~ s/\[000000\.\]\[/\[/g; # [000000.][ ==> [
49 $path =~ s/\[000000\./\[/g; # [000000. ==> [
50 $path =~ s/\.\]\[000000\]/\]/g; # .][000000] ==> ]
51 $path =~ s/\.\]\[/\./g; # foo.][bar ==> foo.bar
52 1 while ($path =~ s/([\[\.])(-+)\.(-+)([\.\]])/$1$2$3$4/);
53 # That loop does the following
54 # with any amount of dashes:
59 1 while ($path =~ s/([\[\.])[^\]\.]+\.-(-+)([\]\.])/$1$2$3/);
60 # That loop does the following
61 # with any amount (minimum 2)
68 # And then, the remaining cases
69 $path =~ s/\[\.-/[-/; # [.- ==> [-
70 $path =~ s/\.[^\]\.]+\.-\./\./g; # .foo.-. ==> .
71 $path =~ s/\[[^\]\.]+\.-\./\[/g; # [foo.-. ==> [
72 $path =~ s/\.[^\]\.]+\.-\]/\]/g; # .foo.-] ==> ]
73 $path =~ s/\[[^\]\.]+\.-\]/\[000000\]/g;# [foo.-] ==> [000000]
74 $path =~ s/\[\]// unless $path eq '[]'; # [] ==>
79 =item catdir (override)
81 Concatenates a list of file specifications, and returns the result as a
82 VMS-syntax directory specification. No check is made for "impossible"
83 cases (e.g. elements other than the first being absolute filespecs).
90 my @dirs = grep {defined() && length()} @_;
94 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
95 my ($spath,$sdir) = ($path,$dir);
96 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
97 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
98 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
100 # Special case for VMS absolute directory specs: these will have had device
101 # prepended during trip through Unix syntax in eliminate_macros(), since
102 # Unix syntax has no way to express "absolute from the top of this device's
104 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
107 if (not defined $dir or not length $dir) { $rslt = ''; }
108 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
109 else { $rslt = vmspath($dir); }
111 return $self->canonpath($rslt);
114 =item catfile (override)
116 Concatenates a list of file specifications, and returns the result as a
117 VMS-syntax file specification.
123 my $file = $self->canonpath(pop());
124 my @files = grep {defined() && length()} @_;
128 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
130 $spath =~ s/\.dir\Z(?!\n)//;
131 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
132 $rslt = "$spath$file";
135 $rslt = $self->eliminate_macros($spath);
136 $rslt = vmsify($rslt.((defined $rslt) && ($rslt ne '') ? '/' : '').unixify($file));
139 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
140 return $self->canonpath($rslt);
144 =item curdir (override)
146 Returns a string representation of the current directory: '[]'
154 =item devnull (override)
156 Returns a string representation of the null device: '_NLA0:'
164 =item rootdir (override)
166 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
171 return 'SYS$DISK:[000000]';
174 =item tmpdir (override)
176 Returns a string representation of the first writable directory
177 from the following list or '' if none are writable:
182 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
183 is tainted, it is not used.
189 return $tmpdir if defined $tmpdir;
190 $tmpdir = $_[0]->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
193 =item updir (override)
195 Returns a string representation of the parent directory: '[-]'
203 =item case_tolerant (override)
205 VMS file specification syntax is case-tolerant.
213 =item path (override)
215 Translate logical name DCL$PATH as a searchlist, rather than trying
216 to C<split> string value of C<$ENV{'PATH'}>.
222 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
226 =item file_name_is_absolute (override)
228 Checks for VMS directory spec as well as Unix separators.
232 sub file_name_is_absolute {
233 my ($self,$file) = @_;
234 # If it's a logical name, expand it.
235 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
236 return scalar($file =~ m!^/!s ||
237 $file =~ m![<\[][^.\-\]>]! ||
241 =item splitpath (override)
243 Splits using VMS syntax.
248 my($self,$path) = @_;
249 my($dev,$dir,$file) = ('','','');
251 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
252 return ($1 || '',$2 || '',$3);
255 =item splitdir (override)
257 Split dirspec using VMS syntax.
262 my($self,$dirspec) = @_;
263 $dirspec =~ tr/<>/[]/; # < and > ==> [ and ]
264 $dirspec =~ s/\]\[\./\.\]\[/g; # ][. ==> .][
265 $dirspec =~ s/\[000000\.\]\[/\[/g; # [000000.][ ==> [
266 $dirspec =~ s/\[000000\./\[/g; # [000000. ==> [
267 $dirspec =~ s/\.\]\[000000\]/\]/g; # .][000000] ==> ]
268 $dirspec =~ s/\.\]\[/\./g; # foo.][bar ==> foo.bar
269 while ($dirspec =~ s/(^|[\[\<\.])\-(\-+)($|[\]\>\.])/$1-.$2$3/g) {}
270 # That loop does the following
271 # with any amount of dashes:
276 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
277 $dirspec =~ s/^(\[|<)\./$1/;
278 my(@dirs) = split /(?<!\^)\./, vmspath($dirspec);
279 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
284 =item catpath (override)
286 Construct a complete filespec using VMS syntax
291 my($self,$dev,$dir,$file) = @_;
293 # We look for a volume in $dev, then in $dir, but not both
294 my ($dir_volume, $dir_dir, $dir_file) = $self->splitpath($dir);
295 $dev = $dir_volume unless length $dev;
296 $dir = length $dir_file ? $self->catfile($dir_dir, $dir_file) : $dir_dir;
298 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
299 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
300 if (length($dev) or length($dir)) {
301 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
302 $dir = vmspath($dir);
307 =item abs2rel (override)
309 Use VMS syntax when converting filespecs.
315 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
318 my($path,$base) = @_;
319 $base = $self->_cwd() unless defined $base and length $base;
321 for ($path, $base) { $_ = $self->canonpath($_) }
323 # Are we even starting $path on the same (node::)device as $base? Note that
324 # logical paths or nodename differences may be on the "same device"
325 # but the comparison that ignores device differences so as to concatenate
326 # [---] up directory specs is not even a good idea in cases where there is
327 # a logical path difference between $path and $base nodename and/or device.
328 # Hence we fall back to returning the absolute $path spec
329 # if there is a case blind device (or node) difference of any sort
330 # and we do not even try to call $parse() or consult %ENV for $trnlnm()
331 # (this module needs to run on non VMS platforms after all).
333 my ($path_volume, $path_directories, $path_file) = $self->splitpath($path);
334 my ($base_volume, $base_directories, $base_file) = $self->splitpath($base);
335 return $path unless lc($path_volume) eq lc($base_volume);
337 for ($path, $base) { $_ = $self->rel2abs($_) }
339 # Now, remove all leading components that are the same
340 my @pathchunks = $self->splitdir( $path_directories );
341 my $pathchunks = @pathchunks;
342 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
343 my @basechunks = $self->splitdir( $base_directories );
344 my $basechunks = @basechunks;
345 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
347 while ( @pathchunks &&
349 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
355 # @basechunks now contains the directories to climb out of,
356 # @pathchunks now has the directories to descend in to.
357 if ((@basechunks > 0) || ($basechunks != $pathchunks)) {
358 $path_directories = join '.', ('-' x @basechunks, @pathchunks) ;
361 $path_directories = join '.', @pathchunks;
363 $path_directories = '['.$path_directories.']';
364 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
368 =item rel2abs (override)
370 Use VMS syntax when converting filespecs.
376 my ($path,$base ) = @_;
377 return undef unless defined $path;
378 if ($path =~ m/\//) {
379 $path = ( -d $path || $path =~ m/\/\z/ # educated guessing about
380 ? vmspath($path) # whether it's a directory
383 $base = vmspath($base) if defined $base && $base =~ m/\//;
384 # Clean up and split up $path
385 if ( ! $self->file_name_is_absolute( $path ) ) {
386 # Figure out the effective $base and clean it up.
387 if ( !defined( $base ) || $base eq '' ) {
390 elsif ( ! $self->file_name_is_absolute( $base ) ) {
391 $base = $self->rel2abs( $base ) ;
394 $base = $self->canonpath( $base ) ;
398 my ( $path_directories, $path_file ) =
399 ($self->splitpath( $path ))[1,2] ;
401 my ( $base_volume, $base_directories ) =
402 $self->splitpath( $base ) ;
404 $path_directories = '' if $path_directories eq '[]' ||
405 $path_directories eq '<>';
408 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
409 $path_directories =~ m{^[^.\[<]}s
411 $base_directories = "$base_directories$sep$path_directories";
412 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
414 $path = $self->catpath( $base_volume, $base_directories, $path_file );
417 return $self->canonpath( $path ) ;
421 # eliminate_macros() and fixpath() are MakeMaker-specific methods
422 # which are used inside catfile() and catdir(). MakeMaker has its own
423 # copies as of 6.06_03 which are the canonical ones. We leave these
424 # here, in peace, so that File::Spec continues to work with MakeMakers
427 # Please consider these two methods deprecated. Do not patch them,
428 # patch the ones in ExtUtils::MM_VMS instead.
429 sub eliminate_macros {
430 my($self,$path) = @_;
431 return '' unless (defined $path) && ($path ne '');
432 $self = {} unless ref $self;
435 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
438 my($npath) = unixify($path);
440 my($head,$macro,$tail);
442 # perform m##g in scalar context so it acts as an iterator
443 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
445 ($head,$macro,$tail) = ($1,$2,$3);
446 if (ref $self->{$macro}) {
447 if (ref $self->{$macro} eq 'ARRAY') {
448 $macro = join ' ', @{$self->{$macro}};
451 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
452 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
453 $macro = "\cB$macro\cB";
457 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
458 $npath = "$head$macro$tail";
461 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
465 # Deprecated. See the note above for eliminate_macros().
467 my($self,$path,$force_path) = @_;
468 return '' unless $path;
469 $self = bless {} unless ref $self;
470 my($fixedpath,$prefix,$name);
474 map { $self->fixpath($_,$force_path) }
478 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
479 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
480 $fixedpath = vmspath($self->eliminate_macros($path));
483 $fixedpath = vmsify($self->eliminate_macros($path));
486 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
487 my($vmspre) = $self->eliminate_macros("\$($prefix)");
488 # is it a dir or just a name?
489 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
490 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
491 $fixedpath = vmspath($fixedpath) if $force_path;
495 $fixedpath = vmspath($fixedpath) if $force_path;
497 # No hints, so we try to guess
498 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
499 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
502 # Trim off root dirname if it's had other dirs inserted in front of it.
503 $fixedpath =~ s/\.000000([\]>])/$1/;
504 # Special case for VMS absolute directory specs: these will have had device
505 # prepended during trip through Unix syntax in eliminate_macros(), since
506 # Unix syntax has no way to express "absolute from the top of this device's
508 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
517 Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
519 This program is free software; you can redistribute it and/or modify
520 it under the same terms as Perl itself.
524 See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
525 implementation of these methods, not the semantics.
527 An explanation of VMS file specs can be found at
528 L<"http://h71000.www7.hp.com/doc/731FINAL/4506/4506pro_014.html#apps_locating_naming_files">.