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) = @_;
138 if ($path =~ m|/|) { # Fake Unix
139 my $pathify = $path =~ m|/\z|;
140 $path = $self->SUPER::canonpath($path);
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
153 Concatenates a list of file specifications, and returns the result as a
154 VMS-syntax directory specification. No check is made for "impossible"
155 cases (e.g. elements other than the first being absolute filespecs).
160 my ($self,@dirs) = @_;
162 @dirs = grep($_,@dirs);
165 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
166 my ($spath,$sdir) = ($path,$dir);
167 $spath =~ s/\.dir\z//; $sdir =~ s/\.dir\z//;
168 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\z/s;
169 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
171 # Special case for VMS absolute directory specs: these will have had device
172 # prepended during trip through Unix syntax in eliminate_macros(), since
173 # Unix syntax has no way to express "absolute from the top of this device's
175 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
178 if ($dir =~ /^\$\([^\)]+\)\z/s) { $rslt = $dir; }
179 else { $rslt = vmspath($dir); }
186 Concatenates a list of file specifications, and returns the result as a
187 VMS-syntax file specification.
192 my ($self,@files) = @_;
193 my $file = pop @files;
194 @files = grep($_,@files);
197 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
199 $spath =~ s/\.dir\z//;
200 if ($spath =~ /^[^\)\]\/:>]+\)\z/s && basename($file) eq $file) {
201 $rslt = "$spath$file";
204 $rslt = $self->eliminate_macros($spath);
205 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
208 else { $rslt = vmsify($file); }
213 =item curdir (override)
215 Returns a string representation of the current directory: '[]'
223 =item devnull (override)
225 Returns a string representation of the null device: '_NLA0:'
233 =item rootdir (override)
235 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
240 return 'SYS$DISK:[000000]';
243 =item tmpdir (override)
245 Returns a string representation of the first writable directory
246 from the following list or '' if none are writable:
255 return $tmpdir if defined $tmpdir;
256 foreach ('/sys$scratch', $ENV{TMPDIR}) {
257 next unless defined && -d && -w _;
261 $tmpdir = '' unless defined $tmpdir;
265 =item updir (override)
267 Returns a string representation of the parent directory: '[-]'
275 =item case_tolerant (override)
277 VMS file specification syntax is case-tolerant.
285 =item path (override)
287 Translate logical name DCL$PATH as a searchlist, rather than trying
288 to C<split> string value of C<$ENV{'PATH'}>.
294 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
298 =item file_name_is_absolute (override)
300 Checks for VMS directory spec as well as Unix separators.
304 sub file_name_is_absolute {
305 my ($self,$file) = @_;
306 # If it's a logical name, expand it.
307 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\z/s && $ENV{$file};
308 return scalar($file =~ m!^/!s ||
309 $file =~ m![<\[][^.\-\]>]! ||
313 =item splitpath (override)
315 Splits using VMS syntax.
320 my($self,$path) = @_;
321 my($dev,$dir,$file) = ('','','');
323 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
324 return ($1 || '',$2 || '',$3);
327 =item splitdir (override)
329 Split dirspec using VMS syntax.
334 my($self,$dirspec) = @_;
335 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
336 my(@dirs) = split('\.', vmspath($dirspec));
337 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\z//s;
342 =item catpath (override)
344 Construct a complete filespec using VMS syntax
349 my($self,$dev,$dir,$file) = @_;
350 if ($dev =~ m|^/+([^/]+)|) { $dev =~ "$1:"; }
351 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\z/; }
352 $dir = vmspath($dir);
360 return File::Spec::Unix::abs2rel( $self, @_ )
361 if ( join( '', @_ ) =~ m{/} ) ;
363 my($path,$base) = @_;
365 # Note: we use '/' to glue things together here, then let canonpath()
366 # clean them up at the end.
369 if ( ! $self->file_name_is_absolute( $path ) ) {
370 $path = $self->rel2abs( $path ) ;
373 $path = $self->canonpath( $path ) ;
376 # Figure out the effective $base and clean it up.
377 if ( !defined( $base ) || $base eq '' ) {
380 elsif ( ! $self->file_name_is_absolute( $base ) ) {
381 $base = $self->rel2abs( $base ) ;
384 $base = $self->canonpath( $base ) ;
388 my ( undef, $path_directories, $path_file ) =
389 $self->splitpath( $path, 1 ) ;
391 $path_directories = $1
392 if $path_directories =~ /^\[(.*)\]\z/s ;
394 my ( undef, $base_directories, undef ) =
395 $self->splitpath( $base, 1 ) ;
397 $base_directories = $1
398 if $base_directories =~ /^\[(.*)\]\z/s ;
400 # Now, remove all leading components that are the same
401 my @pathchunks = $self->splitdir( $path_directories );
402 my @basechunks = $self->splitdir( $base_directories );
404 while ( @pathchunks &&
406 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
412 # @basechunks now contains the directories to climb out of,
413 # @pathchunks now has the directories to descend in to.
414 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
415 $path_directories =~ s{\.\z}{} ;
416 return $self->catpath( '', $path_directories, $path_file ) ;
422 return File::Spec::Unix::rel2abs( $self, @_ )
423 if ( join( '', @_ ) =~ m{/} ) ;
425 my ($path,$base ) = @_;
426 # Clean up and split up $path
427 if ( ! $self->file_name_is_absolute( $path ) ) {
428 # Figure out the effective $base and clean it up.
429 if ( !defined( $base ) || $base eq '' ) {
432 elsif ( ! $self->file_name_is_absolute( $base ) ) {
433 $base = $self->rel2abs( $base ) ;
436 $base = $self->canonpath( $base ) ;
440 my ( undef, $path_directories, $path_file ) =
441 $self->splitpath( $path ) ;
443 my ( $base_volume, $base_directories, undef ) =
444 $self->splitpath( $base ) ;
448 if ( $base_directories =~ m{[^.]\z} &&
449 $path_directories =~ m{^[^.]}s
451 $base_directories = "$base_directories$sep$path_directories" ;
453 $path = $self->catpath( $base_volume, $base_directories, $path_file );
456 return $self->canonpath( $path ) ;