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