File::Spec bugs (spotted by Hack Kampbjorn <hack@hackdata.com>)
[p5sagit/p5-mst-13.2.git] / lib / File / Spec / VMS.pm
CommitLineData
270d1e39 1package File::Spec::VMS;
2
cbc7acb0 3use strict;
4use vars qw(@ISA);
5require File::Spec::Unix;
270d1e39 6@ISA = qw(File::Spec::Unix);
7
cbc7acb0 8use File::Basename;
9use VMS::Filespec;
270d1e39 10
11=head1 NAME
12
13File::Spec::VMS - methods for VMS file specs
14
15=head1 SYNOPSIS
16
cbc7acb0 17 require File::Spec::VMS; # Done internally by File::Spec if needed
270d1e39 18
19=head1 DESCRIPTION
20
21See File::Spec::Unix for a documentation of the methods provided
22there. This package overrides the implementation of these methods, not
23the semantics.
24
a45bd81d 25=over
26
377875b9 27=item eliminate_macros
28
29Expands MM[KS]/Make macros in a text string, using the contents of
30identically named elements of C<%$self>, and returns the result
31as a file specification in Unix syntax.
32
1f47e8e2 33=cut
34
35sub eliminate_macros {
36 my($self,$path) = @_;
37 return '' unless $path;
38 $self = {} unless ref $self;
39 my($npath) = unixify($path);
40 my($complex) = 0;
41 my($head,$macro,$tail);
42
43 # perform m##g in scalar context so it acts as an iterator
14a089c5 44 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#gs) {
1f47e8e2 45 if ($self->{$2}) {
46 ($head,$macro,$tail) = ($1,$2,$3);
47 if (ref $self->{$macro}) {
48 if (ref $self->{$macro} eq 'ARRAY') {
49 $macro = join ' ', @{$self->{$macro}};
50 }
51 else {
52 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
53 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
54 $macro = "\cB$macro\cB";
55 $complex = 1;
56 }
57 }
1b1e14d3 58 else { ($macro = unixify($self->{$macro})) =~ s#/\z##; }
1f47e8e2 59 $npath = "$head$macro$tail";
60 }
61 }
14a089c5 62 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#gs; }
1f47e8e2 63 $npath;
64}
65
377875b9 66=item fixpath
67
68Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
69in any directory specification, in order to avoid juxtaposing two
70VMS-syntax directories when MM[SK] is run. Also expands expressions which
71are all macro, so that we can tell how long the expansion is, and avoid
72overrunning DCL's command buffer when MM[KS] is running.
73
74If optional second argument has a TRUE value, then the return string is
75a VMS-syntax directory specification, if it is FALSE, the return string
76is a VMS-syntax file specification, and if it is not specified, fixpath()
77checks to see whether it matches the name of a directory in the current
78default directory, and returns a directory or file specification accordingly.
79
80=cut
81
1f47e8e2 82sub fixpath {
83 my($self,$path,$force_path) = @_;
84 return '' unless $path;
85 $self = bless {} unless ref $self;
86 my($fixedpath,$prefix,$name);
87
1b1e14d3 88 if ($path =~ m#^\$\([^\)]+\)\z#s || $path =~ m#[/:>\]]#) {
89 if ($force_path or $path =~ /(?:DIR\)|\])\z/) {
1f47e8e2 90 $fixedpath = vmspath($self->eliminate_macros($path));
91 }
92 else {
93 $fixedpath = vmsify($self->eliminate_macros($path));
94 }
95 }
1b1e14d3 96 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#s)) && $self->{$prefix}) {
1f47e8e2 97 my($vmspre) = $self->eliminate_macros("\$($prefix)");
98 # is it a dir or just a name?
1b1e14d3 99 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR\z/) ? vmspath($vmspre) : '';
1f47e8e2 100 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
101 $fixedpath = vmspath($fixedpath) if $force_path;
102 }
103 else {
104 $fixedpath = $path;
105 $fixedpath = vmspath($fixedpath) if $force_path;
106 }
107 # No hints, so we try to guess
108 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
109 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
110 }
46726cbe 111
1f47e8e2 112 # Trim off root dirname if it's had other dirs inserted in front of it.
113 $fixedpath =~ s/\.000000([\]>])/$1/;
46726cbe 114 # Special case for VMS absolute directory specs: these will have had device
115 # prepended during trip through Unix syntax in eliminate_macros(), since
116 # Unix syntax has no way to express "absolute from the top of this device's
117 # directory tree".
118 if ($path =~ /^[\[>][^.\-]/) { $fixedpath =~ s/^[^\[<]+//; }
1f47e8e2 119 $fixedpath;
120}
121
a45bd81d 122=back
1f47e8e2 123
270d1e39 124=head2 Methods always loaded
125
126=over
127
46726cbe 128=item canonpath (override)
129
130Removes redundant portions of file specifications according to VMS syntax
131
132=cut
133
134sub canonpath {
135 my($self,$path,$reduce_ricochet) = @_;
136
137 if ($path =~ m|/|) { # Fake Unix
14a089c5 138 my $pathify = $path =~ m|/\z|;
46726cbe 139 $path = $self->SUPER::canonpath($path,$reduce_ricochet);
140 if ($pathify) { return vmspath($path); }
141 else { return vmsify($path); }
142 }
143 else {
144 $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar
145 $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo
146 $path =~ s/[\[<\.]([^\[<\.]+)\.-\.\1//g; # bar.foo.-.foo ==> bar.
147 if ($reduce_ricochet) { $path =~ s/[^\[\-<.]+\.\-//g; }
148 return $path;
149 }
150}
151
270d1e39 152=item catdir
153
154Concatenates a list of file specifications, and returns the result as a
46726cbe 155VMS-syntax directory specification. No check is made for "impossible"
156cases (e.g. elements other than the first being absolute filespecs).
270d1e39 157
158=cut
159
160sub catdir {
cbc7acb0 161 my ($self,@dirs) = @_;
162 my $dir = pop @dirs;
270d1e39 163 @dirs = grep($_,@dirs);
cbc7acb0 164 my $rslt;
270d1e39 165 if (@dirs) {
cbc7acb0 166 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
167 my ($spath,$sdir) = ($path,$dir);
14a089c5 168 $spath =~ s/\.dir\z//; $sdir =~ s/\.dir\z//;
1b1e14d3 169 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\z/s;
cbc7acb0 170 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
46726cbe 171
172 # Special case for VMS absolute directory specs: these will have had device
173 # prepended during trip through Unix syntax in eliminate_macros(), since
174 # Unix syntax has no way to express "absolute from the top of this device's
175 # directory tree".
1b1e14d3 176 if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; }
270d1e39 177 }
cbc7acb0 178 else {
1b1e14d3 179 if ($dir =~ /^\$\([^\)]+\)\z/s) { $rslt = $dir; }
180 else { $rslt = vmspath($dir); }
270d1e39 181 }
cbc7acb0 182 return $rslt;
270d1e39 183}
184
185=item catfile
186
187Concatenates a list of file specifications, and returns the result as a
46726cbe 188VMS-syntax file specification.
270d1e39 189
190=cut
191
192sub catfile {
cbc7acb0 193 my ($self,@files) = @_;
194 my $file = pop @files;
270d1e39 195 @files = grep($_,@files);
cbc7acb0 196 my $rslt;
270d1e39 197 if (@files) {
cbc7acb0 198 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
199 my $spath = $path;
14a089c5 200 $spath =~ s/\.dir\z//;
1b1e14d3 201 if ($spath =~ /^[^\)\]\/:>]+\)\z/s && basename($file) eq $file) {
cbc7acb0 202 $rslt = "$spath$file";
203 }
204 else {
205 $rslt = $self->eliminate_macros($spath);
206 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
207 }
270d1e39 208 }
209 else { $rslt = vmsify($file); }
cbc7acb0 210 return $rslt;
270d1e39 211}
212
46726cbe 213
270d1e39 214=item curdir (override)
215
cbc7acb0 216Returns a string representation of the current directory: '[]'
270d1e39 217
218=cut
219
220sub curdir {
221 return '[]';
222}
223
99804bbb 224=item devnull (override)
225
cbc7acb0 226Returns a string representation of the null device: '_NLA0:'
99804bbb 227
228=cut
229
230sub devnull {
cbc7acb0 231 return "_NLA0:";
99804bbb 232}
233
270d1e39 234=item rootdir (override)
235
cbc7acb0 236Returns a string representation of the root directory: 'SYS$DISK:[000000]'
270d1e39 237
238=cut
239
240sub rootdir {
cbc7acb0 241 return 'SYS$DISK:[000000]';
242}
243
244=item tmpdir (override)
245
246Returns a string representation of the first writable directory
247from the following list or '' if none are writable:
248
249 /sys$scratch
250 $ENV{TMPDIR}
251
252=cut
253
254my $tmpdir;
255sub tmpdir {
256 return $tmpdir if defined $tmpdir;
257 foreach ('/sys$scratch', $ENV{TMPDIR}) {
258 next unless defined && -d && -w _;
259 $tmpdir = $_;
260 last;
261 }
262 $tmpdir = '' unless defined $tmpdir;
263 return $tmpdir;
270d1e39 264}
265
266=item updir (override)
267
cbc7acb0 268Returns a string representation of the parent directory: '[-]'
270d1e39 269
270=cut
271
272sub updir {
273 return '[-]';
274}
275
46726cbe 276=item case_tolerant (override)
277
278VMS file specification syntax is case-tolerant.
279
280=cut
281
282sub case_tolerant {
283 return 1;
284}
285
270d1e39 286=item path (override)
287
288Translate logical name DCL$PATH as a searchlist, rather than trying
289to C<split> string value of C<$ENV{'PATH'}>.
290
291=cut
292
293sub path {
cbc7acb0 294 my (@dirs,$dir,$i);
270d1e39 295 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 296 return @dirs;
270d1e39 297}
298
299=item file_name_is_absolute (override)
300
301Checks for VMS directory spec as well as Unix separators.
302
303=cut
304
305sub file_name_is_absolute {
cbc7acb0 306 my ($self,$file) = @_;
270d1e39 307 # If it's a logical name, expand it.
1b1e14d3 308 $file = $ENV{$file} while $file =~ /^[\w\$\-]+\z/s && $ENV{$file};
309 return scalar($file =~ m!^/!s ||
cbc7acb0 310 $file =~ m![<\[][^.\-\]>]! ||
311 $file =~ /:[^<\[]/);
270d1e39 312}
313
46726cbe 314=item splitpath (override)
315
316Splits using VMS syntax.
317
318=cut
319
320sub splitpath {
321 my($self,$path) = @_;
322 my($dev,$dir,$file) = ('','','');
323
1b1e14d3 324 vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s;
46726cbe 325 return ($1 || '',$2 || '',$3);
326}
327
328=item splitdir (override)
329
330Split dirspec using VMS syntax.
331
332=cut
333
334sub splitdir {
335 my($self,$dirspec) = @_;
336 $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g;
337 my(@dirs) = split('\.', vmspath($dirspec));
1b1e14d3 338 $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\z//s;
46726cbe 339 @dirs;
340}
341
342
343=item catpath (override)
344
345Construct a complete filespec using VMS syntax
346
347=cut
348
349sub catpath {
350 my($self,$dev,$dir,$file) = @_;
351 if ($dev =~ m|^/+([^/]+)|) { $dev =~ "$1:"; }
14a089c5 352 else { $dev .= ':' unless $dev eq '' or $dev =~ /:\z/; }
46726cbe 353 $dir = vmspath($dir);
354 "$dev$dir$file";
355}
356
0994714a 357=item splitpath
358
359 ($volume,$directories,$file) = File::Spec->splitpath( $path );
360 ($volume,$directories,$file) = File::Spec->splitpath( $path, $no_file );
361
362Splits a VMS path in to volume, directory, and filename portions.
363Ignores $no_file, if present, since VMS paths indicate the 'fileness' of a
364file.
365
366The results can be passed to L</catpath()> to get back a path equivalent to
367(usually identical to) the original path.
368
369=cut
370
371sub splitpath {
372 my $self = shift ;
373 my ($path, $nofile) = @_;
374
375 my ($volume,$directory,$file) ;
376
377 if ( $path =~ m{/} ) {
378 $path =~
379 m{^ ( (?: /[^/]* )? )
14a089c5 380 ( (?: .*/(?:[^/]+\.dir)? )? )
0994714a 381 (.*)
1b1e14d3 382 }xs;
0994714a 383 $volume = $1;
384 $directory = $2;
385 $file = $3;
386 }
387 else {
388 $path =~
389 m{^ ( (?: (?: (?: [\w\$-]+ (?: "[^"]*")?:: )? [\w\$-]+: )? ) )
390 ( (?:\[.*\])? )
391 (.*)
1b1e14d3 392 }xs;
0994714a 393 $volume = $1;
394 $directory = $2;
395 $file = $3;
396 }
397
398 $directory = $1
1b1e14d3 399 if $directory =~ /^\[(.*)\]\z/s ;
0994714a 400
401 return ($volume,$directory,$file);
402}
403
404
405=item splitdir
406
407The opposite of L</catdir()>.
408
409 @dirs = File::Spec->splitdir( $directories );
410
411$directories must be only the directory portion of the path.
412
413'[' and ']' delimiters are optional. An empty string argument is
414equivalent to '[]': both return an array with no elements.
415
416=cut
417
418sub splitdir {
419 my $self = shift ;
420 my $directories = $_[0] ;
421
422 return File::Spec::Unix::splitdir( $self, @_ )
423 if ( $directories =~ m{/} ) ;
424
1b1e14d3 425 $directories =~ s/^\[(.*)\]\z/$1/s ;
0994714a 426
427 #
428 # split() likes to forget about trailing null fields, so here we
429 # check to be sure that there will not be any before handling the
430 # simple case.
431 #
1b1e14d3 432 if ( $directories !~ m{\.\z} ) {
0994714a 433 return split( m{\.}, $directories );
434 }
435 else {
436 #
437 # since there was a trailing separator, add a file name to the end,
438 # then do the split, then replace it with ''.
439 #
440 my( @directories )= split( m{\.}, "${directories}dummy" ) ;
441 $directories[ $#directories ]= '' ;
442 return @directories ;
443 }
444}
445
446
447sub catpath {
448 my $self = shift;
449
450 return File::Spec::Unix::catpath( $self, @_ )
451 if ( join( '', @_ ) =~ m{/} ) ;
452
453 my ($volume,$directory,$file) = @_;
454
455 $volume .= ':'
1b1e14d3 456 if $volume =~ /[^:]\z/ ;
0994714a 457
458 $directory = "[$directory"
1b1e14d3 459 if $directory =~ /^[^\[]/s ;
0994714a 460
461 $directory .= ']'
1b1e14d3 462 if $directory =~ /[^\]]\z/ ;
0994714a 463
464 return "$volume$directory$file" ;
465}
466
467
468sub abs2rel {
469 my $self = shift;
470
471 return File::Spec::Unix::abs2rel( $self, @_ )
472 if ( join( '', @_ ) =~ m{/} ) ;
473
474 my($path,$base) = @_;
475
476 # Note: we use '/' to glue things together here, then let canonpath()
477 # clean them up at the end.
478
479 # Clean up $path
480 if ( ! $self->file_name_is_absolute( $path ) ) {
481 $path = $self->rel2abs( $path ) ;
482 }
483 else {
484 $path = $self->canonpath( $path ) ;
485 }
486
487 # Figure out the effective $base and clean it up.
1d7cb664 488 if ( !defined( $base ) || $base eq '' ) {
0994714a 489 $base = cwd() ;
490 }
1d7cb664 491 elsif ( ! $self->file_name_is_absolute( $base ) ) {
492 $base = $self->rel2abs( $base ) ;
493 }
0994714a 494 else {
495 $base = $self->canonpath( $base ) ;
496 }
497
498 # Split up paths
499 my ( undef, $path_directories, $path_file ) =
500 $self->splitpath( $path, 1 ) ;
501
502 $path_directories = $1
1b1e14d3 503 if $path_directories =~ /^\[(.*)\]\z/s ;
0994714a 504
505 my ( undef, $base_directories, undef ) =
506 $self->splitpath( $base, 1 ) ;
507
508 $base_directories = $1
1b1e14d3 509 if $base_directories =~ /^\[(.*)\]\z/s ;
0994714a 510
511 # Now, remove all leading components that are the same
512 my @pathchunks = $self->splitdir( $path_directories );
513 my @basechunks = $self->splitdir( $base_directories );
514
515 while ( @pathchunks &&
516 @basechunks &&
517 lc( $pathchunks[0] ) eq lc( $basechunks[0] )
518 ) {
519 shift @pathchunks ;
520 shift @basechunks ;
521 }
522
523 # @basechunks now contains the directories to climb out of,
524 # @pathchunks now has the directories to descend in to.
525 $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ;
1b1e14d3 526 $path_directories =~ s{\.\z}{} ;
0994714a 527 return $self->catpath( '', $path_directories, $path_file ) ;
528}
529
530
531sub rel2abs($;$;) {
532 my $self = shift ;
533 return File::Spec::Unix::rel2abs( $self, @_ )
534 if ( join( '', @_ ) =~ m{/} ) ;
535
536 my ($path,$base ) = @_;
537 # Clean up and split up $path
538 if ( ! $self->file_name_is_absolute( $path ) ) {
539 # Figure out the effective $base and clean it up.
540 if ( !defined( $base ) || $base eq '' ) {
541 $base = cwd() ;
542 }
543 elsif ( ! $self->file_name_is_absolute( $base ) ) {
544 $base = $self->rel2abs( $base ) ;
545 }
546 else {
547 $base = $self->canonpath( $base ) ;
548 }
549
550 # Split up paths
551 my ( undef, $path_directories, $path_file ) =
552 $self->splitpath( $path ) ;
553
554 my ( $base_volume, $base_directories, undef ) =
555 $self->splitpath( $base ) ;
556
557 my $sep = '' ;
558 $sep = '.'
1b1e14d3 559 if ( $base_directories =~ m{[^.]\z} &&
560 $path_directories =~ m{^[^.]}s
0994714a 561 ) ;
562 $base_directories = "$base_directories$sep$path_directories" ;
563
564 $path = $self->catpath( $base_volume, $base_directories, $path_file );
565 }
566
567 return $self->canonpath( $path ) ;
568}
569
570
cbc7acb0 571=back
270d1e39 572
cbc7acb0 573=head1 SEE ALSO
574
575L<File::Spec>
576
577=cut
578
5791;