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;
43 my($npath) = unixify($path);
45 my($head,$macro,$tail);
47 # perform m##g in scalar context so it acts as an iterator
48 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
50 ($head,$macro,$tail) = ($1,$2,$3);
51 if (ref $self->{$macro}) {
52 if (ref $self->{$macro} eq 'ARRAY') {
53 $macro = join ' ', @{$self->{$macro}};
56 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
57 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
58 $macro = "\cB$macro\cB";
62 else { ($macro = unixify($self->{$macro})) =~ s#/\z##; }
63 $npath = "$head$macro$tail";
66 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
72 Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
73 in any directory specification, in order to avoid juxtaposing two
74 VMS-syntax directories when MM[SK] is run. Also expands expressions which
75 are all macro, so that we can tell how long the expansion is, and avoid
76 overrunning DCL's command buffer when MM[KS] is running.
78 If optional second argument has a TRUE value, then the return string is
79 a VMS-syntax directory specification, if it is FALSE, the return string
80 is a VMS-syntax file specification, and if it is not specified, fixpath()
81 checks to see whether it matches the name of a directory in the current
82 default directory, and returns a directory or file specification accordingly.
87 my($self,$path,$force_path) = @_;
88 return '' unless $path;
89 $self = bless {} unless ref $self;
90 my($fixedpath,$prefix,$name);
92 if ($path =~ m#^\$\([^\)]+\)\z#s || $path =~ m#[/:>\]]#) {
93 if ($force_path or $path =~ /(?:DIR\)|\])\z/) {
94 $fixedpath = vmspath($self->eliminate_macros($path));
97 $fixedpath = vmsify($self->eliminate_macros($path));
100 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
101 my($vmspre) = $self->eliminate_macros("\$($prefix)");
102 # is it a dir or just a name?
103 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\z/) ? vmspath($vmspre) : '';
104 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
105 $fixedpath = vmspath($fixedpath) if $force_path;
109 $fixedpath = vmspath($fixedpath) if $force_path;
111 # No hints, so we try to guess
112 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
113 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
116 # Trim off root dirname if it's had other dirs inserted in front of it.
117 $fixedpath =~ s/\.000000([\]>])/$1/;
118 # Special case for VMS absolute directory specs: these will have had device
119 # prepended during trip through Unix syntax in eliminate_macros(), since
120 # Unix syntax has no way to express "absolute from the top of this device's
122 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
128 =head2 Methods always loaded
132 =item canonpath (override)
134 Removes redundant portions of file specifications according to VMS syntax.
139 my($self,$path) = @_;
141 if ($path =~ m|/|) { # Fake Unix
142 my $pathify = $path =~ m|/\z|;
143 $path = $self->SUPER::canonpath($path);
144 if ($pathify) { return vmspath($path); }
145 else { return vmsify($path); }
148 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
149 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo
150 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
151 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
152 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
153 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
154 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
161 Concatenates a list of file specifications, and returns the result as a
162 VMS-syntax directory specification. No check is made for "impossible"
163 cases (e.g. elements other than the first being absolute filespecs).
168 my ($self,@dirs) = @_;
170 @dirs = grep($_,@dirs);
173 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
174 my ($spath,$sdir) = ($path,$dir);
175 $spath =~ s/\.dir\z//; $sdir =~ s/\.dir\z//;
176 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\z/s;
177 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
179 # Special case for VMS absolute directory specs: these will have had device
180 # prepended during trip through Unix syntax in eliminate_macros(), since
181 # Unix syntax has no way to express "absolute from the top of this device's
183 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
186 if (not defined $dir or not length $dir) { $rslt = ''; }
187 elsif ($dir =~ /^\$\([^\)]+\)\z/s) { $rslt = $dir; }
188 else { $rslt = vmspath($dir); }
190 return $self->canonpath($rslt);
195 Concatenates a list of file specifications, and returns the result as a
196 VMS-syntax file specification.
201 my ($self,@files) = @_;
202 my $file = pop @files;
203 @files = grep($_,@files);
206 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
208 $spath =~ s/\.dir\z//;
209 if ($spath =~ /^[^\)\]\/:>]+\)\z/s && basename($file) eq $file) {
210 $rslt = "$spath$file";
213 $rslt = $self->eliminate_macros($spath);
214 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
217 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
218 return $self->canonpath($rslt);
222 =item curdir (override)
224 Returns a string representation of the current directory: '[]'
232 =item devnull (override)
234 Returns a string representation of the null device: '_NLA0:'
242 =item rootdir (override)
244 Returns a string representation of the root directory: 'SYS$DISK:[000000]'
249 return 'SYS$DISK:[000000]';
252 =item tmpdir (override)
254 Returns a string representation of the first writable directory
255 from the following list or '' if none are writable:
264 return $tmpdir if defined $tmpdir;
265 foreach ('sys$scratch', $ENV{TMPDIR}) {
266 next unless defined && -d && -w _;
270 $tmpdir = '' unless defined $tmpdir;
274 =item updir (override)
276 Returns a string representation of the parent directory: '[-]'
284 =item case_tolerant (override)
286 VMS file specification syntax is case-tolerant.
294 =item path (override)
296 Translate logical name DCL$PATH as a searchlist, rather than trying
297 to C<split> string value of C<$ENV{'PATH'}>.
303 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
307 =item file_name_is_absolute (override)
309 Checks for VMS directory spec as well as Unix separators.
313 sub file_name_is_absolute {
314 my ($self,$file) = @_;
315 # If it's a logical name, expand it.
316 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\z/s && $ENV{$file};
317 return scalar($file =~ m!^/!s ||
318 $file =~ m![<\[][^.\-\]>]! ||
322 =item splitpath (override)
324 Splits using VMS syntax.
329 my($self,$path) = @_;
330 my($dev,$dir,$file) = ('','','');
332 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
333 return ($1 || '',$2 || '',$3);
336 =item splitdir (override)
338 Split dirspec using VMS syntax.
343 my($self,$dirspec) = @_;
344 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
345 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
346 my(@dirs) = split('\.', vmspath($dirspec));
347 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\z//s;
352 =item catpath (override)
354 Construct a complete filespec using VMS syntax
359 my($self,$dev,$dir,$file) = @_;
360 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
361 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\z/; }
362 if (length($dev) or length($dir)) {
363 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
364 $dir = vmspath($dir);
369 =item abs2rel (override)
371 Use VMS syntax when converting filespecs.
378 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
379 if ( join( '', @_ ) =~ m{/} ) ;
381 my($path,$base) = @_;
383 # Note: we use '/' to glue things together here, then let canonpath()
384 # clean them up at the end.
387 if ( ! $self->file_name_is_absolute( $path ) ) {
388 $path = $self->rel2abs( $path ) ;
391 $path = $self->canonpath( $path ) ;
394 # Figure out the effective $base and clean it up.
395 if ( !defined( $base ) || $base eq '' ) {
398 elsif ( ! $self->file_name_is_absolute( $base ) ) {
399 $base = $self->rel2abs( $base ) ;
402 $base = $self->canonpath( $base ) ;
406 my ( undef, $path_directories, $path_file ) =
407 $self->splitpath( $path, 1 ) ;
409 $path_directories = $1
410 if $path_directories =~ /^\[(.*)\]\z/s ;
412 my ( undef, $base_directories, undef ) =
413 $self->splitpath( $base, 1 ) ;
415 $base_directories = $1
416 if $base_directories =~ /^\[(.*)\]\z/s ;
418 # Now, remove all leading components that are the same
419 my @pathchunks = $self->splitdir( $path_directories );
420 my @basechunks = $self->splitdir( $base_directories );
422 while ( @pathchunks &&
424 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
430 # @basechunks now contains the directories to climb out of,
431 # @pathchunks now has the directories to descend in to.
432 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
433 $path_directories =~ s{\.\z}{} ;
434 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
438 =item rel2abs (override)
440 Use VMS syntax when converting filespecs.
446 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
447 if ( join( '', @_ ) =~ m{/} ) ;
449 my ($path,$base ) = @_;
450 # Clean up and split up $path
451 if ( ! $self->file_name_is_absolute( $path ) ) {
452 # Figure out the effective $base and clean it up.
453 if ( !defined( $base ) || $base eq '' ) {
456 elsif ( ! $self->file_name_is_absolute( $base ) ) {
457 $base = $self->rel2abs( $base ) ;
460 $base = $self->canonpath( $base ) ;
464 my ( undef, $path_directories, $path_file ) =
465 $self->splitpath( $path ) ;
467 my ( $base_volume, $base_directories, undef ) =
468 $self->splitpath( $base ) ;
470 $path_directories = '' if $path_directories eq '[]' ||
471 $path_directories eq '<>';
474 if ( $base_directories =~ m{[^.\]>]\z} &&
475 $path_directories =~ m{^[^.\[<]}s
477 $base_directories = "$base_directories$sep$path_directories";
478 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
480 $path = $self->catpath( $base_volume, $base_directories, $path_file );
483 return $self->canonpath( $path ) ;