This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix failure in Archive::Tar tests when perl is built
[perl5.git] / lib / File / Spec / VMS.pm
CommitLineData
270d1e39
GS
1package File::Spec::VMS;
2
cbc7acb0 3use strict;
ee8c7f54 4use vars qw(@ISA $VERSION);
cbc7acb0 5require File::Spec::Unix;
ee8c7f54 6
07824bd1 7$VERSION = '1.4';
ee8c7f54 8
270d1e39
GS
9@ISA = qw(File::Spec::Unix);
10
cbc7acb0
JD
11use File::Basename;
12use VMS::Filespec;
270d1e39
GS
13
14=head1 NAME
15
16File::Spec::VMS - methods for VMS file specs
17
18=head1 SYNOPSIS
19
cbc7acb0 20 require File::Spec::VMS; # Done internally by File::Spec if needed
270d1e39
GS
21
22=head1 DESCRIPTION
23
24See File::Spec::Unix for a documentation of the methods provided
25there. This package overrides the implementation of these methods, not
26the semantics.
27
bbc7dcd2 28=over 4
a45bd81d 29
377875b9
CB
30=item eliminate_macros
31
32Expands MM[KS]/Make macros in a text string, using the contents of
33identically named elements of C<%$self>, and returns the result
34as a file specification in Unix syntax.
35
1f47e8e2
CB
36=cut
37
38sub eliminate_macros {
39 my($self,$path) = @_;
40 return '' unless $path;
41 $self = {} unless ref $self;
bdb84599
CB
42
43 if ($path =~ /\s/) {
44 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
45 }
46
1f47e8e2
CB
47 my($npath) = unixify($path);
48 my($complex) = 0;
49 my($head,$macro,$tail);
50
51 # perform m##g in scalar context so it acts as an iterator
14a089c5 52 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
1f47e8e2
CB
53 if ($self->{$2}) {
54 ($head,$macro,$tail) = ($1,$2,$3);
55 if (ref $self->{$macro}) {
56 if (ref $self->{$macro} eq 'ARRAY') {
57 $macro = join ' ', @{$self->{$macro}};
58 }
59 else {
60 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
61 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
62 $macro = "\cB$macro\cB";
63 $complex = 1;
64 }
65 }
ee8c7f54 66 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
1f47e8e2
CB
67 $npath = "$head$macro$tail";
68 }
69 }
14a089c5 70 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
1f47e8e2
CB
71 $npath;
72}
73
377875b9
CB
74=item fixpath
75
76Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
77in any directory specification, in order to avoid juxtaposing two
78VMS-syntax directories when MM[SK] is run. Also expands expressions which
79are all macro, so that we can tell how long the expansion is, and avoid
80overrunning DCL's command buffer when MM[KS] is running.
81
82If optional second argument has a TRUE value, then the return string is
83a VMS-syntax directory specification, if it is FALSE, the return string
84is a VMS-syntax file specification, and if it is not specified, fixpath()
85checks to see whether it matches the name of a directory in the current
86default directory, and returns a directory or file specification accordingly.
87
88=cut
89
1f47e8e2
CB
90sub fixpath {
91 my($self,$path,$force_path) = @_;
92 return '' unless $path;
93 $self = bless {} unless ref $self;
94 my($fixedpath,$prefix,$name);
95
bdb84599
CB
96 if ($path =~ /\s/) {
97 return join ' ',
98 map { $self->fixpath($_,$force_path) }
99 split /\s+/, $path;
100 }
101
ee8c7f54
CB
102 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
103 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
1f47e8e2
CB
104 $fixedpath = vmspath($self->eliminate_macros($path));
105 }
106 else {
107 $fixedpath = vmsify($self->eliminate_macros($path));
108 }
109 }
1b1e14d3 110 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
1f47e8e2
CB
111 my($vmspre) = $self->eliminate_macros("\$($prefix)");
112 # is it a dir or just a name?
ee8c7f54 113 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
1f47e8e2
CB
114 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
115 $fixedpath = vmspath($fixedpath) if $force_path;
116 }
117 else {
118 $fixedpath = $path;
119 $fixedpath = vmspath($fixedpath) if $force_path;
120 }
121 # No hints, so we try to guess
122 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
123 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
124 }
46726cbe 125
1f47e8e2
CB
126 # Trim off root dirname if it's had other dirs inserted in front of it.
127 $fixedpath =~ s/\.000000([\]>])/$1/;
46726cbe
CB
128 # Special case for VMS absolute directory specs: these will have had device
129 # prepended during trip through Unix syntax in eliminate_macros(), since
130 # Unix syntax has no way to express "absolute from the top of this device's
131 # directory tree".
132 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
1f47e8e2
CB
133 $fixedpath;
134}
135
a45bd81d 136=back
1f47e8e2 137
270d1e39
GS
138=head2 Methods always loaded
139
bbc7dcd2 140=over 4
270d1e39 141
46726cbe
CB
142=item canonpath (override)
143
fd7385b9 144Removes redundant portions of file specifications according to VMS syntax.
46726cbe
CB
145
146=cut
147
148sub canonpath {
fd7385b9 149 my($self,$path) = @_;
46726cbe
CB
150
151 if ($path =~ m|/|) { # Fake Unix
ee8c7f54 152 my $pathify = $path =~ m|/\Z(?!\n)|;
fd7385b9 153 $path = $self->SUPER::canonpath($path);
46726cbe
CB
154 if ($pathify) { return vmspath($path); }
155 else { return vmsify($path); }
156 }
157 else {
bdc74e5c
CB
158 $path =~ tr/<>/[]/; # < and > ==> [ and ]
159 $path =~ s/\]\[\./\.\]\[/g; # ][. ==> .][
160 $path =~ s/\[000000\.\]\[/\[/g; # [000000.][ ==> [
161 $path =~ s/\[000000\./\[/g; # [000000. ==> [
162 $path =~ s/\.\]\[000000\]/\]/g; # .][000000] ==> ]
163 $path =~ s/\.\]\[/\./g; # foo.][bar ==> foo.bar
164 1 while ($path =~ s/([\[\.])(-+)\.(-+)([\.\]])/$1$2$3$4/);
165 # That loop does the following
166 # with any amount of dashes:
167 # .-.-. ==> .--.
168 # [-.-. ==> [--.
169 # .-.-] ==> .--]
170 # [-.-] ==> [--]
171 1 while ($path =~ s/([\[\.])[^\]\.]+\.-(-+)([\]\.])/$1$2$3/);
172 # That loop does the following
173 # with any amount (minimum 2)
174 # of dashes:
175 # .foo.--. ==> .-.
176 # .foo.--] ==> .-]
177 # [foo.--. ==> [-.
178 # [foo.--] ==> [-]
179 #
180 # And then, the remaining cases
181 $path =~ s/\[\.-/[-/; # [.- ==> [-
182 $path =~ s/\.[^\]\.]+\.-\./\./g; # .foo.-. ==> .
183 $path =~ s/\[[^\]\.]+\.-\./\[/g; # [foo.-. ==> [
184 $path =~ s/\.[^\]\.]+\.-\]/\]/g; # .foo.-] ==> ]
99f36a73 185 $path =~ s/\[[^\]\.]+\.-\]/\[000000\]/g;# [foo.-] ==> [000000]
bdc74e5c
CB
186 $path =~ s/\[\]//; # [] ==>
187 return $path;
46726cbe
CB
188 }
189}
190
270d1e39
GS
191=item catdir
192
193Concatenates a list of file specifications, and returns the result as a
46726cbe
CB
194VMS-syntax directory specification. No check is made for "impossible"
195cases (e.g. elements other than the first being absolute filespecs).
270d1e39
GS
196
197=cut
198
199sub catdir {
cbc7acb0
JD
200 my ($self,@dirs) = @_;
201 my $dir = pop @dirs;
270d1e39 202 @dirs = grep($_,@dirs);
cbc7acb0 203 my $rslt;
270d1e39 204 if (@dirs) {
cbc7acb0
JD
205 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
206 my ($spath,$sdir) = ($path,$dir);
ee8c7f54
CB
207 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
208 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
cbc7acb0 209 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
46726cbe 210
fd7385b9
CB
211 # Special case for VMS absolute directory specs: these will have had device
212 # prepended during trip through Unix syntax in eliminate_macros(), since
213 # Unix syntax has no way to express "absolute from the top of this device's
214 # directory tree".
215 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
270d1e39 216 }
cbc7acb0 217 else {
fd7385b9 218 if (not defined $dir or not length $dir) { $rslt = ''; }
ee8c7f54 219 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
fd7385b9 220 else { $rslt = vmspath($dir); }
270d1e39 221 }
099f76bb 222 return $self->canonpath($rslt);
270d1e39
GS
223}
224
225=item catfile
226
227Concatenates a list of file specifications, and returns the result as a
46726cbe 228VMS-syntax file specification.
270d1e39
GS
229
230=cut
231
232sub catfile {
cbc7acb0 233 my ($self,@files) = @_;
9ba1b7d2 234 my $file = $self->canonpath(pop @files);
270d1e39 235 @files = grep($_,@files);
cbc7acb0 236 my $rslt;
270d1e39 237 if (@files) {
cbc7acb0
JD
238 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
239 my $spath = $path;
ee8c7f54
CB
240 $spath =~ s/\.dir\Z(?!\n)//;
241 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
cbc7acb0
JD
242 $rslt = "$spath$file";
243 }
244 else {
245 $rslt = $self->eliminate_macros($spath);
246 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
247 }
270d1e39 248 }
fd7385b9 249 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
099f76bb 250 return $self->canonpath($rslt);
270d1e39
GS
251}
252
46726cbe 253
270d1e39
GS
254=item curdir (override)
255
cbc7acb0 256Returns a string representation of the current directory: '[]'
270d1e39
GS
257
258=cut
259
260sub curdir {
261 return '[]';
262}
263
99804bbb
GS
264=item devnull (override)
265
cbc7acb0 266Returns a string representation of the null device: '_NLA0:'
99804bbb
GS
267
268=cut
269
270sub devnull {
cbc7acb0 271 return "_NLA0:";
99804bbb
GS
272}
273
270d1e39
GS
274=item rootdir (override)
275
cbc7acb0 276Returns a string representation of the root directory: 'SYS$DISK:[000000]'
270d1e39
GS
277
278=cut
279
280sub rootdir {
cbc7acb0
JD
281 return 'SYS$DISK:[000000]';
282}
283
284=item tmpdir (override)
285
286Returns a string representation of the first writable directory
287from the following list or '' if none are writable:
288
188ff3c1 289 sys$scratch:
cbc7acb0
JD
290 $ENV{TMPDIR}
291
a384e9e1
RGS
292Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
293is tainted, it is not used.
294
cbc7acb0
JD
295=cut
296
297my $tmpdir;
298sub tmpdir {
299 return $tmpdir if defined $tmpdir;
60598624 300 $tmpdir = $_[0]->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
270d1e39
GS
301}
302
303=item updir (override)
304
cbc7acb0 305Returns a string representation of the parent directory: '[-]'
270d1e39
GS
306
307=cut
308
309sub updir {
310 return '[-]';
311}
312
46726cbe
CB
313=item case_tolerant (override)
314
315VMS file specification syntax is case-tolerant.
316
317=cut
318
319sub case_tolerant {
320 return 1;
321}
322
270d1e39
GS
323=item path (override)
324
325Translate logical name DCL$PATH as a searchlist, rather than trying
326to C<split> string value of C<$ENV{'PATH'}>.
327
328=cut
329
330sub path {
cbc7acb0 331 my (@dirs,$dir,$i);
270d1e39 332 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 333 return @dirs;
270d1e39
GS
334}
335
336=item file_name_is_absolute (override)
337
338Checks for VMS directory spec as well as Unix separators.
339
340=cut
341
342sub file_name_is_absolute {
cbc7acb0 343 my ($self,$file) = @_;
270d1e39 344 # If it's a logical name, expand it.
ee8c7f54 345 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
1b1e14d3 346 return scalar($file =~ m!^/!s ||
cbc7acb0
JD
347 $file =~ m![<\[][^.\-\]>]! ||
348 $file =~ /:[^<\[]/);
270d1e39
GS
349}
350
46726cbe
CB
351=item splitpath (override)
352
353Splits using VMS syntax.
354
355=cut
356
357sub splitpath {
358 my($self,$path) = @_;
359 my($dev,$dir,$file) = ('','','');
360
1b1e14d3 361 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
46726cbe
CB
362 return ($1 || '',$2 || '',$3);
363}
364
365=item splitdir (override)
366
367Split dirspec using VMS syntax.
368
369=cut
370
371sub splitdir {
372 my($self,$dirspec) = @_;
bdc74e5c
CB
373 $dirspec =~ tr/<>/[]/; # < and > ==> [ and ]
374 $dirspec =~ s/\]\[\./\.\]\[/g; # ][. ==> .][
375 $dirspec =~ s/\[000000\.\]\[/\[/g; # [000000.][ ==> [
376 $dirspec =~ s/\[000000\./\[/g; # [000000. ==> [
377 $dirspec =~ s/\.\]\[000000\]/\]/g; # .][000000] ==> ]
378 $dirspec =~ s/\.\]\[/\./g; # foo.][bar ==> foo.bar
379 while ($dirspec =~ s/(^|[\[\<\.])\-(\-+)($|[\]\>\.])/$1-.$2$3/g) {}
380 # That loop does the following
381 # with any amount of dashes:
382 # .--. ==> .-.-.
383 # [--. ==> [-.-.
384 # .--] ==> .-.-]
385 # [--] ==> [-.-]
fd7385b9 386 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
46726cbe 387 my(@dirs) = split('\.', vmspath($dirspec));
ee8c7f54 388 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
46726cbe
CB
389 @dirs;
390}
391
392
393=item catpath (override)
394
395Construct a complete filespec using VMS syntax
396
397=cut
398
399sub catpath {
400 my($self,$dev,$dir,$file) = @_;
638113eb
JH
401
402 # We look for a volume in $dev, then in $dir, but not both
403 my ($dir_volume, $dir_dir, $dir_file) = $self->splitpath($dir);
404 $dev = $dir_volume unless length $dev;
405 $dir = length $dir_file ? $self->catfile($dir_dir, $dir_file) : $dir_dir;
406
fd7385b9 407 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
ee8c7f54 408 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
fd7385b9
CB
409 if (length($dev) or length($dir)) {
410 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
411 $dir = vmspath($dir);
0994714a 412 }
fd7385b9 413 "$dev$dir$file";
0994714a
GS
414}
415
fd7385b9 416=item abs2rel (override)
0994714a 417
fd7385b9 418Use VMS syntax when converting filespecs.
0994714a
GS
419
420=cut
421
0994714a
GS
422sub abs2rel {
423 my $self = shift;
fd7385b9 424 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
638113eb 425 if grep m{/}, @_;
0994714a
GS
426
427 my($path,$base) = @_;
638113eb 428 $base = $self->_cwd() unless defined $base and length $base;
0994714a 429
638113eb 430 for ($path, $base) { $_ = $self->canonpath($_) }
0994714a 431
d84c672d
JH
432 # Are we even starting $path on the same (node::)device as $base? Note that
433 # logical paths or nodename differences may be on the "same device"
434 # but the comparison that ignores device differences so as to concatenate
435 # [---] up directory specs is not even a good idea in cases where there is
436 # a logical path difference between $path and $base nodename and/or device.
437 # Hence we fall back to returning the absolute $path spec
438 # if there is a case blind device (or node) difference of any sort
439 # and we do not even try to call $parse() or consult %ENV for $trnlnm()
440 # (this module needs to run on non VMS platforms after all).
638113eb
JH
441
442 my ($path_volume, $path_directories, $path_file) = $self->splitpath($path);
443 my ($base_volume, $base_directories, $base_file) = $self->splitpath($base);
444 return $path unless lc($path_volume) eq lc($base_volume);
d84c672d 445
638113eb 446 for ($path, $base) { $_ = $self->rel2abs($_) }
0994714a
GS
447
448 # Now, remove all leading components that are the same
449 my @pathchunks = $self->splitdir( $path_directories );
737c380e 450 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
0994714a 451 my @basechunks = $self->splitdir( $base_directories );
737c380e 452 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
0994714a
GS
453
454 while ( @pathchunks &&
455 @basechunks &&
456 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
457 ) {
458 shift @pathchunks ;
459 shift @basechunks ;
460 }
461
462 # @basechunks now contains the directories to climb out of,
463 # @pathchunks now has the directories to descend in to.
638113eb 464 $path_directories = join '.', ('-' x @basechunks, @pathchunks) ;
fd7385b9 465 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
0994714a
GS
466}
467
468
fd7385b9
CB
469=item rel2abs (override)
470
471Use VMS syntax when converting filespecs.
472
473=cut
474
786b702f 475sub rel2abs {
0994714a 476 my $self = shift ;
0994714a 477 my ($path,$base ) = @_;
bdc74e5c 478 return undef unless defined $path;
99f36a73
RGS
479 if ($path =~ m/\//) {
480 $path = ( -d $path || $path =~ m/\/\z/ # educated guessing about
481 ? vmspath($path) # whether it's a directory
482 : vmsify($path) );
483 }
bdc74e5c 484 $base = vmspath($base) if defined $base && $base =~ m/\//;
0994714a
GS
485 # Clean up and split up $path
486 if ( ! $self->file_name_is_absolute( $path ) ) {
487 # Figure out the effective $base and clean it up.
488 if ( !defined( $base ) || $base eq '' ) {
0fab864c 489 $base = $self->_cwd;
0994714a
GS
490 }
491 elsif ( ! $self->file_name_is_absolute( $base ) ) {
492 $base = $self->rel2abs( $base ) ;
493 }
494 else {
495 $base = $self->canonpath( $base ) ;
496 }
497
498 # Split up paths
ee8c7f54
CB
499 my ( $path_directories, $path_file ) =
500 ($self->splitpath( $path ))[1,2] ;
0994714a 501
ee8c7f54 502 my ( $base_volume, $base_directories ) =
0994714a
GS
503 $self->splitpath( $base ) ;
504
fd7385b9
CB
505 $path_directories = '' if $path_directories eq '[]' ||
506 $path_directories eq '<>';
0994714a
GS
507 my $sep = '' ;
508 $sep = '.'
ee8c7f54 509 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
fd7385b9 510 $path_directories =~ m{^[^.\[<]}s
0994714a 511 ) ;
fd7385b9
CB
512 $base_directories = "$base_directories$sep$path_directories";
513 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
0994714a
GS
514
515 $path = $self->catpath( $base_volume, $base_directories, $path_file );
516 }
517
518 return $self->canonpath( $path ) ;
519}
520
521
cbc7acb0 522=back
270d1e39 523
99f36a73
RGS
524=head1 COPYRIGHT
525
526Copyright (c) 2004 by the Perl 5 Porters. All rights reserved.
527
528This program is free software; you can redistribute it and/or modify
529it under the same terms as Perl itself.
530
cbc7acb0
JD
531=head1 SEE ALSO
532
72f15715
T
533See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
534implementation of these methods, not the semantics.
cbc7acb0 535
638113eb
JH
536An explanation of VMS file specs can be found at
537L<"http://h71000.www7.hp.com/doc/731FINAL/4506/4506pro_014.html#apps_locating_naming_files">.
538
cbc7acb0
JD
539=cut
540
5411;