1 package File::Spec::VMS;
4 use vars qw(@ISA $VERSION);
5 require File::Spec::Unix;
6 @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;
42 my($npath) = unixify($path);
44 my($head,$macro,$tail);
46 # perform m##g in scalar context so it acts as an iterator
47 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
49 ($head,$macro,$tail) = ($1,$2,$3);
50 if (ref $self->{$macro}) {
51 if (ref $self->{$macro} eq 'ARRAY') {
52 $macro = join ' ', @{$self->{$macro}};
55 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
56 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
57 $macro = "\cB$macro\cB";
61 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
62 $npath = "$head$macro$tail";
65 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
71 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
72 in any directory specification, in order to avoid juxtaposing two
73 VMS-syntax directories when MM[SK] is run. Also expands expressions which
74 are all macro, so that we can tell how long the expansion is, and avoid
75 overrunning DCL's command buffer when MM[KS] is running.
77 If optional second argument has a TRUE value, then the return string is
78 a VMS-syntax directory specification, if it is FALSE, the return string
79 is a VMS-syntax file specification, and if it is not specified, fixpath()
80 checks to see whether it matches the name of a directory in the current
81 default directory, and returns a directory or file specification accordingly.
86 my($self,$path,$force_path) = @_;
87 return '' unless $path;
88 $self = bless {} unless ref $self;
89 my($fixedpath,$prefix,$name);
91 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
92 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
93 $fixedpath = vmspath($self->eliminate_macros($path));
96 $fixedpath = vmsify($self->eliminate_macros($path));
99 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
100 my($vmspre) = $self->eliminate_macros("\$($prefix)");
101 # is it a dir or just a name?
102 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
103 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
104 $fixedpath = vmspath($fixedpath) if $force_path;
108 $fixedpath = vmspath($fixedpath) if $force_path;
110 # No hints, so we try to guess
111 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
112 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
115 # Trim off root dirname if it's had other dirs inserted in front of it.
116 $fixedpath =~ s/\.000000([\]>])/$1/;
117 # Special case for VMS absolute directory specs: these will have had device
118 # prepended during trip through Unix syntax in eliminate_macros(), since
119 # Unix syntax has no way to express "absolute from the top of this device's
121 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
127 =head2 Methods always loaded
131 =item canonpath (override)
133 Removes redundant portions of file specifications according to VMS syntax.
138 my($self,$path) = @_;
140 if ($path =~ m|/|) { # Fake Unix
141 my $pathify = $path =~ m|/\Z(?!\n)|;
142 $path = $self->SUPER::canonpath($path);
143 if ($pathify) { return vmspath($path); }
144 else { return vmsify($path); }
147 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
148 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo
149 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
150 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
151 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
152 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
153 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
160 Concatenates a list of file specifications, and returns the result as a
161 VMS-syntax directory specification. No check is made for "impossible"
162 cases (e.g. elements other than the first being absolute filespecs).
167 my ($self,@dirs) = @_;
169 @dirs = grep($_,@dirs);
172 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
173 my ($spath,$sdir) = ($path,$dir);
174 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
175 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
176 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
178 # Special case for VMS absolute directory specs: these will have had device
179 # prepended during trip through Unix syntax in eliminate_macros(), since
180 # Unix syntax has no way to express "absolute from the top of this device's
182 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
185 if (not defined $dir or not length $dir) { $rslt = ''; }
186 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
187 else { $rslt = vmspath($dir); }
189 return $self->canonpath($rslt);
194 Concatenates a list of file specifications, and returns the result as a
195 VMS-syntax file specification.
200 my ($self,@files) = @_;
201 my $file = pop @files;
202 @files = grep($_,@files);
205 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
207 $spath =~ s/\.dir\Z(?!\n)//;
208 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
209 $rslt = "$spath$file";
212 $rslt = $self->eliminate_macros($spath);
213 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
216 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
217 return $self->canonpath($rslt);
221 =item curdir (override)
223 Returns a string representation of the current directory: '[]'
231 =item devnull (override)
233 Returns a string representation of the null device: '_NLA0:'
241 =item rootdir (override)
243 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
248 return 'SYS$DISK:[000000]';
251 =item tmpdir (override)
253 Returns a string representation of the first writable directory
254 from the following list or '' if none are writable:
263 return $tmpdir if defined $tmpdir;
264 foreach ('sys$scratch', $ENV{TMPDIR}) {
265 next unless defined && -d && -w _;
269 $tmpdir = '' unless defined $tmpdir;
273 =item updir (override)
275 Returns a string representation of the parent directory: '[-]'
283 =item case_tolerant (override)
285 VMS file specification syntax is case-tolerant.
293 =item path (override)
295 Translate logical name DCL$PATH as a searchlist, rather than trying
296 to C<split> string value of C<$ENV{'PATH'}>.
302 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
306 =item file_name_is_absolute (override)
308 Checks for VMS directory spec as well as Unix separators.
312 sub file_name_is_absolute {
313 my ($self,$file) = @_;
314 # If it's a logical name, expand it.
315 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
316 return scalar($file =~ m!^/!s ||
317 $file =~ m![<\[][^.\-\]>]! ||
321 =item splitpath (override)
323 Splits using VMS syntax.
328 my($self,$path) = @_;
329 my($dev,$dir,$file) = ('','','');
331 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
332 return ($1 || '',$2 || '',$3);
335 =item splitdir (override)
337 Split dirspec using VMS syntax.
342 my($self,$dirspec) = @_;
343 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
344 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
345 my(@dirs) = split('\.', vmspath($dirspec));
346 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
351 =item catpath (override)
353 Construct a complete filespec using VMS syntax
358 my($self,$dev,$dir,$file) = @_;
359 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
360 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
361 if (length($dev) or length($dir)) {
362 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
363 $dir = vmspath($dir);
368 =item abs2rel (override)
370 Use VMS syntax when converting filespecs.
377 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
378 if ( join( '', @_ ) =~ m{/} ) ;
380 my($path,$base) = @_;
382 # Note: we use '/' to glue things together here, then let canonpath()
383 # clean them up at the end.
386 if ( ! $self->file_name_is_absolute( $path ) ) {
387 $path = $self->rel2abs( $path ) ;
390 $path = $self->canonpath( $path ) ;
393 # Figure out the effective $base and clean it up.
394 if ( !defined( $base ) || $base eq '' ) {
397 elsif ( ! $self->file_name_is_absolute( $base ) ) {
398 $base = $self->rel2abs( $base ) ;
401 $base = $self->canonpath( $base ) ;
405 my ( $path_directories, $path_file ) =
406 ($self->splitpath( $path, 1 ))[1,2] ;
408 $path_directories = $1
409 if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
411 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
413 $base_directories = $1
414 if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
416 # Now, remove all leading components that are the same
417 my @pathchunks = $self->splitdir( $path_directories );
418 my @basechunks = $self->splitdir( $base_directories );
420 while ( @pathchunks &&
422 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
428 # @basechunks now contains the directories to climb out of,
429 # @pathchunks now has the directories to descend in to.
430 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
431 $path_directories =~ s{\.\Z(?!\n)}{} ;
432 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
436 =item rel2abs (override)
438 Use VMS syntax when converting filespecs.
444 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
445 if ( join( '', @_ ) =~ m{/} ) ;
447 my ($path,$base ) = @_;
448 # Clean up and split up $path
449 if ( ! $self->file_name_is_absolute( $path ) ) {
450 # Figure out the effective $base and clean it up.
451 if ( !defined( $base ) || $base eq '' ) {
454 elsif ( ! $self->file_name_is_absolute( $base ) ) {
455 $base = $self->rel2abs( $base ) ;
458 $base = $self->canonpath( $base ) ;
462 my ( $path_directories, $path_file ) =
463 ($self->splitpath( $path ))[1,2] ;
465 my ( $base_volume, $base_directories ) =
466 $self->splitpath( $base ) ;
468 $path_directories = '' if $path_directories eq '[]' ||
469 $path_directories eq '<>';
472 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
473 $path_directories =~ m{^[^.\[<]}s
475 $base_directories = "$base_directories$sep$path_directories";
476 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
478 $path = $self->catpath( $base_volume, $base_directories, $path_file );
481 return $self->canonpath( $path ) ;