1 package File::Spec::VMS;
5 require File::Spec::Unix;
6 @ISA = qw(File::Spec::Unix);
14 File::Spec::VMS - methods for VMS file specs
18 require File::Spec::VMS; # Done internally by File::Spec if needed
22 See File::Spec::Unix for a documentation of the methods provided
23 there. This package overrides the implementation of these methods, not
28 =item eliminate_macros
30 Expands MM[KS]/Make macros in a text string, using the contents of
31 identically named elements of C<%$self>, and returns the result
32 as a file specification in Unix syntax.
36 sub eliminate_macros {
38 return '' unless $path;
39 $self = {} unless ref $self;
40 my($npath) = unixify($path);
42 my($head,$macro,$tail);
44 # perform m##g in scalar context so it acts as an iterator
45 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
47 ($head,$macro,$tail) = ($1,$2,$3);
48 if (ref $self->{$macro}) {
49 if (ref $self->{$macro} eq 'ARRAY') {
50 $macro = join ' ', @{$self->{$macro}};
53 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
54 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
55 $macro = "\cB$macro\cB";
59 else { ($macro = unixify($self->{$macro})) =~ s#/\z##; }
60 $npath = "$head$macro$tail";
63 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
69 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
70 in any directory specification, in order to avoid juxtaposing two
71 VMS-syntax directories when MM[SK] is run. Also expands expressions which
72 are all macro, so that we can tell how long the expansion is, and avoid
73 overrunning DCL's command buffer when MM[KS] is running.
75 If optional second argument has a TRUE value, then the return string is
76 a VMS-syntax directory specification, if it is FALSE, the return string
77 is a VMS-syntax file specification, and if it is not specified, fixpath()
78 checks to see whether it matches the name of a directory in the current
79 default directory, and returns a directory or file specification accordingly.
84 my($self,$path,$force_path) = @_;
85 return '' unless $path;
86 $self = bless {} unless ref $self;
87 my($fixedpath,$prefix,$name);
89 if ($path =~ m#^\$\([^\)]+\)\z#s || $path =~ m#[/:>\]]#) {
90 if ($force_path or $path =~ /(?:DIR\)|\])\z/) {
91 $fixedpath = vmspath($self->eliminate_macros($path));
94 $fixedpath = vmsify($self->eliminate_macros($path));
97 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
98 my($vmspre) = $self->eliminate_macros("\$($prefix)");
99 # is it a dir or just a name?
100 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\z/) ? vmspath($vmspre) : '';
101 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
102 $fixedpath = vmspath($fixedpath) if $force_path;
106 $fixedpath = vmspath($fixedpath) if $force_path;
108 # No hints, so we try to guess
109 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
110 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
113 # Trim off root dirname if it's had other dirs inserted in front of it.
114 $fixedpath =~ s/\.000000([\]>])/$1/;
115 # Special case for VMS absolute directory specs: these will have had device
116 # prepended during trip through Unix syntax in eliminate_macros(), since
117 # Unix syntax has no way to express "absolute from the top of this device's
119 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
125 =head2 Methods always loaded
129 =item canonpath (override)
131 Removes redundant portions of file specifications according to VMS syntax
136 my($self,$path,$reduce_ricochet) = @_;
138 if ($path =~ m|/|) { # Fake Unix
139 my $pathify = $path =~ m|/\z|;
140 $path = $self->SUPER::canonpath($path,$reduce_ricochet);
141 if ($pathify) { return vmspath($path); }
142 else { return vmsify($path); }
145 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
146 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo
147 if ($reduce_ricochet) {
148 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/g;
149 $path =~ s/([\[<\.])([^\[<\.]+)\.-\.?/$1/g;
157 Concatenates a list of file specifications, and returns the result as a
158 VMS-syntax directory specification. No check is made for "impossible"
159 cases (e.g. elements other than the first being absolute filespecs).
164 my ($self,@dirs) = @_;
166 @dirs = grep($_,@dirs);
169 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
170 my ($spath,$sdir) = ($path,$dir);
171 $spath =~ s/\.dir\z//; $sdir =~ s/\.dir\z//;
172 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\z/s;
173 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
175 # Special case for VMS absolute directory specs: these will have had device
176 # prepended during trip through Unix syntax in eliminate_macros(), since
177 # Unix syntax has no way to express "absolute from the top of this device's
179 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
182 if ($dir =~ /^\$\([^\)]+\)\z/s) { $rslt = $dir; }
183 else { $rslt = vmspath($dir); }
190 Concatenates a list of file specifications, and returns the result as a
191 VMS-syntax file specification.
196 my ($self,@files) = @_;
197 my $file = pop @files;
198 @files = grep($_,@files);
201 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
203 $spath =~ s/\.dir\z//;
204 if ($spath =~ /^[^\)\]\/:>]+\)\z/s && basename($file) eq $file) {
205 $rslt = "$spath$file";
208 $rslt = $self->eliminate_macros($spath);
209 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
212 else { $rslt = vmsify($file); }
217 =item curdir (override)
219 Returns a string representation of the current directory: '[]'
227 =item devnull (override)
229 Returns a string representation of the null device: '_NLA0:'
237 =item rootdir (override)
239 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
244 return 'SYS$DISK:[000000]';
247 =item tmpdir (override)
249 Returns a string representation of the first writable directory
250 from the following list or '' if none are writable:
259 return $tmpdir if defined $tmpdir;
260 foreach ('/sys$scratch', $ENV{TMPDIR}) {
261 next unless defined && -d && -w _;
265 $tmpdir = '' unless defined $tmpdir;
269 =item updir (override)
271 Returns a string representation of the parent directory: '[-]'
279 =item case_tolerant (override)
281 VMS file specification syntax is case-tolerant.
289 =item path (override)
291 Translate logical name DCL$PATH as a searchlist, rather than trying
292 to C<split> string value of C<$ENV{'PATH'}>.
298 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
302 =item file_name_is_absolute (override)
304 Checks for VMS directory spec as well as Unix separators.
308 sub file_name_is_absolute {
309 my ($self,$file) = @_;
310 # If it's a logical name, expand it.
311 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\z/s && $ENV{$file};
312 return scalar($file =~ m!^/!s ||
313 $file =~ m![<\[][^.\-\]>]! ||
317 =item splitpath (override)
319 Splits using VMS syntax.
324 my($self,$path) = @_;
325 my($dev,$dir,$file) = ('','','');
327 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
328 return ($1 || '',$2 || '',$3);
331 =item splitdir (override)
333 Split dirspec using VMS syntax.
338 my($self,$dirspec) = @_;
339 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
340 my(@dirs) = split('\.', vmspath($dirspec));
341 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\z//s;
346 =item catpath (override)
348 Construct a complete filespec using VMS syntax
353 my($self,$dev,$dir,$file) = @_;
354 if ($dev =~ m|^/+([^/]+)|) { $dev =~ "$1:"; }
355 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\z/; }
356 $dir = vmspath($dir);
362 ($volume,$directories,$file) = File::Spec->splitpath( $path );
363 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
365 Splits a VMS path in to volume, directory, and filename portions.
366 Ignores $no_file, if present, since VMS paths indicate the 'fileness' of a
369 The results can be passed to L</catpath()> to get back a path equivalent to
370 (usually identical to) the original path.
376 my ($path, $nofile) = @_;
378 my ($volume,$directory,$file) ;
380 if ( $path =~ m{/} ) {
382 m{^ ( (?: /[^/]* )? )
383 ( (?: .*/(?:[^/]+\.dir)? )? )
392 m{^ ( (?: (?: (?: [\w\$-]+ (?: "[^"]*")?:: )? [\w\$-]+: )? ) )
402 if $directory =~ /^\[(.*)\]\z/s ;
404 return ($volume,$directory,$file);
410 The opposite of L</catdir()>.
412 @dirs = File::Spec->splitdir( $directories );
414 $directories must be only the directory portion of the path.
416 '[' and ']' delimiters are optional. An empty string argument is
417 equivalent to '[]': both return an array with no elements.
423 my $directories = $_[0] ;
425 return File::Spec::Unix::splitdir( $self, @_ )
426 if ( $directories =~ m{/} ) ;
428 $directories =~ s/^\[(.*)\]\z/$1/s ;
431 # split() likes to forget about trailing null fields, so here we
432 # check to be sure that there will not be any before handling the
435 if ( $directories !~ m{\.\z} ) {
436 return split( m{\.}, $directories );
440 # since there was a trailing separator, add a file name to the end,
441 # then do the split, then replace it with ''.
443 my( @directories )= split( m{\.}, "${directories}dummy" ) ;
444 $directories[ $#directories ]= '' ;
445 return @directories ;
453 return File::Spec::Unix::catpath( $self, @_ )
454 if ( join( '', @_ ) =~ m{/} ) ;
456 my ($volume,$directory,$file) = @_;
459 if $volume =~ /[^:]\z/ ;
461 $directory = "[$directory"
462 if $directory =~ /^[^\[]/s ;
465 if $directory =~ /[^\]]\z/ ;
467 return "$volume$directory$file" ;
474 return File::Spec::Unix::abs2rel( $self, @_ )
475 if ( join( '', @_ ) =~ m{/} ) ;
477 my($path,$base) = @_;
479 # Note: we use '/' to glue things together here, then let canonpath()
480 # clean them up at the end.
483 if ( ! $self->file_name_is_absolute( $path ) ) {
484 $path = $self->rel2abs( $path ) ;
487 $path = $self->canonpath( $path ) ;
490 # Figure out the effective $base and clean it up.
491 if ( !defined( $base ) || $base eq '' ) {
494 elsif ( ! $self->file_name_is_absolute( $base ) ) {
495 $base = $self->rel2abs( $base ) ;
498 $base = $self->canonpath( $base ) ;
502 my ( undef, $path_directories, $path_file ) =
503 $self->splitpath( $path, 1 ) ;
505 $path_directories = $1
506 if $path_directories =~ /^\[(.*)\]\z/s ;
508 my ( undef, $base_directories, undef ) =
509 $self->splitpath( $base, 1 ) ;
511 $base_directories = $1
512 if $base_directories =~ /^\[(.*)\]\z/s ;
514 # Now, remove all leading components that are the same
515 my @pathchunks = $self->splitdir( $path_directories );
516 my @basechunks = $self->splitdir( $base_directories );
518 while ( @pathchunks &&
520 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
526 # @basechunks now contains the directories to climb out of,
527 # @pathchunks now has the directories to descend in to.
528 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
529 $path_directories =~ s{\.\z}{} ;
530 return $self->catpath( '', $path_directories, $path_file ) ;
536 return File::Spec::Unix::rel2abs( $self, @_ )
537 if ( join( '', @_ ) =~ m{/} ) ;
539 my ($path,$base ) = @_;
540 # Clean up and split up $path
541 if ( ! $self->file_name_is_absolute( $path ) ) {
542 # Figure out the effective $base and clean it up.
543 if ( !defined( $base ) || $base eq '' ) {
546 elsif ( ! $self->file_name_is_absolute( $base ) ) {
547 $base = $self->rel2abs( $base ) ;
550 $base = $self->canonpath( $base ) ;
554 my ( undef, $path_directories, $path_file ) =
555 $self->splitpath( $path ) ;
557 my ( $base_volume, $base_directories, undef ) =
558 $self->splitpath( $base ) ;
562 if ( $base_directories =~ m{[^.]\z} &&
563 $path_directories =~ m{^[^.]}s
565 $base_directories = "$base_directories$sep$path_directories" ;
567 $path = $self->catpath( $base_volume, $base_directories, $path_file );
570 return $self->canonpath( $path ) ;