This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
fix File::Spec->abs2rel() to return absolute $path more often for VMS
[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 {
bcd0738c 158 $path =~ s/([\[<])000000\./$1/g; # [000000.foo ==> [foo
0e9538e6 159 $path =~ s/([^-]+)\.(\]\[|><)?000000([\]\>])/$1$3/g; # foo.000000] ==> foo]
fd7385b9 160 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
099f76bb 161 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
fd7385b9
CB
162 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
163 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
164 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
165 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
9ba1b7d2 166 $path =~ s/^[\[<\]>]{2}//; # []foo ==> foo
46726cbe
CB
167 return $path;
168 }
169}
170
270d1e39
GS
171=item catdir
172
173Concatenates a list of file specifications, and returns the result as a
46726cbe
CB
174VMS-syntax directory specification. No check is made for "impossible"
175cases (e.g. elements other than the first being absolute filespecs).
270d1e39
GS
176
177=cut
178
179sub catdir {
cbc7acb0
JD
180 my ($self,@dirs) = @_;
181 my $dir = pop @dirs;
270d1e39 182 @dirs = grep($_,@dirs);
cbc7acb0 183 my $rslt;
270d1e39 184 if (@dirs) {
cbc7acb0
JD
185 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
186 my ($spath,$sdir) = ($path,$dir);
ee8c7f54
CB
187 $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//;
188 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s;
cbc7acb0 189 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
46726cbe 190
fd7385b9
CB
191 # Special case for VMS absolute directory specs: these will have had device
192 # prepended during trip through Unix syntax in eliminate_macros(), since
193 # Unix syntax has no way to express "absolute from the top of this device's
194 # directory tree".
195 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
270d1e39 196 }
cbc7acb0 197 else {
fd7385b9 198 if (not defined $dir or not length $dir) { $rslt = ''; }
ee8c7f54 199 elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; }
fd7385b9 200 else { $rslt = vmspath($dir); }
270d1e39 201 }
099f76bb 202 return $self->canonpath($rslt);
270d1e39
GS
203}
204
205=item catfile
206
207Concatenates a list of file specifications, and returns the result as a
46726cbe 208VMS-syntax file specification.
270d1e39
GS
209
210=cut
211
212sub catfile {
cbc7acb0 213 my ($self,@files) = @_;
9ba1b7d2 214 my $file = $self->canonpath(pop @files);
270d1e39 215 @files = grep($_,@files);
cbc7acb0 216 my $rslt;
270d1e39 217 if (@files) {
cbc7acb0
JD
218 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
219 my $spath = $path;
ee8c7f54
CB
220 $spath =~ s/\.dir\Z(?!\n)//;
221 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
cbc7acb0
JD
222 $rslt = "$spath$file";
223 }
224 else {
225 $rslt = $self->eliminate_macros($spath);
226 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
227 }
270d1e39 228 }
fd7385b9 229 else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; }
099f76bb 230 return $self->canonpath($rslt);
270d1e39
GS
231}
232
46726cbe 233
270d1e39
GS
234=item curdir (override)
235
cbc7acb0 236Returns a string representation of the current directory: '[]'
270d1e39
GS
237
238=cut
239
240sub curdir {
241 return '[]';
242}
243
99804bbb
GS
244=item devnull (override)
245
cbc7acb0 246Returns a string representation of the null device: '_NLA0:'
99804bbb
GS
247
248=cut
249
250sub devnull {
cbc7acb0 251 return "_NLA0:";
99804bbb
GS
252}
253
270d1e39
GS
254=item rootdir (override)
255
cbc7acb0 256Returns a string representation of the root directory: 'SYS$DISK:[000000]'
270d1e39
GS
257
258=cut
259
260sub rootdir {
cbc7acb0
JD
261 return 'SYS$DISK:[000000]';
262}
263
264=item tmpdir (override)
265
266Returns a string representation of the first writable directory
267from the following list or '' if none are writable:
268
188ff3c1 269 sys$scratch:
cbc7acb0
JD
270 $ENV{TMPDIR}
271
a384e9e1
RGS
272Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
273is tainted, it is not used.
274
cbc7acb0
JD
275=cut
276
277my $tmpdir;
278sub tmpdir {
279 return $tmpdir if defined $tmpdir;
07824bd1
JH
280 my $self = shift;
281 $tmpdir = $self->_tmpdir( 'sys$scratch:', $ENV{TMPDIR} );
270d1e39
GS
282}
283
284=item updir (override)
285
cbc7acb0 286Returns a string representation of the parent directory: '[-]'
270d1e39
GS
287
288=cut
289
290sub updir {
291 return '[-]';
292}
293
46726cbe
CB
294=item case_tolerant (override)
295
296VMS file specification syntax is case-tolerant.
297
298=cut
299
300sub case_tolerant {
301 return 1;
302}
303
270d1e39
GS
304=item path (override)
305
306Translate logical name DCL$PATH as a searchlist, rather than trying
307to C<split> string value of C<$ENV{'PATH'}>.
308
309=cut
310
311sub path {
cbc7acb0 312 my (@dirs,$dir,$i);
270d1e39 313 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 314 return @dirs;
270d1e39
GS
315}
316
317=item file_name_is_absolute (override)
318
319Checks for VMS directory spec as well as Unix separators.
320
321=cut
322
323sub file_name_is_absolute {
cbc7acb0 324 my ($self,$file) = @_;
270d1e39 325 # If it's a logical name, expand it.
ee8c7f54 326 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
1b1e14d3 327 return scalar($file =~ m!^/!s ||
cbc7acb0
JD
328 $file =~ m![<\[][^.\-\]>]! ||
329 $file =~ /:[^<\[]/);
270d1e39
GS
330}
331
46726cbe
CB
332=item splitpath (override)
333
334Splits using VMS syntax.
335
336=cut
337
338sub splitpath {
339 my($self,$path) = @_;
340 my($dev,$dir,$file) = ('','','');
341
1b1e14d3 342 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
46726cbe
CB
343 return ($1 || '',$2 || '',$3);
344}
345
346=item splitdir (override)
347
348Split dirspec using VMS syntax.
349
350=cut
351
352sub splitdir {
353 my($self,$dirspec) = @_;
354 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
fd7385b9 355 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
46726cbe 356 my(@dirs) = split('\.', vmspath($dirspec));
ee8c7f54 357 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
46726cbe
CB
358 @dirs;
359}
360
361
362=item catpath (override)
363
364Construct a complete filespec using VMS syntax
365
366=cut
367
368sub catpath {
369 my($self,$dev,$dir,$file) = @_;
fd7385b9 370 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
ee8c7f54 371 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
fd7385b9
CB
372 if (length($dev) or length($dir)) {
373 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
374 $dir = vmspath($dir);
0994714a 375 }
fd7385b9 376 "$dev$dir$file";
0994714a
GS
377}
378
fd7385b9 379=item abs2rel (override)
0994714a 380
fd7385b9 381Use VMS syntax when converting filespecs.
0994714a
GS
382
383=cut
384
0994714a
GS
385sub abs2rel {
386 my $self = shift;
387
fd7385b9 388 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
0994714a
GS
389 if ( join( '', @_ ) =~ m{/} ) ;
390
391 my($path,$base) = @_;
392
393 # Note: we use '/' to glue things together here, then let canonpath()
394 # clean them up at the end.
395
396 # Clean up $path
397 if ( ! $self->file_name_is_absolute( $path ) ) {
398 $path = $self->rel2abs( $path ) ;
399 }
400 else {
401 $path = $self->canonpath( $path ) ;
402 }
403
404 # Figure out the effective $base and clean it up.
1d7cb664 405 if ( !defined( $base ) || $base eq '' ) {
0fab864c 406 $base = $self->canonpath( $self->_cwd ) ;
0994714a 407 }
1d7cb664
GS
408 elsif ( ! $self->file_name_is_absolute( $base ) ) {
409 $base = $self->rel2abs( $base ) ;
410 }
0994714a
GS
411 else {
412 $base = $self->canonpath( $base ) ;
413 }
414
415 # Split up paths
ee8c7f54
CB
416 my ( $path_directories, $path_file ) =
417 ($self->splitpath( $path, 1 ))[1,2] ;
0994714a
GS
418
419 $path_directories = $1
ee8c7f54 420 if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a 421
ee8c7f54 422 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
0994714a
GS
423
424 $base_directories = $1
ee8c7f54 425 if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a
GS
426
427 # Now, remove all leading components that are the same
428 my @pathchunks = $self->splitdir( $path_directories );
737c380e 429 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
0994714a 430 my @basechunks = $self->splitdir( $base_directories );
737c380e 431 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
0994714a
GS
432
433 while ( @pathchunks &&
434 @basechunks &&
435 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
436 ) {
437 shift @pathchunks ;
438 shift @basechunks ;
439 }
440
441 # @basechunks now contains the directories to climb out of,
442 # @pathchunks now has the directories to descend in to.
443 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
ee8c7f54 444 $path_directories =~ s{\.\Z(?!\n)}{} ;
fd7385b9 445 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
0994714a
GS
446}
447
448
fd7385b9
CB
449=item rel2abs (override)
450
451Use VMS syntax when converting filespecs.
452
453=cut
454
786b702f 455sub rel2abs {
0994714a 456 my $self = shift ;
fd7385b9 457 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
0994714a
GS
458 if ( join( '', @_ ) =~ m{/} ) ;
459
460 my ($path,$base ) = @_;
461 # Clean up and split up $path
462 if ( ! $self->file_name_is_absolute( $path ) ) {
463 # Figure out the effective $base and clean it up.
464 if ( !defined( $base ) || $base eq '' ) {
0fab864c 465 $base = $self->_cwd;
0994714a
GS
466 }
467 elsif ( ! $self->file_name_is_absolute( $base ) ) {
468 $base = $self->rel2abs( $base ) ;
469 }
470 else {
471 $base = $self->canonpath( $base ) ;
472 }
473
474 # Split up paths
ee8c7f54
CB
475 my ( $path_directories, $path_file ) =
476 ($self->splitpath( $path ))[1,2] ;
0994714a 477
ee8c7f54 478 my ( $base_volume, $base_directories ) =
0994714a
GS
479 $self->splitpath( $base ) ;
480
fd7385b9
CB
481 $path_directories = '' if $path_directories eq '[]' ||
482 $path_directories eq '<>';
0994714a
GS
483 my $sep = '' ;
484 $sep = '.'
ee8c7f54 485 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
fd7385b9 486 $path_directories =~ m{^[^.\[<]}s
0994714a 487 ) ;
fd7385b9
CB
488 $base_directories = "$base_directories$sep$path_directories";
489 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
0994714a
GS
490
491 $path = $self->catpath( $base_volume, $base_directories, $path_file );
492 }
493
494 return $self->canonpath( $path ) ;
495}
496
497
cbc7acb0 498=back
270d1e39 499
cbc7acb0
JD
500=head1 SEE ALSO
501
72f15715
T
502See L<File::Spec> and L<File::Spec::Unix>. This package overrides the
503implementation of these methods, not the semantics.
cbc7acb0
JD
504
505=cut
506
5071;