This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump up version numbers.
[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
88d01e8d 7$VERSION = '1.2';
ee8c7f54 8
270d1e39
GS
9@ISA = qw(File::Spec::Unix);
10
178326fd 11use Cwd;
cbc7acb0
JD
12use File::Basename;
13use VMS::Filespec;
270d1e39
GS
14
15=head1 NAME
16
17File::Spec::VMS - methods for VMS file specs
18
19=head1 SYNOPSIS
20
cbc7acb0 21 require File::Spec::VMS; # Done internally by File::Spec if needed
270d1e39
GS
22
23=head1 DESCRIPTION
24
25See File::Spec::Unix for a documentation of the methods provided
26there. This package overrides the implementation of these methods, not
27the semantics.
28
bbc7dcd2 29=over 4
a45bd81d 30
377875b9
CB
31=item eliminate_macros
32
33Expands MM[KS]/Make macros in a text string, using the contents of
34identically named elements of C<%$self>, and returns the result
35as a file specification in Unix syntax.
36
1f47e8e2
CB
37=cut
38
39sub eliminate_macros {
40 my($self,$path) = @_;
41 return '' unless $path;
42 $self = {} unless ref $self;
bdb84599
CB
43
44 if ($path =~ /\s/) {
45 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
46 }
47
1f47e8e2
CB
48 my($npath) = unixify($path);
49 my($complex) = 0;
50 my($head,$macro,$tail);
51
52 # perform m##g in scalar context so it acts as an iterator
14a089c5 53 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
1f47e8e2
CB
54 if ($self->{$2}) {
55 ($head,$macro,$tail) = ($1,$2,$3);
56 if (ref $self->{$macro}) {
57 if (ref $self->{$macro} eq 'ARRAY') {
58 $macro = join ' ', @{$self->{$macro}};
59 }
60 else {
61 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
62 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
63 $macro = "\cB$macro\cB";
64 $complex = 1;
65 }
66 }
ee8c7f54 67 else { ($macro = unixify($self->{$macro})) =~ s#/\Z(?!\n)##; }
1f47e8e2
CB
68 $npath = "$head$macro$tail";
69 }
70 }
14a089c5 71 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
1f47e8e2
CB
72 $npath;
73}
74
377875b9
CB
75=item fixpath
76
77Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
78in any directory specification, in order to avoid juxtaposing two
79VMS-syntax directories when MM[SK] is run. Also expands expressions which
80are all macro, so that we can tell how long the expansion is, and avoid
81overrunning DCL's command buffer when MM[KS] is running.
82
83If optional second argument has a TRUE value, then the return string is
84a VMS-syntax directory specification, if it is FALSE, the return string
85is a VMS-syntax file specification, and if it is not specified, fixpath()
86checks to see whether it matches the name of a directory in the current
87default directory, and returns a directory or file specification accordingly.
88
89=cut
90
1f47e8e2
CB
91sub fixpath {
92 my($self,$path,$force_path) = @_;
93 return '' unless $path;
94 $self = bless {} unless ref $self;
95 my($fixedpath,$prefix,$name);
96
bdb84599
CB
97 if ($path =~ /\s/) {
98 return join ' ',
99 map { $self->fixpath($_,$force_path) }
100 split /\s+/, $path;
101 }
102
ee8c7f54
CB
103 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
104 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
1f47e8e2
CB
105 $fixedpath = vmspath($self->eliminate_macros($path));
106 }
107 else {
108 $fixedpath = vmsify($self->eliminate_macros($path));
109 }
110 }
1b1e14d3 111 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
1f47e8e2
CB
112 my($vmspre) = $self->eliminate_macros("\$($prefix)");
113 # is it a dir or just a name?
ee8c7f54 114 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\Z(?!\n)/) ? vmspath($vmspre) : '';
1f47e8e2
CB
115 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
116 $fixedpath = vmspath($fixedpath) if $force_path;
117 }
118 else {
119 $fixedpath = $path;
120 $fixedpath = vmspath($fixedpath) if $force_path;
121 }
122 # No hints, so we try to guess
123 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
124 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
125 }
46726cbe 126
1f47e8e2
CB
127 # Trim off root dirname if it's had other dirs inserted in front of it.
128 $fixedpath =~ s/\.000000([\]>])/$1/;
46726cbe
CB
129 # Special case for VMS absolute directory specs: these will have had device
130 # prepended during trip through Unix syntax in eliminate_macros(), since
131 # Unix syntax has no way to express "absolute from the top of this device's
132 # directory tree".
133 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
1f47e8e2
CB
134 $fixedpath;
135}
136
a45bd81d 137=back
1f47e8e2 138
270d1e39
GS
139=head2 Methods always loaded
140
bbc7dcd2 141=over 4
270d1e39 142
46726cbe
CB
143=item canonpath (override)
144
fd7385b9 145Removes redundant portions of file specifications according to VMS syntax.
46726cbe
CB
146
147=cut
148
149sub canonpath {
fd7385b9 150 my($self,$path) = @_;
46726cbe
CB
151
152 if ($path =~ m|/|) { # Fake Unix
ee8c7f54 153 my $pathify = $path =~ m|/\Z(?!\n)|;
fd7385b9 154 $path = $self->SUPER::canonpath($path);
46726cbe
CB
155 if ($pathify) { return vmspath($path); }
156 else { return vmsify($path); }
157 }
158 else {
fd7385b9 159 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
7b6ffde5
CB
160 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> [foo
161 $path =~ s/([^-]+)\.000000([\]\>])/$1$2/; # foo.000000] ==> foo]
099f76bb 162 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
fd7385b9
CB
163 $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar]
164 $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s
165 $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo
166 $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode
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
JD
213 my ($self,@files) = @_;
214 my $file = 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
272=cut
273
274my $tmpdir;
275sub tmpdir {
276 return $tmpdir if defined $tmpdir;
188ff3c1 277 foreach ('sys$scratch:', $ENV{TMPDIR}) {
cbc7acb0
JD
278 next unless defined && -d && -w _;
279 $tmpdir = $_;
280 last;
281 }
282 $tmpdir = '' unless defined $tmpdir;
283 return $tmpdir;
270d1e39
GS
284}
285
286=item updir (override)
287
cbc7acb0 288Returns a string representation of the parent directory: '[-]'
270d1e39
GS
289
290=cut
291
292sub updir {
293 return '[-]';
294}
295
46726cbe
CB
296=item case_tolerant (override)
297
298VMS file specification syntax is case-tolerant.
299
300=cut
301
302sub case_tolerant {
303 return 1;
304}
305
270d1e39
GS
306=item path (override)
307
308Translate logical name DCL$PATH as a searchlist, rather than trying
309to C<split> string value of C<$ENV{'PATH'}>.
310
311=cut
312
313sub path {
cbc7acb0 314 my (@dirs,$dir,$i);
270d1e39 315 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 316 return @dirs;
270d1e39
GS
317}
318
319=item file_name_is_absolute (override)
320
321Checks for VMS directory spec as well as Unix separators.
322
323=cut
324
325sub file_name_is_absolute {
cbc7acb0 326 my ($self,$file) = @_;
270d1e39 327 # If it's a logical name, expand it.
ee8c7f54 328 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
1b1e14d3 329 return scalar($file =~ m!^/!s ||
cbc7acb0
JD
330 $file =~ m![<\[][^.\-\]>]! ||
331 $file =~ /:[^<\[]/);
270d1e39
GS
332}
333
46726cbe
CB
334=item splitpath (override)
335
336Splits using VMS syntax.
337
338=cut
339
340sub splitpath {
341 my($self,$path) = @_;
342 my($dev,$dir,$file) = ('','','');
343
1b1e14d3 344 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
46726cbe
CB
345 return ($1 || '',$2 || '',$3);
346}
347
348=item splitdir (override)
349
350Split dirspec using VMS syntax.
351
352=cut
353
354sub splitdir {
355 my($self,$dirspec) = @_;
356 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
fd7385b9 357 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
46726cbe 358 my(@dirs) = split('\.', vmspath($dirspec));
ee8c7f54 359 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
46726cbe
CB
360 @dirs;
361}
362
363
364=item catpath (override)
365
366Construct a complete filespec using VMS syntax
367
368=cut
369
370sub catpath {
371 my($self,$dev,$dir,$file) = @_;
fd7385b9 372 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
ee8c7f54 373 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
fd7385b9
CB
374 if (length($dev) or length($dir)) {
375 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
376 $dir = vmspath($dir);
0994714a 377 }
fd7385b9 378 "$dev$dir$file";
0994714a
GS
379}
380
fd7385b9 381=item abs2rel (override)
0994714a 382
fd7385b9 383Use VMS syntax when converting filespecs.
0994714a
GS
384
385=cut
386
0994714a
GS
387sub abs2rel {
388 my $self = shift;
389
fd7385b9 390 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
0994714a
GS
391 if ( join( '', @_ ) =~ m{/} ) ;
392
393 my($path,$base) = @_;
394
395 # Note: we use '/' to glue things together here, then let canonpath()
396 # clean them up at the end.
397
398 # Clean up $path
399 if ( ! $self->file_name_is_absolute( $path ) ) {
400 $path = $self->rel2abs( $path ) ;
401 }
402 else {
403 $path = $self->canonpath( $path ) ;
404 }
405
406 # Figure out the effective $base and clean it up.
1d7cb664 407 if ( !defined( $base ) || $base eq '' ) {
0994714a
GS
408 $base = cwd() ;
409 }
1d7cb664
GS
410 elsif ( ! $self->file_name_is_absolute( $base ) ) {
411 $base = $self->rel2abs( $base ) ;
412 }
0994714a
GS
413 else {
414 $base = $self->canonpath( $base ) ;
415 }
416
417 # Split up paths
ee8c7f54
CB
418 my ( $path_directories, $path_file ) =
419 ($self->splitpath( $path, 1 ))[1,2] ;
0994714a
GS
420
421 $path_directories = $1
ee8c7f54 422 if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a 423
ee8c7f54 424 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
0994714a
GS
425
426 $base_directories = $1
ee8c7f54 427 if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a
GS
428
429 # Now, remove all leading components that are the same
430 my @pathchunks = $self->splitdir( $path_directories );
431 my @basechunks = $self->splitdir( $base_directories );
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 '' ) {
465 $base = cwd() ;
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
502L<File::Spec>
503
504=cut
505
5061;