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