1 package File::Spec::VMS;
4 use vars qw(@ISA $VERSION);
5 require File::Spec::Unix;
9 @ISA = qw(File::Spec::Unix);
17 File::Spec::VMS - methods for VMS file specs
21 require File::Spec::VMS; # Done internally by File::Spec if needed
25 See File::Spec::Unix for a documentation of the methods provided
26 there. This package overrides the implementation of these methods, not
31 =item eliminate_macros
33 Expands MM[KS]/Make macros in a text string, using the contents of
34 identically named elements of C<%$self>, and returns the result
35 as a file specification in Unix syntax.
39 sub eliminate_macros {
41 return '' unless $path;
42 $self = {} unless ref $self;
45 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
48 my($npath) = unixify($path);
50 my($head,$macro,$tail);
52 # perform m##g in scalar context so it acts as an iterator
53 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
55 ($head,$macro,$tail) = ($1,$2,$3);
56 if (ref $self->{$macro}) {
57 if (ref $self->{$macro} eq 'ARRAY') {
58 $macro = join ' ', @{$self->{$macro}};
61 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
62 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
63 $macro = "\cB$macro\cB";
67 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
68 $npath = "$head$macro$tail";
71 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
77 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
78 in any directory specification, in order to avoid juxtaposing two
79 VMS-syntax directories when MM[SK] is run. Also expands expressions which
80 are all macro, so that we can tell how long the expansion is, and avoid
81 overrunning DCL's command buffer when MM[KS] is running.
83 If optional second argument has a TRUE value, then the return string is
84 a VMS-syntax directory specification, if it is FALSE, the return string
85 is a VMS-syntax file specification, and if it is not specified, fixpath()
86 checks to see whether it matches the name of a directory in the current
87 default directory, and returns a directory or file specification accordingly.
92 my($self,$path,$force_path) = @_;
93 return '' unless $path;
94 $self = bless {} unless ref $self;
95 my($fixedpath,$prefix,$name);
99 map { $self->fixpath($_,$force_path) }
103 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
104 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
105 $fixedpath = vmspath($self->eliminate_macros($path));
108 $fixedpath = vmsify($self->eliminate_macros($path));
111 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
112 my($vmspre) = $self->eliminate_macros("\$($prefix)");
113 # is it a dir or just a name?
114 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
115 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
116 $fixedpath = vmspath($fixedpath) if $force_path;
120 $fixedpath = vmspath($fixedpath) if $force_path;
122 # No hints, so we try to guess
123 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
124 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
127 # Trim off root dirname if it's had other dirs inserted in front of it.
128 $fixedpath =~ s/\.000000([\]>])/$1/;
129 # Special case for VMS absolute directory specs: these will have had device
130 # prepended during trip through Unix syntax in eliminate_macros(), since
131 # Unix syntax has no way to express "absolute from the top of this device's
133 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
139 =head2 Methods always loaded
143 =item canonpath (override)
145 Removes redundant portions of file specifications according to VMS syntax.
150 my($self,$path) = @_;
152 if ($path =~ m|/|) { # Fake Unix
153 my $pathify = $path =~ m|/\Z(?!\n)|;
154 $path = $self->SUPER::canonpath($path);
155 if ($pathify) { return vmspath($path); }
156 else { return vmsify($path); }
159 $path =~ s/([\[<])000000\./$1/g; # [000000.foo ==> [foo
160 $path =~ s/([^-]+)\.(\]\[|><)?000000([\]\>])/$1$3/g; # foo.000000] ==> foo]
161 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
162 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
163 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
164 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
165 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
166 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
167 $path =~ s/^[\[<\]>]{2}//; # []foo ==> foo
174 Concatenates a list of file specifications, and returns the result as a
175 VMS-syntax directory specification. No check is made for "impossible"
176 cases (e.g. elements other than the first being absolute filespecs).
181 my ($self,@dirs) = @_;
183 @dirs = grep($_,@dirs);
186 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
187 my ($spath,$sdir) = ($path,$dir);
188 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
189 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
190 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
192 # Special case for VMS absolute directory specs: these will have had device
193 # prepended during trip through Unix syntax in eliminate_macros(), since
194 # Unix syntax has no way to express "absolute from the top of this device's
196 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
199 if (not defined $dir or not length $dir) { $rslt = ''; }
200 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
201 else { $rslt = vmspath($dir); }
203 return $self->canonpath($rslt);
208 Concatenates a list of file specifications, and returns the result as a
209 VMS-syntax file specification.
214 my ($self,@files) = @_;
215 my $file = $self->canonpath(pop @files);
216 @files = grep($_,@files);
219 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
221 $spath =~ s/\.dir\Z(?!\n)//;
222 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
223 $rslt = "$spath$file";
226 $rslt = $self->eliminate_macros($spath);
227 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
230 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
231 return $self->canonpath($rslt);
235 =item curdir (override)
237 Returns a string representation of the current directory: '[]'
245 =item devnull (override)
247 Returns a string representation of the null device: '_NLA0:'
255 =item rootdir (override)
257 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
262 return 'SYS$DISK:[000000]';
265 =item tmpdir (override)
267 Returns a string representation of the first writable directory
268 from the following list or '' if none are writable:
273 Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
274 is tainted, it is not used.
280 return $tmpdir if defined $tmpdir;
281 my @dirlist = ('sys$scratch:', $ENV{TMPDIR});
284 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
285 require Scalar::Util;
286 pop @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
290 next unless defined && -d && -w _;
294 $tmpdir = File::Spec->curdir unless defined $tmpdir;
298 =item updir (override)
300 Returns a string representation of the parent directory: '[-]'
308 =item case_tolerant (override)
310 VMS file specification syntax is case-tolerant.
318 =item path (override)
320 Translate logical name DCL$PATH as a searchlist, rather than trying
321 to C<split> string value of C<$ENV{'PATH'}>.
327 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
331 =item file_name_is_absolute (override)
333 Checks for VMS directory spec as well as Unix separators.
337 sub file_name_is_absolute {
338 my ($self,$file) = @_;
339 # If it's a logical name, expand it.
340 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
341 return scalar($file =~ m!^/!s ||
342 $file =~ m![<\[][^.\-\]>]! ||
346 =item splitpath (override)
348 Splits using VMS syntax.
353 my($self,$path) = @_;
354 my($dev,$dir,$file) = ('','','');
356 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
357 return ($1 || '',$2 || '',$3);
360 =item splitdir (override)
362 Split dirspec using VMS syntax.
367 my($self,$dirspec) = @_;
368 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
369 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
370 my(@dirs) = split('\.', vmspath($dirspec));
371 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
376 =item catpath (override)
378 Construct a complete filespec using VMS syntax
383 my($self,$dev,$dir,$file) = @_;
384 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
385 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
386 if (length($dev) or length($dir)) {
387 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
388 $dir = vmspath($dir);
393 =item abs2rel (override)
395 Use VMS syntax when converting filespecs.
402 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
403 if ( join( '', @_ ) =~ m{/} ) ;
405 my($path,$base) = @_;
407 # Note: we use '/' to glue things together here, then let canonpath()
408 # clean them up at the end.
411 if ( ! $self->file_name_is_absolute( $path ) ) {
412 $path = $self->rel2abs( $path ) ;
415 $path = $self->canonpath( $path ) ;
418 # Figure out the effective $base and clean it up.
419 if ( !defined( $base ) || $base eq '' ) {
421 $base = $self->canonpath( $base ) ;
423 elsif ( ! $self->file_name_is_absolute( $base ) ) {
424 $base = $self->rel2abs( $base ) ;
427 $base = $self->canonpath( $base ) ;
431 my ( $path_directories, $path_file ) =
432 ($self->splitpath( $path, 1 ))[1,2] ;
434 $path_directories = $1
435 if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
437 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
439 $base_directories = $1
440 if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
442 # Now, remove all leading components that are the same
443 my @pathchunks = $self->splitdir( $path_directories );
444 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
445 my @basechunks = $self->splitdir( $base_directories );
446 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
448 while ( @pathchunks &&
450 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
456 # @basechunks now contains the directories to climb out of,
457 # @pathchunks now has the directories to descend in to.
458 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
459 $path_directories =~ s{\.\Z(?!\n)}{} ;
460 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
464 =item rel2abs (override)
466 Use VMS syntax when converting filespecs.
472 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
473 if ( join( '', @_ ) =~ m{/} ) ;
475 my ($path,$base ) = @_;
476 # Clean up and split up $path
477 if ( ! $self->file_name_is_absolute( $path ) ) {
478 # Figure out the effective $base and clean it up.
479 if ( !defined( $base ) || $base eq '' ) {
482 elsif ( ! $self->file_name_is_absolute( $base ) ) {
483 $base = $self->rel2abs( $base ) ;
486 $base = $self->canonpath( $base ) ;
490 my ( $path_directories, $path_file ) =
491 ($self->splitpath( $path ))[1,2] ;
493 my ( $base_volume, $base_directories ) =
494 $self->splitpath( $base ) ;
496 $path_directories = '' if $path_directories eq '[]' ||
497 $path_directories eq '<>';
500 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
501 $path_directories =~ m{^[^.\[<]}s
503 $base_directories = "$base_directories$sep$path_directories";
504 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
506 $path = $self->catpath( $base_volume, $base_directories, $path_file );
509 return $self->canonpath( $path ) ;