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 eliminate_macros
32 Expands MM[KS]/Make macros in a text string, using the contents of
33 identically named elements of C<%$self>, and returns the result
34 as a file specification in Unix syntax.
38 sub eliminate_macros {
40 return '' unless $path;
41 $self = {} unless ref $self;
44 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
47 my($npath) = unixify($path);
49 my($head,$macro,$tail);
51 # perform m##g in scalar context so it acts as an iterator
52 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
54 ($head,$macro,$tail) = ($1,$2,$3);
55 if (ref $self->{$macro}) {
56 if (ref $self->{$macro} eq 'ARRAY') {
57 $macro = join ' ', @{$self->{$macro}};
60 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
61 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
62 $macro = "\cB$macro\cB";
66 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
67 $npath = "$head$macro$tail";
70 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
76 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
77 in any directory specification, in order to avoid juxtaposing two
78 VMS-syntax directories when MM[SK] is run. Also expands expressions which
79 are all macro, so that we can tell how long the expansion is, and avoid
80 overrunning DCL's command buffer when MM[KS] is running.
82 If optional second argument has a TRUE value, then the return string is
83 a VMS-syntax directory specification, if it is FALSE, the return string
84 is a VMS-syntax file specification, and if it is not specified, fixpath()
85 checks to see whether it matches the name of a directory in the current
86 default directory, and returns a directory or file specification accordingly.
91 my($self,$path,$force_path) = @_;
92 return '' unless $path;
93 $self = bless {} unless ref $self;
94 my($fixedpath,$prefix,$name);
98 map { $self->fixpath($_,$force_path) }
102 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
103 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
104 $fixedpath = vmspath($self->eliminate_macros($path));
107 $fixedpath = vmsify($self->eliminate_macros($path));
110 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
111 my($vmspre) = $self->eliminate_macros("\$($prefix)");
112 # is it a dir or just a name?
113 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
114 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
115 $fixedpath = vmspath($fixedpath) if $force_path;
119 $fixedpath = vmspath($fixedpath) if $force_path;
121 # No hints, so we try to guess
122 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
123 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
126 # Trim off root dirname if it's had other dirs inserted in front of it.
127 $fixedpath =~ s/\.000000([\]>])/$1/;
128 # Special case for VMS absolute directory specs: these will have had device
129 # prepended during trip through Unix syntax in eliminate_macros(), since
130 # Unix syntax has no way to express "absolute from the top of this device's
132 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
138 =head2 Methods always loaded
142 =item canonpath (override)
144 Removes redundant portions of file specifications according to VMS syntax.
149 my($self,$path) = @_;
151 if ($path =~ m|/|) { # Fake Unix
152 my $pathify = $path =~ m|/\Z(?!\n)|;
153 $path = $self->SUPER::canonpath($path);
154 if ($pathify) { return vmspath($path); }
155 else { return vmsify($path); }
158 $path =~ s/([\[<])000000\./$1/g; # [000000.foo ==> [foo
159 $path =~ s/([^-]+)\.(\]\[|><)?000000([\]\>])/$1$3/g; # foo.000000] ==> foo]
160 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
161 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
162 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
163 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
164 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
165 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
166 $path =~ s/^[\[<\]>]{2}//; # []foo ==> foo
173 Concatenates a list of file specifications, and returns the result as a
174 VMS-syntax directory specification. No check is made for "impossible"
175 cases (e.g. elements other than the first being absolute filespecs).
180 my ($self,@dirs) = @_;
182 @dirs = grep($_,@dirs);
185 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
186 my ($spath,$sdir) = ($path,$dir);
187 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
188 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
189 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
191 # Special case for VMS absolute directory specs: these will have had device
192 # prepended during trip through Unix syntax in eliminate_macros(), since
193 # Unix syntax has no way to express "absolute from the top of this device's
195 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
198 if (not defined $dir or not length $dir) { $rslt = ''; }
199 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
200 else { $rslt = vmspath($dir); }
202 return $self->canonpath($rslt);
207 Concatenates a list of file specifications, and returns the result as a
208 VMS-syntax file specification.
213 my ($self,@files) = @_;
214 my $file = $self->canonpath(pop @files);
215 @files = grep($_,@files);
218 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
220 $spath =~ s/\.dir\Z(?!\n)//;
221 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
222 $rslt = "$spath$file";
225 $rslt = $self->eliminate_macros($spath);
226 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
229 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
230 return $self->canonpath($rslt);
234 =item curdir (override)
236 Returns a string representation of the current directory: '[]'
244 =item devnull (override)
246 Returns a string representation of the null device: '_NLA0:'
254 =item rootdir (override)
256 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
261 return 'SYS$DISK:[000000]';
264 =item tmpdir (override)
266 Returns a string representation of the first writable directory
267 from the following list or '' if none are writable:
272 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
273 is tainted, it is not used.
279 return $tmpdir if defined $tmpdir;
281 $tmpdir = $self->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
284 =item updir (override)
286 Returns a string representation of the parent directory: '[-]'
294 =item case_tolerant (override)
296 VMS file specification syntax is case-tolerant.
304 =item path (override)
306 Translate logical name DCL$PATH as a searchlist, rather than trying
307 to C<split> string value of C<$ENV{'PATH'}>.
313 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
317 =item file_name_is_absolute (override)
319 Checks for VMS directory spec as well as Unix separators.
323 sub file_name_is_absolute {
324 my ($self,$file) = @_;
325 # If it's a logical name, expand it.
326 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
327 return scalar($file =~ m!^/!s ||
328 $file =~ m![<\[][^.\-\]>]! ||
332 =item splitpath (override)
334 Splits using VMS syntax.
339 my($self,$path) = @_;
340 my($dev,$dir,$file) = ('','','');
342 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
343 return ($1 || '',$2 || '',$3);
346 =item splitdir (override)
348 Split dirspec using VMS syntax.
353 my($self,$dirspec) = @_;
354 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
355 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
356 my(@dirs) = split('\.', vmspath($dirspec));
357 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
362 =item catpath (override)
364 Construct a complete filespec using VMS syntax
369 my($self,$dev,$dir,$file) = @_;
371 # We look for a volume in $dev, then in $dir, but not both
372 my ($dir_volume, $dir_dir, $dir_file) = $self->splitpath($dir);
373 $dev = $dir_volume unless length $dev;
374 $dir = length $dir_file ? $self->catfile($dir_dir, $dir_file) : $dir_dir;
376 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
377 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
378 if (length($dev) or length($dir)) {
379 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
380 $dir = vmspath($dir);
385 =item abs2rel (override)
387 Use VMS syntax when converting filespecs.
393 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
396 my($path,$base) = @_;
397 $base = $self->_cwd() unless defined $base and length $base;
399 for ($path, $base) { $_ = $self->canonpath($_) }
401 # Are we even starting $path on the same (node::)device as $base? Note that
402 # logical paths or nodename differences may be on the "same device"
403 # but the comparison that ignores device differences so as to concatenate
404 # [---] up directory specs is not even a good idea in cases where there is
405 # a logical path difference between $path and $base nodename and/or device.
406 # Hence we fall back to returning the absolute $path spec
407 # if there is a case blind device (or node) difference of any sort
408 # and we do not even try to call $parse() or consult %ENV for $trnlnm()
409 # (this module needs to run on non VMS platforms after all).
411 my ($path_volume, $path_directories, $path_file) = $self->splitpath($path);
412 my ($base_volume, $base_directories, $base_file) = $self->splitpath($base);
413 return $path unless lc($path_volume) eq lc($base_volume);
415 for ($path, $base) { $_ = $self->rel2abs($_) }
417 # Now, remove all leading components that are the same
418 my @pathchunks = $self->splitdir( $path_directories );
419 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
420 my @basechunks = $self->splitdir( $base_directories );
421 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
423 while ( @pathchunks &&
425 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
431 # @basechunks now contains the directories to climb out of,
432 # @pathchunks now has the directories to descend in to.
433 $path_directories = join '.', ('-' x @basechunks, @pathchunks) ;
434 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
438 =item rel2abs (override)
440 Use VMS syntax when converting filespecs.
446 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
447 if ( join( '', @_ ) =~ m{/} ) ;
449 my ($path,$base ) = @_;
450 # Clean up and split up $path
451 if ( ! $self->file_name_is_absolute( $path ) ) {
452 # Figure out the effective $base and clean it up.
453 if ( !defined( $base ) || $base eq '' ) {
456 elsif ( ! $self->file_name_is_absolute( $base ) ) {
457 $base = $self->rel2abs( $base ) ;
460 $base = $self->canonpath( $base ) ;
464 my ( $path_directories, $path_file ) =
465 ($self->splitpath( $path ))[1,2] ;
467 my ( $base_volume, $base_directories ) =
468 $self->splitpath( $base ) ;
470 $path_directories = '' if $path_directories eq '[]' ||
471 $path_directories eq '<>';
474 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
475 $path_directories =~ m{^[^.\[<]}s
477 $base_directories = "$base_directories$sep$path_directories";
478 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
480 $path = $self->catpath( $base_volume, $base_directories, $path_file );
483 return $self->canonpath( $path ) ;
491 See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
492 implementation of these methods, not the semantics.
494 An explanation of VMS file specs can be found at
495 L<"http://h71000.www7.hp.com/doc/731FINAL/4506/4506pro_014.html#apps_locating_naming_files">.