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