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 | |
7 | $VERSION = '1.1'; |
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 | |
a45bd81d |
29 | =over |
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 | |
141 | =over |
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 { |
fd7385b9 |
159 | $path =~ s-\]\[--g; $path =~ s/><//g; # foo.][bar ==> foo.bar |
160 | $path =~ s/([\[<])000000\./$1/; # [000000.foo ==> foo |
099f76bb |
161 | 1 while $path =~ s{([\[<-])\.-}{$1-}; # [.-.- ==> [-- |
fd7385b9 |
162 | $path =~ s/\.[^\[<\.]+\.-([\]\>])/$1/; # bar.foo.-] ==> bar] |
163 | $path =~ s/([\[<])(-+)/$1 . "\cx" x length($2)/e; # encode leading '-'s |
164 | $path =~ s/([\[<\.])([^\[<\.\cx]+)\.-\.?/$1/g; # bar.-.foo ==> foo |
165 | $path =~ s/([\[<])(\cx+)/$1 . '-' x length($2)/e; # then decode |
46726cbe |
166 | return $path; |
167 | } |
168 | } |
169 | |
270d1e39 |
170 | =item catdir |
171 | |
172 | Concatenates a list of file specifications, and returns the result as a |
46726cbe |
173 | VMS-syntax directory specification. No check is made for "impossible" |
174 | cases (e.g. elements other than the first being absolute filespecs). |
270d1e39 |
175 | |
176 | =cut |
177 | |
178 | sub catdir { |
cbc7acb0 |
179 | my ($self,@dirs) = @_; |
180 | my $dir = pop @dirs; |
270d1e39 |
181 | @dirs = grep($_,@dirs); |
cbc7acb0 |
182 | my $rslt; |
270d1e39 |
183 | if (@dirs) { |
cbc7acb0 |
184 | my $path = (@dirs == 1 ? $dirs[0] : $self->catdir(@dirs)); |
185 | my ($spath,$sdir) = ($path,$dir); |
ee8c7f54 |
186 | $spath =~ s/\.dir\Z(?!\n)//; $sdir =~ s/\.dir\Z(?!\n)//; |
187 | $sdir = $self->eliminate_macros($sdir) unless $sdir =~ /^[\w\-]+\Z(?!\n)/s; |
cbc7acb0 |
188 | $rslt = $self->fixpath($self->eliminate_macros($spath)."/$sdir",1); |
46726cbe |
189 | |
fd7385b9 |
190 | # Special case for VMS absolute directory specs: these will have had device |
191 | # prepended during trip through Unix syntax in eliminate_macros(), since |
192 | # Unix syntax has no way to express "absolute from the top of this device's |
193 | # directory tree". |
194 | if ($spath =~ /^[\[<][^.\-]/s) { $rslt =~ s/^[^\[<]+//s; } |
270d1e39 |
195 | } |
cbc7acb0 |
196 | else { |
fd7385b9 |
197 | if (not defined $dir or not length $dir) { $rslt = ''; } |
ee8c7f54 |
198 | elsif ($dir =~ /^\$\([^\)]+\)\Z(?!\n)/s) { $rslt = $dir; } |
fd7385b9 |
199 | else { $rslt = vmspath($dir); } |
270d1e39 |
200 | } |
099f76bb |
201 | return $self->canonpath($rslt); |
270d1e39 |
202 | } |
203 | |
204 | =item catfile |
205 | |
206 | Concatenates a list of file specifications, and returns the result as a |
46726cbe |
207 | VMS-syntax file specification. |
270d1e39 |
208 | |
209 | =cut |
210 | |
211 | sub catfile { |
cbc7acb0 |
212 | my ($self,@files) = @_; |
213 | my $file = pop @files; |
270d1e39 |
214 | @files = grep($_,@files); |
cbc7acb0 |
215 | my $rslt; |
270d1e39 |
216 | if (@files) { |
cbc7acb0 |
217 | my $path = (@files == 1 ? $files[0] : $self->catdir(@files)); |
218 | my $spath = $path; |
ee8c7f54 |
219 | $spath =~ s/\.dir\Z(?!\n)//; |
220 | if ($spath =~ /^[^\)\]\/:>]+\)\Z(?!\n)/s && basename($file) eq $file) { |
cbc7acb0 |
221 | $rslt = "$spath$file"; |
222 | } |
223 | else { |
224 | $rslt = $self->eliminate_macros($spath); |
225 | $rslt = vmsify($rslt.($rslt ? '/' : '').unixify($file)); |
226 | } |
270d1e39 |
227 | } |
fd7385b9 |
228 | else { $rslt = (defined($file) && length($file)) ? vmsify($file) : ''; } |
099f76bb |
229 | return $self->canonpath($rslt); |
270d1e39 |
230 | } |
231 | |
46726cbe |
232 | |
270d1e39 |
233 | =item curdir (override) |
234 | |
cbc7acb0 |
235 | Returns a string representation of the current directory: '[]' |
270d1e39 |
236 | |
237 | =cut |
238 | |
239 | sub curdir { |
240 | return '[]'; |
241 | } |
242 | |
99804bbb |
243 | =item devnull (override) |
244 | |
cbc7acb0 |
245 | Returns a string representation of the null device: '_NLA0:' |
99804bbb |
246 | |
247 | =cut |
248 | |
249 | sub devnull { |
cbc7acb0 |
250 | return "_NLA0:"; |
99804bbb |
251 | } |
252 | |
270d1e39 |
253 | =item rootdir (override) |
254 | |
cbc7acb0 |
255 | Returns a string representation of the root directory: 'SYS$DISK:[000000]' |
270d1e39 |
256 | |
257 | =cut |
258 | |
259 | sub rootdir { |
cbc7acb0 |
260 | return 'SYS$DISK:[000000]'; |
261 | } |
262 | |
263 | =item tmpdir (override) |
264 | |
265 | Returns a string representation of the first writable directory |
266 | from the following list or '' if none are writable: |
267 | |
4b19af01 |
268 | sys$scratch: |
cbc7acb0 |
269 | $ENV{TMPDIR} |
270 | |
271 | =cut |
272 | |
273 | my $tmpdir; |
274 | sub tmpdir { |
275 | return $tmpdir if defined $tmpdir; |
4b19af01 |
276 | foreach ('sys$scratch:', $ENV{TMPDIR}) { |
cbc7acb0 |
277 | next unless defined && -d && -w _; |
278 | $tmpdir = $_; |
279 | last; |
280 | } |
281 | $tmpdir = '' unless defined $tmpdir; |
282 | return $tmpdir; |
270d1e39 |
283 | } |
284 | |
285 | =item updir (override) |
286 | |
cbc7acb0 |
287 | Returns a string representation of the parent directory: '[-]' |
270d1e39 |
288 | |
289 | =cut |
290 | |
291 | sub updir { |
292 | return '[-]'; |
293 | } |
294 | |
46726cbe |
295 | =item case_tolerant (override) |
296 | |
297 | VMS file specification syntax is case-tolerant. |
298 | |
299 | =cut |
300 | |
301 | sub case_tolerant { |
302 | return 1; |
303 | } |
304 | |
270d1e39 |
305 | =item path (override) |
306 | |
307 | Translate logical name DCL$PATH as a searchlist, rather than trying |
308 | to C<split> string value of C<$ENV{'PATH'}>. |
309 | |
310 | =cut |
311 | |
312 | sub path { |
cbc7acb0 |
313 | my (@dirs,$dir,$i); |
270d1e39 |
314 | while ($dir = $ENV{'DCL$PATH;' . $i++}) { push(@dirs,$dir); } |
cbc7acb0 |
315 | return @dirs; |
270d1e39 |
316 | } |
317 | |
318 | =item file_name_is_absolute (override) |
319 | |
320 | Checks for VMS directory spec as well as Unix separators. |
321 | |
322 | =cut |
323 | |
324 | sub file_name_is_absolute { |
cbc7acb0 |
325 | my ($self,$file) = @_; |
270d1e39 |
326 | # If it's a logical name, expand it. |
ee8c7f54 |
327 | $file = $ENV{$file} while $file =~ /^[\w\$\-]+\Z(?!\n)/s && $ENV{$file}; |
1b1e14d3 |
328 | return scalar($file =~ m!^/!s || |
cbc7acb0 |
329 | $file =~ m![<\[][^.\-\]>]! || |
330 | $file =~ /:[^<\[]/); |
270d1e39 |
331 | } |
332 | |
46726cbe |
333 | =item splitpath (override) |
334 | |
335 | Splits using VMS syntax. |
336 | |
337 | =cut |
338 | |
339 | sub splitpath { |
340 | my($self,$path) = @_; |
341 | my($dev,$dir,$file) = ('','',''); |
342 | |
1b1e14d3 |
343 | vmsify($path) =~ /(.+:)?([\[<].*[\]>])?(.*)/s; |
46726cbe |
344 | return ($1 || '',$2 || '',$3); |
345 | } |
346 | |
347 | =item splitdir (override) |
348 | |
349 | Split dirspec using VMS syntax. |
350 | |
351 | =cut |
352 | |
353 | sub splitdir { |
354 | my($self,$dirspec) = @_; |
355 | $dirspec =~ s/\]\[//g; $dirspec =~ s/\-\-/-.-/g; |
fd7385b9 |
356 | $dirspec = "[$dirspec]" unless $dirspec =~ /[\[<]/; # make legal |
46726cbe |
357 | my(@dirs) = split('\.', vmspath($dirspec)); |
ee8c7f54 |
358 | $dirs[0] =~ s/^[\[<]//s; $dirs[-1] =~ s/[\]>]\Z(?!\n)//s; |
46726cbe |
359 | @dirs; |
360 | } |
361 | |
362 | |
363 | =item catpath (override) |
364 | |
365 | Construct a complete filespec using VMS syntax |
366 | |
367 | =cut |
368 | |
369 | sub catpath { |
370 | my($self,$dev,$dir,$file) = @_; |
fd7385b9 |
371 | if ($dev =~ m|^/+([^/]+)|) { $dev = "$1:"; } |
ee8c7f54 |
372 | else { $dev .= ':' unless $dev eq '' or $dev =~ /:\Z(?!\n)/; } |
fd7385b9 |
373 | if (length($dev) or length($dir)) { |
374 | $dir = "[$dir]" unless $dir =~ /[\[<\/]/; |
375 | $dir = vmspath($dir); |
0994714a |
376 | } |
fd7385b9 |
377 | "$dev$dir$file"; |
0994714a |
378 | } |
379 | |
fd7385b9 |
380 | =item abs2rel (override) |
0994714a |
381 | |
fd7385b9 |
382 | Use VMS syntax when converting filespecs. |
0994714a |
383 | |
384 | =cut |
385 | |
0994714a |
386 | sub abs2rel { |
387 | my $self = shift; |
388 | |
fd7385b9 |
389 | return vmspath(File::Spec::Unix::abs2rel( $self, @_ )) |
0994714a |
390 | if ( join( '', @_ ) =~ m{/} ) ; |
391 | |
392 | my($path,$base) = @_; |
393 | |
394 | # Note: we use '/' to glue things together here, then let canonpath() |
395 | # clean them up at the end. |
396 | |
397 | # Clean up $path |
398 | if ( ! $self->file_name_is_absolute( $path ) ) { |
399 | $path = $self->rel2abs( $path ) ; |
400 | } |
401 | else { |
402 | $path = $self->canonpath( $path ) ; |
403 | } |
404 | |
405 | # Figure out the effective $base and clean it up. |
1d7cb664 |
406 | if ( !defined( $base ) || $base eq '' ) { |
0994714a |
407 | $base = cwd() ; |
408 | } |
1d7cb664 |
409 | elsif ( ! $self->file_name_is_absolute( $base ) ) { |
410 | $base = $self->rel2abs( $base ) ; |
411 | } |
0994714a |
412 | else { |
413 | $base = $self->canonpath( $base ) ; |
414 | } |
415 | |
416 | # Split up paths |
ee8c7f54 |
417 | my ( $path_directories, $path_file ) = |
418 | ($self->splitpath( $path, 1 ))[1,2] ; |
0994714a |
419 | |
420 | $path_directories = $1 |
ee8c7f54 |
421 | if $path_directories =~ /^\[(.*)\]\Z(?!\n)/s ; |
0994714a |
422 | |
ee8c7f54 |
423 | my $base_directories = ($self->splitpath( $base, 1 ))[1] ; |
0994714a |
424 | |
425 | $base_directories = $1 |
ee8c7f54 |
426 | if $base_directories =~ /^\[(.*)\]\Z(?!\n)/s ; |
0994714a |
427 | |
428 | # Now, remove all leading components that are the same |
429 | my @pathchunks = $self->splitdir( $path_directories ); |
430 | my @basechunks = $self->splitdir( $base_directories ); |
431 | |
432 | while ( @pathchunks && |
433 | @basechunks && |
434 | lc( $pathchunks[0] ) eq lc( $basechunks[0] ) |
435 | ) { |
436 | shift @pathchunks ; |
437 | shift @basechunks ; |
438 | } |
439 | |
440 | # @basechunks now contains the directories to climb out of, |
441 | # @pathchunks now has the directories to descend in to. |
442 | $path_directories = '-.' x @basechunks . join( '.', @pathchunks ) ; |
ee8c7f54 |
443 | $path_directories =~ s{\.\Z(?!\n)}{} ; |
fd7385b9 |
444 | return $self->canonpath( $self->catpath( '', $path_directories, $path_file ) ) ; |
0994714a |
445 | } |
446 | |
447 | |
fd7385b9 |
448 | =item rel2abs (override) |
449 | |
450 | Use VMS syntax when converting filespecs. |
451 | |
452 | =cut |
453 | |
4b19af01 |
454 | sub rel2abs { |
0994714a |
455 | my $self = shift ; |
fd7385b9 |
456 | return vmspath(File::Spec::Unix::rel2abs( $self, @_ )) |
0994714a |
457 | if ( join( '', @_ ) =~ m{/} ) ; |
458 | |
459 | my ($path,$base ) = @_; |
460 | # Clean up and split up $path |
461 | if ( ! $self->file_name_is_absolute( $path ) ) { |
462 | # Figure out the effective $base and clean it up. |
463 | if ( !defined( $base ) || $base eq '' ) { |
464 | $base = cwd() ; |
465 | } |
466 | elsif ( ! $self->file_name_is_absolute( $base ) ) { |
467 | $base = $self->rel2abs( $base ) ; |
468 | } |
469 | else { |
470 | $base = $self->canonpath( $base ) ; |
471 | } |
472 | |
473 | # Split up paths |
ee8c7f54 |
474 | my ( $path_directories, $path_file ) = |
475 | ($self->splitpath( $path ))[1,2] ; |
0994714a |
476 | |
ee8c7f54 |
477 | my ( $base_volume, $base_directories ) = |
0994714a |
478 | $self->splitpath( $base ) ; |
479 | |
fd7385b9 |
480 | $path_directories = '' if $path_directories eq '[]' || |
481 | $path_directories eq '<>'; |
0994714a |
482 | my $sep = '' ; |
483 | $sep = '.' |
ee8c7f54 |
484 | if ( $base_directories =~ m{[^.\]>]\Z(?!\n)} && |
fd7385b9 |
485 | $path_directories =~ m{^[^.\[<]}s |
0994714a |
486 | ) ; |
fd7385b9 |
487 | $base_directories = "$base_directories$sep$path_directories"; |
488 | $base_directories =~ s{\.?[\]>][\[<]\.?}{.}; |
0994714a |
489 | |
490 | $path = $self->catpath( $base_volume, $base_directories, $path_file ); |
491 | } |
492 | |
493 | return $self->canonpath( $path ) ; |
494 | } |
495 | |
496 | |
cbc7acb0 |
497 | =back |
270d1e39 |
498 | |
cbc7acb0 |
499 | =head1 SEE ALSO |
500 | |
501 | L<File::Spec> |
502 | |
503 | =cut |
504 | |
505 | 1; |