Re: File/Spec/t/rel2abs2rel2whatever broken again
[p5sagit/p5-mst-13.2.git] / lib / File / Spec / VMS.pm
CommitLineData
270d1e39 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 9@ISA = qw(File::Spec::Unix);
10
178326fd 11use Cwd;
cbc7acb0 12use File::Basename;
13use VMS::Filespec;
270d1e39 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 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 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 37=cut
38
39sub eliminate_macros {
40 my($self,$path) = @_;
41 return '' unless $path;
42 $self = {} unless ref $self;
bdb84599 43
44 if ($path =~ /\s/) {
45 return join ' ', map { $self->eliminate_macros($_) } split /\s+/, $path;
46 }
47
1f47e8e2 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 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 68 $npath = "$head$macro$tail";
69 }
70 }
14a089c5 71 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
1f47e8e2 72 $npath;
73}
74
377875b9 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 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 97 if ($path =~ /\s/) {
98 return join ' ',
99 map { $self->fixpath($_,$force_path) }
100 split /\s+/, $path;
101 }
102
ee8c7f54 103 if ($path =~ m#^\$\([^\)]+\)\Z(?!\n)#s || $path =~ m#[/:>\]]#) {
104 if ($force_path or $path =~ /(?:DIR\)|\])\Z(?!\n)/) {
1f47e8e2 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 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 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 127 # Trim off root dirname if it's had other dirs inserted in front of it.
128 $fixedpath =~ s/\.000000([\]>])/$1/;
46726cbe 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 134 $fixedpath;
135}
136
a45bd81d 137=back
1f47e8e2 138
270d1e39 139=head2 Methods always loaded
140
bbc7dcd2 141=over 4
270d1e39 142
46726cbe 143=item canonpath (override)
144
fd7385b9 145Removes redundant portions of file specifications according to VMS syntax.
46726cbe 146
147=cut
148
149sub canonpath {
fd7385b9 150 my($self,$path) = @_;
46726cbe 151
152 if ($path =~ m|/|) { # Fake Unix
ee8c7f54 153 my $pathify = $path =~ m|/\Z(?!\n)|;
fd7385b9 154 $path = $self->SUPER::canonpath($path);
46726cbe 155 if ($pathify) { return vmspath($path); }
156 else { return vmsify($path); }
157 }
158 else {
bcd0738c 159 $path =~ s/([\[<])000000\./$1/g; # [000000.foo ==> [foo
0e9538e6 160 $path =~ s/([^-]+)\.(\]\[|><)?000000([\]\>])/$1$3/g; # foo.000000] ==> foo]
fd7385b9 161 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
099f76bb 162 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [--
fd7385b9 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 167 return $path;
168 }
169}
170
270d1e39 171=item catdir
172
173Concatenates a list of file specifications, and returns the result as a
46726cbe 174VMS-syntax directory specification. No check is made for "impossible"
175cases (e.g. elements other than the first being absolute filespecs).
270d1e39 176
177=cut
178
179sub catdir {
cbc7acb0 180 my ($self,@dirs) = @_;
181 my $dir = pop @dirs;
270d1e39 182 @dirs = grep($_,@dirs);
cbc7acb0 183 my $rslt;
270d1e39 184 if (@dirs) {
cbc7acb0 185 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
186 my ($spath,$sdir) = ($path,$dir);
ee8c7f54 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 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 203}
204
205=item catfile
206
207Concatenates a list of file specifications, and returns the result as a
46726cbe 208VMS-syntax file specification.
270d1e39 209
210=cut
211
212sub catfile {
cbc7acb0 213 my ($self,@files) = @_;
214 my $file = pop @files;
270d1e39 215 @files = grep($_,@files);
cbc7acb0 216 my $rslt;
270d1e39 217 if (@files) {
cbc7acb0 218 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
219 my $spath = $path;
ee8c7f54 220 $spath =~ s/\.dir\Z(?!\n)//;
221 if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) {
cbc7acb0 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 231}
232
46726cbe 233
270d1e39 234=item curdir (override)
235
cbc7acb0 236Returns a string representation of the current directory: '[]'
270d1e39 237
238=cut
239
240sub curdir {
241 return '[]';
242}
243
99804bbb 244=item devnull (override)
245
cbc7acb0 246Returns a string representation of the null device: '_NLA0:'
99804bbb 247
248=cut
249
250sub devnull {
cbc7acb0 251 return "_NLA0:";
99804bbb 252}
253
270d1e39 254=item rootdir (override)
255
cbc7acb0 256Returns a string representation of the root directory: 'SYS$DISK:[000000]'
270d1e39 257
258=cut
259
260sub rootdir {
cbc7acb0 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 270 $ENV{TMPDIR}
271
a384e9e1 272Since perl 5.8.0, if running under taint mode, and if $ENV{TMPDIR}
273is tainted, it is not used.
274
cbc7acb0 275=cut
276
277my $tmpdir;
278sub tmpdir {
279 return $tmpdir if defined $tmpdir;
a384e9e1 280 my @dirlist = ('sys$scratch:', $ENV{TMPDIR});
281 {
282 no strict 'refs';
283 if (${"\cTAINT"}) { # Check for taint mode on perl >= 5.8.0
284 require Scalar::Util;
285 pop @dirlist if Scalar::Util::tainted($ENV{TMPDIR});
286 }
287 }
288 foreach (@dirlist) {
cbc7acb0 289 next unless defined && -d && -w _;
290 $tmpdir = $_;
291 last;
292 }
293 $tmpdir = '' unless defined $tmpdir;
294 return $tmpdir;
270d1e39 295}
296
297=item updir (override)
298
cbc7acb0 299Returns a string representation of the parent directory: '[-]'
270d1e39 300
301=cut
302
303sub updir {
304 return '[-]';
305}
306
46726cbe 307=item case_tolerant (override)
308
309VMS file specification syntax is case-tolerant.
310
311=cut
312
313sub case_tolerant {
314 return 1;
315}
316
270d1e39 317=item path (override)
318
319Translate logical name DCL$PATH as a searchlist, rather than trying
320to C<split> string value of C<$ENV{'PATH'}>.
321
322=cut
323
324sub path {
cbc7acb0 325 my (@dirs,$dir,$i);
270d1e39 326 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 327 return @dirs;
270d1e39 328}
329
330=item file_name_is_absolute (override)
331
332Checks for VMS directory spec as well as Unix separators.
333
334=cut
335
336sub file_name_is_absolute {
cbc7acb0 337 my ($self,$file) = @_;
270d1e39 338 # If it's a logical name, expand it.
ee8c7f54 339 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file};
1b1e14d3 340 return scalar($file =~ m!^/!s ||
cbc7acb0 341 $file =~ m![<\[][^.\-\]>]! ||
342 $file =~ /:[^<\[]/);
270d1e39 343}
344
46726cbe 345=item splitpath (override)
346
347Splits using VMS syntax.
348
349=cut
350
351sub splitpath {
352 my($self,$path) = @_;
353 my($dev,$dir,$file) = ('','','');
354
1b1e14d3 355 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
46726cbe 356 return ($1 || '',$2 || '',$3);
357}
358
359=item splitdir (override)
360
361Split dirspec using VMS syntax.
362
363=cut
364
365sub splitdir {
366 my($self,$dirspec) = @_;
367 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
fd7385b9 368 $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal
46726cbe 369 my(@dirs) = split('\.', vmspath($dirspec));
ee8c7f54 370 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s;
46726cbe 371 @dirs;
372}
373
374
375=item catpath (override)
376
377Construct a complete filespec using VMS syntax
378
379=cut
380
381sub catpath {
382 my($self,$dev,$dir,$file) = @_;
fd7385b9 383 if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; }
ee8c7f54 384 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; }
fd7385b9 385 if (length($dev) or length($dir)) {
386 $dir = "[$dir]" unless $dir =~ /[\[<\/]/;
387 $dir = vmspath($dir);
0994714a 388 }
fd7385b9 389 "$dev$dir$file";
0994714a 390}
391
fd7385b9 392=item abs2rel (override)
0994714a 393
fd7385b9 394Use VMS syntax when converting filespecs.
0994714a 395
396=cut
397
0994714a 398sub abs2rel {
399 my $self = shift;
400
fd7385b9 401 return vmspath(File::Spec::Unix::abs2rel( $self, @_ ))
0994714a 402 if ( join( '', @_ ) =~ m{/} ) ;
403
404 my($path,$base) = @_;
405
406 # Note: we use '/' to glue things together here, then let canonpath()
407 # clean them up at the end.
408
409 # Clean up $path
410 if ( ! $self->file_name_is_absolute( $path ) ) {
411 $path = $self->rel2abs( $path ) ;
412 }
413 else {
414 $path = $self->canonpath( $path ) ;
415 }
416
417 # Figure out the effective $base and clean it up.
1d7cb664 418 if ( !defined( $base ) || $base eq '' ) {
0994714a 419 $base = cwd() ;
737c380e 420 $base = $self->canonpath( $base ) ;
0994714a 421 }
1d7cb664 422 elsif ( ! $self->file_name_is_absolute( $base ) ) {
423 $base = $self->rel2abs( $base ) ;
424 }
0994714a 425 else {
426 $base = $self->canonpath( $base ) ;
427 }
428
429 # Split up paths
ee8c7f54 430 my ( $path_directories, $path_file ) =
431 ($self->splitpath( $path, 1 ))[1,2] ;
0994714a 432
433 $path_directories = $1
ee8c7f54 434 if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a 435
ee8c7f54 436 my $base_directories = ($self->splitpath( $base, 1 ))[1] ;
0994714a 437
438 $base_directories = $1
ee8c7f54 439 if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ;
0994714a 440
441 # Now, remove all leading components that are the same
442 my @pathchunks = $self->splitdir( $path_directories );
737c380e 443 unshift(@pathchunks,'000000') unless $pathchunks[0] eq '000000';
0994714a 444 my @basechunks = $self->splitdir( $base_directories );
737c380e 445 unshift(@basechunks,'000000') unless $basechunks[0] eq '000000';
0994714a 446
447 while ( @pathchunks &&
448 @basechunks &&
449 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
450 ) {
451 shift @pathchunks ;
452 shift @basechunks ;
453 }
454
455 # @basechunks now contains the directories to climb out of,
456 # @pathchunks now has the directories to descend in to.
457 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
ee8c7f54 458 $path_directories =~ s{\.\Z(?!\n)}{} ;
fd7385b9 459 return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ;
0994714a 460}
461
462
fd7385b9 463=item rel2abs (override)
464
465Use VMS syntax when converting filespecs.
466
467=cut
468
786b702f 469sub rel2abs {
0994714a 470 my $self = shift ;
fd7385b9 471 return vmspath(File::Spec::Unix::rel2abs( $self, @_ ))
0994714a 472 if ( join( '', @_ ) =~ m{/} ) ;
473
474 my ($path,$base ) = @_;
475 # Clean up and split up $path
476 if ( ! $self->file_name_is_absolute( $path ) ) {
477 # Figure out the effective $base and clean it up.
478 if ( !defined( $base ) || $base eq '' ) {
479 $base = cwd() ;
480 }
481 elsif ( ! $self->file_name_is_absolute( $base ) ) {
482 $base = $self->rel2abs( $base ) ;
483 }
484 else {
485 $base = $self->canonpath( $base ) ;
486 }
487
488 # Split up paths
ee8c7f54 489 my ( $path_directories, $path_file ) =
490 ($self->splitpath( $path ))[1,2] ;
0994714a 491
ee8c7f54 492 my ( $base_volume, $base_directories ) =
0994714a 493 $self->splitpath( $base ) ;
494
fd7385b9 495 $path_directories = '' if $path_directories eq '[]' ||
496 $path_directories eq '<>';
0994714a 497 my $sep = '' ;
498 $sep = '.'
ee8c7f54 499 if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} &&
fd7385b9 500 $path_directories =~ m{^[^.\[<]}s
0994714a 501 ) ;
fd7385b9 502 $base_directories = "$base_directories$sep$path_directories";
503 $base_directories =~ s{\.?[\]>][\[<]\.?}{.};
0994714a 504
505 $path = $self->catpath( $base_volume, $base_directories, $path_file );
506 }
507
508 return $self->canonpath( $path ) ;
509}
510
511
cbc7acb0 512=back
270d1e39 513
cbc7acb0 514=head1 SEE ALSO
515
516L<File::Spec>
517
518=cut
519
5201;