Re-integrate 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
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
5bc28da9 25=item eliminate_macros
26
27Expands MM[KS]/Make macros in a text string, using the contents of
28identically named elements of C<%$self>, and returns the result
29as a file specification in Unix syntax.
30
1f47e8e2 31=cut
32
33sub eliminate_macros {
34 my($self,$path) = @_;
35 return '' unless $path;
36 $self = {} unless ref $self;
37 my($npath) = unixify($path);
38 my($complex) = 0;
39 my($head,$macro,$tail);
40
41 # perform m##g in scalar context so it acts as an iterator
42 while ($npath =~ m#(.*?)\$\((\S+?)\)(.*)#g) {
43 if ($self->{$2}) {
44 ($head,$macro,$tail) = ($1,$2,$3);
45 if (ref $self->{$macro}) {
46 if (ref $self->{$macro} eq 'ARRAY') {
47 $macro = join ' ', @{$self->{$macro}};
48 }
49 else {
50 print "Note: can't expand macro \$($macro) containing ",ref($self->{$macro}),
51 "\n\t(using MMK-specific deferred substitutuon; MMS will break)\n";
52 $macro = "\cB$macro\cB";
53 $complex = 1;
54 }
55 }
56 else { ($macro = unixify($self->{$macro})) =~ s#/$##; }
57 $npath = "$head$macro$tail";
58 }
59 }
60 if ($complex) { $npath =~ s#\cB(.*?)\cB#\${$1}#g; }
61 $npath;
62}
63
5bc28da9 64=item fixpath
65
66Catchall routine to clean up problem MM[SK]/Make macros. Expands macros
67in any directory specification, in order to avoid juxtaposing two
68VMS-syntax directories when MM[SK] is run. Also expands expressions which
69are all macro, so that we can tell how long the expansion is, and avoid
70overrunning DCL's command buffer when MM[KS] is running.
71
72If optional second argument has a TRUE value, then the return string is
73a VMS-syntax directory specification, if it is FALSE, the return string
74is a VMS-syntax file specification, and if it is not specified, fixpath()
75checks to see whether it matches the name of a directory in the current
76default directory, and returns a directory or file specification accordingly.
77
78=cut
79
1f47e8e2 80sub fixpath {
81 my($self,$path,$force_path) = @_;
82 return '' unless $path;
83 $self = bless {} unless ref $self;
84 my($fixedpath,$prefix,$name);
85
86 if ($path =~ m#^\$\([^\)]+\)$# || $path =~ m#[/:>\]]#) {
87 if ($force_path or $path =~ /(?:DIR\)|\])$/) {
88 $fixedpath = vmspath($self->eliminate_macros($path));
89 }
90 else {
91 $fixedpath = vmsify($self->eliminate_macros($path));
92 }
93 }
94 elsif ((($prefix,$name) = ($path =~ m#^\$\(([^\)]+)\)(.+)#)) && $self->{$prefix}) {
95 my($vmspre) = $self->eliminate_macros("\$($prefix)");
96 # is it a dir or just a name?
97 $vmspre = ($vmspre =~ m|/| or $prefix =~ /DIR$/) ? vmspath($vmspre) : '';
98 $fixedpath = ($vmspre ? $vmspre : $self->{$prefix}) . $name;
99 $fixedpath = vmspath($fixedpath) if $force_path;
100 }
101 else {
102 $fixedpath = $path;
103 $fixedpath = vmspath($fixedpath) if $force_path;
104 }
105 # No hints, so we try to guess
106 if (!defined($force_path) and $fixedpath !~ /[:>(.\]]/) {
107 $fixedpath = vmspath($fixedpath) if -d $fixedpath;
108 }
109 # Trim off root dirname if it's had other dirs inserted in front of it.
110 $fixedpath =~ s/\.000000([\]>])/$1/;
111 $fixedpath;
112}
113
114
270d1e39 115=head2 Methods always loaded
116
117=over
118
119=item catdir
120
121Concatenates a list of file specifications, and returns the result as a
122VMS-syntax directory specification.
123
124=cut
125
126sub catdir {
cbc7acb0 127 my ($self,@dirs) = @_;
128 my $dir = pop @dirs;
270d1e39 129 @dirs = grep($_,@dirs);
cbc7acb0 130 my $rslt;
270d1e39 131 if (@dirs) {
cbc7acb0 132 my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs));
133 my ($spath,$sdir) = ($path,$dir);
134 $spath =~ s/.dir$//; $sdir =~ s/.dir$//;
135 $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+$/;
136 $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1);
270d1e39 137 }
cbc7acb0 138 else {
139 if ($dir =~ /^\$\([^\)]+\)$/) { $rslt = $dir; }
140 else { $rslt = vmspath($dir); }
270d1e39 141 }
cbc7acb0 142 return $rslt;
270d1e39 143}
144
145=item catfile
146
147Concatenates a list of file specifications, and returns the result as a
148VMS-syntax directory specification.
149
150=cut
151
152sub catfile {
cbc7acb0 153 my ($self,@files) = @_;
154 my $file = pop @files;
270d1e39 155 @files = grep($_,@files);
cbc7acb0 156 my $rslt;
270d1e39 157 if (@files) {
cbc7acb0 158 my $path = (@files == 1 ? $files[0] : $self->catdir(@files));
159 my $spath = $path;
160 $spath =~ s/.dir$//;
161 if ($spath =~ /^[^\)\]\/:>]+\)$/ && basename($file) eq $file) {
162 $rslt = "$spath$file";
163 }
164 else {
165 $rslt = $self->eliminate_macros($spath);
166 $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file));
167 }
270d1e39 168 }
169 else { $rslt = vmsify($file); }
cbc7acb0 170 return $rslt;
270d1e39 171}
172
173=item curdir (override)
174
cbc7acb0 175Returns a string representation of the current directory: '[]'
270d1e39 176
177=cut
178
179sub curdir {
180 return '[]';
181}
182
99804bbb 183=item devnull (override)
184
cbc7acb0 185Returns a string representation of the null device: '_NLA0:'
99804bbb 186
187=cut
188
189sub devnull {
cbc7acb0 190 return "_NLA0:";
99804bbb 191}
192
270d1e39 193=item rootdir (override)
194
cbc7acb0 195Returns a string representation of the root directory: 'SYS$DISK:[000000]'
270d1e39 196
197=cut
198
199sub rootdir {
cbc7acb0 200 return 'SYS$DISK:[000000]';
201}
202
203=item tmpdir (override)
204
205Returns a string representation of the first writable directory
206from the following list or '' if none are writable:
207
208 /sys$scratch
209 $ENV{TMPDIR}
210
211=cut
212
213my $tmpdir;
214sub tmpdir {
215 return $tmpdir if defined $tmpdir;
216 foreach ('/sys$scratch', $ENV{TMPDIR}) {
217 next unless defined && -d && -w _;
218 $tmpdir = $_;
219 last;
220 }
221 $tmpdir = '' unless defined $tmpdir;
222 return $tmpdir;
270d1e39 223}
224
225=item updir (override)
226
cbc7acb0 227Returns a string representation of the parent directory: '[-]'
270d1e39 228
229=cut
230
231sub updir {
232 return '[-]';
233}
234
235=item path (override)
236
237Translate logical name DCL$PATH as a searchlist, rather than trying
238to C<split> string value of C<$ENV{'PATH'}>.
239
240=cut
241
242sub path {
cbc7acb0 243 my (@dirs,$dir,$i);
270d1e39 244 while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); }
cbc7acb0 245 return @dirs;
270d1e39 246}
247
248=item file_name_is_absolute (override)
249
250Checks for VMS directory spec as well as Unix separators.
251
252=cut
253
254sub file_name_is_absolute {
cbc7acb0 255 my ($self,$file) = @_;
270d1e39 256 # If it's a logical name, expand it.
cbc7acb0 257 $file = $ENV{$file} while $file =~ /^[\w\$\-]+$/ && $ENV{$file};
258 return scalar($file =~ m!^/! ||
259 $file =~ m![<\[][^.\-\]>]! ||
260 $file =~ /:[^<\[]/);
270d1e39 261}
262
cbc7acb0 263=back
270d1e39 264
cbc7acb0 265=head1 SEE ALSO
266
267L<File::Spec>
268
269=cut
270
2711;