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