1 package File::Spec::VMS;
5 require File::Spec::Unix;
6 @ISA = qw(File::Spec::Unix);
13 File::Spec::VMS - methods for VMS file specs
17 require File::Spec::VMS; # Done internally by File::Spec if needed
21 See File::Spec::Unix for a documentation of the methods provided
22 there. This package overrides the implementation of these methods, not
27 =item eliminate_macros
29 Expands MM[KS]/Make macros in a text string, using the contents of
30 identically named elements of C<%$self>, and returns the result
31 as a file specification in Unix syntax.
35 sub eliminate_macros {
37 return '' unless $path;
38 $self = {} unless ref $self;
39 my($npath) = unixify($path);
41 my($head,$macro,$tail);
43 # perform m##g in scalar context so it acts as an iterator
44 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#g) {
46 ($head,$macro,$tail) = ($1,$2,$3);
47 if (ref $self->{$macro}) {
48 if (ref $self->{$macro} eq 'ARRAY') {
49 $macro = join ' ', @{$self->{$macro}};
52 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
53 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
54 $macro = "\cB$macro\cB";
58 else { ($macro = unixify($self->{$macro})) =~ s#/$##; }
59 $npath = "$head$macro$tail";
62 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#g; }
68 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
69 in any directory specification, in order to avoid juxtaposing two
70 VMS-syntax directories when MM[SK] is run. Also expands expressions which
71 are all macro, so that we can tell how long the expansion is, and avoid
72 overrunning DCL's command buffer when MM[KS] is running.
74 If optional second argument has a TRUE value, then the return string is
75 a VMS-syntax directory specification, if it is FALSE, the return string
76 is a VMS-syntax file specification, and if it is not specified, fixpath()
77 checks to see whether it matches the name of a directory in the current
78 default directory, and returns a directory or file specification accordingly.
83 my($self,$path,$force_path) = @_;
84 return '' unless $path;
85 $self = bless {} unless ref $self;
86 my($fixedpath,$prefix,$name);
88 if ($path =~ m#^\$\([^\)]+\)$# || $path =~ m#[/:>\]]#) {
89 if ($force_path or $path =~ /(?:DIR\)|\])$/) {
90 $fixedpath = vmspath($self->eliminate_macros($path));
93 $fixedpath = vmsify($self->eliminate_macros($path));
96 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#)) && $self->{$prefix}) {
97 my($vmspre) = $self->eliminate_macros("\$($prefix)");
98 # is it a dir or just a name?
99 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR$/) ? vmspath($vmspre) : '';
100 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
101 $fixedpath = vmspath($fixedpath) if $force_path;
105 $fixedpath = vmspath($fixedpath) if $force_path;
107 # No hints, so we try to guess
108 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
109 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
112 # Trim off root dirname if it's had other dirs inserted in front of it.
113 $fixedpath =~ s/\.000000([\]>])/$1/;
114 # Special case for VMS absolute directory specs: these will have had device
115 # prepended during trip through Unix syntax in eliminate_macros(), since
116 # Unix syntax has no way to express "absolute from the top of this device's
118 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
124 =head2 Methods always loaded
128 =item canonpath (override)
130 Removes redundant portions of file specifications according to VMS syntax
135 my($self,$path,$reduce_ricochet) = @_;
137 if ($path =~ m|/|) { # Fake Unix
138 my $pathify = $path =~ m|/$|;
139 $path = $self->SUPER::canonpath($path,$reduce_ricochet);
140 if ($pathify) { return vmspath($path); }
141 else { return vmsify($path); }
144 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
145 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo
146 $path =~ s/[\[<\.]([^\[<\.]+)\.-\.\1//g; # bar.foo.-.foo ==> bar.
147 if ($reduce_ricochet) { $path =~ s/[^\[\-<.]+\.\-//g; }
154 Concatenates a list of file specifications, and returns the result as a
155 VMS-syntax directory specification. No check is made for "impossible"
156 cases (e.g. elements other than the first being absolute filespecs).
161 my ($self,@dirs) = @_;
163 @dirs = grep($_,@dirs);
166 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
167 my ($spath,$sdir) = ($path,$dir);
168 $spath =~ s/.dir$//; $sdir =~ s/.dir$//;
169 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+$/;
170 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
172 # Special case for VMS absolute directory specs: these will have had device
173 # prepended during trip through Unix syntax in eliminate_macros(), since
174 # Unix syntax has no way to express "absolute from the top of this device's
176 if ($spath =~ /^[\[<][^.\-]/) { $rslt =~ s/^[^\[<]+//; }
179 if ($dir =~ /^\$\([^\)]+\)$/) { $rslt = $dir; }
180 else { $rslt = vmspath($dir); }
187 Concatenates a list of file specifications, and returns the result as a
188 VMS-syntax file specification.
193 my ($self,@files) = @_;
194 my $file = pop @files;
195 @files = grep($_,@files);
198 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
201 if ($spath =~ /^[^\)\]\/:>]+\)$/ && basename($file) eq $file) {
202 $rslt = "$spath$file";
205 $rslt = $self->eliminate_macros($spath);
206 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
209 else { $rslt = vmsify($file); }
214 =item curdir (override)
216 Returns a string representation of the current directory: '[]'
224 =item devnull (override)
226 Returns a string representation of the null device: '_NLA0:'
234 =item rootdir (override)
236 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
241 return 'SYS$DISK:[000000]';
244 =item tmpdir (override)
246 Returns a string representation of the first writable directory
247 from the following list or '' if none are writable:
256 return $tmpdir if defined $tmpdir;
257 foreach ('/sys$scratch', $ENV{TMPDIR}) {
258 next unless defined && -d && -w _;
262 $tmpdir = '' unless defined $tmpdir;
266 =item updir (override)
268 Returns a string representation of the parent directory: '[-]'
276 =item case_tolerant (override)
278 VMS file specification syntax is case-tolerant.
286 =item path (override)
288 Translate logical name DCL$PATH as a searchlist, rather than trying
289 to C<split> string value of C<$ENV{'PATH'}>.
295 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
299 =item file_name_is_absolute (override)
301 Checks for VMS directory spec as well as Unix separators.
305 sub file_name_is_absolute {
306 my ($self,$file) = @_;
307 # If it's a logical name, expand it.
308 $file = $ENV{$file} while $file =~ /^[\w\$\-]+$/ && $ENV{$file};
309 return scalar($file =~ m!^/! ||
310 $file =~ m![<\[][^.\-\]>]! ||
314 =item splitpath (override)
316 Splits using VMS syntax.
321 my($self,$path) = @_;
322 my($dev,$dir,$file) = ('','','');
324 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/;
325 return ($1 || '',$2 || '',$3);
328 =item splitdir (override)
330 Split dirspec using VMS syntax.
335 my($self,$dirspec) = @_;
336 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
337 my(@dirs) = split('\.', vmspath($dirspec));
338 $dirs[0] =~ s/^[\[<]//; $dirs[-1] =~ s/[\]>]$//;
343 =item catpath (override)
345 Construct a complete filespec using VMS syntax
350 my($self,$dev,$dir,$file) = @_;
351 if ($dev =~ m|^/+([^/]+)|) { $dev =~ "$1:"; }
352 else { $dev .= ':' unless $dev eq '' or $dev =~ /:$/; }
353 $dir = vmspath($dir);
359 ($volume,$directories,$file) = File::Spec->splitpath( $path );
360 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
362 Splits a VMS path in to volume, directory, and filename portions.
363 Ignores $no_file, if present, since VMS paths indicate the 'fileness' of a
366 The results can be passed to L</catpath()> to get back a path equivalent to
367 (usually identical to) the original path.
373 my ($path, $nofile) = @_;
375 my ($volume,$directory,$file) ;
377 if ( $path =~ m{/} ) {
379 m{^ ( (?: /[^/]* )? )
380 ( (?: .*/(?:[^/]+.dir)? )? )
389 m{^ ( (?: (?: (?: [\w\$-]+ (?: "[^"]*")?:: )? [\w\$-]+: )? ) )
399 if $directory =~ /^\[(.*)\]$/ ;
401 return ($volume,$directory,$file);
407 The opposite of L</catdir()>.
409 @dirs = File::Spec->splitdir( $directories );
411 $directories must be only the directory portion of the path.
413 '[' and ']' delimiters are optional. An empty string argument is
414 equivalent to '[]': both return an array with no elements.
420 my $directories = $_[0] ;
422 return File::Spec::Unix::splitdir( $self, @_ )
423 if ( $directories =~ m{/} ) ;
425 $directories =~ s/^\[(.*)\]$/$1/ ;
428 # split() likes to forget about trailing null fields, so here we
429 # check to be sure that there will not be any before handling the
432 if ( $directories !~ m{\.$} ) {
433 return split( m{\.}, $directories );
437 # since there was a trailing separator, add a file name to the end,
438 # then do the split, then replace it with ''.
440 my( @directories )= split( m{\.}, "${directories}dummy" ) ;
441 $directories[ $#directories ]= '' ;
442 return @directories ;
450 return File::Spec::Unix::catpath( $self, @_ )
451 if ( join( '', @_ ) =~ m{/} ) ;
453 my ($volume,$directory,$file) = @_;
456 if $volume =~ /[^:]$/ ;
458 $directory = "[$directory"
459 if $directory =~ /^[^\[]/ ;
462 if $directory =~ /[^\]]$/ ;
464 return "$volume$directory$file" ;
471 return File::Spec::Unix::abs2rel( $self, @_ )
472 if ( join( '', @_ ) =~ m{/} ) ;
474 my($path,$base) = @_;
476 # Note: we use '/' to glue things together here, then let canonpath()
477 # clean them up at the end.
480 if ( ! $self->file_name_is_absolute( $path ) ) {
481 $path = $self->rel2abs( $path ) ;
484 $path = $self->canonpath( $path ) ;
487 # Figure out the effective $base and clean it up.
488 if ( ! $self->file_name_is_absolute( $base ) ) {
489 $base = $self->rel2abs( $base ) ;
491 elsif ( !defined( $base ) || $base eq '' ) {
495 $base = $self->canonpath( $base ) ;
499 my ( undef, $path_directories, $path_file ) =
500 $self->splitpath( $path, 1 ) ;
502 $path_directories = $1
503 if $path_directories =~ /^\[(.*)\]$/ ;
505 my ( undef, $base_directories, undef ) =
506 $self->splitpath( $base, 1 ) ;
508 $base_directories = $1
509 if $base_directories =~ /^\[(.*)\]$/ ;
511 # Now, remove all leading components that are the same
512 my @pathchunks = $self->splitdir( $path_directories );
513 my @basechunks = $self->splitdir( $base_directories );
515 while ( @pathchunks &&
517 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
523 # @basechunks now contains the directories to climb out of,
524 # @pathchunks now has the directories to descend in to.
525 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
526 $path_directories =~ s{\.$}{} ;
527 return $self->catpath( '', $path_directories, $path_file ) ;
533 return File::Spec::Unix::rel2abs( $self, @_ )
534 if ( join( '', @_ ) =~ m{/} ) ;
536 my ($path,$base ) = @_;
537 # Clean up and split up $path
538 if ( ! $self->file_name_is_absolute( $path ) ) {
539 # Figure out the effective $base and clean it up.
540 if ( !defined( $base ) || $base eq '' ) {
543 elsif ( ! $self->file_name_is_absolute( $base ) ) {
544 $base = $self->rel2abs( $base ) ;
547 $base = $self->canonpath( $base ) ;
551 my ( undef, $path_directories, $path_file ) =
552 $self->splitpath( $path ) ;
554 my ( $base_volume, $base_directories, undef ) =
555 $self->splitpath( $base ) ;
559 if ( $base_directories =~ m{[^.]$} &&
560 $path_directories =~ m{^[^.]}
562 $base_directories = "$base_directories$sep$path_directories" ;
564 $path = $self->catpath( $base_volume, $base_directories, $path_file );
567 return $self->canonpath( $path ) ;