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